vc-extras 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/README.md +128 -71
  2. package/agent-install.mjs +159 -0
  3. package/agents/tester.md +23 -23
  4. package/install-extras.mjs +41 -109
  5. package/install-menu.mjs +22 -43
  6. package/install-skills.mjs +112 -112
  7. package/package.json +72 -28
  8. package/plugin-registry.mjs +73 -0
  9. package/plugins/config-ui/index.ts +159 -159
  10. package/plugins/config-ui/lib/arguments.mjs +24 -24
  11. package/plugins/config-ui/lib/documents.mjs +67 -67
  12. package/plugins/config-ui/lib/frontmatter.mjs +91 -91
  13. package/plugins/config-ui/lib/open.mjs +16 -16
  14. package/plugins/config-ui/lib/ownership.mjs +99 -99
  15. package/plugins/config-ui/lib/page.mjs +11 -11
  16. package/plugins/config-ui/lib/patch.mjs +35 -35
  17. package/plugins/config-ui/lib/paths.mjs +14 -14
  18. package/plugins/config-ui/lib/plugin-fields.mjs +86 -86
  19. package/plugins/config-ui/lib/plugin-validation.mjs +28 -28
  20. package/plugins/config-ui/lib/redaction.mjs +8 -8
  21. package/plugins/config-ui/lib/registry.mjs +282 -282
  22. package/plugins/config-ui/lib/secrets.mjs +44 -44
  23. package/plugins/config-ui/lib/server.mjs +250 -250
  24. package/plugins/config-ui/lib/sources.mjs +96 -96
  25. package/plugins/config-ui/lib/state.mjs +156 -156
  26. package/plugins/config-ui/lib/themes.mjs +113 -113
  27. package/plugins/config-ui/lib/writer.mjs +690 -690
  28. package/plugins/config-ui/project-local-owner.ts +106 -106
  29. package/plugins/config-ui/vc-extras.plugin.json +13 -0
  30. package/plugins/config-ui/web/app.mjs +997 -997
  31. package/plugins/config-ui/web/index.html +342 -342
  32. package/plugins/pi-blackhole/vc-extras.plugin.json +12 -0
  33. package/plugins/pi-btw/vc-extras.plugin.json +12 -0
  34. package/plugins/pi-claude-code/vc-extras.plugin.json +12 -0
  35. package/plugins/pi-fff/vc-extras.plugin.json +12 -0
  36. package/plugins/pi-jev-auto-mode/vc-extras.plugin.json +12 -0
  37. package/plugins/pi-mcp-adapter/vc-extras.plugin.json +12 -0
  38. package/plugins/pi-web-access/vc-extras.plugin.json +12 -0
  39. package/plugins/vc-ask/claude-hook.mjs +37 -0
  40. package/plugins/vc-ask/codex-native.mjs +28 -0
  41. package/plugins/vc-ask/host-channel.mjs +75 -0
  42. package/plugins/vc-ask/index.ts +57 -0
  43. package/plugins/vc-ask/native-questions.mjs +33 -0
  44. package/plugins/vc-ask/package.json +10 -0
  45. package/plugins/vc-ask/payload.ts +170 -0
  46. package/plugins/vc-ask/questionnaire/LICENSE.ask-user-question +21 -0
  47. package/plugins/vc-ask/questionnaire/UPSTREAM.md +104 -0
  48. package/plugins/vc-ask/questionnaire/answer-channel.ts +135 -0
  49. package/plugins/vc-ask/questionnaire/config.ts +180 -0
  50. package/plugins/vc-ask/questionnaire/events.ts +154 -0
  51. package/plugins/vc-ask/questionnaire/execute.ts +688 -0
  52. package/plugins/vc-ask/questionnaire/i18n.ts +73 -0
  53. package/plugins/vc-ask/questionnaire/inline-entry.ts +312 -0
  54. package/plugins/vc-ask/questionnaire/locales/de.json +43 -0
  55. package/plugins/vc-ask/questionnaire/locales/en.json +42 -0
  56. package/plugins/vc-ask/questionnaire/locales/es.json +43 -0
  57. package/plugins/vc-ask/questionnaire/locales/fr.json +43 -0
  58. package/plugins/vc-ask/questionnaire/locales/pt-BR.json +43 -0
  59. package/plugins/vc-ask/questionnaire/locales/pt.json +43 -0
  60. package/plugins/vc-ask/questionnaire/locales/ru.json +43 -0
  61. package/plugins/vc-ask/questionnaire/locales/uk.json +43 -0
  62. package/plugins/vc-ask/questionnaire/locales/zh.json +46 -0
  63. package/plugins/vc-ask/questionnaire/presentation.ts +11 -0
  64. package/plugins/vc-ask/questionnaire/question-preview.ts +6 -0
  65. package/plugins/vc-ask/questionnaire/rpc.ts +221 -0
  66. package/plugins/vc-ask/questionnaire/session-timeout.ts +67 -0
  67. package/plugins/vc-ask/questionnaire/state/build-questionnaire.ts +144 -0
  68. package/plugins/vc-ask/questionnaire/state/external-editor.ts +62 -0
  69. package/plugins/vc-ask/questionnaire/state/geometry.ts +131 -0
  70. package/plugins/vc-ask/questionnaire/state/key-router.ts +382 -0
  71. package/plugins/vc-ask/questionnaire/state/panel-router.ts +32 -0
  72. package/plugins/vc-ask/questionnaire/state/questionnaire-session.ts +642 -0
  73. package/plugins/vc-ask/questionnaire/state/row-intent.ts +143 -0
  74. package/plugins/vc-ask/questionnaire/state/selectors/contract.ts +24 -0
  75. package/plugins/vc-ask/questionnaire/state/selectors/derivations.ts +40 -0
  76. package/plugins/vc-ask/questionnaire/state/selectors/focus.ts +17 -0
  77. package/plugins/vc-ask/questionnaire/state/selectors/projections.ts +99 -0
  78. package/plugins/vc-ask/questionnaire/state/state-reducer.ts +455 -0
  79. package/plugins/vc-ask/questionnaire/state/state.ts +90 -0
  80. package/plugins/vc-ask/questionnaire/state/vertical.ts +158 -0
  81. package/plugins/vc-ask/questionnaire/timeout.ts +59 -0
  82. package/plugins/vc-ask/questionnaire/tool/external-answer.ts +103 -0
  83. package/plugins/vc-ask/questionnaire/tool/format-answer.ts +24 -0
  84. package/plugins/vc-ask/questionnaire/tool/normalize-params.ts +37 -0
  85. package/plugins/vc-ask/questionnaire/tool/response-envelope.ts +75 -0
  86. package/plugins/vc-ask/questionnaire/tool/types.ts +74 -0
  87. package/plugins/vc-ask/questionnaire/tool/validate-questionnaire.ts +75 -0
  88. package/plugins/vc-ask/questionnaire/view/card-theme.ts +245 -0
  89. package/plugins/vc-ask/questionnaire/view/components/inline-input.ts +66 -0
  90. package/plugins/vc-ask/questionnaire/view/components/multi-select-view.ts +177 -0
  91. package/plugins/vc-ask/questionnaire/view/components/option-list-view.ts +65 -0
  92. package/plugins/vc-ask/questionnaire/view/components/preview/markdown-content-cache.ts +101 -0
  93. package/plugins/vc-ask/questionnaire/view/components/preview/preview-block-renderer.ts +130 -0
  94. package/plugins/vc-ask/questionnaire/view/components/preview/preview-box-renderer.ts +86 -0
  95. package/plugins/vc-ask/questionnaire/view/components/preview/preview-layout-decider.ts +204 -0
  96. package/plugins/vc-ask/questionnaire/view/components/preview/preview-pane.ts +257 -0
  97. package/plugins/vc-ask/questionnaire/view/components/submit-picker.ts +65 -0
  98. package/plugins/vc-ask/questionnaire/view/components/tab-bar.ts +57 -0
  99. package/plugins/vc-ask/questionnaire/view/components/wrapping-select.ts +269 -0
  100. package/plugins/vc-ask/questionnaire/view/dialog-builder.ts +319 -0
  101. package/plugins/vc-ask/questionnaire/view/document-theme.ts +53 -0
  102. package/plugins/vc-ask/questionnaire/view/document-view.ts +306 -0
  103. package/plugins/vc-ask/questionnaire/view/panel-chrome.ts +133 -0
  104. package/plugins/vc-ask/questionnaire/view/props-adapter.ts +77 -0
  105. package/plugins/vc-ask/questionnaire/view/stateful-view.ts +24 -0
  106. package/plugins/vc-ask/questionnaire/view/tab-components.ts +16 -0
  107. package/plugins/vc-ask/questionnaire/view/tab-content-strategy.ts +280 -0
  108. package/plugins/vc-ask/questionnaire/view/vertical-view.ts +218 -0
  109. package/plugins/vc-ask/user-input-events.ts +34 -0
  110. package/plugins/vc-ask/vc-extras.plugin.json +18 -0
  111. package/plugins/vc-footer/agents/claude-code/adapter.mjs +29 -0
  112. package/plugins/vc-footer/agents/claude-code/cli.mjs +23 -0
  113. package/plugins/vc-footer/agents/claude-code/renderer.mjs +1 -0
  114. package/plugins/vc-footer/agents/claude-code/types.d.ts +38 -0
  115. package/plugins/vc-footer/agents/codex/adapter.mjs +13 -0
  116. package/plugins/vc-footer/agents/codex/mcp.mjs +45 -0
  117. package/plugins/vc-footer/agents/codex/native.mjs +19 -0
  118. package/plugins/vc-footer/agents/codex/renderer.mjs +2 -0
  119. package/plugins/vc-footer/agents/pi/adapter.mjs +23 -0
  120. package/plugins/vc-footer/agents/pi/index.js +84 -0
  121. package/plugins/vc-footer/agents/pi/project-local-owner.mjs +97 -0
  122. package/plugins/vc-footer/agents/pi/renderer.mjs +5 -0
  123. package/plugins/vc-footer/core/data.mjs +218 -0
  124. package/plugins/vc-footer/core/render.mjs +291 -0
  125. package/plugins/vc-footer/core/types.d.ts +29 -0
  126. package/plugins/vc-footer/package.json +22 -0
  127. package/plugins/vc-footer/vc-extras.plugin.json +32 -0
  128. package/skills/animate/LICENSE +21 -21
  129. package/skills/animate/RECIPES.md +324 -324
  130. package/skills/animate/SKILL.md +208 -208
  131. package/skills/animate/UPSTREAM.md +19 -19
  132. package/skills/animation-performance/LICENSE +21 -21
  133. package/skills/animation-performance/SKILL.md +223 -223
  134. package/skills/animation-performance/UPSTREAM.md +19 -19
  135. package/skills/answer-me-with-html/SKILL.md +174 -174
  136. package/skills/answer-me-with-html/scripts/am.mjs +12896 -12896
  137. package/skills/apple-design/LICENSE +21 -21
  138. package/skills/apple-design/SKILL.md +291 -291
  139. package/skills/apple-design/UPSTREAM.md +19 -19
  140. package/skills/bad-smell/SKILL.md +44 -44
  141. package/skills/commit/SKILL.md +90 -90
  142. package/skills/emil-design-eng/LICENSE +21 -21
  143. package/skills/emil-design-eng/SKILL.md +677 -677
  144. package/skills/emil-design-eng/UPSTREAM.md +19 -19
  145. package/skills/find-the-truth/SKILL.md +38 -38
  146. package/skills/find-the-truth/references/trace-method.md +48 -48
  147. package/skills/find-the-truth/references/truth-method.md +39 -39
  148. package/skills/gestures/LICENSE +21 -21
  149. package/skills/gestures/SKILL.md +107 -107
  150. package/skills/gestures/UPSTREAM.md +19 -19
  151. package/skills/humanizer-zh/LICENSE +21 -21
  152. package/skills/humanizer-zh/SKILL.md +409 -409
  153. package/skills/humanizer-zh/UPSTREAM.md +7 -7
  154. package/skills/impeccable/LICENSE +191 -191
  155. package/skills/impeccable/NOTICE.md +11 -11
  156. package/skills/impeccable/SKILL.md +89 -89
  157. package/skills/impeccable/licenses/modern-screenshot-MIT.txt +8 -8
  158. package/skills/impeccable/licenses/platform-design-skills-MIT.txt +21 -21
  159. package/skills/impeccable/reference/adapt.md +318 -318
  160. package/skills/impeccable/reference/adapt.native.md +58 -58
  161. package/skills/impeccable/reference/android.md +46 -46
  162. package/skills/impeccable/reference/animate.md +89 -89
  163. package/skills/impeccable/reference/audit.md +139 -139
  164. package/skills/impeccable/reference/audit.native.md +139 -139
  165. package/skills/impeccable/reference/bolder.md +33 -33
  166. package/skills/impeccable/reference/clarify.md +94 -94
  167. package/skills/impeccable/reference/colorize.md +86 -86
  168. package/skills/impeccable/reference/component-review.md +63 -63
  169. package/skills/impeccable/reference/craft-floor.md +44 -44
  170. package/skills/impeccable/reference/craft.md +5 -5
  171. package/skills/impeccable/reference/critique.md +819 -819
  172. package/skills/impeccable/reference/degraded/asset-producer.md +41 -41
  173. package/skills/impeccable/reference/degraded/documenter.md +23 -23
  174. package/skills/impeccable/reference/degraded/finish-reviewer.md +37 -37
  175. package/skills/impeccable/reference/degraded/manual-edit-applier.md +92 -92
  176. package/skills/impeccable/reference/delight.md +70 -70
  177. package/skills/impeccable/reference/distill.md +111 -111
  178. package/skills/impeccable/reference/doctor.md +54 -54
  179. package/skills/impeccable/reference/document.md +416 -416
  180. package/skills/impeccable/reference/extract.md +69 -69
  181. package/skills/impeccable/reference/generate.md +101 -101
  182. package/skills/impeccable/reference/harden.md +362 -362
  183. package/skills/impeccable/reference/hooks.md +114 -114
  184. package/skills/impeccable/reference/init.md +131 -131
  185. package/skills/impeccable/reference/ios.md +51 -51
  186. package/skills/impeccable/reference/layout.md +84 -84
  187. package/skills/impeccable/reference/live-setup.md +104 -104
  188. package/skills/impeccable/reference/live.md +327 -327
  189. package/skills/impeccable/reference/mode-operate.md +21 -21
  190. package/skills/impeccable/reference/mode-persuade.md +19 -19
  191. package/skills/impeccable/reference/mode-read.md +21 -21
  192. package/skills/impeccable/reference/new-work.md +152 -152
  193. package/skills/impeccable/reference/onboard.md +234 -234
  194. package/skills/impeccable/reference/operate.md +61 -61
  195. package/skills/impeccable/reference/optimize.md +257 -257
  196. package/skills/impeccable/reference/overdrive.md +127 -127
  197. package/skills/impeccable/reference/polish.md +105 -105
  198. package/skills/impeccable/reference/quieter.md +99 -99
  199. package/skills/impeccable/reference/region-map.md +26 -26
  200. package/skills/impeccable/reference/routing.md +24 -24
  201. package/skills/impeccable/reference/shape.md +59 -59
  202. package/skills/impeccable/reference/typeset.md +90 -90
  203. package/skills/impeccable/reference/visualize.md +47 -47
  204. package/skills/impeccable/scripts/VERSION +1 -1
  205. package/skills/impeccable/scripts/command-metadata.json +98 -98
  206. package/skills/impeccable/scripts/data/font-index-failures.json +120 -120
  207. package/skills/impeccable/scripts/engine-checksums.txt +9 -9
  208. package/skills/impeccable/scripts/impeccable +249 -249
  209. package/skills/impeccable/scripts/impeccable.cmd +278 -278
  210. package/skills/impeccable/scripts/live-browser-dom.js +320 -320
  211. package/skills/impeccable/scripts/live-browser-ignores.js +246 -246
  212. package/skills/impeccable/scripts/live-browser-session.js +183 -183
  213. package/skills/impeccable/scripts/live-browser.js +13862 -13862
  214. package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -14
  215. package/skills/improve-animations/AUDIT.md +115 -115
  216. package/skills/improve-animations/LICENSE +21 -21
  217. package/skills/improve-animations/PLAN-TEMPLATE.md +73 -73
  218. package/skills/improve-animations/SKILL.md +110 -110
  219. package/skills/improve-animations/UPSTREAM.md +19 -19
  220. package/skills/pick-ui-library/LICENSE +21 -21
  221. package/skills/pick-ui-library/SKILL.md +85 -85
  222. package/skills/pick-ui-library/UPSTREAM.md +19 -19
  223. package/skills/review-animations/LICENSE +21 -21
  224. package/skills/review-animations/SKILL.md +120 -120
  225. package/skills/review-animations/STANDARDS.md +187 -187
  226. package/skills/review-animations/UPSTREAM.md +19 -19
  227. package/skills/test/SKILL.md +29 -29
  228. package/skills/test/references/test-method.md +60 -60
  229. package/skills/write-doc/SKILL.md +115 -115
  230. package/skills/write-doc/examples/result-report.md +53 -53
  231. package/skills/write-doc/examples/task-summary.md +45 -45
  232. package/skills/write-doc/references/terminology.md +147 -147
  233. package/specs/ask-host-protocol-v1.md +58 -0
  234. package/specs/design-demo.md +1102 -1102
  235. package/specs/frontend-immersive.md +28 -28
  236. package/specs/plugin-install-v2.md +123 -0
  237. package/specs/plugin-install.md +38 -0
  238. package/specs/web-modes/explore.md +31 -31
  239. package/specs/web-modes/risk.md +15 -15
  240. package/specs/web-modes/verify.md +16 -16
  241. package/vc-extras.mjs +46 -62
  242. package/plugins/essrj/context.ts +0 -47
  243. package/plugins/essrj/data.ts +0 -190
  244. package/plugins/essrj/index.ts +0 -139
  245. package/plugins/essrj/package.json +0 -12
  246. package/plugins/essrj/project-local-owner.ts +0 -106
  247. package/plugins/essrj/render.ts +0 -217
  248. package/plugins/essrj/types.ts +0 -40
  249. package/plugins.json +0 -38
