opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[Feature] Allow the user to run their favorite shell in opencode

Open veracioux opened this issue 1 year ago • 7 comments

How about we allow ! commands to run the user's preferred shell in interactive mode?

Idea demo

https://github.com/user-attachments/assets/84875c76-9419-464c-a350-1587cc21070a

Brainstorm:

  1. Introduce a config variable called shell or similar, which the user can set to any shell, e.g. bash, sh, fish
  2. When the user types ! we start that shell (spawn a pty and attach that shell process to it)
  3. The user types their commandline in that shell, presses enter, and the shell will execute the command
  4. We detect when the shell has finished running the command and kill it (see implementation idea below)
  5. The whole output is available to the LLM

Benefits:

  • The user can have their *rc files applied for custom prompts etc
  • The user can benefit from their shell's completions
  • OC can benefit from the user's shell history
  • I think this might be a panacea of a lot of potential feature requests (existing or future)

Bonus ideas:

  • For some commands we might be able to detect if they are TUI-like commands and for those we can make sure they are displayed over the entire terminal window instead of just the small prompt box. I'm thinking fzf.
  • We can even try to detect the PWD from the shell and when they cd in the shell, also change the PWD of the OC session

Implementation idea for item no. 4

The easiest way I can think of is this:

  • Introduce a command like opencode shell prompt-start or opencode shell done
  • Instruct the user to modify their shell prompt so that it runs opencode shell prompt-start when the prompt is displayed (or alternatively, run opencode shell done in the shell's post-exec hook if supported (I'm a fish user and there you can use the fish_postexec hook)
  • That command signals to the parent OC process that the shell command is done and opencode kills the shell

See also

This is heavily inspired by vterm.el and its handling of the shell. vterm.el (via libvterm) tracks the shell prompt and PWD through invisible OSC escape sequences printed by the shell itself, e.g. \e]51;Auser@host:/pwd\e\\. These sequences are injected at each prompt via a small shell function (like vterm_prompt_end) that’s called from the shell’s prompt hook (PROMPT_COMMAND in bash, fish_prompt in fish, etc.).
vterm.el listens for those sequences to precisely detect where the prompt ends and updates Emacs’ default-directory to match the shell’s current working directory.

Issues that could be addressed with this

  • https://github.com/sst/opencode/issues/3212#issuecomment-3412627160

I volunteer to implement this. TBH I'll prolly implement this in my fork in any case, but I'll wait for insights and ideas from the OC team :)

veracioux avatar Oct 16 '25 21:10 veracioux

@veracioux i thought we already do that? If you "!" it reads the $SHELL value doesnt it

rekram1-node avatar Oct 16 '25 21:10 rekram1-node

@rekram1-node Not quite what I meant. See the demo I just attached to the description.

veracioux avatar Oct 16 '25 22:10 veracioux

Ahh i see yeah thats cool

rekram1-node avatar Oct 16 '25 22:10 rekram1-node

whoopsie

it was literally your first sentence lmao:

interactive mode?

rekram1-node avatar Oct 16 '25 22:10 rekram1-node

@rekram1-node I added "interactive" after edit - so my fault :D

veracioux avatar Oct 16 '25 22:10 veracioux

Feature Request: stdin Passthrough for Interactive Process Input (e.g., sudo passwords, SSH passphrases, confirmation prompts) I'd like to highlight a specific sub-feature within the broader PTY discussion that I think deserves explicit attention: stdin passthrough during agent-executed commands. Currently, when OpenCode runs a command that mid-execution requires user input — such as a sudo password prompt, an SSH passphrase, an npm login OTP, or a simple y/n confirmation — the process hangs and times out. There's no mechanism for the agent to detect that a process is waiting for input and hand control back to the user. What the ideal behavior looks like:

Agent runs a command (e.g., sudo apt install ...) The process writes a prompt to the terminal (e.g., [sudo] password for user:) OpenCode detects the process is waiting for stdin It surfaces the prompt to the user and accepts masked/raw input The input is piped back to the running process and execution resumes

Precedents in other CLIs:

Gemini CLI implements this via node-pty + enableInteractiveShell, with a Ctrl+F to focus the interactive session OpenAI Codex CLI handles this with a dedicated write_stdin tool that feeds keystrokes to a running PTY session

I also noticed that v1.2.21 already includes a fix(pty): pty session handle leak — which suggests PTY infrastructure is at least partially in the codebase. This might mean the remaining work to expose stdin passthrough is closer than it appears. This isn't just a convenience — it's a blocker for any agentic workflow that touches system administration, authentication, or package management. Would love to see this scoped and tracked explicitly, even if it lands after the broader PTY work.

ArabianCowboy avatar Mar 22 '26 17:03 ArabianCowboy

To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

github-actions[bot] avatar May 22 '26 03:05 github-actions[bot]