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

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.7",
3
+ "version": "1.0.8",
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",
@@ -35,6 +35,7 @@
35
35
  "state/key-router.ts",
36
36
  "state/questionnaire-session.ts",
37
37
  "state/row-intent.ts",
38
+ "state/selectors/contract.ts",
38
39
  "state/selectors/derivations.ts",
39
40
  "state/selectors/focus.ts",
40
41
  "state/selectors/projections.ts",
@@ -19,6 +19,7 @@ import { DialogView } from "../view/dialog-builder.js";
19
19
  import { QuestionnairePropsAdapter } from "../view/props-adapter.js";
20
20
  import type { TabComponents } from "../view/tab-components.js";
21
21
  import type { InputBuffer } from "./input-buffer.js";
22
+ import type { PerTabSelector } from "./selectors/contract.js";
22
23
  import { selectActivePreviewPaneIndex } from "./selectors/derivations.js";
23
24
  import {
24
25
  selectChatRowProps,
@@ -141,33 +142,13 @@ export function buildQuestionnaire(config: QuestionnaireBuildConfig): Questionna
141
142
  );
142
143
 
143
144
  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
- : []),
145
+ globalBinding({ component: dialog, select: selectDialogProps }),
146
+ globalBinding({ component: chatRow, select: selectChatRowProps }),
147
+ ...(submitPicker ? [globalBinding({ component: submitPicker, select: selectSubmitPickerProps })] : []),
148
+ ...(tabBar ? [globalBinding({ component: tabBar, select: selectTabBarProps })] : []),
168
149
  ];
169
150
 
170
- const isActiveTab = (s: QuestionnaireState, ctx: { i: number; totalQuestions: number }): boolean => {
151
+ const isActiveTab: PerTabSelector<boolean> = (s, ctx) => {
171
152
  const paneIdx = ctx.totalQuestions <= 0 ? 0 : Math.min(s.currentTab, ctx.totalQuestions - 1);
172
153
  return ctx.i === paneIdx;
173
154
  };
@@ -175,23 +156,17 @@ export function buildQuestionnaire(config: QuestionnaireBuildConfig): Questionna
175
156
  const perTabBindings: ReadonlyArray<BoundPerTabBinding> = [
176
157
  perTabBinding({
177
158
  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),
159
+ predicate: isActiveTab,
160
+ select: selectOptionListProps,
181
161
  }),
182
162
  perTabBinding({
183
163
  resolve: (tab) => tab.preview,
184
- predicate: (s, ctx) => isActiveTab(s, ctx),
185
- select: (s, ctx) => selectPreviewPaneProps(s, ctx.activeView),
164
+ predicate: isActiveTab,
165
+ select: selectPreviewPaneProps,
186
166
  }),
187
167
  perTabBinding({
188
168
  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
- },
169
+ select: selectMultiSelectProps,
195
170
  }),
196
171
  ];
197
172
 
@@ -0,0 +1,23 @@
1
+ import type { QuestionData } from "../../tool/types.js";
2
+ import type { PreviewPaneProps } from "../../view/components/preview/preview-pane.js";
3
+ import type { WrappingSelectItem } from "../../view/components/wrapping-select.js";
4
+ import type { ActiveView, StatefulView } from "../../view/stateful-view.js";
5
+ import type { TabComponents } from "../../view/tab-components.js";
6
+ import type { QuestionnaireState } from "../state.js";
7
+
8
+ export interface BindingContext {
9
+ readonly questions: readonly QuestionData[];
10
+ readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
11
+ readonly totalQuestions: number;
12
+ readonly activeView: ActiveView;
13
+ readonly inputBuffer: string;
14
+ readonly activePreviewPane: StatefulView<PreviewPaneProps>;
15
+ }
16
+
17
+ export interface PerTabBindingContext extends BindingContext {
18
+ readonly tab: TabComponents;
19
+ readonly i: number;
20
+ }
21
+
22
+ export type GlobalSelector<P> = (state: QuestionnaireState, ctx: BindingContext) => P;
23
+ export type PerTabSelector<P> = (state: QuestionnaireState, ctx: PerTabBindingContext) => P;
@@ -1,4 +1,3 @@
1
- import type { QuestionData } from "../../tool/types.js";
2
1
  import { SENTINEL_LABELS } from "../../tool/types.js";
