@@ -16,24 +16,24 @@ import {
1616import { getFiles } from '../../npm-app/src/project-files'
1717import { 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
2421export 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
3939type 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 {
262272function 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
0 commit comments