[PR #12012] feat(plugin): add shell.env hook for manipulating environment in tools and shell #14028

Closed
opened 2026-02-16 18:18:50 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12012

State: closed
Merged: Yes


The motivation for this PR is the desire to run a single opencode server that can run against an arbitrary number of projects, and provide a different environment to tools run within each. At present this can be done by informing the agent that it should, e.g., load .env or wrap calls in a doppler utility, but this is error prone and adds more things for the LLM to think about.

FWIW I'm shy about proposing a change to your plugin system so not at all concerned if this should go through some design critique from you guys before it totally fits your expectations.

What does this PR do?

Introduces a shell.env plugin hook that allows a plugin to introduce environment variables that will be added to the env for (1) llm tool calls, (2) shell mode !command, and (3) PTY terminals.

I purposely avoided hooking into instance-level caching for three reasons:

  • Preference for not making secrets into part of opencode's application state
  • Preference for leaving whether or not to cache values up to the plugin implementation
  • Preference for minimizing LOC touched on this PR

How did you verify your code works?

  1. Ran the pre-commit hooks
  2. Created a temporary / bs tool that adds a single shell.env and ran bun dev .
export const TestShellEnvPlugin = async () => {
  return {
    "shell.env": async (input, output) => {
      output.env.PLUGIN_TEST_VAR = "it_works_456"
    },
  }
}

The screenshot below confirms that LLM tool calls contain the injected variable and so do !command commands

Screenshot 2026-02-03 at 2 40 26 PM

Closes #12018

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12012 **State:** closed **Merged:** Yes --- The motivation for this PR is the desire to run a single opencode server that can run against an arbitrary number of projects, and provide a different environment to tools run within each. At present this can be done by informing the agent that it should, e.g., load .env or wrap calls in a doppler utility, but this is error prone and adds more things for the LLM to think about. FWIW I'm shy about proposing a change to your plugin system so not at all concerned if this should go through some design critique from you guys before it totally fits your expectations. ### What does this PR do? Introduces a `shell.env` plugin hook that allows a plugin to introduce environment variables that will be added to the env for (1) llm tool calls, (2) shell mode `!command`, and (3) PTY terminals. I purposely avoided hooking into instance-level caching for three reasons: - Preference for not making secrets into part of opencode's application state - Preference for leaving _whether or not to cache values_ up to the plugin implementation - Preference for minimizing LOC touched on this PR ### How did you verify your code works? 1. Ran the pre-commit hooks 2. Created a temporary / bs tool that adds a single `shell.env` and ran `bun dev .` ``` export const TestShellEnvPlugin = async () => { return { "shell.env": async (input, output) => { output.env.PLUGIN_TEST_VAR = "it_works_456" }, } } ``` The screenshot below confirms that LLM tool calls contain the injected variable and so do `!command` commands <img width="590" height="457" alt="Screenshot 2026-02-03 at 2 40 26 PM" src="https://github.com/user-attachments/assets/7d0702cf-4d08-44a2-b3d6-7cc46639d18b" /> Closes #12018
yindo added the pull-request label 2026-02-16 18:18:50 -05:00
yindo closed this issue 2026-02-16 18:18:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14028