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?version=175&format=json
128 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 175, 2 versions changed it

You're reading version 175. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

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(input: unknown): IterableIterator<string> {
22 const active = new Set<object>();
23 function* string(value: string): IterableIterator<string> {
24 yield '"';
25 for (let from = 0; from < value.length; ) {
26 let end = Math.min(from + 16384, value.length);
27 const last = value.charCodeAt(end - 1),
28 next = value.charCodeAt(end);
29 if (last >= 0xd800 && last <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) end--;
30 yield JSON.stringify(value.slice(from, end)).slice(1, -1);
31 from = end;
32 }
33 yield '"';
34 }
35 const normalize = (value: unknown, key: string): unknown => {
36 if (value instanceof JsonStringChunks) return value;
37 if (value !== null && typeof value === "object") {
38 const streaming = streamingValues.get(value);
39 if (streaming) return streaming();
40 const toJSON = (value as { toJSON?: unknown }).toJSON;
41 if (typeof toJSON === "function") return toJSON.call(value, key);
42 }
43 return value;
44 };
45 const omitted = (value: unknown) => value === undefined || typeof value === "function" || typeof value === "symbol";
46 function* encode(value: unknown): IterableIterator<string> {
47 if (value instanceof JsonStringChunks) {
48 yield '"';
49 let pending = "";
50 for (const chunk of value.chunks()) {
51 if (typeof chunk !== "string" || chunk.length > 16384) throw Error("JSON string chunk exceeds bound");
52 const text = pending + chunk;
53 let end = text.length;
54 const last = text.charCodeAt(end - 1);
55 if (last >= 0xd800 && last <= 0xdbff) end--;
56 pending = text.slice(end);
57 if (end) yield JSON.stringify(text.slice(0, end)).slice(1, -1);
58 }
59 if (pending) yield JSON.stringify(pending).slice(1, -1);
60 yield '"';
61 } else if (typeof value === "string") yield* string(value);
62 else if (value === null || typeof value === "boolean" || typeof value === "number") yield JSON.stringify(value);
63 else if (typeof value !== "object" || value === null) throw Error("unsupported canonical JSON value");
64 else {
65 if (active.has(value)) throw Error("cyclic canonical JSON value");
66 if (active.size >= 128) throw Error("canonical JSON depth exceeds bound");
67 active.add(value);
68 try {
69 if (Array.isArray(value)) {
70 yield "[";
71 const length = value.length;
72 for (let i = 0; i < length; i++) {
73 if (i) yield ",";
74 const child = normalize(value[i], String(i));
75 yield* encode(omitted(child) ? null : child);
76 }
77 yield "]";
78 } else {
79 yield "{";
80 let first = true;
81 for (const key of Object.keys(value)) {
82 const child = normalize((value as Record<string, unknown>)[key], key);
83 if (omitted(child)) continue;
84 if (!first) yield ",";
85 first = false;
86 yield* string(key);
87 yield ":";
88 yield* encode(child);
89 }
90 yield "}";
91 }
92 } finally {
93 active.delete(value);
94 }
95 }
96 }
97 yield* encode(normalize(input, ""));
100/** Exact serialized-content equality, retaining only the current pair of chunks. */
101export function equalJsonValues(left: unknown, right: unknown): boolean {
102 const a = jsonValueChunks(left),
103 b = jsonValueChunks(right);
104 let x = a.next(),
105 y = b.next(),
106 i = 0,
107 j = 0;
108 try {
109 for (;;) {
110 while (!x.done && i === x.value.length) {
111 x = a.next();
112 i = 0;
113 }
114 while (!y.done && j === y.value.length) {
115 y = b.next();
116 j = 0;
117 }
118 if (x.done || y.done) return x.done === y.done;
119 const length = Math.min(x.value.length - i, y.value.length - j);
120 if (x.value.slice(i, i + length) !== y.value.slice(j, j + length)) return false;
121 i += length;
122 j += length;
123 }
124 } finally {
125 a.return?.();
126 b.return?.();
127 }