@juicesharp/rpiv-ask-user-question 1.0.6 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-ask-user-question",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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",
@@ -45,6 +45,7 @@
45
45
  "tool/types.ts",
46
46
  "tool/validate-questionnaire.ts",
47
47
  "view/body-residual-spacer.ts",
48
+ "view/component-binding.ts",
48
49
  "view/components/chat-row-view.ts",
49
50
  "view/components/multi-select-view.ts",
50
51
  "view/components/option-list-view.ts",
@@ -1,6 +1,12 @@
1
1
  import { getMarkdownTheme, type Theme } from "@mariozechner/pi-coding-agent";
2
2
  import { Input } from "@mariozechner/pi-tui";
3
3
  import { type QuestionData, SENTINEL_LABELS } from "../tool/types.js";
4
+ import {
5
+ type BoundGlobalBinding,
6
+ type BoundPerTabBinding,
7
+ globalBinding,
8
+ perTabBinding,
9
+ } from "../view/component-binding.js";
4
10
  import { ChatRowView } from "../view/components/chat-row-view.js";
5
11
  import { MultiSelectView } from "../view/components/multi-select-view.js";
6
12
  import { OptionListView } from "../view/components/option-list-view.js";
@@ -9,13 +15,20 @@ import { PreviewPane } from "../view/components/preview/preview-pane.js";
9
15
  import { SubmitPicker } from "../view/components/submit-picker.js";
10
16
  import { TabBar } from "../view/components/tab-bar.js";
11
17
  import type { WrappingSelectItem, WrappingSelectTheme } from "../view/components/wrapping-select.js";
12
- import { buildDialog } from "../view/dialog-builder.js";
18
+ import { DialogView } from "../view/dialog-builder.js";
13
19
  import { QuestionnairePropsAdapter } from "../view/props-adapter.js";
14
20
  import type { TabComponents } from "../view/tab-components.js";
15
21
  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";
22
+ import { selectActivePreviewPaneIndex } from "./selectors/derivations.js";
23
+ import {
24
+ selectChatRowProps,
25
+ selectDialogProps,
26
+ selectMultiSelectProps,
27
+ selectOptionListProps,
28
+ selectPreviewPaneProps,
29
+ selectSubmitPickerProps,
30
+ selectTabBarProps,
31
+ } from "./selectors/projections.js";
19
32
  import type { QuestionnaireState } from "./state.js";
20
33
 
