pi-ask-user 0.5.1 → 0.6.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.
package/index.ts CHANGED
@@ -9,23 +9,23 @@ import type { ExtensionAPI, Theme } from "@mariozechner/pi-coding-agent";
9
9
  import { getMarkdownTheme } from "@mariozechner/pi-coding-agent";
10
10
  import { Type } from "@sinclair/typebox";
11
11
  import {
12
- Container,
13
- type Component,
14
- decodeKittyPrintable,
15
- Editor,
16
- type EditorTheme,
17
- fuzzyFilter,
18
- Key,
19
- type Keybinding,
20
- type KeybindingsManager,
21
- Markdown,
22
- type MarkdownTheme,
23
- matchesKey,
24
- Spacer,
25
- Text,
26
- type TUI,
27
- truncateToWidth,
28
- wrapTextWithAnsi,
12
+ Container,
13
+ type Component,
14
+ decodeKittyPrintable,
15
+ Editor,
16
+ type EditorTheme,
17
+ fuzzyFilter,
18
+ Key,
19
+ type Keybinding,
20
+ type KeybindingsManager,
21
+ Markdown,
22
+ type MarkdownTheme,
23
+ matchesKey,
24
+ Spacer,
25
+ Text,
26
+ type TUI,
27
+ truncateToWidth,
28
+ wrapTextWithAnsi,
29
29
  } from "@mariozechner/pi-tui";
30
30
  import { renderSingleSelectRows } from "./single-select-layout";
31
31
 
