@lune_99/pi-ask-question 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,109 @@
1
+ import { stripTerminalSequences, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
2
+
3
+ import type {
4
+ AnswerMessageQuestion,
5
+ AskQuestionAnswer,
6
+ AskQuestionRequest,
7
+ } from "./questionManager.ts";
8
+
9
+ /** Widest a tab label gets before it is cut, so several tabs still fit one row. */
10
+ export const TAB_LABEL_MAX_WIDTH = 16;
11
+
12
+ /**
13
+ * Tab label of a question: the header the model writes for it.
14
+ *
15
+ * There is deliberately no fallback. Truncating the question would put a sentence in the tab,
16
+ * and a generated "Q1" would carry no information the user can use, so the tool schema requires
17
+ * a header instead. The width cut is only a guard: a CJK header is wider than its code units.
18
+ */
19
+ export function questionTabLabel(question: AnswerMessageQuestion): string {
20
+ return truncateToWidth(question.header?.trim() ?? "", TAB_LABEL_MAX_WIDTH, "…");
21
+ }
22
+
23
+ /**
24
+ * `<header>: ` prefix of a question, empty only for a restored snapshot without a header.
25
+ *
26
+ * Callers show the question text after this, so falling back to the question text here would
27
+ * print the same sentence twice.
28
+ */
29
+ export function questionHeaderPrefix(question: AnswerMessageQuestion): string {
30
+ const label = questionTabLabel(question);
31
+
32
+ return label.length === 0 ? "" : `${label}: `;
33
+ }
34
+
35
+ /** Collapse a question (or option text) onto one row for a dock or a header. */
36
+ export function summarizeText(text: string, maxWidth: number): string {
37
+ const singleLine = stripTerminalSequences(text)
38
+ .replace(/[\r\n\t]/g, " ")
39
+ .replace(/ +/g, " ")
40
+ .trim();
41
+
42
+ if (maxWidth <= 0) {
43
+ return "";
44
+ }
45
+
46
+ // The budget is terminal columns: a CJK question is longer on screen than in code units.
47
+ return visibleWidth(singleLine) <= maxWidth
48
+ ? singleLine
49
+ : truncateToWidth(singleLine, maxWidth, "…");
50
+ }
51
+
52
+ /** Numbered label of one option as the panel shows it: `2. Name`. */
53
+ export function optionLabel(question: AnswerMessageQuestion, index: number): string {
54
+ const label = question.options?.[index]?.label ?? `option ${index + 1}`;
55
+
56
+ return `${index + 1}. ${label}`;
57
+ }
58
+
59
+ /** Model-facing summary of one answer; undefined while the question is unanswered. */
60
+ export function formatAnswer(
61
+ question: AnswerMessageQuestion,
62
+ answer: AskQuestionAnswer | undefined,
63
+ ): string | undefined {
64
+ if (!answer) {
65
+ return undefined;
66
+ }
67
+
68
+ if (answer.customText !== undefined && answer.customText.length > 0) {
69
+ return `wrote: ${answer.customText}`;
70
+ }
71
+
72
+ if (answer.selectedIndexes.length === 0) {
73
+ return undefined;
74
+ }
75
+
76
+ return `selected: ${answer.selectedIndexes.map((index) => optionLabel(question, index)).join(", ")}`;
77
+ }
78
+
79
+ /** Model-facing text of the follow-up message that carries answered or skipped questions. */
80
+ export function formatAnswerMessage(request: AskQuestionRequest): string {
81
+ if (request.status === "skipped") {
82
+ return [
83
+ "The user skipped these questions without answering.",
84
+ "",
85
+ ...request.questions.map((question) => question.question),
86
+ ].join("\n");
87
+ }
88
+
89
+ const lines: string[] = ["The user answered these questions.", ""];
90
+
91
+ for (const [index, question] of request.questions.entries()) {
92
+ lines.push(question.question, `answer: ${formatAnswer(question, request.answers?.[index]) ?? "unanswered"}`, "");
93
+ }
94
+
95
+ return lines.join("\n").trimEnd();
96
+ }
97
+
98
+ /**
99
+ * Model-facing text of the tool result, which returns before any answer exists.
100
+ *
101
+ * The questions are left out on purpose: they are the tool call's own arguments, and the
102
+ * request id is nothing the model can act on - neither belongs in the context twice.
103
+ */
104
+ export function formatPendingMessage(): string {
105
+ return [
106
+ "Questions are pending. Continue any independent work.",
107
+ "The user's answers will arrive as a follow-up message.",
108
+ ].join("\n");
109
+ }
@@ -0,0 +1,34 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+
3
+ import { deliverQuestionResult, type QuestionDeliveryOptions } from "./question-delivery.ts";
4
+ import { questionManager } from "./questionManager.ts";
5
+
6
+ export { ASK_QUESTION_ANSWER_MESSAGE } from "./question-delivery.ts";
7
+
8
+ export type QuestionAnswerNotificationOptions = QuestionDeliveryOptions;
9
+
10
+ /**
11
+ * Attempt delivery as soon as a request settles.
12
+ *
13
+ * This is the initial attempt only. The persistence listener runs first, so the settled
14
+ * request is already in the durable outbox when this send happens; if pi drops the message,
15
+ * reconciliation recovers it instead of the answer being lost with the call.
16
+ */
17
+ export function registerQuestionAnswerNotifications(
18
+ pi: ExtensionAPI,
19
+ options: QuestionAnswerNotificationOptions = {},
20
+ ): () => void {
21
+ return questionManager.subscribe((event) => {
22
+ if (event.type !== "request-answered" && event.type !== "request-skipped") {
23
+ return;
24
+ }
25
+
26
+ const request = questionManager.get(event.id);
27
+
28
+ if (!request) {
29
+ return;
30
+ }
31
+
32
+ deliverQuestionResult(pi, request, options);
33
+ });
34
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Data shapes shared by the question state, the tools, the panel and the preview pane.
3
+ *
4
+ * They live apart from `questionManager.ts` so a pure renderer can describe a question
5
+ * without importing the state singleton: the preview pane is a function of the question
6
+ * data it is handed, never of what happens to be pending.
7
+ */
8
+
9
+ export interface AskQuestionOption {
10
+ label: string;
11
+ description?: string;
12
+ /** Markdown shown in the preview pane while this option is focused. */
13
+ preview?: string;
14
+ }
15
+
16
+ export interface AskQuestion {
17
+ /**
18
+ * Short label for the panel tabs, so several questions can be told apart without an
19
+ * invented "Q1"/"Q2" numbering. The panel falls back to the question text when absent.
20
+ */
21
+ header?: string;
22
+ /** The question text shown to the user and sent back to the model. */
23
+ question: string;
24
+ /** Optional user-visible content above the answer controls, such as a preview or snippet. */
25
+ displayText?: string;
26
+ /** Predefined choices; omitted for a free-form question. */
27
+ options?: AskQuestionOption[];
28
+ multiSelect?: boolean;
29
+ }
30
+
31
+ /**
32
+ * The question fields an answer row, a tab label or a model-facing answer reads.
33
+ *
34
+ * `displayText` and `option.preview` are panel display input: copying them into the delivered
35
+ * message would keep every preview in the transcript for good.
36
+ */
37
+ export interface AnswerMessageQuestion {
38
+ header?: string;
39
+ question: string;
40
+ options?: Array<{ label: string }>;
41
+ }
42
+
43
+ /** One answered question: either option indexes or the custom text the user wrote. */
44
+ export interface AskQuestionAnswer {
45
+ selectedIndexes: number[];
46
+ customText?: string;
47
+ }
48
+
49
+ /**
50
+ * Panel state that must survive an Esc close and a restart.
51
+ *
52
+ * The panel component is recreated on every open, so it is restored from here instead
53
+ * of holding long-lived state of its own. Indexes line up with `questions`.
54
+ */
55
+ export interface AskQuestionDraft {
56
+ /** Focused tab: a question index, or `questions.length` for the submit tab. */
57
+ currentIndex: number;
58
+ /** Focused row inside the focused question: an option index, the custom row, or the next row. */
59
+ optionIndex: number;
60
+ answers: (AskQuestionAnswer | undefined)[];
61
+ /** Text written but not committed yet, so a deferred close does not lose it. */
62
+ customDrafts: (string | undefined)[];
63
+ }
64
+
65
+ export type AskQuestionStatus = "pending" | "answered" | "skipped";
66
+
67
+ export interface AskQuestionRequest {
68
+ id: string;
69
+ createdAt: number;
70
+ questions: AskQuestion[];
71
+ /**
72
+ * Order in which the panel showed this request, used to pick which one to show next: a
73
+ * request that was never shown comes first, otherwise the least recently shown one does.
74
+ */
75
+ shownSeq?: number;
76
+ status: AskQuestionStatus;
77
+ draft: AskQuestionDraft;
78
+ answers?: AskQuestionAnswer[];
79
+ settledAt?: number;
80
+ }
81
+
82
+ export interface AskQuestionStateSnapshot {
83
+ requests: AskQuestionRequest[];
84
+ /**
85
+ * Settled requests whose answer message is not confirmed in the session yet.
86
+ *
87
+ * `pi.sendMessage()` only attempts delivery, so these stay durable until the active
88
+ * branch really contains their message; a request is never moved back to `requests`.
89
+ */
90
+ outbox: AskQuestionRequest[];
91
+ }