Skip to content

Commit 5517774

Browse files
committed
move reasoning response for subagents
1 parent c9794da commit 5517774

22 files changed

+122
-38
lines changed

cli/src/hooks/use-send-message.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import { getLoadedAgentsData } from '../utils/local-agent-registry'
1515
import { logger } from '../utils/logger'
1616

1717
import type { ElapsedTimeTracker } from './use-elapsed-time'
18+
import type { StreamStatus } from './use-message-queue'
1819
import type { ChatMessage, ContentBlock, ToolContentBlock } from '../types/chat'
1920
import type { SendMessageFn } from '../types/contracts/send-message'
2021
import type { ParamsOf } from '../types/function-params'
2122
import type { SetElement } from '../types/utils'
2223
import type { AgentMode } from '../utils/constants'
23-
import type { StreamStatus } from './use-message-queue'
2424
import type { AgentDefinition, ToolName } from '@codebuff/sdk'
2525
import type { SetStateAction } from 'react'
2626
const hiddenToolNames = new Set<ToolName | 'spawn_agent_inline'>([
@@ -853,7 +853,11 @@ export const useSendMessage = ({
853853
maxAgentSteps: 40,
854854

855855
handleStreamChunk: (event) => {
856-
if (typeof event === 'string' || event.type === 'reasoning_chunk') {
856+
if (
857+
typeof event === 'string' ||
858+
(event.type === 'reasoning_chunk' &&
859+
event.ancestorRunIds.length === 0)
860+
) {
857861
const eventObj:
858862
| { type: 'text'; text: string }
859863
| { type: 'reasoning'; text: string } =
@@ -876,7 +880,10 @@ export const useSendMessage = ({
876880

877881
rootStreamSeenRef.current = true
878882
appendRootChunk(eventObj)
879-
} else if (event.type === 'subagent_chunk') {
883+
} else if (
884+
event.type === 'subagent_chunk' ||
885+
event.type === 'reasoning_chunk'
886+
) {
880887
const { agentId, chunk } = event
881888

882889
const previous =
@@ -886,13 +893,15 @@ export const useSendMessage = ({
886893
}
887894
agentStreamAccumulatorsRef.current.set(agentId, previous + chunk)
888895

896+
// TODO: Add reasoning chunks to a separate component
889897
updateAgentContent(agentId, {
890898
type: 'text',
891899
content: chunk,
892900
})
893901
return
894902
} else {
895903
event satisfies never
904+
throw new Error('Unhandled event type')
896905
}
897906
},
898907

common/src/types/print-mode.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ export const printModeToolResultSchema = z.object({
4444
})
4545
export type PrintModeToolResult = z.infer<typeof printModeToolResultSchema>
4646

47-
export const printModeReasoningSchema = z.object({
48-
type: z.literal('reasoning'),
49-
text: z.string(),
50-
})
51-
export type PrintModeReasoning = z.infer<typeof printModeReasoningSchema>
52-
5347
export const printModeTextSchema = z.object({
5448
type: z.literal('text'),
5549
text: z.string(),
@@ -102,7 +96,6 @@ export const printModeEventSchema = z.discriminatedUnion('type', [
10296
printModeDownloadStatusSchema,
10397
printModeErrorSchema,
10498
printModeFinishSchema,
105-
printModeReasoningSchema,
10699
printModeStartSchema,
107100
printModeSubagentFinishSchema,
108101
printModeSubagentStartSchema,

evals/scaffolding.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export async function runAgentStepScaffolding(
228228
localAgentTemplates,
229229
agentState,
230230
prompt,
231+
ancestorRunIds: [],
231232
spawnParams: undefined,
232233
repoUrl: undefined,
233234
repoId: undefined,

npm-app/src/client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,9 +1155,8 @@ export class Client {
11551155
printModeLog(chunk)
11561156
printSubagentHeader(chunk)
11571157
if (
1158-
(chunk.type === 'reasoning' && chunk.text) ||
1159-
(chunk.type === 'reasoning_delta' &&
1160-
chunk.ancestorRunIds.length === 0)
1158+
chunk.type === 'reasoning_delta' &&
1159+
chunk.ancestorRunIds.length === 0
11611160
) {
11621161
if (!this.streamStarted) {
11631162
this.streamStarted = true

packages/agent-runtime/src/__tests__/cost-aggregation.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ describe('Cost Aggregation System', () => {
186186
fileContext: mockFileContext,
187187
clientSessionId: 'test-session',
188188
userInputId: 'test-input',
189+
ancestorRunIds: [],
189190
writeToClient: () => {},
190191
getLatestState: () => ({ messages: [] }),
191192
state: mockValidatedState,
@@ -267,6 +268,7 @@ describe('Cost Aggregation System', () => {
267268
fileContext: mockFileContext,
268269
clientSessionId: 'test-session',
269270
userInputId: 'test-input',
271+
ancestorRunIds: [],
270272
writeToClient: () => {},
271273
getLatestState: () => ({ messages: [] }),
272274
state: mockValidatedState,
@@ -425,6 +427,7 @@ describe('Cost Aggregation System', () => {
425427
fileContext: mockFileContext,
426428
clientSessionId: 'test-session',
427429
userInputId: 'test-input',
430+
ancestorRunIds: [],
428431
writeToClient: () => {},
429432
getLatestState: () => ({ messages: [] }),
430433
state: mockValidatedState,

packages/agent-runtime/src/__tests__/loop-agent-steps.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
172172
localAgentTemplates,
173173
userId: TEST_USER_ID,
174174
clientSessionId: 'test-session',
175+
ancestorRunIds: [],
175176
onResponseChunk: () => {},
176177
})
177178

@@ -219,6 +220,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
219220
localAgentTemplates,
220221
userId: TEST_USER_ID,
221222
clientSessionId: 'test-session',
223+
ancestorRunIds: [],
222224
onResponseChunk: () => {},
223225
})
224226

@@ -268,6 +270,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
268270
localAgentTemplates,
269271
userId: TEST_USER_ID,
270272
clientSessionId: 'test-session',
273+
ancestorRunIds: [],
271274
onResponseChunk: () => {},
272275
})
273276

@@ -316,6 +319,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
316319
localAgentTemplates,
317320
userId: TEST_USER_ID,
318321
clientSessionId: 'test-session',
322+
ancestorRunIds: [],
319323
onResponseChunk: () => {},
320324
})
321325

@@ -357,6 +361,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
357361
localAgentTemplates,
358362
userId: TEST_USER_ID,
359363
clientSessionId: 'test-session',
364+
ancestorRunIds: [],
360365
onResponseChunk: () => {},
361366
})
362367

@@ -390,6 +395,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
390395
localAgentTemplates,
391396
userId: TEST_USER_ID,
392397
clientSessionId: 'test-session',
398+
ancestorRunIds: [],
393399
onResponseChunk: () => {},
394400
})
395401

@@ -425,6 +431,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
425431
localAgentTemplates,
426432
userId: TEST_USER_ID,
427433
clientSessionId: 'test-session',
434+
ancestorRunIds: [],
428435
onResponseChunk: () => {},
429436
})
430437

@@ -477,6 +484,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
477484
localAgentTemplates,
478485
userId: TEST_USER_ID,
479486
clientSessionId: 'test-session',
487+
ancestorRunIds: [],
480488
onResponseChunk: () => {},
481489
})
482490

@@ -534,6 +542,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
534542
localAgentTemplates,
535543
userId: TEST_USER_ID,
536544
clientSessionId: 'test-session',
545+
ancestorRunIds: [],
537546
onResponseChunk: () => {},
538547
})
539548

@@ -605,6 +614,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
605614
localAgentTemplates,
606615
userId: TEST_USER_ID,
607616
clientSessionId: 'test-session',
617+
ancestorRunIds: [],
608618
onResponseChunk: () => {},
609619
})
610620

@@ -689,6 +699,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
689699
localAgentTemplates,
690700
userId: TEST_USER_ID,
691701
clientSessionId: 'test-session',
702+
ancestorRunIds: [],
692703
onResponseChunk: () => {},
693704
})
694705

