opencode-openai-session-router 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +217 -2
  3. package/dist/accounts.d.ts +31 -0
  4. package/dist/bun.lock.txt +671 -0
  5. package/dist/chunks/index-85yd61pa.js +29 -0
  6. package/dist/chunks/index-n7sr1hee.js +803 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.js +1842 -0
  9. package/dist/network.d.ts +8 -0
  10. package/dist/panel-contract.d.ts +14 -0
  11. package/dist/persistence.d.ts +40 -0
  12. package/dist/relay-protocol.d.ts +86 -0
  13. package/dist/relay.d.ts +50 -0
  14. package/dist/retry-control.d.ts +41 -0
  15. package/dist/router.d.ts +55 -0
  16. package/dist/rpc.d.ts +98 -0
  17. package/dist/rpc.js +7 -0
  18. package/dist/runtime.d.ts +94 -0
  19. package/dist/settings.d.ts +32 -0
  20. package/dist/sidebar-colors.d.ts +10 -0
  21. package/dist/sidebar-model.d.ts +82 -0
  22. package/dist/sidebar.d.ts +33 -0
  23. package/dist/sidebar.js +12 -0
  24. package/dist/tui.d.ts +3 -0
  25. package/dist/tui.js +25 -0
  26. package/package.json +52 -4
  27. package/scripts/build.ts +39 -0
  28. package/src/accounts.ts +130 -0
  29. package/src/index.ts +203 -0
  30. package/src/network.ts +25 -0
  31. package/src/panel-contract.ts +15 -0
  32. package/src/persistence.ts +96 -0
  33. package/src/relay-protocol.ts +157 -0
  34. package/src/relay.ts +593 -0
  35. package/src/retry-control.ts +92 -0
  36. package/src/router.ts +156 -0
  37. package/src/rpc.ts +28 -0
  38. package/src/runtime.ts +319 -0
  39. package/src/settings.ts +68 -0
  40. package/src/sidebar-colors.ts +132 -0
  41. package/src/sidebar-model.ts +257 -0
  42. package/src/sidebar.tsx +438 -0
  43. package/src/tui.tsx +12 -0
  44. package/tsconfig.build.json +12 -0
  45. package/tsconfig.json +16 -0
