@stablekernel/pi-bifrost 0.1.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 +26 -0
- package/package.json +25 -0
- package/src/index.ts +316 -0
- package/src/plugin.test.ts +346 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @stablekernel/pi-bifrost
|
|
2
|
+
|
|
3
|
+
Pi extension that registers a [Bifrost](https://docs.getbifrost.ai) LLM gateway as a model provider, with per-project virtual-key resolution delegated to `gateway-cli`.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
- Registers two providers:
|
|
8
|
+
- `bifrost-openai` — the OpenAI-compatible surface, every chat model the gateway serves.
|
|
9
|
+
- `bifrost-anthropic` — the Anthropic Messages surface, Claude models only. The Anthropic wire carries prompt-cache control blocks that only Claude accepts, so non-Claude models are registered on the OpenAI surface instead of being listed twice with one copy broken.
|
|
10
|
+
- Fetches each surface's model list, limits and pricing through `gateway-cli models --wire=…` at startup — no catalog is checked in.
|
|
11
|
+
- Seeds reasoning capability per model: catalog-flagged models register `reasoning: true`. On the OpenAI surface they also get a `thinkingLevelMap` translating pi's thinking levels to the model's effort values (unapplicable levels are hidden, never passed through by name) plus `supportsReasoningEffort`, so pi's per-request effort picker sends the provider's own values. On the Anthropic surface the capability is flagged and pi's Claude thinking handling applies. Toggle-only reasoning models get no effort map. The default effort itself is pi's own thinking-level default.
|
|
12
|
+
- Never holds the key: pi resolves it per request via the `!gateway-cli key print` command form, so the per-project key cascade (environment override → project → user default) applies on every turn.
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
- `gateway-cli` on `PATH` (or the `GATEWAY_CLI_BIN` env var pointing at it), connected once with `gateway-cli connect`.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pi install npm:@stablekernel/pi-bifrost
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
UNLICENSED — published for installation convenience; all rights reserved. Intended for use within the organization that operates the Bifrost gateway.
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stablekernel/pi-bifrost",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi extension that registers the Bifrost gateway as a model provider, resolving per-project virtual keys via gateway-cli.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"pi": {
|
|
11
|
+
"extensions": [
|
|
12
|
+
"./src/index.ts"
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"typecheck": "tsc --noEmit"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/bun": "^1.3.14",
|
|
20
|
+
"@types/node": "^22.20.1",
|
|
21
|
+
"typescript": "^5.9.3"
|
|
22
|
+
},
|
|
23
|
+
"license": "UNLICENSED",
|
|
24
|
+
"private": false
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal structural type for the part of pi's ExtensionAPI this extension
|
|
5
|
+
* uses.
|
|
6
|
+
*
|
|
7
|
+
* Declared locally on purpose. Importing the type from
|
|
8
|
+
* `@earendil-works/pi-coding-agent` would make typechecking depend on a
|
|
9
|
+
* globally installed pi being resolvable from node_modules (in practice a
|
|
10
|
+
* symlink into the Homebrew prefix), which breaks CI and any contributor
|
|
11
|
+
* without that exact layout. Pi loads this file at runtime and injects the
|
|
12
|
+
* real object; only the call shape below needs to match.
|
|
13
|
+
*
|
|
14
|
+
* Shape per docs/custom-provider.md: pi.registerProvider(id, config).
|
|
15
|
+
*/
|
|
16
|
+
type ProviderModelConfig = {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
reasoning: boolean;
|
|
20
|
+
input: Array<"text" | "image">;
|
|
21
|
+
cost: {
|
|
22
|
+
input: number;
|
|
23
|
+
output: number;
|
|
24
|
+
cacheRead: number;
|
|
25
|
+
cacheWrite: number;
|
|
26
|
+
};
|
|
27
|
+
contextWindow: number;
|
|
28
|
+
maxTokens: number;
|
|
29
|
+
/** Maps pi thinking levels to provider effort values; null hides a level
|
|
30
|
+
* the model does not accept. Omitted when the model has no effort levels. */
|
|
31
|
+
thinkingLevelMap?: Partial<
|
|
32
|
+
Record<
|
|
33
|
+
"off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max",
|
|
34
|
+
string | null
|
|
35
|
+
>
|
|
36
|
+
>;
|
|
37
|
+
/** Provider/model compat flags pi reads. Only the subset this provider sets. */
|
|
38
|
+
compat?: {
|
|
39
|
+
supportsReasoningEffort?: boolean;
|
|
40
|
+
forceAdaptiveThinking?: boolean;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type ProviderConfig = {
|
|
45
|
+
baseUrl: string;
|
|
46
|
+
apiKey: string;
|
|
47
|
+
api: "anthropic-messages" | "openai-completions";
|
|
48
|
+
models: ProviderModelConfig[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type ExtensionAPI = {
|
|
52
|
+
registerProvider(id: string, config: ProviderConfig): void;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Overridable so a non-PATH install still works. */
|
|
56
|
+
const CLI = process.env.GATEWAY_CLI_BIN ?? "gateway-cli";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Suffix for models nothing could price.
|
|
60
|
+
*
|
|
61
|
+
* Pi's model shape requires `cost`, and an absent rate card defaults to
|
|
62
|
+
* zeros, which renders as "free" rather than "unknown". Since the number
|
|
63
|
+
* cannot express uncertainty, the name says it instead.
|
|
64
|
+
*/
|
|
65
|
+
const UNPRICED_SUFFIX = " (pricing unknown)";
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Non-secret wiring from `gateway-cli resolve --json`. The virtual key is
|
|
69
|
+
* never in here: pi resolves it at request time through the `!command`
|
|
70
|
+
* apiKey form, so the key stays out of this extension's memory and logs.
|
|
71
|
+
*/
|
|
72
|
+
type Resolution = {
|
|
73
|
+
env: string;
|
|
74
|
+
root: string;
|
|
75
|
+
anthropic_base_url: string;
|
|
76
|
+
openai_base_url: string;
|
|
77
|
+
has_key: boolean;
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One model as reported by `gateway-cli models`. Limits are always present;
|
|
83
|
+
* `cost` is omitted when unknown, never zeroed. `reasoning` and
|
|
84
|
+
* `reasoning_efforts` come from the catalog; absent means nothing advertised
|
|
85
|
+
* the capability.
|
|
86
|
+
*/
|
|
87
|
+
type CliModel = {
|
|
88
|
+
id: string;
|
|
89
|
+
name: string;
|
|
90
|
+
context_window: number;
|
|
91
|
+
max_output_tokens: number;
|
|
92
|
+
cost?: {
|
|
93
|
+
input: number;
|
|
94
|
+
output: number;
|
|
95
|
+
cache_read: number;
|
|
96
|
+
cache_write: number;
|
|
97
|
+
};
|
|
98
|
+
limits_source: string;
|
|
99
|
+
cost_source?: string;
|
|
100
|
+
reasoning?: boolean;
|
|
101
|
+
reasoning_efforts?: string[];
|
|
102
|
+
reasoning_budget_min?: number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
type CliModels = { models?: CliModel[] };
|
|
106
|
+
|
|
107
|
+
type WireSurface = {
|
|
108
|
+
id: string;
|
|
109
|
+
label: string;
|
|
110
|
+
baseUrl: string;
|
|
111
|
+
api: "anthropic-messages" | "openai-completions";
|
|
112
|
+
/** `--wire` value for `gateway-cli models`. */
|
|
113
|
+
wire: "anthropic" | "openai";
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Bifrost gateway provider for pi.
|
|
118
|
+
*
|
|
119
|
+
* Both the model list and its per-model limits and pricing come from
|
|
120
|
+
* `gateway-cli models`, which reads the gateway live and fills the gaps the
|
|
121
|
+
* gateway leaves. Keeping that logic in the CLI means one implementation
|
|
122
|
+
* shared with the OpenCode plugin, one catalog cache instead of one per
|
|
123
|
+
* harness, and — because the CLI holds the virtual key itself — no path by
|
|
124
|
+
* which the secret enters this process. Pi still resolves the credential
|
|
125
|
+
* separately at request time via the `!command` apiKey form.
|
|
126
|
+
*/
|
|
127
|
+
export default async function (pi: ExtensionAPI): Promise<void> {
|
|
128
|
+
const info = resolve();
|
|
129
|
+
if (!info) {
|
|
130
|
+
// gateway-cli absent or project not configured: register nothing rather
|
|
131
|
+
// than surfacing a provider that cannot authenticate.
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const surfaces: WireSurface[] = [
|
|
136
|
+
{
|
|
137
|
+
id: "bifrost-anthropic",
|
|
138
|
+
label: `Bifrost Anthropic (${info.env})`,
|
|
139
|
+
baseUrl: info.anthropic_base_url,
|
|
140
|
+
api: "anthropic-messages",
|
|
141
|
+
wire: "anthropic",
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: "bifrost-openai",
|
|
145
|
+
label: `Bifrost OpenAI (${info.env})`,
|
|
146
|
+
baseUrl: info.openai_base_url,
|
|
147
|
+
api: "openai-completions",
|
|
148
|
+
wire: "openai",
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
for (const surface of surfaces) {
|
|
153
|
+
const models = listModels(surface.wire, surface.api);
|
|
154
|
+
if (models.length === 0) continue;
|
|
155
|
+
|
|
156
|
+
pi.registerProvider(surface.id, {
|
|
157
|
+
baseUrl: surface.baseUrl,
|
|
158
|
+
// `!command` runs the command for the whole value, so the key is read
|
|
159
|
+
// fresh from the cascade instead of being frozen at startup.
|
|
160
|
+
apiKey: `!${CLI} key print`,
|
|
161
|
+
api: surface.api,
|
|
162
|
+
models,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Ask the CLI for one wire's models, already enriched.
|
|
169
|
+
*
|
|
170
|
+
* The CLI also applies the filters every harness needs: non-chat modalities
|
|
171
|
+
* are dropped, and the Anthropic wire is restricted to Claude models because
|
|
172
|
+
* Bifrost sends prompt-cache blocks through it that only Claude accepts.
|
|
173
|
+
*/
|
|
174
|
+
export function toPiModels(
|
|
175
|
+
payload: CliModels | null,
|
|
176
|
+
api: "anthropic-messages" | "openai-completions",
|
|
177
|
+
): ProviderModelConfig[] {
|
|
178
|
+
const out: ProviderModelConfig[] = [];
|
|
179
|
+
for (const m of payload?.models ?? []) {
|
|
180
|
+
if (!m?.id) continue;
|
|
181
|
+
const priced = m.cost !== undefined;
|
|
182
|
+
const reasoning = !!m.reasoning;
|
|
183
|
+
const model: ProviderModelConfig = {
|
|
184
|
+
id: m.id,
|
|
185
|
+
name: priced ? m.name : `${m.name}${UNPRICED_SUFFIX}`,
|
|
186
|
+
reasoning,
|
|
187
|
+
input: ["text"],
|
|
188
|
+
cost: {
|
|
189
|
+
input: m.cost?.input ?? 0,
|
|
190
|
+
output: m.cost?.output ?? 0,
|
|
191
|
+
cacheRead: m.cost?.cache_read ?? 0,
|
|
192
|
+
cacheWrite: m.cost?.cache_write ?? 0,
|
|
193
|
+
},
|
|
194
|
+
contextWindow: m.context_window,
|
|
195
|
+
maxTokens: m.max_output_tokens,
|
|
196
|
+
};
|
|
197
|
+
if (reasoning) {
|
|
198
|
+
const map = thinkingLevelMap(m.reasoning_efforts);
|
|
199
|
+
if (api === "openai-completions") {
|
|
200
|
+
// pi-ai sends `reasoning_effort` only when supportsReasoningEffort is
|
|
201
|
+
// true and the user has picked a thinking level; the map translates
|
|
202
|
+
// pi's levels to the provider's values and nulls the rest.
|
|
203
|
+
if (map) {
|
|
204
|
+
model.thinkingLevelMap = map;
|
|
205
|
+
model.compat = { supportsReasoningEffort: true };
|
|
206
|
+
} else {
|
|
207
|
+
// Toggle-only reasoning: no effort levels to map. Flag the
|
|
208
|
+
// capability; pi leaves the provider's default on/off path alone.
|
|
209
|
+
model.compat = { supportsReasoningEffort: false };
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Anthropic-messages surface: reasoning:true tells pi the model can
|
|
213
|
+
// think; pi's built-in Claude handling applies. forceAdaptiveThinking
|
|
214
|
+
// for custom providers is not verified against pi-ai, so it is not set
|
|
215
|
+
// here — see the package README for the open gap.
|
|
216
|
+
}
|
|
217
|
+
out.push(model);
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** pi's thinking levels, lowest to highest. */
|
|
223
|
+
const PI_LEVELS = [
|
|
224
|
+
"off",
|
|
225
|
+
"minimal",
|
|
226
|
+
"low",
|
|
227
|
+
"medium",
|
|
228
|
+
"high",
|
|
229
|
+
"xhigh",
|
|
230
|
+
"max",
|
|
231
|
+
] as const;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Build a thinkingLevelMap from a model's advertised effort values. Each pi
|
|
235
|
+
* level maps to the same-named provider value when the model accepts it, and
|
|
236
|
+
* to null (hidden) otherwise. `off` is never an effort value, so it maps to
|
|
237
|
+
* null: when the user turns thinking off, pi-ai omits `reasoning_effort`
|
|
238
|
+
* rather than sending a nonsense string. Returns undefined for a model with
|
|
239
|
+
* no effort levels (toggle-only reasoning).
|
|
240
|
+
*/
|
|
241
|
+
function thinkingLevelMap(
|
|
242
|
+
efforts: string[] | undefined,
|
|
243
|
+
): Partial<Record<(typeof PI_LEVELS)[number], string | null>> | undefined {
|
|
244
|
+
if (!efforts || efforts.length === 0) return undefined;
|
|
245
|
+
const accepted = new Set(efforts);
|
|
246
|
+
const map: Partial<Record<(typeof PI_LEVELS)[number], string | null>> = {};
|
|
247
|
+
for (const lvl of PI_LEVELS) {
|
|
248
|
+
map[lvl] = lvl === "off" ? null : accepted.has(lvl) ? lvl : null;
|
|
249
|
+
}
|
|
250
|
+
return map;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function listModels(
|
|
254
|
+
wire: "anthropic" | "openai",
|
|
255
|
+
api: "anthropic-messages" | "openai-completions",
|
|
256
|
+
): ProviderModelConfig[] {
|
|
257
|
+
const raw = run(["models", `--wire=${wire}`]);
|
|
258
|
+
if (!raw) return [];
|
|
259
|
+
try {
|
|
260
|
+
return toPiModels(JSON.parse(raw) as CliModels, api);
|
|
261
|
+
} catch {
|
|
262
|
+
return [];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function resolve(): Resolution | null {
|
|
267
|
+
const raw = run(["resolve", "--json"]);
|
|
268
|
+
if (!raw) return null;
|
|
269
|
+
try {
|
|
270
|
+
const parsed = JSON.parse(raw) as Resolution;
|
|
271
|
+
if (!parsed.has_key) return null;
|
|
272
|
+
// Allowlist the outbound origins before pi is pointed at them. Everything
|
|
273
|
+
// here is attacker-influenced in principle: a committed
|
|
274
|
+
// .gateway/config.json selects the environment.
|
|
275
|
+
if (!isAllowedGatewayUrl(parsed.anthropic_base_url)) return null;
|
|
276
|
+
if (!isAllowedGatewayUrl(parsed.openai_base_url)) return null;
|
|
277
|
+
return parsed;
|
|
278
|
+
} catch {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Accept only TLS gateway origins, plus plain-HTTP loopback so a local
|
|
285
|
+
* Bifrost can be developed against. Rejects any other scheme or host, which
|
|
286
|
+
* keeps a malformed or hostile config from redirecting pi's traffic — and the
|
|
287
|
+
* credential that goes with it — somewhere else.
|
|
288
|
+
*/
|
|
289
|
+
export function isAllowedGatewayUrl(raw: string): boolean {
|
|
290
|
+
let url: URL;
|
|
291
|
+
try {
|
|
292
|
+
url = new URL(raw);
|
|
293
|
+
} catch {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
if (url.protocol === "https:") return true;
|
|
297
|
+
const loopback =
|
|
298
|
+
url.hostname === "localhost" ||
|
|
299
|
+
url.hostname === "127.0.0.1" ||
|
|
300
|
+
url.hostname === "::1";
|
|
301
|
+
return url.protocol === "http:" && loopback;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function run(args: string[]): string | null {
|
|
305
|
+
try {
|
|
306
|
+
return execFileSync(CLI, args, {
|
|
307
|
+
encoding: "utf8",
|
|
308
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
309
|
+
// The models call reaches the gateway and may refresh the catalog.
|
|
310
|
+
timeout: 30_000,
|
|
311
|
+
maxBuffer: 8 << 20,
|
|
312
|
+
}).trim();
|
|
313
|
+
} catch {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { afterEach, describe, expect, mock, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
const RESOLUTION = {
|
|
4
|
+
env: "prod",
|
|
5
|
+
root: "/repo",
|
|
6
|
+
anthropic_base_url: "https://gw.example/anthropic",
|
|
7
|
+
openai_base_url: "https://gw.example/openai/v1",
|
|
8
|
+
has_key: true,
|
|
9
|
+
headers: { "X-Org-Route": "prod" },
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Shape of one model as `gateway-cli models` reports it. */
|
|
13
|
+
type CliModel = {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
context_window: number;
|
|
17
|
+
max_output_tokens: number;
|
|
18
|
+
cost?: {
|
|
19
|
+
input: number;
|
|
20
|
+
output: number;
|
|
21
|
+
cache_read: number;
|
|
22
|
+
cache_write: number;
|
|
23
|
+
};
|
|
24
|
+
limits_source: string;
|
|
25
|
+
cost_source?: string;
|
|
26
|
+
/** Reasoning capability and applicable efforts, from the catalog. */
|
|
27
|
+
reasoning?: boolean;
|
|
28
|
+
reasoning_efforts?: string[];
|
|
29
|
+
reasoning_budget_min?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function cliPayload(models: CliModel[]): string {
|
|
33
|
+
return JSON.stringify({
|
|
34
|
+
wire: "openai-completions",
|
|
35
|
+
env: "prod",
|
|
36
|
+
catalog: {
|
|
37
|
+
source: "https://models.dev/api.json",
|
|
38
|
+
fetched: "2026-01-01T00:00:00Z",
|
|
39
|
+
},
|
|
40
|
+
models,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const HAIKU: CliModel = {
|
|
45
|
+
id: "claude-haiku-4-5-20251001",
|
|
46
|
+
name: "Claude Haiku 4.5",
|
|
47
|
+
context_window: 200000,
|
|
48
|
+
max_output_tokens: 64000,
|
|
49
|
+
cost: { input: 1, output: 5, cache_read: 0.1, cache_write: 1.25 },
|
|
50
|
+
limits_source: "gateway",
|
|
51
|
+
cost_source: "catalog",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const NOVA: CliModel = {
|
|
55
|
+
id: "bedrock/amazon.nova-lite-v1:0",
|
|
56
|
+
name: "Nova Lite",
|
|
57
|
+
context_window: 300000,
|
|
58
|
+
max_output_tokens: 8192,
|
|
59
|
+
cost: { input: 0.06, output: 0.24, cache_read: 0.015, cache_write: 0 },
|
|
60
|
+
limits_source: "catalog",
|
|
61
|
+
cost_source: "catalog",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** A model nothing could price. */
|
|
65
|
+
const UNPRICED: CliModel = {
|
|
66
|
+
id: "bedrock/deepseek.v3.1",
|
|
67
|
+
name: "deepseek.v3.1 (bedrock)",
|
|
68
|
+
context_window: 200000,
|
|
69
|
+
max_output_tokens: 4096,
|
|
70
|
+
limits_source: "floor",
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Replace node:child_process before importing the extension under test. */
|
|
74
|
+
function mockCLI(responses: Record<string, string>) {
|
|
75
|
+
const calls: string[] = [];
|
|
76
|
+
mock.module("node:child_process", () => ({
|
|
77
|
+
execFileSync: (_bin: string, args: string[]) => {
|
|
78
|
+
const cmd = args.join(" ");
|
|
79
|
+
calls.push(cmd);
|
|
80
|
+
if (!(cmd in responses)) throw new Error(`command failed: ${cmd}`);
|
|
81
|
+
return responses[cmd];
|
|
82
|
+
},
|
|
83
|
+
}));
|
|
84
|
+
return calls;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const OK_CLI = {
|
|
88
|
+
"resolve --json": JSON.stringify(RESOLUTION),
|
|
89
|
+
"models --wire=anthropic": cliPayload([HAIKU]),
|
|
90
|
+
"models --wire=openai": cliPayload([NOVA]),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
type Registered = { id: string; config: any };
|
|
94
|
+
|
|
95
|
+
async function runExtension(): Promise<Registered[]> {
|
|
96
|
+
const registered: Registered[] = [];
|
|
97
|
+
const pi = {
|
|
98
|
+
registerProvider: (id: string, config: any) =>
|
|
99
|
+
registered.push({ id, config }),
|
|
100
|
+
};
|
|
101
|
+
// Fresh import each time so the mocked module is picked up.
|
|
102
|
+
const mod = await import(`./index.js?${Math.random()}`);
|
|
103
|
+
await mod.default(pi as never);
|
|
104
|
+
return registered;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
afterEach(() => {
|
|
108
|
+
mock.restore();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("provider registration", () => {
|
|
112
|
+
test("registers both wire surfaces with a command-backed key", async () => {
|
|
113
|
+
mockCLI(OK_CLI);
|
|
114
|
+
const registered = await runExtension();
|
|
115
|
+
expect(registered.map((r) => r.id).sort()).toEqual([
|
|
116
|
+
"bifrost-anthropic",
|
|
117
|
+
"bifrost-openai",
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
const anthropic = registered.find((r) => r.id === "bifrost-anthropic")!;
|
|
121
|
+
expect(anthropic.config.api).toBe("anthropic-messages");
|
|
122
|
+
expect(anthropic.config.baseUrl).toBe("https://gw.example/anthropic");
|
|
123
|
+
expect(anthropic.config.models[0].id).toBe("claude-haiku-4-5-20251001");
|
|
124
|
+
|
|
125
|
+
const openai = registered.find((r) => r.id === "bifrost-openai")!;
|
|
126
|
+
expect(openai.config.api).toBe("openai-completions");
|
|
127
|
+
expect(openai.config.models[0].id).toBe("bedrock/amazon.nova-lite-v1:0");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("asks the CLI per wire, so each surface gets its own id spelling", async () => {
|
|
131
|
+
const calls = mockCLI(OK_CLI);
|
|
132
|
+
await runExtension();
|
|
133
|
+
expect(calls).toContain("models --wire=anthropic");
|
|
134
|
+
expect(calls).toContain("models --wire=openai");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("never reads the key: model discovery goes through the CLI", async () => {
|
|
138
|
+
// The CLI holds the credential and returns only non-secret metadata, so
|
|
139
|
+
// `key print` must not be invoked at startup at all.
|
|
140
|
+
const calls = mockCLI(OK_CLI);
|
|
141
|
+
const registered = await runExtension();
|
|
142
|
+
expect(calls).not.toContain("key print");
|
|
143
|
+
for (const r of registered) {
|
|
144
|
+
expect(r.config.apiKey).toStartWith("!");
|
|
145
|
+
expect(r.config.apiKey).toContain("key print");
|
|
146
|
+
expect(JSON.stringify(r.config)).not.toContain("sk-bf-");
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("passes gateway limits through unchanged", async () => {
|
|
151
|
+
mockCLI(OK_CLI);
|
|
152
|
+
const registered = await runExtension();
|
|
153
|
+
const model = registered.find((r) => r.id === "bifrost-anthropic")!.config
|
|
154
|
+
.models[0];
|
|
155
|
+
expect(model.contextWindow).toBe(200000);
|
|
156
|
+
expect(model.maxTokens).toBe(64000);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("maps snake_case cache rates onto pi's camelCase cost", async () => {
|
|
160
|
+
mockCLI(OK_CLI);
|
|
161
|
+
const registered = await runExtension();
|
|
162
|
+
const model = registered.find((r) => r.id === "bifrost-anthropic")!.config
|
|
163
|
+
.models[0];
|
|
164
|
+
expect(model.cost).toEqual({
|
|
165
|
+
input: 1,
|
|
166
|
+
output: 5,
|
|
167
|
+
cacheRead: 0.1,
|
|
168
|
+
cacheWrite: 1.25,
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("registers nothing when gateway-cli is missing", async () => {
|
|
173
|
+
mockCLI({});
|
|
174
|
+
expect(await runExtension()).toEqual([]);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("registers nothing when the project has no key yet", async () => {
|
|
178
|
+
mockCLI({
|
|
179
|
+
"resolve --json": JSON.stringify({ ...RESOLUTION, has_key: false }),
|
|
180
|
+
});
|
|
181
|
+
expect(await runExtension()).toEqual([]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("skips a surface the CLI cannot list", async () => {
|
|
185
|
+
mockCLI({
|
|
186
|
+
"resolve --json": JSON.stringify(RESOLUTION),
|
|
187
|
+
"models --wire=anthropic": cliPayload([HAIKU]),
|
|
188
|
+
// openai wire omitted: the command fails
|
|
189
|
+
});
|
|
190
|
+
const registered = await runExtension();
|
|
191
|
+
expect(registered.map((r) => r.id)).toEqual(["bifrost-anthropic"]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("registers nothing when the CLI emits unparseable output", async () => {
|
|
195
|
+
mockCLI({
|
|
196
|
+
"resolve --json": JSON.stringify(RESOLUTION),
|
|
197
|
+
"models --wire=anthropic": "not json",
|
|
198
|
+
"models --wire=openai": "not json",
|
|
199
|
+
});
|
|
200
|
+
expect(await runExtension()).toEqual([]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe("unpriced models", () => {
|
|
205
|
+
test("marks the name rather than advertising a zero rate as free", async () => {
|
|
206
|
+
// Pi's model shape requires `cost` and defaults it to zeros, which renders
|
|
207
|
+
// as free. Since the number cannot say "unknown", the name must.
|
|
208
|
+
const { toPiModels } = await import("./index.js");
|
|
209
|
+
const [model] = toPiModels({ models: [UNPRICED] }, "openai-completions");
|
|
210
|
+
expect(model.name).toContain("pricing unknown");
|
|
211
|
+
expect(model.cost).toEqual({
|
|
212
|
+
input: 0,
|
|
213
|
+
output: 0,
|
|
214
|
+
cacheRead: 0,
|
|
215
|
+
cacheWrite: 0,
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("leaves a priced model's name alone", async () => {
|
|
220
|
+
const { toPiModels } = await import("./index.js");
|
|
221
|
+
const [model] = toPiModels({ models: [HAIKU] }, "openai-completions");
|
|
222
|
+
expect(model.name).toBe("Claude Haiku 4.5");
|
|
223
|
+
expect(model.name).not.toContain("pricing unknown");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("tolerates an empty or absent model list", async () => {
|
|
227
|
+
const { toPiModels } = await import("./index.js");
|
|
228
|
+
expect(toPiModels(null, "openai-completions")).toEqual([]);
|
|
229
|
+
expect(toPiModels({}, "openai-completions")).toEqual([]);
|
|
230
|
+
expect(toPiModels({ models: [] }, "openai-completions")).toEqual([]);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("reasoning", () => {
|
|
235
|
+
const KIMI: CliModel = {
|
|
236
|
+
id: "fireworks/models/kimi-k3",
|
|
237
|
+
name: "Kimi K3",
|
|
238
|
+
context_window: 256000,
|
|
239
|
+
max_output_tokens: 32768,
|
|
240
|
+
cost: { input: 0.6, output: 2.5, cache_read: 0.1, cache_write: 0.6 },
|
|
241
|
+
limits_source: "gateway",
|
|
242
|
+
reasoning: true,
|
|
243
|
+
reasoning_efforts: ["low", "medium", "high", "max"],
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const TOGGLE_ONLY: CliModel = {
|
|
247
|
+
id: "fireworks/models/qwen3p8-max",
|
|
248
|
+
name: "Qwen3.8 Max",
|
|
249
|
+
context_window: 128000,
|
|
250
|
+
max_output_tokens: 8192,
|
|
251
|
+
limits_source: "catalog",
|
|
252
|
+
reasoning: true,
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
test("maps the full effort set onto pi's thinking levels", async () => {
|
|
256
|
+
const { toPiModels } = await import("./index.js");
|
|
257
|
+
const [model] = toPiModels({ models: [KIMI] }, "openai-completions");
|
|
258
|
+
expect(model.reasoning).toBe(true);
|
|
259
|
+
expect(model.compat).toEqual({ supportsReasoningEffort: true });
|
|
260
|
+
// Levels the model accepts map to the provider value; pi levels with no
|
|
261
|
+
// counterpart are null (hidden), not passed through by name.
|
|
262
|
+
expect(model.thinkingLevelMap).toEqual({
|
|
263
|
+
off: null,
|
|
264
|
+
minimal: null,
|
|
265
|
+
low: "low",
|
|
266
|
+
medium: "medium",
|
|
267
|
+
high: "high",
|
|
268
|
+
xhigh: null,
|
|
269
|
+
max: "max",
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("nulls pi levels a partial effort set does not cover", async () => {
|
|
274
|
+
// deepseek-v4-flash accepts only high/max: low/medium/minimal must be
|
|
275
|
+
// hidden, or pi-ai would send those strings and the provider 400s.
|
|
276
|
+
const FLASH: CliModel = {
|
|
277
|
+
...KIMI,
|
|
278
|
+
id: "fireworks/models/deepseek-v4-flash",
|
|
279
|
+
reasoning_efforts: ["high", "max"],
|
|
280
|
+
};
|
|
281
|
+
const { toPiModels } = await import("./index.js");
|
|
282
|
+
const [model] = toPiModels({ models: [FLASH] }, "openai-completions");
|
|
283
|
+
expect(model.thinkingLevelMap).toEqual({
|
|
284
|
+
off: null,
|
|
285
|
+
minimal: null,
|
|
286
|
+
low: null,
|
|
287
|
+
medium: null,
|
|
288
|
+
high: "high",
|
|
289
|
+
xhigh: null,
|
|
290
|
+
max: "max",
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("flags toggle-only reasoning without an effort map", async () => {
|
|
295
|
+
const { toPiModels } = await import("./index.js");
|
|
296
|
+
const [model] = toPiModels({ models: [TOGGLE_ONLY] }, "openai-completions");
|
|
297
|
+
expect(model.reasoning).toBe(true);
|
|
298
|
+
expect(model.thinkingLevelMap).toBeUndefined();
|
|
299
|
+
expect(model.compat).toEqual({ supportsReasoningEffort: false });
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("leaves a non-reasoning model alone", async () => {
|
|
303
|
+
const { toPiModels } = await import("./index.js");
|
|
304
|
+
const [model] = toPiModels({ models: [NOVA] }, "openai-completions");
|
|
305
|
+
expect(model.reasoning).toBe(false);
|
|
306
|
+
expect(model.thinkingLevelMap).toBeUndefined();
|
|
307
|
+
expect(model.compat).toBeUndefined();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("flags capability on the anthropic surface without an effort map", async () => {
|
|
311
|
+
// Claude models think adaptively on the anthropic-messages surface; the
|
|
312
|
+
// openai-only reasoning_effort machinery stays off there.
|
|
313
|
+
const { toPiModels } = await import("./index.js");
|
|
314
|
+
const CLAUDE: CliModel = { ...KIMI, id: "claude-sonnet-4-6" };
|
|
315
|
+
const [model] = toPiModels({ models: [CLAUDE] }, "anthropic-messages");
|
|
316
|
+
expect(model.reasoning).toBe(true);
|
|
317
|
+
expect(model.thinkingLevelMap).toBeUndefined();
|
|
318
|
+
expect(model.compat).toBeUndefined();
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe("gateway URL allowlist", () => {
|
|
323
|
+
test("accepts TLS gateways and loopback, rejects everything else", async () => {
|
|
324
|
+
const { isAllowedGatewayUrl } = await import("./index.js");
|
|
325
|
+
expect(isAllowedGatewayUrl("https://gateway.example.com/anthropic")).toBe(
|
|
326
|
+
true,
|
|
327
|
+
);
|
|
328
|
+
expect(isAllowedGatewayUrl("http://localhost:8080/openai/v1")).toBe(true);
|
|
329
|
+
expect(isAllowedGatewayUrl("http://127.0.0.1:8080")).toBe(true);
|
|
330
|
+
// Plain HTTP to a remote host would leak the key in transit.
|
|
331
|
+
expect(isAllowedGatewayUrl("http://evil.example/anthropic")).toBe(false);
|
|
332
|
+
expect(isAllowedGatewayUrl("file:///etc/passwd")).toBe(false);
|
|
333
|
+
expect(isAllowedGatewayUrl("not a url")).toBe(false);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("registers nothing when the resolver reports a non-TLS remote gateway", async () => {
|
|
337
|
+
mockCLI({
|
|
338
|
+
...OK_CLI,
|
|
339
|
+
"resolve --json": JSON.stringify({
|
|
340
|
+
...RESOLUTION,
|
|
341
|
+
anthropic_base_url: "http://evil.example/anthropic",
|
|
342
|
+
}),
|
|
343
|
+
});
|
|
344
|
+
expect(await runExtension()).toEqual([]);
|
|
345
|
+
});
|
|
346
|
+
});
|