-
Notifications
You must be signed in to change notification settings - Fork 0
Architecture
eddy.wijaya edited this page Apr 3, 2026
·
1 revision
Component-Service architecture with clean data flow from auth to display.
opencode-codex-quota/
βββ package.json
βββ tsconfig.json
βββ biome.json
βββ src/
β βββ index.ts # Plugin entry point, tool definition
β βββ types.ts # All TypeScript interfaces + DisplayMode
β βββ services/
β β βββ auth-reader.ts # Read auth.json, parse JWT, extract account_id + email
β β βββ api-client.ts # Call wham/usage endpoint, return typed QuotaResponse
β βββ formatter/
β βββ markdown.ts # Transform QuotaResponse β raw Markdown string
β βββ errors.ts # Error codes β Markdown error messages
βββ tests/
βββ auth-reader.test.ts
βββ api-client.test.ts
βββ markdown.test.ts
βββ errors.test.ts
βββ index.test.ts
βββ integration.test.ts
User/Agent β /codex_quota [mode?]
β
ββ Slash command path:
β ββ command.execute.before hook
β ββ Injects wrapper instruction with mode argument
β ββ LLM calls codex_quota tool
β
ββ Direct tool path:
ββ Plugin tool execute()
β
ββ 1. AuthReader.read()
β ββ Read auth.json
β ββ Find OAuth entry (codex β openai β chatgpt β opencode)
β ββ Parse JWT β { token, accountId, email }
β
ββ 2. ApiClient.query(token, accountId)
β ββ GET chatgpt.com/backend-api/wham/usage
β ββ Validate response β QuotaResponse
β
ββ 3. Formatter.format(response, mode)
ββ Build Markdown sections
ββ Conditional includes
ββ Raw Markdown string β OpenCode TUI (Glamour)
Reads ~/.local/share/opencode/auth.json, finds OAuth credentials, parses JWT.
| Function | Purpose |
|---|---|
readAuth(path?) |
Read auth.json, find valid provider, return AuthResult
|
parseJwt(token) |
Decode JWT payload, extract chatgpt_account_id + email
|
Provider key priority: codex β openai β chatgpt β opencode (first-match-wins)
Calls the ChatGPT quota API with OAuth token.
| Function | Purpose |
|---|---|
queryQuota(token, accountId) |
GET wham/usage, validate schema, return ApiResult
|
Timeout: 10 seconds (AbortController)
Transforms typed API response into display Markdown.
| Function | Purpose |
|---|---|
formatQuota(response, mode) |
Main entry β routes to compact or full formatter |
buildProgressBar(percent) |
12-char progress bar with clamping |
formatResetClock(timestamp) |
Local clock string (same-day or with date) |
Maps error codes to user-friendly Markdown messages.
| Function | Purpose |
|---|---|
formatError(code, partialData?) |
E1βE11 β formatted Markdown error |
export default {
id: "opencode-codex-quota",
server: codexQuotaServer,
} satisfies PluginModule| Hook | Purpose |
|---|---|
config |
Registers /codex_quota command with template: "" and subtask: true
|
command.execute.before |
Reads input.arguments, injects wrapper instruction with mode |
tool.codex_quota |
The actual quota tool β accepts optional mode arg |
User types: /codex_quota compact
β
command.execute.before hook fires
β
Reads arguments β "compact"
β
Resolves mode β "compact"
β
Injects instruction: "Call the codex_quota tool now with mode=compact..."
β
LLM receives instruction, calls tool
β
Tool returns Markdown β TUI renders
| Aspect | Detail |
|---|---|
| Language | TypeScript (strict) |
| Runtime | Node.js >= 18 |
| Plugin SDK |
@opencode-ai/plugin (Zod-based tool.schema) |
| Test Framework | Vitest |
| Linter | Biome |
| Build |
tsc β dist/
|
| Dependencies | Zero runtime β only @opencode-ai/plugin (peer) |
- Usage β See the plugin in action
- Technical-Reference β Detailed type definitions and API spec
- Development β Set up for local development