@stablekernel/pi-bifrost 0.13.1 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +12 -2
- package/src/plugin.test.ts +101 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Pi extension that registers a [Bifrost](https://docs.getbifrost.ai) LLM gateway
|
|
|
8
8
|
- `bifrost-openai` — the OpenAI-compatible surface, every chat model the gateway serves. First-party OpenAI GPT models (not `gpt-oss`) are sent over the Responses API (`api: "openai-responses"` per model): gpt-6-class models reject chat completions through the gateway.
|
|
9
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
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; `claude-sonnet-5-5` also sets `forceAdaptiveThinking`, because pi 1.4.0 otherwise sends `thinking.type=enabled` and Bifrost returns a 400 (verified live 2026-11-25; Opus 5.5, Haiku 5.5 and Fable 5.2 work without it). Models with effort levels also set `supportsPerMessageEffort` (omp
|
|
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; `claude-sonnet-5-5` also sets `forceAdaptiveThinking`, because pi 1.4.0 otherwise sends `thinking.type=enabled` and Bifrost returns a 400 (verified live 2026-11-25; Opus 5.5, Haiku 5.5 and Fable 5.2 work without it). Models with effort levels also set `supportsPerMessageEffort` (omp); those whose id is in pi-ai 1.7.0's catalog of mid-conversation-effort Claude models (Opus 5 and 5.5, Sonnet 5.5, Haiku 5.5, Fable 5.1 and 5.2) also set `supportsMidConvoEffort` (the flag pi reads) so thinking-level changes keep the prompt cache. Cache-keeping has been verified live only on `claude-opus-5-5` through Bifrost; other models remain unverified ([G-012](../../KNOWN-GAPS.md)). Toggle-only reasoning models get no effort map. The default effort itself is pi's own thinking-level default.
|
|
12
12
|
- Adds rolling prompt-cache breakpoints for GPT-6.2 on Bedrock (`bedrock/openai.gpt-6.2-*`). Those models reuse a cached prefix only up to a content block marked `prompt_cache_breakpoint: {mode: "explicit"}`, so without markers every agent turn was a full cache write. A `before_provider_request` hook marks the last input item of each request and the item that ended the previous request; string content on a marked item is sent as a one-block `input_text` list. Other models, 6.0/6.1 included, cache implicitly and are left untouched. Verified live 2026-11-24 on `gpt-6.2-sol` through omp: cache reads per turn went from 0, 0, 0, 0 to 44.5K, 46.0K, 47.6K, 49.0K (about 97% of the prompt).
|
|
13
13
|
- Sets `user` on chat-completions requests to Fireworks models (`fireworks/*`) to a hash of the conversation's first messages, Fireworks' documented session-affinity key (Bifrost forwards it), so a session's turns tend to reach the replica that holds its cached prefix. It is a hint, not a pin: measured live 2026-11-24 on `kimi-k3.1`, follow-up turns that missed the previous prefix fell from 26% to 18% (200 turns each). The rest is Fireworks routing ([G-020](../../KNOWN-GAPS.md)). A `user` the harness already set is kept.
|
|
14
14
|
- 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.
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -59,10 +59,13 @@ type ProviderModelConfig = {
|
|
|
59
59
|
supportsReasoningEffort?: boolean;
|
|
60
60
|
supportsDeveloperRole?: boolean;
|
|
61
61
|
forceAdaptiveThinking?: boolean;
|
|
62
|
-
/** omp
|
|
62
|
+
/** omp: send effort changes as mid-conversation messages
|
|
63
63
|
* (beta mid-conversation-output-config-2026-07-01) instead of rewriting
|
|
64
|
-
* the top-level output_config.
|
|
64
|
+
* the top-level output_config. pi reads supportsMidConvoEffort instead. */
|
|
65
65
|
supportsPerMessageEffort?: boolean;
|
|
66
|
+
/** pi: same behavior under pi's name (read by the pi-ai release that ships
|
|
67
|
+
* this key, 1.7.0 verified); pi sends the beta and a fixed top-level effort. */
|
|
68
|
+
supportsMidConvoEffort?: boolean;
|
|
66
69
|
};
|
|
67
70
|
};
|
|
68
71
|
|
|
@@ -376,6 +379,7 @@ export function toPiModels(
|
|
|
376
379
|
if (api === "anthropic-messages") {
|
|
377
380
|
const compat: NonNullable<ProviderModelConfig["compat"]> = {};
|
|
378
381
|
if (map) compat.supportsPerMessageEffort = true;
|
|
382
|
+
if (map && PI_MID_CONVO_EFFORT.test(m.id)) compat.supportsMidConvoEffort = true;
|
|
379
383
|
if (ADAPTIVE_ONLY.test(m.id)) compat.forceAdaptiveThinking = true;
|
|
380
384
|
if (Object.keys(compat).length > 0) model.compat = compat;
|
|
381
385
|
}
|
|
@@ -404,6 +408,12 @@ const OPENAI_GPT = /(?:^|\/)openai[./]gpt-(?!oss)/;
|
|
|
404
408
|
* adaptive-only; verified live 2026-11-25 (Opus/Haiku 5.5, Fable 5.2 are fine). */
|
|
405
409
|
const ADAPTIVE_ONLY = /^claude-sonnet-5-5$/;
|
|
406
410
|
|
|
411
|
+
/** Claude ids pi's own catalog marks supportsMidConvoEffort (pi-ai 1.7.0
|
|
412
|
+
* providers/data/anthropic.json). Effort levels alone do not imply support:
|
|
413
|
+
* claude-fable-5, claude-sonnet-5 and 4.x ids lack the flag there. */
|
|
414
|
+
const PI_MID_CONVO_EFFORT =
|
|
415
|
+
/^claude-(?:opus-5(?:-5)?|sonnet-5-5|haiku-5-5|fable-5-[12])$/;
|
|
416
|
+
|
|
407
417
|
/**
|
|
408
418
|
* Build a thinkingLevelMap from a model's advertised effort values. Each pi
|
|
409
419
|
* level maps to the same-named provider value when the model accepts it, and
|
package/src/plugin.test.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
mock,
|
|
7
7
|
test,
|
|
8
8
|
} from "bun:test";
|
|
9
|
-
import { mkdtempSync } from "node:fs";
|
|
9
|
+
import { existsSync, mkdtempSync, readFileSync } from "node:fs";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import type { CliModel } from "./index.js";
|
|
@@ -513,14 +513,113 @@ describe("reasoning", () => {
|
|
|
513
513
|
const byId = Object.fromEntries(models.map((m) => [m.id, m.compat]));
|
|
514
514
|
expect(byId["claude-sonnet-5-5"]).toEqual({
|
|
515
515
|
supportsPerMessageEffort: true,
|
|
516
|
+
supportsMidConvoEffort: true,
|
|
516
517
|
forceAdaptiveThinking: true,
|
|
517
518
|
});
|
|
518
519
|
for (const id of ["claude-opus-5-5", "claude-haiku-5-5", "claude-fable-5-2"]) {
|
|
519
|
-
expect(byId[id]).toEqual({ supportsPerMessageEffort: true });
|
|
520
|
+
expect(byId[id]).toEqual({ supportsPerMessageEffort: true, supportsMidConvoEffort: true });
|
|
520
521
|
}
|
|
521
522
|
});
|
|
522
523
|
});
|
|
523
524
|
|
|
525
|
+
// Installed-package regression: feed the registered model through the real
|
|
526
|
+
// pi-ai Anthropic payload builder (no network; onPayload aborts the call).
|
|
527
|
+
// pi-ai is not a repo dependency, so locate Pi's managed install (or
|
|
528
|
+
// PI_AI_ANTHROPIC_MESSAGES). CI has no Pi: the suite skips there unless
|
|
529
|
+
// PI_AI_REQUIRED=1, which turns a missing install into a failure.
|
|
530
|
+
const MID_CONVO_BETA = "mid-conversation-output-config-2026-07-01";
|
|
531
|
+
function piAiPath(): string {
|
|
532
|
+
if (process.env.PI_AI_ANTHROPIC_MESSAGES) return process.env.PI_AI_ANTHROPIC_MESSAGES;
|
|
533
|
+
const agent = join(process.env.HOME ?? "", ".pi/agent/install");
|
|
534
|
+
try {
|
|
535
|
+
const v = readFileSync(join(agent, "current-version"), "utf8").trim();
|
|
536
|
+
return join(agent, "releases", v, "node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js");
|
|
537
|
+
} catch {
|
|
538
|
+
return "";
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
const PI_AI = piAiPath();
|
|
542
|
+
const PI_AI_PRESENT = PI_AI !== "" && existsSync(PI_AI);
|
|
543
|
+
if (process.env.PI_AI_REQUIRED === "1") {
|
|
544
|
+
test("pi-ai install is present when required", () => {
|
|
545
|
+
expect(PI_AI_PRESENT).toBe(true);
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
describe.skipIf(!PI_AI_PRESENT)("pi-ai outbound payload on effort change", () => {
|
|
549
|
+
type Payload = {
|
|
550
|
+
betas?: string[];
|
|
551
|
+
output_config?: { effort: string };
|
|
552
|
+
messages: { role: string; output_config?: { effort: string } }[];
|
|
553
|
+
};
|
|
554
|
+
async function payload(m: { id: string; compat: object }, effort: string): Promise<Payload> {
|
|
555
|
+
// Runtime-selected path into the installed Pi; a static import cannot exist.
|
|
556
|
+
const { stream } = await import(PI_AI);
|
|
557
|
+
const model = {
|
|
558
|
+
id: m.id, name: "x", api: "anthropic-messages", provider: "bifrost",
|
|
559
|
+
baseUrl: "http://127.0.0.1:1", reasoning: true, input: ["text"],
|
|
560
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
561
|
+
contextWindow: 200000, maxTokens: 8000, compat: m.compat,
|
|
562
|
+
};
|
|
563
|
+
const ts = Date.now();
|
|
564
|
+
const context = {
|
|
565
|
+
messages: [
|
|
566
|
+
{ role: "user", content: "hi", timestamp: ts },
|
|
567
|
+
{
|
|
568
|
+
role: "assistant", content: [{ type: "text", text: "ok" }], api: "anthropic-messages",
|
|
569
|
+
provider: "bifrost", model: model.id, providerThinkingLevel: "medium",
|
|
570
|
+
usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2,
|
|
571
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
|
|
572
|
+
stopReason: "stop", timestamp: ts,
|
|
573
|
+
},
|
|
574
|
+
{ role: "user", content: "again", timestamp: ts },
|
|
575
|
+
],
|
|
576
|
+
};
|
|
577
|
+
let captured: Payload | undefined;
|
|
578
|
+
const s = stream(model, context, {
|
|
579
|
+
apiKey: "test-key", thinkingEnabled: true, effort,
|
|
580
|
+
// pi-ai is untyped here, so the payload is asserted by shape.
|
|
581
|
+
onPayload: (p: unknown) => { captured = p as Payload; throw new Error("stop"); },
|
|
582
|
+
});
|
|
583
|
+
for await (const _ of s) { /* drain */ }
|
|
584
|
+
if (!captured) throw new Error("onPayload never ran");
|
|
585
|
+
return captured;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// Registers `id` as bifrost would for an effort-capable Claude and returns
|
|
589
|
+
// the sanitized payload for the pi-ai builder.
|
|
590
|
+
async function registered(id: string, effort: string, extraCompat: object = {}) {
|
|
591
|
+
const { toPiModels } = await import("./index.js");
|
|
592
|
+
const claude: CliModel = {
|
|
593
|
+
id, name: id, context_window: 200000, max_output_tokens: 8000,
|
|
594
|
+
cost: { input: 1, output: 1, cache_read: 0, cache_write: 0 },
|
|
595
|
+
limits_source: "gateway", reasoning: true, reasoning_efforts: ["low", "medium", "high"],
|
|
596
|
+
};
|
|
597
|
+
const [m] = toPiModels({ models: [claude] }, "anthropic-messages");
|
|
598
|
+
return payload({ id, compat: { ...m.compat, ...extraCompat } }, effort);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
test("supported Claude: effort goes per message, prefix and top-level effort stay fixed", async () => {
|
|
602
|
+
const p = await registered("claude-opus-5-5", "low");
|
|
603
|
+
expect(p.betas).toContain(MID_CONVO_BETA);
|
|
604
|
+
expect(p.output_config).toEqual({ effort: "low" });
|
|
605
|
+
const markers = p.messages.filter((x) => x.role === "system").map((x) => x.output_config?.effort);
|
|
606
|
+
expect(markers).toEqual(["medium", "low"]);
|
|
607
|
+
// Same prefix, different effort: only the trailing marker differs.
|
|
608
|
+
const p2 = await registered("claude-opus-5-5", "high");
|
|
609
|
+
expect(p2.output_config).toEqual(p.output_config);
|
|
610
|
+
expect(p2.messages.slice(0, -1)).toEqual(p.messages.slice(0, -1));
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
test("effort-capable Claude outside pi's mid-convo catalog keeps top-level effort, no beta", async () => {
|
|
614
|
+
// claude-sonnet-5 has no supportsMidConvoEffort in pi's catalog; pi's catalog
|
|
615
|
+
// sets forceAdaptiveThinking for it, so it is added here to get a top-level effort.
|
|
616
|
+
const p = await registered("claude-sonnet-5", "low", { forceAdaptiveThinking: true });
|
|
617
|
+
expect(p.betas ?? []).not.toContain(MID_CONVO_BETA);
|
|
618
|
+
expect(p.messages.some((x) => x.role === "system")).toBe(false);
|
|
619
|
+
expect(p.output_config).toEqual({ effort: "low" });
|
|
620
|
+
});
|
|
621
|
+
});
|
|
622
|
+
|
|
524
623
|
describe("responses routing", () => {
|
|
525
624
|
// gpt-6-class models 400 on chat completions ("use /v1/responses"), so
|
|
526
625
|
// first-party OpenAI GPT models ride the Responses API instead.
|