@@ -36,66 +36,122 @@ const ASK_USER_VERSION: string = (_require("./package.json") as { version: strin
36
36
  type AskOptionInput = QuestionOption | string;
37
37
 
38
38
  interface AskParams {
39
- question: string;
40
- context?: string;
41
- options?: AskOptionInput[];
42
- allowMultiple?: boolean;
43
- allowFreeform?: boolean;
44
- timeout?: number;
39
+ question: string;
40
+ context?: string;
41
+ options?: AskOptionInput[];
42
+ allowMultiple?: boolean;
43
+ allowFreeform?: boolean;
44
+ allowComment?: boolean;
45
+ timeout?: number;
45
46
  }
46
47
 
48
+ type AskResponse =
49
+ | {
50
+ kind: "selection";
51
+ selections: string[];
52
+ comment?: string;
53
+ }
54
+ | {
55
+ kind: "freeform";
56
+ text: string;
57
+ };
58
+
47
59
  interface AskToolDetails {
48
- question: string;
49
- context?: string;
50
- options: QuestionOption[];
51
- answer: string | null;
52
- cancelled: boolean;
53
- wasCustom?: boolean;
60
+ question: string;
61
+ context?: string;
62
+ options: QuestionOption[];
63
+ response: AskResponse | null;
64
+ cancelled: boolean;
54
65
  }
55
66
 
56
- interface AskUIResult {
57
- answer: string;
58
- wasCustom: boolean;
59
- }
67
+ type AskUIResult = AskResponse;
60
68
 
61
69
  function normalizeOptions(options: AskOptionInput[]): QuestionOption[] {
62
- return options
63
- .map((option) => {
64
- if (typeof option === "string") {
65
- return { title: option };
66
- }
67
- if (option && typeof option === "object" && typeof option.title === "string") {
68
- return { title: option.title, description: option.description };
69
- }
70
- return null;
71
- })
72
- .filter((option): option is QuestionOption => option !== null);
70
+ return options
71
+ .map((option) => {
72
+ if (typeof option === "string") {
73
+ return { title: option };
74
+ }
75
+ if (option && typeof option === "object" && typeof option.title === "string") {
76
+ return { title: option.title, description: option.description };
77
+ }
78
+ return null;
79
+ })
80
+ .filter((option): option is QuestionOption => option !== null);
73
81
  }
74
82
 
75
83
  function formatOptionsForMessage(options: QuestionOption[]): string {
76
- return options
77
- .map((option, index) => {
78
- const desc = option.description ? ` — ${option.description}` : "";
79
- return `${index + 1}. ${option.title}${desc}`;
80
- })
81
- .join("\n");
84
+ return options
85
+ .map((option, index) => {
86
+ const desc = option.description ? ` — ${option.description}` : "";
87
+ return `${index + 1}. ${option.title}${desc}`;
88
+ })
89
+ .join("\n");
90
+ }
91
+
92
+ function normalizeOptionalComment(text: string | null | undefined): string | undefined {
93
+ const trimmed = text?.trim();
94
+ return trimmed ? trimmed : undefined;
95
+ }
96
+
97
+ function createFreeformResponse(text: string | null | undefined): AskResponse | null {
98
+ const trimmed = text?.trim();
99
+ return trimmed ? { kind: "freeform", text: trimmed } : null;
100
+ }
101
+
102
+ function createSelectionResponse(selections: string[], comment?: string | null): AskResponse | null {
103
+ const normalizedSelections = selections.map((selection) => selection.trim()).filter(Boolean);
104
+ if (normalizedSelections.length === 0) return null;
105
+
106
+ const normalizedComment = normalizeOptionalComment(comment);
107
+ return normalizedComment
108
+ ? { kind: "selection", selections: normalizedSelections, comment: normalizedComment }
109
+ : { kind: "selection", selections: normalizedSelections };
110
+ }
111
+
112
+ function formatResponseSummary(response: AskResponse): string {
113
+ if (response.kind === "freeform") return response.text;
114
+
115
+ const selections = response.selections.join(", ");
116
+ return response.comment ? `${selections} — ${response.comment}` : selections;
117
+ }
118
+
119
+ function buildCommentPrompt(prompt: string, selections: string[]): string {
120
+ const label = selections.length === 1 ? "Selected option" : "Selected options";
121
+ const lines = selections.map((selection) => `- ${selection}`).join("\n");
122
+ return `${prompt}\n\n${label}:\n${lines}`;
123
+ }
124
+
125
+ function parseDialogSelections(input: string): string[] {
126
+ return input
127
+ .split(",")
128
+ .map((selection) => selection.trim())
129
+ .filter(Boolean);
130
+ }
131
+
132
+ function isCancelledInput(value: unknown): value is null | undefined {
133
+ return value === null || value === undefined;
134
+ }
135
+
136
+ function isSelectionResponse(response: AskResponse): response is Extract<AskResponse, { kind: "selection" }> {
137
+ return response.kind === "selection";
82
138
  }
83
139
 
84
140
  function createSelectListTheme(theme: Theme) {
85
- return {
86
- selectedPrefix: (t: string) => theme.fg("accent", t),
87
- selectedText: (t: string) => theme.fg("accent", t),
88
- description: (t: string) => theme.fg("muted", t),
89
- scrollInfo: (t: string) => theme.fg("dim", t),
90
- noMatch: (t: string) => theme.fg("warning", t),
91
- };
141
+ return {
142
+ selectedPrefix: (t: string) => theme.fg("accent", t),
143
+ selectedText: (t: string) => theme.fg("accent", t),
144
+ description: (t: string) => theme.fg("muted", t),
145
+ scrollInfo: (t: string) => theme.fg("dim", t),
146
+ noMatch: (t: string) => theme.fg("warning", t),
147
+ };
92
148
  }
93
149
 
94
150
  function createEditorTheme(theme: Theme): EditorTheme {
95
- return {
96
- borderColor: (s: string) => theme.fg("accent", s),
97
- selectList: createSelectListTheme(theme),
98
- };
151
+ return {
152
+ borderColor: (s: string) => theme.fg("accent", s),
153
+ selectList: createSelectListTheme(theme),
154
+ };
99
155
  }
100
156
 
101
157
  const BOX_BORDER_LEFT = "│ ";
@@ -103,71 +159,75 @@ const BOX_BORDER_RIGHT = " │";
103
159
  const BOX_BORDER_OVERHEAD = BOX_BORDER_LEFT.length + BOX_BORDER_RIGHT.length;
104
160
 
105
161
  class BoxBorderTop implements Component {
106
- private color: (s: string) => string;
107
- private title?: string;
108
- private titleColor?: (s: string) => string;
109
- constructor(color: (s: string) => string, title?: string, titleColor?: (s: string) => string) {
110
- this.color = color;
111
- this.title = title;
112
- this.titleColor = titleColor;
113
- }
114
- invalidate(): void {}
115
- render(width: number): string[] {
116
- const inner = Math.max(0, width - 2);
117
- if (!this.title || inner < this.title.length + 4) {
118
- return [this.color(`╭${"─".repeat(inner)}╮`)];
119
- }
120
- const label = ` ${this.title} `;
121
- const remaining = inner - 1 - label.length;
122
- const titleStyle = this.titleColor ?? this.color;
123
- return [
124
- this.color("╭─") + titleStyle(label) + this.color("─".repeat(Math.max(0, remaining)) + "╮"),
125
- ];
126
- }
162
+ private color: (s: string) => string;
163
+ private title?: string;
164
+ private titleColor?: (s: string) => string;
165
+ constructor(color: (s: string) => string, title?: string, titleColor?: (s: string) => string) {
166
+ this.color = color;
167
+ this.title = title;
168
+ this.titleColor = titleColor;
169
+ }
170
+ invalidate(): void { }
171
+ render(width: number): string[] {
172
+ const inner = Math.max(0, width - 2);
173
+ if (!this.title || inner < this.title.length + 4) {
174
+ return [this.color(`╭${"─".repeat(inner)}╮`)];
175
+ }
176
+ const label = ` ${this.title} `;
177
+ const remaining = inner - 1 - label.length;
178
+ const titleStyle = this.titleColor ?? this.color;
179
+ return [
180
+ this.color("╭─") + titleStyle(label) + this.color("─".repeat(Math.max(0, remaining)) + "╮"),
181
+ ];
182
+ }
127
183
  }
128
184
 
129
185
  class BoxBorderBottom implements Component {
130
- private color: (s: string) => string;
131
- private label?: string;
132
- private labelColor?: (s: string) => string;
133
- constructor(color: (s: string) => string, label?: string, labelColor?: (s: string) => string) {
134
- this.color = color;
135
- this.label = label;
136
- this.labelColor = labelColor;
137
- }
138
- invalidate(): void {}
139
- render(width: number): string[] {
140
- const inner = Math.max(0, width - 2);
141
- if (!this.label || inner < this.label.length + 4) {
142
- return [this.color(`╰${"─".repeat(inner)}╯`)];
143
- }
144
- const tag = ` ${this.label} `;
145
- const leftDashes = inner - tag.length - 1;
146
- const style = this.labelColor ?? this.color;
147
- return [
148
- this.color("╰" + "─".repeat(Math.max(0, leftDashes))) + style(tag) + this.color("─╯"),
149
- ];
150
- }
186
+ private color: (s: string) => string;
187
+ private label?: string;
188
+ private labelColor?: (s: string) => string;
189
+ constructor(color: (s: string) => string, label?: string, labelColor?: (s: string) => string) {
190
+ this.color = color;
191
+ this.label = label;
192
+ this.labelColor = labelColor;
193
+ }
194
+ invalidate(): void { }
195
+ render(width: number): string[] {
196
+ const inner = Math.max(0, width - 2);
197
+ if (!this.label || inner < this.label.length + 4) {
198
+ return [this.color(`╰${"─".repeat(inner)}╯`)];
199
+ }
200
+ const tag = ` ${this.label} `;
201
+ const leftDashes = inner - tag.length - 1;
202
+ const style = this.labelColor ?? this.color;
203
+ return [
204
+ this.color("╰" + "─".repeat(Math.max(0, leftDashes))) + style(tag) + this.color("─╯"),
205
+ ];
206
+ }
151
207
  }
152
208
 
153
209
  function formatKeyList(keys: string[]): string {
154
- return keys.join("/");
210
+ return keys.join("/");
155
211
  }
156
212
 
157
213
  function keybindingHint(
158
- theme: Theme,
159
- keybindings: KeybindingsManager,
160
- keybinding: Keybinding,
161
- description: string,
214
+ theme: Theme,
215
+ keybindings: KeybindingsManager,
216
+ keybinding: Keybinding,
217
+ description: string,
162
218
  ): string {
163
- return `${theme.fg("dim", formatKeyList(keybindings.getKeys(keybinding)))}${theme.fg("muted", ` ${description}`)}`;
219
+ return `${theme.fg("dim", formatKeyList(keybindings.getKeys(keybinding)))}${theme.fg("muted", ` ${description}`)}`;
164
220
  }
165
221
 
166
222
  function literalHint(theme: Theme, key: string, description: string): string {
167
- return `${theme.fg("dim", key)}${theme.fg("muted", ` ${description}`)}`;
223
+ return `${theme.fg("dim", key)}${theme.fg("muted", ` ${description}`)}`;
168
224
  }
169
225
 
170
- type AskMode = "select" | "freeform";
226
+ function isCommentToggleKey(data: string): boolean {
227
+ return matchesKey(data, Key.ctrl("g"));
228
+ }
229
+
230
+ type AskMode = "select" | "freeform" | "comment";
171
231
 
172
232
  const ASK_OVERLAY_MAX_HEIGHT_RATIO = 0.85;
173
233
  const ASK_OVERLAY_WIDTH = "92%";
@@ -176,474 +236,572 @@ const SINGLE_SELECT_SPLIT_PANE_MIN_WIDTH = 84;
176
236
  const SINGLE_SELECT_SPLIT_PANE_LEFT_MIN_WIDTH = 32;
177
237
  const SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH = 28;
178
238
  const SINGLE_SELECT_SPLIT_PANE_SEPARATOR = " │ ";
239
+ const FREEFORM_SENTINEL = "\u270f\ufe0f Type custom response...";
240
+ const COMMENT_TOGGLE_LABEL = "Add extra context after selection";
179
241
 
180
242
  class MultiSelectList implements Component {
181
- private options: QuestionOption[];
182
- private allowFreeform: boolean;
183
- private theme: Theme;
184
- private keybindings: KeybindingsManager;
185
- private selectedIndex = 0;
186
- private checked = new Set<number>();
187
- private cachedWidth?: number;
188
- private cachedLines?: string[];
189
-
190
- public onCancel?: () => void;
191
- public onSubmit?: (result: string) => void;
192
- public onEnterFreeform?: () => void;
193
-
194
- constructor(options: QuestionOption[], allowFreeform: boolean, theme: Theme, keybindings: KeybindingsManager) {
195
- this.options = options;
196
- this.allowFreeform = allowFreeform;
197
- this.theme = theme;
198
- this.keybindings = keybindings;
199
- }
200
-
201
- invalidate(): void {
202
- this.cachedWidth = undefined;
203
- this.cachedLines = undefined;
204
- }
205
-
206
- private getItemCount(): number {
207
- return this.options.length + (this.allowFreeform ? 1 : 0);
208
- }
209
-
210
- private isFreeformRow(index: number): boolean {
211
- return this.allowFreeform && index === this.options.length;
212
- }
213
-
214
- private toggle(index: number): void {
215
- if (index < 0 || index >= this.options.length) return;
216
- if (this.checked.has(index)) this.checked.delete(index);
217
- else this.checked.add(index);
218
- }
219
-
220
- handleInput(data: string): void {
221
- if (this.keybindings.matches(data, "tui.select.cancel")) {
222
- this.onCancel?.();
223
- return;
224
- }
225
-
226
- const count = this.getItemCount();
227
- if (count === 0) {
228
- this.onCancel?.();
229
- return;
230
- }
231
-
232
- if (this.keybindings.matches(data, "tui.select.up") || matchesKey(data, Key.shift("tab"))) {
233
- this.selectedIndex = this.selectedIndex === 0 ? count - 1 : this.selectedIndex - 1;
234
- this.invalidate();
235
- return;
236
- }
237
-
238
- if (this.keybindings.matches(data, "tui.select.down") || matchesKey(data, Key.tab)) {
239
- this.selectedIndex = this.selectedIndex === count - 1 ? 0 : this.selectedIndex + 1;
240
- this.invalidate();
241
- return;
242
- }
243
-
244
- // Number keys (1-9) toggle checkboxes for normal items
245
- const numMatch = data.match(/^[1-9]$/);
246
- if (numMatch) {
247
- const idx = Number.parseInt(numMatch[0], 10) - 1;
248
- if (idx >= 0 && idx < this.options.length) {
249
- this.toggle(idx);
250
- this.selectedIndex = Math.min(idx, count - 1);
251
- this.invalidate();
252
- }
253
- return;
254
- }
255
-
256
- if (matchesKey(data, Key.space)) {
257
- if (this.isFreeformRow(this.selectedIndex)) {
258
- this.onEnterFreeform?.();
259
- return;
260
- }
261
- this.toggle(this.selectedIndex);
262
- this.invalidate();
263
- return;
264
- }
265
-
266
- if (this.keybindings.matches(data, "tui.select.confirm")) {
267
- if (this.isFreeformRow(this.selectedIndex)) {
268
- this.onEnterFreeform?.();
269
- return;
270
- }
271
-
272
- const selectedTitles = Array.from(this.checked)
273
- .sort((a, b) => a - b)
274
- .map((i) => this.options[i]?.title)
275
- .filter((t): t is string => !!t);
276
-
277
- // If nothing checked, fall back to current row
278
- const fallback = this.options[this.selectedIndex]?.title;
279
- const result = selectedTitles.length > 0 ? selectedTitles.join(", ") : fallback;
280
-
281
- if (result) this.onSubmit?.(result);
282
- else this.onCancel?.();
283
- }
284
- }
285
-
286
- render(width: number): string[] {
287
- if (this.cachedLines && this.cachedWidth === width) {
288
- return this.cachedLines;
289
- }
290
-
291
- const theme = this.theme;
292
- const count = this.getItemCount();
293
- const maxVisible = Math.min(count, 10);
294
-
295
- if (count === 0) {
296
- this.cachedLines = [theme.fg("warning", "No options")];
297
- this.cachedWidth = width;
298
- return this.cachedLines;
299
- }
300
-
301
- const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(maxVisible / 2), count - maxVisible));
302
- const endIndex = Math.min(startIndex + maxVisible, count);
303
-
304
- const lines: string[] = [];
305
-
306
- for (let i = startIndex; i < endIndex; i++) {
307
- const isSelected = i === this.selectedIndex;
308
- const prefix = isSelected ? theme.fg("accent", "→") : " ";
309
-
310
- if (this.isFreeformRow(i)) {
311
- const label = theme.fg("text", theme.bold("Type something."));
312
- const desc = theme.fg("muted", "Enter a custom response");
313
- const line = `${prefix} ${label} ${theme.fg("dim", "—")} ${desc}`;
314
- lines.push(truncateToWidth(line, width, ""));
315
- continue;
316
- }
317
-
318
- const option = this.options[i];
319
- if (!option) continue;
320
-
321
- const checkbox = this.checked.has(i) ? theme.fg("success", "[✓]") : theme.fg("dim", "[ ]");
322
- const num = theme.fg("dim", `${i + 1}.`);
323
- const title = isSelected
324
- ? theme.fg("accent", theme.bold(option.title))
325
- : theme.fg("text", theme.bold(option.title));
326
-
327
- const firstLine = `${prefix} ${num} ${checkbox} ${title}`;
328
- lines.push(truncateToWidth(firstLine, width, ""));
329
-
330
- if (option.description) {
331
- const indent = " ";
332
- const wrapWidth = Math.max(10, width - indent.length);
333
- const wrapped = wrapTextWithAnsi(option.description, wrapWidth);
334
- for (const w of wrapped) {
335
- lines.push(truncateToWidth(indent + theme.fg("muted", w), width, ""));
336
- }
337
- }
338
- }
339
-
340
- // Scroll indicator
341
- if (startIndex > 0 || endIndex < count) {
342
- lines.push(theme.fg("dim", truncateToWidth(` (${this.selectedIndex + 1}/${count})`, width, "")));
343
- }
344
-
345
- this.cachedWidth = width;
346
- this.cachedLines = lines;
347
- return lines;
348
- }
243
+ private options: QuestionOption[];
244
+ private allowFreeform: boolean;
245
+ private allowComment: boolean;
246
+ private theme: Theme;
247
+ private keybindings: KeybindingsManager;
248
+ private selectedIndex = 0;
249
+ private checked = new Set<number>();
250
+ private commentEnabled = false;
251
+ private cachedWidth?: number;
252
+ private cachedLines?: string[];
253
+
254
+ public onCancel?: () => void;
255
+ public onSubmit?: (result: string[]) => void;
256
+ public onEnterFreeform?: () => void;
257
+
258
+ constructor(
259
+ options: QuestionOption[],
260
+ allowFreeform: boolean,
261
+ allowComment: boolean,
262
+ theme: Theme,
263
+ keybindings: KeybindingsManager,
264
+ ) {
265
+ this.options = options;
266
+ this.allowFreeform = allowFreeform;
267
+ this.allowComment = allowComment;
268
+ this.theme = theme;
269
+ this.keybindings = keybindings;
270
+ }
271
+
272
+ public isCommentEnabled(): boolean {
273
+ return this.commentEnabled;
274
+ }
275
+
276
+ invalidate(): void {
277
+ this.cachedWidth = undefined;
278
+ this.cachedLines = undefined;
279
+ }
280
+
281
+ private getItemCount(): number {
282
+ return this.options.length + (this.allowComment ? 1 : 0) + (this.allowFreeform ? 1 : 0);
283
+ }
284
+
285
+ private getCommentToggleIndex(): number | null {
286
+ return this.allowComment ? this.options.length : null;
287
+ }
288
+
289
+ private getFreeformIndex(): number {
290
+ return this.options.length + (this.allowComment ? 1 : 0);
291
+ }
292
+
293
+ private isCommentToggleRow(index: number): boolean {
294
+ const toggleIndex = this.getCommentToggleIndex();
295
+ return toggleIndex !== null && index === toggleIndex;
296
+ }
297
+
298
+ private isFreeformRow(index: number): boolean {
299
+ return this.allowFreeform && index === this.getFreeformIndex();
300
+ }
301
+
302
+ private toggle(index: number): void {
303
+ if (index < 0 || index >= this.options.length) return;
304
+ if (this.checked.has(index)) this.checked.delete(index);
305
+ else this.checked.add(index);
306
+ }
307
+
308
+ private toggleComment(): void {
309
+ if (!this.allowComment) return;
310
+ this.commentEnabled = !this.commentEnabled;
311
+ this.invalidate();
312
+ }
313
+
314
+ handleInput(data: string): void {
315
+ if (this.keybindings.matches(data, "tui.select.cancel")) {
316
+ this.onCancel?.();
317
+ return;
318
+ }
319
+
320
+ const count = this.getItemCount();
321
+ if (count === 0) {
322
+ this.onCancel?.();
323
+ return;
324
+ }
325
+
326
+ if (this.allowComment && isCommentToggleKey(data)) {
327
+ this.toggleComment();
328
+ return;
329
+ }
330
+
331
+ if (this.keybindings.matches(data, "tui.select.up") || matchesKey(data, Key.shift("tab"))) {
332
+ this.selectedIndex = this.selectedIndex === 0 ? count - 1 : this.selectedIndex - 1;
333
+ this.invalidate();
334
+ return;
335
+ }
336
+
337
+ if (this.keybindings.matches(data, "tui.select.down") || matchesKey(data, Key.tab)) {
338
+ this.selectedIndex = this.selectedIndex === count - 1 ? 0 : this.selectedIndex + 1;
339
+ this.invalidate();
340
+ return;
341
+ }
342
+
343
+ const numMatch = data.match(/^[1-9]$/);
344
+ if (numMatch) {
345
+ const idx = Number.parseInt(numMatch[0], 10) - 1;
346
+ if (idx >= 0 && idx < this.options.length) {
347
+ this.toggle(idx);
348
+ this.selectedIndex = Math.min(idx, count - 1);
349
+ this.invalidate();
350
+ }
351
+ return;
352
+ }
353
+
354
+ if (matchesKey(data, Key.space)) {
355
+ if (this.isCommentToggleRow(this.selectedIndex)) {
356
+ this.toggleComment();
357
+ return;
358
+ }
359
+ if (this.isFreeformRow(this.selectedIndex)) {
360
+ this.onEnterFreeform?.();
361
+ return;
362
+ }
363
+ this.toggle(this.selectedIndex);
364
+ this.invalidate();
365
+ return;
366
+ }
367
+
368
+ if (this.keybindings.matches(data, "tui.select.confirm")) {
369
+ if (this.isCommentToggleRow(this.selectedIndex)) {
370
+ this.toggleComment();
371
+ return;
372
+ }
373
+ if (this.isFreeformRow(this.selectedIndex)) {
374
+ this.onEnterFreeform?.();
375
+ return;
376
+ }
377
+
378
+ const selectedTitles = Array.from(this.checked)
379
+ .sort((a, b) => a - b)
380
+ .map((i) => this.options[i]?.title)
381
+ .filter((t): t is string => !!t);
382
+
383
+ const fallback = this.options[this.selectedIndex]?.title;
384
+ const result = selectedTitles.length > 0 ? selectedTitles : fallback ? [fallback] : [];
385
+
386
+ if (result.length > 0) this.onSubmit?.(result);
387
+ else this.onCancel?.();
388
+ }
389
+ }
390
+
391
+ render(width: number): string[] {
392
+ if (this.cachedLines && this.cachedWidth === width) {
393
+ return this.cachedLines;
394
+ }
395
+
396
+ const theme = this.theme;
397
+ const count = this.getItemCount();
398
+ const maxVisible = Math.min(count, 10);
399
+
400
+ if (count === 0) {
401
+ this.cachedLines = [theme.fg("warning", "No options")];
402
+ this.cachedWidth = width;
403
+ return this.cachedLines;
404
+ }
405
+
406
+ const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(maxVisible / 2), count - maxVisible));
407
+ const endIndex = Math.min(startIndex + maxVisible, count);
408
+
409
+ const lines: string[] = [];
410
+
411
+ for (let i = startIndex; i < endIndex; i++) {
412
+ const isSelected = i === this.selectedIndex;
413
+ const prefix = isSelected ? theme.fg("accent", "→") : " ";
414
+
415
+ if (this.isCommentToggleRow(i)) {
416
+ const checkbox = this.commentEnabled ? theme.fg("success", "[✓]") : theme.fg("dim", "[ ]");
417
+ const label = isSelected
418
+ ? theme.fg("accent", theme.bold(COMMENT_TOGGLE_LABEL))
419
+ : theme.fg("text", theme.bold(COMMENT_TOGGLE_LABEL));
420
+ lines.push(truncateToWidth(`${prefix} ${checkbox} ${label}`, width, ""));
421
+ continue;
422
+ }
423
+
424
+ if (this.isFreeformRow(i)) {
425
+ const label = theme.fg("text", theme.bold("Type something."));
426
+ const desc = theme.fg("muted", "Enter a custom response");
427
+ const line = `${prefix} ${label} ${theme.fg("dim", "—")} ${desc}`;
428
+ lines.push(truncateToWidth(line, width, ""));
429
+ continue;
430
+ }
431
+
432
+ const option = this.options[i];
433
+ if (!option) continue;
434
+
435
+ const checkbox = this.checked.has(i) ? theme.fg("success", "[✓]") : theme.fg("dim", "[ ]");
436
+ const num = theme.fg("dim", `${i + 1}.`);
437
+ const title = isSelected
438
+ ? theme.fg("accent", theme.bold(option.title))
439
+ : theme.fg("text", theme.bold(option.title));
440
+
441
+ const firstLine = `${prefix} ${num} ${checkbox} ${title}`;
442
+ lines.push(truncateToWidth(firstLine, width, ""));
443
+
444
+ if (option.description) {
445
+ const indent = " ";
446
+ const wrapWidth = Math.max(10, width - indent.length);
447
+ const wrapped = wrapTextWithAnsi(option.description, wrapWidth);
448
+ for (const w of wrapped) {
449
+ lines.push(truncateToWidth(indent + theme.fg("muted", w), width, ""));
450
+ }
451
+ }
452
+ }
453
+
454
+ if (startIndex > 0 || endIndex < count) {
455
+ lines.push(theme.fg("dim", truncateToWidth(` (${this.selectedIndex + 1}/${count})`, width, "")));
456
+ }
457
+
458
+ this.cachedWidth = width;
459
+ this.cachedLines = lines;
460
+ return lines;
461
+ }
349
462
  }