21
34
  export interface QuestionnaireBuildConfig {
@@ -39,7 +52,9 @@ export interface QuestionnaireBuilt {
39
52
  /**
40
53
  * Pure factory: assembles every TUI component, the props adapter, and a
41
54
  * lifecycle handle. Session-state dependencies arrive via `getCurrentTab` and
42
- * the `inputBuffer` cell.
55
+ * the `inputBuffer` cell. Initial paint is delegated to
56
+ * `adapter.apply(initialState)` (called by the session at construction-end);
57
+ * no selector is invoked here.
43
58
  */
44
59
  export function buildQuestionnaire(config: QuestionnaireBuildConfig): QuestionnaireBuilt {
45
60
  const { tui, theme, questions, itemsByTab, isMulti, initialState, inputBuffer, getCurrentTab } = config;
@@ -54,16 +69,16 @@ export function buildQuestionnaire(config: QuestionnaireBuildConfig): Questionna
54
69
  const chatRow = new ChatRowView({
55
70
  item: { kind: "chat", label: SENTINEL_LABELS.chat },
56
71
  theme: selectTheme,
57
- initialProps: {
58
- focused: false,
59
- numbering: chatNumberingFor(itemsByTab[0] ?? []),
60
- },
61
72
  });
62
73
  const notesInput = new Input();
63
74
 
64
75
  const markdownTheme = getMarkdownTheme();
65
76
  const getTerminalWidth = () => tui.terminal.columns;
66
- const initialActiveView = selectActiveView(initialState, totalQuestions);
77
+
78
+ // Concrete-typed locals retained for the height callbacks
79
+ // (`naturalHeight` / `maxNaturalHeight` are not on `StatefulView<P>`).
80
+ const previewPanesByTab: PreviewPane[] = [];
81
+ const multiSelectViewsByTab: (MultiSelectView | undefined)[] = [];
67
82
 
68
83
  const tabsByIndex: ReadonlyArray<TabComponents> = questions.map((q, i) => {
69
84
  const optionList = new OptionListView({ items: itemsByTab[i] ?? [], theme: selectTheme });
@@ -73,27 +88,25 @@ export function buildQuestionnaire(config: QuestionnaireBuildConfig): Questionna
73
88
  getTerminalWidth,
74
89
  optionListView: optionList,
75
90
  previewBlock,
76
- initialProps: { notesVisible: false, selectedIndex: 0, focused: false },
77
91
  });
78
- const multiSelect = q.multiSelect
79
- ? new MultiSelectView(theme, q, selectMultiSelectProps(initialState, q, initialActiveView))
80
- : undefined;
92
+ const multiSelect = q.multiSelect ? new MultiSelectView(theme, q) : undefined;
93
+
94
+ previewPanesByTab.push(preview);
95
+ multiSelectViewsByTab.push(multiSelect);
96
+
81
97
  return { optionList, preview, multiSelect };
82
98
  });
83
99
 
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;
100
+ const submitPicker = isMulti ? new SubmitPicker(theme) : undefined;
101
+ const tabBar = isMulti ? new TabBar(theme) : undefined;
88
102
 
89
103
  const computeGlobalContentHeight = (width: number): number => {
90
104
  let max = 0;
91
105
  for (let i = 0; i < questions.length; i++) {
92
106
  const q = questions[i];
93
- const tab = tabsByIndex[i];
94
107
  const h = q?.multiSelect
95
- ? (tab?.multiSelect?.naturalHeight(width) ?? 0)
96
- : (tab?.preview.maxNaturalHeight(width) ?? 0);
108
+ ? (multiSelectViewsByTab[i]?.naturalHeight(width) ?? 0)
109
+ : (previewPanesByTab[i]?.maxNaturalHeight(width) ?? 0);
97
110
  if (h > max) max = h;
98
111
  }
99
112
  return Math.max(1, max);
@@ -101,47 +114,102 @@ export function buildQuestionnaire(config: QuestionnaireBuildConfig): Questionna
101
114
  const computeCurrentContentHeight = (width: number): number => {
102
115
  const idx = Math.min(getCurrentTab(), questions.length - 1);
103
116
  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);
117
+ if (!q) return 0;
118
+ const h = q.multiSelect
119
+ ? (multiSelectViewsByTab[idx]?.naturalHeight(width) ?? 0)
120
+ : (previewPanesByTab[idx]?.naturalHeight(width) ?? 0);
107
121
  return Math.max(0, h);
108
122
  };
109
123
 
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,
124
+ const initialActivePreviewPane =
125
+ previewPanesByTab[selectActivePreviewPaneIndex(initialState.currentTab, totalQuestions)] ?? previewPanesByTab[0]!;
126
+
127
+ const dialog = new DialogView(
128
+ {
129
+ theme,
130
+ questions,
131
+ tabBar,
132
+ notesInput,
133
+ chatRow,
134
+ isMulti,
135
+ tabsByIndex,
136
+ submitPicker,
137
+ getBodyHeight: computeGlobalContentHeight,
138
+ getCurrentBodyHeight: computeCurrentContentHeight,
118
139
  },
119
- tabBar,
120
- notesInput,
121
- chatRow,
122
- isMulti,
123
- tabsByIndex,
124
- submitPicker,
125
- getBodyHeight: computeGlobalContentHeight,
126
- getCurrentBodyHeight: computeCurrentContentHeight,
127
- });
140
+ { state: initialState, activePreviewPane: initialActivePreviewPane },
141
+ );
142
+
143
+ const globalBindings: ReadonlyArray<BoundGlobalBinding> = [
144
+ globalBinding({
145
+ component: dialog,
146
+ select: (s, ctx) => selectDialogProps(s, ctx.activePreviewPane),
147
+ }),
148
+ globalBinding({
149
+ component: chatRow,
150
+ select: (s, ctx) => selectChatRowProps(s, ctx.itemsByTab, ctx.totalQuestions, ctx.activeView),
151
+ }),
152
+ ...(submitPicker
153
+ ? [
154
+ globalBinding({
155
+ component: submitPicker,
156
+ select: (s, ctx) => selectSubmitPickerProps(s, ctx.totalQuestions, ctx.activeView),
157
+ }),
158
+ ]
159
+ : []),
160
+ ...(tabBar
161
+ ? [
162
+ globalBinding({
163
+ component: tabBar,
164
+ select: (s, ctx) => selectTabBarProps(s, ctx.questions),
165
+ }),
166
+ ]
167
+ : []),
168
+ ];
169
+
170
+ const isActiveTab = (s: QuestionnaireState, ctx: { i: number; totalQuestions: number }): boolean => {
171
+ const paneIdx = ctx.totalQuestions <= 0 ? 0 : Math.min(s.currentTab, ctx.totalQuestions - 1);
172
+ return ctx.i === paneIdx;
173
+ };
174
+
175
+ const perTabBindings: ReadonlyArray<BoundPerTabBinding> = [
176
+ perTabBinding({
177
+ resolve: (tab) => tab.optionList,
178
+ predicate: (s, ctx) => isActiveTab(s, ctx),
179
+ select: (s, ctx) =>
180
+ selectOptionListProps(s, ctx.itemsByTab[ctx.i] ?? [], ctx.questions, ctx.activeView, ctx.inputBuffer),
181
+ }),
182
+ perTabBinding({
183
+ resolve: (tab) => tab.preview,
184
+ predicate: (s, ctx) => isActiveTab(s, ctx),
185
+ select: (s, ctx) => selectPreviewPaneProps(s, ctx.activeView),
186
+ }),
187
+ perTabBinding({
188
+ resolve: (tab) => tab.multiSelect,
189
+ select: (s, ctx) => {
190
+ const q = ctx.questions[ctx.i];
191
+ if (!q) return { rows: [], nextActive: false, nextLabel: SENTINEL_LABELS.next };
192
+ const isLast = ctx.i === ctx.questions.length - 1;
193
+ return selectMultiSelectProps(s, q, ctx.activeView, isLast);
194
+ },
195
+ }),
196
+ ];
128
197
 
129
198
  const adapter = new QuestionnairePropsAdapter({
130
199
  tui,
131
200
  questions,
132
201
  itemsByTab,
133
202
  tabsByIndex,
134
- chatRow,
135
- submitPicker,
136
- tabBar,
137
- dialog,
138
203
  inputBuffer,
204
+ globalBindings,
205
+ perTabBindings,
206
+ extraInvalidatables: [notesInput],
139
207
  });
140
208
 
141
209
  return {
142
210
  adapter,
143
211
  notesInput,
144
212
  render: (w) => dialog.render(w),
145
- invalidate: () => dialog.invalidate(),
213
+ invalidate: () => adapter.invalidate(),
146
214
  };
147
215
  }
@@ -1,31 +1,22 @@
1
1
  import type { QuestionData } from "../../tool/types.js";
2
+ import { SENTINEL_LABELS } from "../../tool/types.js";
2
3
  import type { ChatRowViewProps } from "../../view/components/chat-row-view.js";
