Skip to content

Commit 4532582

Browse files
committed
sdk: Add env parameter to client and run(), pass through to run_terminal_command impl
1 parent d31d05b commit 4532582

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

sdk/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class CodebuffClient {
4545
* @param agentDefinitions - (Optional) Array of custom agent definitions. Each object should satisfy the AgentDefinition type. You can input the agent's id field into the agent parameter to run that agent.
4646
* @param customToolDefinitions - (Optional) Array of custom tool definitions that extend the agent's capabilities. Each tool definition includes a name, Zod schema for input validation, and a handler function. These tools can be called by the agent during execution.
4747
* @param maxAgentSteps - (Optional) Maximum number of steps the agent can take before stopping. Use this as a safety measure in case your agent starts going off the rails. A reasonable number is around 20.
48+
* @param env - (Optional) Environment variables to pass to terminal commands executed by the agent. These will be merged with process.env, with the custom values taking precedence. Can also be provided in individual run() calls to override.
4849
*
4950
* @returns A Promise that resolves to a RunState JSON object which you can pass to a subsequent run() call to continue the run. Use result.output to get the agent's output.
5051
*/

sdk/src/run.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type CodebuffClientOptions = {
5353
knowledgeFiles?: Record<string, string>
5454
agentDefinitions?: AgentDefinition[]
5555
maxAgentSteps?: number
56+
env?: Record<string, string>
5657

5758
handleEvent?: (event: PrintModeEvent) => void | Promise<void>
5859
handleStreamChunk?: (
@@ -111,6 +112,7 @@ export async function run({
111112
knowledgeFiles,
112113
agentDefinitions,
113114
maxAgentSteps = MAX_AGENT_STEPS_DEFAULT,
115+
env,
114116

115117
handleEvent,
116118
handleStreamChunk,
@@ -227,6 +229,7 @@ export async function run({
227229
: {},
228230
cwd,
229231
fs,
232+
env,
230233
})
231234
},
232235
requestMcpToolData: async ({ mcpConfig, toolNames }) => {
@@ -421,12 +424,14 @@ async function handleToolCall({
421424
customToolDefinitions,
422425
cwd,
423426
fs,
427+
env,
424428
}: {
425429
action: ServerAction<'tool-call-request'>
426430
overrides: NonNullable<CodebuffClientOptions['overrideTools']>
427431
customToolDefinitions: Record<string, CustomToolDefinition>
428432
cwd?: string
429433
fs: CodebuffFileSystem
434+
env?: Record<string, string>
430435
}): ReturnType<WebSocketHandler['handleToolCall']> {
431436
const toolName = action.toolName
432437
const input = action.input
@@ -468,6 +473,7 @@ async function handleToolCall({
468473
result = await runTerminalCommand({
469474
...input,
470475
cwd: path.resolve(resolvedCwd, input.cwd ?? '.'),
476+
env,
471477
} as Parameters<typeof runTerminalCommand>[0])
472478
} else if (toolName === 'code_search') {
473479
result = await codeSearch({

sdk/src/tools/run-terminal-command.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export function runTerminalCommand({
1515
process_type,
1616
cwd,
1717
timeout_seconds,
18+
env,
1819
}: {
1920
command: string
2021
process_type: 'SYNC' | 'BACKGROUND'
2122
cwd: string
2223
timeout_seconds: number
24+
env?: Record<string, string>
2325
}): Promise<CodebuffToolOutput<'run_terminal_command'>> {
2426
if (process_type === 'BACKGROUND') {
2527
throw new Error('BACKGROUND process_type not implemented')
@@ -37,6 +39,7 @@ export function runTerminalCommand({
3739
cwd: resolvedCwd,
3840
env: {
3941
...process.env,
42+
...(env ?? {}),
4043
FORCE_COLOR: '1',
4144
CLICOLOR: '1',
4245
CLICOLOR_FORCE: '1',

0 commit comments

Comments
 (0)