-
Notifications
You must be signed in to change notification settings - Fork 519
fix anthropic tool call bug #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
47a1948
daf3d57
971c8e0
e77eb40
dcd5907
8bc716d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,10 @@ export async function processStream( | |
| userId, | ||
| } = params | ||
| const fullResponseChunks: string[] = [fullResponse] | ||
| const messageHistoryBeforeStream = expireMessages( | ||
| agentState.messageHistory, | ||
| 'agentStep', | ||
| ) | ||
|
|
||
| // === MUTABLE STATE === | ||
| const toolResults: ToolMessage[] = [] | ||
|
|
@@ -111,9 +115,11 @@ export async function processStream( | |
| return (chunk: string | PrintModeEvent) => { | ||
| if (typeof chunk !== 'string') { | ||
| if (chunk.type === 'tool_call') { | ||
| assistantMessages.push( | ||
| assistantMessage({ ...chunk, type: 'tool-call' }), | ||
| ) | ||
| if (chunk.includeToolCall !== false) { | ||
| assistantMessages.push( | ||
| assistantMessage({ ...chunk, type: 'tool-call' }), | ||
| ) | ||
| } | ||
| } else if (isXmlMode && chunk.type === 'tool_result') { | ||
| const toolResultMessage: ToolMessage = { | ||
| role: 'tool', | ||
|
|
@@ -182,7 +188,7 @@ export async function processStream( | |
| : (toolName as ToolName), | ||
| input: transformed ? transformed.input : input, | ||
| fromHandleSteps: false, | ||
| skipDirectResultPush: isXmlMode, | ||
| skipDirectResultPush: true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems we should refactor to delete this param entirely if always true |
||
| fileProcessingState, | ||
| fullResponse: fullResponseChunks.join(''), | ||
| previousToolCallFinished: previousPromise, | ||
|
|
@@ -199,7 +205,7 @@ export async function processStream( | |
| ...params, | ||
| toolName, | ||
| input, | ||
| skipDirectResultPush: isXmlMode, | ||
| skipDirectResultPush: true, | ||
| fileProcessingState, | ||
| fullResponse: fullResponseChunks.join(''), | ||
| previousToolCallFinished: previousPromise, | ||
|
|
@@ -327,20 +333,20 @@ export async function processStream( | |
| } | ||
| } | ||
|
|
||
| // === FINALIZATION === | ||
| agentState.messageHistory = buildArray<Message>([ | ||
| ...expireMessages(agentState.messageHistory, 'agentStep'), | ||
| ...assistantMessages, | ||
| ...toolResultsToAddAfterStream, | ||
| ]) | ||
|
|
||
| if (!signal.aborted) { | ||
| resolveStreamDonePromise() | ||
| await previousToolCallFinished | ||
| } | ||
|
|
||
| // Error messages must come AFTER tool results for proper API ordering | ||
| agentState.messageHistory.push(...errorMessages) | ||
| // === FINALIZATION === | ||
| // Build message history from the pre-stream snapshot so tool_calls and | ||
| // tool_results are always appended in deterministic order. | ||
| agentState.messageHistory = buildArray<Message>([ | ||
| ...messageHistoryBeforeStream, | ||
| ...assistantMessages, | ||
| ...toolResultsToAddAfterStream, | ||
| ...errorMessages, | ||
| ]) | ||
|
Comment on lines
+314
to
+322
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conceptually, I think it does make more sense to do all the mutating of message history here, in one place. |
||
|
|
||
| return { | ||
| fullResponse: fullResponseChunks.join(''), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,7 +146,7 @@ export async function executeToolCall<T extends ToolName>( | |
| previousToolCallFinished, | ||
| toolCalls, | ||
| toolResults, | ||
| toolResultsToAddAfterStream: _toolResultsToAddAfterStream, | ||
| toolResultsToAddAfterStream, | ||
| userInputId, | ||
|
|
||
| onCostCalculated, | ||
|
|
@@ -350,6 +350,10 @@ export async function executeToolCall<T extends ToolName>( | |
|
|
||
| toolResults.push(toolResult) | ||
|
|
||
| if (!excludeToolFromMessageHistory) { | ||
| toolResultsToAddAfterStream.push(toolResult) | ||
| } | ||
|
|
||
| if (!excludeToolFromMessageHistory && !params.skipDirectResultPush) { | ||
| agentState.messageHistory.push(toolResult) | ||
| } | ||
|
|
@@ -450,7 +454,7 @@ export async function executeCustomToolCall( | |
| toolCallId, | ||
| toolCalls, | ||
| toolResults, | ||
| toolResultsToAddAfterStream: _toolResultsToAddAfterStream, | ||
| toolResultsToAddAfterStream, | ||
| userInputId, | ||
| } = params | ||
| const toolCall: CustomToolCall | ToolCallError = parseRawCustomToolCall({ | ||
|
|
@@ -560,6 +564,10 @@ export async function executeCustomToolCall( | |
|
|
||
| toolResults.push(toolResult) | ||
|
|
||
| if (!excludeToolFromMessageHistory) { | ||
| toolResultsToAddAfterStream.push(toolResult) | ||
| } | ||
|
|
||
| if (!excludeToolFromMessageHistory && !params.skipDirectResultPush) { | ||
| agentState.messageHistory.push(toolResult) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the new paradigm, we can delete this if and We might want to rename toolResultsToAddAfterStream to toolResultsToAddToMessageHistory, since all tool results are added after stream and these are the ones included in the message history in particular |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary, this is run right before each step already (in runAgentStep)