OpenCode Use Cases
OpenCode is more than just a chatbot; it's a scripting engine driven by language models. Here are common patterns used by the community.
1. The Legacy Refactor
Scenario: You have a 500-line Python file that needs to be broken into modules. Why OpenCode: It understands file structure and can move contents while updating imports.
opencode start "Take main.py. Split the database logic into db.py and the utils into utils.py. Update all imports."
- Advantage: OpenCode will run
pytestafter the move (if you ask) to ensure it didn't break anything.
2. Test Driven Development (TDD) Loop
Scenario: You want to implement a new feature rigorously. Workflow:
- Writer a failing test file
feature.test.ts. - Run the agent.
opencode run "Run npm test. Read the failure. Implement the code in feature.ts to make the test pass."
- Advantage: The agent gets immediate feedback from the CLI output provided by the test runner.
3. Documentation "Gardener"
Scenario: Your README.md is out of date with the actual API.
Why OpenCode: It acts as a reader that can diff code vs docs.
opencode run "Read all exported functions in src/api. Update docs/API.md to match the current signatures."
4. The "Translator" (Language Migration)
Scenario: Porting a React Class component to a Hook.
opencode run "Read Component.jsx. Rewrite it as a Functional Component using Hooks. Keep the same prop types."
5. Security Audit (Local)
Scenario: Checking for hardcoded secrets before commit. Why OpenCode: Using a local model (Ollama) means you aren't sending your potential secrets to OpenAI to be checked.
# Using local Llama 3
opencode run "Scan src/ for any strings that look like API keys. Report line numbers."
6. MCP Power User: Database Chat
With the Postgres MCP Server configured:
- "Show me the schema for the 'users' table."
- "Write a migration to add a 'last_login' column."
- "Generate a seed script with 50 fake users."
The agent queries the DB to understand the types before writing code.
Next Steps
- Ready to try these? Install OpenCode.
- Need to connect to a DB? Read MCP Explained.