{"draft":null,"from":175,"to":318,"files":[{"path":"scripts/autosettle/jev.ts","added":122,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/jev.ts\n@@ -0,0 +1,122 @@\n+// Ask Jev (TypeSafe's System One model) how a held push's collision should be\n+// settled (#19). Jev answers a Choice with a probability per option, so it can\n+// pick among abso's own settle choices but can't write code.\n+//\n+// API: POST https://api.typesafe.ai/v1/systemone, per https://docs.typesafe.ai/api.md.\n+// The key comes from JEV_API_KEY (or TYPESAFE_API_KEY, the SDK's own name) and\n+// is never printed or written anywhere.\n+\n+import { execFileSync } from \"node:child_process\";\n+import { confidenceOf, SIGNS, systemOneBody } from \"../../worker/project/autosettle.ts\";\n+\n+// The questions are the tool's own (worker/project/autosettle.ts), so Jev and Clef are asked alike.\n+export { systemOneBody };\n+\n+export const ENDPOINT = \"https://api.typesafe.ai/v1/systemone\";\n+\n+/** abso's settle choices, plus a hand edit by an agent. */\n+export type Pick = \"yours\" | \"theirs\" | \"both\" | \"agent\";\n+export const PICKS: readonly Pick[] = [\"yours\", \"theirs\", \"both\", \"agent\"];\n+\n+/** One collision as the held agent sees it: a marker block and what's around it. */\n+export interface Hold {\n+\treadonly file: string;\n+\treadonly base: readonly string[];\n+\treadonly yours: readonly string[];\n+\treadonly theirs: readonly string[];\n+\treadonly before: readonly string[];\n+\treadonly after: readonly string[];\n+\t/** The held push's message: the settler's reason. */\n+\treadonly yourMessage?: string;\n+\treadonly yourAgent?: string;\n+\t/** The push that got there first: its reason. */\n+\treadonly theirMessage?: string;\n+\treadonly theirAgent?: string;\n+\treadonly issue?: string;\n+}\n+\n+export interface Verdict {\n+\treadonly pick: Pick;\n+\t/** Averaged over both option orders. */\n+\treadonly probabilities: Record<Pick, number>;\n+\t/** Choice confidence on the averaged probabilities: 0 at an even split, 1 when certain. */\n+\treadonly confidence: number;\n+\t/** Each order's own answer, to see whether the order moved it. */\n+\treadonly orders: readonly { pick: Pick; probabilities: Record<Pick, number>; confidence: number }[];\n+\t/** Yes-probabilities of the side questions. */\n+\treadonly signs: Record<string, number>;\n+\treadonly model: string;\n+\treadonly ms: number;\n+\treadonly inputTokens: number;\n+}\n+\n+export function apiKey(): string {\n+\tconst key = process.env.JEV_API_KEY ?? process.env.TYPESAFE_API_KEY ?? userVariable(\"JEV_API_KEY\");\n+\tif (!key) throw new Error(\"No Jev key: set JEV_API_KEY in the environment.\");\n+\treturn key;\n+}\n+\n+/** On Windows, a user variable set after this shell started (setx, or the Environment Variables dialog). */\n+function userVariable(name: string): string | undefined {\n+\tif (process.platform !== \"win32\") return undefined;\n+\ttry {\n+\t\tconst out = execFileSync(\n+\t\t\t\"powershell.exe\",\n+\t\t\t[\"-NoProfile\", \"-Command\", `[Environment]::GetEnvironmentVariable('${name}','User')`],\n+\t\t\t{ encoding: \"utf8\", stdio: [\"ignore\", \"pipe\", \"ignore\"] },\n+\t\t).trim();\n+\t\treturn out || undefined;\n+\t} catch {\n+\t\treturn undefined;\n+\t}\n+}\n+\n+function top(p: Record<Pick, number>): Pick {\n+\treturn PICKS.reduce((a, b) => (p[b] > p[a] ? b : a));\n+}\n+\n+export interface SystemOneAnswer {\n+\tmodel: string;\n+\tanswers: Record<string, { type: string; probabilities?: Record<string, number>; noul?: number; choice?: string }>;\n+\tusage?: { input_tokens?: number };\n+}\n+\n+/** Ask Jev once. */\n+export async function ask(h: Hold, opts: { model?: string; key?: string } = {}): Promise<Verdict> {\n+\tconst started = performance.now();\n+\tconst res = await fetch(ENDPOINT, {\n+\t\tmethod: \"POST\",\n+\t\theaders: { authorization: `Bearer ${opts.key ?? apiKey()}`, \"content-type\": \"application/json\" },\n+\t\tbody: JSON.stringify(systemOneBody(h, opts.model ?? \"jev-latest\")),\n+\t});\n+\tconst ms = performance.now() - started;\n+\tif (!res.ok) throw new Error(`Jev answered ${res.status}: ${(await res.text()).slice(0, 500)}`);\n+\treturn verdictOf((await res.json()) as SystemOneAnswer, ms);\n+}\n+\n+/** A System One answer to systemOneBody's questions, as a Verdict. */\n+export function verdictOf(out: SystemOneAnswer, ms: number): Verdict {\n+\tconst orders = [\"settle\", \"settle_reversed\"].map((id) => {\n+\t\tconst probabilities = Object.fromEntries(PICKS.map((k) => [k, out.answers[id]!.probabilities![k] ?? 0])) as Record<\n+\t\t\tPick,\n+\t\t\tnumber\n+\t\t>;\n+\t\treturn { pick: top(probabilities), probabilities, confidence: confidenceOf(probabilities) };\n+\t});\n+\tconst probabilities = Object.fromEntries(\n+\t\tPICKS.map((k) => [k, (orders[0]!.probabilities[k] + orders[1]!.probabilities[k]) / 2]),\n+\t) as Record<Pick, number>;\n+\treturn {\n+\t\tpick: top(probabilities),\n+\t\tprobabilities,\n+\t\tconfidence: confidenceOf(probabilities),\n+\t\torders,\n+\t\tsigns: Object.fromEntries(Object.keys(SIGNS).map((id) => [id, out.answers[id]?.noul ?? Number.NaN])),\n+\t\tmodel: out.model,\n+\t\tms,\n+\t\tinputTokens: out.usage?.input_tokens ?? 0,\n+\t};\n+}\n+\n+/** Dollars for one call: Jev charges $0.042 per million input tokens, output free (docs.typesafe.ai/models). */\n+export const dollars = (inputTokens: number): number => (inputTokens * 0.042) / 1e6;"}],"unmatched":[]}