@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
62 lines • 2.75 kB
JavaScript
export const PRESENCE_PAUSE_RECORD = "presence.pause";
export const PRESENCE_RESUME_RECORD = "presence.resume";
const OPEN_PAUSE_PREFIX = "presence.open:";
const openKey = (sessionId, machine) => `${OPEN_PAUSE_PREFIX}${JSON.stringify([sessionId, machine])}`;
const isStoreArray = (value) => Array.isArray(value);
function decodePause(value) {
if (typeof value !== "object" || value === null || isStoreArray(value))
throw new Error("presence: an open pause document is not an object");
const { pauseId, sessionId, machine, role } = value;
if (typeof pauseId !== "string" ||
typeof sessionId !== "string" ||
typeof machine !== "string" ||
(role !== "owner" && role !== "hand"))
throw new Error("presence: an open pause document is malformed");
return { pauseId, sessionId, machine, role };
}
const encodePause = (pause) => ({ ...pause });
/** All writers on one writable store epoch share its single admission line. */
export function createPresenceLedger(options) {
const { store, epoch, admission } = options;
const read = async (key) => {
const value = await store.getDocument(key);
return value === undefined ? undefined : decodePause(value);
};
return {
pause: (sessionId, machine, role) => admission.admit(async () => {
const key = openKey(sessionId, machine);
const open = await read(key);
if (open !== undefined)
return open;
const pause = { pauseId: store.mintId(), sessionId, machine, role };
await store.commit(epoch, [
{
op: "append",
id: `${PRESENCE_PAUSE_RECORD}:${pause.pauseId}`,
kind: PRESENCE_PAUSE_RECORD,
data: encodePause(pause),
},
{ op: "put", key, data: encodePause(pause) },
], () => { });
return pause;
}),
resume: (sessionId, machine) => admission.admit(async () => {
const key = openKey(sessionId, machine);
const open = await read(key);
if (open === undefined)
return undefined;
await store.commit(epoch, [
{
op: "append",
id: `${PRESENCE_RESUME_RECORD}:${open.pauseId}`,
kind: PRESENCE_RESUME_RECORD,
data: encodePause(open),
},
{ op: "delete", key },
], () => { });
return open;
}),
openPauses: async () => (await store.scanDocuments(OPEN_PAUSE_PREFIX)).map(({ data }) => decodePause(data)),
};
}
//# sourceMappingURL=presence-ledger.js.map