OpenCode CLI¶
CLI tool for inspecting and managing an OpenCode database. Connects to the local OpenCode SQLite database (~/.local/share/opencode/opencode.db) to list sessions and projects, and to delete sessions or projects. Also supports API key rotation from a local keys file.
Usage¶
Aliases¶
This command is available under several aliases (all invoke the same CLI):
opc(default)opencode-cli
Commands¶
list session¶
Lists all sessions in a table format showing slug, title, and version.
list project¶
Lists all projects in a table format showing truncated ID, name, and worktree path.
delete session <id>¶
Deletes a single session and all its descendant sessions by ID.
delete sessions¶
Deletes all sessions from the database (irreversible).
delete project <id>¶
Deletes a project's sessions and project record from the OpenCode database (not the real project folder on disk).
The <id> is the project UUID shown in the first column of opc list project. Run that command first to find the project ID you want to delete.
auth rotate¶
Rotates the active OpenCode API key by picking the first working key from a local keys file. Iterates through available keys (excluding the current one) and tests each against the OpenCode API, selecting the first that returns a valid response.
Use --proxy to route API requests through a proxy server:
Options¶
| Flag | Description |
|---|---|
-h, --help |
Show help message |
-p, --proxy <url> |
Proxy URL for API requests. Supports http://, https://, socks4://, socks5:// protocols. Format: protocol://ip:port or protocol://user:pass@ip:port |
Examples¶
List all sessions:
Delete a specific session:
Delete a project's sessions and project record from the OpenCode database (ID obtained from opc list project):
Rotate to a working API key:
Rotate through a SOCKS5 proxy:
How it works¶
- Database check: On every command except
--help, the tool first verifies the OpenCode database is accessible viacheckDatabase(). - List: Queries the
sessionorprojecttable and displays results in formatted tables with dynamic column widths. - Delete: Uses recursive SQL (Common Table Expressions) to delete sessions and their descendants, or cascading deletes for project sessions.
- Auth rotate: Reads
opencode.keysfrom the project config (viagetConfig()), filters out the currently active key, tests each remaining key viacheckOpenCodeApi(), and sets the first one that responds successfully. If--proxyis provided, it's passed through tocheckOpenCodeApi()which creates aProxyAgentfor the HTTP/SOCKS proxy.
Requirements¶
For auth rotate, add an opencode.keys array to your project config. Create a binary-collections.config.{js,cjs,mjs} file:
// binary-collections.config.js (ESM) or .cjs (CJS)
export default {
opencode: {
keys: [
{ name: 'primary', key: 'sk-xxxxxx' },
{ name: 'backup', key: 'sk-yyyyyy' }
]
}
};
Source¶
See src/opencode-cli.ts.