@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
57 lines • 2.88 kB
JavaScript
import { readSeededBlock, readSeededFields } from "./compact-seeded-block-view.js";
import { readCompactBlocks } from "./compact-text-view.js";
async function readRowBlocks(ref, components, valid) {
const blocks = [];
for await (const [, block] of components.blockRoots.entries(ref, valid))
blocks.push(block);
const view = await readCompactBlocks(components.store.text, blocks, valid);
const content = [];
for (const [index, block] of blocks.entries())
if (block.seeded)
content[block.contentIndex] = await readSeededBlock(components.store, block.seeded, {
text: view[index].text,
valid,
});
return { frozen: blocks, blocks: view, content };
}
/** The live messages of a cut, read for a legacy view; never the settled history. Called under the cut's lease. */
export async function readActiveMessages(frozen, components, valid) {
const messages = [];
for await (const [, row] of components.messageRoots.entries(frozen.active, valid)) {
const { frozen: rows, blocks, content } = await readRowBlocks(row.blocks, components, valid);
messages.push({
messageId: row.messageId,
fields: row.fields ? await readSeededFields(components.store, row.fields, valid) : undefined,
blocks: rows.map((block, index) => ({
contentIndex: block.contentIndex,
text: blocks[index].text,
phase: block.seeded?.phase,
block: content[block.contentIndex],
})),
});
}
valid();
return messages;
}
/** Whole-view compatibility assembly, called only while the owner holds its cut operation lease. */
export async function readCompactState(frozen, components, valid) {
const readMessage = async (row) => {
const { blocks, content } = await readRowBlocks(row.blocks, components, valid);
if (!row.fields)
return { blocks, ...(content.length ? { message: { content } } : {}) };
const fields = await readSeededFields(components.store, row.fields, valid);
return {
blocks,
message: "content" in fields ? fields : { ...fields, content },
...(row.correlation ? { correlation: await readSeededFields(components.store, row.correlation, valid) } : {}),
};
};
const messages = [];
for (const [messageId, row] of await components.settled.entries(frozen.settled))
messages.push({ messageId, persistence: row.persistence, ...(await readMessage(row)) });
for await (const [, row] of components.messageRoots.entries(frozen.active, valid))
messages.push({ messageId: row.messageId, persistence: undefined, ...(await readMessage(row)) });
valid();
return { sequence: frozen.sequence, messages };
}
//# sourceMappingURL=compact-state-view.js.map