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

Versions 174 to 175

Compare

+70 −0 in 1 place
new file, 70 lines
1+import type { ReadView, VisibleLine } from "../../src/engine/index.ts";
2+import { StoredText } from "../../src/engine/stored-text.ts";
3+import { eachCodeLine } from "./names.ts";
4+
5+/** Parsed code is temporary derived data, discarded with the scratch transaction. */
6+export function withDefinitionCode<T>(
7+ view: ReadView,
8+ visible: readonly VisibleLine[],
9+ operation: (code: readonly string[]) => T,
10+): T {
11+ function* source() {
12+ for (const line of visible) yield line.text;
13+ }
14+ if (!view.graph.hasOrderedChildren) return operation([...eachCodeLine(source())]);
15+ const iterator = eachCodeLine(source());
16+ let prefix: string[] = [];
17+ let size = 0;
18+ let pending = iterator.next();
19+ while (!pending.done && size + pending.value.length + 1 <= 65536) {
20+ prefix.push(pending.value);
21+ size += pending.value.length + 1;
22+ pending = iterator.next();
23+ }
24+ if (pending.done) return operation(prefix);
25+ return view.graph.withProjectionScratch((storage) => {
26+ const text = new StoredText(storage, "definition-code");
27+ let count = 0;
28+ for (const line of prefix) text.write(String(count++), line);
29+ prefix = [];
30+ text.write(String(count++), pending.value!);
31+ for (const line of iterator) text.write(String(count++), line);
32+ const cache = new Map<number, string>();
33+ let characters = 0;
34+ const read = (index: number) => {
35+ if (index < 0 || index >= count) return undefined;
36+ const cached = cache.get(index);
37+ if (cached !== undefined) return cached;
38+ const value = text.read(String(index));
39+ if (value.length <= 65536) {
40+ while (cache.size >= 64 || characters + value.length > 65536) {
41+ const first = cache.keys().next().value!;
42+ characters -= cache.get(first)!.length;
43+ cache.delete(first);
44+ }
45+ cache.set(index, value);
46+ characters += value.length;
47+ }
48+ return value;
49+ };
50+ let active = true;
51+ const code = new Proxy([] as string[], {
52+ has(target, key) {
53+ if (!active) throw Error("definition code scope has ended");
54+ if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return Number(key) < count;
55+ return Reflect.has(target, key);
56+ },
57+ get(target, key, receiver) {
58+ if (!active) throw Error("definition code scope has ended");
59+ if (key === "length") return count;
60+ if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return read(Number(key));
61+ return Reflect.get(target, key, receiver);
62+ },
63+ });
64+ try {
65+ return operation(code);
66+ } finally {
67+ active = false;
68+ }
69+ });
70+}
MCP
compare on this site's /mcp
CLI
abso diff 174 175 worker/project/definition-code.ts --json
JSON
/christopher/abso-core/compare/174..175?file=worker%2Fproject%2Fdefinition-code.ts&format=json