@juicesharp/rpiv-ask-user-question 0.13.0 → 0.13.2

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 CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Let the model ask you structured clarifying questions instead of guessing. `rpiv-ask-user-question` adds the `ask_user_question` tool to [Pi Agent](https://github.com/badlogic/pi-mono) — a tabbed dialog with single- and multi-select questions, side-by-side previews, per-option notes, and a Submit tab that reviews answers before they go back to the model.
7
7
 
8
- ![Single-question dialog](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/single-question.jpg)
8
+ ![Side-by-side code preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/code-preview.jpg)
9
9
 
10
10
  ## Features
11
11
 
@@ -21,7 +21,7 @@ Let the model ask you structured clarifying questions instead of guessing. `rpiv
21
21
 
22
22
  | | |
23
23
  |---|---|
24
- | ![Multi-tab + ASCII preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/multi-tab-preview.jpg) | ![Side-by-side code preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/code-preview.jpg) |
24
+ | ![Single-question dialog](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/single-question.jpg) | ![Multi-tab + ASCII preview](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/multi-tab-preview.jpg) |
25
25
  | ![Multi-select with checkboxes](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/multi-select.jpg) | ![Submit tab review](https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-ask-user-question/docs/submit-tab.jpg) |
26
26
 
27
27
  ## Install
@@ -40,6 +40,24 @@ const RESERVED_LABEL_SET: ReadonlySet<string> = new Set(RESERVED_LABELS);
40
40
  const BACKSPACE_CHARS = new Set(["\x7f", "\b"]);
41
41
  const ESC_SEQUENCE_PREFIX = "\x1b";
42
42
 
43
+ /**
44
+ * Numbering for the chat row's WrappingSelect, computed from the active tab's items.
45
+ *
46
+ * The chat row lives in its own one-item WrappingSelect; the host calls this on every tab
47
+ * switch / selection update to keep the chat row's `N. ` label continuous with the visible
48
+ * numbered rows of the active tab. The shape `{ offset, total }` mirrors `WrappingSelect.setNumbering(numberStartOffset, totalItemsForNumbering)` directly.
49
+ */
50
+ export function chatNumberingFor(items: readonly WrappingSelectItem[]): {
51
+ offset: number;
52
+ total: number;
53
+ } {
54
+ // Count only the visible-numbered rows. The Next sentinel renders without a number
55
+ // (see MultiSelectOptions), so it must NOT advance the chat row's number — otherwise
56
+ // chat reads as "6." next to options labeled 1-4.
57
+ const count = items.filter((i) => !i.isNext).length;
58
+ return { offset: count, total: count + 1 };
59
+ }
60
+
43
61
  export function buildItemsForQuestion(question: QuestionData): WrappingSelectItem[] {
44
62
  const items = question.options.map((o) => ({ label: o.label, description: o.description }));
45
63
  // Multi-select gets a "Next" sentinel row at the bottom so `Enter` on regular option rows
@@ -306,7 +324,8 @@ Preview content is rendered as markdown in a monospace box. Multi-line text with
306
324
  // Keep the chat row's number aligned with whichever tab is active. The chat row
307
325
  // is always the (last + 1) entry in the logical numbering for that tab.
308
326
  const activeTabItems = itemsByTab[Math.min(currentTab, questions.length - 1)] ?? [];
309
- chatList.setNumbering(activeTabItems.length, activeTabItems.length + 1);
327
+ const chatNumbering = chatNumberingFor(activeTabItems);
328
+ chatList.setNumbering(chatNumbering.offset, chatNumbering.total);
310
329
  // Multi-select rows now carry their own `N.` numbers (CC parity), so the chat row
311
330
  // continues that contiguous numbering on every tab — no special-case suppression.
312
331
  chatList.setShowNumbering(true);
@@ -506,6 +525,17 @@ Preview content is rendered as markdown in a monospace box. Multi-line text with
506
525
  return;
507
526
  case "focus_options":
508
527
  chatFocused = false;
528
+ // When the dispatcher carries a target index, update optionIndex too — that
529
+ // drives the continuous chat ↔ options cycle (DOWN-from-chat → 0,
530
+ // UP-from-chat → items.length - 1). Without `optionIndex`, focus simply
531
+ // returns to wherever the user was (legacy contract preserved).
532
+ if (action.optionIndex !== undefined) {
533
+ optionIndex = action.optionIndex;
534
+ inputMode = !!currentItem()?.isOther;
535
+ if (!inputMode) {
536
+ previewPanes[currentTab]?.clearInputBuffer();
537
+ }
538
+ }
509
539
  refreshDialog();
510
540
  return;
511
541
  case "submit_nav":
package/dispatch.ts CHANGED
@@ -22,7 +22,13 @@ export type QuestionnaireAction =
22
22
  | { kind: "submit" }
23
23
  | { kind: "submit_nav"; nextIndex: 0 | 1 }
24
24
  | { kind: "focus_chat" }
25
- | { kind: "focus_options" }
25
+ /**
26
+ * Optional `optionIndex` lets the dispatcher tell the host where to land when leaving the
27
+ * chat row, so UP/DOWN form a continuous cycle through `[chat, option0, …, optionLast]`
28
+ * (Defect 2). When omitted, the host preserves the existing optionIndex (UP-from-chat
29
+ * legacy behavior — currently still emitted by some branches and consumers).
30
+ */
31
+ | { kind: "focus_options"; optionIndex?: number }
26
32
  | { kind: "ignore" };
27
33
 
28
34
  export interface QuestionnaireKeybindings {
@@ -146,7 +152,7 @@ function tabSwitchAction(data: string, state: QuestionnaireDispatchState): Quest
146
152
 
147
153
  // DOWN navigation helper shared by inputMode and normal nav branches.
148
154
  // Emits focus_chat at the boundary (last item) so the host can transfer focus to the chat row
149
- // without mutating optionIndex — UP-from-chat then restores prior selection.
155
+ // without mutating optionIndex — UP-from-chat then lands on items.length - 1 (continuous cycle).
150
156
  function nextNavOnDown(state: QuestionnaireDispatchState): QuestionnaireAction {
151
157
  if (state.items.length > 0 && state.optionIndex === state.items.length - 1) {
152
158
  return { kind: "focus_chat" };
@@ -154,6 +160,16 @@ function nextNavOnDown(state: QuestionnaireDispatchState): QuestionnaireAction {
154
160
  return { kind: "nav", nextIndex: wrapTab(state.optionIndex + 1, Math.max(1, state.items.length)) };
155
161
  }
156
162
 
163
+ // UP navigation helper, symmetric with nextNavOnDown. At the TOP boundary (optionIndex 0)
164
+ // emits focus_chat so the cycle wraps `[chat, option0, …, optionLast]` — without this, UP at
165
+ // option 0 would skip the chat row entirely (Defect 2). Above the boundary, decrement.
166
+ function prevNavOnUp(state: QuestionnaireDispatchState): QuestionnaireAction {
167
+ if (state.items.length > 0 && state.optionIndex === 0) {
168
+ return { kind: "focus_chat" };
169
+ }
170
+ return { kind: "nav", nextIndex: wrapTab(state.optionIndex - 1, Math.max(1, state.items.length)) };
171
+ }
172
+
157
173
  export function handleQuestionnaireInput(data: string, state: QuestionnaireDispatchState): QuestionnaireAction {
158
174
  const kb = state.keybindings;
159
175
 
@@ -170,10 +186,17 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
170
186
  if (!answer) return { kind: "ignore" };
171
187
  return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state) };
172
188
  }
173
- if (kb.matches(data, KEYBIND_UP)) return { kind: "focus_options" };
174
- // DOWN from the chat row returns focus to the options column. Without this, the chat
175
- // row becomes a one-way trap on DOWN, which made it feel unselectable in some flows.
176
- if (kb.matches(data, KEYBIND_DOWN)) return { kind: "focus_options" };
189
+ // Continuous cycle: UP from chat → bottom of options (last navigable row), DOWN from
190
+ // chat → top of options (option 0). Symmetric with UP-at-top → focus_chat and
191
+ // DOWN-at-bottom → focus_chat below; together they form one wrapping cycle through
192
+ // `[chat, option0, …, optionLast]`.
193
+ if (kb.matches(data, KEYBIND_UP)) {
194
+ const last = Math.max(0, state.items.length - 1);
195
+ return { kind: "focus_options", optionIndex: last };
196
+ }
197
+ if (kb.matches(data, KEYBIND_DOWN)) {
198
+ return { kind: "focus_options", optionIndex: 0 };
199
+ }
177
200
  const tab = tabSwitchAction(data, state);
178
201
  if (tab) return tab;
179
202
  return { kind: "ignore" };
@@ -187,7 +210,7 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
187
210
  }
188
211
  if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
189
212
  if (kb.matches(data, KEYBIND_UP)) {
190
- return { kind: "nav", nextIndex: wrapTab(state.optionIndex - 1, Math.max(1, state.items.length)) };
213
+ return prevNavOnUp(state);
191
214
  }
192
215
  if (kb.matches(data, KEYBIND_DOWN)) {
193
216
  return nextNavOnDown(state);
@@ -224,7 +247,7 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
224
247
  }
225
248
 
226
249
  if (kb.matches(data, KEYBIND_UP)) {
227
- return { kind: "nav", nextIndex: wrapTab(state.optionIndex - 1, Math.max(1, state.items.length)) };
250
+ return prevNavOnUp(state);
228
251
  }
229
252
  if (kb.matches(data, KEYBIND_DOWN)) {
230
253
  return nextNavOnDown(state);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
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",