@stablekernel/opencode-bifrost 0.8.0 → 0.9.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 +0 -29
- package/package.json +1 -1
- package/src/index.test.ts +2 -5
- package/src/index.ts +1 -1
- package/src/shared.test.ts +3 -36
- package/src/shared.ts +4 -25
- package/src/v2.test.ts +3 -11
- package/src/v2.ts +1 -1
- package/src/datasheet-supplement.test.ts +0 -133
- package/src/datasheet.test.ts +0 -355
- package/src/datasheet.ts +0 -303
package/README.md
CHANGED
|
@@ -40,35 +40,6 @@ the moment you run it:
|
|
|
40
40
|
Either way the result arrives as a message part, never as an error: a failure
|
|
41
41
|
prints one line. The virtual key is never printed or logged.
|
|
42
42
|
|
|
43
|
-
## Datasheet supplement (off by default)
|
|
44
|
-
|
|
45
|
-
Wildcard-provider models — e.g. a fresh Fireworks launch that the CLI's
|
|
46
|
-
models.dev source has not cataloged yet — never reach `gateway-cli models`,
|
|
47
|
-
so they cannot be listed here either. When configured, this plugin also reads
|
|
48
|
-
the datasheet feed Bifrost itself syncs from and appends the wildcard-provider
|
|
49
|
-
ids the CLI list lacks; CLI-listed ids always win. The supplement applies to
|
|
50
|
-
the single `bifrost` provider.
|
|
51
|
-
|
|
52
|
-
Activation (first wins):
|
|
53
|
-
|
|
54
|
-
1. env `GATEWAY_DATASHEET_URL`
|
|
55
|
-
2. `~/.config/bifrost/datasheet.json` — `{"url": "..."}`
|
|
56
|
-
3. `<project>/.gateway/datasheet.json` — same shape
|
|
57
|
-
|
|
58
|
-
The URL must be `https://`; a source with anything else is treated as absent.
|
|
59
|
-
|
|
60
|
-
Behavior: the feed is fetched once per hour and cached under
|
|
61
|
-
`~/.cache/opencode/datasheet-cache.json` (or `$XDG_CACHE_HOME`), with a 3s
|
|
62
|
-
hard timeout and a stale-cache fallback on failure. A failed first fetch
|
|
63
|
-
also triggers a background cache fill, so a slow or cold feed costs one
|
|
64
|
-
session rather than every start. Unconfigured or unreachable, the
|
|
65
|
-
supplement is silently skipped — startup never blocks on it.
|
|
66
|
-
|
|
67
|
-
Provenance: supplement-only models are tagged `limits_source: "datasheet"`
|
|
68
|
-
and `cost_source: "datasheet"` in the `gateway-cli models` payload they ride
|
|
69
|
-
in. The tags stop there — opencode's config schema has no provenance field,
|
|
70
|
-
so they are not forwarded into the emitted `opencode.json`.
|
|
71
|
-
|
|
72
43
|
## Requirements
|
|
73
44
|
|
|
74
45
|
- `gateway-cli` on `PATH` (or the `GATEWAY_CLI_BIN` env var pointing at it), connected once with `gateway-cli connect`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stablekernel/opencode-bifrost",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "OpenCode plugin that routes models through the Bifrost gateway, resolving per-project virtual keys via gateway-cli. Supports OpenCode 1.x and 2.x.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
package/src/index.test.ts
CHANGED
|
@@ -21,11 +21,8 @@ const ORIGINAL_XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME;
|
|
|
21
21
|
const ORIGINAL_FETCH = globalThis.fetch;
|
|
22
22
|
|
|
23
23
|
beforeEach(() => {
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
// HOME at an empty temp dir, so neither ~/.config/bifrost/datasheet.json
|
|
27
|
-
// nor the real ~/.cache/opencode cache is read or written.
|
|
28
|
-
delete process.env.GATEWAY_DATASHEET_URL;
|
|
24
|
+
// Point HOME at an empty temp dir so the real ~/.cache/opencode
|
|
25
|
+
// version-check cache is never read or written.
|
|
29
26
|
// /bifrost-refresh reads its gateway and key from the environment; a real
|
|
30
27
|
// BIFROST_GATEWAY_URL in the developer's shell must not reach these tests.
|
|
31
28
|
delete process.env.BIFROST_GATEWAY_URL;
|
package/src/index.ts
CHANGED
|
@@ -135,7 +135,7 @@ export const BifrostGateway: Plugin = async ({ client, directory, $ }) => {
|
|
|
135
135
|
).provider ??= {});
|
|
136
136
|
const existing: ProviderEntry = providers[PROVIDER_ID] ?? {};
|
|
137
137
|
const models = toConfigModels(
|
|
138
|
-
await listCliModels(cli
|
|
138
|
+
await listCliModels(cli),
|
|
139
139
|
info.reasoning_effort,
|
|
140
140
|
);
|
|
141
141
|
const merged = { ...models, ...(existing.models ?? {}) };
|
package/src/shared.test.ts
CHANGED
|
@@ -1,42 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mkdtempSync } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
5
2
|
import {
|
|
6
3
|
listCliModels,
|
|
7
4
|
readCliKey,
|
|
8
5
|
resolveInfo,
|
|
9
6
|
} from "./shared.js";
|
|
10
7
|
|
|
11
|
-
/** HOME as the suite found it, restored after each test. */
|
|
12
|
-
const ORIGINAL_HOME = process.env.HOME;
|
|
13
|
-
|
|
14
|
-
/** XDG_CACHE_HOME as the suite found it, restored after each test. */
|
|
15
|
-
const ORIGINAL_XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
|
|
16
|
-
|
|
17
|
-
/** GATEWAY_DATASHEET_URL as the suite found it, restored after each test. */
|
|
18
|
-
const ORIGINAL_DATASHEET_URL = process.env.GATEWAY_DATASHEET_URL;
|
|
19
|
-
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
// The listCliModels tests run the real datasheet supplement. Keep it
|
|
22
|
-
// deterministic and machine-independent: point HOME and the cache root
|
|
23
|
-
// at an empty temp dir, so neither ~/.config/bifrost/datasheet.json nor
|
|
24
|
-
// a warm ~/.cache/opencode cache is read, and drop any feed override
|
|
25
|
-
// from the developer's shell.
|
|
26
|
-
delete process.env.GATEWAY_DATASHEET_URL;
|
|
27
|
-
delete process.env.XDG_CACHE_HOME;
|
|
28
|
-
process.env.HOME = mkdtempSync(join(tmpdir(), "gw-oc-home-"));
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
afterEach(() => {
|
|
32
|
-
if (ORIGINAL_HOME === undefined) delete process.env.HOME;
|
|
33
|
-
else process.env.HOME = ORIGINAL_HOME;
|
|
34
|
-
if (ORIGINAL_XDG_CACHE_HOME === undefined) delete process.env.XDG_CACHE_HOME;
|
|
35
|
-
else process.env.XDG_CACHE_HOME = ORIGINAL_XDG_CACHE_HOME;
|
|
36
|
-
if (ORIGINAL_DATASHEET_URL === undefined) delete process.env.GATEWAY_DATASHEET_URL;
|
|
37
|
-
else process.env.GATEWAY_DATASHEET_URL = ORIGINAL_DATASHEET_URL;
|
|
38
|
-
});
|
|
39
|
-
|
|
40
8
|
/**
|
|
41
9
|
* Records every exec and answers from a scripted map. These three helpers
|
|
42
10
|
* are the whole CLI-discovery surface both majors share, so the tests pin
|
|
@@ -124,7 +92,6 @@ describe("listCliModels", () => {
|
|
|
124
92
|
const calls: string[][] = [];
|
|
125
93
|
const payload = await listCliModels(
|
|
126
94
|
fakeExec({ "models --wire=openai": PAYLOAD }, calls),
|
|
127
|
-
"/repo",
|
|
128
95
|
);
|
|
129
96
|
expect(payload?.models?.map((m) => m.id)).toEqual([
|
|
130
97
|
"bedrock/amazon.nova-lite-v1:0",
|
|
@@ -133,9 +100,9 @@ describe("listCliModels", () => {
|
|
|
133
100
|
});
|
|
134
101
|
|
|
135
102
|
test("returns null when the CLI is missing or emits garbage", async () => {
|
|
136
|
-
expect(await listCliModels(async () => null
|
|
103
|
+
expect(await listCliModels(async () => null)).toBeNull();
|
|
137
104
|
expect(
|
|
138
|
-
await listCliModels(fakeExec({ "models --wire=openai": "not json" }, [])
|
|
105
|
+
await listCliModels(fakeExec({ "models --wire=openai": "not json" }, [])),
|
|
139
106
|
).toBeNull();
|
|
140
107
|
});
|
|
141
108
|
});
|
package/src/shared.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { datasheetSupplement, mergeModels } from "./datasheet.js";
|
|
4
3
|
import { keyFileValue, type Exec } from "./refresh.js";
|
|
5
4
|
|
|
6
5
|
/** Provider id registered with OpenCode. */
|
|
@@ -96,13 +95,6 @@ function versionCheckPath(): string | null {
|
|
|
96
95
|
return `${base}/opencode/bifrost-version-check.json`;
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
/** Where the datasheet feed cache lives; same base as the version check. */
|
|
100
|
-
function datasheetCacheDir(): string | null {
|
|
101
|
-
const home = process.env.HOME || homedir() || null;
|
|
102
|
-
const base = process.env.XDG_CACHE_HOME ?? (home ? `${home}/.cache` : null);
|
|
103
|
-
return base ? `${base}/opencode` : null;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
98
|
/**
|
|
107
99
|
* Run `gateway-cli resolve --json` through `exec` and allowlist the origin it
|
|
108
100
|
* reports. Null when the CLI is missing, the payload is malformed, or the
|
|
@@ -133,27 +125,14 @@ export async function readCliKey(exec: Exec): Promise<string | null> {
|
|
|
133
125
|
}
|
|
134
126
|
|
|
135
127
|
/**
|
|
136
|
-
* Fetch the model list via `gateway-cli models --wire=openai
|
|
137
|
-
*
|
|
138
|
-
* (a feed Bifrost itself syncs from); CLI-listed ids always win. `root` is
|
|
139
|
-
* the project directory, where a per-project `.gateway/datasheet.json` may
|
|
140
|
-
* live — under OpenCode 2 the process cwd is the background service's, not
|
|
141
|
-
* the project's. Null when the CLI is missing or its payload is malformed.
|
|
128
|
+
* Fetch the model list via `gateway-cli models --wire=openai`. Null when the
|
|
129
|
+
* CLI is missing or its payload is malformed.
|
|
142
130
|
*/
|
|
143
|
-
export async function listCliModels(
|
|
144
|
-
exec: Exec,
|
|
145
|
-
root: string,
|
|
146
|
-
): Promise<CliModels | null> {
|
|
131
|
+
export async function listCliModels(exec: Exec): Promise<CliModels | null> {
|
|
147
132
|
const raw = await exec(["models", "--wire=openai"]);
|
|
148
133
|
if (!raw) return null;
|
|
149
134
|
try {
|
|
150
|
-
|
|
151
|
-
const cacheDir = datasheetCacheDir();
|
|
152
|
-
if (cacheDir) {
|
|
153
|
-
const extra = await datasheetSupplement(root, cacheDir);
|
|
154
|
-
payload.models = mergeModels(payload.models ?? [], extra);
|
|
155
|
-
}
|
|
156
|
-
return payload;
|
|
135
|
+
return JSON.parse(raw) as CliModels;
|
|
157
136
|
} catch {
|
|
158
137
|
return null;
|
|
159
138
|
}
|
package/src/v2.test.ts
CHANGED
|
@@ -12,9 +12,6 @@ const ORIGINAL_HOME = process.env.HOME;
|
|
|
12
12
|
/** XDG_CACHE_HOME as the suite found it, restored after each test. */
|
|
13
13
|
const ORIGINAL_XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
|
|
14
14
|
|
|
15
|
-
/** GATEWAY_DATASHEET_URL as the suite found it, restored after each test. */
|
|
16
|
-
const ORIGINAL_DATASHEET_URL = process.env.GATEWAY_DATASHEET_URL;
|
|
17
|
-
|
|
18
15
|
/** BIFROST_GATEWAY_URL as the suite found it, restored after each test. */
|
|
19
16
|
const ORIGINAL_GATEWAY_URL = process.env.BIFROST_GATEWAY_URL;
|
|
20
17
|
|
|
@@ -28,12 +25,9 @@ const ORIGINAL_VK = process.env.BIFROST_VK;
|
|
|
28
25
|
const ORIGINAL_XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME;
|
|
29
26
|
|
|
30
27
|
beforeEach(() => {
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
// ~/.config/bifrost/datasheet.json nor a warm ~/.cache/opencode cache
|
|
35
|
-
// is read, and drop any feed override from the developer's shell.
|
|
36
|
-
delete process.env.GATEWAY_DATASHEET_URL;
|
|
28
|
+
// Keep the version-check cache machine-independent: point HOME and the
|
|
29
|
+
// cache root at an empty temp dir so the real ~/.cache/opencode is never
|
|
30
|
+
// read or written.
|
|
37
31
|
delete process.env.XDG_CACHE_HOME;
|
|
38
32
|
// /bifrost-refresh reads its gateway and key from the environment; a real
|
|
39
33
|
// BIFROST_GATEWAY_URL in the developer's shell must not reach these tests.
|
|
@@ -52,8 +46,6 @@ afterEach(() => {
|
|
|
52
46
|
else process.env.HOME = ORIGINAL_HOME;
|
|
53
47
|
if (ORIGINAL_XDG_CACHE_HOME === undefined) delete process.env.XDG_CACHE_HOME;
|
|
54
48
|
else process.env.XDG_CACHE_HOME = ORIGINAL_XDG_CACHE_HOME;
|
|
55
|
-
if (ORIGINAL_DATASHEET_URL === undefined) delete process.env.GATEWAY_DATASHEET_URL;
|
|
56
|
-
else process.env.GATEWAY_DATASHEET_URL = ORIGINAL_DATASHEET_URL;
|
|
57
49
|
if (ORIGINAL_GATEWAY_URL === undefined) delete process.env.BIFROST_GATEWAY_URL;
|
|
58
50
|
else process.env.BIFROST_GATEWAY_URL = ORIGINAL_GATEWAY_URL;
|
|
59
51
|
if (ORIGINAL_DISCOVERY_URL === undefined) delete process.env.BIFROST_DISCOVERY_URL;
|
package/src/v2.ts
CHANGED
|
@@ -285,7 +285,7 @@ export async function setupV2(ctx: V2Context, exec?: Exec): Promise<void> {
|
|
|
285
285
|
const key = await readCliKey(run);
|
|
286
286
|
if (!key) return;
|
|
287
287
|
|
|
288
|
-
const models = toV2Models(await listCliModels(run
|
|
288
|
+
const models = toV2Models(await listCliModels(run), info.reasoning_effort);
|
|
289
289
|
if (models.length === 0) return;
|
|
290
290
|
|
|
291
291
|
await ctx.provider.transform((editor) => {
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test";
|
|
2
|
-
import { mkdtempSync } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { BifrostGateway } from "./index.js";
|
|
6
|
-
|
|
7
|
-
// Exercises the REAL ./datasheet.js (no mock.module): the supplement is
|
|
8
|
-
// env + fetch driven, so the test points GATEWAY_DATASHEET_URL at a stubbed
|
|
9
|
-
// fetch and HOME at a temp dir. This keeps the whole supplement path —
|
|
10
|
-
// datasheet.js through shared.ts's listCliModels — honest without
|
|
11
|
-
// rewriting module bindings, which leak across test files in one process.
|
|
12
|
-
|
|
13
|
-
const ORIGINAL_FETCH = globalThis.fetch;
|
|
14
|
-
const ORIGINAL_HOME = process.env.HOME;
|
|
15
|
-
const ORIGINAL_XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
|
|
16
|
-
const ORIGINAL_DATASHEET_URL = process.env.GATEWAY_DATASHEET_URL;
|
|
17
|
-
|
|
18
|
-
const EXTRA_FLASH = "glm-5p3-flash";
|
|
19
|
-
|
|
20
|
-
const RESOLUTION = {
|
|
21
|
-
env: "prod",
|
|
22
|
-
root: "/repo",
|
|
23
|
-
anthropic_base_url: "https://gw.example.com/anthropic",
|
|
24
|
-
openai_base_url: "https://gw.example.com/openai/v1",
|
|
25
|
-
has_key: true,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const SHELL: Record<string, string> = {
|
|
29
|
-
"resolve --json": JSON.stringify(RESOLUTION),
|
|
30
|
-
"key print": "sk-bf-projectkey\n",
|
|
31
|
-
"models --wire=openai": JSON.stringify({
|
|
32
|
-
wire: "openai-completions",
|
|
33
|
-
env: "prod",
|
|
34
|
-
models: [
|
|
35
|
-
{
|
|
36
|
-
id: "anthropic/claude-haiku-4-5-20251001",
|
|
37
|
-
name: "Claude Haiku 4.5",
|
|
38
|
-
context_window: 200000,
|
|
39
|
-
max_output_tokens: 64000,
|
|
40
|
-
cost: { input: 1, output: 5, cache_read: 0.1, cache_write: 1.25 },
|
|
41
|
-
limits_source: "gateway",
|
|
42
|
-
cost_source: "catalog",
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
id: "bedrock/amazon.nova-lite-v1:0",
|
|
46
|
-
name: "Nova Lite",
|
|
47
|
-
context_window: 300000,
|
|
48
|
-
max_output_tokens: 8192,
|
|
49
|
-
cost: { input: 0.06, output: 0.24, cache_read: 0.015, cache_write: 0 },
|
|
50
|
-
limits_source: "catalog",
|
|
51
|
-
cost_source: "catalog",
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
}),
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
function fakeShell(responses: Record<string, string>) {
|
|
58
|
-
return (_strings: TemplateStringsArray, ...values: unknown[]) => {
|
|
59
|
-
const argv = (values[1] as string[]) ?? [];
|
|
60
|
-
const cmd = argv.join(" ");
|
|
61
|
-
const chain = {
|
|
62
|
-
cwd: () => chain,
|
|
63
|
-
quiet: () => chain,
|
|
64
|
-
text: async () => {
|
|
65
|
-
if (!(cmd in responses)) throw new Error(`command failed: ${cmd}`);
|
|
66
|
-
return responses[cmd];
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
return chain;
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
test("appends datasheet supplement ids to the bifrost provider's models map", async () => {
|
|
74
|
-
// Home holds no ~/.config/bifrost/datasheet.json, so the env override is
|
|
75
|
-
// the only activation; the cache dir under it is otherwise empty.
|
|
76
|
-
const home = mkdtempSync(join(tmpdir(), "gw-oc-ds-"));
|
|
77
|
-
process.env.HOME = home;
|
|
78
|
-
// datasheetCacheDir() checks XDG_CACHE_HOME before HOME, so pin it under
|
|
79
|
-
// the temp home too — otherwise CI/Linux reads and dirties the real cache.
|
|
80
|
-
process.env.XDG_CACHE_HOME = join(home, ".cache");
|
|
81
|
-
process.env.GATEWAY_DATASHEET_URL = "https://feed.example/datasheet";
|
|
82
|
-
// The stubbed feed answers immediately, so the supplement is active this run.
|
|
83
|
-
globalThis.fetch = (async () => ({
|
|
84
|
-
ok: true,
|
|
85
|
-
status: 200,
|
|
86
|
-
json: async () => ({
|
|
87
|
-
"fireworks_ai/glm-5p3-flash": {
|
|
88
|
-
mode: "chat",
|
|
89
|
-
base_model: "glm-5p3-flash",
|
|
90
|
-
max_input_tokens: 1048576,
|
|
91
|
-
max_output_tokens: 131072,
|
|
92
|
-
},
|
|
93
|
-
}),
|
|
94
|
-
})) as never;
|
|
95
|
-
try {
|
|
96
|
-
const hooks = await BifrostGateway({
|
|
97
|
-
directory: "/repo",
|
|
98
|
-
$: fakeShell(SHELL),
|
|
99
|
-
client: {
|
|
100
|
-
app: { log: async () => undefined },
|
|
101
|
-
instance: { dispose: async () => {} },
|
|
102
|
-
},
|
|
103
|
-
} as never);
|
|
104
|
-
const cfg = {
|
|
105
|
-
command: {},
|
|
106
|
-
provider: {},
|
|
107
|
-
} as unknown as {
|
|
108
|
-
command: Record<string, unknown>;
|
|
109
|
-
provider: Record<
|
|
110
|
-
string,
|
|
111
|
-
{ models: Record<string, { limit: { context: number; output: number } }> }
|
|
112
|
-
>;
|
|
113
|
-
};
|
|
114
|
-
await hooks.config!(cfg as never);
|
|
115
|
-
const ids = Object.keys(cfg.provider.bifrost.models);
|
|
116
|
-
// The CLI's list survives; the supplemented id is appended alongside.
|
|
117
|
-
expect(ids).toContain("anthropic/claude-haiku-4-5-20251001");
|
|
118
|
-
expect(ids).toContain("bedrock/amazon.nova-lite-v1:0");
|
|
119
|
-
expect(ids).toContain(EXTRA_FLASH);
|
|
120
|
-
expect(cfg.provider.bifrost.models[EXTRA_FLASH]!.limit).toEqual({
|
|
121
|
-
context: 1048576,
|
|
122
|
-
output: 131072,
|
|
123
|
-
});
|
|
124
|
-
} finally {
|
|
125
|
-
globalThis.fetch = ORIGINAL_FETCH;
|
|
126
|
-
if (ORIGINAL_HOME === undefined) delete process.env.HOME;
|
|
127
|
-
else process.env.HOME = ORIGINAL_HOME;
|
|
128
|
-
if (ORIGINAL_XDG_CACHE_HOME === undefined) delete process.env.XDG_CACHE_HOME;
|
|
129
|
-
else process.env.XDG_CACHE_HOME = ORIGINAL_XDG_CACHE_HOME;
|
|
130
|
-
if (ORIGINAL_DATASHEET_URL === undefined) delete process.env.GATEWAY_DATASHEET_URL;
|
|
131
|
-
else process.env.GATEWAY_DATASHEET_URL = ORIGINAL_DATASHEET_URL;
|
|
132
|
-
}
|
|
133
|
-
});
|
package/src/datasheet.test.ts
DELETED
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
existsSync,
|
|
4
|
-
mkdirSync,
|
|
5
|
-
mkdtempSync,
|
|
6
|
-
readFileSync,
|
|
7
|
-
writeFileSync,
|
|
8
|
-
} from "node:fs";
|
|
9
|
-
import { tmpdir } from "node:os";
|
|
10
|
-
import { join } from "node:path";
|
|
11
|
-
import {
|
|
12
|
-
type FetchLike,
|
|
13
|
-
type SupplementalModel,
|
|
14
|
-
datasheetUrl,
|
|
15
|
-
fetchDatasheet,
|
|
16
|
-
mergeModels,
|
|
17
|
-
parseDatasheet,
|
|
18
|
-
} from "./datasheet.js";
|
|
19
|
-
import { toConfigModels, type CliModels } from "./index.js";
|
|
20
|
-
|
|
21
|
-
const glmFlash = {
|
|
22
|
-
provider: "fireworks_ai",
|
|
23
|
-
mode: "chat",
|
|
24
|
-
base_model: "glm-5.3-flash",
|
|
25
|
-
context_length: 1048576,
|
|
26
|
-
max_input_tokens: 1048576,
|
|
27
|
-
max_output_tokens: 131072,
|
|
28
|
-
input_cost_per_token: 0.00000015,
|
|
29
|
-
cache_read_input_token_cost: 0.000000029,
|
|
30
|
-
output_cost_per_token: 0.0000005,
|
|
31
|
-
supports_reasoning: true,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
describe("parseDatasheet", () => {
|
|
35
|
-
test("maps a fireworks_ai chat entry, converting per-token to per-million", () => {
|
|
36
|
-
const out = parseDatasheet({
|
|
37
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": glmFlash,
|
|
38
|
-
});
|
|
39
|
-
expect(out).toHaveLength(1);
|
|
40
|
-
expect(out[0]).toEqual({
|
|
41
|
-
id: "accounts/fireworks/models/glm-5p3-flash",
|
|
42
|
-
name: "glm-5.3-flash",
|
|
43
|
-
context_window: 1048576,
|
|
44
|
-
max_output_tokens: 131072,
|
|
45
|
-
limits_source: "datasheet",
|
|
46
|
-
cost_source: "datasheet",
|
|
47
|
-
cost: {
|
|
48
|
-
input: 0.15,
|
|
49
|
-
output: 0.5,
|
|
50
|
-
cache_read: 0.029,
|
|
51
|
-
cache_write: 0,
|
|
52
|
-
},
|
|
53
|
-
reasoning: true,
|
|
54
|
-
} satisfies SupplementalModel);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("maps supports_image_input to image_input only when true", () => {
|
|
58
|
-
const out = parseDatasheet({
|
|
59
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": {
|
|
60
|
-
...glmFlash,
|
|
61
|
-
supports_image_input: true,
|
|
62
|
-
},
|
|
63
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p2": {
|
|
64
|
-
...glmFlash,
|
|
65
|
-
supports_image_input: false,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
expect(out).toHaveLength(2);
|
|
69
|
-
const byId = new Map(out.map((m) => [m.id, m]));
|
|
70
|
-
expect(byId.get("accounts/fireworks/models/glm-5p3-flash")?.image_input).toBe(true);
|
|
71
|
-
expect(byId.get("accounts/fireworks/models/glm-5p2")?.image_input).toBeUndefined();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("maps supports_vision to image_input — the upstream field name (1570 rows vs 8 for supports_image_input)", () => {
|
|
75
|
-
const out = parseDatasheet({
|
|
76
|
-
"fireworks_ai/accounts/fireworks/models/deepseek-v4-flash-vision-exp": {
|
|
77
|
-
...glmFlash,
|
|
78
|
-
supports_vision: true,
|
|
79
|
-
},
|
|
80
|
-
"fireworks_ai/accounts/fireworks/models/deepseek-v4-flash": {
|
|
81
|
-
...glmFlash,
|
|
82
|
-
supports_vision: false,
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
expect(out).toHaveLength(2);
|
|
86
|
-
const byId = new Map(out.map((m) => [m.id, m]));
|
|
87
|
-
expect(byId.get("accounts/fireworks/models/deepseek-v4-flash-vision-exp")?.image_input).toBe(true);
|
|
88
|
-
expect(byId.get("accounts/fireworks/models/deepseek-v4-flash")?.image_input).toBeUndefined();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("falls back to max_input_tokens when context_length is absent — upstream rows never carry context_length", () => {
|
|
92
|
-
const { context_length, ...upstreamShape } = glmFlash;
|
|
93
|
-
const out = parseDatasheet({
|
|
94
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": upstreamShape,
|
|
95
|
-
});
|
|
96
|
-
expect(out).toHaveLength(1);
|
|
97
|
-
expect(out[0].context_window).toBe(upstreamShape.max_input_tokens);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("reads cache_write from cache_creation_input_token_cost — the upstream field name (503 rows vs 0)", () => {
|
|
101
|
-
const out = parseDatasheet({
|
|
102
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": {
|
|
103
|
-
...glmFlash,
|
|
104
|
-
cache_creation_input_token_cost: 0.00000015,
|
|
105
|
-
},
|
|
106
|
-
});
|
|
107
|
-
expect(out).toHaveLength(1);
|
|
108
|
-
expect(out[0].cost?.cache_write).toBe(0.15);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test("drops non-chat entries, unknown providers, and rows without limits", () => {
|
|
112
|
-
const out = parseDatasheet({
|
|
113
|
-
"fireworks_ai/accounts/fireworks/models/qwen3-embedding-8b": {
|
|
114
|
-
...glmFlash,
|
|
115
|
-
mode: "embedding",
|
|
116
|
-
},
|
|
117
|
-
"other_provider/some/model": glmFlash,
|
|
118
|
-
"fireworks_ai/accounts/fireworks/models/no-limits": {
|
|
119
|
-
...glmFlash,
|
|
120
|
-
context_length: undefined,
|
|
121
|
-
max_input_tokens: undefined,
|
|
122
|
-
},
|
|
123
|
-
"fireworks_ai/accounts/fireworks/models/no-max-out": {
|
|
124
|
-
...glmFlash,
|
|
125
|
-
max_output_tokens: undefined,
|
|
126
|
-
},
|
|
127
|
-
});
|
|
128
|
-
expect(out).toEqual([]);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test("unpriced entry omits cost entirely and falls back to slug name", () => {
|
|
132
|
-
const out = parseDatasheet({
|
|
133
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": {
|
|
134
|
-
mode: "chat",
|
|
135
|
-
context_length: 1000,
|
|
136
|
-
max_output_tokens: 100,
|
|
137
|
-
},
|
|
138
|
-
});
|
|
139
|
-
expect(out[0]?.name).toBe("glm-5p3-flash");
|
|
140
|
-
expect(out[0]?.cost).toBeUndefined();
|
|
141
|
-
expect(out[0]?.cost_source).toBeUndefined();
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe("mergeModels", () => {
|
|
146
|
-
test("appends ids the CLI list lacks; CLI entry wins on collision", () => {
|
|
147
|
-
// mergeModels is generic over {id: string}: dedup is by id only, so the
|
|
148
|
-
// test uses plain {id, name} literals rather than full model shapes.
|
|
149
|
-
const cli = [
|
|
150
|
-
{ id: "a", name: "A" },
|
|
151
|
-
{ id: "b", name: "B" },
|
|
152
|
-
];
|
|
153
|
-
const extra = [
|
|
154
|
-
{ id: "b", name: "B-datasheet" },
|
|
155
|
-
{ id: "c", name: "C" },
|
|
156
|
-
];
|
|
157
|
-
expect(mergeModels(cli, extra)).toEqual([
|
|
158
|
-
{ id: "a", name: "A" },
|
|
159
|
-
{ id: "b", name: "B" },
|
|
160
|
-
{ id: "c", name: "C" },
|
|
161
|
-
]);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe("datasheetUrl", () => {
|
|
166
|
-
const tmp = mkdtempSync(join(tmpdir(), "gw-ds-"));
|
|
167
|
-
const proj = join(tmp, "proj");
|
|
168
|
-
const home = join(tmp, "home");
|
|
169
|
-
mkdirSync(join(proj, ".gateway"), { recursive: true });
|
|
170
|
-
mkdirSync(join(home, ".config", "bifrost"), { recursive: true });
|
|
171
|
-
|
|
172
|
-
test("env var wins over files", () => {
|
|
173
|
-
expect(datasheetUrl("https://env/datasheet", home, proj)).toBe(
|
|
174
|
-
"https://env/datasheet",
|
|
175
|
-
);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test("user file beats project file", () => {
|
|
179
|
-
writeFileSync(
|
|
180
|
-
join(home, ".config", "bifrost", "datasheet.json"),
|
|
181
|
-
`{"url": "https://user/datasheet"}`,
|
|
182
|
-
);
|
|
183
|
-
writeFileSync(
|
|
184
|
-
join(proj, ".gateway", "datasheet.json"),
|
|
185
|
-
`{"url": "https://proj/datasheet"}`,
|
|
186
|
-
);
|
|
187
|
-
expect(datasheetUrl(undefined, home, proj)).toBe("https://user/datasheet");
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
test("project file used when user-level absent; null when neither", () => {
|
|
191
|
-
// Self-seeded: does not depend on files written by earlier tests.
|
|
192
|
-
writeFileSync(
|
|
193
|
-
join(proj, ".gateway", "datasheet.json"),
|
|
194
|
-
`{"url": "https://proj/datasheet"}`,
|
|
195
|
-
);
|
|
196
|
-
// a home with no datasheet.json falls through to the project file
|
|
197
|
-
const homeEmpty = join(tmp, "home-empty");
|
|
198
|
-
mkdirSync(homeEmpty, { recursive: true });
|
|
199
|
-
expect(datasheetUrl(undefined, homeEmpty, proj)).toBe(
|
|
200
|
-
"https://proj/datasheet",
|
|
201
|
-
);
|
|
202
|
-
expect(datasheetUrl(undefined, homeEmpty, join(tmp, "other"))).toBeNull();
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test("non-https URLs are treated as absent and fall through", () => {
|
|
206
|
-
// Feeds steer local model listings, so every source must be TLS: an
|
|
207
|
-
// http:// project file is skipped, and with nothing else yielding a
|
|
208
|
-
// URL the result is null.
|
|
209
|
-
const projHttp = join(tmp, "proj-http");
|
|
210
|
-
mkdirSync(join(projHttp, ".gateway"), { recursive: true });
|
|
211
|
-
writeFileSync(
|
|
212
|
-
join(projHttp, ".gateway", "datasheet.json"),
|
|
213
|
-
`{"url": "http://insecure/datasheet"}`,
|
|
214
|
-
);
|
|
215
|
-
const homeEmpty = join(tmp, "home-empty-http");
|
|
216
|
-
mkdirSync(homeEmpty, { recursive: true });
|
|
217
|
-
expect(datasheetUrl(undefined, homeEmpty, projHttp)).toBeNull();
|
|
218
|
-
// A non-TLS env override likewise falls through to an https file.
|
|
219
|
-
writeFileSync(
|
|
220
|
-
join(projHttp, ".gateway", "datasheet.json"),
|
|
221
|
-
`{"url": "https://proj/datasheet"}`,
|
|
222
|
-
);
|
|
223
|
-
expect(
|
|
224
|
-
datasheetUrl("http://insecure-env/datasheet", homeEmpty, projHttp),
|
|
225
|
-
).toBe("https://proj/datasheet");
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
describe("fetchDatasheet", () => {
|
|
230
|
-
const tmp = mkdtempSync(join(tmpdir(), "gw-ds-cache-"));
|
|
231
|
-
// Each test gets its own cache file so results never depend on run order.
|
|
232
|
-
const cachePath = (name: string) => join(tmp, `${name}.json`);
|
|
233
|
-
const ok: FetchLike = async () => ({
|
|
234
|
-
ok: true,
|
|
235
|
-
json: async () => ({
|
|
236
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": glmFlash,
|
|
237
|
-
}),
|
|
238
|
-
});
|
|
239
|
-
const fail: FetchLike = async () => ({ ok: false, json: async () => ({}) });
|
|
240
|
-
|
|
241
|
-
test("fetches, returns parsed body, writes cache", async () => {
|
|
242
|
-
const body = await fetchDatasheet(
|
|
243
|
-
"https://feed/datasheet",
|
|
244
|
-
cachePath("t1"),
|
|
245
|
-
ok,
|
|
246
|
-
1000,
|
|
247
|
-
);
|
|
248
|
-
expect(
|
|
249
|
-
body?.["fireworks_ai/accounts/fireworks/models/glm-5p3-flash"],
|
|
250
|
-
).toBeDefined();
|
|
251
|
-
const cached = JSON.parse(readFileSync(cachePath("t1"), "utf8"));
|
|
252
|
-
expect(cached.fetchedAt).toBe(1000);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
test("fresh cache short-circuits the fetch", async () => {
|
|
256
|
-
// Seed a cache first so the counting call below has something to hit.
|
|
257
|
-
await fetchDatasheet("https://feed/datasheet", cachePath("t2"), ok, 1000);
|
|
258
|
-
let called = 0;
|
|
259
|
-
const counting: FetchLike = async () => {
|
|
260
|
-
called++;
|
|
261
|
-
return ok("u");
|
|
262
|
-
};
|
|
263
|
-
await fetchDatasheet(
|
|
264
|
-
"https://feed/datasheet",
|
|
265
|
-
cachePath("t2"),
|
|
266
|
-
counting,
|
|
267
|
-
1000 + 1000,
|
|
268
|
-
);
|
|
269
|
-
expect(called).toBe(0);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
test("expired cache refetches; fetch failure falls back to stale cache", async () => {
|
|
273
|
-
await fetchDatasheet("https://feed/datasheet", cachePath("t3"), ok, 1000);
|
|
274
|
-
const body = await fetchDatasheet(
|
|
275
|
-
"https://feed/datasheet",
|
|
276
|
-
cachePath("t3"),
|
|
277
|
-
fail,
|
|
278
|
-
1000 + 3_600_000 + 1,
|
|
279
|
-
);
|
|
280
|
-
expect(
|
|
281
|
-
body?.["fireworks_ai/accounts/fireworks/models/glm-5p3-flash"],
|
|
282
|
-
).toBeDefined();
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
test("fetch failure with no cache returns null", async () => {
|
|
286
|
-
expect(
|
|
287
|
-
await fetchDatasheet(
|
|
288
|
-
"https://feed/datasheet",
|
|
289
|
-
cachePath("absent"),
|
|
290
|
-
fail,
|
|
291
|
-
1000,
|
|
292
|
-
),
|
|
293
|
-
).toBeNull();
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
test("failed fetch with no cache backfills the cache in the background", async () => {
|
|
297
|
-
const path = cachePath("backfill");
|
|
298
|
-
let calls = 0;
|
|
299
|
-
const flaky: FetchLike = async () => {
|
|
300
|
-
calls++;
|
|
301
|
-
if (calls === 1) throw new Error("cold start");
|
|
302
|
-
return {
|
|
303
|
-
ok: true,
|
|
304
|
-
json: async () => ({
|
|
305
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": glmFlash,
|
|
306
|
-
}),
|
|
307
|
-
};
|
|
308
|
-
};
|
|
309
|
-
const first = await fetchDatasheet(
|
|
310
|
-
"https://feed/datasheet",
|
|
311
|
-
path,
|
|
312
|
-
flaky,
|
|
313
|
-
1000,
|
|
314
|
-
);
|
|
315
|
-
expect(first).toBeNull(); // this session skips the supplement
|
|
316
|
-
// the detached backfill (call 2) lands the cache for the next session
|
|
317
|
-
const deadline = Date.now() + 2000;
|
|
318
|
-
while (Date.now() < deadline) {
|
|
319
|
-
if (existsSync(path)) break;
|
|
320
|
-
await new Promise((r) => setTimeout(r, 25));
|
|
321
|
-
}
|
|
322
|
-
expect(calls).toBe(2);
|
|
323
|
-
const doc = JSON.parse(readFileSync(path, "utf8")) as {
|
|
324
|
-
fetchedAt: number;
|
|
325
|
-
};
|
|
326
|
-
expect(doc.fetchedAt).toBeGreaterThan(1000);
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
describe("end-to-end: CLI payload + datasheet extra -> opencode config models", () => {
|
|
331
|
-
test("supplemented id reaches the provider config with datasheet limits", () => {
|
|
332
|
-
const cliPayload: CliModels = {
|
|
333
|
-
models: [
|
|
334
|
-
{
|
|
335
|
-
id: "accounts/fireworks/models/glm-5p2",
|
|
336
|
-
name: "GLM 5.2",
|
|
337
|
-
context_window: 200000,
|
|
338
|
-
max_output_tokens: 32768,
|
|
339
|
-
limits_source: "gateway",
|
|
340
|
-
},
|
|
341
|
-
],
|
|
342
|
-
};
|
|
343
|
-
const extra = parseDatasheet({
|
|
344
|
-
"fireworks_ai/accounts/fireworks/models/glm-5p3-flash": glmFlash,
|
|
345
|
-
});
|
|
346
|
-
const merged = mergeModels(cliPayload.models ?? [], extra);
|
|
347
|
-
const cfg = toConfigModels({ models: merged }, undefined);
|
|
348
|
-
const flash = cfg["accounts/fireworks/models/glm-5p3-flash"];
|
|
349
|
-
expect(flash).toBeDefined();
|
|
350
|
-
expect(flash?.limit?.context).toBe(1048576);
|
|
351
|
-
expect(flash?.limit?.output).toBe(131072);
|
|
352
|
-
// CLI-listed model survives the merge untouched
|
|
353
|
-
expect(cfg["accounts/fireworks/models/glm-5p2"]).toBeDefined();
|
|
354
|
-
});
|
|
355
|
-
});
|
package/src/datasheet.ts
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Datasheet supplement: a second, faster id source for wildcard-provider
|
|
3
|
-
* models the CLI cannot list yet.
|
|
4
|
-
*
|
|
5
|
-
* The CLI sources Fireworks (wildcard-provider) ids from models.dev, which
|
|
6
|
-
* lags provider launches by days and is cached 24h. Bifrost itself syncs
|
|
7
|
-
* from the datasheet-proxy feed hourly, so a model merged there is routable —
|
|
8
|
-
* this module reads the same feed and merges the ids the CLI list lacks.
|
|
9
|
-
* The virtual key never passes through here: the feed is public.
|
|
10
|
-
*
|
|
11
|
-
* Off by default. Activated by (first wins; the URL must be https — any
|
|
12
|
-
* source yielding something else is treated as absent):
|
|
13
|
-
* 1. env GATEWAY_DATASHEET_URL
|
|
14
|
-
* 2. ~/.config/bifrost/datasheet.json {"url": "<function-url>/datasheet"}
|
|
15
|
-
* 3. <project-root>/.gateway/datasheet.json
|
|
16
|
-
*
|
|
17
|
-
* Security note: entries only produce local picker listings and metadata;
|
|
18
|
-
* requests still go to the CLI-resolved (allowlisted) gateway origin. A
|
|
19
|
-
* hostile feed cannot redirect traffic or exfiltrate the key.
|
|
20
|
-
*/
|
|
21
|
-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
22
|
-
import { dirname, join } from "node:path";
|
|
23
|
-
|
|
24
|
-
/** One row of the datasheet feed (flat map keyed by "<provider>/<model-id>"). */
|
|
25
|
-
export type DatasheetEntry = {
|
|
26
|
-
provider?: string;
|
|
27
|
-
mode?: string;
|
|
28
|
-
base_model?: string;
|
|
29
|
-
context_length?: number;
|
|
30
|
-
max_input_tokens?: number;
|
|
31
|
-
max_output_tokens?: number;
|
|
32
|
-
max_tokens?: number;
|
|
33
|
-
input_cost_per_token?: number;
|
|
34
|
-
output_cost_per_token?: number;
|
|
35
|
-
cache_read_input_token_cost?: number;
|
|
36
|
-
// Upstream rows name the cache-write rate `cache_creation_input_token_cost`
|
|
37
|
-
// (503 rows); the `cache_write_…` spelling appears on zero rows and is
|
|
38
|
-
// kept for symmetry with Bifrost's own pricing-card vocabulary.
|
|
39
|
-
cache_write_input_token_cost?: number;
|
|
40
|
-
cache_creation_input_token_cost?: number;
|
|
41
|
-
supports_reasoning?: boolean;
|
|
42
|
-
// Upstream rows express image capability as `supports_vision` (1,570
|
|
43
|
-
// rows); `supports_image_input` appears on only 8. Either true counts.
|
|
44
|
-
supports_vision?: boolean;
|
|
45
|
-
supports_image_input?: boolean;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/** A model in the same shape `gateway-cli models` emits (structural subset). */
|
|
49
|
-
export type SupplementalModel = {
|
|
50
|
-
id: string;
|
|
51
|
-
name: string;
|
|
52
|
-
context_window: number;
|
|
53
|
-
max_output_tokens: number;
|
|
54
|
-
limits_source: string;
|
|
55
|
-
cost_source?: string;
|
|
56
|
-
cost?: {
|
|
57
|
-
input: number;
|
|
58
|
-
output: number;
|
|
59
|
-
cache_read: number;
|
|
60
|
-
cache_write: number;
|
|
61
|
-
};
|
|
62
|
-
reasoning?: boolean;
|
|
63
|
-
image_input?: boolean;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/** Datasheet provider prefixes whose entries this module understands. */
|
|
67
|
-
const PROVIDER_PREFIXES = ["fireworks_ai/"];
|
|
68
|
-
|
|
69
|
-
/** How long a fetched feed is reused before refetching. */
|
|
70
|
-
const DATASHEET_TTL_MS = 60 * 60 * 1000;
|
|
71
|
-
|
|
72
|
-
/** Fetch hard timeout: the supplement must never block plugin init long. */
|
|
73
|
-
const FETCH_TIMEOUT_MS = 3000;
|
|
74
|
-
|
|
75
|
-
/** Budget for the background cache fill after a failed fetch: init is never
|
|
76
|
-
* blocked by it, so it can afford to outlast a cold feed start. */
|
|
77
|
-
const BACKFILL_TIMEOUT_MS = 15_000;
|
|
78
|
-
|
|
79
|
-
export type FetchLike = (
|
|
80
|
-
url: string,
|
|
81
|
-
init?: { signal?: AbortSignal },
|
|
82
|
-
) => Promise<{ ok: boolean; status?: number; json(): Promise<unknown> }>;
|
|
83
|
-
|
|
84
|
-
type CacheDoc = { fetchedAt: number; body: Record<string, DatasheetEntry> };
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Discover the feed URL: env, then user file, then project file. Only
|
|
88
|
-
* https:// URLs are honored; a source with anything else (including a
|
|
89
|
-
* non-TLS env override) falls through to the next candidate.
|
|
90
|
-
*/
|
|
91
|
-
export function datasheetUrl(
|
|
92
|
-
env: string | undefined,
|
|
93
|
-
home: string | undefined,
|
|
94
|
-
root: string,
|
|
95
|
-
): string | null {
|
|
96
|
-
if (env && env.startsWith("https://")) return env;
|
|
97
|
-
const candidates = [
|
|
98
|
-
home ? join(home, ".config", "bifrost", "datasheet.json") : null,
|
|
99
|
-
join(root, ".gateway", "datasheet.json"),
|
|
100
|
-
];
|
|
101
|
-
for (const p of candidates) {
|
|
102
|
-
if (!p) continue;
|
|
103
|
-
try {
|
|
104
|
-
const parsed = JSON.parse(readFileSync(p, "utf8")) as { url?: unknown };
|
|
105
|
-
if (
|
|
106
|
-
typeof parsed.url === "string" &&
|
|
107
|
-
parsed.url.startsWith("https://")
|
|
108
|
-
) {
|
|
109
|
-
return parsed.url;
|
|
110
|
-
}
|
|
111
|
-
} catch {
|
|
112
|
-
// absent or malformed: try the next candidate
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function readCache(path: string): CacheDoc | null {
|
|
119
|
-
try {
|
|
120
|
-
const doc = JSON.parse(readFileSync(path, "utf8")) as CacheDoc;
|
|
121
|
-
if (
|
|
122
|
-
typeof doc.fetchedAt === "number" &&
|
|
123
|
-
doc.body &&
|
|
124
|
-
typeof doc.body === "object"
|
|
125
|
-
) {
|
|
126
|
-
return doc;
|
|
127
|
-
}
|
|
128
|
-
} catch {
|
|
129
|
-
// absent or malformed: treat as no cache
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function writeCache(path: string, doc: CacheDoc): void {
|
|
135
|
-
try {
|
|
136
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
137
|
-
writeFileSync(path, JSON.stringify(doc));
|
|
138
|
-
} catch {
|
|
139
|
-
// cache write failure is non-fatal: refetch next init
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Default fetch adapter: narrows the platform Response to FetchLike's
|
|
145
|
-
* subset so callers can inject a mock without touching the global fetch.
|
|
146
|
-
*/
|
|
147
|
-
const defaultFetch: FetchLike = async (u, i) => {
|
|
148
|
-
const resp = await fetch(u, i);
|
|
149
|
-
return { ok: resp.ok, status: resp.status, json: () => resp.json() };
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Fetch the feed, honoring a fresh cache. On fetch failure a stale cache is
|
|
154
|
-
* served; with no cache at all the result is null and the caller skips the
|
|
155
|
-
* supplement. Garbage responses (non-object JSON, HTTP error) are treated
|
|
156
|
-
* as fetch failures so they never poison the cache. A fetch failure with no
|
|
157
|
-
* cache also triggers an unawaited backfill fetch (15s budget) that only
|
|
158
|
-
* writes the cache, so a cold feed costs one session, not one per start.
|
|
159
|
-
*/
|
|
160
|
-
export async function fetchDatasheet(
|
|
161
|
-
url: string,
|
|
162
|
-
cachePath: string,
|
|
163
|
-
fetchImpl: FetchLike = defaultFetch,
|
|
164
|
-
now: number = Date.now(),
|
|
165
|
-
): Promise<Record<string, DatasheetEntry> | null> {
|
|
166
|
-
const cached = readCache(cachePath);
|
|
167
|
-
if (cached && now - cached.fetchedAt < DATASHEET_TTL_MS) return cached.body;
|
|
168
|
-
try {
|
|
169
|
-
const resp = await fetchImpl(url, {
|
|
170
|
-
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
171
|
-
});
|
|
172
|
-
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
|
173
|
-
const body = (await resp.json()) as Record<string, DatasheetEntry>;
|
|
174
|
-
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
175
|
-
throw new Error("unexpected feed shape");
|
|
176
|
-
}
|
|
177
|
-
writeCache(cachePath, { fetchedAt: now, body });
|
|
178
|
-
return body;
|
|
179
|
-
} catch {
|
|
180
|
-
if (!cached) {
|
|
181
|
-
// Fire-and-forget warm fill: a slow feed (cold start) must not block
|
|
182
|
-
// plugin init, but the next session should find a warm cache. Never
|
|
183
|
-
// awaited; every failure path is swallowed (writeCache guards, and
|
|
184
|
-
// the .catch below covers the rest).
|
|
185
|
-
void fetchImpl(url, {
|
|
186
|
-
signal: AbortSignal.timeout(BACKFILL_TIMEOUT_MS),
|
|
187
|
-
})
|
|
188
|
-
.then(async (resp) => {
|
|
189
|
-
if (!resp.ok) return;
|
|
190
|
-
const body = (await resp.json()) as Record<string, DatasheetEntry>;
|
|
191
|
-
if (body && typeof body === "object" && !Array.isArray(body)) {
|
|
192
|
-
writeCache(cachePath, { fetchedAt: Date.now(), body });
|
|
193
|
-
}
|
|
194
|
-
})
|
|
195
|
-
.catch(() => {});
|
|
196
|
-
}
|
|
197
|
-
return cached?.body ?? null;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function perMillion(rate: number | undefined): number {
|
|
202
|
-
return typeof rate === "number" ? Math.round(rate * 1_000_000 * 1e8) / 1e8 : 0;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Turn feed rows into model entries. Keeps only chat-mode rows under a known
|
|
207
|
-
* provider prefix, requires usable limits, converts per-token rates to
|
|
208
|
-
* per-million (mirroring the CLI's perTokenToPerMTok).
|
|
209
|
-
*/
|
|
210
|
-
export function parseDatasheet(
|
|
211
|
-
body: Record<string, DatasheetEntry>,
|
|
212
|
-
): SupplementalModel[] {
|
|
213
|
-
const out: SupplementalModel[] = [];
|
|
214
|
-
for (const [key, e] of Object.entries(body)) {
|
|
215
|
-
if (!e || typeof e !== "object" || e.mode !== "chat") continue;
|
|
216
|
-
const prefix = PROVIDER_PREFIXES.find((p) => key.startsWith(p));
|
|
217
|
-
if (!prefix) continue;
|
|
218
|
-
const id = key.slice(prefix.length);
|
|
219
|
-
if (!id) continue;
|
|
220
|
-
// Upstream rows never carry `context_length` (0 of 4,336); they state
|
|
221
|
-
// `max_input_tokens` instead. Either sets the context window.
|
|
222
|
-
const contextLength =
|
|
223
|
-
typeof e.context_length === "number"
|
|
224
|
-
? e.context_length
|
|
225
|
-
: typeof e.max_input_tokens === "number"
|
|
226
|
-
? e.max_input_tokens
|
|
227
|
-
: undefined;
|
|
228
|
-
if (
|
|
229
|
-
typeof contextLength !== "number" ||
|
|
230
|
-
typeof e.max_output_tokens !== "number"
|
|
231
|
-
) {
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
const slug = id.split("/").pop() as string;
|
|
235
|
-
const priced =
|
|
236
|
-
typeof e.input_cost_per_token === "number" ||
|
|
237
|
-
typeof e.output_cost_per_token === "number";
|
|
238
|
-
out.push({
|
|
239
|
-
id,
|
|
240
|
-
name:
|
|
241
|
-
typeof e.base_model === "string" && e.base_model ? e.base_model : slug,
|
|
242
|
-
context_window: contextLength,
|
|
243
|
-
max_output_tokens: e.max_output_tokens,
|
|
244
|
-
limits_source: "datasheet",
|
|
245
|
-
...(priced
|
|
246
|
-
? {
|
|
247
|
-
cost_source: "datasheet",
|
|
248
|
-
cost: {
|
|
249
|
-
input: perMillion(e.input_cost_per_token),
|
|
250
|
-
output: perMillion(e.output_cost_per_token),
|
|
251
|
-
cache_read: perMillion(e.cache_read_input_token_cost),
|
|
252
|
-
cache_write: perMillion(
|
|
253
|
-
e.cache_write_input_token_cost ??
|
|
254
|
-
e.cache_creation_input_token_cost,
|
|
255
|
-
),
|
|
256
|
-
},
|
|
257
|
-
}
|
|
258
|
-
: {}),
|
|
259
|
-
...(e.supports_reasoning === true ? { reasoning: true } : {}),
|
|
260
|
-
...(e.supports_image_input === true || e.supports_vision === true
|
|
261
|
-
? { image_input: true }
|
|
262
|
-
: {}),
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
return out;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/** Merge supplemented ids into the CLI list. The CLI entry wins on collision. */
|
|
269
|
-
export function mergeModels<A extends { id: string }>(
|
|
270
|
-
cli: A[],
|
|
271
|
-
extra: A[],
|
|
272
|
-
): A[] {
|
|
273
|
-
const seen = new Set(cli.map((m) => m.id));
|
|
274
|
-
return [...cli, ...extra.filter((m) => !seen.has(m.id))];
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Entry point for the plugin: resolve the feed URL for this project, fetch
|
|
279
|
-
* (cached), and return supplementable models. Empty when the feature is off
|
|
280
|
-
* or the feed is unreachable and uncached — never throws, never blocks init
|
|
281
|
-
* beyond FETCH_TIMEOUT_MS.
|
|
282
|
-
*
|
|
283
|
-
* cacheDir may be any writable directory; the cache file is named
|
|
284
|
-
* datasheet-cache.json so it can never collide with a discovery config file.
|
|
285
|
-
*/
|
|
286
|
-
export async function datasheetSupplement(
|
|
287
|
-
root: string,
|
|
288
|
-
cacheDir: string,
|
|
289
|
-
fetchImpl?: FetchLike,
|
|
290
|
-
): Promise<SupplementalModel[]> {
|
|
291
|
-
const url = datasheetUrl(
|
|
292
|
-
process.env.GATEWAY_DATASHEET_URL,
|
|
293
|
-
process.env.HOME,
|
|
294
|
-
root,
|
|
295
|
-
);
|
|
296
|
-
if (!url) return [];
|
|
297
|
-
const body = await fetchDatasheet(
|
|
298
|
-
url,
|
|
299
|
-
join(cacheDir, "datasheet-cache.json"),
|
|
300
|
-
fetchImpl,
|
|
301
|
-
);
|
|
302
|
-
return body ? parseDatasheet(body) : [];
|
|
303
|
-
}
|