From 31b649e9ac1dd6ff461dc89d123ebcbd1ea66abb Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 30 Jul 2025 22:10:29 -0700 Subject: [PATCH] test: fix test attmpt --- src/streamChatResponse.ts | 29 ++++++++++++++++++++--------- src/streamChatResponse.vitest.ts | 7 ++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 77e9f557f..65db28518 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -474,13 +474,16 @@ export async function preprocessStreamedToolCalls( export async function executeStreamedToolCalls( preprocessedCalls: PreprocessedToolCall[], callbacks?: StreamCallbacks -): Promise { +): Promise<{ + hasRejection: boolean; + chatHistoryEntries: ChatCompletionToolMessageParam[]; +}> { const chatHistoryEntries: ChatCompletionToolMessageParam[] = []; - let remainingCanceled = false; + let hasRejection = false; // Execute each preprocessed tool call for (const toolCall of preprocessedCalls) { - if (remainingCanceled) { + if (hasRejection) { const cancelledMessage = `Cancelled due to previous tool rejection`; chatHistoryEntries.push({ role: "tool", @@ -574,7 +577,7 @@ export async function executeStreamedToolCalls( callbacks?.onToolResult?.(deniedMessage, toolCall.name); logger.debug("Tool call rejected - stopping stream"); - remainingCanceled = true; + hasRejection = true; continue; } @@ -612,7 +615,10 @@ export async function executeStreamedToolCalls( } } - return chatHistoryEntries; + return { + hasRejection, + chatHistoryEntries, + }; } // Main function that handles the conversation loop @@ -704,10 +710,15 @@ export async function streamChatResponse( chatHistory.push(...errorChatEntries); // Execute the valid preprocessed tool calls - const toolResults = await executeStreamedToolCalls( - preprocessedCalls, - callbacks - ); + const { chatHistoryEntries: toolResults, hasRejection } = + await executeStreamedToolCalls(preprocessedCalls, callbacks); + + if (isHeadless && hasRejection) { + logger.debug( + "Tool call rejected in headless mode - returning current content" + ); + return finalResponse || content || fullResponse; + } chatHistory.push(...toolResults); } else if (content) { diff --git a/src/streamChatResponse.vitest.ts b/src/streamChatResponse.vitest.ts index e0cefb346..7fbc9bf9f 100644 --- a/src/streamChatResponse.vitest.ts +++ b/src/streamChatResponse.vitest.ts @@ -492,7 +492,7 @@ describe("executeStreamedToolCalls", () => { onToolError: vi.fn(), }; - const chatHistoryEntries = await executeStreamedToolCalls( + const { chatHistoryEntries } = await executeStreamedToolCalls( preprocessedCalls, callbacks ); @@ -568,12 +568,13 @@ describe("executeStreamedToolCalls", () => { onToolPermissionRequest: vi.fn(), }; - const chatHistoryEntries = await executeStreamedToolCalls( + const { chatHistoryEntries, hasRejection } = await executeStreamedToolCalls( preprocessedCalls, callbacks ); // Verify results + expect(hasRejection).toEqual(true); expect(chatHistoryEntries).toHaveLength(2); expect(chatHistoryEntries[0].content).toBe("Permission denied by user"); expect(chatHistoryEntries[1].content).toBe( @@ -624,7 +625,7 @@ describe("executeStreamedToolCalls", () => { onToolError: vi.fn(), }; - const chatHistoryEntries = await executeStreamedToolCalls( + const { chatHistoryEntries, hasRejection } = await executeStreamedToolCalls( preprocessedCalls, callbacks );