|
1 | 1 | # PROJECT KNOWLEDGE BASE |
2 | 2 |
|
3 | | -Generated: 2026-04-25 |
4 | | -Source baseline: 3331324cb14d2b80dd8dfb424619870a88476706 |
| 3 | +Generated: 2026-05-03 |
5 | 4 | Branch: main |
| 5 | +Package version: 6.1.8 |
6 | 6 |
|
7 | 7 | ## OVERVIEW |
8 | | -OpenCode plugin: intercepts OpenAI SDK calls, routes through ChatGPT Codex backend with multi-account OAuth rotation. |
| 8 | + |
| 9 | +`oc-codex-multi-auth` is an OpenCode plugin for ChatGPT Plus/Pro OAuth, Codex/GPT-5 request routing, multi-account rotation, account switching, health checks, quota status, diagnostics, and recovery tools. The npm bin is an installer that manages OpenCode provider/TUI config; OpenCode loads `index.ts` as the provider plugin and `tui.ts` as the prompt quota status plugin. Runtime account state stays local under `~/.opencode` with per-project pools enabled by default. |
9 | 10 |
|
10 | 11 | ## STRUCTURE |
11 | | -``` |
| 12 | + |
| 13 | +```text |
12 | 14 | ./ |
13 | | -├── index.ts # plugin entry (context wiring + 5-stage fetch pipeline) |
14 | | -├── lib/ # core logic (see lib/AGENTS.md) |
| 15 | +├── index.ts # OpenCode provider plugin entry: auth loader, fetch pipeline, tool registry context |
| 16 | +├── tui.ts # OpenCode TUI plugin: prompt quota status and quota details |
| 17 | +├── lib/ # core runtime logic (see lib/AGENTS.md) |
15 | 18 | ├── test/ # vitest suites (see test/AGENTS.md) |
16 | | -├── scripts/ # install + build helpers |
17 | | -├── config/ # opencode.json examples (legacy/modern) |
18 | | -├── docs/ # architecture + user docs |
| 19 | +├── scripts/ # installer, build, audit, and validation helpers |
| 20 | +├── config/ # opencode.json examples (modern/full/legacy/minimal) |
| 21 | +├── docs/ # public docs, architecture, audits, maintainer guides |
| 22 | +├── skills/ # repo-local setup skill |
19 | 23 | ├── assets/ # static assets |
| 24 | +├── .codex-plugin/ # plugin metadata for Codex skill/plugin tooling |
20 | 25 | └── dist/ # build output (generated, do not edit) |
21 | 26 | ``` |
22 | 27 |
|
23 | 28 | ## WHERE TO LOOK |
| 29 | + |
24 | 30 | | Task | Location | Notes | |
25 | 31 | | --- | --- | --- | |
26 | | -| Plugin orchestration | `index.ts` | 5-stage request pipeline: URL rewrite, body transform, OAuth headers, SSE conversion, error handling | |
| 32 | +| Installer behavior | `scripts/install-oc-codex-multi-auth.js`, `scripts/install-oc-codex-multi-auth-core.js` | npm bin, config merge, cache cleanup, TUI config enablement | |
| 33 | +| Plugin orchestration | `index.ts` | OAuth loader, request pipeline, metrics, recovery, `ToolContext` assembly | |
| 34 | +| TUI quota status | `tui.ts`, `lib/tui-status.ts`, `lib/tui-quota-cache.ts`, `lib/codex-usage.ts` | prompt quota status, quota details, shared quota cache | |
27 | 35 | | Tool registry | `lib/tools/index.ts` + `lib/tools/codex-*.ts` | 21 registered `codex-*` tools | |
28 | | -| OAuth flow + PKCE | `lib/auth/auth.ts` | token refresh, JWT decode | |
29 | | -| OAuth callback server | `lib/auth/server.ts` | binds port 1455 | |
30 | | -| Multi-account rotation | `lib/accounts.ts` | health scoring, cooldown, selection | |
31 | | -| Account storage | `lib/storage.ts` + `lib/storage/` | V3 facade, per-project/global paths, keychain, import/export | |
32 | | -| Request transformation | `lib/request/request-transformer.ts` | model normalization, prompt injection | |
33 | | -| Headers + rate limits | `lib/request/fetch-helpers.ts` | Codex headers, error mapping | |
34 | | -| SSE to JSON | `lib/request/response-handler.ts` | stream parsing | |
35 | | -| Prompt templates | `lib/prompts/codex.ts` | model-family detection, GitHub ETag cache | |
36 | | -| Config parsing | `lib/config.ts` | CODEX_MODE, plugin options | |
37 | | -| Session recovery | `lib/recovery/` | conversation state persistence | |
38 | | -| Graceful shutdown | `lib/shutdown.ts` | cleanup on process exit | |
39 | | -| Health monitoring | `lib/health.ts` | account health status | |
| 36 | +| OAuth flow + PKCE | `lib/auth/auth.ts`, `lib/auth/server.ts`, `lib/auth/device-code.ts`, `lib/auth/login-runner.ts` | browser/device/manual login, token refresh, workspace selection | |
| 37 | +| OAuth scopes | `lib/auth/scopes.ts` | connector scope validation and re-auth checks | |
| 38 | +| Multi-account rotation | `lib/accounts.ts`, `lib/accounts/`, `lib/rotation.ts` | health scoring, cooldowns, token bucket, recovery | |
| 39 | +| Account storage | `lib/storage.ts`, `lib/storage/` | V3 facade, per-project/global paths, keychain, backup/import/export | |
| 40 | +| Request transformation | `lib/request/request-transformer.ts` | model normalization, prompt injection, stateless compatibility | |
| 41 | +| Headers + rate limits | `lib/request/fetch-helpers.ts` | Codex headers, error mapping, fallback, token refresh | |
| 42 | +| Retry budgets | `lib/request/retry-budget.ts`, `lib/request/rate-limit-backoff.ts` | bounded retry classes, exponential backoff | |
| 43 | +| SSE to JSON | `lib/request/response-handler.ts` | stream parsing and empty-response detection | |
| 44 | +| Prompt templates | `lib/prompts/codex.ts`, `lib/prompts/opencode-codex.ts`, `lib/prompts/codex-opencode-bridge.ts` | model-family detection, Codex prompt cache, bridge prompts | |
| 45 | +| Config parsing | `lib/config.ts`, `lib/schemas.ts` | plugin config and environment overrides | |
| 46 | +| Session recovery | `lib/recovery/`, `lib/recovery.ts` | recoverable error handling and auto-resume | |
| 47 | +| Health monitoring | `lib/health.ts`, `lib/parallel-probe.ts` | account health status and concurrent probes | |
40 | 48 | | Circuit breaker | `lib/circuit-breaker.ts` | failure isolation | |
41 | | -| Tests | `test/` | vitest globals, coverage threshold gate | |
| 49 | +| Public architecture | `docs/architecture.md` | user-facing architecture overview | |
| 50 | +| Maintainer architecture | `docs/development/ARCHITECTURE.md` | current subsystem map and invariants | |
| 51 | +| Discoverability guide | `docs/development/GITHUB_DISCOVERABILITY.md` | repo description/topics/search wording | |
| 52 | +| Tests | `test/` | Vitest, property tests, docs parity, installer, tool modules, TUI quota | |
42 | 53 |
|
43 | 54 | ## CONVENTIONS |
44 | | -- Source: root `index.ts` + `lib/`; `dist/` is generated output. |
| 55 | + |
| 56 | +- Source: root `index.ts`, `tui.ts`, `lib/`, and `scripts/`; `dist/` is generated output. |
45 | 57 | - ESLint flat config: `no-explicit-any` enforced, unused args prefixed `_`. |
46 | | -- Tests relax lint rules (see `eslint.config.js`). |
47 | | -- Build cleans `dist/`, then emits `dist/lib/oauth-success.html` from the TypeScript source via `scripts/copy-oauth-success.js`. |
48 | 58 | - ESM only (`"type": "module"`), Node >= 18. |
| 59 | +- Canonical package/plugin name is `oc-codex-multi-auth`. |
| 60 | +- The npm bin is an installer, not a long-running runtime command. |
| 61 | +- OpenCode loads the provider plugin and TUI plugin from built package exports. |
| 62 | +- Default installer mode writes compact modern OpenCode config; `--full` adds explicit selector IDs; `--legacy` writes legacy explicit-only config. |
| 63 | +- Runtime requests preserve Codex stateless requirements: `store: false` and `reasoning.encrypted_content`. |
| 64 | +- Per-project account storage is enabled by default. |
| 65 | +- Optional OS keychain backend is opt-in with `CODEX_KEYCHAIN=1`. |
49 | 66 |
|
50 | 67 | ## ANTI-PATTERNS (THIS PROJECT) |
| 68 | + |
51 | 69 | - Do not edit `dist/` or `tmp*` directories. |
52 | | -- Do not use `as any`, `@ts-ignore`, `@ts-expect-error`. |
| 70 | +- Do not use `as any`, `@ts-ignore`, or `@ts-expect-error`. |
53 | 71 | - Do not open public security issues; see `SECURITY.md`. |
54 | | -- Do not hardcode ports other than 1455 for OAuth server. |
| 72 | +- Do not hardcode ports other than OAuth callback port `1455`; use existing constants/helpers. |
| 73 | +- Do not remove `store: false` or `reasoning.encrypted_content` from shipped config templates. |
| 74 | +- Do not treat `oc-chatgpt-multi-auth` as current except in migration/cleanup logic. |
| 75 | +- Do not expose account emails, access tokens, refresh tokens, or raw prompt/response bodies in normal diagnostics. |
| 76 | +- Do not silently delete JSON credentials when keychain operations fail. |
55 | 77 |
|
56 | 78 | ## COMMANDS |
| 79 | + |
57 | 80 | ```bash |
58 | | -npm run build # clean dist + tsc + copy oauth-success.html |
59 | | -npm run typecheck # type checking only |
60 | | -npm test # vitest once |
61 | | -npm run test:coverage # vitest coverage |
62 | | -npm run audit:ci # prod audit + dev allowlist |
63 | | -npm run test:watch # vitest watch mode |
64 | | -npm run lint # eslint |
| 81 | +npm run build # clean dist + tsc + copy oauth-success.html |
| 82 | +npm run typecheck # type checking only |
| 83 | +npm test # vitest once |
| 84 | +npm run test:coverage # vitest coverage |
| 85 | +npm run audit:ci # prod audit + dev allowlist |
| 86 | +npm run test:watch # vitest watch mode |
| 87 | +npm run lint # eslint |
65 | 88 | ``` |
66 | 89 |
|
67 | 90 | ## NOTES |
| 91 | + |
68 | 92 | - OAuth callback: `http://127.0.0.1:1455/auth/callback`. |
69 | 93 | - ChatGPT backend requires `store: false`, include `reasoning.encrypted_content`. |
70 | | -- Per-project accounts: `~/.opencode/projects/<project-key>/oc-codex-multi-auth-accounts.json` (walks up to find project root). |
| 94 | +- OpenCode config: `~/.config/opencode/opencode.json`. |
| 95 | +- OpenCode TUI config: `~/.config/opencode/tui.json`. |
| 96 | +- OpenCode auth tokens: `~/.opencode/auth/openai.json`. |
| 97 | +- Plugin config: `~/.opencode/openai-codex-auth-config.json`. |
| 98 | +- Per-project accounts: `~/.opencode/projects/<project-key>/oc-codex-multi-auth-accounts.json`. |
71 | 99 | - Global accounts: `~/.opencode/oc-codex-multi-auth-accounts.json`. |
72 | | -- Prompt templates synced from Codex CLI GitHub releases with ETag caching. |
73 | | -- 5xx server errors trigger account rotation and health penalty (same as network errors). |
| 100 | +- Flagged accounts: `~/.opencode/oc-codex-multi-auth-flagged-accounts.json`. |
| 101 | +- Request logs: `~/.opencode/logs/codex-plugin/` when logging is enabled. |
| 102 | +- Prompt templates sync from Codex CLI GitHub releases with ETag caching. |
| 103 | +- 5xx server errors trigger account rotation and health penalty like network errors. |
74 | 104 | - API deprecation/sunset headers (RFC 8594) are logged as warnings. |
75 | 105 | - StorageError preserves original stack traces via `cause` parameter. |
76 | | -- saveToDiskDebounced errors are logged but don't crash the plugin. |
77 | | - |
78 | | -## SKILL MAPPING (for delegate_task) |
79 | | - |
80 | | -Skills to load when delegating tasks in this codebase. |
81 | | - |
82 | | -### Core Skills (load on most tasks) |
83 | | - |
84 | | -| Skill | Justification | |
85 | | -|-------|---------------| |
86 | | -| `typescript-senior` | Strict mode, template literal types (`QuotaKey`), discriminated unions (`TokenResult`), Zod inference | |
87 | | -| `node-backend` | ESM-first, `node:crypto`, Buffer API, async patterns | |
88 | | -| `testing-js` | Vitest coverage gates, `vi.mock`, `vi.useFakeTimers` | |
89 | | -| `mcp-builder` | Uses `@opencode-ai/plugin/tool` pattern for tool registration | |
90 | | - |
91 | | -### Domain-Specific Skills (load when touching these areas) |
92 | | - |
93 | | -| Skill | When to Load | Key Files | |
94 | | -|-------|--------------|-----------| |
95 | | -| `auth-patterns` | OAuth flow, PKCE, JWT, token refresh | `lib/auth/auth.ts`, `lib/refresh-queue.ts` | |
96 | | -| `secrets-management` | Token storage, credential handling | `lib/storage.ts`, account JSON files | |
97 | | -| `api-design` | Request transformation, headers, SSE | `lib/request/` directory | |
98 | | -| `error-observability` | Circuit breaker, health scoring, logging | `lib/circuit-breaker.ts`, `lib/logger.ts`, `lib/health.ts` | |
99 | | -| `git-master` | Any git operations | - | |
100 | | -| `github` | PRs, GitHub API (ETag caching) | `lib/prompts/codex.ts` | |
101 | | - |
102 | | -### Situational Skills |
103 | | - |
104 | | -| Skill | When to Load | |
105 | | -|-------|--------------| |
106 | | -| `clean-architecture` | Refactoring, new module design | |
107 | | -| `property-based-testing` | Testing rotation logic, rate-limit edge cases | |
108 | | - |
109 | | -### Quick Reference |
110 | | - |
111 | | -```typescript |
112 | | -// Auth work |
113 | | -delegate_task(category="...", load_skills=["typescript-senior", "node-backend", "auth-patterns", "secrets-management"]) |
114 | | - |
115 | | -// Request pipeline work |
116 | | -delegate_task(category="...", load_skills=["typescript-senior", "node-backend", "api-design", "error-observability"]) |
117 | | - |
118 | | -// Testing |
119 | | -delegate_task(category="...", load_skills=["typescript-senior", "node-backend", "testing-js"]) |
120 | | - |
121 | | -// Plugin architecture |
122 | | -delegate_task(category="...", load_skills=["typescript-senior", "node-backend", "mcp-builder"]) |
123 | | - |
124 | | -// Git/GitHub operations |
125 | | -delegate_task(category="quick", load_skills=["git-master", "github"]) |
126 | | -``` |
| 106 | +- `saveToDiskDebounced` errors are logged but do not crash the plugin. |
0 commit comments