3
- import type { MultiSelectViewProps } from "../../view/components/multi-select-view.js";
4
+ import { MULTI_SUBMIT_LABEL, type MultiSelectViewProps } from "../../view/components/multi-select-view.js";
4
5
  import type { OptionListViewProps } from "../../view/components/option-list-view.js";
5
6
  import type { PreviewPaneProps } from "../../view/components/preview/preview-pane.js";
6
7
  import type { SubmitPickerProps } from "../../view/components/submit-picker.js";
7
8
  import type { TabBarProps } from "../../view/components/tab-bar.js";
8
9
  import type { WrappingSelectItem } from "../../view/components/wrapping-select.js";
9
- import type { ActiveView } from "../../view/stateful-view.js";
10
+ import type { DialogProps } from "../../view/dialog-builder.js";
11
+ import type { ActiveView, StatefulView } from "../../view/stateful-view.js";
10
12
  import type { QuestionnaireState } from "../state.js";
11
13
  import { chatNumberingFor, selectActiveTabItems, selectConfirmedIndicator } from "./derivations.js";
12
14
 
13
- /**
14
- * Per-tick projection for a `MultiSelectView` instance. Pre-computes
15
- * `checked` and `active` per row + the `nextActive` flag so the component's
16
- * render body is pure styling.
17
- *
18
- * Broadcast-safe: every multi-select tab's MSO receives a projection per
19
- * tick, but only the active tab actually renders its body via
20
- * `QuestionTabStrategy.bodyComponent`. Non-active MSO instances see
21
- * `focused === false` because activeView is gated on `state.notesVisible` /
22
- * `state.chatFocused` / Submit-tab — none of which are true while in options
23
- * mode on the active tab.
24
- */
25
15
  export function selectMultiSelectProps(
26
16
  state: QuestionnaireState,
27
17
  question: QuestionData,
28
18
  activeView: ActiveView,
19
+ isLastQuestion: boolean,
29
20
  ): MultiSelectViewProps {
30
21
  const focused = activeView === "options";
31
22
  const rows: { checked: boolean; active: boolean }[] = [];
@@ -36,7 +27,8 @@ export function selectMultiSelectProps(
36
27
  });
37
28
  }
38
29
  const nextActive = focused && state.optionIndex === question.options.length;
39
- return { rows, nextActive };
30
+ const nextLabel = isLastQuestion ? MULTI_SUBMIT_LABEL : SENTINEL_LABELS.next;
31
+ return { rows, nextActive, nextLabel };
40
32
  }
41
33
 
42
34
  /**
@@ -141,3 +133,17 @@ export function selectChatRowProps(
141
133
  numbering: chatNumberingFor(activeItems),
142
134
  };
143
135
  }
136
+
137
+ /**
138
+ * Per-tick projection for the dialog. Mirrors the inline literal previously
139
+ * at `props-adapter.ts:68`. The `activePreviewPane` is resolved per tick by
140
+ * the adapter (via `selectActivePreviewPaneIndex` over `tabsByIndex`) and
141
+ * passed in as a `StatefulView<PreviewPaneProps>` reference — not a derived
142
+ * state value — so the selector signature takes it as a separate arg.
143
+ */
144
+ export function selectDialogProps(
145
+ state: QuestionnaireState,
146
+ activePreviewPane: StatefulView<PreviewPaneProps>,
147
+ ): DialogProps {
148
+ return { state, activePreviewPane };
149
+ }
@@ -170,6 +170,7 @@ export function reduce(state: QuestionnaireState, action: QuestionnaireAction, c
170
170
  const answers = new Map(state.answers);
171
171
  answers.set(answer.questionIndex, answer);
172
172
  const next: QuestionnaireState = { ...state, answers };
173
+ if (answer.kind === "chat") return doneFor(next, ctx, false);
173
174
  if (action.autoAdvanceTab !== undefined) return switchTabResult(next, action.autoAdvanceTab, ctx);
174
175
  return doneFor(next, ctx, false);
175
176
  }
