Skip to content

Commit db4156e

Browse files
committed
inject handleStepsLogChunk
1 parent 1e419d4 commit db4156e

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

backend/src/client-wrapper.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { subscribeToAction } from './websockets/websocket-action'
66

77
import type { ServerAction } from '@codebuff/common/actions'
88
import type {
9+
HandleStepsLogChunkFn,
910
RequestFilesFn,
1011
RequestMcpToolDataFn,
1112
RequestOptionalFileFn,
@@ -197,7 +198,6 @@ export function sendSubagentChunkWs(
197198
): ReturnType<SendSubagentChunkFn> {
198199
const { ws, userInputId, agentId, agentType, chunk, prompt } = params
199200
return sendAction(ws, {
200-
...params,
201201
type: 'subagent-response-chunk',
202202
userInputId,
203203
agentId,
@@ -206,3 +206,19 @@ export function sendSubagentChunkWs(
206206
prompt,
207207
})
208208
}
209+
210+
export function handleStepsLogChunkWs(
211+
params: {
212+
ws: WebSocket
213+
} & ParamsOf<HandleStepsLogChunkFn>,
214+
): ReturnType<HandleStepsLogChunkFn> {
215+
const { ws, userInputId, runId, level, data, message } = params
216+
return sendAction(ws, {
217+
type: 'handlesteps-log-chunk',
218+
userInputId,
219+
agentId: runId,
220+
level,
221+
data,
222+
message,
223+
})
224+
}

backend/src/run-programmatic-step.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import type {
1414
StepGenerator,
1515
PublicAgentState,
1616
} from '@codebuff/common/types/agent-template'
17+
import type { HandleStepsLogChunkFn } from '@codebuff/common/types/contracts/client'
18+
import type { Logger } from '@codebuff/common/types/contracts/logger'
19+
import type {
20+
ParamsExcluding,
21+
ParamsOf,
22+
} from '@codebuff/common/types/function-params'
1723
import type {
1824
ToolResultOutput,
1925
ToolResultPart,
2026
} from '@codebuff/common/types/messages/content-part'
2127
import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
2228
import type { AgentState } from '@codebuff/common/types/session-state'
23-
import type { ParamsExcluding, ParamsOf } from '@codebuff/common/types/function-params'
24-
import type { Logger } from '@codebuff/common/types/contracts/logger'
2529
import type { WebSocket } from 'ws'
2630

2731
// Global sandbox manager for QuickJS contexts
@@ -59,6 +63,7 @@ export async function runProgrammaticStep(
5963
localAgentTemplates: Record<string, AgentTemplate>
6064
stepsComplete: boolean
6165
stepNumber: number
66+
handleStepsLogChunk: HandleStepsLogChunkFn
6267
logger: Logger
6368
} & ParamsExcluding<
6469
typeof executeToolCall,
@@ -89,6 +94,7 @@ export async function runProgrammaticStep(
8994
ws,
9095
localAgentTemplates,
9196
stepsComplete,
97+
handleStepsLogChunk,
9298
logger,
9399
} = params
94100
let { stepNumber } = params
@@ -111,10 +117,9 @@ export async function runProgrammaticStep(
111117
(level: 'debug' | 'info' | 'warn' | 'error') =>
112118
(data: any, msg?: string) => {
113119
logger[level](data, msg) // Log to backend
114-
sendAction(ws, {
115-
type: 'handlesteps-log-chunk',
120+
handleStepsLogChunk({
116121
userInputId,
117-
agentId: agentState.agentId,
122+
runId: agentState.runId ?? 'undefined',
118123
level,
119124
data,
120125
message: msg,

backend/src/websockets/middleware.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getUserInfoFromApiKey } from './auth'
1616
import { updateRequestContext } from './request-context'
1717
import { sendAction } from './websocket-action'
1818
import {
19+
handleStepsLogChunkWs,
1920
requestFilesWs,
2021
requestMcpToolDataWs,
2122
requestOptionalFileWs,
@@ -154,6 +155,8 @@ export class WebSocketMiddleware {
154155
: undefined
155156

156157
const scopedDeps: AgentRuntimeScopedDeps = {
158+
handleStepsLogChunk: (params) =>
159+
handleStepsLogChunkWs({ ...params, ws }),
157160
requestToolCall: (params) => requestToolCallWs({ ...params, ws }),
158161
requestMcpToolData: (params) => requestMcpToolDataWs({ ...params, ws }),
159162
requestFiles: (params) => requestFilesWs({ ...params, ws }),

common/src/testing/impl/agent-runtime.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export const TEST_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
4141
export const TEST_AGENT_RUNTIME_SCOPED_IMPL =
4242
Object.freeze<AgentRuntimeScopedDeps>({
4343
// Database
44+
handleStepsLogChunk: () => {
45+
throw new Error('handleStepsLogChunk not implemented in test runtime')
46+
},
4447
requestToolCall: () => {
4548
throw new Error('requestToolCall not implemented in test runtime')
4649
},

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
HandleStepsLogChunkFn,
23
RequestFilesFn,
34
RequestMcpToolDataFn,
45
RequestOptionalFileFn,
@@ -38,6 +39,7 @@ export type AgentRuntimeDeps = {
3839

3940
export type AgentRuntimeScopedDeps = {
4041
// Client (WebSocket)
42+
handleStepsLogChunk: HandleStepsLogChunkFn
4143
requestToolCall: RequestToolCallFn
4244
requestMcpToolData: RequestMcpToolDataFn
4345
requestFiles: RequestFilesFn

common/src/types/contracts/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ export type SendSubagentChunkFn = (params: {
3636
chunk: string
3737
prompt?: string | undefined
3838
}) => void
39+
40+
export type HandleStepsLogChunkFn = (params: {
41+
userInputId: string
42+
runId: string
43+
level: 'debug' | 'info' | 'warn' | 'error'
44+
data: unknown
45+
message?: string
46+
}) => void

0 commit comments

Comments
 (0)