@sagmans/dsh-provider-extra 0.3.0 → 0.5.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 +95 -14
- package/dist/catalog-default.js +39 -0
- package/dist/catalog-protocols.js +15 -0
- package/dist/catalog-routes.js +369 -0
- package/dist/catalog-runtime.js +135 -0
- package/dist/catalog.js +65 -0
- package/dist/codex.js +38 -7
- package/dist/extra-models.js +93 -0
- package/dist/index.js +108 -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-verify.js +10 -5
- package/dist/opencode-go.js +10 -41
- package/docs/catalog-v1.example.json +40 -0
- package/docs/catalog.md +218 -0
- package/package.json +3 -1
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,76 +31,84 @@
|
|
|
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
|
-
|
|
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.
|
|
47
|
+
const goExtraModelSchema = Schema.object({
|
|
48
|
+
id: Schema.string().required(),
|
|
49
|
+
name: Schema.string(),
|
|
50
|
+
template: Schema.string().default(DEFAULT_EXTRA_MODEL_TEMPLATE),
|
|
51
|
+
});
|
|
52
|
+
// The Codex route ships no default template: a declaration must name the
|
|
53
|
+
// sibling it clones, because a clone from another vendor's catalog would be
|
|
54
|
+
// dispatched as if the subscription served it.
|
|
55
|
+
const codexExtraModelSchema = Schema.object({
|
|
56
|
+
id: Schema.string().required(),
|
|
57
|
+
name: Schema.string(),
|
|
58
|
+
template: Schema.string(),
|
|
59
|
+
});
|
|
60
|
+
// The assertion carries the one thing schemastery cannot state: every array in
|
|
61
|
+
// this entry is read-only to the plugin, which only ever copies it, while an
|
|
62
|
+
// array member schema is typed as the mutable array it validates.
|
|
63
|
+
export const Config = Schema.transform(Schema.object({
|
|
64
|
+
// Keep raw catalog presence: schemastery otherwise materializes absent arrays.
|
|
65
|
+
catalog: Schema.any(),
|
|
42
66
|
apiKeyEnv: Schema.string().role('credential-ref').default(DEFAULT_OPENCODE_API_KEY_ENV),
|
|
43
67
|
routeId: Schema.string().default(OPENCODE_GO_PROVIDER_ID),
|
|
44
68
|
displayName: Schema.string().default('OpenCode Go'),
|
|
45
69
|
baseURL: Schema.string(),
|
|
46
70
|
fallbackSessionId: Schema.string(),
|
|
47
71
|
headers: Schema.dict(Schema.string()),
|
|
72
|
+
extraModels: Schema.array(goExtraModelSchema),
|
|
73
|
+
models: Schema.array(Schema.string()),
|
|
48
74
|
codexEnabled: Schema.boolean().default(true),
|
|
49
75
|
codexRouteId: Schema.string().default(DEFAULT_CODEX_ROUTE_ID),
|
|
50
76
|
codexDisplayName: Schema.string().default(DEFAULT_CODEX_DISPLAY_NAME),
|
|
77
|
+
codexExtraModels: Schema.array(codexExtraModelSchema),
|
|
78
|
+
codexModels: Schema.array(Schema.string()),
|
|
79
|
+
codexTransport: Schema.union(CODEX_TRANSPORTS),
|
|
51
80
|
loginCommandEnabled: Schema.boolean().default(true),
|
|
52
81
|
loginCommandName: Schema.string().default(DEFAULT_LOGIN_COMMAND_NAME),
|
|
53
|
-
})
|
|
82
|
+
}), (value) => {
|
|
83
|
+
// Object-level validation also sees explicit null, which field transforms skip.
|
|
84
|
+
// Loader validates before disposal, retaining the previous snapshot on failure.
|
|
85
|
+
compileCatalog(value.catalog);
|
|
86
|
+
return value;
|
|
87
|
+
}, true);
|
|
88
|
+
/**
|
|
89
|
+
* One configured model selection, or nothing when the entry declared none.
|
|
90
|
+
* schemastery materializes an undeclared array member as an empty array, so an
|
|
91
|
+
* empty declaration is the absence of a selection: the route serves everything
|
|
92
|
+
* its catalog and extras resolved, exactly as a profile that never named the
|
|
93
|
+
* field does.
|
|
94
|
+
*/
|
|
95
|
+
function selection(declared) {
|
|
96
|
+
return declared === undefined || declared.length === 0 ? undefined : declared;
|
|
97
|
+
}
|
|
54
98
|
export const name = 'dsh-provider-extra';
|
|
55
99
|
export const inject = ['llm'];
|
|
56
|
-
const extraModelSchema = Schema.object({
|
|
57
|
-
id: Schema.string().required(),
|
|
58
|
-
name: Schema.string(),
|
|
59
|
-
template: Schema.string().default(DEFAULT_EXTRA_MODEL_TEMPLATE),
|
|
60
|
-
});
|
|
61
100
|
const SectionSchema = Schema.object({
|
|
62
|
-
extraModels: Schema.array(
|
|
101
|
+
extraModels: Schema.array(goExtraModelSchema).default([]),
|
|
102
|
+
codexExtraModels: Schema.array(codexExtraModelSchema).default([]),
|
|
63
103
|
});
|
|
64
|
-
/**
|
|
65
|
-
* Every catalog provider that ships an interactive login, in catalog order.
|
|
66
|
-
* The catalog is the authority on how a provider signs in — which methods it
|
|
67
|
-
* offers, and what each is called — so the picker never re-decides that here.
|
|
68
|
-
*/
|
|
69
|
-
function loginChoices() {
|
|
70
|
-
const choices = [];
|
|
71
|
-
for (const provider of builtinProviders()) {
|
|
72
|
-
const oauth = provider.auth?.oauth;
|
|
73
|
-
if (oauth?.login !== undefined) {
|
|
74
|
-
choices.push({
|
|
75
|
-
providerId: provider.id,
|
|
76
|
-
providerName: provider.name,
|
|
77
|
-
authType: 'oauth',
|
|
78
|
-
methodLabel: oauth.loginLabel ?? oauth.name,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
const apiKey = provider.auth?.apiKey;
|
|
82
|
-
if (apiKey?.login !== undefined) {
|
|
83
|
-
choices.push({
|
|
84
|
-
providerId: provider.id,
|
|
85
|
-
providerName: provider.name,
|
|
86
|
-
authType: 'api_key',
|
|
87
|
-
methodLabel: apiKey.name,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return choices;
|
|
92
|
-
}
|
|
93
|
-
/** The catalog provider behind one sign-in choice. */
|
|
94
|
-
function catalogLoginProvider(providerId) {
|
|
95
|
-
const found = builtinProviders().find(provider => provider.id === providerId);
|
|
96
|
-
if (found === undefined) {
|
|
97
|
-
throw new Error('dsh-provider-extra: the installed pi-ai catalog no longer ships provider "' + providerId + '"');
|
|
98
|
-
}
|
|
99
|
-
return found;
|
|
100
|
-
}
|
|
101
104
|
export function apply(ctx, config) {
|
|
105
|
+
const snapshot = compileCatalog(config.catalog);
|
|
106
|
+
if (snapshot !== undefined) {
|
|
107
|
+
mountCatalog(ctx, snapshot, config);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const models = selection(config.models);
|
|
111
|
+
const codexModels = selection(config.codexModels);
|
|
102
112
|
const route = {
|
|
103
113
|
provider: config.routeId,
|
|
104
114
|
displayName: config.displayName,
|
|
@@ -106,19 +116,42 @@ export function apply(ctx, config) {
|
|
|
106
116
|
...config.baseURL === undefined ? {} : { baseURL: config.baseURL },
|
|
107
117
|
...config.fallbackSessionId === undefined ? {} : { fallbackSessionId: config.fallbackSessionId },
|
|
108
118
|
...config.headers === undefined ? {} : { headers: { ...config.headers } },
|
|
119
|
+
...models === undefined ? {} : { models },
|
|
109
120
|
};
|
|
110
121
|
const codex = {
|
|
111
122
|
provider: config.codexRouteId,
|
|
112
123
|
displayName: config.codexDisplayName,
|
|
124
|
+
...codexModels === undefined ? {} : { models: codexModels },
|
|
125
|
+
...config.codexTransport === undefined ? {} : { transport: config.codexTransport },
|
|
126
|
+
};
|
|
127
|
+
// The entry's own declarations are the base layer the settings section sits
|
|
128
|
+
// over, and the value the routes serve on a harness with no settings document
|
|
129
|
+
// at all — which is why they seed the thunk below instead of only the base.
|
|
130
|
+
const entry = {
|
|
131
|
+
extraModels: [...config.extraModels ?? []],
|
|
132
|
+
codexExtraModels: [...config.codexExtraModels ?? []],
|
|
113
133
|
};
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
|
|
134
|
+
// A declared selection is configuration, not a request fact, so it is proven
|
|
135
|
+
// once here. Registration resolves a profile for its route metadata and
|
|
136
|
+
// reports a failure as a refused route, which would bury the one message that
|
|
137
|
+
// names the misconfigured id; resolving per request would bury it the same
|
|
138
|
+
// way. The extras a selection resolves against at this point are the entry's
|
|
139
|
+
// own, because a settings document may not have arrived yet.
|
|
140
|
+
if (models !== undefined)
|
|
141
|
+
buildOpenCodeGoProfile({ ...route, extraModels: entry.extraModels });
|
|
142
|
+
if (codexModels !== undefined)
|
|
143
|
+
buildCodexProfile({ ...codex, extraModels: entry.codexExtraModels });
|
|
144
|
+
// The catalog check stays boot-time even though settings can now extend the
|
|
145
|
+
// profile per request: a catalog drift (pi-ai no longer shipping Codex) must
|
|
146
|
+
// stand this route down once, loudly, here, instead of failing every request
|
|
147
|
+
// on both routes.
|
|
148
|
+
let codexServable = false;
|
|
119
149
|
if (config.codexEnabled) {
|
|
120
150
|
try {
|
|
121
|
-
|
|
151
|
+
// Catalog drift alone: the selection was already proven above, so it
|
|
152
|
+
// cannot be what this check reports.
|
|
153
|
+
buildCodexProfile({ provider: codex.provider, displayName: codex.displayName });
|
|
154
|
+
codexServable = true;
|
|
122
155
|
}
|
|
123
156
|
catch (error) {
|
|
124
157
|
ctx.logger.error('dsh-provider-extra: codex route "' + codex.provider + '" disabled; the installed pi-ai catalog cannot serve it');
|
|
@@ -128,13 +161,20 @@ export function apply(ctx, config) {
|
|
|
128
161
|
// Route wiring is boot-time, but the model list is per-request: the section
|
|
129
162
|
// thunk below tracks the settings overlay, so a committed extras change
|
|
130
163
|
// reaches the next operation with no rebuild and no restart.
|
|
131
|
-
let currentSection = () =>
|
|
164
|
+
let currentSection = () => entry;
|
|
132
165
|
const profiles = () => {
|
|
166
|
+
const section = currentSection();
|
|
167
|
+
// Selections stay bound to the entry extras that passed mount-time validation.
|
|
133
168
|
const entries = [
|
|
134
|
-
[route.provider, buildOpenCodeGoProfile({
|
|
169
|
+
[route.provider, buildOpenCodeGoProfile({
|
|
170
|
+
...route, extraModels: models !== undefined ? entry.extraModels : section.extraModels,
|
|
171
|
+
})],
|
|
135
172
|
];
|
|
136
|
-
if (
|
|
137
|
-
entries.push([codex.provider,
|
|
173
|
+
if (codexServable) {
|
|
174
|
+
entries.push([codex.provider, buildCodexProfile({
|
|
175
|
+
...codex, extraModels: codexModels !== undefined ? entry.codexExtraModels : section.codexExtraModels,
|
|
176
|
+
})]);
|
|
177
|
+
}
|
|
138
178
|
return new Map(entries);
|
|
139
179
|
};
|
|
140
180
|
const adapter = new PiAiAdapter({
|
|
@@ -190,7 +230,7 @@ export function apply(ctx, config) {
|
|
|
190
230
|
// all-or-nothing: one call for both would drop a working route when the
|
|
191
231
|
// other collides. llm-pi-ai's directory already lists the catalog id, so
|
|
192
232
|
// no directory entry is registered here — core's serves the Models page.
|
|
193
|
-
if (
|
|
233
|
+
if (codexServable) {
|
|
194
234
|
try {
|
|
195
235
|
ctx.llm.registerAdapter([codex.provider], adapter);
|
|
196
236
|
}
|
|
@@ -215,7 +255,10 @@ export function apply(ctx, config) {
|
|
|
215
255
|
ctx.logger.warn(error);
|
|
216
256
|
}
|
|
217
257
|
ctx.inject(['settings'], (settingsCtx) => {
|
|
218
|
-
|
|
258
|
+
// The section overrides the entry where a settings document exists and
|
|
259
|
+
// stands in for it where none does; the settings service falls back to this
|
|
260
|
+
// same value when it detaches.
|
|
261
|
+
settingsCtx.settings.installSection(ctx, SETTINGS_NS, SectionSchema, entry, {
|
|
219
262
|
setSource: (source) => { currentSection = source; },
|
|
220
263
|
// No registration facts derive from the section: the route set is fixed
|
|
221
264
|
// at composition and the adapter rebuilds its snapshot on every
|
|
@@ -223,81 +266,5 @@ export function apply(ctx, config) {
|
|
|
223
266
|
onChange: () => { },
|
|
224
267
|
});
|
|
225
268
|
});
|
|
226
|
-
|
|
227
|
-
// it runs in this process, so the credential lands in the store the routes
|
|
228
|
-
// already read and no bin path or peer tree is involved. It signs into any
|
|
229
|
-
// provider the installed catalog ships a login for, not only the two routes
|
|
230
|
-
// this plugin mounts, because a human asking to sign in means the provider
|
|
231
|
-
// they name and nothing narrower. Profiles without a command registry
|
|
232
|
-
// (headless compositions) keep the package bin.
|
|
233
|
-
if (config.loginCommandEnabled) {
|
|
234
|
-
ctx.inject(['commands'], (commandCtx) => {
|
|
235
|
-
const host = {
|
|
236
|
-
choices: loginChoices,
|
|
237
|
-
login: async (choice, interaction) => {
|
|
238
|
-
const credentials = () => ctx.get('credentials');
|
|
239
|
-
const auth = codexAuth(credentials);
|
|
240
|
-
const models = createModels(auth);
|
|
241
|
-
// pi-ai's collection starts empty: the catalog provider carrying the
|
|
242
|
-
// login implementation has to be handed to it before the flow runs.
|
|
243
|
-
models.setProvider(catalogLoginProvider(choice.providerId));
|
|
244
|
-
if (choice.authType !== 'api_key') {
|
|
245
|
-
await models.login(choice.providerId, choice.authType, interaction);
|
|
246
|
-
return await declareProviderRoute(ctx.get('settings'), choice.providerId);
|
|
247
|
-
}
|
|
248
|
-
// A grant the provider minted proves itself, but a key proves nothing
|
|
249
|
-
// until a request carries it, so the key is spent from a store that
|
|
250
|
-
// forgets and the profile hears of it only once it has been accepted.
|
|
251
|
-
const pending = createModels({ credentials: new PendingCredentialStore(), authContext: auth.authContext });
|
|
252
|
-
pending.setProvider(catalogLoginProvider(choice.providerId));
|
|
253
|
-
const credential = await pending.login(choice.providerId, choice.authType, interaction);
|
|
254
|
-
await proveApiKey(pending, choice.providerId);
|
|
255
|
-
await auth.credentials.modify(choice.providerId, async () => credential);
|
|
256
|
-
// The credential is only reachable through a declared route, so the
|
|
257
|
-
// sign-in is not finished until one exists.
|
|
258
|
-
return await declareProviderRoute(ctx.get('settings'), choice.providerId);
|
|
259
|
-
},
|
|
260
|
-
stored: async (providerId) => {
|
|
261
|
-
const credentials = ctx.get('credentials');
|
|
262
|
-
if (credentials === undefined)
|
|
263
|
-
return undefined;
|
|
264
|
-
const record = await credentials.readRecord(recordKeyFor(providerId));
|
|
265
|
-
if (record === undefined)
|
|
266
|
-
return undefined;
|
|
267
|
-
return record.kind === 'api-key' ? 'api_key' : 'oauth';
|
|
268
|
-
},
|
|
269
|
-
reference: async (providerId) => {
|
|
270
|
-
// A route this plugin mounts names its reference in this plugin's
|
|
271
|
-
// config; every other route is whatever llm-pi-ai was configured
|
|
272
|
-
// with, which is the same document a sign-in declares into.
|
|
273
|
-
const ref = declaredCredentialRef(ctx.get('settings'), providerId)
|
|
274
|
-
?? (providerId === route.provider ? route.apiKeyEnv : undefined);
|
|
275
|
-
if (ref === undefined || ref.length === 0)
|
|
276
|
-
return undefined;
|
|
277
|
-
const credentials = ctx.get('credentials');
|
|
278
|
-
if (credentials === undefined)
|
|
279
|
-
return { ref };
|
|
280
|
-
const info = await credentials.describe(ref);
|
|
281
|
-
return { ref, ...info.configured && info.source !== undefined ? { source: info.source } : {} };
|
|
282
|
-
},
|
|
283
|
-
// Resolved per call, never at mount: a composition that mounts no
|
|
284
|
-
// session UI still gets the command, and says so when it runs.
|
|
285
|
-
ask: async (request) => {
|
|
286
|
-
const userQuestions = ctx.get('userQuestions');
|
|
287
|
-
if (userQuestions === undefined) {
|
|
288
|
-
throw new Error('dsh-provider-extra: this composition mounts no session UI that can ask the sign-in questions');
|
|
289
|
-
}
|
|
290
|
-
return await userQuestions.ask({
|
|
291
|
-
questions: [...request.questions],
|
|
292
|
-
agent: request.agent,
|
|
293
|
-
...request.signal === undefined ? {} : { signal: request.signal },
|
|
294
|
-
});
|
|
295
|
-
},
|
|
296
|
-
};
|
|
297
|
-
commandCtx.commands.register(createLoginCommand(host, config.loginCommandName));
|
|
298
|
-
// Registration is silent otherwise, and the command is the one surface a
|
|
299
|
-
// user cannot see in a config dump: saying it exists is the diagnosis.
|
|
300
|
-
ctx.logger.info('dsh-provider-extra: /' + config.loginCommandName + ' signs in any provider with an interactive login');
|
|
301
|
-
});
|
|
302
|
-
}
|
|
269
|
+
mountLoginCommand(ctx, config, { profiles });
|
|
303
270
|
}
|
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
|
},
|