@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
67 lines • 3.14 kB
JavaScript
/**
* The walk steps of a JSON-normal value already in memory (a call header, a descriptor row), produced iteratively
* with one frame per open container, so a deep value never recurses.
*/
export function* literalSteps(value) {
const stack = [];
let pending = { value };
for (;;) {
if (pending) {
const current = pending.value;
pending = undefined;
if (typeof current === "string")
yield { kind: "string", text: current, more: false };
else if (typeof current === "object" && current !== null) {
const array = Array.isArray(current);
yield { kind: "open", container: array ? "array" : "object" };
stack.push({ children: array ? current : Object.entries(current).flat(), index: 0 });
}
else if (typeof current === "number" || typeof current === "boolean" || current === null)
yield { kind: "scalar", value: current };
else
yield { kind: "scalar", value: null };
continue;
}
const frame = stack[stack.length - 1];
if (!frame)
return;
if (frame.index < frame.children.length)
pending = { value: frame.children[frame.index++] };
else {
stack.pop();
yield { kind: "close" };
}
}
}
/** One JSON object from named fields, each field's value streamed from its own steps. */
export async function* objectSteps(fields) {
yield { kind: "open", container: "object" };
for await (const [name, steps] of fields) {
yield { kind: "string", text: name, more: false };
yield* steps;
}
yield { kind: "close" };
}
export async function* arraySteps(items) {
yield { kind: "open", container: "array" };
for await (const steps of items)
yield* steps;
yield { kind: "close" };
}
/** Stored JSON text (one value split across leaves) as the value itself, one leaf resident at a time. */
export async function* storedSteps(store, leaves) {
for (const [index, leaf] of leaves.entries())
yield { kind: "raw", text: await store.readLeaf(leaf), more: index + 1 < leaves.length };
}
export const literalFields = (value) => Object.entries(value).map(([name, item]) => [name, literalSteps(item)]);
/** Stored text ranges as one JSON string, chunk by chunk, then a literal suffix; an empty range is `""`. */
export async function* textSteps(texts, roots, valid, suffix = "") {
for (const root of roots)
for await (const chunk of texts.chunks(root, valid))
yield { kind: "string", text: chunk, more: true };
yield { kind: "string", text: suffix, more: false };
}
/** Seeded fields (a record of stored values) as one object, each value walked with one page resident. */
export const fieldSteps = (values, fields, valid) => objectSteps(Object.entries(fields).map(([name, ref]) => [name, values.walk(ref, valid)]));
export const walkedSteps = (values, ref, valid) => ref ? values.walk(ref, valid) : literalSteps(null);
//# sourceMappingURL=compact-catalog-steps.js.map