3
2
  import type { ChatRowViewProps } from "../../view/components/chat-row-view.js";
4
3
  import { MULTI_SUBMIT_LABEL, type MultiSelectViewProps } from "../../view/components/multi-select-view.js";
@@ -6,19 +5,14 @@ import type { OptionListViewProps } from "../../view/components/option-list-view
6
5
  import type { PreviewPaneProps } from "../../view/components/preview/preview-pane.js";
7
6
  import type { SubmitPickerProps } from "../../view/components/submit-picker.js";
8
7
  import type { TabBarProps } from "../../view/components/tab-bar.js";
9
- import type { WrappingSelectItem } from "../../view/components/wrapping-select.js";
10
8
  import type { DialogProps } from "../../view/dialog-builder.js";
11
- import type { ActiveView, StatefulView } from "../../view/stateful-view.js";
12
- import type { QuestionnaireState } from "../state.js";
9
+ import type { GlobalSelector, PerTabSelector } from "./contract.js";
13
10
  import { chatNumberingFor, selectActiveTabItems, selectConfirmedIndicator } from "./derivations.js";
14
11
 
15
- export function selectMultiSelectProps(
16
- state: QuestionnaireState,
17
- question: QuestionData,
18
- activeView: ActiveView,
19
- isLastQuestion: boolean,
20
- ): MultiSelectViewProps {
21
- const focused = activeView === "options";
12
+ export const selectMultiSelectProps: PerTabSelector<MultiSelectViewProps> = (state, ctx) => {
13
+ const question = ctx.questions[ctx.i];
14
+ if (!question) return { rows: [], nextActive: false, nextLabel: SENTINEL_LABELS.next };
15
+ const focused = ctx.activeView === "options";
22
16
  const rows: { checked: boolean; active: boolean }[] = [];
23
17
  for (let i = 0; i < question.options.length; i++) {
24
18
  rows.push({
@@ -27,79 +21,41 @@ export function selectMultiSelectProps(
27
21
  });
28
22
  }
29
23
  const nextActive = focused && state.optionIndex === question.options.length;
24
+ const isLastQuestion = ctx.i === ctx.questions.length - 1;
30
25
  const nextLabel = isLastQuestion ? MULTI_SUBMIT_LABEL : SENTINEL_LABELS.next;
31
26
  return { rows, nextActive, nextLabel };
32
- }
27
+ };
33
28
 
34
- /**
35
- * Per-tick projection for the active tab's `OptionListView`. Threads the
36
- * runtime-owned `inputBuffer` through to the view so the rendering primitive
37
- * (`WrappingSelect`) sees the value without per-keystroke reducer involvement.
38
- */
39
- export function selectOptionListProps(
40
- state: QuestionnaireState,
41
- items: readonly WrappingSelectItem[],
42
- questions: readonly QuestionData[],
43
- activeView: ActiveView,
44
- inputBuffer: string,
45
- ): OptionListViewProps {
46
- const focused = activeView === "options";
47
- const confirmed = selectConfirmedIndicator(questions, state.currentTab, state.answers, items);
29
+ export const selectOptionListProps: PerTabSelector<OptionListViewProps> = (state, ctx) => {
30
+ const items = ctx.itemsByTab[ctx.i] ?? [];
31
+ const focused = ctx.activeView === "options";
32
+ const confirmed = selectConfirmedIndicator(ctx.questions, state.currentTab, state.answers, items);
48
33
  return {
49
34
  selectedIndex: state.optionIndex,
50
35
  focused,
51
- inputBuffer,
36
+ inputBuffer: ctx.inputBuffer,
52
37
  ...(confirmed ? { confirmed } : {}),
53
38
  };
54
- }
39
+ };
55
40
 
56
- /**
57
- * Per-tick projection for the SubmitPicker. Two rows fixed (Submit /
58
- * Cancel); only `active` per row varies. Focus derives from the
59
- * `activeView === "submit"` discriminant.
60
- */
61
- export function selectSubmitPickerProps(
62
- state: QuestionnaireState,
63
- totalQuestions: number,
64
- activeView: ActiveView,
65
- ): SubmitPickerProps {
66
- const focused = activeView === "submit";
67
- void totalQuestions;
41
+ export const selectSubmitPickerProps: GlobalSelector<SubmitPickerProps> = (state, ctx) => {
42
+ const focused = ctx.activeView === "submit";
68
43
  return {
69
44
  rows: [
70
45
  { active: focused && state.submitChoiceIndex === 0 },
71
46
  { active: focused && state.submitChoiceIndex === 1 },
72
47
  ],
73
48
  };
74
- }
49
+ };
75
50
 
76
- /**
77
- * Per-tick projection for `PreviewPane`. Eliminates the sibling-coupling
78
- * where `PreviewPane` formerly read `optionListView.getSelectedIndex()` /
79
- * `isFocused()` live during render. Both `OptionListView` and
80
- * `PreviewPane.setProps` derive `selectedIndex` and `focused` from the same
81
- * canonical state per tick.
82
- */
83
- export function selectPreviewPaneProps(state: QuestionnaireState, activeView: ActiveView): PreviewPaneProps {
84
- return {
85
- notesVisible: state.notesVisible,
86
- selectedIndex: state.optionIndex,
87
- focused: activeView === "options",
88
- };
89
- }
51
+ export const selectPreviewPaneProps: PerTabSelector<PreviewPaneProps> = (state, ctx) => ({
52
+ notesVisible: state.notesVisible,
53
+ selectedIndex: state.optionIndex,
54
+ focused: ctx.activeView === "options",
55
+ });
90
56
 
91
- /**
92
- * Per-tick projection for the TabBar. Hoists all per-render derivations
93
- * (`allAnswered`, `answered`, `isActive`, `submitActive`) out of `tab-bar.ts`
94
- * into the selector. The Submit slot is an explicit `submit` field, not a
95
- * hidden `totalTabs` index. `Q{n}` fallback label is computed here once per
96
- * tab; `header` reads directly from question data.
97
- */
98
- export function selectTabBarProps(
99
- state: QuestionnaireState,
100
- questions: ReadonlyArray<{ header?: string; question: string }>,
101
- ): TabBarProps {
102
- const tabs = questions.map((q, i) => ({
57
+ export const selectTabBarProps: GlobalSelector<TabBarProps> = (state, ctx) => {
58
+ const tabs = ctx.questions.map((q, i) => ({
103
59
  label: q.header && q.header.length > 0 ? q.header : `Q${i + 1}`,
104
60
  answered: state.answers.has(i),
105
61
  active: i === state.currentTab,
@@ -107,43 +63,21 @@ export function selectTabBarProps(
107
63
  return {
108
64
  tabs,
109
65
  submit: {
110
- active: state.currentTab === questions.length,
111
- allAnswered: state.answers.size === questions.length && questions.length > 0,
66
+ active: state.currentTab === ctx.questions.length,
67
+ allAnswered: state.answers.size === ctx.questions.length && ctx.questions.length > 0,
112
68
  },
113
69
  };
114
- }
70
+ };
115
71
 
116
- /**
117
- * Per-tick projection for `ChatRowView`. Combines focus discriminant with
118
- * the numbering derivation (`chatNumberingFor` over the active tab's
119
- * items). `activeView === "chat"` is observably equivalent to
120
- * `state.chatFocused` because the dispatcher cascade and reducer's defensive
121
- * clears ensure `chatFocused` and `notesVisible`/Submit-tab cannot be true
122
- * simultaneously.
123
- */
124
- export function selectChatRowProps(
125
- state: QuestionnaireState,
126
- itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
127
- totalQuestions: number,
128
- activeView: ActiveView,
129
- ): ChatRowViewProps {
130
- const activeItems = selectActiveTabItems(itemsByTab, state.currentTab, totalQuestions);
72
+ export const selectChatRowProps: GlobalSelector<ChatRowViewProps> = (state, ctx) => {
73
+ const activeItems = selectActiveTabItems(ctx.itemsByTab, state.currentTab, ctx.totalQuestions);
131
74
  return {
132
- focused: activeView === "chat",
75
+ focused: ctx.activeView === "chat",
133
76
  numbering: chatNumberingFor(activeItems),
134
77
  };
135
- }
78
+ };
136
79
 
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
- }
80
+ export const selectDialogProps: GlobalSelector<DialogProps> = (state, ctx) => ({
81
+ state,
82
+ activePreviewPane: ctx.activePreviewPane,
83
+ });
@@ -1,90 +1,33 @@
1
+ import type {
2
+ BindingContext,
3
+ GlobalSelector,
4
+ PerTabBindingContext,
5
+ PerTabSelector,
6
+ } from "../state/selectors/contract.js";
1
7
  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";
8
+ import type { StatefulView } from "./stateful-view.js";
6
9
  import type { TabComponents } from "./tab-components.js";
7
10
 
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
11
  export interface ComponentBinding<P> {
44
12
  readonly component: StatefulView<P>;
45
- readonly select: (state: QuestionnaireState, ctx: BindingContext) => P;
13
+ readonly select: GlobalSelector<P>;
46
14
  }
47
15
 
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
16
  export interface PerTabBinding<P> {
56
17
  readonly resolve: (tab: TabComponents) => StatefulView<P> | undefined;
57
- readonly select: (state: QuestionnaireState, ctx: PerTabBindingContext) => P;
58
- readonly predicate?: (state: QuestionnaireState, ctx: PerTabBindingContext) => boolean;
18
+ readonly select: PerTabSelector<P>;
19
+ readonly predicate?: PerTabSelector<boolean>;
59
20
  }
60
21
 
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
22
  export interface BoundGlobalBinding {
68
23
  apply(state: QuestionnaireState, ctx: BindingContext): void;
69
24
  invalidate(): void;
70
25
  }
71
26
 
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
27
  export interface BoundPerTabBinding {
79
28
  apply(state: QuestionnaireState, ctx: PerTabBindingContext): void;
80
29
  }
81
30
 
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
31
  export function globalBinding<P>(spec: ComponentBinding<P>): BoundGlobalBinding {
89
32
  return {
90
33
  apply: (state, ctx) => spec.component.setProps(spec.select(state, ctx)),
@@ -92,13 +35,6 @@ export function globalBinding<P>(spec: ComponentBinding<P>): BoundGlobalBinding
92
35
  };
93
36
  }
94
37
 
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
38
  export function perTabBinding<P>(spec: PerTabBinding<P>): BoundPerTabBinding {
103
39
  return {
104
40
  apply: (state, ctx) => {
@@ -1,14 +1,10 @@
1
1
  import type { InputBuffer } from "../state/input-buffer.js";
2
+ import type { BindingContext, PerTabBindingContext } from "../state/selectors/contract.js";
2
3
  import { selectActivePreviewPaneIndex } from "../state/selectors/derivations.js";
3
4
  import { selectActiveView } from "../state/selectors/focus.js";
4
5
  import type { QuestionnaireState } from "../state/state.js";
5
6
  import type { QuestionData } from "../tool/types.js";
6
- import type {
7
- BindingContext,
8
- BoundGlobalBinding,
9
- BoundPerTabBinding,
10
- PerTabBindingContext,
11
- } from "./component-binding.js";
7
+ import type { BoundGlobalBinding, BoundPerTabBinding } from "./component-binding.js";
12
8
  import type { WrappingSelectItem } from "./components/wrapping-select.js";
13
9
  import type { TabComponents } from "./tab-components.js";
14
10