UNPKG

@code-yeongyu/senpi

Version:

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

222 lines • 9.77 kB
/** * Ask-user question overlay: tab bar of question headers, numbered options * with descriptions, per-question own-answer editor, a Submit tab with review * rows and the comment editor, submit footer and a countdown chip. Layout lives in * ask-user-question-render.ts, interaction rules in ask-user-question-state.ts * and key dispatch in ask-user-question-keys.ts. */ import { Container, Input, Spacer, Text } from "@earendil-works/pi-tui"; import { AskUserCountdown } from "./ask-user-countdown.js"; import { handleAskUserKeyInput } from "./ask-user-question-keys.js"; import { AskUserQuestionTabs, isQuestionMouseAction, questionMouseRegion } from "./ask-user-question-mouse.js"; import { renderCommentLabel, renderHintsLine, renderNotice, renderOwnAnswerLabel, renderQuestionLine, renderQuestionList, renderSubmitLine, renderSubmitSummary, renderTabLabels, renderTitle, } from "./ask-user-question-render.js"; import { AskUserQuestionState, formatCountdownLabel, NOT_ANSWERED_NOTICE, } from "./ask-user-question-state.js"; import { DynamicBorder } from "./dynamic-border.js"; export class AskUserQuestionComponent extends Container { constructor(request, done, opts = {}) { super(); this.ownAnswerInput = new Input(); this.commentInput = new Input(); this.listContainer = new Container(); this.ownAnswerContainer = new Container(); this.submitContainer = new Container(); this.countdownLabel = ""; this.settled = false; this._focused = false; this.state = new AskUserQuestionState(request); this.doneCallback = done; this.options = opts; this.keyHandlerContext = { state: this.state, ownAnswerInput: this.ownAnswerInput, commentInput: this.commentInput, finish: (status, autoResolvedAfterMs) => this.finish(status, autoResolvedAfterMs), attemptSubmit: () => this.attemptSubmit(), openOwnAnswer: (initialText) => this.openOwnAnswer(initialText), commitOwnAnswer: () => this.commitOwnAnswer(), emitProgress: () => this.emitProgress(), updateAll: () => this.updateAll(), }; this.addChild(new DynamicBorder()); this.addChild(new Spacer(1)); this.titleText = new Text("", 1, 0); this.addChild(this.titleText); this.tabText = new AskUserQuestionTabs((index) => { if (index === this.state.request.questions.length) this.clickSubmit(); else { this.state.jumpToQuestion(index); this.updateAll(); } }); this.addChild(this.tabText); this.questionText = new Text("", 1, 0); this.addChild(this.questionText); this.addChild(this.listContainer); this.addChild(this.ownAnswerContainer); this.addChild(this.submitContainer); this.noticeText = new Text("", 1, 0); this.addChild(this.noticeText); this.submitText = new Text("", 1, 0); this.addChild(questionMouseRegion(this.submitText, () => this.clickSubmit())); this.hintsText = new Text("", 1, 0); this.addChild(this.hintsText); this.addChild(new Spacer(1)); this.addChild(new DynamicBorder()); if (opts.initialDraft) { this.state.restoreDraft(opts.initialDraft); if (this.state.comment !== undefined) this.commentInput.setValue(this.state.comment); } if (opts.initialQuestionIndex !== undefined) this.state.jumpToQuestion(opts.initialQuestionIndex); const timeoutMs = opts.timeoutMs ?? request.timeoutMs; if (timeoutMs > 0 || opts.getDeadlineAtMs) { this.countdown = new AskUserCountdown(timeoutMs, opts.tui, (remainingMs) => { this.countdownLabel = formatCountdownLabel(remainingMs); this.updateTitle(); this.options.tui?.requestRender(); }, () => this.finish("timed_out", timeoutMs), opts.getDeadlineAtMs); } this.updateAll(); } get focused() { return this._focused; } set focused(value) { this._focused = value; this.updateAll(); } handleInput(data) { handleAskUserKeyInput(this.keyHandlerContext, data); } handleMouse(event) { if (this.settled || !isQuestionMouseAction(event)) return undefined; return super.handleMouse(event); } dispose() { this.countdown?.dispose(); } clickSubmit() { if (this.state.focus === "submit") this.attemptSubmit(); else { this.state.enterSubmit(); this.updateAll(); } } /** Widget entry commits visible selection feedback before resolving a one-question answer. */ clickOption(index, submitSingleQuestion = false) { const question = this.state.activeQuestion; const immediate = submitSingleQuestion && this.state.request.questions.length === 1 && !question.multiSelect; this.state.highlightIndex = index; this.state.activateOption(question.id, question.options[index].label); if (!question.multiSelect && !immediate) this.state.advance(); this.emitProgress(); this.updateAll(); if (immediate) { this.options.tui?.renderNow(); this.attemptSubmit(); } } openOwnAnswer(initialText) { this.state.focus = "own-answer"; const existing = this.state.textFor(this.state.activeQuestion.id) ?? ""; this.ownAnswerInput.setValue(""); if (existing !== "") this.ownAnswerInput.handleInput(existing); if (initialText !== undefined) this.ownAnswerInput.handleInput(initialText); this.updateAll(); } commitOwnAnswer() { this.state.setOwnAnswer(this.state.activeQuestion.id, this.ownAnswerInput.getValue()); this.ownAnswerInput.setValue(""); this.emitProgress(); } attemptSubmit() { this.state.comment = this.commentInput.getValue(); const outcome = this.state.submitOutcome(this.state.notice !== undefined); if (!outcome) { this.state.notice = NOT_ANSWERED_NOTICE; this.updateAll(); return; } this.state.acceptPartialSubmit(); this.finish(outcome); } emitProgress() { const draft = this.state.refreshDraft(); if (this.state.focus === "own-answer") { const live = this.ownAnswerInput.getValue().trim(); if (live !== "") { draft.answers = { ...draft.answers, [this.state.activeQuestion.id]: { selected: [], text: live }, }; } } this.options.onProgress?.(draft); } finish(status, autoResolvedAfterMs) { if (this.settled) return; this.settled = true; this.countdown?.dispose(); this.state.comment = this.commentInput.getValue(); const response = this.state.buildResponse(status, autoResolvedAfterMs); // The widget cancels only on the user's own key (Esc, Ctrl+C); an abort never reaches here. this.doneCallback(status === "cancelled" ? { ...response, cancelOrigin: "user" } : response); } applyFocusFlags() { this.ownAnswerInput.focused = this._focused && this.state.focus === "own-answer"; this.commentInput.focused = this._focused && this.state.isCommentFocused; } updateTitle() { this.titleText.setText(renderTitle(this.countdownLabel)); } updateAll() { this.updateTitle(); this.applyFocusFlags(); this.tabText.setTabs(renderTabLabels(this.state)); this.questionText.setText(this.state.focus === "submit" ? "Review your answers" : renderQuestionLine(this.state.activeQuestion)); this.listContainer.clear(); const lines = this.state.focus === "submit" ? renderSubmitSummary(this.state) : renderQuestionList(this.state, this._focused); let optionIndex = 0; let description = false; for (const line of lines) { const text = new Text(line, 1, 0); if (this.state.focus === "submit" || description) { this.listContainer.addChild(text); description = false; } else if (optionIndex < this.state.activeQuestion.options.length) { const index = optionIndex++; this.listContainer.addChild(questionMouseRegion(text, () => this.clickOption(index))); description = Boolean(this.state.activeQuestion.options[index].description); } else { this.listContainer.addChild(questionMouseRegion(text, () => this.openOwnAnswer())); } } this.ownAnswerContainer.clear(); if (this.state.focus === "own-answer") { this.ownAnswerContainer.addChild(new Text(renderOwnAnswerLabel(), 1, 0)); this.ownAnswerContainer.addChild(this.ownAnswerInput); } this.submitContainer.clear(); if (this.state.focus === "submit") { this.submitContainer.addChild(new Spacer(1)); this.submitContainer.addChild(new Text(renderCommentLabel(), 1, 0)); this.submitContainer.addChild(this.commentInput); } this.noticeText.setText(renderNotice(this.state.notice)); this.submitText.setText(renderSubmitLine(this.state)); this.hintsText.setText(renderHintsLine(this.state)); } } //# sourceMappingURL=ask-user-question.js.map