@sagmans/dsh-provider-extra 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.
- package/LICENSE +21 -0
- package/README.md +152 -0
- package/cordis.patch.yml +6 -0
- package/dist/codex-login.js +110 -0
- package/dist/codex.js +226 -0
- package/dist/index.js +167 -0
- package/dist/login-cli.js +66 -0
- package/dist/opencode-go.js +186 -0
- package/package.json +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sercan Sagman - ahmetsercansagman@gmail.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# dsh-provider-extra
|
|
2
|
+
|
|
3
|
+
Extra provider routes for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness):
|
|
4
|
+
|
|
5
|
+
- **OpenCode Go:** sends the live conversation ID in `x-opencode-session` for routing and prompt caching.
|
|
6
|
+
- **OpenAI Codex:** uses a ChatGPT subscription through pi-ai's OAuth flow and the harness credential store.
|
|
7
|
+
|
|
8
|
+
Published on npm as [`@sagmans/dsh-provider-extra`](https://www.npmjs.com/package/@sagmans/dsh-provider-extra); every release carries a provenance attestation built by the tag workflow, and no npm token is stored. The code is [MIT licensed](LICENSE).
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
- Node.js 24 LTS (verified with 24.20.0).
|
|
13
|
+
- pnpm 11.21.0 for this repository.
|
|
14
|
+
- A DeepSeek Harness install on the supported line: `>=0.1.5-rc.1 <0.1.6` (verified against `0.1.5-rc.2`). The plugin declares that range as a peer dependency, so a profile resolves the harness copy it already has rather than a second framework instance.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
Register the bundle in each profile you use. Web and TUI are separate compositions:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
dsh plugin --profile web add @sagmans/dsh-provider-extra
|
|
22
|
+
dsh plugin --profile tui add @sagmans/dsh-provider-extra
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
A release of the harness CLI is installable without a launcher already on `PATH`:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
pnpm dlx @deepseek-ai/dsh@0.1.5-rc.2 plugin --profile web add @sagmans/dsh-provider-extra
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The package declares `dsh.bundle.patch`. The CLI adds it to the profile's bundle list, and its patch loads the compiled plugin automatically. A bare `pnpm link` is not the registration procedure.
|
|
32
|
+
|
|
33
|
+
To remove it from a profile:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
dsh plugin --profile web remove @sagmans/dsh-provider-extra
|
|
37
|
+
dsh plugin --profile tui remove @sagmans/dsh-provider-extra
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
These commands remove the profile dependency and bundle activation, not stored credentials. Restart the affected profiles after adding or removing the bundle.
|
|
41
|
+
|
|
42
|
+
### Install from source
|
|
43
|
+
|
|
44
|
+
For unreleased work, clone this repository and link the checkout instead:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
git clone https://github.com/assagman/dsh-provider-extra.git
|
|
48
|
+
cd dsh-provider-extra
|
|
49
|
+
pnpm install --frozen-lockfile
|
|
50
|
+
pnpm run check
|
|
51
|
+
dsh plugin --profile web add "link:$PWD"
|
|
52
|
+
dsh plugin --profile tui add "link:$PWD"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Rebuild with `pnpm run build` after source changes, then restart the affected profiles. The bundle uses compiled JavaScript without a TypeScript loader. Keep the linked checkout at its registered path.
|
|
56
|
+
|
|
57
|
+
### Optional profile overrides
|
|
58
|
+
|
|
59
|
+
Defaults use the `opencode-go` and `openai-codex` routes, `OPENCODE_API_KEY`, and the fallback session ID `dsh-provider-extra`.
|
|
60
|
+
|
|
61
|
+
To customize them, add an ID-targeted override to `$DSH_HOME/profiles/web/cordis.patch.yml`, `$DSH_HOME/profiles/tui/cordis.patch.yml`, or both. `DSH_HOME` defaults to `~/.dsh`.
|
|
62
|
+
|
|
63
|
+
```yaml
|
|
64
|
+
- id: dsh-provider-extra
|
|
65
|
+
config:
|
|
66
|
+
apiKeyEnv: OPENCODE_GO_API_KEY
|
|
67
|
+
routeId: opencode-go-session
|
|
68
|
+
displayName: OpenCode Go (session)
|
|
69
|
+
# codexEnabled: false
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Preserve unrelated profile entries. Do not add a manual `insert` or `name`: the bundle owns plugin activation. Overrides can remain after removal without keeping the plugin active.
|
|
73
|
+
|
|
74
|
+
If you used the earlier manual registration, replace its `insert` block with an ID-targeted override before running `add`. Keep your existing `config` values. A leftover manual insert can cause duplicate loading or keep the plugin active after `remove`.
|
|
75
|
+
|
|
76
|
+
Keep `opencode-go` and `openai-codex` out of the built-in `llm-pi-ai` provider configuration. A route can have only one adapter. A duplicate produces `DUPLICATE_ADAPTER`. The plugin logs the conflict and leaves that route with its existing owner. You can choose a different `routeId`, such as `opencode-go-session`, when you need both Go routes.
|
|
77
|
+
|
|
78
|
+
Start a profile with the same harness install that owns the profiles:
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
dsh web
|
|
82
|
+
# Or:
|
|
83
|
+
dsh --profile tui
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Select a model from the registered route in the model picker.
|
|
87
|
+
|
|
88
|
+
## OpenCode Go credentials and models
|
|
89
|
+
|
|
90
|
+
Store the API key through the harness credential service, using the reference named by `apiKeyEnv`. Alternatively, supply that environment variable to the harness. Do not put keys in the patch file. `baseURL` and `headers` can override the gateway endpoint and add static headers. For the standard gateway, leave them unset.
|
|
91
|
+
|
|
92
|
+
The plugin reuses `PiAiAdapter` and pi-ai's `opencode-go` catalog. It injects the routing header on both `prepareCall()` and direct stream dispatch. The request's session ID takes precedence over `fallbackSessionId` and static headers. Without a request ID or configured fallback, the plugin sends no session header.
|
|
93
|
+
|
|
94
|
+
The pinned catalog is extended with `deepseek-flash` (DeepSeek V4.1 Flash), cloned from `deepseek-v4-flash`. Add other models in `$DSH_HOME/settings.yaml` without rebuilding or restarting:
|
|
95
|
+
|
|
96
|
+
```yaml
|
|
97
|
+
dsh-provider-extra:
|
|
98
|
+
extraModels:
|
|
99
|
+
- id: deepseek-flash
|
|
100
|
+
name: DeepSeek V4.1 Flash
|
|
101
|
+
template: deepseek-v4-flash
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Each entry clones wire behavior from its template. Later entries win by ID. A catalog-owned ID is not replaced. An unknown template becomes a model diagnostic without disabling the route.
|
|
105
|
+
|
|
106
|
+
## Codex sign-in
|
|
107
|
+
|
|
108
|
+
Run the attended login with the same `DSH_HOME` as your harness. An installed package ships the entry point:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
dsh-provider-extra-login
|
|
112
|
+
# If the server uses a non-default credentials file:
|
|
113
|
+
dsh-provider-extra-login --credentials-path /absolute/path/to/.credentials.yaml
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
From a source checkout, `pnpm codex:login` runs the same entry point through the TypeScript loader.
|
|
117
|
+
|
|
118
|
+
Choose device-code login for a headless host or browser login for a desktop. Follow the URL and prompts printed by pi-ai. The browser callback uses `localhost:1455`. The prompt also accepts a pasted redirect URL.
|
|
119
|
+
|
|
120
|
+
The grant is stored in `$DSH_HOME/.credentials.yaml` by default. Writes use the harness document lock, and the running adapter reads the grant on the next request. Never commit the grant or include it in a bug report. Renaming `codexRouteId` changes the credential address, so keep the default unless a separate grant is intentional. Set `codexEnabled: false` to disable this route.
|
|
121
|
+
|
|
122
|
+
## Development and verification
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
pnpm install --frozen-lockfile
|
|
126
|
+
pnpm run check
|
|
127
|
+
pnpm audit --audit-level high
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`check` runs type checking, 30 source tests, the build, three integration tests, the 22 release guard tests, and the package smoke. The plain-Node smoke test mounts both compiled routes in the real Cordis/LLM runtime. Two CLI tests exercise add, repeated add, profile overrides, and remove in disposable web/TUI profiles; they drive the registry CLI this repository develops against (`@deepseek-ai/dsh@0.1.5-rc.2`), so no harness checkout is needed.
|
|
131
|
+
|
|
132
|
+
Tests use a local mock gateway or seeded grants. They require no API key, OAuth login, or paid provider requests.
|
|
133
|
+
|
|
134
|
+
CI installs from the registry with read-only permissions and no account credentials, verifies dependency signatures and attestations, and runs the same checks. New dependency releases must be at least seven days old, and `pnpm-workspace.yaml` limits which lifecycle scripts may run.
|
|
135
|
+
|
|
136
|
+
The package smoke test does not prove a real account can authenticate or a remote provider is available. To verify those, install a candidate into a profile built on the supported harness line and send one message through each configured route.
|
|
137
|
+
|
|
138
|
+
Before sending a pull request, run the checks and describe the behavior changed. Report security problems privately to the repository maintainer, not in public issues. Remove keys, grants, conversation content, and machine-specific paths from shared logs.
|
|
139
|
+
|
|
140
|
+
## Releasing
|
|
141
|
+
|
|
142
|
+
Published artefacts carry a provenance attestation, which only a CI provider can issue, so releases ship from the tag workflow rather than a laptop.
|
|
143
|
+
|
|
144
|
+
1. Bump `version` in `package.json`, land it on `main` through a reviewed PR, and wait for CI to pass on the merged SHA.
|
|
145
|
+
2. Tag that SHA with a signed tag and push it. The tag ruleset admits repository admins only.
|
|
146
|
+
3. [`.github/workflows/release.yml`](.github/workflows/release.yml) re-runs the checks and the package smoke; the publish job then waits for a maintainer's approval on the `npm-release` environment before it publishes with OIDC trusted publishing and automatic provenance.
|
|
147
|
+
|
|
148
|
+
The workflow stores no npm token: the registry trusts `release.yml` on the `npm-release` environment, and [`scripts/npm/release.py`](scripts/npm/release.py) creates both the environment and that trust. The full runbook is [RELEASE.md](RELEASE.md).
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal sign-in for the Codex subscription route.
|
|
3
|
+
*
|
|
4
|
+
* This is the other half of the seam DSH core leaves unconnected: it drives
|
|
5
|
+
* pi-ai's own Codex OAuth login (browser or device-code, chosen at the
|
|
6
|
+
* prompt) against the harness credential store, so the grant lands in the
|
|
7
|
+
* same record the route reads. It runs as the package bin (src/login-cli.ts),
|
|
8
|
+
* not inside the server, because a sign-in is an attended one-off: it needs a
|
|
9
|
+
* human watching for the URL and code.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-provider-extra/codex-login
|
|
12
|
+
*/
|
|
13
|
+
import { CODEX_CATALOG_ID } from "./codex.js";
|
|
14
|
+
/** pi-ai auth type id for the subscription login. */
|
|
15
|
+
const OAUTH_TYPE = 'oauth';
|
|
16
|
+
/** Render one pi-ai login event as terminal output lines. */
|
|
17
|
+
export function renderEvent(event) {
|
|
18
|
+
switch (event.type) {
|
|
19
|
+
case 'info': {
|
|
20
|
+
const link = event.links?.[0];
|
|
21
|
+
return [event.message + (link === undefined ? '' : ' ' + link.url)];
|
|
22
|
+
}
|
|
23
|
+
case 'auth_url':
|
|
24
|
+
return [(event.instructions ?? 'Open this page to continue signing in:'), event.url];
|
|
25
|
+
case 'device_code':
|
|
26
|
+
return [
|
|
27
|
+
'Enter this code on the verification page to finish signing in:',
|
|
28
|
+
' URL: ' + event.verificationUri,
|
|
29
|
+
' Code: ' + event.userCode,
|
|
30
|
+
];
|
|
31
|
+
case 'progress':
|
|
32
|
+
return [event.message];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Render one pi-ai prompt as the terminal question to ask. */
|
|
36
|
+
export function renderPrompt(prompt) {
|
|
37
|
+
switch (prompt.type) {
|
|
38
|
+
case 'select': {
|
|
39
|
+
const choices = prompt.options.map((option, index) => `${index + 1}) ${option.label}`).join(' | ');
|
|
40
|
+
return { question: `${prompt.message} ${choices} — answer 1-${prompt.options.length} or id: `, options: prompt.options };
|
|
41
|
+
}
|
|
42
|
+
case 'secret':
|
|
43
|
+
case 'text':
|
|
44
|
+
case 'manual_code': {
|
|
45
|
+
const hint = prompt.placeholder === undefined ? '' : ' (' + prompt.placeholder + ')';
|
|
46
|
+
return { question: prompt.message + hint + ' ' };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Answer one pi-ai prompt from the terminal. A select resolves the option
|
|
52
|
+
* id, never the position: pi-ai matches on id, and echoing the position back
|
|
53
|
+
* would sign into whatever drifts into that slot next.
|
|
54
|
+
*/
|
|
55
|
+
export async function answerPrompt(terminal, prompt) {
|
|
56
|
+
const rendered = renderPrompt(prompt);
|
|
57
|
+
if (rendered.options !== undefined) {
|
|
58
|
+
const options = rendered.options;
|
|
59
|
+
const raw = await terminal.question(rendered.question);
|
|
60
|
+
const picked = Number.parseInt(raw.trim(), 10);
|
|
61
|
+
if (Number.isInteger(picked) && picked >= 1 && picked <= options.length) {
|
|
62
|
+
const option = options[picked - 1];
|
|
63
|
+
if (option !== undefined)
|
|
64
|
+
return option.id;
|
|
65
|
+
}
|
|
66
|
+
const byId = options.find(option => option.id === raw.trim());
|
|
67
|
+
if (byId !== undefined)
|
|
68
|
+
return byId.id;
|
|
69
|
+
throw new Error('dsh-provider-extra: answer the prompt with a number 1-' + String(options.length) + ' or an option id');
|
|
70
|
+
}
|
|
71
|
+
return terminal.question(rendered.question);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The terminal as a pi-ai AuthInteraction. Prompt withdrawal (the
|
|
75
|
+
* manual_code race against the local callback server) surfaces as a
|
|
76
|
+
* rejection, which is exactly what pi-ai's login expects from a lost race.
|
|
77
|
+
*/
|
|
78
|
+
export function terminalInteraction(terminal) {
|
|
79
|
+
return {
|
|
80
|
+
signal: terminal.signal,
|
|
81
|
+
notify: (event) => {
|
|
82
|
+
for (const line of renderEvent(event))
|
|
83
|
+
terminal.print(line);
|
|
84
|
+
},
|
|
85
|
+
prompt: (prompt) => answerPrompt(terminal, prompt),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Run the Codex OAuth login to completion against an already-built Models
|
|
90
|
+
* collection. The collection carries the harness-backed store, so pi-ai
|
|
91
|
+
* persists the grant itself; this function only conducts the conversation.
|
|
92
|
+
* Secrets stay out of output: success names the provider, never the tokens.
|
|
93
|
+
*
|
|
94
|
+
* @param models - collection holding the catalog Codex provider and the harness store.
|
|
95
|
+
* @param provider - the catalog Codex provider object to sign in with.
|
|
96
|
+
* @param terminal - line I/O for the human attending the sign-in.
|
|
97
|
+
*/
|
|
98
|
+
export async function runCodexLogin(models, provider, terminal) {
|
|
99
|
+
models.setProvider(provider);
|
|
100
|
+
await models.login(CODEX_CATALOG_ID, OAUTH_TYPE, terminalInteraction(terminal));
|
|
101
|
+
terminal.print('Signed in to OpenAI Codex. The subscription grant is stored; the route serves it with no restart.');
|
|
102
|
+
}
|
|
103
|
+
/** A readline-backed terminal over stdin/stdout. */
|
|
104
|
+
export function readlineTerminal(rl, signal) {
|
|
105
|
+
return {
|
|
106
|
+
question: (prompt) => rl.question(prompt),
|
|
107
|
+
print: (line) => { console.log(line); },
|
|
108
|
+
signal,
|
|
109
|
+
};
|
|
110
|
+
}
|
package/dist/codex.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Codex (ChatGPT subscription) route over pi-ai's OAuth.
|
|
3
|
+
*
|
|
4
|
+
* DSH core already translates pi-ai's Codex login into the neutral
|
|
5
|
+
* authorization vocabulary, but no shipped surface consumes that seam: no CLI
|
|
6
|
+
* command, no web UI, no RPC reaches it. So this plugin owns both halves the
|
|
7
|
+
* seam would have connected — the route serving subscription requests, and
|
|
8
|
+
* the login script (src/codex-login.ts) writing the grant — while reusing
|
|
9
|
+
* pi-ai's OAuth implementation itself, never reimplementing it.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-provider-extra/codex
|
|
12
|
+
*/
|
|
13
|
+
import { defaultProviderAuthContext } from '@earendil-works/pi-ai';
|
|
14
|
+
import { builtinProviders } from '@earendil-works/pi-ai/providers/all';
|
|
15
|
+
import { credentialKey, credentialKeyId, credentialKeyScope, isCredentialKeySegment, } from '@deepseek-ai/dsh-credentials';
|
|
16
|
+
import { LlmError, resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
|
|
17
|
+
/** pi-ai catalog id of the ChatGPT-subscription provider this route serves. */
|
|
18
|
+
export const CODEX_CATALOG_ID = 'openai-codex';
|
|
19
|
+
/**
|
|
20
|
+
* Default route id. It deliberately equals the catalog id: the credential
|
|
21
|
+
* record address derives from it, so sharing the id shares the grant with
|
|
22
|
+
* DSH core's own Codex flow instead of forking a second sign-in.
|
|
23
|
+
*/
|
|
24
|
+
export const DEFAULT_CODEX_ROUTE_ID = CODEX_CATALOG_ID;
|
|
25
|
+
/** Display name for selectors, matching pi-ai's subscription label. */
|
|
26
|
+
export const DEFAULT_CODEX_DISPLAY_NAME = 'OpenAI Codex';
|
|
27
|
+
/**
|
|
28
|
+
* Record scope for the OAuth grant. Identical to dsh-llm-pi-ai's own scope,
|
|
29
|
+
* which is what makes a grant written by the login script readable by core's
|
|
30
|
+
* flow (and vice versa) instead of stranding two sign-ins in two namespaces.
|
|
31
|
+
*/
|
|
32
|
+
const RECORD_SCOPE = 'llm-pi-ai';
|
|
33
|
+
/** The record address for one pi-ai provider id. */
|
|
34
|
+
export function recordKeyFor(providerId) {
|
|
35
|
+
return credentialKey(RECORD_SCOPE, providerId);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The JSON image of one grant payload: plain objects lose their
|
|
39
|
+
* explicitly-undefined members, exactly as JSON.stringify would render them.
|
|
40
|
+
* pi-ai credentials idiomatically carry optional members as explicit
|
|
41
|
+
* undefined, which the credential store's strict validator refuses as
|
|
42
|
+
* unrepresentable.
|
|
43
|
+
*/
|
|
44
|
+
function jsonImage(value) {
|
|
45
|
+
if (Array.isArray(value))
|
|
46
|
+
return value.map(entry => entry === undefined ? null : jsonImage(entry));
|
|
47
|
+
if (typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype) {
|
|
48
|
+
const image = {};
|
|
49
|
+
for (const [key, member] of Object.entries(value)) {
|
|
50
|
+
if (member !== undefined)
|
|
51
|
+
image[key] = jsonImage(member);
|
|
52
|
+
}
|
|
53
|
+
return image;
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
/** Translate a stored record into the credential pi-ai expects. */
|
|
58
|
+
function toPiCredential(record) {
|
|
59
|
+
if (record === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
if (record.kind === 'api-key') {
|
|
62
|
+
return {
|
|
63
|
+
type: 'api_key',
|
|
64
|
+
...record.key === undefined ? {} : { key: record.key },
|
|
65
|
+
...record.env === undefined ? {} : { env: { ...record.env } },
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return record.payload;
|
|
69
|
+
}
|
|
70
|
+
/** Translate a pi-ai credential into the record to store. */
|
|
71
|
+
function toRecord(credential) {
|
|
72
|
+
if (credential.type === 'api_key') {
|
|
73
|
+
return {
|
|
74
|
+
kind: 'api-key',
|
|
75
|
+
...credential.key === undefined ? {} : { key: credential.key },
|
|
76
|
+
...credential.env === undefined ? {} : { env: { ...credential.env } },
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return { kind: 'grant', payload: jsonImage(credential) };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A pi-ai CredentialStore over the harness credential records, addressed at
|
|
83
|
+
* the shared record scope so core and extra read each other's grants.
|
|
84
|
+
*/
|
|
85
|
+
export class HarnessCredentialStore {
|
|
86
|
+
credentials;
|
|
87
|
+
constructor(credentials) {
|
|
88
|
+
this.credentials = credentials;
|
|
89
|
+
}
|
|
90
|
+
async read(providerId) {
|
|
91
|
+
const credentials = this.credentials();
|
|
92
|
+
if (credentials === undefined)
|
|
93
|
+
return undefined;
|
|
94
|
+
if (!isCredentialKeySegment(providerId))
|
|
95
|
+
return undefined;
|
|
96
|
+
return toPiCredential(await credentials.readRecord(recordKeyFor(providerId)));
|
|
97
|
+
}
|
|
98
|
+
async list() {
|
|
99
|
+
const stored = await this.credentials()?.listRecords() ?? [];
|
|
100
|
+
const mine = [];
|
|
101
|
+
for (const entry of stored) {
|
|
102
|
+
// Records another plugin owns are not this collection's to report:
|
|
103
|
+
// their payloads are written in a format pi-ai never agreed to.
|
|
104
|
+
if (credentialKeyScope(entry.key) !== RECORD_SCOPE)
|
|
105
|
+
continue;
|
|
106
|
+
mine.push({
|
|
107
|
+
providerId: credentialKeyId(entry.key),
|
|
108
|
+
type: entry.kind === 'api-key' ? 'api_key' : 'oauth',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return mine;
|
|
112
|
+
}
|
|
113
|
+
async modify(providerId, mutate) {
|
|
114
|
+
if (!isCredentialKeySegment(providerId)) {
|
|
115
|
+
throw new LlmError('dsh-provider-extra: provider id "' + providerId + '" cannot address a stored credential record (a record id is a'
|
|
116
|
+
+ ' lowercase hyphenated identifier)', 'UNSTORABLE_PROVIDER_ID');
|
|
117
|
+
}
|
|
118
|
+
const credentials = this.credentials();
|
|
119
|
+
if (credentials === undefined) {
|
|
120
|
+
throw new LlmError('dsh-provider-extra: this composition mounts no credentials service, so there is nowhere to store the'
|
|
121
|
+
+ ' credential a sign-in produces; mount one (dsh-credentials-local) to sign in', 'NO_CREDENTIAL_STORE');
|
|
122
|
+
}
|
|
123
|
+
const stored = await credentials.modifyRecord(recordKeyFor(providerId), async (current) => {
|
|
124
|
+
const next = await mutate(toPiCredential(current));
|
|
125
|
+
return next === undefined ? undefined : toRecord(next);
|
|
126
|
+
});
|
|
127
|
+
return toPiCredential(stored);
|
|
128
|
+
}
|
|
129
|
+
async delete(providerId) {
|
|
130
|
+
if (!isCredentialKeySegment(providerId))
|
|
131
|
+
return;
|
|
132
|
+
await this.credentials()?.deleteRecord(recordKeyFor(providerId));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The installed pi-ai catalog's Codex provider, whose API implementation,
|
|
137
|
+
* OAuth auth, and model list this route reuses rather than reconstructing.
|
|
138
|
+
* Exported for the login script, which signs into this same object so the
|
|
139
|
+
* grant it writes is the one the route reads.
|
|
140
|
+
* @throws Error when the installed pi-ai catalog no longer ships Codex.
|
|
141
|
+
*/
|
|
142
|
+
export function catalogCodex() {
|
|
143
|
+
const found = builtinProviders().find(provider => provider.id === CODEX_CATALOG_ID);
|
|
144
|
+
if (found === undefined) {
|
|
145
|
+
throw new Error('dsh-provider-extra: the installed pi-ai catalog has no provider "' + CODEX_CATALOG_ID + '";'
|
|
146
|
+
+ ' pin @earendil-works/pi-ai to a catalog that ships it');
|
|
147
|
+
}
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The catalog provider re-keyed for this route. Dispatch stays with the
|
|
152
|
+
* catalog provider so its API implementation and OAuth quirks survive, while
|
|
153
|
+
* identity and models answer to the route configuration.
|
|
154
|
+
*/
|
|
155
|
+
function routeProvider(config, catalog) {
|
|
156
|
+
const models = catalog.getModels().map(model => ({
|
|
157
|
+
...model,
|
|
158
|
+
provider: config.provider,
|
|
159
|
+
}));
|
|
160
|
+
// The request-level apiKey override stays absent (see codexApiKey): auth
|
|
161
|
+
// resolves from the collection store holding the OAuth grant, which is
|
|
162
|
+
// also what lets pi-ai refresh an expired token under its own lock.
|
|
163
|
+
return {
|
|
164
|
+
id: config.provider,
|
|
165
|
+
name: config.displayName,
|
|
166
|
+
auth: catalog.auth,
|
|
167
|
+
getModels: () => models,
|
|
168
|
+
stream: (model, context, options) => catalog.stream(model, context, options),
|
|
169
|
+
streamSimple: (model, context, options) => catalog.streamSimple(model, context, options),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// The resolved profile defaults below mirror dsh-llm-pi-ai's config
|
|
173
|
+
// resolution, which does not export them; drift is caught by that package's
|
|
174
|
+
// own tests, as with the OpenCode Go route.
|
|
175
|
+
/** Default maximum idle interval while an adapter stream read is outstanding. */
|
|
176
|
+
const STREAM_IDLE_TIMEOUT_MS = 300_000;
|
|
177
|
+
/** Default request-level bound on base64-encoded image payload per request. */
|
|
178
|
+
const MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
|
|
179
|
+
/** Default total-pixel budget preserving one 2048px normalized attachment. */
|
|
180
|
+
const REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
|
|
181
|
+
/** Default raw encoded-byte target before inline base64 expansion. */
|
|
182
|
+
const REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
|
|
183
|
+
/**
|
|
184
|
+
* The resolved profile for the route. It names no credential reference: the
|
|
185
|
+
* subscription authenticates from the stored OAuth grant, never from an
|
|
186
|
+
* environment key, so there is nothing to fail loud about at request time —
|
|
187
|
+
* pi-ai's own honest refusal names the missing sign-in instead.
|
|
188
|
+
*/
|
|
189
|
+
export function buildCodexProfile(config) {
|
|
190
|
+
const catalog = catalogCodex();
|
|
191
|
+
return {
|
|
192
|
+
provider: config.provider,
|
|
193
|
+
displayName: config.displayName,
|
|
194
|
+
streamIdleTimeoutMs: STREAM_IDLE_TIMEOUT_MS,
|
|
195
|
+
maxRequestImageBytes: MAX_REQUEST_IMAGE_BYTES,
|
|
196
|
+
requestImagePixelBudget: REQUEST_IMAGE_PIXEL_BUDGET,
|
|
197
|
+
requestImageMaxBytes: REQUEST_IMAGE_MAX_BYTES,
|
|
198
|
+
retryPolicy: resolveRetryPolicy(undefined, 'dsh-provider-extra: openai-codex'),
|
|
199
|
+
modelErrors: new Map(),
|
|
200
|
+
configuredMaxTokens: new Map(),
|
|
201
|
+
piProvider: routeProvider(config, catalog),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The per-request credential for the Codex route: always absent, deferring
|
|
206
|
+
* to the collection's OAuth store. A named reference would let a stray
|
|
207
|
+
* environment key (OPENAI_API_KEY and friends) bill another tenant for a
|
|
208
|
+
* request the deployment meant to authenticate as the subscription.
|
|
209
|
+
*/
|
|
210
|
+
export function codexApiKey() {
|
|
211
|
+
return Promise.resolve(undefined);
|
|
212
|
+
}
|
|
213
|
+
// The ambient context answers from the process environment (pi-ai's own
|
|
214
|
+
// default) rather than the harness seam: Codex resolves no ambient
|
|
215
|
+
// credential env at request time — its only env knob,
|
|
216
|
+
// PI_OAUTH_CALLBACK_HOST, is login-time and process-owned — so seam lookup
|
|
217
|
+
// would add a dependency for a question that is never asked.
|
|
218
|
+
/**
|
|
219
|
+
* Auth for the Codex route: the harness-backed grant store plus pi-ai's own
|
|
220
|
+
* ambient context. The OpenCode Go route shares this injection unharmed: its
|
|
221
|
+
* key arrives as the request-level override, which pi-ai prefers over every
|
|
222
|
+
* store read, so a store holding only the Codex grant changes nothing for it.
|
|
223
|
+
*/
|
|
224
|
+
export function codexAuth(credentials) {
|
|
225
|
+
return { credentials: new HarnessCredentialStore(credentials), authContext: defaultProviderAuthContext() };
|
|
226
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-provider-extra: extra LLM provider routes for DeepSeek Harness.
|
|
3
|
+
*
|
|
4
|
+
* Mounts two routes today. OpenCode Go stamps the x-opencode-session header
|
|
5
|
+
* from the live dsh session id, which the gateway requires for routing and
|
|
6
|
+
* prompt-cache affinity and which the shipped adapters do not send. OpenAI
|
|
7
|
+
* Codex serves a ChatGPT subscription through pi-ai's OAuth: the grant lives
|
|
8
|
+
* in the harness credential store (see src/codex-login.ts for the sign-in),
|
|
9
|
+
* because no shipped surface consumes DSH core's own Codex authorization flow.
|
|
10
|
+
*
|
|
11
|
+
* Registration is the profile's job: `dsh plugin add` installs the package and
|
|
12
|
+
* its bundle patch mounts this module, so no path is ever written down.
|
|
13
|
+
*
|
|
14
|
+
* dsh plugin --profile web add @sagmans/dsh-provider-extra
|
|
15
|
+
*
|
|
16
|
+
* An ID-targeted override changes the config after that:
|
|
17
|
+
*
|
|
18
|
+
* - id: dsh-provider-extra
|
|
19
|
+
* config:
|
|
20
|
+
* apiKeyEnv: OPENCODE_API_KEY
|
|
21
|
+
* # routeId: opencode-go # default; keep it out of llm-pi-ai providers
|
|
22
|
+
* # baseURL: https://opencode.ai/zen/go/v1
|
|
23
|
+
* # fallbackSessionId: dsh-provider-extra
|
|
24
|
+
* # codexEnabled: true # sign in with: dsh-provider-extra-login
|
|
25
|
+
* # codexRouteId: openai-codex # default; keep it out of llm-pi-ai providers
|
|
26
|
+
*
|
|
27
|
+
* @module dsh-provider-extra
|
|
28
|
+
*/
|
|
29
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
30
|
+
import { PiAiAdapter } from '@deepseek-ai/dsh-llm-pi-ai';
|
|
31
|
+
import { LlmError, assertUsableApiKey } from '@deepseek-ai/dsh-llm';
|
|
32
|
+
import { DEFAULT_EXTRA_MODEL_TEMPLATE, DEFAULT_OPENCODE_API_KEY_ENV, OPENCODE_GO_PROVIDER_ID, buildOpenCodeGoProfile, } from "./opencode-go.js";
|
|
33
|
+
import { DEFAULT_CODEX_DISPLAY_NAME, DEFAULT_CODEX_ROUTE_ID, buildCodexProfile, codexApiKey, codexAuth, } from "./codex.js";
|
|
34
|
+
/** Settings namespace configuration surfaces address this plugin's section by. */
|
|
35
|
+
const SETTINGS_NS = 'dsh-provider-extra';
|
|
36
|
+
export const Config = Schema.object({
|
|
37
|
+
apiKeyEnv: Schema.string().role('credential-ref').default(DEFAULT_OPENCODE_API_KEY_ENV),
|
|
38
|
+
routeId: Schema.string().default(OPENCODE_GO_PROVIDER_ID),
|
|
39
|
+
displayName: Schema.string().default('OpenCode Go'),
|
|
40
|
+
baseURL: Schema.string(),
|
|
41
|
+
fallbackSessionId: Schema.string(),
|
|
42
|
+
headers: Schema.dict(Schema.string()),
|
|
43
|
+
codexEnabled: Schema.boolean().default(true),
|
|
44
|
+
codexRouteId: Schema.string().default(DEFAULT_CODEX_ROUTE_ID),
|
|
45
|
+
codexDisplayName: Schema.string().default(DEFAULT_CODEX_DISPLAY_NAME),
|
|
46
|
+
});
|
|
47
|
+
export const name = 'dsh-provider-extra';
|
|
48
|
+
export const inject = ['llm'];
|
|
49
|
+
const extraModelSchema = Schema.object({
|
|
50
|
+
id: Schema.string().required(),
|
|
51
|
+
name: Schema.string(),
|
|
52
|
+
template: Schema.string().default(DEFAULT_EXTRA_MODEL_TEMPLATE),
|
|
53
|
+
});
|
|
54
|
+
const SectionSchema = Schema.object({
|
|
55
|
+
extraModels: Schema.array(extraModelSchema).default([]),
|
|
56
|
+
});
|
|
57
|
+
export function apply(ctx, config) {
|
|
58
|
+
const route = {
|
|
59
|
+
provider: config.routeId,
|
|
60
|
+
displayName: config.displayName,
|
|
61
|
+
apiKeyEnv: config.apiKeyEnv,
|
|
62
|
+
...config.baseURL === undefined ? {} : { baseURL: config.baseURL },
|
|
63
|
+
...config.fallbackSessionId === undefined ? {} : { fallbackSessionId: config.fallbackSessionId },
|
|
64
|
+
...config.headers === undefined ? {} : { headers: { ...config.headers } },
|
|
65
|
+
};
|
|
66
|
+
const codex = {
|
|
67
|
+
provider: config.codexRouteId,
|
|
68
|
+
displayName: config.codexDisplayName,
|
|
69
|
+
};
|
|
70
|
+
// The Codex profile is boot-time, not per-request: it takes no settings
|
|
71
|
+
// inputs, and building it per operation would let a catalog drift (pi-ai no
|
|
72
|
+
// longer shipping Codex) fail every request on both routes instead of just
|
|
73
|
+
// standing this route down once, loudly, here.
|
|
74
|
+
let codexProfile;
|
|
75
|
+
if (config.codexEnabled) {
|
|
76
|
+
try {
|
|
77
|
+
codexProfile = buildCodexProfile(codex);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
ctx.logger.error('dsh-provider-extra: codex route "' + codex.provider + '" disabled; the installed pi-ai catalog cannot serve it');
|
|
81
|
+
ctx.logger.error(error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Route wiring is boot-time, but the model list is per-request: the section
|
|
85
|
+
// thunk below tracks the settings overlay, so a committed extras change
|
|
86
|
+
// reaches the next operation with no rebuild and no restart.
|
|
87
|
+
let currentSection = () => ({ extraModels: [] });
|
|
88
|
+
const profiles = () => {
|
|
89
|
+
const entries = [
|
|
90
|
+
[route.provider, buildOpenCodeGoProfile({ ...route, extraModels: currentSection().extraModels })],
|
|
91
|
+
];
|
|
92
|
+
if (codexProfile !== undefined)
|
|
93
|
+
entries.push([codex.provider, codexProfile]);
|
|
94
|
+
return new Map(entries);
|
|
95
|
+
};
|
|
96
|
+
const adapter = new PiAiAdapter({
|
|
97
|
+
profiles,
|
|
98
|
+
resolveApiKey: async (provider) => {
|
|
99
|
+
// The Codex route authenticates from the stored OAuth grant, never
|
|
100
|
+
// from a key: absent here is what lets the collection store serve it.
|
|
101
|
+
if (provider === codex.provider)
|
|
102
|
+
return codexApiKey();
|
|
103
|
+
const credentials = ctx.get('credentials');
|
|
104
|
+
const hit = credentials !== undefined
|
|
105
|
+
? (await credentials.resolve(route.apiKeyEnv))?.value
|
|
106
|
+
: process.env[route.apiKeyEnv];
|
|
107
|
+
if (hit !== undefined && hit.length > 0) {
|
|
108
|
+
return assertUsableApiKey(hit, name, route.apiKeyEnv);
|
|
109
|
+
}
|
|
110
|
+
throw new LlmError('dsh-provider-extra: no credential for route "' + route.provider + '"; its profile resolves ' + route.apiKeyEnv
|
|
111
|
+
+ ', which is not set — store it through the credentials service or export it', 'MISSING_CREDENTIAL');
|
|
112
|
+
},
|
|
113
|
+
// One shared injection for both routes: the OpenCode Go key arrives as
|
|
114
|
+
// the request-level override (preferred over every store read), so the
|
|
115
|
+
// harness-backed grant store serves Codex while changing nothing for it.
|
|
116
|
+
auth: codexAuth(() => ctx.get('credentials')),
|
|
117
|
+
});
|
|
118
|
+
// A route another adapter already owns (opencode-go configured under
|
|
119
|
+
// llm-pi-ai) must not brick the whole composition: the refusal names the
|
|
120
|
+
// remediation and every other plugin keeps working.
|
|
121
|
+
try {
|
|
122
|
+
ctx.llm.registerAdapter([route.provider], adapter);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
ctx.logger.error('dsh-provider-extra: route "' + route.provider + '" was refused;'
|
|
126
|
+
+ " remove it from llm-pi-ai's providers section to serve it here");
|
|
127
|
+
ctx.logger.error(error);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
// Separately from the OpenCode Go route, because registration is
|
|
131
|
+
// all-or-nothing: one call for both would drop a working route when the
|
|
132
|
+
// other collides. llm-pi-ai's directory already lists the catalog id, so
|
|
133
|
+
// no directory entry is registered here — core's serves the Models page.
|
|
134
|
+
if (codexProfile !== undefined) {
|
|
135
|
+
try {
|
|
136
|
+
ctx.llm.registerAdapter([codex.provider], adapter);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
ctx.logger.warn('dsh-provider-extra: codex route "' + codex.provider + '" stays with its existing owner;'
|
|
140
|
+
+ " remove it from llm-pi-ai's providers section to serve it here");
|
|
141
|
+
ctx.logger.warn(error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
ctx.llm.registerConfigurableProviders([{
|
|
146
|
+
provider: route.provider,
|
|
147
|
+
displayName: route.displayName,
|
|
148
|
+
settingsNs: SETTINGS_NS,
|
|
149
|
+
settingsPath: [route.provider],
|
|
150
|
+
}]);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
// llm-pi-ai already lists every catalog id in the directory, including
|
|
154
|
+
// opencode-go; its entry keeps serving the Models page here.
|
|
155
|
+
ctx.logger.warn('dsh-provider-extra: directory entry for "' + route.provider + '" stays with its existing owner');
|
|
156
|
+
ctx.logger.warn(error);
|
|
157
|
+
}
|
|
158
|
+
ctx.inject(['settings'], (settingsCtx) => {
|
|
159
|
+
settingsCtx.settings.installSection(ctx, SETTINGS_NS, SectionSchema, { extraModels: [] }, {
|
|
160
|
+
setSource: (source) => { currentSection = source; },
|
|
161
|
+
// No registration facts derive from the section: the route set is fixed
|
|
162
|
+
// at composition and the adapter rebuilds its snapshot on every
|
|
163
|
+
// operation, so a committed extras change applies alone.
|
|
164
|
+
onChange: () => { },
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Sign the Codex subscription route in: dsh-provider-extra-login [--credentials-path <file>].
|
|
4
|
+
*
|
|
5
|
+
* Ships as the package bin rather than a checkout script because the grant has
|
|
6
|
+
* to be obtainable from an installed package: a registry install has no
|
|
7
|
+
* repository to run tooling from, and a sign-in is attended anyway, so the
|
|
8
|
+
* installed package must carry the entry point that reaches it.
|
|
9
|
+
*
|
|
10
|
+
* Runs pi-ai's own Codex OAuth login (browser or device-code, chosen at the
|
|
11
|
+
* prompt) against the harness credentials document the server reads, so the
|
|
12
|
+
* grant is live for the next request with no restart.
|
|
13
|
+
*
|
|
14
|
+
* The document defaults exactly as the server's credentials-local does
|
|
15
|
+
* (<harness home>/.credentials.yaml); pass --credentials-path only when the
|
|
16
|
+
* server overrides it. Writes serialize against the running server through
|
|
17
|
+
* the document's own cross-process lock.
|
|
18
|
+
*/
|
|
19
|
+
import { stdin as input, stdout as output } from 'node:process';
|
|
20
|
+
import { createInterface } from 'node:readline/promises';
|
|
21
|
+
import { Context } from '@deepseek-ai/cordis';
|
|
22
|
+
import LocalCredentialProvider, { resolveSpec } from '@deepseek-ai/dsh-credentials-local';
|
|
23
|
+
import { createModels } from '@earendil-works/pi-ai';
|
|
24
|
+
import { catalogCodex, codexAuth } from "./codex.js";
|
|
25
|
+
import { readlineTerminal, runCodexLogin } from "./codex-login.js";
|
|
26
|
+
/** Flag naming an explicit credentials document. */
|
|
27
|
+
const PATH_FLAG = '--credentials-path';
|
|
28
|
+
/** The explicit document override from argv, if the server overrides its own. */
|
|
29
|
+
function credentialsPath(argv) {
|
|
30
|
+
const at = argv.indexOf(PATH_FLAG);
|
|
31
|
+
const value = at === -1 ? undefined : argv[at + 1];
|
|
32
|
+
if (at !== -1 && (value === undefined || value.startsWith('--'))) {
|
|
33
|
+
throw new Error('dsh-provider-extra: ' + PATH_FLAG + ' needs a file path');
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
async function main(argv) {
|
|
38
|
+
const path = credentialsPath(argv);
|
|
39
|
+
const ctx = new Context();
|
|
40
|
+
const fiber = await ctx.plugin(LocalCredentialProvider, {
|
|
41
|
+
...(path === undefined ? {} : { path }),
|
|
42
|
+
watch: false,
|
|
43
|
+
});
|
|
44
|
+
console.log('Credentials: ' + resolveSpec(path === undefined ? {} : { path }).filename);
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
process.on('SIGINT', () => { controller.abort(); });
|
|
47
|
+
const rl = createInterface({ input, output });
|
|
48
|
+
try {
|
|
49
|
+
const models = createModels(codexAuth(() => ctx.get('credentials')));
|
|
50
|
+
await runCodexLogin(models, catalogCodex(), readlineTerminal(rl, controller.signal));
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
rl.close();
|
|
54
|
+
await fiber.dispose();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
await main(process.argv.slice(2));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error instanceof Error)
|
|
62
|
+
console.error(error.message);
|
|
63
|
+
else
|
|
64
|
+
console.error(error);
|
|
65
|
+
process.exitCode = 1;
|
|
66
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenCode Go route with per-session routing identity.
|
|
3
|
+
*
|
|
4
|
+
* The OpenCode Go gateway rejects requests that carry no session identity
|
|
5
|
+
* (HTTP 400 MissingSessionID) and uses a stable per-conversation id to optimize
|
|
6
|
+
* routing and prompt caching. The shipped dsh pi-ai adapter forwards the
|
|
7
|
+
* session id into pi-ai's options, but pi-ai emits session headers only for
|
|
8
|
+
* providers whose compat opts in, so this route injects the header itself at
|
|
9
|
+
* the provider delegate — the one boundary every dispatch path (direct
|
|
10
|
+
* stream() and the runtime's prepareCall()) reaches with the per-request
|
|
11
|
+
* options still in hand.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-provider-extra/opencode-go
|
|
14
|
+
*/
|
|
15
|
+
import { defaultProviderAuthContext } from '@earendil-works/pi-ai';
|
|
16
|
+
import { builtinProviders } from '@earendil-works/pi-ai/providers/all';
|
|
17
|
+
import { resolveRetryPolicy } from '@deepseek-ai/dsh-llm';
|
|
18
|
+
/** pi-ai catalog id of the OpenCode Go provider this route mirrors. */
|
|
19
|
+
export const OPENCODE_GO_PROVIDER_ID = 'opencode-go';
|
|
20
|
+
/** Routing header the OpenCode Go gateway requires on every model request. */
|
|
21
|
+
export const SESSION_HEADER_NAME = 'x-opencode-session';
|
|
22
|
+
/** Environment variable read for the API key when no credential service is present. */
|
|
23
|
+
export const DEFAULT_OPENCODE_API_KEY_ENV = 'OPENCODE_API_KEY';
|
|
24
|
+
/** Wire id OpenCode Go serves DeepSeek V4.1 Flash under. */
|
|
25
|
+
export const DEEPSEEK_V41_FLASH_ID = 'deepseek-flash';
|
|
26
|
+
/** Display name for DeepSeek V4.1 Flash, matching the OpenCode Go model list. */
|
|
27
|
+
export const DEEPSEEK_V41_FLASH_NAME = 'DeepSeek V4.1 Flash';
|
|
28
|
+
/** Default template sibling an extra model inherits its wire behavior from. */
|
|
29
|
+
export const DEFAULT_EXTRA_MODEL_TEMPLATE = 'deepseek-v4-flash';
|
|
30
|
+
/** Shipped extras for ids the installed catalog predates while the gateway already serves them. */
|
|
31
|
+
const DEFAULT_EXTRA_MODELS = [
|
|
32
|
+
{ id: DEEPSEEK_V41_FLASH_ID, name: DEEPSEEK_V41_FLASH_NAME, template: DEFAULT_EXTRA_MODEL_TEMPLATE },
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* Resolve declared extras against the installed catalog. Later specs win over
|
|
36
|
+
* earlier ones by id, so a settings entry reshapes the shipped default under
|
|
37
|
+
* the same id; an id the catalog itself ships stays with the catalog.
|
|
38
|
+
* Failures land in `modelErrors` beside serviceable models instead of
|
|
39
|
+
* refusing the route, because one mistyped template must not silence the
|
|
40
|
+
* other twenty-seven.
|
|
41
|
+
* @param catalog - installed catalog models to clone wire behavior from.
|
|
42
|
+
* @param specs - declared extras, shipped defaults first.
|
|
43
|
+
* @returns the resolved extras and per-model failure diagnostics.
|
|
44
|
+
*/
|
|
45
|
+
export function resolveExtraModels(catalog, specs) {
|
|
46
|
+
const models = [];
|
|
47
|
+
const modelErrors = new Map();
|
|
48
|
+
const shipped = new Set(catalog.map(model => model.id));
|
|
49
|
+
const byId = new Map();
|
|
50
|
+
for (const spec of specs)
|
|
51
|
+
byId.set(spec.id, spec);
|
|
52
|
+
for (const spec of byId.values()) {
|
|
53
|
+
if (spec.id.length === 0) {
|
|
54
|
+
modelErrors.set(spec.id, 'dsh-provider-extra: an extra model has an empty id');
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (shipped.has(spec.id))
|
|
58
|
+
continue;
|
|
59
|
+
const templateId = spec.template ?? DEFAULT_EXTRA_MODEL_TEMPLATE;
|
|
60
|
+
const template = catalog.find(model => model.id === templateId);
|
|
61
|
+
if (template === undefined) {
|
|
62
|
+
modelErrors.set(spec.id, `dsh-provider-extra: extra model "${spec.id}" names template "${templateId}",`
|
|
63
|
+
+ ' which the installed catalog does not describe');
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
models.push({ ...template, id: spec.id, name: spec.name ?? template.name });
|
|
67
|
+
}
|
|
68
|
+
return { models, modelErrors };
|
|
69
|
+
}
|
|
70
|
+
// The resolved profile defaults below mirror dsh-llm-pi-ai's config resolution,
|
|
71
|
+
// which does not export them; drift is caught by that package's own tests.
|
|
72
|
+
/** Default maximum idle interval while an adapter stream read is outstanding. */
|
|
73
|
+
const STREAM_IDLE_TIMEOUT_MS = 300_000;
|
|
74
|
+
/** Default request-level bound on base64-encoded image payload per request. */
|
|
75
|
+
const MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
|
|
76
|
+
/** Default total-pixel budget preserving one 2048px normalized attachment. */
|
|
77
|
+
const REQUEST_IMAGE_PIXEL_BUDGET = 2048 * 2048;
|
|
78
|
+
/** Default raw encoded-byte target before inline base64 expansion. */
|
|
79
|
+
const REQUEST_IMAGE_MAX_BYTES = 1024 * 1024;
|
|
80
|
+
/**
|
|
81
|
+
* The installed pi-ai catalog's OpenCode Go provider, whose API implementations
|
|
82
|
+
* and auth this route reuses rather than reconstructing.
|
|
83
|
+
* @throws Error when the installed pi-ai catalog no longer ships OpenCode Go.
|
|
84
|
+
*/
|
|
85
|
+
function catalogOpenCodeGo() {
|
|
86
|
+
const found = builtinProviders().find(provider => provider.id === OPENCODE_GO_PROVIDER_ID);
|
|
87
|
+
if (found === undefined) {
|
|
88
|
+
throw new Error('dsh-provider-extra: the installed pi-ai catalog has no provider "' + OPENCODE_GO_PROVIDER_ID + '";'
|
|
89
|
+
+ ' pin @earendil-works/pi-ai to a catalog that ships it');
|
|
90
|
+
}
|
|
91
|
+
return found;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Stamp the routing header onto one request's stream options. The request's
|
|
95
|
+
* session id wins, the configured fallback answers sessionless requests, and
|
|
96
|
+
* no id at all sends nothing — an absent header surfaces the gateway's own
|
|
97
|
+
* refusal rather than hiding it behind a fabricated identity. The session
|
|
98
|
+
* header also wins a collision with a configured static header of the same
|
|
99
|
+
* name, because the live identity is the fact the gateway routes on.
|
|
100
|
+
* @param options - the per-request stream options pi-ai is about to dispatch.
|
|
101
|
+
* @param config - the route configuration.
|
|
102
|
+
* @returns the options with the routing header merged in.
|
|
103
|
+
*/
|
|
104
|
+
function withRoutingHeader(options, config) {
|
|
105
|
+
if (options === undefined)
|
|
106
|
+
return undefined;
|
|
107
|
+
const routingId = options.sessionId ?? config.fallbackSessionId;
|
|
108
|
+
if (routingId === undefined)
|
|
109
|
+
return options;
|
|
110
|
+
// The spread keeps every member of the exact option type pi-ai handed over;
|
|
111
|
+
// only the routing header is set, so the cast only widens back to that type.
|
|
112
|
+
return { ...options, headers: { ...options.headers, [SESSION_HEADER_NAME]: routingId } };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The catalog provider re-keyed for this route, mirroring dsh-llm-pi-ai's
|
|
116
|
+
* catalog-provider reuse: dispatch stays with the catalog provider so its API
|
|
117
|
+
* implementations and quirks survive, while identity and models answer to the
|
|
118
|
+
* route configuration. The delegates are also where the routing header enters:
|
|
119
|
+
* every dispatch path funnels through them with the request's own options.
|
|
120
|
+
* @param config - the route configuration.
|
|
121
|
+
* @param catalog - the installed catalog provider to reuse.
|
|
122
|
+
* @param extras - resolved extra models, re-keyed here like catalog models.
|
|
123
|
+
* @returns the provider to register into the adapter's model collection.
|
|
124
|
+
*/
|
|
125
|
+
function routeProvider(config, catalog, extras) {
|
|
126
|
+
const models = [...catalog.getModels(), ...extras].map(model => ({
|
|
127
|
+
...model,
|
|
128
|
+
...config.baseURL === undefined ? {} : { baseUrl: config.baseURL },
|
|
129
|
+
provider: config.provider,
|
|
130
|
+
}));
|
|
131
|
+
return {
|
|
132
|
+
id: config.provider,
|
|
133
|
+
name: config.displayName,
|
|
134
|
+
...config.baseURL === undefined ? {} : { baseUrl: config.baseURL },
|
|
135
|
+
auth: catalog.auth,
|
|
136
|
+
getModels: () => models,
|
|
137
|
+
stream: (model, context, options) => catalog.stream(model, context, withRoutingHeader(options, config)),
|
|
138
|
+
streamSimple: (model, context, options) => catalog.streamSimple(model, context, withRoutingHeader(options, config)),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The resolved profile for the route: static facts only, because everything
|
|
143
|
+
* per-request lives in the provider delegates where the request's options flow.
|
|
144
|
+
* @param config - the route configuration.
|
|
145
|
+
* @returns the resolved profile the adapter freezes into its snapshot.
|
|
146
|
+
*/
|
|
147
|
+
export function buildOpenCodeGoProfile(config) {
|
|
148
|
+
const catalog = catalogOpenCodeGo();
|
|
149
|
+
const extras = resolveExtraModels(catalog.getModels(), [...DEFAULT_EXTRA_MODELS, ...(config.extraModels ?? [])]);
|
|
150
|
+
return {
|
|
151
|
+
provider: config.provider,
|
|
152
|
+
displayName: config.displayName,
|
|
153
|
+
...config.headers === undefined ? {} : { headers: { ...config.headers } },
|
|
154
|
+
streamIdleTimeoutMs: STREAM_IDLE_TIMEOUT_MS,
|
|
155
|
+
maxRequestImageBytes: MAX_REQUEST_IMAGE_BYTES,
|
|
156
|
+
requestImagePixelBudget: REQUEST_IMAGE_PIXEL_BUDGET,
|
|
157
|
+
requestImageMaxBytes: REQUEST_IMAGE_MAX_BYTES,
|
|
158
|
+
retryPolicy: resolveRetryPolicy(undefined, 'dsh-provider-extra: opencode-go'),
|
|
159
|
+
modelErrors: extras.modelErrors,
|
|
160
|
+
configuredMaxTokens: new Map(),
|
|
161
|
+
piProvider: routeProvider(config, catalog, extras.models),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Auth for a route whose only method is the harness-resolved API key: the
|
|
166
|
+
* in-memory store stays empty because nothing logs in, and the ambient context
|
|
167
|
+
* answers the provider's environment questions from process.env.
|
|
168
|
+
*/
|
|
169
|
+
export function openCodeGoAuth() {
|
|
170
|
+
return { credentials: new EmptyCredentialStore(), authContext: defaultProviderAuthContext() };
|
|
171
|
+
}
|
|
172
|
+
/** Credential store that holds nothing: the harness resolves this route's key itself. */
|
|
173
|
+
class EmptyCredentialStore {
|
|
174
|
+
read() {
|
|
175
|
+
return Promise.resolve(undefined);
|
|
176
|
+
}
|
|
177
|
+
list() {
|
|
178
|
+
return Promise.resolve([]);
|
|
179
|
+
}
|
|
180
|
+
modify(_providerId, fn) {
|
|
181
|
+
return fn(undefined);
|
|
182
|
+
}
|
|
183
|
+
delete() {
|
|
184
|
+
return Promise.resolve();
|
|
185
|
+
}
|
|
186
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sagmans/dsh-provider-extra",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Extra LLM provider routes for DeepSeek Harness: OpenCode Go with per-session x-opencode-session routing, and OpenAI Codex over ChatGPT-subscription OAuth.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"deepseek-harness",
|
|
7
|
+
"dsh",
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"plugin",
|
|
10
|
+
"llm",
|
|
11
|
+
"provider",
|
|
12
|
+
"opencode",
|
|
13
|
+
"codex",
|
|
14
|
+
"cordis"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/index.js",
|
|
21
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"bin": {
|
|
25
|
+
"dsh-provider-extra-login": "./dist/login-cli.js"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"cordis.patch.yml",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^24.0.0"
|
|
34
|
+
},
|
|
35
|
+
"packageManager": "pnpm@11.21.0",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"registry": "https://registry.npmjs.org/"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/assagman/dsh-provider-extra.git"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/assagman/dsh-provider-extra#readme",
|
|
45
|
+
"bugs": "https://github.com/assagman/dsh-provider-extra/issues",
|
|
46
|
+
"dsh": {
|
|
47
|
+
"bundle": {
|
|
48
|
+
"patch": "./cordis.patch.yml"
|
|
49
|
+
},
|
|
50
|
+
"compatibility": {
|
|
51
|
+
"dsh": ">=0.1.5-rc.1 <0.1.6",
|
|
52
|
+
"dshReleases": {
|
|
53
|
+
"0.1.5-rc.2": "compatible"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"codex:login": "node --import tsx/esm src/login-cli.ts",
|
|
59
|
+
"test": "node --import tsx/esm --test \"tests/*.spec.ts\"",
|
|
60
|
+
"typecheck": "tsc --noEmit",
|
|
61
|
+
"build": "tsc -p tsconfig.build.json",
|
|
62
|
+
"test:build": "node --test \"tests/*.test.mjs\"",
|
|
63
|
+
"test:release": "python3 -m unittest discover -s tests/release -p 'test_*.py'",
|
|
64
|
+
"pack-smoke": "node tools/pack-smoke.mjs",
|
|
65
|
+
"check": "pnpm run typecheck && pnpm test && pnpm run build && pnpm run test:build && pnpm run test:release && pnpm run pack-smoke",
|
|
66
|
+
"prepack": "pnpm run build",
|
|
67
|
+
"prepublishOnly": "pnpm run typecheck && pnpm test && pnpm run test:release"
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@deepseek-ai/cordis": "^4.0.2",
|
|
71
|
+
"@deepseek-ai/dsh-credentials": ">=0.1.5-rc.1 <0.1.6",
|
|
72
|
+
"@deepseek-ai/dsh-credentials-local": ">=0.1.5-rc.1 <0.1.6",
|
|
73
|
+
"@deepseek-ai/dsh-llm": ">=0.1.5-rc.1 <0.1.6",
|
|
74
|
+
"@deepseek-ai/dsh-llm-pi-ai": ">=0.1.5-rc.1 <0.1.6",
|
|
75
|
+
"@deepseek-ai/dsh-settings": ">=0.1.5-rc.1 <0.1.6",
|
|
76
|
+
"@deepseek-ai/schemastery": "^3.18.2",
|
|
77
|
+
"@earendil-works/pi-ai": "^0.85.1"
|
|
78
|
+
},
|
|
79
|
+
"devDependencies": {
|
|
80
|
+
"@deepseek-ai/cordis": "4.0.2",
|
|
81
|
+
"@deepseek-ai/dsh": "0.1.5-rc.2",
|
|
82
|
+
"@deepseek-ai/dsh-credentials": "0.1.5-rc.2",
|
|
83
|
+
"@deepseek-ai/dsh-credentials-local": "0.1.5-rc.2",
|
|
84
|
+
"@deepseek-ai/dsh-llm": "0.1.5-rc.2",
|
|
85
|
+
"@deepseek-ai/dsh-llm-pi-ai": "0.1.5-rc.2",
|
|
86
|
+
"@deepseek-ai/dsh-settings": "0.1.5-rc.2",
|
|
87
|
+
"@deepseek-ai/schemastery": "3.18.2",
|
|
88
|
+
"@earendil-works/pi-ai": "0.85.1",
|
|
89
|
+
"@types/node": "22.20.0",
|
|
90
|
+
"tsx": "4.22.4",
|
|
91
|
+
"typescript": "6.0.3"
|
|
92
|
+
}
|
|
93
|
+
}
|