@sagmans/dsh-provider-extra 0.8.1 → 0.9.2
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 +272 -91
- package/cordis.patch.yml +3 -0
- package/dist/auth-remote.js +333 -0
- package/dist/catalog-default.js +5 -5
- package/dist/catalog-routes.js +241 -96
- package/dist/catalog-runtime.js +132 -80
- package/dist/catalog.js +50 -26
- package/dist/client.js +887 -90
- package/dist/codex-login.js +21 -12
- package/dist/codex.js +34 -19
- package/dist/extra-models.js +19 -9
- package/dist/headless-startup.js +36 -21
- package/dist/index.js +73 -42
- package/dist/login-attempt.js +25 -19
- package/dist/login-choice.js +24 -14
- package/dist/login-cli.js +7 -5
- package/dist/login-command.js +33 -11
- package/dist/login-host.js +99 -41
- package/dist/login-route.js +9 -3
- package/dist/login-status.js +10 -4
- package/dist/opencode-go.js +37 -18
- package/dist/plugin-error.js +12 -0
- package/dist/service-tiers.js +40 -18
- package/dist/sign-in.js +20 -0
- package/dist/tier-client.js +886 -89
- package/dist/tier-invocation.js +51 -20
- package/dist/tier-remote.js +7 -4
- package/dist/tier-store.js +25 -11
- package/dist/tui-actions.js +29 -12
- package/docs/catalog-v1.example.json +1 -3
- package/docs/catalog.md +1 -2
- package/package.json +42 -32
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in transport for the Models page, over the harness authorization seam.
|
|
3
|
+
*
|
|
4
|
+
* DSH core owns the flows (pi-ai's Codex OAuth among them) but no shipped
|
|
5
|
+
* surface starts one. This adapter carries a single interactive attempt to the
|
|
6
|
+
* browser half: it lists the flows, buffers their notices and questions, and
|
|
7
|
+
* hands out frames by cursor. Polling keeps the Host contract to unary
|
|
8
|
+
* invocations, so the plugin needs no generated stream descriptors.
|
|
9
|
+
*
|
|
10
|
+
* Markdown-free by design: the seam validates the credential commit, and this
|
|
11
|
+
* module never reads a token, never logs an answer, and never decides that a
|
|
12
|
+
* sign-in succeeded on its own.
|
|
13
|
+
*/
|
|
14
|
+
import { randomUUID } from 'node:crypto';
|
|
15
|
+
import { credentialKeyId } from '@deepseek-ai/dsh-credentials';
|
|
16
|
+
import { AUTH_CONTRIBUTION, AUTH_NAMESPACE, AUTH_PACKAGE, } from './tier-client.js';
|
|
17
|
+
/** A page that closes mid-login must not keep its credential key claimed until restart. */
|
|
18
|
+
const ATTEMPT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
19
|
+
/** A finished attempt stays addressable so a panel opened around the settlement can read it. */
|
|
20
|
+
const SETTLED_RETENTION_MS = 2 * 60 * 1000;
|
|
21
|
+
/** Retained attempts stay bounded so abandoned tabs cannot grow Host memory. */
|
|
22
|
+
const MAX_ATTEMPTS = 16;
|
|
23
|
+
/** A real sign-in emits a handful of frames; the bound is a corruption guard. */
|
|
24
|
+
const MAX_EVENTS = 128;
|
|
25
|
+
/** Matches the wire bound; the client enforces the same limit on its input. */
|
|
26
|
+
const MAX_ANSWER = 8_192;
|
|
27
|
+
/** A provider failure may quote a response body; one bounded line is all a panel shows. */
|
|
28
|
+
const MAX_FAILURE = 512;
|
|
29
|
+
const NO_FLOW = 'This provider has no sign-in flow in the installed harness';
|
|
30
|
+
const ALREADY_RUNNING = 'A sign-in for this provider is already running';
|
|
31
|
+
const UNKNOWN_ATTEMPT = 'This sign-in attempt is no longer available';
|
|
32
|
+
const UNKNOWN_PROMPT = 'That question is no longer waiting';
|
|
33
|
+
const ANSWER_TOO_LONG = 'The sign-in answer is too long';
|
|
34
|
+
const CANCELLED = 'The sign-in attempt was cancelled';
|
|
35
|
+
const TIMED_OUT = 'The sign-in attempt timed out';
|
|
36
|
+
/** One line a panel may show for a failure, with control characters removed. */
|
|
37
|
+
function failureLine(error) {
|
|
38
|
+
const raw = error instanceof Error ? error.message : String(error);
|
|
39
|
+
const line = raw
|
|
40
|
+
.replaceAll(/\s+/gu, ' ')
|
|
41
|
+
.replaceAll(/[\p{Cc}]/gu, '')
|
|
42
|
+
.trim();
|
|
43
|
+
if (line.length === 0)
|
|
44
|
+
return 'Sign-in failed.';
|
|
45
|
+
if (line.length <= MAX_FAILURE)
|
|
46
|
+
return line;
|
|
47
|
+
return `${line.slice(0, MAX_FAILURE)}…`;
|
|
48
|
+
}
|
|
49
|
+
/** Project a seam notice; absent members stay absent on the wire. */
|
|
50
|
+
function noticeEvent(notice) {
|
|
51
|
+
return {
|
|
52
|
+
type: 'notice',
|
|
53
|
+
notice: {
|
|
54
|
+
message: notice.message,
|
|
55
|
+
...(notice.url === undefined ? {} : { url: notice.url }),
|
|
56
|
+
...(notice.code === undefined ? {} : { code: notice.code }),
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** Project a seam prompt, assigning the attempt-scoped identity answers name. */
|
|
61
|
+
function promptEvent(promptId, prompt) {
|
|
62
|
+
const base = {
|
|
63
|
+
promptId,
|
|
64
|
+
kind: prompt.kind,
|
|
65
|
+
message: prompt.message,
|
|
66
|
+
...(prompt.placeholder === undefined ? {} : { placeholder: prompt.placeholder }),
|
|
67
|
+
};
|
|
68
|
+
if (prompt.kind !== 'select')
|
|
69
|
+
return { type: 'prompt', prompt: base };
|
|
70
|
+
return {
|
|
71
|
+
type: 'prompt',
|
|
72
|
+
prompt: {
|
|
73
|
+
...base,
|
|
74
|
+
options: (prompt.options ?? []).map((option) => ({
|
|
75
|
+
id: option.id,
|
|
76
|
+
label: option.label,
|
|
77
|
+
...(option.description === undefined ? {} : { description: option.description }),
|
|
78
|
+
})),
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** Host-side half of the hand-written Typert contribution. */
|
|
83
|
+
const HOST_CONTRIBUTION = {
|
|
84
|
+
package: AUTH_PACKAGE,
|
|
85
|
+
face: 'host',
|
|
86
|
+
schemas: [],
|
|
87
|
+
model: { services: [], events: [], objects: [] },
|
|
88
|
+
invocations: AUTH_CONTRIBUTION.descriptors,
|
|
89
|
+
};
|
|
90
|
+
/** The Remote service the client namespace dispatches to. */
|
|
91
|
+
class AuthRemote {
|
|
92
|
+
ctx;
|
|
93
|
+
authorization;
|
|
94
|
+
typertRemote = {
|
|
95
|
+
service: this,
|
|
96
|
+
serviceKey: AUTH_NAMESPACE,
|
|
97
|
+
namespace: AUTH_NAMESPACE,
|
|
98
|
+
};
|
|
99
|
+
attempts = new Map();
|
|
100
|
+
active = new Map();
|
|
101
|
+
constructor(ctx, authorization) {
|
|
102
|
+
this.ctx = ctx;
|
|
103
|
+
this.authorization = authorization;
|
|
104
|
+
}
|
|
105
|
+
/** Every flow, with whether its credential is already stored. */
|
|
106
|
+
async list() {
|
|
107
|
+
const credentials = this.ctx.get('credentials');
|
|
108
|
+
return Promise.all(this.authorization.list().map(async (flow) => {
|
|
109
|
+
let provider = flow.key;
|
|
110
|
+
try {
|
|
111
|
+
provider = credentialKeyId(flow.key);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// A flow key outside the record grammar still renders; its card just
|
|
115
|
+
// never matches a provider id.
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
key: flow.key,
|
|
119
|
+
provider,
|
|
120
|
+
label: flow.label,
|
|
121
|
+
stored: credentials === undefined
|
|
122
|
+
? false
|
|
123
|
+
: await credentials
|
|
124
|
+
.describeRecord(flow.key)
|
|
125
|
+
.then((record) => record.configured)
|
|
126
|
+
// A flow whose key the store cannot describe has no stored
|
|
127
|
+
// credential it could authenticate with.
|
|
128
|
+
.catch(() => false),
|
|
129
|
+
inFlight: flow.inFlight,
|
|
130
|
+
};
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
/** Start one attempt; the flow runs in the background because it asks first. */
|
|
134
|
+
async begin(key) {
|
|
135
|
+
const flow = this.authorization.list().find((candidate) => candidate.key === key);
|
|
136
|
+
if (flow === undefined)
|
|
137
|
+
throw new Error(NO_FLOW);
|
|
138
|
+
if (flow.inFlight || this.active.has(flow.key))
|
|
139
|
+
throw new Error(ALREADY_RUNNING);
|
|
140
|
+
const attempt = {
|
|
141
|
+
id: randomUUID(),
|
|
142
|
+
key: flow.key,
|
|
143
|
+
controller: new AbortController(),
|
|
144
|
+
events: [],
|
|
145
|
+
prompts: new Map(),
|
|
146
|
+
settled: false,
|
|
147
|
+
nextPrompt: 1,
|
|
148
|
+
timeout: undefined,
|
|
149
|
+
retention: undefined,
|
|
150
|
+
};
|
|
151
|
+
this.attempts.set(attempt.id, attempt);
|
|
152
|
+
this.active.set(attempt.key, attempt);
|
|
153
|
+
this.prune();
|
|
154
|
+
attempt.timeout = setTimeout(() => {
|
|
155
|
+
attempt.controller.abort(new Error(TIMED_OUT));
|
|
156
|
+
}, ATTEMPT_TIMEOUT_MS);
|
|
157
|
+
attempt.timeout.unref();
|
|
158
|
+
void this.run(attempt);
|
|
159
|
+
return { attemptId: attempt.id };
|
|
160
|
+
}
|
|
161
|
+
/** Frames after the caller's cursor, plus whether this batch ended the attempt. */
|
|
162
|
+
poll(attemptId, cursor) {
|
|
163
|
+
const attempt = this.attempts.get(attemptId);
|
|
164
|
+
if (attempt === undefined)
|
|
165
|
+
throw new Error(UNKNOWN_ATTEMPT);
|
|
166
|
+
const start = Math.min(cursor, attempt.events.length);
|
|
167
|
+
const events = attempt.events.slice(start);
|
|
168
|
+
return {
|
|
169
|
+
cursor: attempt.events.length,
|
|
170
|
+
events,
|
|
171
|
+
done: events.at(-1)?.type === 'settled',
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** Deliver one answer to the flow; false means the question is already gone. */
|
|
175
|
+
answer(attemptId, promptId, value) {
|
|
176
|
+
if (value.length > MAX_ANSWER)
|
|
177
|
+
throw new Error(ANSWER_TOO_LONG);
|
|
178
|
+
const attempt = this.attempts.get(attemptId);
|
|
179
|
+
const pending = attempt?.prompts.get(promptId);
|
|
180
|
+
if (attempt === undefined || pending === undefined)
|
|
181
|
+
return false;
|
|
182
|
+
attempt.prompts.delete(promptId);
|
|
183
|
+
pending.detach?.();
|
|
184
|
+
pending.resolve(value);
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
/** Refuse one question as the human's decision, not as a broken surface. */
|
|
188
|
+
decline(attemptId, promptId) {
|
|
189
|
+
const attempt = this.attempts.get(attemptId);
|
|
190
|
+
const pending = attempt?.prompts.get(promptId);
|
|
191
|
+
if (attempt === undefined || pending === undefined)
|
|
192
|
+
return false;
|
|
193
|
+
attempt.prompts.delete(promptId);
|
|
194
|
+
pending.detach?.();
|
|
195
|
+
pending.reject(new Error(UNKNOWN_PROMPT));
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
/** Withdraw the attempt; the seam settles it cancelled even before the flow notices. */
|
|
199
|
+
cancel(attemptId) {
|
|
200
|
+
const attempt = this.attempts.get(attemptId);
|
|
201
|
+
if (attempt === undefined || attempt.settled)
|
|
202
|
+
return;
|
|
203
|
+
attempt.controller.abort(new Error(CANCELLED));
|
|
204
|
+
}
|
|
205
|
+
/** Run one flow and publish its settlement exactly once. */
|
|
206
|
+
async run(attempt) {
|
|
207
|
+
let status = 'failed';
|
|
208
|
+
let message;
|
|
209
|
+
try {
|
|
210
|
+
const outcome = await this.authorization.begin({
|
|
211
|
+
key: attempt.key,
|
|
212
|
+
signal: attempt.controller.signal,
|
|
213
|
+
interaction: {
|
|
214
|
+
notify: (notice) => {
|
|
215
|
+
this.push(attempt, noticeEvent(notice));
|
|
216
|
+
},
|
|
217
|
+
prompt: (prompt) => this.ask(attempt, prompt),
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
status = outcome.status;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (attempt.controller.signal.aborted)
|
|
224
|
+
status = 'cancelled';
|
|
225
|
+
else {
|
|
226
|
+
message = failureLine(error);
|
|
227
|
+
this.ctx.logger.warn(`dsh-provider-extra: sign-in for "${attempt.key}" failed: ${message}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
this.finish(attempt, status, message);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** Put one seam question to the poller and hold the flow until it is answered. */
|
|
235
|
+
ask(attempt, prompt) {
|
|
236
|
+
const promptId = `p${attempt.nextPrompt}`;
|
|
237
|
+
attempt.nextPrompt += 1;
|
|
238
|
+
return new Promise((resolve, reject) => {
|
|
239
|
+
const pending = { resolve, reject };
|
|
240
|
+
const withdraw = () => {
|
|
241
|
+
this.withdraw(attempt, promptId);
|
|
242
|
+
};
|
|
243
|
+
if (prompt.signal?.aborted === true) {
|
|
244
|
+
reject(new Error(UNKNOWN_PROMPT));
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (prompt.signal !== undefined) {
|
|
248
|
+
prompt.signal.addEventListener('abort', withdraw, { once: true });
|
|
249
|
+
pending.detach = () => {
|
|
250
|
+
prompt.signal?.removeEventListener('abort', withdraw);
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
attempt.prompts.set(promptId, pending);
|
|
254
|
+
this.push(attempt, promptEvent(promptId, prompt));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/** Retire a question the flow itself no longer needs answered. */
|
|
258
|
+
withdraw(attempt, promptId) {
|
|
259
|
+
const pending = attempt.prompts.get(promptId);
|
|
260
|
+
if (pending === undefined)
|
|
261
|
+
return;
|
|
262
|
+
attempt.prompts.delete(promptId);
|
|
263
|
+
pending.detach?.();
|
|
264
|
+
this.push(attempt, { type: 'prompt-closed', promptId });
|
|
265
|
+
pending.reject(new Error(UNKNOWN_PROMPT));
|
|
266
|
+
}
|
|
267
|
+
/** Release the key, close every question, and publish the terminal frame. */
|
|
268
|
+
finish(attempt, status, message) {
|
|
269
|
+
if (attempt.settled)
|
|
270
|
+
return;
|
|
271
|
+
attempt.settled = true;
|
|
272
|
+
if (attempt.timeout !== undefined)
|
|
273
|
+
clearTimeout(attempt.timeout);
|
|
274
|
+
attempt.timeout = undefined;
|
|
275
|
+
if (this.active.get(attempt.key) === attempt)
|
|
276
|
+
this.active.delete(attempt.key);
|
|
277
|
+
for (const promptId of [...attempt.prompts.keys()]) {
|
|
278
|
+
const pending = attempt.prompts.get(promptId);
|
|
279
|
+
attempt.prompts.delete(promptId);
|
|
280
|
+
pending?.detach?.();
|
|
281
|
+
this.push(attempt, { type: 'prompt-closed', promptId });
|
|
282
|
+
pending?.reject(new Error(CANCELLED));
|
|
283
|
+
}
|
|
284
|
+
this.push(attempt, {
|
|
285
|
+
type: 'settled',
|
|
286
|
+
status,
|
|
287
|
+
...(message === undefined ? {} : { message }),
|
|
288
|
+
});
|
|
289
|
+
attempt.retention = setTimeout(() => {
|
|
290
|
+
this.attempts.delete(attempt.id);
|
|
291
|
+
}, SETTLED_RETENTION_MS);
|
|
292
|
+
attempt.retention.unref();
|
|
293
|
+
}
|
|
294
|
+
/** Append one frame within the bound. */
|
|
295
|
+
push(attempt, event) {
|
|
296
|
+
if (attempt.events.length < MAX_EVENTS)
|
|
297
|
+
attempt.events.push(event);
|
|
298
|
+
else
|
|
299
|
+
this.ctx.logger.warn('dsh-provider-extra: sign-in attempt exceeded its frame bound');
|
|
300
|
+
}
|
|
301
|
+
/** Keep retained attempts bounded, never evicting a live one. */
|
|
302
|
+
prune() {
|
|
303
|
+
if (this.attempts.size <= MAX_ATTEMPTS)
|
|
304
|
+
return;
|
|
305
|
+
for (const [id, attempt] of this.attempts) {
|
|
306
|
+
if (this.attempts.size <= MAX_ATTEMPTS)
|
|
307
|
+
return;
|
|
308
|
+
if (!attempt.settled)
|
|
309
|
+
continue;
|
|
310
|
+
if (attempt.retention !== undefined)
|
|
311
|
+
clearTimeout(attempt.retention);
|
|
312
|
+
this.attempts.delete(id);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/** Mount the transport when the composition has both the seam and Typert. */
|
|
317
|
+
export function mountProviderAuth(ctx) {
|
|
318
|
+
ctx.inject(['authorization', 'typert'], (owner) => {
|
|
319
|
+
const authorization = owner.get('authorization');
|
|
320
|
+
const service = new AuthRemote(owner, authorization);
|
|
321
|
+
owner.provide(AUTH_NAMESPACE, service);
|
|
322
|
+
owner.effect(() => {
|
|
323
|
+
try {
|
|
324
|
+
return owner.get('typert').register(HOST_CONTRIBUTION);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
owner.logger.error('dsh-provider-extra: sign-in transport registration failed');
|
|
328
|
+
owner.logger.error(error);
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
}
|
package/dist/catalog-default.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { compileCatalog } from './catalog.js';
|
|
2
|
+
import { pluginLlmError } from './plugin-error.js';
|
|
3
3
|
const EDITOR_SERVICE = 'configEditor';
|
|
4
4
|
const PERSISTENCE_UNAVAILABLE = 'CONFIG_PERSISTENCE_UNAVAILABLE';
|
|
5
5
|
/** A detached selection prevents callers from mutating the catalog's default. */
|
|
@@ -8,7 +8,7 @@ export function catalogDefault(owner, snapshot, requireOwnership) {
|
|
|
8
8
|
currentSelection() {
|
|
9
9
|
requireOwnership?.();
|
|
10
10
|
if (snapshot.selection === null) {
|
|
11
|
-
throw
|
|
11
|
+
throw pluginLlmError('dsh-provider-extra: the catalog selects no default model', 'NO_DEFAULT_MODEL');
|
|
12
12
|
}
|
|
13
13
|
return { ...snapshot.selection };
|
|
14
14
|
},
|
|
@@ -20,7 +20,7 @@ export function catalogDefault(owner, snapshot, requireOwnership) {
|
|
|
20
20
|
const entry = owner.fiber.entry;
|
|
21
21
|
const editor = owner.get(EDITOR_SERVICE);
|
|
22
22
|
if (entry === undefined || typeof editor?.edit !== 'function') {
|
|
23
|
-
throw
|
|
23
|
+
throw pluginLlmError('dsh-provider-extra: this host cannot persist catalog.default; edit the owning profile Config', PERSISTENCE_UNAVAILABLE);
|
|
24
24
|
}
|
|
25
25
|
await editor.edit(entry, (current) => {
|
|
26
26
|
requireOwnership?.();
|
|
@@ -28,7 +28,7 @@ export function catalogDefault(owner, snapshot, requireOwnership) {
|
|
|
28
28
|
// restoring membership removed by another profile edit.
|
|
29
29
|
const latest = compileCatalog(current.catalog);
|
|
30
30
|
if (latest === undefined) {
|
|
31
|
-
throw
|
|
31
|
+
throw pluginLlmError('dsh-provider-extra: the owning Config no longer contains a catalog', 'CATALOG_OWNER_CHANGED');
|
|
32
32
|
}
|
|
33
33
|
const updated = { ...latest.config, default: latest.resolveSelection(selection) };
|
|
34
34
|
compileCatalog(updated);
|