Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/scripts/autosettle/jev.ts?format=json
122 linesautosettle 100%Last change autosettle in version 203, 4 versions changed it
1// Ask Jev (TypeSafe's System One model) how a held push's collision should be
2// settled (#19). Jev answers a Choice with a probability per option, so it can
3// pick among abso's own settle choices but can't write code.
4//
5// API: POST https://api.typesafe.ai/v1/systemone, per https://docs.typesafe.ai/api.md.
6// The key comes from JEV_API_KEY (or TYPESAFE_API_KEY, the SDK's own name) and
7// is never printed or written anywhere.
8
9import { execFileSync } from "node:child_process";
10import { confidenceOf, SIGNS, systemOneBody } from "../../worker/project/autosettle.ts";
12// The questions are the tool's own (worker/project/autosettle.ts), so Jev and Clef are asked alike.
13export { systemOneBody };
15export const ENDPOINT = "https://api.typesafe.ai/v1/systemone";
17/** abso's settle choices, plus a hand edit by an agent. */
18export type Pick = "yours" | "theirs" | "both" | "agent";
19export const PICKS: readonly Pick[] = ["yours", "theirs", "both", "agent"];
21/** One collision as the held agent sees it: a marker block and what's around it. */
22export interface Hold {
23 readonly file: string;
24 readonly base: readonly string[];
25 readonly yours: readonly string[];
26 readonly theirs: readonly string[];
27 readonly before: readonly string[];
28 readonly after: readonly string[];
29 /** The held push's message: the settler's reason. */
30 readonly yourMessage?: string;
31 readonly yourAgent?: string;
32 /** The push that got there first: its reason. */
33 readonly theirMessage?: string;
34 readonly theirAgent?: string;
35 readonly issue?: string;
38export interface Verdict {
39 readonly pick: Pick;
40 /** Averaged over both option orders. */
41 readonly probabilities: Record<Pick, number>;
42 /** Choice confidence on the averaged probabilities: 0 at an even split, 1 when certain. */
43 readonly confidence: number;
44 /** Each order's own answer, to see whether the order moved it. */
45 readonly orders: readonly { pick: Pick; probabilities: Record<Pick, number>; confidence: number }[];
46 /** Yes-probabilities of the side questions. */
47 readonly signs: Record<string, number>;
48 readonly model: string;
49 readonly ms: number;
50 readonly inputTokens: number;
53export function apiKey(): string {
54 const key = process.env.JEV_API_KEY ?? process.env.TYPESAFE_API_KEY ?? userVariable("JEV_API_KEY");
55 if (!key) throw new Error("No Jev key: set JEV_API_KEY in the environment.");
56 return key;
59/** On Windows, a user variable set after this shell started (setx, or the Environment Variables dialog). */
60function userVariable(name: string): string | undefined {
61 if (process.platform !== "win32") return undefined;
62 try {
63 const out = execFileSync(
64 "powershell.exe",
65 ["-NoProfile", "-Command", `[Environment]::GetEnvironmentVariable('${name}','User')`],
66 { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] },
67 ).trim();
68 return out || undefined;
69 } catch {
70 return undefined;
71 }
74function top(p: Record<Pick, number>): Pick {
75 return PICKS.reduce((a, b) => (p[b] > p[a] ? b : a));
78export interface SystemOneAnswer {
79 model: string;
80 answers: Record<string, { type: string; probabilities?: Record<string, number>; noul?: number; choice?: string }>;
81 usage?: { input_tokens?: number };
84/** Ask Jev once. */
85export async function ask(h: Hold, opts: { model?: string; key?: string } = {}): Promise<Verdict> {
86 const started = performance.now();
87 const res = await fetch(ENDPOINT, {
88 method: "POST",
89 headers: { authorization: `Bearer ${opts.key ?? apiKey()}`, "content-type": "application/json" },
90 body: JSON.stringify(systemOneBody(h, opts.model ?? "jev-latest")),
91 });
92 const ms = performance.now() - started;
93 if (!res.ok) throw new Error(`Jev answered ${res.status}: ${(await res.text()).slice(0, 500)}`);
94 return verdictOf((await res.json()) as SystemOneAnswer, ms);
97/** A System One answer to systemOneBody's questions, as a Verdict. */
98export function verdictOf(out: SystemOneAnswer, ms: number): Verdict {
99 const orders = ["settle", "settle_reversed"].map((id) => {
100 const probabilities = Object.fromEntries(PICKS.map((k) => [k, out.answers[id]!.probabilities![k] ?? 0])) as Record<
101 Pick,
102 number
103 >;
104 return { pick: top(probabilities), probabilities, confidence: confidenceOf(probabilities) };
105 });
106 const probabilities = Object.fromEntries(
107 PICKS.map((k) => [k, (orders[0]!.probabilities[k] + orders[1]!.probabilities[k]) / 2]),
108 ) as Record<Pick, number>;
109 return {
110 pick: top(probabilities),
111 probabilities,
112 confidence: confidenceOf(probabilities),
113 orders,
114 signs: Object.fromEntries(Object.keys(SIGNS).map((id) => [id, out.answers[id]?.noul ?? Number.NaN])),
115 model: out.model,
116 ms,
117 inputTokens: out.usage?.input_tokens ?? 0,
118 };
121/** Dollars for one call: Jev charges $0.042 per million input tokens, output free (docs.typesafe.ai/models). */
122export const dollars = (inputTokens: number): number => (inputTokens * 0.042) / 1e6;