@stablekernel/opencode-bifrost 0.6.0 → 0.8.0-rc.1

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/src/v2.ts ADDED
@@ -0,0 +1,319 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ import { REFRESH_COMMAND, refresh, refreshReport, type Exec } from "./refresh.js";
4
+ import {
5
+ CLI,
6
+ PROVIDER_ID,
7
+ UNPRICED_SUFFIX,
8
+ listCliModels,
9
+ readCliKey,
10
+ readKeyFile,
11
+ REFRESH_DESCRIPTION,
12
+ resolveInfo,
13
+ type CliModels,
14
+ } from "./shared.js";
15
+
16
+ const exec = promisify(execFile);
17
+
18
+ /**
19
+ * The ai-sdk package OpenCode 2 instantiates for an OpenAI-compatible
20
+ * provider. Its own OpenAI-compatible providers carry this same id, so the
21
+ * gateway route is built by the host rather than bundled here.
22
+ */
23
+ const PROVIDER_PACKAGE = "@opencode/ai/providers/openai-compatible";
24
+
25
+ /**
26
+ * Directories searched for the CLI when it is not on the inherited PATH.
27
+ *
28
+ * OpenCode 2 serves plugins from a long-lived background service that is not
29
+ * started from the user's login shell, so its PATH frequently lacks the
30
+ * install dir `install.sh` uses. Without this, every CLI call fails with
31
+ * ENOENT and the provider silently never registers.
32
+ */
33
+ const FALLBACK_BIN_DIRS = [
34
+ `${process.env.HOME ?? ""}/.local/bin`,
35
+ "/usr/local/bin",
36
+ "/opt/homebrew/bin",
37
+ ];
38
+
39
+ /**
40
+ * One model in the shape OpenCode 2's catalog holds. Verified against
41
+ * OpenCode 2.0.14: `cost` is a list and `variants` a list of `{id, settings}`,
42
+ * where v1 used a rate-card object and an effort-keyed map.
43
+ */
44
+ export type V2Model = {
45
+ id: string;
46
+ modelID: string;
47
+ providerID: string;
48
+ name: string;
49
+ package: string;
50
+ capabilities: { tools: boolean; input: string[]; output: string[] };
51
+ variants: { id: string; settings: Record<string, unknown> }[];
52
+ // OpenCode 2's Model.Info.settings is open-ended (Schema.StructWithRest)
53
+ // and merges into the provider options, so it carries the seeded default
54
+ // reasoning effort; a chosen variant's settings override it.
55
+ settings?: Record<string, unknown>;
56
+ time: { released: number };
57
+ cost: { input: number; output: number; cache: { read: number; write: number } }[];
58
+ status: string;
59
+ enabled: boolean;
60
+ limit: { context: number; output: number };
61
+ };
62
+
63
+ /**
64
+ * Map the CLI's model list into OpenCode 2's catalog shape.
65
+ *
66
+ * Pricing is passed through only when known. OpenCode renders per-model cost,
67
+ * so a zeroed rate would advertise a paid model as free; an empty list leaves
68
+ * the figure blank and the name carries the caveat instead.
69
+ *
70
+ * The config cascade's default reasoning effort is seeded as the model's own
71
+ * `settings.reasoningEffort` when the model accepts it: 2.x merges model
72
+ * settings into the provider options, and a chosen variant's settings
73
+ * override them (packages/core/src/model-resolver.ts).
74
+ */
75
+ export function toV2Models(
76
+ payload: CliModels | null,
77
+ defaultEffort?: string,
78
+ ): V2Model[] {
79
+ const out: V2Model[] = [];
80
+ for (const m of payload?.models ?? []) {
81
+ if (!m?.id) continue;
82
+ if (m.disabled) continue; // unavailable on this key: never offer it
83
+ const efforts = m.reasoning ? (m.reasoning_efforts ?? []) : [];
84
+ out.push({
85
+ id: m.id,
86
+ modelID: m.id,
87
+ providerID: PROVIDER_ID,
88
+ name: m.cost ? m.name : `${m.name}${UNPRICED_SUFFIX}`,
89
+ package: PROVIDER_PACKAGE,
90
+ capabilities: {
91
+ tools: true,
92
+ input: m.image_input ? ["text", "image"] : ["text"],
93
+ output: ["text"],
94
+ },
95
+ variants: efforts.map((e) => ({
96
+ id: e,
97
+ // Served over the OpenAI-compatible wire, whose ai-sdk option is
98
+ // `reasoningEffort`; the host's native routes use `effort`.
99
+ settings: { reasoningEffort: e },
100
+ })),
101
+ // The gateway reports no release date, and inventing one would reorder a
102
+ // picker that sorts by it. Epoch means "unknown", not "ancient".
103
+ time: { released: 0 },
104
+ cost: m.cost
105
+ ? [
106
+ {
107
+ input: m.cost.input,
108
+ output: m.cost.output,
109
+ cache: { read: m.cost.cache_read, write: m.cost.cache_write },
110
+ },
111
+ ]
112
+ : [],
113
+ // Seeded default effort: model-level settings merge into the provider
114
+ // options; a variant's own settings.reasoningEffort wins when picked.
115
+ ...(defaultEffort && efforts.includes(defaultEffort)
116
+ ? { settings: { reasoningEffort: defaultEffort } }
117
+ : {}),
118
+ status: "active",
119
+ enabled: true,
120
+ limit: { context: m.context_window, output: m.max_output_tokens },
121
+ });
122
+ }
123
+ return out;
124
+ }
125
+
126
+ /**
127
+ * Run the CLI in `cwd` with the fallback install dirs appended to the
128
+ * inherited PATH (OpenCode 2 serves plugins from a long-lived background
129
+ * service whose PATH frequently lacks the install dir `install.sh` uses;
130
+ * appending keeps a real `gateway-cli` ahead of a stale fallback copy).
131
+ * Null on any failure, matching the v1 helper: a missing CLI is an expected
132
+ * state (sandboxes), not an error to surface.
133
+ */
134
+ async function cli(args: string[], cwd: string): Promise<string | null> {
135
+ const path = [process.env.PATH ?? "", ...FALLBACK_BIN_DIRS]
136
+ .filter((p) => p.length > 0)
137
+ .join(":");
138
+ try {
139
+ const { stdout } = await exec(CLI, args, {
140
+ cwd,
141
+ env: { ...process.env, PATH: path },
142
+ });
143
+ return stdout.trim();
144
+ } catch {
145
+ return null;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Minimal structural types for the part of OpenCode 2's plugin context this
151
+ * setup function uses.
152
+ *
153
+ * Declared locally on purpose. Importing the type from an OpenCode 2 package
154
+ * would make typechecking depend on a globally installed opencode being
155
+ * resolvable from node_modules, which breaks CI and any contributor without
156
+ * that exact layout. The host loads this file at runtime and injects the real
157
+ * object; only the call shapes below need to match.
158
+ *
159
+ * Verified against @opencode/plugin 2.0.14/2.0.22 (dist/promise/*.d.ts):
160
+ * `location.directory` is the session's project directory, `app` is only
161
+ * `{name, version, channel}` (no logger), `command`/`provider` expose a
162
+ * `transform` over a catalog editor whose `add` appends an entry, a command
163
+ * definition is `{name, description?, execute(input)}` (there is no command
164
+ * hook — the CommandDomain offers only list/transform/reload), and
165
+ * `session.synthetic` posts a message into a session, which is how a
166
+ * command's `execute` shows its result to the user. The 2.x TUI and web
167
+ * timeline render only the synthetic's `description`, so the report goes
168
+ * there, not just in `text`.
169
+ */
170
+
171
+ /** The command-catalog editor: `add` appends one command definition. */
172
+ type V2CommandInvocation = {
173
+ sessionID: string;
174
+ prompt: { text: string };
175
+ delivery: string;
176
+ };
177
+ type V2CommandEditor = {
178
+ add: (definition: {
179
+ name: string;
180
+ description?: string;
181
+ execute: (input: V2CommandInvocation) => Promise<void>;
182
+ }) => void;
183
+ };
184
+
185
+ /** The provider entry OpenCode 2's catalog holds for this plugin. */
186
+ type V2ProviderInfo = {
187
+ id: string;
188
+ name: string;
189
+ activation: string;
190
+ package: string;
191
+ // Request headers live at the top level of Provider.Info, not in settings:
192
+ // provider.add skips the AISDKNative rewrite that moves settings.headers to
193
+ // headers, and the model snapshot merges provider.headers into every model.
194
+ headers?: Record<string, string>;
195
+ settings: {
196
+ baseURL: string;
197
+ apiKey: string;
198
+ };
199
+ };
200
+
201
+ /** The provider-catalog editor: `add` appends the info plus its models. */
202
+ type V2ProviderEditor = {
203
+ add: (entry: { info: V2ProviderInfo; models: V2Model[] }) => void;
204
+ };
205
+ /** What setupV2 needs from the host's plugin context. */
206
+ type V2Context = {
207
+ location?: { directory: string };
208
+ command: {
209
+ transform: (fn: (editor: V2CommandEditor) => void) => Promise<void>;
210
+ };
211
+ provider: {
212
+ transform: (fn: (editor: V2ProviderEditor) => void) => Promise<void>;
213
+ };
214
+ session?: {
215
+ synthetic: (input: {
216
+ sessionID: string;
217
+ text: string;
218
+ description?: string;
219
+ delivery?: string;
220
+ resume?: boolean;
221
+ }) => Promise<unknown>;
222
+ };
223
+ };
224
+
225
+ /**
226
+ * OpenCode 2 `setup()` for the Bifrost provider. Mounted on the package's
227
+ * default export by ./index.ts, next to the v1 `server()`.
228
+ *
229
+ * OpenCode 2 replaced v1's config and auth hooks with registry transforms, so
230
+ * the provider is added to the catalog draft here. Discovery still runs
231
+ * gateway-cli in the session's directory, which is what keeps the per-project
232
+ * key cascade working (see BifrostGateway in ./index.ts).
233
+ *
234
+ * `exec` overrides the CLI runner (tests inject a fake); production uses the
235
+ * default, which appends the fallback install dirs to PATH.
236
+ *
237
+ * A throwing transform disables the plugin wholesale, so every step that can
238
+ * fail is contained: a missing CLI leaves the provider unregistered and the
239
+ * rest of the session intact.
240
+ */
241
+ export async function setupV2(ctx: V2Context, exec?: Exec): Promise<void> {
242
+ const directory: string = ctx?.location?.directory ?? process.cwd();
243
+ const run: Exec = exec ?? ((args) => cli(args, directory));
244
+ // Registered before the provider check on purpose: the case this command
245
+ // exists for is the sandbox with no gateway-cli at all, where resolve()
246
+ // returns null and nothing else would be registered. `known` starts empty
247
+ // and is filled in once the provider's models are registered.
248
+ const known: string[] = [];
249
+ await ctx.command.transform((editor) => {
250
+ editor.add({
251
+ name: REFRESH_COMMAND,
252
+ description: REFRESH_DESCRIPTION,
253
+ execute: async (input) => {
254
+ let text: string;
255
+ try {
256
+ const result = await refresh({
257
+ known,
258
+ exec: run,
259
+ readKeyFile,
260
+ storedKey: async () => null,
261
+ env: process.env,
262
+ });
263
+ text = refreshReport(result, result.source === "cli");
264
+ } catch {
265
+ text = "bifrost models: refresh failed.";
266
+ }
267
+ // The only channel a v2 command has for user-visible output: post the
268
+ // report as a synthetic message. The TUI and web timeline render only
269
+ // `description` for synthetics (rows.ts drops one without it), so the
270
+ // report must be the description, not just the text. resume: false
271
+ // keeps it from kicking off a model turn.
272
+ await ctx.session?.synthetic({
273
+ sessionID: input.sessionID,
274
+ text,
275
+ description: text,
276
+ resume: false,
277
+ });
278
+ },
279
+ });
280
+ });
281
+
282
+ const info = await resolveInfo(run);
283
+ if (!info) return;
284
+
285
+ const key = await readCliKey(run);
286
+ if (!key) return;
287
+
288
+ const models = toV2Models(await listCliModels(run, directory), info.reasoning_effort);
289
+ if (models.length === 0) return;
290
+
291
+ await ctx.provider.transform((editor) => {
292
+ editor.add({
293
+ info: {
294
+ id: PROVIDER_ID,
295
+ name: `Bifrost Gateway (${info.env})`,
296
+ activation: "enabled",
297
+ package: PROVIDER_PACKAGE,
298
+ // Org routing headers ride on every request; 2.x merges only the
299
+ // provider's top-level `headers` into its models, so they must not
300
+ // sit under settings (which the ai-sdk route forwards as provider
301
+ // options, never as HTTP headers).
302
+ ...(info.headers && Object.keys(info.headers).length > 0
303
+ ? { headers: info.headers }
304
+ : {}),
305
+ settings: {
306
+ baseURL: info.openai_base_url,
307
+ // Passed into the host's in-memory catalog draft for request
308
+ // headers; this package never writes or logs it. Discovery is
309
+ // CLI-only, so the allowlist gate above is what keeps the key away
310
+ // from any non-gateway origin.
311
+ apiKey: key,
312
+ },
313
+ },
314
+ models,
315
+ });
316
+ });
317
+
318
+ known.push(...models.map((m) => m.id));
319
+ }