UNPKG

@code-yeongyu/senpi

Version:

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

61 lines • 2.67 kB
import { parseAskUserAnswerFrame } from "../ask-user/format.js"; import { sanitizeTodoText } from "./todo-format.js"; import { isTodoAsk } from "./todo-storage.js"; import { TODO_RESTORE_REQUEST_TYPE, TODO_STATE_ENTRY_TYPE } from "./todo-types.js"; export const ASK_TEXT_LIMIT = 200; export function firstTextBlock(content) { if (typeof content === "string") return content; if (!Array.isArray(content)) return undefined; for (const block of content) { if (typeof block === "object" && block !== null && block.type === "text" && typeof block.text === "string") { return block.text; } } return undefined; } export function truncateAskText(text) { const codePoints = Array.from(text); if (codePoints.length <= ASK_TEXT_LIMIT) return text; return `${codePoints.slice(0, ASK_TEXT_LIMIT).join("")}… (+${codePoints.length - ASK_TEXT_LIMIT} chars)`; } /** * The ask a newly created list anchors to. The first list of a branch anchors to the * session's first user request; a later list-creating call is an explicit redirect and * anchors to the newest user request. Ask-user answer frames are replies, not requests. * A compaction restore request newer than both the last list and the last user request * re-emits the ask its snapshot carried; the model never supplies an ask itself. */ export function captureListAsk(entries, capturedAt) { let lastStateIndex = -1; let restore; const requests = []; entries.forEach((entry, index) => { if (entry.type === "custom" && entry.customType === TODO_STATE_ENTRY_TYPE) { lastStateIndex = index; return; } if (entry.type === "custom_message" && entry.customType === TODO_RESTORE_REQUEST_TYPE) { const ask = entry.details?.ask; if (isTodoAsk(ask)) restore = { index, ask }; return; } if (entry.type !== "message" || entry.message.role !== "user") return; const raw = firstTextBlock(entry.message.content); if (raw === undefined || parseAskUserAnswerFrame(raw)) return; const text = sanitizeTodoText(raw); if (text) requests.push({ index, entryId: entry.id, text }); }); const latest = requests.at(-1); if (restore && restore.index > lastStateIndex && restore.index > (latest?.index ?? -1)) return { ...restore.ask }; const chosen = lastStateIndex === -1 ? requests[0] : latest; return chosen ? { entryId: chosen.entryId, text: truncateAskText(chosen.text), capturedAt } : undefined; } //# sourceMappingURL=todo-ask.js.map