@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
67 lines • 2.32 kB
JavaScript
import { isRecord } from "./work-custody-record.js";
/** Occurrence identity outlives clients, turns, and process restarts. */
export class LifecycleAttention {
constructor(sessionId, restored) {
this.open = new Map();
this.sessionId = sessionId;
this.sequence = restored?.attentionSeq ?? 0;
for (const occurrence of restored?.synthetic ?? [])
this.open.set(occurrence.key, occurrence.wait);
}
get attentionSeq() {
return this.sequence;
}
get synthetic() {
return [...this.open].map(([key, wait]) => ({ key, wait }));
}
reconcile(requests) {
const wanted = new Set(requests.map((request) => request.key));
for (const key of this.open.keys())
if (!wanted.has(key))
this.open.delete(key);
return requests.map(({ key, kind }) => {
const existing = this.open.get(key);
if (existing?.kind === kind)
return existing;
const wait = {
kind,
originSessionId: this.sessionId,
requestId: `${key}:${++this.sequence}`,
revision: 1,
since: new Date().toISOString(),
};
this.open.set(key, wait);
return wait;
});
}
}
export function readHumanWait(value) {
if (!isRecord(value))
return undefined;
switch (value.kind) {
case "question":
case "approval":
case "goal_blocked":
case "goal_paused":
case "stalled":
case "interrupted_run":
break;
default:
return undefined;
}
if (typeof value.originSessionId !== "string" ||
typeof value.requestId !== "string" ||
typeof value.revision !== "number" ||
typeof value.since !== "string")
return undefined;
return {
kind: value.kind,
originSessionId: value.originSessionId,
requestId: value.requestId,
revision: value.revision,
since: value.since,
...(typeof value.childSessionId === "string" ? { childSessionId: value.childSessionId } : {}),
...(typeof value.childLabel === "string" ? { childLabel: value.childLabel } : {}),
};
}
//# sourceMappingURL=lifecycle-attention.js.map