@stablekernel/opencode-bifrost 0.5.4 → 0.6.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 +1 -1
- package/src/index.test.ts +313 -4
- package/src/index.ts +115 -2
- package/src/refresh.test.ts +235 -0
- package/src/refresh.ts +294 -0
package/README.md
CHANGED
|
@@ -11,6 +11,32 @@ OpenCode plugin that routes model traffic through a [Bifrost](https://docs.getbi
|
|
|
11
11
|
|
|
12
12
|
No secret is held by the plugin; the key is read at runtime.
|
|
13
13
|
|
|
14
|
+
## `/bifrost-refresh`
|
|
15
|
+
|
|
16
|
+
Registered on every startup, with or without `gateway-cli` — in a sandbox that
|
|
17
|
+
has no CLI at all the plugin registers no provider, so this command is the only
|
|
18
|
+
way to see what the gateway offers. It reconciles the model list whenever you
|
|
19
|
+
want it, without restarting OpenCode. Two modes, chosen by what is available at
|
|
20
|
+
the moment you run it:
|
|
21
|
+
|
|
22
|
+
- **`gateway-cli` present** — runs `gateway-cli sync`, which reconciles the
|
|
23
|
+
saved selection on disk, and prints its output. Once that turn ends, the
|
|
24
|
+
plugin disposes the instance so the TUI re-bootstraps and the model picker
|
|
25
|
+
re-reads the reconciled list (disposing mid-turn would cut the turn short).
|
|
26
|
+
Since the list was re-registered, the report does not tell you to restart.
|
|
27
|
+
- **`gateway-cli` gone** (sandboxed machine) — asks
|
|
28
|
+
`$BIFROST_DISCOVERY_URL` (or `$BIFROST_GATEWAY_URL`) `/v1/models` directly and
|
|
29
|
+
reports what the gateway offers versus what this session registered: new ids,
|
|
30
|
+
ids no longer offered, or "this session's list is current". **Report only —
|
|
31
|
+
nothing is written**, here or anywhere else, and no instance is disposed; the
|
|
32
|
+
text says to restart because this mode cannot re-register anything. The key
|
|
33
|
+
comes from `$BIFROST_VK`, else the CLI's own
|
|
34
|
+
`~/.config/bifrost/keys/default.env`, else the credential stored by
|
|
35
|
+
`opencode auth login`.
|
|
36
|
+
|
|
37
|
+
Either way the result arrives as a message part, never as an error: a failure
|
|
38
|
+
prints one line. The virtual key is never printed or logged.
|
|
39
|
+
|
|
14
40
|
## Datasheet supplement (off by default)
|
|
15
41
|
|
|
16
42
|
Wildcard-provider models — e.g. a fresh Fireworks launch that the CLI's
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stablekernel/opencode-bifrost",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "OpenCode plugin that routes models through the Bifrost gateway, resolving per-project virtual keys via gateway-cli.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
package/src/index.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import type { Hooks } from "@opencode-ai/plugin";
|
|
3
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
3
4
|
import { tmpdir } from "node:os";
|
|
4
5
|
import { join } from "node:path";
|
|
5
6
|
import {
|
|
@@ -13,18 +14,36 @@ import {
|
|
|
13
14
|
/** HOME as the suite found it, restored after each test. */
|
|
14
15
|
const ORIGINAL_HOME = process.env.HOME;
|
|
15
16
|
|
|
17
|
+
/** XDG_CONFIG_HOME as the suite found it, restored after each test. */
|
|
18
|
+
const ORIGINAL_XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME;
|
|
19
|
+
|
|
20
|
+
/** global fetch as the suite found it, restored after each test. */
|
|
21
|
+
const ORIGINAL_FETCH = globalThis.fetch;
|
|
22
|
+
|
|
16
23
|
beforeEach(() => {
|
|
17
24
|
// The config-hook tests run the real datasheetSupplement. Keep it
|
|
18
25
|
// deterministic and machine-independent: drop the env override and point
|
|
19
26
|
// HOME at an empty temp dir, so neither ~/.config/bifrost/datasheet.json
|
|
20
27
|
// nor the real ~/.cache/opencode cache is read or written.
|
|
21
28
|
delete process.env.GATEWAY_DATASHEET_URL;
|
|
29
|
+
// /bifrost-refresh reads its gateway and key from the environment; a real
|
|
30
|
+
// BIFROST_GATEWAY_URL in the developer's shell must not reach these tests.
|
|
31
|
+
delete process.env.BIFROST_GATEWAY_URL;
|
|
32
|
+
delete process.env.BIFROST_DISCOVERY_URL;
|
|
33
|
+
delete process.env.BIFROST_VK;
|
|
34
|
+
// The key-file lookup prefers XDG_CONFIG_HOME; CI sets it, a developer
|
|
35
|
+
// shell may too, and either would point those tests away from the temp
|
|
36
|
+
// HOME they just wrote a key into.
|
|
37
|
+
delete process.env.XDG_CONFIG_HOME;
|
|
22
38
|
process.env.HOME = mkdtempSync(join(tmpdir(), "gw-oc-home-"));
|
|
23
39
|
});
|
|
24
40
|
|
|
25
41
|
afterEach(() => {
|
|
42
|
+
globalThis.fetch = ORIGINAL_FETCH;
|
|
26
43
|
if (ORIGINAL_HOME === undefined) delete process.env.HOME;
|
|
27
44
|
else process.env.HOME = ORIGINAL_HOME;
|
|
45
|
+
if (ORIGINAL_XDG_CONFIG_HOME === undefined) delete process.env.XDG_CONFIG_HOME;
|
|
46
|
+
else process.env.XDG_CONFIG_HOME = ORIGINAL_XDG_CONFIG_HOME;
|
|
28
47
|
});
|
|
29
48
|
|
|
30
49
|
const RESOLUTION = {
|
|
@@ -79,6 +98,15 @@ const UNPRICED: CliModel = {
|
|
|
79
98
|
limits_source: "floor",
|
|
80
99
|
};
|
|
81
100
|
|
|
101
|
+
/** A model the gateway added after this session registered its list. */
|
|
102
|
+
const NEW_MODEL: CliModel = {
|
|
103
|
+
id: "fireworks/models/new-one",
|
|
104
|
+
name: "New One",
|
|
105
|
+
context_window: 128000,
|
|
106
|
+
max_output_tokens: 8192,
|
|
107
|
+
limits_source: "gateway",
|
|
108
|
+
};
|
|
109
|
+
|
|
82
110
|
/**
|
|
83
111
|
* Fake Bun shell. The plugin calls $`${CLI} ${args}`.cwd(d).quiet().text(),
|
|
84
112
|
* so the tag must return a chainable object.
|
|
@@ -106,9 +134,30 @@ const OK_SHELL = {
|
|
|
106
134
|
"models --wire=openai": cliPayload([HAIKU, NOVA]),
|
|
107
135
|
};
|
|
108
136
|
|
|
109
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Client stub for the plugin's two SDK surfaces: the stale-version log and
|
|
139
|
+
* the instance dispose /bifrost-refresh fires after a CLI run. `fail` makes
|
|
140
|
+
* dispose behave like a dead instance: it must never break the command.
|
|
141
|
+
*/
|
|
142
|
+
function fakeClient(disposed: boolean[] = [], fail = false) {
|
|
143
|
+
return {
|
|
144
|
+
app: { log: async () => undefined },
|
|
145
|
+
instance: {
|
|
146
|
+
dispose: async () => {
|
|
147
|
+
disposed.push(true);
|
|
148
|
+
if (fail) throw new Error("instance is gone");
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function load(
|
|
155
|
+
responses: Record<string, string>,
|
|
156
|
+
calls: string[] = [],
|
|
157
|
+
client: unknown = fakeClient(),
|
|
158
|
+
) {
|
|
110
159
|
const $ = fakeShell(responses, calls);
|
|
111
|
-
return BifrostGateway({ directory: "/repo",
|
|
160
|
+
return BifrostGateway({ directory: "/repo", $, client } as never);
|
|
112
161
|
}
|
|
113
162
|
|
|
114
163
|
/** The per-model shape the config hook writes into `provider.<id>.models`. */
|
|
@@ -133,7 +182,10 @@ type TestProvider = {
|
|
|
133
182
|
};
|
|
134
183
|
|
|
135
184
|
/** The opencode config object the plugin mutates, as tests read it back. */
|
|
136
|
-
type TestConfig = {
|
|
185
|
+
type TestConfig = {
|
|
186
|
+
provider: Record<string, TestProvider>;
|
|
187
|
+
command?: Record<string, { template: string; description?: string }>;
|
|
188
|
+
};
|
|
137
189
|
|
|
138
190
|
async function applyConfig(responses: Record<string, string>, seed = {}) {
|
|
139
191
|
const hooks = await load(responses);
|
|
@@ -326,6 +378,28 @@ describe("image input", () => {
|
|
|
326
378
|
expect(m.modalities).toBeUndefined();
|
|
327
379
|
expect(m.attachment).toBeUndefined();
|
|
328
380
|
});
|
|
381
|
+
|
|
382
|
+
test("registers the reported vision route from a gap-filled CLI payload", () => {
|
|
383
|
+
// Regression for #55: a selection served without image_input registered
|
|
384
|
+
// fireworks/deepseek-v4.1-flash as text-only and OpenCode silently dropped
|
|
385
|
+
// image parts. The CLI now gap-fills that flag from the catalog; the gate
|
|
386
|
+
// must honor it.
|
|
387
|
+
const models = toConfigModels({
|
|
388
|
+
models: [
|
|
389
|
+
{
|
|
390
|
+
id: "fireworks/deepseek-v4.1-flash",
|
|
391
|
+
name: "DeepSeek V4.1 Flash",
|
|
392
|
+
context_window: 1048576,
|
|
393
|
+
max_output_tokens: 393216,
|
|
394
|
+
limits_source: "catalog",
|
|
395
|
+
image_input: true,
|
|
396
|
+
},
|
|
397
|
+
],
|
|
398
|
+
});
|
|
399
|
+
const m = models["fireworks/deepseek-v4.1-flash"];
|
|
400
|
+
expect(m.attachment).toBe(true);
|
|
401
|
+
expect(m.modalities?.input).toEqual(["text", "image"]);
|
|
402
|
+
});
|
|
329
403
|
});
|
|
330
404
|
|
|
331
405
|
describe("unpriced models", () => {
|
|
@@ -507,6 +581,241 @@ describe("auth.methods", () => {
|
|
|
507
581
|
});
|
|
508
582
|
});
|
|
509
583
|
|
|
584
|
+
describe("/bifrost-refresh", () => {
|
|
585
|
+
/** Run the command hook and return the parts it pushed. */
|
|
586
|
+
async function runRefresh(
|
|
587
|
+
hooks: Hooks,
|
|
588
|
+
command = "bifrost-refresh",
|
|
589
|
+
sessionID = "ses_test",
|
|
590
|
+
) {
|
|
591
|
+
const parts: Array<{ type: string; text: string }> = [];
|
|
592
|
+
await hooks["command.execute.before"]!(
|
|
593
|
+
{ command, sessionID, arguments: "" },
|
|
594
|
+
{ parts } as never,
|
|
595
|
+
);
|
|
596
|
+
return parts;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** Deliver a session event, the way the host does once a turn ends. */
|
|
600
|
+
async function runIdle(hooks: Hooks, sessionID: string) {
|
|
601
|
+
await hooks.event!({
|
|
602
|
+
event: { type: "session.idle", properties: { sessionID } },
|
|
603
|
+
} as never);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
test("registers the command whether or not the provider registers", async () => {
|
|
607
|
+
const cfg = await applyConfig(OK_SHELL);
|
|
608
|
+
expect(cfg.command?.["bifrost-refresh"]?.description).toBe(
|
|
609
|
+
"Reconcile the Bifrost model list (gateway-cli sync), or list the " +
|
|
610
|
+
"gateway directly when the CLI is missing",
|
|
611
|
+
);
|
|
612
|
+
// The template is what the model sees if the hook cannot inject; opencode
|
|
613
|
+
// rejects a command with an empty one.
|
|
614
|
+
expect(cfg.command?.["bifrost-refresh"]?.template).toBeTruthy();
|
|
615
|
+
|
|
616
|
+
// The sandbox this command exists for: no gateway-cli means no provider,
|
|
617
|
+
// but the command must still be there to list the gateway directly.
|
|
618
|
+
const none = await applyConfig({});
|
|
619
|
+
expect(none.command?.["bifrost-refresh"]).toBeDefined();
|
|
620
|
+
expect(none.provider).toBeUndefined();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test("fetches the gateway when no provider could register", async () => {
|
|
624
|
+
const disposed: boolean[] = [];
|
|
625
|
+
// Nothing responds: no resolve, no sync, no key print.
|
|
626
|
+
const hooks = await load({}, [], fakeClient(disposed));
|
|
627
|
+
const cfg = {} as TestConfig;
|
|
628
|
+
await hooks.config!(cfg as never);
|
|
629
|
+
expect(cfg.provider).toBeUndefined();
|
|
630
|
+
|
|
631
|
+
process.env.BIFROST_GATEWAY_URL = "https://gw.example";
|
|
632
|
+
process.env.BIFROST_VK = "sk-bf-envkey";
|
|
633
|
+
const requests: Array<{ url: string; auth?: string }> = [];
|
|
634
|
+
globalThis.fetch = (async (
|
|
635
|
+
url: string,
|
|
636
|
+
init?: { headers?: Record<string, string> },
|
|
637
|
+
) => {
|
|
638
|
+
requests.push({ url, auth: init?.headers?.Authorization });
|
|
639
|
+
return {
|
|
640
|
+
ok: true,
|
|
641
|
+
status: 200,
|
|
642
|
+
json: async () => ({ data: [{ id: NEW_MODEL.id }] }),
|
|
643
|
+
};
|
|
644
|
+
}) as never;
|
|
645
|
+
|
|
646
|
+
const parts = await runRefresh(hooks);
|
|
647
|
+
expect(requests.filter((r) => r.url.endsWith("/v1/models"))).toEqual([
|
|
648
|
+
{ url: "https://gw.example/v1/models", auth: "Bearer sk-bf-envkey" },
|
|
649
|
+
]);
|
|
650
|
+
expect(parts).toHaveLength(1);
|
|
651
|
+
expect(parts[0].type).toBe("text");
|
|
652
|
+
// The engine words the rest of the no-models-registered case; what this
|
|
653
|
+
// plugin owns is that the gateway answered and its report reached the
|
|
654
|
+
// session.
|
|
655
|
+
expect(parts[0].text).toContain("bifrost models: 1 offered by the gateway:");
|
|
656
|
+
expect(disposed).toEqual([]);
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
test("leaves every other command alone", async () => {
|
|
660
|
+
const disposed: boolean[] = [];
|
|
661
|
+
const hooks = await load(OK_SHELL, [], fakeClient(disposed));
|
|
662
|
+
const parts = await runRefresh(hooks, "other-command");
|
|
663
|
+
expect(parts).toEqual([]);
|
|
664
|
+
await runIdle(hooks, "ses_test");
|
|
665
|
+
expect(disposed).toEqual([]);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
test("runs the CLI and re-bootstraps the instance once the turn ends", async () => {
|
|
669
|
+
const disposed: boolean[] = [];
|
|
670
|
+
const calls: string[] = [];
|
|
671
|
+
const hooks = await load(
|
|
672
|
+
{ ...OK_SHELL, sync: "reconciled 2 models\n" },
|
|
673
|
+
calls,
|
|
674
|
+
// Dispose failing must not cost the report.
|
|
675
|
+
fakeClient(disposed, true),
|
|
676
|
+
);
|
|
677
|
+
await hooks.config!({} as never);
|
|
678
|
+
|
|
679
|
+
const parts = await runRefresh(hooks, "bifrost-refresh", "ses_1");
|
|
680
|
+
// The CLI did the reconciling, so its own output is the whole report.
|
|
681
|
+
expect(parts).toEqual([{ type: "text", text: "reconciled 2 models" }]);
|
|
682
|
+
expect(calls).toContain("sync");
|
|
683
|
+
// Disposing inside the command would end the very turn that asked for it,
|
|
684
|
+
// so the instance waits for that session to go idle.
|
|
685
|
+
expect(disposed).toEqual([]);
|
|
686
|
+
|
|
687
|
+
await runIdle(hooks, "ses_2");
|
|
688
|
+
expect(disposed).toEqual([]);
|
|
689
|
+
|
|
690
|
+
await runIdle(hooks, "ses_1");
|
|
691
|
+
expect(disposed).toHaveLength(1);
|
|
692
|
+
|
|
693
|
+
// One dispose per refresh: a repeat idle has nothing left to do.
|
|
694
|
+
await runIdle(hooks, "ses_1");
|
|
695
|
+
expect(disposed).toHaveLength(1);
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
test("reports the gateway's list without disposing when the CLI cannot sync", async () => {
|
|
699
|
+
const disposed: boolean[] = [];
|
|
700
|
+
// Everything but `sync`: the plugin registers through the CLI, then the
|
|
701
|
+
// command finds the CLI unavailable — the sandbox this was built for.
|
|
702
|
+
const hooks = await load(
|
|
703
|
+
{
|
|
704
|
+
"resolve --json": JSON.stringify(RESOLUTION),
|
|
705
|
+
"models --wire=openai": cliPayload([HAIKU, NOVA]),
|
|
706
|
+
},
|
|
707
|
+
[],
|
|
708
|
+
fakeClient(disposed),
|
|
709
|
+
);
|
|
710
|
+
const cfg = {} as TestConfig;
|
|
711
|
+
await hooks.config!(cfg as never);
|
|
712
|
+
// Whatever this session registered is the "known" side of the diff.
|
|
713
|
+
const registeredIds = Object.keys(cfg.provider.bifrost.models);
|
|
714
|
+
// No `key print` either: the credential is OpenCode's own auth entry,
|
|
715
|
+
// which the command may only use because the loader read it first.
|
|
716
|
+
await hooks.auth!.loader!(
|
|
717
|
+
async () => ({ type: "api", key: "sk-bf-storedkey" }) as never,
|
|
718
|
+
{} as never,
|
|
719
|
+
);
|
|
720
|
+
|
|
721
|
+
process.env.BIFROST_GATEWAY_URL = "https://gw.example";
|
|
722
|
+
const requests: Array<{ url: string; auth?: string }> = [];
|
|
723
|
+
globalThis.fetch = (async (
|
|
724
|
+
url: string,
|
|
725
|
+
init?: { headers?: Record<string, string> },
|
|
726
|
+
) => {
|
|
727
|
+
requests.push({ url, auth: init?.headers?.Authorization });
|
|
728
|
+
return {
|
|
729
|
+
ok: true,
|
|
730
|
+
status: 200,
|
|
731
|
+
json: async () => ({
|
|
732
|
+
data: [
|
|
733
|
+
...registeredIds.map((id) => ({ id })),
|
|
734
|
+
{ id: NEW_MODEL.id },
|
|
735
|
+
],
|
|
736
|
+
}),
|
|
737
|
+
};
|
|
738
|
+
}) as never;
|
|
739
|
+
|
|
740
|
+
const parts = await runRefresh(hooks);
|
|
741
|
+
expect(requests.filter((r) => r.url.endsWith("/v1/models"))).toEqual([
|
|
742
|
+
{ url: "https://gw.example/v1/models", auth: "Bearer sk-bf-storedkey" },
|
|
743
|
+
]);
|
|
744
|
+
expect(parts).toEqual([
|
|
745
|
+
{
|
|
746
|
+
type: "text",
|
|
747
|
+
text:
|
|
748
|
+
`bifrost models: ${registeredIds.length + 1} offered by the gateway.\n` +
|
|
749
|
+
"New since this session started: fireworks/models/new-one — restart to use them.",
|
|
750
|
+
},
|
|
751
|
+
]);
|
|
752
|
+
// Nothing was re-registered, so there is nothing for a restart to pick up
|
|
753
|
+
// — and nothing to re-bootstrap for either.
|
|
754
|
+
expect(disposed).toEqual([]);
|
|
755
|
+
await runIdle(hooks, "ses_test");
|
|
756
|
+
expect(disposed).toEqual([]);
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test("falls back to the CLI's own key file", async () => {
|
|
760
|
+
// A sandbox can hold the key file without the binary: the environment has
|
|
761
|
+
// nothing and the auth store was never used.
|
|
762
|
+
const dir = join(process.env.HOME!, ".config", "bifrost", "keys");
|
|
763
|
+
mkdirSync(dir, { recursive: true });
|
|
764
|
+
writeFileSync(
|
|
765
|
+
join(dir, "default.env"),
|
|
766
|
+
"export BIFROST_VK=sk-bf-keyfile\n",
|
|
767
|
+
);
|
|
768
|
+
const hooks = await load({}, [], fakeClient());
|
|
769
|
+
process.env.BIFROST_GATEWAY_URL = "https://gw.example";
|
|
770
|
+
const requests: Array<{ url: string; auth?: string }> = [];
|
|
771
|
+
globalThis.fetch = (async (
|
|
772
|
+
url: string,
|
|
773
|
+
init?: { headers?: Record<string, string> },
|
|
774
|
+
) => {
|
|
775
|
+
requests.push({ url, auth: init?.headers?.Authorization });
|
|
776
|
+
return {
|
|
777
|
+
ok: true,
|
|
778
|
+
status: 200,
|
|
779
|
+
json: async () => ({ data: [{ id: NEW_MODEL.id }] }),
|
|
780
|
+
};
|
|
781
|
+
}) as never;
|
|
782
|
+
|
|
783
|
+
const parts = await runRefresh(hooks);
|
|
784
|
+
expect(requests.filter((r) => r.url.endsWith("/v1/models"))).toEqual([
|
|
785
|
+
{ url: "https://gw.example/v1/models", auth: "Bearer sk-bf-keyfile" },
|
|
786
|
+
]);
|
|
787
|
+
expect(parts).toEqual([
|
|
788
|
+
{
|
|
789
|
+
type: "text",
|
|
790
|
+
text:
|
|
791
|
+
"bifrost models: 1 offered by the gateway: fireworks/models/new-one\n" +
|
|
792
|
+
"None registered in this session — install gateway-cli (or point GATEWAY_CLI_BIN at it) and restart to use them.",
|
|
793
|
+
},
|
|
794
|
+
]);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
test("turns a failed refresh into one readable line", async () => {
|
|
798
|
+
const disposed: boolean[] = [];
|
|
799
|
+
const hooks = await load({}, [], fakeClient(disposed));
|
|
800
|
+
process.env.BIFROST_GATEWAY_URL = "https://gw.example";
|
|
801
|
+
process.env.BIFROST_VK = "sk-bf-envkey";
|
|
802
|
+
globalThis.fetch = (async () => {
|
|
803
|
+
throw new Error("socket hang up");
|
|
804
|
+
}) as never;
|
|
805
|
+
|
|
806
|
+
const parts = await runRefresh(hooks);
|
|
807
|
+
expect(parts).toEqual([
|
|
808
|
+
{
|
|
809
|
+
type: "text",
|
|
810
|
+
text:
|
|
811
|
+
"bifrost models: could not list models from " +
|
|
812
|
+
"https://gw.example/v1/models",
|
|
813
|
+
},
|
|
814
|
+
]);
|
|
815
|
+
expect(disposed).toEqual([]);
|
|
816
|
+
});
|
|
817
|
+
});
|
|
818
|
+
|
|
510
819
|
describe("gateway URL allowlist", () => {
|
|
511
820
|
test("accepts TLS and loopback, rejects everything else", () => {
|
|
512
821
|
expect(isAllowedGatewayUrl("https://gateway.example.com/openai/v1")).toBe(
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import { datasheetSupplement, mergeModels } from "./datasheet.js";
|
|
5
|
+
import {
|
|
6
|
+
REFRESH_COMMAND,
|
|
7
|
+
keyFileValue,
|
|
8
|
+
refresh,
|
|
9
|
+
refreshReport,
|
|
10
|
+
} from "./refresh.js";
|
|
4
11
|
|
|
5
12
|
/** Provider id registered with OpenCode. */
|
|
6
13
|
const PROVIDER_ID = "bifrost";
|
|
@@ -153,6 +160,24 @@ function datasheetCacheDir(): string | null {
|
|
|
153
160
|
return base ? `${base}/opencode` : null;
|
|
154
161
|
}
|
|
155
162
|
|
|
163
|
+
/**
|
|
164
|
+
* The virtual key from the CLI's own default key file. Only the CLI-less
|
|
165
|
+
* refresh path reads it: normal provider registration never needs the key,
|
|
166
|
+
* and the value stays in memory for the one request header that uses it.
|
|
167
|
+
*/
|
|
168
|
+
function readKeyFile(): string | null {
|
|
169
|
+
const home = process.env.HOME || homedir();
|
|
170
|
+
const base =
|
|
171
|
+
process.env.XDG_CONFIG_HOME ?? (home ? `${home}/.config` : null);
|
|
172
|
+
if (!base) return null;
|
|
173
|
+
try {
|
|
174
|
+
const path = `${base}/bifrost/keys/default.env`;
|
|
175
|
+
return keyFileValue(readFileSync(path, "utf8"));
|
|
176
|
+
} catch {
|
|
177
|
+
return null; // absent, unreadable, or not a key file
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
156
181
|
/**
|
|
157
182
|
* The latest published version, from a fresh-enough cache file when present
|
|
158
183
|
* and the npm registry otherwise. Null means unknown — offline, timeout, or
|
|
@@ -245,6 +270,19 @@ async function announceIfStale(log: (message: string) => void): Promise<void> {
|
|
|
245
270
|
}
|
|
246
271
|
|
|
247
272
|
export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
273
|
+
/** Ids the config hook registered; the known side of a refresh diff. */
|
|
274
|
+
let registered: string[] = [];
|
|
275
|
+
|
|
276
|
+
/** The credential OpenCode holds, remembered by the auth loader below. */
|
|
277
|
+
let storedCredential: string | null = null;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Session whose turn ends with an instance dispose. Disposing during the
|
|
281
|
+
* command itself tears down that very turn — the reply never lands — so the
|
|
282
|
+
* event hook below waits for the session to go idle instead.
|
|
283
|
+
*/
|
|
284
|
+
let disposeAfterSession: string | null = null;
|
|
285
|
+
|
|
248
286
|
async function cli(args: string[]): Promise<string | null> {
|
|
249
287
|
try {
|
|
250
288
|
const out = await $`${CLI} ${args}`.cwd(directory).quiet().text();
|
|
@@ -311,6 +349,22 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
311
349
|
.catch(() => {});
|
|
312
350
|
}).catch(() => {});
|
|
313
351
|
|
|
352
|
+
// Registered before the provider check on purpose: the case this
|
|
353
|
+
// command exists for is the sandbox with no gateway-cli at all, where
|
|
354
|
+
// resolve() returns null and nothing else would be registered. That
|
|
355
|
+
// session has the fewest other ways to learn what the gateway offers.
|
|
356
|
+
// The template only reaches the model if the command hook below cannot
|
|
357
|
+
// inject its report.
|
|
358
|
+
(cfg.command ??= {})[REFRESH_COMMAND] = {
|
|
359
|
+
description:
|
|
360
|
+
"Reconcile the Bifrost model list (gateway-cli sync), or list " +
|
|
361
|
+
"the gateway directly when the CLI is missing",
|
|
362
|
+
template:
|
|
363
|
+
"Refresh the Bifrost model list: run `gateway-cli sync` if it is " +
|
|
364
|
+
"installed, otherwise list the gateway's /v1/models directly, " +
|
|
365
|
+
"and report what changed.",
|
|
366
|
+
};
|
|
367
|
+
|
|
314
368
|
const info = await resolve();
|
|
315
369
|
if (!info) return;
|
|
316
370
|
|
|
@@ -319,6 +373,10 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
319
373
|
).provider ??= {});
|
|
320
374
|
const existing: ProviderEntry = providers[PROVIDER_ID] ?? {};
|
|
321
375
|
const models = toConfigModels(await listModels(), info.reasoning_effort);
|
|
376
|
+
const merged = { ...models, ...(existing.models ?? {}) };
|
|
377
|
+
// What this session is about to register; /bifrost-refresh diffs the
|
|
378
|
+
// gateway's live listing against exactly this.
|
|
379
|
+
registered = Object.keys(merged);
|
|
322
380
|
|
|
323
381
|
providers[PROVIDER_ID] = {
|
|
324
382
|
npm: "@ai-sdk/openai-compatible",
|
|
@@ -332,7 +390,7 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
332
390
|
: {}),
|
|
333
391
|
...(existing.options ?? {}),
|
|
334
392
|
},
|
|
335
|
-
models:
|
|
393
|
+
models: merged,
|
|
336
394
|
};
|
|
337
395
|
},
|
|
338
396
|
|
|
@@ -355,8 +413,12 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
355
413
|
stored &&
|
|
356
414
|
stored.type === "api" &&
|
|
357
415
|
stored.key.startsWith("sk-bf-")
|
|
358
|
-
)
|
|
416
|
+
) {
|
|
359
417
|
apiKey = stored.key;
|
|
418
|
+
// Kept for /bifrost-refresh, whose CLI-less path has no
|
|
419
|
+
// getAuth() of its own.
|
|
420
|
+
storedCredential = stored.key;
|
|
421
|
+
}
|
|
360
422
|
}
|
|
361
423
|
if (!apiKey) return {};
|
|
362
424
|
return {
|
|
@@ -383,6 +445,57 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
383
445
|
},
|
|
384
446
|
],
|
|
385
447
|
},
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* `/bifrost-refresh`: reconcile the model list, or report the gateway's
|
|
451
|
+
* when gateway-cli is not installed. The result rides in as a text part,
|
|
452
|
+
* so the user sees it without the model having to relay it.
|
|
453
|
+
*/
|
|
454
|
+
"command.execute.before": async (input, output) => {
|
|
455
|
+
if (input.command !== REFRESH_COMMAND) return;
|
|
456
|
+
let text: string;
|
|
457
|
+
try {
|
|
458
|
+
const result = await refresh({
|
|
459
|
+
known: registered,
|
|
460
|
+
exec: cli,
|
|
461
|
+
readKeyFile,
|
|
462
|
+
storedKey: async () => storedCredential,
|
|
463
|
+
env: process.env,
|
|
464
|
+
});
|
|
465
|
+
text = refreshReport(result, result.source === "cli");
|
|
466
|
+
if (result.source === "cli") {
|
|
467
|
+
// The CLI reconciled on disk, so this instance's picker is stale and
|
|
468
|
+
// a re-bootstrap is what shows the new list. Not here, though: this
|
|
469
|
+
// hook runs inside the command's own turn, and disposing mid-turn
|
|
470
|
+
// ends it before the reply is produced.
|
|
471
|
+
disposeAfterSession = input.sessionID;
|
|
472
|
+
}
|
|
473
|
+
} catch {
|
|
474
|
+
text = "bifrost models: refresh failed.";
|
|
475
|
+
}
|
|
476
|
+
// Ids are assigned by the host when it builds the message part, so the
|
|
477
|
+
// input shape is narrower than the Part union this array is typed as.
|
|
478
|
+
output.parts.push(
|
|
479
|
+
{ type: "text", text } as (typeof output.parts)[number],
|
|
480
|
+
);
|
|
481
|
+
},
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Runs the deferred dispose once the session that asked for a refresh has
|
|
485
|
+
* gone idle — the turn is over, so re-bootstrapping can no longer cut it
|
|
486
|
+
* short.
|
|
487
|
+
*/
|
|
488
|
+
event: async ({ event }) => {
|
|
489
|
+
if (event.type !== "session.idle") return;
|
|
490
|
+
if (event.properties.sessionID !== disposeAfterSession) return;
|
|
491
|
+
disposeAfterSession = null;
|
|
492
|
+
// Fire-and-forget: the report is already in front of the user.
|
|
493
|
+
try {
|
|
494
|
+
void client.instance.dispose().catch(() => {});
|
|
495
|
+
} catch {
|
|
496
|
+
// Nothing to do; the instance was already gone.
|
|
497
|
+
}
|
|
498
|
+
},
|
|
386
499
|
};
|
|
387
500
|
};
|
|
388
501
|
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
diffModels,
|
|
4
|
+
gatewayFromEnv,
|
|
5
|
+
keyFileValue,
|
|
6
|
+
liveModelIds,
|
|
7
|
+
refresh,
|
|
8
|
+
refreshReport,
|
|
9
|
+
type FetchLike,
|
|
10
|
+
} from "./refresh.js";
|
|
11
|
+
|
|
12
|
+
const KEY = "sk-bf-testkey";
|
|
13
|
+
|
|
14
|
+
/** A fetch stub that records calls and answers with the given body. */
|
|
15
|
+
function stubFetch(body: unknown, ok = true) {
|
|
16
|
+
const calls: { url: string; headers?: Record<string, string> }[] = [];
|
|
17
|
+
const fetchImpl: FetchLike = async (url, init) => {
|
|
18
|
+
calls.push({ url, headers: init?.headers });
|
|
19
|
+
return { ok, status: ok ? 200 : 403, json: async () => body };
|
|
20
|
+
};
|
|
21
|
+
return { fetchImpl, calls };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("gatewayFromEnv", () => {
|
|
25
|
+
test("prefers the discovery host and takes only a virtual key", () => {
|
|
26
|
+
expect(
|
|
27
|
+
gatewayFromEnv({
|
|
28
|
+
BIFROST_DISCOVERY_URL: "https://discovery.example/",
|
|
29
|
+
BIFROST_GATEWAY_URL: "https://traffic.example",
|
|
30
|
+
BIFROST_VK: KEY,
|
|
31
|
+
}),
|
|
32
|
+
).toEqual({ baseUrl: "https://discovery.example", apiKey: KEY });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("falls back to the traffic host and reports a missing key as null", () => {
|
|
36
|
+
expect(gatewayFromEnv({ BIFROST_GATEWAY_URL: "https://gw.example" })).toEqual({
|
|
37
|
+
baseUrl: "https://gw.example",
|
|
38
|
+
apiKey: null,
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("a non-key value never becomes an Authorization header", () => {
|
|
43
|
+
const target = gatewayFromEnv({ BIFROST_GATEWAY_URL: "https://gw.example", BIFROST_VK: "hunter2" });
|
|
44
|
+
expect(target?.apiKey).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("plain HTTP is allowed for loopback only", () => {
|
|
48
|
+
expect(gatewayFromEnv({ BIFROST_GATEWAY_URL: "http://127.0.0.1:8080" })?.baseUrl).toBe(
|
|
49
|
+
"http://127.0.0.1:8080",
|
|
50
|
+
);
|
|
51
|
+
expect(gatewayFromEnv({ BIFROST_GATEWAY_URL: "http://gw.example" })).toBeNull();
|
|
52
|
+
expect(gatewayFromEnv({ BIFROST_GATEWAY_URL: "not a url" })).toBeNull();
|
|
53
|
+
expect(gatewayFromEnv({})).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("keyFileValue", () => {
|
|
58
|
+
test("reads the CLI's export line and nothing else", () => {
|
|
59
|
+
expect(keyFileValue(`# comment\nrequire_x=1\nexport BIFROST_VK=${KEY}\n`)).toBe(KEY);
|
|
60
|
+
expect(keyFileValue(`export BIFROST_VK="sk-bf-quoted"\n`)).toBe("sk-bf-quoted");
|
|
61
|
+
expect(keyFileValue("export OTHER_VK=sk-bf-nope\n")).toBeNull();
|
|
62
|
+
expect(keyFileValue("export BIFROST_VK=\n")).toBeNull();
|
|
63
|
+
expect(keyFileValue("export BIFROST_VK=not-a-vk\n")).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("liveModelIds and diffModels", () => {
|
|
68
|
+
test("ids come from a data array and junk is ignored", () => {
|
|
69
|
+
expect(liveModelIds({ data: [{ id: "a" }, { id: 7 }, {}, "x"] })).toEqual(["a"]);
|
|
70
|
+
expect(liveModelIds(null)).toEqual([]);
|
|
71
|
+
expect(liveModelIds({ data: "no" })).toEqual([]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("diff is membership, both directions, sorted", () => {
|
|
75
|
+
expect(diffModels(["a", "gone"], ["a", "new2", "new1"])).toEqual({
|
|
76
|
+
added: ["new1", "new2"],
|
|
77
|
+
removed: ["gone"],
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("refresh", () => {
|
|
83
|
+
test("the CLI wins when it answers, and no request is made", async () => {
|
|
84
|
+
const { fetchImpl, calls } = stubFetch({});
|
|
85
|
+
const result = await refresh({
|
|
86
|
+
known: ["a"],
|
|
87
|
+
exec: async () => "model selection updated (openai): added 1 new model(s): b",
|
|
88
|
+
fetchImpl,
|
|
89
|
+
env: { BIFROST_GATEWAY_URL: "https://gw.example", BIFROST_VK: KEY },
|
|
90
|
+
});
|
|
91
|
+
expect(result.source).toBe("cli");
|
|
92
|
+
expect(result.cliOutput).toContain("added 1 new model(s)");
|
|
93
|
+
expect(calls).toHaveLength(0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("without the CLI it lists the gateway directly and diffs the session", async () => {
|
|
97
|
+
const { fetchImpl, calls } = stubFetch({ data: [{ id: "a" }, { id: "b" }] });
|
|
98
|
+
const result = await refresh({
|
|
99
|
+
known: ["a", "old"],
|
|
100
|
+
exec: async () => null,
|
|
101
|
+
fetchImpl,
|
|
102
|
+
env: { BIFROST_GATEWAY_URL: "https://gw.example", BIFROST_VK: KEY },
|
|
103
|
+
});
|
|
104
|
+
expect(result).toEqual({
|
|
105
|
+
source: "gateway",
|
|
106
|
+
live: ["a", "b"],
|
|
107
|
+
added: ["b"],
|
|
108
|
+
removed: ["old"],
|
|
109
|
+
knownCount: 2,
|
|
110
|
+
});
|
|
111
|
+
expect(calls[0]?.url).toBe("https://gw.example/v1/models");
|
|
112
|
+
expect(calls[0]?.headers?.Authorization).toBe(`Bearer ${KEY}`);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("a key file stands in for the environment key", async () => {
|
|
116
|
+
const { fetchImpl } = stubFetch({ data: [{ id: "a" }] });
|
|
117
|
+
const result = await refresh({
|
|
118
|
+
known: ["a"],
|
|
119
|
+
exec: async () => null,
|
|
120
|
+
readKeyFile: () => KEY,
|
|
121
|
+
fetchImpl,
|
|
122
|
+
env: { BIFROST_GATEWAY_URL: "https://gw.example" },
|
|
123
|
+
});
|
|
124
|
+
expect(result.error).toBeUndefined();
|
|
125
|
+
expect(result.live).toEqual(["a"]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("says what is missing instead of failing silently", async () => {
|
|
129
|
+
const noUrl = await refresh({ known: [], exec: async () => null, env: {} });
|
|
130
|
+
expect(noUrl.error).toContain("BIFROST_GATEWAY_URL");
|
|
131
|
+
|
|
132
|
+
const noKey = await refresh({
|
|
133
|
+
known: [],
|
|
134
|
+
exec: async () => null,
|
|
135
|
+
env: { BIFROST_GATEWAY_URL: "https://gw.example" },
|
|
136
|
+
});
|
|
137
|
+
expect(noKey.error).toContain("BIFROST_VK");
|
|
138
|
+
|
|
139
|
+
const { fetchImpl } = stubFetch({}, false);
|
|
140
|
+
const rejected = await refresh({
|
|
141
|
+
known: [],
|
|
142
|
+
exec: async () => null,
|
|
143
|
+
fetchImpl,
|
|
144
|
+
env: { BIFROST_GATEWAY_URL: "https://gw.example", BIFROST_VK: KEY },
|
|
145
|
+
});
|
|
146
|
+
expect(rejected.error).toContain("could not list models");
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("refreshReport", () => {
|
|
151
|
+
test("the CLI's own output is the report", () => {
|
|
152
|
+
expect(
|
|
153
|
+
refreshReport({
|
|
154
|
+
source: "cli",
|
|
155
|
+
live: [],
|
|
156
|
+
added: [],
|
|
157
|
+
removed: [],
|
|
158
|
+
knownCount: 1,
|
|
159
|
+
cliOutput: "done\n",
|
|
160
|
+
}),
|
|
161
|
+
).toBe("done");
|
|
162
|
+
expect(
|
|
163
|
+
refreshReport({
|
|
164
|
+
source: "cli",
|
|
165
|
+
live: [],
|
|
166
|
+
added: [],
|
|
167
|
+
removed: [],
|
|
168
|
+
knownCount: 1,
|
|
169
|
+
cliOutput: " ",
|
|
170
|
+
}),
|
|
171
|
+
).toContain("nothing to change");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("errors and diffs read as one line each", () => {
|
|
175
|
+
expect(
|
|
176
|
+
refreshReport({
|
|
177
|
+
source: "gateway",
|
|
178
|
+
live: [],
|
|
179
|
+
added: [],
|
|
180
|
+
removed: [],
|
|
181
|
+
knownCount: 0,
|
|
182
|
+
error: "no virtual key",
|
|
183
|
+
}),
|
|
184
|
+
).toBe("bifrost models: no virtual key");
|
|
185
|
+
|
|
186
|
+
const added = refreshReport({
|
|
187
|
+
source: "gateway",
|
|
188
|
+
live: ["a", "b"],
|
|
189
|
+
added: ["b"],
|
|
190
|
+
removed: [],
|
|
191
|
+
knownCount: 1,
|
|
192
|
+
});
|
|
193
|
+
expect(added).toContain("2 offered by the gateway");
|
|
194
|
+
expect(added).toContain("restart to use them");
|
|
195
|
+
|
|
196
|
+
const applied = refreshReport(
|
|
197
|
+
{ source: "gateway", live: ["a", "b"], added: ["b"], removed: [], knownCount: 1 },
|
|
198
|
+
true,
|
|
199
|
+
);
|
|
200
|
+
expect(applied).toContain("Added this session: b");
|
|
201
|
+
expect(applied).not.toContain("restart");
|
|
202
|
+
|
|
203
|
+
expect(
|
|
204
|
+
refreshReport({ source: "gateway", live: ["a"], added: [], removed: [], knownCount: 1 }),
|
|
205
|
+
).toContain("current");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a session with nothing registered is not told every model is new", () => {
|
|
209
|
+
const report = refreshReport({
|
|
210
|
+
source: "gateway",
|
|
211
|
+
live: ["a", "b", "c"],
|
|
212
|
+
added: ["a", "b", "c"],
|
|
213
|
+
removed: [],
|
|
214
|
+
knownCount: 0,
|
|
215
|
+
});
|
|
216
|
+
expect(report).toContain("None registered in this session");
|
|
217
|
+
expect(report).not.toContain("New since this session started");
|
|
218
|
+
// The offered ids are the only listing a sandbox user gets.
|
|
219
|
+
expect(report).toContain("offered by the gateway: a, b, c");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("a long list is capped so one notification stays readable", () => {
|
|
223
|
+
const ids = Array.from({ length: 11 }, (_, i) => `m${i}`);
|
|
224
|
+
const report = refreshReport({
|
|
225
|
+
source: "gateway",
|
|
226
|
+
live: ids,
|
|
227
|
+
added: ids,
|
|
228
|
+
removed: [],
|
|
229
|
+
knownCount: 1,
|
|
230
|
+
});
|
|
231
|
+
expect(report).toContain("(+3 more)");
|
|
232
|
+
expect(report).toContain("m7");
|
|
233
|
+
expect(report).not.toContain("m8");
|
|
234
|
+
});
|
|
235
|
+
});
|
package/src/refresh.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared engine for the `/bifrost-refresh` slash command: run the gateway CLI
|
|
3
|
+
* when it is installed, otherwise ask the gateway directly.
|
|
4
|
+
*
|
|
5
|
+
* The CLI stays the one implementation of model *enrichment* — limits,
|
|
6
|
+
* pricing, availability probes, the curated selection — and the one that
|
|
7
|
+
* persists anything. This module exists for the sandbox case the command was
|
|
8
|
+
* asked for: no binary on PATH, so the command lists what the gateway offers
|
|
9
|
+
* by reading `$BIFROST_GATEWAY_URL` / `$BIFROST_VK` (or the CLI's own key
|
|
10
|
+
* file) and reports the difference against the models this session
|
|
11
|
+
* registered. Nothing here writes config, and the key never leaves the
|
|
12
|
+
* request header.
|
|
13
|
+
*
|
|
14
|
+
* Byte-identical in packages/pi-bifrost and packages/opencode-bifrost;
|
|
15
|
+
* `make check-refresh-sync` fails the build if they drift.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The subcommand name both harnesses register. */
|
|
19
|
+
export const REFRESH_COMMAND = "bifrost-refresh";
|
|
20
|
+
|
|
21
|
+
/** Which path answered: the CLI (it also reconciled), or a direct fetch. */
|
|
22
|
+
export type RefreshSource = "cli" | "gateway";
|
|
23
|
+
|
|
24
|
+
/** One refresh's outcome, ready to render. */
|
|
25
|
+
export type RefreshResult = {
|
|
26
|
+
source: RefreshSource;
|
|
27
|
+
/** Ids the gateway offers right now; empty when neither path answered. */
|
|
28
|
+
live: string[];
|
|
29
|
+
/** Live ids this session does not have — a restart or re-register shows them. */
|
|
30
|
+
added: string[];
|
|
31
|
+
/** Session ids the gateway no longer lists. */
|
|
32
|
+
removed: string[];
|
|
33
|
+
/** How many models this session had registered, to word the report. */
|
|
34
|
+
knownCount: number;
|
|
35
|
+
/** The CLI's own output, when it ran. */
|
|
36
|
+
cliOutput?: string;
|
|
37
|
+
/** Why nothing could be fetched; only set when live is empty. */
|
|
38
|
+
error?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Runs `<cli> <args>` in the project directory; null when it is unavailable. */
|
|
42
|
+
export type Exec = (args: string[]) => Promise<string | null>;
|
|
43
|
+
|
|
44
|
+
/** Narrow fetch shape, injectable so tests need no network. */
|
|
45
|
+
export type FetchLike = (
|
|
46
|
+
url: string,
|
|
47
|
+
init?: { headers?: Record<string, string>; signal?: AbortSignal },
|
|
48
|
+
) => Promise<{ ok: boolean; status?: number; json(): Promise<unknown> }>;
|
|
49
|
+
|
|
50
|
+
/** The gateway coordinates a CLI-less refresh needs. */
|
|
51
|
+
export type GatewayTarget = { baseUrl: string; apiKey: string | null };
|
|
52
|
+
|
|
53
|
+
/** The virtual-key variable the CLI exports; both harnesses read it too. */
|
|
54
|
+
export const KEY_ENV = "BIFROST_VK";
|
|
55
|
+
|
|
56
|
+
/** Gateway host override, matching gateway-cli's own variable. */
|
|
57
|
+
export const URL_ENV = "BIFROST_GATEWAY_URL";
|
|
58
|
+
|
|
59
|
+
/** Discovery host override, for a proxy that does not serve /v1/models. */
|
|
60
|
+
export const DISCOVERY_ENV = "BIFROST_DISCOVERY_URL";
|
|
61
|
+
|
|
62
|
+
/** The `/v1/models` path, same surface `gateway-cli models` reads. */
|
|
63
|
+
const MODELS_PATH = "/v1/models";
|
|
64
|
+
|
|
65
|
+
/** Bounds the direct fetch so the command never hangs a session. */
|
|
66
|
+
const FETCH_TIMEOUT_MS = 5000;
|
|
67
|
+
|
|
68
|
+
/** Matches the CLI's own key-file line (`export BIFROST_VK=sk-bf-…`). */
|
|
69
|
+
const KEY_LINE = new RegExp(`^export\\s+${KEY_ENV}\\s*=\\s*(.+)$`);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Pull the key out of a CLI key file (`export BIFROST_VK=sk-bf-…`). Anything
|
|
73
|
+
* unrecognized — another variable, a comment, a truncated line, a value that
|
|
74
|
+
* is not a virtual key — is null, so a stray file can never supply a
|
|
75
|
+
* non-key to a request header.
|
|
76
|
+
*/
|
|
77
|
+
export function keyFileValue(text: string): string | null {
|
|
78
|
+
for (const line of text.split("\n")) {
|
|
79
|
+
const match = KEY_LINE.exec(line.trim());
|
|
80
|
+
if (!match) continue;
|
|
81
|
+
const value = match[1].trim().replace(/^["']|["']$/g, "");
|
|
82
|
+
return value.startsWith("sk-bf-") ? value : null;
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Resolve the gateway from environment alone: the discovery host (or the
|
|
89
|
+
* traffic host) plus the key. The URL is required — a harness package cannot
|
|
90
|
+
* know which deployment this project targets, and guessing a host would send
|
|
91
|
+
* a real key somewhere unintended. A missing key still returns a target, so
|
|
92
|
+
* the caller can report "no key" instead of "no gateway".
|
|
93
|
+
*/
|
|
94
|
+
export function gatewayFromEnv(
|
|
95
|
+
env: Record<string, string | undefined>,
|
|
96
|
+
): GatewayTarget | null {
|
|
97
|
+
const baseUrl = env[DISCOVERY_ENV] || env[URL_ENV];
|
|
98
|
+
if (!baseUrl) return null;
|
|
99
|
+
let url: URL;
|
|
100
|
+
try {
|
|
101
|
+
url = new URL(baseUrl);
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const loopback =
|
|
106
|
+
url.hostname === "localhost" ||
|
|
107
|
+
url.hostname === "127.0.0.1" ||
|
|
108
|
+
url.hostname === "::1";
|
|
109
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const apiKey = env[KEY_ENV]?.startsWith("sk-bf-") ? env[KEY_ENV]! : null;
|
|
113
|
+
return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Ids from a `/v1/models` body, tolerating anything unexpected. */
|
|
117
|
+
export function liveModelIds(payload: unknown): string[] {
|
|
118
|
+
const data = (payload as { data?: unknown } | null)?.data;
|
|
119
|
+
if (!Array.isArray(data)) return [];
|
|
120
|
+
const ids: string[] = [];
|
|
121
|
+
for (const entry of data) {
|
|
122
|
+
const id = (entry as { id?: unknown } | null)?.id;
|
|
123
|
+
if (typeof id === "string" && id.length > 0) ids.push(id);
|
|
124
|
+
}
|
|
125
|
+
return ids;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Diff the live listing against the ids this session registered. Membership
|
|
130
|
+
* is exact: an id prefix (BIFROST_MODEL_PREFIX) is the caller's business, so
|
|
131
|
+
* both sides must be compared in the same spelling.
|
|
132
|
+
*/
|
|
133
|
+
export function diffModels(
|
|
134
|
+
known: string[],
|
|
135
|
+
live: string[],
|
|
136
|
+
): { added: string[]; removed: string[] } {
|
|
137
|
+
const have = new Set(known);
|
|
138
|
+
const now = new Set(live);
|
|
139
|
+
return {
|
|
140
|
+
added: live.filter((id) => !have.has(id)).sort(),
|
|
141
|
+
removed: known.filter((id) => !now.has(id)).sort(),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Fetch the live listing with the given key; null on any failure. */
|
|
146
|
+
async function fetchLive(
|
|
147
|
+
target: GatewayTarget,
|
|
148
|
+
apiKey: string,
|
|
149
|
+
fetchImpl: FetchLike,
|
|
150
|
+
): Promise<string[] | null> {
|
|
151
|
+
try {
|
|
152
|
+
const res = await fetchImpl(`${target.baseUrl}${MODELS_PATH}`, {
|
|
153
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
154
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
155
|
+
});
|
|
156
|
+
if (!res.ok) return null;
|
|
157
|
+
return liveModelIds(await res.json());
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Run one refresh.
|
|
165
|
+
*
|
|
166
|
+
* The CLI wins whenever it answers: `sync` reconciles the saved selection and
|
|
167
|
+
* rewrites launched harnesses, which a direct listing cannot do. Only when it
|
|
168
|
+
* is missing or fails does the gateway get asked directly, and then the
|
|
169
|
+
* result is a report — nothing is persisted.
|
|
170
|
+
*/
|
|
171
|
+
export async function refresh(opts: {
|
|
172
|
+
/** Ids this session registered (already in the client's spelling). */
|
|
173
|
+
known: string[];
|
|
174
|
+
/** Runs the CLI; null means "not installed here". */
|
|
175
|
+
exec: Exec;
|
|
176
|
+
/** Reads the CLI's key file, when the environment has no key. */
|
|
177
|
+
readKeyFile?: () => string | null;
|
|
178
|
+
/** A harness-stored credential (OpenCode's auth store); last resort. */
|
|
179
|
+
storedKey?: () => Promise<string | null>;
|
|
180
|
+
fetchImpl?: FetchLike;
|
|
181
|
+
env?: Record<string, string | undefined>;
|
|
182
|
+
}): Promise<RefreshResult> {
|
|
183
|
+
const knownCount = opts.known.length;
|
|
184
|
+
const cliOutput = await opts.exec(["sync"]);
|
|
185
|
+
if (cliOutput !== null) {
|
|
186
|
+
// The CLI reconciled on disk; what it printed is the whole story unless
|
|
187
|
+
// the caller wants to know what the session itself is missing.
|
|
188
|
+
return { source: "cli", live: [], added: [], removed: [], knownCount, cliOutput };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const env = opts.env ?? {};
|
|
192
|
+
const target = gatewayFromEnv(env);
|
|
193
|
+
if (!target) {
|
|
194
|
+
return {
|
|
195
|
+
source: "gateway",
|
|
196
|
+
live: [],
|
|
197
|
+
added: [],
|
|
198
|
+
removed: [],
|
|
199
|
+
knownCount,
|
|
200
|
+
error: `no gateway to ask: set ${URL_ENV} (and ${KEY_ENV}) or install gateway-cli`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const apiKey =
|
|
204
|
+
target.apiKey ??
|
|
205
|
+
opts.readKeyFile?.() ??
|
|
206
|
+
(await opts.storedKey?.().catch(() => null)) ??
|
|
207
|
+
null;
|
|
208
|
+
if (!apiKey) {
|
|
209
|
+
return {
|
|
210
|
+
source: "gateway",
|
|
211
|
+
live: [],
|
|
212
|
+
added: [],
|
|
213
|
+
removed: [],
|
|
214
|
+
knownCount,
|
|
215
|
+
error: `no virtual key: set ${KEY_ENV}, store one with gateway-cli, or install gateway-cli`,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const live = await fetchLive(target, apiKey, opts.fetchImpl ?? defaultFetch);
|
|
219
|
+
if (live === null) {
|
|
220
|
+
return {
|
|
221
|
+
source: "gateway",
|
|
222
|
+
live: [],
|
|
223
|
+
added: [],
|
|
224
|
+
removed: [],
|
|
225
|
+
knownCount,
|
|
226
|
+
error: `could not list models from ${target.baseUrl}${MODELS_PATH}`,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return { source: "gateway", live, knownCount, ...diffModels(opts.known, live) };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Default fetch adapter, narrowed to FetchLike so callers can inject one
|
|
234
|
+
* without touching the global fetch.
|
|
235
|
+
*/
|
|
236
|
+
const defaultFetch: FetchLike = async (url, init) => {
|
|
237
|
+
const res = await fetch(url, init);
|
|
238
|
+
return { ok: res.ok, status: res.status, json: () => res.json() };
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
/** How many ids a report line lists before it summarizes the rest. */
|
|
242
|
+
const IDS_PER_LINE = 8;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Render up to IDS_PER_LINE ids, then a count. A sandbox whose session
|
|
246
|
+
* registered nothing would otherwise print the gateway's whole lineup into a
|
|
247
|
+
* one-line notification.
|
|
248
|
+
*/
|
|
249
|
+
function idList(ids: string[]): string {
|
|
250
|
+
if (ids.length <= IDS_PER_LINE) return ids.join(", ");
|
|
251
|
+
return `${ids.slice(0, IDS_PER_LINE).join(", ")} (+${ids.length - IDS_PER_LINE} more)`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Render a result as the text the command puts in front of the user. `applied`
|
|
256
|
+
* says the harness re-registered the list already, so the text must not tell
|
|
257
|
+
* the user to restart for models that are already there.
|
|
258
|
+
*/
|
|
259
|
+
export function refreshReport(result: RefreshResult, applied = false): string {
|
|
260
|
+
if (result.source === "cli") {
|
|
261
|
+
const out = (result.cliOutput ?? "").trim();
|
|
262
|
+
return out.length > 0 ? out : "gateway-cli sync: nothing to change.";
|
|
263
|
+
}
|
|
264
|
+
if (result.error) return `bifrost models: ${result.error}`;
|
|
265
|
+
const heading = `bifrost models: ${result.live.length} offered by the gateway`;
|
|
266
|
+
// With nothing registered there is no diff to show, so the headline names
|
|
267
|
+
// what is on offer instead — the sandbox user has no other way to see it.
|
|
268
|
+
const lines =
|
|
269
|
+
result.knownCount === 0 && result.live.length > 0
|
|
270
|
+
? [`${heading}: ${idList(result.live)}`]
|
|
271
|
+
: [`${heading}.`];
|
|
272
|
+
if (result.added.length > 0) {
|
|
273
|
+
if (applied) {
|
|
274
|
+
lines.push(`Added this session: ${idList(result.added)}`);
|
|
275
|
+
} else if (result.knownCount === 0) {
|
|
276
|
+
// Nothing was registered here, so nothing is "new since startup": this
|
|
277
|
+
// session simply has no gateway models (the CLI-less sandbox case).
|
|
278
|
+
lines.push(
|
|
279
|
+
`None registered in this session — install gateway-cli (or point GATEWAY_CLI_BIN at it) and restart to use them.`,
|
|
280
|
+
);
|
|
281
|
+
} else {
|
|
282
|
+
lines.push(
|
|
283
|
+
`New since this session started: ${idList(result.added)} — restart to use them.`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (result.removed.length > 0) {
|
|
288
|
+
lines.push(`No longer offered: ${idList(result.removed)}`);
|
|
289
|
+
}
|
|
290
|
+
if (result.added.length === 0 && result.removed.length === 0) {
|
|
291
|
+
lines.push("This session's list is current.");
|
|
292
|
+
}
|
|
293
|
+
return lines.join("\n");
|
|
294
|
+
}
|