UNPKG

@code-yeongyu/senpi

Version:

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

57 lines • 2.26 kB
import { contentText } from "@earendil-works/pi-ai"; const EDIT_CODES = { empty: "empty", "not-assistant": "not_assistant", "not-found": "not_found", "stale-leaf": "stale_leaf", }; export class AssistantEditError extends Error { constructor(reason, message) { super(message); this.name = "AssistantEditError"; this.reason = reason; } get code() { return EDIT_CODES[this.reason]; } } /** Thrown by every tree mutation while a response is streaming; outranks the stale-leaf guard. */ export class SessionStreamingError extends Error { constructor() { super("Wait for the current response to finish before navigating the session tree."); this.code = "streaming"; this.name = "SessionStreamingError"; } } /** Optimistic-concurrency guard: the caller's leaf token must equal the session's current leaf. */ export function assertExpectedLeaf(expectedLeafId, currentLeafId) { if (expectedLeafId === undefined || expectedLeafId === currentLeafId) return; throw new AssistantEditError("stale-leaf", `Session leaf moved (expected ${expectedLeafId}, now ${currentLeafId ?? "root"})`); } export function assistantTextEquals(original, text) { return contentText(original.content, "").trim() === text.trim(); } /** * Text replaces every content block: thinking signatures and tool calls belong to the abandoned * response, and a leaf ending in tool calls without results is rejected by providers. * Model identity and usage stay so per-path cost and context estimates remain accurate. */ export function buildEditedAssistantMessage(original, text) { const trimmed = text.trim(); if (trimmed.length === 0) { throw new AssistantEditError("empty", "Edited assistant response cannot be empty"); } return { role: "assistant", content: [{ type: "text", text: trimmed }], api: original.api, provider: original.provider, model: original.model, ...(original.responseModel !== undefined ? { responseModel: original.responseModel } : {}), usage: original.usage, stopReason: "stop", timestamp: Date.now(), }; } //# sourceMappingURL=edited-assistant-message.js.map