@sagmans/dsh-provider-extra 0.4.0 → 0.6.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/README.md +90 -23
- package/dist/catalog-default.js +39 -0
- package/dist/catalog-protocols.js +15 -0
- package/dist/catalog-routes.js +374 -0
- package/dist/catalog-runtime.js +135 -0
- package/dist/catalog.js +65 -0
- package/dist/codex.js +30 -4
- package/dist/extra-models.js +47 -6
- package/dist/index.js +110 -141
- package/dist/login-attempt.js +29 -7
- package/dist/login-choice.js +25 -14
- package/dist/login-command.js +16 -9
- package/dist/login-contract.js +3 -1
- package/dist/login-host.js +196 -0
- package/dist/login-route.js +19 -0
- package/dist/login-verify.js +10 -5
- package/dist/opencode-go.js +9 -5
- package/docs/catalog-v1.example.json +40 -0
- package/docs/catalog.md +224 -0
- package/package.json +27 -21
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-provider-extra: extra LLM provider routes for DeepSeek Harness.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Without a catalog, mounts two additive routes. An opted-in catalog owns
|
|
5
|
+
* the profile's complete model selection and default instead.
|
|
6
|
+
* OpenCode Go stamps the x-opencode-session header
|
|
5
7
|
* from the live dsh session id, which the gateway requires for routing and
|
|
6
8
|
* prompt-cache affinity and which the shipped adapters do not send. OpenAI
|
|
7
9
|
* Codex serves a ChatGPT subscription through pi-ai's OAuth: the grant lives
|
|
@@ -29,30 +31,23 @@
|
|
|
29
31
|
import Schema from '@deepseek-ai/schemastery';
|
|
30
32
|
import { PiAiAdapter } from '@deepseek-ai/dsh-llm-pi-ai';
|
|
31
33
|
import { LlmError, assertUsableApiKey, resolveImageAttachmentAccess } from '@deepseek-ai/dsh-llm';
|
|
32
|
-
import { createModels } from '@earendil-works/pi-ai';
|
|
33
|
-
import { builtinProviders } from '@earendil-works/pi-ai/providers/all';
|
|
34
34
|
import { DEFAULT_EXTRA_MODEL_TEMPLATE, DEFAULT_OPENCODE_API_KEY_ENV, OPENCODE_GO_PROVIDER_ID, buildOpenCodeGoProfile, } from "./opencode-go.js";
|
|
35
|
-
import { DEFAULT_CODEX_DISPLAY_NAME, DEFAULT_CODEX_ROUTE_ID, buildCodexProfile, codexApiKey, codexAuth,
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
35
|
+
import { DEFAULT_CODEX_DISPLAY_NAME, DEFAULT_CODEX_ROUTE_ID, buildCodexProfile, codexApiKey, codexAuth, } from "./codex.js";
|
|
36
|
+
import { CODEX_TRANSPORTS } from "./codex.js";
|
|
37
|
+
import { DEFAULT_LOGIN_COMMAND_NAME } from "./login-command.js";
|
|
38
|
+
import { mountLoginCommand } from "./login-host.js";
|
|
39
|
+
import { compileCatalog } from "./catalog.js";
|
|
40
|
+
import { mountCatalog } from "./catalog-runtime.js";
|
|
41
|
+
export { compileCatalog } from "./catalog.js";
|
|
42
|
+
export { buildCatalogProfile } from "./catalog-routes.js";
|
|
39
43
|
/** Settings namespace configuration surfaces address this plugin's section by. */
|
|
40
44
|
const SETTINGS_NS = 'dsh-provider-extra';
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
headers: Schema.dict(Schema.string()),
|
|
48
|
-
codexEnabled: Schema.boolean().default(true),
|
|
49
|
-
codexRouteId: Schema.string().default(DEFAULT_CODEX_ROUTE_ID),
|
|
50
|
-
codexDisplayName: Schema.string().default(DEFAULT_CODEX_DISPLAY_NAME),
|
|
51
|
-
loginCommandEnabled: Schema.boolean().default(true),
|
|
52
|
-
loginCommandName: Schema.string().default(DEFAULT_LOGIN_COMMAND_NAME),
|
|
53
|
-
});
|
|
54
|
-
export const name = 'dsh-provider-extra';
|
|
55
|
-
export const inject = ['llm'];
|
|
45
|
+
// Declared extra models: the composition entry and the settings section share
|
|
46
|
+
// these two shapes, so one document's declaration is valid in the other. Both
|
|
47
|
+
// are left to inference rather than annotated with the interfaces below: a
|
|
48
|
+
// schemastery release on a verified line widens a required field's output to
|
|
49
|
+
// `string | Volatile<string>` for a value a loader may pass as a getter, and an
|
|
50
|
+
// annotation in the declaration's own shape then fails to compile on that line.
|
|
56
51
|
const goExtraModelSchema = Schema.object({
|
|
57
52
|
id: Schema.string().required(),
|
|
58
53
|
name: Schema.string(),
|
|
@@ -66,48 +61,60 @@ const codexExtraModelSchema = Schema.object({
|
|
|
66
61
|
name: Schema.string(),
|
|
67
62
|
template: Schema.string(),
|
|
68
63
|
});
|
|
64
|
+
// The assertion carries the one thing schemastery cannot state: every array in
|
|
65
|
+
// this entry is read-only to the plugin, which only ever copies it, while an
|
|
66
|
+
// array member schema is typed as the mutable array it validates.
|
|
67
|
+
export const Config = Schema.transform(Schema.object({
|
|
68
|
+
// Keep raw catalog presence: schemastery otherwise materializes absent arrays.
|
|
69
|
+
catalog: Schema.any(),
|
|
70
|
+
apiKeyEnv: Schema.string().role('credential-ref').default(DEFAULT_OPENCODE_API_KEY_ENV),
|
|
71
|
+
routeId: Schema.string().default(OPENCODE_GO_PROVIDER_ID),
|
|
72
|
+
displayName: Schema.string().default('OpenCode Go'),
|
|
73
|
+
baseURL: Schema.string(),
|
|
74
|
+
fallbackSessionId: Schema.string(),
|
|
75
|
+
headers: Schema.dict(Schema.string()),
|
|
76
|
+
extraModels: Schema.array(goExtraModelSchema),
|
|
77
|
+
models: Schema.array(Schema.string()),
|
|
78
|
+
codexEnabled: Schema.boolean().default(true),
|
|
79
|
+
codexRouteId: Schema.string().default(DEFAULT_CODEX_ROUTE_ID),
|
|
80
|
+
codexDisplayName: Schema.string().default(DEFAULT_CODEX_DISPLAY_NAME),
|
|
81
|
+
codexExtraModels: Schema.array(codexExtraModelSchema),
|
|
82
|
+
codexModels: Schema.array(Schema.string()),
|
|
83
|
+
codexTransport: Schema.union(CODEX_TRANSPORTS),
|
|
84
|
+
loginCommandEnabled: Schema.boolean().default(true),
|
|
85
|
+
loginCommandName: Schema.string().default(DEFAULT_LOGIN_COMMAND_NAME),
|
|
86
|
+
}), (value) => {
|
|
87
|
+
// Object-level validation also sees explicit null, which field transforms skip.
|
|
88
|
+
// Loader validates before disposal, retaining the previous snapshot on failure.
|
|
89
|
+
compileCatalog(value.catalog);
|
|
90
|
+
return value;
|
|
91
|
+
}, true);
|
|
92
|
+
/**
|
|
93
|
+
* One configured model selection, or nothing when the entry declared none.
|
|
94
|
+
* schemastery materializes an undeclared array member as an empty array, so an
|
|
95
|
+
* empty declaration is the absence of a selection: the route serves everything
|
|
96
|
+
* its catalog and extras resolved, exactly as a profile that never named the
|
|
97
|
+
* field does.
|
|
98
|
+
*/
|
|
99
|
+
function selection(declared) {
|
|
100
|
+
return declared === undefined || declared.length === 0 ? undefined : declared;
|
|
101
|
+
}
|
|
102
|
+
export const name = 'dsh-provider-extra';
|
|
103
|
+
export const inject = ['llm'];
|
|
104
|
+
// Inferred for the reason above; the section and the entry still read the same
|
|
105
|
+
// declaration form, because both are built from these two schema values.
|
|
69
106
|
const SectionSchema = Schema.object({
|
|
70
107
|
extraModels: Schema.array(goExtraModelSchema).default([]),
|
|
71
108
|
codexExtraModels: Schema.array(codexExtraModelSchema).default([]),
|
|
72
109
|
});
|
|
73
|
-
/**
|
|
74
|
-
* Every catalog provider that ships an interactive login, in catalog order.
|
|
75
|
-
* The catalog is the authority on how a provider signs in — which methods it
|
|
76
|
-
* offers, and what each is called — so the picker never re-decides that here.
|
|
77
|
-
*/
|
|
78
|
-
function loginChoices() {
|
|
79
|
-
const choices = [];
|
|
80
|
-
for (const provider of builtinProviders()) {
|
|
81
|
-
const oauth = provider.auth?.oauth;
|
|
82
|
-
if (oauth?.login !== undefined) {
|
|
83
|
-
choices.push({
|
|
84
|
-
providerId: provider.id,
|
|
85
|
-
providerName: provider.name,
|
|
86
|
-
authType: 'oauth',
|
|
87
|
-
methodLabel: oauth.loginLabel ?? oauth.name,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const apiKey = provider.auth?.apiKey;
|
|
91
|
-
if (apiKey?.login !== undefined) {
|
|
92
|
-
choices.push({
|
|
93
|
-
providerId: provider.id,
|
|
94
|
-
providerName: provider.name,
|
|
95
|
-
authType: 'api_key',
|
|
96
|
-
methodLabel: apiKey.name,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return choices;
|
|
101
|
-
}
|
|
102
|
-
/** The catalog provider behind one sign-in choice. */
|
|
103
|
-
function catalogLoginProvider(providerId) {
|
|
104
|
-
const found = builtinProviders().find(provider => provider.id === providerId);
|
|
105
|
-
if (found === undefined) {
|
|
106
|
-
throw new Error('dsh-provider-extra: the installed pi-ai catalog no longer ships provider "' + providerId + '"');
|
|
107
|
-
}
|
|
108
|
-
return found;
|
|
109
|
-
}
|
|
110
110
|
export function apply(ctx, config) {
|
|
111
|
+
const snapshot = compileCatalog(config.catalog);
|
|
112
|
+
if (snapshot !== undefined) {
|
|
113
|
+
mountCatalog(ctx, snapshot, config);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const models = selection(config.models);
|
|
117
|
+
const codexModels = selection(config.codexModels);
|
|
111
118
|
const route = {
|
|
112
119
|
provider: config.routeId,
|
|
113
120
|
displayName: config.displayName,
|
|
@@ -115,11 +122,31 @@ export function apply(ctx, config) {
|
|
|
115
122
|
...config.baseURL === undefined ? {} : { baseURL: config.baseURL },
|
|
116
123
|
...config.fallbackSessionId === undefined ? {} : { fallbackSessionId: config.fallbackSessionId },
|
|
117
124
|
...config.headers === undefined ? {} : { headers: { ...config.headers } },
|
|
125
|
+
...models === undefined ? {} : { models },
|
|
118
126
|
};
|
|
119
127
|
const codex = {
|
|
120
128
|
provider: config.codexRouteId,
|
|
121
129
|
displayName: config.codexDisplayName,
|
|
130
|
+
...codexModels === undefined ? {} : { models: codexModels },
|
|
131
|
+
...config.codexTransport === undefined ? {} : { transport: config.codexTransport },
|
|
132
|
+
};
|
|
133
|
+
// The entry's own declarations are the base layer the settings section sits
|
|
134
|
+
// over, and the value the routes serve on a harness with no settings document
|
|
135
|
+
// at all — which is why they seed the thunk below instead of only the base.
|
|
136
|
+
const entry = {
|
|
137
|
+
extraModels: [...config.extraModels ?? []],
|
|
138
|
+
codexExtraModels: [...config.codexExtraModels ?? []],
|
|
122
139
|
};
|
|
140
|
+
// A declared selection is configuration, not a request fact, so it is proven
|
|
141
|
+
// once here. Registration resolves a profile for its route metadata and
|
|
142
|
+
// reports a failure as a refused route, which would bury the one message that
|
|
143
|
+
// names the misconfigured id; resolving per request would bury it the same
|
|
144
|
+
// way. The extras a selection resolves against at this point are the entry's
|
|
145
|
+
// own, because a settings document may not have arrived yet.
|
|
146
|
+
if (models !== undefined)
|
|
147
|
+
buildOpenCodeGoProfile({ ...route, extraModels: entry.extraModels });
|
|
148
|
+
if (codexModels !== undefined)
|
|
149
|
+
buildCodexProfile({ ...codex, extraModels: entry.codexExtraModels });
|
|
123
150
|
// The catalog check stays boot-time even though settings can now extend the
|
|
124
151
|
// profile per request: a catalog drift (pi-ai no longer shipping Codex) must
|
|
125
152
|
// stand this route down once, loudly, here, instead of failing every request
|
|
@@ -127,7 +154,9 @@ export function apply(ctx, config) {
|
|
|
127
154
|
let codexServable = false;
|
|
128
155
|
if (config.codexEnabled) {
|
|
129
156
|
try {
|
|
130
|
-
|
|
157
|
+
// Catalog drift alone: the selection was already proven above, so it
|
|
158
|
+
// cannot be what this check reports.
|
|
159
|
+
buildCodexProfile({ provider: codex.provider, displayName: codex.displayName });
|
|
131
160
|
codexServable = true;
|
|
132
161
|
}
|
|
133
162
|
catch (error) {
|
|
@@ -138,14 +167,19 @@ export function apply(ctx, config) {
|
|
|
138
167
|
// Route wiring is boot-time, but the model list is per-request: the section
|
|
139
168
|
// thunk below tracks the settings overlay, so a committed extras change
|
|
140
169
|
// reaches the next operation with no rebuild and no restart.
|
|
141
|
-
let currentSection = () =>
|
|
170
|
+
let currentSection = () => entry;
|
|
142
171
|
const profiles = () => {
|
|
143
172
|
const section = currentSection();
|
|
173
|
+
// Selections stay bound to the entry extras that passed mount-time validation.
|
|
144
174
|
const entries = [
|
|
145
|
-
[route.provider, buildOpenCodeGoProfile({
|
|
175
|
+
[route.provider, buildOpenCodeGoProfile({
|
|
176
|
+
...route, extraModels: models !== undefined ? entry.extraModels : section.extraModels,
|
|
177
|
+
})],
|
|
146
178
|
];
|
|
147
179
|
if (codexServable) {
|
|
148
|
-
entries.push([codex.provider, buildCodexProfile({
|
|
180
|
+
entries.push([codex.provider, buildCodexProfile({
|
|
181
|
+
...codex, extraModels: codexModels !== undefined ? entry.codexExtraModels : section.codexExtraModels,
|
|
182
|
+
})]);
|
|
149
183
|
}
|
|
150
184
|
return new Map(entries);
|
|
151
185
|
};
|
|
@@ -227,7 +261,18 @@ export function apply(ctx, config) {
|
|
|
227
261
|
ctx.logger.warn(error);
|
|
228
262
|
}
|
|
229
263
|
ctx.inject(['settings'], (settingsCtx) => {
|
|
230
|
-
settingsCtx.settings
|
|
264
|
+
const settings = settingsCtx.settings;
|
|
265
|
+
// A line with no section API publishes forms from this entry's own Config
|
|
266
|
+
// instead, and a committed form edit restarts the entry, so that line's
|
|
267
|
+
// extras arrive as a fresh mount of this apply() rather than as a
|
|
268
|
+
// notification. Installing a section there would fail on the missing
|
|
269
|
+
// method; leaving this line's Config alone would ignore every later edit.
|
|
270
|
+
if (typeof settings.installSection !== 'function')
|
|
271
|
+
return;
|
|
272
|
+
// The section overrides the entry where a settings document exists and
|
|
273
|
+
// stands in for it where none does; the settings service falls back to this
|
|
274
|
+
// same value when it detaches.
|
|
275
|
+
settings.installSection(ctx, SETTINGS_NS, SectionSchema, entry, {
|
|
231
276
|
setSource: (source) => { currentSection = source; },
|
|
232
277
|
// No registration facts derive from the section: the route set is fixed
|
|
233
278
|
// at composition and the adapter rebuilds its snapshot on every
|
|
@@ -235,81 +280,5 @@ export function apply(ctx, config) {
|
|
|
235
280
|
onChange: () => { },
|
|
236
281
|
});
|
|
237
282
|
});
|
|
238
|
-
|
|
239
|
-
// it runs in this process, so the credential lands in the store the routes
|
|
240
|
-
// already read and no bin path or peer tree is involved. It signs into any
|
|
241
|
-
// provider the installed catalog ships a login for, not only the two routes
|
|
242
|
-
// this plugin mounts, because a human asking to sign in means the provider
|
|
243
|
-
// they name and nothing narrower. Profiles without a command registry
|
|
244
|
-
// (headless compositions) keep the package bin.
|
|
245
|
-
if (config.loginCommandEnabled) {
|
|
246
|
-
ctx.inject(['commands'], (commandCtx) => {
|
|
247
|
-
const host = {
|
|
248
|
-
choices: loginChoices,
|
|
249
|
-
login: async (choice, interaction) => {
|
|
250
|
-
const credentials = () => ctx.get('credentials');
|
|
251
|
-
const auth = codexAuth(credentials);
|
|
252
|
-
const models = createModels(auth);
|
|
253
|
-
// pi-ai's collection starts empty: the catalog provider carrying the
|
|
254
|
-
// login implementation has to be handed to it before the flow runs.
|
|
255
|
-
models.setProvider(catalogLoginProvider(choice.providerId));
|
|
256
|
-
if (choice.authType !== 'api_key') {
|
|
257
|
-
await models.login(choice.providerId, choice.authType, interaction);
|
|
258
|
-
return await declareProviderRoute(ctx.get('settings'), choice.providerId);
|
|
259
|
-
}
|
|
260
|
-
// A grant the provider minted proves itself, but a key proves nothing
|
|
261
|
-
// until a request carries it, so the key is spent from a store that
|
|
262
|
-
// forgets and the profile hears of it only once it has been accepted.
|
|
263
|
-
const pending = createModels({ credentials: new PendingCredentialStore(), authContext: auth.authContext });
|
|
264
|
-
pending.setProvider(catalogLoginProvider(choice.providerId));
|
|
265
|
-
const credential = await pending.login(choice.providerId, choice.authType, interaction);
|
|
266
|
-
await proveApiKey(pending, choice.providerId);
|
|
267
|
-
await auth.credentials.modify(choice.providerId, async () => credential);
|
|
268
|
-
// The credential is only reachable through a declared route, so the
|
|
269
|
-
// sign-in is not finished until one exists.
|
|
270
|
-
return await declareProviderRoute(ctx.get('settings'), choice.providerId);
|
|
271
|
-
},
|
|
272
|
-
stored: async (providerId) => {
|
|
273
|
-
const credentials = ctx.get('credentials');
|
|
274
|
-
if (credentials === undefined)
|
|
275
|
-
return undefined;
|
|
276
|
-
const record = await credentials.readRecord(recordKeyFor(providerId));
|
|
277
|
-
if (record === undefined)
|
|
278
|
-
return undefined;
|
|
279
|
-
return record.kind === 'api-key' ? 'api_key' : 'oauth';
|
|
280
|
-
},
|
|
281
|
-
reference: async (providerId) => {
|
|
282
|
-
// A route this plugin mounts names its reference in this plugin's
|
|
283
|
-
// config; every other route is whatever llm-pi-ai was configured
|
|
284
|
-
// with, which is the same document a sign-in declares into.
|
|
285
|
-
const ref = declaredCredentialRef(ctx.get('settings'), providerId)
|
|
286
|
-
?? (providerId === route.provider ? route.apiKeyEnv : undefined);
|
|
287
|
-
if (ref === undefined || ref.length === 0)
|
|
288
|
-
return undefined;
|
|
289
|
-
const credentials = ctx.get('credentials');
|
|
290
|
-
if (credentials === undefined)
|
|
291
|
-
return { ref };
|
|
292
|
-
const info = await credentials.describe(ref);
|
|
293
|
-
return { ref, ...info.configured && info.source !== undefined ? { source: info.source } : {} };
|
|
294
|
-
},
|
|
295
|
-
// Resolved per call, never at mount: a composition that mounts no
|
|
296
|
-
// session UI still gets the command, and says so when it runs.
|
|
297
|
-
ask: async (request) => {
|
|
298
|
-
const userQuestions = ctx.get('userQuestions');
|
|
299
|
-
if (userQuestions === undefined) {
|
|
300
|
-
throw new Error('dsh-provider-extra: this composition mounts no session UI that can ask the sign-in questions');
|
|
301
|
-
}
|
|
302
|
-
return await userQuestions.ask({
|
|
303
|
-
questions: [...request.questions],
|
|
304
|
-
agent: request.agent,
|
|
305
|
-
...request.signal === undefined ? {} : { signal: request.signal },
|
|
306
|
-
});
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
commandCtx.commands.register(createLoginCommand(host, config.loginCommandName));
|
|
310
|
-
// Registration is silent otherwise, and the command is the one surface a
|
|
311
|
-
// user cannot see in a config dump: saying it exists is the diagnosis.
|
|
312
|
-
ctx.logger.info('dsh-provider-extra: /' + config.loginCommandName + ' signs in any provider with an interactive login');
|
|
313
|
-
});
|
|
314
|
-
}
|
|
283
|
+
mountLoginCommand(ctx, config, { profiles });
|
|
315
284
|
}
|
package/dist/login-attempt.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* @module dsh-provider-extra/login-attempt
|
|
11
11
|
*/
|
|
12
12
|
import { renderEvent } from "./codex-login.js";
|
|
13
|
+
import { StoredCredentialError } from "./login-contract.js";
|
|
13
14
|
import { answerLabels, answerText } from "./login-choice.js";
|
|
14
15
|
/** Question ids belong to the caller; the answer echoes them back. */
|
|
15
16
|
const PROMPT_QUESTION_ID = 'prompt';
|
|
@@ -43,6 +44,8 @@ const WAIT_DETAIL = 'Finish on that page, then choose Done. The sign-in complete
|
|
|
43
44
|
const DECLARED_ROUTE_NOTICE = ' The provider was added to the llm-pi-ai settings, so its models work now.';
|
|
44
45
|
/** Said when a configured route already carried the sign-in. */
|
|
45
46
|
const PRESENT_ROUTE_NOTICE = ' The credential is stored and the route reads it on its next request.';
|
|
47
|
+
/** A failed confirmation cannot imply that a completed host login rolled back. */
|
|
48
|
+
const READBACK_FAILURE_PREFIX = 'The sign-in completed, but credential readback failed: ';
|
|
46
49
|
/**
|
|
47
50
|
* Said when nothing here can serve the provider. A stored credential with no
|
|
48
51
|
* route is the state that later fails a turn, so the failure belongs to the
|
|
@@ -76,7 +79,7 @@ function openWait(host, invocation, choice, attempt) {
|
|
|
76
79
|
detail: [...attempt.notices, WAIT_DETAIL].join('\n'),
|
|
77
80
|
options: [{ label: DONE_LABEL }, { label: CANCEL_LABEL }],
|
|
78
81
|
}],
|
|
79
|
-
signal: abort.signal,
|
|
82
|
+
signal: AbortSignal.any([attempt.signal, abort.signal]),
|
|
80
83
|
});
|
|
81
84
|
const settled = asked.then((answer) => {
|
|
82
85
|
if (answerLabels(answer, NOTICE_QUESTION_ID).includes(CANCEL_LABEL)) {
|
|
@@ -137,7 +140,7 @@ async function askPrompt(host, invocation, prompt, choice, attempt) {
|
|
|
137
140
|
const ask = host.ask({
|
|
138
141
|
agent: invocation.agent,
|
|
139
142
|
questions: [promptQuestion(prompt, choice)],
|
|
140
|
-
signal: attempt.
|
|
143
|
+
signal: attempt.signal,
|
|
141
144
|
});
|
|
142
145
|
if (prompt.type === 'select') {
|
|
143
146
|
const options = prompt.options;
|
|
@@ -183,7 +186,7 @@ async function askPrompt(host, invocation, prompt, choice, attempt) {
|
|
|
183
186
|
/** The interaction one attempt hands to pi-ai's login. */
|
|
184
187
|
function attemptInteraction(host, invocation, choice, attempt) {
|
|
185
188
|
return {
|
|
186
|
-
signal: attempt.
|
|
189
|
+
signal: attempt.signal,
|
|
187
190
|
notify: (event) => {
|
|
188
191
|
attempt.notices.push(...renderEvent(event));
|
|
189
192
|
if (attempt.wait === undefined)
|
|
@@ -204,9 +207,11 @@ function successText(choice, route) {
|
|
|
204
207
|
return signedIn + UNAVAILABLE_ROUTE_NOTICE;
|
|
205
208
|
return signedIn + PRESENT_ROUTE_NOTICE;
|
|
206
209
|
}
|
|
207
|
-
/**
|
|
210
|
+
/** Failed attempts must distinguish cancelled work from credentials already committed. */
|
|
208
211
|
function describeFailure(error, attempt, choice) {
|
|
209
|
-
if (
|
|
212
|
+
if (error instanceof StoredCredentialError)
|
|
213
|
+
return error.message;
|
|
214
|
+
if (attempt.declined || (attempt.signal.aborted && !attempt.expired))
|
|
210
215
|
return 'The ' + choice.providerName + ' sign-in was cancelled.';
|
|
211
216
|
if (attempt.expired)
|
|
212
217
|
return 'The ' + choice.providerName + ' sign-in timed out; start it again to get a fresh code.';
|
|
@@ -215,7 +220,15 @@ function describeFailure(error, attempt, choice) {
|
|
|
215
220
|
}
|
|
216
221
|
/** Run one sign-in end to end: the conversation plus the stored-credential check. */
|
|
217
222
|
export async function runChoice(host, invocation, choice) {
|
|
218
|
-
const
|
|
223
|
+
const abort = new AbortController();
|
|
224
|
+
const attempt = {
|
|
225
|
+
abort,
|
|
226
|
+
signal: AbortSignal.any([invocation.signal, abort.signal]),
|
|
227
|
+
notices: [],
|
|
228
|
+
wait: undefined,
|
|
229
|
+
declined: false,
|
|
230
|
+
expired: false,
|
|
231
|
+
};
|
|
219
232
|
const deadline = setTimeout(() => {
|
|
220
233
|
attempt.expired = true;
|
|
221
234
|
attempt.abort.abort();
|
|
@@ -223,12 +236,21 @@ export async function runChoice(host, invocation, choice) {
|
|
|
223
236
|
if (typeof deadline === 'object')
|
|
224
237
|
deadline.unref();
|
|
225
238
|
try {
|
|
239
|
+
attempt.signal.throwIfAborted();
|
|
226
240
|
const route = await host.login(choice, attemptInteraction(host, invocation, choice, attempt));
|
|
241
|
+
// A resolved host may already have committed credentials; late cancellation cannot undo them.
|
|
227
242
|
await closeWait(attempt);
|
|
228
243
|
// pi-ai persists during login, so resolving is not yet proof: only a
|
|
229
244
|
// record read back is. A flow that resolves without one is a catalog bug
|
|
230
245
|
// the human must hear about rather than a silent no-op sign-in.
|
|
231
|
-
|
|
246
|
+
let stored;
|
|
247
|
+
try {
|
|
248
|
+
stored = await host.stored(choice.providerId);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
252
|
+
throw new StoredCredentialError(READBACK_FAILURE_PREFIX + message, { cause: error });
|
|
253
|
+
}
|
|
232
254
|
if (stored === undefined) {
|
|
233
255
|
return {
|
|
234
256
|
kind: 'error',
|
package/dist/login-choice.js
CHANGED
|
@@ -14,9 +14,9 @@ export const KEY_WORD = 'key';
|
|
|
14
14
|
export const OAUTH_WORD = 'oauth';
|
|
15
15
|
/** Question ids belong to the caller; the answer echoes them back. */
|
|
16
16
|
const PICKER_QUESTION_ID = 'provider';
|
|
17
|
-
/**
|
|
17
|
+
/** Route identity can be case-sensitive even when method words are not. */
|
|
18
18
|
export function words(rawInput) {
|
|
19
|
-
return rawInput.trim().
|
|
19
|
+
return rawInput.trim().split(/\s+/u).filter(word => word.length > 0);
|
|
20
20
|
}
|
|
21
21
|
/** The label one choice shows: its provider alone when that provider has a single method. */
|
|
22
22
|
function choiceLabel(choice, shared) {
|
|
@@ -34,7 +34,9 @@ function optionLabels(choices) {
|
|
|
34
34
|
for (const choice of choices) {
|
|
35
35
|
perProvider.set(choice.providerId, (perProvider.get(choice.providerId) ?? 0) + 1);
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
const labels = choices.map(choice => choiceLabel(choice, (perProvider.get(choice.providerId) ?? 0) > 1));
|
|
38
|
+
return labels.map((label, index) => labels.filter(candidate => candidate === label).length > 1
|
|
39
|
+
? label + ' · ' + choices[index].providerId + ' · ' + choices[index].authType : label);
|
|
38
40
|
}
|
|
39
41
|
/** The question the picker asks, one option per provider-method pair. */
|
|
40
42
|
export function pickerQuestion(choices) {
|
|
@@ -74,24 +76,33 @@ export function resolveChoice(choices, answer) {
|
|
|
74
76
|
const text = answerText(answer, PICKER_QUESTION_ID);
|
|
75
77
|
if (text === undefined)
|
|
76
78
|
return undefined;
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
const input = words(text);
|
|
80
|
+
const named = choiceByWords(choices, input);
|
|
81
|
+
if (named !== undefined)
|
|
82
|
+
return named;
|
|
83
|
+
// A bare method is useful only when it identifies exactly one credential owner.
|
|
84
|
+
const method = input.length === 1 ? input[0].toLowerCase() : undefined;
|
|
85
|
+
if (method !== KEY_WORD && method !== OAUTH_WORD)
|
|
86
|
+
return undefined;
|
|
87
|
+
const matches = choices.filter(choice => choice.authType === (method === KEY_WORD ? 'api_key' : 'oauth'));
|
|
88
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
83
89
|
}
|
|
84
90
|
/** The choice a provider id (and optional method word) names. */
|
|
85
91
|
export function choiceByWords(choices, input) {
|
|
86
92
|
const providerId = input[0];
|
|
87
|
-
if (providerId === undefined)
|
|
88
|
-
return undefined;
|
|
89
|
-
const named = choices.filter(choice => choice.providerId === providerId);
|
|
90
|
-
if (named.length === 0)
|
|
93
|
+
if (providerId === undefined || input.length > 2)
|
|
91
94
|
return undefined;
|
|
92
|
-
|
|
95
|
+
let named = choices.filter(choice => choice.providerId === providerId);
|
|
96
|
+
if (named.length === 0) {
|
|
97
|
+
named = choices.filter(choice => choice.providerId.toLowerCase() === providerId.toLowerCase());
|
|
98
|
+
if (new Set(named.map(choice => choice.providerId)).size !== 1)
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const method = input[1]?.toLowerCase();
|
|
93
102
|
if (method === undefined)
|
|
94
103
|
return named[0];
|
|
104
|
+
if (method !== KEY_WORD && method !== OAUTH_WORD)
|
|
105
|
+
return undefined;
|
|
95
106
|
const authType = method === KEY_WORD ? 'api_key' : 'oauth';
|
|
96
107
|
return named.find(choice => choice.authType === authType);
|
|
97
108
|
}
|
package/dist/login-command.js
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*
|
|
20
20
|
* @module dsh-provider-extra/login-command
|
|
21
21
|
*/
|
|
22
|
-
import { KEY_WORD, OAUTH_WORD, choiceByWords, pickerQuestion, resolveChoice, words } from "./login-choice.js";
|
|
22
|
+
import { KEY_WORD, OAUTH_WORD, answerText, choiceByWords, pickerQuestion, resolveChoice, words } from "./login-choice.js";
|
|
23
23
|
import { runChoice } from "./login-attempt.js";
|
|
24
24
|
import { statusOf } from "./login-status.js";
|
|
25
25
|
/** Command name a profile gets unless it renames the command. */
|
|
@@ -44,7 +44,7 @@ export function createLoginCommand(host, commandName) {
|
|
|
44
44
|
recordInput: false,
|
|
45
45
|
handler: async (invocation) => {
|
|
46
46
|
const input = words(invocation.rawInput);
|
|
47
|
-
if (input.length === 1 && input[0] === STATUS_WORD)
|
|
47
|
+
if (input.length === 1 && input[0]?.toLowerCase() === STATUS_WORD)
|
|
48
48
|
return await statusOf(host);
|
|
49
49
|
if (input.length > 2)
|
|
50
50
|
return { kind: 'error', text: 'dsh-provider-extra: too many arguments. ' + usage };
|
|
@@ -52,14 +52,23 @@ export function createLoginCommand(host, commandName) {
|
|
|
52
52
|
if (choices.length === 0) {
|
|
53
53
|
return { kind: 'error', text: 'dsh-provider-extra: this composition mounts no provider with an interactive sign-in' };
|
|
54
54
|
}
|
|
55
|
+
const unknownChoice = (input) => {
|
|
56
|
+
const known = [...new Set(choices.map(entry => entry.providerId))].join(', ');
|
|
57
|
+
return { kind: 'error', text: 'dsh-provider-extra: no sign-in named "' + input.join(' ') + '". Known providers: ' + known + '. ' + usage };
|
|
58
|
+
};
|
|
55
59
|
let choice;
|
|
56
60
|
if (input.length === 0) {
|
|
57
61
|
try {
|
|
58
|
-
|
|
62
|
+
const question = pickerQuestion(choices);
|
|
63
|
+
const answer = await host.ask({
|
|
59
64
|
agent: invocation.agent,
|
|
60
|
-
questions: [
|
|
65
|
+
questions: [question],
|
|
61
66
|
signal: invocation.signal,
|
|
62
|
-
})
|
|
67
|
+
});
|
|
68
|
+
choice = resolveChoice(choices, answer);
|
|
69
|
+
const typed = answerText(answer, question.id);
|
|
70
|
+
if (choice === undefined && typed !== undefined)
|
|
71
|
+
return unknownChoice(words(typed));
|
|
63
72
|
}
|
|
64
73
|
catch (error) {
|
|
65
74
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -70,10 +79,8 @@ export function createLoginCommand(host, commandName) {
|
|
|
70
79
|
}
|
|
71
80
|
else {
|
|
72
81
|
choice = choiceByWords(choices, input);
|
|
73
|
-
if (choice === undefined)
|
|
74
|
-
|
|
75
|
-
return { kind: 'error', text: 'dsh-provider-extra: no sign-in named "' + input.join(' ') + '". Known providers: ' + known + '. ' + usage };
|
|
76
|
-
}
|
|
82
|
+
if (choice === undefined)
|
|
83
|
+
return unknownChoice(input);
|
|
77
84
|
}
|
|
78
85
|
return await runChoice(host, invocation, choice);
|
|
79
86
|
},
|