Extensibility: Plugin System
Relevant source files
The following files were used as context for generating this wiki page:
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/codex.txt
- packages/opencode/src/agent/generate.txt
- packages/opencode/AGENTS.md
- packages/opencode/src/command/template/initialize.txt
- packages/opencode/src/command/template/review.txt
- packages/opencode/src/session/prompt/beast.txt
The OpenCode extensibility system is centered around AI agents that can perform a variety of software engineering tasks. These agents are governed by detailed system prompts and can be customized on a per-repository basis using AGENTS.md files. This allows for a flexible and powerful way to extend OpenCode's core functionality with specialized behaviors, tools, and workflows tailored to specific projects.
The system is designed to be autonomous, with agents capable of understanding requests, planning, implementing solutions, and verifying their work. Extensibility is achieved through the creation of new agent personas and instructions, as well as providing repository-specific guidelines that agents must follow.
Agents as Plugins
The primary mechanism for extensibility is the AI agent. An agent is an entity with a specific persona, a set of instructions (system prompt), and access to a suite of tools to interact with the user's environment. Different agents can be designed for different tasks, such as fixing bugs, adding features, refactoring code, or even creating other agents.
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt
Core Mandates
All agents operate under a set of core mandates that ensure safety, consistency, and efficiency.
| Mandate | Description |
|---|---|
| Conventions | Rigorously adhere to existing project conventions found in code, tests, and configuration. |
| Libraries/Frameworks | Never assume a library is available. Verify its usage in the project first. |
| Style & Structure | Mimic the style, formatting, naming, and architecture of the existing codebase. |
| Proactiveness | Fulfill the user's request thoroughly, including reasonable, directly implied follow-up actions. |
| Confirm Ambiguity | Do not take significant actions beyond the clear scope of a request without user confirmation. |
| Path Construction | Always construct full absolute paths for file system tool arguments. |
| Security First | Never introduce code that exposes, logs, or commits secrets or other sensitive information. |
Sources: packages/opencode/src/session/prompt/gemini.txt:5-30
Agent Workflows
Agents follow structured workflows to handle user requests. The two primary workflows are for general software engineering tasks and creating new applications.
Software Engineering Task Workflow
This workflow is used for tasks like fixing bugs, adding features, or refactoring.
This diagram illustrates the five-step process an agent follows for software engineering tasks, from understanding the request to final verification.
Sources: packages/opencode/src/session/prompt/gemini.txt:33-54
New Application Workflow
This workflow is a more autonomous process for scaffolding and building a complete prototype from a user's high-level request. It involves proposing a plan, getting user approval, and then implementing the full application.
Sources: packages/opencode/src/session/prompt/gemini.txt:56-85
AGENTS.md for Repository-Specific Instructions
A key feature of the extensibility system is the AGENTS.md file. This file allows developers to provide specific instructions, conventions, and commands for agents operating within a repository.
The initialize command can be used to generate a baseline AGENTS.md file by analyzing the codebase for build/lint/test commands and code style guidelines.
Sources: packages/opencode/src/command/template/initialize.txt, packages/opencode/AGENTS.md
AGENTS.md Scoping and Precedence
The rules for how AGENTS.md files are applied are clearly defined:
- Scope: An
AGENTS.mdfile's instructions apply to the entire directory tree where it is located. - Precedence: More-deeply-nested
AGENTS.mdfiles take precedence over those in parent directories. - Overrides: Direct user instructions in a prompt override any conflicting instructions from an
AGENTS.mdfile.
The following diagram shows how AGENTS.md files in different directories would apply.
This diagram shows that root/AGENTS.md applies to all files, but root/src/AGENTS.md provides more specific rules for files within the src directory, overriding the root-level rules for those files.
Sources: packages/opencode/src/session/prompt/codex.txt:20-33
Agent Generation
The system is meta-extensible, meaning it includes an agent designed specifically to create other agents. This "agent architect" takes a user's high-level requirements and generates a complete agent configuration in a structured JSON format.
Sources: packages/opencode/src/agent/generate.txt
Generation Process
This sequence shows the process where a user's request is transformed into a structured JSON agent configuration by the agent architect.
Sources: packages/opencode/src/agent/generate.txt:6-31
Agent Configuration Schema
The generated agent configuration must be a valid JSON object with a specific structure.
| Field | Description |
|---|---|
identifier | A unique, descriptive identifier using lowercase letters, numbers, and hyphens (e.g., 'code-reviewer'). |
whenToUse | A precise description of when to use the agent, including examples. It must start with "Use this agent when...". |
systemPrompt | The complete system prompt governing the agent's behavior, written in the second person ("You are..."). |
Sources: packages/opencode/src/agent/generate.txt:54-59
Here is an example of the JSON structure:
{
"identifier": "A unique, descriptive identifier using lowercase letters, numbers, and hyphens (e.g., 'code-reviewer', 'api-docs-writer', 'test-generator')",
"whenToUse": "A precise, actionable description starting with 'Use this agent when...' that clearly defines the triggering conditions and use cases. Ensure you include examples as described above.",
"systemPrompt": "The complete system prompt that will govern the agent's behavior, written in second person ('You are...', 'You will...') and structured for maximum clarity and effectiveness"
}
Sources: packages/opencode/src/agent/generate.txt:54-59
Available Tools
Agents are provided with a set of tools to interact with the file system and execute commands. These tools are the agent's interface to the user's environment.
- File System & Search:
grep,glob,read,write,edit - Execution:
bashfor running shell commands. - Planning:
todowritefor creating and managing step-by-step plans. - Web Research:
webfetchfor gathering information from URLs, including Google searches.
When using the bash tool for commands that modify the system, the agent is required to first explain the command's purpose and potential impact to the user.
Sources: packages/opencode/src/session/prompt/gemini.txt:100-112, packages/opencode/src/session/prompt/codex.txt:190-196, packages/opencode/src/session/prompt/beast.txt:46-55
Example Agent: Code Reviewer
The review command template defines a specialized "code reviewer" agent. This agent demonstrates how a specific persona and set of instructions can be combined to create a powerful, focused tool.
The reviewer agent can analyze:
- Uncommitted changes (
git diff) - A specific commit (
git show <hash>) - A branch comparison (
git diff <branch>...HEAD) - A pull request (
gh pr diff <url_or_number>)
It is instructed to focus on bugs, structural integrity, and obvious performance issues, while avoiding flagging stylistic preferences.