@tagma/sdk 0.6.0 → 0.6.2

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.
Files changed (48) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +573 -573
  3. package/dist/bootstrap.d.ts +11 -1
  4. package/dist/bootstrap.d.ts.map +1 -1
  5. package/dist/bootstrap.js +18 -9
  6. package/dist/bootstrap.js.map +1 -1
  7. package/dist/drivers/opencode.d.ts.map +1 -1
  8. package/dist/drivers/opencode.js +47 -17
  9. package/dist/drivers/opencode.js.map +1 -1
  10. package/dist/engine.d.ts +8 -0
  11. package/dist/engine.d.ts.map +1 -1
  12. package/dist/engine.js +17 -16
  13. package/dist/engine.js.map +1 -1
  14. package/dist/plugin-registry.test.d.ts +2 -0
  15. package/dist/plugin-registry.test.d.ts.map +1 -0
  16. package/dist/plugin-registry.test.js +188 -0
  17. package/dist/plugin-registry.test.js.map +1 -0
  18. package/dist/registry.d.ts +52 -28
  19. package/dist/registry.d.ts.map +1 -1
  20. package/dist/registry.js +126 -91
  21. package/dist/registry.js.map +1 -1
  22. package/dist/sdk.d.ts +1 -1
  23. package/dist/sdk.d.ts.map +1 -1
  24. package/dist/sdk.js +1 -1
  25. package/dist/sdk.js.map +1 -1
  26. package/package.json +2 -2
  27. package/src/bootstrap.ts +46 -37
  28. package/src/completions/output-check.ts +92 -92
  29. package/src/dag.ts +245 -245
  30. package/src/drivers/opencode.ts +410 -371
  31. package/src/engine.ts +1228 -1220
  32. package/src/hooks.ts +193 -193
  33. package/src/middlewares/static-context.ts +49 -49
  34. package/src/pipeline-runner.ts +173 -173
  35. package/src/plugin-registry.test.ts +230 -0
  36. package/src/prompt-doc.ts +49 -49
  37. package/src/registry.ts +316 -267
  38. package/src/runner.ts +460 -460
  39. package/src/schema.test.ts +101 -101
  40. package/src/schema.ts +338 -338
  41. package/src/sdk.ts +120 -118
  42. package/src/task-ref.test.ts +401 -401
  43. package/src/task-ref.ts +120 -120
  44. package/src/validate-raw.ts +412 -412
  45. package/dist/drivers/claude-code.d.ts +0 -3
  46. package/dist/drivers/claude-code.d.ts.map +0 -1
  47. package/dist/drivers/claude-code.js +0 -225
  48. package/dist/drivers/claude-code.js.map +0 -1