@@ -0,0 +1,455 @@
1
+ import type { QuestionAnswer, QuestionData, QuestionnaireResult } from "../tool/types.js";
2
+ import type { WrappingSelectItem } from "../view/components/wrapping-select.js";
3
+ import type { QuestionnaireAction } from "./key-router.js";
4
+ import {
5
+ clampDockRows,
6
+ clampFloat,
7
+ dockCapacity,
8
+ floatDefault,
9
+ floatPanel,
10
+ isDocked,
11
+ maximize,
12
+ type TerminalSize,
13
+ } from "./geometry.js";
14
+ import { ROW_INTENT_META } from "./row-intent.js";
15
+ import { reduceVertical, REVIEW_SUBMIT_ROW } from "./vertical.js";
16
+ import type { QuestionnaireState } from "./state.js";
17
+
18
+ /** Fallback viewport for reducer callers without a live terminal (unit tests). */
19
+ const DEFAULT_SIZE: TerminalSize = { columns: 80, rows: 24 };
20
+
21
+ /** Session-lifetime constants. No live-component reads — peripheral values live on canonical state. */
22
+ export interface ApplyContext {
23
+ document?: boolean;
24
+ vertical?: boolean;
25
+ questions: readonly QuestionData[];
26
+ itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
27
+ /** Live terminal viewport; geometry clamping needs it. Absent only in unit tests. */
28
+ size?: TerminalSize;
29
+ }
30
+
31
+ /**
32
+ * Declarative side-effects emitted by `reduce`. The runtime executes them after
33
+ * committing the new state, then asks the props-adapter to re-project. Closed set —
34
+ * adding an effect requires updating both the union AND the runtime's `runEffect` switch
35
+ * (compiler-enforced exhaustive). No string-keyed escape hatch.
36
+ */
37
+ export type Effect =
38
+ | { kind: "set_input_buffer"; value: string }
39
+ | { kind: "clear_input_buffer" }
40
+ | { kind: "open_input_editor"; value: string }
41
+ | { kind: "set_notes_value"; value: string }
42
+ | { kind: "set_notes_focused"; focused: boolean }
43
+ | { kind: "forward_notes_keystroke"; data: string }
44
+ /** Synchronize the session's collapsed presentation after `toggle_collapsed`. */
45
+ | { kind: "set_collapsed"; collapsed: boolean }
46
+ | { kind: "done"; result: QuestionnaireResult };
47
+
48
+ export interface ApplyResult {
49
+ state: QuestionnaireState;
50
+ effects: readonly Effect[];
51
+ }
52
+
53
+ function orderedAnswers(state: QuestionnaireState, questions: readonly QuestionData[]): QuestionAnswer[] {
54
+ const out: QuestionAnswer[] = [];
55
+ for (let i = 0; i < questions.length; i++) {
56
+ const a = state.answers.get(i);
57
+ if (a) out.push(a);
58
+ }
59
+ return out;
60
+ }
61
+
62
+ function syncMultiSelectFromAnswers(
63
+ answers: ReadonlyMap<number, QuestionAnswer>,
64
+ questions: readonly QuestionData[],
65
+ tab: number,
66
+ ): ReadonlySet<number> {
67
+ const q = questions[tab];
68
+ if (!q?.multiSelect) return new Set();
69
+ const saved = answers.get(tab);
70
+ const labels = saved?.selected ?? [];
71
+ const indices = new Set<number>();
72
+ for (let i = 0; i < q.options.length; i++) {
73
+ if (labels.includes(q.options[i]!.label)) indices.add(i);
74
+ }
75
+ return indices;
76
+ }
77
+
78
+ function persistMultiSelectAnswer(state: QuestionnaireState, ctx: ApplyContext): ReadonlyMap<number, QuestionAnswer> {
79
+ const q = ctx.questions[state.currentTab];
80
+ if (!q?.multiSelect) return state.answers;
81
+ const selected: string[] = [];
82
+ for (let i = 0; i < q.options.length; i++) {
83
+ if (state.multiSelectChecked.has(i)) selected.push(q.options[i]!.label);
84
+ }
85
+ const out = new Map(state.answers);
86
+ if (selected.length === 0) {
87
+ out.delete(state.currentTab);
88
+ return out;
89
+ }
90
+ const pendingNotes = state.notesByTab.get(state.currentTab);
91
+ out.set(state.currentTab, {
92
+ questionIndex: state.currentTab,
93
+ question: q.question,
94
+ kind: "multi",
95
+ answer: null,
96
+ selected,
97
+ ...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
98
+ });
99
+ return out;
100
+ }
101
+
102
+ /**
103
+ * Note text to seed the editor/draft with for a tab. The in-flight side-band store
104
+ * (`notesByTab`) is authoritative — before an option is confirmed the note lives ONLY
105
+ * there; `answer.notes` is a mirror written on exit/confirm.
106
+ */
107
+ function notesValueFor(state: QuestionnaireState, tab: number): string {
108
+ return state.notesByTab.get(tab) ?? state.answers.get(tab)?.notes ?? "";
109
+ }
110
+
111
+ function customDraftValueFor(state: QuestionnaireState, tab: number): string {
112
+ const draft = state.customDraftsByTab.get(tab);
113
+ if (draft !== undefined) return draft;
114
+ const answer = state.answers.get(tab);
115
+ return answer?.kind === "custom" && typeof answer.answer === "string" ? answer.answer : "";
116
+ }
117
+
118
+ function setCustomDraft(state: QuestionnaireState, tab: number, value: string): ReadonlyMap<number, string> {
119
+ const drafts = new Map(state.customDraftsByTab);
120
+ drafts.set(tab, value);
121
+ return drafts;
122
+ }
123
+
124
+ function withoutCustomDraft(state: QuestionnaireState, tab: number): ReadonlyMap<number, string> {
125
+ if (!state.customDraftsByTab.has(tab)) return state.customDraftsByTab;
126
+ const drafts = new Map(state.customDraftsByTab);
127
+ drafts.delete(tab);
128
+ return drafts;
129
+ }
130
+
131
+ function switchTabResult(state: QuestionnaireState, nextTab: number, ctx: ApplyContext): ApplyResult {
132
+ const notesValue = notesValueFor(state, nextTab);
133
+ const transitioned: QuestionnaireState = {
134
+ ...state,
135
+ currentTab: nextTab,
136
+ optionIndex: 0,
137
+ inputMode: false,
138
+ notesVisible: false,
139
+ submitChoiceIndex: 0,
140
+ multiSelectChecked: syncMultiSelectFromAnswers(state.answers, ctx.questions, nextTab),
141
+ notesDraft: notesValue,
142
+ previewScrollOffset: ctx.questions[nextTab]?.preview ? 0 : null,
143
+ };
144
+ return {
145
+ state: transitioned,
146
+ effects: [
147
+ { kind: "set_notes_focused", focused: false },
148
+ { kind: "set_notes_value", value: notesValue },
149
+ { kind: "set_input_buffer", value: customDraftValueFor(state, nextTab) },
150
+ ],
151
+ };
152
+ }
153
+
154
+ function doneFor(state: QuestionnaireState, ctx: ApplyContext, cancelled: boolean): ApplyResult {
155
+ const result: QuestionnaireResult = {
156
+ answers: orderedAnswers(state, ctx.questions),
157
+ cancelled,
158
+ ...(ctx.vertical && !cancelled ? { discussion: true, unanswered: ctx.questions.flatMap((_, i) => state.answers.has(i) ? [] : [i]) } : {}),
159
+ ...(state.globalNote && state.globalNote.length > 0 ? { globalNote: state.globalNote } : {}),
160
+ };
161
+ return { state, effects: [{ kind: "done", result }] };
162
+ }
163
+
164
+ /**
165
+ * Per-kind handler signature: action payload narrows to the matching union member
166
+ * via `Extract`, so handlers consume fully-typed actions without `as` casts.
167
+ */
168
+ type Handler<K extends QuestionnaireAction["kind"]> = (
169
+ state: QuestionnaireState,
170
+ action: Extract<QuestionnaireAction, { kind: K }>,
171
+ ctx: ApplyContext,
172
+ ) => ApplyResult;
173
+
174
+ const navHandler: Handler<"nav"> = (state, action, ctx) => {
175
+ const items = ctx.itemsByTab[state.currentTab] ?? [];
176
+ const item = items[action.nextIndex];
177
+ const inputMode = item ? ROW_INTENT_META[item.kind].activatesInputMode : false;
178
+ const customDraftsByTab = state.inputMode
179
+ ? setCustomDraft(state, state.currentTab, action.inputValue)
180
+ : state.customDraftsByTab;
181
+ const next: QuestionnaireState = {
182
+ ...state,
183
+ optionIndex: action.nextIndex,
184
+ inputMode,
185
+ customDraftsByTab,
186
+ previewScrollOffset: ctx.document ? state.previewScrollOffset : null,
187
+ };
188
+ if (!inputMode) return { state: next, effects: [] };
189
+ return {
190
+ state: next,
191
+ effects: [{ kind: "set_input_buffer", value: customDraftValueFor(next, state.currentTab) }],
192
+ };
193
+ };
194
+
195
+ const inputClearHandler: Handler<"input_clear"> = (state, _action, ctx) => {
196
+ const next = ctx.vertical ? reduceVertical(state, { kind: "list_save", value: "", edited: true }, ctx).state
197
+ : { ...state, customDraftsByTab: setCustomDraft(state, state.currentTab, "") };
198
+ return { state: next, effects: [{ kind: "clear_input_buffer" }] };
199
+ };
200
+ const inputEditHandler: Handler<"input_edit"> = (state, action, _ctx) => ({
201
+ state,
202
+ effects: [{ kind: "open_input_editor", value: action.value }],
203
+ });
204
+ const inputReplaceHandler: Handler<"input_replace"> = (state, action, ctx) => {
205
+ const next = ctx.vertical ? reduceVertical(state, { kind: "list_save", value: action.value, edited: true }, ctx).state
206
+ : { ...state, customDraftsByTab: setCustomDraft(state, state.currentTab, action.value) };
207
+ return { state: next, effects: [{ kind: "set_input_buffer", value: action.value }] };
208
+ };
209
+
210
+ const tabSwitchHandler: Handler<"tab_switch"> = (state, action, ctx) => switchTabResult(state, action.nextTab, ctx);
211
+
212
+ const confirmHandler: Handler<"confirm"> = (state, action, ctx) => {
213
+ let answer = action.answer;
214
+ if (answer.kind === "option" && answer.answer) {
215
+ const q = ctx.questions[answer.questionIndex];
216
+ const matched = q?.options.find((o) => o.label === answer.answer);
217
+ if (matched?.preview && matched.preview.length > 0) {
218
+ answer = { ...answer, preview: matched.preview };
219
+ }
220
+ }
221
+ const pendingNotes = state.notesByTab.get(answer.questionIndex);
222
+ if (pendingNotes && pendingNotes.length > 0) {
223
+ answer = { ...answer, notes: pendingNotes };
224
+ }
225
+ const answers = new Map(state.answers);
226
+ answers.set(answer.questionIndex, answer);
227
+ // Custom free-text on a multi-select tab is mutually exclusive with checkbox selections:
228
+ // clear the checked set immediately so [✔] glyphs vanish on Enter. (A custom answer
229
+ // carries no `selected` array, so syncMultiSelectFromAnswers keeps it empty on tab-back.)
230
+ const isCustomMulti = answer.kind === "custom" && ctx.questions[answer.questionIndex]?.multiSelect === true;
231
+ const customDraftsByTab =
232
+ answer.kind === "custom" ? withoutCustomDraft(state, answer.questionIndex) : state.customDraftsByTab;
233
+ const next: QuestionnaireState = {
234
+ ...state,
235
+ answers,
236
+ customDraftsByTab,
237
+ ...(isCustomMulti ? { multiSelectChecked: new Set<number>() } : {}),
238
+ };
239
+ if (action.autoAdvanceTab !== undefined) return switchTabResult(next, action.autoAdvanceTab, ctx);
240
+ return doneFor(next, ctx, false);
241
+ };
242
+
243
+ const toggleHandler: Handler<"toggle"> = (state, action, ctx) => {
244
+ const checked = new Set(state.multiSelectChecked);
245
+ if (checked.has(action.index)) checked.delete(action.index);
246
+ else checked.add(action.index);
247
+ const intermediate: QuestionnaireState = { ...state, multiSelectChecked: checked };
248
+ const answers = persistMultiSelectAnswer(intermediate, ctx);
249
+ return { state: { ...intermediate, answers }, effects: [] };
250
+ };
251
+
252
+ const multiConfirmHandler: Handler<"multi_confirm"> = (state, action, ctx) => {
253
+ const q = ctx.questions[state.currentTab];
254
+ if (!q) return { state, effects: [] };
255
+ const pendingNotes = state.notesByTab.get(state.currentTab);
256
+ const answers = new Map(state.answers);
257
+ answers.set(state.currentTab, {
258
+ questionIndex: state.currentTab,
259
+ question: q.question,
260
+ kind: "multi",
261
+ answer: null,
262
+ selected: action.selected,
263
+ ...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
264
+ });
265
+ const synced: QuestionnaireState = {
266
+ ...state,
267
+ answers,
268
+ multiSelectChecked: syncMultiSelectFromAnswers(answers, ctx.questions, state.currentTab),
269
+ };
270
+ if (action.autoAdvanceTab !== undefined) return switchTabResult(synced, action.autoAdvanceTab, ctx);
271
+ return doneFor(synced, ctx, false);
272
+ };
273
+
274
+ const notesEnterHandler: Handler<"notes_enter"> = (state, _action, ctx) => {
275
+ // On the review screen the notes editor edits the whole-questionnaire note instead of a
276
+ // per-question one, so the same editor and key handling serve both.
277
+ const review = state.verticalScreen === "review";
278
+ const value = review ? state.globalNote ?? state.notesDraft ?? "" : notesValueFor(state, state.currentTab);
279
+ return {
280
+ state: { ...state, notesVisible: true, notesDraft: value, previewScrollOffset: ctx.document ? state.previewScrollOffset : null },
281
+ effects: [
282
+ { kind: "set_notes_value", value },
283
+ { kind: "set_notes_focused", focused: true },
284
+ ],
285
+ };
286
+ };
287
+
288
+ const notesExitHandler: Handler<"notes_exit"> = (state, _action, _ctx) => {
289
+ const trimmed = state.notesDraft.trim();
290
+ if (state.verticalScreen === "review") {
291
+ return {
292
+ state: { ...state, globalNote: trimmed.length > 0 ? trimmed : undefined, notesVisible: false },
293
+ effects: [{ kind: "set_notes_focused", focused: false }],
294
+ };
295
+ }
296
+ const notes = new Map(state.notesByTab);
297
+ const answers = new Map(state.answers);
298
+ if (trimmed.length === 0) {
299
+ notes.delete(state.currentTab);
300
+ const prev = answers.get(state.currentTab);
301
+ if (prev?.notes) {
302
+ const stripped = { ...prev };
303
+ delete (stripped as { notes?: string }).notes;
304
+ answers.set(state.currentTab, stripped);
305
+ }
306
+ } else {
307
+ notes.set(state.currentTab, trimmed);
308
+ const prev = answers.get(state.currentTab);
309
+ if (prev) answers.set(state.currentTab, { ...prev, notes: trimmed });
310
+ }
311
+ return {
312
+ state: { ...state, notesByTab: notes, answers, notesVisible: false },
313
+ effects: [{ kind: "set_notes_focused", focused: false }],
314
+ };
315
+ };
316
+
317
+ /** Panel geometry: floating actions no-op while docked; `[⤢]` promotes a docked panel to the card form. */
318
+ const panelMoveHandler: Handler<"panel_move"> = (state, action, ctx) => {
319
+ if (isDocked(state)) return { state, effects: [] };
320
+ const size = ctx.size ?? DEFAULT_SIZE;
321
+ const current = floatPanel(state, size);
322
+ const panel = clampFloat({ ...current, col: action.col, row: action.row }, size);
323
+ return { state: { ...state, panel, panelAutoSize: false }, effects: [] };
324
+ };
325
+
326
+ const panelResizeHandler: Handler<"panel_resize"> = (state, action, ctx) => {
327
+ if (isDocked(state)) return { state, effects: [] };
328
+ const size = ctx.size ?? DEFAULT_SIZE;
329
+ const current = floatPanel(state, size);
330
+ return { state: { ...state, panelAutoSize: false, panel: clampFloat({ ...current, w: action.w, h: action.h }, size) }, effects: [] };
331
+ };
332
+
333
+ const panelMaximizeHandler: Handler<"panel_maximize"> = (state, _action, ctx) => {
334
+ const size = ctx.size ?? DEFAULT_SIZE;
335
+ const current = isDocked(state) ? dockedRect(state, size) : floatPanel(state, size);
336
+ if (current.w >= size.columns && current.h >= size.rows) {
337
+ const restored = clampFloat(state.restorePanel ?? floatDefault(size), size);
338
+ return { state: { ...state, panelForm: "float", panel: restored, restorePanel: null, panelAutoSize: false }, effects: [] };
339
+ }
340
+ return { state: { ...state, panelForm: "float", panel: maximize(size), restorePanel: current, panelAutoSize: false }, effects: [] };
341
+ };
342
+
343
+ const panelResetHandler: Handler<"panel_reset"> = (state, _action, ctx) => ({
344
+ state: { ...state, panelForm: "float", panel: floatDefault(ctx.size ?? DEFAULT_SIZE), restorePanel: null, panelAutoSize: true },
345
+ effects: [],
346
+ });
347
+
348
+ const panelPromoteHandler: Handler<"panel_promote"> = (state, _action, ctx) => {
349
+ const size = ctx.size ?? DEFAULT_SIZE;
350
+ return { state: { ...state, panelForm: "float", panel: floatDefault(size), restorePanel: state.panel ?? null }, effects: [] };
351
+ };
352
+
353
+ const dockResizeHandler: Handler<"dock_resize"> = (state, action, ctx) => {
354
+ const size = ctx.size ?? DEFAULT_SIZE;
355
+ return { state: { ...state, dockRows: clampDockRows(action.rows, size, ctx.vertical) }, effects: [] };
356
+ };
357
+
358
+ /** Dock rect without content measurement; only used by handlers that need a rectangle, not a height. */
359
+ function dockedRect(state: QuestionnaireState, size: TerminalSize) {
360
+ const h = clampDockRows(state.dockRows ?? size.rows, size);
361
+ return { col: 0, row: Math.max(0, size.rows - h), w: size.columns, h };
362
+ }
363
+
364
+ const cancelHandler: Handler<"cancel"> = (s, _a, c) => doneFor(s, c, true);
365
+ const submitHandler: Handler<"submit"> = (s, _a, c) => {
366
+ return doneFor(s, c, false);
367
+ };
368
+ const reviewEnterHandler: Handler<"review_enter"> = (s, _a, _c) => ({
369
+ // The review opens with the submit row focused: Enter, Enter submits, and the global note
370
+ // sits one row above it.
371
+ state: { ...s, verticalScreen: "review", optionIndex: REVIEW_SUBMIT_ROW, inputMode: false, notesVisible: false },
372
+ effects: [],
373
+ });
374
+ const reviewFocusHandler: Handler<"review_focus"> = (s, a, _c) => ({
375
+ state: { ...s, optionIndex: a.index, verticalReviewIndex: a.index },
376
+ effects: [],
377
+ });
378
+ const reviewExitHandler: Handler<"review_exit"> = (s, _a, c) =>
379
+ c.vertical ? reduceVertical(s, { kind: "list_focus", question: c.questions.length - 1 }, c) : {
380
+ state: { ...s, currentTab: Math.min(s.currentTab, c.questions.length - 1), verticalScreen: "list", optionIndex: 0 },
381
+ effects: [],
382
+ };
383
+ const submitNavHandler: Handler<"submit_nav"> = (s, a, _c) => ({
384
+ state: { ...s, submitChoiceIndex: a.nextIndex },
385
+ effects: [],
386
+ });
387
+ const notesForwardHandler: Handler<"notes_forward"> = (s, a, _c) => ({
388
+ state: s,
389
+ effects: [{ kind: "forward_notes_keystroke", data: a.data }],
390
+ });
391
+ const previewScrollHandler: Handler<"preview_scroll"> = (state, action, _ctx) => ({
392
+ state: { ...state, previewScrollOffset: action.offset },
393
+ effects: [],
394
+ });
395
+ const toggleCollapsedHandler: Handler<"toggle_collapsed"> = (s, _a, _c) => ({
396
+ state: { ...s, collapsed: !s.collapsed },
397
+ effects: [{ kind: "set_collapsed", collapsed: !s.collapsed }],
398
+ });
399
+ const ignoreHandler: Handler<"ignore"> = (s, _a, _c) => ({ state: s, effects: [] });
400
+
401
+ /**
402
+ * Compile-time-exhaustive dispatch table. `{ [K in Kind]: Handler<K> }` requires
403
+ * an entry per union member — adding a new `QuestionnaireAction` variant fails to
404
+ * compile here until a handler is registered, mirroring the `Record<RowKind, …>`
405
+ * pattern used by `ROW_INTENT_META`.
406
+ */
407
+ const HANDLERS: { [K in QuestionnaireAction["kind"]]: Handler<K> } = {
408
+ cancel_arm: reduceVertical,
409
+ list_focus: reduceVertical,
410
+ list_edit: reduceVertical,
411
+ list_save: reduceVertical,
412
+ list_rollback: reduceVertical,
413
+ list_select: reduceVertical,
414
+ list_defer: reduceVertical,
415
+ review_open: reduceVertical,
416
+ review_enter: (s,a,c) => c.vertical ? reduceVertical(s,a,c) : reviewEnterHandler(s,a,c),
417
+ review_focus: reviewFocusHandler,
418
+ review_exit: reviewExitHandler,
419
+ nav: navHandler,
420
+ input_clear: inputClearHandler,
421
+ input_edit: inputEditHandler,
422
+ input_replace: inputReplaceHandler,
423
+ tab_switch: tabSwitchHandler,
424
+ confirm: confirmHandler,
425
+ toggle: toggleHandler,
426
+ multi_confirm: multiConfirmHandler,
427
+ cancel: cancelHandler,
428
+ notes_enter: notesEnterHandler,
429
+ notes_exit: notesExitHandler,
430
+ notes_forward: notesForwardHandler,
431
+ submit: submitHandler,
432
+ submit_nav: submitNavHandler,
433
+ preview_scroll: previewScrollHandler,
434
+ toggle_collapsed: toggleCollapsedHandler,
435
+ panel_move: panelMoveHandler,
436
+ panel_resize: panelResizeHandler,
437
+ panel_maximize: panelMaximizeHandler,
438
+ panel_reset: panelResetHandler,
439
+ panel_promote: panelPromoteHandler,
440
+ dock_resize: dockResizeHandler,
441
+ ignore: ignoreHandler,
442
+ };
443
+
444
+ /**
445
+ * Pure reducer: (state, action, ctx) → (state, Effect[]). Mirrors `rpiv-todo`'s `applyTaskMutation`.
446
+ * Delegates to `HANDLERS` — per-kind handlers above are pure, named, and individually testable.
447
+ * `ignore` is also handled outside the reducer by `handleIgnoreInline` in the runtime fast path.
448
+ */
449
+ export function reduce(state: QuestionnaireState, action: QuestionnaireAction, ctx: ApplyContext): ApplyResult {
450
+ const handler = HANDLERS[action.kind] as Handler<typeof action.kind>;
451
+ // A cancellation confirmation is armed only until the next unrelated action.
452
+ const current = ctx.vertical && action.kind !== "cancel_arm" && action.kind !== "cancel"
453
+ ? { ...state, cancelArmed: false, verticalMessage: "" } : state;
454
+ return handler(current, action as never, ctx);
455
+ }
@@ -0,0 +1,90 @@
1
+ import type { QuestionAnswer, QuestionData } from "../tool/types.js";
2
+ import type { PanelGeometry } from "./geometry.js";
3
+ import type { WrappingSelectItem } from "../view/components/wrapping-select.js";
4
+
5
+ export interface QuestionnaireViewport {
6
+ scrollStart: number;
7
+ pageSize: number;
8
+ maxScrollStart: number;
9
+ }
10
+
11
+ /**
12
+ * Canonical state for the questionnaire dialog. Single source of truth — both the
13
+ * dispatcher (`routeKey`) and the view layer read this same shape.
14
+ */
15
+ export interface QuestionnaireState {
16
+ cancelArmed?: boolean;
17
+ /** Questions explicitly left for later; never option answers. */
18
+ deferredQuestions?: ReadonlySet<number>;
19
+ verticalMessage?: string;
20
+ verticalReviewIndex?: number;
21
+ currentTab: number;
22
+ optionIndex: number;
23
+ inputMode: boolean;
24
+ notesVisible: boolean;
25
+ answers: ReadonlyMap<number, QuestionAnswer>;
26
+ multiSelectChecked: ReadonlySet<number>;
27
+ /** In-flight custom answers keyed by tab. A present empty string overrides an older answer. */
28
+ customDraftsByTab: ReadonlyMap<number, string>;
29
+ /**
30
+ * Pre-answer notes side-band, keyed by tab index. Decoupled from `answers` so adding
31
+ * notes does NOT mark a question answered (the Submit-tab missing-check would falsely
32
+ * pass otherwise). Merged into the answer at confirm time.
33
+ */
34
+ notesByTab: ReadonlyMap<number, string>;
35
+ /** Focused row in the Submit-tab picker (0 = Submit, 1 = Cancel). Reset on tab switch. */
36
+ submitChoiceIndex: number;
37
+ /** Canonical mirror of the in-flight notes editor; runtime mirrors after `forward_notes_keystroke`. */
38
+ notesDraft: string;
39
+ /** Explicit plan-preview viewport. `null` restores automatic focus-following for option navigation. */
40
+ previewScrollOffset: number | null;
41
+ /**
42
+ * Collapsed mode: the active transcript entry shrinks to a single hint row so earlier
43
+ * agent output is easy to read. Toggled by `Ctrl+]` from any state; while true, question
44
+ * mutations are blocked except cancellation (see `routeKey`), while unhandled keys can
45
+ * still reach the host for transcript scrolling or other input.
46
+ */
47
+ collapsed: boolean;
48
+ /** Presentation form: `dock` sits in the input area, `float` is a movable card. */
49
+ panelForm?: "dock" | "float";
50
+ /** False after an explicit move/resize/maximize; otherwise fit content within viewport. */
51
+ panelAutoSize?: boolean;
52
+ /** Floating panel rect in terminal cells (0-based screen coordinates). */
53
+ panel?: PanelGeometry;
54
+ /** Rect restored by the next minimise; set when a panel is maximised or promoted. */
55
+ restorePanel?: PanelGeometry | null;
56
+ /** User-set dock height in rows; `null` fits the content up to the dock capacity. */
57
+ dockRows?: number | null;
58
+ /**
59
+ * Vertical list screen: the question list, or the pre-submit review. The review is only
60
+ * reachable in the vertical multi-question flow; single-question and document asks submit
61
+ * straight from the option row.
62
+ */
63
+ verticalScreen?: "list" | "review";
64
+ /** Whole-questionnaire note, collected on the review screen and reported in the result. */
65
+ globalNote?: string;
66
+ }
67
+
68
+ /**
69
+ * Per-tick context the dispatcher needs alongside canonical state. Held separately
70
+ * because `keybindings` / `inputBuffer` must never reach view setProps consumers.
71
+ */
72
+ export interface QuestionnaireRuntime {
73
+ document?: boolean;
74
+ vertical?: boolean;
75
+ keybindings: { matches(data: string, name: string): boolean };
76
+ inputBuffer: string;
77
+ canMoveInputUp: boolean;
78
+ canMoveInputDown: boolean;
79
+ questions: readonly QuestionData[];
80
+ isMulti: boolean;
81
+ currentItem: WrappingSelectItem | undefined;
82
+ items: readonly WrappingSelectItem[];
83
+ viewport: QuestionnaireViewport;
84
+ /**
85
+ * Key spec for the collapse/expand shortcut, e.g. `"ctrl+]"` or `"alt+o"`. Resolved
86
+ * from `AskUserQuestionConfig.collapseKey` (or the package default). When `"off"`,
87
+ * the collapse shortcut is disabled.
88
+ */
89
+ collapseKey: string;
90
+ }