Skip to content

Commit 83eb622

Browse files
committed
inject sendSubagentChunk
1 parent 16e294f commit 83eb622

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

backend/src/client-wrapper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
RequestFilesFn,
1010
RequestMcpToolDataFn,
1111
RequestOptionalFileFn,
12+
SendSubagentChunkFn,
1213
} from '@codebuff/common/types/contracts/client'
1314
import type { ParamsOf } from '@codebuff/common/types/function-params'
1415
import type { MCPConfig } from '@codebuff/common/types/mcp'
@@ -188,3 +189,20 @@ export async function requestOptionalFileWs(
188189
const files = await requestFilesWs({ ws, filePaths: [filePath] })
189190
return toOptionalFile(files[filePath] ?? null)
190191
}
192+
193+
export function sendSubagentChunkWs(
194+
params: {
195+
ws: WebSocket
196+
} & ParamsOf<SendSubagentChunkFn>,
197+
): ReturnType<SendSubagentChunkFn> {
198+
const { ws, userInputId, agentId, agentType, chunk, prompt } = params
199+
return sendAction(ws, {
200+
...params,
201+
type: 'subagent-response-chunk',
202+
userInputId,
203+
agentId,
204+
agentType,
205+
chunk,
206+
prompt,
207+
})
208+
}

backend/src/tools/stream-parser.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { cloneDeep } from 'lodash'
1010

1111
import { executeBatchStrReplaces } from './batch-str-replace'
1212
import { expireMessages } from '../util/messages'
13-
import { sendAction } from '../websockets/websocket-action'
1413
import { processStreamWithTags } from '../xml-stream-parser'
1514
import { executeCustomToolCall, executeToolCall } from './tool-executor'
1615

@@ -19,6 +18,7 @@ import type { CustomToolCall, ExecuteToolCallParams } from './tool-executor'
1918
import type { AgentTemplate } from '../templates/types'
2019
import type { ToolName } from '@codebuff/common/tools/constants'
2120
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
21+
import type { SendSubagentChunkFn } from '@codebuff/common/types/contracts/client'
2222
import type { StreamChunk } from '@codebuff/common/types/contracts/llm'
2323
import type { Logger } from '@codebuff/common/types/contracts/logger'
2424
import type { ParamsExcluding } from '@codebuff/common/types/function-params'
@@ -58,6 +58,7 @@ export async function processStreamWithTools(
5858
agentContext: Record<string, Subgoal>
5959
onResponseChunk: (chunk: string | PrintModeEvent) => void
6060
fullResponse: string
61+
sendSubagentChunk: SendSubagentChunkFn
6162
logger: Logger
6263
} & Omit<
6364
ExecuteToolCallParams<any>,
@@ -90,6 +91,7 @@ export async function processStreamWithTools(
9091
system,
9192
agentState,
9293
onResponseChunk,
94+
sendSubagentChunk,
9395
logger,
9496
} = params
9597
const fullResponseChunks: string[] = [params.fullResponse]
@@ -118,19 +120,7 @@ export async function processStreamWithTools(
118120
repoId,
119121
agentTemplate,
120122
localAgentTemplates,
121-
sendSubagentChunk: (data: {
122-
userInputId: string
123-
agentId: string
124-
agentType: string
125-
chunk: string
126-
prompt?: string
127-
}) => {
128-
sendAction(ws, {
129-
type: 'subagent-response-chunk',
130-
...data,
131-
})
132-
},
133-
123+
sendSubagentChunk,
134124
agentState,
135125
agentContext,
136126
messages,

backend/src/websockets/middleware.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
requestMcpToolDataWs,
2121
requestOptionalFileWs,
2222
requestToolCallWs,
23+
sendSubagentChunkWs,
2324
} from '../client-wrapper'
2425
import { withAppContext } from '../context/app-context'
2526
import { BACKEND_AGENT_RUNTIME_IMPL } from '../impl/agent-runtime'
@@ -158,6 +159,7 @@ export class WebSocketMiddleware {
158159
requestFiles: (params) => requestFilesWs({ ...params, ws }),
159160
requestOptionalFile: (params) =>
160161
requestOptionalFileWs({ ...params, ws }),
162+
sendSubagentChunk: (params) => sendSubagentChunkWs({ ...params, ws }),
161163
}
162164

163165
// Use the new combined context - much cleaner!

common/src/types/contracts/agent-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
RequestMcpToolDataFn,
44
RequestOptionalFileFn,
55
RequestToolCallFn,
6+
SendSubagentChunkFn,
67
} from './client'
78
import type {
89
AddAgentStepFn,
@@ -41,4 +42,5 @@ export type AgentRuntimeScopedDeps = {
4142
requestMcpToolData: RequestMcpToolDataFn
4243
requestFiles: RequestFilesFn
4344
requestOptionalFile: RequestOptionalFileFn
45+
sendSubagentChunk: SendSubagentChunkFn
4446
}

common/src/types/contracts/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ export type RequestFilesFn = (params: {
2828
export type RequestOptionalFileFn = (params: {
2929
filePath: string
3030
}) => Promise<string | null>
31+
32+
export type SendSubagentChunkFn = (params: {
33+
userInputId: string
34+
agentId: string
35+
agentType: string
36+
chunk: string
37+
prompt?: string | undefined
38+
}) => void

evals/scaffolding.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import fs from 'fs'
44
import path from 'path'
55

66
import {
7+
requestFilesWs,
78
requestMcpToolDataWs,
9+
requestOptionalFileWs,
810
requestToolCallWs,
11+
sendSubagentChunkWs,
912
} from '@codebuff/backend/client-wrapper'
1013
import { runAgentStep } from '@codebuff/backend/run-agent-step'
1114
import { assembleLocalAgentTemplates } from '@codebuff/backend/templates/agent-registry'
@@ -28,7 +31,6 @@ import type {
2831
SDKAssistantMessage,
2932
SDKUserMessage,
3033
} from '@anthropic-ai/claude-code'
31-
import type { requestFilesWs as originalRequestFiles } from '@codebuff/backend/websockets/websocket-action'
3234
import type { ClientToolCall } from '@codebuff/common/tools/list'
3335
import type { AgentRuntimeScopedDeps } from '@codebuff/common/types/contracts/agent-runtime'
3436
import type {
@@ -80,7 +82,7 @@ export function createFileReadingMock(projectRoot: string) {
8082
files[filePath] = readMockFile(projectRoot, filePath)
8183
}
8284
return Promise.resolve(files)
83-
}) satisfies typeof originalRequestFiles,
85+
}) satisfies typeof requestFilesWs,
8486
requestToolCall: (async (params: {
8587
ws: WebSocket
8688
userInputId: string
@@ -188,6 +190,11 @@ export async function runAgentStepScaffolding(
188190
requestToolCall: (params) => requestToolCallWs({ ...params, ws: mockWs }),
189191
requestMcpToolData: (params) =>
190192
requestMcpToolDataWs({ ...params, ws: mockWs }),
193+
requestFiles: (params) => requestFilesWs({ ...params, ws: mockWs }),
194+
requestOptionalFile: (params) =>
195+
requestOptionalFileWs({ ...params, ws: mockWs }),
196+
sendSubagentChunk: (params) =>
197+
sendSubagentChunkWs({ ...params, ws: mockWs }),
191198
}
192199
const result = await runAgentStep({
193200
...EVALS_AGENT_RUNTIME_IMPL,

0 commit comments

Comments
 (0)