Skip to content

Commit daf3d57

Browse files
committed
pr comments
1 parent 47a1948 commit daf3d57

File tree

5 files changed

+21
-75
lines changed

5 files changed

+21
-75
lines changed

packages/agent-runtime/src/run-programmatic-step.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function runProgrammaticStep(
8282
| 'toolCallId'
8383
| 'toolCalls'
8484
| 'toolResults'
85-
| 'toolResultsToAddAfterStream'
85+
| 'toolResultsToAddToMessageHistory'
8686
> &
8787
ParamsExcluding<
8888
AddAgentStepFn,
@@ -428,7 +428,7 @@ type ExecuteToolCallsArrayParams = Omit<
428428
| 'autoInsertEndStepParam'
429429
| 'excludeToolFromMessageHistory'
430430
| 'toolCallId'
431-
| 'toolResultsToAddAfterStream'
431+
| 'toolResultsToAddToMessageHistory'
432432
> & {
433433
agentState: AgentState
434434
onResponseChunk: (chunk: string | PrintModeEvent) => void
@@ -494,7 +494,7 @@ async function executeSingleToolCall(
494494
excludeToolFromMessageHistory,
495495
fromHandleSteps: true,
496496
toolCallId,
497-
toolResultsToAddAfterStream: [],
497+
toolResultsToAddToMessageHistory: [],
498498

499499
onResponseChunk: (chunk: string | PrintModeEvent) => {
500500
if (typeof chunk === 'string') {

packages/agent-runtime/src/tools/stream-parser.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
executeToolCall,
1515
tryTransformAgentToolCall,
1616
} from './tool-executor'
17-
import { expireMessages, withSystemTags } from '../util/messages'
17+
import { withSystemTags } from '../util/messages'
1818

1919
import type { CustomToolCall, ExecuteToolCallParams } from './tool-executor'
2020
import type { AgentTemplate } from '../templates/types'
@@ -60,7 +60,7 @@ export async function processStream(
6060
| 'toolCalls'
6161
| 'toolName'
6262
| 'toolResults'
63-
| 'toolResultsToAddAfterStream'
63+
| 'toolResultsToAddToMessageHistory'
6464
> &
6565
ParamsExcluding<
6666
typeof processStreamWithTools,
@@ -84,14 +84,11 @@ export async function processStream(
8484
userId,
8585
} = params
8686
const fullResponseChunks: string[] = [fullResponse]
87-
const messageHistoryBeforeStream = expireMessages(
88-
agentState.messageHistory,
89-
'agentStep',
90-
)
87+
const messageHistoryBeforeStream = [...agentState.messageHistory]
9188

9289
// === MUTABLE STATE ===
9390
const toolResults: ToolMessage[] = []
94-
const toolResultsToAddAfterStream: ToolMessage[] = []
91+
const toolResultsToAddToMessageHistory: ToolMessage[] = []
9592
const toolCalls: (CodebuffToolCall | CustomToolCall)[] = []
9693
const assistantMessages: Message[] = []
9794
let hadToolCallError = false
@@ -149,7 +146,7 @@ export async function processStream(
149146
// isXmlMode=false: defer execution, results added at end (for native tool calls)
150147
function createToolExecutionCallback(toolName: string, isXmlMode: boolean) {
151148
const responseHandler = createResponseHandler(isXmlMode)
152-
const resultsArray = isXmlMode ? [] : toolResultsToAddAfterStream
149+
const resultsArray = isXmlMode ? [] : toolResultsToAddToMessageHistory
153150

154151
return {
155152
onTagStart: () => {},
@@ -188,14 +185,14 @@ export async function processStream(
188185
: (toolName as ToolName),
189186
input: transformed ? transformed.input : input,
190187
fromHandleSteps: false,
191-
skipDirectResultPush: true,
188+
192189
fileProcessingState,
193190
fullResponse: fullResponseChunks.join(''),
194191
previousToolCallFinished: previousPromise,
195192
toolCallId,
196193
toolCalls,
197194
toolResults,
198-
toolResultsToAddAfterStream: resultsArray,
195+
toolResultsToAddToMessageHistory: resultsArray,
199196
onCostCalculated,
200197
onResponseChunk: responseHandler,
201198
})
@@ -205,14 +202,14 @@ export async function processStream(
205202
...params,
206203
toolName,
207204
input,
208-
skipDirectResultPush: true,
205+
209206
fileProcessingState,
210207
fullResponse: fullResponseChunks.join(''),
211208
previousToolCallFinished: previousPromise,
212209
toolCallId,
213210
toolCalls,
214211
toolResults,
215-
toolResultsToAddAfterStream: resultsArray,
212+
toolResultsToAddToMessageHistory: resultsArray,
216213
onResponseChunk: responseHandler,
217214
})
218215
}
@@ -250,7 +247,7 @@ export async function processStream(
250247
content: jsonToolResult({ errorMessage: error }),
251248
}
252249
toolResults.push(cloneDeep(toolResult))
253-
toolResultsToAddAfterStream.push(cloneDeep(toolResult))
250+
toolResultsToAddToMessageHistory.push(cloneDeep(toolResult))
254251
},
255252
loggerOptions: {
256253
userId,
@@ -344,7 +341,7 @@ export async function processStream(
344341
agentState.messageHistory = buildArray<Message>([
345342
...messageHistoryBeforeStream,
346343
...assistantMessages,
347-
...toolResultsToAddAfterStream,
344+
...toolResultsToAddToMessageHistory,
348345
...errorMessages,
349346
])
350347

packages/agent-runtime/src/tools/tool-executor.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ export type ExecuteToolCallParams<T extends string = ToolName> = {
120120
toolCallId: string | undefined
121121
toolCalls: (CodebuffToolCall | CustomToolCall)[]
122122
toolResults: ToolMessage[]
123-
toolResultsToAddAfterStream: ToolMessage[]
124-
skipDirectResultPush?: boolean
123+
toolResultsToAddToMessageHistory: ToolMessage[]
125124
userId: string | undefined
126125
userInputId: string
127126

@@ -146,7 +145,7 @@ export async function executeToolCall<T extends ToolName>(
146145
previousToolCallFinished,
147146
toolCalls,
148147
toolResults,
149-
toolResultsToAddAfterStream,
148+
toolResultsToAddToMessageHistory,
150149
userInputId,
151150

152151
onCostCalculated,
@@ -351,11 +350,7 @@ export async function executeToolCall<T extends ToolName>(
351350
toolResults.push(toolResult)
352351

353352
if (!excludeToolFromMessageHistory) {
354-
toolResultsToAddAfterStream.push(toolResult)
355-
}
356-
357-
if (!excludeToolFromMessageHistory && !params.skipDirectResultPush) {
358-
agentState.messageHistory.push(toolResult)
353+
toolResultsToAddToMessageHistory.push(toolResult)
359354
}
360355

361356
// After tool completes, resolve any pending creditsUsed promise
@@ -454,7 +449,7 @@ export async function executeCustomToolCall(
454449
toolCallId,
455450
toolCalls,
456451
toolResults,
457-
toolResultsToAddAfterStream,
452+
toolResultsToAddToMessageHistory,
458453
userInputId,
459454
} = params
460455
const toolCall: CustomToolCall | ToolCallError = parseRawCustomToolCall({
@@ -565,13 +560,10 @@ export async function executeCustomToolCall(
565560
toolResults.push(toolResult)
566561

567562
if (!excludeToolFromMessageHistory) {
568-
toolResultsToAddAfterStream.push(toolResult)
563+
toolResultsToAddToMessageHistory.push(toolResult)
569564
}
570565

571-
if (!excludeToolFromMessageHistory && !params.skipDirectResultPush) {
572-
agentState.messageHistory.push(toolResult)
573-
}
574-
return
566+
return
575567
})
576568
}
577569

packages/internal/src/openai-compatible/chat/convert-to-openai-compatible-chat-messages.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,5 @@ export function convertToOpenAICompatibleChatMessages(
138138
}
139139
}
140140

141-
// Debug: dump OpenAI-format message summary to catch tool_use_id mismatches
142-
console.error('[SDK DEBUG] OpenAI-format messages (' + messages.length + '):')
143-
for (let i = 0; i < messages.length; i++) {
144-
const m = messages[i] as Record<string, unknown>
145-
const role = m.role as string
146-
if (role === 'tool') {
147-
console.error(` [${i}] tool tool_call_id=${(m as { tool_call_id?: string }).tool_call_id}`)
148-
} else if (role === 'assistant') {
149-
const toolCalls = (m as { tool_calls?: Array<{ id: string; function?: { name: string } }> }).tool_calls
150-
if (toolCalls?.length) {
151-
const ids = toolCalls.map(tc => `${tc.function?.name}:${tc.id}`)
152-
console.error(` [${i}] assistant tool_calls=[${ids.join(', ')}]`)
153-
} else {
154-
console.error(` [${i}] assistant (text)`)
155-
}
156-
} else {
157-
console.error(` [${i}] ${role}`)
158-
}
159-
}
160-
161141
return messages
162142
}

sdk/src/impl/llm.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -225,34 +225,11 @@ export async function* promptAiSdkStream(
225225
}
226226
}
227227

228-
const convertedMessages = convertCbToModelMessages(params)
229-
230-
// Debug: dump message summary to catch tool_use_id mismatches before they hit the API
231-
console.error('[SDK DEBUG] promptAiSdkStream messages (' + convertedMessages.length + '):')
232-
for (let i = 0; i < convertedMessages.length; i++) {
233-
const m = convertedMessages[i] as Record<string, unknown>
234-
const role = m.role as string
235-
const content = m.content
236-
if (role === 'tool' && Array.isArray(content)) {
237-
const toolIds = (content as Array<{ toolCallId?: string; type?: string }>)
238-
.filter(c => c.type === 'tool-result')
239-
.map(c => c.toolCallId)
240-
console.error(` [${i}] ${role} toolCallIds=${JSON.stringify(toolIds)}`)
241-
} else if (role === 'assistant' && Array.isArray(content)) {
242-
const parts = (content as Array<{ type?: string; toolCallId?: string; toolName?: string }>)
243-
.map(c => c.type === 'tool-call' ? `tool-call(${c.toolName}:${c.toolCallId})` : c.type)
244-
console.error(` [${i}] ${role} parts=[${parts.join(', ')}]`)
245-
} else {
246-
const tags = (m as { tags?: string[] }).tags
247-
console.error(` [${i}] ${role}${tags ? ' tags=' + JSON.stringify(tags) : ''}`)
248-
}
249-
}
250-
251228
const response = streamText({
252229
...params,
253230
prompt: undefined,
254231
model: aiSDKModel,
255-
messages: convertedMessages,
232+
messages: convertCbToModelMessages(params),
256233
// When using Claude OAuth, disable retries so we can immediately fall back to Codebuff
257234
// backend on rate limit errors instead of retrying 4 times first
258235
...(isClaudeOAuth && { maxRetries: 0 }),

0 commit comments

Comments
 (0)