GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(plugin): add shell.env hook for manipulating environment in tool… · un33k/opencode@a30696f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • opencode/src
      • pty
        • index.ts
      • session
        • prompt.ts
      • tool
        • bash.ts
    • plugin/src
      • index.ts
    • web/src/content/docs
      • plugins.mdx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/pty/index.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/pty/index.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { WSContext } from "hono/ws"
88
import { Instance } from "../project/instance"
99
import { lazy } from "@opencode-ai/util/lazy"
1010
import { Shell } from "@/shell/shell"
11+
import { Plugin } from "@/plugin"
1112

1213
export namespace Pty {
1314
const log = Log.create({ service: "pty" })
@@ -102,9 +103,11 @@ export namespace Pty {
102103
}
103104

104105
const cwd = input.cwd || Instance.directory
106+
const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} })
105107
const env = {
106108
...process.env,
107109
...input.env,
110+
...shellEnv.env,
108111
TERM: "xterm-256color",
109112
OPENCODE_TERMINAL: "1",
110113
} as Record<string, string>
Collapse file

‎packages/opencode/src/session/prompt.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,12 +1500,15 @@ NOTE: At any point in time through this workflow you should feel free to ask the
15001500
const matchingInvocation = invocations[shellName] ?? invocations[""]
15011501
const args = matchingInvocation?.args
15021502

1503+
const cwd = Instance.directory
1504+
const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} })
15031505
const proc = spawn(shell, args, {
1504-
cwd: Instance.directory,
1506+
cwd,
15051507
detached: process.platform !== "win32",
15061508
stdio: ["ignore", "pipe", "pipe"],
15071509
env: {
15081510
...process.env,
1511+
...shellEnv.env,
15091512
TERM: "dumb",
15101513
},
15111514
})
Collapse file

‎packages/opencode/src/tool/bash.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/tool/bash.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Shell } from "@/shell/shell"
1616

1717
import { BashArity } from "@/permission/arity"
1818
import { Truncate } from "./truncation"
19+
import { Plugin } from "@/plugin"
1920

2021
const MAX_METADATA_LENGTH = 30_000
2122
const DEFAULT_TIMEOUT = Flag.OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS || 2 * 60 * 1000
@@ -162,11 +163,13 @@ export const BashTool = Tool.define("bash", async () => {
162163
})
163164
}
164165

166+
const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} })
165167
const proc = spawn(params.command, {
166168
shell,
167169
cwd,
168170
env: {
169171
...process.env,
172+
...shellEnv.env,
170173
},
171174
stdio: ["ignore", "pipe", "pipe"],
172175
detached: process.platform !== "win32",
Collapse file

‎packages/plugin/src/index.ts‎

Copy file name to clipboardExpand all lines: packages/plugin/src/index.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export interface Hooks {
185185
input: { tool: string; sessionID: string; callID: string },
186186
output: { args: any },
187187
) => Promise<void>
188+
"shell.env"?: (input: { cwd: string }, output: { env: Record<string, string> }) => Promise<void>
188189
"tool.execute.after"?: (
189190
input: { tool: string; sessionID: string; callID: string },
190191
output: {
Collapse file

‎packages/web/src/content/docs/plugins.mdx‎

Copy file name to clipboardExpand all lines: packages/web/src/content/docs/plugins.mdx
+21Lines changed: 21 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ Plugins can subscribe to events as seen below in the Examples section. Here is a
192192

193193
- `todo.updated`
194194

195+
#### Shell Events
196+
197+
- `shell.env`
198+
195199
#### Tool Events
196200

197201
- `tool.execute.after`
@@ -254,6 +258,23 @@ export const EnvProtection = async ({ project, client, $, directory, worktree })
254258

255259
---
256260

261+
### Inject environment variables
262+
263+
Inject environment variables into all shell execution (AI tools and user terminals):
264+
265+
```javascript title=".opencode/plugins/inject-env.js"
266+
export const InjectEnvPlugin = async () => {
267+
return {
268+
"shell.env": async (input, output) => {
269+
output.env.MY_API_KEY = "secret"
270+
output.env.PROJECT_ROOT = input.cwd
271+
},
272+
}
273+
}
274+
```
275+
276+
---
277+
257278
### Custom tools
258279

259280
Plugins can also add custom tools to opencode:

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.