From 60e288454c92a2483066dbca2bbe0d26b5affa07 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Tue, 12 Aug 2025 04:14:42 +0000 Subject: [PATCH] Refactor CLI input handling in cli.js to streamline the input structure and enhance the OpenAI call process. Introduce a new variable for previous response tracking and update file listing in list_files.js to return sorted file entries directly, improving output consistency. --- cli.js | 32 ++++++++++++++++---------------- tools/list_files.js | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cli.js b/cli.js index 4234e47..937b5f5 100644 --- a/cli.js +++ b/cli.js @@ -6,7 +6,7 @@ import terminalKit from 'terminal-kit'; //npm install tiktoken //csk-8jftdte6r6vf8fdvp9xkyek5t3jnc6jfhh93d3ewfcwxxvh9 -import { promises as fs } from "node:fs"; +import { promises as fs, unwatchFile } from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; @@ -65,20 +65,20 @@ async function loadTools() { ); return Object.fromEntries(toolEntries); } - +let counter = 0; +let previousResponseId; while(true){ - let counter = 0; + // Block for user input before kicking off the LLM loop const userText = await askUserForInput(); await streamOnce(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }), userText ); async function streamOnce(openai, userText) { const toolsByFile = await loadTools(); - let previousResponseId; - let input = [ - {"role": "developer", "content": [ { - "type": "input_text","text": `You are an interactive CLI AI assistant. Follow the user's instructions. + + const systemprompt = {"role": "developer", "content": [ { + "type": "input_text","text": `You are an interactive CLI AI assistant. Follow the user's instructions. If a tool is available and relevant, plan to use it. Tools: @@ -87,15 +87,15 @@ patch_files - (zum anlegen, ändern und löschen von Dateien) read_file - (nach zeilen) ripgrep - suchmusater und dateimuster websearch - eine Google Suche machen mit Schlüsselwörtern` - }] }, - {"role": "user", "content": [ {"type": "input_text","text": userText } ]}, - ] - - while(input.length > 0){ + }]}; + + const input = [{"role": "user", "content": [ {"type": "input_text","text": userText } ]}]; + + do{ const call = { model: 'gpt-5-mini', - input: input, + input: counter == 0 ? [systemprompt,...structuredClone(input)] : structuredClone(input), text: { format: { type: 'text' }, verbosity: 'low' }, reasoning: { effort: 'minimal', summary: 'detailed' }, tools: Object.values(toolsByFile).map(t => t.def), @@ -106,7 +106,7 @@ websearch - eine Google Suche machen mit Schlüsselwörtern` console.log("\n\n\n\n\n------NEW OPENAI CALL-"+input.length+"-------------" ,"\n",counter++,"\n",'----INPUT-----------------' ,"\n",call.input.map(i => JSON.stringify(i)),"\n", - '--------CALL-------------',"\n"); + '--------CALL-------------',call,"\n"); const stream = await openai.responses.stream(call); stream.on('response.created', (event) => { previousResponseId = event.response.id; @@ -156,7 +156,7 @@ websearch - eine Google Suche machen mit Schlüsselwörtern` }); await Array.fromAsync(stream); - input=[]; + input.length = 0; for (const call of functionCalls) { //try { @@ -171,7 +171,7 @@ websearch - eine Google Suche machen mit Schlüsselwörtern` // console.error('Error in function call:', call.name, err); //} } - } + }while(input.length > 0) //console.log('OPENAI STREAM FINISHED'); } diff --git a/tools/list_files.js b/tools/list_files.js index cdb1d4a..1b91466 100644 --- a/tools/list_files.js +++ b/tools/list_files.js @@ -173,7 +173,7 @@ export async function run(args) { const files = await listEntriesRecursive(resolvedBase, chrootResolved, depth === -1 ? Infinity : depth, includeHidden); return { cwd, - files: JSON.stringify(files.sort((a, b) => a[0].localeCompare(b[0]))), // Sort for consistent output + files: files.sort((a, b) => a[0].localeCompare(b[0])), // Sort for consistent output }; } catch (err) { return { err: `Failed to list files: ${err?.message || String(err)}` };