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/src/engine/json-value-chunks.ts?format=json
133 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 184, 3 versions changed it
1/** Internal values may expose an exact streaming representation without changing public toJSON. */
2const streamingValues = new WeakMap<object, () => unknown>();
3export function registerJsonChunkValue(value: object, representation: () => unknown): void {
4 streamingValues.set(value, representation);
5}
6
7/** A JSON string backed by bounded, replayable UTF-16 chunks. */
8export class JsonStringChunks {
9 readonly chunks: () => Iterable<string>;
10 constructor(chunks: () => Iterable<string>) {
11 this.chunks = chunks;
12 }
13 toJSON(): string {
14 return [...this.chunks()].join("");
15 }
18/** JSON.stringify-compatible chunks for canonical data, without a whole-value string.
19 * Individual input strings and object key lists still belong to the caller.
20 */
21export function* jsonValueChunks(
22 input: unknown,
23 options: { maxDepth?: number; unboxPrimitives?: boolean } = {},
24): IterableIterator<string> {
25 const active = new Set<object>();
26 function* string(value: string): IterableIterator<string> {
27 yield '"';
28 for (let from = 0; from < value.length; ) {
29 let end = Math.min(from + 16384, value.length);
30 const last = value.charCodeAt(end - 1),
31 next = value.charCodeAt(end);
32 if (last >= 0xd800 && last <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) end--;
33 yield JSON.stringify(value.slice(from, end)).slice(1, -1);
34 from = end;
35 }
36 yield '"';
37 }
38 const normalize = (value: unknown, key: string): unknown => {
39 if (value instanceof JsonStringChunks) return value;
40 if (value !== null && typeof value === "object") {
41 const streaming = streamingValues.get(value);
42 if (streaming) return streaming();
43 const toJSON = (value as { toJSON?: unknown }).toJSON;
44 if (typeof toJSON === "function") value = toJSON.call(value, key);
45 }
46 if (options.unboxPrimitives && (value instanceof String || value instanceof Number || value instanceof Boolean))
47 return value.valueOf();
48 return value;
49 };
50 const omitted = (value: unknown) => value === undefined || typeof value === "function" || typeof value === "symbol";
51 function* encode(value: unknown): IterableIterator<string> {
52 if (value instanceof JsonStringChunks) {
53 yield '"';
54 let pending = "";
55 for (const chunk of value.chunks()) {
56 if (typeof chunk !== "string" || chunk.length > 16384) throw Error("JSON string chunk exceeds bound");
57 const text = pending + chunk;
58 let end = text.length;
59 const last = text.charCodeAt(end - 1);
60 if (last >= 0xd800 && last <= 0xdbff) end--;
61 pending = text.slice(end);
62 if (end) yield JSON.stringify(text.slice(0, end)).slice(1, -1);
63 }
64 if (pending) yield JSON.stringify(pending).slice(1, -1);
65 yield '"';
66 } else if (typeof value === "string") yield* string(value);
67 else if (value === null || typeof value === "boolean" || typeof value === "number") yield JSON.stringify(value);
68 else if (typeof value !== "object" || value === null) throw Error("unsupported canonical JSON value");
69 else {
70 if (active.has(value)) throw Error("cyclic canonical JSON value");
71 if (active.size >= (options.maxDepth ?? 128)) throw Error("canonical JSON depth exceeds bound");
72 active.add(value);
73 try {
74 if (Array.isArray(value)) {
75 yield "[";
76 const length = value.length;
77 for (let i = 0; i < length; i++) {
78 if (i) yield ",";
79 const child = normalize(value[i], String(i));
80 yield* encode(omitted(child) ? null : child);
81 }
82 yield "]";
83 } else {
84 yield "{";
85 let first = true;
86 for (const key of Object.keys(value)) {
87 const child = normalize((value as Record<string, unknown>)[key], key);
88 if (omitted(child)) continue;
89 if (!first) yield ",";
90 first = false;
91 yield* string(key);
92 yield ":";
93 yield* encode(child);
94 }
95 yield "}";
96 }
97 } finally {
98 active.delete(value);
99 }
100 }
101 }
102 yield* encode(normalize(input, ""));
105/** Exact serialized-content equality, retaining only the current pair of chunks. */
106export function equalJsonValues(left: unknown, right: unknown): boolean {
107 const a = jsonValueChunks(left),
108 b = jsonValueChunks(right);
109 let x = a.next(),
110 y = b.next(),
111 i = 0,
112 j = 0;
113 try {
114 for (;;) {
115 while (!x.done && i === x.value.length) {
116 x = a.next();
117 i = 0;
118 }
119 while (!y.done && j === y.value.length) {
120 y = b.next();
121 j = 0;
122 }
123 if (x.done || y.done) return x.done === y.done;
124 const length = Math.min(x.value.length - i, y.value.length - j);
125 if (x.value.slice(i, i + length) !== y.value.slice(j, j + length)) return false;
126 i += length;
127 j += length;
128 }
129 } finally {
130 a.return?.();
131 b.return?.();
132 }