From 90041f33136d83962dde763c37dc7af6f3f7efbc Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 20:30:35 -0700 Subject: [PATCH] Update headless mode to output only final response and return full conversation --- src/index.ts | 16 ++++++++++++---- src/streamChatResponse.ts | 20 ++++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index ad150b7b5..7edd796a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -156,14 +156,22 @@ async function chat() { } try { - await streamChatResponse(chatHistory, assistant, client); + const finalResponse = await streamChatResponse(chatHistory, assistant, client); + + // In headless mode, only print the final response + if (args.isHeadless && finalResponse.trim()) { + console.log(finalResponse); + } + // Save session after each successful response saveSession(chatHistory); } catch (e: any) { console.error(`\n${chalk.red(`Error: ${e.message}`)}`); - console.info( - chalk.dim(`Chat history:\n${JSON.stringify(chatHistory, null, 2)}`) - ); + if (!args.isHeadless) { + console.info( + chalk.dim(`Chat history:\n${JSON.stringify(chatHistory, null, 2)}`) + ); + } } } } diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 9526e0701..9fb6f67d2 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -6,6 +6,7 @@ import type { ChatCompletionMessageToolCall, ChatCompletionTool, } from "openai/resources.mjs"; +import { parseArgs } from "./args.js"; import { MCPService } from "./mcp.js"; import { executeToolCall } from "./tools.js"; import { BUILTIN_TOOLS } from "./tools/index.js"; @@ -66,10 +67,12 @@ export async function streamChatResponse( callbacks?: StreamCallbacks, abortController?: AbortController ) { + const args = parseArgs(); + const isHeadless = args.isHeadless; // Prepare tools for the API call const toolsForRequest = getAllTools(); - let aiResponse = ""; + let fullResponse = ""; let currentToolCalls: TODO[] = []; let shouldContinueConversation = true; @@ -97,7 +100,7 @@ export async function streamChatResponse( process.exit(1); } - aiResponse = ""; + let aiResponse = ""; currentToolCalls = []; let currentToolCallId = ""; let toolArguments = ""; @@ -113,10 +116,11 @@ export async function streamChatResponse( const content = chunk.choices[0].delta.content; if (callbacks?.onContent) { callbacks.onContent(content); - } else { + } else if (!isHeadless) { process.stdout.write(chalk.white(content)); } aiResponse += content; + fullResponse += content; } // Handle tool calls @@ -167,7 +171,7 @@ export async function streamChatResponse( toolCall.startNotified = true; if (callbacks?.onToolStart) { callbacks.onToolStart(toolCall.name, toolCall.arguments); - } else { + } else if (!isHeadless) { process.stdout.write( `\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold( toolCall.name @@ -184,7 +188,7 @@ export async function streamChatResponse( } } - if (!callbacks?.onContent) { + if (!callbacks?.onContent && !isHeadless) { console.info(); // Add a newline after the response } @@ -238,7 +242,7 @@ export async function streamChatResponse( if (callbacks?.onToolResult) { callbacks.onToolResult(toolResult, toolCall.name); - } else { + } else if (!isHeadless) { console.info(chalk.green(toolResult) + "\n"); } } catch (error) { @@ -252,7 +256,7 @@ export async function streamChatResponse( }); if (callbacks?.onToolError) { callbacks.onToolError(errorMessage, toolCall.name); - } else { + } else if (!isHeadless) { console.info( `${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red( ")" @@ -270,5 +274,5 @@ export async function streamChatResponse( } } - return aiResponse; + return fullResponse; }