Skip to content

Commit 4df173b

Browse files
committed
feat(bash): add env parameter for setting environment variables
Add optional `env` parameter to the bash tool that allows passing environment variables to the spawned process. This enables plugins to inject environment variables like AGENT_MODE and AGENT_ID to track whether commands are running in primary or subagent sessions. Changes: - Add `env` parameter to bash tool schema (z.record(z.string(), z.string())) - Merge env vars with process.env in spawn call - Add tests for single and multiple environment variables
1 parent 5d3dba6 commit 4df173b

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/opencode/src/tool/bash.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const Parameters = z.object({
6464
.describe(
6565
"Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory 'foo'",
6666
),
67+
env: z.record(z.string(), z.string()).optional().describe("Environment variables to set for the command"),
6768
})
6869

6970
type Part = {
@@ -487,7 +488,7 @@ export const BashTool = Tool.define("bash", async () => {
487488
name,
488489
command: params.command,
489490
cwd,
490-
env: await shellEnv(ctx, cwd),
491+
env: { ...(await shellEnv(ctx, cwd)), ...params.env },
491492
timeout,
492493
description: params.description,
493494
},

packages/opencode/test/tool/bash.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,3 +1097,43 @@ describe("tool.bash truncation", () => {
10971097
})
10981098
})
10991099
})
1100+
1101+
describe("tool.bash env", () => {
1102+
test("sets environment variables", async () => {
1103+
await Instance.provide({
1104+
directory: projectRoot,
1105+
fn: async () => {
1106+
const bash = await BashTool.init()
1107+
const result = await bash.execute(
1108+
{
1109+
command: "echo $TEST_VAR",
1110+
description: "Echo environment variable",
1111+
env: { TEST_VAR: "hello_world" },
1112+
},
1113+
ctx,
1114+
)
1115+
expect(result.metadata.exit).toBe(0)
1116+
expect(result.output).toContain("hello_world")
1117+
},
1118+
})
1119+
})
1120+
1121+
test("sets multiple environment variables", async () => {
1122+
await Instance.provide({
1123+
directory: projectRoot,
1124+
fn: async () => {
1125+
const bash = await BashTool.init()
1126+
const result = await bash.execute(
1127+
{
1128+
command: "echo $VAR1 $VAR2",
1129+
description: "Echo multiple environment variables",
1130+
env: { VAR1: "foo", VAR2: "bar" },
1131+
},
1132+
ctx,
1133+
)
1134+
expect(result.metadata.exit).toBe(0)
1135+
expect(result.output).toContain("foo bar")
1136+
},
1137+
})
1138+
})
1139+
})

0 commit comments

Comments
 (0)