Skip to content
Myr

opencode.ts

This file

Path

scripts/lib/swarm/harnesses/opencode.ts

99 lines · 2.5 KiB · TypeScript

Last changed by

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

History of this file

Take it

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

lines 1–3why, one step back
1// `--standalone` runs a private server in the contributor's env, not the machine's
2// background service. The last step prints no `step_finish` before the process3// exits (seen on 2.0.7), so the run settles on exit; an `error` event settles it at once.

Merge branch 'build/711-swarm-drivers-and-verdict-loop'oscabrielbefore adoption3210514

lines 4–99why, one step back
4import { Option, Schema } from "effect";
56import { HarnessEvent, lineAs, Tokens } from "./event.ts";7import type { HarnessDriver } from "./event.ts";89const StepStart = Schema.Struct({10 sessionID: Schema.NonEmptyString,11 type: Schema.Literal("step_start"),12});1314const StepFinish = Schema.Struct({15 part: Schema.Struct({16 reason: Schema.optionalKey(Schema.String),17 tokens: Schema.Struct({18 cache: Schema.optionalKey(Schema.Struct({ read: Tokens, write: Tokens })),19 input: Tokens,20 output: Tokens,21 reasoning: Schema.optionalKey(Tokens),22 }),23 }),24 type: Schema.Literal("step_finish"),25});2627const ErrorEvent = Schema.Struct({28 error: Schema.Struct({29 message: Schema.optionalKey(Schema.String),30 type: Schema.String,31 }),32 type: Schema.Literal("error"),33});3435const stepStartOf = lineAs(StepStart);3637const stepFinishOf = lineAs(StepFinish);3839const errorOf = lineAs(ErrorEvent);4041const parse = (line: string): readonly HarnessEvent[] => {42 const start = stepStartOf(line);4344 if (Option.isSome(start)) {45 return [HarnessEvent.SessionStarted({ id: start.value.sessionID })];46 }4748 const finish = stepFinishOf(line);4950 if (Option.isSome(finish)) {51 const { reason, tokens } = finish.value.part;5253 return [54 HarnessEvent.Usage({55 input:56 tokens.input + (tokens.cache?.read ?? 0) + (tokens.cache?.write ?? 0),57 output: tokens.output + (tokens.reasoning ?? 0),58 stopReason: Option.fromUndefinedOr(reason),59 }),60 ];61 }6263 return Option.match(errorOf(line), {64 onNone: () => [],65 onSome: ({ error }) => [66 HarnessEvent.Failed({67 reason:68 error.message === undefined69 ? error.type70 : `${error.type}: ${error.message}`,71 }),72 HarnessEvent.Settled({ stopReason: Option.some(error.type) }),73 ],74 });75};7677const headless = (model: string) => [78 "run",79 "--standalone",80 "--format",81 "json",82 "--auto",83 "-m",84 model,85];8687export const opencode: HarnessDriver = {88 files: () => [],89 harness: "opencode",90 parse,91 resume: ({ model, prompt, session }) => [92 "opencode",93 ...headless(model),94 "-s",95 session,96 prompt,97 ],98 start: ({ brief, model }) => ["opencode", ...headless(model), brief],99};