@@ -1,371 +1,410 @@
1
- import type {
2
- DriverPlugin,
3
- DriverCapabilities,
4
- DriverResultMeta,
5
- TaskConfig,
6
- TrackConfig,
7
- DriverContext,
8
- SpawnSpec,
9
- } from '../types';
10
-
11
- const DEFAULT_MODEL = 'opencode/big-pickle';
12
-
13
- // NOTE on Windows multi-line prompts: `opencode` resolves to `opencode.cmd`,
14
- // an npm-generated batch wrapper. cmd.exe silently truncates argv elements
15
- // at the first newline, so a multi-line prompt reaches the model as only
16
- // its first line. The SDK's runner auto-unwraps npm .cmd shims into direct
17
- // `node <js-entry>` invocations so newlines survive, and this driver can
18
- // keep using the bare `opencode` name on every platform.
19
-
20
- // tagma uses a provider-neutral reasoning_effort vocabulary (low|medium|high)
21
- // but opencode's `--variant` is provider-specific (e.g. high|max|minimal).
22
- // Map the tagma values to the closest opencode variant:
23
- // low → minimal (least thinking)
24
- // medium → <no flag, provider default>
25
- // high → high (most thinking)
26
- // Unknown values pass through unchanged so users who target a specific
27
- // opencode variant (e.g. "max") still work.
28
- const EFFORT_TO_VARIANT: Record<string, string | null> = {
29
- low: 'minimal',
30
- medium: null,
31
- high: 'high',
32
- };
33
-
34
- // ── Auto-install + free-model picker ───────────────────────────────────────
35
- //
36
- // The opencode driver is SDK-built-in, but the `opencode` CLI isn't; we
37
- // auto-install it on demand (via `bun install -g opencode-ai`) and pick a
38
- // sensible default model from whatever the CLI reports. Both checks are
39
- // process-cached via module-level variables so each concern runs at most
40
- // once per SDK process.
41
- //
42
- // Design:
43
- // - User-provided `model:` wins; we only compute a default when it's empty.
44
- // - Failure modes never throw — they fall back to `DEFAULT_MODEL` and let
45
- // the subsequent `opencode run` spawn fail with its own error. Avoids
46
- // two confusing errors for one missing dependency.
47
-
48
- interface OpencodeModelInfo {
49
- id?: string;
50
- providerID?: string;
51
- status?: string;
52
- cost?: { input?: number; output?: number };
53
- limit?: { context?: number };
54
- }
55
-
56
- let opencodeReady: boolean | undefined;
57
- let cachedDefaultModel: string | undefined;
58
-
59
- async function runCapture(
60
- args: string[],
61
- ): Promise<{ code: number; stdout: string; stderr: string }> {
62
- try {
63
- const proc = Bun.spawn(args, { stdout: 'pipe', stderr: 'pipe' });
64
- const [stdout, stderr, code] = await Promise.all([
65
- new Response(proc.stdout).text(),
66
- new Response(proc.stderr).text(),
67
- proc.exited,
68
- ]);
69
- return { code, stdout, stderr };
70
- } catch {
71
- return { code: -1, stdout: '', stderr: '' };
72
- }
73
- }
74
-
75
- async function ensureOpencodeInstalled(): Promise<boolean> {
76
- if (opencodeReady !== undefined) return opencodeReady;
77
-
78
- // Probe existing install first — users who already have it get no delay.
79
- const probe = await runCapture(['opencode', '--version']);
80
- if (probe.code === 0) {
81
- opencodeReady = true;
82
- return true;
83
- }
84
-
85
- console.error(
86
- '[driver:opencode] opencode CLI not found — installing via `bun install -g opencode-ai`... (this may take up to a minute)',
87
- );
88
- // Use inherit here so the user sees bun's own progress during the one-time
89
- // install; runCapture would swallow it.
90
- const install = Bun.spawn(['bun', 'install', '-g', 'opencode-ai'], {
91
- stdout: 'inherit',
92
- stderr: 'inherit',
93
- });
94
- const installCode = await install.exited;
95
- if (installCode !== 0) {
96
- console.error('[driver:opencode] install failed — opencode run will likely fail below.');
97
- opencodeReady = false;
98
- return false;
99
- }
100
-
101
- // Bun installs globals under `~/.bun/bin` (or `%USERPROFILE%\.bun\bin`),
102
- // which isn't on this process's cached PATH unless the user already has
103
- // bun set up. Ask bun for the directory and prepend it so bare `opencode`
104
- // resolves in this process without requiring a shell reload.
105
- const bin = await runCapture(['bun', 'pm', 'bin', '-g']);
106
- if (bin.code === 0) {
107
- const dir = bin.stdout.trim();
108
- const sep = process.platform === 'win32' ? ';' : ':';
109
- const current = process.env.PATH ?? '';
110
- if (dir && !current.split(sep).includes(dir)) {
111
- process.env.PATH = `${dir}${sep}${current}`;
112
- }
113
- }
114
-
115
- const verify = await runCapture(['opencode', '--version']);
116
- opencodeReady = verify.code === 0;
117
- if (!opencodeReady) {
118
- console.error(
119
- '[driver:opencode] `opencode` still not resolvable after install — check that bun global bin is on PATH.',
120
- );
121
- }
122
- return opencodeReady;
123
- }
124
-
125
- // `opencode models --verbose` emits "<provider>/<id>\n{...json...}\n" pairs.
126
- // Walk balanced braces rather than split on newlines so we survive any
127
- // whitespace oddities in the JSON payload.
128
- function parseVerboseModels(stdout: string): OpencodeModelInfo[] {
129
- const out: OpencodeModelInfo[] = [];
130
- let depth = 0;
131
- let start = -1;
132
- for (let i = 0; i < stdout.length; i++) {
133
- const c = stdout[i];
134
- if (c === '{') {
135
- if (depth === 0) start = i;
136
- depth++;
137
- } else if (c === '}') {
138
- depth--;
139
- if (depth === 0 && start !== -1) {
140
- try {
141
- out.push(JSON.parse(stdout.slice(start, i + 1)) as OpencodeModelInfo);
142
- } catch {
143
- /* skip malformed block */
144
- }
145
- start = -1;
146
- }
147
- }
148
- }
149
- return out;
150
- }
151
-
152
- function pickFreeModel(models: OpencodeModelInfo[]): string | null {
153
- const fullId = (m: OpencodeModelInfo): string =>
154
- `${m.providerID ?? 'opencode'}/${m.id ?? ''}`;
155
- const eligible = models.filter((m) => {
156
- if (!m.id || m.id === 'big-pickle') return false;
157
- if (m.status && m.status !== 'active') return false;
158
- const cost = m.cost;
159
- if (!cost || cost.input !== 0 || cost.output !== 0) return false;
160
- const ctx = m.limit?.context;
161
- if (typeof ctx !== 'number' || ctx <= 128000) return false;
162
- return true;
163
- });
164
- // Prefer models explicitly labelled "-free" by the provider those are
165
- // a stronger stability signal than "cost happens to be 0 right now".
166
- const preferred = eligible.filter((m) => m.id?.endsWith('-free'));
167
- const pool = preferred.length > 0 ? preferred : eligible;
168
- if (pool.length === 0) return null;
169
- // Deterministic pick: sort by full id so upstream model-list reordering
170
- // doesn't flip our choice between runs.
171
- pool.sort((a, b) => fullId(a).localeCompare(fullId(b)));
172
- return fullId(pool[0]);
173
- }
174
-
175
- async function resolveDefaultModel(): Promise<string> {
176
- if (cachedDefaultModel !== undefined) return cachedDefaultModel;
177
- const ready = await ensureOpencodeInstalled();
178
- if (!ready) {
179
- cachedDefaultModel = DEFAULT_MODEL;
180
- return cachedDefaultModel;
181
- }
182
- console.error('[driver:opencode] resolving free opencode model...');
183
- const { code, stdout } = await runCapture(['opencode', 'models', '--verbose']);
184
- if (code !== 0) {
185
- cachedDefaultModel = DEFAULT_MODEL;
186
- return cachedDefaultModel;
187
- }
188
- const picked = pickFreeModel(parseVerboseModels(stdout));
189
- cachedDefaultModel = picked ?? DEFAULT_MODEL;
190
- console.error(`[driver:opencode] default model: ${cachedDefaultModel}`);
191
- return cachedDefaultModel;
192
- }
193
-
194
- export const OpenCodeDriver: DriverPlugin = {
195
- name: 'opencode',
196
-
197
- capabilities: {
198
- sessionResume: true, // supports --session
199
- systemPrompt: false, // no --system-prompt flag; prepend to prompt instead
200
- outputFormat: true, // supports --format json
201
- } satisfies DriverCapabilities,
202
-
203
- resolveModel(): string {
204
- return DEFAULT_MODEL;
205
- },
206
-
207
- async buildCommand(task: TaskConfig, track: TrackConfig, ctx: DriverContext): Promise<SpawnSpec> {
208
- const explicitModel = task.model ?? track.model;
209
- // Always make sure the opencode CLI is usable before we spawn it — even
210
- // when the user pinned a model. If missing, ensureOpencodeInstalled
211
- // auto-installs it via `bun install -g opencode-ai`.
212
- if (explicitModel) await ensureOpencodeInstalled();
213
- // Otherwise resolveDefaultModel both ensures the CLI and picks a free
214
- // model from `opencode models --verbose` (cached per-process).
215
- const model = explicitModel ?? (await resolveDefaultModel());
216
- // Resolve reasoning_effort opencode --variant. SDK schema layer already
217
- // resolved task track pipeline inheritance, so we only need to read
218
- // task.reasoning_effort here.
219
- const rawEffort = task.reasoning_effort ?? track.reasoning_effort;
220
- const variant = rawEffort
221
- ? rawEffort in EFFORT_TO_VARIANT
222
- ? EFFORT_TO_VARIANT[rawEffort]
223
- : rawEffort
224
- : null;
225
-
226
- let prompt = task.prompt!;
227
-
228
- // agent_profile has no dedicated flag; prepend to prompt
229
- const profile = task.agent_profile ?? track.agent_profile;
230
- if (profile) {
231
- prompt = `[Role]\n${profile}\n\n[Task]\n${prompt}`;
232
- }
233
-
234
- // continue_from: prefer session resume, fall back to text injection
235
- let sessionId: string | null = null;
236
- if (task.continue_from) {
237
- sessionId = ctx.sessionMap.get(task.continue_from) ?? null;
238
- if (!sessionId) {
239
- // no session — degrade to text context passthrough
240
- let prev: string | null = null;
241
- if (ctx.normalizedMap.has(task.continue_from)) {
242
- prev = ctx.normalizedMap.get(task.continue_from)!;
243
- }
244
- if (prev !== null) {
245
- prompt = `[Previous Output]\n${prev}\n\n[Current Task]\n${prompt}`;
246
- }
247
- }
248
- }
249
-
250
- // opencode run does not support stdin (no `-` placeholder like codex exec).
251
- // Prompt is always a positional argument. Flags must be declared before `--`;
252
- // the prompt follows after so that leading `--flag` content cannot be
253
- // misread by opencode's argument parser (flag-injection mitigation).
254
- // Shell-level injection is already prevented by Bun.spawn's direct argv array.
255
- // Windows cmd.exe argv truncation on the `.cmd` wrapper is handled by the
256
- // SDK runner's shim unwrapping see note at the top of this file.
257
- const args: string[] = [
258
- 'opencode',
259
- 'run',
260
- '--model',
261
- model,
262
- '--format',
263
- 'json', // JSON output for parseResult
264
- ];
265
-
266
- // `--variant` must precede `--` like every other flag. opencode rejects
267
- // unknown variant names with a clear error, so we don't pre-validate.
268
- if (variant) {
269
- args.push('--variant', variant);
270
- }
271
-
272
- // session resume (must appear before --)
273
- if (sessionId) {
274
- args.push('--session', sessionId);
275
- }
276
-
277
- // `--` (POSIX end-of-options) isolates prompt from flag parsing
278
- args.push('--', prompt);
279
-
280
- return { args, cwd: task.cwd ?? ctx.workDir };
281
- },
282
-
283
- parseResult(stdout: string): DriverResultMeta {
284
- // opencode --format json emits NDJSON — one JSON object per line
285
- // (step_start / text / step_finish / …). The previous single
286
- // `JSON.parse(stdout)` always threw on this shape and fell through to
287
- // the catch, returning sessionId:null and losing session resume.
288
- // Walk line-by-line, pick up the first sessionID we see, concatenate
289
- // any text-type parts into normalizedOutput, and bail early on error
290
- // payloads.
291
- const lines = stdout.split(/\r?\n/);
292
- let sessionId: string | undefined;
293
- const textParts: string[] = [];
294
- let sawAnyJson = false;
295
- let errorReason: string | null = null;
296
-
297
- for (const raw of lines) {
298
- const line = raw.trim();
299
- if (!line) continue;
300
- let json: Record<string, unknown>;
301
- try {
302
- json = JSON.parse(line) as Record<string, unknown>;
303
- } catch {
304
- continue; // tolerate interleaved non-JSON noise
305
- }
306
- sawAnyJson = true;
307
-
308
- // M12: opencode sometimes emits {type:"error", error:{...}} with
309
- // exit 0 for transient API failures. Force-fail so downstream
310
- // skip_downstream / stop_all kicks in.
311
- if (json.type === 'error') {
312
- const err = json.error as { message?: unknown } | string | undefined;
313
- const msg =
314
- typeof err === 'object' && err !== null && typeof err.message === 'string'
315
- ? err.message
316
- : typeof err === 'string'
317
- ? err
318
- : null;
319
- errorReason = msg
320
- ? `opencode reported error: ${msg}`
321
- : 'opencode emitted an error JSON payload';
322
- // D21: stop at the first error. Continuing meant subsequent text
323
- // lines got accumulated into `textParts` only to be discarded by
324
- // the error-return below, and a later `{type:"error"}` would
325
- // silently overwrite the original cause operators then debugged
326
- // a downstream symptom while the root-cause line scrolled past.
327
- break;
328
- }
329
-
330
- // Session id — opencode uses `sessionID` (camelCase with capital D).
331
- // Keep `session_id` / `sessionId` as fallbacks for forward/backward
332
- // compatibility with other shapes.
333
- if (!sessionId) {
334
- const sid =
335
- (json.sessionID as string | undefined) ??
336
- (json.session_id as string | undefined) ??
337
- (json.sessionId as string | undefined) ??
338
- null;
339
- if (typeof sid === 'string' && sid.length > 0) sessionId = sid;
340
- }
341
-
342
- // Extract human-readable text from text-type parts.
343
- if (json.type === 'text') {
344
- const part = json.part as { text?: unknown } | undefined;
345
- if (part && typeof part.text === 'string') {
346
- textParts.push(part.text);
347
- }
348
- } else if (typeof json.result === 'string') {
349
- textParts.push(json.result);
350
- } else if (typeof json.content === 'string') {
351
- textParts.push(json.content);
352
- }
353
- }
354
-
355
- if (errorReason) {
356
- return { forceFailure: true, forceFailureReason: errorReason };
357
- }
358
-
359
- // If nothing parsed as JSON, treat stdout as plain text.
360
- const normalizedOutput = !sawAnyJson
361
- ? stdout
362
- : textParts.length > 0
363
- ? textParts.join('\n')
364
- : stdout;
365
-
366
- return {
367
- sessionId,
368
- normalizedOutput,
369
- };
370
- },
371
- };
1
+ import type {
2
+ DriverPlugin,
3
+ DriverCapabilities,
4
+ DriverResultMeta,
5
+ TaskConfig,
6
+ TrackConfig,
7
+ DriverContext,
8
+ SpawnSpec,
9
+ } from '../types';
10
+
11
+ const DEFAULT_MODEL = 'opencode/big-pickle';
12
+
13
+ // NOTE on Windows multi-line prompts: `opencode` resolves to `opencode.cmd`,
14
+ // an npm-generated batch wrapper. cmd.exe silently truncates argv elements
15
+ // at the first newline, so a multi-line prompt reaches the model as only
16
+ // its first line. The SDK's runner auto-unwraps npm .cmd shims into direct
17
+ // `node <js-entry>` invocations so newlines survive, and this driver can
18
+ // keep using the bare `opencode` name on every platform.
19
+
20
+ // tagma uses a provider-neutral reasoning_effort vocabulary (low|medium|high)
21
+ // but opencode's `--variant` is provider-specific (e.g. high|max|minimal).
22
+ // Map the tagma values to the closest opencode variant:
23
+ // low → minimal (least thinking)
24
+ // medium → <no flag, provider default>
25
+ // high → high (most thinking)
26
+ // Unknown values pass through unchanged so users who target a specific
27
+ // opencode variant (e.g. "max") still work.
28
+ const EFFORT_TO_VARIANT: Record<string, string | null> = {
29
+ low: 'minimal',
30
+ medium: null,
31
+ high: 'high',
32
+ };
33
+
34
+ // ── Auto-install + free-model picker ───────────────────────────────────────
35
+ //
36
+ // The opencode driver is SDK-built-in, but the `opencode` CLI isn't. Two
37
+ // provisioning paths:
38
+ //
39
+ // 1. Desktop app the Electron shell ships a platform-matched opencode
40
+ // binary under resources/opencode/bin/, prepended to the sidecar's PATH
41
+ // at launch (see apps/electron/src/runtime-paths.ts). In-app updates
42
+ // drop a newer copy into userData/opencode/bin/ which wins via PATH
43
+ // precedence. That path resolves on the first `opencode --version`
44
+ // probe below; no auto-install ever fires.
45
+ //
46
+ // 2. SDK direct use when bun is on PATH we fall through to
47
+ // `bun install -g opencode-ai`, identical to the pre-desktop behavior.
48
+ //
49
+ // When BOTH paths are unavailable (no bundled binary, no bun) we fail with
50
+ // an actionable error pointing at the desktop Settings panel instead of
51
+ // silently letting `opencode run` ENOENT later — the old behavior swallowed
52
+ // the root cause in runCapture's catch and left the user staring at an
53
+ // opaque "exit code -1". The result is process-memoized so subsequent
54
+ // tasks in the same run surface the same error without re-probing.
55
+
56
+ interface OpencodeModelInfo {
57
+ id?: string;
58
+ providerID?: string;
59
+ status?: string;
60
+ cost?: { input?: number; output?: number };
61
+ limit?: { context?: number };
62
+ }
63
+
64
+ // Memoize BOTH success and failure. On failure we stash the message so every
65
+ // subsequent ensureOpencodeInstalled() throws the identical error — re-running
66
+ // the bun-install probe for each task of a failed run would just be slow and
67
+ // produce confusing interleaved stderr.
68
+ let opencodeReady: boolean | undefined;
69
+ let opencodeReadyError: string | undefined;
70
+ let cachedDefaultModel: string | undefined;
71
+
72
+ async function runCapture(
73
+ args: string[],
74
+ ): Promise<{ code: number; stdout: string; stderr: string }> {
75
+ try {
76
+ const proc = Bun.spawn(args, { stdout: 'pipe', stderr: 'pipe' });
77
+ const [stdout, stderr, code] = await Promise.all([
78
+ new Response(proc.stdout).text(),
79
+ new Response(proc.stderr).text(),
80
+ proc.exited,
81
+ ]);
82
+ return { code, stdout, stderr };
83
+ } catch {
84
+ return { code: -1, stdout: '', stderr: '' };
85
+ }
86
+ }
87
+
88
+ // Shared tail for every failure message the Tagma desktop app exposes a
89
+ // one-click installer at the same npm source path this driver would reach
90
+ // for, so point users there first. Users running the SDK as a library still
91
+ // see the manual bun/npm hint.
92
+ const SETUP_HINT =
93
+ 'If you are using the Tagma desktop app, open Editor Settings → OpenCode CLI to install or update the bundled binary. ' +
94
+ 'Otherwise install it manually: `bun install -g opencode-ai` or `npm install -g opencode-ai`.';
95
+
96
+ async function ensureOpencodeInstalled(): Promise<void> {
97
+ if (opencodeReady === true) return;
98
+ if (opencodeReady === false && opencodeReadyError) {
99
+ throw new Error(opencodeReadyError);
100
+ }
101
+
102
+ // Probe existing install first — this is the hot path for desktop users
103
+ // (bundled binary in PATH) and for anyone who already has opencode.
104
+ const probe = await runCapture(['opencode', '--version']);
105
+ if (probe.code === 0) {
106
+ opencodeReady = true;
107
+ return;
108
+ }
109
+
110
+ // Distinguish "bun is missing" from "bun is here but install failed" so
111
+ // the error we surface points at the right next step. If bun is absent we
112
+ // skip the install attempt entirely — spawning with `bun` as argv[0]
113
+ // would just ENOENT inside runCapture's catch and look identical to a
114
+ // failed install.
115
+ const bunProbe = await runCapture(['bun', '--version']);
116
+ if (bunProbe.code !== 0) {
117
+ opencodeReady = false;
118
+ opencodeReadyError = `OpenCode CLI is not available and \`bun\` is not installed. ${SETUP_HINT}`;
119
+ throw new Error(opencodeReadyError);
120
+ }
121
+
122
+ console.error(
123
+ '[driver:opencode] opencode CLI not found — installing via `bun install -g opencode-ai`... (this may take up to a minute)',
124
+ );
125
+ // Use inherit here so the user sees bun's own progress during the one-time
126
+ // install; runCapture would swallow it.
127
+ const install = Bun.spawn(['bun', 'install', '-g', 'opencode-ai'], {
128
+ stdout: 'inherit',
129
+ stderr: 'inherit',
130
+ });
131
+ const installCode = await install.exited;
132
+ if (installCode !== 0) {
133
+ opencodeReady = false;
134
+ opencodeReadyError = `\`bun install -g opencode-ai\` failed (exit code ${installCode}). ${SETUP_HINT}`;
135
+ throw new Error(opencodeReadyError);
136
+ }
137
+
138
+ // Bun installs globals under `~/.bun/bin` (or `%USERPROFILE%\.bun\bin`),
139
+ // which isn't on this process's cached PATH unless the user already has
140
+ // bun set up. Ask bun for the directory and prepend it so bare `opencode`
141
+ // resolves in this process without requiring a shell reload.
142
+ const bin = await runCapture(['bun', 'pm', 'bin', '-g']);
143
+ if (bin.code === 0) {
144
+ const dir = bin.stdout.trim();
145
+ const sep = process.platform === 'win32' ? ';' : ':';
146
+ const current = process.env.PATH ?? '';
147
+ if (dir && !current.split(sep).includes(dir)) {
148
+ process.env.PATH = `${dir}${sep}${current}`;
149
+ }
150
+ }
151
+
152
+ const verify = await runCapture(['opencode', '--version']);
153
+ if (verify.code !== 0) {
154
+ opencodeReady = false;
155
+ opencodeReadyError =
156
+ '`opencode` is not resolvable after `bun install -g opencode-ai` completed. ' +
157
+ "Bun's global bin directory is probably not on PATH — add it manually or restart the app.";
158
+ throw new Error(opencodeReadyError);
159
+ }
160
+ opencodeReady = true;
161
+ }
162
+
163
+ // `opencode models --verbose` emits "<provider>/<id>\n{...json...}\n" pairs.
164
+ // Walk balanced braces rather than split on newlines so we survive any
165
+ // whitespace oddities in the JSON payload.
166
+ function parseVerboseModels(stdout: string): OpencodeModelInfo[] {
167
+ const out: OpencodeModelInfo[] = [];
168
+ let depth = 0;
169
+ let start = -1;
170
+ for (let i = 0; i < stdout.length; i++) {
171
+ const c = stdout[i];
172
+ if (c === '{') {
173
+ if (depth === 0) start = i;
174
+ depth++;
175
+ } else if (c === '}') {
176
+ depth--;
177
+ if (depth === 0 && start !== -1) {
178
+ try {
179
+ out.push(JSON.parse(stdout.slice(start, i + 1)) as OpencodeModelInfo);
180
+ } catch {
181
+ /* skip malformed block */
182
+ }
183
+ start = -1;
184
+ }
185
+ }
186
+ }
187
+ return out;
188
+ }
189
+
190
+ function pickFreeModel(models: OpencodeModelInfo[]): string | null {
191
+ const fullId = (m: OpencodeModelInfo): string =>
192
+ `${m.providerID ?? 'opencode'}/${m.id ?? ''}`;
193
+ const eligible = models.filter((m) => {
194
+ if (!m.id || m.id === 'big-pickle') return false;
195
+ if (m.status && m.status !== 'active') return false;
196
+ const cost = m.cost;
197
+ if (!cost || cost.input !== 0 || cost.output !== 0) return false;
198
+ const ctx = m.limit?.context;
199
+ if (typeof ctx !== 'number' || ctx <= 128000) return false;
200
+ return true;
201
+ });
202
+ // Prefer models explicitly labelled "-free" by the provider — those are
203
+ // a stronger stability signal than "cost happens to be 0 right now".
204
+ const preferred = eligible.filter((m) => m.id?.endsWith('-free'));
205
+ const pool = preferred.length > 0 ? preferred : eligible;
206
+ if (pool.length === 0) return null;
207
+ // Deterministic pick: sort by full id so upstream model-list reordering
208
+ // doesn't flip our choice between runs.
209
+ pool.sort((a, b) => fullId(a).localeCompare(fullId(b)));
210
+ return fullId(pool[0]);
211
+ }
212
+
213
+ async function resolveDefaultModel(): Promise<string> {
214
+ if (cachedDefaultModel !== undefined) return cachedDefaultModel;
215
+ // ensureOpencodeInstalled now throws with an actionable message when the
216
+ // CLI can't be provisioned, so we let the error bubble up to the task
217
+ // runner instead of silently falling back to DEFAULT_MODEL (which would
218
+ // produce a second confusing ENOENT a few lines later in `opencode run`).
219
+ await ensureOpencodeInstalled();
220
+ console.error('[driver:opencode] resolving free opencode model...');
221
+ const { code, stdout } = await runCapture(['opencode', 'models', '--verbose']);
222
+ if (code !== 0) {
223
+ cachedDefaultModel = DEFAULT_MODEL;
224
+ return cachedDefaultModel;
225
+ }
226
+ const picked = pickFreeModel(parseVerboseModels(stdout));
227
+ cachedDefaultModel = picked ?? DEFAULT_MODEL;
228
+ console.error(`[driver:opencode] default model: ${cachedDefaultModel}`);
229
+ return cachedDefaultModel;
230
+ }
231
+
232
+ export const OpenCodeDriver: DriverPlugin = {
233
+ name: 'opencode',
234
+
235
+ capabilities: {
236
+ sessionResume: true, // supports --session
237
+ systemPrompt: false, // no --system-prompt flag; prepend to prompt instead
238
+ outputFormat: true, // supports --format json
239
+ } satisfies DriverCapabilities,
240
+
241
+ resolveModel(): string {
242
+ return DEFAULT_MODEL;
243
+ },
244
+
245
+ async buildCommand(task: TaskConfig, track: TrackConfig, ctx: DriverContext): Promise<SpawnSpec> {
246
+ const explicitModel = task.model ?? track.model;
247
+ // Always make sure the opencode CLI is usable before we spawn it — even
248
+ // when the user pinned a model. ensureOpencodeInstalled throws with an
249
+ // actionable message when the binary is neither present on PATH (desktop
250
+ // bundles it there via runtime-paths.ts) nor installable via bun.
251
+ if (explicitModel) await ensureOpencodeInstalled();
252
+ // Otherwise resolveDefaultModel both ensures the CLI and picks a free
253
+ // model from `opencode models --verbose` (cached per-process).
254
+ const model = explicitModel ?? (await resolveDefaultModel());
255
+ // Resolve reasoning_effort opencode --variant. SDK schema layer already
256
+ // resolved task track pipeline inheritance, so we only need to read
257
+ // task.reasoning_effort here.
258
+ const rawEffort = task.reasoning_effort ?? track.reasoning_effort;
259
+ const variant = rawEffort
260
+ ? rawEffort in EFFORT_TO_VARIANT
261
+ ? EFFORT_TO_VARIANT[rawEffort]
262
+ : rawEffort
263
+ : null;
264
+
265
+ let prompt = task.prompt!;
266
+
267
+ // agent_profile has no dedicated flag; prepend to prompt
268
+ const profile = task.agent_profile ?? track.agent_profile;
269
+ if (profile) {
270
+ prompt = `[Role]\n${profile}\n\n[Task]\n${prompt}`;
271
+ }
272
+
273
+ // continue_from: prefer session resume, fall back to text injection
274
+ let sessionId: string | null = null;
275
+ if (task.continue_from) {
276
+ sessionId = ctx.sessionMap.get(task.continue_from) ?? null;
277
+ if (!sessionId) {
278
+ // no session — degrade to text context passthrough
279
+ let prev: string | null = null;
280
+ if (ctx.normalizedMap.has(task.continue_from)) {
281
+ prev = ctx.normalizedMap.get(task.continue_from)!;
282
+ }
283
+ if (prev !== null) {
284
+ prompt = `[Previous Output]\n${prev}\n\n[Current Task]\n${prompt}`;
285
+ }
286
+ }
287
+ }
288
+
289
+ // opencode run does not support stdin (no `-` placeholder like codex exec).
290
+ // Prompt is always a positional argument. Flags must be declared before `--`;
291
+ // the prompt follows after so that leading `--flag` content cannot be
292
+ // misread by opencode's argument parser (flag-injection mitigation).
293
+ // Shell-level injection is already prevented by Bun.spawn's direct argv array.
294
+ // Windows cmd.exe argv truncation on the `.cmd` wrapper is handled by the
295
+ // SDK runner's shim unwrapping see note at the top of this file.
296
+ const args: string[] = [
297
+ 'opencode',
298
+ 'run',
299
+ '--model',
300
+ model,
301
+ '--format',
302
+ 'json', // JSON output for parseResult
303
+ ];
304
+
305
+ // `--variant` must precede `--` like every other flag. opencode rejects
306
+ // unknown variant names with a clear error, so we don't pre-validate.
307
+ if (variant) {
308
+ args.push('--variant', variant);
309
+ }
310
+
311
+ // session resume (must appear before --)
312
+ if (sessionId) {
313
+ args.push('--session', sessionId);
314
+ }
315
+
316
+ // `--` (POSIX end-of-options) isolates prompt from flag parsing
317
+ args.push('--', prompt);
318
+
319
+ return { args, cwd: task.cwd ?? ctx.workDir };
320
+ },
321
+
322
+ parseResult(stdout: string): DriverResultMeta {
323
+ // opencode --format json emits NDJSON one JSON object per line
324
+ // (step_start / text / step_finish / …). The previous single
325
+ // `JSON.parse(stdout)` always threw on this shape and fell through to
326
+ // the catch, returning sessionId:null and losing session resume.
327
+ // Walk line-by-line, pick up the first sessionID we see, concatenate
328
+ // any text-type parts into normalizedOutput, and bail early on error
329
+ // payloads.
330
+ const lines = stdout.split(/\r?\n/);
331
+ let sessionId: string | undefined;
332
+ const textParts: string[] = [];
333
+ let sawAnyJson = false;
334
+ let errorReason: string | null = null;
335
+
336
+ for (const raw of lines) {
337
+ const line = raw.trim();
338
+ if (!line) continue;
339
+ let json: Record<string, unknown>;
340
+ try {
341
+ json = JSON.parse(line) as Record<string, unknown>;
342
+ } catch {
343
+ continue; // tolerate interleaved non-JSON noise
344
+ }
345
+ sawAnyJson = true;
346
+
347
+ // M12: opencode sometimes emits {type:"error", error:{...}} with
348
+ // exit 0 for transient API failures. Force-fail so downstream
349
+ // skip_downstream / stop_all kicks in.
350
+ if (json.type === 'error') {
351
+ const err = json.error as { message?: unknown } | string | undefined;
352
+ const msg =
353
+ typeof err === 'object' && err !== null && typeof err.message === 'string'
354
+ ? err.message
355
+ : typeof err === 'string'
356
+ ? err
357
+ : null;
358
+ errorReason = msg
359
+ ? `opencode reported error: ${msg}`
360
+ : 'opencode emitted an error JSON payload';
361
+ // D21: stop at the first error. Continuing meant subsequent text
362
+ // lines got accumulated into `textParts` only to be discarded by
363
+ // the error-return below, and a later `{type:"error"}` would
364
+ // silently overwrite the original cause — operators then debugged
365
+ // a downstream symptom while the root-cause line scrolled past.
366
+ break;
367
+ }
368
+
369
+ // Session id — opencode uses `sessionID` (camelCase with capital D).
370
+ // Keep `session_id` / `sessionId` as fallbacks for forward/backward
371
+ // compatibility with other shapes.
372
+ if (!sessionId) {
373
+ const sid =
374
+ (json.sessionID as string | undefined) ??
375
+ (json.session_id as string | undefined) ??
376
+ (json.sessionId as string | undefined) ??
377
+ null;
378
+ if (typeof sid === 'string' && sid.length > 0) sessionId = sid;
379
+ }
380
+
381
+ // Extract human-readable text from text-type parts.
382
+ if (json.type === 'text') {
383
+ const part = json.part as { text?: unknown } | undefined;
384
+ if (part && typeof part.text === 'string') {
385
+ textParts.push(part.text);
386
+ }
387
+ } else if (typeof json.result === 'string') {
388
+ textParts.push(json.result);
389
+ } else if (typeof json.content === 'string') {
390
+ textParts.push(json.content);
391
+ }
392
+ }
393
+
394
+ if (errorReason) {
395
+ return { forceFailure: true, forceFailureReason: errorReason };
396
+ }
397
+
398
+ // If nothing parsed as JSON, treat stdout as plain text.
399
+ const normalizedOutput = !sawAnyJson
400
+ ? stdout
401
+ : textParts.length > 0
402
+ ? textParts.join('\n')
403
+ : stdout;
404
+
405
+ return {
406
+ sessionId,
407
+ normalizedOutput,
408
+ };
409
+ },
410
+ };