@router-for-me/pi-cliproxyapi-provider 1.4.4 → 1.4.6
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 +14 -25
- package/extensions/index.ts +23 -404
- package/extensions/lib.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@ Pi provider extension that discovers models from [CLIProxyAPI](https://github.co
|
|
|
4
4
|
|
|
5
5
|
## What it does
|
|
6
6
|
|
|
7
|
-
1. Registers a provider that always appears in `/login` (account sign-in path)
|
|
8
|
-
2. Interactive setup collects `baseUrl` + `apiKey`.
|
|
7
|
+
1. Registers a provider that always appears in `/login` (account sign-in path).
|
|
8
|
+
2. Interactive setup collects `baseUrl` + `apiKey` via `/login CLIProxyAPI` or `/login cliproxyapi`.
|
|
9
9
|
3. Fetches `{root}/v1/models?client_version=pi`.
|
|
10
10
|
4. Maps the CLIProxyAPI catalog into pi models, including Fast service-tier capability.
|
|
11
11
|
5. Registers inference against `{root}/backend-api/`.
|
|
@@ -29,15 +29,7 @@ pi -e /absolute/path/to/pi-cliproxyapi-provider
|
|
|
29
29
|
|
|
30
30
|
This plugin needs both **baseUrl** and **apiKey**. pi's built-in `/login` only supports multi-field prompts on the account/OAuth path, so CLIProxyAPI appears under **Sign in with an account** (not API key).
|
|
31
31
|
|
|
32
|
-
### Preferred:
|
|
33
|
-
|
|
34
|
-
```text
|
|
35
|
-
/cliproxyapi
|
|
36
|
-
```
|
|
37
|
-
|
|
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
|
-
|
|
40
|
-
### Or use /login shortcuts (skip the menu)
|
|
32
|
+
### Preferred: /login shortcuts
|
|
41
33
|
|
|
42
34
|
```text
|
|
43
35
|
/login CLIProxyAPI
|
|
@@ -71,13 +63,13 @@ Final login validation calls `{root}/v1/models?client_version=pi`:
|
|
|
71
63
|
- **HTTP 200** → login succeeds (empty model list is still OK)
|
|
72
64
|
- **non-200 / network error** → login fails and you are prompted to re-enter base URL + API key
|
|
73
65
|
|
|
74
|
-
On success
|
|
66
|
+
On success:
|
|
75
67
|
|
|
76
68
|
- models are registered immediately in the current session (0 models is allowed)
|
|
77
69
|
- `baseUrl` / `apiKey` are written to `~/.pi/agent/cliproxyapi.json`
|
|
78
|
-
-
|
|
70
|
+
- pi also stores the returned credential in `~/.pi/agent/auth.json`
|
|
79
71
|
|
|
80
|
-
Re-run `/
|
|
72
|
+
Re-run `/login CLIProxyAPI` or `/login cliproxyapi` anytime to reconfigure. The built-in `/logout` command only removes credentials saved in `auth.json`; it does not erase `cliproxyapi.json`. Remove or update that file if you also need to clear the provider configuration.
|
|
81
73
|
|
|
82
74
|
## Non-interactive configuration
|
|
83
75
|
|
|
@@ -172,7 +164,7 @@ Unsupported pi thinking levels are set to `null` so they are hidden in the UI. C
|
|
|
172
164
|
|
|
173
165
|
If you previously maintained a static provider such as `cpa-responses` in `~/.pi/agent/models.json`:
|
|
174
166
|
|
|
175
|
-
1. Install this package and run `/
|
|
167
|
+
1. Install this package and run `/login CLIProxyAPI` or `/login cliproxyapi` (or set `cliproxyapi.json`).
|
|
176
168
|
2. Point `defaultProvider` / `enabledModels` at `cliproxyapi/<model-id>` (or set `providerId` to `cpa-responses` for a drop-in id).
|
|
177
169
|
3. Remove the hand-maintained models array once the dynamic list looks correct.
|
|
178
170
|
|
|
@@ -188,15 +180,12 @@ Disable just this helper via `pi config` if you only want the CLIProxyAPI provid
|
|
|
188
180
|
|
|
189
181
|
## Failure behavior
|
|
190
182
|
|
|
191
|
-
- Before setup / without credentials: provider still appears in `/login`;
|
|
192
|
-
- After successful `/login`:
|
|
193
|
-
-
|
|
194
|
-
- If
|
|
195
|
-
-
|
|
196
|
-
-
|
|
197
|
-
- If CPA is unreachable during startup (non-401), a warning is logged; setup stays available via `/cliproxyapi` (when not logged in) or `/login`.
|
|
198
|
-
- Login/setup final step validates credentials by requesting models:
|
|
199
|
-
- HTTP 200 (including empty catalog) → credentials are persisted; `/login` hides `/cliproxyapi`
|
|
183
|
+
- Before setup / without credentials: provider still appears in `/login`; no models are listed yet.
|
|
184
|
+
- After successful `/login`: models are registered; credentials are stored in `auth.json` and mirrored to `cliproxyapi.json`.
|
|
185
|
+
- The built-in `/logout` command removes only the matching `auth.json` credential; environment variables and `cliproxyapi.json` are unchanged.
|
|
186
|
+
- If a models request returns **HTTP 401** or CPA is unreachable during startup, a warning is logged; reconfigure via `/login CLIProxyAPI` or fix config/env.
|
|
187
|
+
- Login final step validates credentials by requesting models:
|
|
188
|
+
- HTTP 200 (including empty catalog) → credentials are persisted
|
|
200
189
|
- non-200 / network / invalid baseUrl → nothing is persisted; re-enter baseUrl + API key
|
|
201
|
-
- If CPA returns HTTP 200 with zero usable models: login still succeeds; re-run
|
|
190
|
+
- If CPA returns HTTP 200 with zero usable models: login still succeeds; re-run `/login CLIProxyAPI` later after models become available.
|
|
202
191
|
- 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
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* CLIProxyAPI dynamic model provider for pi.
|
|
3
3
|
*
|
|
4
|
-
* Supports login-style setup via `/login
|
|
4
|
+
* Supports login-style setup via `/login`:
|
|
5
5
|
* 1. Provider is registered as OAuth-only so `/login CLIProxyAPI` / `/login cliproxyapi`
|
|
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
|
-
* 2. Preferred shortcuts: `/login CLIProxyAPI
|
|
9
|
-
* or the dedicated `/cliproxyapi` command (hidden after successful /login).
|
|
8
|
+
* 2. Preferred shortcuts: `/login CLIProxyAPI` or `/login cliproxyapi`.
|
|
10
9
|
* 3. Setup prompts for baseUrl + apiKey.
|
|
11
10
|
* 4. Final login step validates credentials via /v1/models?client_version=pi
|
|
12
11
|
* (HTTP 200 = success even if the catalog is empty; otherwise re-prompt).
|
|
@@ -20,12 +19,7 @@
|
|
|
20
19
|
*/
|
|
21
20
|
|
|
22
21
|
import type { Api, Model, OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
|
23
|
-
import {
|
|
24
|
-
type ExtensionAPI,
|
|
25
|
-
type ExtensionCommandContext,
|
|
26
|
-
type ExtensionContext,
|
|
27
|
-
getAgentDir,
|
|
28
|
-
} from "@earendil-works/pi-coding-agent";
|
|
22
|
+
import { type ExtensionAPI, type ExtensionContext, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
29
23
|
import { CLIPROXYAPI_CODEX_API, type CliproxyCodexStreamSimple, loadCliproxyCodexStreams } from "./codex-stream.ts";
|
|
30
24
|
import { FastModeController } from "./fast.ts";
|
|
31
25
|
import { FastFooterController } from "./fast-footer.ts";
|
|
@@ -48,25 +42,6 @@ import {
|
|
|
48
42
|
saveConfigFile,
|
|
49
43
|
} from "./lib.ts";
|
|
50
44
|
|
|
51
|
-
/** Show/hide /cliproxyapi in the slash-command menu. */
|
|
52
|
-
interface SetupCommandsController {
|
|
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
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
45
|
class ConfigPersistenceError extends Error {
|
|
71
46
|
constructor(cause: unknown) {
|
|
72
47
|
const message = cause instanceof Error ? cause.message : String(cause);
|
|
@@ -83,6 +58,14 @@ function logInfo(message: string): void {
|
|
|
83
58
|
console.info(`[pi-cliproxyapi-provider] ${message}`);
|
|
84
59
|
}
|
|
85
60
|
|
|
61
|
+
function hasLoginCredential(agentDir: string, providerId: string): boolean {
|
|
62
|
+
try {
|
|
63
|
+
return Boolean(loadAuthConnection(agentDir, providerId)?.apiKey);
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
86
69
|
function buildOAuthCredentials(baseUrlInput: string, apiKey: string): OAuthCredentials {
|
|
87
70
|
return {
|
|
88
71
|
refresh: encodeRefreshMeta(baseUrlInput),
|
|
@@ -153,29 +136,10 @@ async function configureAndRegister(options: {
|
|
|
153
136
|
apiKey: string;
|
|
154
137
|
defaultBaseUrl: string;
|
|
155
138
|
streamSimple: CliproxyCodexStreamSimple;
|
|
156
|
-
setupCommands: SetupCommandsController;
|
|
157
139
|
fastMode: FastModeController;
|
|
158
|
-
/**
|
|
159
|
-
* When true, register a configured API key for ambient request auth.
|
|
160
|
-
* Only used by `/cliproxyapi` (no auth.json write). `/login` must leave this
|
|
161
|
-
* false so the provider stays OAuth-only and `/login <provider>` skips the
|
|
162
|
-
* API-key vs account selector.
|
|
163
|
-
*/
|
|
164
|
-
registerConfiguredApiKey?: boolean;
|
|
165
140
|
}): Promise<{ modelCount: number; modelsUrl: string }> {
|
|
166
|
-
const {
|
|
167
|
-
|
|
168
|
-
agentDir,
|
|
169
|
-
providerId,
|
|
170
|
-
providerName,
|
|
171
|
-
baseUrlInput,
|
|
172
|
-
apiKey,
|
|
173
|
-
defaultBaseUrl,
|
|
174
|
-
streamSimple,
|
|
175
|
-
setupCommands,
|
|
176
|
-
fastMode,
|
|
177
|
-
registerConfiguredApiKey = false,
|
|
178
|
-
} = options;
|
|
141
|
+
const { pi, agentDir, providerId, providerName, baseUrlInput, apiKey, defaultBaseUrl, streamSimple, fastMode } =
|
|
142
|
+
options;
|
|
179
143
|
|
|
180
144
|
const loaded = await loadMappedModels(baseUrlInput, apiKey);
|
|
181
145
|
|
|
@@ -190,16 +154,16 @@ async function configureAndRegister(options: {
|
|
|
190
154
|
throw new ConfigPersistenceError(error);
|
|
191
155
|
}
|
|
192
156
|
|
|
157
|
+
// /login stores oauth credentials itself; keep the provider OAuth-only so
|
|
158
|
+
// `/login <provider>` skips the API-key vs account selector.
|
|
193
159
|
registerProvider(pi, {
|
|
194
160
|
providerId,
|
|
195
161
|
providerName,
|
|
196
162
|
baseUrlInput,
|
|
197
|
-
apiKey: registerConfiguredApiKey ? apiKey : undefined,
|
|
198
163
|
models: loaded.models,
|
|
199
164
|
defaultBaseUrl: baseUrlInput || defaultBaseUrl,
|
|
200
165
|
agentDir,
|
|
201
166
|
streamSimple,
|
|
202
|
-
setupCommands,
|
|
203
167
|
fastMode,
|
|
204
168
|
});
|
|
205
169
|
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
@@ -214,10 +178,9 @@ function createOAuthHandlers(options: {
|
|
|
214
178
|
providerName: string;
|
|
215
179
|
defaultBaseUrl: string;
|
|
216
180
|
streamSimple: CliproxyCodexStreamSimple;
|
|
217
|
-
setupCommands: SetupCommandsController;
|
|
218
181
|
fastMode: FastModeController;
|
|
219
182
|
}) {
|
|
220
|
-
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple,
|
|
183
|
+
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, fastMode } = options;
|
|
221
184
|
|
|
222
185
|
return {
|
|
223
186
|
name: providerName,
|
|
@@ -243,12 +206,9 @@ function createOAuthHandlers(options: {
|
|
|
243
206
|
apiKey,
|
|
244
207
|
defaultBaseUrl,
|
|
245
208
|
streamSimple,
|
|
246
|
-
setupCommands,
|
|
247
209
|
fastMode,
|
|
248
210
|
});
|
|
249
211
|
|
|
250
|
-
// /login succeeded — dedicated setup is unnecessary until logout.
|
|
251
|
-
setupCommands.hide();
|
|
252
212
|
logInfo(`login ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
|
|
253
213
|
return buildOAuthCredentials(baseUrlInput, apiKey);
|
|
254
214
|
} catch (error) {
|
|
@@ -258,10 +218,6 @@ function createOAuthHandlers(options: {
|
|
|
258
218
|
callbacks.onProgress?.(message);
|
|
259
219
|
throw error;
|
|
260
220
|
}
|
|
261
|
-
// Auth failures should keep the setup command available for reconfiguration.
|
|
262
|
-
if (isUnauthorizedModelsError(error)) {
|
|
263
|
-
setupCommands.show();
|
|
264
|
-
}
|
|
265
221
|
callbacks.onProgress?.(`Login validation failed: ${message}\nPlease re-enter base URL and API key.`);
|
|
266
222
|
// Keep last baseUrl as the next default so retyping is easier.
|
|
267
223
|
promptDefaultBaseUrl = baseUrlInput || promptDefaultBaseUrl;
|
|
@@ -309,22 +265,11 @@ function registerProvider(
|
|
|
309
265
|
defaultBaseUrl: string;
|
|
310
266
|
agentDir: string;
|
|
311
267
|
streamSimple: CliproxyCodexStreamSimple;
|
|
312
|
-
setupCommands: SetupCommandsController;
|
|
313
268
|
fastMode: FastModeController;
|
|
314
269
|
},
|
|
315
270
|
): void {
|
|
316
|
-
const {
|
|
317
|
-
|
|
318
|
-
providerName,
|
|
319
|
-
baseUrlInput,
|
|
320
|
-
apiKey,
|
|
321
|
-
models,
|
|
322
|
-
defaultBaseUrl,
|
|
323
|
-
agentDir,
|
|
324
|
-
streamSimple,
|
|
325
|
-
setupCommands,
|
|
326
|
-
fastMode,
|
|
327
|
-
} = options;
|
|
271
|
+
const { providerId, providerName, baseUrlInput, apiKey, models, defaultBaseUrl, agentDir, streamSimple, fastMode } =
|
|
272
|
+
options;
|
|
328
273
|
|
|
329
274
|
const endpoints = resolveEndpoints(baseUrlInput);
|
|
330
275
|
const oauth = createOAuthHandlers({
|
|
@@ -334,7 +279,6 @@ function registerProvider(
|
|
|
334
279
|
providerName,
|
|
335
280
|
defaultBaseUrl,
|
|
336
281
|
streamSimple,
|
|
337
|
-
setupCommands,
|
|
338
282
|
fastMode,
|
|
339
283
|
});
|
|
340
284
|
|
|
@@ -349,291 +293,13 @@ function registerProvider(
|
|
|
349
293
|
streamSimple,
|
|
350
294
|
// OAuth-only keeps `/login <provider>` on the multi-field account path.
|
|
351
295
|
// Pass apiKey only for ambient request auth when no /login credential exists
|
|
352
|
-
// (config file / env
|
|
296
|
+
// (config file / env). Never pass both for /login flows.
|
|
353
297
|
oauth,
|
|
354
298
|
...(apiKey ? { apiKey } : {}),
|
|
355
299
|
...(models && models.length > 0 ? { models } : {}),
|
|
356
300
|
});
|
|
357
301
|
}
|
|
358
302
|
|
|
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
|
-
*/
|
|
365
|
-
function createSetupCommandsController(options: {
|
|
366
|
-
pi: ExtensionAPI;
|
|
367
|
-
agentDir: string;
|
|
368
|
-
providerId: string;
|
|
369
|
-
providerName: string;
|
|
370
|
-
defaultBaseUrl: string;
|
|
371
|
-
streamSimple: CliproxyCodexStreamSimple;
|
|
372
|
-
fastMode: FastModeController;
|
|
373
|
-
}): SetupCommandsController {
|
|
374
|
-
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, fastMode } = options;
|
|
375
|
-
|
|
376
|
-
// Self-referential controller so handlers can hide/show after validation.
|
|
377
|
-
const setupCommands: SetupCommandsController = {
|
|
378
|
-
show() {
|
|
379
|
-
/* replaced below */
|
|
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
|
-
},
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
let visible = false;
|
|
396
|
-
// Captured extension.commands Map (pi has no public unregisterCommand API).
|
|
397
|
-
let commandMap: Map<string, unknown> | null = null;
|
|
398
|
-
|
|
399
|
-
const handler = async (_args: string, ctx: ExtensionCommandContext): Promise<void> => {
|
|
400
|
-
if (!ctx.hasUI) {
|
|
401
|
-
ctx.ui.notify(
|
|
402
|
-
`Interactive UI unavailable. Use /login ${providerName}, set ${CONFIG_FILE_NAME}, or CLIPROXYAPI_* env vars.`,
|
|
403
|
-
"error",
|
|
404
|
-
);
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
if (hasLoginCredential(agentDir, providerId)) {
|
|
409
|
-
// Still authenticated via /login — hide the dedicated command and redirect.
|
|
410
|
-
setupCommands.hide();
|
|
411
|
-
ctx.ui.notify(
|
|
412
|
-
`CLIProxyAPI credentials are already stored by /login. Use /login ${providerName} to replace them, or run /logout before /cliproxyapi.`,
|
|
413
|
-
"error",
|
|
414
|
-
);
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const promptDefaultBaseUrl = resolveDefaultBaseUrl(agentDir, providerId) || defaultBaseUrl;
|
|
419
|
-
|
|
420
|
-
const baseUrlRaw = await ctx.ui.input(`CLIProxyAPI base URL [${promptDefaultBaseUrl}]:`, promptDefaultBaseUrl);
|
|
421
|
-
if (baseUrlRaw === undefined) {
|
|
422
|
-
ctx.ui.notify("CLIProxyAPI setup cancelled.", "info");
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
const baseUrlInput = firstNonEmpty(baseUrlRaw, promptDefaultBaseUrl)!;
|
|
426
|
-
|
|
427
|
-
try {
|
|
428
|
-
resolveEndpoints(baseUrlInput);
|
|
429
|
-
} catch (error) {
|
|
430
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
431
|
-
ctx.ui.notify(`Invalid base URL: ${message}`, "error");
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const apiKeyRaw = await ctx.ui.input("CLIProxyAPI API key:", "sk-...");
|
|
436
|
-
if (apiKeyRaw === undefined) {
|
|
437
|
-
ctx.ui.notify("CLIProxyAPI setup cancelled.", "info");
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
const apiKey = apiKeyRaw.trim();
|
|
441
|
-
if (!apiKey) {
|
|
442
|
-
ctx.ui.notify("API key cannot be empty.", "error");
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Final setup step: validate by calling /v1/models.
|
|
447
|
-
// HTTP 200 (even with an empty catalog) means success; otherwise re-prompt.
|
|
448
|
-
let currentBaseUrl = baseUrlInput;
|
|
449
|
-
let currentApiKey = apiKey;
|
|
450
|
-
while (true) {
|
|
451
|
-
try {
|
|
452
|
-
const result = await configureAndRegister({
|
|
453
|
-
pi,
|
|
454
|
-
agentDir,
|
|
455
|
-
providerId,
|
|
456
|
-
providerName,
|
|
457
|
-
baseUrlInput: currentBaseUrl,
|
|
458
|
-
apiKey: currentApiKey,
|
|
459
|
-
defaultBaseUrl,
|
|
460
|
-
streamSimple,
|
|
461
|
-
setupCommands,
|
|
462
|
-
fastMode,
|
|
463
|
-
// Dedicated command does not write auth.json; keep ambient apiKey
|
|
464
|
-
// so requests work in the current session.
|
|
465
|
-
registerConfiguredApiKey: true,
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
logInfo(`command setup ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
|
|
469
|
-
ctx.ui.notify(`CLIProxyAPI configured: ${result.modelCount} models from ${result.modelsUrl}`, "info");
|
|
470
|
-
return;
|
|
471
|
-
} catch (error) {
|
|
472
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
473
|
-
logWarn(`command setup failed: ${message}`);
|
|
474
|
-
if (error instanceof ConfigPersistenceError) {
|
|
475
|
-
ctx.ui.notify(message, "error");
|
|
476
|
-
return;
|
|
477
|
-
}
|
|
478
|
-
if (isUnauthorizedModelsError(error)) {
|
|
479
|
-
setupCommands.show();
|
|
480
|
-
}
|
|
481
|
-
ctx.ui.notify(`CLIProxyAPI validation failed: ${message}. Please re-enter base URL and API key.`, "error");
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// Collect replacement credentials before retrying validation.
|
|
485
|
-
while (true) {
|
|
486
|
-
const retryBaseUrlRaw = await ctx.ui.input(`CLIProxyAPI base URL [${currentBaseUrl}]:`, currentBaseUrl);
|
|
487
|
-
if (retryBaseUrlRaw === undefined) {
|
|
488
|
-
ctx.ui.notify("CLIProxyAPI setup cancelled.", "info");
|
|
489
|
-
return;
|
|
490
|
-
}
|
|
491
|
-
const retryBaseUrl = firstNonEmpty(retryBaseUrlRaw, currentBaseUrl)!;
|
|
492
|
-
try {
|
|
493
|
-
resolveEndpoints(retryBaseUrl);
|
|
494
|
-
} catch (urlError) {
|
|
495
|
-
const urlMessage = urlError instanceof Error ? urlError.message : String(urlError);
|
|
496
|
-
ctx.ui.notify(`Invalid base URL: ${urlMessage}`, "error");
|
|
497
|
-
continue;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const retryApiKeyRaw = await ctx.ui.input("CLIProxyAPI API key:", "sk-...");
|
|
501
|
-
if (retryApiKeyRaw === undefined) {
|
|
502
|
-
ctx.ui.notify("CLIProxyAPI setup cancelled.", "info");
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
const retryApiKey = retryApiKeyRaw.trim();
|
|
506
|
-
if (!retryApiKey) {
|
|
507
|
-
ctx.ui.notify("API key cannot be empty.", "error");
|
|
508
|
-
continue;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
currentBaseUrl = retryBaseUrl;
|
|
512
|
-
currentApiKey = retryApiKey;
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const registerCommand = (): void => {
|
|
519
|
-
pi.registerCommand("cliproxyapi", {
|
|
520
|
-
description: "Configure CLIProxyAPI (baseUrl + API key) and load models. Prefer this or /login CLIProxyAPI.",
|
|
521
|
-
handler,
|
|
522
|
-
});
|
|
523
|
-
};
|
|
524
|
-
|
|
525
|
-
setupCommands.show = (): void => {
|
|
526
|
-
if (visible) {
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
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
|
-
|
|
556
|
-
visible = true;
|
|
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
|
-
}
|
|
583
|
-
};
|
|
584
|
-
|
|
585
|
-
return setupCommands;
|
|
586
|
-
}
|
|
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
|
-
|
|
637
303
|
export function registerFastCommand(options: {
|
|
638
304
|
pi: ExtensionAPI;
|
|
639
305
|
agentDir: string;
|
|
@@ -713,37 +379,6 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
713
379
|
});
|
|
714
380
|
fastFooter.register(pi);
|
|
715
381
|
|
|
716
|
-
const setupCommands = createSetupCommandsController({
|
|
717
|
-
pi,
|
|
718
|
-
agentDir,
|
|
719
|
-
providerId: identity.providerId,
|
|
720
|
-
providerName: identity.providerName,
|
|
721
|
-
defaultBaseUrl,
|
|
722
|
-
streamSimple,
|
|
723
|
-
fastMode,
|
|
724
|
-
});
|
|
725
|
-
|
|
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
|
-
}
|
|
746
|
-
|
|
747
382
|
// Always register oauth so the provider is visible in /login immediately after install.
|
|
748
383
|
registerProvider(pi, {
|
|
749
384
|
providerId: identity.providerId,
|
|
@@ -752,15 +387,13 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
752
387
|
defaultBaseUrl,
|
|
753
388
|
agentDir,
|
|
754
389
|
streamSimple,
|
|
755
|
-
setupCommands,
|
|
756
390
|
fastMode,
|
|
757
391
|
});
|
|
758
392
|
|
|
759
393
|
const connection = resolveConnection(agentDir, identity.providerId);
|
|
760
394
|
if (!connection) {
|
|
761
|
-
setupCommands.syncFromAuth();
|
|
762
395
|
logInfo(
|
|
763
|
-
`not configured yet.
|
|
396
|
+
`not configured yet. Use /login ${identity.providerName} or /login ${identity.providerId}. ` +
|
|
764
397
|
`Menu path: /login → Sign in with an account → ${identity.providerName}. ` +
|
|
765
398
|
`Or set ${CONFIG_FILE_NAME} / CLIPROXYAPI_API_KEY.`,
|
|
766
399
|
);
|
|
@@ -773,7 +406,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
773
406
|
// Prefer OAuth-only registration when /login already stored credentials so
|
|
774
407
|
// `/login <provider>` jumps straight into the multi-field flow. Fall back to
|
|
775
408
|
// ambient apiKey only for config-file / env setups without auth.json.
|
|
776
|
-
const hasStoredLogin =
|
|
409
|
+
const hasStoredLogin = hasLoginCredential(agentDir, identity.providerId);
|
|
777
410
|
registerProvider(pi, {
|
|
778
411
|
providerId: identity.providerId,
|
|
779
412
|
providerName: identity.providerName,
|
|
@@ -783,29 +416,15 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
783
416
|
defaultBaseUrl,
|
|
784
417
|
agentDir,
|
|
785
418
|
streamSimple,
|
|
786
|
-
setupCommands,
|
|
787
419
|
fastMode,
|
|
788
420
|
});
|
|
789
|
-
// Keep /cliproxyapi hidden when authenticated via /login; show for config-only setups.
|
|
790
|
-
setupCommands.syncFromAuth();
|
|
791
421
|
} catch (error) {
|
|
792
422
|
const message = error instanceof Error ? error.message : String(error);
|
|
793
423
|
if (isUnauthorizedModelsError(error)) {
|
|
794
|
-
|
|
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
|
-
);
|
|
424
|
+
logWarn(`models request unauthorized (${message}). Use /login ${identity.providerName} to reconfigure.`);
|
|
802
425
|
} else {
|
|
803
|
-
// Other failures (network, etc.): still offer setup when not logged in via /login.
|
|
804
|
-
setupCommands.syncFromAuth();
|
|
805
426
|
logWarn(
|
|
806
|
-
`failed to load models (${message}).
|
|
807
|
-
(setupCommands.isVisible() ? "/cliproxyapi or " : "") +
|
|
808
|
-
`/login for reconfiguration.`,
|
|
427
|
+
`failed to load models (${message}). Use /login ${identity.providerName} or check ${CONFIG_FILE_NAME} / CLIPROXYAPI_* env vars.`,
|
|
809
428
|
);
|
|
810
429
|
}
|
|
811
430
|
}
|
package/extensions/lib.ts
CHANGED
|
@@ -18,7 +18,7 @@ export const CONFIG_FILE_NAME = "cliproxyapi.json";
|
|
|
18
18
|
export const AUTH_FILE_NAME = "auth.json";
|
|
19
19
|
export const CLIENT_VERSION = "pi";
|
|
20
20
|
|
|
21
|
-
/** Keep login credentials effectively permanent; reconfigure via /
|
|
21
|
+
/** Keep login credentials effectively permanent; reconfigure via /login. */
|
|
22
22
|
export const CREDENTIAL_TTL_MS = 100 * 365 * 24 * 60 * 60 * 1000;
|
|
23
23
|
|
|
24
24
|
export const ZERO_COST = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } as const;
|
|
@@ -387,7 +387,7 @@ export function toPiModel(model: CodexClientModel): PiProviderModel | null {
|
|
|
387
387
|
};
|
|
388
388
|
}
|
|
389
389
|
|
|
390
|
-
/** HTTP error from /v1/models (used to detect 401
|
|
390
|
+
/** HTTP error from /v1/models (used to detect 401). */
|
|
391
391
|
export class ModelsHttpError extends Error {
|
|
392
392
|
readonly status: number;
|
|
393
393
|
readonly statusText: string;
|
package/package.json
CHANGED