@juicesharp/rpiv-ask-user-question 1.0.2 → 1.0.4
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/apply-action.ts +276 -0
- package/ask-user-question.ts +9 -6
- package/dispatch.ts +9 -6
- package/format-answer.ts +15 -8
- package/multi-select-options.ts +4 -3
- package/option-list-view.ts +93 -0
- package/package.json +5 -1
- package/preview-block-renderer.ts +106 -0
- package/preview-pane.ts +58 -141
- package/questionnaire-session.ts +33 -24
- package/questionnaire-state.ts +7 -7
- package/types.ts +41 -9
- package/view-adapter.ts +133 -0
- package/wrapping-select.ts +27 -21
package/preview-pane.ts
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE,
|
|
5
|
-
MAX_PREVIEW_HEIGHT_STACKED,
|
|
6
|
-
MarkdownContentCache,
|
|
7
|
-
NOTES_AFFORDANCE_OVERHEAD,
|
|
8
|
-
} from "./markdown-content-cache.js";
|
|
9
|
-
import {
|
|
10
|
-
BORDER_HORIZONTAL_OVERHEAD,
|
|
11
|
-
BORDER_INNER_PADDING_HORIZONTAL,
|
|
12
|
-
BORDER_VERTICAL_OVERHEAD,
|
|
13
|
-
computeBoxDimensions,
|
|
14
|
-
renderBorderedBox,
|
|
15
|
-
} from "./preview-box-renderer.js";
|
|
1
|
+
import { type Component, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
2
|
+
import type { OptionListView } from "./option-list-view.js";
|
|
3
|
+
import type { PreviewBlockRenderer } from "./preview-block-renderer.js";
|
|
16
4
|
import {
|
|
17
5
|
bodyWidths,
|
|
18
6
|
columnWidths,
|
|
@@ -22,15 +10,15 @@ import {
|
|
|
22
10
|
STACKED_GAP_ROWS,
|
|
23
11
|
} from "./preview-layout-decider.js";
|
|
24
12
|
import type { QuestionData } from "./types.js";
|
|
25
|
-
import { WrappingSelect, type WrappingSelectItem, type WrappingSelectTheme } from "./wrapping-select.js";
|
|
26
13
|
|
|
14
|
+
// ----- Re-exports for test imports — keep `./preview-pane.js` as the public surface -----
|
|
27
15
|
export {
|
|
28
16
|
MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE,
|
|
29
17
|
MAX_PREVIEW_HEIGHT_STACKED,
|
|
30
18
|
NO_PREVIEW_TEXT,
|
|
31
19
|
NOTES_AFFORDANCE_OVERHEAD,
|
|
32
20
|
} from "./markdown-content-cache.js";
|
|
33
|
-
|
|
21
|
+
export { NOTES_AFFORDANCE_TEXT, PreviewBlockRenderer } from "./preview-block-renderer.js";
|
|
34
22
|
export {
|
|
35
23
|
BORDER_HORIZONTAL_OVERHEAD,
|
|
36
24
|
BORDER_INNER_PADDING_HORIZONTAL,
|
|
@@ -47,158 +35,103 @@ export {
|
|
|
47
35
|
STACKED_GAP_ROWS,
|
|
48
36
|
} from "./preview-layout-decider.js";
|
|
49
37
|
|
|
50
|
-
// ----- Constants owned by the composer itself -----
|
|
51
|
-
export const MAX_VISIBLE_OPTIONS = 10;
|
|
52
|
-
/** Affordance text shown below the bordered preview when focused on a preview-bearing option. */
|
|
53
|
-
export const NOTES_AFFORDANCE_TEXT = "Notes: press n to add notes";
|
|
54
|
-
|
|
55
38
|
export interface PreviewPaneConfig {
|
|
56
|
-
items: readonly WrappingSelectItem[];
|
|
57
39
|
question: QuestionData;
|
|
58
|
-
theme: Theme;
|
|
59
|
-
markdownTheme: MarkdownTheme;
|
|
60
40
|
getTerminalWidth: () => number;
|
|
41
|
+
optionListView: OptionListView;
|
|
42
|
+
previewBlock: PreviewBlockRenderer;
|
|
61
43
|
}
|
|
62
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Thin layout composer. Owns one local field — `notesVisible`. Delegates option-side rendering
|
|
47
|
+
* to `OptionListView` (which owns `selectedIndex`, `focused`, input buffer, confirmedIndex) and
|
|
48
|
+
* preview-side rendering to `PreviewBlockRenderer` (which owns the markdown cache and bordered-
|
|
49
|
+
* box composition).
|
|
50
|
+
*
|
|
51
|
+
* `naturalHeight` and `maxNaturalHeight` query both children's heights; `render` combines them
|
|
52
|
+
* via `decideLayout` (mode threaded into both calls — never re-derived).
|
|
53
|
+
*/
|
|
63
54
|
export class PreviewPane implements Component {
|
|
64
55
|
private readonly question: QuestionData;
|
|
65
|
-
private readonly theme: Theme;
|
|
66
56
|
private readonly getTerminalWidth: () => number;
|
|
67
|
-
private readonly
|
|
68
|
-
private readonly
|
|
69
|
-
private selectedIndex = 0;
|
|
70
|
-
private focused = false;
|
|
57
|
+
private readonly optionListView: OptionListView;
|
|
58
|
+
private readonly previewBlock: PreviewBlockRenderer;
|
|
71
59
|
private notesVisible = false;
|
|
72
60
|
|
|
73
61
|
constructor(config: PreviewPaneConfig) {
|
|
74
62
|
this.question = config.question;
|
|
75
|
-
this.theme = config.theme;
|
|
76
63
|
this.getTerminalWidth = config.getTerminalWidth;
|
|
77
|
-
this.
|
|
78
|
-
|
|
79
|
-
const selectTheme: WrappingSelectTheme = {
|
|
80
|
-
selectedText: (t) => this.theme.fg("accent", this.theme.bold(t)),
|
|
81
|
-
description: (t) => this.theme.fg("muted", t),
|
|
82
|
-
scrollInfo: (t) => this.theme.fg("dim", t),
|
|
83
|
-
};
|
|
84
|
-
// Reserve a slot for the chat row so the number column is wide enough whether or
|
|
85
|
-
// not the user navigates into chat. Chat row uses (items.length + 1).
|
|
86
|
-
this.options = new WrappingSelect(config.items, Math.min(config.items.length, MAX_VISIBLE_OPTIONS), selectTheme, {
|
|
87
|
-
numberStartOffset: 0,
|
|
88
|
-
totalItemsForNumbering: config.items.length + 1,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
setSelectedIndex(index: number): void {
|
|
93
|
-
this.selectedIndex = index;
|
|
94
|
-
this.options.setSelectedIndex(index);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
setFocused(focused: boolean): void {
|
|
98
|
-
this.focused = focused;
|
|
99
|
-
this.options.setFocused(focused);
|
|
64
|
+
this.optionListView = config.optionListView;
|
|
65
|
+
this.previewBlock = config.previewBlock;
|
|
100
66
|
}
|
|
101
67
|
|
|
102
68
|
setNotesVisible(visible: boolean): void {
|
|
103
69
|
this.notesVisible = visible;
|
|
104
70
|
}
|
|
105
71
|
|
|
106
|
-
setConfirmedIndex(index: number | undefined, labelOverride?: string): void {
|
|
107
|
-
this.options.setConfirmedIndex(index, labelOverride);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
setInputBuffer(text: string): void {
|
|
111
|
-
this.options.setInputBuffer(text);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
invalidateCache(): void {
|
|
115
|
-
this.cache.invalidate();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
getInputBuffer(): string {
|
|
119
|
-
return this.options.getInputBuffer();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
appendInput(text: string): void {
|
|
123
|
-
this.options.appendInput(text);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
backspaceInput(): void {
|
|
127
|
-
this.options.backspaceInput();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
clearInputBuffer(): void {
|
|
131
|
-
this.options.clearInputBuffer();
|
|
132
|
-
}
|
|
133
|
-
|
|
134
72
|
handleInput(_data: string): void {}
|
|
135
73
|
|
|
136
74
|
invalidate(): void {
|
|
137
|
-
this.
|
|
138
|
-
this.
|
|
75
|
+
this.previewBlock.invalidate();
|
|
76
|
+
this.optionListView.invalidate();
|
|
139
77
|
}
|
|
140
78
|
|
|
141
79
|
render(width: number): string[] {
|
|
142
|
-
if (this.question.multiSelect === true) return this.
|
|
80
|
+
if (this.question.multiSelect === true) return this.optionListView.render(width);
|
|
143
81
|
// Spec: hide the preview pane entirely when no option carries a `preview`.
|
|
144
|
-
if (!this.
|
|
82
|
+
if (!this.previewBlock.hasAnyPreview()) return this.optionListView.render(width);
|
|
145
83
|
|
|
146
84
|
const mode = decideLayout(this.getTerminalWidth(), width);
|
|
147
|
-
if (mode === "side-by-side") return this.renderSideBySide(width);
|
|
85
|
+
if (mode === "side-by-side") return this.renderSideBySide(width, mode);
|
|
148
86
|
|
|
149
87
|
// Stacked: options + blank gap + preview block.
|
|
150
88
|
return [
|
|
151
|
-
...this.
|
|
89
|
+
...this.optionListView.render(width),
|
|
152
90
|
...Array(STACKED_GAP_ROWS).fill(""),
|
|
153
|
-
...this.
|
|
91
|
+
...this.previewBlock.renderBlock(
|
|
92
|
+
width,
|
|
93
|
+
this.optionListView.getSelectedIndex(),
|
|
94
|
+
mode,
|
|
95
|
+
this.optionListView.isFocused(),
|
|
96
|
+
this.notesVisible,
|
|
97
|
+
),
|
|
154
98
|
];
|
|
155
99
|
}
|
|
156
100
|
|
|
157
101
|
naturalHeight(width: number): number {
|
|
158
|
-
if (this.question.multiSelect === true) return this.
|
|
159
|
-
if (!this.
|
|
102
|
+
if (this.question.multiSelect === true) return this.optionListView.render(width).length;
|
|
103
|
+
if (!this.previewBlock.hasAnyPreview()) return this.optionListView.render(width).length;
|
|
160
104
|
const mode = decideLayout(this.getTerminalWidth(), width);
|
|
161
105
|
const { optionsWidth, previewWidth } = bodyWidths(width, mode);
|
|
162
|
-
const optionsHeight = this.
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
106
|
+
const optionsHeight = this.optionListView.render(optionsWidth).length;
|
|
107
|
+
const previewBlockHeight = this.previewBlock.blockHeight(
|
|
108
|
+
previewWidth,
|
|
109
|
+
this.optionListView.getSelectedIndex(),
|
|
110
|
+
mode,
|
|
111
|
+
);
|
|
112
|
+
if (mode === "side-by-side") return Math.max(optionsHeight, previewBlockHeight);
|
|
113
|
+
return optionsHeight + STACKED_GAP_ROWS + previewBlockHeight;
|
|
166
114
|
}
|
|
167
115
|
|
|
168
116
|
maxNaturalHeight(width: number): number {
|
|
169
|
-
if (this.question.multiSelect === true) return this.
|
|
170
|
-
if (!this.
|
|
117
|
+
if (this.question.multiSelect === true) return this.optionListView.render(width).length;
|
|
118
|
+
if (!this.previewBlock.hasAnyPreview()) return this.optionListView.render(width).length;
|
|
171
119
|
const mode = decideLayout(this.getTerminalWidth(), width);
|
|
172
120
|
const { optionsWidth, previewWidth } = bodyWidths(width, mode);
|
|
173
|
-
const optionsHeight = this.
|
|
121
|
+
const optionsHeight = this.optionListView.render(optionsWidth).length;
|
|
174
122
|
let maxPreviewBlock = 0;
|
|
175
123
|
for (let i = 0; i < this.question.options.length; i++) {
|
|
176
|
-
const h = this.
|
|
124
|
+
const h = this.previewBlock.blockHeight(previewWidth, i, mode);
|
|
177
125
|
if (h > maxPreviewBlock) maxPreviewBlock = h;
|
|
178
126
|
}
|
|
179
127
|
if (mode === "side-by-side") return Math.max(optionsHeight, maxPreviewBlock);
|
|
180
128
|
return optionsHeight + STACKED_GAP_ROWS + maxPreviewBlock;
|
|
181
129
|
}
|
|
182
130
|
|
|
183
|
-
|
|
184
|
-
* Height of the preview block for a given option at a given outer column width.
|
|
185
|
-
* Layout `mode` is THREADED in (not re-derived from `width`) — eliminates the
|
|
186
|
-
* bug class where derivation from a column width (already < pane width post-split)
|
|
187
|
-
* capped height too short.
|
|
188
|
-
*/
|
|
189
|
-
private previewBlockHeight(width: number, optionIndex: number, mode: PreviewLayoutMode): number {
|
|
190
|
-
const cap = mode === "side-by-side" ? MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE : MAX_PREVIEW_HEIGHT_STACKED;
|
|
191
|
-
const contentBudget = Math.max(1, cap - BORDER_VERTICAL_OVERHEAD - NOTES_AFFORDANCE_OVERHEAD);
|
|
192
|
-
const innerWidth = Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL);
|
|
193
|
-
const rawRows = this.cache.bodyFor(optionIndex, innerWidth).length;
|
|
194
|
-
const contentRows = Math.min(rawRows, contentBudget);
|
|
195
|
-
return BORDER_VERTICAL_OVERHEAD + contentRows + NOTES_AFFORDANCE_OVERHEAD;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private renderSideBySide(width: number): string[] {
|
|
131
|
+
private renderSideBySide(width: number, mode: PreviewLayoutMode): string[] {
|
|
199
132
|
const { leftWidth, rightWidth, gap } = columnWidths(width);
|
|
200
|
-
const leftLines = this.
|
|
201
|
-
const rightLines = this.renderPaddedPreviewLines(rightWidth,
|
|
133
|
+
const leftLines = this.optionListView.render(leftWidth);
|
|
134
|
+
const rightLines = this.renderPaddedPreviewLines(rightWidth, mode);
|
|
202
135
|
const rows = Math.max(leftLines.length, rightLines.length);
|
|
203
136
|
const gapStr = " ".repeat(gap);
|
|
204
137
|
const out: string[] = [];
|
|
@@ -214,31 +147,15 @@ export class PreviewPane implements Component {
|
|
|
214
147
|
}
|
|
215
148
|
|
|
216
149
|
private renderPaddedPreviewLines(colWidth: number, mode: PreviewLayoutMode): string[] {
|
|
217
|
-
const
|
|
150
|
+
const inner = Math.max(1, colWidth - PREVIEW_PADDING_LEFT);
|
|
151
|
+
const contentLines = this.previewBlock.renderBlock(
|
|
152
|
+
inner,
|
|
153
|
+
this.optionListView.getSelectedIndex(),
|
|
154
|
+
mode,
|
|
155
|
+
this.optionListView.isFocused(),
|
|
156
|
+
this.notesVisible,
|
|
157
|
+
);
|
|
218
158
|
const pad = " ".repeat(PREVIEW_PADDING_LEFT);
|
|
219
159
|
return contentLines.map((l) => (l === "" ? "" : `${pad}${l}`));
|
|
220
160
|
}
|
|
221
|
-
|
|
222
|
-
private renderPreviewLines(width: number, mode: PreviewLayoutMode): string[] {
|
|
223
|
-
const cap = mode === "side-by-side" ? MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE : MAX_PREVIEW_HEIGHT_STACKED;
|
|
224
|
-
const contentBudget = Math.max(1, cap - BORDER_VERTICAL_OVERHEAD - NOTES_AFFORDANCE_OVERHEAD);
|
|
225
|
-
const maxInnerWidth = Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL);
|
|
226
|
-
|
|
227
|
-
const raw = this.cache.bodyFor(this.selectedIndex, maxInnerWidth);
|
|
228
|
-
const truncated = raw.length > contentBudget;
|
|
229
|
-
const hidden = truncated ? raw.length - contentBudget : 0;
|
|
230
|
-
const contentLines = truncated ? raw.slice(0, contentBudget) : raw;
|
|
231
|
-
|
|
232
|
-
const { boxWidth } = computeBoxDimensions(contentLines, maxInnerWidth);
|
|
233
|
-
|
|
234
|
-
const colorFn = (s: string) => this.theme.fg("accent", s);
|
|
235
|
-
const boxedLines = renderBorderedBox(contentLines, boxWidth, colorFn, hidden);
|
|
236
|
-
|
|
237
|
-
// Notes affordance row — reserved CONSTANTLY when hasAnyPreview (height stability
|
|
238
|
-
// of the affordance row's offset relative to the box). Text appears only when
|
|
239
|
-
// focused on a preview-bearing option AND not in notes mode.
|
|
240
|
-
const showAffordance = this.focused && !this.notesVisible && this.cache.has(this.selectedIndex);
|
|
241
|
-
const affordance = showAffordance ? this.theme.fg("muted", NOTES_AFFORDANCE_TEXT) : "";
|
|
242
|
-
return [...boxedLines, "", affordance];
|
|
243
|
-
}
|
|
244
161
|
}
|
package/questionnaire-session.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { type ApplyContext, applyAction, type Effect } from "./apply-action.js";
|
|
|
4
4
|
import { buildDialog, type DialogComponent } from "./dialog-builder.js";
|
|
5
5
|
import { handleQuestionnaireInput, type QuestionnaireAction } from "./dispatch.js";
|
|
6
6
|
import { MultiSelectOptions } from "./multi-select-options.js";
|
|
7
|
+
import { OptionListView } from "./option-list-view.js";
|
|
8
|
+
import { PreviewBlockRenderer } from "./preview-block-renderer.js";
|
|
7
9
|
import { PreviewPane } from "./preview-pane.js";
|
|
8
10
|
import {
|
|
9
11
|
computeFocusedOptionHasPreview,
|
|
@@ -12,11 +14,10 @@ import {
|
|
|
12
14
|
} from "./questionnaire-state.js";
|
|
13
15
|
import { SubmitPicker } from "./submit-picker.js";
|
|
14
16
|
import { TabBar } from "./tab-bar.js";
|
|
15
|
-
import type
|
|
17
|
+
import { type QuestionData, type QuestionnaireResult, type QuestionParams, SENTINEL_LABELS } from "./types.js";
|
|
16
18
|
import { QuestionnaireViewAdapter } from "./view-adapter.js";
|
|
17
19
|
import { WrappingSelect, type WrappingSelectItem, type WrappingSelectTheme } from "./wrapping-select.js";
|
|
18
20
|
|
|
19
|
-
const CHAT_ABOUT_THIS_LABEL = "Chat about this";
|
|
20
21
|
const BACKSPACE_CHARS = new Set(["\x7f", "\b"]);
|
|
21
22
|
const ESC_SEQUENCE_PREFIX = "\x1b";
|
|
22
23
|
|
|
@@ -61,6 +62,7 @@ export class QuestionnaireSession {
|
|
|
61
62
|
private readonly questions: readonly QuestionData[];
|
|
62
63
|
private readonly isMulti: boolean;
|
|
63
64
|
private readonly itemsByTab: WrappingSelectItem[][];
|
|
65
|
+
private readonly optionListViewsByTab: OptionListView[];
|
|
64
66
|
private readonly previewPanes: PreviewPane[];
|
|
65
67
|
private readonly multiSelectOptionsByTab: ReadonlyArray<MultiSelectOptions | undefined>;
|
|
66
68
|
private readonly submitPicker: SubmitPicker | undefined;
|
|
@@ -90,22 +92,27 @@ export class QuestionnaireSession {
|
|
|
90
92
|
description: (t) => config.theme.fg("muted", t),
|
|
91
93
|
scrollInfo: (t) => config.theme.fg("dim", t),
|
|
92
94
|
};
|
|
93
|
-
this.chatList = new WrappingSelect([{
|
|
95
|
+
this.chatList = new WrappingSelect([{ kind: "chat", label: SENTINEL_LABELS.chat }], 1, selectTheme);
|
|
94
96
|
this.notesInput = new Input();
|
|
95
97
|
|
|
98
|
+
this.optionListViewsByTab = this.itemsByTab.map((items) => new OptionListView({ items, theme: selectTheme }));
|
|
99
|
+
|
|
96
100
|
const markdownTheme = getMarkdownTheme();
|
|
97
101
|
const getTerminalWidth = () => this.tui.terminal.columns;
|
|
98
102
|
|
|
99
|
-
this.previewPanes = this.questions.map(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
103
|
+
this.previewPanes = this.questions.map((q, i) => {
|
|
104
|
+
const previewBlock = new PreviewBlockRenderer({
|
|
105
|
+
question: q,
|
|
106
|
+
theme: config.theme,
|
|
107
|
+
markdownTheme,
|
|
108
|
+
});
|
|
109
|
+
return new PreviewPane({
|
|
110
|
+
question: q,
|
|
111
|
+
getTerminalWidth,
|
|
112
|
+
optionListView: this.optionListViewsByTab[i]!,
|
|
113
|
+
previewBlock,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
109
116
|
|
|
110
117
|
const initialSnap = this.snapshot();
|
|
111
118
|
this.multiSelectOptionsByTab = this.questions.map((q) =>
|
|
@@ -143,6 +150,7 @@ export class QuestionnaireSession {
|
|
|
143
150
|
tui: this.tui,
|
|
144
151
|
questions: this.questions,
|
|
145
152
|
itemsByTab: this.itemsByTab,
|
|
153
|
+
optionListViewsByTab: this.optionListViewsByTab,
|
|
146
154
|
previewPanes: this.previewPanes,
|
|
147
155
|
chatList: this.chatList,
|
|
148
156
|
multiSelectOptionsByTab: this.multiSelectOptionsByTab,
|
|
@@ -195,10 +203,10 @@ export class QuestionnaireSession {
|
|
|
195
203
|
private runEffect(effect: Effect): void {
|
|
196
204
|
switch (effect.kind) {
|
|
197
205
|
case "set_input_buffer":
|
|
198
|
-
this.
|
|
206
|
+
this.optionListViewsByTab[this.state.currentTab]?.setInputBuffer(effect.value);
|
|
199
207
|
return;
|
|
200
208
|
case "clear_input_buffer":
|
|
201
|
-
this.
|
|
209
|
+
this.optionListViewsByTab[this.state.currentTab]?.clearInputBuffer();
|
|
202
210
|
return;
|
|
203
211
|
case "set_notes_value":
|
|
204
212
|
this.notesInput.setValue(effect.value);
|
|
@@ -217,18 +225,19 @@ export class QuestionnaireSession {
|
|
|
217
225
|
|
|
218
226
|
/**
|
|
219
227
|
* Inline `ignore` handler — preserves per-keystroke buffer mutation when in inputMode.
|
|
220
|
-
*
|
|
221
|
-
*
|
|
228
|
+
* Routes directly to OptionListView (no PreviewPane proxy). Bypasses the reducer because no
|
|
229
|
+
* canonical state changes; bypasses the view-adapter because only the OptionListView's own
|
|
230
|
+
* buffer needs to update before the next render.
|
|
222
231
|
*/
|
|
223
232
|
private handleIgnoreInline(data: string): void {
|
|
224
233
|
if (!this.state.inputMode) return;
|
|
225
|
-
const
|
|
226
|
-
if (!
|
|
234
|
+
const view = this.optionListViewsByTab[this.state.currentTab];
|
|
235
|
+
if (!view) return;
|
|
227
236
|
if (BACKSPACE_CHARS.has(data)) {
|
|
228
|
-
|
|
237
|
+
view.backspaceInput();
|
|
229
238
|
this.tui.requestRender();
|
|
230
239
|
} else if (data && !data.startsWith(ESC_SEQUENCE_PREFIX)) {
|
|
231
|
-
|
|
240
|
+
view.appendInput(data);
|
|
232
241
|
this.tui.requestRender();
|
|
233
242
|
}
|
|
234
243
|
}
|
|
@@ -237,7 +246,7 @@ export class QuestionnaireSession {
|
|
|
237
246
|
return {
|
|
238
247
|
...this.state,
|
|
239
248
|
keybindings: getKeybindings(),
|
|
240
|
-
inputBuffer: this.
|
|
249
|
+
inputBuffer: this.optionListViewsByTab[this.state.currentTab]?.getInputBuffer() ?? "",
|
|
241
250
|
questions: this.questions,
|
|
242
251
|
isMulti: this.isMulti,
|
|
243
252
|
currentItem: this.currentItem(),
|
|
@@ -254,10 +263,10 @@ export class QuestionnaireSession {
|
|
|
254
263
|
}
|
|
255
264
|
|
|
256
265
|
private currentItem(): WrappingSelectItem | undefined {
|
|
257
|
-
if (this.state.chatFocused) return {
|
|
266
|
+
if (this.state.chatFocused) return { kind: "chat", label: SENTINEL_LABELS.chat };
|
|
258
267
|
const arr = this.itemsByTab[this.state.currentTab] ?? [];
|
|
259
268
|
if (this.state.optionIndex < arr.length) return arr[this.state.optionIndex];
|
|
260
|
-
return {
|
|
269
|
+
return { kind: "chat", label: SENTINEL_LABELS.chat };
|
|
261
270
|
}
|
|
262
271
|
|
|
263
272
|
private computeGlobalContentHeight(width: number): number {
|
package/questionnaire-state.ts
CHANGED
|
@@ -87,7 +87,7 @@ export function chatNumberingFor(items: readonly WrappingSelectItem[]): {
|
|
|
87
87
|
// Count only the visible-numbered rows. The Next sentinel renders without a number
|
|
88
88
|
// (see MultiSelectOptions), so it must NOT advance the chat row's number — otherwise
|
|
89
89
|
// chat reads as "6." next to options labeled 1-4.
|
|
90
|
-
const count = items.filter((i) =>
|
|
90
|
+
const count = items.filter((i) => i.kind !== "next").length;
|
|
91
91
|
return { offset: count, total: count + 1 };
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -95,7 +95,7 @@ export function chatNumberingFor(items: readonly WrappingSelectItem[]): {
|
|
|
95
95
|
* Which row in the active tab should be marked as "previously confirmed"? Drives the
|
|
96
96
|
* `WrappingSelect` confirmed-row indicator (label + ` ✔`) when the user navigates back
|
|
97
97
|
* to a question they already answered. Returns `undefined` when no marker should be drawn —
|
|
98
|
-
* multi-select handles its own `[✔]` boxes via `multiSelectChecked`, `
|
|
98
|
+
* multi-select handles its own `[✔]` boxes via `multiSelectChecked`, `kind: "chat"` ends the
|
|
99
99
|
* dialog so the row can never be re-entered, and a missing/non-matching answer (defensive)
|
|
100
100
|
* silently skips the marker.
|
|
101
101
|
*/
|
|
@@ -108,14 +108,14 @@ export function selectConfirmedIndicator(
|
|
|
108
108
|
const q = questions[currentTab];
|
|
109
109
|
if (!q || q.multiSelect === true) return undefined;
|
|
110
110
|
const prior = answers.get(currentTab);
|
|
111
|
-
if (!prior || prior.
|
|
112
|
-
if (prior.
|
|
113
|
-
const otherIndex = items.findIndex((it) => it.
|
|
111
|
+
if (!prior || prior.kind === "chat") return undefined;
|
|
112
|
+
if (prior.kind === "custom") {
|
|
113
|
+
const otherIndex = items.findIndex((it) => it.kind === "other");
|
|
114
114
|
if (otherIndex < 0) return undefined;
|
|
115
115
|
return { index: otherIndex, labelOverride: prior.answer ?? "" };
|
|
116
116
|
}
|
|
117
|
-
if (typeof prior.answer !== "string") return undefined;
|
|
118
|
-
const index = items.findIndex((it) =>
|
|
117
|
+
if (prior.kind !== "option" || typeof prior.answer !== "string") return undefined;
|
|
118
|
+
const index = items.findIndex((it) => it.kind === "option" && it.label === prior.answer);
|
|
119
119
|
if (index < 0) return undefined;
|
|
120
120
|
return { index };
|
|
121
121
|
}
|
package/types.ts
CHANGED
|
@@ -6,18 +6,40 @@ export const MAX_OPTIONS = 4;
|
|
|
6
6
|
export const MAX_HEADER_LENGTH = 12;
|
|
7
7
|
export const MAX_LABEL_LENGTH = 60;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* User-facing labels for the three runtime sentinel rows, keyed by their
|
|
11
|
+
* `WrappingSelectItem.kind` discriminator. Single source of truth — every
|
|
12
|
+
* construction site (`ask-user-question.ts`, `questionnaire-session.ts`,
|
|
13
|
+
* `multi-select-options.ts`) reads from here, and `RESERVED_LABELS` is
|
|
14
|
+
* derived from it. Adding a new sentinel requires extending the
|
|
15
|
+
* `WrappingSelectItem` union AND this map in lockstep — both reads are
|
|
16
|
+
* compiler-enforced via the kind-keyed lookup at construction sites.
|
|
17
|
+
*/
|
|
18
|
+
export const SENTINEL_LABELS = {
|
|
19
|
+
other: "Type something.",
|
|
20
|
+
chat: "Chat about this",
|
|
21
|
+
next: "Next",
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export type SentinelKind = keyof typeof SENTINEL_LABELS;
|
|
25
|
+
export type SentinelLabel = (typeof SENTINEL_LABELS)[SentinelKind];
|
|
26
|
+
|
|
9
27
|
/**
|
|
10
28
|
* Labels reserved for Pi-internal sentinels — authoring an option with any
|
|
11
|
-
* of these labels triggers the `reserved_label` runtime guard.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
29
|
+
* of these labels triggers the `reserved_label` runtime guard. Three of the
|
|
30
|
+
* four come from `SENTINEL_LABELS` (the runtime kinds); `"Other"` is
|
|
31
|
+
* reserved for CC parity only (the model is conditioned to reach for
|
|
32
|
+
* "Other" in CC; we reject it so the runtime sentinel is the single source
|
|
33
|
+
* of truth) and has no runtime kind.
|
|
16
34
|
*
|
|
17
35
|
* Reserved unconditionally — multiSelect questions also reject these labels
|
|
18
36
|
* even though the runtime sentinel is suppressed there.
|
|
37
|
+
*
|
|
38
|
+
* Order is pinned by `types.test.ts:292` — keep the explicit
|
|
39
|
+
* `["Other", other, chat, next]` literal so consumers using
|
|
40
|
+
* `RESERVED_LABELS[i]` indexing or `Set` membership see no behavior change.
|
|
19
41
|
*/
|
|
20
|
-
export const RESERVED_LABELS = ["Other",
|
|
42
|
+
export const RESERVED_LABELS = ["Other", SENTINEL_LABELS.other, SENTINEL_LABELS.chat, SENTINEL_LABELS.next] as const;
|
|
21
43
|
export type ReservedLabel = (typeof RESERVED_LABELS)[number];
|
|
22
44
|
|
|
23
45
|
export const OptionSchema = Type.Object({
|
|
@@ -77,20 +99,30 @@ export type OptionData = Static<typeof OptionSchema>;
|
|
|
77
99
|
export type QuestionData = Static<typeof QuestionSchema>;
|
|
78
100
|
export type QuestionParams = Static<typeof QuestionParamsSchema>;
|
|
79
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Answer-intent discriminated union. `kind` replaces the legacy boolean-flag
|
|
104
|
+
* discriminators (`wasCustom`/`wasChat`). Mirrors the row-side `WrappingSelectItem.kind`
|
|
105
|
+
* vocabulary where possible; `multi` is the multi-select variant (no row-side analog).
|
|
106
|
+
*
|
|
107
|
+
* Variant semantics:
|
|
108
|
+
* - `option`: user picked one of the author-defined options. `answer` is the option's label.
|
|
109
|
+
* - `custom`: user typed free-text via the "Type something." row. `answer` is the typed text or null.
|
|
110
|
+
* - `chat`: user picked the chat sentinel. `answer` is the literal "Chat about this".
|
|
111
|
+
* - `multi`: user committed multi-select choices. `selected` carries chosen labels; `answer` is null.
|
|
112
|
+
*/
|
|
80
113
|
export interface QuestionAnswer {
|
|
81
114
|
questionIndex: number;
|
|
82
115
|
question: string;
|
|
116
|
+
kind: "option" | "custom" | "chat" | "multi";
|
|
83
117
|
answer: string | null;
|
|
84
118
|
selected?: string[];
|
|
85
|
-
wasCustom?: boolean;
|
|
86
|
-
wasChat?: boolean;
|
|
87
119
|
notes?: string;
|
|
88
120
|
/**
|
|
89
121
|
* Markdown text from the matched option's `preview` field, populated only
|
|
90
122
|
* when the user lands on a single-select option carrying a `preview`.
|
|
91
123
|
* Used by `buildQuestionnaireResponse` to echo `selected preview: <preview>`
|
|
92
124
|
* into the LLM-facing envelope. Undefined for multi-select, custom-text
|
|
93
|
-
* (`
|
|
125
|
+
* (`kind: "custom"`), and chat (`kind: "chat"`) answers.
|
|
94
126
|
*/
|
|
95
127
|
preview?: string;
|
|
96
128
|
}
|
package/view-adapter.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { DialogComponent } from "./dialog-builder.js";
|
|
2
|
+
import type { MultiSelectOptions } from "./multi-select-options.js";
|
|
3
|
+
import type { OptionListView } from "./option-list-view.js";
|
|
4
|
+
import type { PreviewPane } from "./preview-pane.js";
|
|
5
|
+
import {
|
|
6
|
+
chatNumberingFor,
|
|
7
|
+
type QuestionnaireState,
|
|
8
|
+
selectActivePreviewPaneIndex,
|
|
9
|
+
selectActiveTabItems,
|
|
10
|
+
selectConfirmedIndicator,
|
|
11
|
+
selectOptionsFocused,
|
|
12
|
+
selectSubmitPickerFocused,
|
|
13
|
+
} from "./questionnaire-state.js";
|
|
14
|
+
import type { SubmitPicker } from "./submit-picker.js";
|
|
15
|
+
import type { TabBar } from "./tab-bar.js";
|
|
16
|
+
import type { QuestionData } from "./types.js";
|
|
17
|
+
import type { WrappingSelect, WrappingSelectItem } from "./wrapping-select.js";
|
|
18
|
+
|
|
19
|
+
export interface QuestionnaireViewAdapterConfig {
|
|
20
|
+
tui: { requestRender(): void };
|
|
21
|
+
questions: readonly QuestionData[];
|
|
22
|
+
itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
23
|
+
optionListViewsByTab: ReadonlyArray<OptionListView>;
|
|
24
|
+
previewPanes: readonly PreviewPane[];
|
|
25
|
+
chatList: WrappingSelect;
|
|
26
|
+
multiSelectOptionsByTab: ReadonlyArray<MultiSelectOptions | undefined>;
|
|
27
|
+
submitPicker: SubmitPicker | undefined;
|
|
28
|
+
tabBar: TabBar | undefined;
|
|
29
|
+
dialog: DialogComponent;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* View fan-out: drives every component setter from the canonical state via named selectors.
|
|
34
|
+
*
|
|
35
|
+
* `OptionListView` receives the option-side setters directly (`setSelectedIndex`, `setFocused`,
|
|
36
|
+
* `setConfirmedIndex`) — no mirrored cells on `PreviewPane`. `PreviewPane` only receives
|
|
37
|
+
* `setNotesVisible` (its sole local state).
|
|
38
|
+
*
|
|
39
|
+
* The adapter owns the components but never owns mutable state — every projection is read fresh
|
|
40
|
+
* from the input `state` argument, so there is no risk of stale view-side data.
|
|
41
|
+
*/
|
|
42
|
+
export class QuestionnaireViewAdapter {
|
|
43
|
+
private readonly tui: QuestionnaireViewAdapterConfig["tui"];
|
|
44
|
+
private readonly questions: readonly QuestionData[];
|
|
45
|
+
private readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
46
|
+
private readonly optionListViewsByTab: ReadonlyArray<OptionListView>;
|
|
47
|
+
private readonly previewPanes: readonly PreviewPane[];
|
|
48
|
+
private readonly chatList: WrappingSelect;
|
|
49
|
+
private readonly multiSelectOptionsByTab: ReadonlyArray<MultiSelectOptions | undefined>;
|
|
50
|
+
private readonly submitPicker: SubmitPicker | undefined;
|
|
51
|
+
private readonly tabBar: TabBar | undefined;
|
|
52
|
+
private readonly dialog: DialogComponent;
|
|
53
|
+
|
|
54
|
+
constructor(config: QuestionnaireViewAdapterConfig) {
|
|
55
|
+
this.tui = config.tui;
|
|
56
|
+
this.questions = config.questions;
|
|
57
|
+
this.itemsByTab = config.itemsByTab;
|
|
58
|
+
this.optionListViewsByTab = config.optionListViewsByTab;
|
|
59
|
+
this.previewPanes = config.previewPanes;
|
|
60
|
+
this.chatList = config.chatList;
|
|
61
|
+
this.multiSelectOptionsByTab = config.multiSelectOptionsByTab;
|
|
62
|
+
this.submitPicker = config.submitPicker;
|
|
63
|
+
this.tabBar = config.tabBar;
|
|
64
|
+
this.dialog = config.dialog;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Replace the dialog's active preview pane. Called by the runtime when it executes the
|
|
69
|
+
* `set_active_preview_pane` effect emitted by `applyAction`'s tab-switching paths. Must
|
|
70
|
+
* run before the next `apply()` so the dialog's strategy reads the new pane via its live
|
|
71
|
+
* getter.
|
|
72
|
+
*/
|
|
73
|
+
setActivePreviewPane(paneIndex: number): void {
|
|
74
|
+
const pane = this.previewPanes[paneIndex] ?? this.previewPanes[0];
|
|
75
|
+
if (pane) this.dialog.setPreviewPane(pane);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Project canonical state through selectors → component setters and request a render.
|
|
80
|
+
* Idempotent — calling twice with the same state produces the same setter sequence.
|
|
81
|
+
*/
|
|
82
|
+
apply(state: QuestionnaireState): void {
|
|
83
|
+
const totalQuestions = this.questions.length;
|
|
84
|
+
const optionsFocused = selectOptionsFocused(state);
|
|
85
|
+
|
|
86
|
+
this.dialog.setState(state);
|
|
87
|
+
|
|
88
|
+
const paneIndex = selectActivePreviewPaneIndex(state.currentTab, totalQuestions);
|
|
89
|
+
|
|
90
|
+
const view = this.optionListViewsByTab[paneIndex] ?? this.optionListViewsByTab[0];
|
|
91
|
+
if (view) {
|
|
92
|
+
view.setSelectedIndex(state.optionIndex);
|
|
93
|
+
view.setFocused(optionsFocused);
|
|
94
|
+
const confirmed = selectConfirmedIndicator(
|
|
95
|
+
this.questions,
|
|
96
|
+
state.currentTab,
|
|
97
|
+
state.answers,
|
|
98
|
+
this.itemsByTab[paneIndex] ?? [],
|
|
99
|
+
);
|
|
100
|
+
view.setConfirmedIndex(confirmed?.index, confirmed?.labelOverride);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const pane = this.previewPanes[paneIndex] ?? this.previewPanes[0];
|
|
104
|
+
pane?.setNotesVisible(state.notesVisible);
|
|
105
|
+
|
|
106
|
+
this.chatList.setFocused(state.chatFocused);
|
|
107
|
+
|
|
108
|
+
for (const mso of this.multiSelectOptionsByTab) {
|
|
109
|
+
if (!mso) continue;
|
|
110
|
+
mso.setState(state);
|
|
111
|
+
mso.setFocused(optionsFocused);
|
|
112
|
+
}
|
|
113
|
+
if (this.submitPicker) {
|
|
114
|
+
this.submitPicker.setState(state);
|
|
115
|
+
this.submitPicker.setFocused(selectSubmitPickerFocused(state.currentTab, totalQuestions));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const activeItems = selectActiveTabItems(this.itemsByTab, state.currentTab, totalQuestions);
|
|
119
|
+
const numbering = chatNumberingFor(activeItems);
|
|
120
|
+
this.chatList.setNumbering(numbering.offset, numbering.total);
|
|
121
|
+
|
|
122
|
+
if (this.tabBar) {
|
|
123
|
+
this.tabBar.setConfig({
|
|
124
|
+
questions: this.questions,
|
|
125
|
+
answers: new Map(state.answers),
|
|
126
|
+
activeTabIndex: state.currentTab,
|
|
127
|
+
totalTabs: totalQuestions + 1,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.tui.requestRender();
|
|
132
|
+
}
|
|
133
|
+
}
|