@juicesharp/rpiv-ask-user-question 1.0.5 → 1.0.6
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 +0 -2
- package/index.ts +2 -7
- package/package.json +13 -7
- package/state/build-questionnaire.ts +147 -0
- package/state/input-buffer.ts +31 -0
- package/state/{dispatch.ts → key-router.ts} +55 -57
- package/state/questionnaire-session.ts +43 -158
- package/state/row-intent.ts +6 -8
- package/state/selectors/derivations.ts +91 -0
- package/state/selectors/focus.ts +19 -0
- package/state/selectors/projections.ts +143 -0
- package/state/{apply-action.ts → state-reducer.ts} +17 -29
- package/state/state.ts +39 -0
- package/tool/validate-questionnaire.ts +3 -5
- package/view/components/chat-row-view.ts +1 -1
- package/view/components/{multi-select-options.ts → multi-select-view.ts} +6 -6
- package/view/components/option-list-view.ts +9 -55
- package/view/components/tab-bar.ts +1 -1
- package/view/components/wrapping-select.ts +2 -2
- package/view/dialog-builder.ts +13 -51
- package/view/props-adapter.ts +102 -0
- package/view/stateful-view.ts +6 -19
- package/view/tab-components.ts +10 -0
- package/view/tab-content-strategy.ts +15 -31
- package/state/questionnaire-state.ts +0 -321
- package/view/view-adapter.ts +0 -111
package/ask-user-question.ts
CHANGED
|
@@ -16,8 +16,6 @@ import type { WrappingSelectItem } from "./view/components/wrapping-select.js";
|
|
|
16
16
|
|
|
17
17
|
const ERROR_NO_UI = "Error: UI not available (running in non-interactive mode)";
|
|
18
18
|
|
|
19
|
-
export { chatNumberingFor } from "./state/questionnaire-state.js";
|
|
20
|
-
|
|
21
19
|
export function buildItemsForQuestion(question: QuestionData): WrappingSelectItem[] {
|
|
22
20
|
const items: WrappingSelectItem[] = question.options.map((o) => ({
|
|
23
21
|
kind: "option",
|
package/index.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* rpiv-ask-user-question — Pi extension
|
|
3
|
-
*
|
|
4
|
-
* Registers the `ask_user_question` tool, which surfaces a structured
|
|
5
|
-
* option selector (plus free-text "Other" fallback) to disambiguate
|
|
6
|
-
* underspecified user requests.
|
|
7
|
-
*
|
|
8
|
-
* Extracted from rpiv-pi@7525a5d. Tool name preserved verbatim.
|
|
2
|
+
* rpiv-ask-user-question — Pi extension. Registers the `ask_user_question`
|
|
3
|
+
* tool: a structured option selector with a free-text "Other" fallback.
|
|
9
4
|
*/
|
|
10
5
|
|
|
11
6
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-ask-user-question",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
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",
|
|
@@ -30,18 +30,23 @@
|
|
|
30
30
|
"index.ts",
|
|
31
31
|
"LICENSE",
|
|
32
32
|
"README.md",
|
|
33
|
-
"state/
|
|
34
|
-
"state/
|
|
33
|
+
"state/build-questionnaire.ts",
|
|
34
|
+
"state/input-buffer.ts",
|
|
35
|
+
"state/key-router.ts",
|
|
35
36
|
"state/questionnaire-session.ts",
|
|
36
|
-
"state/questionnaire-state.ts",
|
|
37
37
|
"state/row-intent.ts",
|
|
38
|
+
"state/selectors/derivations.ts",
|
|
39
|
+
"state/selectors/focus.ts",
|
|
40
|
+
"state/selectors/projections.ts",
|
|
41
|
+
"state/state-reducer.ts",
|
|
42
|
+
"state/state.ts",
|
|
38
43
|
"tool/format-answer.ts",
|
|
39
44
|
"tool/response-envelope.ts",
|
|
40
45
|
"tool/types.ts",
|
|
41
46
|
"tool/validate-questionnaire.ts",
|
|
42
47
|
"view/body-residual-spacer.ts",
|
|
43
48
|
"view/components/chat-row-view.ts",
|
|
44
|
-
"view/components/multi-select-
|
|
49
|
+
"view/components/multi-select-view.ts",
|
|
45
50
|
"view/components/option-list-view.ts",
|
|
46
51
|
"view/components/preview/markdown-content-cache.ts",
|
|
47
52
|
"view/components/preview/preview-block-renderer.ts",
|
|
@@ -52,9 +57,10 @@
|
|
|
52
57
|
"view/components/tab-bar.ts",
|
|
53
58
|
"view/components/wrapping-select.ts",
|
|
54
59
|
"view/dialog-builder.ts",
|
|
60
|
+
"view/props-adapter.ts",
|
|
55
61
|
"view/stateful-view.ts",
|
|
56
|
-
"view/tab-
|
|
57
|
-
"view/
|
|
62
|
+
"view/tab-components.ts",
|
|
63
|
+
"view/tab-content-strategy.ts"
|
|
58
64
|
],
|
|
59
65
|
"pi": {
|
|
60
66
|
"extensions": [
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getMarkdownTheme, type Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Input } from "@mariozechner/pi-tui";
|
|
3
|
+
import { type QuestionData, SENTINEL_LABELS } from "../tool/types.js";
|
|
4
|
+
import { ChatRowView } from "../view/components/chat-row-view.js";
|
|
5
|
+
import { MultiSelectView } from "../view/components/multi-select-view.js";
|
|
6
|
+
import { OptionListView } from "../view/components/option-list-view.js";
|
|
7
|
+
import { PreviewBlockRenderer } from "../view/components/preview/preview-block-renderer.js";
|
|
8
|
+
import { PreviewPane } from "../view/components/preview/preview-pane.js";
|
|
9
|
+
import { SubmitPicker } from "../view/components/submit-picker.js";
|
|
10
|
+
import { TabBar } from "../view/components/tab-bar.js";
|
|
11
|
+
import type { WrappingSelectItem, WrappingSelectTheme } from "../view/components/wrapping-select.js";
|
|
12
|
+
import { buildDialog } from "../view/dialog-builder.js";
|
|
13
|
+
import { QuestionnairePropsAdapter } from "../view/props-adapter.js";
|
|
14
|
+
import type { TabComponents } from "../view/tab-components.js";
|
|
15
|
+
import type { InputBuffer } from "./input-buffer.js";
|
|
16
|
+
import { chatNumberingFor, selectActivePreviewPaneIndex } from "./selectors/derivations.js";
|
|
17
|
+
import { selectActiveView } from "./selectors/focus.js";
|
|
18
|
+
import { selectMultiSelectProps, selectSubmitPickerProps, selectTabBarProps } from "./selectors/projections.js";
|
|
19
|
+
import type { QuestionnaireState } from "./state.js";
|
|
20
|
+
|
|
21
|
+
export interface QuestionnaireBuildConfig {
|
|
22
|
+
tui: { terminal: { columns: number }; requestRender(): void };
|
|
23
|
+
theme: Theme;
|
|
24
|
+
questions: readonly QuestionData[];
|
|
25
|
+
itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
26
|
+
isMulti: boolean;
|
|
27
|
+
initialState: QuestionnaireState;
|
|
28
|
+
inputBuffer: InputBuffer;
|
|
29
|
+
getCurrentTab: () => number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface QuestionnaireBuilt {
|
|
33
|
+
adapter: QuestionnairePropsAdapter;
|
|
34
|
+
notesInput: Input;
|
|
35
|
+
render: (width: number) => string[];
|
|
36
|
+
invalidate: () => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Pure factory: assembles every TUI component, the props adapter, and a
|
|
41
|
+
* lifecycle handle. Session-state dependencies arrive via `getCurrentTab` and
|
|
42
|
+
* the `inputBuffer` cell.
|
|
43
|
+
*/
|
|
44
|
+
export function buildQuestionnaire(config: QuestionnaireBuildConfig): QuestionnaireBuilt {
|
|
45
|
+
const { tui, theme, questions, itemsByTab, isMulti, initialState, inputBuffer, getCurrentTab } = config;
|
|
46
|
+
const totalQuestions = questions.length;
|
|
47
|
+
|
|
48
|
+
const selectTheme: WrappingSelectTheme = {
|
|
49
|
+
selectedText: (t) => theme.fg("accent", theme.bold(t)),
|
|
50
|
+
description: (t) => theme.fg("muted", t),
|
|
51
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const chatRow = new ChatRowView({
|
|
55
|
+
item: { kind: "chat", label: SENTINEL_LABELS.chat },
|
|
56
|
+
theme: selectTheme,
|
|
57
|
+
initialProps: {
|
|
58
|
+
focused: false,
|
|
59
|
+
numbering: chatNumberingFor(itemsByTab[0] ?? []),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const notesInput = new Input();
|
|
63
|
+
|
|
64
|
+
const markdownTheme = getMarkdownTheme();
|
|
65
|
+
const getTerminalWidth = () => tui.terminal.columns;
|
|
66
|
+
const initialActiveView = selectActiveView(initialState, totalQuestions);
|
|
67
|
+
|
|
68
|
+
const tabsByIndex: ReadonlyArray<TabComponents> = questions.map((q, i) => {
|
|
69
|
+
const optionList = new OptionListView({ items: itemsByTab[i] ?? [], theme: selectTheme });
|
|
70
|
+
const previewBlock = new PreviewBlockRenderer({ question: q, theme, markdownTheme });
|
|
71
|
+
const preview = new PreviewPane({
|
|
72
|
+
question: q,
|
|
73
|
+
getTerminalWidth,
|
|
74
|
+
optionListView: optionList,
|
|
75
|
+
previewBlock,
|
|
76
|
+
initialProps: { notesVisible: false, selectedIndex: 0, focused: false },
|
|
77
|
+
});
|
|
78
|
+
const multiSelect = q.multiSelect
|
|
79
|
+
? new MultiSelectView(theme, q, selectMultiSelectProps(initialState, q, initialActiveView))
|
|
80
|
+
: undefined;
|
|
81
|
+
return { optionList, preview, multiSelect };
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const submitPicker = isMulti
|
|
85
|
+
? new SubmitPicker(theme, selectSubmitPickerProps(initialState, totalQuestions, initialActiveView))
|
|
86
|
+
: undefined;
|
|
87
|
+
const tabBar = isMulti ? new TabBar(selectTabBarProps(initialState, questions), theme) : undefined;
|
|
88
|
+
|
|
89
|
+
const computeGlobalContentHeight = (width: number): number => {
|
|
90
|
+
let max = 0;
|
|
91
|
+
for (let i = 0; i < questions.length; i++) {
|
|
92
|
+
const q = questions[i];
|
|
93
|
+
const tab = tabsByIndex[i];
|
|
94
|
+
const h = q?.multiSelect
|
|
95
|
+
? (tab?.multiSelect?.naturalHeight(width) ?? 0)
|
|
96
|
+
: (tab?.preview.maxNaturalHeight(width) ?? 0);
|
|
97
|
+
if (h > max) max = h;
|
|
98
|
+
}
|
|
99
|
+
return Math.max(1, max);
|
|
100
|
+
};
|
|
101
|
+
const computeCurrentContentHeight = (width: number): number => {
|
|
102
|
+
const idx = Math.min(getCurrentTab(), questions.length - 1);
|
|
103
|
+
const q = questions[idx];
|
|
104
|
+
const tab = tabsByIndex[idx];
|
|
105
|
+
if (!q || !tab) return 0;
|
|
106
|
+
const h = q.multiSelect ? (tab.multiSelect?.naturalHeight(width) ?? 0) : tab.preview.naturalHeight(width);
|
|
107
|
+
return Math.max(0, h);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const dialog = buildDialog({
|
|
111
|
+
theme,
|
|
112
|
+
questions,
|
|
113
|
+
initialProps: {
|
|
114
|
+
state: initialState,
|
|
115
|
+
activePreviewPane:
|
|
116
|
+
tabsByIndex[selectActivePreviewPaneIndex(initialState.currentTab, totalQuestions)]?.preview ??
|
|
117
|
+
tabsByIndex[0]!.preview,
|
|
118
|
+
},
|
|
119
|
+
tabBar,
|
|
120
|
+
notesInput,
|
|
121
|
+
chatRow,
|
|
122
|
+
isMulti,
|
|
123
|
+
tabsByIndex,
|
|
124
|
+
submitPicker,
|
|
125
|
+
getBodyHeight: computeGlobalContentHeight,
|
|
126
|
+
getCurrentBodyHeight: computeCurrentContentHeight,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const adapter = new QuestionnairePropsAdapter({
|
|
130
|
+
tui,
|
|
131
|
+
questions,
|
|
132
|
+
itemsByTab,
|
|
133
|
+
tabsByIndex,
|
|
134
|
+
chatRow,
|
|
135
|
+
submitPicker,
|
|
136
|
+
tabBar,
|
|
137
|
+
dialog,
|
|
138
|
+
inputBuffer,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
adapter,
|
|
143
|
+
notesInput,
|
|
144
|
+
render: (w) => dialog.render(w),
|
|
145
|
+
invalidate: () => dialog.invalidate(),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session-owned input-buffer cell. Mutable by design — the reducer never
|
|
3
|
+
* touches it. Mutation paths: effect handlers (`set_input_buffer`,
|
|
4
|
+
* `clear_input_buffer`) and the inline-key fast path (`handleIgnoreInline`
|
|
5
|
+
* for printable keys + backspace). The value flows to the view per tick via
|
|
6
|
+
* `runtime.inputBuffer` → `selectOptionListProps` → OptionListView.
|
|
7
|
+
*/
|
|
8
|
+
export class InputBuffer {
|
|
9
|
+
private value = "";
|
|
10
|
+
|
|
11
|
+
get(): string {
|
|
12
|
+
return this.value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
set(value: string): void {
|
|
16
|
+
this.value = value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
clear(): void {
|
|
20
|
+
this.value = "";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
append(chunk: string): void {
|
|
24
|
+
this.value = this.value + chunk;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
backspace(): void {
|
|
28
|
+
if (this.value.length === 0) return;
|
|
29
|
+
this.value = this.value.slice(0, -1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Key, matchesKey } from "@mariozechner/pi-tui";
|
|
2
2
|
import type { QuestionAnswer } from "../tool/types.js";
|
|
3
|
-
import type { QuestionnaireDispatchSnapshot } from "./questionnaire-state.js";
|
|
4
3
|
import { ROW_INTENT_META } from "./row-intent.js";
|
|
4
|
+
import type { QuestionnaireRuntime, QuestionnaireState } from "./state.js";
|
|
5
5
|
|
|
6
6
|
const KEYBIND_UP = "tui.select.up";
|
|
7
7
|
const KEYBIND_DOWN = "tui.select.down";
|
|
@@ -24,12 +24,10 @@ export type QuestionnaireAction =
|
|
|
24
24
|
| { kind: "submit_nav"; nextIndex: 0 | 1 }
|
|
25
25
|
| { kind: "focus_chat" }
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* (Defect 2). When omitted, the host preserves the existing optionIndex (UP-from-chat
|
|
30
|
-
* legacy behavior — currently still emitted by some branches and consumers).
|
|
27
|
+
* Carries the target index so UP/DOWN form a continuous cycle through
|
|
28
|
+
* `[chat, option0, …, optionLast]`.
|
|
31
29
|
*/
|
|
32
|
-
| { kind: "focus_options"; optionIndex
|
|
30
|
+
| { kind: "focus_options"; optionIndex: number }
|
|
33
31
|
| { kind: "ignore" };
|
|
34
32
|
|
|
35
33
|
export interface QuestionnaireKeybindings {
|
|
@@ -41,32 +39,32 @@ export function wrapTab(index: number, total: number): number {
|
|
|
41
39
|
return ((index % total) + total) % total;
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
export function allAnswered(state:
|
|
45
|
-
if (
|
|
46
|
-
for (let i = 0; i <
|
|
42
|
+
export function allAnswered(state: QuestionnaireState, runtime: QuestionnaireRuntime): boolean {
|
|
43
|
+
if (runtime.questions.length === 0) return false;
|
|
44
|
+
for (let i = 0; i < runtime.questions.length; i++) {
|
|
47
45
|
if (!state.answers.has(i)) return false;
|
|
48
46
|
}
|
|
49
47
|
return true;
|
|
50
48
|
}
|
|
51
49
|
|
|
52
|
-
function totalTabs(
|
|
53
|
-
return
|
|
50
|
+
function totalTabs(runtime: QuestionnaireRuntime): number {
|
|
51
|
+
return runtime.isMulti ? runtime.questions.length + 1 : 1;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
function computeAutoAdvanceTab(state:
|
|
57
|
-
if (!
|
|
58
|
-
if (state.currentTab <
|
|
59
|
-
return
|
|
54
|
+
function computeAutoAdvanceTab(state: QuestionnaireState, runtime: QuestionnaireRuntime): number | undefined {
|
|
55
|
+
if (!runtime.isMulti) return undefined;
|
|
56
|
+
if (state.currentTab < runtime.questions.length - 1) return state.currentTab + 1;
|
|
57
|
+
return runtime.questions.length;
|
|
60
58
|
}
|
|
61
59
|
|
|
62
|
-
function buildSingleSelectAnswer(state:
|
|
63
|
-
const q =
|
|
60
|
+
function buildSingleSelectAnswer(state: QuestionnaireState, runtime: QuestionnaireRuntime): QuestionAnswer | null {
|
|
61
|
+
const q = runtime.questions[state.currentTab];
|
|
64
62
|
if (!q) return null;
|
|
65
63
|
|
|
66
64
|
// Chat sentinel takes priority over inputMode: when chatFocused=true, the host overrides
|
|
67
65
|
// currentItem() to return the chat sentinel even if inputMode is still true (e.g. user
|
|
68
66
|
// navigated from "Type something." and DOWN focused the chat row).
|
|
69
|
-
const item =
|
|
67
|
+
const item = runtime.currentItem;
|
|
70
68
|
if (item?.kind === "chat") {
|
|
71
69
|
return {
|
|
72
70
|
questionIndex: state.currentTab,
|
|
@@ -77,7 +75,7 @@ function buildSingleSelectAnswer(state: QuestionnaireDispatchSnapshot): Question
|
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
if (state.inputMode) {
|
|
80
|
-
const label =
|
|
78
|
+
const label = runtime.inputBuffer;
|
|
81
79
|
return {
|
|
82
80
|
questionIndex: state.currentTab,
|
|
83
81
|
question: q.question,
|
|
@@ -100,8 +98,8 @@ function buildSingleSelectAnswer(state: QuestionnaireDispatchSnapshot): Question
|
|
|
100
98
|
};
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
function buildMultiSelected(state:
|
|
104
|
-
const q =
|
|
101
|
+
function buildMultiSelected(state: QuestionnaireState, runtime: QuestionnaireRuntime): string[] {
|
|
102
|
+
const q = runtime.questions[state.currentTab];
|
|
105
103
|
if (!q) return [];
|
|
106
104
|
const out: string[] = [];
|
|
107
105
|
for (let i = 0; i < q.options.length; i++) {
|
|
@@ -113,9 +111,13 @@ function buildMultiSelected(state: QuestionnaireDispatchSnapshot): string[] {
|
|
|
113
111
|
return out;
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
function tabSwitchAction(
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
function tabSwitchAction(
|
|
115
|
+
data: string,
|
|
116
|
+
state: QuestionnaireState,
|
|
117
|
+
runtime: QuestionnaireRuntime,
|
|
118
|
+
): QuestionnaireAction | null {
|
|
119
|
+
if (!runtime.isMulti) return null;
|
|
120
|
+
const total = totalTabs(runtime);
|
|
119
121
|
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
120
122
|
return { kind: "tab_switch", nextTab: wrapTab(state.currentTab + 1, total) };
|
|
121
123
|
}
|
|
@@ -125,28 +127,24 @@ function tabSwitchAction(data: string, state: QuestionnaireDispatchSnapshot): Qu
|
|
|
125
127
|
return null;
|
|
126
128
|
}
|
|
127
129
|
|
|
128
|
-
// DOWN
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
function nextNavOnDown(state: QuestionnaireDispatchSnapshot): QuestionnaireAction {
|
|
132
|
-
if (state.items.length > 0 && state.optionIndex === state.items.length - 1) {
|
|
130
|
+
// DOWN at the last item emits focus_chat so the cycle [chat, option0, …, optionLast] wraps.
|
|
131
|
+
function nextNavOnDown(state: QuestionnaireState, runtime: QuestionnaireRuntime): QuestionnaireAction {
|
|
132
|
+
if (runtime.items.length > 0 && state.optionIndex === runtime.items.length - 1) {
|
|
133
133
|
return { kind: "focus_chat" };
|
|
134
134
|
}
|
|
135
|
-
return { kind: "nav", nextIndex: wrapTab(state.optionIndex + 1, Math.max(1,
|
|
135
|
+
return { kind: "nav", nextIndex: wrapTab(state.optionIndex + 1, Math.max(1, runtime.items.length)) };
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
// UP
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
function prevNavOnUp(state: QuestionnaireDispatchSnapshot): QuestionnaireAction {
|
|
142
|
-
if (state.items.length > 0 && state.optionIndex === 0) {
|
|
138
|
+
// UP at the top item emits focus_chat (symmetric with nextNavOnDown).
|
|
139
|
+
function prevNavOnUp(state: QuestionnaireState, runtime: QuestionnaireRuntime): QuestionnaireAction {
|
|
140
|
+
if (runtime.items.length > 0 && state.optionIndex === 0) {
|
|
143
141
|
return { kind: "focus_chat" };
|
|
144
142
|
}
|
|
145
|
-
return { kind: "nav", nextIndex: wrapTab(state.optionIndex - 1, Math.max(1,
|
|
143
|
+
return { kind: "nav", nextIndex: wrapTab(state.optionIndex - 1, Math.max(1, runtime.items.length)) };
|
|
146
144
|
}
|
|
147
145
|
|
|
148
|
-
export function
|
|
149
|
-
const kb =
|
|
146
|
+
export function routeKey(data: string, state: QuestionnaireState, runtime: QuestionnaireRuntime): QuestionnaireAction {
|
|
147
|
+
const kb = runtime.keybindings;
|
|
150
148
|
|
|
151
149
|
if (state.notesVisible) {
|
|
152
150
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "notes_exit" };
|
|
@@ -157,45 +155,45 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
|
|
|
157
155
|
if (state.chatFocused) {
|
|
158
156
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
|
|
159
157
|
if (kb.matches(data, KEYBIND_CONFIRM)) {
|
|
160
|
-
const answer = buildSingleSelectAnswer(state);
|
|
158
|
+
const answer = buildSingleSelectAnswer(state, runtime);
|
|
161
159
|
if (!answer) return { kind: "ignore" };
|
|
162
|
-
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state) };
|
|
160
|
+
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state, runtime) };
|
|
163
161
|
}
|
|
164
162
|
// Continuous cycle: UP from chat → bottom of options (last navigable row), DOWN from
|
|
165
163
|
// chat → top of options (option 0). Symmetric with UP-at-top → focus_chat and
|
|
166
164
|
// DOWN-at-bottom → focus_chat below; together they form one wrapping cycle through
|
|
167
165
|
// `[chat, option0, …, optionLast]`.
|
|
168
166
|
if (kb.matches(data, KEYBIND_UP)) {
|
|
169
|
-
const last = Math.max(0,
|
|
167
|
+
const last = Math.max(0, runtime.items.length - 1);
|
|
170
168
|
return { kind: "focus_options", optionIndex: last };
|
|
171
169
|
}
|
|
172
170
|
if (kb.matches(data, KEYBIND_DOWN)) {
|
|
173
171
|
return { kind: "focus_options", optionIndex: 0 };
|
|
174
172
|
}
|
|
175
|
-
const tab = tabSwitchAction(data, state);
|
|
173
|
+
const tab = tabSwitchAction(data, state, runtime);
|
|
176
174
|
if (tab) return tab;
|
|
177
175
|
return { kind: "ignore" };
|
|
178
176
|
}
|
|
179
177
|
|
|
180
178
|
if (state.inputMode) {
|
|
181
179
|
if (kb.matches(data, KEYBIND_CONFIRM)) {
|
|
182
|
-
const answer = buildSingleSelectAnswer(state);
|
|
180
|
+
const answer = buildSingleSelectAnswer(state, runtime);
|
|
183
181
|
if (!answer) return { kind: "ignore" };
|
|
184
|
-
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state) };
|
|
182
|
+
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state, runtime) };
|
|
185
183
|
}
|
|
186
184
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
|
|
187
185
|
if (kb.matches(data, KEYBIND_UP)) {
|
|
188
|
-
return prevNavOnUp(state);
|
|
186
|
+
return prevNavOnUp(state, runtime);
|
|
189
187
|
}
|
|
190
188
|
if (kb.matches(data, KEYBIND_DOWN)) {
|
|
191
|
-
return nextNavOnDown(state);
|
|
189
|
+
return nextNavOnDown(state, runtime);
|
|
192
190
|
}
|
|
193
191
|
return { kind: "ignore" };
|
|
194
192
|
}
|
|
195
193
|
|
|
196
|
-
if (
|
|
194
|
+
if (runtime.isMulti && state.currentTab === runtime.questions.length) {
|
|
197
195
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
|
|
198
|
-
const tab = tabSwitchAction(data, state);
|
|
196
|
+
const tab = tabSwitchAction(data, state, runtime);
|
|
199
197
|
if (tab) return tab;
|
|
200
198
|
if (kb.matches(data, KEYBIND_UP) || kb.matches(data, KEYBIND_DOWN)) {
|
|
201
199
|
const delta = kb.matches(data, KEYBIND_DOWN) ? 1 : -1;
|
|
@@ -211,10 +209,10 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
|
|
|
211
209
|
return { kind: "ignore" };
|
|
212
210
|
}
|
|
213
211
|
|
|
214
|
-
const tab = tabSwitchAction(data, state);
|
|
212
|
+
const tab = tabSwitchAction(data, state, runtime);
|
|
215
213
|
if (tab) return tab;
|
|
216
214
|
|
|
217
|
-
const q =
|
|
215
|
+
const q = runtime.questions[state.currentTab];
|
|
218
216
|
if (!q) return { kind: "ignore" };
|
|
219
217
|
|
|
220
218
|
if (data === NOTES_ACTIVATE_KEY && !q.multiSelect && state.focusedOptionHasPreview) {
|
|
@@ -222,14 +220,14 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
|
|
|
222
220
|
}
|
|
223
221
|
|
|
224
222
|
if (kb.matches(data, KEYBIND_UP)) {
|
|
225
|
-
return prevNavOnUp(state);
|
|
223
|
+
return prevNavOnUp(state, runtime);
|
|
226
224
|
}
|
|
227
225
|
if (kb.matches(data, KEYBIND_DOWN)) {
|
|
228
|
-
return nextNavOnDown(state);
|
|
226
|
+
return nextNavOnDown(state, runtime);
|
|
229
227
|
}
|
|
230
228
|
|
|
231
229
|
if (q.multiSelect) {
|
|
232
|
-
const focusedKind =
|
|
230
|
+
const focusedKind = runtime.currentItem?.kind;
|
|
233
231
|
const focusedMeta = focusedKind ? ROW_INTENT_META[focusedKind] : undefined;
|
|
234
232
|
// Space toggles the focused row's checkbox. Suppressed on rows whose META declares
|
|
235
233
|
// `blocksMultiToggle` (the Next sentinel) — Next is not a real option and has no
|
|
@@ -250,8 +248,8 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
|
|
|
250
248
|
// question would have no way to commit at all.
|
|
251
249
|
return {
|
|
252
250
|
kind: "multi_confirm",
|
|
253
|
-
selected: buildMultiSelected(state),
|
|
254
|
-
autoAdvanceTab: computeAutoAdvanceTab(state),
|
|
251
|
+
selected: buildMultiSelected(state, runtime),
|
|
252
|
+
autoAdvanceTab: computeAutoAdvanceTab(state, runtime),
|
|
255
253
|
};
|
|
256
254
|
}
|
|
257
255
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
|
|
@@ -259,9 +257,9 @@ export function handleQuestionnaireInput(data: string, state: QuestionnaireDispa
|
|
|
259
257
|
}
|
|
260
258
|
|
|
261
259
|
if (kb.matches(data, KEYBIND_CONFIRM)) {
|
|
262
|
-
const answer = buildSingleSelectAnswer(state);
|
|
260
|
+
const answer = buildSingleSelectAnswer(state, runtime);
|
|
263
261
|
if (!answer) return { kind: "ignore" };
|
|
264
|
-
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state) };
|
|
262
|
+
return { kind: "confirm", answer, autoAdvanceTab: computeAutoAdvanceTab(state, runtime) };
|
|
265
263
|
}
|
|
266
264
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "cancel" };
|
|
267
265
|
return { kind: "ignore" };
|