- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/scripts/autosettle/settle-one.ts?format=json
102 linesautosettle 100%Last change autosettle in version 198, 3 versions changed it
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.
3//
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]
5//
6// --via workers-ai (the default) needs workers-ai/ running under wrangler dev; --via jev needs JEV_API_KEY.
8import { readFileSync } from "node:fs";
9import { findBlocks } from "../../client/src/settle.ts";
10import { askClef } from "./clef.ts";
11import { ask, dollars, type Hold, PICKS } from "./jev.ts";
12import { askWorkersAi } from "./workers-ai.ts";
14const CONTEXT = 15;
15const CLOSE = /^>>>>>>> theirs: (.*?), version (\d+) "(.*)"$/;
17/** Each marker block in a file's text as a Hold. */
18export function holdsIn(
19 text: string,
20 file: string,
21 extra: { yourMessage?: string; yourAgent?: string; issue?: string } = {},
22): Hold[] {
23 const lines = text.split(/\r?\n/);
24 return findBlocks(lines).map((b) => {
25 const close = CLOSE.exec(lines[b.end]!);
26 return {
27 file,
28 base: b.base,
29 yours: b.yours,
30 theirs: b.theirs,
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] } : {}),
34 ...extra,
35 };
36 });
37}
39function flag(args: string[], name: string): string | undefined {
40 const i = args.indexOf(name);
41 return i === -1 ? undefined : args[i + 1];
42}
44async function main(): Promise<void> {
45 const args = process.argv.slice(2);
46 const path = args[0];
47 if (!path || path.startsWith("--")) {
48 console.error(
49 'usage: node scripts/autosettle/settle-one.ts <file> [--message "..."] [--issue "..."] [--name <path>] [--json]',
50 );
51 process.exit(2);
52 }
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") } : {}),
56 });
57 if (holds.length === 0) {
58 console.error(`${path} has no marker blocks.`);
59 process.exit(1);
60 }
61 const results = [];
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"))
68 console.log(
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`,
70 );
71 continue;
72 }
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"))
77 console.log(
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`,
79 );
80 continue;
81 }
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(", ")}`);
88 console.log(
89 ` each order: ${v.orders.map((o) => `${o.pick} ${o.confidence.toFixed(2)}`).join(" / ")}; side questions: ${Object.entries(
90 v.signs,
91 )
92 .map(([k, p]) => `${k} ${pct(p)}`)
93 .join(", ")}`,
94 );
95 console.log(
96 ` ${v.model}, ${v.ms.toFixed(0)} ms, ${v.inputTokens} input tokens ($${dollars(v.inputTokens).toFixed(6)})`,
97 );
98 }
99 if (args.includes("--json")) console.log(JSON.stringify(results, null, "\t"));
100}
102if (process.argv[1]?.endsWith("settle-one.ts")) await main();