@router-for-me/pi-cliproxyapi-provider 1.4.2 → 1.4.4
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 +8 -7
- package/extensions/index.ts +180 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ This plugin needs both **baseUrl** and **apiKey**. pi's built-in `/login` only s
|
|
|
35
35
|
/cliproxyapi
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
This slash command
|
|
38
|
+
This slash command is shown only when CLIProxyAPI is **not** already authenticated via `/login` (no matching credential in `auth.json`). After a successful `/login`, it is removed from the command registry and filtered out of `/` autocomplete; reconfigure with `/login CLIProxyAPI`, or run `/logout` first to bring `/cliproxyapi` back. Config-file / env-only setups (no `/login` credential) keep the command available for reconfiguration. Restart pi or run `/reload` after upgrading so the new visibility logic is loaded.
|
|
39
39
|
|
|
40
40
|
### Or use /login shortcuts (skip the menu)
|
|
41
41
|
|
|
@@ -189,13 +189,14 @@ Disable just this helper via `pi config` if you only want the CLIProxyAPI provid
|
|
|
189
189
|
## Failure behavior
|
|
190
190
|
|
|
191
191
|
- Before setup / without credentials: provider still appears in `/login`; `/cliproxyapi` is shown; no models are listed yet.
|
|
192
|
-
- After successful login
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
-
|
|
196
|
-
- If
|
|
192
|
+
- After successful `/login`: `/cliproxyapi` is hidden; reconfigure via `/login CLIProxyAPI`.
|
|
193
|
+
- After successful `/cliproxyapi` (config-only, no `auth.json` login): `/cliproxyapi` stays available for reconfiguration.
|
|
194
|
+
- If an `auth.json` credential exists and `/cliproxyapi` is invoked somehow, it asks you to use `/login CLIProxyAPI` or run `/logout` first so the old credential cannot override the new key.
|
|
195
|
+
- The built-in `/logout` command removes only the matching `auth.json` credential; environment variables and `cliproxyapi.json` are unchanged. After logout, `/cliproxyapi` is shown again (on the next submitted input or session start).
|
|
196
|
+
- If a models request returns **HTTP 401** and no `/login` credential remains, the setup command is shown for reconfiguration; otherwise use `/login`.
|
|
197
|
+
- If CPA is unreachable during startup (non-401), a warning is logged; setup stays available via `/cliproxyapi` (when not logged in) or `/login`.
|
|
197
198
|
- Login/setup final step validates credentials by requesting models:
|
|
198
|
-
- HTTP 200 (including empty catalog) → credentials are persisted;
|
|
199
|
+
- HTTP 200 (including empty catalog) → credentials are persisted; `/login` hides `/cliproxyapi`
|
|
199
200
|
- non-200 / network / invalid baseUrl → nothing is persisted; re-enter baseUrl + API key
|
|
200
201
|
- If CPA returns HTTP 200 with zero usable models: login still succeeds; re-run setup later after models become available.
|
|
201
202
|
- If the selected model does not provide a non-empty `service_tiers` array: the request is left unchanged; `/fast` still updates the global preference and warns when enabling it.
|
package/extensions/index.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* skip the API-key vs account selector and go straight to multi-field prompts
|
|
7
7
|
* (pi only supports multi-field prompts on the account/OAuth path).
|
|
8
8
|
* 2. Preferred shortcuts: `/login CLIProxyAPI`, `/login cliproxyapi`,
|
|
9
|
-
* or the dedicated `/cliproxyapi` command.
|
|
9
|
+
* or the dedicated `/cliproxyapi` command (hidden after successful /login).
|
|
10
10
|
* 3. Setup prompts for baseUrl + apiKey.
|
|
11
11
|
* 4. Final login step validates credentials via /v1/models?client_version=pi
|
|
12
12
|
* (HTTP 200 = success even if the catalog is empty; otherwise re-prompt).
|
|
@@ -48,9 +48,23 @@ import {
|
|
|
48
48
|
saveConfigFile,
|
|
49
49
|
} from "./lib.ts";
|
|
50
50
|
|
|
51
|
-
/**
|
|
51
|
+
/** Show/hide /cliproxyapi in the slash-command menu. */
|
|
52
52
|
interface SetupCommandsController {
|
|
53
53
|
show(): void;
|
|
54
|
+
hide(): void;
|
|
55
|
+
isVisible(): boolean;
|
|
56
|
+
/** Hide when /login credentials exist; show otherwise. */
|
|
57
|
+
syncFromAuth(): void;
|
|
58
|
+
/** Whether /login currently has a stored credential for this provider. */
|
|
59
|
+
hasLoginCredential(): boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function hasLoginCredential(agentDir: string, providerId: string): boolean {
|
|
63
|
+
try {
|
|
64
|
+
return Boolean(loadAuthConnection(agentDir, providerId)?.apiKey);
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
54
68
|
}
|
|
55
69
|
|
|
56
70
|
class ConfigPersistenceError extends Error {
|
|
@@ -233,6 +247,8 @@ function createOAuthHandlers(options: {
|
|
|
233
247
|
fastMode,
|
|
234
248
|
});
|
|
235
249
|
|
|
250
|
+
// /login succeeded — dedicated setup is unnecessary until logout.
|
|
251
|
+
setupCommands.hide();
|
|
236
252
|
logInfo(`login ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
|
|
237
253
|
return buildOAuthCredentials(baseUrlInput, apiKey);
|
|
238
254
|
} catch (error) {
|
|
@@ -340,7 +356,12 @@ function registerProvider(
|
|
|
340
356
|
});
|
|
341
357
|
}
|
|
342
358
|
|
|
343
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* Register /cliproxyapi and allow hiding it after /login.
|
|
361
|
+
*
|
|
362
|
+
* pi has registerCommand but no unregisterCommand, so hide() removes the entry
|
|
363
|
+
* from the extension's internal commands Map captured during first registration.
|
|
364
|
+
*/
|
|
344
365
|
function createSetupCommandsController(options: {
|
|
345
366
|
pi: ExtensionAPI;
|
|
346
367
|
agentDir: string;
|
|
@@ -352,14 +373,28 @@ function createSetupCommandsController(options: {
|
|
|
352
373
|
}): SetupCommandsController {
|
|
353
374
|
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, fastMode } = options;
|
|
354
375
|
|
|
355
|
-
// Self-referential controller so
|
|
376
|
+
// Self-referential controller so handlers can hide/show after validation.
|
|
356
377
|
const setupCommands: SetupCommandsController = {
|
|
357
378
|
show() {
|
|
358
379
|
/* replaced below */
|
|
359
380
|
},
|
|
381
|
+
hide() {
|
|
382
|
+
/* replaced below */
|
|
383
|
+
},
|
|
384
|
+
isVisible() {
|
|
385
|
+
return false;
|
|
386
|
+
},
|
|
387
|
+
syncFromAuth() {
|
|
388
|
+
/* replaced below */
|
|
389
|
+
},
|
|
390
|
+
hasLoginCredential() {
|
|
391
|
+
return false;
|
|
392
|
+
},
|
|
360
393
|
};
|
|
361
394
|
|
|
362
395
|
let visible = false;
|
|
396
|
+
// Captured extension.commands Map (pi has no public unregisterCommand API).
|
|
397
|
+
let commandMap: Map<string, unknown> | null = null;
|
|
363
398
|
|
|
364
399
|
const handler = async (_args: string, ctx: ExtensionCommandContext): Promise<void> => {
|
|
365
400
|
if (!ctx.hasUI) {
|
|
@@ -370,7 +405,9 @@ function createSetupCommandsController(options: {
|
|
|
370
405
|
return;
|
|
371
406
|
}
|
|
372
407
|
|
|
373
|
-
if (
|
|
408
|
+
if (hasLoginCredential(agentDir, providerId)) {
|
|
409
|
+
// Still authenticated via /login — hide the dedicated command and redirect.
|
|
410
|
+
setupCommands.hide();
|
|
374
411
|
ctx.ui.notify(
|
|
375
412
|
`CLIProxyAPI credentials are already stored by /login. Use /login ${providerName} to replace them, or run /logout before /cliproxyapi.`,
|
|
376
413
|
"error",
|
|
@@ -489,14 +526,114 @@ function createSetupCommandsController(options: {
|
|
|
489
526
|
if (visible) {
|
|
490
527
|
return;
|
|
491
528
|
}
|
|
492
|
-
|
|
529
|
+
|
|
530
|
+
if (!commandMap) {
|
|
531
|
+
// Capture the extension.commands Map during first registration.
|
|
532
|
+
const originalSet = Map.prototype.set;
|
|
533
|
+
Map.prototype.set = function setWithCapture(this: Map<unknown, unknown>, key: unknown, value: unknown) {
|
|
534
|
+
if (
|
|
535
|
+
key === "cliproxyapi" &&
|
|
536
|
+
value &&
|
|
537
|
+
typeof value === "object" &&
|
|
538
|
+
"handler" in (value as Record<string, unknown>)
|
|
539
|
+
) {
|
|
540
|
+
commandMap = this as Map<string, unknown>;
|
|
541
|
+
}
|
|
542
|
+
return originalSet.call(this, key, value);
|
|
543
|
+
} as typeof Map.prototype.set;
|
|
544
|
+
try {
|
|
545
|
+
registerCommand();
|
|
546
|
+
} finally {
|
|
547
|
+
Map.prototype.set = originalSet;
|
|
548
|
+
}
|
|
549
|
+
if (!commandMap) {
|
|
550
|
+
logWarn("could not capture command map; /cliproxyapi cannot be hidden after login");
|
|
551
|
+
}
|
|
552
|
+
} else {
|
|
553
|
+
registerCommand();
|
|
554
|
+
}
|
|
555
|
+
|
|
493
556
|
visible = true;
|
|
494
|
-
logInfo("setup command
|
|
557
|
+
logInfo("setup command shown: /cliproxyapi");
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
setupCommands.hide = (): void => {
|
|
561
|
+
if (!visible) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (!commandMap) {
|
|
565
|
+
logWarn("setup command remains visible (no command map capture for unregister)");
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
commandMap.delete("cliproxyapi");
|
|
569
|
+
visible = false;
|
|
570
|
+
logInfo("setup command hidden (CLIProxyAPI already authenticated via /login)");
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
setupCommands.isVisible = (): boolean => visible;
|
|
574
|
+
|
|
575
|
+
setupCommands.hasLoginCredential = (): boolean => hasLoginCredential(agentDir, providerId);
|
|
576
|
+
|
|
577
|
+
setupCommands.syncFromAuth = (): void => {
|
|
578
|
+
if (hasLoginCredential(agentDir, providerId)) {
|
|
579
|
+
setupCommands.hide();
|
|
580
|
+
} else {
|
|
581
|
+
setupCommands.show();
|
|
582
|
+
}
|
|
495
583
|
};
|
|
496
584
|
|
|
497
585
|
return setupCommands;
|
|
498
586
|
}
|
|
499
587
|
|
|
588
|
+
/**
|
|
589
|
+
* pi's slash autocomplete snapshots commands at setup time. Deleting from the
|
|
590
|
+
* extension command Map alone does not refresh that snapshot, so also filter
|
|
591
|
+
* `/cliproxyapi` out of live suggestions while a /login credential exists.
|
|
592
|
+
*/
|
|
593
|
+
function installSetupCommandAutocompleteFilter(options: {
|
|
594
|
+
pi: ExtensionAPI;
|
|
595
|
+
agentDir: string;
|
|
596
|
+
providerId: string;
|
|
597
|
+
setupCommands: SetupCommandsController;
|
|
598
|
+
}): void {
|
|
599
|
+
const { pi, agentDir, providerId, setupCommands } = options;
|
|
600
|
+
let installed = false;
|
|
601
|
+
|
|
602
|
+
pi.on("session_start", (_event, ctx) => {
|
|
603
|
+
setupCommands.syncFromAuth();
|
|
604
|
+
|
|
605
|
+
if (installed) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
if (typeof ctx.ui.addAutocompleteProvider !== "function") {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
installed = true;
|
|
612
|
+
|
|
613
|
+
ctx.ui.addAutocompleteProvider((current) => ({
|
|
614
|
+
triggerCharacters: current.triggerCharacters,
|
|
615
|
+
shouldTriggerFileCompletion: current.shouldTriggerFileCompletion?.bind(current),
|
|
616
|
+
applyCompletion: current.applyCompletion.bind(current),
|
|
617
|
+
async getSuggestions(lines, cursorLine, cursorCol, suggestionOptions) {
|
|
618
|
+
// Keep Map visibility in sync when the user opens `/`.
|
|
619
|
+
setupCommands.syncFromAuth();
|
|
620
|
+
|
|
621
|
+
const result = await current.getSuggestions(lines, cursorLine, cursorCol, suggestionOptions);
|
|
622
|
+
if (!result?.items?.length || !hasLoginCredential(agentDir, providerId)) {
|
|
623
|
+
return result;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const items = result.items.filter((item) => item.value !== "cliproxyapi" && item.label !== "cliproxyapi");
|
|
627
|
+
if (items.length === 0) {
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
630
|
+
return { ...result, items };
|
|
631
|
+
},
|
|
632
|
+
}));
|
|
633
|
+
logInfo("installed /cliproxyapi autocomplete filter for logged-in sessions");
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
500
637
|
export function registerFastCommand(options: {
|
|
501
638
|
pi: ExtensionAPI;
|
|
502
639
|
agentDir: string;
|
|
@@ -586,7 +723,26 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
586
723
|
fastMode,
|
|
587
724
|
});
|
|
588
725
|
|
|
589
|
-
|
|
726
|
+
// Reconcile Map visibility + autocomplete filtering when auth changes.
|
|
727
|
+
// pi has no logout extension event and slash autocomplete is snapshotted, so we:
|
|
728
|
+
// 1) delete/re-add the command Map entry
|
|
729
|
+
// 2) filter suggestions live while a /login credential exists
|
|
730
|
+
// 3) re-check on submitted input after /logout
|
|
731
|
+
installSetupCommandAutocompleteFilter({
|
|
732
|
+
pi,
|
|
733
|
+
agentDir,
|
|
734
|
+
providerId: identity.providerId,
|
|
735
|
+
setupCommands,
|
|
736
|
+
});
|
|
737
|
+
pi.on("input", () => {
|
|
738
|
+
setupCommands.syncFromAuth();
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
// Initial visibility: hide when already logged in via /login.
|
|
742
|
+
setupCommands.syncFromAuth();
|
|
743
|
+
if (setupCommands.hasLoginCredential()) {
|
|
744
|
+
logInfo("CLIProxyAPI already authenticated via /login; /cliproxyapi stays hidden");
|
|
745
|
+
}
|
|
590
746
|
|
|
591
747
|
// Always register oauth so the provider is visible in /login immediately after install.
|
|
592
748
|
registerProvider(pi, {
|
|
@@ -602,7 +758,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
602
758
|
|
|
603
759
|
const connection = resolveConnection(agentDir, identity.providerId);
|
|
604
760
|
if (!connection) {
|
|
605
|
-
setupCommands.
|
|
761
|
+
setupCommands.syncFromAuth();
|
|
606
762
|
logInfo(
|
|
607
763
|
`not configured yet. Prefer /cliproxyapi or /login ${identity.providerName}. ` +
|
|
608
764
|
`Menu path: /login → Sign in with an account → ${identity.providerName}. ` +
|
|
@@ -630,17 +786,26 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
630
786
|
setupCommands,
|
|
631
787
|
fastMode,
|
|
632
788
|
});
|
|
789
|
+
// Keep /cliproxyapi hidden when authenticated via /login; show for config-only setups.
|
|
790
|
+
setupCommands.syncFromAuth();
|
|
633
791
|
} catch (error) {
|
|
634
792
|
const message = error instanceof Error ? error.message : String(error);
|
|
635
793
|
if (isUnauthorizedModelsError(error)) {
|
|
636
|
-
// 401 →
|
|
637
|
-
setupCommands.
|
|
638
|
-
logWarn(
|
|
794
|
+
// 401 → prefer /login when auth.json still has a credential; otherwise show setup.
|
|
795
|
+
setupCommands.syncFromAuth();
|
|
796
|
+
logWarn(
|
|
797
|
+
`models request unauthorized (${message}). ` +
|
|
798
|
+
(setupCommands.isVisible()
|
|
799
|
+
? "Setup command available: /cliproxyapi."
|
|
800
|
+
: `Use /login ${identity.providerName} or /logout then /cliproxyapi.`),
|
|
801
|
+
);
|
|
639
802
|
} else {
|
|
640
|
-
// Other failures (network, etc.): still offer setup
|
|
641
|
-
setupCommands.
|
|
803
|
+
// Other failures (network, etc.): still offer setup when not logged in via /login.
|
|
804
|
+
setupCommands.syncFromAuth();
|
|
642
805
|
logWarn(
|
|
643
|
-
`failed to load models (${message}). Provider remains available via
|
|
806
|
+
`failed to load models (${message}). Provider remains available via ` +
|
|
807
|
+
(setupCommands.isVisible() ? "/cliproxyapi or " : "") +
|
|
808
|
+
`/login for reconfiguration.`,
|
|
644
809
|
);
|
|
645
810
|
}
|
|
646
811
|
}
|
package/package.json
CHANGED