Skip to content

Commit e3c563e

Browse files
committed
sdk run(): handle event
1 parent 14173c5 commit e3c563e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

sdk/src/client.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
SessionState,
1515
} from '../../common/src/types/session-state'
1616
import { getFiles } from '../../npm-app/src/project-files'
17+
import { PrintModeEvent } from '../../common/src/types/print-mode'
1718

1819
export type ClientToolName =
1920
| 'read_files'
@@ -43,9 +44,15 @@ type RunState = {
4344

4445
export class CodebuffClient {
4546
public cwd: string
47+
4648
private readonly websocketHandler: WebSocketHandler
4749
private readonly overrideTools: CodebuffClientOptions['overrideTools']
4850
private readonly fingerprintId = `codebuff-sdk-${Math.random().toString(36).substring(2, 15)}`
51+
52+
private readonly promptIdToHandleEvent: Record<
53+
string,
54+
(event: PrintModeEvent) => void
55+
> = {}
4956
private readonly promptIdToResolveResponse: Record<
5057
string,
5158
{ resolve: (response: any) => void; reject: (error: any) => void }
@@ -69,7 +76,9 @@ export class CodebuffClient {
6976
this.overrideTools = overrideTools
7077
this.websocketHandler = new WebSocketHandler({
7178
apiKey,
72-
onWebsocketError: () => {},
79+
onWebsocketError: (error) => {
80+
onError({ message: error.message })
81+
},
7382
onWebsocketReconnect: () => {},
7483
onRequestReconnect: async () => {},
7584
onResponseError: async (error) => {
@@ -80,7 +89,13 @@ export class CodebuffClient {
8089
onCostResponse: async () => {},
8190
onUsageResponse: async () => {},
8291

83-
onResponseChunk: async () => {},
92+
onResponseChunk: async (action) => {
93+
const { userInputId, chunk } = action
94+
const handleEvent = this.promptIdToHandleEvent[userInputId]
95+
if (handleEvent && typeof chunk === 'object') {
96+
handleEvent(chunk)
97+
}
98+
},
8499
onSubagentResponseChunk: async () => {},
85100

86101
onPromptResponse: this.handlePromptResponse.bind(this),
@@ -97,8 +112,8 @@ export class CodebuffClient {
97112
* @param agent - The agent to run, e.g. 'base' or 'codebuff/file-picker@0.0.1'
98113
* @param prompt - The user prompt, e.g. 'Add a console.log to the index file'
99114
* @param params - (Optional) The parameters to pass to the agent.
100-
* @param handleEvent - A function to handle events.
101115
*
116+
* @param handleEvent - (Optional) A function to handle events.
102117
* @param previousState - (Optional) Continue a previous run with the return value of a previous run.
103118
*
104119
* @param allFiles - (Optional) All the files in the project, in an object of file path to file content. Improves codebuff's ability to locate files.
@@ -120,7 +135,7 @@ export class CodebuffClient {
120135
agent: string
121136
prompt: string
122137
params?: Record<string, any>
123-
handleEvent: (event: any) => void
138+
handleEvent?: (event: PrintModeEvent) => void
124139
previousState?: RunState
125140
allFiles?: Record<string, string>
126141
knowledgeFiles?: Record<string, string>
@@ -137,6 +152,9 @@ export class CodebuffClient {
137152
maxAgentSteps,
138153
})
139154
const toolResults = previousState?.toolResults ?? []
155+
if (handleEvent) {
156+
this.promptIdToHandleEvent[promptId] = handleEvent
157+
}
140158
this.websocketHandler.sendInput({
141159
promptId,
142160
prompt,

sdk/src/tools/change-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import z from 'zod'
22
import fs from 'fs'
33
import path from 'path'
4-
import { applyPatch } from '@codebuff/common/util/patch'
4+
import { applyPatch } from '../../../common/src/util/patch'
55

66
const FileChangeSchema = z.object({
77
type: z.enum(['patch', 'file']),

0 commit comments

Comments
 (0)