UNPKG

@code-yeongyu/senpi

Version:

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

50 lines • 1.9 kB
import { contentText } from "@earendil-works/pi-ai"; const EDIT_CODES = { empty: "empty", "not-user": "not_user", "not-found": "not_found", "stale-leaf": "stale_leaf", }; export class UserEditError extends Error { constructor(reason, message) { super(message); this.name = "UserEditError"; this.reason = reason; } get code() { return EDIT_CODES[this.reason]; } } /** * Optimistic-concurrency guard for the user-edit surface: the caller's leaf token must equal the * session's current leaf. Mirrors `assertExpectedLeaf` so a user edit reports its own error type. */ export function assertExpectedUserLeaf(expectedLeafId, currentLeafId) { if (expectedLeafId === undefined || expectedLeafId === currentLeafId) return; throw new UserEditError("stale-leaf", `Session leaf moved (expected ${expectedLeafId}, now ${currentLeafId ?? "root"})`); } export function userTextEquals(original, text) { return contentText(original.content, "").trim() === text.trim(); } /** * Text replaces every text block; every other block is carried over untouched. Unlike an edited * assistant response - whose thinking and tool calls belong to the abandoned branch - a prompt's * attachments are the user's own input, so dropping them would destroy data the model still needs. */ export function buildEditedUserMessage(original, text) { const trimmed = text.trim(); if (trimmed.length === 0) { throw new UserEditError("empty", "Edited user message cannot be empty"); } const attachments = Array.isArray(original.content) ? original.content.filter((block) => block.type !== "text") : []; const content = [{ type: "text", text: trimmed }, ...attachments]; return { role: "user", content, timestamp: Date.now(), }; } //# sourceMappingURL=edited-user-message.js.map