package/README.md CHANGED
@@ -1,3 +1,218 @@
1
- # Temporary Holding Version
1
+ # OpenCode OpenAI Session Router
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Session-based routing across your saved OpenCode OpenAI accounts, with a compact
4
+ sidebar for the orchestrator and its subagents.
5
+
6
+ **Experimental · Targets OpenCode 2.4.4 · Requires Bun · GPL-3.0-only**
7
+
8
+ Local tests cover routing, persistence, streaming and sidebar interactions.
9
+ Live rate-limit failover and cross-account encrypted-history compatibility have
10
+ not been fully verified. This project is not affiliated with OpenAI or OpenCode.
11
+
12
+ ## What it does
13
+
14
+ - Assigns new eligible sessions to compatible accounts in round-robin order.
15
+ - Keeps each session on its assigned account, including after plugin reloads.
16
+ - Tries the next compatible account on a rate limit, before response output.
17
+ - Changes the stored assignment only after an alternate account succeeds.
18
+ - Leaves other providers and unsupported authentication methods alone.
19
+ - Shows session titles and account numbers in the main session's right sidebar.
20
+
21
+ It uses OpenCode's existing account management and credential refresh. There is
22
+ no second login flow, token file or globally active account switching.
23
+
24
+ ## Installation
25
+
26
+ ### From npm
27
+
28
+ After the first npm publication, add the package name to your OpenCode
29
+ configuration. OpenCode installs the package and its dependencies:
30
+
31
+ ```jsonc
32
+ {
33
+ "$schema": "https://opencode.ai/config.json",
34
+ "plugins": ["opencode-openai-session-router"]
35
+ }
36
+ ```
37
+
38
+ You can pin a published version with `opencode-openai-session-router@X.Y.Z`.
39
+ Preserve your other plugins and settings.
40
+
41
+ ### From GitHub
42
+
43
+ After the source has been pushed, OpenCode also accepts a Git package specifier:
44
+
45
+ ```jsonc
46
+ {
47
+ "$schema": "https://opencode.ai/config.json",
48
+ "plugins": [
49
+ "github:iiNothh/opencode-openai-session-router#main"
50
+ ]
51
+ }
52
+ ```
53
+
54
+ `main` follows the development branch. For a reproducible installation, replace
55
+ it with a published tag such as `vX.Y.Z`; use an actual version from
56
+ [GitHub Releases](https://github.com/iiNothh/opencode-openai-session-router/releases).
57
+ Git installation requires Git.
58
+
59
+ For a separate npm-managed project, the equivalent dependency command is:
60
+
61
+ ```sh
62
+ npm install "git+https://github.com/iiNothh/opencode-openai-session-router.git#main"
63
+ ```
64
+
65
+ That command installs a dependency, but does not activate it in OpenCode by
66
+ itself. Use the OpenCode configuration above, or a directory URL pointing to the
67
+ installed package.
68
+
69
+ ### From a local clone
70
+
71
+ ```sh
72
+ git clone https://github.com/iiNothh/opencode-openai-session-router.git
73
+ cd opencode-openai-session-router
74
+ bun install --frozen-lockfile
75
+ ```
76
+
77
+ Merge the clone's directory URL into your existing `plugins` array:
78
+
79
+ ```jsonc
80
+ {
81
+ "$schema": "https://opencode.ai/config.json",
82
+ "plugins": [
83
+ "file:///absolute/path/to/opencode-openai-session-router"
84
+ ]
85
+ }
86
+ ```
87
+
88
+ On Windows, use a URL such as
89
+ `file:///C:/projects/opencode-openai-session-router`. Replace the example with
90
+ your actual clone directory; its name does not have to match the package name.
91
+ Preserve your other plugins and settings.
92
+
93
+ Every installation method loads both server routing and the TUI sidebar.
94
+ OpenCode reloads configuration on save; use `/reload` if needed.
95
+
96
+ Runtime exports intentionally point to the included TypeScript sources, because
97
+ OpenCode 2.4.4 disables install lifecycle scripts for plugins. Git installations
98
+ do not depend on `prepare` or a generated `dist` directory. Release packages also
99
+ include compiled output; they remain OpenCode/Bun plugins, not plain Node.js
100
+ programs.
101
+
102
+ See [OpenCode plugin documentation](https://opencode.ai/docs/build/plugins) for
103
+ configuration details.
104
+
105
+ ### Use your existing accounts
106
+
107
+ Add accounts through OpenCode's own OpenAI account management. With routing
108
+ **ON**, new eligible model requests are assigned automatically. An account pool
109
+ needs at least two compatible accounts for rotation.
110
+
111
+ ## Supported accounts
112
+
113
+ | Authentication | Routing |
114
+ | --- | --- |
115
+ | ChatGPT browser / headless OAuth | Shared compatible pool |
116
+ | ChatGPT token-sharing OAuth | Separate compatible pool |
117
+ | API keys, unsupported methods or custom providers | OpenCode's native behavior |
118
+
119
+ Incompatible authentication families are never mixed. Two accounts visible in
120
+ OpenCode are not necessarily two accounts in the same routing pool. Both native
121
+ OpenAI package entrypoints in 2.4.4 are recognized; third-party OpenAI-compatible
122
+ providers are not included.
123
+
124
+ ## Sidebar
125
+
126
+ The **Account Router** section appears in the main session's right sidebar:
127
+
128
+ - The orchestrator stays first, followed by the most recently used subagents.
129
+ - Ten sessions are shown initially; **Show N more** reveals up to ten additional
130
+ sessions, and **Show less** restores the initial view.
131
+ - Each row shows an account number and the session title. Missing agent prefixes
132
+ are added; long titles are truncated to the available width.
133
+ - Click a row to open its session. Hover highlights the row without changing its
134
+ agent color.
135
+ - The header collapses the list independently of the routing switch.
136
+ - All controls activate on a completed primary-button press and release.
137
+ Control labels are nonselectable; session titles remain selectable.
138
+
139
+ Agent colors use public OpenCode theme and agent data, following OMO-Slim 3.0.9's
140
+ palette-selection behavior without requiring or importing that plugin.
141
+
142
+ ### ON / OFF
143
+
144
+ **ON** routes newly admitted eligible requests through this plugin.
145
+ **OFF** delegates subsequent requests to OpenCode's selected account.
146
+
147
+ Already-admitted responses continue normally. Switching OFF does not erase
148
+ assignment history or the allocation cursor. While OFF, displayed numbers
149
+ describe stored history, not necessarily the account OpenCode is currently using.
150
+ The preference is saved per Location and is controlled only from the sidebar.
151
+
152
+ ### Account numbers
153
+
154
+ Numbers start at **0** and match OpenCode's **Connected accounts** menu: sorted by
155
+ credential label, then stable credential ID. Selecting another account does not
156
+ renumber existing assignments. Renaming, adding or removing accounts can change
157
+ their menu positions.
158
+
159
+ Assignments are stored using stable native credential IDs, never display
160
+ numbers. `—` means an unknown, removed or non-OpenAI assignment; another account
161
+ at the same number does not inherit its history.
162
+
163
+ ## Rate-limit behavior
164
+
165
+ Each logical operation tries compatible accounts in order, without cooldowns,
166
+ reset-time tracking or persistent account blacklists. Recognized structured
167
+ WebSocket throttle errors are also handled when no numeric status is supplied.
168
+
169
+ Intermediate failures stay inside the original model invocation, so they do not
170
+ send premature subagent completion reports. If every candidate fails, the
171
+ original assignment remains and OpenCode receives the final error normally.
172
+
173
+ Once observable output has started, a rate limit stops the request instead of
174
+ replaying it. Partial responses are not automatically continued.
175
+
176
+ ## Important limitations
177
+
178
+ - **Version-sensitive:** tested against OpenCode 2.4.4 and Bun 1.3.14 on Windows.
179
+ Other versions and platforms are not yet verified.
180
+ - **WebSocket overhead:** fresh connection affinity sends full native history
181
+ rather than reusing cross-call incremental checkpoints.
182
+ - **Diagnostics:** unusual non-JSON 429 bodies may retain the native message and
183
+ status but receive a different native error category.
184
+ - **Transport boundaries:** connect-time native HTTP fallback is supported.
185
+ Later connection failures are not silently bridged or replayed.
186
+ - **Live validation:** automated checks do not independently prove which real
187
+ upstream account was charged. Real 429 recovery, encrypted-history portability
188
+ and physical mouse behavior still need manual validation.
189
+
190
+ Session associations and the routing preference are stored through OpenCode's
191
+ plugin storage without OAuth tokens. Independent processes writing the same
192
+ Location and moving session history between Locations are not supported.
193
+
194
+ ## Development
195
+
196
+ ```sh
197
+ bun install --frozen-lockfile
198
+ bun run typecheck
199
+ bun run test
200
+ bun run build
201
+ ```
202
+
203
+ The suite uses synthetic accounts and owned local transports. Never use saved
204
+ credentials in automated tests.
205
+
206
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for contributor notes.
207
+
208
+ ## Automated releases
209
+
210
+ Validated pushes to `main` increase the patch version, build the package and
211
+ create a tagged GitHub Release. Pull requests only run checks. npm publication
212
+ uses tokenless trusted publishing and is enabled separately after the package
213
+ owner configures it; no npm credentials are stored in this repository.
214
+
215
+ ## License
216
+
217
+ Licensed under the [GNU General Public License, version 3 only](LICENSE)
218
+ (`GPL-3.0-only`).
@@ -0,0 +1,31 @@
1
+ import type { ConnectionInfo } from "@opencode/client";
2
+ import type { Credential } from "@opencode/schema/credential";
3
+ import type { RouterRuntime } from "./runtime";
4
+ export type OAuthFamily = "codex" | "token-sharing";
5
+ export declare function family(methodID: string): OAuthFamily | undefined;
6
+ export declare const nativeBase: (value: OAuthFamily) => "https://chatgpt.com/backend-api/codex" | "https://api.openai.com/v1";
7
+ export interface NativeAccountPort {
8
+ active(signal?: AbortSignal): Promise<ConnectionInfo | undefined>;
9
+ connections(signal?: AbortSignal): Promise<readonly ConnectionInfo[]>;
10
+ resolve(connection: ConnectionInfo, signal?: AbortSignal): Promise<Credential.Value | undefined>;
11
+ }
12
+ /** Only identity/family metadata is retained; native resolution owns refresh. */
13
+ export declare class NativeAccounts {
14
+ private readonly port;
15
+ private readonly runtime;
16
+ private connections;
17
+ private families;
18
+ private tail;
19
+ private closed;
20
+ constructor(port: NativeAccountPort, runtime: RouterRuntime);
21
+ invalidate(): void;
22
+ /** Startup/idle display uses connection metadata, never OAuth resolution. */
23
+ refreshMetadata(signal?: AbortSignal): Promise<void>;
24
+ pool(signal?: AbortSignal): Promise<OAuthFamily | undefined>;
25
+ headers(id: string, selected: OAuthFamily, source: Headers, signal?: AbortSignal): Promise<Headers>;
26
+ stop(): void;
27
+ dispose(): void;
28
+ }
29
+ export declare const LOCAL_AUTH = "x-opencode-local-router-auth";
30
+ /** Native nonsensitive headers/encoding survive; credentials and hop headers do not. */
31
+ export declare function upstreamHeaders(source: Headers): Headers;