OpenCode Integration
OpenCode integration uses a TypeScript plugin that shells out to hcom for lifecycle management and message delivery.Message Delivery
Automatic: Messages are delivered via plugin polling when agent goes idle. How it works:- Plugin registers TCP notify endpoint
- When messages arrive, hcom sends TCP wake to plugin
- Plugin calls
hcom opencode-read --format - Messages injected into OpenCode context
Plugin Installation
OpenCode uses a TypeScript plugin instead of config file hooks.Automatic Install
Run OpenCode via hcom launcher:~/.config/opencode/plugins/hcom.ts.
Manual Install
If you prefer to run OpenCode directly:Verify Installation
Launch Modes
PTY Mode (Recommended)
Launch with hcom for full integration:- Automatic message delivery via plugin
- Terminal screen capture (
hcom term) - Automatic cleanup on kill (
hcom kill) - Fork and resume support
Vanilla Mode
OpenCode does not support vanilla mode without the plugin. The plugin is required for all hcom integration.Hook Types
OpenCode uses 4 hook commands (not file-based hooks):| Hook | When | Purpose |
|---|---|---|
| opencode-start | Session created | Bind session, return bootstrap |
| opencode-status | Status/idle events | Update instance status |
| opencode-read | Plugin polls | Fetch pending messages |
| opencode-stop | Session deleted | Finalize session |
Plugin Architecture
Plugin File
Location:~/.config/opencode/plugins/hcom.ts
Embedded source: Compiled into hcom binary, written on install.
Plugin structure:
TCP Notify Endpoint
Plugin registers a TCP server for wake notifications:- Plugin starts TCP server on random port
- Passes port to
opencode-start --notify-port - hcom stores endpoint in database
- When messages arrive, hcom sends TCP wake
- Plugin’s
deliverPendingToIdle()fires
- Plugin can’t hook into hcom’s event loop
- Polling would be inefficient
- TCP wake provides instant notification
Message Polling
Plugin usesopencode-read command:
Configuration
Default Arguments
System Prompt
OpenCode does not support system prompts via environment. Use OpenCode’s native system prompt configuration.Session Binding
PTY Mode
Binding happens atopencode-start:
- Plugin fires
onSessionCreatedevent - Calls
hcom opencode-start --session-id <id> --notify-port <port> - hcom binds session to process via
HCOM_PROCESS_ID - Returns JSON:
{"name": "luna", "bootstrap": "..."} - Plugin injects bootstrap into session
Re-binding (Compaction/Resume)
If session already bound (after compaction):opencode-startdetects existing binding- Re-injects bootstrap
- Preserves identity across restarts
Launch Examples
Single Agent
Multiple Agents
Fork and Resume
OpenCode Database Path
OpenCode stores sessions in SQLite database: Location:$XDG_DATA_HOME/opencode/opencode.db
Default: ~/.local/share/opencode/opencode.db
hcom stores this as transcript_path for consistency with other tools.
Message Delivery Flow
- Message arrives: Another agent sends message
- Event logged: Message stored in hcom database
- Notify endpoints: hcom sends TCP wake to plugin
- Plugin wakes:
deliverPendingToIdle()fires - Check messages: Plugin calls
opencode-read --check - Fetch messages: Plugin calls
opencode-read --format - Inject: Plugin adds messages to session context
- Acknowledge: Plugin calls
opencode-read --ack
Advanced
Plugin Update
Plugin is embedded in hcom binary. When hcom updates:ensure_plugin_installed()checks if current- Compares installed vs embedded source
- Re-writes plugin if outdated
- No OpenCode restart needed (auto-reload)
Plugin Scan Directories
Plugin can be in multiple locations:~/.config/opencode/plugin/(legacy)~/.config/opencode/plugins/(current)$OPENCODE_CONFIG_DIR/plugin/$OPENCODE_CONFIG_DIR/plugins/
~/.config/opencode/plugins/) but detects all.
Last Event ID Initialization
On first launch,opencode-start initializes last_event_id:
- Check if
last_event_id == 0(new instance) - Use
HCOM_LAUNCH_EVENT_IDenv var if set - Otherwise use current max event ID
- Prevents historical messages from flooding new instance
- Prevents delivering all historical messages
- Preserves message history for resume
- Respects launch event boundary
Message Read Modes
opencode-read supports 4 modes:
Default: Raw JSON array (does NOT advance cursor)
Troubleshooting
Plugin not loading
- Check plugin exists:
ls ~/.config/opencode/plugins/hcom.ts - Check OpenCode logs for plugin errors
- Reinstall plugin:
hcom hooks add opencode
Messages not arriving
- Check agent status:
hcom listshowslistening - Verify plugin loaded: Check OpenCode plugin manager
- Check TCP notify:
hcom list <name> --json | grep notify - Try manual send:
hcom send @opencode-instance -- test
TCP notify not working
- Check firewall: TCP wake uses localhost
- Check port in use: Plugin uses random port
- Restart OpenCode session
Plugin outdated
Reference
Plugin location:~/.config/opencode/plugins/hcom.ts
Plugin source: Embedded in hcom binary
Plugin functions: