Skip to content

Commit 9ff51ff

Browse files
committed
add logging for chunks
1 parent 2404339 commit 9ff51ff

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

backend/src/run-agent-step.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ export const runAgentStep = async (
402402
toolResults: newToolResults,
403403
state,
404404
fullResponse: fullResponseAfterStream,
405+
fullResponseChunks,
405406
} = await processStreamWithTools({
406407
stream,
407408
ws,
@@ -470,6 +471,7 @@ export const runAgentStep = async (
470471
agentId: agentState.agentId,
471472
prompt,
472473
fullResponse,
474+
fullResponseChunks,
473475
toolCalls,
474476
toolResults,
475477
agentContext: newAgentContext,

backend/src/tools/stream-parser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function processStreamWithTools<T extends string>(options: {
5555
agentState,
5656
onResponseChunk,
5757
} = options
58-
let fullResponse = options.fullResponse
58+
const fullResponseChunks: string[] = [options.fullResponse]
5959

6060
const messages = [...options.messages]
6161

@@ -105,7 +105,7 @@ export async function processStreamWithTools<T extends string>(options: {
105105
agentStepId,
106106
clientSessionId,
107107
userInputId,
108-
fullResponse,
108+
fullResponse: fullResponseChunks.join(''),
109109
onResponseChunk,
110110
state,
111111
userId,
@@ -135,19 +135,25 @@ export async function processStreamWithTools<T extends string>(options: {
135135

136136
for await (const chunk of streamWithTags) {
137137
onResponseChunk(chunk)
138-
fullResponse += chunk
138+
fullResponseChunks.push(chunk)
139139
}
140140

141141
state.messages = buildArray<CodebuffMessage>([
142142
...expireMessages(state.messages, 'agentStep'),
143-
fullResponse && {
143+
fullResponseChunks.length > 0 && {
144144
role: 'assistant' as const,
145-
content: fullResponse,
145+
content: fullResponseChunks.join(''),
146146
},
147147
])
148148

149149
resolveStreamDonePromise()
150150
await previousToolCallFinished
151151

152-
return { toolCalls, toolResults, state, fullResponse }
152+
return {
153+
toolCalls,
154+
toolResults,
155+
state,
156+
fullResponse: fullResponseChunks.join(''),
157+
fullResponseChunks,
158+
}
153159
}

backend/src/xml-stream-parser.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,12 @@ export async function* processStreamWithTags(
5151

5252
function extractToolCalls(): string[] {
5353
const matches: string[] = []
54-
let lastIndex = -1
54+
let lastIndex = 0
5555
for (const match of buffer.matchAll(toolExtractionPattern)) {
5656
lastIndex = match.index + match[0].length
5757
matches.push(match[1])
5858
}
5959

60-
if (lastIndex === -1) {
61-
lastIndex = 0
62-
}
6360
buffer = removeProcessedToolCalls(buffer.slice(lastIndex))
6461
return matches
6562
}

0 commit comments

Comments
 (0)