@byokkit/cmd-provider-openai 0.1.0 → 0.1.1
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.
- package/README.md +5 -83
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,94 +1,16 @@
|
|
|
1
1
|
# @byokkit/cmd-provider-openai
|
|
2
2
|
|
|
3
|
-
OpenAI (Codex
|
|
4
|
-
[`ProviderModule`](../../plans/providers.plan.md). It ports Command Code's real OpenAI/Codex
|
|
5
|
-
**loopback OAuth** (the flow in `packages/command/src/auth/codex.ts`) onto the harness's reusable
|
|
6
|
-
provider/auth primitives — no core or harness changes required to use it.
|
|
3
|
+
OpenAI (ChatGPT/Codex) OAuth for the cmd CLI, packaged as an external BYOK provider.
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
> (`openai`), but on the wire it speaks Command Code's `codex` auth lane (see parity note below).
|
|
10
|
-
|
|
11
|
-
## What it does
|
|
12
|
-
|
|
13
|
-
- Declares one provider module: `id: 'openai'`, `displayName: 'OpenAI'`.
|
|
14
|
-
- Transport is the server-routed **gateway** (`{kind: 'gateway', route: '/alpha/generate'}`) — the
|
|
15
|
-
backend picks the real model, so the module declares no model specs and only owns auth.
|
|
16
|
-
- One `oauth` login method using an **autonomous loopback** (RFC 8252) PKCE flow:
|
|
17
|
-
1. `authorize()` starts a local callback server on `127.0.0.1:1455`, builds the OpenAI authorize
|
|
18
|
-
URL (PKCE `S256`, CSRF `state`, and the Codex-specific params
|
|
19
|
-
`id_token_add_organizations` / `codex_cli_simplified_flow` / `originator=codex_cli_rs`), and
|
|
20
|
-
returns a `pending` result with the URL + a `callback()`.
|
|
21
|
-
2. `callback()` awaits the browser redirect (5-minute timeout), validates `state`, then exchanges
|
|
22
|
-
the code for tokens (form-encoded) at `https://auth.openai.com/oauth/token`.
|
|
23
|
-
- A `refresh` method + an `auth.loader` that turns a stored credential into request headers,
|
|
24
|
-
refreshing on the 5-minute expiry skew (single-flight; a failed refresh keeps the existing token).
|
|
25
|
-
|
|
26
|
-
## Usage
|
|
27
|
-
|
|
28
|
-
```ts
|
|
29
|
-
import openaiProvider, {createOpenAIProvider} from '@byokkit/cmd-provider-openai';
|
|
30
|
-
import {createProviderRegistry} from '@commandcode/harness';
|
|
31
|
-
|
|
32
|
-
// Default export: a ready-to-register module (global fetch + clock).
|
|
33
|
-
const registry = createProviderRegistry({
|
|
34
|
-
modules: [openaiProvider /* …other modules */],
|
|
35
|
-
transport,
|
|
36
|
-
runtime,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// Or build one with injected I/O (tests / custom fetch):
|
|
40
|
-
const custom = createOpenAIProvider({fetchImpl, now: () => Date.now()});
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Login is driven through the registry (`registry.authMethods({providerId: 'openai'})` →
|
|
44
|
-
`registry.login({providerId: 'openai', methodId: 'oauth', onPending})`). `onPending` receives the
|
|
45
|
-
`url` + `instructions`; for this autonomous loopback flow it just opens the URL — `callback()`
|
|
46
|
-
resolves on its own once OpenAI redirects back.
|
|
47
|
-
|
|
48
|
-
## Parity with Command Code (`auth/codex.ts`)
|
|
49
|
-
|
|
50
|
-
These values are a faithful port and **must not drift**:
|
|
51
|
-
|
|
52
|
-
| Aspect | Value |
|
|
53
|
-
| --- | --- |
|
|
54
|
-
| OAuth client id | `app_EMoamEEZ73f0CkXaXp7hrann` |
|
|
55
|
-
| Issuer / endpoints | `https://auth.openai.com` → `/oauth/authorize`, `/oauth/token` |
|
|
56
|
-
| Scopes | `openid profile email offline_access` |
|
|
57
|
-
| Redirect URI | `http://localhost:1455/auth/callback` (fixed port; server binds `127.0.0.1`) |
|
|
58
|
-
| Token encoding | form-urlencoded (`application/x-www-form-urlencoded`) |
|
|
59
|
-
| Gateway auth header (token) | `x-oauth-token: Bearer <access>` |
|
|
60
|
-
| **Gateway auth header (provider)** | **`x-oauth-provider: codex`** |
|
|
61
|
-
|
|
62
|
-
> **The provider header value is `codex`, not `openai`.** Command Code's `getOAuthCredentials`
|
|
63
|
-
> sends `oauthProvider = PROVIDER.CODEX = 'codex'` for this flow, and the `/alpha/generate` gateway
|
|
64
|
-
> recognizes OpenAI/Codex traffic by that value. The module's `id` (`openai`) is only the local
|
|
65
|
-
> credential-store / registry key.
|
|
66
|
-
|
|
67
|
-
### Loopback decisions
|
|
68
|
-
|
|
69
|
-
- **Fixed port `1455`, not ephemeral.** OpenAI only accepts the pre-registered native redirect URI
|
|
70
|
-
for the Codex client, so the harness loopback server is pinned to port `1455` / path
|
|
71
|
-
`/auth/callback`. (The harness `createLoopbackCallbackServer` supports overriding its default
|
|
72
|
-
ephemeral port — we override it here.)
|
|
73
|
-
- **Binds `127.0.0.1`, advertises `localhost`.** Exactly like `codex.ts`: the `redirect_uri` host is
|
|
74
|
-
`localhost` (what OpenAI registered) while the server listens on `127.0.0.1`.
|
|
75
|
-
- **CSRF / state check is the module's job.** The harness loopback surfaces the returned `state`; we
|
|
76
|
-
compare it to the value we sent and reject on mismatch.
|
|
77
|
-
|
|
78
|
-
### Minor, intentional divergence
|
|
79
|
-
|
|
80
|
-
- The refresh-token grant omits the optional `scope` parameter that `codex.ts` sends. Per OAuth 2.0,
|
|
81
|
-
a refresh without `scope` returns tokens with the originally granted scopes, so this is functionally
|
|
82
|
-
equivalent; we use the harness's `refreshAccessToken` helper for it.
|
|
83
|
-
- Codex's `chatgpt_account_id` (parsed from the id_token JWT) is not stored. The harness `Credential`
|
|
84
|
-
has no field for it and **it is never sent to the gateway** (`commandAuthHeaders` only emits the
|
|
85
|
-
token + provider headers), so it is irrelevant to wire parity.
|
|
5
|
+
Signs you in with a ChatGPT Pro/Plus subscription through OpenAI's loopback PKCE OAuth flow (RFC 8252): a local callback server on `127.0.0.1:1455`, browser approval, then automatic token exchange and refresh.
|
|
86
6
|
|
|
87
7
|
## Development
|
|
88
8
|
|
|
89
9
|
```sh
|
|
90
10
|
pnpm install
|
|
91
11
|
pnpm typecheck
|
|
92
|
-
pnpm test
|
|
12
|
+
pnpm test
|
|
93
13
|
pnpm build
|
|
94
14
|
```
|
|
15
|
+
|
|
16
|
+
Tests inject `fetch` and the clock; no real network.
|
package/package.json
CHANGED