@sagmans/dsh-provider-extra 0.8.0 → 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 +43 -33
package/dist/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
window.__ModuleLoader__.load({id:"@sagmans/dsh-provider-extra",factory(require){const exports={};
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.TIER_CONTRIBUTION = exports.TIER_SAVE_ERROR = exports.TIER_DEFAULT = exports.TIER_NAMESPACE = exports.TIER_PACKAGE = void 0;
|
|
4
|
+
exports.AUTH_CONTRIBUTION = exports.TIER_CONTRIBUTION = exports.AUTH_PACKAGE = exports.AUTH_NAMESPACE = exports.TIER_SAVE_ERROR = exports.TIER_DEFAULT = exports.TIER_NAMESPACE = exports.TIER_PACKAGE = void 0;
|
|
5
5
|
exports.parseTierIdentity = parseTierIdentity;
|
|
6
6
|
exports.parseTier = parseTier;
|
|
7
7
|
exports.parseTierView = parseTierView;
|
|
@@ -20,17 +20,37 @@ const COMMAND_SERVICE = 'commandUi';
|
|
|
20
20
|
const MODEL_SERVICE = 'modelDirectories';
|
|
21
21
|
const SESSION_SERVICE = 'sessions';
|
|
22
22
|
const REMOTE_SERVICE = 'remote';
|
|
23
|
-
const TIER_REMOTE_SERVICE = REMOTE_SERVICE
|
|
23
|
+
const TIER_REMOTE_SERVICE = `${REMOTE_SERVICE}.${exports.TIER_NAMESPACE}`;
|
|
24
24
|
const UNAVAILABLE = 'Service tiers are unavailable for this model or session';
|
|
25
25
|
const STALE_ROUTE = 'Model changed; reopen /service-tier before saving';
|
|
26
26
|
const COMMAND_DESCRIPTION = 'Choose the current model’s shared service tier';
|
|
27
27
|
const SEARCH_PLACEHOLDER = 'Search service tiers';
|
|
28
28
|
const NO_RESULTS = 'No matching service tiers';
|
|
29
29
|
const TIER_IDS = ['auto', 'default', 'priority'];
|
|
30
|
+
/** Sign-in transport lives beside the tier namespace; both are Web-only surfaces. */
|
|
31
|
+
exports.AUTH_NAMESPACE = 'providerExtraAuth';
|
|
32
|
+
/** Its own contribution identity: the tier face already claims the package's `host` face. */
|
|
33
|
+
exports.AUTH_PACKAGE = `${exports.TIER_PACKAGE}/sign-in`;
|
|
34
|
+
const AUTH_REMOTE_SERVICE = `${REMOTE_SERVICE}.${exports.AUTH_NAMESPACE}`;
|
|
35
|
+
/** The Models page keys provider cards by the adapter family's settings namespace. */
|
|
36
|
+
const PI_AI_SETTINGS_NS = 'llm-pi-ai';
|
|
37
|
+
/** Routes whose OAuth flow this surface offers today; add an id after proving its flow. */
|
|
38
|
+
const SIGN_IN_PROVIDERS = ['openai-codex'];
|
|
39
|
+
/** A restart begins a fresh attempt list; attempts stay addressable while this page polls it. */
|
|
40
|
+
const AUTH_POLL_MS = 400;
|
|
41
|
+
const AUTH_INVALID = 'Invalid sign-in data';
|
|
42
|
+
const AUTH_LIST_ERROR = 'Could not read sign-in state';
|
|
43
|
+
const AUTH_START_ERROR = 'Could not start sign-in';
|
|
44
|
+
const AUTH_POLL_ERROR = 'Sign-in stopped responding; close this panel and try again';
|
|
45
|
+
const AUTH_ANSWER_ERROR = 'Could not deliver that answer';
|
|
46
|
+
const AUTH_COPY_FAILED = 'Copy failed';
|
|
47
|
+
const AUTH_ANSWER_MAX = 8_192;
|
|
30
48
|
/** Reject extra fields instead of forwarding accidental provider state. */
|
|
31
49
|
function record(value, keys) {
|
|
32
|
-
if (value === null ||
|
|
33
|
-
|
|
50
|
+
if (value === null ||
|
|
51
|
+
typeof value !== 'object' ||
|
|
52
|
+
Array.isArray(value) ||
|
|
53
|
+
Reflect.ownKeys(value).some((key) => typeof key !== 'string' || !keys.includes(key)))
|
|
34
54
|
throw new TypeError(INVALID_WIRE);
|
|
35
55
|
return value;
|
|
36
56
|
}
|
|
@@ -44,7 +64,7 @@ function parseTierIdentity(value) {
|
|
|
44
64
|
function parseTier(value) {
|
|
45
65
|
if (value === undefined)
|
|
46
66
|
return undefined;
|
|
47
|
-
if (typeof value !== 'string' || !TIER_IDS.some(id => id === value))
|
|
67
|
+
if (typeof value !== 'string' || !TIER_IDS.some((id) => id === value))
|
|
48
68
|
throw new TypeError(INVALID_WIRE);
|
|
49
69
|
return value;
|
|
50
70
|
}
|
|
@@ -53,14 +73,18 @@ function parseTierView(value) {
|
|
|
53
73
|
const input = record(value, ['choices', 'current']);
|
|
54
74
|
if (!Array.isArray(input.choices))
|
|
55
75
|
throw new TypeError(INVALID_WIRE);
|
|
56
|
-
const choices = input.choices.map(value => {
|
|
76
|
+
const choices = input.choices.map((value) => {
|
|
57
77
|
const choice = record(value, ['id', 'name', 'description']);
|
|
58
78
|
const id = parseTier(choice.id);
|
|
59
79
|
if (id === undefined)
|
|
60
80
|
throw new TypeError(INVALID_WIRE);
|
|
61
|
-
return {
|
|
81
|
+
return {
|
|
82
|
+
id,
|
|
83
|
+
name: parseTierIdentity(choice.name),
|
|
84
|
+
description: parseTierIdentity(choice.description),
|
|
85
|
+
};
|
|
62
86
|
});
|
|
63
|
-
const ids = new Set(choices.map(choice => choice.id));
|
|
87
|
+
const ids = new Set(choices.map((choice) => choice.id));
|
|
64
88
|
if (ids.size !== choices.length)
|
|
65
89
|
throw new TypeError(INVALID_WIRE);
|
|
66
90
|
const current = parseTier(input.current);
|
|
@@ -78,9 +102,9 @@ function parseVoid(value) {
|
|
|
78
102
|
function strict(typeSymbol, parse) {
|
|
79
103
|
return { mode: 'strict', typeSymbol, create: () => ({ parse }), decode: parse };
|
|
80
104
|
}
|
|
81
|
-
const IDENTITY_CODEC = strict(exports.TIER_PACKAGE
|
|
82
|
-
const TIER_CODEC = strict(exports.TIER_PACKAGE
|
|
83
|
-
const VIEW_CODEC = strict(exports.TIER_PACKAGE
|
|
105
|
+
const IDENTITY_CODEC = strict(`${exports.TIER_PACKAGE}.TierIdentity`, parseTierIdentity);
|
|
106
|
+
const TIER_CODEC = strict(`${exports.TIER_PACKAGE}.TierSelection`, parseTier);
|
|
107
|
+
const VIEW_CODEC = strict(`${exports.TIER_PACKAGE}.TierView`, parseTierView);
|
|
84
108
|
const VOID_CODEC = strict('void', parseVoid);
|
|
85
109
|
const ROUTE_PARAMETERS = [
|
|
86
110
|
{ name: 'provider', wire: 'provider', source: 'json', codec: IDENTITY_CODEC },
|
|
@@ -90,12 +114,231 @@ const ROUTE_PARAMETERS = [
|
|
|
90
114
|
exports.TIER_CONTRIBUTION = {
|
|
91
115
|
package: exports.TIER_PACKAGE,
|
|
92
116
|
descriptors: [
|
|
93
|
-
{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
117
|
+
{
|
|
118
|
+
id: `${exports.TIER_NAMESPACE}/describe`,
|
|
119
|
+
service: exports.TIER_NAMESPACE,
|
|
120
|
+
namespace: exports.TIER_NAMESPACE,
|
|
121
|
+
method: 'describe',
|
|
122
|
+
invocation: { kind: 'direct' },
|
|
123
|
+
parameters: ROUTE_PARAMETERS,
|
|
124
|
+
cancellation: { parameter: 'signal' },
|
|
125
|
+
result: VIEW_CODEC,
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
id: `${exports.TIER_NAMESPACE}/select`,
|
|
129
|
+
service: exports.TIER_NAMESPACE,
|
|
130
|
+
namespace: exports.TIER_NAMESPACE,
|
|
131
|
+
method: 'select',
|
|
132
|
+
invocation: { kind: 'direct' },
|
|
133
|
+
parameters: [
|
|
134
|
+
...ROUTE_PARAMETERS,
|
|
135
|
+
{ name: 'tier', wire: 'tier', source: 'json', acceptsUndefined: true, codec: TIER_CODEC },
|
|
136
|
+
],
|
|
137
|
+
cancellation: { parameter: 'signal' },
|
|
138
|
+
result: VOID_CODEC,
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
/** Empty or non-text values cannot address an attempt, prompt, or flow. */
|
|
143
|
+
function parseAuthText(value) {
|
|
144
|
+
if (typeof value !== 'string' || value.length === 0)
|
|
145
|
+
throw new TypeError(AUTH_INVALID);
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
/** Cursors are non-negative frame counts; a fractional or negative one is a bug, not a resume point. */
|
|
149
|
+
function parseAuthCursor(value) {
|
|
150
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)
|
|
151
|
+
throw new TypeError(AUTH_INVALID);
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
/** Booleans cross as booleans; a JSON 0/1 would hide a version skew. */
|
|
155
|
+
function parseAuthBoolean(value) {
|
|
156
|
+
if (typeof value !== 'boolean')
|
|
157
|
+
throw new TypeError(AUTH_INVALID);
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
/** One notice, with only the fields its frame declares. */
|
|
161
|
+
function parseAuthNotice(value) {
|
|
162
|
+
const input = record(value, ['message', 'url', 'code']);
|
|
163
|
+
return {
|
|
164
|
+
message: parseAuthText(input.message),
|
|
165
|
+
...(input.url === undefined ? {} : { url: parseAuthText(input.url) }),
|
|
166
|
+
...(input.code === undefined ? {} : { code: parseAuthText(input.code) }),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** One option; the description is display-only context. */
|
|
170
|
+
function parseAuthOption(value) {
|
|
171
|
+
const input = record(value, ['id', 'label', 'description']);
|
|
172
|
+
return {
|
|
173
|
+
id: parseAuthText(input.id),
|
|
174
|
+
label: parseAuthText(input.label),
|
|
175
|
+
...(input.description === undefined ? {} : { description: parseAuthText(input.description) }),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** One prompt; a select must carry choices and the others must not. */
|
|
179
|
+
function parseAuthPrompt(value) {
|
|
180
|
+
const input = record(value, ['promptId', 'kind', 'message', 'placeholder', 'options']);
|
|
181
|
+
const kind = input.kind;
|
|
182
|
+
if (kind !== 'text' && kind !== 'secret' && kind !== 'select')
|
|
183
|
+
throw new TypeError(AUTH_INVALID);
|
|
184
|
+
const base = {
|
|
185
|
+
promptId: parseAuthText(input.promptId),
|
|
186
|
+
kind,
|
|
187
|
+
message: parseAuthText(input.message),
|
|
188
|
+
...(input.placeholder === undefined ? {} : { placeholder: parseAuthText(input.placeholder) }),
|
|
189
|
+
};
|
|
190
|
+
if (kind !== 'select') {
|
|
191
|
+
if (input.options !== undefined)
|
|
192
|
+
throw new TypeError(AUTH_INVALID);
|
|
193
|
+
return base;
|
|
194
|
+
}
|
|
195
|
+
if (!Array.isArray(input.options) || input.options.length === 0)
|
|
196
|
+
throw new TypeError(AUTH_INVALID);
|
|
197
|
+
return { ...base, options: input.options.map(parseAuthOption) };
|
|
198
|
+
}
|
|
199
|
+
/** One frame; the tag decides which members must be present. */
|
|
200
|
+
function parseAuthEvent(value) {
|
|
201
|
+
const tagged = value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
202
|
+
if (!tagged || !('type' in value))
|
|
203
|
+
throw new TypeError(AUTH_INVALID);
|
|
204
|
+
switch (value.type) {
|
|
205
|
+
case 'notice': {
|
|
206
|
+
const input = record(value, ['type', 'notice']);
|
|
207
|
+
return { type: 'notice', notice: parseAuthNotice(input.notice) };
|
|
208
|
+
}
|
|
209
|
+
case 'prompt': {
|
|
210
|
+
const input = record(value, ['type', 'prompt']);
|
|
211
|
+
return { type: 'prompt', prompt: parseAuthPrompt(input.prompt) };
|
|
212
|
+
}
|
|
213
|
+
case 'prompt-closed': {
|
|
214
|
+
const input = record(value, ['type', 'promptId']);
|
|
215
|
+
return { type: 'prompt-closed', promptId: parseAuthText(input.promptId) };
|
|
216
|
+
}
|
|
217
|
+
case 'settled': {
|
|
218
|
+
const input = record(value, ['type', 'status', 'message']);
|
|
219
|
+
const status = input.status;
|
|
220
|
+
if (status !== 'authorized' && status !== 'cancelled' && status !== 'failed')
|
|
221
|
+
throw new TypeError(AUTH_INVALID);
|
|
222
|
+
return {
|
|
223
|
+
type: 'settled',
|
|
224
|
+
status,
|
|
225
|
+
...(input.message === undefined ? {} : { message: parseAuthText(input.message) }),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
default:
|
|
229
|
+
throw new TypeError(AUTH_INVALID);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** One flow list; a duplicated key would render two controls for one credential. */
|
|
233
|
+
function parseAuthEntries(value) {
|
|
234
|
+
if (!Array.isArray(value))
|
|
235
|
+
throw new TypeError(AUTH_INVALID);
|
|
236
|
+
const entries = value.map((item) => {
|
|
237
|
+
const input = record(item, ['key', 'provider', 'label', 'stored', 'inFlight']);
|
|
238
|
+
return {
|
|
239
|
+
key: parseAuthText(input.key),
|
|
240
|
+
provider: parseAuthText(input.provider),
|
|
241
|
+
label: parseAuthText(input.label),
|
|
242
|
+
stored: parseAuthBoolean(input.stored),
|
|
243
|
+
inFlight: parseAuthBoolean(input.inFlight),
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
if (new Set(entries.map((entry) => entry.key)).size !== entries.length)
|
|
247
|
+
throw new TypeError(AUTH_INVALID);
|
|
248
|
+
return entries;
|
|
249
|
+
}
|
|
250
|
+
/** One start result. */
|
|
251
|
+
function parseAuthStart(value) {
|
|
252
|
+
const input = record(value, ['attemptId']);
|
|
253
|
+
return { attemptId: parseAuthText(input.attemptId) };
|
|
254
|
+
}
|
|
255
|
+
/** One poll result; `done` without the settlement frame would strand the panel. */
|
|
256
|
+
function parseAuthPoll(value) {
|
|
257
|
+
const input = record(value, ['cursor', 'events', 'done']);
|
|
258
|
+
if (!Array.isArray(input.events))
|
|
259
|
+
throw new TypeError(AUTH_INVALID);
|
|
260
|
+
const events = input.events.map(parseAuthEvent);
|
|
261
|
+
const done = parseAuthBoolean(input.done);
|
|
262
|
+
if (done && events.at(-1)?.type !== 'settled')
|
|
263
|
+
throw new TypeError(AUTH_INVALID);
|
|
264
|
+
return { cursor: parseAuthCursor(input.cursor), events, done };
|
|
265
|
+
}
|
|
266
|
+
const AUTH_TEXT_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthText`, parseAuthText);
|
|
267
|
+
const AUTH_CURSOR_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthCursor`, parseAuthCursor);
|
|
268
|
+
const AUTH_BOOLEAN_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthBoolean`, parseAuthBoolean);
|
|
269
|
+
const AUTH_ENTRIES_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthEntries`, parseAuthEntries);
|
|
270
|
+
const AUTH_START_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthStart`, parseAuthStart);
|
|
271
|
+
const AUTH_POLL_CODEC = strict(`${exports.AUTH_PACKAGE}.AuthPoll`, parseAuthPoll);
|
|
272
|
+
/** The attempt transport is poll-based so the Host needs no stream descriptor. */
|
|
273
|
+
exports.AUTH_CONTRIBUTION = {
|
|
274
|
+
package: exports.AUTH_PACKAGE,
|
|
275
|
+
descriptors: [
|
|
276
|
+
{
|
|
277
|
+
id: `${exports.AUTH_NAMESPACE}/list`,
|
|
278
|
+
service: exports.AUTH_NAMESPACE,
|
|
279
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
280
|
+
method: 'list',
|
|
281
|
+
invocation: { kind: 'direct' },
|
|
282
|
+
parameters: [],
|
|
283
|
+
result: AUTH_ENTRIES_CODEC,
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
id: `${exports.AUTH_NAMESPACE}/begin`,
|
|
287
|
+
service: exports.AUTH_NAMESPACE,
|
|
288
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
289
|
+
method: 'begin',
|
|
290
|
+
invocation: { kind: 'direct' },
|
|
291
|
+
parameters: [{ name: 'key', wire: 'key', source: 'json', codec: AUTH_TEXT_CODEC }],
|
|
292
|
+
result: AUTH_START_CODEC,
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
id: `${exports.AUTH_NAMESPACE}/poll`,
|
|
296
|
+
service: exports.AUTH_NAMESPACE,
|
|
297
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
298
|
+
method: 'poll',
|
|
299
|
+
invocation: { kind: 'direct' },
|
|
300
|
+
parameters: [
|
|
301
|
+
{ name: 'attemptId', wire: 'attemptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
302
|
+
{ name: 'cursor', wire: 'cursor', source: 'json', codec: AUTH_CURSOR_CODEC },
|
|
303
|
+
],
|
|
304
|
+
result: AUTH_POLL_CODEC,
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
id: `${exports.AUTH_NAMESPACE}/answer`,
|
|
308
|
+
service: exports.AUTH_NAMESPACE,
|
|
309
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
310
|
+
method: 'answer',
|
|
311
|
+
invocation: { kind: 'direct' },
|
|
312
|
+
parameters: [
|
|
313
|
+
{ name: 'attemptId', wire: 'attemptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
314
|
+
{ name: 'promptId', wire: 'promptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
315
|
+
{ name: 'value', wire: 'value', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
316
|
+
],
|
|
317
|
+
result: AUTH_BOOLEAN_CODEC,
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
id: `${exports.AUTH_NAMESPACE}/decline`,
|
|
321
|
+
service: exports.AUTH_NAMESPACE,
|
|
322
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
323
|
+
method: 'decline',
|
|
324
|
+
invocation: { kind: 'direct' },
|
|
325
|
+
parameters: [
|
|
326
|
+
{ name: 'attemptId', wire: 'attemptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
327
|
+
{ name: 'promptId', wire: 'promptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
328
|
+
],
|
|
329
|
+
result: AUTH_BOOLEAN_CODEC,
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
id: `${exports.AUTH_NAMESPACE}/cancel`,
|
|
333
|
+
service: exports.AUTH_NAMESPACE,
|
|
334
|
+
namespace: exports.AUTH_NAMESPACE,
|
|
335
|
+
method: 'cancel',
|
|
336
|
+
invocation: { kind: 'direct' },
|
|
337
|
+
parameters: [
|
|
338
|
+
{ name: 'attemptId', wire: 'attemptId', source: 'json', codec: AUTH_TEXT_CODEC },
|
|
339
|
+
],
|
|
340
|
+
result: VOID_CODEC,
|
|
341
|
+
},
|
|
99
342
|
],
|
|
100
343
|
};
|
|
101
344
|
/** Remote failures contain transport detail that must not become UI error text. */
|
|
@@ -105,9 +348,605 @@ function resultValue(result, message) {
|
|
|
105
348
|
const envelope = record(result, ['ok', 'value']);
|
|
106
349
|
return envelope.value;
|
|
107
350
|
}
|
|
108
|
-
/**
|
|
351
|
+
/** Open the menu for the session's current route; any route change after this cancels its rows. */
|
|
352
|
+
async function openTierMenu(menu, session, requestSignal) {
|
|
353
|
+
const opening = new AbortController();
|
|
354
|
+
const signal = AbortSignal.any([requestSignal, menu.lifetime, menu.registered, opening.signal]);
|
|
355
|
+
signal.throwIfAborted();
|
|
356
|
+
if (!menu.available(session))
|
|
357
|
+
return [];
|
|
358
|
+
const directory = menu.models.directoryFor(session.sessionId);
|
|
359
|
+
const current = directory.store.getSnapshot().current;
|
|
360
|
+
// The menu has nothing to offer until the session has chosen a model.
|
|
361
|
+
if (!current)
|
|
362
|
+
return [];
|
|
363
|
+
const provider = parseTierIdentity(current.provider);
|
|
364
|
+
const model = parseTierIdentity(current.model);
|
|
365
|
+
const invalidate = () => {
|
|
366
|
+
const latest = directory.store.getSnapshot().current;
|
|
367
|
+
try {
|
|
368
|
+
if (menu.available(session) &&
|
|
369
|
+
menu.models.directoryFor(session.sessionId) === directory &&
|
|
370
|
+
latest?.provider === provider &&
|
|
371
|
+
latest.model === model)
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
/* A removed session scope must not retain permission to write. */
|
|
376
|
+
}
|
|
377
|
+
opening.abort(new Error(STALE_ROUTE));
|
|
378
|
+
};
|
|
379
|
+
const checkRoute = () => {
|
|
380
|
+
invalidate();
|
|
381
|
+
signal.throwIfAborted();
|
|
382
|
+
};
|
|
383
|
+
// A route that changes away and back still invalidates the original billing decision.
|
|
384
|
+
const unsubscribe = directory.store.subscribe(invalidate);
|
|
385
|
+
signal.addEventListener('abort', unsubscribe, { once: true });
|
|
386
|
+
let view;
|
|
387
|
+
try {
|
|
388
|
+
await directory.load();
|
|
389
|
+
checkRoute();
|
|
390
|
+
view = parseTierView(resultValue(await menu.tiers.describe(provider, model, signal), READ_ERROR));
|
|
391
|
+
checkRoute();
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
const error = signal.aborted ? signal.reason : new Error(READ_ERROR);
|
|
395
|
+
opening.abort();
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
if (view.choices.length === 0) {
|
|
399
|
+
opening.abort();
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
const options = [
|
|
403
|
+
{ id: exports.TIER_DEFAULT, label: DEFAULT_LABEL, active: view.current === undefined },
|
|
404
|
+
...view.choices.map((choice) => ({
|
|
405
|
+
id: choice.id,
|
|
406
|
+
label: choice.name,
|
|
407
|
+
detail: choice.description,
|
|
408
|
+
active: view.current === choice.id,
|
|
409
|
+
})),
|
|
410
|
+
];
|
|
411
|
+
for (const option of options) {
|
|
412
|
+
const tier = option.id === exports.TIER_DEFAULT ? undefined : option.id;
|
|
413
|
+
menu.selections.set(option, {
|
|
414
|
+
sessionId: session.sessionId,
|
|
415
|
+
async select() {
|
|
416
|
+
checkRoute();
|
|
417
|
+
try {
|
|
418
|
+
parseVoid(resultValue(await menu.tiers.select(provider, model, tier, signal), exports.TIER_SAVE_ERROR));
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
signal.throwIfAborted();
|
|
422
|
+
throw new Error(exports.TIER_SAVE_ERROR);
|
|
423
|
+
}
|
|
424
|
+
checkRoute();
|
|
425
|
+
// The stock shell does not abort its signal on successful selection.
|
|
426
|
+
opening.abort();
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
return options;
|
|
431
|
+
}
|
|
432
|
+
/** Resolve React only when the browser half runs; the Host bundle has no React. */
|
|
433
|
+
function reactRuntime() {
|
|
434
|
+
return require('react');
|
|
435
|
+
}
|
|
436
|
+
/** Only a keyless OAuth route gets a sign-in control; a key field already covers the rest. */
|
|
437
|
+
function signInEligible(props) {
|
|
438
|
+
const entry = props.provider;
|
|
439
|
+
return (entry !== undefined &&
|
|
440
|
+
SIGN_IN_PROVIDERS.includes(entry.provider) &&
|
|
441
|
+
entry.nativeAuth?.oauth === true &&
|
|
442
|
+
(entry.nativeAuth.env?.length ?? 0) === 0);
|
|
443
|
+
}
|
|
444
|
+
/** Keep transport detail out of the panel while still naming what failed. */
|
|
445
|
+
function authFailure(error) {
|
|
446
|
+
if (error instanceof Error && error.message.length > 0)
|
|
447
|
+
return error.message;
|
|
448
|
+
return AUTH_POLL_ERROR;
|
|
449
|
+
}
|
|
450
|
+
const AUTH_STYLES = {
|
|
451
|
+
wrap: { display: 'flex', flexDirection: 'column', gap: '8px', marginTop: '8px' },
|
|
452
|
+
row: { display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: '8px' },
|
|
453
|
+
button: {
|
|
454
|
+
alignSelf: 'flex-start',
|
|
455
|
+
border: '1px solid var(--dsh-border, #555)',
|
|
456
|
+
borderRadius: '6px',
|
|
457
|
+
background: 'transparent',
|
|
458
|
+
color: 'inherit',
|
|
459
|
+
cursor: 'pointer',
|
|
460
|
+
font: 'inherit',
|
|
461
|
+
padding: '4px 12px',
|
|
462
|
+
},
|
|
463
|
+
primary: {
|
|
464
|
+
alignSelf: 'flex-start',
|
|
465
|
+
border: '1px solid transparent',
|
|
466
|
+
borderRadius: '6px',
|
|
467
|
+
background: 'var(--dsh-accent, #3b82f6)',
|
|
468
|
+
color: 'var(--dsh-accent-foreground, #ffffff)',
|
|
469
|
+
cursor: 'pointer',
|
|
470
|
+
font: 'inherit',
|
|
471
|
+
padding: '4px 12px',
|
|
472
|
+
},
|
|
473
|
+
hint: { opacity: 0.7, fontSize: '0.9em' },
|
|
474
|
+
notice: { margin: 0 },
|
|
475
|
+
error: { color: '#e5484d', margin: 0 },
|
|
476
|
+
field: { display: 'flex', flexDirection: 'column', gap: '6px' },
|
|
477
|
+
input: {
|
|
478
|
+
border: '1px solid currentColor',
|
|
479
|
+
borderRadius: '6px',
|
|
480
|
+
background: 'transparent',
|
|
481
|
+
color: 'inherit',
|
|
482
|
+
font: 'inherit',
|
|
483
|
+
padding: '4px 8px',
|
|
484
|
+
width: '100%',
|
|
485
|
+
},
|
|
486
|
+
link: { color: 'inherit', textDecoration: 'underline', cursor: 'pointer' },
|
|
487
|
+
};
|
|
488
|
+
/** Clipboard access can fail without permission; the label reports which happened. */
|
|
489
|
+
async function copyToClipboard(value) {
|
|
490
|
+
try {
|
|
491
|
+
await navigator.clipboard.writeText(value);
|
|
492
|
+
return 'copied';
|
|
493
|
+
}
|
|
494
|
+
catch {
|
|
495
|
+
return 'failed';
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/** Read and validate the flow list; an unavailable Host reads as no flows. */
|
|
499
|
+
async function readAuthEntries(auth) {
|
|
500
|
+
return parseAuthEntries(resultValue(await auth.list(), AUTH_LIST_ERROR));
|
|
501
|
+
}
|
|
502
|
+
/** Start one attempt and return the handle its poller uses. */
|
|
503
|
+
async function beginAttempt(auth, key) {
|
|
504
|
+
return parseAuthStart(resultValue(await auth.begin(key), AUTH_START_ERROR)).attemptId;
|
|
505
|
+
}
|
|
506
|
+
/** Poll one attempt until it settles or the panel closes. */
|
|
507
|
+
async function pollAttempt(target) {
|
|
508
|
+
let cursor = 0;
|
|
509
|
+
while (target.live()) {
|
|
510
|
+
const poll = parseAuthPoll(resultValue(await target.auth.poll(target.attemptId, cursor), AUTH_POLL_ERROR));
|
|
511
|
+
cursor = poll.cursor;
|
|
512
|
+
for (const event of poll.events)
|
|
513
|
+
target.apply(event);
|
|
514
|
+
if (poll.done)
|
|
515
|
+
return;
|
|
516
|
+
await new Promise((resolve) => {
|
|
517
|
+
setTimeout(resolve, AUTH_POLL_MS);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** One notice, with only the actions its own fields warrant. */
|
|
522
|
+
function SignInNotice(props) {
|
|
523
|
+
const { createElement: h } = reactRuntime();
|
|
524
|
+
const { notice } = props;
|
|
525
|
+
return h('div', { style: AUTH_STYLES.field }, [
|
|
526
|
+
h('p', { key: 'message', style: AUTH_STYLES.notice }, notice.message),
|
|
527
|
+
notice.url === undefined
|
|
528
|
+
? null
|
|
529
|
+
: h('div', { key: 'url', style: AUTH_STYLES.row }, [
|
|
530
|
+
h('a', {
|
|
531
|
+
key: 'open',
|
|
532
|
+
href: notice.url,
|
|
533
|
+
target: '_blank',
|
|
534
|
+
rel: 'noreferrer noopener',
|
|
535
|
+
style: AUTH_STYLES.link,
|
|
536
|
+
}, 'Open sign-in page'),
|
|
537
|
+
h('button', {
|
|
538
|
+
key: 'copy',
|
|
539
|
+
type: 'button',
|
|
540
|
+
style: AUTH_STYLES.button,
|
|
541
|
+
onClick: () => {
|
|
542
|
+
props.onCopy(notice.url ?? '');
|
|
543
|
+
},
|
|
544
|
+
}, props.copy === 'copied'
|
|
545
|
+
? 'Copied'
|
|
546
|
+
: props.copy === 'failed'
|
|
547
|
+
? AUTH_COPY_FAILED
|
|
548
|
+
: 'Copy link'),
|
|
549
|
+
]),
|
|
550
|
+
notice.code === undefined
|
|
551
|
+
? null
|
|
552
|
+
: h('p', { key: 'code', style: AUTH_STYLES.notice }, `Verification code: ${notice.code}`),
|
|
553
|
+
]);
|
|
554
|
+
}
|
|
555
|
+
/** One question: choices are buttons, typed answers are an input. */
|
|
556
|
+
function SignInPrompt(props) {
|
|
557
|
+
const { createElement: h } = reactRuntime();
|
|
558
|
+
const { prompt, busy } = props;
|
|
559
|
+
const choices = prompt.kind === 'select'
|
|
560
|
+
? h('div', { key: 'options', style: AUTH_STYLES.row }, (prompt.options ?? []).map((option) => h('button', {
|
|
561
|
+
key: option.id,
|
|
562
|
+
type: 'button',
|
|
563
|
+
disabled: busy,
|
|
564
|
+
style: AUTH_STYLES.button,
|
|
565
|
+
onClick: () => {
|
|
566
|
+
props.onSubmit(option.id);
|
|
567
|
+
},
|
|
568
|
+
}, option.label)))
|
|
569
|
+
: h('div', { key: 'typed', style: AUTH_STYLES.field }, [
|
|
570
|
+
h('input', {
|
|
571
|
+
key: 'input',
|
|
572
|
+
type: prompt.kind === 'secret' ? 'password' : 'text',
|
|
573
|
+
value: props.answer,
|
|
574
|
+
maxLength: AUTH_ANSWER_MAX,
|
|
575
|
+
placeholder: prompt.placeholder ?? '',
|
|
576
|
+
'aria-label': prompt.message,
|
|
577
|
+
disabled: busy,
|
|
578
|
+
style: AUTH_STYLES.input,
|
|
579
|
+
onChange: (event) => {
|
|
580
|
+
props.onAnswer(event.target.value);
|
|
581
|
+
},
|
|
582
|
+
onKeyDown: (event) => {
|
|
583
|
+
if (event.key === 'Enter')
|
|
584
|
+
props.onSubmit(props.answer);
|
|
585
|
+
},
|
|
586
|
+
}),
|
|
587
|
+
h('div', { key: 'actions', style: AUTH_STYLES.row }, [
|
|
588
|
+
h('button', {
|
|
589
|
+
key: 'continue',
|
|
590
|
+
type: 'button',
|
|
591
|
+
disabled: busy || props.answer.trim() === '',
|
|
592
|
+
style: AUTH_STYLES.primary,
|
|
593
|
+
onClick: () => {
|
|
594
|
+
props.onSubmit(props.answer);
|
|
595
|
+
},
|
|
596
|
+
}, 'Continue'),
|
|
597
|
+
h('button', {
|
|
598
|
+
key: 'decline',
|
|
599
|
+
type: 'button',
|
|
600
|
+
disabled: busy,
|
|
601
|
+
style: AUTH_STYLES.button,
|
|
602
|
+
onClick: props.onDecline,
|
|
603
|
+
}, 'Decline'),
|
|
604
|
+
]),
|
|
605
|
+
]);
|
|
606
|
+
return h('div', { style: AUTH_STYLES.field }, [
|
|
607
|
+
h('p', { key: 'message', style: AUTH_STYLES.notice }, prompt.message),
|
|
608
|
+
choices,
|
|
609
|
+
]);
|
|
610
|
+
}
|
|
611
|
+
/** The opened panel: instructions, the current question, and the exit. */
|
|
612
|
+
function SignInPanel(props) {
|
|
613
|
+
const { createElement: h } = reactRuntime();
|
|
614
|
+
const running = props.status === 'running';
|
|
615
|
+
const outcome = props.status === 'authorized'
|
|
616
|
+
? h('p', { key: 'ok', style: AUTH_STYLES.notice }, 'Signed in. No API key was stored.')
|
|
617
|
+
: props.status === 'cancelled'
|
|
618
|
+
? h('p', { key: 'cancelled', style: AUTH_STYLES.notice }, 'Sign-in was cancelled.')
|
|
619
|
+
: null;
|
|
620
|
+
return h('div', { style: AUTH_STYLES.field }, [
|
|
621
|
+
props.failure === undefined
|
|
622
|
+
? null
|
|
623
|
+
: h('p', { key: 'failure', style: AUTH_STYLES.error }, props.failure),
|
|
624
|
+
props.notice === undefined
|
|
625
|
+
? null
|
|
626
|
+
: h(SignInNotice, {
|
|
627
|
+
key: 'notice',
|
|
628
|
+
notice: props.notice,
|
|
629
|
+
copy: props.copy,
|
|
630
|
+
onCopy: props.onCopy,
|
|
631
|
+
}),
|
|
632
|
+
props.prompt === undefined
|
|
633
|
+
? null
|
|
634
|
+
: h(SignInPrompt, {
|
|
635
|
+
key: 'prompt',
|
|
636
|
+
prompt: props.prompt,
|
|
637
|
+
answer: props.answer,
|
|
638
|
+
busy: props.busy,
|
|
639
|
+
onAnswer: props.onAnswer,
|
|
640
|
+
onSubmit: props.onSubmit,
|
|
641
|
+
onDecline: props.onDecline,
|
|
642
|
+
}),
|
|
643
|
+
running
|
|
644
|
+
? h('p', { key: 'waiting', style: AUTH_STYLES.hint }, 'Waiting for the provider…')
|
|
645
|
+
: null,
|
|
646
|
+
outcome,
|
|
647
|
+
h('button', { key: 'close', type: 'button', style: AUTH_STYLES.button, onClick: props.onClose }, running ? 'Cancel' : 'Close'),
|
|
648
|
+
]);
|
|
649
|
+
}
|
|
650
|
+
/** Poll one attempt for as long as the panel is open, reporting failures as a settlement. */
|
|
651
|
+
function useAttemptPolling(auth, attemptId, apply, onFailure) {
|
|
652
|
+
const { useEffect, useRef } = reactRuntime();
|
|
653
|
+
// The callbacks are fresh each render; only the attempt identity may restart
|
|
654
|
+
// the poller, or every frame would replay from cursor zero.
|
|
655
|
+
const sink = useRef(apply);
|
|
656
|
+
sink.current = apply;
|
|
657
|
+
const fail = useRef(onFailure);
|
|
658
|
+
fail.current = onFailure;
|
|
659
|
+
useEffect(() => {
|
|
660
|
+
if (attemptId === undefined)
|
|
661
|
+
return;
|
|
662
|
+
let polling = true;
|
|
663
|
+
void pollAttempt({
|
|
664
|
+
auth,
|
|
665
|
+
attemptId,
|
|
666
|
+
apply: (event) => {
|
|
667
|
+
sink.current(event);
|
|
668
|
+
},
|
|
669
|
+
live: () => polling,
|
|
670
|
+
}).catch((error) => {
|
|
671
|
+
if (polling)
|
|
672
|
+
fail.current(authFailure(error));
|
|
673
|
+
});
|
|
674
|
+
return () => {
|
|
675
|
+
polling = false;
|
|
676
|
+
};
|
|
677
|
+
}, [attemptId, auth]);
|
|
678
|
+
}
|
|
679
|
+
/** Deliver one typed answer; the refusal message is ready for the panel. */
|
|
680
|
+
async function submitAnswer(auth, attemptId, prompt, value) {
|
|
681
|
+
const accepted = parseAuthBoolean(resultValue(await auth.answer(attemptId, prompt.promptId, value), AUTH_ANSWER_ERROR));
|
|
682
|
+
return accepted ? undefined : AUTH_ANSWER_ERROR;
|
|
683
|
+
}
|
|
684
|
+
/** Refuse one question; a transport failure is reported to the caller. */
|
|
685
|
+
async function declineQuestion(auth, attemptId, prompt) {
|
|
686
|
+
await auth.decline(attemptId, prompt.promptId);
|
|
687
|
+
}
|
|
688
|
+
/** The card: a stored-grant summary and the panel an open attempt shows. */
|
|
689
|
+
function ProviderSignInView(props) {
|
|
690
|
+
const { createElement: h } = reactRuntime();
|
|
691
|
+
const stored = props.entry?.stored === true;
|
|
692
|
+
const state = props.entry === undefined
|
|
693
|
+
? (props.failure ?? 'No sign-in flow is registered for this provider')
|
|
694
|
+
: stored
|
|
695
|
+
? 'Signed in'
|
|
696
|
+
: 'Sign in with the provider account';
|
|
697
|
+
return h('div', { style: AUTH_STYLES.wrap }, [
|
|
698
|
+
h('div', { key: 'head', style: AUTH_STYLES.row }, [
|
|
699
|
+
h('button', {
|
|
700
|
+
key: 'button',
|
|
701
|
+
type: 'button',
|
|
702
|
+
disabled: props.entry === undefined || props.busy || props.status === 'running',
|
|
703
|
+
onClick: props.onStart,
|
|
704
|
+
style: stored ? AUTH_STYLES.button : AUTH_STYLES.primary,
|
|
705
|
+
}, props.entry === undefined ? 'Sign-in unavailable' : stored ? 'Sign in again' : 'Sign in'),
|
|
706
|
+
h('span', { key: 'state', style: AUTH_STYLES.hint }, state),
|
|
707
|
+
]),
|
|
708
|
+
props.open
|
|
709
|
+
? h(SignInPanel, {
|
|
710
|
+
key: 'panel',
|
|
711
|
+
notice: props.notice,
|
|
712
|
+
prompt: props.prompt,
|
|
713
|
+
status: props.status,
|
|
714
|
+
failure: props.failure,
|
|
715
|
+
answer: props.answer,
|
|
716
|
+
busy: props.busy,
|
|
717
|
+
copy: props.copy,
|
|
718
|
+
onAnswer: props.onAnswer,
|
|
719
|
+
onSubmit: props.onSubmit,
|
|
720
|
+
onDecline: props.onDecline,
|
|
721
|
+
onCopy: props.onCopy,
|
|
722
|
+
onClose: props.onClose,
|
|
723
|
+
})
|
|
724
|
+
: null,
|
|
725
|
+
]);
|
|
726
|
+
}
|
|
727
|
+
/** Load the flow list once per provider and report a failed read. */
|
|
728
|
+
function useFlowEntries(auth, reload, onFailure) {
|
|
729
|
+
const { useEffect, useRef, useState } = reactRuntime();
|
|
730
|
+
const [entries, setEntries] = useState(undefined);
|
|
731
|
+
const fail = useRef(onFailure);
|
|
732
|
+
fail.current = onFailure;
|
|
733
|
+
useEffect(() => {
|
|
734
|
+
let current = true;
|
|
735
|
+
void readAuthEntries(auth).then((list) => {
|
|
736
|
+
if (current)
|
|
737
|
+
setEntries(list);
|
|
738
|
+
}, (error) => {
|
|
739
|
+
if (!current)
|
|
740
|
+
return;
|
|
741
|
+
setEntries([]);
|
|
742
|
+
fail.current(authFailure(error));
|
|
743
|
+
});
|
|
744
|
+
return () => {
|
|
745
|
+
current = false;
|
|
746
|
+
};
|
|
747
|
+
}, [auth, reload]);
|
|
748
|
+
return entries;
|
|
749
|
+
}
|
|
750
|
+
/** Copy feedback, reset after the label has been read. */
|
|
751
|
+
function useCopyFeedback() {
|
|
752
|
+
const { useEffect, useState } = reactRuntime();
|
|
753
|
+
const [copy, setCopy] = useState(undefined);
|
|
754
|
+
useEffect(() => {
|
|
755
|
+
if (copy === undefined)
|
|
756
|
+
return;
|
|
757
|
+
const timer = setTimeout(() => {
|
|
758
|
+
setCopy(undefined);
|
|
759
|
+
}, 2000);
|
|
760
|
+
return () => {
|
|
761
|
+
clearTimeout(timer);
|
|
762
|
+
};
|
|
763
|
+
}, [copy]);
|
|
764
|
+
return {
|
|
765
|
+
copy,
|
|
766
|
+
copyValue: (value) => {
|
|
767
|
+
void copyToClipboard(value).then(setCopy);
|
|
768
|
+
},
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
/** Withdraw whatever attempt is still running when the card unmounts. */
|
|
772
|
+
function useCancelOnUnmount(auth, running) {
|
|
773
|
+
const { useEffect, useRef } = reactRuntime();
|
|
774
|
+
const live = useRef(undefined);
|
|
775
|
+
live.current = running;
|
|
776
|
+
useEffect(() => {
|
|
777
|
+
return () => {
|
|
778
|
+
const attempt = live.current;
|
|
779
|
+
if (attempt !== undefined)
|
|
780
|
+
void auth.cancel(attempt);
|
|
781
|
+
};
|
|
782
|
+
}, [auth]);
|
|
783
|
+
}
|
|
784
|
+
const EMPTY_FLOW = { open: false, answer: '', busy: false };
|
|
785
|
+
/** Keep one attempt's visible state in one place, so no handler clears it halfway. */
|
|
786
|
+
function reduceFlow(state, action) {
|
|
787
|
+
switch (action.type) {
|
|
788
|
+
case 'open':
|
|
789
|
+
return {
|
|
790
|
+
...EMPTY_FLOW,
|
|
791
|
+
open: true,
|
|
792
|
+
busy: true,
|
|
793
|
+
status: 'running',
|
|
794
|
+
};
|
|
795
|
+
case 'busy':
|
|
796
|
+
return { ...state, busy: action.busy };
|
|
797
|
+
case 'started':
|
|
798
|
+
return { ...state, busy: false, attemptId: action.attemptId };
|
|
799
|
+
case 'failed':
|
|
800
|
+
return { ...state, busy: false, status: 'failed', failure: action.message };
|
|
801
|
+
case 'notice':
|
|
802
|
+
return { ...state, notice: action.notice };
|
|
803
|
+
case 'prompt':
|
|
804
|
+
return { ...state, prompt: action.prompt, answer: '' };
|
|
805
|
+
case 'prompt-closed':
|
|
806
|
+
return state.prompt?.promptId === action.promptId ? { ...state, prompt: undefined } : state;
|
|
807
|
+
case 'answer':
|
|
808
|
+
return { ...state, answer: action.value };
|
|
809
|
+
case 'settled':
|
|
810
|
+
return {
|
|
811
|
+
...state,
|
|
812
|
+
prompt: undefined,
|
|
813
|
+
status: action.status,
|
|
814
|
+
...(action.status === 'failed' ? { failure: action.message ?? 'Sign-in failed.' } : {}),
|
|
815
|
+
};
|
|
816
|
+
case 'close':
|
|
817
|
+
return EMPTY_FLOW;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
/** Map one polled frame onto its transition. */
|
|
821
|
+
function flowAction(event) {
|
|
822
|
+
if (event.type === 'notice')
|
|
823
|
+
return { type: 'notice', notice: event.notice };
|
|
824
|
+
if (event.type === 'prompt')
|
|
825
|
+
return { type: 'prompt', prompt: event.prompt };
|
|
826
|
+
if (event.type === 'prompt-closed')
|
|
827
|
+
return { type: 'prompt-closed', promptId: event.promptId };
|
|
828
|
+
return {
|
|
829
|
+
type: 'settled',
|
|
830
|
+
status: event.status,
|
|
831
|
+
...(event.message === undefined ? {} : { message: event.message }),
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
/** The clickable face of one provider's stored grant and running attempt. */
|
|
835
|
+
function ProviderSignInCard(props) {
|
|
836
|
+
const { createElement: h, useReducer, useState } = reactRuntime();
|
|
837
|
+
const [ui, dispatch] = useReducer(reduceFlow, EMPTY_FLOW);
|
|
838
|
+
const [reload, setReload] = useState(0);
|
|
839
|
+
const fail = (message) => {
|
|
840
|
+
dispatch({ type: 'failed', message });
|
|
841
|
+
};
|
|
842
|
+
const entries = useFlowEntries(props.auth, reload, fail);
|
|
843
|
+
const entry = entries?.find((candidate) => candidate.provider === props.provider.provider);
|
|
844
|
+
const { copy, copyValue } = useCopyFeedback();
|
|
845
|
+
useCancelOnUnmount(props.auth, ui.status === 'running' ? ui.attemptId : undefined);
|
|
846
|
+
const apply = (event) => {
|
|
847
|
+
dispatch(flowAction(event));
|
|
848
|
+
if (event.type === 'settled' && event.status === 'authorized')
|
|
849
|
+
setReload((value) => value + 1);
|
|
850
|
+
};
|
|
851
|
+
useAttemptPolling(props.auth, ui.attemptId, apply, (message) => {
|
|
852
|
+
dispatch({ type: 'failed', message });
|
|
853
|
+
});
|
|
854
|
+
const start = async () => {
|
|
855
|
+
if (entry === undefined || ui.busy)
|
|
856
|
+
return;
|
|
857
|
+
dispatch({ type: 'open' });
|
|
858
|
+
try {
|
|
859
|
+
dispatch({ type: 'started', attemptId: await beginAttempt(props.auth, entry.key) });
|
|
860
|
+
}
|
|
861
|
+
catch (error) {
|
|
862
|
+
dispatch({ type: 'failed', message: authFailure(error) });
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
const close = () => {
|
|
866
|
+
if (ui.status === 'running' && ui.attemptId !== undefined)
|
|
867
|
+
void props.auth.cancel(ui.attemptId);
|
|
868
|
+
dispatch({ type: 'close' });
|
|
869
|
+
};
|
|
870
|
+
const submit = async (value) => {
|
|
871
|
+
const { prompt, attemptId } = ui;
|
|
872
|
+
if (prompt === undefined || attemptId === undefined || ui.busy || value.trim() === '')
|
|
873
|
+
return;
|
|
874
|
+
dispatch({ type: 'busy', busy: true });
|
|
875
|
+
try {
|
|
876
|
+
const refusal = await submitAnswer(props.auth, attemptId, prompt, value);
|
|
877
|
+
if (refusal === undefined) {
|
|
878
|
+
dispatch({ type: 'answer', value: '' });
|
|
879
|
+
dispatch({ type: 'prompt-closed', promptId: prompt.promptId });
|
|
880
|
+
}
|
|
881
|
+
else
|
|
882
|
+
dispatch({ type: 'failed', message: refusal });
|
|
883
|
+
}
|
|
884
|
+
catch (error) {
|
|
885
|
+
dispatch({ type: 'failed', message: authFailure(error) });
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
const decline = async () => {
|
|
889
|
+
const { prompt, attemptId } = ui;
|
|
890
|
+
if (prompt === undefined || attemptId === undefined || ui.busy)
|
|
891
|
+
return;
|
|
892
|
+
dispatch({ type: 'busy', busy: true });
|
|
893
|
+
try {
|
|
894
|
+
await declineQuestion(props.auth, attemptId, prompt);
|
|
895
|
+
dispatch({ type: 'prompt-closed', promptId: prompt.promptId });
|
|
896
|
+
}
|
|
897
|
+
catch (error) {
|
|
898
|
+
dispatch({ type: 'failed', message: authFailure(error) });
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
return h(ProviderSignInView, {
|
|
902
|
+
entry,
|
|
903
|
+
open: ui.open,
|
|
904
|
+
status: ui.status,
|
|
905
|
+
notice: ui.notice,
|
|
906
|
+
prompt: ui.prompt,
|
|
907
|
+
failure: ui.failure,
|
|
908
|
+
answer: ui.answer,
|
|
909
|
+
busy: ui.busy,
|
|
910
|
+
copy,
|
|
911
|
+
onStart: () => {
|
|
912
|
+
void start();
|
|
913
|
+
},
|
|
914
|
+
onAnswer: (value) => {
|
|
915
|
+
dispatch({ type: 'answer', value });
|
|
916
|
+
},
|
|
917
|
+
onSubmit: (value) => {
|
|
918
|
+
void submit(value);
|
|
919
|
+
},
|
|
920
|
+
onDecline: () => {
|
|
921
|
+
void decline();
|
|
922
|
+
},
|
|
923
|
+
onCopy: copyValue,
|
|
924
|
+
onClose: close,
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
/** Bind the card to one page's Remote namespace; eligibility runs before any hook. */
|
|
928
|
+
function createProviderSignIn(auth) {
|
|
929
|
+
return (props) => signInEligible(props)
|
|
930
|
+
? reactRuntime().createElement(ProviderSignInCard, { ...props, auth })
|
|
931
|
+
: null;
|
|
932
|
+
}
|
|
933
|
+
/** Mount the attempt namespace and register the Models card extension. */
|
|
934
|
+
function applyProviderSignIn(ctx) {
|
|
935
|
+
ctx.inject([REMOTE_SERVICE], (owner) => {
|
|
936
|
+
const remote = owner.get(REMOTE_SERVICE);
|
|
937
|
+
owner.effect(async () => {
|
|
938
|
+
const unmount = await remote.$mount(exports.AUTH_CONTRIBUTION);
|
|
939
|
+
owner.inject(['slots', AUTH_REMOTE_SERVICE], (scope) => {
|
|
940
|
+
const slots = scope.get('slots');
|
|
941
|
+
const auth = scope.get(AUTH_REMOTE_SERVICE);
|
|
942
|
+
slots.inject('settings.models.provider-card', () => slots.register({ name: 'settings.models.provider-card', key: PI_AI_SETTINGS_NS }, createProviderSignIn(auth)));
|
|
943
|
+
});
|
|
944
|
+
return unmount;
|
|
945
|
+
});
|
|
946
|
+
});
|
|
947
|
+
}
|
|
109
948
|
function apply(ctx) {
|
|
110
|
-
ctx.inject([COMMAND_SERVICE, MODEL_SERVICE, SESSION_SERVICE, REMOTE_SERVICE], owner => {
|
|
949
|
+
ctx.inject([COMMAND_SERVICE, MODEL_SERVICE, SESSION_SERVICE, REMOTE_SERVICE], (owner) => {
|
|
111
950
|
const command = owner.get(COMMAND_SERVICE);
|
|
112
951
|
const models = owner.get(MODEL_SERVICE);
|
|
113
952
|
const sessions = owner.get(SESSION_SERVICE);
|
|
@@ -120,17 +959,21 @@ function apply(ctx) {
|
|
|
120
959
|
const state = models.directoryFor(session.sessionId).store.getSnapshot();
|
|
121
960
|
return state.current !== null && state.routable !== false;
|
|
122
961
|
};
|
|
123
|
-
owner.effect(() => () => {
|
|
962
|
+
owner.effect(() => () => {
|
|
963
|
+
lifetime.abort();
|
|
964
|
+
});
|
|
124
965
|
owner.effect(async () => {
|
|
125
966
|
const unmount = await remote.$mount(exports.TIER_CONTRIBUTION);
|
|
126
967
|
if (lifetime.signal.aborted)
|
|
127
968
|
return unmount;
|
|
128
969
|
try {
|
|
129
970
|
// The dynamically mounted namespace needs its own declared Cordis dependency.
|
|
130
|
-
const registration = owner.inject([TIER_REMOTE_SERVICE], scope => {
|
|
971
|
+
const registration = owner.inject([TIER_REMOTE_SERVICE], (scope) => {
|
|
131
972
|
const tiers = scope.get(TIER_REMOTE_SERVICE);
|
|
132
973
|
const registered = new AbortController();
|
|
133
|
-
scope.effect(() => () => {
|
|
974
|
+
scope.effect(() => () => {
|
|
975
|
+
registered.abort();
|
|
976
|
+
});
|
|
134
977
|
scope.effect(() => {
|
|
135
978
|
const unregister = command.register({
|
|
136
979
|
name: COMMAND_NAME,
|
|
@@ -139,72 +982,19 @@ function apply(ctx) {
|
|
|
139
982
|
available,
|
|
140
983
|
ui: {
|
|
141
984
|
kind: 'popupSelect',
|
|
142
|
-
searchLabels: () => ({
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
if (available(session) && models.directoryFor(session.sessionId) === directory
|
|
157
|
-
&& latest?.provider === provider && latest.model === model)
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
catch { /* A removed session scope must not retain permission to write. */ }
|
|
161
|
-
opening.abort(new Error(STALE_ROUTE));
|
|
162
|
-
};
|
|
163
|
-
const checkRoute = () => { invalidate(); signal.throwIfAborted(); };
|
|
164
|
-
// A route that changes away and back still invalidates the original billing decision.
|
|
165
|
-
const unsubscribe = directory.store.subscribe(invalidate);
|
|
166
|
-
signal.addEventListener('abort', unsubscribe, { once: true });
|
|
167
|
-
let view;
|
|
168
|
-
try {
|
|
169
|
-
await directory.load();
|
|
170
|
-
checkRoute();
|
|
171
|
-
view = parseTierView(resultValue(await tiers.describe(provider, model, signal), READ_ERROR));
|
|
172
|
-
checkRoute();
|
|
173
|
-
}
|
|
174
|
-
catch {
|
|
175
|
-
const error = signal.aborted ? signal.reason : new Error(READ_ERROR);
|
|
176
|
-
opening.abort();
|
|
177
|
-
throw error;
|
|
178
|
-
}
|
|
179
|
-
if (view.choices.length === 0) {
|
|
180
|
-
opening.abort();
|
|
181
|
-
return [];
|
|
182
|
-
}
|
|
183
|
-
const options = [
|
|
184
|
-
{ id: exports.TIER_DEFAULT, label: DEFAULT_LABEL, active: view.current === undefined },
|
|
185
|
-
...view.choices.map(choice => ({ id: choice.id, label: choice.name, detail: choice.description, active: view.current === choice.id })),
|
|
186
|
-
];
|
|
187
|
-
for (const option of options) {
|
|
188
|
-
const tier = option.id === exports.TIER_DEFAULT ? undefined : option.id;
|
|
189
|
-
selections.set(option, {
|
|
190
|
-
sessionId: session.sessionId,
|
|
191
|
-
async select() {
|
|
192
|
-
checkRoute();
|
|
193
|
-
try {
|
|
194
|
-
parseVoid(resultValue(await tiers.select(provider, model, tier, signal), exports.TIER_SAVE_ERROR));
|
|
195
|
-
}
|
|
196
|
-
catch {
|
|
197
|
-
signal.throwIfAborted();
|
|
198
|
-
throw new Error(exports.TIER_SAVE_ERROR);
|
|
199
|
-
}
|
|
200
|
-
checkRoute();
|
|
201
|
-
// The stock shell does not abort its signal on successful selection.
|
|
202
|
-
opening.abort();
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
return options;
|
|
207
|
-
},
|
|
985
|
+
searchLabels: () => ({
|
|
986
|
+
placeholder: SEARCH_PLACEHOLDER,
|
|
987
|
+
empty: UNAVAILABLE,
|
|
988
|
+
noResults: NO_RESULTS,
|
|
989
|
+
}),
|
|
990
|
+
options: (session, requestSignal) => openTierMenu({
|
|
991
|
+
models,
|
|
992
|
+
tiers,
|
|
993
|
+
selections,
|
|
994
|
+
available,
|
|
995
|
+
lifetime: lifetime.signal,
|
|
996
|
+
registered: registered.signal,
|
|
997
|
+
}, session, requestSignal),
|
|
208
998
|
async onSelect(option, session) {
|
|
209
999
|
const selection = selections.get(option);
|
|
210
1000
|
if (selection === undefined || selection.sessionId !== session.sessionId)
|
|
@@ -213,11 +1003,17 @@ function apply(ctx) {
|
|
|
213
1003
|
},
|
|
214
1004
|
},
|
|
215
1005
|
});
|
|
216
|
-
return () => {
|
|
1006
|
+
return () => {
|
|
1007
|
+
command.dismiss(COMMAND_NAME);
|
|
1008
|
+
unregister();
|
|
1009
|
+
};
|
|
217
1010
|
});
|
|
218
1011
|
});
|
|
219
1012
|
await registration;
|
|
220
|
-
return async () => {
|
|
1013
|
+
return async () => {
|
|
1014
|
+
await registration.dispose();
|
|
1015
|
+
await unmount();
|
|
1016
|
+
};
|
|
221
1017
|
}
|
|
222
1018
|
catch (error) {
|
|
223
1019
|
await unmount();
|
|
@@ -225,6 +1021,7 @@ function apply(ctx) {
|
|
|
225
1021
|
}
|
|
226
1022
|
});
|
|
227
1023
|
});
|
|
1024
|
+
applyProviderSignIn(ctx);
|
|
228
1025
|
}
|
|
229
1026
|
|
|
230
1027
|
return exports;}});
|