{"draft":null,"from":175,"to":318,"files":[{"path":"scripts/autosettle/clef.ts","added":45,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/clef.ts\n@@ -0,0 +1,45 @@\n+// Ask Clef, Cloudflare's decision model on Workers AI, how a hold should be settled (#19). Clef takes Jev's\n+// System One request (state and typed questions) and answers with a probability per option, so the questions are\n+// jev.ts's own; it goes through the local Worker (workers-ai/, wrangler dev), which has the AI binding.\n+\n+import { type Hold, type SystemOneAnswer, systemOneBody, type Verdict, verdictOf } from \"./jev.ts\";\n+import { refused, ruled } from \"./rules.ts\";\n+import { WORKER } from \"./workers-ai.ts\";\n+\n+export interface ClefVerdict extends Verdict {\n+\treadonly reason?: string;\n+\treadonly by: \"rule\" | \"model\" | \"turned down\";\n+\treadonly modelPick?: Verdict[\"pick\"];\n+}\n+\n+/** Code's rules first, then Clef, then code's check on its pick, as for the Workers AI text model. */\n+export async function askClef(\n+\th: Hold,\n+\topts: { model?: \"clef\" | \"clef-flash\"; url?: string } = {},\n+): Promise<ClefVerdict> {\n+\tconst rule = ruled(h);\n+\tif (rule)\n+\t\treturn {\n+\t\t\tpick: rule.pick,\n+\t\t\treason: rule.reason,\n+\t\t\tconfidence: 1,\n+\t\t\tprobabilities: { yours: 0, theirs: 0, both: 0, agent: 0, [rule.pick]: 1 },\n+\t\t\torders: [],\n+\t\t\tsigns: {},\n+\t\t\tmodel: \"rule\",\n+\t\t\tms: 0,\n+\t\t\tinputTokens: 0,\n+\t\t\tby: \"rule\",\n+\t\t};\n+\tconst started = performance.now();\n+\tconst res = await fetch(`${opts.url ?? WORKER}/system-one`, {\n+\t\tmethod: \"POST\",\n+\t\theaders: { \"content-type\": \"application/json\" },\n+\t\tbody: JSON.stringify(systemOneBody(h, opts.model ?? \"clef\")),\n+\t});\n+\tconst out = (await res.json()) as SystemOneAnswer & { error?: string };\n+\tif (!res.ok || out.error || !out.answers) throw new Error(`Clef: ${out.error ?? res.status}`);\n+\tconst v = verdictOf({ ...out, model: out.model ?? opts.model ?? \"clef\" }, performance.now() - started);\n+\tconst no = refused(h, v.pick);\n+\treturn no ? { ...v, pick: \"agent\", reason: no, by: \"turned down\", modelPick: v.pick } : { ...v, by: \"model\" };\n+}"}],"unmatched":[]}