Versions 40 to 318
new file, 102 lines
1+// Ask a model how to settle each marker block in one file (#19), the way abso 2+// pull leaves a held push's file. Prints its pick; changes nothing. 4+// node scripts/autosettle/settle-one.ts <file> [--via workers-ai|clef|jev] [--model <id>] [--message "<your held push's message>"] [--issue "..."] [--name <path in the project>] [--json] 6+// --via workers-ai (the default) needs workers-ai/ running under wrangler dev; --via jev needs JEV_API_KEY. 8+import { readFileSync } from "node:fs"; 9+import { findBlocks } from "../../client/src/settle.ts"; 10+import { askClef } from "./clef.ts"; 11+import { ask, dollars, type Hold, PICKS } from "./jev.ts"; 12+import { askWorkersAi } from "./workers-ai.ts"; 15+const CLOSE = /^>>>>>>> theirs: (.*?), version (\d+) "(.*)"$/; 17+/** Each marker block in a file's text as a Hold. */ 18+export function holdsIn( 21+ extra: { yourMessage?: string; yourAgent?: string; issue?: string } = {}, 23+ const lines = text.split(/\r?\n/); 24+ return findBlocks(lines).map((b) => { 25+ const close = CLOSE.exec(lines[b.end]!); 31+ before: lines.slice(Math.max(0, b.start - CONTEXT), b.start), 32+ after: lines.slice(b.end + 1, b.end + 1 + CONTEXT), 33+ ...(close ? { theirAgent: close[1], theirMessage: close[3] } : {}), 39+function flag(args: string[], name: string): string | undefined { 40+ const i = args.indexOf(name); 41+ return i === -1 ? undefined : args[i + 1]; 44+async function main(): Promise<void> { 45+ const args = process.argv.slice(2); 46+ const path = args[0]; 47+ if (!path || path.startsWith("--")) { 49+ 'usage: node scripts/autosettle/settle-one.ts <file> [--message "..."] [--issue "..."] [--name <path>] [--json]', 53+ const holds = holdsIn(readFileSync(path, "utf8"), flag(args, "--name") ?? path, { 54+ ...(flag(args, "--message") ? { yourMessage: flag(args, "--message") } : {}), 55+ ...(flag(args, "--issue") ? { issue: flag(args, "--issue") } : {}), 57+ if (holds.length === 0) { 58+ console.error(`${path} has no marker blocks.`); 62+ const model = flag(args, "--model") ? { model: flag(args, "--model")! } : {}; 63+ for (const [i, h] of holds.entries()) { 64+ if (flag(args, "--via") === "clef") { 65+ const c = await askClef(h, { model: (flag(args, "--model") as "clef" | "clef-flash" | undefined) ?? "clef" }); 66+ results.push({ block: i + 1, ...c }); 67+ if (!args.includes("--json")) 69+ `Block ${i + 1} of ${holds.length} in ${h.file}: ${c.pick}, confidence ${c.confidence.toFixed(2)} (${c.by}${c.modelPick ? `, Clef said ${c.modelPick}` : ""})\n probabilities: ${PICKS.map((k) => `${k} ${(c.probabilities[k] * 100).toFixed(0)}%`).join(", ")}\n ${c.model}, ${c.ms.toFixed(0)} ms`, 73+ if (flag(args, "--via") !== "jev") { 74+ const w = await askWorkersAi(h, model); 75+ results.push({ block: i + 1, ...w }); 76+ if (!args.includes("--json")) 78+ `Block ${i + 1} of ${holds.length} in ${h.file}: ${w.pick}, confidence ${w.confidence.toFixed(2)}\n ${w.reason}\n ${w.model}, ${w.ms.toFixed(0)} ms (model ${w.modelMs} ms), ${w.inputTokens} input tokens`, 82+ const v = await ask(h, model); 83+ results.push({ block: i + 1, ...v }); 84+ if (args.includes("--json")) continue; 85+ const pct = (x: number) => `${(x * 100).toFixed(0)}%`; 86+ console.log(`Block ${i + 1} of ${holds.length} in ${h.file}: ${v.pick}, confidence ${v.confidence.toFixed(2)}`); 87+ console.log(` probabilities: ${PICKS.map((k) => `${k} ${pct(v.probabilities[k])}`).join(", ")}`); 89+ ` each order: ${v.orders.map((o) => `${o.pick} ${o.confidence.toFixed(2)}`).join(" / ")}; side questions: ${Object.entries( 92+ .map(([k, p]) => `${k} ${pct(p)}`) 96+ ` ${v.model}, ${v.ms.toFixed(0)} ms, ${v.inputTokens} input tokens ($${dollars(v.inputTokens).toFixed(6)})`, 99+ if (args.includes("--json")) console.log(JSON.stringify(results, null, "\t")); 102+if (process.argv[1]?.endsWith("settle-one.ts")) await main();