Skip to content

Commit ba79fe2

Browse files
committed
sdk fixes suggested by gpt5
1 parent 4aabf24 commit ba79fe2

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

sdk/src/client.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ import {
1616
import { getFiles } from '../../npm-app/src/project-files'
1717
import { PrintModeEvent } from '../../common/src/types/print-mode'
1818

19-
export type ClientToolName =
20-
| 'read_files'
21-
| 'write_file'
22-
| 'run_terminal_command'
19+
type ClientToolName = 'write_file' | 'run_terminal_command'
2320

2421
export type CodebuffClientOptions = {
2522
cwd: string
2623
onError: (error: { message: string }) => void
27-
overrideTools: Record<
28-
ClientToolName,
29-
(
30-
args: Extract<ServerAction, { type: 'tool-call-request' }>['args'],
31-
) => Promise<{ toolResultMessage: string }>
32-
> & {
33-
readFiles: (
34-
filePath: string[],
35-
) => Promise<{ files: Record<string, string | null> }>
36-
}
24+
overrideTools: Partial<
25+
Record<
26+
ClientToolName,
27+
(
28+
args: Extract<ServerAction, { type: 'tool-call-request' }>['args'],
29+
) => Promise<{ toolResultMessage: string }>
30+
> & {
31+
// Include read_files separately, since it has a different signature.
32+
read_files: (
33+
filePath: string[],
34+
) => Promise<{ files: Record<string, string | null> }>
35+
}
36+
>
3737
}
3838

3939
type RunState = {
@@ -59,9 +59,14 @@ export class CodebuffClient {
5959

6060
constructor({ cwd, onError, overrideTools }: CodebuffClientOptions) {
6161
// TODO: download binary automatically
62-
if (execFileSync('which', [CODEBUFF_BINARY]).toString().trim() === '') {
62+
const isWindows = process.platform === 'win32'
63+
if (
64+
execFileSync(isWindows ? 'where' : 'which', [CODEBUFF_BINARY])
65+
.toString()
66+
.trim() === ''
67+
) {
6368
throw new Error(
64-
`Could not find ${CODEBUFF_BINARY} in PATH. Please run "npm i -g codebuff" to install the codebuff.`,
69+
`Could not find ${CODEBUFF_BINARY} in PATH. Please run "npm i -g codebuff" to install codebuff.`,
6570
)
6671
}
6772
if (!process.env[API_KEY_ENV_VAR]) {
@@ -141,6 +146,8 @@ export class CodebuffClient {
141146
agentConfig?: Record<string, any>
142147
maxAgentSteps?: number
143148
}): Promise<RunState> {
149+
await this.websocketHandler.connect()
150+
144151
const promptId = Math.random().toString(36).substring(2, 15)
145152
const sessionState =
146153
previousState?.sessionState ??
@@ -190,17 +197,20 @@ export class CodebuffClient {
190197
}
191198

192199
if (promiseActions) {
193-
const { sessionState, toolResults } = action
200+
const { sessionState, toolResults } = parsedAction.data
194201
const state: RunState = {
195202
sessionState,
196203
toolResults,
197204
}
198205
promiseActions.resolve(state)
206+
207+
delete this.promptIdToResolveResponse[action.promptId]
208+
delete this.promptIdToHandleEvent[action.promptId]
199209
}
200210
}
201211

202212
private async readFiles(filePath: string[]) {
203-
const override = this.overrideTools.readFiles
213+
const override = this.overrideTools.read_files
204214
if (override) {
205215
const overrideResult = await override(filePath)
206216
return overrideResult.files
@@ -262,6 +272,7 @@ export class CodebuffClient {
262272
function initialSessionState(
263273
cwd: string,
264274
options: {
275+
// TODO: Parse allFiles into fileTree, fileTokenScores, tokenCallers
265276
allFiles?: Record<string, string>
266277
knowledgeFiles?: Record<string, string>
267278
agentConfig?: Record<string, any>
@@ -289,11 +300,11 @@ function initialSessionState(
289300
shellConfigFiles: {},
290301
systemInfo: {
291302
platform: process.platform,
292-
shell: 'bash',
303+
shell: process.platform === 'win32' ? 'cmd.exe' : 'bash',
293304
nodeVersion: process.version,
294305
arch: process.arch,
295306
homedir: os.homedir(),
296-
cpus: 16,
307+
cpus: os.cpus().length ?? 1,
297308
},
298309
})
299310

sdk/src/tools/change-file.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export function changeFile(
1313
parameters: unknown,
1414
cwd: string,
1515
): { toolResultMessage: string } {
16+
if (cwd.includes('../')) {
17+
throw new Error('cwd cannot include ../')
18+
}
1619
const fileChange = FileChangeSchema.parse(parameters)
1720
const lines = fileChange.content.split('\n')
1821

sdk/src/websocket-client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class WebSocketHandler {
5858
private onSubagentResponseChunk: WebSocketHandlerOptionsWithDefaults['onSubagentResponseChunk']
5959
private onPromptResponse: WebSocketHandlerOptionsWithDefaults['onPromptResponse']
6060
private apiKey: string
61+
private isConnected = false
6162

6263
constructor({
6364
onWebsocketError = () => {},
@@ -98,8 +99,11 @@ export class WebSocketHandler {
9899
}
99100

100101
public async connect() {
101-
await this.cbWebSocket.connect()
102-
this.setupSubscriptions()
102+
if (!this.isConnected) {
103+
await this.cbWebSocket.connect()
104+
this.setupSubscriptions()
105+
this.isConnected = true
106+
}
103107
}
104108

105109
public reconnect() {

0 commit comments

Comments
 (0)