pi-cliproxyapi-provider 0.15.46 → 0.15.47
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 +5 -5
- package/data/models-dev-fallback.json +1 -1
- package/extensions/index.ts +6 -0
- package/package.json +1 -1
- package/src/gateway-telemetry.ts +64 -0
- package/src/model-api.ts +40 -5
- package/src/model-capabilities.ts +24 -0
- package/src/provider.ts +8 -2
- package/src/registration.ts +35 -2
- package/src/runtime.ts +2 -0
- package/src/types.ts +1 -0
package/extensions/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { registerCliproxyapiCommand } from "../src/commands.ts";
|
|
|
10
10
|
import { getDiscoveryApiKey } from "../src/auth.ts";
|
|
11
11
|
import { loadProviderSettings } from "../src/settings.ts";
|
|
12
12
|
import { registerCodexCompatiblePayloadAdapter } from "../src/codex-compat.ts";
|
|
13
|
+
import { registerGatewayTelemetry } from "../src/gateway-telemetry.ts";
|
|
13
14
|
|
|
14
15
|
const extensionDir = dirname(fileURLToPath(import.meta.url));
|
|
15
16
|
const packageRoot = dirname(extensionDir);
|
|
@@ -30,11 +31,16 @@ export default async function (pi: ExtensionAPI) {
|
|
|
30
31
|
const runtime = new ProviderRuntime({ pi, config, catalog });
|
|
31
32
|
registerCodexCompatiblePayloadAdapter(pi, config.providerName);
|
|
32
33
|
registerCliproxyapiCommand(pi, runtime, catalog);
|
|
34
|
+
registerGatewayTelemetry(pi as never);
|
|
33
35
|
await runtime.start();
|
|
34
36
|
} catch (error) {
|
|
35
37
|
registerCodexCompatiblePayloadAdapter(pi, config.providerName);
|
|
36
38
|
registerCliproxyapiCommand(pi);
|
|
39
|
+
registerGatewayTelemetry(pi as never);
|
|
37
40
|
pi.registerProvider(config.providerName, buildProviderRegistration(config, buildUnavailableProviderModels()).config);
|
|
38
41
|
console.warn(`[pi-cliproxyapi-provider] registered placeholder provider after startup failure: ${error instanceof Error ? error.message : String(error)}`);
|
|
39
42
|
}
|
|
40
43
|
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
export { registerGatewayTelemetry } from "../src/gateway-telemetry.ts";
|
package/package.json
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
function readPositiveNumber(raw: string | null): number | undefined {
|
|
2
|
+
if (raw == null || raw.trim() === "") return undefined;
|
|
3
|
+
const parsed = Number(raw);
|
|
4
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return undefined;
|
|
5
|
+
return parsed;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function tokensPerSecondFromHeaders(headers: Headers): number | undefined {
|
|
9
|
+
return readPositiveNumber(
|
|
10
|
+
headers.get("x-cliproxyapi-tokens-per-second") ??
|
|
11
|
+
headers.get("x-cliproxy-tokens-per-second") ??
|
|
12
|
+
headers.get("x-omniroute-tokens-per-second"),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function captureGatewayTokensPerSecond(response: Response): number | undefined {
|
|
17
|
+
return tokensPerSecondFromHeaders(response.headers);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function wrapFetchCaptureTokensPerSecond(
|
|
21
|
+
fetchImpl: typeof fetch,
|
|
22
|
+
onCapture: (tps: number) => void,
|
|
23
|
+
): typeof fetch {
|
|
24
|
+
return async (input, init) => {
|
|
25
|
+
const response = await fetchImpl(input, init);
|
|
26
|
+
const tps = captureGatewayTokensPerSecond(response);
|
|
27
|
+
if (tps !== undefined) onCapture(tps);
|
|
28
|
+
return response;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function formatGatewayTokensPerSecond(tps: number | undefined): string {
|
|
33
|
+
return tps === undefined ? "--" : tps.toFixed(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function registerGatewayTelemetry(pi: {
|
|
37
|
+
on(event: string, handler: (...args: never[]) => unknown): void;
|
|
38
|
+
}): void {
|
|
39
|
+
let captured: number | undefined;
|
|
40
|
+
let restoreFetch: (() => void) | undefined;
|
|
41
|
+
const install = () => {
|
|
42
|
+
if (restoreFetch) return;
|
|
43
|
+
const originalFetch = globalThis.fetch.bind(globalThis);
|
|
44
|
+
globalThis.fetch = wrapFetchCaptureTokensPerSecond(originalFetch, (tps) => {
|
|
45
|
+
captured = tps;
|
|
46
|
+
});
|
|
47
|
+
restoreFetch = () => {
|
|
48
|
+
globalThis.fetch = originalFetch;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
pi.on("session_start", (() => {
|
|
52
|
+
captured = undefined;
|
|
53
|
+
install();
|
|
54
|
+
}) as (...args: never[]) => unknown);
|
|
55
|
+
pi.on("agent_settled", ((_event: unknown, ctx: { ui?: { notify?: (message: string, type?: string) => void } }) => {
|
|
56
|
+
ctx.ui?.notify?.(`TPS ${formatGatewayTokensPerSecond(captured)} tok/s`, "info");
|
|
57
|
+
captured = undefined;
|
|
58
|
+
}) as (...args: never[]) => unknown);
|
|
59
|
+
pi.on("session_shutdown", (() => {
|
|
60
|
+
restoreFetch?.();
|
|
61
|
+
restoreFetch = undefined;
|
|
62
|
+
captured = undefined;
|
|
63
|
+
}) as (...args: never[]) => unknown);
|
|
64
|
+
}
|
package/src/model-api.ts
CHANGED
|
@@ -6,21 +6,56 @@ export interface ModelApiContext {
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
const GPT_5_6_MODEL = /^gpt-5\.6(?:-|$)/;
|
|
9
|
+
const GPT_6_MODEL = /^gpt-6(?:\.\d+)?(?:-|$)/;
|
|
10
|
+
const CLAUDE_MODEL = /^claude(?:-|$)/;
|
|
9
11
|
|
|
10
12
|
function modelName(id: string): string {
|
|
11
13
|
return id.slice(id.lastIndexOf("/") + 1);
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
function matchesModelId(context: ModelApiContext, pattern: RegExp): boolean {
|
|
15
17
|
const ids = [context.availableModelId, context.metadataModelId].filter((id): id is string => id !== undefined);
|
|
16
|
-
return ids.some((id) =>
|
|
18
|
+
return ids.some((id) => pattern.test(modelName(id)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isGpt56Model(context: ModelApiContext): boolean {
|
|
22
|
+
return matchesModelId(context, GPT_5_6_MODEL);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** GPT-6 family (`gpt-6-astra`, and any `gpt-6.x-*` successor). */
|
|
26
|
+
export function isGpt6Model(context: ModelApiContext): boolean {
|
|
27
|
+
return matchesModelId(context, GPT_6_MODEL);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The Codex Responses family: every GPT model CLIProxyAPI serves from a Codex
|
|
32
|
+
* OAuth credential rather than a plain OpenAI API key. They share the same
|
|
33
|
+
* Responses-only upstream, the same usage shape, and the same conservative
|
|
34
|
+
* 272000 context window unless the CPA route is known to allow more.
|
|
35
|
+
*/
|
|
36
|
+
export function isCodexResponsesModel(context: ModelApiContext): boolean {
|
|
37
|
+
return isGpt56Model(context) || isGpt6Model(context);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function isClaudeModel(context: ModelApiContext): boolean {
|
|
41
|
+
return matchesModelId(context, CLAUDE_MODEL);
|
|
17
42
|
}
|
|
18
43
|
|
|
19
44
|
/**
|
|
20
45
|
* Select a model-level API when a mixed CLIProxyAPI catalog cannot share the
|
|
21
|
-
* provider default.
|
|
22
|
-
*
|
|
46
|
+
* provider default.
|
|
47
|
+
*
|
|
48
|
+
* - GPT-5.6 (including Codex variants) and GPT-6 (`gpt-6-astra`) use the
|
|
49
|
+
* Responses API so Pi receives the Responses usage shape needed for
|
|
50
|
+
* token-cost accounting. CLIProxyAPI serves these from Codex OAuth
|
|
51
|
+
* credentials, whose upstream is Responses-only.
|
|
52
|
+
* - Claude models use the Anthropic Messages API, which CLIProxyAPI serves
|
|
53
|
+
* faithfully at `/v1/messages`. The Chat Completions shape cannot represent
|
|
54
|
+
* signed thinking blocks or per-turn thinking effort, so routing Claude
|
|
55
|
+
* through it drops signatures and breaks multi-turn reasoning replay.
|
|
23
56
|
*/
|
|
24
57
|
export function getModelApiOverride(context: ModelApiContext): ProviderModelConfigLike["api"] | undefined {
|
|
25
|
-
|
|
58
|
+
if (isCodexResponsesModel(context)) return "openai-responses";
|
|
59
|
+
if (isClaudeModel(context)) return "anthropic-messages";
|
|
60
|
+
return undefined;
|
|
26
61
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ThinkingLevelMap } from "@earendil-works/pi-ai";
|
|
2
|
+
import { isGpt6Model } from "./model-api.ts";
|
|
2
3
|
|
|
3
4
|
export interface ModelCapabilityContext {
|
|
4
5
|
availableModelId: string;
|
|
@@ -25,6 +26,22 @@ const GPT_5_6_THINKING_LEVEL_MAP: ThinkingLevelMap = {
|
|
|
25
26
|
max: "max",
|
|
26
27
|
};
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* GPT-6 Astra cannot switch reasoning off (its catalog entry lists only
|
|
31
|
+
* low..max plus an `ultra` level pi has no slot for), and it has no `minimal`
|
|
32
|
+
* effort: the lowest it accepts is `low`, so pi's `minimal` maps down to it.
|
|
33
|
+
* Mirrors pi's native `openai-codex/gpt-6-astra` definition.
|
|
34
|
+
*/
|
|
35
|
+
const GPT_6_THINKING_LEVEL_MAP: ThinkingLevelMap = {
|
|
36
|
+
off: null,
|
|
37
|
+
minimal: "low",
|
|
38
|
+
low: "low",
|
|
39
|
+
medium: "medium",
|
|
40
|
+
high: "high",
|
|
41
|
+
xhigh: "xhigh",
|
|
42
|
+
max: "max",
|
|
43
|
+
};
|
|
44
|
+
|
|
28
45
|
function includesModelFamily(context: ModelCapabilityContext, family: string): boolean {
|
|
29
46
|
return [context.availableModelId, context.metadataModelId]
|
|
30
47
|
.filter((id): id is string => id !== undefined)
|
|
@@ -39,6 +56,13 @@ const MODEL_CAPABILITY_RULES: readonly ModelCapabilityRule[] = [
|
|
|
39
56
|
thinkingLevelMap: GPT_5_6_THINKING_LEVEL_MAP,
|
|
40
57
|
},
|
|
41
58
|
},
|
|
59
|
+
{
|
|
60
|
+
matches: (context) => isGpt6Model(context),
|
|
61
|
+
overrides: {
|
|
62
|
+
reasoning: true,
|
|
63
|
+
thinkingLevelMap: GPT_6_THINKING_LEVEL_MAP,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
42
66
|
];
|
|
43
67
|
|
|
44
68
|
export function getModelCapabilityOverrides(context: ModelCapabilityContext): ModelCapabilityOverrides {
|
package/src/provider.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CpaModel } from "./cpa.ts";
|
|
2
2
|
import { findMetadataMatch, type MetadataMatchMethod } from "./matching.ts";
|
|
3
|
-
import { getModelApiOverride,
|
|
3
|
+
import { getModelApiOverride, isCodexResponsesModel, type ModelApiContext } from "./model-api.ts";
|
|
4
4
|
import { getModelCapabilityOverrides } from "./model-capabilities.ts";
|
|
5
5
|
import type { Gpt56ContextWindowMode } from "./settings.ts";
|
|
6
6
|
import type {
|
|
@@ -11,6 +11,12 @@ import type {
|
|
|
11
11
|
ProviderModelOverrides,
|
|
12
12
|
} from "./types.ts";
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Pi's conservative context window for the Codex Responses family (GPT-5.6
|
|
16
|
+
* and GPT-6). models.dev advertises up to 1050000 for these models, but a
|
|
17
|
+
* CLIProxyAPI route only allows that when its `max-context-length` override is
|
|
18
|
+
* set, so the `gpt56ContextWindow` setting must opt in explicitly.
|
|
19
|
+
*/
|
|
14
20
|
export const GPT_5_6_CANONICAL_CONTEXT_WINDOW = 272000;
|
|
15
21
|
|
|
16
22
|
export const PI_MODEL_DEFAULTS = {
|
|
@@ -66,7 +72,7 @@ function contextWindowForModel(
|
|
|
66
72
|
metadataContextWindow: number | undefined,
|
|
67
73
|
mode: Gpt56ContextWindowMode,
|
|
68
74
|
): number {
|
|
69
|
-
if (!
|
|
75
|
+
if (!isCodexResponsesModel(context)) return metadataContextWindow ?? PI_MODEL_DEFAULTS.contextWindow;
|
|
70
76
|
if (mode === "full") return metadataContextWindow ?? GPT_5_6_CANONICAL_CONTEXT_WINDOW;
|
|
71
77
|
return GPT_5_6_CANONICAL_CONTEXT_WINDOW;
|
|
72
78
|
}
|
package/src/registration.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RefreshModelsContext } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { ProviderConfig, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { isClaudeModel } from "./model-api.ts";
|
|
3
4
|
import type { CpaProviderConfig } from "./types.ts";
|
|
4
5
|
import type { ProviderModelConfigLike } from "./types.ts";
|
|
5
6
|
|
|
@@ -8,7 +9,23 @@ export interface ProviderRegistration {
|
|
|
8
9
|
config: ProviderConfig;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The base URL an `anthropic-messages` model must carry.
|
|
14
|
+
*
|
|
15
|
+
* Pi's Anthropic driver builds its endpoint as `<baseUrl>/v1/messages`, while
|
|
16
|
+
* the provider base URL points at CLIProxyAPI's OpenAI-compatible root, which
|
|
17
|
+
* conventionally ends in `/v1` (model discovery is `<baseUrl>/models`). Reusing
|
|
18
|
+
* it verbatim would request `/v1/v1/messages` and get a 404, so Claude models
|
|
19
|
+
* publish a model-level base URL with that one trailing `/v1` removed.
|
|
20
|
+
*/
|
|
21
|
+
export function anthropicBaseUrl(providerBaseUrl: string): string {
|
|
22
|
+
return providerBaseUrl.replace(/\/+$/u, "").replace(/\/v1$/u, "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function normalizeProviderModels(
|
|
26
|
+
models: ProviderModelConfigLike[],
|
|
27
|
+
providerBaseUrl?: string,
|
|
28
|
+
): ProviderModelConfig[] {
|
|
12
29
|
return models.map((model) => ({
|
|
13
30
|
...model,
|
|
14
31
|
// CLIProxyAPI accepts OpenAI-compatible function tools for both Chat
|
|
@@ -18,7 +35,23 @@ export function normalizeProviderModels(models: ProviderModelConfigLike[]): Prov
|
|
|
18
35
|
compat: {
|
|
19
36
|
...model.compat,
|
|
20
37
|
supportsStrictMode: false,
|
|
38
|
+
// Claude models run on the Anthropic Messages API (see model-api.ts) and
|
|
39
|
+
// use the same adaptive thinking path pi uses for anthropic/* models, so
|
|
40
|
+
// effort rides on a top-level `output_config`.
|
|
41
|
+
//
|
|
42
|
+
// `supportsMidConvoEffort` is deliberately NOT set. It makes pi attach a
|
|
43
|
+
// per-message `output_config` to synthesized system messages behind the
|
|
44
|
+
// mid-conversation-output-config beta; CLIProxyAPI does not honor that
|
|
45
|
+
// beta and rejects the request with
|
|
46
|
+
// `messages.N.output_config: Extra inputs are not permitted`. Top-level
|
|
47
|
+
// `output_config` is accepted, so adaptive thinking still works.
|
|
48
|
+
...(isClaudeModel({ availableModelId: model.id })
|
|
49
|
+
? { forceAdaptiveThinking: true }
|
|
50
|
+
: {}),
|
|
21
51
|
},
|
|
52
|
+
...(providerBaseUrl && isClaudeModel({ availableModelId: model.id })
|
|
53
|
+
? { baseUrl: anthropicBaseUrl(providerBaseUrl) }
|
|
54
|
+
: {}),
|
|
22
55
|
})) as ProviderModelConfig[];
|
|
23
56
|
}
|
|
24
57
|
|
|
@@ -36,7 +69,7 @@ export function buildProviderRegistration(
|
|
|
36
69
|
apiKey: config.authRequired ? "$CLIPROXYAPI_API_KEY" : "cliproxyapi-no-auth",
|
|
37
70
|
authHeader: config.authRequired && config.authHeader,
|
|
38
71
|
headers: Object.keys(config.headers).length > 0 ? config.headers : undefined,
|
|
39
|
-
models: normalizeProviderModels(models),
|
|
72
|
+
models: normalizeProviderModels(models, config.baseUrl),
|
|
40
73
|
refreshModels,
|
|
41
74
|
},
|
|
42
75
|
};
|
package/src/runtime.ts
CHANGED
|
@@ -41,6 +41,7 @@ export class ProviderRuntime {
|
|
|
41
41
|
const snapshot = await this.options.catalog.load();
|
|
42
42
|
return normalizeProviderModels(
|
|
43
43
|
snapshot.built.models.length > 0 ? snapshot.built.models : buildUnavailableProviderModels(),
|
|
44
|
+
this.options.config.baseUrl,
|
|
44
45
|
);
|
|
45
46
|
}
|
|
46
47
|
const mode = context.force ? "manual" : "background";
|
|
@@ -55,6 +56,7 @@ export class ProviderRuntime {
|
|
|
55
56
|
result.snapshot.built.models.length > 0
|
|
56
57
|
? result.snapshot.built.models
|
|
57
58
|
: buildUnavailableProviderModels(),
|
|
59
|
+
this.options.config.baseUrl,
|
|
58
60
|
);
|
|
59
61
|
}
|
|
60
62
|
|
package/src/types.ts
CHANGED