Skip to content

Commit 714a46f

Browse files
committed
feat(bash): expand shell.env hook context with messageID and agent
Plugins can now access messageID and agent in the shell.env hook, enabling them to inject full session callback context into bash env vars.
1 parent 5d3dba6 commit 714a46f

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

packages/opencode/src/tool/bash.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ async function ask(ctx: Tool.Context, scan: Scan) {
304304
}
305305

306306
async function shellEnv(ctx: Tool.Context, cwd: string) {
307-
const extra = await Plugin.trigger("shell.env", { cwd, sessionID: ctx.sessionID, callID: ctx.callID }, { env: {} })
307+
const extra = await Plugin.trigger(
308+
"shell.env",
309+
{ cwd, sessionID: ctx.sessionID, callID: ctx.callID, messageID: ctx.messageID, agent: ctx.agent },
310+
{ env: {} },
311+
)
308312
return {
309313
...process.env,
310314
...extra.env,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { readFileSync } from "fs"
3+
import { resolve } from "path"
4+
5+
describe("shell.env hook type alignment", () => {
6+
const pluginSrc = readFileSync(resolve(import.meta.dir, "../../../plugin/src/index.ts"), "utf-8")
7+
const bashSrc = readFileSync(resolve(import.meta.dir, "../../src/tool/bash.ts"), "utf-8")
8+
9+
// Regression: bash.ts passed messageID and agent to the shell.env hook,
10+
// but the plugin SDK type only declared { cwd, sessionID?, callID? }.
11+
// Plugin authors couldn't see the new fields in their IDE.
12+
test("plugin SDK shell.env input type includes messageID", () => {
13+
const match = pluginSrc.match(/"shell\.env"\?[\s\S]*?input:\s*\{([^}]+)\}/)
14+
expect(match).not.toBeNull()
15+
expect(match![1]).toContain("messageID")
16+
})
17+
18+
test("plugin SDK shell.env input type includes agent", () => {
19+
const match = pluginSrc.match(/"shell\.env"\?[\s\S]*?input:\s*\{([^}]+)\}/)
20+
expect(match).not.toBeNull()
21+
expect(match![1]).toContain("agent")
22+
})
23+
24+
test("bash.ts passes messageID and agent to shell.env trigger", () => {
25+
// Verify bash.ts actually sends these fields so the types stay in sync
26+
const triggerMatch = bashSrc.match(/Plugin\.trigger\(\s*"shell\.env"[\s\S]*?\{([^}]+)\}/)
27+
expect(triggerMatch).not.toBeNull()
28+
expect(triggerMatch![1]).toContain("messageID")
29+
expect(triggerMatch![1]).toContain("agent")
30+
})
31+
})

packages/plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export interface Hooks {
234234
output: { args: any },
235235
) => Promise<void>
236236
"shell.env"?: (
237-
input: { cwd: string; sessionID?: string; callID?: string },
237+
input: { cwd: string; sessionID?: string; callID?: string; messageID?: string; agent?: string },
238238
output: { env: Record<string, string> },
239239
) => Promise<void>
240240
"tool.execute.after"?: (

0 commit comments

Comments
 (0)