@router-for-me/pi-cliproxyapi-provider 1.4.2 → 1.4.3

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 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 remains available so the connection can be reconfigured at any time. If the provider currently has credentials saved by `/login`, use `/login CLIProxyAPI` to replace them, or run `/logout` before using a dedicated command. This avoids reporting a successful reconfiguration while pi is still prioritizing the stored login credential.
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 hidden from the `/` menu; 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.
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 or setup: `/cliproxyapi` remains available for reconfiguration.
193
- - If an `auth.json` credential exists, the dedicated command asks you to use `/login CLIProxyAPI` or run `/logout` first so the old credential cannot override the new key.
194
- - The built-in `/logout` command removes only the matching `auth.json` credential; environment variables and `cliproxyapi.json` are unchanged.
195
- - If a models request returns **HTTP 401**, the setup command remains available for reconfiguration.
196
- - If CPA is unreachable during startup (non-401), a warning is logged; the setup command remains available via `/cliproxyapi` or `/login`.
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; the setup command remains available
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.
@@ -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,13 @@ import {
48
48
  saveConfigFile,
49
49
  } from "./lib.ts";
50
50
 
51
- /** Registers /cliproxyapi in the slash-command menu. */
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;
54
58
  }
55
59
 
56
60
  class ConfigPersistenceError extends Error {
@@ -233,6 +237,8 @@ function createOAuthHandlers(options: {
233
237
  fastMode,
234
238
  });
235
239
 
240
+ // /login succeeded — dedicated setup is unnecessary until logout.
241
+ setupCommands.hide();
236
242
  logInfo(`login ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
237
243
  return buildOAuthCredentials(baseUrlInput, apiKey);
238
244
  } catch (error) {
@@ -340,7 +346,12 @@ function registerProvider(
340
346
  });
341
347
  }
342
348
 
343
- /** Register the always-available /cliproxyapi setup command. */
349
+ /**
350
+ * Register /cliproxyapi and allow hiding it after /login.
351
+ *
352
+ * pi has registerCommand but no unregisterCommand, so hide() removes the entry
353
+ * from the extension's internal commands Map captured during first registration.
354
+ */
344
355
  function createSetupCommandsController(options: {
345
356
  pi: ExtensionAPI;
346
357
  agentDir: string;
@@ -352,14 +363,25 @@ function createSetupCommandsController(options: {
352
363
  }): SetupCommandsController {
353
364
  const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, fastMode } = options;
354
365
 
355
- // Self-referential controller so validation failures can ensure commands are registered.
366
+ // Self-referential controller so handlers can hide/show after validation.
356
367
  const setupCommands: SetupCommandsController = {
357
368
  show() {
358
369
  /* replaced below */
359
370
  },
371
+ hide() {
372
+ /* replaced below */
373
+ },
374
+ isVisible() {
375
+ return false;
376
+ },
377
+ syncFromAuth() {
378
+ /* replaced below */
379
+ },
360
380
  };
361
381
 
362
382
  let visible = false;
383
+ // Captured extension.commands Map (pi has no public unregisterCommand API).
384
+ let commandMap: Map<string, unknown> | null = null;
363
385
 
364
386
  const handler = async (_args: string, ctx: ExtensionCommandContext): Promise<void> => {
365
387
  if (!ctx.hasUI) {
@@ -371,6 +393,8 @@ function createSetupCommandsController(options: {
371
393
  }
372
394
 
373
395
  if (loadAuthConnection(agentDir, providerId)?.apiKey) {
396
+ // Still authenticated via /login — hide the dedicated command and redirect.
397
+ setupCommands.hide();
374
398
  ctx.ui.notify(
375
399
  `CLIProxyAPI credentials are already stored by /login. Use /login ${providerName} to replace them, or run /logout before /cliproxyapi.`,
376
400
  "error",
@@ -489,9 +513,58 @@ function createSetupCommandsController(options: {
489
513
  if (visible) {
490
514
  return;
491
515
  }
492
- registerCommand();
516
+
517
+ if (!commandMap) {
518
+ // Capture the extension.commands Map during first registration.
519
+ const originalSet = Map.prototype.set;
520
+ Map.prototype.set = function setWithCapture(this: Map<unknown, unknown>, key: unknown, value: unknown) {
521
+ if (
522
+ key === "cliproxyapi" &&
523
+ value &&
524
+ typeof value === "object" &&
525
+ "handler" in (value as Record<string, unknown>)
526
+ ) {
527
+ commandMap = this as Map<string, unknown>;
528
+ }
529
+ return originalSet.call(this, key, value);
530
+ } as typeof Map.prototype.set;
531
+ try {
532
+ registerCommand();
533
+ } finally {
534
+ Map.prototype.set = originalSet;
535
+ }
536
+ if (!commandMap) {
537
+ logWarn("could not capture command map; /cliproxyapi cannot be hidden after login");
538
+ }
539
+ } else {
540
+ registerCommand();
541
+ }
542
+
493
543
  visible = true;
494
- logInfo("setup command registered: /cliproxyapi");
544
+ logInfo("setup command shown: /cliproxyapi");
545
+ };
546
+
547
+ setupCommands.hide = (): void => {
548
+ if (!visible) {
549
+ return;
550
+ }
551
+ if (!commandMap) {
552
+ logWarn("setup command remains visible (no command map capture for unregister)");
553
+ return;
554
+ }
555
+ commandMap.delete("cliproxyapi");
556
+ visible = false;
557
+ logInfo("setup command hidden (CLIProxyAPI already authenticated via /login)");
558
+ };
559
+
560
+ setupCommands.isVisible = (): boolean => visible;
561
+
562
+ setupCommands.syncFromAuth = (): void => {
563
+ if (loadAuthConnection(agentDir, providerId)?.apiKey) {
564
+ setupCommands.hide();
565
+ } else {
566
+ setupCommands.show();
567
+ }
495
568
  };
496
569
 
497
570
  return setupCommands;
@@ -586,7 +659,17 @@ export default async function (pi: ExtensionAPI): Promise<void> {
586
659
  fastMode,
587
660
  });
588
661
 
589
- setupCommands.show();
662
+ // Reconcile visibility after /logout (auth.json removed) or when credentials reappear.
663
+ // pi has no logout extension event, so re-check on session lifecycle and submitted input.
664
+ pi.on("session_start", () => {
665
+ setupCommands.syncFromAuth();
666
+ });
667
+ pi.on("input", () => {
668
+ setupCommands.syncFromAuth();
669
+ });
670
+
671
+ // Initial visibility: hide when already logged in via /login.
672
+ setupCommands.syncFromAuth();
590
673
 
591
674
  // Always register oauth so the provider is visible in /login immediately after install.
592
675
  registerProvider(pi, {
@@ -602,7 +685,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
602
685
 
603
686
  const connection = resolveConnection(agentDir, identity.providerId);
604
687
  if (!connection) {
605
- setupCommands.show();
688
+ setupCommands.syncFromAuth();
606
689
  logInfo(
607
690
  `not configured yet. Prefer /cliproxyapi or /login ${identity.providerName}. ` +
608
691
  `Menu path: /login → Sign in with an account → ${identity.providerName}. ` +
@@ -630,17 +713,26 @@ export default async function (pi: ExtensionAPI): Promise<void> {
630
713
  setupCommands,
631
714
  fastMode,
632
715
  });
716
+ // Keep /cliproxyapi hidden when authenticated via /login; show for config-only setups.
717
+ setupCommands.syncFromAuth();
633
718
  } catch (error) {
634
719
  const message = error instanceof Error ? error.message : String(error);
635
720
  if (isUnauthorizedModelsError(error)) {
636
- // 401 → credentials rejected; keep the setup command available for reconfiguration.
637
- setupCommands.show();
638
- logWarn(`models request unauthorized (${message}). Setup command remains available: /cliproxyapi.`);
721
+ // 401 → prefer /login when auth.json still has a credential; otherwise show setup.
722
+ setupCommands.syncFromAuth();
723
+ logWarn(
724
+ `models request unauthorized (${message}). ` +
725
+ (setupCommands.isVisible()
726
+ ? "Setup command available: /cliproxyapi."
727
+ : `Use /login ${identity.providerName} or /logout then /cliproxyapi.`),
728
+ );
639
729
  } else {
640
- // Other failures (network, etc.): still offer setup so the user can fix baseUrl/key.
641
- setupCommands.show();
730
+ // Other failures (network, etc.): still offer setup when not logged in via /login.
731
+ setupCommands.syncFromAuth();
642
732
  logWarn(
643
- `failed to load models (${message}). Provider remains available via /cliproxyapi or /login for reconfiguration.`,
733
+ `failed to load models (${message}). Provider remains available via ` +
734
+ (setupCommands.isVisible() ? "/cliproxyapi or " : "") +
735
+ `/login for reconfiguration.`,
644
736
  );
645
737
  }
646
738
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@router-for-me/pi-cliproxyapi-provider",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
5
5
  "type": "module",
6
6
  "keywords": [