-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathask-question-ui.ts
More file actions
328 lines (280 loc) · 10 KB
/
ask-question-ui.ts
File metadata and controls
328 lines (280 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
/**
* ask-question-ui — Reusable TUI for asking a single question
*
* Renders either a freeform text editor or a multiple-choice list with optional
* inline editing. Used by kbrainstorm extension and available for any extension
* needing interactive Q&A.
*/
import type { ExtensionUIContext, ThemeColor } from "@mariozechner/pi-coding-agent";
import type { EditorTheme } from "@mariozechner/pi-tui";
import { Editor, Key, matchesKey, truncateToWidth, wrapTextWithAnsi } from "@mariozechner/pi-tui";
// ── Public types ─────────────────────────────────────────────────────────────
/** A selectable option with an optional description. */
export interface OptionWithDesc {
label: string;
description?: string;
}
/** Parameters for showAskQuestion. */
export interface AskQuestionUIParams {
question: string;
context?: string;
options?: OptionWithDesc[];
}
/** Result returned from showAskQuestion, or null if the user skipped. */
export interface AskQuestionUIResult {
answer: string;
wasCustom: boolean;
index?: number;
}
/** Details shape stored alongside tool results for rendering. */
export interface AskQuestionDetails {
question: string;
context?: string;
options: string[];
answer: string | null;
wasCustom?: boolean;
}
// ── Internal types ───────────────────────────────────────────────────────────
/** Display option — extends OptionWithDesc with a freeform-slot flag. */
interface DisplayOption extends OptionWithDesc {
isOther?: boolean;
}
/** Minimal theme interface — subset of Theme used by render helpers. */
interface ThemeFg {
fg: (color: ThemeColor, text: string) => string;
}
// ── Shared helpers ───────────────────────────────────────────────────────────
/** Build the EditorTheme shared by both question modes. */
function buildEditorTheme(theme: ThemeFg): EditorTheme {
return {
borderColor: (s) => theme.fg("accent", s),
selectList: {
selectedPrefix: (t) => theme.fg("accent", t),
selectedText: (t) => theme.fg("accent", t),
description: (t) => theme.fg("muted", t),
scrollInfo: (t) => theme.fg("dim", t),
noMatch: (t) => theme.fg("warning", t),
},
};
}
/** Render the question header: top border, question text, optional context, trailing blank line. */
function renderQuestionHeader(
lines: string[],
add: (s: string) => void,
theme: ThemeFg,
params: AskQuestionUIParams,
contentWidth: number,
width: number,
): void {
add(theme.fg("accent", "─".repeat(width)));
for (const line of wrapTextWithAnsi(theme.fg("text", ` ${params.question}`), contentWidth)) {
add(line);
}
if (params.context) {
lines.push("");
for (const line of wrapTextWithAnsi(theme.fg("muted", ` ${params.context}`), contentWidth)) {
add(line);
}
}
lines.push("");
}
// ── Public API ───────────────────────────────────────────────────────────────
/**
* Show an interactive TUI question.
*
* - With `params.options`: numbered list with Tab-toggled inline editor,
* plus a "Type something." freeform fallback. Supports ↑↓, Enter, 1-9 quick select.
* - Without options: freeform text editor.
*
* @param ui - The ExtensionUIContext (must support `custom`).
* @param onWaiting - Called before the UI blocks, so callers can emit events.
* @param params - Question text, optional context, and optional options.
* @returns The user's answer, or null if they pressed Escape.
*/
export async function showAskQuestion(
ui: ExtensionUIContext,
onWaiting: () => void,
params: AskQuestionUIParams,
): Promise<AskQuestionUIResult | null> {
if (!params.options?.length) {
return showFreeformQuestion(ui, onWaiting, params);
}
return showMultipleChoiceQuestion(ui, onWaiting, params);
}
// ── Freeform question ────────────────────────────────────────────────────────
/** Freeform text editor question — no predefined options. */
async function showFreeformQuestion(
ui: ExtensionUIContext,
onWaiting: () => void,
params: AskQuestionUIParams,
): Promise<AskQuestionUIResult | null> {
onWaiting();
const result = await ui.custom<{ answer: string } | null>((tui, theme, _kb, done) => {
let cachedLines: string[] | undefined;
const editor = new Editor(tui, buildEditorTheme(theme));
editor.onSubmit = (value) => {
const trimmed = value.trim();
if (trimmed) done({ answer: trimmed });
};
function refresh(): void {
cachedLines = undefined;
tui.requestRender();
}
function handleInput(data: string): void {
if (matchesKey(data, Key.escape)) {
done(null);
return;
}
editor.handleInput(data);
refresh();
}
function render(width: number): string[] {
if (cachedLines) return cachedLines;
const lines: string[] = [];
const add = (s: string): void => { lines.push(truncateToWidth(s, width)); };
const contentWidth = Math.min(width - 4, 100);
renderQuestionHeader(lines, add, theme, params, contentWidth, width);
add(theme.fg("muted", " Your answer:"));
for (const line of editor.render(width - 2)) {
add(` ${line}`);
}
lines.push("");
add(theme.fg("dim", " Enter to submit • Shift+Enter for newline • Esc to skip"));
add(theme.fg("accent", "─".repeat(width)));
cachedLines = lines;
return lines;
}
return {
render,
invalidate: () => { cachedLines = undefined; },
handleInput,
};
});
if (!result) return null;
return { answer: result.answer, wasCustom: true };
}
// ── Multiple-choice question ─────────────────────────────────────────────────
/**
* Multiple-choice question with optional inline editing.
*
* The editor is hidden by default — the user navigates with ↑↓ and submits with
* Enter or 1-9. Tab toggles an inline editor pre-filled with the selected option.
* The "Type something." fallback auto-enters editing mode when selected.
*/
async function showMultipleChoiceQuestion(
ui: ExtensionUIContext,
onWaiting: () => void,
params: AskQuestionUIParams,
): Promise<AskQuestionUIResult | null> {
const allOptions: DisplayOption[] = [...params.options!, { label: "Type something.", isOther: true }];
onWaiting();
return ui.custom<{ answer: string; wasCustom: boolean; index?: number } | null>(
(tui, theme, _kb, done) => {
let optionIndex = 0;
let editing = false;
let cachedLines: string[] | undefined;
const editor = new Editor(tui, buildEditorTheme(theme));
editor.onSubmit = (value) => {
const trimmed = value.trim();
if (!trimmed) return;
const sourceOpt = allOptions[optionIndex];
const isOther = sourceOpt?.isOther === true;
done({
answer: trimmed,
wasCustom: isOther || trimmed !== sourceOpt?.label,
index: isOther ? undefined : optionIndex + 1,
});
};
function refresh(): void {
cachedLines = undefined;
tui.requestRender();
}
function enterEditMode(): void {
const opt = allOptions[optionIndex];
editor.setText(opt.isOther ? "" : opt.label);
editing = true;
refresh();
}
function submitOption(index: number): void {
const opt = allOptions[index];
if (opt.isOther) {
optionIndex = index;
enterEditMode();
return;
}
done({ answer: opt.label, wasCustom: false, index: index + 1 });
}
function handleInput(data: string): void {
// Editing mode — editor owns input, Esc exits back to selection
if (editing) {
if (matchesKey(data, Key.escape)) {
editing = false;
refresh();
return;
}
editor.handleInput(data);
refresh();
return;
}
// Selection mode
if (data.length === 1 && data >= "1" && data <= "9") {
const idx = parseInt(data, 10) - 1;
if (idx < allOptions.length) submitOption(idx);
return;
}
if (matchesKey(data, Key.up)) {
optionIndex = Math.max(0, optionIndex - 1);
refresh();
return;
}
if (matchesKey(data, Key.down)) {
optionIndex = Math.min(allOptions.length - 1, optionIndex + 1);
refresh();
return;
}
if (matchesKey(data, Key.enter)) { submitOption(optionIndex); return; }
if (matchesKey(data, Key.tab)) { enterEditMode(); return; }
if (matchesKey(data, Key.escape)) { done(null); return; }
}
function render(width: number): string[] {
if (cachedLines) return cachedLines;
const lines: string[] = [];
const add = (s: string): void => { lines.push(truncateToWidth(s, width)); };
const contentWidth = Math.min(width - 4, 100);
renderQuestionHeader(lines, add, theme, params, contentWidth, width);
for (let i = 0; i < allOptions.length; i++) {
const opt = allOptions[i];
const selected = i === optionIndex;
if (selected) {
add(theme.fg("accent", "> ") + theme.fg("accent", `${i + 1}. ${opt.label}`));
} else {
add(` ${theme.fg("text", `${i + 1}. ${opt.label}`)}`);
}
if (opt.description) {
for (const dl of wrapTextWithAnsi(theme.fg("muted", ` ${opt.description}`), contentWidth)) {
add(dl);
}
}
if (selected && editing) {
for (const line of editor.render(width - 6)) {
add(` ${line}`);
}
}
}
lines.push("");
const hint = editing
? " Enter to submit • Shift+Enter for newline • Esc to stop editing"
: " ↑↓ navigate • Enter to submit • Tab to edit • 1-9 quick select • Esc to skip";
add(theme.fg("dim", hint));
add(theme.fg("accent", "─".repeat(width)));
cachedLines = lines;
return lines;
}
return {
render,
invalidate: () => { cachedLines = undefined; },
handleInput,
};
},
);
}