350
463
 
351
464
  class WrappedSingleSelectList implements Component {
352
- private options: QuestionOption[];
353
- private allowFreeform: boolean;
354
- private theme: Theme;
355
- private keybindings: KeybindingsManager;
356
- private selectedIndex = 0;
357
- private searchQuery = "";
358
- private maxVisibleRows = 12;
359
- private cachedWidth?: number;
360
- private cachedLines?: string[];
361
-
362
- public onCancel?: () => void;
363
- public onSubmit?: (result: string) => void;
364
- public onEnterFreeform?: () => void;
365
-
366
- constructor(options: QuestionOption[], allowFreeform: boolean, theme: Theme, keybindings: KeybindingsManager) {
367
- this.options = options;
368
- this.allowFreeform = allowFreeform;
369
- this.theme = theme;
370
- this.keybindings = keybindings;
371
- }
372
-
373
- setMaxVisibleRows(rows: number): void {
374
- const next = Math.max(1, Math.floor(rows));
375
- if (next !== this.maxVisibleRows) {
376
- this.maxVisibleRows = next;
377
- this.invalidate();
378
- }
379
- }
380
-
381
- invalidate(): void {
382
- this.cachedWidth = undefined;
383
- this.cachedLines = undefined;
384
- }
385
-
386
- private getFilteredOptions(): QuestionOption[] {
387
- return fuzzyFilter(this.options, this.searchQuery, (option) => `${option.title} ${option.description ?? ""}`);
388
- }
389
-
390
- private getItemCount(filteredOptions: QuestionOption[]): number {
391
- return filteredOptions.length + (this.allowFreeform ? 1 : 0);
392
- }
393
-
394
- private isFreeformRow(index: number, filteredOptions: QuestionOption[]): boolean {
395
- return this.allowFreeform && index === filteredOptions.length;
396
- }
397
-
398
- private setSearchQuery(query: string): void {
399
- this.searchQuery = query;
400
- this.selectedIndex = 0;
401
- this.invalidate();
402
- }
403
-
404
- private popSearchCharacter(): void {
405
- if (!this.searchQuery) return;
406
- const characters = [...this.searchQuery];
407
- characters.pop();
408
- this.setSearchQuery(characters.join(""));
409
- }
410
-
411
- private getPrintableInput(data: string): string | null {
412
- const kittyPrintable = decodeKittyPrintable(data);
413
- if (kittyPrintable !== undefined) return kittyPrintable;
414
-
415
- const characters = [...data];
416
- if (characters.length !== 1) return null;
417
-
418
- const [character] = characters;
419
- if (!character) return null;
420
-
421
- const code = character.charCodeAt(0);
422
- if (code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f)) {
423
- return null;
424
- }
425
-
426
- return character;
427
- }
428
-
429
- private styleListLine(line: string, width: number): string {
430
- const trimmed = line.trim();
431
-
432
- if (trimmed.startsWith("(")) {
433
- return truncateToWidth(this.theme.fg("dim", line), width, "");
434
- }
435
-
436
- if (line.startsWith(" ")) {
437
- return truncateToWidth(this.theme.fg("muted", line), width, "");
438
- }
439
-
440
- if (line.startsWith("→")) {
441
- return truncateToWidth(this.theme.fg("accent", this.theme.bold(line)), width, "");
442
- }
443
-
444
- return truncateToWidth(this.theme.fg("text", line), width, "");
445
- }
446
-
447
- private getSplitPaneWidths(width: number): { left: number; right: number } | null {
448
- if (width < SINGLE_SELECT_SPLIT_PANE_MIN_WIDTH) return null;
449
-
450
- const availableWidth = width - SINGLE_SELECT_SPLIT_PANE_SEPARATOR.length;
451
- if (availableWidth < SINGLE_SELECT_SPLIT_PANE_LEFT_MIN_WIDTH + SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH) {
452
- return null;
453
- }
454
-
455
- const preferredLeftWidth = Math.floor(availableWidth * 0.42);
456
- const left = Math.max(
457
- SINGLE_SELECT_SPLIT_PANE_LEFT_MIN_WIDTH,
458
- Math.min(preferredLeftWidth, availableWidth - SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH),
459
- );
460
- const right = availableWidth - left;
461
-
462
- if (right < SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH) return null;
463
- return { left, right };
464
- }
465
-
466
- private buildListLines(width: number, filteredOptions: QuestionOption[], hideDescriptions = false): string[] {
467
- const lines: string[] = [];
468
- const count = this.getItemCount(filteredOptions);
469
- const searchValue = this.searchQuery ? this.theme.fg("text", this.searchQuery) : this.theme.fg("dim", "type to filter");
470
- lines.push(truncateToWidth(`${this.theme.fg("accent", "Filter:")} ${searchValue}`, width, ""));
471
-
472
- if (this.searchQuery && filteredOptions.length === 0) {
473
- lines.push(truncateToWidth(this.theme.fg("warning", "No matching options"), width, ""));
474
- }
475
-
476
- if (count === 0) {
477
- if (!this.searchQuery) {
478
- lines.push(truncateToWidth(this.theme.fg("warning", "No options"), width, ""));
479
- }
480
- return lines.slice(0, this.maxVisibleRows);
481
- }
482
-
483
- const maxRows = Math.max(1, this.maxVisibleRows - lines.length);
484
- const optionLines = renderSingleSelectRows({
485
- options: filteredOptions,
486
- selectedIndex: this.selectedIndex,
487
- width,
488
- allowFreeform: this.allowFreeform,
489
- maxRows,
490
- hideDescriptions,
491
- }).map((line) => this.styleListLine(line, width));
492
-
493
- lines.push(...optionLines);
494
- return lines.slice(0, this.maxVisibleRows);
495
- }
496
-
497
- private buildPreviewLines(width: number, filteredOptions: QuestionOption[], maxLines: number): string[] {
498
- if (maxLines <= 0) return [];
499
-
500
- let mdTheme: MarkdownTheme | undefined;
501
- try {
502
- mdTheme = getMarkdownTheme();
503
- } catch {}
504
-
505
- // Build a markdown string for the preview content
506
- let md = "";
507
-
508
- if (this.isFreeformRow(this.selectedIndex, filteredOptions)) {
509
- md += "## Custom response\n\n";
510
- md += "Open the editor to write **any** answer.\n\n";
511
- md += "*Use this when none of the listed options fit.*\n";
512
- if (this.searchQuery) {
513
- md += `\n> Current filter: \`${this.searchQuery}\`\n`;
514
- }
515
- } else {
516
- const selected = filteredOptions[this.selectedIndex];
517
- if (!selected) {
518
- md += "*No option selected*\n";
519
- } else {
520
- md += `## ${selected.title}\n\n`;
521
- if (selected.description?.trim()) {
522
- md += `${selected.description}\n`;
523
- } else {
524
- md += "*No additional details provided for this option.*\n";
525
- }
526
- md += `\n---\n\nPress \`Enter\` to select this option.\n`;
527
- if (this.searchQuery) {
528
- md += `\n> Filter: \`${this.searchQuery}\`\n`;
529
- }
530
- }
531
- }
532
-
533
- // Render via Markdown component if theme is available, otherwise fall back to plain text
534
- let lines: string[];
535
- if (mdTheme) {
536
- const mdComponent = new Markdown(md.trim(), 0, 0, mdTheme);
537
- lines = mdComponent.render(width);
538
- } else {
539
- lines = [];
540
- for (const line of wrapTextWithAnsi(md.trim(), Math.max(10, width))) {
541
- lines.push(truncateToWidth(line, width, ""));
542
- }
543
- }
544
-
545
- // Trim trailing blanks
546
- while (lines.length > 0 && lines[lines.length - 1]?.trim() === "") {
547
- lines.pop();
548
- }
549
-
550
- if (lines.length <= maxLines) return lines;
551
- if (maxLines === 1) return [truncateToWidth(this.theme.fg("dim", "…"), width, "")];
552
-
553
- const visibleLines = lines.slice(0, maxLines - 1);
554
- visibleLines.push(truncateToWidth(this.theme.fg("dim", "…"), width, ""));
555
- return visibleLines;
556
- }
557
-
558
- handleInput(data: string): void {
559
- if (this.searchQuery && matchesKey(data, Key.escape)) {
560
- this.setSearchQuery("");
561
- return;
562
- }
563
-
564
- if (this.keybindings.matches(data, "tui.select.cancel")) {
565
- this.onCancel?.();
566
- return;
567
- }
568
-
569
- const filteredOptions = this.getFilteredOptions();
570
- const count = this.getItemCount(filteredOptions);
571
-
572
- if ((this.keybindings.matches(data, "tui.select.up") || matchesKey(data, Key.shift("tab"))) && count > 0) {
573
- this.selectedIndex = this.selectedIndex === 0 ? count - 1 : this.selectedIndex - 1;
574
- this.invalidate();
575
- return;
576
- }
577
-
578
- if ((this.keybindings.matches(data, "tui.select.down") || matchesKey(data, Key.tab)) && count > 0) {
579
- this.selectedIndex = this.selectedIndex === count - 1 ? 0 : this.selectedIndex + 1;
580
- this.invalidate();
581
- return;
582
- }
583
-
584
- const numMatch = data.match(/^[1-9]$/);
585
- if (numMatch && count > 0) {
586
- const idx = Number.parseInt(numMatch[0], 10) - 1;
587
- if (idx >= 0 && idx < count) {
588
- this.selectedIndex = idx;
589
- this.invalidate();
590
- return;
591
- }
592
- }
593
-
594
- if (this.keybindings.matches(data, "tui.select.confirm") && count > 0) {
595
- if (this.isFreeformRow(this.selectedIndex, filteredOptions)) {
596
- this.onEnterFreeform?.();
597
- return;
598
- }
599
-
600
- const result = filteredOptions[this.selectedIndex]?.title;
601
- if (result) this.onSubmit?.(result);
602
- else this.onCancel?.();
603
- return;
604
- }
605
-
606
- if (this.keybindings.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, Key.backspace)) {
607
- this.popSearchCharacter();
608
- return;
609
- }
610
-
611
- const printableInput = this.getPrintableInput(data);
612
- if (printableInput) {
613
- this.setSearchQuery(this.searchQuery + printableInput);
614
- }
615
- }
616
-
617
- render(width: number): string[] {
618
- if (this.cachedLines && this.cachedWidth === width) {
619
- return this.cachedLines;
620
- }
621
-
622
- const filteredOptions = this.getFilteredOptions();
623
- const count = this.getItemCount(filteredOptions);
624
- this.selectedIndex = count > 0 ? Math.max(0, Math.min(this.selectedIndex, count - 1)) : 0;
625
-
626
- const splitPane = this.getSplitPaneWidths(width);
627
- let lines: string[];
628
-
629
- if (!splitPane) {
630
- lines = this.buildListLines(width, filteredOptions);
631
- } else {
632
- const listLines = this.buildListLines(splitPane.left, filteredOptions, true);
633
- const previewLines = this.buildPreviewLines(splitPane.right, filteredOptions, this.maxVisibleRows);
634
- const rowCount = Math.min(this.maxVisibleRows, Math.max(listLines.length, previewLines.length));
635
- const separator = this.theme.fg("dim", SINGLE_SELECT_SPLIT_PANE_SEPARATOR);
636
- lines = Array.from({ length: rowCount }, (_, index) => {
637
- const left = truncateToWidth(listLines[index] ?? "", splitPane.left, "", true);
638
- const right = truncateToWidth(previewLines[index] ?? "", splitPane.right, "");
639
- return `${left}${separator}${right}`;
640
- });
641
- }
642
-
643
- this.cachedWidth = width;
644
- this.cachedLines = lines;
645
- return lines;
646
- }
465
+ private options: QuestionOption[];
466
+ private allowFreeform: boolean;
467
+ private allowComment: boolean;
468
+ private theme: Theme;
469
+ private keybindings: KeybindingsManager;
470
+ private selectedIndex = 0;
471
+ private searchQuery = "";
472
+ private commentEnabled = false;
473
+ private maxVisibleRows = 12;
474
+ private cachedWidth?: number;
475
+ private cachedLines?: string[];
476
+
477
+ public onCancel?: () => void;
478
+ public onSubmit?: (result: string) => void;
479
+ public onEnterFreeform?: () => void;
480
+
481
+ constructor(
482
+ options: QuestionOption[],
483
+ allowFreeform: boolean,
484
+ allowComment: boolean,
485
+ theme: Theme,
486
+ keybindings: KeybindingsManager,
487
+ ) {
488
+ this.options = options;
489
+ this.allowFreeform = allowFreeform;
490
+ this.allowComment = allowComment;
491
+ this.theme = theme;
492
+ this.keybindings = keybindings;
493
+ }
494
+
495
+ public isCommentEnabled(): boolean {
496
+ return this.commentEnabled;
497
+ }
498
+
499
+ setMaxVisibleRows(rows: number): void {
500
+ const next = Math.max(1, Math.floor(rows));
501
+ if (next !== this.maxVisibleRows) {
502
+ this.maxVisibleRows = next;
503
+ this.invalidate();
504
+ }
505
+ }
506
+
507
+ invalidate(): void {
508
+ this.cachedWidth = undefined;
509
+ this.cachedLines = undefined;
510
+ }
511
+
512
+ private getFilteredOptions(): QuestionOption[] {
513
+ return fuzzyFilter(this.options, this.searchQuery, (option) => `${option.title} ${option.description ?? ""}`);
514
+ }
515
+
516
+ private getItemCount(filteredOptions: QuestionOption[]): number {
517
+ return filteredOptions.length + (this.allowComment ? 1 : 0) + (this.allowFreeform ? 1 : 0);
518
+ }
519
+
520
+ private isCommentToggleRow(index: number, filteredOptions: QuestionOption[]): boolean {
521
+ return this.allowComment && index === filteredOptions.length;
522
+ }
523
+
524
+ private isFreeformRow(index: number, filteredOptions: QuestionOption[]): boolean {
525
+ return this.allowFreeform && index === filteredOptions.length + (this.allowComment ? 1 : 0);
526
+ }
527
+
528
+ private toggleComment(): void {
529
+ if (!this.allowComment) return;
530
+ this.commentEnabled = !this.commentEnabled;
531
+ this.invalidate();
532
+ }
533
+
534
+ private setSearchQuery(query: string): void {
535
+ this.searchQuery = query;
536
+ this.selectedIndex = 0;
537
+ this.invalidate();
538
+ }
539
+
540
+ private popSearchCharacter(): void {
541
+ if (!this.searchQuery) return;
542
+ const characters = [...this.searchQuery];
543
+ characters.pop();
544
+ this.setSearchQuery(characters.join(""));
545
+ }
546
+
547
+ private getPrintableInput(data: string): string | null {
548
+ const kittyPrintable = decodeKittyPrintable(data);
549
+ if (kittyPrintable !== undefined) return kittyPrintable;
550
+
551
+ const characters = [...data];
552
+ if (characters.length !== 1) return null;
553
+
554
+ const [character] = characters;
555
+ if (!character) return null;
556
+
557
+ const code = character.charCodeAt(0);
558
+ if (code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f)) {
559
+ return null;
560
+ }
561
+
562
+ return character;
563
+ }
564
+
565
+ private styleListLine(line: string, width: number, isSelected: boolean): string {
566
+ const trimmed = line.trim();
567
+
568
+ if (trimmed.startsWith("(")) {
569
+ return truncateToWidth(this.theme.fg("dim", line), width, "");
570
+ }
571
+
572
+ if (isSelected) {
573
+ return truncateToWidth(this.theme.fg("accent", this.theme.bold(line)), width, "");
574
+ }
575
+
576
+ if (line.startsWith(" ")) {
577
+ return truncateToWidth(this.theme.fg("muted", line), width, "");
578
+ }
579
+
580
+ if (line.startsWith("→")) {
581
+ return truncateToWidth(this.theme.fg("accent", this.theme.bold(line)), width, "");
582
+ }
583
+
584
+ return truncateToWidth(this.theme.fg("text", line), width, "");
585
+ }
586
+
587
+ private getSplitPaneWidths(width: number): { left: number; right: number } | null {
588
+ if (width < SINGLE_SELECT_SPLIT_PANE_MIN_WIDTH) return null;
589
+
590
+ const availableWidth = width - SINGLE_SELECT_SPLIT_PANE_SEPARATOR.length;
591
+ if (availableWidth < SINGLE_SELECT_SPLIT_PANE_LEFT_MIN_WIDTH + SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH) {
592
+ return null;
593
+ }
594
+
595
+ const preferredLeftWidth = Math.floor(availableWidth * 0.42);
596
+ const left = Math.max(
597
+ SINGLE_SELECT_SPLIT_PANE_LEFT_MIN_WIDTH,
598
+ Math.min(preferredLeftWidth, availableWidth - SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH),
599
+ );
600
+ const right = availableWidth - left;
601
+
602
+ if (right < SINGLE_SELECT_SPLIT_PANE_RIGHT_MIN_WIDTH) return null;
603
+ return { left, right };
604
+ }
605
+
606
+ private buildListLines(width: number, filteredOptions: QuestionOption[], hideDescriptions = false): string[] {
607
+ const lines: string[] = [];
608
+ const count = this.getItemCount(filteredOptions);
609
+ const searchValue = this.searchQuery ? this.theme.fg("text", this.searchQuery) : this.theme.fg("dim", "type to filter");
610
+ lines.push(truncateToWidth(`${this.theme.fg("accent", "Filter:")} ${searchValue}`, width, ""));
611
+
612
+ if (this.searchQuery && filteredOptions.length === 0) {
613
+ lines.push(truncateToWidth(this.theme.fg("warning", "No matching options"), width, ""));
614
+ }
615
+
616
+ if (count === 0) {
617
+ if (!this.searchQuery) {
618
+ lines.push(truncateToWidth(this.theme.fg("warning", "No options"), width, ""));
619
+ }
620
+ return lines.slice(0, this.maxVisibleRows);
621
+ }
622
+
623
+ const maxRows = Math.max(1, this.maxVisibleRows - lines.length);
624
+ const optionRows = renderSingleSelectRows({
625
+ options: filteredOptions,
626
+ selectedIndex: this.selectedIndex,
627
+ width,
628
+ allowFreeform: this.allowFreeform,
629
+ allowComment: this.allowComment,
630
+ commentEnabled: this.commentEnabled,
631
+ maxRows,
632
+ hideDescriptions,
633
+ });
634
+ const optionLines = optionRows.map((row) => this.styleListLine(row.line, width, row.selected));
635
+
636
+ lines.push(...optionLines);
637
+ return lines.slice(0, this.maxVisibleRows);
638
+ }
639
+
640
+ private buildPreviewLines(width: number, filteredOptions: QuestionOption[], maxLines: number): string[] {
641
+ if (maxLines <= 0) return [];
642
+
643
+ let mdTheme: MarkdownTheme | undefined;
644
+ try {
645
+ mdTheme = getMarkdownTheme();
646
+ } catch { }
647
+
648
+ let md = "";
649
+
650
+ if (this.isCommentToggleRow(this.selectedIndex, filteredOptions)) {
651
+ md += "## Additional context\n\n";
652
+ md += `Currently: **${this.commentEnabled ? "Enabled" : "Disabled"}**\n\n`;
653
+ md += "Turn this on when the selected option needs extra explanation before the tool submits.\n";
654
+ } else if (this.isFreeformRow(this.selectedIndex, filteredOptions)) {
655
+ md += "## Custom response\n\n";
656
+ md += "Open the editor to write **any** answer.\n\n";
657
+ md += "*Use this when none of the listed options fit.*\n";
658
+ if (this.searchQuery) {
659
+ md += `\n> Current filter: \`${this.searchQuery}\`\n`;
660
+ }
661
+ } else {
662
+ const selected = filteredOptions[this.selectedIndex];
663
+ if (!selected) {
664
+ md += "*No option selected*\n";
665
+ } else {
666
+ md += `## ${selected.title}\n\n`;
667
+ if (selected.description?.trim()) {
668
+ md += `${selected.description}\n`;
669
+ } else {
670
+ md += "*No additional details provided for this option.*\n";
671
+ }
672
+ md += `\n---\n\nPress \`Enter\` to select this option.\n`;
673
+ if (this.searchQuery) {
674
+ md += `\n> Filter: \`${this.searchQuery}\`\n`;
675
+ }
676
+ }
677
+ }
678
+
679
+ let lines: string[];
680
+ if (mdTheme) {
681
+ const mdComponent = new Markdown(md.trim(), 0, 0, mdTheme);
682
+ lines = mdComponent.render(width);
683
+ } else {
684
+ lines = [];
685
+ for (const line of wrapTextWithAnsi(md.trim(), Math.max(10, width))) {
686
+ lines.push(truncateToWidth(line, width, ""));
687
+ }
688
+ }
689
+
690
+ while (lines.length > 0 && lines[lines.length - 1]?.trim() === "") {
691
+ lines.pop();
692
+ }
693
+
694
+ if (lines.length <= maxLines) return lines;
695
+ if (maxLines === 1) return [truncateToWidth(this.theme.fg("dim", "…"), width, "")];
696
+
697
+ const visibleLines = lines.slice(0, maxLines - 1);
698
+ visibleLines.push(truncateToWidth(this.theme.fg("dim", "…"), width, ""));
699
+ return visibleLines;
700
+ }
701
+
702
+ handleInput(data: string): void {
703
+ if (this.searchQuery && matchesKey(data, Key.escape)) {
704
+ this.setSearchQuery("");
705
+ return;
706
+ }
707
+
708
+ if (this.keybindings.matches(data, "tui.select.cancel")) {
709
+ this.onCancel?.();
710
+ return;
711
+ }
712
+
713
+ if (this.allowComment && isCommentToggleKey(data)) {
714
+ this.toggleComment();
715
+ return;
716
+ }
717
+
718
+ const filteredOptions = this.getFilteredOptions();
719
+ const count = this.getItemCount(filteredOptions);
720
+
721
+ if ((this.keybindings.matches(data, "tui.select.up") || matchesKey(data, Key.shift("tab"))) && count > 0) {
722
+ this.selectedIndex = this.selectedIndex === 0 ? count - 1 : this.selectedIndex - 1;
723
+ this.invalidate();
724
+ return;
725
+ }
726
+
727
+ if ((this.keybindings.matches(data, "tui.select.down") || matchesKey(data, Key.tab)) && count > 0) {
728
+ this.selectedIndex = this.selectedIndex === count - 1 ? 0 : this.selectedIndex + 1;
729
+ this.invalidate();
730
+ return;
731
+ }
732
+
733
+ const numMatch = data.match(/^[1-9]$/);
734
+ if (numMatch && filteredOptions.length > 0) {
735
+ const idx = Number.parseInt(numMatch[0], 10) - 1;
736
+ if (idx >= 0 && idx < filteredOptions.length) {
737
+ this.selectedIndex = idx;
738
+ this.invalidate();
739
+ return;
740
+ }
741
+ }
742
+
743
+ if (matchesKey(data, Key.space) && count > 0 && this.isCommentToggleRow(this.selectedIndex, filteredOptions)) {
744
+ this.toggleComment();
745
+ return;
746
+ }
747
+
748
+ if (this.keybindings.matches(data, "tui.select.confirm") && count > 0) {
749
+ if (this.isCommentToggleRow(this.selectedIndex, filteredOptions)) {
750
+ this.toggleComment();
751
+ return;
752
+ }
753
+ if (this.isFreeformRow(this.selectedIndex, filteredOptions)) {
754
+ this.onEnterFreeform?.();
755
+ return;
756
+ }
757
+
758
+ const result = filteredOptions[this.selectedIndex]?.title;
759
+ if (result) this.onSubmit?.(result);
760
+ else this.onCancel?.();
761
+ return;
762
+ }
763
+
764
+ if (this.keybindings.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, Key.backspace)) {
765
+ this.popSearchCharacter();
766
+ return;
767
+ }
768
+
769
+ const printableInput = this.getPrintableInput(data);
770
+ if (printableInput) {
771
+ this.setSearchQuery(this.searchQuery + printableInput);
772
+ }
773
+ }
774
+
775
+ render(width: number): string[] {
776
+ if (this.cachedLines && this.cachedWidth === width) {
777
+ return this.cachedLines;
778
+ }
779
+
780
+ const filteredOptions = this.getFilteredOptions();
781
+ const count = this.getItemCount(filteredOptions);
782
+ this.selectedIndex = count > 0 ? Math.max(0, Math.min(this.selectedIndex, count - 1)) : 0;
783
+
784
+ const splitPane = this.getSplitPaneWidths(width);
785
+ let lines: string[];
786
+
787
+ if (!splitPane) {
788
+ lines = this.buildListLines(width, filteredOptions);
789
+ } else {
790
+ const listLines = this.buildListLines(splitPane.left, filteredOptions, true);
791
+ const previewLines = this.buildPreviewLines(splitPane.right, filteredOptions, this.maxVisibleRows);
792
+ const rowCount = Math.min(this.maxVisibleRows, Math.max(listLines.length, previewLines.length));
793
+ const separator = this.theme.fg("dim", SINGLE_SELECT_SPLIT_PANE_SEPARATOR);
794
+ lines = Array.from({ length: rowCount }, (_, index) => {
795
+ const left = truncateToWidth(listLines[index] ?? "", splitPane.left, "", true);
796
+ const right = truncateToWidth(previewLines[index] ?? "", splitPane.right, "");
797
+ return `${left}${separator}${right}`;
798
+ });
799
+ }
800
+
801
+ this.cachedWidth = width;
802
+ this.cachedLines = lines;
803
+ return lines;
804
+ }
647
805
  }