@@ -763,6 +774,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
763774
localAgentTemplates,
764775
userId: TEST_USER_ID,
765776
clientSessionId: 'test-session',
777+
ancestorRunIds: [],
766778
onResponseChunk: () => {},
767779
})
768780

@@ -810,6 +822,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
810822
localAgentTemplates,
811823
userId: TEST_USER_ID,
812824
clientSessionId: 'test-session',
825+
ancestorRunIds: [],
813826
onResponseChunk: () => {},
814827
})
815828

@@ -879,6 +892,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
879892
localAgentTemplates,
880893
userId: TEST_USER_ID,
881894
clientSessionId: 'test-session',
895+
ancestorRunIds: [],
882896
onResponseChunk: () => {},
883897
})
884898

packages/agent-runtime/src/__tests__/malformed-tool-call.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('malformed tool call error handling', () => {
6363
...agentRuntimeImpl,
6464
stream: createMockStream([]),
6565
runId: 'test-run-id',
66+
ancestorRunIds: [],
6667
agentStepId: 'test-step',
6768
clientSessionId: 'test-session',
6869
fingerprintId: 'test-fingerprint',

packages/agent-runtime/src/__tests__/prompt-caching-subagents.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
145145
localAgentTemplates: mockLocalAgentTemplates,
146146
userId: TEST_USER_ID,
147147
clientSessionId: 'test-session',
148+
ancestorRunIds: [],
148149
onResponseChunk: () => {},
149150
})
150151

