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/worker/project/record-file.ts?draft=login-timeout&format=json
82 linesFirst import 100%0 versions changed it
1// Make one file in the agent's view what the folder has. Writes go through
2// RecordFileHost; today the host is Project.
3
4import {
5 type AttrName,
6 type AttrSet,
7 diffLines,
8 type EditInput,
9 type Hunk,
10 type SpliceEdit,
11 splitLines,
12 type View,
13} from "../../src/engine/index.ts";
14import type { AgentState, Entry, Meta, Mode } from "./project-types.ts";
16/** What recording one file needs from a project. */
17export interface RecordFileHost {
18 addDraft(
19 agent: AgentState,
20 view: View,
21 input: { edits: readonly EditInput[]; attrs?: readonly AttrSet[] },
22 time: number,
23 meta: Meta,
24 entries: Entry[],
25 ): void;
28/**
29 * Make one file in the agent's view what the folder has, as one draft
30 * change: `text` (null: the file is gone; "": an empty file; undefined:
31 * leave its lines alone) and `mode` (only the fields given). Returns
32 * whether anything changed.
33 */
34export function recordFile(
35 host: RecordFileHost,
36 agent: AgentState,
37 view: View,
38 path: string,
39 text: string | null | undefined,
40 time: number,
41 meta: Meta,
42 entries: Entry[],
43 mode: Mode = {},
44 blob?: string,
45 hunks?: readonly Hunk[],
46): boolean {
47 const was = view.attrs(path);
48 const exists = view.hasFile(path);
49 const lines = text === undefined ? undefined : text === null ? "" : text;
50 const edits: SpliceEdit[] =
51 lines === undefined || view.text(path) === lines
52 ? []
53 : (
54 hunks ??
55 diffLines(
56 view.lines(path).map((l) => l.text),
57 splitLines(lines),
58 )
59 ).map((h) => ({ file: path, start: h.start, end: h.end, lines: h.lines }));
60 const attrs: AttrSet[] = [];
61 const set = (name: AttrName, now: string | null, value: string | null): void => {
62 if (now !== value) attrs.push({ file: path, name, value });
63 };
64 const link = mode.link !== undefined ? mode.link : text === null ? null : was.link;
65 if (text === null) {
66 // Gone: no lines, and nothing keeps it.
67 set("exists", was.exists ? "1" : null, null);
68 set("exec", was.exec ? "1" : null, null);
69 } else if (text === "" && link === null && blob === undefined) {
70 // An empty file is kept by its exists attribute.
71 set("exists", was.exists ? "1" : null, "1");
72 }
73 // A binary file is its blob and no lines; text, or no file, clears the blob.
74 if (blob !== undefined) set("blob", was.blob, blob);
75 else if (text !== undefined) set("blob", was.blob, null);
76 if (mode.exec !== undefined && text !== null) set("exec", was.exec ? "1" : null, mode.exec ? "1" : null);
77 set("link", was.link, link);
78 if (edits.length === 0 && attrs.length === 0) return false;
79 if (!exists && text === null && attrs.length === 0) return false;
80 host.addDraft(agent, view, { edits, attrs }, time, meta, entries);
81 return true;