648
806
 
649
807
  /**
@@ -651,566 +809,739 @@ class WrappedSingleSelectList implements Component {
651
809
  * component between SelectList/MultiSelectList and an Editor (freeform mode).
652
810
  */
653
811
  class AskComponent extends Container {
654
- private question: string;
655
- private context?: string;
656
- private options: QuestionOption[];
657
- private allowMultiple: boolean;
658
- private allowFreeform: boolean;
659
- private tui: TUI;
660
- private theme: Theme;
661
- private keybindings: KeybindingsManager;
662
- private onDone: (result: AskUIResult | null) => void;
663
-
664
- private mode: AskMode = "select";
665
-
666
- // Static layout components
667
- private titleText: Text;
668
- private questionText: Text;
669
- private contextComponent?: Component;
670
- private modeContainer: Container;
671
- private helpText: Text;
672
-
673
- // Mode components
674
- private singleSelectList?: WrappedSingleSelectList;
675
- private multiSelectList?: MultiSelectList;
676
- private editor?: Editor;
677
-
678
- // Focusable - propagate to Editor for IME cursor positioning
679
- private _focused = false;
680
- get focused(): boolean {
681
- return this._focused;
682
- }
683
- set focused(value: boolean) {
684
- this._focused = value;
685
- if (this.editor && this.mode === "freeform") {
686
- (this.editor as any).focused = value;
687
- }
688
- }
689
-
690
- constructor(
691
- question: string,
692
- context: string | undefined,
693
- options: QuestionOption[],
694
- allowMultiple: boolean,
695
- allowFreeform: boolean,
696
- tui: TUI,
697
- theme: Theme,
698
- keybindings: KeybindingsManager,
699
- onDone: (result: AskUIResult | null) => void,
700
- ) {
701
- super();
702
-
703
- this.question = question;
704
- this.context = context;
705
- this.options = options;
706
- this.allowMultiple = allowMultiple;
707
- this.allowFreeform = allowFreeform;
708
- this.tui = tui;
709
- this.theme = theme;
710
- this.keybindings = keybindings;
711
- this.onDone = onDone;
712
-
713
- // Layout skeleton
714
- this.addChild(new BoxBorderTop(
715
- (s: string) => theme.fg("accent", s),
716
- "ask_user",
717
- (s: string) => theme.fg("dim", theme.bold(s)),
718
- ));
719
- this.addChild(new Spacer(1));
720
-
721
- this.titleText = new Text("", 1, 0);
722
- this.addChild(this.titleText);
723
- this.addChild(new Spacer(1));
724
-
725
- this.questionText = new Text("", 1, 0);
726
- this.addChild(this.questionText);
727
-
728
- if (this.context) {
729
- this.addChild(new Spacer(1));
730
- let mdTheme: MarkdownTheme | undefined;
731
- try {
732
- mdTheme = getMarkdownTheme();
733
- } catch {}
734
- if (mdTheme) {
735
- this.contextComponent = new Markdown("", 1, 0, mdTheme);
736
- } else {
737
- this.contextComponent = new Text("", 1, 0);
738
- }
739
- this.addChild(this.contextComponent);
740
- }
741
-
742
- this.addChild(new Spacer(1));
743
-
744
- this.modeContainer = new Container();
745
- this.addChild(this.modeContainer);
746
-
747
- this.addChild(new Spacer(1));
748
- this.helpText = new Text("", 1, 0);
749
- this.addChild(this.helpText);
750
-
751
- this.addChild(new Spacer(1));
752
- this.addChild(new BoxBorderBottom(
753
- (s: string) => theme.fg("accent", s),
754
- `v${ASK_USER_VERSION}`,
755
- (s: string) => theme.fg("dim", s),
756
- ));
757
-
758
- this.updateStaticText();
759
- this.showSelectMode();
760
- }
761
-
762
- override invalidate(): void {
763
- super.invalidate();
764
- this.updateStaticText();
765
- this.updateHelpText();
766
- }
767
-
768
- override render(width: number): string[] {
769
- const innerWidth = Math.max(1, width - BOX_BORDER_OVERHEAD);
770
-
771
- if (this.mode === "select" && !this.allowMultiple) {
772
- const overlayMaxHeight = Math.max(12, Math.floor(this.tui.terminal.rows * ASK_OVERLAY_MAX_HEIGHT_RATIO));
773
- const staticLines = this.countStaticLines(innerWidth);
774
- const availableOptionRows = Math.max(4, overlayMaxHeight - staticLines);
775
- this.ensureSingleSelectList().setMaxVisibleRows(availableOptionRows);
776
- }
777
-
778
- // Render children at the inner width (excluding side border characters)
779
- const rawLines = super.render(innerWidth);
780
-
781
- // First and last lines are the top/bottom box borders — pass through at full width.
782
- // All inner lines get wrapped with side borders.
783
- const borderColor = (s: string) => this.theme.fg("accent", s);
784
- const titleColor = (s: string) => this.theme.fg("dim", this.theme.bold(s));
785
- return rawLines.map((line, index) => {
786
- if (index === 0 || index === rawLines.length - 1) {
787
- // Box top/bottom borders already rendered at innerWidth — re-render at full width
788
- if (index === 0) return new BoxBorderTop(borderColor, "ask_user", titleColor).render(width)[0];
789
- return new BoxBorderBottom(borderColor, `v${ASK_USER_VERSION}`, (s: string) => this.theme.fg("dim", s)).render(width)[0];
790
- }
791
- const padded = truncateToWidth(line, innerWidth, "", true);
792
- return `${borderColor(BOX_BORDER_LEFT)}${padded}${borderColor(BOX_BORDER_RIGHT)}`;
793
- });
794
- }
795
-
796
- private countWrappedLines(text: string, width: number): number {
797
- return Math.max(1, wrapTextWithAnsi(text, Math.max(10, width - 2)).length);
798
- }
799
-
800
- private countStaticLines(width: number): number {
801
- const titleLines = 1;
802
- const questionLines = this.countWrappedLines(this.question, width);
803
- const contextLines = this.context ? 1 + this.countWrappedLines(this.context, width) : 0;
804
- const helpLines = 1;
805
- const borderLines = 2;
806
- const spacerLines = this.context ? 6 : 5;
807
- return borderLines + spacerLines + titleLines + questionLines + contextLines + helpLines;
808
- }
809
-
810
- private updateStaticText(): void {
811
- const theme = this.theme;
812
- this.titleText.setText(theme.fg("accent", theme.bold("Question")));
813
- this.questionText.setText(theme.fg("text", theme.bold(this.question)));
814
- if (this.contextComponent && this.context) {
815
- if (this.contextComponent instanceof Markdown) {
816
- (this.contextComponent as Markdown).setText(
817
- `**Context:**\n${this.context}`,
818
- );
819
- } else {
820
- (this.contextComponent as Text).setText(
821
- `${theme.fg("accent", theme.bold("Context:"))}\n${theme.fg("dim", this.context)}`,
822
- );
823
- }
824
- }
825
- }
826
-
827
- private updateHelpText(): void {
828
- const theme = this.theme;
829
- if (this.mode === "freeform") {
830
- const alternateCancelKeys = this.keybindings
831
- .getKeys("tui.select.cancel")
832
- .filter((key) => key !== "escape" && key !== "esc");
833
- const hints = [
834
- keybindingHint(theme, this.keybindings, "tui.input.submit", "submit"),
835
- keybindingHint(theme, this.keybindings, "tui.input.newLine", "newline"),
836
- literalHint(theme, "esc", "back"),
837
- alternateCancelKeys.length > 0 ? literalHint(theme, formatKeyList(alternateCancelKeys), "cancel") : null,
838
- ]
839
- .filter((hint): hint is string => !!hint)
840
- .join(" ");
841
- this.helpText.setText(theme.fg("dim", hints));
842
- return;
843
- }
844
-
845
- if (this.allowMultiple) {
846
- const hints = [
847
- literalHint(theme, "↑↓", "navigate"),
848
- literalHint(theme, "space", "toggle"),
849
- keybindingHint(theme, this.keybindings, "tui.select.confirm", "submit"),
850
- keybindingHint(theme, this.keybindings, "tui.select.cancel", "cancel"),
851
- ].join(" • ");
852
- this.helpText.setText(theme.fg("dim", hints));
853
- } else {
854
- const alternateCancelKeys = this.keybindings
855
- .getKeys("tui.select.cancel")
856
- .filter((key) => key !== "escape" && key !== "esc");
857
- const hints = [
858
- literalHint(theme, "type", "filter"),
859
- keybindingHint(theme, this.keybindings, "tui.editor.deleteCharBackward", "erase"),
860
- literalHint(theme, "↑↓", "navigate"),
861
- keybindingHint(theme, this.keybindings, "tui.select.confirm", "select"),
862
- literalHint(theme, "esc", "clear/cancel"),
863
- alternateCancelKeys.length > 0
864
- ? literalHint(theme, formatKeyList(alternateCancelKeys), "cancel")
865
- : null,
866
- ]
867
- .filter((hint): hint is string => !!hint)
868
- .join(" ");
869
- this.helpText.setText(theme.fg("dim", hints));
870
- }
871
- }
872
-
873
- private ensureSingleSelectList(): WrappedSingleSelectList {
874
- if (this.singleSelectList) return this.singleSelectList;
875
-
876
- const list = new WrappedSingleSelectList(this.options, this.allowFreeform, this.theme, this.keybindings);
877
- list.onSubmit = (result) => this.onDone({ answer: result, wasCustom: false });
878
- list.onCancel = () => this.onDone(null);
879
- list.onEnterFreeform = () => this.showFreeformMode();
880
-
881
- this.singleSelectList = list;
882
- return list;
883
- }
884
-
885
- private ensureMultiSelectList(): MultiSelectList {
886
- if (this.multiSelectList) return this.multiSelectList;
887
-
888
- const list = new MultiSelectList(this.options, this.allowFreeform, this.theme, this.keybindings);
889
- list.onCancel = () => this.onDone(null);
890
- list.onSubmit = (result) => this.onDone({ answer: result, wasCustom: false });
891
- list.onEnterFreeform = () => this.showFreeformMode();
892
-
893
- this.multiSelectList = list;
894
- return list;
895
- }
896
-
897
- private ensureEditor(): Editor {
898
- if (this.editor) return this.editor;
899
- const editor = new Editor(this.tui, createEditorTheme(this.theme));
900
- editor.disableSubmit = false;
901
- editor.onSubmit = (text: string) => {
902
- const trimmed = text.trim();
903
- this.onDone(trimmed ? { answer: trimmed, wasCustom: true } : null);
904
- };
905
- this.editor = editor;
906
- return editor;
907
- }
908
-
909
- private showSelectMode(): void {
910
- this.mode = "select";
911
- this.modeContainer.clear();
912
-
913
- if (this.allowMultiple) {
914
- this.modeContainer.addChild(this.ensureMultiSelectList());
915
- } else {
916
- this.modeContainer.addChild(this.ensureSingleSelectList());
917
- }
918
-
919
- this.updateHelpText();
920
- this.invalidate();
921
- this.tui.requestRender();
922
- }
923
-
924
- private showFreeformMode(): void {
925
- this.mode = "freeform";
926
- this.modeContainer.clear();
927
-
928
- const editor = this.ensureEditor();
929
- (editor as any).focused = this._focused;
930
-
931
- this.modeContainer.addChild(new Text(this.theme.fg("accent", this.theme.bold("Custom response")), 1, 0));
932
- this.modeContainer.addChild(new Spacer(1));
933
- this.modeContainer.addChild(editor);
934
-
935
- this.updateHelpText();
936
- this.invalidate();
937
- this.tui.requestRender();
938
- }
939
-
940
- handleInput(data: string): void {
941
- if (this.mode === "freeform") {
942
- if (matchesKey(data, Key.escape)) {
943
- this.showSelectMode();
944
- return;
945
- }
946
-
947
- if (this.keybindings.matches(data, "tui.select.cancel")) {
948
- this.onDone(null);
949
- return;
950
- }
951
-
952
- this.ensureEditor().handleInput(data);
953
- this.tui.requestRender();
954
- return;
955
- }
956
-
957
- // Selection mode
958
- if (this.allowMultiple) {
959
- this.ensureMultiSelectList().handleInput?.(data);
960
- this.tui.requestRender();
961
- return;
962
- }
963
-
964
- this.ensureSingleSelectList().handleInput?.(data);
965
- this.tui.requestRender();
966
- }
812
+ private question: string;
813
+ private context?: string;
814
+ private options: QuestionOption[];
815
+ private allowMultiple: boolean;
816
+ private allowFreeform: boolean;
817
+ private allowComment: boolean;
818
+ private tui: TUI;
819
+ private theme: Theme;
820
+ private keybindings: KeybindingsManager;
821
+ private onDone: (result: AskUIResult | null) => void;
822
+
823
+ private mode: AskMode = "select";
824
+ private pendingSelections: string[] = [];
825
+ private freeformDraft = "";
826
+ private commentDraft = "";
827
+
828
+ // Static layout components
829
+ private titleText: Text;
830
+ private questionText: Text;
831
+ private contextComponent?: Component;
832
+ private modeContainer: Container;
833
+ private helpText: Text;
834
+
835
+ // Mode components
836
+ private singleSelectList?: WrappedSingleSelectList;
837
+ private multiSelectList?: MultiSelectList;
838
+ private editor?: Editor;
839
+
840
+ // Focusable - propagate to Editor for IME cursor positioning
841
+ private _focused = false;
842
+ get focused(): boolean {
843
+ return this._focused;
844
+ }
845
+ set focused(value: boolean) {
846
+ this._focused = value;
847
+ if (this.editor && (this.mode === "freeform" || this.mode === "comment")) {
848
+ (this.editor as any).focused = value;
849
+ }
850
+ }
851
+
852
+ constructor(
853
+ question: string,
854
+ context: string | undefined,
855
+ options: QuestionOption[],
856
+ allowMultiple: boolean,
857
+ allowFreeform: boolean,
858
+ allowComment: boolean,
859
+ tui: TUI,
860
+ theme: Theme,
861
+ keybindings: KeybindingsManager,
862
+ onDone: (result: AskUIResult | null) => void,
863
+ ) {
864
+ super();
865
+
866
+ this.question = question;
867
+ this.context = context;
868
+ this.options = options;
869
+ this.allowMultiple = allowMultiple;
870
+ this.allowFreeform = allowFreeform;
871
+ this.allowComment = allowComment;
872
+ this.tui = tui;
873
+ this.theme = theme;
874
+ this.keybindings = keybindings;
875
+ this.onDone = onDone;
876
+
877
+ // Layout skeleton
878
+ this.addChild(new BoxBorderTop(
879
+ (s: string) => theme.fg("accent", s),
880
+ "ask_user",
881
+ (s: string) => theme.fg("dim", theme.bold(s)),
882
+ ));
883
+ this.addChild(new Spacer(1));
884
+
885
+ this.titleText = new Text("", 1, 0);
886
+ this.addChild(this.titleText);
887
+ this.addChild(new Spacer(1));
888
+
889
+ this.questionText = new Text("", 1, 0);
890
+ this.addChild(this.questionText);
891
+
892
+ if (this.context) {
893
+ this.addChild(new Spacer(1));
894
+ let mdTheme: MarkdownTheme | undefined;
895
+ try {
896
+ mdTheme = getMarkdownTheme();
897
+ } catch { }
898
+ if (mdTheme) {
899
+ this.contextComponent = new Markdown("", 1, 0, mdTheme);
900
+ } else {
901
+ this.contextComponent = new Text("", 1, 0);
902
+ }
903
+ this.addChild(this.contextComponent);
904
+ }
905
+
906
+ this.addChild(new Spacer(1));
907
+
908
+ this.modeContainer = new Container();
909
+ this.addChild(this.modeContainer);
910
+
911
+ this.addChild(new Spacer(1));
912
+ this.helpText = new Text("", 1, 0);
913
+ this.addChild(this.helpText);
914
+
915
+ this.addChild(new Spacer(1));
916
+ this.addChild(new BoxBorderBottom(
917
+ (s: string) => theme.fg("accent", s),
918
+ `v${ASK_USER_VERSION}`,
919
+ (s: string) => theme.fg("dim", s),
920
+ ));
921
+
922
+ this.updateStaticText();
923
+ this.showSelectMode();
924
+ }
925
+
926
+ override invalidate(): void {
927
+ super.invalidate();
928
+ this.updateStaticText();
929
+ this.updateHelpText();
930
+ }
931
+
932
+ override render(width: number): string[] {
933
+ const innerWidth = Math.max(1, width - BOX_BORDER_OVERHEAD);
934
+
935
+ if (this.mode === "select" && !this.allowMultiple) {
936
+ const overlayMaxHeight = Math.max(12, Math.floor(this.tui.terminal.rows * ASK_OVERLAY_MAX_HEIGHT_RATIO));
937
+ const staticLines = this.countStaticLines(innerWidth);
938
+ const availableOptionRows = Math.max(4, overlayMaxHeight - staticLines);
939
+ this.ensureSingleSelectList().setMaxVisibleRows(availableOptionRows);
940
+ }
941
+
942
+ // Render children at the inner width (excluding side border characters)
943
+ const rawLines = super.render(innerWidth);
944
+
945
+ // First and last lines are the top/bottom box borders pass through at full width.
946
+ // All inner lines get wrapped with side borders.
947
+ const borderColor = (s: string) => this.theme.fg("accent", s);
948
+ const titleColor = (s: string) => this.theme.fg("dim", this.theme.bold(s));
949
+ return rawLines.map((line, index) => {
950
+ if (index === 0 || index === rawLines.length - 1) {
951
+ // Box top/bottom borders already rendered at innerWidth — re-render at full width
952
+ if (index === 0) return new BoxBorderTop(borderColor, "ask_user", titleColor).render(width)[0];
953
+ return new BoxBorderBottom(borderColor, `v${ASK_USER_VERSION}`, (s: string) => this.theme.fg("dim", s)).render(width)[0];
954
+ }
955
+ const padded = truncateToWidth(line, innerWidth, "", true);
956
+ return `${borderColor(BOX_BORDER_LEFT)}${padded}${borderColor(BOX_BORDER_RIGHT)}`;
957
+ });
958
+ }
959
+
960
+ private countWrappedLines(text: string, width: number): number {
961
+ return Math.max(1, wrapTextWithAnsi(text, Math.max(10, width - 2)).length);
962
+ }
963
+
964
+ private countStaticLines(width: number): number {
965
+ const titleLines = 1;
966
+ const questionLines = this.countWrappedLines(this.question, width);
967
+ const contextLines = this.context ? 1 + this.countWrappedLines(this.context, width) : 0;
968
+ const helpLines = 1;
969
+ const borderLines = 2;
970
+ const spacerLines = this.context ? 6 : 5;
971
+ return borderLines + spacerLines + titleLines + questionLines + contextLines + helpLines;
972
+ }
973
+
974
+ private updateStaticText(): void {
975
+ const theme = this.theme;
976
+ const title = this.mode === "comment" ? "Optional comment" : "Question";
977
+ this.titleText.setText(theme.fg("accent", theme.bold(title)));
978
+ this.questionText.setText(theme.fg("text", theme.bold(this.question)));
979
+ if (this.contextComponent && this.context) {
980
+ if (this.contextComponent instanceof Markdown) {
981
+ (this.contextComponent as Markdown).setText(
982
+ `**Context:**\n${this.context}`,
983
+ );
984
+ } else {
985
+ (this.contextComponent as Text).setText(
986
+ `${theme.fg("accent", theme.bold("Context:"))}\n${theme.fg("dim", this.context)}`,
987
+ );
988
+ }
989
+ }
990
+ }
991
+
992
+ private updateHelpText(): void {
993
+ const theme = this.theme;
994
+ if (this.mode === "freeform" || this.mode === "comment") {
995
+ const alternateCancelKeys = this.keybindings
996
+ .getKeys("tui.select.cancel")
997
+ .filter((key) => key !== "escape" && key !== "esc");
998
+ const hints = [
999
+ keybindingHint(theme, this.keybindings, "tui.input.submit", this.mode === "comment" ? "submit/skip" : "submit"),
1000
+ keybindingHint(theme, this.keybindings, "tui.input.newLine", "newline"),
1001
+ literalHint(theme, "esc", "back"),
1002
+ alternateCancelKeys.length > 0 ? literalHint(theme, formatKeyList(alternateCancelKeys), "cancel") : null,
1003
+ ]
1004
+ .filter((hint): hint is string => !!hint)
1005
+ .join(" ");
1006
+ this.helpText.setText(theme.fg("dim", hints));
1007
+ return;
1008
+ }
1009
+
1010
+ if (this.allowMultiple) {
1011
+ const hints = [
1012
+ literalHint(theme, "↑↓", "navigate"),
1013
+ literalHint(theme, "space", "toggle"),
1014
+ this.allowComment ? literalHint(theme, "ctrl+g", "toggle context") : null,
1015
+ keybindingHint(theme, this.keybindings, "tui.select.confirm", "submit"),
1016
+ keybindingHint(theme, this.keybindings, "tui.select.cancel", "cancel"),
1017
+ ]
1018
+ .filter((hint): hint is string => !!hint)
1019
+ .join(" ");
1020
+ this.helpText.setText(theme.fg("dim", hints));
1021
+ } else {
1022
+ const alternateCancelKeys = this.keybindings
1023
+ .getKeys("tui.select.cancel")
1024
+ .filter((key) => key !== "escape" && key !== "esc");
1025
+ const hints = [
1026
+ literalHint(theme, "type", "filter"),
1027
+ keybindingHint(theme, this.keybindings, "tui.editor.deleteCharBackward", "erase"),
1028
+ literalHint(theme, "↑↓", "navigate"),
1029
+ this.allowComment ? literalHint(theme, "ctrl+g", "toggle context") : null,
1030
+ keybindingHint(theme, this.keybindings, "tui.select.confirm", "select"),
1031
+ literalHint(theme, "esc", "clear/cancel"),
1032
+ alternateCancelKeys.length > 0
1033
+ ? literalHint(theme, formatKeyList(alternateCancelKeys), "cancel")
1034
+ : null,
1035
+ ]
1036
+ .filter((hint): hint is string => !!hint)
1037
+ .join(" ");
1038
+ this.helpText.setText(theme.fg("dim", hints));
1039
+ }
1040
+ }
1041
+
1042
+ private ensureSingleSelectList(): WrappedSingleSelectList {
1043
+ if (this.singleSelectList) return this.singleSelectList;
1044
+
1045
+ const list = new WrappedSingleSelectList(
1046
+ this.options,
1047
+ this.allowFreeform,
1048
+ this.allowComment,
1049
+ this.theme,
1050
+ this.keybindings,
1051
+ );
1052
+ list.onSubmit = (result) => this.handleSelectionSubmit([result], list.isCommentEnabled());
1053
+ list.onCancel = () => this.onDone(null);
1054
+ list.onEnterFreeform = () => this.showFreeformMode();
1055
+
1056
+ this.singleSelectList = list;
1057
+ return list;
1058
+ }
1059
+
1060
+ private ensureMultiSelectList(): MultiSelectList {
1061
+ if (this.multiSelectList) return this.multiSelectList;
1062
+
1063
+ const list = new MultiSelectList(
1064
+ this.options,
1065
+ this.allowFreeform,
1066
+ this.allowComment,
1067
+ this.theme,
1068
+ this.keybindings,
1069
+ );
1070
+ list.onCancel = () => this.onDone(null);
1071
+ list.onSubmit = (result) => this.handleSelectionSubmit(result, list.isCommentEnabled());
1072
+ list.onEnterFreeform = () => this.showFreeformMode();
1073
+
1074
+ this.multiSelectList = list;
1075
+ return list;
1076
+ }
1077
+
1078
+ private ensureEditor(): Editor {
1079
+ if (this.editor) return this.editor;
1080
+ const editor = new Editor(this.tui, createEditorTheme(this.theme));
1081
+ editor.disableSubmit = false;
1082
+ editor.onSubmit = (text: string) => {
1083
+ this.handleEditorSubmit(text);
1084
+ };
1085
+ this.editor = editor;
1086
+ return editor;
1087
+ }
1088
+
1089
+ private saveEditorDraft(): void {
1090
+ if (!this.editor) return;
1091
+ const getText = (this.editor as any).getText;
1092
+ if (typeof getText !== "function") return;
1093
+
1094
+ const currentText = String(getText.call(this.editor) ?? "");
1095
+ if (this.mode === "freeform") {
1096
+ this.freeformDraft = currentText;
1097
+ } else if (this.mode === "comment") {
1098
+ this.commentDraft = currentText;
1099
+ }
1100
+ }
1101
+
1102
+ private setEditorText(text: string): void {
1103
+ const editor = this.ensureEditor();
1104
+ const setText = (editor as any).setText;
1105
+ if (typeof setText === "function") {
1106
+ setText.call(editor, text);
1107
+ }
1108
+ }
1109
+
1110
+ private handleSelectionSubmit(selections: string[], wantsComment: boolean): void {
1111
+ if (this.allowComment && wantsComment) {
1112
+ this.pendingSelections = selections;
1113
+ this.commentDraft = "";
1114
+ this.showCommentMode();
1115
+ return;
1116
+ }
1117
+
1118
+ this.onDone(createSelectionResponse(selections));
1119
+ }
1120
+
1121
+ private handleEditorSubmit(text: string): void {
1122
+ if (this.mode === "freeform") {
1123
+ this.onDone(createFreeformResponse(text));
1124
+ return;
1125
+ }
1126
+
1127
+ if (this.mode === "comment") {
1128
+ this.commentDraft = text;
1129
+ this.onDone(createSelectionResponse(this.pendingSelections, text));
1130
+ }
1131
+ }
1132
+
1133
+ private showSelectMode(): void {
1134
+ if (this.mode === "freeform" || this.mode === "comment") {
1135
+ this.saveEditorDraft();
1136
+ }
1137
+
1138
+ this.mode = "select";
1139
+ this.pendingSelections = [];
1140
+ this.modeContainer.clear();
1141
+
1142
+ if (this.allowMultiple) {
1143
+ this.modeContainer.addChild(this.ensureMultiSelectList());
1144
+ } else {
1145
+ this.modeContainer.addChild(this.ensureSingleSelectList());
1146
+ }
1147
+
1148
+ this.updateHelpText();
1149
+ this.invalidate();
1150
+ this.tui.requestRender();
1151
+ }
1152
+
1153
+ private showFreeformMode(): void {
1154
+ if (this.mode === "comment") {
1155
+ this.saveEditorDraft();
1156
+ }
1157
+
1158
+ this.mode = "freeform";
1159
+ this.modeContainer.clear();
1160
+
1161
+ const editor = this.ensureEditor();
1162
+ this.setEditorText(this.freeformDraft);
1163
+ (editor as any).focused = this._focused;
1164
+
1165
+ this.modeContainer.addChild(new Text(this.theme.fg("accent", this.theme.bold("Custom response")), 1, 0));
1166
+ this.modeContainer.addChild(new Spacer(1));
1167
+ this.modeContainer.addChild(editor);
1168
+
1169
+ this.updateHelpText();
1170
+ this.invalidate();
1171
+ this.tui.requestRender();
1172
+ }
1173
+
1174
+ private showCommentMode(): void {
1175
+ if (this.mode === "freeform") {
1176
+ this.saveEditorDraft();
1177
+ }
1178
+
1179
+ this.mode = "comment";
1180
+ this.modeContainer.clear();
1181
+
1182
+ const editor = this.ensureEditor();
1183
+ this.setEditorText(this.commentDraft);
1184
+ (editor as any).focused = this._focused;
1185
+
1186
+ const selectedLabel = this.pendingSelections.length === 1 ? "Selected option:" : "Selected options:";
1187
+ this.modeContainer.addChild(new Text(this.theme.fg("accent", this.theme.bold(selectedLabel)), 1, 0));
1188
+ this.modeContainer.addChild(new Text(this.theme.fg("text", this.pendingSelections.join(", ")), 1, 0));
1189
+ this.modeContainer.addChild(new Spacer(1));
1190
+ this.modeContainer.addChild(editor);
1191
+
1192
+ this.updateHelpText();
1193
+ this.invalidate();
1194
+ this.tui.requestRender();
1195
+ }
1196
+
1197
+ handleInput(data: string): void {
1198
+ if (this.mode === "freeform" || this.mode === "comment") {
1199
+ if (matchesKey(data, Key.escape)) {
1200
+ this.showSelectMode();
1201
+ return;
1202
+ }
1203
+
1204
+ if (this.keybindings.matches(data, "tui.select.cancel")) {
1205
+ this.onDone(null);
1206
+ return;
1207
+ }
1208
+
1209
+ this.ensureEditor().handleInput(data);
1210
+ this.tui.requestRender();
1211
+ return;
1212
+ }
1213
+
1214
+ if (this.allowMultiple) {
1215
+ this.ensureMultiSelectList().handleInput?.(data);
1216
+ this.tui.requestRender();
1217
+ return;
1218
+ }
1219
+
1220
+ this.ensureSingleSelectList().handleInput?.(data);
1221
+ this.tui.requestRender();
1222
+ }
1223
+ }
1224
+
1225
+ /**
1226
+ * RPC/headless fallback: use dialog methods (select/input) instead of the rich TUI overlay.
1227
+ * ctx.ui.custom() returns undefined in RPC mode, so we degrade gracefully.
1228
+ */
1229
+ async function askViaDialogs(
1230
+ ui: { select: Function; input: Function },
1231
+ question: string,
1232
+ context: string | undefined,
1233
+ options: QuestionOption[],
1234
+ allowMultiple: boolean,
1235
+ allowFreeform: boolean,
1236
+ allowComment: boolean,
1237
+ timeout?: number,
1238
+ ): Promise<AskUIResult | null> {
1239
+ const dialogOpts = timeout ? { timeout } : undefined;
1240
+ const prompt = context ? `${question}\n\nContext:\n${context}` : question;
1241
+
1242
+ if (allowMultiple) {
1243
+ const optionList = formatOptionsForMessage(options);
1244
+ const rawSelections = await ui.input(
1245
+ `${prompt}\n\nOptions (select one or more):\n${optionList}`,
1246
+ "Type your selection(s)...",
1247
+ dialogOpts,
1248
+ ) as string | undefined;
1249
+ if (isCancelledInput(rawSelections)) return null;
1250
+
1251
+ const selections = parseDialogSelections(rawSelections);
1252
+ if (selections.length === 0) return null;
1253
+
1254
+ if (!allowComment) {
1255
+ return createSelectionResponse(selections);
1256
+ }
1257
+
1258
+ const comment = await ui.input(
1259
+ buildCommentPrompt(prompt, selections),
1260
+ "Optional comment (press Enter to skip)...",
1261
+ dialogOpts,
1262
+ ) as string | undefined;
1263
+ return createSelectionResponse(selections, comment);
1264
+ }
1265
+
1266
+ const selectOptions = options.map((o) => o.title);
1267
+ if (allowFreeform) selectOptions.push(FREEFORM_SENTINEL);
1268
+
1269
+ const selected = await ui.select(prompt, selectOptions, dialogOpts) as string | undefined;
1270
+ if (isCancelledInput(selected)) return null;
1271
+
1272
+ if (selected === FREEFORM_SENTINEL) {
1273
+ const answer = await ui.input(prompt, "Type your answer...", dialogOpts) as string | undefined;
1274
+ if (isCancelledInput(answer)) return null;
1275
+ return createFreeformResponse(answer);
1276
+ }
1277
+
1278
+ if (!allowComment) {
1279
+ return createSelectionResponse([selected]);
1280
+ }
1281
+
1282
+ const comment = await ui.input(
1283
+ buildCommentPrompt(prompt, [selected]),
1284
+ "Optional comment (press Enter to skip)...",
1285
+ dialogOpts,
1286
+ ) as string | undefined;
1287
+ return createSelectionResponse([selected], comment);
967
1288
  }
