@juicesharp/rpiv-ask-user-question 2.12.0 → 2.13.1
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 +1 -1
- package/ask-user-question.ts +86 -38
- package/docs/configuration.md +1 -1
- package/docs/keyboard.md +38 -11
- package/package.json +3 -2
- package/presentation.ts +122 -0
- package/state/build-questionnaire.ts +9 -1
- package/state/questionnaire-session.ts +5 -1
- package/state/state.ts +1 -1
- package/view/dialog-builder.ts +61 -29
- package/view/tab-content-strategy.ts +3 -1
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ When the model asks several things at once, `Tab` moves between them and a Submi
|
|
|
42
42
|
- **Compare real artifacts, not just labels** — an option can carry a markdown `preview` (ASCII mockup, code, diagram, config) that renders in a bordered box beside the option list.
|
|
43
43
|
- **One interruption, not five** — up to four questions arrive in a single tabbed dialog, and the Submit tab lists your answers and names anything still blank before you commit.
|
|
44
44
|
- **Notes on any answer — or on all of them** — `n` opens a multiline note editor on any question tab, and on the Submit tab it opens one global note for the whole questionnaire. Per-question notes reach the model as `user notes: <text>`, the global note as `global note: <text>`; neither marks a question answered.
|
|
45
|
-
- **
|
|
45
|
+
- **Keep reading while you answer** — the question opens below the transcript, and `PageUp`/`PageDown` or the mouse wheel keep scrolling the conversation; `Ctrl+]` shrinks the dialog to one line when you want more room, then brings it back with your answers intact.
|
|
46
46
|
- **Works outside the terminal too** — in RPC and ACP hosts such as the VS Code pendant or Zed the questionnaire walks through the host's native dialogs (notes are terminal-only and do not carry over), and in non-interactive runs the tool is removed from the model's tool list instead of failing every call.
|
|
47
47
|
|
|
48
48
|
## Configuration
|
package/ask-user-question.ts
CHANGED
|
@@ -15,6 +15,14 @@ import {
|
|
|
15
15
|
} from "./events.js";
|
|
16
16
|
// Static import is fine — rpc-fallback pulls only types + the i18n bridge,
|
|
17
17
|
// none of the ~560ms TUI render graph that QuestionnaireSession lazy-loads.
|
|
18
|
+
import {
|
|
19
|
+
mustYieldInlineSlot,
|
|
20
|
+
type Presentation,
|
|
21
|
+
resolvePresentation,
|
|
22
|
+
rowBudgetForHost,
|
|
23
|
+
STACKED_OVERLAY_PRESENTATION,
|
|
24
|
+
YIELD_TO_OVERLAY,
|
|
25
|
+
} from "./presentation.js";
|
|
18
26
|
import { type DialogUI, hasDialogUI, runRpcQuestionnaire } from "./rpc-fallback.js";
|
|
19
27
|
import { displayLabel, t } from "./state/i18n-bridge.js";
|
|
20
28
|
import { sentinelsToAppend } from "./state/row-intent.js";
|
|
@@ -190,16 +198,23 @@ function makeSessionFactory(config: {
|
|
|
190
198
|
itemsByTab: WrappingSelectItem[][];
|
|
191
199
|
collapseKey: string;
|
|
192
200
|
canReopenWhileHidden: boolean;
|
|
201
|
+
presentation: Presentation;
|
|
193
202
|
sessionRef: SessionRef;
|
|
194
203
|
Session: SessionModule["QuestionnaireSession"];
|
|
195
204
|
}) {
|
|
196
|
-
const { ctx, typed, itemsByTab, collapseKey, canReopenWhileHidden, sessionRef, Session } = config;
|
|
205
|
+
const { ctx, typed, itemsByTab, collapseKey, canReopenWhileHidden, presentation, sessionRef, Session } = config;
|
|
197
206
|
return (
|
|
198
207
|
tui: TUI,
|
|
199
208
|
theme: Theme,
|
|
200
209
|
keybindings: import("./state/questionnaire-session.js").QuestionnaireSessionConfig["keybindings"],
|
|
201
210
|
done: (result: QuestionnaireResult) => void,
|
|
202
211
|
): import("./state/questionnaire-session.js").QuestionnaireSessionComponent => {
|
|
212
|
+
// The factory runs at mount time — the first point the TUI is reachable — so this is
|
|
213
|
+
// where an inline mount notices an overlay already on screen and gives the slot up.
|
|
214
|
+
if (!presentation.overlay && mustYieldInlineSlot(tui)) {
|
|
215
|
+
done(YIELD_TO_OVERLAY);
|
|
216
|
+
return INERT_COMPONENT;
|
|
217
|
+
}
|
|
203
218
|
const session = new Session({
|
|
204
219
|
tui,
|
|
205
220
|
theme,
|
|
@@ -226,12 +241,69 @@ function makeSessionFactory(config: {
|
|
|
226
241
|
},
|
|
227
242
|
collapseKey,
|
|
228
243
|
canReopenWhileHidden,
|
|
244
|
+
rowBudget: rowBudgetForHost(tui.terminal, presentation.rowBudget),
|
|
229
245
|
});
|
|
230
246
|
sessionRef.current = session;
|
|
231
247
|
return session.component;
|
|
232
248
|
};
|
|
233
249
|
}
|
|
234
250
|
|
|
251
|
+
/** Stand-in returned by a mount that resolved before mounting (Pi discards it unmounted). */
|
|
252
|
+
const INERT_COMPONENT: import("./state/questionnaire-session.js").QuestionnaireSessionComponent = {
|
|
253
|
+
render: () => [],
|
|
254
|
+
invalidate: () => {},
|
|
255
|
+
handleInput: () => {},
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* One `ctx.ui.custom` mount under `presentation`. The overlay additionally gets its handle
|
|
260
|
+
* captured (so collapse can `setHidden()`) and a raw terminal listener for the collapse key,
|
|
261
|
+
* which stays reachable while the overlay is hidden (pi-tui routes no input to a hidden
|
|
262
|
+
* overlay's `component.handleInput`). Inline has no handle to hide and keeps focus while
|
|
263
|
+
* collapsed, so it needs no listener.
|
|
264
|
+
*/
|
|
265
|
+
async function mountQuestionnaire(config: {
|
|
266
|
+
ctx: ExtensionContext;
|
|
267
|
+
typed: QuestionParams;
|
|
268
|
+
itemsByTab: WrappingSelectItem[][];
|
|
269
|
+
collapseKey: string;
|
|
270
|
+
presentation: Presentation;
|
|
271
|
+
Session: SessionModule["QuestionnaireSession"];
|
|
272
|
+
}): Promise<QuestionnaireResult | undefined> {
|
|
273
|
+
const { ctx, collapseKey, presentation } = config;
|
|
274
|
+
const sessionRef: SessionRef = { current: null };
|
|
275
|
+
const overlayHandleRef: OverlayHandleRef = { current: undefined };
|
|
276
|
+
const removeOverlayInputListener = presentation.hideOnCollapse
|
|
277
|
+
? registerCollapseKeyListener(ctx, collapseKey, sessionRef, overlayHandleRef)
|
|
278
|
+
: undefined;
|
|
279
|
+
// Hiding the overlay is only reversible through the raw listener above, so the session
|
|
280
|
+
// may emit `setHidden` only when it was actually registered; otherwise collapse falls
|
|
281
|
+
// back to the visible one-line row.
|
|
282
|
+
const canReopenWhileHidden = removeOverlayInputListener !== undefined;
|
|
283
|
+
try {
|
|
284
|
+
return await ctx.ui.custom<QuestionnaireResult>(
|
|
285
|
+
makeSessionFactory({ ...config, canReopenWhileHidden, sessionRef }),
|
|
286
|
+
presentation.overlay
|
|
287
|
+
? {
|
|
288
|
+
overlay: true,
|
|
289
|
+
overlayOptions: {
|
|
290
|
+
anchor: "bottom-center",
|
|
291
|
+
width: "100%",
|
|
292
|
+
maxHeight: "100%",
|
|
293
|
+
margin: { left: 0, right: 0, bottom: 0 },
|
|
294
|
+
},
|
|
295
|
+
onHandle: (handle) => {
|
|
296
|
+
overlayHandleRef.current = handle;
|
|
297
|
+
sessionRef.current?.setOverlayHandle(handle);
|
|
298
|
+
},
|
|
299
|
+
}
|
|
300
|
+
: { overlay: false },
|
|
301
|
+
);
|
|
302
|
+
} finally {
|
|
303
|
+
removeOverlayInputListener?.();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
235
307
|
/**
|
|
236
308
|
* A TUI questionnaire ALWAYS resolves a QuestionnaireResult (cancel included), so
|
|
237
309
|
* `undefined` uniquely means "host cannot render", never "user declined". RPC builds
|
|
@@ -353,46 +425,22 @@ export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
|
|
|
353
425
|
// via the `collapseKey` config field. `resolveCollapseKey` also accepts the
|
|
354
426
|
// sentinel value `"off"` to disable the shortcut entirely.
|
|
355
427
|
const collapseKey = resolveCollapseKey(loadConfig());
|
|
356
|
-
|
|
357
|
-
//
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const sessionRef: SessionRef = { current: null };
|
|
362
|
-
const overlayHandleRef: OverlayHandleRef = { current: undefined };
|
|
363
|
-
const removeOverlayInputListener = registerCollapseKeyListener(ctx, collapseKey, sessionRef, overlayHandleRef);
|
|
364
|
-
// Hiding the overlay is only reversible through the raw listener above, so
|
|
365
|
-
// the session may emit `setHidden` only when it was actually registered;
|
|
366
|
-
// otherwise collapse falls back to the visible one-line row.
|
|
367
|
-
const canReopenWhileHidden = removeOverlayInputListener !== undefined;
|
|
428
|
+
// Pi's editor slot (#253); the overlay only while another questionnaire holds the slot,
|
|
429
|
+
// or stacked when another overlay is already on screen (see below).
|
|
430
|
+
let presentation = resolvePresentation();
|
|
431
|
+
const mount = (p: Presentation) =>
|
|
432
|
+
mountQuestionnaire({ ctx, typed, itemsByTab, collapseKey, presentation: p, Session: QuestionnaireSession });
|
|
368
433
|
|
|
369
434
|
emitAskUserBlockedEvent(pi, true);
|
|
370
435
|
try {
|
|
371
436
|
emitTerminalAttention();
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
sessionRef,
|
|
380
|
-
Session: QuestionnaireSession,
|
|
381
|
-
}),
|
|
382
|
-
{
|
|
383
|
-
overlay: true,
|
|
384
|
-
overlayOptions: {
|
|
385
|
-
anchor: "bottom-center",
|
|
386
|
-
width: "100%",
|
|
387
|
-
maxHeight: "100%",
|
|
388
|
-
margin: { left: 0, right: 0, bottom: 0 },
|
|
389
|
-
},
|
|
390
|
-
onHandle: (handle) => {
|
|
391
|
-
overlayHandleRef.current = handle;
|
|
392
|
-
sessionRef.current?.setOverlayHandle(handle);
|
|
393
|
-
},
|
|
394
|
-
},
|
|
395
|
-
);
|
|
437
|
+
let result = await mount(presentation);
|
|
438
|
+
if (result === YIELD_TO_OVERLAY) {
|
|
439
|
+
// Another overlay was on screen: stack on top of it instead (see mustYieldInlineSlot).
|
|
440
|
+
presentation.release?.();
|
|
441
|
+
presentation = STACKED_OVERLAY_PRESENTATION;
|
|
442
|
+
result = await mount(presentation);
|
|
443
|
+
}
|
|
396
444
|
|
|
397
445
|
if (result === undefined) {
|
|
398
446
|
return resolveUndefinedResult(ctx, typed);
|
|
@@ -400,7 +448,7 @@ export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
|
|
|
400
448
|
|
|
401
449
|
return buildQuestionnaireResponse(result, typed);
|
|
402
450
|
} finally {
|
|
403
|
-
|
|
451
|
+
presentation.release?.();
|
|
404
452
|
emitAskUserBlockedEvent(pi, false);
|
|
405
453
|
}
|
|
406
454
|
},
|
package/docs/configuration.md
CHANGED
|
@@ -58,7 +58,7 @@ type are likewise dropped back to their default without a warning.
|
|
|
58
58
|
|
|
59
59
|
| Setting | What it does | Default |
|
|
60
60
|
| --- | --- | --- |
|
|
61
|
-
| `collapseKey` | Key that collapses and expands the dialog
|
|
61
|
+
| `collapseKey` | Key that collapses and expands the dialog. | `"ctrl+]"` |
|
|
62
62
|
| `guidance.description` | Full text of the tool description the model sees. Replaces the built-in default entirely — no merging. | built-in description |
|
|
63
63
|
| `guidance.promptSnippet` | One-line snippet describing the tool in the system prompt. | built-in snippet |
|
|
64
64
|
| `guidance.promptGuidelines` | List of usage guidelines given to the model. | 4 built-in guidelines |
|
package/docs/keyboard.md
CHANGED
|
@@ -70,20 +70,47 @@ nothing but a global note still returns an answered result rather than a decline
|
|
|
70
70
|
Inside the editor, `Shift+Enter` inserts a newline, while `Esc` and `Enter` close it; other
|
|
71
71
|
keystrokes edit the buffer, so `n` types an `n`. Pasted line breaks are preserved.
|
|
72
72
|
|
|
73
|
-
##
|
|
73
|
+
## Where the dialog appears
|
|
74
|
+
|
|
75
|
+
The questionnaire opens in Pi's editor area, below the transcript — where Pi's own select
|
|
76
|
+
and input prompts appear. The transcript keeps scrolling with `PageUp`/`PageDown` and the
|
|
77
|
+
wheel while the dialog keeps its own keys. The dialog takes at most 70% of the terminal
|
|
78
|
+
height (at least 12 rows); a taller questionnaire scrolls its options inside that space,
|
|
79
|
+
keeping the header, the question, the focused option and the key hints visible.
|
|
80
|
+
|
|
81
|
+
Wheel and trackpad scrolling reach the transcript only when the terminal reports them as
|
|
82
|
+
mouse events. A terminal with mouse reporting turned off (Warp, for one) converts them
|
|
83
|
+
into ↑/↓ key presses instead, which move between the dialog's options. `PageUp`/`PageDown`
|
|
84
|
+
(`Fn+↑`/`Fn+↓` on a Mac keyboard) scroll the transcript either way.
|
|
74
85
|
|
|
75
|
-
|
|
76
|
-
stack and shrinks to a single dim hint row, so the transcript it was covering becomes
|
|
77
|
-
readable and chat scrolling resumes. Press the same key to bring the questionnaire back
|
|
78
|
-
with your answers intact. The first time you collapse, Pi notifies you with the key to
|
|
79
|
-
press — that message names your configured key.
|
|
86
|
+
Two cases open the questionnaire as an overlay instead:
|
|
80
87
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
88
|
+
- **A second question while one is open.** Only one questionnaire fits in the editor area,
|
|
89
|
+
so the second covers the bottom of the transcript as an overlay.
|
|
90
|
+
- **A question arriving while another overlay, such as `/btw`, is open.** The question
|
|
91
|
+
stacks on top, so the question you see is the one your keys reach. `Ctrl+]` shrinks it to
|
|
92
|
+
its one-line row so `/btw` is readable behind it; the question keeps the keys, so `Esc`
|
|
93
|
+
cancels the question, and `/btw` closes after the question is answered or cancelled.
|
|
94
|
+
|
|
95
|
+
## Collapse mode
|
|
84
96
|
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
`Ctrl+]` gets the dialog out of the way. In the editor area it shrinks the questionnaire
|
|
98
|
+
to a single dim hint row for more room. The row keeps focus, so the same key brings the
|
|
99
|
+
questionnaire back with your answers intact.
|
|
100
|
+
|
|
101
|
+
When the questionnaire opened as an overlay for a second question, collapse marks the
|
|
102
|
+
overlay hidden in Pi's overlay stack, so the transcript it was covering
|
|
103
|
+
becomes readable and chat scrolling resumes. The first time you collapse, Pi notifies you
|
|
104
|
+
with the key to press — that message names your configured key. Because Pi routes no
|
|
105
|
+
input to a hidden overlay, the collapse key is additionally captured at the raw terminal
|
|
106
|
+
level. It only acts when the questionnaire is hidden or focused, so a different overlay
|
|
107
|
+
on top of it (for example `/btw`) keeps its keystrokes. A question that opened on top of
|
|
108
|
+
an overlay that was already showing (for example `/btw`) is never hidden this way: collapse
|
|
109
|
+
shrinks it to the one-line row and it keeps focus, because Pi closes overlays from the top
|
|
110
|
+
of the stack and would otherwise remove the hidden question when `/btw` closes.
|
|
111
|
+
|
|
112
|
+
While collapsed, every keystroke other than cancel or expand is ignored, so you cannot
|
|
113
|
+
mutate answers you cannot see.
|
|
87
114
|
|
|
88
115
|
The default `ctrl+]` is free in Terminal.app, iTerm2, Warp, tmux, zellij and screen. On
|
|
89
116
|
keyboard layouts where `]` sits on the shifted layer — Latin American `es-AR` / `es-MX`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-ask-user-question",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.1",
|
|
4
4
|
"description": "Pi extension. A structured questionnaire the model can put to you when it would otherwise guess, with typed options instead of free-form replies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"config.ts",
|
|
36
36
|
"events.ts",
|
|
37
37
|
"index.ts",
|
|
38
|
+
"presentation.ts",
|
|
38
39
|
"reconcile.ts",
|
|
39
40
|
"LICENSE",
|
|
40
41
|
"locales/",
|
|
@@ -96,6 +97,6 @@
|
|
|
96
97
|
}
|
|
97
98
|
},
|
|
98
99
|
"dependencies": {
|
|
99
|
-
"@juicesharp/rpiv-config": "^2.
|
|
100
|
+
"@juicesharp/rpiv-config": "^2.13.1"
|
|
100
101
|
}
|
|
101
102
|
}
|
package/presentation.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { QuestionnaireResult } from "./tool/types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Inline row budget. A component in Pi's editor slot only receives a width, and the
|
|
5
|
+
* host clips the slot from the bottom when the dock outgrows the screen, so the inline
|
|
6
|
+
* dialog caps itself — the same idiom Pi's own editor uses in that slot
|
|
7
|
+
* (`max(5, floor(rows * 0.3))`). The floor keeps the dialog chrome plus a few option
|
|
8
|
+
* rows; past the cap, `DialogView`'s scroll-to-focus partition takes over.
|
|
9
|
+
*/
|
|
10
|
+
export const INLINE_ROW_RATIO = 0.7;
|
|
11
|
+
export const INLINE_MIN_ROWS = 12;
|
|
12
|
+
|
|
13
|
+
/** Maps the terminal's row count to the rows the dialog may render. */
|
|
14
|
+
export type RowBudget = (terminalRows: number) => number;
|
|
15
|
+
|
|
16
|
+
/** The whole of `terminal.rows`: the overlay, and any host that already allocates the dialog's rows. */
|
|
17
|
+
export const fullRowBudget: RowBudget = (terminalRows) => terminalRows;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Marker a host sets on `tui.terminal` when it reports the rows it has allocated to the
|
|
21
|
+
* questionnaire as `terminal.rows` (rpiv-pi's lane console `cappedTui`). The inline cap
|
|
22
|
+
* exists to leave the transcript room in Pi's editor slot; applying it to an allocation
|
|
23
|
+
* would shrink an already-sized band a second time. A registry symbol, so hosts need no
|
|
24
|
+
* import from this package.
|
|
25
|
+
*/
|
|
26
|
+
export const HOST_ALLOCATED_ROWS = Symbol.for("rpiv-ask-user-question:host-allocated-rows");
|
|
27
|
+
|
|
28
|
+
function hostAllocatesRows(terminal: object): boolean {
|
|
29
|
+
return (terminal as Record<symbol, unknown>)[HOST_ALLOCATED_ROWS] === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The presentation's budget, unless the host has already allocated the rows itself. */
|
|
33
|
+
export function rowBudgetForHost(terminal: object, budget: RowBudget): RowBudget {
|
|
34
|
+
return hostAllocatesRows(terminal) ? fullRowBudget : budget;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolved by an inline mount that gives the editor slot up (see `mustYieldInlineSlot`);
|
|
39
|
+
* `execute()` remounts as an overlay on seeing it. Compared by identity — a valid cancel
|
|
40
|
+
* result, so it is harmless should it ever escape.
|
|
41
|
+
*/
|
|
42
|
+
export const YIELD_TO_OVERLAY: QuestionnaireResult = Object.freeze({
|
|
43
|
+
answers: [],
|
|
44
|
+
cancelled: true,
|
|
45
|
+
}) as QuestionnaireResult;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* True when an inline mount would land underneath an overlay already on screen (e.g. an
|
|
49
|
+
* open `/btw`). pi-tui hands focus to the newly mounted editor-slot component while
|
|
50
|
+
* that overlay keeps painting over it, so keys — Esc included — would reach a question
|
|
51
|
+
* the user cannot see. Stacking as an overlay instead keeps the question visible and
|
|
52
|
+
* focused, and pi-tui returns focus to the other overlay when it closes. A host that
|
|
53
|
+
* allocates the rows itself (rpiv-pi lane console) owns its geometry and is left alone.
|
|
54
|
+
*/
|
|
55
|
+
export function mustYieldInlineSlot(tui: { terminal: object; hasOverlay?: () => boolean }): boolean {
|
|
56
|
+
return !hostAllocatesRows(tui.terminal) && typeof tui.hasOverlay === "function" && tui.hasOverlay();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const inlineRowBudget: RowBudget = (terminalRows) =>
|
|
60
|
+
Math.min(terminalRows, Math.max(INLINE_MIN_ROWS, Math.floor(terminalRows * INLINE_ROW_RATIO)));
|
|
61
|
+
|
|
62
|
+
export interface Presentation {
|
|
63
|
+
/** True → focused overlay (`ctx.ui.custom` overlay options); false → Pi's editor slot. */
|
|
64
|
+
overlay: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Overlay only: collapse may fully hide it (`OverlayHandle.setHidden`, reopened by the
|
|
67
|
+
* raw collapse listener). False when stacked over another overlay — see
|
|
68
|
+
* `STACKED_OVERLAY_PRESENTATION`.
|
|
69
|
+
*/
|
|
70
|
+
hideOnCollapse?: boolean;
|
|
71
|
+
rowBudget: RowBudget;
|
|
72
|
+
/** Frees the inline slot; present only when this presentation claimed it. Call once, on teardown. */
|
|
73
|
+
release?: () => void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Pi runs tool calls in parallel by default and has a single editor slot: a second
|
|
77
|
+
// inline mount would replace the first, whose `custom()` promise would never settle.
|
|
78
|
+
// Overlays stack safely, so a questionnaire that finds the slot taken degrades to one
|
|
79
|
+
// instead of waiting — waiting could stall on a lane's parked question indefinitely.
|
|
80
|
+
let inlineSlotHeld = false;
|
|
81
|
+
|
|
82
|
+
function claimInlineSlot(): (() => void) | undefined {
|
|
83
|
+
if (inlineSlotHeld) return undefined;
|
|
84
|
+
inlineSlotHeld = true;
|
|
85
|
+
let released = false;
|
|
86
|
+
return () => {
|
|
87
|
+
if (released) return;
|
|
88
|
+
released = true;
|
|
89
|
+
inlineSlotHeld = false;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const OVERLAY_PRESENTATION: Presentation = Object.freeze({
|
|
94
|
+
overlay: true,
|
|
95
|
+
rowBudget: fullRowBudget,
|
|
96
|
+
hideOnCollapse: true,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The overlay a question stacks as when another overlay (e.g. `/btw`) is already open.
|
|
101
|
+
* Collapse keeps it visible as the one-row hint instead of hiding it: Pi closes an
|
|
102
|
+
* overlay-mode `ui.custom` with `hideOverlay()`, which pops the TOPMOST overlay — a
|
|
103
|
+
* hidden questionnaire above `/btw` would be popped when `/btw` closes, stranding `/btw`
|
|
104
|
+
* on screen and leaving the questionnaire's promise unsettled (the agent hangs). The
|
|
105
|
+
* visible row keeps focus, so the overlay below stays readable but is closed after the
|
|
106
|
+
* question.
|
|
107
|
+
*/
|
|
108
|
+
export const STACKED_OVERLAY_PRESENTATION: Presentation = Object.freeze({
|
|
109
|
+
overlay: true,
|
|
110
|
+
rowBudget: fullRowBudget,
|
|
111
|
+
hideOnCollapse: false,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Inline in Pi's editor slot — where Pi's own select/input/editor prompts mount, so the
|
|
116
|
+
* transcript keeps scrolling while a question is open (#253). The overlay is only the
|
|
117
|
+
* fallback when another questionnaire already holds the slot.
|
|
118
|
+
*/
|
|
119
|
+
export function resolvePresentation(): Presentation {
|
|
120
|
+
const release = claimInlineSlot();
|
|
121
|
+
return release ? { overlay: false, rowBudget: inlineRowBudget, release } : OVERLAY_PRESENTATION;
|
|
122
|
+
}
|
|
@@ -47,6 +47,12 @@ export interface QuestionnaireBuildConfig {
|
|
|
47
47
|
* of runtime context.
|
|
48
48
|
*/
|
|
49
49
|
collapseKey: string;
|
|
50
|
+
/**
|
|
51
|
+
* Maps `tui.terminal.rows` to the rows the dialog may render — identity for the
|
|
52
|
+
* overlay, a capped share for the inline editor slot (`../presentation.ts`). Applied
|
|
53
|
+
* inside `getTerminalRows`, so `DialogView`'s overflow partition is mode-agnostic.
|
|
54
|
+
*/
|
|
55
|
+
rowBudget: (terminalRows: number) => number;
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
export interface QuestionnaireBuilt {
|
|
@@ -117,13 +123,14 @@ class QuestionnaireBuilder {
|
|
|
117
123
|
private readonly initialState: QuestionnaireState;
|
|
118
124
|
private readonly getCurrentTab: () => number;
|
|
119
125
|
private readonly collapseKey: string;
|
|
126
|
+
private readonly rowBudget: (terminalRows: number) => number;
|
|
120
127
|
|
|
121
128
|
private readonly selectTheme: WrappingSelectTheme;
|
|
122
129
|
private readonly markdownTheme = getMarkdownTheme();
|
|
123
130
|
private readonly notesInput: Editor;
|
|
124
131
|
private readonly inlineInput: Editor;
|
|
125
132
|
private readonly getTerminalWidth = () => this.tui.terminal.columns;
|
|
126
|
-
private readonly getTerminalRows = () => this.tui.terminal.rows;
|
|
133
|
+
private readonly getTerminalRows = () => this.rowBudget(this.tui.terminal.rows);
|
|
127
134
|
|
|
128
135
|
constructor(config: QuestionnaireBuildConfig) {
|
|
129
136
|
this.tui = config.tui;
|
|
@@ -134,6 +141,7 @@ class QuestionnaireBuilder {
|
|
|
134
141
|
this.initialState = config.initialState;
|
|
135
142
|
this.getCurrentTab = config.getCurrentTab;
|
|
136
143
|
this.collapseKey = config.collapseKey;
|
|
144
|
+
this.rowBudget = config.rowBudget;
|
|
137
145
|
|
|
138
146
|
this.selectTheme = this.makeSelectTheme();
|
|
139
147
|
const textEditorTheme = editorTheme(this.theme);
|
|
@@ -30,6 +30,8 @@ export interface QuestionnaireSessionConfig {
|
|
|
30
30
|
* would hide the overlay into a state nothing can reopen.
|
|
31
31
|
*/
|
|
32
32
|
canReopenWhileHidden: boolean;
|
|
33
|
+
/** Terminal rows → rows the dialog may render; see `QuestionnaireBuildConfig.rowBudget`. */
|
|
34
|
+
rowBudget: (terminalRows: number) => number;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
export interface QuestionnaireSessionComponent {
|
|
@@ -107,6 +109,7 @@ export class QuestionnaireSession {
|
|
|
107
109
|
initialState: this.state,
|
|
108
110
|
getCurrentTab: () => this.state.currentTab,
|
|
109
111
|
collapseKey: this.collapseKey,
|
|
112
|
+
rowBudget: config.rowBudget,
|
|
110
113
|
});
|
|
111
114
|
|
|
112
115
|
this.notesInput = built.notesInput;
|
|
@@ -127,7 +130,8 @@ export class QuestionnaireSession {
|
|
|
127
130
|
}
|
|
128
131
|
|
|
129
132
|
/**
|
|
130
|
-
* Collapsed render: a single dim row
|
|
133
|
+
* Collapsed render: a single dim row (inline presentation: in the editor slot, where it
|
|
134
|
+
* keeps focus so the same key expands it). For the overlay, pi-tui sizes
|
|
131
135
|
* the overlay to `min(lines.length, maxHeight)`, so returning one line shrinks
|
|
132
136
|
* the bottom-anchored overlay from full-height to one row and the transcript
|
|
133
137
|
* behind it becomes readable (#47). The overlay stays focused and in the
|
package/state/state.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface QuestionnaireState {
|
|
|
33
33
|
* - Hosts with an `OverlayHandle` AND a raw `onTerminalInput` listener (real pi-tui):
|
|
34
34
|
* the `set_overlay_hidden` effect fully hides the overlay; the raw listener registered
|
|
35
35
|
* in `execute()` reopens it, because pi-tui routes no input to a hidden overlay.
|
|
36
|
-
* -
|
|
36
|
+
* - Inline presentation, or hosts without the raw listener/handle: the dialog stays visible and
|
|
37
37
|
* shrinks to a single hint row. The row keeps focus and input routing, so the same key
|
|
38
38
|
* expands it and Esc cancels, and the expand key never falls through to an underlying
|
|
39
39
|
* overlay (e.g. `/btw`). The session gates `set_overlay_hidden` on the listener's
|
package/view/dialog-builder.ts
CHANGED
|
@@ -57,6 +57,13 @@ export const REVIEW_HEADING = "Review your answers";
|
|
|
57
57
|
export const READY_PROMPT = "Ready to submit your answers?";
|
|
58
58
|
export const INCOMPLETE_WARNING_PREFIX = "⚠ Answer remaining questions before submitting:";
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Rows the scroll window must keep for the body before the heading (header badge +
|
|
62
|
+
* question) is pinned above it. Below this the heading scrolls with the body, so a
|
|
63
|
+
* tiny terminal still reaches its options.
|
|
64
|
+
*/
|
|
65
|
+
const MIN_SCROLL_ROWS_WITH_PINNED_HEADING = 3;
|
|
66
|
+
|
|
60
67
|
const OVERFLOW_UP = "↑";
|
|
61
68
|
const OVERFLOW_DOWN = "↓";
|
|
62
69
|
const OVERFLOW_BOTH = "↕";
|
|
@@ -75,30 +82,43 @@ function renderChromeOnly(natural: string[], topFixed: number, bottomFixed: numb
|
|
|
75
82
|
/** Scroll window start, centered on the focused option; top-anchored when there is no interactive focus. */
|
|
76
83
|
function computeScrollStart(
|
|
77
84
|
bodyRange: [number, number] | undefined,
|
|
78
|
-
|
|
85
|
+
rowsBeforeBody: number,
|
|
79
86
|
availableMiddle: number,
|
|
80
87
|
middleRows: number,
|
|
81
88
|
): number {
|
|
82
89
|
if (!bodyRange) return 0;
|
|
83
|
-
const focusedRowInMiddle =
|
|
90
|
+
const focusedRowInMiddle = rowsBeforeBody + bodyRange[0];
|
|
84
91
|
const focusedHeight = bodyRange[1] - bodyRange[0];
|
|
85
92
|
// Center the focused item vertically in the available middle space.
|
|
86
93
|
const idealStart = focusedRowInMiddle - Math.floor(Math.max(0, availableMiddle - focusedHeight) / 2);
|
|
87
94
|
return Math.max(0, Math.min(idealStart, middleRows - availableMiddle));
|
|
88
95
|
}
|
|
89
96
|
|
|
90
|
-
/**
|
|
91
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Mark the scroll window edges with overflow arrows; combined ↕ on a single-row middle.
|
|
99
|
+
* An arrow replaces the row it lands on, so rows inside `protectedRows` (the focused item,
|
|
100
|
+
* in window coordinates, end-exclusive) are never drawn over — losing the affordance beats
|
|
101
|
+
* losing the row the user is on (#261).
|
|
102
|
+
*/
|
|
103
|
+
function decorateOverflow(
|
|
104
|
+
scrollableMiddle: string[],
|
|
105
|
+
hasUp: boolean,
|
|
106
|
+
hasDown: boolean,
|
|
107
|
+
theme: Theme,
|
|
108
|
+
protectedRows: readonly [number, number] | undefined,
|
|
109
|
+
): void {
|
|
110
|
+
const drawable = (row: number) => !protectedRows || row < protectedRows[0] || row >= protectedRows[1];
|
|
92
111
|
if (hasUp && hasDown && scrollableMiddle.length === 1) {
|
|
93
112
|
// Single-row middle: combined ↕ avoids the prior collision where ↓ overwrote ↑.
|
|
94
|
-
scrollableMiddle[0] = theme.fg("dim", OVERFLOW_BOTH);
|
|
113
|
+
if (drawable(0)) scrollableMiddle[0] = theme.fg("dim", OVERFLOW_BOTH);
|
|
95
114
|
return;
|
|
96
115
|
}
|
|
97
|
-
if (hasUp && scrollableMiddle.length > 0) {
|
|
116
|
+
if (hasUp && scrollableMiddle.length > 0 && drawable(0)) {
|
|
98
117
|
scrollableMiddle[0] = theme.fg("dim", OVERFLOW_UP);
|
|
99
118
|
}
|
|
100
|
-
|
|
101
|
-
|
|
119
|
+
const last = scrollableMiddle.length - 1;
|
|
120
|
+
if (hasDown && last > 0 && drawable(last)) {
|
|
121
|
+
scrollableMiddle[last] = theme.fg("dim", OVERFLOW_DOWN);
|
|
102
122
|
}
|
|
103
123
|
}
|
|
104
124
|
|
|
@@ -124,7 +144,10 @@ export interface DialogConfig {
|
|
|
124
144
|
getBodyHeight: (width: number) => number;
|
|
125
145
|
/** Body height of the CURRENTLY active tab/option. The chrome subtracts this from `getBodyHeight` to absorb the residual OUTSIDE the bordered region. */
|
|
126
146
|
getCurrentBodyHeight: (width: number) => number;
|
|
127
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* Row budget getter, read at render time: `tui.terminal.rows` passed through the
|
|
149
|
+
* presentation's row budget (identity for the overlay, capped for inline).
|
|
150
|
+
*/
|
|
128
151
|
getTerminalRows: () => number;
|
|
129
152
|
/**
|
|
130
153
|
* Resolved collapse/expand key spec (`resolveCollapseKey` output: `"ctrl+]"`,
|
|
@@ -192,7 +215,6 @@ export class DialogView implements StatefulView<DialogProps> {
|
|
|
192
215
|
|
|
193
216
|
// Cache heading rows (avoid double construction in render and container build).
|
|
194
217
|
const headingRowCache = strategy.headingRows(state);
|
|
195
|
-
const headingCount = headingRowCache.length;
|
|
196
218
|
|
|
197
219
|
// Build container WITHOUT residual spacer — spacer handled below based on overflow.
|
|
198
220
|
const natural = this.buildContainerFromStrategy(strategy, headingRowCache).render(width);
|
|
@@ -201,7 +223,6 @@ export class DialogView implements StatefulView<DialogProps> {
|
|
|
201
223
|
// TabBar.render() returns [tabLine, ""] — always 2 rows.
|
|
202
224
|
const topFixed = 1 + (this.config.isMulti && this.config.tabBar ? 2 : 0) + 1;
|
|
203
225
|
const bottomFixed = 1 + strategy.footerRowCount;
|
|
204
|
-
const middleRows = natural.length - topFixed - bottomFixed;
|
|
205
226
|
|
|
206
227
|
// Residual spacer: equalizes total height across tabs (only needed when no overflow).
|
|
207
228
|
const spacerRows = Math.max(
|
|
@@ -214,35 +235,46 @@ export class DialogView implements StatefulView<DialogProps> {
|
|
|
214
235
|
|
|
215
236
|
const termRows = this.config.getTerminalRows();
|
|
216
237
|
|
|
217
|
-
|
|
218
|
-
|
|
238
|
+
// The spacer is padding for a stable cross-tab footprint, not content: when only the
|
|
239
|
+
// padding overflows, clamp it rather than entering the scroll partition, which assumes
|
|
240
|
+
// the middle region itself is longer than the window (#260).
|
|
241
|
+
if (natural.length <= termRows) {
|
|
242
|
+
return renderFitsTerminal(natural, Math.min(spacerRows, termRows - natural.length));
|
|
219
243
|
}
|
|
220
244
|
|
|
221
|
-
// OVERFLOW — apply 3-region partition with scroll-to-focus.
|
|
222
|
-
|
|
245
|
+
// OVERFLOW — apply 3-region partition with scroll-to-focus. The heading joins the
|
|
246
|
+
// fixed top whenever the body keeps a usable window, so the question being answered
|
|
247
|
+
// never scrolls out of view; heading height is measured in rendered rows, since a
|
|
248
|
+
// long question wraps.
|
|
249
|
+
const headingHeight = headingRowCache.reduce((rows, c) => rows + c.render(width).length, 0);
|
|
250
|
+
const pinHeading = termRows - topFixed - headingHeight - bottomFixed >= MIN_SCROLL_ROWS_WITH_PINNED_HEADING;
|
|
251
|
+
const top = pinHeading ? topFixed + headingHeight : topFixed;
|
|
252
|
+
const middleRows = natural.length - top - bottomFixed;
|
|
253
|
+
const availableMiddle = Math.max(0, termRows - top - bottomFixed);
|
|
223
254
|
if (availableMiddle === 0) {
|
|
224
|
-
return renderChromeOnly(natural,
|
|
255
|
+
return renderChromeOnly(natural, top, bottomFixed, termRows);
|
|
225
256
|
}
|
|
226
257
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
);
|
|
233
|
-
const scrollableMiddle = natural.slice(
|
|
258
|
+
// Never slice past the middle region into the bottom-fixed rows, which are appended
|
|
259
|
+
// below — that would draw the bottom border and hint twice (#260).
|
|
260
|
+
const windowRows = Math.min(availableMiddle, middleRows);
|
|
261
|
+
const rowsBeforeBody = pinHeading ? 0 : headingHeight;
|
|
262
|
+
const focusedRange = strategy.focusedItemRowRange(width, state);
|
|
263
|
+
const scrollStart = computeScrollStart(focusedRange, rowsBeforeBody, windowRows, middleRows);
|
|
264
|
+
const scrollableMiddle = natural.slice(top + scrollStart, top + scrollStart + windowRows);
|
|
265
|
+
const focusedInWindow: [number, number] | undefined = focusedRange && [
|
|
266
|
+
rowsBeforeBody + focusedRange[0] - scrollStart,
|
|
267
|
+
rowsBeforeBody + focusedRange[1] - scrollStart,
|
|
268
|
+
];
|
|
234
269
|
decorateOverflow(
|
|
235
270
|
scrollableMiddle,
|
|
236
271
|
scrollStart > 0,
|
|
237
|
-
scrollStart +
|
|
272
|
+
scrollStart + windowRows < middleRows,
|
|
238
273
|
this.config.theme,
|
|
274
|
+
focusedInWindow,
|
|
239
275
|
);
|
|
240
276
|
|
|
241
|
-
const result = [
|
|
242
|
-
...natural.slice(0, topFixed),
|
|
243
|
-
...scrollableMiddle,
|
|
244
|
-
...natural.slice(natural.length - bottomFixed),
|
|
245
|
-
];
|
|
277
|
+
const result = [...natural.slice(0, top), ...scrollableMiddle, ...natural.slice(natural.length - bottomFixed)];
|
|
246
278
|
// Safety: never exceed terminal rows (covers the availableMiddle === 0 case
|
|
247
279
|
// where topFixed + bottomFixed > termRows).
|
|
248
280
|
return result.length > termRows ? result.slice(0, termRows) : result;
|
|
@@ -243,7 +243,9 @@ export class SubmitTabStrategy implements TabContentStrategy {
|
|
|
243
243
|
"warning",
|
|
244
244
|
`${t("review.incomplete", INCOMPLETE_WARNING_PREFIX)} ${missing.join(", ")}`,
|
|
245
245
|
);
|
|
246
|
-
|
|
246
|
+
// One row, never wrapped: `footerRowCount` counts it as one, and a wrapped prompt
|
|
247
|
+
// (long list of unanswered headers) would desync the chrome's row math (#261).
|
|
248
|
+
const out: Component[] = [new Spacer(1), new OneLineClippedText(promptText, 1)];
|
|
247
249
|
if (this.config.submitPicker) {
|
|
248
250
|
out.push(this.config.submitPicker);
|
|
249
251
|
} else {
|