Core Tools
Relevant source files
The following files were used as context for generating this wiki page:- packages/opencode/src/tool/registry.ts
- packages/opencode/src/tool/bash.ts
- packages/opencode/src/tool/edit.ts
- packages/opencode/src/tool/patch.ts
- packages/opencode/src/tool/websearch.ts
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/codex.txt
- packages/opencode/src/session/prompt/qwen.txt
- README.md
The Core Tools are a suite of functions that provide the OpenCode agent with the fundamental capabilities to interact with a user's development environment. These tools enable the agent to perform a wide range of software engineering tasks, from exploring the codebase and gathering information to executing commands and modifying files. They form the action-oriented layer of the agent, translating its plans into concrete operations on the file system and in the shell.
The agent's primary workflow involves using these tools in a sequence: first, information-gathering tools like glob and read are used to understand the context. Then, a plan is formulated. Finally, modification tools like edit, patch, and bash are used to implement the plan. This structured use of tools ensures that changes are grounded in the existing project conventions and are verified for correctness.
Tool Registry
The ToolRegistry is a central component responsible for managing the lifecycle of all available tools. It allows for the registration, retrieval, and listing of tools that the agent can invoke.
Sources: packages/opencode/src/tool/registry.ts
Architecture
The ToolRegistry class uses a Map to store tool instances, keyed by their unique identifiers. This provides an efficient way to look up and manage tools.
This diagram illustrates the structure of the ToolRegistry class and its methods.
Key Methods
| Method | Description |
|---|---|
register(tool) | Adds a new tool instance to the registry. |
get(name) | Retrieves a tool by its name (identifier). |
all() | Returns an array of all registered tools, formatted for use by the AI model. |
tool(name, description) | A decorator function that registers a class as a tool with a given name and description. |
Sources: packages/opencode/src/tool/registry.ts:11-29
Shell Interaction
The ability to execute shell commands is a critical capability for the agent, enabling it to run builds, tests, linters, and other command-line utilities.
bash Tool
The bash tool is the primary interface for executing shell commands. It is designed with safety and control in mind, providing options for running commands in the background and setting timeouts.
Sources: packages/opencode/src/tool/bash.ts, packages/opencode/src/session/prompt/gemini.txt
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
command | string | The shell command to execute. | (required) |
background | boolean | If true, the command runs as a background process. This is recommended for long-running processes like servers. | false |
timeout | number | The maximum execution time in milliseconds before the process is terminated. | 60000 |
Sources: packages/opencode/src/tool/bash.ts:12-20
Operational Flow and Safety
The agent is explicitly instructed to be cautious when using the bash tool for commands that modify the file system or system state. It must provide a brief explanation of the command's purpose and potential impact before execution to ensure user understanding and safety.
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/qwen.txt
The following diagram shows the execution flow of the bash tool.
File System Operations
The agent uses a set of tools to directly read and modify files within the project workspace. Agent instructions mandate the use of absolute paths for all file operations to ensure clarity and prevent ambiguity.
Sources: packages/opencode/src/session/prompt/gemini.txt
edit Tool
The edit tool performs a search-and-replace operation within a specified file. It is useful for making targeted changes to existing code.
| Parameter | Type | Description |
|---|---|---|
path | string | The absolute path to the file to be edited. |
find | string | The content to search for. |
replace | string | The content to replace the find string with. |
Sources: packages/opencode/src/tool/edit.ts:11-15
patch Tool
The patch tool applies a patch in the unified diff format to a file. This is suitable for applying more complex, multi-line changes generated by the agent.
| Parameter | Type | Description |
|---|---|---|
path | string | The absolute path to the file to be patched. |
patch | string | The patch content in unified diff format. |
Sources: packages/opencode/src/tool/patch.ts:11-14
Information Gathering
Before making any changes, the agent must understand the existing codebase. A set of information-gathering tools facilitates this "Understand" phase of the workflow.
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt
websearch Tool
For information outside the local codebase, the websearch tool provides the ability to query the internet. It uses DuckDuckGo as the search provider.
| Parameter | Type | Description |
|---|---|---|
query | string | The search query. |
Sources: packages/opencode/src/tool/websearch.ts:11-13
Codebase Exploration Tools
While the source files for grep, glob, and read are not provided, their usage is heavily documented in the agent's system prompts, establishing them as core tools for codebase exploration.
| Tool | Purpose |
|---|---|
grep | Searches for text patterns within files to locate specific code snippets, function definitions, or variable usage. |
glob | Finds files and directories matching a specified pattern, helping to understand the project structure. |
read | Reads the content of a file to understand its logic, context, and conventions. |
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt
Agent Task Workflow
The core tools are orchestrated within a structured workflow to address software engineering tasks. The following sequence diagram illustrates a typical interaction for a refactoring task.
This diagram shows how the agent uses various tools to complete a user's request.
Conclusion
The Core Tools are the essential instruments that empower the OpenCode agent to function as a capable software engineering assistant. Through a well-defined registry, specialized tools for shell interaction, file modification, and information gathering, the agent can safely and effectively understand, plan, and execute tasks within a developer's environment. The structured workflow mandated by the agent's prompts ensures these tools are used methodically, prioritizing project conventions and verification.