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/README.md +9 -3
- package/index.ts +1465 -1134
- package/package.json +1 -1
- package/single-select-layout.ts +41 -13
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
wasCustom?: boolean;
|
|
60
|
+
question: string;
|
|
61
|
+
context?: string;
|
|
62
|
+
options: QuestionOption[];
|
|
63
|
+
response: AskResponse | null;
|
|
64
|
+
cancelled: boolean;
|
|
54
65
|
}
|
|
55
66
|
|
|
56
|
-
|
|
57
|
-
answer: string;
|
|
58
|
-
wasCustom: boolean;
|
|
59
|
-
}
|
|
67
|
+
type AskUIResult = AskResponse;
|
|
60
68
|
|
|
61
69
|
function normalizeOptions(options: AskOptionInput[]): QuestionOption[] {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
210
|
+
return keys.join("/");
|
|
155
211
|
}
|
|
156
212
|
|
|
157
213
|
function keybindingHint(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
214
|
+
theme: Theme,
|
|
215
|
+
keybindings: KeybindingsManager,
|
|
216
|
+
keybinding: Keybinding,
|
|
217
|
+
description: string,
|
|
162
218
|
): string {
|
|
163
|
-
|
|
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
|
-
|
|
223
|
+
return `${theme.fg("dim", key)}${theme.fg("muted", ` ${description}`)}`;
|
|
168
224
|
}
|
|
169
225
|
|
|
170
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
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
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
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
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
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
|
}
|