Centralized tool factory and registry for the OpenCode plugin system. This directory defines all executable tools exposed to OpenCode agents, including:
These tools enable agents to perform file operations, orchestrate multi-model consensus, manage background tasks, and interact with external systems while maintaining security boundaries through the OpenCode tool schema.
Each tool is implemented as a factory function that returns a ToolDefinition record compatible with the @opencode-ai/plugin SDK. The pattern provides:
src/tools/index.ts| Tool Family | Purpose | Key Components |
|---|---|---|
| Council | Multi-LLM consensus synthesis (orchestrator dispatches councillors as subagents) | agents/council.ts, agents/index.ts |
| Task Management | Background task lifecycle and HITL continuation control | cancel-task.ts, wait-for-user.ts, background-job-board.ts |
| ACP Integration | External agent protocol execution | acp-run.ts, ACP client implementation |
| Code Intelligence | AST-based code manipulation | ast-grep/ directory, tools.ts |
| Web Fetching | Intelligent web content retrieval | smartfetch/ directory, tool.ts |
| Preset Management | Runtime agent configuration | preset-manager.ts, TUI state integration |
ctx.ask()runtime-preset.ts1. Plugin Initialization (src/index.ts)
└─> registerTools() calls each tool factory with dependencies
2. Tool Factory Execution
├─> Accepts PluginInput context and domain-specific dependencies
├─> Validates configuration and environment
├─> Returns ToolDefinition record with execute() handler
└─> Registers tool with OpenCode via plugin API
3. Tool Invocation
├─> Agent calls tool with validated arguments
├─> Tool executes business logic
├─> May call ctx.ask() for user permission
├─> Returns structured result or error
└─> OpenCode presents result to agent
1. Orchestrator invokes cancel_task tool
├─> Validates calling agent is 'orchestrator'
├─> Resolves task_id to BackgroundJobBoard entry
├─> Calls abortSessionWithTimeout() to signal cancellation
├─> Verifies session stopped via status polling
├─> Marks job as cancelled in BackgroundJobBoard
└─> Returns cancellation confirmation
1. Orchestrator gives the user concrete manual steps
└─> Invokes wait_for_user as its final tool action
├─> Validates session ID, agent identity, and managed-session ownership
├─> Arms task-session-manager.beginUserWait()
├─> Revokes pending automatic-continuation reservations
└─> Returns the versioned waiting_for_user protocol marker
1. Agent invokes acp_run tool
├─> Validates calling agent matches configured agent name
├─> Spawns ACP client process with config
├─> Sends prompt via JSON-RPC over stdin/stdout
├─> Handles permission requests via ctx.ask()
├─> Collects streaming output chunks
├─> Enforces timeout if configured
└─> Returns concatenated output or error
1. Agent invokes ast_grep_search or ast_grep_replace
├─> Validates language support and pattern syntax
├─> Ensures CLI binary available (downloads if needed)
├─> Executes sg (AST-grep CLI) process
├─> Parses JSON output into structured matches/edits
└─> Returns typed results to agent
1. Agent invokes webfetch tool
├─> Validates URL and configuration
├─> Checks cache for fresh content
├─> If cache miss, fetches via network with timeout
├─> Optionally processes with secondary model
├─> Caches result for future requests
└─> Returns extracted content to agent
Main Plugin (src/index.ts):
registerTools() - Registers all exported tools with OpenCodegetToolDefinitions() - Composes tool set for plugin initialization
Agents (src/agents/):
acp_run tool for specialized tasksast_grep_search/ast_grep_replace for code manipulationCLI (src/cli/):
/preset command| Dependency | Purpose |
|---|---|
@opencode-ai/plugin |
Tool schema and execution framework |
Council Config (src/config/council-schema.ts) |
Councillor model/preset definitions |
BackgroundJobBoard (src/utils/) |
Background task tracking and cleanup |
Config System (src/config/) |
ACP agent configurations and presets |
TUI State (src/tui-state.ts) |
Preset visualization in terminal UI |
AST-grep CLI |
Pattern matching and transformation engine |
Network Utilities |
Web fetching and caching |
Tools Layer → Background Layer
├─ cancel_task tool → BackgroundJobBoard.resolve() → abortSessionWithTimeout()
└─> Returns cancellation status
Tools Layer → Config Layer
├─ acp_run tool → AcpAgentsConfig from config system
├─ preset-manager → Preset configurations from plugin config
└─> Validates and applies runtime configuration
Tools Layer → AST-grep Layer
├─ ast_grep_search/ast_grep_replace → CLI binary execution
└─> Returns typed AST matches and edit results
Tools Layer → Web Layer
└─ webfetch tool → Network utilities with caching and model processing
src/config/agents.ts, consumed by acp_run.tsoh-my-opencode-slim.jsonc), managed by preset-manager.tscouncil.tscancel-task.ts implements robust abort verification with polling and cleanupctx.ask()ast-grep/ tools auto-download CLI on first use// AST-grep tools
export { createAcpRunTool } from './acp-run';
export { ast_grep_replace, ast_grep_search } from './ast-grep';
// Task management
export { createCancelTaskTool } from './cancel-task';
export { createWaitForUserTool } from './wait-for-user';
// Preset management
export type { PresetManager } from './preset-manager';
export { createPresetManager } from './preset-manager';
// Web fetching
export { createWebfetchTool } from './smartfetch';
src/config/agents.ts as AcpAgentsConfigcommand, args, cwd, permissionModeask (prompt user), reject (auto-deny), allow (auto-approve)councillor-<name>)presets fieldAgentOverrideConfig*.test.ts files