Skip to content

Commit cf966d6

Browse files
committed
Fix sdk bug where sessionState got overridden by CodebuffClient this.options
1 parent 8010b77 commit cf966d6

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

sdk/src/client.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import type { RunOptions, CodebuffClientOptions } from './run'
66
import type { RunState } from './run-state'
77

88
export class CodebuffClient {
9-
public options: Required<CodebuffClientOptions> & { fingerprintId: string }
9+
public options: CodebuffClientOptions & {
10+
apiKey: string
11+
fingerprintId: string
12+
}
1013

1114
constructor(options: CodebuffClientOptions) {
1215
const foundApiKey = options.apiKey ?? process.env[API_KEY_ENV_VAR]
@@ -18,21 +21,13 @@ export class CodebuffClient {
1821

1922
this.options = {
2023
apiKey: foundApiKey,
21-
cwd: process.cwd(),
22-
projectFiles: {},
23-
knowledgeFiles: {},
24-
agentDefinitions: [],
25-
maxAgentSteps: MAX_AGENT_STEPS_DEFAULT,
2624
handleEvent: (event) => {
2725
if (event.type === 'error') {
2826
throw new Error(
2927
`Received error: ${event.message}.\n\nProvide a handleEvent function to handle this error.`,
3028
)
3129
}
3230
},
33-
handleStreamChunk: () => {},
34-
overrideTools: {},
35-
customToolDefinitions: [],
3631
fingerprintId: `codebuff-sdk-${Math.random().toString(36).substring(2, 15)}`,
3732
...options,
3833
}

sdk/src/run.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
} from '../../common/src/types/messages/content-part'
3131
import type { PrintModeEvent } from '../../common/src/types/print-mode'
3232
import type { SessionState } from '../../common/src/types/session-state'
33+
import { MAX_AGENT_STEPS_DEFAULT } from '../../common/src/constants/agents'
3334

3435
export type CodebuffClientOptions = {
3536
// Provide an API key or set the CODEBUFF_API_KEY environment variable.
@@ -72,11 +73,11 @@ export async function run({
7273
apiKey,
7374
fingerprintId,
7475

75-
cwd,
76+
cwd = process.cwd(),
7677
projectFiles,
7778
knowledgeFiles,
7879
agentDefinitions,
79-
maxAgentSteps,
80+
maxAgentSteps = MAX_AGENT_STEPS_DEFAULT,
8081

8182
handleEvent,
8283
handleStreamChunk,
@@ -90,11 +91,14 @@ export async function run({
9091
previousRun,
9192
extraToolResults,
9293
}: RunOptions &
93-
Required<CodebuffClientOptions> & {
94+
CodebuffClientOptions & {
95+
apiKey: string
9496
fingerprintId: string
9597
}): Promise<RunState> {
9698
function onError(error: { message: string }) {
97-
handleEvent({ type: 'error', message: error.message })
99+
if (handleEvent) {
100+
handleEvent({ type: 'error', message: error.message })
101+
}
98102
}
99103

100104
let resolve: (value: RunReturnType) => any = () => {}
@@ -114,24 +118,26 @@ export async function run({
114118
onError({ message: error.message })
115119
},
116120
readFiles: ({ filePaths }) =>
117-
readFiles({ filePaths, override: overrideTools.read_files, cwd }),
121+
readFiles({ filePaths, override: overrideTools?.read_files, cwd }),
118122
handleToolCall: (action) =>
119123
handleToolCall({
120124
action,
121-
overrides: overrideTools,
122-
customToolDefinitions: Object.fromEntries(
123-
customToolDefinitions.map((def) => [def.toolName, def]),
124-
),
125+
overrides: overrideTools ?? {},
126+
customToolDefinitions: customToolDefinitions
127+
? Object.fromEntries(
128+
customToolDefinitions.map((def) => [def.toolName, def]),
129+
)
130+
: {},
125131
cwd,
126132
}),
127133
onCostResponse: async () => {},
128134

129135
onResponseChunk: async (action) => {
130136
const { userInputId, chunk } = action
131137
if (typeof chunk === 'string') {
132-
handleStreamChunk(chunk)
138+
handleStreamChunk?.(chunk)
133139
} else {
134-
handleEvent(chunk)
140+
handleEvent?.(chunk)
135141
}
136142
},
137143
onSubagentResponseChunk: async () => {},

0 commit comments

Comments
 (0)