@@ -180,6 +181,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
180181
localAgentTemplates: mockLocalAgentTemplates,
181182
userId: TEST_USER_ID,
182183
clientSessionId: 'test-session',
184+
ancestorRunIds: [],
183185
onResponseChunk: () => {},
184186
parentSystemPrompt: parentSystemPrompt,
185187
})
@@ -229,6 +231,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
229231
localAgentTemplates: mockLocalAgentTemplates,
230232
userId: TEST_USER_ID,
231233
clientSessionId: 'test-session',
234+
ancestorRunIds: [],
232235
onResponseChunk: () => {},
233236
})
234237

@@ -258,6 +261,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
258261
localAgentTemplates: mockLocalAgentTemplates,
259262
userId: TEST_USER_ID,
260263
clientSessionId: 'test-session',
264+
ancestorRunIds: [],
261265
onResponseChunk: () => {},
262266
parentSystemPrompt: parentSystemPrompt,
263267
})
@@ -308,6 +312,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
308312
localAgentTemplates: mockLocalAgentTemplates,
309313
userId: TEST_USER_ID,
310314
clientSessionId: 'test-session',
315+
ancestorRunIds: [],
311316
onResponseChunk: () => {},
312317
})
313318

@@ -340,6 +345,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
340345
localAgentTemplates: mockLocalAgentTemplates,
341346
userId: TEST_USER_ID,
342347
clientSessionId: 'test-session',
348+
ancestorRunIds: [],
343349
onResponseChunk: () => {},
344350
parentSystemPrompt: parentSystemPrompt,
345351
})
@@ -416,6 +422,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
416422
localAgentTemplates: mockLocalAgentTemplates,
417423
userId: TEST_USER_ID,
418424
clientSessionId: 'test-session',
425+
ancestorRunIds: [],
419426
onResponseChunk: () => {},
420427
})
421428

@@ -445,6 +452,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
445452
localAgentTemplates: mockLocalAgentTemplates,
446453
userId: TEST_USER_ID,
447454
clientSessionId: 'test-session',
455+
ancestorRunIds: [],
448456
onResponseChunk: () => {},
449457
parentSystemPrompt: parentSystemPrompt,
450458
})
@@ -505,6 +513,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
505513
localAgentTemplates: mockLocalAgentTemplates,
506514
userId: TEST_USER_ID,
507515
clientSessionId: 'test-session',
516+
ancestorRunIds: [],
508517
onResponseChunk: () => {},
509518
})
510519

@@ -537,6 +546,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
537546
localAgentTemplates: mockLocalAgentTemplates,
538547
userId: TEST_USER_ID,
539548
clientSessionId: 'test-session',
549+
ancestorRunIds: [],
540550
onResponseChunk: () => {},
541551
parentSystemPrompt: parentSystemPrompt,
542552
})

packages/agent-runtime/src/__tests__/read-docs-tool.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
104104
...agentRuntimeImpl,
105105
textOverride: null,
106106
runId: 'test-run-id',
107+
ancestorRunIds: [],
107108
repoId: undefined,
108109
repoUrl: undefined,
109110
system: 'Test system prompt',
@@ -163,6 +164,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
163164
...agentRuntimeImpl,
164165
textOverride: null,
165166
runId: 'test-run-id',
167+
ancestorRunIds: [],
166168
repoId: undefined,
167169
repoUrl: undefined,
168170
system: 'Test system prompt',
@@ -214,6 +216,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
214216
...agentRuntimeImpl,
215217
textOverride: null,
216218
runId: 'test-run-id',
219+
ancestorRunIds: [],
217220
repoId: undefined,
218221
repoUrl: undefined,
219222
system: 'Test system prompt',
@@ -265,6 +268,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
265268
...agentRuntimeImpl,
266269
textOverride: null,
267270
runId: 'test-run-id',
271+
ancestorRunIds: [],
268272
repoId: undefined,
269273
repoUrl: undefined,
270274
system: 'Test system prompt',
@@ -315,6 +319,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
315319
...agentRuntimeImpl,
316320
textOverride: null,
317321
runId: 'test-run-id',
322+
ancestorRunIds: [],
318323
repoId: undefined,
319324
repoUrl: undefined,
320325
system: 'Test system prompt',
@@ -367,6 +372,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
367372
...agentRuntimeImpl,
368373
textOverride: null,
369374
runId: 'test-run-id',
375+
ancestorRunIds: [],
370376
repoId: undefined,
371377
repoUrl: undefined,
372378
system: 'Test system prompt',
@@ -424,6 +430,7 @@ describe('read_docs tool with researcher agent (via web API facade)', () => {
424430
...agentRuntimeImpl,
425431
textOverride: null,
426432
runId: 'test-run-id',
433+
ancestorRunIds: [],
427434
repoId: undefined,
428435
repoUrl: undefined,
429436
system: 'Test system prompt',

0 commit comments

Comments
 (0)