opencode-cli-dispatch
v1.1.1
Published
OpenCode plugin for delegating conversations to external CLI agents (Claude, Codex, etc.)
Maintainers
Readme
opencode-cli-dispatch
An OpenCode plugin that delegates a conversation to an external CLI coding agent (Claude Code, Codex, or any other CLI agent you configure) and streams its response back into the OpenCode chat. Delegation is sticky on a best-effort basis: once started, follow-up messages in the session keep being routed to the same delegate until you explicitly exit — but this relies on the model calling the delegate's reply tool, which no hook can force (see Known limitations). An explicit /<delegate> <message> command is the reliable way to reach the delegate.
Quickstart
# 1. install the plugin in your opencode project (npm install once published, or use the local tarball for now)
bun add opencode-cli-dispatch
# bun add -g opencode-cli-dispatch # if you want it available everywhere// 2. register it — opencode.json (project) or ~/.config/opencode/opencode.json (global)
{
"plugin": ["opencode-cli-dispatch"]
}3. in opencode: /claude hello # or /codex helloSlash commands (/<delegate>, /cc, /opencode) are written to ~/.config/opencode/commands/ automatically on plugin load.
Stuck? Run npx opencode-cli-dispatch doctor (or cli-dispatch doctor --fix) — it checks registration, config, binaries, authentication, writability, and slash commands, and tells you exactly what to fix.
After installing or updating
opencode loads plugins once at startup. After installing or updating
opencode-cli-dispatch (or regenerating slash commands), quit and restart
opencode. Sessions started before an update won't have the
claude_start / codex_start tools — /cc and /codex detect this and
tell you to restart.
Full docs: Installation · Configuration · Claude Code adapter · Codex adapter
Features
- Multi-delegate: any number of CLI agents can be configured side by side (ships with
claudeandcodexpresets). - Best-effort sticky routing: after
/ccor/codex, subsequent messages — including plain text and other slash commands — are forwarded to the active delegate until/opencodeis run. This depends on model compliance; a model answering directly is outside the plugin's control. - Session resume: each delegate keeps its own external session id, so follow-ups resume the underlying CLI session (
--resume/exec resume) instead of starting fresh. - Auto-generated commands:
/{name},/{name}_reply-driving slash commands are generated per configured delegate, plus a shared/opencodeexit command. - Change summaries: each delegate turn diffs the git worktree before/after the run and appends a short summary (
git diff --stat+ new untracked files) to the response. - Health checks: a
{name}_checktool spawns the delegate in an isolated temp directory to verify it can actually write files with the configured permission flags, without touching your workspace. - Restrictive-agent guard: warns instead of silently misbehaving when the active OpenCode agent (e.g.
plan) injects a system prompt that blocks tool calls, which would otherwise make the delegate silently fail.
How it works
Each configured delegate (e.g. claude, codex) gets three tools generated at plugin load time:
| Tool | Purpose |
|---|---|
| {name}_start | Starts a new CLI session with a prompt, spawns the binary, and records the external session id. |
| {name}_reply | Resumes the active session for this conversation with a follow-up prompt. |
| {name}_check | Spawns the delegate in an isolated temp dir and confirms it can write files (permission/sandbox sanity check). |
Hooks wire this into OpenCode's chat loop:
experimental.chat.system.transforminjects a routing rule into the system prompt while a delegation is active, so the model calls{name}_replyinstead of answering directly.chat.messagetracks the current OpenCode agent per session (used for the restrictive-agent guard) and cleans up injected@mentionboilerplate text.command.execute.beforeintercepts/opencodeto deterministically clear the active delegation for the session.eventreacts tosession.idle: if a turn during an active delegation called none of the configured delegate tools (and wasn't a user-initiated abort), it disconnects the delegation and posts a visible notice instead of leaving it silently active — see Known limitations.
Active delegate state (which delegate, which external session id, which OpenCode agent) is kept in an in-memory session store, keyed by OpenCode session id.
Installation
See docs/installation.md for building, packaging, and per-project or global install options.
Configuration
See docs/configuration.md for delegate configuration, verified models, permissions, and known limitations.
Claude Code adapter
See docs/claude-code-adapter.md for using the adapter with Claude Code as the host.
Codex host adapter
Codex can also act as a host, delegating to claude, codex, or any other configured CLI agent with the same sticky-routing/verified-models/prompt-sanitization contract — implemented on Codex's MCP server + hooks + custom prompts.
Setup:
cli-dispatch codex setup # install/update global Codex config
cli-dispatch codex doctor # verify installation
cli-dispatch codex uninstall # remove generated artifactsThen in Codex, use /prompts:claude <message> to delegate, /prompts:opencode to exit. Sticky routing is best-effort, same as OpenCode.
See docs/codex-adapter.md for configuration, session-id discovery, and security details.
Development
bun install
bun run build # tsc -> dist/
bun run dev # tsc --watch
bun test # bun test, see src/__tests__Source layout:
| File | Responsibility |
|---|---|
| src/index.ts | Plugin entry point; wires config, tools, hooks together. |
| src/config.ts | Config loading/validation and {placeholder} argv resolution. |
| src/delegate-tools.ts | {name}_start / {name}_reply tool implementations, git-diff change summaries. |
| src/health-check.ts | {name}_check tool — isolated writability probe. |
| src/hooks.ts | System-prompt routing injection, agent tracking, /opencode exit handling, session-idle routing-violation detection. |
| src/commands.ts | Generates the /{name} and /opencode markdown command files. |
| src/routing-rule.ts | Builds the sticky-routing instruction injected into the system prompt. |
| src/run-delegate.ts | Spawns the delegate binary and streams stdout to a parser. |
| src/parse-events.ts | Per-CLI stdout event parsers (claude, codex, opencode, raw). |
| src/session-store.ts | In-memory per-OpenCode-session delegate/agent state. |
| src/claude-code-adapter/ | Claude Code host adapter: MCP server, PreToolUse/UserPromptSubmit hooks, file-backed session store. |
| src/codex-adapter/ | Codex host adapter: MCP server, UserPromptSubmit/PreToolUse/SessionEnd hooks, generated prompts, cli-dispatch codex setup CLI. |
License
MIT
