@juicesharp/rpiv-ask-user-question 0.13.2 → 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/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/dialog-builder.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { DynamicBorder, type Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { type Component, Container, type Input, Spacer
|
|
2
|
+
import { type Component, Container, type Input, Spacer } from "@mariozechner/pi-tui";
|
|
3
3
|
import { BodyResidualSpacer } from "./body-residual-spacer.js";
|
|
4
4
|
import type { MultiSelectOptions } from "./multi-select-options.js";
|
|
5
5
|
import type { PreviewPane } from "./preview-pane.js";
|
|
6
|
+
import type { QuestionnaireState } from "./questionnaire-state.js";
|
|
6
7
|
import type { TabBar } from "./tab-bar.js";
|
|
7
|
-
import
|
|
8
|
+
import { QuestionTabStrategy, SubmitTabStrategy, type TabStrategy } from "./tab-strategy.js";
|
|
9
|
+
import type { QuestionData } from "./types.js";
|
|
8
10
|
import type { WrappingSelect } from "./wrapping-select.js";
|
|
9
11
|
|
|
10
12
|
// Hint text — these constants are also referenced by tests as substrings of the rendered
|
|
@@ -24,26 +26,7 @@ export const REVIEW_HEADING = "Review your answers";
|
|
|
24
26
|
export const READY_PROMPT = "Ready to submit your answers?";
|
|
25
27
|
export const INCOMPLETE_WARNING_PREFIX = "⚠ Answer remaining questions before submitting:";
|
|
26
28
|
|
|
27
|
-
export
|
|
28
|
-
currentTab: number;
|
|
29
|
-
optionIndex: number;
|
|
30
|
-
notesVisible: boolean;
|
|
31
|
-
inputMode: boolean;
|
|
32
|
-
answers: ReadonlyMap<number, QuestionAnswer>;
|
|
33
|
-
multiSelectChecked: ReadonlySet<number>;
|
|
34
|
-
/**
|
|
35
|
-
* True iff the currently-focused option carries a non-empty `preview` string.
|
|
36
|
-
* Set by `ask-user-question.ts:snapshotState()` via `computeFocusedOptionHasPreview()`.
|
|
37
|
-
* Gates the "n to add notes" hint chip in `buildHintText()` (Decision 8).
|
|
38
|
-
*/
|
|
39
|
-
focusedOptionHasPreview: boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Focused row in the Submit-tab picker (0 = Submit answers, 1 = Cancel).
|
|
42
|
-
* Mirrored from `QuestionnaireDispatchState.submitChoiceIndex`; consumed by
|
|
43
|
-
* `SubmitPicker.setState` inside `buildSubmitContainer`. Default 0; reset on every tab switch.
|
|
44
|
-
*/
|
|
45
|
-
submitChoiceIndex: number;
|
|
46
|
-
}
|
|
29
|
+
export type DialogState = QuestionnaireState;
|
|
47
30
|
|
|
48
31
|
export interface DialogConfig {
|
|
49
32
|
theme: Theme;
|
|
@@ -58,8 +41,8 @@ export interface DialogConfig {
|
|
|
58
41
|
/**
|
|
59
42
|
* Submit-tab Submit/Cancel picker. Optional so the type stays
|
|
60
43
|
* compatible with single-question mode (no Submit Tab) and with tests that
|
|
61
|
-
* exercise non-submit code paths.
|
|
62
|
-
*
|
|
44
|
+
* exercise non-submit code paths. SubmitTabStrategy falls back to Spacer
|
|
45
|
+
* rows when undefined.
|
|
63
46
|
*/
|
|
64
47
|
submitPicker?: Component;
|
|
65
48
|
/**
|
|
@@ -68,8 +51,8 @@ export interface DialogConfig {
|
|
|
68
51
|
*/
|
|
69
52
|
getBodyHeight: (width: number) => number;
|
|
70
53
|
/**
|
|
71
|
-
* Body height of the CURRENTLY active tab/option.
|
|
72
|
-
*
|
|
54
|
+
* Body height of the CURRENTLY active tab/option. The chrome wrapper subtracts
|
|
55
|
+
* this from `getBodyHeight` to absorb the height residual OUTSIDE the bordered
|
|
73
56
|
* region — the body itself renders at its natural height with no internal padding.
|
|
74
57
|
*/
|
|
75
58
|
getCurrentBodyHeight: (width: number) => number;
|
|
@@ -83,6 +66,29 @@ export interface DialogComponent extends Component {
|
|
|
83
66
|
export function buildDialog(config: DialogConfig): DialogComponent {
|
|
84
67
|
let liveConfig: DialogConfig = config;
|
|
85
68
|
|
|
69
|
+
const questionStrategy: TabStrategy = new QuestionTabStrategy({
|
|
70
|
+
theme: config.theme,
|
|
71
|
+
questions: config.questions,
|
|
72
|
+
// Live getter — reads liveConfig.previewPane on every call so dialog.setPreviewPane
|
|
73
|
+
// updates flow through to the strategy without re-construction.
|
|
74
|
+
getPreviewPane: () => liveConfig.previewPane,
|
|
75
|
+
multiSelectOptionsByTab: config.multiSelectOptionsByTab,
|
|
76
|
+
notesInput: config.notesInput,
|
|
77
|
+
chatList: config.chatList,
|
|
78
|
+
isMulti: config.isMulti,
|
|
79
|
+
getCurrentBodyHeight: config.getCurrentBodyHeight,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const submitStrategy: TabStrategy | undefined = config.isMulti
|
|
83
|
+
? new SubmitTabStrategy({
|
|
84
|
+
theme: config.theme,
|
|
85
|
+
questions: config.questions,
|
|
86
|
+
submitPicker: config.submitPicker,
|
|
87
|
+
})
|
|
88
|
+
: undefined;
|
|
89
|
+
|
|
90
|
+
const maxFooterRowCount = Math.max(questionStrategy.footerRowCount, submitStrategy?.footerRowCount ?? 0);
|
|
91
|
+
|
|
86
92
|
const component: DialogComponent = {
|
|
87
93
|
setState(state: DialogState) {
|
|
88
94
|
liveConfig = { ...liveConfig, state };
|
|
@@ -98,184 +104,51 @@ export function buildDialog(config: DialogConfig): DialogComponent {
|
|
|
98
104
|
liveConfig.chatList.invalidate();
|
|
99
105
|
},
|
|
100
106
|
render(width: number): string[] {
|
|
101
|
-
|
|
107
|
+
const onSubmit = liveConfig.isMulti && liveConfig.state.currentTab === liveConfig.questions.length;
|
|
108
|
+
const strategy = onSubmit && submitStrategy ? submitStrategy : questionStrategy;
|
|
109
|
+
return buildContainerFromStrategy(strategy, liveConfig, maxFooterRowCount).render(width);
|
|
102
110
|
},
|
|
103
111
|
};
|
|
104
112
|
return component;
|
|
105
113
|
}
|
|
106
114
|
|
|
107
|
-
function renderDialog(config: DialogConfig, width: number): string[] {
|
|
108
|
-
const { state, questions, isMulti } = config;
|
|
109
|
-
const onSubmit = isMulti && state.currentTab === questions.length;
|
|
110
|
-
if (onSubmit) {
|
|
111
|
-
return buildSubmitContainer(config).render(width);
|
|
112
|
-
}
|
|
113
|
-
return buildQuestionContainer(config).render(width);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
115
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
116
|
+
* Chrome wrapper that lays out a tab strategy's content with structural height
|
|
117
|
+
* equality. The residual spacer absorbs both body-height and footer-row-count
|
|
118
|
+
* asymmetry in one expression — the prior `summary.length + offset` compensation is gone.
|
|
119
119
|
*
|
|
120
|
-
*
|
|
121
|
-
* border + tabBar + Spacer +
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* Submit Tab mirrors that exactly:
|
|
125
|
-
* - "question text" line → REVIEW_HEADING (bold accent, always shown)
|
|
126
|
-
* - body → bullet+arrow summary container (omits unanswered)
|
|
127
|
-
* - chat row + hint footer → Spacer(1) + prompt-or-warning(1) + Spacer(1) +
|
|
128
|
-
* submitPicker(2) — 5 lines below the bottom border
|
|
129
|
-
* vs question footer's 4. The extra row is offset by
|
|
130
|
-
* `submitBodyHeight = summary + 1` so total height
|
|
131
|
-
* still matches a question tab's.
|
|
120
|
+
* Total dialog height (constant per `getBodyHeight` per render):
|
|
121
|
+
* topChromeRows (border + tabBar? + Spacer(1)) +
|
|
122
|
+
* headingRows + body + Spacer(1) + midRows + bottom-border + footerRows + residual
|
|
123
|
+
* = topChromeRows + (heading constant in multi mode) + getBodyHeight + 2 + maxFooterRowCount
|
|
132
124
|
*/
|
|
133
|
-
function
|
|
134
|
-
const { theme,
|
|
125
|
+
function buildContainerFromStrategy(strategy: TabStrategy, config: DialogConfig, maxFooterRowCount: number): Container {
|
|
126
|
+
const { theme, isMulti, tabBar, state } = config;
|
|
135
127
|
const container = new Container();
|
|
136
128
|
const border = () => new DynamicBorder((s) => theme.fg("accent", s));
|
|
137
129
|
|
|
138
|
-
//
|
|
139
|
-
// summary for answered questions only (CC parity — unanswered rows are omitted; the
|
|
140
|
-
// warning header is the sole signal of incompleteness).
|
|
141
|
-
const missing: string[] = [];
|
|
142
|
-
const summary = new Container();
|
|
143
|
-
for (let i = 0; i < questions.length; i++) {
|
|
144
|
-
const q = questions[i];
|
|
145
|
-
const label = q.header && q.header.length > 0 ? q.header : `Q${i + 1}`;
|
|
146
|
-
const a = state.answers.get(i);
|
|
147
|
-
if (!a) {
|
|
148
|
-
missing.push(label);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
const answerText = formatAnswerForSummary(a);
|
|
152
|
-
summary.addChild(new Text(theme.fg("muted", ` ● ${label}`), 1, 0));
|
|
153
|
-
summary.addChild(new Text(` ${theme.fg("muted", "→")} ${theme.fg("text", answerText)}`, 1, 0));
|
|
154
|
-
if (a.notes && a.notes.length > 0) {
|
|
155
|
-
summary.addChild(new Text(theme.fg("dim", ` notes: ${a.notes}`), 1, 0));
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Top chrome — mirrors buildQuestionContainer.
|
|
130
|
+
// Top chrome — common to every tab.
|
|
160
131
|
container.addChild(border());
|
|
161
132
|
if (isMulti && tabBar) container.addChild(tabBar);
|
|
162
133
|
container.addChild(new Spacer(1));
|
|
163
134
|
|
|
164
|
-
//
|
|
165
|
-
|
|
135
|
+
// Strategy-supplied content.
|
|
136
|
+
for (const c of strategy.headingRows(state)) container.addChild(c);
|
|
137
|
+
container.addChild(strategy.bodyComponent(state));
|
|
166
138
|
container.addChild(new Spacer(1));
|
|
139
|
+
for (const c of strategy.midRows(state)) container.addChild(c);
|
|
167
140
|
|
|
168
|
-
//
|
|
169
|
-
// border by BodyResidualSpacer (mirrors buildQuestionContainer).
|
|
170
|
-
container.addChild(summary);
|
|
171
|
-
container.addChild(new Spacer(1));
|
|
172
|
-
|
|
173
|
-
// Bottom border + chrome-mirror layout. Below-border lines:
|
|
174
|
-
// Spacer(1) + prompt-or-warning(1) + Spacer(1) + submitPicker(2) = 5 lines
|
|
175
|
-
// vs question-tab's Spacer + chat(1) + Spacer + hint(1) = 4 lines.
|
|
176
|
-
// The +1 below-border row is absorbed by `submitBodyHeight + 1` so total dialog
|
|
177
|
-
// height still matches a question tab's.
|
|
141
|
+
// Bottom chrome — common.
|
|
178
142
|
container.addChild(border());
|
|
179
|
-
container.addChild(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
// Fallback when host hasn't wired the picker (defensive — Phase 4 always wires it
|
|
190
|
-
// in multi-question mode). Keeps the line count at 5 so height equality holds.
|
|
191
|
-
container.addChild(new Spacer(1));
|
|
192
|
-
container.addChild(new Spacer(1));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// +1 absorbs the extra Spacer added between prompt and submitPicker so total
|
|
196
|
-
// submit-tab height equals a question tab's.
|
|
197
|
-
const submitBodyHeight = (w: number) => summary.render(w).length + 1;
|
|
198
|
-
container.addChild(new BodyResidualSpacer(config.getBodyHeight, submitBodyHeight));
|
|
143
|
+
for (const c of strategy.footerRows(state)) container.addChild(c);
|
|
144
|
+
|
|
145
|
+
// Residual: equalize total height across strategies. Replaces both the
|
|
146
|
+
// per-tab residual asymmetry and the prior +1 magic.
|
|
147
|
+
container.addChild(
|
|
148
|
+
new BodyResidualSpacer(
|
|
149
|
+
(w) => config.getBodyHeight(w) + maxFooterRowCount,
|
|
150
|
+
(w) => strategy.bodyHeight(w, state) + strategy.footerRowCount,
|
|
151
|
+
),
|
|
152
|
+
);
|
|
199
153
|
return container;
|
|
200
154
|
}
|
|
201
|
-
|
|
202
|
-
function buildQuestionContainer(config: DialogConfig): Container {
|
|
203
|
-
const { theme, questions, state, previewPane, tabBar, notesInput, chatList, isMulti } = config;
|
|
204
|
-
const question = questions[state.currentTab];
|
|
205
|
-
const container = new Container();
|
|
206
|
-
const border = () => new DynamicBorder((s) => theme.fg("accent", s));
|
|
207
|
-
|
|
208
|
-
container.addChild(border());
|
|
209
|
-
if (isMulti && tabBar) container.addChild(tabBar);
|
|
210
|
-
container.addChild(new Spacer(1));
|
|
211
|
-
|
|
212
|
-
// In MULTI-question mode the tab bar already shows the per-tab header (e.g. "H1", "H2"),
|
|
213
|
-
// so rendering it again inside the dialog body adds 2 redundant lines that make the
|
|
214
|
-
// Submit Tab look "collapsed" by comparison. Skip the inner header in multi mode.
|
|
215
|
-
if (!isMulti && question?.header && question.header.length > 0) {
|
|
216
|
-
container.addChild(new Text(theme.bg("selectedBg", ` ${question.header} `), 1, 0));
|
|
217
|
-
container.addChild(new Spacer(1));
|
|
218
|
-
}
|
|
219
|
-
if (question) {
|
|
220
|
-
container.addChild(new Text(theme.bold(question.question), 1, 0));
|
|
221
|
-
container.addChild(new Spacer(1));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const mso = config.multiSelectOptionsByTab[state.currentTab];
|
|
225
|
-
if (question?.multiSelect === true && mso) {
|
|
226
|
-
mso.setState(state);
|
|
227
|
-
container.addChild(mso);
|
|
228
|
-
} else {
|
|
229
|
-
container.addChild(previewPane);
|
|
230
|
-
}
|
|
231
|
-
container.addChild(new Spacer(1));
|
|
232
|
-
|
|
233
|
-
if (state.notesVisible) {
|
|
234
|
-
container.addChild(new Text(theme.fg("muted", "Notes:"), 1, 0));
|
|
235
|
-
container.addChild(notesInput);
|
|
236
|
-
container.addChild(new Spacer(1));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
container.addChild(border());
|
|
240
|
-
// Footer sits IMMEDIATELY after the bottom border on every tab: one blank line,
|
|
241
|
-
// chat row, one blank, controls hint. The residual height absorber is appended
|
|
242
|
-
// AFTER the controls so any extra rows fall at the very bottom of the dialog
|
|
243
|
-
// (where they're least visible) rather than between the bordered region and
|
|
244
|
-
// the footer.
|
|
245
|
-
container.addChild(new Spacer(1));
|
|
246
|
-
container.addChild(chatList);
|
|
247
|
-
container.addChild(new Spacer(1));
|
|
248
|
-
container.addChild(new Text(theme.fg("dim", buildHintText(question, isMulti, state)), 1, 0));
|
|
249
|
-
container.addChild(new BodyResidualSpacer(config.getBodyHeight, config.getCurrentBodyHeight));
|
|
250
|
-
return container;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Build the controls hint line. Order is fixed so HINT_SINGLE / HINT_MULTI remain contiguous
|
|
255
|
-
* substrings of the rendered output:
|
|
256
|
-
*
|
|
257
|
-
* Enter to select · ↑/↓ to navigate (← HINT_SINGLE prefix)
|
|
258
|
-
* [· Space to toggle] (multiSelect only)
|
|
259
|
-
* [· n to add notes] (single-select + answered)
|
|
260
|
-
* [· Tab to switch questions] (multi-question only)
|
|
261
|
-
* · Esc to cancel
|
|
262
|
-
*/
|
|
263
|
-
function buildHintText(question: QuestionData | undefined, isMulti: boolean, state: DialogState): string {
|
|
264
|
-
const parts: string[] = ["Enter to select", "↑/↓ to navigate"];
|
|
265
|
-
if (question?.multiSelect === true) parts.push("Space to toggle");
|
|
266
|
-
// Notes hint visible whenever focused option has preview AND not currently editing notes.
|
|
267
|
-
// Pre-answer notes are now reachable (no `answers.has(currentTab)` gate) per Decision 8.
|
|
268
|
-
if (question && question.multiSelect !== true && state.focusedOptionHasPreview && !state.notesVisible) {
|
|
269
|
-
parts.push("n to add notes");
|
|
270
|
-
}
|
|
271
|
-
if (isMulti) parts.push("Tab to switch questions");
|
|
272
|
-
parts.push("Esc to cancel");
|
|
273
|
-
return parts.join(" · ");
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function formatAnswerForSummary(a: QuestionAnswer): string {
|
|
277
|
-
if (a.wasChat) return "User wants to chat about this";
|
|
278
|
-
if (a.selected && a.selected.length > 0) return a.selected.join(", ");
|
|
279
|
-
if (a.wasCustom) return a.answer ?? "(no input)";
|
|
280
|
-
return a.answer ?? "(no answer)";
|
|
281
|
-
}
|
package/dispatch.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Key, matchesKey } from "@mariozechner/pi-tui";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { QuestionnaireDispatchSnapshot } from "./questionnaire-state.js";
|
|
3
|
+
import type { QuestionAnswer } from "./types.js";
|
|
4
4
|
|
|
5
5
|
const KEYBIND_UP = "tui.select.up";
|
|
6
6
|
const KEYBIND_DOWN = "tui.select.down";
|
|
@@ -35,34 +35,7 @@ export interface QuestionnaireKeybindings {
|
|
|
35
35
|
matches(data: string, name: string): boolean;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
export
|
|
39
|
-
currentTab: number;
|
|
40
|
-
optionIndex: number;
|
|
41
|
-
inputMode: boolean;
|
|
42
|
-
notesMode: boolean;
|
|
43
|
-
chatFocused: boolean;
|
|
44
|
-
answers: ReadonlyMap<number, QuestionAnswer>;
|
|
45
|
-
multiSelectIndices: ReadonlySet<number>;
|
|
46
|
-
questions: readonly QuestionData[];
|
|
47
|
-
isMulti: boolean;
|
|
48
|
-
keybindings: QuestionnaireKeybindings;
|
|
49
|
-
currentItem: WrappingSelectItem | undefined;
|
|
50
|
-
inputBuffer: string;
|
|
51
|
-
items: readonly WrappingSelectItem[];
|
|
52
|
-
/**
|
|
53
|
-
* True iff the currently-focused option carries a non-empty `preview` string.
|
|
54
|
-
* Set by `ask-user-question.ts:dispatchSnapshot()` via `computeFocusedOptionHasPreview()`.
|
|
55
|
-
* Gates the `notes_enter` action — notes are scoped to preview-bearing options
|
|
56
|
-
* (Decision 8 + reference image showing affordance only on preview-bearing rows).
|
|
57
|
-
*/
|
|
58
|
-
focusedOptionHasPreview: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Focused row in the Submit-tab picker (0 = Submit answers, 1 = Cancel).
|
|
61
|
-
* Read by the Submit-tab dispatch branch to decide which action Enter emits;
|
|
62
|
-
* mutated by the host on `submit_nav`. Default 0; reset on every tab switch.
|
|
63
|
-
*/
|
|
64
|
-
submitChoiceIndex: number;
|
|
65
|
-
}
|
|
38
|
+
export type QuestionnaireDispatchState = QuestionnaireDispatchSnapshot;
|
|
66
39
|
|
|
67
40
|
export function wrapTab(index: number, total: number): number {
|
|
68
41
|
if (total <= 0) return 0;
|
|
@@ -130,7 +103,7 @@ function buildMultiSelected(state: QuestionnaireDispatchState): string[] {
|
|
|
130
103
|
if (!q) return [];
|
|
131
104
|
const out: string[] = [];
|
|
132
105
|
for (let i = 0; i < q.options.length; i++) {
|
|
133
|
-
if (state.
|
|
106
|
+
if (state.multiSelectChecked.has(i)) {
|
|
134
107
|
const label = q.options[i]?.label;
|
|
135
108
|
if (typeof label === "string") out.push(label);
|
|
136
109
|
}
|
|
@@ -173,7 +146,7 @@ function prevNavOnUp(state: QuestionnaireDispatchState): QuestionnaireAction {
|
|
|
173
146
|
export function handleQuestionnaireInput(data: string, state: QuestionnaireDispatchState): QuestionnaireAction {
|
|
174
147
|
const kb = state.keybindings;
|
|
175
148
|
|
|
176
|
-
if (state.
|
|
149
|
+
if (state.notesVisible) {
|
|
177
150
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "notes_exit" };
|
|
178
151
|
if (kb.matches(data, KEYBIND_CONFIRM)) return { kind: "notes_exit" };
|
|
179
152
|
return { kind: "ignore" };
|
package/format-answer.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { QuestionAnswer } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Continuation message used in the LLM-facing envelope. Two-sentence imperative form
|
|
5
|
+
* — the model needs the "Continue the conversation…" directive to know what to do next
|
|
6
|
+
* after the user picks chat instead of answering.
|
|
7
|
+
*/
|
|
8
|
+
export const CHAT_CONTINUATION_MESSAGE =
|
|
9
|
+
"User wants to chat about this. Continue the conversation to help them decide.";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One-sentence summary form shown in the on-screen Submit-tab review pane. The dialog
|
|
13
|
+
* already shows the question; the imperative continuation directive belongs in the
|
|
14
|
+
* envelope, not in the user-facing summary box.
|
|
15
|
+
*/
|
|
16
|
+
export const CHAT_SUMMARY_MESSAGE = "User wants to chat about this";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Placeholder for empty / null answer text. Used uniformly across both variants — the
|
|
20
|
+
* earlier `(no answer)` fallback in the dialog summary was accidental drift; tests pin
|
|
21
|
+
* `(no input)` only.
|
|
22
|
+
*/
|
|
23
|
+
export const NO_INPUT_PLACEHOLDER = "(no input)";
|
|
24
|
+
|
|
25
|
+
export type FormatAnswerVariant = "summary" | "envelope";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Format a `QuestionAnswer` to its scalar string form. Variant controls only the
|
|
29
|
+
* `wasChat` branch — the envelope's two-sentence imperative is needed by the LLM, the
|
|
30
|
+
* dialog summary's one-sentence reminder is not. All other branches return identical
|
|
31
|
+
* strings; the `wasCustom` empty-string handling and the default-fallback both unify
|
|
32
|
+
* on `NO_INPUT_PLACEHOLDER`.
|
|
33
|
+
*/
|
|
34
|
+
export function formatAnswerScalar(a: QuestionAnswer, variant: FormatAnswerVariant): string {
|
|
35
|
+
if (a.wasChat) return variant === "envelope" ? CHAT_CONTINUATION_MESSAGE : CHAT_SUMMARY_MESSAGE;
|
|
36
|
+
if (a.selected && a.selected.length > 0) return a.selected.join(", ");
|
|
37
|
+
if (a.wasCustom) return a.answer && a.answer.length > 0 ? a.answer : NO_INPUT_PLACEHOLDER;
|
|
38
|
+
return a.answer ?? NO_INPUT_PLACEHOLDER;
|
|
39
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Markdown, type MarkdownTheme, visibleWidth } from "@mariozechner/pi-tui";
|
|
3
|
+
import { stripFenceMarkers } from "./preview-box-renderer.js";
|
|
4
|
+
import type { QuestionData } from "./types.js";
|
|
5
|
+
|
|
6
|
+
/** CC parity in side-by-side layout. */
|
|
7
|
+
export const MAX_PREVIEW_HEIGHT_SIDE_BY_SIDE = 20;
|
|
8
|
+
/** Preserves narrow-terminal protection in stacked layout. */
|
|
9
|
+
export const MAX_PREVIEW_HEIGHT_STACKED = 15;
|
|
10
|
+
export const NO_PREVIEW_TEXT = "No preview available";
|
|
11
|
+
/** 1 blank separator + 1 affordance text row reserved when `hasAnyPreview` (height stability of the affordance row's offset relative to the box). */
|
|
12
|
+
export const NOTES_AFFORDANCE_OVERHEAD = 2;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Per-question cache for rendered markdown previews. Width-keyed: switching the
|
|
16
|
+
* inner width invalidates every cached `Markdown`'s render output (pi-tui's
|
|
17
|
+
* `Markdown.render(width)` re-wraps when width changes).
|
|
18
|
+
*
|
|
19
|
+
* Replaces the inline `previewTexts`, `markdownCache`, `cachedWidth` triple from
|
|
20
|
+
* the previous monolithic `preview-pane.ts`. One Markdown per option, lazy on
|
|
21
|
+
* first request, never re-constructed — count semantics frozen by tests.
|
|
22
|
+
*/
|
|
23
|
+
export class MarkdownContentCache {
|
|
24
|
+
private readonly previewTexts: Map<number, string>;
|
|
25
|
+
private readonly markdownCache: Map<number, Markdown>;
|
|
26
|
+
private cachedWidth: number | undefined;
|
|
27
|
+
private readonly theme: Theme;
|
|
28
|
+
private readonly markdownTheme: MarkdownTheme;
|
|
29
|
+
|
|
30
|
+
constructor(question: QuestionData, theme: Theme, markdownTheme: MarkdownTheme) {
|
|
31
|
+
this.theme = theme;
|
|
32
|
+
this.markdownTheme = markdownTheme;
|
|
33
|
+
this.previewTexts = new Map();
|
|
34
|
+
for (let i = 0; i < question.options.length; i++) {
|
|
35
|
+
const raw = question.options[i]?.preview;
|
|
36
|
+
if (raw && raw.length > 0) this.previewTexts.set(i, raw);
|
|
37
|
+
}
|
|
38
|
+
this.markdownCache = new Map();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
hasAnyPreview(): boolean {
|
|
42
|
+
return this.previewTexts.size > 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
has(optionIndex: number): boolean {
|
|
46
|
+
return this.previewTexts.has(optionIndex);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Compute the body lines for a given option at a given inner width. Width changes
|
|
51
|
+
* invalidate the per-Markdown render cache.
|
|
52
|
+
*/
|
|
53
|
+
bodyFor(optionIndex: number, innerWidth: number): string[] {
|
|
54
|
+
if (this.cachedWidth !== innerWidth) {
|
|
55
|
+
for (const md of this.markdownCache.values()) md.invalidate();
|
|
56
|
+
this.cachedWidth = innerWidth;
|
|
57
|
+
}
|
|
58
|
+
const text = this.previewTexts.get(optionIndex);
|
|
59
|
+
if (!text) {
|
|
60
|
+
const placeholder = this.theme.fg("dim", NO_PREVIEW_TEXT);
|
|
61
|
+
const pad = Math.max(0, innerWidth - visibleWidth(placeholder));
|
|
62
|
+
return [placeholder + " ".repeat(pad)];
|
|
63
|
+
}
|
|
64
|
+
let md = this.markdownCache.get(optionIndex);
|
|
65
|
+
if (!md) {
|
|
66
|
+
md = new Markdown(text, 0, 0, this.markdownTheme);
|
|
67
|
+
this.markdownCache.set(optionIndex, md);
|
|
68
|
+
}
|
|
69
|
+
return stripFenceMarkers(md.render(innerWidth));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
invalidate(): void {
|
|
73
|
+
for (const md of this.markdownCache.values()) md.invalidate();
|
|
74
|
+
this.cachedWidth = undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/multi-select-options.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import {
|
|
2
|
+
import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@mariozechner/pi-tui";
|
|
3
3
|
import type { DialogState } from "./dialog-builder.js";
|
|
4
|
+
import type { StatefulComponent } from "./stateful-component.js";
|
|
4
5
|
import type { QuestionData } from "./types.js";
|
|
5
6
|
|
|
6
7
|
const ACTIVE_POINTER = "❯ ";
|
|
@@ -24,7 +25,7 @@ const NEXT_LABEL = "Next";
|
|
|
24
25
|
*
|
|
25
26
|
* `setState(state)` is a pure field reassignment — no render, no invalidate side effects.
|
|
26
27
|
*/
|
|
27
|
-
export class MultiSelectOptions implements
|
|
28
|
+
export class MultiSelectOptions implements StatefulComponent<DialogState> {
|
|
28
29
|
private state: DialogState;
|
|
29
30
|
/**
|
|
30
31
|
* When false, the active-row pointer (`❯`) and the active-row accent/bold styling are
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-ask-user-question",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.3",
|
|
4
4
|
"description": "Pi extension: lets the model ask you a clarifying question with structured options instead of guessing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -26,21 +26,29 @@
|
|
|
26
26
|
"test": "vitest run"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
|
-
"index.ts",
|
|
30
29
|
"ask-user-question.ts",
|
|
31
|
-
"
|
|
32
|
-
"types.ts",
|
|
33
|
-
"columns.ts",
|
|
34
|
-
"tab-bar.ts",
|
|
35
|
-
"preview-pane.ts",
|
|
36
|
-
"dispatch.ts",
|
|
30
|
+
"body-residual-spacer.ts",
|
|
37
31
|
"dialog-builder.ts",
|
|
38
|
-
"
|
|
32
|
+
"dispatch.ts",
|
|
33
|
+
"format-answer.ts",
|
|
34
|
+
"index.ts",
|
|
35
|
+
"LICENSE",
|
|
36
|
+
"markdown-content-cache.ts",
|
|
39
37
|
"multi-select-options.ts",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
38
|
+
"preview-box-renderer.ts",
|
|
39
|
+
"preview-layout-decider.ts",
|
|
40
|
+
"preview-pane.ts",
|
|
41
|
+
"questionnaire-session.ts",
|
|
42
|
+
"questionnaire-state.ts",
|
|
42
43
|
"README.md",
|
|
43
|
-
"
|
|
44
|
+
"response-envelope.ts",
|
|
45
|
+
"stateful-component.ts",
|
|
46
|
+
"submit-picker.ts",
|
|
47
|
+
"tab-bar.ts",
|
|
48
|
+
"tab-strategy.ts",
|
|
49
|
+
"types.ts",
|
|
50
|
+
"validate-questionnaire.ts",
|
|
51
|
+
"wrapping-select.ts"
|
|
44
52
|
],
|
|
45
53
|
"pi": {
|
|
46
54
|
"extensions": [
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
2
|
+
|
|
3
|
+
const ANSI_SGR_RE = /\x1b\[[0-9;]*m/g;
|
|
4
|
+
const ANSI_OSC8_RE = /\x1b\]8;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
5
|
+
const FENCE_MARKER_RE = /^`{3}/;
|
|
6
|
+
|
|
7
|
+
/** Top + bottom border rows consumed by `renderBorderedBox`. */
|
|
8
|
+
export const BORDER_VERTICAL_OVERHEAD = 2;
|
|
9
|
+
/** Left + right vertical bar columns (`│ ... │`) consumed by `renderBorderedBox`. */
|
|
10
|
+
export const BORDER_HORIZONTAL_OVERHEAD = 2;
|
|
11
|
+
/** Inner horizontal padding (1 col) between each border bar and content area. */
|
|
12
|
+
export const BORDER_INNER_PADDING_HORIZONTAL = 1;
|
|
13
|
+
/** Floor for the preview box's inner content width — CC parity (`PreviewBox.minWidth`). */
|
|
14
|
+
export const BOX_MIN_CONTENT_WIDTH = 40;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Drops fenced-code-block marker lines (` ``` ` opener/closer) from rendered markdown.
|
|
18
|
+
* pi-tui's Markdown emits literal opening ` ```lang ` and closing ` ``` ` lines around
|
|
19
|
+
* code blocks; this strip leaves only the highlighted code body. Inline code
|
|
20
|
+
* (`codespan`) is unaffected — pi-tui already renders it without backticks.
|
|
21
|
+
*/
|
|
22
|
+
export function stripFenceMarkers(lines: readonly string[]): string[] {
|
|
23
|
+
return lines.filter((line) => {
|
|
24
|
+
const clean = line.replace(ANSI_SGR_RE, "").replace(ANSI_OSC8_RE, "");
|
|
25
|
+
return !FENCE_MARKER_RE.test(clean);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Wraps `lines` in a 4-sided ASCII border with 1 col of inner horizontal padding.
|
|
31
|
+
* Layout per content row: `│` + ` ` + content padded to `contentInner` + ` ` + `│`,
|
|
32
|
+
* where `contentInner = width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL`.
|
|
33
|
+
* Top/bottom dash runs span corner-to-corner (`width - BORDER_HORIZONTAL_OVERHEAD`). When
|
|
34
|
+
* `hidden > 0`, the bottom-row dash run is replaced with ` ✂ ── N lines hidden ── ` (corners stay).
|
|
35
|
+
*/
|
|
36
|
+
export function renderBorderedBox(
|
|
37
|
+
lines: readonly string[],
|
|
38
|
+
width: number,
|
|
39
|
+
colorFn: (s: string) => string,
|
|
40
|
+
hidden = 0,
|
|
41
|
+
): string[] {
|
|
42
|
+
const dashSpan = Math.max(1, width - BORDER_HORIZONTAL_OVERHEAD);
|
|
43
|
+
const contentInner = Math.max(1, dashSpan - 2 * BORDER_INNER_PADDING_HORIZONTAL);
|
|
44
|
+
const pad = " ".repeat(BORDER_INNER_PADDING_HORIZONTAL);
|
|
45
|
+
const top = colorFn(`┌${"─".repeat(dashSpan)}┐`);
|
|
46
|
+
const out: string[] = [top];
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
const padded = truncateToWidth(line, contentInner, "", true);
|
|
49
|
+
out.push(`${colorFn("│")}${pad}${padded}${pad}${colorFn("│")}`);
|
|
50
|
+
}
|
|
51
|
+
if (hidden > 0) {
|
|
52
|
+
const indicator = ` ✂ ── ${hidden} lines hidden ── `;
|
|
53
|
+
const space = dashSpan - indicator.length;
|
|
54
|
+
const leftFill = "─".repeat(Math.max(0, Math.floor(space / 2)));
|
|
55
|
+
const rightFill = "─".repeat(Math.max(0, dashSpan - leftFill.length - indicator.length));
|
|
56
|
+
out.push(colorFn(`└${leftFill}${indicator}${rightFill}┘`));
|
|
57
|
+
} else {
|
|
58
|
+
out.push(colorFn(`└${"─".repeat(dashSpan)}┘`));
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Compute box dimensions from content lines. Pure of args.
|
|
65
|
+
*
|
|
66
|
+
* CC parity:
|
|
67
|
+
* contentWidth = max(minWidth, widestRenderedLine)
|
|
68
|
+
* boxWidth = min(contentWidth + 4, effectiveMaxWidth)
|
|
69
|
+
*
|
|
70
|
+
* Trailing whitespace is stripped before measuring because pi-tui's
|
|
71
|
+
* `Markdown.render(width)` pads every line to `width`, which would otherwise force
|
|
72
|
+
* the box to fill the whole column allocation.
|
|
73
|
+
*/
|
|
74
|
+
export function computeBoxDimensions(
|
|
75
|
+
contentLines: readonly string[],
|
|
76
|
+
maxInnerWidth: number,
|
|
77
|
+
): { innerWidth: number; boxWidth: number } {
|
|
78
|
+
let widest = Math.min(BOX_MIN_CONTENT_WIDTH, maxInnerWidth);
|
|
79
|
+
for (const line of contentLines) {
|
|
80
|
+
const w = visibleWidth(line.replace(/\s+$/, ""));
|
|
81
|
+
if (w > widest) widest = w;
|
|
82
|
+
}
|
|
83
|
+
const innerWidth = Math.min(widest, maxInnerWidth);
|
|
84
|
+
const boxWidth = innerWidth + BORDER_HORIZONTAL_OVERHEAD + 2 * BORDER_INNER_PADDING_HORIZONTAL;
|
|
85
|
+
return { innerWidth, boxWidth };
|
|
86
|
+
}
|