GeezerVison is showing github.com — feat(plugin): add shell.env hook for manipulating environment in tool… · un33k/opencode@a30696f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/pty/index.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ import type { WSContext } from "hono/ws" | 8 | 8 | import { Instance } from "../project/instance" | 9 | 9 | import { lazy } from "@opencode-ai/util/lazy" | 10 | 10 | import { Shell } from "@/shell/shell" | | 11 | +import { Plugin } from "@/plugin" | 11 | 12 |
| 12 | 13 | export namespace Pty { | 13 | 14 | const log = Log.create({ service: "pty" }) | @@ -102,9 +103,11 @@ export namespace Pty { | 102 | 103 | } | 103 | 104 |
| 104 | 105 | const cwd = input.cwd || Instance.directory | | 106 | + const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} }) | 105 | 107 | const env = { | 106 | 108 | ...process.env, | 107 | 109 | ...input.env, | | 110 | + ...shellEnv.env, | 108 | 111 | TERM: "xterm-256color", | 109 | 112 | OPENCODE_TERMINAL: "1", | 110 | 113 | } as Record<string, string> | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1500,12 +1500,15 @@ NOTE: At any point in time through this workflow you should feel free to ask the | 1500 | 1500 | const matchingInvocation = invocations[shellName] ?? invocations[""] | 1501 | 1501 | const args = matchingInvocation?.args | 1502 | 1502 |
| | 1503 | + const cwd = Instance.directory | | 1504 | + const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} }) | 1503 | 1505 | const proc = spawn(shell, args, { | 1504 | | - cwd: Instance.directory, | | 1506 | + cwd, | 1505 | 1507 | detached: process.platform !== "win32", | 1506 | 1508 | stdio: ["ignore", "pipe", "pipe"], | 1507 | 1509 | env: { | 1508 | 1510 | ...process.env, | | 1511 | + ...shellEnv.env, | 1509 | 1512 | TERM: "dumb", | 1510 | 1513 | }, | 1511 | 1514 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/bash.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { Shell } from "@/shell/shell" | 16 | 16 |
| 17 | 17 | import { BashArity } from "@/permission/arity" | 18 | 18 | import { Truncate } from "./truncation" | | 19 | +import { Plugin } from "@/plugin" | 19 | 20 |
| 20 | 21 | const MAX_METADATA_LENGTH = 30_000 | 21 | 22 | const DEFAULT_TIMEOUT = Flag.OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS || 2 * 60 * 1000 | @@ -162,11 +163,13 @@ export const BashTool = Tool.define("bash", async () => { | 162 | 163 | }) | 163 | 164 | } | 164 | 165 |
| | 166 | + const shellEnv = await Plugin.trigger("shell.env", { cwd }, { env: {} }) | 165 | 167 | const proc = spawn(params.command, { | 166 | 168 | shell, | 167 | 169 | cwd, | 168 | 170 | env: { | 169 | 171 | ...process.env, | | 172 | + ...shellEnv.env, | 170 | 173 | }, | 171 | 174 | stdio: ["ignore", "pipe", "pipe"], | 172 | 175 | detached: process.platform !== "win32", | |
Collapse file Copy file name to clipboardExpand all lines: packages/plugin/src/index.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -185,6 +185,7 @@ export interface Hooks { | 185 | 185 | input: { tool: string; sessionID: string; callID: string }, | 186 | 186 | output: { args: any }, | 187 | 187 | ) => Promise<void> | | 188 | + "shell.env"?: (input: { cwd: string }, output: { env: Record<string, string> }) => Promise<void> | 188 | 189 | "tool.execute.after"?: ( | 189 | 190 | input: { tool: string; sessionID: string; callID: string }, | 190 | 191 | output: { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -192,6 +192,10 @@ Plugins can subscribe to events as seen below in the Examples section. Here is a | 192 | 192 |
| 193 | 193 | - `todo.updated` | 194 | 194 |
| | 195 | +#### Shell Events | | 196 | + | | 197 | +- `shell.env` | | 198 | + | 195 | 199 | #### Tool Events | 196 | 200 |
| 197 | 201 | - `tool.execute.after` | @@ -254,6 +258,23 @@ export const EnvProtection = async ({ project, client, $, directory, worktree }) | 254 | 258 |
| 255 | 259 | --- | 256 | 260 |
| | 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 | + | 257 | 278 | ### Custom tools | 258 | 279 |
| 259 | 280 | Plugins can also add custom tools to opencode: | |
|