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 161 to 162

Compare

+74 −0 in 1 place
new file, 74 lines
1+import { JsonChunkCursor } from "./json-chunk-cursor.ts";
2+
3+export type JsonEvent =
4+ | { kind: "object" | "array" | "end" | "string" | "string-end" }
5+ | { kind: "key" | "text"; value: string }
6+ | { kind: "value"; value: number | boolean | null };
7+
8+/** Structural JSON decoding. Retains nesting and one bounded string fragment, not a decoded tree. */
9+export function* jsonEvents(chunks: Iterable<string>): IterableIterator<JsonEvent> {
10+ const cursor = new JsonChunkCursor(chunks);
11+ const whitespace = () => {
12+ while (cursor.peek() !== undefined && /[ \t\n\r]/.test(cursor.peek()!)) cursor.take();
13+ };
14+ function* value(depth: number): IterableIterator<JsonEvent> {
15+ if (depth > 128) throw Error("JSON nesting exceeds bound");
16+ whitespace();
17+ const first = cursor.peek();
18+ if (first === '"') {
19+ yield { kind: "string" };
20+ for (const text of cursor.stringChunks()) yield { kind: "text", value: text };
21+ yield { kind: "string-end" };
22+ return;
23+ }
24+ if (first === "{" || first === "[") {
25+ const object = cursor.take() === "{";
26+ const close = object ? "}" : "]";
27+ yield { kind: object ? "object" : "array" };
28+ whitespace();
29+ if (cursor.peek() === close) {
30+ cursor.take();
31+ yield { kind: "end" };
32+ return;
33+ }
34+ for (;;) {
35+ if (object) {
36+ whitespace();
37+ let key = "";
38+ for (const text of cursor.stringChunks()) {
39+ key += text;
40+ if (key.length > 4096) throw Error("JSON key exceeds bound");
41+ }
42+ yield { kind: "key", value: key };
43+ whitespace();
44+ if (cursor.take() !== ":") throw Error("expected JSON colon");
45+ }
46+ yield* value(depth + 1);
47+ whitespace();
48+ const separator = cursor.take();
49+ if (separator === close) break;
50+ if (separator !== ",") throw Error("expected JSON separator");
51+ }
52+ yield { kind: "end" };
53+ return;
54+ }
55+ let token = "";
56+ while (cursor.peek() !== undefined && !/[ \t\n\r,\]}]/.test(cursor.peek()!)) {
57+ token += cursor.take();
58+ if (token.length > 256) throw Error("JSON scalar exceeds bound");
59+ }
60+ if (token === "true" || token === "false" || token === "null") {
61+ yield { kind: "value", value: token === "null" ? null : token === "true" };
62+ } else {
63+ if (!/^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/.test(token)) throw Error("invalid JSON scalar");
64+ yield { kind: "value", value: Number(token) };
65+ }
66+ }
67+ try {
68+ yield* value(0);
69+ whitespace();
70+ if (cursor.peek() !== undefined) throw Error("trailing JSON input");
71+ } finally {
72+ cursor.close();
73+ }
74+}
MCP
compare on this site's /mcp
CLI
abso diff 161 162 server/src/json-events.ts --json
JSON
/christopher/abso-core/compare/161..162?file=server%2Fsrc%2Fjson-events.ts&format=json