Example Code
The complete working example - your own OpenCode in Python.
Why Build Your Own OpenCode?
OpenCode is an open-source AI coding agent with 86k+ GitHub stars. It uses:- str_replace editing - Precise text replacement (not diff patches)
- Filesystem exploration - read, grep, glob, list tools
- Dual agents - “build” (full access) and “plan” (read-only)
How OpenCode Tools Map to HUD
| OpenCode Tool | HUD Implementation | Description |
|---|---|---|
bash | hud.tools.coding.ShellTool | Execute shell commands |
edit | hud.tools.coding.EditTool | str_replace file editing |
write | EditTool.create | Create new files |
read | hud.tools.filesystem.ReadTool | Read file contents |
grep | hud.tools.filesystem.GrepTool | Search file contents |
glob | hud.tools.filesystem.GlobTool | Find files by pattern |
list | hud.tools.filesystem.ListTool | List directory contents |
BaseTool subclasses - just env.add_tool() them.
Build Your OpenCode
Complete Local Agent
Plan Mode (Read-Only Agent)
OpenCode’s “plan” agent is read-only for safe codebase exploration. Just omit the coding tools:Tool Specifications
Edit Tool (str_replace)
TheEditTool matches OpenCode’s edit tool with these commands:
| Command | Description |
|---|---|
view | View file contents with line numbers |
create | Create a new file |
str_replace | Replace exact text with new text |
insert | Insert text at a specific line |
undo_edit | Undo the last edit |
Shell Tool
Persistent bash session with:- Auto-restart on errors
- Dynamic timeout support
- Output capture (stdout/stderr)
CLI Usage
Comparison with OpenCode
| Feature | OpenCode | Your HUD OpenCode |
|---|---|---|
| str_replace editing | edit tool | EditTool (same spec) |
| Shell execution | bash tool | ShellTool/BashTool |
| File reading | read tool | ReadTool |
| Regex search | grep tool | GrepTool |
| File finding | glob tool | GlobTool |
| Directory listing | list tool | ListTool |
| Build/Plan agents | Built-in | Create separate environments |
| LSP integration | Experimental | Not yet (use shell + lsp commands) |
| Observability | Internal logs | Full traces on hud.ai |
| Evaluation | Manual | Built-in with hud.eval |
See Also
- OpenCode - The original open-source coding agent
- Build Your Own Codex - OpenAI Codex-style agent with V4A diffs
- Tools Reference - Complete tool documentation
- Agents Reference - Agent configuration options