@router-for-me/pi-cliproxyapi-provider 1.4.10 → 1.4.12
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 +49 -27
- package/extensions/fast-footer.ts +38 -4
- package/extensions/index.ts +372 -39
- package/extensions/lib.ts +457 -7
- package/extensions/pause.ts +65 -0
- package/extensions/tps.ts +23 -4
- package/package.json +1 -1
package/extensions/index.ts
CHANGED
|
@@ -34,14 +34,17 @@ import {
|
|
|
34
34
|
isUnauthorizedModelsError,
|
|
35
35
|
loadAuthConnection,
|
|
36
36
|
loadConfigFile,
|
|
37
|
-
loadMappedModels,
|
|
38
37
|
type PiProviderModel,
|
|
39
38
|
resolveConnection,
|
|
40
39
|
resolveEndpoints,
|
|
41
40
|
resolveFastDefault,
|
|
42
41
|
resolveIdentity,
|
|
42
|
+
resolveMappedModels,
|
|
43
|
+
resolvePauseDefault,
|
|
43
44
|
saveConfigFile,
|
|
44
45
|
} from "./lib.ts";
|
|
46
|
+
import type { PauseController } from "./pause.ts";
|
|
47
|
+
import { pauseController, waitForPauseToEnd } from "./pause.ts";
|
|
45
48
|
import { registerTransientNetworkErrorRetry } from "./retry.ts";
|
|
46
49
|
|
|
47
50
|
class ConfigPersistenceError extends Error {
|
|
@@ -52,6 +55,28 @@ class ConfigPersistenceError extends Error {
|
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
interface RefreshResult {
|
|
59
|
+
modelCount: number;
|
|
60
|
+
modelsUrl: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class ModelRefreshCoordinator {
|
|
64
|
+
private generation = 0;
|
|
65
|
+
private activeController: AbortController | undefined;
|
|
66
|
+
|
|
67
|
+
begin(): { generation: number; signal: AbortSignal } {
|
|
68
|
+
this.activeController?.abort();
|
|
69
|
+
const controller = new AbortController();
|
|
70
|
+
this.activeController = controller;
|
|
71
|
+
this.generation += 1;
|
|
72
|
+
return { generation: this.generation, signal: controller.signal };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
isCurrent(generation: number): boolean {
|
|
76
|
+
return this.generation === generation;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
55
80
|
function logWarn(message: string): void {
|
|
56
81
|
console.warn(`[pi-cliproxyapi-provider] ${message}`);
|
|
57
82
|
}
|
|
@@ -139,11 +164,33 @@ async function configureAndRegister(options: {
|
|
|
139
164
|
defaultBaseUrl: string;
|
|
140
165
|
streamSimple: CliproxyCodexStreamSimple;
|
|
141
166
|
fastMode: FastModeController;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
167
|
+
refreshCoordinator: ModelRefreshCoordinator;
|
|
168
|
+
onFastModeChange?: (enabled: boolean, ctx: ExtensionContext) => Promise<void>;
|
|
169
|
+
}): Promise<RefreshResult> {
|
|
170
|
+
const {
|
|
171
|
+
pi,
|
|
172
|
+
agentDir,
|
|
173
|
+
providerId,
|
|
174
|
+
providerName,
|
|
175
|
+
baseUrlInput,
|
|
176
|
+
apiKey,
|
|
177
|
+
defaultBaseUrl,
|
|
178
|
+
streamSimple,
|
|
179
|
+
fastMode,
|
|
180
|
+
refreshCoordinator,
|
|
181
|
+
onFastModeChange,
|
|
182
|
+
} = options;
|
|
183
|
+
|
|
184
|
+
const refresh = refreshCoordinator.begin();
|
|
185
|
+
const { loaded } = await resolveMappedModels(agentDir, baseUrlInput, apiKey, {
|
|
186
|
+
forceRefresh: true,
|
|
187
|
+
fastMode: fastMode.isEnabled(),
|
|
188
|
+
signal: refresh.signal,
|
|
189
|
+
shouldCommit: () => refreshCoordinator.isCurrent(refresh.generation),
|
|
190
|
+
});
|
|
191
|
+
if (!refreshCoordinator.isCurrent(refresh.generation)) {
|
|
192
|
+
throw new Error("Model refresh was superseded by a newer request.");
|
|
193
|
+
}
|
|
147
194
|
|
|
148
195
|
try {
|
|
149
196
|
saveConfigFile(agentDir, {
|
|
@@ -167,6 +214,8 @@ async function configureAndRegister(options: {
|
|
|
167
214
|
agentDir,
|
|
168
215
|
streamSimple,
|
|
169
216
|
fastMode,
|
|
217
|
+
refreshCoordinator,
|
|
218
|
+
onFastModeChange,
|
|
170
219
|
});
|
|
171
220
|
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
172
221
|
|
|
@@ -181,8 +230,20 @@ function createOAuthHandlers(options: {
|
|
|
181
230
|
defaultBaseUrl: string;
|
|
182
231
|
streamSimple: CliproxyCodexStreamSimple;
|
|
183
232
|
fastMode: FastModeController;
|
|
233
|
+
refreshCoordinator: ModelRefreshCoordinator;
|
|
234
|
+
onFastModeChange?: (enabled: boolean, ctx: ExtensionContext) => Promise<void>;
|
|
184
235
|
}) {
|
|
185
|
-
const {
|
|
236
|
+
const {
|
|
237
|
+
pi,
|
|
238
|
+
agentDir,
|
|
239
|
+
providerId,
|
|
240
|
+
providerName,
|
|
241
|
+
defaultBaseUrl,
|
|
242
|
+
streamSimple,
|
|
243
|
+
fastMode,
|
|
244
|
+
refreshCoordinator,
|
|
245
|
+
onFastModeChange,
|
|
246
|
+
} = options;
|
|
186
247
|
|
|
187
248
|
return {
|
|
188
249
|
name: providerName,
|
|
@@ -209,6 +270,8 @@ function createOAuthHandlers(options: {
|
|
|
209
270
|
defaultBaseUrl,
|
|
210
271
|
streamSimple,
|
|
211
272
|
fastMode,
|
|
273
|
+
refreshCoordinator,
|
|
274
|
+
onFastModeChange,
|
|
212
275
|
});
|
|
213
276
|
|
|
214
277
|
logInfo(`login ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
|
|
@@ -268,10 +331,23 @@ function registerProvider(
|
|
|
268
331
|
agentDir: string;
|
|
269
332
|
streamSimple: CliproxyCodexStreamSimple;
|
|
270
333
|
fastMode: FastModeController;
|
|
334
|
+
refreshCoordinator?: ModelRefreshCoordinator;
|
|
335
|
+
onFastModeChange?: (enabled: boolean, ctx: ExtensionContext) => Promise<void>;
|
|
271
336
|
},
|
|
272
337
|
): void {
|
|
273
|
-
const {
|
|
274
|
-
|
|
338
|
+
const {
|
|
339
|
+
providerId,
|
|
340
|
+
providerName,
|
|
341
|
+
baseUrlInput,
|
|
342
|
+
apiKey,
|
|
343
|
+
models,
|
|
344
|
+
defaultBaseUrl,
|
|
345
|
+
agentDir,
|
|
346
|
+
streamSimple,
|
|
347
|
+
fastMode,
|
|
348
|
+
onFastModeChange,
|
|
349
|
+
} = options;
|
|
350
|
+
const refreshCoordinator = options.refreshCoordinator ?? new ModelRefreshCoordinator();
|
|
275
351
|
|
|
276
352
|
const endpoints = resolveEndpoints(baseUrlInput);
|
|
277
353
|
const oauth = createOAuthHandlers({
|
|
@@ -282,6 +358,8 @@ function registerProvider(
|
|
|
282
358
|
defaultBaseUrl,
|
|
283
359
|
streamSimple,
|
|
284
360
|
fastMode,
|
|
361
|
+
refreshCoordinator,
|
|
362
|
+
onFastModeChange,
|
|
285
363
|
});
|
|
286
364
|
|
|
287
365
|
// Replace any previous registration so an earlier ambient apiKey does not linger
|
|
@@ -302,14 +380,64 @@ function registerProvider(
|
|
|
302
380
|
});
|
|
303
381
|
}
|
|
304
382
|
|
|
383
|
+
export function registerPauseCommands(options: {
|
|
384
|
+
pi: ExtensionAPI;
|
|
385
|
+
agentDir: string;
|
|
386
|
+
pauseMode: PauseController;
|
|
387
|
+
}): void {
|
|
388
|
+
const { pi, agentDir, pauseMode } = options;
|
|
389
|
+
|
|
390
|
+
const setPause = async (
|
|
391
|
+
enabled: boolean,
|
|
392
|
+
commandName: string,
|
|
393
|
+
args: string,
|
|
394
|
+
ctx: ExtensionContext,
|
|
395
|
+
): Promise<void> => {
|
|
396
|
+
if (args.trim()) {
|
|
397
|
+
ctx.ui.notify(`Usage: /${commandName}`, "error");
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
try {
|
|
402
|
+
saveConfigFile(agentDir, { pause: enabled });
|
|
403
|
+
} catch (error) {
|
|
404
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
405
|
+
ctx.ui.notify(`Failed to save pause mode: ${message}`, "error");
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
pauseMode.setEnabled(enabled);
|
|
410
|
+
ctx.ui.notify(enabled ? "Requests are paused." : "Requests are continued.", "info");
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
pi.registerCommand("pause", {
|
|
414
|
+
description: "Pause provider requests until /continue is used.",
|
|
415
|
+
handler: async (args, ctx) => setPause(true, "pause", args, ctx),
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
pi.registerCommand("continue", {
|
|
419
|
+
description: "Continue provider requests paused by /pause.",
|
|
420
|
+
handler: async (args, ctx) => setPause(false, "continue", args, ctx),
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function registerPauseGuard(options: { pi: ExtensionAPI; agentDir: string; pauseMode: PauseController }): void {
|
|
425
|
+
const { pi, agentDir, pauseMode } = options;
|
|
426
|
+
pi.on("before_provider_request", async () => {
|
|
427
|
+
await waitForPauseToEnd(agentDir, pauseMode);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
305
431
|
export function registerFastCommand(options: {
|
|
306
432
|
pi: ExtensionAPI;
|
|
307
433
|
agentDir: string;
|
|
308
434
|
providerId: string;
|
|
309
435
|
fastMode: FastModeController;
|
|
310
436
|
onStatusChange?: (ctx: ExtensionContext) => void;
|
|
437
|
+
onModeChange?: (enabled: boolean, ctx: ExtensionContext) => Promise<void>;
|
|
311
438
|
}): void {
|
|
312
|
-
const { pi, agentDir, providerId, fastMode, onStatusChange } = options;
|
|
439
|
+
const { pi, agentDir, providerId, fastMode, onStatusChange, onModeChange } = options;
|
|
440
|
+
let modeChangeInProgress = false;
|
|
313
441
|
|
|
314
442
|
pi.registerCommand("fast", {
|
|
315
443
|
description: "Toggle CLIProxyAPI Fast mode globally.",
|
|
@@ -318,25 +446,144 @@ export function registerFastCommand(options: {
|
|
|
318
446
|
ctx.ui.notify("Usage: /fast", "error");
|
|
319
447
|
return;
|
|
320
448
|
}
|
|
449
|
+
if (modeChangeInProgress) {
|
|
450
|
+
ctx.ui.notify("Fast mode is already being refreshed. Try again when it finishes.", "warning");
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
321
453
|
|
|
322
|
-
|
|
454
|
+
modeChangeInProgress = true;
|
|
323
455
|
try {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
456
|
+
const previousEnabled = fastMode.isEnabled();
|
|
457
|
+
const enabled = !previousEnabled;
|
|
458
|
+
try {
|
|
459
|
+
saveConfigFile(agentDir, { fast: enabled });
|
|
460
|
+
} catch (error) {
|
|
461
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
462
|
+
ctx.ui.notify(`Failed to save Fast mode: ${message}`, "error");
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
fastMode.setEnabled(enabled);
|
|
466
|
+
try {
|
|
467
|
+
await onModeChange?.(enabled, ctx);
|
|
468
|
+
} catch (error) {
|
|
469
|
+
// Restore all three views of the mode after a partial refresh:
|
|
470
|
+
// in-memory request behavior, persisted preference, and model metadata.
|
|
471
|
+
fastMode.setEnabled(previousEnabled);
|
|
472
|
+
const rollbackErrors: string[] = [];
|
|
473
|
+
try {
|
|
474
|
+
saveConfigFile(agentDir, { fast: previousEnabled });
|
|
475
|
+
} catch (rollbackError) {
|
|
476
|
+
rollbackErrors.push(
|
|
477
|
+
`config rollback failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`,
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
try {
|
|
481
|
+
await onModeChange?.(previousEnabled, ctx);
|
|
482
|
+
} catch (rollbackError) {
|
|
483
|
+
rollbackErrors.push(
|
|
484
|
+
`pricing rollback failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`,
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
488
|
+
const rollbackSuffix = rollbackErrors.length > 0 ? ` (${rollbackErrors.join("; ")})` : "";
|
|
489
|
+
ctx.ui.notify(`Failed to refresh model pricing: ${message}${rollbackSuffix}`, "warning");
|
|
490
|
+
onStatusChange?.(ctx);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
onStatusChange?.(ctx);
|
|
494
|
+
|
|
495
|
+
const currentModel = ctx.model;
|
|
496
|
+
if (!currentModel || currentModel.provider !== providerId || !fastMode.isModelSupported(currentModel.id)) {
|
|
497
|
+
if (enabled) {
|
|
498
|
+
ctx.ui.notify("Fast mode is enabled globally, but the current model does not support it.", "warning");
|
|
499
|
+
} else {
|
|
500
|
+
ctx.ui.notify("Fast mode is disabled globally.", "info");
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
} finally {
|
|
504
|
+
modeChangeInProgress = false;
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export function registerRefreshCommand(options: {
|
|
511
|
+
pi: ExtensionAPI;
|
|
512
|
+
agentDir: string;
|
|
513
|
+
providerId: string;
|
|
514
|
+
providerName: string;
|
|
515
|
+
defaultBaseUrl: string;
|
|
516
|
+
streamSimple: CliproxyCodexStreamSimple;
|
|
517
|
+
fastMode: FastModeController;
|
|
518
|
+
refreshCoordinator?: ModelRefreshCoordinator;
|
|
519
|
+
onRefresh?: (connection: NonNullable<ReturnType<typeof resolveConnection>>) => Promise<RefreshResult | undefined>;
|
|
520
|
+
}): void {
|
|
521
|
+
const {
|
|
522
|
+
pi,
|
|
523
|
+
agentDir,
|
|
524
|
+
providerId,
|
|
525
|
+
providerName,
|
|
526
|
+
defaultBaseUrl,
|
|
527
|
+
streamSimple,
|
|
528
|
+
fastMode,
|
|
529
|
+
refreshCoordinator,
|
|
530
|
+
onRefresh,
|
|
531
|
+
} = options;
|
|
532
|
+
|
|
533
|
+
pi.registerCommand("cliproxyapi-refresh", {
|
|
534
|
+
description: "Force refresh CLIProxyAPI models from the remote catalog.",
|
|
535
|
+
handler: async (args, ctx) => {
|
|
536
|
+
if (args.trim()) {
|
|
537
|
+
ctx.ui.notify("Usage: /cliproxyapi-refresh", "error");
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const connection = resolveConnection(agentDir, providerId);
|
|
542
|
+
if (!connection) {
|
|
543
|
+
ctx.ui.notify(
|
|
544
|
+
`CLIProxyAPI is not configured. Use /login ${providerName} or /login ${providerId}.`,
|
|
545
|
+
"error",
|
|
546
|
+
);
|
|
328
547
|
return;
|
|
329
548
|
}
|
|
330
|
-
fastMode.setEnabled(enabled);
|
|
331
|
-
onStatusChange?.(ctx);
|
|
332
549
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (
|
|
336
|
-
|
|
550
|
+
try {
|
|
551
|
+
let result: RefreshResult | undefined;
|
|
552
|
+
if (onRefresh) {
|
|
553
|
+
result = await onRefresh(connection);
|
|
554
|
+
if (!result) return;
|
|
337
555
|
} else {
|
|
338
|
-
|
|
556
|
+
const refresh = refreshCoordinator?.begin();
|
|
557
|
+
const { loaded } = await resolveMappedModels(agentDir, connection.baseUrlInput, connection.apiKey, {
|
|
558
|
+
forceRefresh: true,
|
|
559
|
+
fastMode: fastMode.isEnabled(),
|
|
560
|
+
signal: refresh?.signal,
|
|
561
|
+
shouldCommit: refresh ? () => refreshCoordinator?.isCurrent(refresh.generation) ?? true : undefined,
|
|
562
|
+
});
|
|
563
|
+
if (refresh && !refreshCoordinator?.isCurrent(refresh.generation)) return;
|
|
564
|
+
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
565
|
+
|
|
566
|
+
const hasStoredLogin = hasLoginCredential(agentDir, providerId);
|
|
567
|
+
registerProvider(pi, {
|
|
568
|
+
providerId,
|
|
569
|
+
providerName,
|
|
570
|
+
baseUrlInput: connection.baseUrlInput,
|
|
571
|
+
apiKey: hasStoredLogin ? undefined : connection.apiKey,
|
|
572
|
+
models: loaded.models,
|
|
573
|
+
defaultBaseUrl,
|
|
574
|
+
agentDir,
|
|
575
|
+
streamSimple,
|
|
576
|
+
fastMode,
|
|
577
|
+
refreshCoordinator,
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
result = { modelCount: loaded.models.length, modelsUrl: loaded.modelsUrl };
|
|
339
581
|
}
|
|
582
|
+
|
|
583
|
+
ctx.ui.notify(`Refreshed ${result.modelCount} CLIProxyAPI models from ${result.modelsUrl}.`, "info");
|
|
584
|
+
} catch (error) {
|
|
585
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
586
|
+
ctx.ui.notify(`Failed to refresh CLIProxyAPI models: ${message}`, "error");
|
|
340
587
|
}
|
|
341
588
|
},
|
|
342
589
|
});
|
|
@@ -349,6 +596,18 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
349
596
|
const agentDir = getAgentDir();
|
|
350
597
|
const identity = resolveIdentity(agentDir);
|
|
351
598
|
const defaultBaseUrl = resolveDefaultBaseUrl(agentDir, identity.providerId);
|
|
599
|
+
|
|
600
|
+
let pauseEnabled = false;
|
|
601
|
+
try {
|
|
602
|
+
pauseEnabled = resolvePauseDefault(agentDir);
|
|
603
|
+
} catch (error) {
|
|
604
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
605
|
+
logWarn(`invalid pause configuration (${message}); using pause=false`);
|
|
606
|
+
}
|
|
607
|
+
pauseController.setEnabled(pauseEnabled);
|
|
608
|
+
registerPauseCommands({ pi, agentDir, pauseMode: pauseController });
|
|
609
|
+
registerPauseGuard({ pi, agentDir, pauseMode: pauseController });
|
|
610
|
+
|
|
352
611
|
const proactiveCompaction = new ProactiveCompactionController(agentDir, identity.providerId);
|
|
353
612
|
proactiveCompaction.register(pi);
|
|
354
613
|
|
|
@@ -360,6 +619,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
360
619
|
logWarn(`invalid Fast configuration (${message}); using fast=false`);
|
|
361
620
|
}
|
|
362
621
|
const fastMode = new FastModeController(fastEnabled);
|
|
622
|
+
const modelRefreshCoordinator = new ModelRefreshCoordinator();
|
|
363
623
|
|
|
364
624
|
let streamSimple: CliproxyCodexStreamSimple;
|
|
365
625
|
try {
|
|
@@ -376,12 +636,17 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
376
636
|
const fastFooter = new FastFooterController(identity.providerId, fastMode, () =>
|
|
377
637
|
proactiveCompaction.getCompactionSettings(),
|
|
378
638
|
);
|
|
639
|
+
let refreshModelsForFast: ((ctx: ExtensionContext) => Promise<void>) | undefined;
|
|
640
|
+
const onFastModeChange = async (_enabled: boolean, ctx: ExtensionContext): Promise<void> => {
|
|
641
|
+
await refreshModelsForFast?.(ctx);
|
|
642
|
+
};
|
|
379
643
|
registerFastCommand({
|
|
380
644
|
pi,
|
|
381
645
|
agentDir,
|
|
382
646
|
providerId: identity.providerId,
|
|
383
647
|
fastMode,
|
|
384
648
|
onStatusChange: (ctx) => fastFooter.refresh(ctx),
|
|
649
|
+
onModeChange: onFastModeChange,
|
|
385
650
|
});
|
|
386
651
|
fastFooter.register(pi);
|
|
387
652
|
|
|
@@ -394,10 +659,94 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
394
659
|
agentDir,
|
|
395
660
|
streamSimple,
|
|
396
661
|
fastMode,
|
|
662
|
+
refreshCoordinator: modelRefreshCoordinator,
|
|
663
|
+
onFastModeChange: onFastModeChange,
|
|
397
664
|
});
|
|
398
665
|
registerTransientNetworkErrorRetry(pi, identity.providerId);
|
|
399
666
|
|
|
400
667
|
const connection = resolveConnection(agentDir, identity.providerId);
|
|
668
|
+
const registerConfiguredProvider = async (
|
|
669
|
+
currentConnection: NonNullable<ReturnType<typeof resolveConnection>>,
|
|
670
|
+
options: { forceRefresh?: boolean } = {},
|
|
671
|
+
): Promise<RefreshResult | undefined> => {
|
|
672
|
+
const refresh = modelRefreshCoordinator.begin();
|
|
673
|
+
try {
|
|
674
|
+
const { loaded, fromCache } = await resolveMappedModels(
|
|
675
|
+
agentDir,
|
|
676
|
+
currentConnection.baseUrlInput,
|
|
677
|
+
currentConnection.apiKey,
|
|
678
|
+
{
|
|
679
|
+
forceRefresh: options.forceRefresh,
|
|
680
|
+
fastMode: fastMode.isEnabled(),
|
|
681
|
+
signal: refresh.signal,
|
|
682
|
+
shouldCommit: () => modelRefreshCoordinator.isCurrent(refresh.generation),
|
|
683
|
+
},
|
|
684
|
+
);
|
|
685
|
+
if (!modelRefreshCoordinator.isCurrent(refresh.generation)) return undefined;
|
|
686
|
+
|
|
687
|
+
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
688
|
+
|
|
689
|
+
// Prefer OAuth-only registration when /login already stored credentials so
|
|
690
|
+
// `/login <provider>` jumps straight into the multi-field flow. Fall back to
|
|
691
|
+
// ambient apiKey only for config-file / env setups without auth.json.
|
|
692
|
+
const hasStoredLogin = hasLoginCredential(agentDir, identity.providerId);
|
|
693
|
+
registerProvider(pi, {
|
|
694
|
+
providerId: identity.providerId,
|
|
695
|
+
providerName: identity.providerName,
|
|
696
|
+
baseUrlInput: currentConnection.baseUrlInput,
|
|
697
|
+
apiKey: hasStoredLogin ? undefined : currentConnection.apiKey,
|
|
698
|
+
models: loaded.models,
|
|
699
|
+
defaultBaseUrl,
|
|
700
|
+
agentDir,
|
|
701
|
+
streamSimple,
|
|
702
|
+
fastMode,
|
|
703
|
+
refreshCoordinator: modelRefreshCoordinator,
|
|
704
|
+
onFastModeChange,
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
if (fromCache && !options.forceRefresh) {
|
|
708
|
+
void registerConfiguredProvider(currentConnection, { forceRefresh: true }).catch((error) => {
|
|
709
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
710
|
+
logWarn(`failed to refresh cached models (${message}); keeping the cached model list.`);
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
return { modelCount: loaded.models.length, modelsUrl: loaded.modelsUrl };
|
|
715
|
+
} catch (error) {
|
|
716
|
+
if (!modelRefreshCoordinator.isCurrent(refresh.generation)) return undefined;
|
|
717
|
+
throw error;
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
refreshModelsForFast = async (ctx: ExtensionContext): Promise<void> => {
|
|
721
|
+
const currentConnection = resolveConnection(agentDir, identity.providerId);
|
|
722
|
+
if (!currentConnection) return;
|
|
723
|
+
|
|
724
|
+
const refreshed = await registerConfiguredProvider(currentConnection, { forceRefresh: true });
|
|
725
|
+
if (!refreshed) return;
|
|
726
|
+
const currentModel = ctx.model;
|
|
727
|
+
if (!currentModel || currentModel.provider !== identity.providerId) return;
|
|
728
|
+
|
|
729
|
+
const refreshedModel = ctx.modelRegistry.find(identity.providerId, currentModel.id);
|
|
730
|
+
if (!refreshedModel) {
|
|
731
|
+
throw new Error(`Refreshed model ${identity.providerId}/${currentModel.id} is unavailable`);
|
|
732
|
+
}
|
|
733
|
+
if (JSON.stringify(refreshedModel.cost) === JSON.stringify(currentModel.cost)) return;
|
|
734
|
+
if (!(await pi.setModel(refreshedModel))) {
|
|
735
|
+
throw new Error(`Unable to activate refreshed model ${identity.providerId}/${currentModel.id}`);
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
registerRefreshCommand({
|
|
739
|
+
pi,
|
|
740
|
+
agentDir,
|
|
741
|
+
providerId: identity.providerId,
|
|
742
|
+
providerName: identity.providerName,
|
|
743
|
+
defaultBaseUrl,
|
|
744
|
+
streamSimple,
|
|
745
|
+
fastMode,
|
|
746
|
+
refreshCoordinator: modelRefreshCoordinator,
|
|
747
|
+
onRefresh: (currentConnection) => registerConfiguredProvider(currentConnection, { forceRefresh: true }),
|
|
748
|
+
});
|
|
749
|
+
|
|
401
750
|
if (!connection) {
|
|
402
751
|
logInfo(
|
|
403
752
|
`not configured yet. Use /login ${identity.providerName} or /login ${identity.providerId}. ` +
|
|
@@ -408,23 +757,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
408
757
|
}
|
|
409
758
|
|
|
410
759
|
try {
|
|
411
|
-
|
|
412
|
-
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
413
|
-
// Prefer OAuth-only registration when /login already stored credentials so
|
|
414
|
-
// `/login <provider>` jumps straight into the multi-field flow. Fall back to
|
|
415
|
-
// ambient apiKey only for config-file / env setups without auth.json.
|
|
416
|
-
const hasStoredLogin = hasLoginCredential(agentDir, identity.providerId);
|
|
417
|
-
registerProvider(pi, {
|
|
418
|
-
providerId: identity.providerId,
|
|
419
|
-
providerName: identity.providerName,
|
|
420
|
-
baseUrlInput: connection.baseUrlInput,
|
|
421
|
-
apiKey: hasStoredLogin ? undefined : connection.apiKey,
|
|
422
|
-
models: loaded.models,
|
|
423
|
-
defaultBaseUrl,
|
|
424
|
-
agentDir,
|
|
425
|
-
streamSimple,
|
|
426
|
-
fastMode,
|
|
427
|
-
});
|
|
760
|
+
await registerConfiguredProvider(connection);
|
|
428
761
|
} catch (error) {
|
|
429
762
|
const message = error instanceof Error ? error.message : String(error);
|
|
430
763
|
if (isUnauthorizedModelsError(error)) {
|