UNPKG

@code-yeongyu/senpi

Version:

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

237 lines • 9.13 kB
/** * Pure interaction state for the ask-user question overlay. * * Owns per-question selections, own-answer texts, the active tab, the * highlighted row and the response-building rules. Rendering and key decoding * live in ask-user-question.ts; this module must stay free of pi-tui imports * so the rules are unit-testable headless. */ export const COMMENT_LABEL = "Comment (optional; unanswered questions are reported)"; export const OWN_ANSWER_LABEL = "Type your own answer..."; export const NOT_ANSWERED_NOTICE = "You have not answered all questions"; export const DISMISS_NOTICE = "Press Esc again to dismiss (answers will be discarded)"; export const PARTIAL_SUBMIT_NOTICE = "Press Enter again to submit with unanswered questions"; /** Countdown chip label: minutes above five minutes, mm:ss at or below. */ export function formatCountdownLabel(remainingMs) { const totalSeconds = Math.max(0, Math.ceil(remainingMs / 1000)); if (totalSeconds >= 300) return `${Math.ceil(totalSeconds / 60)}m`; const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; } export class AskUserQuestionState { constructor(request) { this.activeIndex = 0; this.highlightIndex = 0; this.focus = "options"; this.dismissPending = false; this.selected = new Map(); this.texts = new Map(); this.request = request; this.submitRowIndex = this.commentRowIndex; } get activeQuestion() { return this.request.questions[this.activeIndex]; } /** Highlightable rows for the active question: options plus the own-answer row. */ get ownAnswerRowIndex() { return this.activeQuestion.options.length; } get rowCount() { return this.ownAnswerRowIndex + 1; } get activeTabIndex() { return this.focus === "submit" ? this.request.questions.length : this.activeIndex; } get commentRowIndex() { return this.request.questions.length; } get isCommentFocused() { return this.focus === "submit" && this.submitRowIndex === this.commentRowIndex; } switchQuestion(delta) { this.switchTab(delta); } switchTab(delta) { const count = this.request.questions.length + 1; const next = (this.activeTabIndex + delta + count) % count; if (next === this.request.questions.length) { this.enterSubmit(); } else { this.jumpToQuestion(next); } } advance() { if (this.activeIndex + 1 < this.request.questions.length) { this.jumpToQuestion(this.activeIndex + 1); return; } this.enterSubmit(); } jumpToQuestion(index) { this.focus = "options"; // A stale caller index must never leave the overlay without an active question. const whole = Number.isFinite(index) ? Math.trunc(index) : 0; this.activeIndex = Math.min(Math.max(0, whole), this.request.questions.length - 1); this.highlightIndex = 0; this.clearTransient(); } enterSubmit() { this.focus = "submit"; this.submitRowIndex = this.commentRowIndex; this.highlightIndex = 0; this.clearTransient(); } returnToOptions() { this.leaveOwnAnswer(0); } /** Close the own-answer editor and highlight `row` of the active question. */ leaveOwnAnswer(row) { this.focus = "options"; this.highlightIndex = Math.min(Math.max(0, row), this.ownAnswerRowIndex); this.clearTransient(); } /** Move the Submit-tab highlight by `delta`, clamped to the review rows and the comment editor. */ moveSubmitRow(delta) { this.submitRowIndex = Math.min(Math.max(0, this.submitRowIndex + delta), this.commentRowIndex); this.clearTransient(); } focusComment() { this.submitRowIndex = this.commentRowIndex; } clearTransient() { this.notice = undefined; this.dismissPending = false; } requestDismiss() { if (!this.hasDraft()) return "cancel"; if (!this.dismissPending) { this.dismissPending = true; this.notice = DISMISS_NOTICE; return "confirm"; } return "cancel"; } acceptPartialSubmit() { this.dismissPending = false; this.notice = undefined; } hasDraft() { if ((this.comment ?? "").trim() !== "") return true; return this.request.questions.some((question) => this.isAnswered(question.id)); } selectedFor(questionId) { return this.selected.get(questionId) ?? []; } textFor(questionId) { return this.texts.get(questionId); } isSelected(questionId, label) { return this.selectedFor(questionId).includes(label); } isAnswered(questionId) { if (this.selectedFor(questionId).length > 0) return true; return (this.texts.get(questionId) ?? "").trim() !== ""; } answeredCount() { return this.request.questions.filter((question) => this.isAnswered(question.id)).length; } /** Select (single) or toggle (multi) an option of the active question. */ activateOption(questionId, label) { const question = this.request.questions.find((entry) => entry.id === questionId); if (!question?.options.some((option) => option.label === label)) return; if (question.multiSelect) { const next = [...this.selectedFor(questionId)]; const index = next.indexOf(label); if (index >= 0) next.splice(index, 1); else next.push(label); this.selected.set(questionId, next); } else { this.selected.set(questionId, [label]); } this.notice = undefined; } /** Replace the question's answer with typed text; empty text is a no-op so existing option selections survive. */ setOwnAnswer(questionId, text) { const trimmed = text.trim(); if (trimmed === "") return; this.selected.delete(questionId); this.texts.set(questionId, trimmed); this.notice = undefined; } clearAnswer(questionId) { this.selected.delete(questionId); this.texts.delete(questionId); this.notice = undefined; } /** Seed selections, own texts and the comment from a previously captured draft. */ restoreDraft(draft) { for (const question of this.request.questions) { const answer = draft.answers?.[question.id]; if (!answer) continue; const selected = answer.selected.filter((label) => question.options.some((option) => option.label === label)); if (selected.length > 0) this.selected.set(question.id, selected); if (answer.text !== undefined && answer.text.trim() !== "") this.texts.set(question.id, answer.text.trim()); } if (draft.comment !== undefined && draft.comment.trim() !== "") this.comment = draft.comment; } answers() { const result = {}; for (const question of this.request.questions) { const selected = this.selectedFor(question.id); const text = this.texts.get(question.id); if (selected.length === 0 && text === undefined) continue; result[question.id] = { selected, ...(text !== undefined ? { text } : {}) }; } return result; } unanswered() { return this.request.questions.filter((question) => !this.isAnswered(question.id)).map((q) => q.id); } /** Submit outcome: comment wins, then answers; undefined means stay open. */ submitOutcome(forcePartial = false) { if ((this.comment ?? "").trim() !== "") return "comment-submitted"; if (this.unanswered().length === 0) return "answered"; if (this.answeredCount() > 0) return "answered"; if (forcePartial) return "answered"; return undefined; } buildResponse(status, autoResolvedAfterMs) { const comment = this.comment !== undefined && this.comment.trim() !== "" ? this.comment : undefined; return { status, ...(status === "answered" || status === "comment-submitted" ? { resolvedBy: "local_ui" } : {}), answers: this.answers(), ...(comment !== undefined ? { comment } : {}), unanswered: this.unanswered(), ...(autoResolvedAfterMs !== undefined ? { autoResolvedAfterMs } : {}), }; } refreshDraft() { const comment = this.comment !== undefined && this.comment.trim() !== "" ? this.comment : undefined; return { answers: this.answers(), ...(comment !== undefined ? { comment } : {}), }; } } //# sourceMappingURL=ask-user-question-state.js.map