UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

110 lines • 4.57 kB
export const HELD_INPUT_ENTRY = "held-input"; export const HELD_INPUT_WITHDRAWN_ENTRY = "held-input-withdrawn"; export const HELD_INPUTS_CONSUMED_ENTRY = "held-inputs-consumed"; /** The held-input entries of the whole history, read without keeping a full-history view (senpi#2537). */ export const heldInputHistory = { initial: () => [], add: (entries, entry) => { if (entry.type === "custom" && (entry.customType === HELD_INPUT_ENTRY || entry.customType === HELD_INPUT_WITHDRAWN_ENTRY || entry.customType === HELD_INPUTS_CONSUMED_ENTRY)) entries.push(entry); return entries; }, }; function isHeldInputData(value) { if (typeof value !== "object" || value === null) return false; const data = value; return (typeof data.inputId === "string" && typeof data.revision === "number" && typeof data.text === "string" && (data.prepare === undefined || data.prepare === true) && (data.images === undefined || (Array.isArray(data.images) && data.images.every(isImageContent)))); } function isImageContent(value) { if (typeof value !== "object" || value === null) return false; const part = value; return part.type === "image" && typeof part.data === "string" && typeof part.mimeType === "string"; } function settledOf(value) { if (typeof value !== "object" || value === null) return []; const inputs = value.inputs; if (!Array.isArray(inputs)) return []; return inputs.filter((input) => typeof input === "object" && input !== null && typeof input.inputId === "string" && typeof input.revision === "number"); } /** * Human input stored while a run-owned hold keeps it from starting a turn (senpi#3811). Idempotent by * input id: the same id and revision is one entry, the same id with another revision is refused, so a * guidance record replayed after a crash is never delivered twice and never silently replaced. */ export class HeldInputs { constructor(entries, append) { this.inputs = new Map(); this.settled = new Map(); this.append = append; for (const entry of entries) { if (entry.type !== "custom") continue; if (entry.customType === HELD_INPUT_ENTRY && isHeldInputData(entry.data)) { this.inputs.set(entry.data.inputId, { ...entry.data, entryId: entry.id }); } else if (entry.customType === HELD_INPUT_WITHDRAWN_ENTRY || entry.customType === HELD_INPUTS_CONSUMED_ENTRY) { for (const input of settledOf(entry.data)) this.settle(input); } } } hold(input) { const existing = this.inputs.get(input.inputId); if (existing !== undefined) { if (existing.revision !== input.revision) return { kind: "input_id_conflict", heldRevision: existing.revision }; return { kind: "held", entryId: existing.entryId, duplicate: true }; } const settledRevision = this.settled.get(input.inputId); if (settledRevision === input.revision) return { kind: "already_delivered", revision: settledRevision }; if (settledRevision !== undefined) return { kind: "input_id_conflict", heldRevision: settledRevision }; const entryId = this.append(HELD_INPUT_ENTRY, input); this.inputs.set(input.inputId, { ...input, entryId }); return { kind: "held", entryId, duplicate: false }; } withdraw(inputId) { const input = this.inputs.get(inputId); if (input === undefined) return false; const settled = { inputId, revision: input.revision }; this.append(HELD_INPUT_WITHDRAWN_ENTRY, { inputs: [settled] }); this.settle(settled); return true; } pending() { return [...this.inputs.values()]; } consume(inputIds) { const held = inputIds.flatMap((id) => { const input = this.inputs.get(id); return input === undefined ? [] : [{ inputId: id, revision: input.revision }]; }); if (held.length === 0) return; this.append(HELD_INPUTS_CONSUMED_ENTRY, { inputs: held }); for (const settled of held) this.settle(settled); } settle(input) { this.inputs.delete(input.inputId); this.settled.set(input.inputId, input.revision); } } //# sourceMappingURL=held-inputs.js.map