@@ -0,0 +1,109 @@
1
+ import type { QuestionnaireState } from "../state/state.js";
2
+ import type { QuestionData } from "../tool/types.js";
3
+ import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
4
+ import type { WrappingSelectItem } from "./components/wrapping-select.js";
5
+ import type { ActiveView, StatefulView } from "./stateful-view.js";
6
+ import type { TabComponents } from "./tab-components.js";
7
+
8
+ /**
9
+ * Per-tick context shared across all bindings. Built once at the top of
10
+ * `apply()`; threaded into every projector closure unchanged. Carries every
11
+ * free variable today's selectors take beyond `state` itself.
12
+ *
13
+ * `activePreviewPane` is typed as `StatefulView<PreviewPaneProps>` (not the
14
+ * concrete `PreviewPane` class) so the adapter can thread the resolved pane
15
+ * through without `as unknown as` casts; the dialog and tab-content-strategy
16
+ * consume it as a `Component` only.
17
+ */
18
+ export interface BindingContext {
19
+ readonly questions: readonly QuestionData[];
20
+ readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
21
+ readonly totalQuestions: number;
22
+ readonly activeView: ActiveView;
23
+ readonly inputBuffer: string;
24
+ readonly activePreviewPane: StatefulView<PreviewPaneProps>;
25
+ }
26
+
27
+ /**
28
+ * Per-tab extension of `BindingContext`. The nested per-tab loop builds this
29
+ * once per tab (extending the global ctx with `tab` + `i`) and threads it to
30
+ * every per-tab binding's projector + predicate.
31
+ */
32
+ export interface PerTabBindingContext extends BindingContext {
33
+ readonly tab: TabComponents;
34
+ readonly i: number;
35
+ }
36
+
37
+ /**
38
+ * Global axis binding spec: paired component + selector, generic in `<P>`. Used
39
+ * at construction sites only; never lands in the adapter's iteration array.
40
+ * `globalBinding(spec)` erases `<P>` via the closure form below — TS verifies
41
+ * `select(s, ctx) → P` matches `component.setProps(P)` at the call site.
42
+ */
43
+ export interface ComponentBinding<P> {
44
+ readonly component: StatefulView<P>;
45
+ readonly select: (state: QuestionnaireState, ctx: BindingContext) => P;
46
+ }
47
+
48
+ /**
49
+ * Per-tab axis binding spec: resolver picks the per-tab component instance
50
+ * from the `TabComponents` slot (returning `undefined` when absent — e.g.,
51
+ * `multiSelect` on non-multi tabs). Optional predicate gates the projection
52
+ * (e.g., `optionList` and `preview` only fire on `i === paneIndex`). Bound
53
+ * via `perTabBinding(spec)` for the same reason as `ComponentBinding`.
54
+ */
55
+ export interface PerTabBinding<P> {
56
+ readonly resolve: (tab: TabComponents) => StatefulView<P> | undefined;
57
+ readonly select: (state: QuestionnaireState, ctx: PerTabBindingContext) => P;
58
+ readonly predicate?: (state: QuestionnaireState, ctx: PerTabBindingContext) => boolean;
59
+ }
60
+
61
+ /**
62
+ * Bound global binding consumed by the adapter's iteration. `<P>` is captured
63
+ * inside the closures returned by `globalBinding`; from the array's
64
+ * perspective each entry is just an `apply` + `invalidate` pair, no generic
65
+ * surface.
66
+ */
67
+ export interface BoundGlobalBinding {
68
+ apply(state: QuestionnaireState, ctx: BindingContext): void;
69
+ invalidate(): void;
70
+ }
71
+
72
+ /**
73
+ * Bound per-tab binding. Mirrors `BoundGlobalBinding`: the adapter calls
74
+ * `apply(state, perTabCtx)` per tab and the closure handles
75
+ * `predicate → resolve → setProps` internally. Missing per-tab components
76
+ * silently skip.
77
+ */
78
+ export interface BoundPerTabBinding {
79
+ apply(state: QuestionnaireState, ctx: PerTabBindingContext): void;
80
+ }
81
+
82
+ /**
83
+ * Existential wrapper for a global binding. Inside this generic helper `<P>`
84
+ * is a free type variable, so TS verifies that `spec.select(...) : P` lines
85
+ * up with `spec.component.setProps(P)` at the call site. Once bound into
86
+ * `BoundGlobalBinding` the array is monomorphic.
87
+ */
88
+ export function globalBinding<P>(spec: ComponentBinding<P>): BoundGlobalBinding {
89
+ return {
90
+ apply: (state, ctx) => spec.component.setProps(spec.select(state, ctx)),
91
+ invalidate: () => spec.component.invalidate(),
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Existential wrapper for a per-tab binding. Same trick as `globalBinding`:
97
+ * `<P>` is local to this function, so the predicate, resolver, and selector
98
+ * all check against one `P`. Resolves the component lazily inside `apply` so
99
+ * missing slots (e.g., non-multi `multiSelect`) skip without a guard at the
100
+ * iteration site.
101
+ */
102
+ export function perTabBinding<P>(spec: PerTabBinding<P>): BoundPerTabBinding {
103
+ return {
104
+ apply: (state, ctx) => {
105
+ if (spec.predicate && !spec.predicate(state, ctx)) return;
106
+ spec.resolve(ctx.tab)?.setProps(spec.select(state, ctx));
107
+ },
108
+ };
109
+ }
@@ -17,7 +17,6 @@ export interface ChatRowViewConfig {
17
17
  /** The single chat sentinel row — `{kind: "chat", label: SENTINEL_LABELS.chat}`. */
18
18
  item: WrappingSelectItem;
19
19
  theme: WrappingSelectTheme;
20
- initialProps: ChatRowViewProps;
21
20
  }
22
21
 
23
22
  /**
@@ -35,10 +34,9 @@ export class ChatRowView implements StatefulView<ChatRowViewProps>, Component {
35
34
 
36
35
  constructor(config: ChatRowViewConfig) {
37
36
  this.select = new WrappingSelect([config.item], 1, config.theme, {
38
- numberStartOffset: config.initialProps.numbering.offset,
39
- totalItemsForNumbering: config.initialProps.numbering.total,
37
+ numberStartOffset: 0,
38
+ totalItemsForNumbering: 1,
40
39
  });
41
- this.select.setFocused(config.initialProps.focused);
42
40
  }
43
41
 
44
42
  setProps(props: ChatRowViewProps): void {
@@ -13,17 +13,12 @@ const BOX_LABEL_GAP = " ";
13
13
  // full prefix column. Wrap width still uses prefixVisibleWidth so naturalHeight matches render.
14
14
  const CONTINUATION_INDENT = " ";
15
15
 
16
- /**
17
- * Per-tick projection of MultiSelectView state. The selector
18
- * (`selectMultiSelectProps`) pre-computes per-row `checked` + `active` so
19
- * `render()` is pure styling — no `state.multiSelectChecked.has(i)` or
20
- * `focused && i === state.optionIndex` predicates inside the render body.
21
- */
16
+ export const MULTI_SUBMIT_LABEL = "Submit";
17
+
22
18
  export interface MultiSelectViewProps {
23
- /** Per-option row state. Length === question.options.length. Order matches question.options[]. */
24
19
  rows: ReadonlyArray<{ checked: boolean; active: boolean }>;
25
- /** True iff the Next sentinel row owns the active pointer. */
26
20
  nextActive: boolean;
21
+ nextLabel: string;
27
22
  }
28
23
 
29
24
  /**
@@ -42,9 +37,8 @@ export class MultiSelectView implements StatefulView<MultiSelectViewProps> {
42
37
  constructor(
43
38
  private readonly theme: Theme,
44
39
  private readonly question: QuestionData,
45
- initialProps: MultiSelectViewProps,
46
40
  ) {
47
- this.props = initialProps;
41
+ this.props = { rows: [], nextActive: false, nextLabel: SENTINEL_LABELS.next };
48
42
  }
49
43
 
50
44
  setProps(props: MultiSelectViewProps): void {
@@ -81,12 +75,10 @@ export class MultiSelectView implements StatefulView<MultiSelectViewProps> {
81
75
  }
82
76
  }
83
77
  }
84
- // Next sentinel — pointer + bare label, no number / no checkbox. Visually distinct from
85
- // option rows so users read it as an action ("commit and advance"), not a togglable row.
86
78
  const nextPointer = this.props.nextActive ? this.theme.fg("accent", ACTIVE_POINTER) : INACTIVE_POINTER;
87
79
  const nextLabel = this.props.nextActive
88
- ? this.theme.fg("accent", this.theme.bold(SENTINEL_LABELS.next))
89
- : SENTINEL_LABELS.next;
80
+ ? this.theme.fg("accent", this.theme.bold(this.props.nextLabel))
81
+ : this.props.nextLabel;
90
82
  lines.push(truncateToWidth(`${nextPointer}${nextLabel}`, width, ""));
91
83
  return lines;
92
84
  }
@@ -56,7 +56,6 @@ export interface PreviewPaneConfig {
56
56
  getTerminalWidth: () => number;
57
57
  optionListView: OptionListView;
58
58
  previewBlock: PreviewBlockRenderer;
59
- initialProps: PreviewPaneProps;
60
59
  }
61
60
 
62
61
  /**
@@ -83,7 +82,7 @@ export class PreviewPane implements StatefulView<PreviewPaneProps>, Component {
83
82
  this.getTerminalWidth = config.getTerminalWidth;
84
83
  this.optionListView = config.optionListView;
85
84
  this.previewBlock = config.previewBlock;
86
- this.props = config.initialProps;
85
+ this.props = { notesVisible: false, selectedIndex: 0, focused: false };
87
86
  }
88
87
 
89
88
  setProps(props: PreviewPaneProps): void {
@@ -33,11 +33,8 @@ export interface SubmitPickerProps {
33
33
  export class SubmitPicker implements StatefulView<SubmitPickerProps> {
34
34
  private props: SubmitPickerProps;
35
35
 
36
- constructor(
37
- private readonly theme: Theme,
38
- initialProps: SubmitPickerProps,
39
- ) {
40
- this.props = initialProps;
36
+ constructor(private readonly theme: Theme) {
37
+ this.props = { rows: [{ active: false }, { active: false }] };
41
38
  }
42
39
 
43
40
  setProps(props: SubmitPickerProps): void {
@@ -19,11 +19,8 @@ export interface TabBarProps {
19
19
  export class TabBar implements StatefulView<TabBarProps> {
20
20
  private props: TabBarProps;
21
21
 
22
- constructor(
23
- initialProps: TabBarProps,
24
- private readonly theme: Theme,
25
- ) {
26
- this.props = initialProps;
22
+ constructor(private readonly theme: Theme) {
23
+ this.props = { tabs: [], submit: { active: false, allAnswered: false } };
27
24
  }
28
25
 
29
26
  setProps(props: TabBarProps): void {
@@ -4,7 +4,7 @@ import type { QuestionnaireState } from "../state/state.js";
4
4
  import type { QuestionData } from "../tool/types.js";
5
5
  import { BodyResidualSpacer } from "./body-residual-spacer.js";
6
6
  import type { ChatRowView } from "./components/chat-row-view.js";
7
- import type { PreviewPane } from "./components/preview/preview-pane.js";
7
+ import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
8
8
  import type { TabBar } from "./components/tab-bar.js";
9
9
  import type { StatefulView } from "./stateful-view.js";
10
10
  import type { TabComponents } from "./tab-components.js";
@@ -29,14 +29,13 @@ export type DialogState = QuestionnaireState;
29
29
  /** Per-tick projection of dialog state. Written by the adapter; read by the strategy thunk. */
30
30
  export interface DialogProps {
31
31
  state: DialogState;
32
- activePreviewPane: PreviewPane;
32
+ activePreviewPane: StatefulView<PreviewPaneProps>;
33
33
  }
34
34
 
35
- /** Construction-time config for `buildDialog`. Frozen after construction. */
35
+ /** Construction-time config for `DialogView`. Frozen after construction. */
36
36
  export interface DialogConfig {
37
37
  theme: Theme;
38
38
  questions: readonly QuestionData[];
39
- initialProps: DialogProps;
40
39
  tabBar: TabBar | undefined;
41
40
  notesInput: Input;
42
41
  chatRow: ChatRowView;
@@ -50,79 +49,86 @@ export interface DialogConfig {
50
49
  getCurrentBodyHeight: (width: number) => number;
51
50
  }
52
51
 
53
- export function buildDialog(config: DialogConfig): StatefulView<DialogProps> {
54
- let liveProps: DialogProps = config.initialProps;
52
+ /**
53
+ * The 7th renderable, promoted from a structural literal to a named class so
54
+ * all view-layer components share one explicit `implements StatefulView<P>`
55
+ * contract. `setProps(DialogProps)` writes the live cell read by the
56
+ * strategy thunk during `render()`. `liveProps.activePreviewPane` is a
57
+ * resolved pane reference threaded by the adapter per tick — the dialog
58
+ * itself does not derive it.
59
+ */
60
+ export class DialogView implements StatefulView<DialogProps> {
61
+ private liveProps: DialogProps;
62
+ private readonly config: DialogConfig;
63
+ private readonly questionStrategy: TabContentStrategy;
64
+ private readonly submitStrategy: TabContentStrategy | undefined;
65
+ private readonly maxFooterRowCount: number;
55
66
 
56
- const questionStrategy: TabContentStrategy = new QuestionTabStrategy({
57
- theme: config.theme,
58
- questions: config.questions,
59
- getPreviewPane: () => liveProps.activePreviewPane,
60
- tabsByIndex: config.tabsByIndex,
61
- notesInput: config.notesInput,
62
- chatRow: config.chatRow,
63
- isMulti: config.isMulti,
64
- getCurrentBodyHeight: config.getCurrentBodyHeight,
65
- });
67
+ constructor(config: DialogConfig, initialProps: DialogProps) {
68
+ this.config = config;
69
+ this.liveProps = initialProps;
70
+ this.questionStrategy = new QuestionTabStrategy({
71
+ theme: config.theme,
72
+ questions: config.questions,
73
+ getPreviewPane: () => this.liveProps.activePreviewPane,
74
+ tabsByIndex: config.tabsByIndex,
75
+ notesInput: config.notesInput,
76
+ chatRow: config.chatRow,
77
+ isMulti: config.isMulti,
78
+ getCurrentBodyHeight: config.getCurrentBodyHeight,
79
+ });
80
+ this.submitStrategy = config.isMulti
81
+ ? new SubmitTabStrategy({
82
+ theme: config.theme,
83
+ questions: config.questions,
84
+ submitPicker: config.submitPicker,
85
+ })
86
+ : undefined;
87
+ this.maxFooterRowCount = Math.max(this.questionStrategy.footerRowCount, this.submitStrategy?.footerRowCount ?? 0);
88
+ }
66
89
 
67
- const submitStrategy: TabContentStrategy | undefined = config.isMulti
68
- ? new SubmitTabStrategy({
69
- theme: config.theme,
70
- questions: config.questions,
71
- submitPicker: config.submitPicker,
72
- })
73
- : undefined;
90
+ setProps(props: DialogProps): void {
91
+ this.liveProps = props;
92
+ }
74
93
 
75
- const maxFooterRowCount = Math.max(questionStrategy.footerRowCount, submitStrategy?.footerRowCount ?? 0);
94
+ handleInput(_data: string): void {}
76
95
 
77
- const component: StatefulView<DialogProps> = {
78
- setProps(props: DialogProps) {
79
- liveProps = props;
80
- },
81
- handleInput() {},
82
- invalidate() {
83
- liveProps.activePreviewPane.invalidate();
84
- config.tabBar?.invalidate();
85
- config.notesInput.invalidate();
86
- config.chatRow.invalidate();
87
- },
88
- render(width: number): string[] {
89
- const onSubmit = config.isMulti && liveProps.state.currentTab === config.questions.length;
90
- const strategy = onSubmit && submitStrategy ? submitStrategy : questionStrategy;
91
- return buildContainerFromStrategy(strategy, config, liveProps, maxFooterRowCount).render(width);
92
- },
93
- };
94
- return component;
95
- }
96
+ // Invalidation is driven by `QuestionnairePropsAdapter.invalidate()`, which
97
+ // owns the full set of renderables (binding registries + extras like
98
+ // `notesInput`). DialogView has no cached layout of its own.
99
+ invalidate(): void {}
100
+
101
+ render(width: number): string[] {
102
+ const onSubmit = this.config.isMulti && this.liveProps.state.currentTab === this.config.questions.length;
103
+ const strategy = onSubmit && this.submitStrategy ? this.submitStrategy : this.questionStrategy;
104
+ return this.buildContainerFromStrategy(strategy).render(width);
105
+ }
96
106
 
97
- function buildContainerFromStrategy(
98
- strategy: TabContentStrategy,
99
- config: DialogConfig,
100
- props: DialogProps,
101
- maxFooterRowCount: number,
102
- ): Container {
103
- const { theme, isMulti, tabBar } = config;
104
- const state = props.state;
105
- const container = new Container();
106
- const border = () => new DynamicBorder((s) => theme.fg("accent", s));
107
+ private buildContainerFromStrategy(strategy: TabContentStrategy): Container {
108
+ const { theme, isMulti, tabBar } = this.config;
109
+ const state = this.liveProps.state;
110
+ const container = new Container();
111
+ const border = () => new DynamicBorder((s) => theme.fg("accent", s));
107
112
 
108
- container.addChild(border());
109
- if (isMulti && tabBar) container.addChild(tabBar);
110
- container.addChild(new Spacer(1));
113
+ container.addChild(border());
114
+ if (isMulti && tabBar) container.addChild(tabBar);
115
+ container.addChild(new Spacer(1));
111
116
 
112
- for (const c of strategy.headingRows(state)) container.addChild(c);
113
- container.addChild(strategy.bodyComponent(state));
114
- container.addChild(new Spacer(1));
115
- for (const c of strategy.midRows(state)) container.addChild(c);
117
+ for (const c of strategy.headingRows(state)) container.addChild(c);
118
+ container.addChild(strategy.bodyComponent(state));
119
+ container.addChild(new Spacer(1));
120
+ for (const c of strategy.midRows(state)) container.addChild(c);
116
121
 
117
- container.addChild(border());
118
- for (const c of strategy.footerRows(state)) container.addChild(c);
122
+ container.addChild(border());
123
+ for (const c of strategy.footerRows(state)) container.addChild(c);
119
124
 
120
- // Residual spacer equalizes total height across strategies; rendered AFTER the bottom border.
121
- container.addChild(
122
- new BodyResidualSpacer(
123
- (w) => config.getBodyHeight(w) + maxFooterRowCount,
124
- (w) => strategy.bodyHeight(w, state) + strategy.footerRowCount,
125
- ),
126
- );
127
- return container;
125
+ // Residual spacer equalizes total height across strategies; rendered AFTER the bottom border.
126
+ container.addChild(
127
+ new BodyResidualSpacer(
128
+ (w) => this.config.getBodyHeight(w) + this.maxFooterRowCount,
129
+ (w) => strategy.bodyHeight(w, state) + strategy.footerRowCount,
130
+ ),
131
+ );
132
+ return container;
133
+ }
128
134
  }
@@ -1,102 +1,112 @@
1
1
  import type { InputBuffer } from "../state/input-buffer.js";
2
2
  import { selectActivePreviewPaneIndex } from "../state/selectors/derivations.js";
3
3
  import { selectActiveView } from "../state/selectors/focus.js";
4
- import {
5
- selectChatRowProps,
6
- selectMultiSelectProps,
7
- selectOptionListProps,
8
- selectPreviewPaneProps,
9
- selectSubmitPickerProps,
10
- selectTabBarProps,
11
- } from "../state/selectors/projections.js";
12
4
  import type { QuestionnaireState } from "../state/state.js";
13
5
  import type { QuestionData } from "../tool/types.js";
14
- import type { ChatRowView } from "./components/chat-row-view.js";
15
- import type { SubmitPicker } from "./components/submit-picker.js";
16
- import type { TabBar } from "./components/tab-bar.js";
6
+ import type {
7
+ BindingContext,
8
+ BoundGlobalBinding,
9
+ BoundPerTabBinding,
10
+ PerTabBindingContext,
11
+ } from "./component-binding.js";
17
12
  import type { WrappingSelectItem } from "./components/wrapping-select.js";
18
- import type { DialogProps } from "./dialog-builder.js";
19
- import type { StatefulView } from "./stateful-view.js";
20
13
  import type { TabComponents } from "./tab-components.js";
21
14
 
15
+ /** Cache-invalidation contract used by the adapter. `pi-tui` `Component` already satisfies it. */
16
+ interface Invalidatable {
17
+ invalidate(): void;
18
+ }
19
+
22
20
  export interface QuestionnairePropsAdapterConfig {
23
21
  tui: { requestRender(): void };
24
22
  questions: readonly QuestionData[];
25
23
  itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
26
24
  tabsByIndex: ReadonlyArray<TabComponents>;
27
- chatRow: ChatRowView;
28
- submitPicker: SubmitPicker | undefined;
29
- tabBar: TabBar | undefined;
30
- dialog: StatefulView<DialogProps>;
31
25
  inputBuffer: InputBuffer;
26
+ globalBindings: ReadonlyArray<BoundGlobalBinding>;
27
+ perTabBindings: ReadonlyArray<BoundPerTabBinding>;
28
+ /**
29
+ * Renderables not reached by the binding registries (e.g. the notes
30
+ * `Input`, which is typed into directly and has no props). Walked by
31
+ * `invalidate()` after the binding-driven components.
32
+ */
33
+ extraInvalidatables?: ReadonlyArray<Invalidatable>;
32
34
  }
33
35
 
34
36
  /**
35
- * View fan-out: drives every component setter from the canonical state via named selectors.
36
- * The `inputBuffer` cell is read per tick so `selectOptionListProps` sees the live value.
37
+ * View fan-out: drives every component setter from the canonical state via
38
+ * two binding registries. `globalBindings` covers the cross-tab components
39
+ * (chatRow, dialog, submitPicker?, tabBar?); `perTabBindings` covers the
40
+ * per-tab kinds (optionList, preview, multiSelect?). The hand-coded fan-out
41
+ * collapses to one global loop + one nested per-tab loop. The `inputBuffer`
42
+ * cell is read per tick into ctx so `selectOptionListProps` sees the live
43
+ * value.
37
44
  */
38
45
  export class QuestionnairePropsAdapter {
39
46
  private readonly tui: QuestionnairePropsAdapterConfig["tui"];
40
47
  private readonly questions: readonly QuestionData[];
41
48
  private readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
42
49
  private readonly tabsByIndex: ReadonlyArray<TabComponents>;
43
- private readonly chatRow: ChatRowView;
44
- private readonly submitPicker: SubmitPicker | undefined;
45
- private readonly tabBar: TabBar | undefined;
46
- private readonly dialog: StatefulView<DialogProps>;
47
50
  private readonly inputBuffer: InputBuffer;
51
+ private readonly globalBindings: ReadonlyArray<BoundGlobalBinding>;
52
+ private readonly perTabBindings: ReadonlyArray<BoundPerTabBinding>;
53
+ private readonly extraInvalidatables: ReadonlyArray<Invalidatable>;
48
54
 
49
55
  constructor(config: QuestionnairePropsAdapterConfig) {
50
56
  this.tui = config.tui;
51
57
  this.questions = config.questions;
52
58
  this.itemsByTab = config.itemsByTab;
53
59
  this.tabsByIndex = config.tabsByIndex;
54
- this.chatRow = config.chatRow;
55
- this.submitPicker = config.submitPicker;
56
- this.tabBar = config.tabBar;
57
- this.dialog = config.dialog;
58
60
  this.inputBuffer = config.inputBuffer;
61
+ this.globalBindings = config.globalBindings;
62
+ this.perTabBindings = config.perTabBindings;
63
+ this.extraInvalidatables = config.extraInvalidatables ?? [];
59
64
  }
60
65
 
61
66
  apply(state: QuestionnaireState): void {
62
67
  const totalQuestions = this.questions.length;
63
68
  const activeView = selectActiveView(state, totalQuestions);
64
-
65
69
  const paneIndex = selectActivePreviewPaneIndex(state.currentTab, totalQuestions);
66
70
  const activePreviewPane = this.tabsByIndex[paneIndex]?.preview ?? this.tabsByIndex[0]!.preview;
67
71
 
68
- this.dialog.setProps({ state, activePreviewPane });
72
+ const ctx: BindingContext = {
73
+ questions: this.questions,
74
+ itemsByTab: this.itemsByTab,
75
+ totalQuestions,
76
+ activeView,
77
+ inputBuffer: this.inputBuffer.get(),
78
+ activePreviewPane,
79
+ };
80
+
81
+ for (const binding of this.globalBindings) {
82
+ binding.apply(state, ctx);
83
+ }
69
84
 
70
85
  for (let i = 0; i < this.tabsByIndex.length; i++) {
71
86
  const tab = this.tabsByIndex[i]!;
72
- const q = this.questions[i];
73
- if (i === paneIndex) {
74
- tab.optionList.setProps(
75
- selectOptionListProps(
76
- state,
77
- this.itemsByTab[i] ?? [],
78
- this.questions,
79
- activeView,
80
- this.inputBuffer.get(),
81
- ),
82
- );
83
- tab.preview.setProps(selectPreviewPaneProps(state, activeView));
84
- }
85
- if (tab.multiSelect && q) {
86
- tab.multiSelect.setProps(selectMultiSelectProps(state, q, activeView));
87
+ const tabCtx: PerTabBindingContext = { ...ctx, tab, i };
88
+ for (const binding of this.perTabBindings) {
89
+ binding.apply(state, tabCtx);
87
90
  }
88
91
  }
89
92
 
90
- this.chatRow.setProps(selectChatRowProps(state, this.itemsByTab, totalQuestions, activeView));
91
-
92
- if (this.submitPicker) {
93
- this.submitPicker.setProps(selectSubmitPickerProps(state, totalQuestions, activeView));
94
- }
93
+ this.tui.requestRender();
94
+ }
95
95
 
96
- if (this.tabBar) {
97
- this.tabBar.setProps(selectTabBarProps(state, this.questions));
96
+ /**
97
+ * Invalidates every owned renderable. Called by the session in place of
98
+ * the old `dialog.invalidate()` forwarding chain — DialogView no longer
99
+ * reaches into siblings (chatRow, tabBar, notesInput, activePreviewPane).
100
+ * Iterates the same registries used by `apply()` plus
101
+ * `extraInvalidatables` for components outside the binding system.
102
+ */
103
+ invalidate(): void {
104
+ for (const b of this.globalBindings) b.invalidate();
105
+ for (const tab of this.tabsByIndex) {
106
+ tab.optionList.invalidate();
107
+ tab.preview.invalidate();
108
+ tab.multiSelect?.invalidate();
98
109
  }
99
-
100
- this.tui.requestRender();
110
+ for (const x of this.extraInvalidatables) x.invalidate();
101
111
  }
102
112
  }
@@ -1,10 +1,18 @@
1
- import type { MultiSelectView } from "./components/multi-select-view.js";
2
- import type { OptionListView } from "./components/option-list-view.js";
3
- import type { PreviewPane } from "./components/preview/preview-pane.js";
1
+ import type { MultiSelectViewProps } from "./components/multi-select-view.js";
2
+ import type { OptionListViewProps } from "./components/option-list-view.js";
3
+ import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
4
+ import type { StatefulView } from "./stateful-view.js";
4
5
 
5
- /** Per-question view components. `multiSelect` is undefined for single-select questions. */
6
+ /**
7
+ * Per-question view components, exposed to the adapter and dialog as
8
+ * `StatefulView<XProps>` views (interface-only consumption). The same
9
+ * runtime instances that satisfy these tightened types also satisfy their
10
+ * concrete classes; `buildQuestionnaire` keeps separate concrete-typed local
11
+ * arrays for the height callbacks (`naturalHeight` / `maxNaturalHeight` are
12
+ * not on `StatefulView<P>`).
13
+ */
6
14
  export interface TabComponents {
7
- optionList: OptionListView;
8
- preview: PreviewPane;
9
- multiSelect?: MultiSelectView;
15
+ optionList: StatefulView<OptionListViewProps>;
16
+ preview: StatefulView<PreviewPaneProps>;
17
+ multiSelect?: StatefulView<MultiSelectViewProps>;
10
18
  }
@@ -3,7 +3,7 @@ import { type Component, Container, type Input, Spacer, Text } from "@mariozechn
3
3
  import { formatAnswerScalar } from "../tool/format-answer.js";
4
4
  import type { QuestionData } from "../tool/types.js";
5
5
  import type { ChatRowView } from "./components/chat-row-view.js";
6
- import type { PreviewPane } from "./components/preview/preview-pane.js";
6
+ import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
7
7
  import {
8
8
  type DialogState,
9
9
  HINT_PART_CANCEL,
@@ -16,6 +16,7 @@ import {
16
16
  READY_PROMPT,
17
17
  REVIEW_HEADING,
18
18
  } from "./dialog-builder.js";
19
+ import type { StatefulView } from "./stateful-view.js";
19
20
  import type { TabComponents } from "./tab-components.js";
20
21
 
21
22
  /**
@@ -46,7 +47,7 @@ export interface TabContentStrategy {
46
47
  export interface QuestionTabStrategyConfig {
47
48
  theme: Theme;
48
49
  questions: readonly QuestionData[];
49
- getPreviewPane: () => PreviewPane;
50
+ getPreviewPane: () => StatefulView<PreviewPaneProps>;
50
51
  tabsByIndex: ReadonlyArray<TabComponents>;
51
52
  notesInput: Input;
52
53
  chatRow: ChatRowView;