@juicesharp/rpiv-ask-user-question 0.13.1 → 0.13.3
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 +2 -2
- package/ask-user-question.ts +19 -567
- package/dialog-builder.ts +60 -187
- package/dispatch.ts +5 -32
- package/format-answer.ts +39 -0
- package/markdown-content-cache.ts +76 -0
- package/multi-select-options.ts +3 -2
- package/package.json +20 -12
- package/preview-box-renderer.ts +86 -0
- package/preview-layout-decider.ts +49 -0
- package/preview-pane.ts +100 -240
- package/questionnaire-session.ts +549 -0
- package/questionnaire-state.ts +83 -0
- package/response-envelope.ts +47 -0
- package/stateful-component.ts +20 -0
- package/submit-picker.ts +3 -2
- package/tab-strategy.ts +190 -0
- package/validate-questionnaire.ts +58 -0
- package/wrapping-select.ts +46 -13
- package/columns.ts +0 -71
- package/fixed-height-box.ts +0 -32
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
Let the model ask you structured clarifying questions instead of guessing. `rpiv-ask-user-question` adds the `ask_user_question` tool to [Pi Agent](https://github.com/badlogic/pi-mono) — a tabbed dialog with single- and multi-select questions, side-by-side previews, per-option notes, and a Submit tab that reviews answers before they go back to the model.
|
|
7
7
|
|
|
8
|
-

|
|
9
9
|
|
|
10
10
|
## Features
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ Let the model ask you structured clarifying questions instead of guessing. `rpiv
|
|
|
21
21
|
|
|
22
22
|
| | |
|
|
23
23
|
|---|---|
|
|
24
|
-
|  |  |
|
|
25
25
|
|  |  |
|
|
26
26
|
|
|
27
27
|
## Install
|
package/ask-user-question.ts
CHANGED
|
@@ -1,62 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { handleQuestionnaireInput, type QuestionnaireDispatchState } from "./dispatch.js";
|
|
5
|
-
import { MultiSelectOptions } from "./multi-select-options.js";
|
|
6
|
-
import { PreviewPane } from "./preview-pane.js";
|
|
7
|
-
import { SubmitPicker } from "./submit-picker.js";
|
|
8
|
-
import { TabBar } from "./tab-bar.js";
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { QuestionnaireSession } from "./questionnaire-session.js";
|
|
3
|
+
import { buildQuestionnaireResponse, buildToolResult } from "./response-envelope.js";
|
|
9
4
|
import {
|
|
10
5
|
MAX_OPTIONS,
|
|
11
6
|
MAX_QUESTIONS,
|
|
12
7
|
MIN_OPTIONS,
|
|
13
|
-
type QuestionAnswer,
|
|
14
8
|
type QuestionData,
|
|
15
9
|
type QuestionnaireResult,
|
|
16
10
|
type QuestionParams,
|
|
17
11
|
QuestionParamsSchema,
|
|
18
|
-
RESERVED_LABELS,
|
|
19
12
|
} from "./types.js";
|
|
20
|
-
import {
|
|
13
|
+
import { validateQuestionnaire } from "./validate-questionnaire.js";
|
|
14
|
+
import type { WrappingSelectItem } from "./wrapping-select.js";
|
|
21
15
|
|
|
22
16
|
const TYPE_SOMETHING_LABEL = "Type something.";
|
|
23
|
-
const CHAT_ABOUT_THIS_LABEL = "Chat about this";
|
|
24
17
|
const NEXT_LABEL = "Next";
|
|
25
|
-
const DECLINE_MESSAGE = "User declined to answer questions";
|
|
26
|
-
const CHAT_CONTINUATION_MESSAGE = "User wants to chat about this. Continue the conversation to help them decide.";
|
|
27
|
-
const NO_INPUT_PLACEHOLDER = "(no input)";
|
|
28
18
|
const ERROR_NO_UI = "Error: UI not available (running in non-interactive mode)";
|
|
29
|
-
const ERROR_TOO_FEW_OPTIONS = `Error: Each question requires at least ${MIN_OPTIONS} options`;
|
|
30
|
-
const ERROR_TOO_MANY_QUESTIONS = `Error: At most ${MAX_QUESTIONS} questions are allowed per invocation`;
|
|
31
|
-
const ERROR_NO_QUESTIONS = "Error: At least one question is required";
|
|
32
|
-
const ERROR_DUPLICATE_QUESTION = "Error: Question text must be unique within an invocation";
|
|
33
|
-
const ERROR_DUPLICATE_OPTION_LABEL = "Error: Option labels must be unique within a question";
|
|
34
|
-
const ERROR_RESERVED_LABEL = `Error: Option label is reserved (${RESERVED_LABELS.join(", ")})`;
|
|
35
|
-
const ENVELOPE_PREFIX = "User has answered your questions:";
|
|
36
|
-
const ENVELOPE_SUFFIX = "You can now continue with the user's answers in mind.";
|
|
37
19
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const BACKSPACE_CHARS = new Set(["\x7f", "\b"]);
|
|
41
|
-
const ESC_SEQUENCE_PREFIX = "\x1b";
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Numbering for the chat row's WrappingSelect, computed from the active tab's items.
|
|
45
|
-
*
|
|
46
|
-
* The chat row lives in its own one-item WrappingSelect; the host calls this on every tab
|
|
47
|
-
* switch / selection update to keep the chat row's `N. ` label continuous with the visible
|
|
48
|
-
* numbered rows of the active tab. The shape `{ offset, total }` mirrors `WrappingSelect.setNumbering(numberStartOffset, totalItemsForNumbering)` directly.
|
|
49
|
-
*/
|
|
50
|
-
export function chatNumberingFor(items: readonly WrappingSelectItem[]): {
|
|
51
|
-
offset: number;
|
|
52
|
-
total: number;
|
|
53
|
-
} {
|
|
54
|
-
// Count only the visible-numbered rows. The Next sentinel renders without a number
|
|
55
|
-
// (see MultiSelectOptions), so it must NOT advance the chat row's number — otherwise
|
|
56
|
-
// chat reads as "6." next to options labeled 1-4.
|
|
57
|
-
const count = items.filter((i) => !i.isNext).length;
|
|
58
|
-
return { offset: count, total: count + 1 };
|
|
59
|
-
}
|
|
20
|
+
export { chatNumberingFor } from "./questionnaire-state.js";
|
|
60
21
|
|
|
61
22
|
export function buildItemsForQuestion(question: QuestionData): WrappingSelectItem[] {
|
|
62
23
|
const items = question.options.map((o) => ({ label: o.label, description: o.description }));
|
|
@@ -107,501 +68,27 @@ Preview content is rendered as markdown in a monospace box. Multi-line text with
|
|
|
107
68
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
108
69
|
const typed = params as unknown as QuestionParams;
|
|
109
70
|
if (!ctx.hasUI) return buildToolResult(ERROR_NO_UI, { answers: [], cancelled: true, error: "no_ui" });
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
return buildToolResult(
|
|
71
|
+
|
|
72
|
+
const validation = validateQuestionnaire(typed);
|
|
73
|
+
if (!validation.ok) {
|
|
74
|
+
return buildToolResult(validation.message, {
|
|
114
75
|
answers: [],
|
|
115
76
|
cancelled: true,
|
|
116
|
-
error:
|
|
77
|
+
error: validation.error,
|
|
117
78
|
});
|
|
118
|
-
|
|
119
|
-
const seenQuestions = new Set<string>();
|
|
120
|
-
for (const q of typed.questions) {
|
|
121
|
-
if (seenQuestions.has(q.question)) {
|
|
122
|
-
return buildToolResult(ERROR_DUPLICATE_QUESTION, {
|
|
123
|
-
answers: [],
|
|
124
|
-
cancelled: true,
|
|
125
|
-
error: "duplicate_question",
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
seenQuestions.add(q.question);
|
|
129
79
|
}
|
|
130
80
|
|
|
131
|
-
|
|
132
|
-
if (q.options.length < MIN_OPTIONS)
|
|
133
|
-
return buildToolResult(ERROR_TOO_FEW_OPTIONS, {
|
|
134
|
-
answers: [],
|
|
135
|
-
cancelled: true,
|
|
136
|
-
error: "empty_options",
|
|
137
|
-
});
|
|
138
|
-
const seenLabels = new Set<string>();
|
|
139
|
-
for (const o of q.options) {
|
|
140
|
-
if (RESERVED_LABEL_SET.has(o.label)) {
|
|
141
|
-
return buildToolResult(ERROR_RESERVED_LABEL, {
|
|
142
|
-
answers: [],
|
|
143
|
-
cancelled: true,
|
|
144
|
-
error: "reserved_label",
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
if (seenLabels.has(o.label)) {
|
|
148
|
-
return buildToolResult(ERROR_DUPLICATE_OPTION_LABEL, {
|
|
149
|
-
answers: [],
|
|
150
|
-
cancelled: true,
|
|
151
|
-
error: "duplicate_option_label",
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
seenLabels.add(o.label);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const questions = typed.questions;
|
|
159
|
-
const isMulti = questions.length > 1;
|
|
160
|
-
const itemsByTab: WrappingSelectItem[][] = questions.map((q) => buildItemsForQuestion(q));
|
|
81
|
+
const itemsByTab: WrappingSelectItem[][] = typed.questions.map((q) => buildItemsForQuestion(q));
|
|
161
82
|
|
|
162
83
|
const result = await ctx.ui.custom<QuestionnaireResult>((tui, theme, _kb, done) => {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
description: (t) => theme.fg("muted", t),
|
|
166
|
-
scrollInfo: (t) => theme.fg("dim", t),
|
|
167
|
-
};
|
|
168
|
-
// Chat row lives in its own one-item WrappingSelect. Its `numberStartOffset` /
|
|
169
|
-
// `totalItemsForNumbering` are updated on every tab switch (see `applySelection` below)
|
|
170
|
-
// so the chat row renders as `(N+1). Chat about this`, where N is the active tab's
|
|
171
|
-
// items.length — i.e. continuous numbering across the divider, never a stale `1.`.
|
|
172
|
-
const chatList = new WrappingSelect([{ label: CHAT_ABOUT_THIS_LABEL, isChat: true }], 1, selectTheme);
|
|
173
|
-
const notesInput = new Input();
|
|
174
|
-
const markdownTheme = getMarkdownTheme();
|
|
175
|
-
const getTerminalWidth = () => tui.terminal.columns;
|
|
176
|
-
|
|
177
|
-
const previewPanes: PreviewPane[] = questions.map(
|
|
178
|
-
(q, i) =>
|
|
179
|
-
new PreviewPane({
|
|
180
|
-
items: itemsByTab[i],
|
|
181
|
-
question: q,
|
|
182
|
-
theme,
|
|
183
|
-
markdownTheme,
|
|
184
|
-
getTerminalWidth,
|
|
185
|
-
}),
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
const initialDialogState: DialogState = {
|
|
189
|
-
currentTab: 0,
|
|
190
|
-
optionIndex: 0,
|
|
191
|
-
notesVisible: false,
|
|
192
|
-
inputMode: false,
|
|
193
|
-
answers: new Map(),
|
|
194
|
-
multiSelectChecked: new Set(),
|
|
195
|
-
focusedOptionHasPreview: false,
|
|
196
|
-
submitChoiceIndex: 0,
|
|
197
|
-
};
|
|
198
|
-
const multiSelectOptionsByTab: ReadonlyArray<MultiSelectOptions | undefined> = questions.map((q) =>
|
|
199
|
-
q.multiSelect ? new MultiSelectOptions(theme, q, initialDialogState) : undefined,
|
|
200
|
-
);
|
|
201
|
-
// Submit-tab Submit/Cancel picker. Multi-question only — single-question dialogs
|
|
202
|
-
// have no Submit Tab so the picker is undefined.
|
|
203
|
-
const submitPicker = isMulti ? new SubmitPicker(theme, initialDialogState) : undefined;
|
|
204
|
-
|
|
205
|
-
const tabBar: TabBar | undefined = isMulti
|
|
206
|
-
? new TabBar(
|
|
207
|
-
{
|
|
208
|
-
questions,
|
|
209
|
-
answers: new Map(),
|
|
210
|
-
activeTabIndex: 0,
|
|
211
|
-
totalTabs: questions.length + 1,
|
|
212
|
-
},
|
|
213
|
-
theme,
|
|
214
|
-
)
|
|
215
|
-
: undefined;
|
|
216
|
-
|
|
217
|
-
let currentTab = 0;
|
|
218
|
-
let optionIndex = 0;
|
|
219
|
-
let inputMode = false;
|
|
220
|
-
let notesMode = false;
|
|
221
|
-
let chatFocused = false;
|
|
222
|
-
let submitChoiceIndex = 0;
|
|
223
|
-
const answers = new Map<number, QuestionAnswer>();
|
|
224
|
-
let multiSelectChecked = new Set<number>();
|
|
225
|
-
// notesByTab: transient pre-answer notes side-map. Decoupled from `answers` so adding notes
|
|
226
|
-
// does NOT make `answers.has(currentTab)` true (otherwise Submit-tab missing-check + allAnswered()
|
|
227
|
-
// would falsely report the question as answered). Merged into the answer at confirm time.
|
|
228
|
-
const notesByTab = new Map<number, string>();
|
|
229
|
-
|
|
230
|
-
const items = (): WrappingSelectItem[] => itemsByTab[currentTab] ?? [];
|
|
231
|
-
const currentItem = (): WrappingSelectItem | undefined => {
|
|
232
|
-
if (chatFocused) return { label: CHAT_ABOUT_THIS_LABEL, isChat: true };
|
|
233
|
-
const arr = items();
|
|
234
|
-
if (optionIndex < arr.length) return arr[optionIndex];
|
|
235
|
-
return { label: CHAT_ABOUT_THIS_LABEL, isChat: true };
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
// Mode-agnostic: just checks "does the currently-focused option carry a preview?".
|
|
239
|
-
// Mode gates (chat focus, input mode, notes mode, multiSelect) layer on top via
|
|
240
|
-
// existing dispatch branches.
|
|
241
|
-
function computeFocusedOptionHasPreview(): boolean {
|
|
242
|
-
const q = questions[currentTab];
|
|
243
|
-
if (!q) return false;
|
|
244
|
-
const opt = q.options[optionIndex];
|
|
245
|
-
return !!opt && typeof opt.preview === "string" && opt.preview.length > 0;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function snapshotState(): DialogState {
|
|
249
|
-
return {
|
|
250
|
-
currentTab,
|
|
251
|
-
optionIndex,
|
|
252
|
-
notesVisible: notesMode,
|
|
253
|
-
inputMode,
|
|
254
|
-
answers: new Map(answers),
|
|
255
|
-
multiSelectChecked: new Set(multiSelectChecked),
|
|
256
|
-
focusedOptionHasPreview: computeFocusedOptionHasPreview(),
|
|
257
|
-
submitChoiceIndex,
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Worst case across all tabs and all options — drives the stable dialog footprint.
|
|
262
|
-
function computeGlobalContentHeight(width: number): number {
|
|
263
|
-
let max = 0;
|
|
264
|
-
for (let i = 0; i < questions.length; i++) {
|
|
265
|
-
const q = questions[i];
|
|
266
|
-
const h = q?.multiSelect
|
|
267
|
-
? (multiSelectOptionsByTab[i]?.naturalHeight(width) ?? 0)
|
|
268
|
-
: (previewPanes[i]?.maxNaturalHeight(width) ?? 0);
|
|
269
|
-
if (h > max) max = h;
|
|
270
|
-
}
|
|
271
|
-
return Math.max(1, max);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// Body height of the currently-active tab/option — drives the residual spacer
|
|
275
|
-
// so the bordered region hugs the actual content with no internal `""` padding.
|
|
276
|
-
function computeCurrentContentHeight(width: number): number {
|
|
277
|
-
const idx = Math.min(currentTab, questions.length - 1);
|
|
278
|
-
const q = questions[idx];
|
|
279
|
-
if (!q) return 0;
|
|
280
|
-
const h = q.multiSelect
|
|
281
|
-
? (multiSelectOptionsByTab[idx]?.naturalHeight(width) ?? 0)
|
|
282
|
-
: (previewPanes[idx]?.naturalHeight(width) ?? 0);
|
|
283
|
-
return Math.max(0, h);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const dialog = buildDialog({
|
|
84
|
+
const session = new QuestionnaireSession({
|
|
85
|
+
tui,
|
|
287
86
|
theme,
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
tabBar,
|
|
292
|
-
notesInput,
|
|
293
|
-
chatList,
|
|
294
|
-
isMulti,
|
|
295
|
-
multiSelectOptionsByTab,
|
|
296
|
-
submitPicker,
|
|
297
|
-
getBodyHeight: (w) => computeGlobalContentHeight(w),
|
|
298
|
-
getCurrentBodyHeight: (w) => computeCurrentContentHeight(w),
|
|
87
|
+
params: typed,
|
|
88
|
+
itemsByTab,
|
|
89
|
+
done,
|
|
299
90
|
});
|
|
300
|
-
|
|
301
|
-
const component = {
|
|
302
|
-
render: (w: number) => dialog.render(w),
|
|
303
|
-
invalidate: () => dialog.invalidate(),
|
|
304
|
-
handleInput: (data: string) => onInput(data),
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
function refreshDialog() {
|
|
308
|
-
dialog.setState(snapshotState());
|
|
309
|
-
applySelection();
|
|
310
|
-
tui.requestRender();
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function applySelection() {
|
|
314
|
-
const pane = previewPanes[Math.min(currentTab, questions.length - 1)] ?? previewPanes[0];
|
|
315
|
-
pane.setSelectedIndex(optionIndex);
|
|
316
|
-
const optionsFocused = !notesMode && !chatFocused;
|
|
317
|
-
pane.setFocused(optionsFocused);
|
|
318
|
-
pane.setNotesVisible(notesMode);
|
|
319
|
-
// Mirror focus into the multi-select renderer too, otherwise its active-row pointer
|
|
320
|
-
// stays drawn while the user is on the chat row / notes input, producing a doubled
|
|
321
|
-
// cursor (`❯ ☑ HTMX` AND `❯ Chat about this` lit at the same time).
|
|
322
|
-
for (const mso of multiSelectOptionsByTab) mso?.setFocused(optionsFocused);
|
|
323
|
-
chatList.setFocused(chatFocused);
|
|
324
|
-
// Keep the chat row's number aligned with whichever tab is active. The chat row
|
|
325
|
-
// is always the (last + 1) entry in the logical numbering for that tab.
|
|
326
|
-
const activeTabItems = itemsByTab[Math.min(currentTab, questions.length - 1)] ?? [];
|
|
327
|
-
const chatNumbering = chatNumberingFor(activeTabItems);
|
|
328
|
-
chatList.setNumbering(chatNumbering.offset, chatNumbering.total);
|
|
329
|
-
// Multi-select rows now carry their own `N.` numbers (CC parity), so the chat row
|
|
330
|
-
// continues that contiguous numbering on every tab — no special-case suppression.
|
|
331
|
-
chatList.setShowNumbering(true);
|
|
332
|
-
if (submitPicker) {
|
|
333
|
-
submitPicker.setState(snapshotState());
|
|
334
|
-
submitPicker.setFocused(currentTab === questions.length);
|
|
335
|
-
}
|
|
336
|
-
if (tabBar) {
|
|
337
|
-
tabBar.setConfig({
|
|
338
|
-
questions,
|
|
339
|
-
answers: new Map(answers),
|
|
340
|
-
activeTabIndex: currentTab,
|
|
341
|
-
totalTabs: questions.length + 1,
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Mirror the current multi-select toggle state into `answers` so it survives tab
|
|
348
|
-
* switches and surfaces in the Submit-tab summary even before the user explicitly
|
|
349
|
-
* commits via `Enter` on the Next sentinel. Called from the `toggle` handler.
|
|
350
|
-
*
|
|
351
|
-
* Conventions:
|
|
352
|
-
* - Non-empty selection → write `{ selected: [labels in option order] }`. Pending
|
|
353
|
-
* notes from `notesByTab` are merged in so a tab-away doesn't drop them.
|
|
354
|
-
* - Empty selection → delete the entry. Distinguishes "I haven't decided yet"
|
|
355
|
-
* (no answer) from "I want to commit empty" (Enter on Next, which always writes).
|
|
356
|
-
*/
|
|
357
|
-
function persistMultiSelectAnswer() {
|
|
358
|
-
const q = questions[currentTab];
|
|
359
|
-
if (!q?.multiSelect) return;
|
|
360
|
-
const selected: string[] = [];
|
|
361
|
-
for (let i = 0; i < q.options.length; i++) {
|
|
362
|
-
if (multiSelectChecked.has(i)) selected.push(q.options[i].label);
|
|
363
|
-
}
|
|
364
|
-
if (selected.length === 0) {
|
|
365
|
-
answers.delete(currentTab);
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const pendingNotes = notesByTab.get(currentTab);
|
|
369
|
-
answers.set(currentTab, {
|
|
370
|
-
questionIndex: currentTab,
|
|
371
|
-
question: q.question,
|
|
372
|
-
answer: null,
|
|
373
|
-
selected,
|
|
374
|
-
...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
function syncMultiSelectFromAnswers() {
|
|
379
|
-
const q = questions[currentTab];
|
|
380
|
-
if (!q?.multiSelect) {
|
|
381
|
-
multiSelectChecked = new Set();
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
const saved = answers.get(currentTab);
|
|
385
|
-
const labels = saved?.selected ?? [];
|
|
386
|
-
const indices = new Set<number>();
|
|
387
|
-
for (let i = 0; i < q.options.length; i++) {
|
|
388
|
-
if (labels.includes(q.options[i].label)) indices.add(i);
|
|
389
|
-
}
|
|
390
|
-
multiSelectChecked = indices;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
function switchTab(nextTab: number) {
|
|
394
|
-
currentTab = nextTab;
|
|
395
|
-
optionIndex = 0;
|
|
396
|
-
inputMode = false;
|
|
397
|
-
notesMode = false;
|
|
398
|
-
chatFocused = false;
|
|
399
|
-
submitChoiceIndex = 0;
|
|
400
|
-
notesInput.focused = false;
|
|
401
|
-
notesInput.setValue(notesByTab.get(currentTab) ?? answers.get(currentTab)?.notes ?? "");
|
|
402
|
-
syncMultiSelectFromAnswers();
|
|
403
|
-
const paneIndex = Math.min(currentTab, questions.length - 1);
|
|
404
|
-
const nextPane = previewPanes[paneIndex] ?? previewPanes[0];
|
|
405
|
-
dialog.setPreviewPane(nextPane);
|
|
406
|
-
dialog.setState(snapshotState());
|
|
407
|
-
refreshDialog();
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
function submitFinal() {
|
|
411
|
-
done({ answers: orderedAnswers(), cancelled: false });
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
function cancel() {
|
|
415
|
-
done({ answers: orderedAnswers(), cancelled: true });
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
function orderedAnswers(): QuestionAnswer[] {
|
|
419
|
-
const out: QuestionAnswer[] = [];
|
|
420
|
-
for (let i = 0; i < questions.length; i++) {
|
|
421
|
-
const a = answers.get(i);
|
|
422
|
-
if (a) out.push(a);
|
|
423
|
-
}
|
|
424
|
-
return out;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function onInput(data: string) {
|
|
428
|
-
if (notesMode) {
|
|
429
|
-
const preAction = handleQuestionnaireInput(data, dispatchSnapshot());
|
|
430
|
-
if (preAction.kind === "notes_exit") {
|
|
431
|
-
commitNotes();
|
|
432
|
-
notesMode = false;
|
|
433
|
-
notesInput.focused = false;
|
|
434
|
-
refreshDialog();
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
|
-
notesInput.handleInput(data);
|
|
438
|
-
tui.requestRender();
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
const action = handleQuestionnaireInput(data, dispatchSnapshot());
|
|
443
|
-
switch (action.kind) {
|
|
444
|
-
case "nav":
|
|
445
|
-
optionIndex = action.nextIndex;
|
|
446
|
-
inputMode = !!currentItem()?.isOther;
|
|
447
|
-
if (!inputMode) {
|
|
448
|
-
previewPanes[currentTab]?.clearInputBuffer();
|
|
449
|
-
}
|
|
450
|
-
refreshDialog();
|
|
451
|
-
return;
|
|
452
|
-
case "tab_switch":
|
|
453
|
-
switchTab(action.nextTab);
|
|
454
|
-
return;
|
|
455
|
-
case "confirm": {
|
|
456
|
-
let answer = action.answer;
|
|
457
|
-
if (!answer.wasChat && !answer.wasCustom && answer.answer) {
|
|
458
|
-
const q = questions[answer.questionIndex];
|
|
459
|
-
const matched = q?.options.find((o) => o.label === answer.answer);
|
|
460
|
-
if (matched?.preview && matched.preview.length > 0) {
|
|
461
|
-
answer = { ...answer, preview: matched.preview };
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
const pendingNotes = notesByTab.get(answer.questionIndex);
|
|
465
|
-
if (pendingNotes && pendingNotes.length > 0) {
|
|
466
|
-
answer = { ...answer, notes: pendingNotes };
|
|
467
|
-
}
|
|
468
|
-
answers.set(answer.questionIndex, answer);
|
|
469
|
-
if (action.autoAdvanceTab !== undefined) {
|
|
470
|
-
switchTab(action.autoAdvanceTab);
|
|
471
|
-
} else {
|
|
472
|
-
submitFinal();
|
|
473
|
-
}
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
case "toggle":
|
|
477
|
-
if (multiSelectChecked.has(action.index)) multiSelectChecked.delete(action.index);
|
|
478
|
-
else multiSelectChecked.add(action.index);
|
|
479
|
-
// Persist on every toggle so tab-switching away (without Enter on Next) doesn't
|
|
480
|
-
// drop the in-progress selection. The Submit-tab summary + tab-back restore both
|
|
481
|
-
// read from `answers`, so this single write keeps both views consistent.
|
|
482
|
-
persistMultiSelectAnswer();
|
|
483
|
-
refreshDialog();
|
|
484
|
-
return;
|
|
485
|
-
case "multi_confirm": {
|
|
486
|
-
const q = questions[currentTab];
|
|
487
|
-
if (!q) return;
|
|
488
|
-
const pendingNotes = notesByTab.get(currentTab);
|
|
489
|
-
answers.set(currentTab, {
|
|
490
|
-
questionIndex: currentTab,
|
|
491
|
-
question: q.question,
|
|
492
|
-
answer: null,
|
|
493
|
-
selected: action.selected,
|
|
494
|
-
...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
|
|
495
|
-
});
|
|
496
|
-
syncMultiSelectFromAnswers();
|
|
497
|
-
// Mirror the single-select `confirm` lifecycle: advance to the next tab in
|
|
498
|
-
// multi-question mode, OR submit the dialog in single-question mode
|
|
499
|
-
// (autoAdvanceTab === undefined when !isMulti).
|
|
500
|
-
if (action.autoAdvanceTab !== undefined) {
|
|
501
|
-
switchTab(action.autoAdvanceTab);
|
|
502
|
-
} else {
|
|
503
|
-
submitFinal();
|
|
504
|
-
}
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
case "cancel":
|
|
508
|
-
cancel();
|
|
509
|
-
return;
|
|
510
|
-
case "notes_enter":
|
|
511
|
-
notesMode = true;
|
|
512
|
-
notesInput.focused = true;
|
|
513
|
-
notesInput.setValue(answers.get(currentTab)?.notes ?? "");
|
|
514
|
-
refreshDialog();
|
|
515
|
-
return;
|
|
516
|
-
case "notes_exit":
|
|
517
|
-
commitNotes();
|
|
518
|
-
notesMode = false;
|
|
519
|
-
notesInput.focused = false;
|
|
520
|
-
refreshDialog();
|
|
521
|
-
return;
|
|
522
|
-
case "focus_chat":
|
|
523
|
-
chatFocused = true;
|
|
524
|
-
refreshDialog();
|
|
525
|
-
return;
|
|
526
|
-
case "focus_options":
|
|
527
|
-
chatFocused = false;
|
|
528
|
-
// When the dispatcher carries a target index, update optionIndex too — that
|
|
529
|
-
// drives the continuous chat ↔ options cycle (DOWN-from-chat → 0,
|
|
530
|
-
// UP-from-chat → items.length - 1). Without `optionIndex`, focus simply
|
|
531
|
-
// returns to wherever the user was (legacy contract preserved).
|
|
532
|
-
if (action.optionIndex !== undefined) {
|
|
533
|
-
optionIndex = action.optionIndex;
|
|
534
|
-
inputMode = !!currentItem()?.isOther;
|
|
535
|
-
if (!inputMode) {
|
|
536
|
-
previewPanes[currentTab]?.clearInputBuffer();
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
refreshDialog();
|
|
540
|
-
return;
|
|
541
|
-
case "submit_nav":
|
|
542
|
-
submitChoiceIndex = action.nextIndex;
|
|
543
|
-
refreshDialog();
|
|
544
|
-
return;
|
|
545
|
-
case "submit":
|
|
546
|
-
submitFinal();
|
|
547
|
-
return;
|
|
548
|
-
case "ignore":
|
|
549
|
-
if (inputMode) {
|
|
550
|
-
const pane = previewPanes[currentTab];
|
|
551
|
-
if (!pane) return;
|
|
552
|
-
if (BACKSPACE_CHARS.has(data)) {
|
|
553
|
-
pane.backspaceInput();
|
|
554
|
-
tui.requestRender();
|
|
555
|
-
} else if (data && !data.startsWith(ESC_SEQUENCE_PREFIX)) {
|
|
556
|
-
pane.appendInput(data);
|
|
557
|
-
tui.requestRender();
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
function commitNotes() {
|
|
565
|
-
const trimmed = notesInput.getValue().trim();
|
|
566
|
-
if (!questions[currentTab]) return;
|
|
567
|
-
if (trimmed.length === 0) {
|
|
568
|
-
notesByTab.delete(currentTab);
|
|
569
|
-
const prev = answers.get(currentTab);
|
|
570
|
-
if (prev?.notes) {
|
|
571
|
-
const next = { ...prev };
|
|
572
|
-
delete (next as { notes?: string }).notes;
|
|
573
|
-
answers.set(currentTab, next);
|
|
574
|
-
}
|
|
575
|
-
return;
|
|
576
|
-
}
|
|
577
|
-
notesByTab.set(currentTab, trimmed);
|
|
578
|
-
const prev = answers.get(currentTab);
|
|
579
|
-
if (prev) answers.set(currentTab, { ...prev, notes: trimmed });
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
function dispatchSnapshot(): QuestionnaireDispatchState {
|
|
583
|
-
return {
|
|
584
|
-
currentTab,
|
|
585
|
-
optionIndex,
|
|
586
|
-
inputMode,
|
|
587
|
-
notesMode,
|
|
588
|
-
chatFocused,
|
|
589
|
-
answers,
|
|
590
|
-
multiSelectIndices: multiSelectChecked,
|
|
591
|
-
questions,
|
|
592
|
-
isMulti,
|
|
593
|
-
keybindings: getKeybindings(),
|
|
594
|
-
currentItem: currentItem(),
|
|
595
|
-
inputBuffer: previewPanes[currentTab]?.getInputBuffer() ?? "",
|
|
596
|
-
items: items(),
|
|
597
|
-
focusedOptionHasPreview: computeFocusedOptionHasPreview(),
|
|
598
|
-
submitChoiceIndex,
|
|
599
|
-
};
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
applySelection();
|
|
603
|
-
dialog.setState(snapshotState());
|
|
604
|
-
return component;
|
|
91
|
+
return session.component;
|
|
605
92
|
});
|
|
606
93
|
|
|
607
94
|
return buildQuestionnaireResponse(result, typed);
|
|
@@ -609,39 +96,4 @@ Preview content is rendered as markdown in a monospace box. Multi-line text with
|
|
|
609
96
|
});
|
|
610
97
|
}
|
|
611
98
|
|
|
612
|
-
export
|
|
613
|
-
if (!result || result.cancelled) {
|
|
614
|
-
return buildToolResult(DECLINE_MESSAGE, {
|
|
615
|
-
answers: result?.answers ?? [],
|
|
616
|
-
cancelled: true,
|
|
617
|
-
});
|
|
618
|
-
}
|
|
619
|
-
const segments: string[] = [];
|
|
620
|
-
for (let i = 0; i < params.questions.length; i++) {
|
|
621
|
-
const a = result.answers.find((x) => x.questionIndex === i);
|
|
622
|
-
if (a) segments.push(buildAnswerSegment(a));
|
|
623
|
-
}
|
|
624
|
-
if (segments.length === 0) return buildToolResult(DECLINE_MESSAGE, { answers: result.answers, cancelled: true });
|
|
625
|
-
return buildToolResult(`${ENVELOPE_PREFIX} ${segments.join(" ")} ${ENVELOPE_SUFFIX}`, result);
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function buildAnswerSegment(a: QuestionAnswer): string {
|
|
629
|
-
const parts: string[] = [`"${a.question}"="${formatAnswerScalar(a)}"`];
|
|
630
|
-
if (a.preview && a.preview.length > 0) parts.push(`selected preview: ${a.preview}`);
|
|
631
|
-
if (a.notes && a.notes.length > 0) parts.push(`user notes: ${a.notes}`);
|
|
632
|
-
return `${parts.join(". ")}.`;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
function formatAnswerScalar(a: QuestionAnswer): string {
|
|
636
|
-
if (a.wasChat) return CHAT_CONTINUATION_MESSAGE;
|
|
637
|
-
if (a.selected && a.selected.length > 0) return a.selected.join(", ");
|
|
638
|
-
if (a.wasCustom) return a.answer && a.answer.length > 0 ? a.answer : NO_INPUT_PLACEHOLDER;
|
|
639
|
-
return a.answer ?? NO_INPUT_PLACEHOLDER;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
export function buildToolResult(text: string, details: QuestionnaireResult) {
|
|
643
|
-
return {
|
|
644
|
-
content: [{ type: "text" as const, text }],
|
|
645
|
-
details,
|
|
646
|
-
};
|
|
647
|
-
}
|
|
99
|
+
export { buildQuestionnaireResponse, buildToolResult };
|