968
1289
 
969
- export default function (pi: ExtensionAPI) {
970
- pi.registerTool({
971
- name: "ask_user",
972
- label: "Ask User",
973
- description:
974
- "Ask the user a question with optional multiple-choice answers. Use this to gather information interactively. Before calling, gather context with tools (read/web/ref) and pass a short summary via the context field.",
975
- promptSnippet:
976
- "Ask the user a question with optional multiple-choice answers to gather information interactively",
977
- promptGuidelines: [
978
- "Before calling ask_user, gather context with tools (read/web/ref) and pass a short summary via the context field.",
979
- "Use ask_user when the user's intent is ambiguous, when a decision requires explicit user input, or when multiple valid options exist.",
980
- ],
981
- parameters: Type.Object({
982
- question: Type.String({ description: "The question to ask the user" }),
983
- context: Type.Optional(
984
- Type.String({
985
- description: "Relevant context to show before the question (summary of findings)",
986
- }),
987
- ),
988
- options: Type.Optional(
989
- Type.Array(
990
- Type.Union([
991
- Type.String({ description: "Short title for this option" }),
992
- Type.Object({
993
- title: Type.String({ description: "Short title for this option" }),
994
- description: Type.Optional(
995
- Type.String({ description: "Longer description explaining this option" }),
996
- ),
997
- }),
998
- ]),
999
- { description: "List of options for the user to choose from" },
1000
- ),
1001
- ),
1002
- allowMultiple: Type.Optional(
1003
- Type.Boolean({ description: "Allow selecting multiple options. Default: false" }),
1004
- ),
1005
- allowFreeform: Type.Optional(
1006
- Type.Boolean({ description: "Add a freeform text option. Default: true" }),
1007
- ),
1008
- timeout: Type.Optional(
1009
- Type.Number({ description: "Auto-dismiss after N milliseconds. Returns null (cancelled) when expired." }),
1010
- ),
1011
- }),
1012
-
1013
- async execute(_toolCallId, params, signal, onUpdate, ctx) {
1014
- if (signal?.aborted) {
1015
- return {
1016
- content: [{ type: "text", text: "Cancelled" }],
1017
- details: { question: params.question, options: [], answer: null, cancelled: true } as AskToolDetails,
1018
- };
1019
- }
1020
-
1021
- const {
1022
- question,
1023
- context,
1024
- options: rawOptions = [],
1025
- allowMultiple = false,
1026
- allowFreeform = true,
1027
- timeout,
1028
- } = params as AskParams;
1029
- const options = normalizeOptions(rawOptions);
1030
- const normalizedContext = context?.trim() || undefined;
1031
-
1032
- if (!ctx.hasUI || !ctx.ui) {
1033
- const optionText = options.length > 0 ? `\n\nOptions:\n${formatOptionsForMessage(options)}` : "";
1034
- const freeformHint = allowFreeform ? "\n\nYou can also answer freely." : "";
1035
- const contextText = normalizedContext ? `\n\nContext:\n${normalizedContext}` : "";
1036
- return {
1037
- content: [
1038
- {
1039
- type: "text",
1040
- text: `Ask requires interactive mode. Please answer:\n\n${question}${contextText}${optionText}${freeformHint}`,
1041
- },
1042
- ],
1043
- isError: true,
1044
- details: { question, context: normalizedContext, options, answer: null, cancelled: true } as AskToolDetails,
1045
- };
1046
- }
1047
-
1048
- // If no options provided, fall back to freeform input prompt.
1049
- if (options.length === 0) {
1050
- const prompt = normalizedContext ? `${question}\n\nContext:\n${normalizedContext}` : question;
1051
- const answer = await ctx.ui.input(prompt, "Type your answer...", timeout ? { timeout } : undefined);
1052
-
1053
- if (!answer) {
1054
- return {
1055
- content: [{ type: "text", text: "User cancelled the question" }],
1056
- details: { question, context: normalizedContext, options, answer: null, cancelled: true } as AskToolDetails,
1057
- };
1058
- }
1059
-
1060
- pi.events.emit("ask:answered", { question, context: normalizedContext, answer, wasCustom: true });
1061
- return {
1062
- content: [{ type: "text", text: `User answered: ${answer}` }],
1063
- details: { question, context: normalizedContext, options, answer, cancelled: false, wasCustom: true } as AskToolDetails,
1064
- };
1065
- }
1066
-
1067
- onUpdate?.({
1068
- content: [{ type: "text", text: "Waiting for user input..." }],
1069
- details: { question, context: normalizedContext, options, answer: null, cancelled: false },
1070
- });
1071
-
1072
- let result: AskUIResult | null;
1073
- try {
1074
- result = await ctx.ui.custom<AskUIResult | null>(
1075
- (tui, theme, keybindings, done) => {
1076
- // Wire AbortSignal so agent cancellation auto-dismisses the overlay
1077
- if (signal) {
1078
- const onAbort = () => done(null);
1079
- signal.addEventListener("abort", onAbort, { once: true });
1080
- }
1081
-
1082
- // Wire timeout for overlay mode
1083
- if (timeout && timeout > 0) {
1084
- setTimeout(() => done(null), timeout);
1085
- }
1086
-
1087
- return new AskComponent(
1088
- question,
1089
- normalizedContext,
1090
- options,
1091
- allowMultiple,
1092
- allowFreeform,
1093
- tui,
1094
- theme,
1095
- keybindings,
1096
- done,
1097
- );
1098
- },
1099
- {
1100
- overlay: true,
1101
- overlayOptions: {
1102
- anchor: "center",
1103
- width: ASK_OVERLAY_WIDTH,
1104
- minWidth: ASK_OVERLAY_MIN_WIDTH,
1105
- maxHeight: "85%",
1106
- margin: 1,
1107
- },
1108
- },
1109
- );
1110
- } catch (error) {
1111
- const message =
1112
- error instanceof Error ? `${error.message}\n${error.stack ?? ""}` : String(error);
1113
- return {
1114
- content: [{ type: "text", text: `Ask tool failed: ${message}` }],
1115
- isError: true,
1116
- details: { error: message },
1117
- };
1118
- }
1119
-
1120
- if (result === null) {
1121
- pi.events.emit("ask:cancelled", { question, context: normalizedContext, options });
1122
- return {
1123
- content: [{ type: "text", text: "User cancelled the question" }],
1124
- details: { question, context: normalizedContext, options, answer: null, cancelled: true } as AskToolDetails,
1125
- };
1126
- }
1127
-
1128
- pi.events.emit("ask:answered", {
1129
- question,
1130
- context: normalizedContext,
1131
- answer: result.answer,
1132
- wasCustom: result.wasCustom,
1133
- });
1134
- return {
1135
- content: [{ type: "text", text: `User answered: ${result.answer}` }],
1136
- details: {
1137
- question,
1138
- context: normalizedContext,
1139
- options,
1140
- answer: result.answer,
1141
- cancelled: false,
1142
- wasCustom: result.wasCustom,
1143
- } as AskToolDetails,
1144
- };
1145
- },
1146
-
1147
- renderCall(args, theme) {
1148
- const question = (args.question as string) || "";
1149
- const rawOptions = Array.isArray(args.options) ? args.options : [];
1150
- let text = theme.fg("toolTitle", theme.bold("ask_user "));
1151
- text += theme.fg("muted", question);
1152
- if (rawOptions.length > 0) {
1153
- const labels = rawOptions.map((o: unknown) =>
1154
- typeof o === "string" ? o : (o as QuestionOption)?.title ?? "",
1155
- );
1156
- text += "\n" + theme.fg("dim", ` ${rawOptions.length} option(s): ${labels.join(", ")}`);
1157
- }
1158
- if (args.allowMultiple) {
1159
- text += theme.fg("dim", " [multi-select]");
1160
- }
1161
- return new Text(text, 0, 0);
1162
- },
1163
-
1164
- renderResult(result, options, theme) {
1165
- const details = result.details as (AskToolDetails & { error?: string }) | undefined;
1166
-
1167
- if (details?.error) {
1168
- return new Text(theme.fg("error", `✗ ${details.error}`), 0, 0);
1169
- }
1170
-
1171
- if (options.isPartial) {
1172
- const waitingText = result.content
1173
- ?.filter((part: { type?: string; text?: string }) => part?.type === "text")
1174
- .map((part: { text?: string }) => part.text ?? "")
1175
- .join("\n")
1176
- .trim() || "Waiting for user input...";
1177
- return new Text(theme.fg("muted", waitingText), 0, 0);
1178
- }
1179
-
1180
- if (!details || details.cancelled) {
1181
- return new Text(theme.fg("warning", "Cancelled"), 0, 0);
1182
- }
1183
-
1184
- const answer = details.answer ?? "";
1185
- let text = theme.fg("success", "✓ ");
1186
- if (details.wasCustom) {
1187
- text += theme.fg("muted", "(wrote) ");
1188
- }
1189
- text += theme.fg("accent", answer);
1190
-
1191
- if (options.expanded) {
1192
- const selectedTitles = new Set(
1193
- answer
1194
- .split(",")
1195
- .map((value) => value.trim())
1196
- .filter(Boolean),
1197
- );
1198
- text += "\n" + theme.fg("dim", `Q: ${details.question}`);
1199
- if (details.context) {
1200
- text += "\n" + theme.fg("dim", details.context);
1201
- }
1202
- if (details.options && details.options.length > 0) {
1203
- text += "\n" + theme.fg("dim", "Options:");
1204
- for (const opt of details.options) {
1205
- const desc = opt.description ? ` — ${opt.description}` : "";
1206
- const isSelected = opt.title === answer || selectedTitles.has(opt.title);
1207
- const marker = isSelected ? theme.fg("success", "●") : theme.fg("dim", "○");
1208
- text += `\n ${marker} ${theme.fg("dim", opt.title)}${theme.fg("dim", desc)}`;
1209
- }
1210
- }
1211
- }
1212
-
1213
- return new Text(text, 0, 0);
1214
- },
1215
- });
1290
+ export default function(pi: ExtensionAPI) {
1291
+ pi.registerTool({
1292
+ name: "ask_user",
1293
+ label: "Ask User",
1294
+ description:
1295
+ "Ask the user a question with optional multiple-choice answers. Use this to gather information interactively. Before calling, gather context with tools (read/web/ref) and pass a short summary via the context field.",
1296
+ promptSnippet:
1297
+ "Ask the user a question with optional multiple-choice answers to gather information interactively",
1298
+ promptGuidelines: [
1299
+ "Before calling ask_user, gather context with tools (read/web/ref) and pass a short summary via the context field.",
1300
+ "Use ask_user when the user's intent is ambiguous, when a decision requires explicit user input, or when multiple valid options exist.",
1301
+ ],
1302
+ parameters: Type.Object({
1303
+ question: Type.String({ description: "The question to ask the user" }),
1304
+ context: Type.Optional(
1305
+ Type.String({
1306
+ description: "Relevant context to show before the question (summary of findings)",
1307
+ }),
1308
+ ),
1309
+ options: Type.Optional(
1310
+ Type.Array(
1311
+ Type.Union([
1312
+ Type.String({ description: "Short title for this option" }),
1313
+ Type.Object({
1314
+ title: Type.String({ description: "Short title for this option" }),
1315
+ description: Type.Optional(
1316
+ Type.String({ description: "Longer description explaining this option" }),
1317
+ ),
1318
+ }),
1319
+ ]),
1320
+ { description: "List of options for the user to choose from" },
1321
+ ),
1322
+ ),
1323
+ allowMultiple: Type.Optional(
1324
+ Type.Boolean({ description: "Allow selecting multiple options. Default: false" }),
1325
+ ),
1326
+ allowFreeform: Type.Optional(
1327
+ Type.Boolean({ description: "Add a freeform text option. Default: true" }),
1328
+ ),
1329
+ allowComment: Type.Optional(
1330
+ Type.Boolean({ description: "Collect an optional comment after selecting one or more options. Default: false" }),
1331
+ ),
1332
+ timeout: Type.Optional(
1333
+ Type.Number({ description: "Auto-dismiss after N milliseconds. Returns null (cancelled) when expired." }),
1334
+ ),
1335
+ }),
1336
+
1337
+ async execute(_toolCallId, params, signal, onUpdate, ctx) {
1338
+ if (signal?.aborted) {
1339
+ return {
1340
+ content: [{ type: "text", text: "Cancelled" }],
1341
+ details: { question: params.question, options: [], response: null, cancelled: true } as AskToolDetails,
1342
+ };
1343
+ }
1344
+
1345
+ const {
1346
+ question,
1347
+ context,
1348
+ options: rawOptions = [],
1349
+ allowMultiple = false,
1350
+ allowFreeform = true,
1351
+ allowComment = false,
1352
+ timeout,
1353
+ } = params as AskParams;
1354
+ const options = normalizeOptions(rawOptions);
1355
+ const normalizedContext = context?.trim() || undefined;
1356
+
1357
+ if (!ctx.hasUI || !ctx.ui) {
1358
+ const optionText = options.length > 0 ? `\n\nOptions:\n${formatOptionsForMessage(options)}` : "";
1359
+ const freeformHint = allowFreeform ? "\n\nYou can also answer freely." : "";
1360
+ const commentHint = allowComment ? "\n\nAfter choosing an option, you may add an optional comment." : "";
1361
+ const contextText = normalizedContext ? `\n\nContext:\n${normalizedContext}` : "";
1362
+ return {
1363
+ content: [
1364
+ {
1365
+ type: "text",
1366
+ text: `Ask requires interactive mode. Please answer:\n\n${question}${contextText}${optionText}${freeformHint}${commentHint}`,
1367
+ },
1368
+ ],
1369
+ isError: true,
1370
+ details: { question, context: normalizedContext, options, response: null, cancelled: true } as AskToolDetails,
1371
+ };
1372
+ }
1373
+
1374
+ if (options.length === 0) {
1375
+ const prompt = normalizedContext ? `${question}\n\nContext:\n${normalizedContext}` : question;
1376
+ const answer = await ctx.ui.input(prompt, "Type your answer...", timeout ? { timeout } : undefined);
1377
+ const response = createFreeformResponse(answer);
1378
+
1379
+ if (!response) {
1380
+ return {
1381
+ content: [{ type: "text", text: "User cancelled the question" }],
1382
+ details: { question, context: normalizedContext, options, response: null, cancelled: true } as AskToolDetails,
1383
+ };
1384
+ }
1385
+
1386
+ pi.events.emit("ask:answered", { question, context: normalizedContext, response });
1387
+ return {
1388
+ content: [{ type: "text", text: `User answered: ${formatResponseSummary(response)}` }],
1389
+ details: { question, context: normalizedContext, options, response, cancelled: false } as AskToolDetails,
1390
+ };
1391
+ }
1392
+
1393
+ onUpdate?.({
1394
+ content: [{ type: "text", text: "Waiting for user input..." }],
1395
+ details: { question, context: normalizedContext, options, response: null, cancelled: false },
1396
+ });
1397
+
1398
+ let result: AskUIResult | null;
1399
+ try {
1400
+ const customResult = await ctx.ui.custom<AskUIResult | null>(
1401
+ (tui, theme, keybindings, done) => {
1402
+ if (signal) {
1403
+ const onAbort = () => done(null);
1404
+ signal.addEventListener("abort", onAbort, { once: true });
1405
+ }
1406
+
1407
+ if (timeout && timeout > 0) {
1408
+ setTimeout(() => done(null), timeout);
1409
+ }
1410
+
1411
+ return new AskComponent(
1412
+ question,
1413
+ normalizedContext,
1414
+ options,
1415
+ allowMultiple,
1416
+ allowFreeform,
1417
+ allowComment,
1418
+ tui,
1419
+ theme,
1420
+ keybindings,
1421
+ done,
1422
+ );
1423
+ },
1424
+ {
1425
+ overlay: true,
1426
+ overlayOptions: {
1427
+ anchor: "center",
1428
+ width: ASK_OVERLAY_WIDTH,
1429
+ minWidth: ASK_OVERLAY_MIN_WIDTH,
1430
+ maxHeight: "85%",
1431
+ margin: 1,
1432
+ },
1433
+ },
1434
+ );
1435
+
1436
+ if (customResult !== undefined) {
1437
+ result = customResult;
1438
+ } else {
1439
+ // RPC/headless mode: degrade to select()/input() dialog protocol
1440
+ result = await askViaDialogs(ctx.ui, question, normalizedContext, options, allowMultiple, allowFreeform, allowComment, timeout);
1441
+ }
1442
+ } catch (error) {
1443
+ const message =
1444
+ error instanceof Error ? `${error.message}\n${error.stack ?? ""}` : String(error);
1445
+ return {
1446
+ content: [{ type: "text", text: `Ask tool failed: ${message}` }],
1447
+ isError: true,
1448
+ details: { error: message },
1449
+ };
1450
+ }
1451
+
1452
+ if (result === null) {
1453
+ pi.events.emit("ask:cancelled", { question, context: normalizedContext, options });
1454
+ return {
1455
+ content: [{ type: "text", text: "User cancelled the question" }],
1456
+ details: { question, context: normalizedContext, options, response: null, cancelled: true } as AskToolDetails,
1457
+ };
1458
+ }
1459
+
1460
+ pi.events.emit("ask:answered", {
1461
+ question,
1462
+ context: normalizedContext,
1463
+ response: result,
1464
+ });
1465
+ return {
1466
+ content: [{ type: "text", text: `User answered: ${formatResponseSummary(result)}` }],
1467
+ details: {
1468
+ question,
1469
+ context: normalizedContext,
1470
+ options,
1471
+ response: result,
1472
+ cancelled: false,
1473
+ } as AskToolDetails,
1474
+ };
1475
+ },
1476
+
1477
+ renderCall(args, theme) {
1478
+ const question = (args.question as string) || "";
1479
+ const rawOptions = Array.isArray(args.options) ? args.options : [];
1480
+ let text = theme.fg("toolTitle", theme.bold("ask_user "));
1481
+ text += theme.fg("muted", question);
1482
+ if (rawOptions.length > 0) {
1483
+ const labels = rawOptions.map((o: unknown) =>
1484
+ typeof o === "string" ? o : (o as QuestionOption)?.title ?? "",
1485
+ );
1486
+ text += "\n" + theme.fg("dim", ` ${rawOptions.length} option(s): ${labels.join(", ")}`);
1487
+ }
1488
+ if (args.allowMultiple) {
1489
+ text += theme.fg("dim", " [multi-select]");
1490
+ }
1491
+ if (args.allowComment) {
1492
+ text += theme.fg("dim", " [optional comment]");
1493
+ }
1494
+ return new Text(text, 0, 0);
1495
+ },
1496
+
1497
+ renderResult(result, options, theme) {
1498
+ const details = result.details as (AskToolDetails & { error?: string }) | undefined;
1499
+
1500
+ if (details?.error) {
1501
+ return new Text(theme.fg("error", `✗ ${details.error}`), 0, 0);
1502
+ }
1503
+
1504
+ if (options.isPartial) {
1505
+ const waitingText = result.content
1506
+ ?.filter((part: { type?: string; text?: string }) => part?.type === "text")
1507
+ .map((part: { text?: string }) => part.text ?? "")
1508
+ .join("\n")
1509
+ .trim() || "Waiting for user input...";
1510
+ return new Text(theme.fg("muted", waitingText), 0, 0);
1511
+ }
1512
+
1513
+ if (!details || details.cancelled || !details.response) {
1514
+ return new Text(theme.fg("warning", "Cancelled"), 0, 0);
1515
+ }
1516
+
1517
+ const response = details.response;
1518
+ let text = theme.fg("success", "✓ ");
1519
+ if (response.kind === "freeform") {
1520
+ text += theme.fg("muted", "(wrote) ");
1521
+ }
1522
+ text += theme.fg("accent", formatResponseSummary(response));
1523
+
1524
+ if (options.expanded) {
1525
+ text += "\n" + theme.fg("dim", `Q: ${details.question}`);
1526
+ if (details.context) {
1527
+ text += "\n" + theme.fg("dim", details.context);
1528
+ }
1529
+
1530
+ if (isSelectionResponse(response) && details.options.length > 0) {
1531
+ const selectedTitles = new Set(response.selections);
1532
+ text += "\n" + theme.fg("dim", "Options:");
1533
+ for (const opt of details.options) {
1534
+ const desc = opt.description ? ` — ${opt.description}` : "";
1535
+ const marker = selectedTitles.has(opt.title) ? theme.fg("success", "●") : theme.fg("dim", "○");
1536
+ text += `\n ${marker} ${theme.fg("dim", opt.title)}${theme.fg("dim", desc)}`;
1537
+ }
1538
+ if (response.comment) {
1539
+ text += `\n${theme.fg("dim", "Comment:")} ${theme.fg("dim", response.comment)}`;
1540
+ }
1541
+ }
1542
+ }
1543
+
1544
+ return new Text(text, 0, 0);
1545
+ },
1546
+ });
1216
1547
  }