@router-for-me/pi-cliproxyapi-provider 1.1.1 → 1.2.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 +27 -4
- package/extensions/codex-stream.ts +49 -2
- package/extensions/fast.ts +63 -0
- package/extensions/index.ts +111 -5
- package/extensions/lib.ts +80 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
# pi-cliproxyapi-provider
|
|
2
2
|
|
|
3
|
-
Pi provider extension that discovers models from [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) and registers them for use in pi. It also ships a small TUI helper that shows elapsed runtime and a TPS summary after each agent turn.
|
|
3
|
+
Pi provider extension that discovers models from [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) and registers them for use in pi. It supports catalog-driven OpenAI Fast mode and also ships a small TUI helper that shows elapsed runtime and a TPS summary after each agent turn.
|
|
4
4
|
|
|
5
5
|
## What it does
|
|
6
6
|
|
|
7
7
|
1. Registers a provider that always appears in `/login` (subscription path) and via `/cliproxyapi` / `/cpa`.
|
|
8
8
|
2. Interactive setup collects `baseUrl` + `apiKey`.
|
|
9
9
|
3. Fetches `{root}/v1/models?client_version=pi`.
|
|
10
|
-
4. Maps the CLIProxyAPI catalog into pi models.
|
|
10
|
+
4. Maps the CLIProxyAPI catalog into pi models, including Fast service-tier capability.
|
|
11
11
|
5. Registers inference against `{root}/backend-api/`.
|
|
12
|
-
6.
|
|
12
|
+
6. Provides `/cpa-fast on|off|status` to request OpenAI priority processing for supported models.
|
|
13
|
+
7. In interactive TUI sessions, shows footer elapsed time during runs and a TPS / token usage toast when the agent settles.
|
|
13
14
|
|
|
14
15
|
## Install
|
|
15
16
|
|
|
@@ -98,7 +99,8 @@ You can still configure without `/login`.
|
|
|
98
99
|
```json
|
|
99
100
|
{
|
|
100
101
|
"baseUrl": "http://127.0.0.1:8317",
|
|
101
|
-
"apiKey": "12345"
|
|
102
|
+
"apiKey": "12345",
|
|
103
|
+
"fast": false
|
|
102
104
|
}
|
|
103
105
|
```
|
|
104
106
|
|
|
@@ -110,6 +112,7 @@ Optional fields:
|
|
|
110
112
|
| `apiKey` | _(required unless set via /login or env)_ | Bearer token / CPA API key |
|
|
111
113
|
| `providerId` | `cliproxyapi` | Provider id shown in `/model` |
|
|
112
114
|
| `providerName` | `CLIProxyAPI` | Display name in `/login` and UI |
|
|
115
|
+
| `fast` | `false` | Default Fast mode for new sessions; only applies to catalog-supported models |
|
|
113
116
|
|
|
114
117
|
### Environment overrides
|
|
115
118
|
|
|
@@ -119,6 +122,7 @@ Optional fields:
|
|
|
119
122
|
| `CLIPROXYAPI_API_KEY` | `apiKey` |
|
|
120
123
|
| `CLIPROXYAPI_PROVIDER_ID` | `providerId` |
|
|
121
124
|
| `CLIPROXYAPI_PROVIDER_NAME` | `providerName` |
|
|
125
|
+
| `CLIPROXYAPI_FAST` | `fast` (`true` / `false`, also accepts `1`, `0`, `yes`, `no`, `on`, `off`) |
|
|
122
126
|
|
|
123
127
|
Resolution order for connection settings:
|
|
124
128
|
|
|
@@ -127,6 +131,8 @@ Resolution order for connection settings:
|
|
|
127
131
|
3. `/login` credentials in `auth.json`
|
|
128
132
|
4. Default baseUrl `http://127.0.0.1:8317`
|
|
129
133
|
|
|
134
|
+
Fast defaults resolve separately as `CLIPROXYAPI_FAST` → `cliproxyapi.json` → `false`.
|
|
135
|
+
|
|
130
136
|
### baseUrl normalization
|
|
131
137
|
|
|
132
138
|
Preferred form is **host:port only**:
|
|
@@ -140,6 +146,22 @@ Preferred form is **host:port only**:
|
|
|
140
146
|
|
|
141
147
|
pi then sends inference traffic to `{inference}/codex/responses`.
|
|
142
148
|
|
|
149
|
+
## Fast mode
|
|
150
|
+
|
|
151
|
+
OpenAI Fast mode requests the priority service tier. It can reduce latency for supported models, but consumes more OpenAI/Codex credits or incurs priority-processing pricing.
|
|
152
|
+
|
|
153
|
+
Fast is **off by default**. Control it for the current pi session with:
|
|
154
|
+
|
|
155
|
+
```text
|
|
156
|
+
/cpa-fast on
|
|
157
|
+
/cpa-fast off
|
|
158
|
+
/cpa-fast status
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
The command is stored with the current pi session and restored when that session is resumed; it does not change the global default. To choose the default for new sessions, set `"fast": true` in `cliproxyapi.json` or set `CLIPROXYAPI_FAST=true`.
|
|
162
|
+
|
|
163
|
+
Fast capability is catalog-driven: the plugin only injects `service_tier: "priority"` when the selected CLIProxyAPI model advertises `priority` in `service_tiers` or `fast` in `additional_speed_tiers`. Unsupported models are left unchanged, and `/cpa-fast status` reports whether Fast is effective for the current model. Fast is independent from pi's reasoning/thinking level.
|
|
164
|
+
|
|
143
165
|
## Model mapping
|
|
144
166
|
|
|
145
167
|
From CPA catalog entry → pi model:
|
|
@@ -184,3 +206,4 @@ Disable just this helper via `pi config` if you only want the CLIProxyAPI provid
|
|
|
184
206
|
- HTTP 200 (including empty catalog) → credentials are persisted; setup commands hidden
|
|
185
207
|
- non-200 / network / invalid baseUrl → nothing is persisted; re-enter baseUrl + API key
|
|
186
208
|
- If CPA returns HTTP 200 with zero usable models: login still succeeds; re-run setup later after models become available.
|
|
209
|
+
- If Fast is enabled for a model that does not advertise Fast capability: the request is left unchanged and `/cpa-fast` reports a warning.
|
|
@@ -33,6 +33,48 @@ export type CliproxyCodexStreams = {
|
|
|
33
33
|
api: typeof CLIPROXYAPI_CODEX_API;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
+
export interface CliproxyCodexStreamOptions {
|
|
37
|
+
shouldUseFast?: (model: Model<Api>) => boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type PayloadHook = NonNullable<SimpleStreamOptions["onPayload"]>;
|
|
41
|
+
|
|
42
|
+
export function withPriorityServiceTier(payload: unknown): unknown {
|
|
43
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
44
|
+
return payload;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
...(payload as Record<string, unknown>),
|
|
48
|
+
service_tier: "priority",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Apply Fast before pi's shared payload hooks so later extensions retain final control. */
|
|
53
|
+
export async function applyFastPayloadHook(
|
|
54
|
+
payload: unknown,
|
|
55
|
+
model: Model<Api>,
|
|
56
|
+
onPayload?: PayloadHook,
|
|
57
|
+
): Promise<unknown> {
|
|
58
|
+
const fastPayload = withPriorityServiceTier(payload);
|
|
59
|
+
const nextPayload = await onPayload?.(fastPayload, model);
|
|
60
|
+
return nextPayload === undefined ? fastPayload : nextPayload;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function wrapStreamSimpleForFast(
|
|
64
|
+
streamSimple: CliproxyCodexStreamSimple,
|
|
65
|
+
shouldUseFast?: (model: Model<Api>) => boolean,
|
|
66
|
+
): CliproxyCodexStreamSimple {
|
|
67
|
+
return (model, context, streamOptions) => {
|
|
68
|
+
if (!shouldUseFast?.(model)) {
|
|
69
|
+
return streamSimple(model, context, streamOptions);
|
|
70
|
+
}
|
|
71
|
+
return streamSimple(model, context, {
|
|
72
|
+
...streamOptions,
|
|
73
|
+
onPayload: (payload, payloadModel) => applyFastPayloadHook(payload, payloadModel, streamOptions?.onPayload),
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
36
78
|
const EXTRACT_ACCOUNT_ID_PATCH = `function extractAccountId(token) {
|
|
37
79
|
// CLIProxyAPI accepts plain API keys as well as ChatGPT JWTs.
|
|
38
80
|
// Never throw: missing account id simply means no chatgpt-account-id header.
|
|
@@ -123,7 +165,10 @@ function resolveOriginalCodexModulePath(): { path: string; dir: string } {
|
|
|
123
165
|
throw new Error(`Cannot resolve openai-codex-responses.js (tried: ${candidates.join(", ") || "none"})`);
|
|
124
166
|
}
|
|
125
167
|
|
|
126
|
-
export async function loadCliproxyCodexStreams(
|
|
168
|
+
export async function loadCliproxyCodexStreams(
|
|
169
|
+
providerIds: string[] = ["cliproxyapi"],
|
|
170
|
+
options: CliproxyCodexStreamOptions = {},
|
|
171
|
+
): Promise<CliproxyCodexStreams> {
|
|
127
172
|
const { path: originalPath, dir: originalDir } = resolveOriginalCodexModulePath();
|
|
128
173
|
const originalSource = readFileSync(originalPath, "utf8");
|
|
129
174
|
const patched = rewriteRelativeImports(patchCodexSource(originalSource, providerIds), originalDir);
|
|
@@ -145,9 +190,11 @@ export async function loadCliproxyCodexStreams(providerIds: string[] = ["cliprox
|
|
|
145
190
|
throw new Error("patched openai-codex-responses module missing streamSimple/stream exports");
|
|
146
191
|
}
|
|
147
192
|
|
|
193
|
+
const streamSimple = wrapStreamSimpleForFast(mod.streamSimple, options.shouldUseFast);
|
|
194
|
+
|
|
148
195
|
return {
|
|
149
196
|
api: CLIPROXYAPI_CODEX_API,
|
|
150
|
-
streamSimple
|
|
197
|
+
streamSimple,
|
|
151
198
|
stream: mod.stream,
|
|
152
199
|
};
|
|
153
200
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export type FastModeSource = "configured-default" | "session-override";
|
|
2
|
+
|
|
3
|
+
export const FAST_MODE_ENTRY_TYPE = "cliproxyapi-fast-mode";
|
|
4
|
+
|
|
5
|
+
export interface FastModeSessionEntry {
|
|
6
|
+
type: string;
|
|
7
|
+
customType?: string;
|
|
8
|
+
data?: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Session-scoped Fast preference plus the capability set advertised by CPA. */
|
|
12
|
+
export class FastModeController {
|
|
13
|
+
private sessionOverride: boolean | undefined;
|
|
14
|
+
private supportedModelIds = new Set<string>();
|
|
15
|
+
|
|
16
|
+
constructor(private readonly defaultEnabled: boolean) {}
|
|
17
|
+
|
|
18
|
+
setSessionEnabled(enabled: boolean): void {
|
|
19
|
+
this.sessionOverride = enabled;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
clearSessionOverride(): void {
|
|
23
|
+
this.sessionOverride = undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
setSupportedModelIds(modelIds: Iterable<string>): void {
|
|
27
|
+
this.supportedModelIds = new Set(
|
|
28
|
+
Array.from(modelIds, (modelId) => modelId.trim()).filter((modelId) => modelId.length > 0),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
isEnabled(): boolean {
|
|
33
|
+
return this.sessionOverride ?? this.defaultEnabled;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getSource(): FastModeSource {
|
|
37
|
+
return this.sessionOverride === undefined ? "configured-default" : "session-override";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
isModelSupported(modelId: string): boolean {
|
|
41
|
+
return this.supportedModelIds.has(modelId.trim());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
isEffectiveFor(modelId: string): boolean {
|
|
45
|
+
return this.isEnabled() && this.isModelSupported(modelId);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function restoreFastModeFromEntries(
|
|
50
|
+
fastMode: FastModeController,
|
|
51
|
+
entries: Iterable<FastModeSessionEntry>,
|
|
52
|
+
): void {
|
|
53
|
+
fastMode.clearSessionOverride();
|
|
54
|
+
for (const entry of entries) {
|
|
55
|
+
if (entry.type !== "custom" || entry.customType !== FAST_MODE_ENTRY_TYPE) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const data = entry.data;
|
|
59
|
+
if (data && typeof data === "object" && typeof (data as { enabled?: unknown }).enabled === "boolean") {
|
|
60
|
+
fastMode.setSessionEnabled((data as { enabled: boolean }).enabled);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/extensions/index.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* (HTTP 200 = success even if the catalog is empty; otherwise re-prompt).
|
|
12
12
|
* 5. On success, models/credentials are saved and `/cliproxyapi` `/cpa` are hidden.
|
|
13
13
|
* 6. Setup commands reappear after `/logout` or when a models request returns HTTP 401.
|
|
14
|
+
* 7. `/cpa-fast` controls catalog-driven priority service tier injection per session.
|
|
14
15
|
*
|
|
15
16
|
* Uses a patched openai-codex-responses implementation that does not require
|
|
16
17
|
* extracting chatgpt_account_id from the API key (plain CPA keys work).
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
import type { Api, Model, OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
|
22
23
|
import { type ExtensionAPI, type ExtensionCommandContext, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
23
24
|
import { CLIPROXYAPI_CODEX_API, type CliproxyCodexStreamSimple, loadCliproxyCodexStreams } from "./codex-stream.ts";
|
|
25
|
+
import { FAST_MODE_ENTRY_TYPE, FastModeController, restoreFastModeFromEntries } from "./fast.ts";
|
|
24
26
|
import {
|
|
25
27
|
CONFIG_FILE_NAME,
|
|
26
28
|
CREDENTIAL_TTL_MS,
|
|
@@ -35,6 +37,7 @@ import {
|
|
|
35
37
|
type PiProviderModel,
|
|
36
38
|
resolveConnection,
|
|
37
39
|
resolveEndpoints,
|
|
40
|
+
resolveFastDefault,
|
|
38
41
|
resolveIdentity,
|
|
39
42
|
saveConfigFile,
|
|
40
43
|
} from "./lib.ts";
|
|
@@ -125,11 +128,23 @@ async function configureAndRegister(options: {
|
|
|
125
128
|
defaultBaseUrl: string;
|
|
126
129
|
streamSimple: CliproxyCodexStreamSimple;
|
|
127
130
|
setupCommands: SetupCommandsController;
|
|
131
|
+
fastMode: FastModeController;
|
|
128
132
|
}): Promise<{ modelCount: number; modelsUrl: string }> {
|
|
129
|
-
const {
|
|
130
|
-
|
|
133
|
+
const {
|
|
134
|
+
pi,
|
|
135
|
+
agentDir,
|
|
136
|
+
providerId,
|
|
137
|
+
providerName,
|
|
138
|
+
baseUrlInput,
|
|
139
|
+
apiKey,
|
|
140
|
+
defaultBaseUrl,
|
|
141
|
+
streamSimple,
|
|
142
|
+
setupCommands,
|
|
143
|
+
fastMode,
|
|
144
|
+
} = options;
|
|
131
145
|
|
|
132
146
|
const loaded = await loadMappedModels(baseUrlInput, apiKey);
|
|
147
|
+
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
133
148
|
|
|
134
149
|
saveConfigFile(agentDir, {
|
|
135
150
|
baseUrl: baseUrlInput,
|
|
@@ -148,6 +163,7 @@ async function configureAndRegister(options: {
|
|
|
148
163
|
agentDir,
|
|
149
164
|
streamSimple,
|
|
150
165
|
setupCommands,
|
|
166
|
+
fastMode,
|
|
151
167
|
});
|
|
152
168
|
|
|
153
169
|
// Login succeeded — hide dedicated setup commands from the slash menu.
|
|
@@ -164,8 +180,9 @@ function createOAuthHandlers(options: {
|
|
|
164
180
|
defaultBaseUrl: string;
|
|
165
181
|
streamSimple: CliproxyCodexStreamSimple;
|
|
166
182
|
setupCommands: SetupCommandsController;
|
|
183
|
+
fastMode: FastModeController;
|
|
167
184
|
}) {
|
|
168
|
-
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, setupCommands } = options;
|
|
185
|
+
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, setupCommands, fastMode } = options;
|
|
169
186
|
|
|
170
187
|
return {
|
|
171
188
|
name: providerName,
|
|
@@ -192,6 +209,7 @@ function createOAuthHandlers(options: {
|
|
|
192
209
|
defaultBaseUrl,
|
|
193
210
|
streamSimple,
|
|
194
211
|
setupCommands,
|
|
212
|
+
fastMode,
|
|
195
213
|
});
|
|
196
214
|
|
|
197
215
|
logInfo(`login ok: registered ${result.modelCount} models from ${result.modelsUrl}`);
|
|
@@ -251,6 +269,7 @@ function registerProvider(
|
|
|
251
269
|
agentDir: string;
|
|
252
270
|
streamSimple: CliproxyCodexStreamSimple;
|
|
253
271
|
setupCommands: SetupCommandsController;
|
|
272
|
+
fastMode: FastModeController;
|
|
254
273
|
},
|
|
255
274
|
): void {
|
|
256
275
|
const {
|
|
@@ -263,6 +282,7 @@ function registerProvider(
|
|
|
263
282
|
agentDir,
|
|
264
283
|
streamSimple,
|
|
265
284
|
setupCommands,
|
|
285
|
+
fastMode,
|
|
266
286
|
} = options;
|
|
267
287
|
|
|
268
288
|
const endpoints = resolveEndpoints(baseUrlInput);
|
|
@@ -274,6 +294,7 @@ function registerProvider(
|
|
|
274
294
|
defaultBaseUrl,
|
|
275
295
|
streamSimple,
|
|
276
296
|
setupCommands,
|
|
297
|
+
fastMode,
|
|
277
298
|
});
|
|
278
299
|
|
|
279
300
|
pi.registerProvider(providerId, {
|
|
@@ -330,8 +351,9 @@ function createSetupCommandsController(options: {
|
|
|
330
351
|
providerName: string;
|
|
331
352
|
defaultBaseUrl: string;
|
|
332
353
|
streamSimple: CliproxyCodexStreamSimple;
|
|
354
|
+
fastMode: FastModeController;
|
|
333
355
|
}): SetupCommandsController {
|
|
334
|
-
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple } = options;
|
|
356
|
+
const { pi, agentDir, providerId, providerName, defaultBaseUrl, streamSimple, fastMode } = options;
|
|
335
357
|
|
|
336
358
|
// Self-referential controller so handlers can hide/show after validation.
|
|
337
359
|
const setupCommands: SetupCommandsController = {
|
|
@@ -403,6 +425,7 @@ function createSetupCommandsController(options: {
|
|
|
403
425
|
defaultBaseUrl,
|
|
404
426
|
streamSimple,
|
|
405
427
|
setupCommands,
|
|
428
|
+
fastMode,
|
|
406
429
|
});
|
|
407
430
|
|
|
408
431
|
// Persist only after successful validation; keep AuthStorage in-memory too.
|
|
@@ -532,6 +555,67 @@ function createSetupCommandsController(options: {
|
|
|
532
555
|
return setupCommands;
|
|
533
556
|
}
|
|
534
557
|
|
|
558
|
+
function registerFastCommand(options: {
|
|
559
|
+
pi: ExtensionAPI;
|
|
560
|
+
providerId: string;
|
|
561
|
+
providerName: string;
|
|
562
|
+
fastMode: FastModeController;
|
|
563
|
+
}): void {
|
|
564
|
+
const { pi, providerId, providerName, fastMode } = options;
|
|
565
|
+
const actions = ["on", "off", "status"] as const;
|
|
566
|
+
|
|
567
|
+
pi.on("session_start", (_event, ctx) => {
|
|
568
|
+
restoreFastModeFromEntries(fastMode, ctx.sessionManager.getBranch());
|
|
569
|
+
});
|
|
570
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
571
|
+
restoreFastModeFromEntries(fastMode, ctx.sessionManager.getBranch());
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
pi.registerCommand("cpa-fast", {
|
|
575
|
+
description: "Control CLIProxyAPI Fast mode for this session: on, off, or status.",
|
|
576
|
+
getArgumentCompletions: (prefix) => {
|
|
577
|
+
const normalizedPrefix = prefix.trim().toLowerCase();
|
|
578
|
+
const matches = actions.filter((action) => action.startsWith(normalizedPrefix));
|
|
579
|
+
return matches.length > 0 ? matches.map((action) => ({ value: action, label: action })) : null;
|
|
580
|
+
},
|
|
581
|
+
handler: async (args, ctx) => {
|
|
582
|
+
const action = args.trim().toLowerCase() || "status";
|
|
583
|
+
if (action !== "on" && action !== "off" && action !== "status") {
|
|
584
|
+
ctx.ui.notify("Usage: /cpa-fast on|off|status", "error");
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (action === "on" || action === "off") {
|
|
589
|
+
const enabled = action === "on";
|
|
590
|
+
fastMode.setSessionEnabled(enabled);
|
|
591
|
+
pi.appendEntry(FAST_MODE_ENTRY_TYPE, { enabled });
|
|
592
|
+
logInfo(`Fast session override ${enabled ? "enabled" : "disabled"}`);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const enabled = fastMode.isEnabled();
|
|
596
|
+
const source = fastMode.getSource() === "session-override" ? "session override" : "configured default";
|
|
597
|
+
const currentModel = ctx.model;
|
|
598
|
+
let detail: string;
|
|
599
|
+
let severity: "info" | "warning" = "info";
|
|
600
|
+
|
|
601
|
+
if (!currentModel) {
|
|
602
|
+
detail = "No model is currently selected.";
|
|
603
|
+
} else if (currentModel.provider !== providerId) {
|
|
604
|
+
detail = `Current model ${currentModel.provider}/${currentModel.id} is not provided by ${providerName}.`;
|
|
605
|
+
} else if (!fastMode.isModelSupported(currentModel.id)) {
|
|
606
|
+
detail = `Current model ${providerId}/${currentModel.id} does not advertise Fast; requests remain unchanged.`;
|
|
607
|
+
if (enabled) severity = "warning";
|
|
608
|
+
} else if (enabled) {
|
|
609
|
+
detail = `Current model ${providerId}/${currentModel.id} will request service_tier=priority.`;
|
|
610
|
+
} else {
|
|
611
|
+
detail = `Current model ${providerId}/${currentModel.id} supports Fast; priority injection is off.`;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
ctx.ui.notify(`${providerName} Fast is ${enabled ? "on" : "off"} (${source}). ${detail}`, severity);
|
|
615
|
+
},
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
535
619
|
export { CLIPROXYAPI_CODEX_API } from "./codex-stream.ts";
|
|
536
620
|
export { resolveEndpoints, toPiModel } from "./lib.ts";
|
|
537
621
|
|
|
@@ -540,9 +624,20 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
540
624
|
const identity = resolveIdentity(agentDir);
|
|
541
625
|
const defaultBaseUrl = resolveDefaultBaseUrl(agentDir, identity.providerId);
|
|
542
626
|
|
|
627
|
+
let fastDefault = false;
|
|
628
|
+
try {
|
|
629
|
+
fastDefault = resolveFastDefault(agentDir);
|
|
630
|
+
} catch (error) {
|
|
631
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
632
|
+
logWarn(`invalid Fast configuration (${message}); using fast=false`);
|
|
633
|
+
}
|
|
634
|
+
const fastMode = new FastModeController(fastDefault);
|
|
635
|
+
|
|
543
636
|
let streamSimple: CliproxyCodexStreamSimple;
|
|
544
637
|
try {
|
|
545
|
-
const streams = await loadCliproxyCodexStreams([identity.providerId, "cliproxyapi"]
|
|
638
|
+
const streams = await loadCliproxyCodexStreams([identity.providerId, "cliproxyapi"], {
|
|
639
|
+
shouldUseFast: (model) => model.provider === identity.providerId && fastMode.isEffectiveFor(model.id),
|
|
640
|
+
});
|
|
546
641
|
streamSimple = streams.streamSimple;
|
|
547
642
|
} catch (error) {
|
|
548
643
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -550,6 +645,13 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
550
645
|
return;
|
|
551
646
|
}
|
|
552
647
|
|
|
648
|
+
registerFastCommand({
|
|
649
|
+
pi,
|
|
650
|
+
providerId: identity.providerId,
|
|
651
|
+
providerName: identity.providerName,
|
|
652
|
+
fastMode,
|
|
653
|
+
});
|
|
654
|
+
|
|
553
655
|
const setupCommands = createSetupCommandsController({
|
|
554
656
|
pi,
|
|
555
657
|
agentDir,
|
|
@@ -557,6 +659,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
557
659
|
providerName: identity.providerName,
|
|
558
660
|
defaultBaseUrl,
|
|
559
661
|
streamSimple,
|
|
662
|
+
fastMode,
|
|
560
663
|
});
|
|
561
664
|
|
|
562
665
|
// Re-show setup commands when the user /logout this provider.
|
|
@@ -577,6 +680,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
577
680
|
agentDir,
|
|
578
681
|
streamSimple,
|
|
579
682
|
setupCommands,
|
|
683
|
+
fastMode,
|
|
580
684
|
});
|
|
581
685
|
|
|
582
686
|
const connection = resolveConnection(agentDir, identity.providerId);
|
|
@@ -592,6 +696,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
592
696
|
|
|
593
697
|
try {
|
|
594
698
|
const loaded = await loadMappedModels(connection.baseUrlInput, connection.apiKey);
|
|
699
|
+
fastMode.setSupportedModelIds(loaded.fastModelIds);
|
|
595
700
|
registerProvider(pi, {
|
|
596
701
|
providerId: identity.providerId,
|
|
597
702
|
providerName: identity.providerName,
|
|
@@ -602,6 +707,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
602
707
|
agentDir,
|
|
603
708
|
streamSimple,
|
|
604
709
|
setupCommands,
|
|
710
|
+
fastMode,
|
|
605
711
|
});
|
|
606
712
|
// Already authenticated — keep /cliproxyapi and /cpa hidden.
|
|
607
713
|
setupCommands.hide();
|
package/extensions/lib.ts
CHANGED
|
@@ -31,6 +31,7 @@ export interface CliproxyConfigFile {
|
|
|
31
31
|
apiKey?: string;
|
|
32
32
|
providerId?: string;
|
|
33
33
|
providerName?: string;
|
|
34
|
+
fast?: boolean;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
export interface ResolvedIdentity {
|
|
@@ -50,6 +51,12 @@ export interface CodexReasoningLevel {
|
|
|
50
51
|
description?: string;
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
export interface CodexServiceTier {
|
|
55
|
+
id?: string;
|
|
56
|
+
name?: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
53
60
|
export interface CodexClientModel {
|
|
54
61
|
slug?: string;
|
|
55
62
|
id?: string;
|
|
@@ -60,6 +67,9 @@ export interface CodexClientModel {
|
|
|
60
67
|
max_context_window?: number;
|
|
61
68
|
input_modalities?: string[];
|
|
62
69
|
supported_reasoning_levels?: CodexReasoningLevel[] | string[];
|
|
70
|
+
default_service_tier?: string | null;
|
|
71
|
+
service_tiers?: Array<CodexServiceTier | string>;
|
|
72
|
+
additional_speed_tiers?: string[];
|
|
63
73
|
visibility?: string;
|
|
64
74
|
}
|
|
65
75
|
|
|
@@ -235,6 +245,44 @@ export function resolveIdentity(agentDir: string): ResolvedIdentity {
|
|
|
235
245
|
};
|
|
236
246
|
}
|
|
237
247
|
|
|
248
|
+
export function parseBooleanSetting(value: string): boolean | undefined {
|
|
249
|
+
switch (value.trim().toLowerCase()) {
|
|
250
|
+
case "1":
|
|
251
|
+
case "true":
|
|
252
|
+
case "yes":
|
|
253
|
+
case "on":
|
|
254
|
+
return true;
|
|
255
|
+
case "0":
|
|
256
|
+
case "false":
|
|
257
|
+
case "no":
|
|
258
|
+
case "off":
|
|
259
|
+
return false;
|
|
260
|
+
default:
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Resolve the Fast default from env, then cliproxyapi.json, then false. */
|
|
266
|
+
export function resolveFastDefault(agentDir: string): boolean {
|
|
267
|
+
const envValue = firstNonEmpty(process.env.CLIPROXYAPI_FAST);
|
|
268
|
+
if (envValue !== undefined) {
|
|
269
|
+
const parsed = parseBooleanSetting(envValue);
|
|
270
|
+
if (parsed === undefined) {
|
|
271
|
+
throw new Error(`CLIPROXYAPI_FAST must be one of: true, false, 1, 0, yes, no, on, off`);
|
|
272
|
+
}
|
|
273
|
+
return parsed;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const file = loadConfigFile(agentDir);
|
|
277
|
+
if (file.fast === undefined) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
if (typeof file.fast !== "boolean") {
|
|
281
|
+
throw new Error(`${CONFIG_FILE_NAME} field "fast" must be a boolean`);
|
|
282
|
+
}
|
|
283
|
+
return file.fast;
|
|
284
|
+
}
|
|
285
|
+
|
|
238
286
|
/**
|
|
239
287
|
* Resolve connection settings.
|
|
240
288
|
* Priority: env > cliproxyapi.json > auth.json (/login) > default baseUrl
|
|
@@ -317,8 +365,29 @@ export function buildInputModalities(model: CodexClientModel): Array<"text" | "i
|
|
|
317
365
|
return input;
|
|
318
366
|
}
|
|
319
367
|
|
|
368
|
+
export function codexModelId(model: CodexClientModel): string {
|
|
369
|
+
return (model.slug ?? model.id ?? "").trim();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export function supportsFastServiceTier(model: CodexClientModel): boolean {
|
|
373
|
+
const serviceTiers = Array.isArray(model.service_tiers) ? model.service_tiers : [];
|
|
374
|
+
for (const tier of serviceTiers) {
|
|
375
|
+
const id =
|
|
376
|
+
typeof tier === "string"
|
|
377
|
+
? tier
|
|
378
|
+
: tier && typeof tier === "object" && typeof tier.id === "string"
|
|
379
|
+
? tier.id
|
|
380
|
+
: undefined;
|
|
381
|
+
if (id?.trim().toLowerCase() === "priority") {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const speedTiers = Array.isArray(model.additional_speed_tiers) ? model.additional_speed_tiers : [];
|
|
386
|
+
return speedTiers.some((tier) => typeof tier === "string" && tier.trim().toLowerCase() === "fast");
|
|
387
|
+
}
|
|
388
|
+
|
|
320
389
|
export function toPiModel(model: CodexClientModel): PiProviderModel | null {
|
|
321
|
-
const id = (model
|
|
390
|
+
const id = codexModelId(model);
|
|
322
391
|
if (!id) {
|
|
323
392
|
return null;
|
|
324
393
|
}
|
|
@@ -404,14 +473,23 @@ export async function fetchCodexModels(modelsUrl: string, apiKey: string): Promi
|
|
|
404
473
|
export async function loadMappedModels(
|
|
405
474
|
baseUrlInput: string,
|
|
406
475
|
apiKey: string,
|
|
407
|
-
): Promise<{ models: PiProviderModel[]; inferenceBaseUrl: string; modelsUrl: string }> {
|
|
476
|
+
): Promise<{ models: PiProviderModel[]; fastModelIds: string[]; inferenceBaseUrl: string; modelsUrl: string }> {
|
|
408
477
|
const endpoints = resolveEndpoints(baseUrlInput);
|
|
409
478
|
const remoteModels = await fetchCodexModels(endpoints.modelsUrl, apiKey);
|
|
410
479
|
const models = remoteModels.map(toPiModel).filter((model): model is PiProviderModel => model !== null);
|
|
480
|
+
const fastModelIds = Array.from(
|
|
481
|
+
new Set(
|
|
482
|
+
remoteModels
|
|
483
|
+
.filter(supportsFastServiceTier)
|
|
484
|
+
.map(codexModelId)
|
|
485
|
+
.filter((modelId) => modelId.length > 0),
|
|
486
|
+
),
|
|
487
|
+
);
|
|
411
488
|
|
|
412
489
|
// Empty catalog is valid: credentials passed (HTTP 200), just no usable models yet.
|
|
413
490
|
return {
|
|
414
491
|
models,
|
|
492
|
+
fastModelIds,
|
|
415
493
|
inferenceBaseUrl: endpoints.inferenceBaseUrl,
|
|
416
494
|
modelsUrl: endpoints.modelsUrl,
|
|
417
495
|
};
|
package/package.json
CHANGED