@astralyn/pi 0.82.6 → 0.82.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.
Files changed (183) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +1 -1
  3. package/dist/cli/session-picker.d.ts.map +1 -1
  4. package/dist/cli/session-picker.js +11 -13
  5. package/dist/cli/session-picker.js.map +1 -1
  6. package/dist/core/extensions/types.d.ts +4 -0
  7. package/dist/core/extensions/types.d.ts.map +1 -1
  8. package/dist/core/extensions/types.js.map +1 -1
  9. package/dist/core/session-manager.d.ts.map +1 -1
  10. package/dist/core/session-manager.js +2 -2
  11. package/dist/core/session-manager.js.map +1 -1
  12. package/dist/core/tools/bash.d.ts +1 -5
  13. package/dist/core/tools/bash.d.ts.map +1 -1
  14. package/dist/core/tools/bash.js +16 -41
  15. package/dist/core/tools/bash.js.map +1 -1
  16. package/dist/core/tools/edit.d.ts.map +1 -1
  17. package/dist/core/tools/edit.js +42 -11
  18. package/dist/core/tools/edit.js.map +1 -1
  19. package/dist/core/tools/find.d.ts.map +1 -1
  20. package/dist/core/tools/find.js +6 -7
  21. package/dist/core/tools/find.js.map +1 -1
  22. package/dist/core/tools/grep.d.ts.map +1 -1
  23. package/dist/core/tools/grep.js +17 -8
  24. package/dist/core/tools/grep.js.map +1 -1
  25. package/dist/core/tools/ls.d.ts.map +1 -1
  26. package/dist/core/tools/ls.js +3 -3
  27. package/dist/core/tools/ls.js.map +1 -1
  28. package/dist/core/tools/read.d.ts.map +1 -1
  29. package/dist/core/tools/read.js +4 -4
  30. package/dist/core/tools/read.js.map +1 -1
  31. package/dist/core/tools/render-utils.d.ts +7 -0
  32. package/dist/core/tools/render-utils.d.ts.map +1 -1
  33. package/dist/core/tools/render-utils.js +11 -0
  34. package/dist/core/tools/render-utils.js.map +1 -1
  35. package/dist/core/tools/write.d.ts.map +1 -1
  36. package/dist/core/tools/write.js +2 -3
  37. package/dist/core/tools/write.js.map +1 -1
  38. package/dist/extensions/deepwiki/render.d.ts.map +1 -1
  39. package/dist/extensions/deepwiki/render.js +34 -22
  40. package/dist/extensions/deepwiki/render.js.map +1 -1
  41. package/dist/extensions/plan/constants.d.ts +32 -4
  42. package/dist/extensions/plan/constants.d.ts.map +1 -1
  43. package/dist/extensions/plan/constants.js +37 -6
  44. package/dist/extensions/plan/constants.js.map +1 -1
  45. package/dist/extensions/plan/index.d.ts +27 -0
  46. package/dist/extensions/plan/index.d.ts.map +1 -1
  47. package/dist/extensions/plan/index.js +150 -44
  48. package/dist/extensions/plan/index.js.map +1 -1
  49. package/dist/extensions/plan/schema.d.ts +5 -0
  50. package/dist/extensions/plan/schema.d.ts.map +1 -1
  51. package/dist/extensions/plan/schema.js.map +1 -1
  52. package/dist/extensions/plan/state.d.ts +11 -2
  53. package/dist/extensions/plan/state.d.ts.map +1 -1
  54. package/dist/extensions/plan/state.js +31 -9
  55. package/dist/extensions/plan/state.js.map +1 -1
  56. package/dist/extensions/plan/storage.d.ts +11 -3
  57. package/dist/extensions/plan/storage.d.ts.map +1 -1
  58. package/dist/extensions/plan/storage.js +45 -20
  59. package/dist/extensions/plan/storage.js.map +1 -1
  60. package/dist/extensions/plan/view.d.ts +15 -7
  61. package/dist/extensions/plan/view.d.ts.map +1 -1
  62. package/dist/extensions/plan/view.js +91 -16
  63. package/dist/extensions/plan/view.js.map +1 -1
  64. package/dist/extensions/question/dialog.d.ts.map +1 -1
  65. package/dist/extensions/question/dialog.js +29 -10
  66. package/dist/extensions/question/dialog.js.map +1 -1
  67. package/dist/extensions/question/index.d.ts.map +1 -1
  68. package/dist/extensions/question/index.js +7 -2
  69. package/dist/extensions/question/index.js.map +1 -1
  70. package/dist/extensions/question/render.d.ts +4 -8
  71. package/dist/extensions/question/render.d.ts.map +1 -1
  72. package/dist/extensions/question/render.js +202 -29
  73. package/dist/extensions/question/render.js.map +1 -1
  74. package/dist/extensions/question/results.d.ts.map +1 -1
  75. package/dist/extensions/question/results.js +1 -0
  76. package/dist/extensions/question/results.js.map +1 -1
  77. package/dist/extensions/question/schema.d.ts.map +1 -1
  78. package/dist/extensions/question/schema.js +2 -2
  79. package/dist/extensions/question/schema.js.map +1 -1
  80. package/dist/extensions/question/types.d.ts +1 -0
  81. package/dist/extensions/question/types.d.ts.map +1 -1
  82. package/dist/extensions/question/types.js +1 -1
  83. package/dist/extensions/question/types.js.map +1 -1
  84. package/dist/extensions/router/stream.d.ts +1 -0
  85. package/dist/extensions/router/stream.d.ts.map +1 -1
  86. package/dist/extensions/router/stream.js +44 -13
  87. package/dist/extensions/router/stream.js.map +1 -1
  88. package/dist/extensions/subagent/constants.d.ts +1 -0
  89. package/dist/extensions/subagent/constants.d.ts.map +1 -1
  90. package/dist/extensions/subagent/constants.js +1 -0
  91. package/dist/extensions/subagent/constants.js.map +1 -1
  92. package/dist/extensions/subagent/index.d.ts.map +1 -1
  93. package/dist/extensions/subagent/index.js +2 -0
  94. package/dist/extensions/subagent/index.js.map +1 -1
  95. package/dist/extensions/subagent/render.d.ts.map +1 -1
  96. package/dist/extensions/subagent/render.js +121 -48
  97. package/dist/extensions/subagent/render.js.map +1 -1
  98. package/dist/extensions/subagent/retry.d.ts +17 -0
  99. package/dist/extensions/subagent/retry.d.ts.map +1 -0
  100. package/dist/extensions/subagent/retry.js +41 -0
  101. package/dist/extensions/subagent/retry.js.map +1 -0
  102. package/dist/extensions/subagent/runner.d.ts.map +1 -1
  103. package/dist/extensions/subagent/runner.js +33 -5
  104. package/dist/extensions/subagent/runner.js.map +1 -1
  105. package/dist/extensions/subagent/sdk-runner.d.ts +6 -0
  106. package/dist/extensions/subagent/sdk-runner.d.ts.map +1 -1
  107. package/dist/extensions/subagent/sdk-runner.js +46 -24
  108. package/dist/extensions/subagent/sdk-runner.js.map +1 -1
  109. package/dist/extensions/subagent/types.d.ts +7 -0
  110. package/dist/extensions/subagent/types.d.ts.map +1 -1
  111. package/dist/extensions/subagent/types.js.map +1 -1
  112. package/dist/extensions/todo/index.d.ts.map +1 -1
  113. package/dist/extensions/todo/index.js +1 -1
  114. package/dist/extensions/todo/index.js.map +1 -1
  115. package/dist/extensions/todo/state.d.ts.map +1 -1
  116. package/dist/extensions/todo/state.js +10 -2
  117. package/dist/extensions/todo/state.js.map +1 -1
  118. package/dist/extensions/todo/view.d.ts +1 -1
  119. package/dist/extensions/todo/view.d.ts.map +1 -1
  120. package/dist/extensions/todo/view.js +179 -34
  121. package/dist/extensions/todo/view.js.map +1 -1
  122. package/dist/migrations.d.ts.map +1 -1
  123. package/dist/migrations.js +2 -1
  124. package/dist/migrations.js.map +1 -1
  125. package/dist/modes/interactive/components/armin.d.ts.map +1 -1
  126. package/dist/modes/interactive/components/armin.js +5 -0
  127. package/dist/modes/interactive/components/armin.js.map +1 -1
  128. package/dist/modes/interactive/components/bash-execution.d.ts +1 -0
  129. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  130. package/dist/modes/interactive/components/bash-execution.js +4 -2
  131. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  132. package/dist/modes/interactive/components/countdown-timer.d.ts +13 -6
  133. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -1
  134. package/dist/modes/interactive/components/countdown-timer.js +54 -17
  135. package/dist/modes/interactive/components/countdown-timer.js.map +1 -1
  136. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
  137. package/dist/modes/interactive/components/extension-selector.js +10 -6
  138. package/dist/modes/interactive/components/extension-selector.js.map +1 -1
  139. package/dist/modes/interactive/components/keybinding-hints.d.ts +11 -1
  140. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
  141. package/dist/modes/interactive/components/keybinding-hints.js +56 -0
  142. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
  143. package/dist/modes/interactive/components/session-selector.d.ts +3 -0
  144. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  145. package/dist/modes/interactive/components/session-selector.js +52 -2
  146. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  147. package/dist/modes/interactive/components/tool-execution.d.ts +10 -0
  148. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  149. package/dist/modes/interactive/components/tool-execution.js +133 -18
  150. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  151. package/dist/modes/interactive/components/tool-group.d.ts +1 -0
  152. package/dist/modes/interactive/components/tool-group.d.ts.map +1 -1
  153. package/dist/modes/interactive/components/tool-group.js +5 -0
  154. package/dist/modes/interactive/components/tool-group.js.map +1 -1
  155. package/dist/modes/interactive/components/tree-selector.d.ts +10 -1
  156. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
  157. package/dist/modes/interactive/components/tree-selector.js +55 -2
  158. package/dist/modes/interactive/components/tree-selector.js.map +1 -1
  159. package/dist/modes/interactive/interactive-mode.d.ts +5 -0
  160. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  161. package/dist/modes/interactive/interactive-mode.js +72 -18
  162. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  163. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  164. package/dist/modes/interactive/theme/theme.js +3 -2
  165. package/dist/modes/interactive/theme/theme.js.map +1 -1
  166. package/dist/utils/paths.d.ts +6 -0
  167. package/dist/utils/paths.d.ts.map +1 -1
  168. package/dist/utils/paths.js +8 -0
  169. package/dist/utils/paths.js.map +1 -1
  170. package/docs/bundled/README.md +1 -0
  171. package/docs/bundled/extensions/deepwiki.md +1 -1
  172. package/docs/bundled/extensions/plan.md +10 -9
  173. package/docs/bundled/extensions/question.md +7 -5
  174. package/docs/bundled/extensions/router.md +3 -4
  175. package/docs/bundled/extensions/statusline.md +0 -1
  176. package/docs/bundled/extensions/subagent.md +4 -3
  177. package/docs/bundled/extensions/todo.md +1 -1
  178. package/docs/bundled/tool-presentation.md +17 -16
  179. package/docs/extensions.md +20 -5
  180. package/docs/rpc.md +1 -1
  181. package/docs/usage.md +1 -1
  182. package/npm-shrinkwrap.json +6 -6
  183. package/package.json +1 -1
@@ -1,3 +1,17 @@
1
+ /**
2
+ * plan/view.ts — approval-dialog context and exit_plan tool rendering.
3
+ *
4
+ * `exit_plan` carries the whole plan as a parameter, and the default renderer
5
+ * echoes parameters verbatim: the row would repeat, escaped onto one line, the
6
+ * markdown the model just printed as response text. Collapsed rows therefore
7
+ * carry the title only, and the plan body is available on expand.
8
+ *
9
+ * The result text is written for the model (it restates tool-state precedence
10
+ * rules), so the result row renders from `details` instead.
11
+ */
12
+ import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
13
+ import { keyText } from "../../modes/interactive/components/keybinding-hints.js";
14
+ import { getMarkdownTheme } from "../../modes/interactive/theme/theme.js";
1
15
  /** Collapse the home prefix so a plan path stays on one line. */
2
16
  export function shortenPlanPath(path) {
3
17
  const rawHome = process.env.USERPROFILE || process.env.HOME || "";
@@ -13,35 +27,86 @@ export function shortenPlanPath(path) {
13
27
  * Context pressure is the fact the compact-or-not decision turns on, so the
14
28
  * dialog states it instead of making the user leave and check the footer.
15
29
  */
30
+ function formatTokenCount(tokenCount) {
31
+ if (tokenCount < 1000)
32
+ return `${tokenCount}`;
33
+ if (tokenCount < 10_000)
34
+ return `${(tokenCount / 1000).toFixed(1)}k`;
35
+ if (tokenCount < 1_000_000)
36
+ return `${Math.round(tokenCount / 1000)}k`;
37
+ if (tokenCount < 10_000_000)
38
+ return `${(tokenCount / 1_000_000).toFixed(1)}M`;
39
+ return `${Math.round(tokenCount / 1_000_000)}M`;
40
+ }
16
41
  export function formatApprovalSubtitle(ctx) {
17
- const percent = ctx.getContextUsage()?.percent;
18
- if (percent == null)
42
+ const usage = ctx.getContextUsage();
43
+ if (usage?.percent == null)
19
44
  return undefined;
20
- return `Context now ${percent.toFixed(0)}% full`;
45
+ const window = usage.contextWindow > 0 ? ` of ${formatTokenCount(usage.contextWindow)}` : "";
46
+ return `Context ${usage.percent.toFixed(1)}%${window}`;
47
+ }
48
+ // ---- kickoff message rendering ----------------------------------------------
49
+ /**
50
+ * The kickoff message body repeats the whole plan for the LLM, so the default
51
+ * renderer would flood the chat with markdown the user already approved.
52
+ * Collapsed: a two-line card (title + saved path with the expand hint).
53
+ * Expanded: the complete kickoff markdown, styled like the exit_plan body.
54
+ */
55
+ export function renderPlanKickoffMessage(message, options, theme) {
56
+ const details = (message.details ?? {});
57
+ const title = typeof details.title === "string" && details.title.trim() ? details.title.trim() : "plan";
58
+ const planPath = typeof details.planPath === "string" ? details.planPath.trim() : "";
59
+ const container = new Container();
60
+ container.addChild(new Text([theme.fg("toolTitle", theme.bold("plan")), theme.fg("text", `Executing "${title}"`)].join(" "), options.outputPad, 0));
61
+ const pathText = planPath ? `${shortenPlanPath(planPath)} ` : "";
62
+ const secondLine = options.expanded
63
+ ? planPath
64
+ ? theme.fg("dim", shortenPlanPath(planPath))
65
+ : undefined
66
+ : theme.fg("dim", `${pathText}(${keyText("app.tools.expand")} to expand)`);
67
+ if (secondLine)
68
+ container.addChild(new Text(secondLine, options.outputPad, 0));
69
+ if (!options.expanded)
70
+ return container;
71
+ const body = typeof message.content === "string"
72
+ ? message.content
73
+ : message.content
74
+ .filter((block) => block.type === "text")
75
+ .map((block) => block.text)
76
+ .join("\n");
77
+ if (body.trim()) {
78
+ container.addChild(new Spacer(1));
79
+ container.addChild(new Markdown(body, options.outputPad, 0, getMarkdownTheme(), {
80
+ color: (text) => theme.fg("toolOutput", text),
81
+ }));
82
+ }
83
+ return container;
21
84
  }
22
85
  function callText(value) {
23
86
  return typeof value === "string" ? value.trim() : "";
24
87
  }
25
- /** Title-only headline when collapsed; plan body and revision target on expand. */
26
- export function formatExitPlanCall(args, theme, expanded) {
88
+ /** Title-only headline when collapsed; Markdown plan body and revision target on expand. */
89
+ export function renderExitPlanCall(args, theme, expanded) {
27
90
  const title = callText(args?.title);
28
91
  const headline = [
29
92
  theme.fg("toolTitle", theme.bold("exit_plan")),
30
93
  title ? theme.fg("text", title) : theme.fg("dim", "…"),
31
94
  ].join(" ");
32
95
  if (!expanded)
33
- return headline;
34
- const lines = [headline];
96
+ return new Text(headline, 0, 0);
97
+ const container = new Container();
98
+ container.addChild(new Text(headline, 0, 0));
35
99
  const revises = callText(args?.revises);
36
100
  if (revises)
37
- lines.push(theme.fg("dim", `revises ${revises}`));
101
+ container.addChild(new Text(theme.fg("dim", `revises ${revises}`), 0, 0));
38
102
  const plan = callText(args?.plan);
39
103
  if (plan) {
40
- lines.push("");
41
- for (const line of plan.split("\n"))
42
- lines.push(theme.fg("dim", line));
104
+ container.addChild(new Spacer(1));
105
+ container.addChild(new Markdown(plan, 0, 0, getMarkdownTheme(), {
106
+ color: (text) => theme.fg("toolOutput", text),
107
+ }));
43
108
  }
44
- return lines.join("\n");
109
+ return container;
45
110
  }
46
111
  // ---- tool result rendering --------------------------------------------------
47
112
  const DECISION_SUMMARY = {
@@ -50,6 +115,9 @@ const DECISION_SUMMARY = {
50
115
  keepPlanning: "Still planning",
51
116
  cancelled: "No approval was pending",
52
117
  };
118
+ function isExitPlanDecision(value) {
119
+ return value === "execute" || value === "compactAndExecute" || value === "keepPlanning" || value === "cancelled";
120
+ }
53
121
  function resultText(result) {
54
122
  return result.content
55
123
  .filter((block) => block.type === "text")
@@ -63,12 +131,19 @@ function resultText(result) {
63
131
  */
64
132
  export function formatExitPlanResult(result, theme) {
65
133
  const details = result.details;
66
- if (!details)
134
+ if (!details || typeof details !== "object" || Array.isArray(details)) {
67
135
  return theme.fg("dim", resultText(result));
136
+ }
137
+ const decision = details.decision;
138
+ if (!isExitPlanDecision(decision))
139
+ return theme.fg("dim", resultText(result));
140
+ const rawPlanPath = details.planPath;
68
141
  const lines = [];
69
- if (details.planPath)
70
- lines.push(theme.fg("success", `Saved to ${shortenPlanPath(details.planPath)}`));
71
- lines.push(theme.fg("dim", DECISION_SUMMARY[details.decision]));
142
+ if (typeof rawPlanPath === "string" && rawPlanPath) {
143
+ lines.push(theme.fg("dim", `Saved to ${shortenPlanPath(rawPlanPath)}`));
144
+ }
145
+ const decisionColor = decision === "cancelled" ? "warning" : "text";
146
+ lines.push(theme.fg(decisionColor, DECISION_SUMMARY[decision]));
72
147
  return lines.join("\n");
73
148
  }
74
149
  //# sourceMappingURL=view.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/extensions/plan/view.ts"],"names":[],"mappings":"AAeA,iEAAiE;AACjE,MAAM,UAAU,eAAe,CAAC,IAAY,EAAU;IACrD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC;IAClE,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAC5C,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACvB,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;IAC7E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IACpE,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;AAAA,CACzD;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,GAAqB,EAAsB;IACjF,MAAM,OAAO,GAAG,GAAG,CAAC,eAAe,EAAE,EAAE,OAAO,CAAC;IAC/C,IAAI,OAAO,IAAI,IAAI;QAAE,OAAO,SAAS,CAAC;IACtC,OAAO,eAAe,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC;AAAA,CACjD;AAOD,SAAS,QAAQ,CAAC,KAAc,EAAU;IACzC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,E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;IACtC,iBAAiB,EAAE,oCAAoC;IACvD,YAAY,EAAE,gBAAgB;IAC9B,SAAS,EAAE,yBAAyB;CACpC,CAAC;AAEF,SAAS,UAAU,CAAC,MAAwC,EAAU;IACrE,OAAO,MAAM,CAAC,OAAO;SACnB,MAAM,CAAC,CAAC,KAAK,EAA2C,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC;SACjF,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC;SAC1B,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACb;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAwC,EAAE,KAAY,EAAU;IACpG,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAC/B,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAEzD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,OAAO,CAAC,QAAQ;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,YAAY,eAAe,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;IACvG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAChE,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACxB","sourcesContent":["/**\n * plan/view.ts — approval-dialog context and exit_plan tool rendering.\n *\n * `exit_plan` carries the whole plan as a parameter, and the default renderer\n * echoes parameters verbatim: the row would repeat, escaped onto one line, the\n * markdown the model just printed as response text. Collapsed rows therefore\n * carry the title only, and the plan body is available on expand.\n *\n * The result text is written for the model (it restates tool-state precedence\n * rules), so the result row renders from `details` instead.\n */\nimport type { AgentToolResult, ExtensionContext } from \"../../core/extensions/types.ts\";\nimport type { Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport type { ExitPlanDecision, ExitPlanDetails } from \"./schema.ts\";\n\n/** Collapse the home prefix so a plan path stays on one line. */\nexport function shortenPlanPath(path: string): string {\n\tconst rawHome = process.env.USERPROFILE || process.env.HOME || \"\";\n\tconst home = rawHome.replace(/[\\\\/]+$/, \"\");\n\tif (!home) return path;\n\tconst normalize = (value: string) => value.replace(/\\\\/g, \"/\").toLowerCase();\n\tif (!normalize(path).startsWith(`${normalize(home)}/`)) return path;\n\treturn `~${path.slice(home.length).replace(/\\\\/g, \"/\")}`;\n}\n\n/**\n * Context pressure is the fact the compact-or-not decision turns on, so the\n * dialog states it instead of making the user leave and check the footer.\n */\nexport function formatApprovalSubtitle(ctx: ExtensionContext): string | undefined {\n\tconst percent = ctx.getContextUsage()?.percent;\n\tif (percent == null) return undefined;\n\treturn `Context now ${percent.toFixed(0)}% full`;\n}\n\n// ---- tool call rendering ----------------------------------------------------\n// Args arrive off the wire and stream in, so every field is read defensively.\n\ntype ExitPlanCallArgs = { title?: unknown; plan?: unknown; revises?: unknown };\n\nfunction callText(value: unknown): string {\n\treturn typeof value === \"string\" ? value.trim() : \"\";\n}\n\n/** Title-only headline when collapsed; plan body and revision target on expand. */\nexport function formatExitPlanCall(args: ExitPlanCallArgs | undefined, theme: Theme, expanded: boolean): string {\n\tconst title = callText(args?.title);\n\tconst headline = [\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"exit_plan\")),\n\t\ttitle ? theme.fg(\"text\", title) : theme.fg(\"dim\", \"…\"),\n\t].join(\" \");\n\tif (!expanded) return headline;\n\n\tconst lines = [headline];\n\tconst revises = callText(args?.revises);\n\tif (revises) lines.push(theme.fg(\"dim\", `revises ${revises}`));\n\tconst plan = callText(args?.plan);\n\tif (plan) {\n\t\tlines.push(\"\");\n\t\tfor (const line of plan.split(\"\\n\")) lines.push(theme.fg(\"dim\", line));\n\t}\n\treturn lines.join(\"\\n\");\n}\n\n// ---- tool result rendering --------------------------------------------------\n\nconst DECISION_SUMMARY: Record<ExitPlanDecision, string> = {\n\texecute: \"Executing with full context\",\n\tcompactAndExecute: \"Compacting context, then executing\",\n\tkeepPlanning: \"Still planning\",\n\tcancelled: \"No approval was pending\",\n};\n\nfunction resultText(result: AgentToolResult<ExitPlanDetails>): string {\n\treturn result.content\n\t\t.filter((block): block is { type: \"text\"; text: string } => block.type === \"text\")\n\t\t.map((block) => block.text)\n\t\t.join(\"\\n\");\n}\n\n/**\n * What the user needs to know: where the plan landed and what happens next.\n * Falls back to the model-facing text when details are missing — a thrown\n * approval (interrupt) has no details to render from.\n */\nexport function formatExitPlanResult(result: AgentToolResult<ExitPlanDetails>, theme: Theme): string {\n\tconst details = result.details;\n\tif (!details) return theme.fg(\"dim\", resultText(result));\n\n\tconst lines: string[] = [];\n\tif (details.planPath) lines.push(theme.fg(\"success\", `Saved to ${shortenPlanPath(details.planPath)}`));\n\tlines.push(theme.fg(\"dim\", DECISION_SUMMARY[details.decision]));\n\treturn lines.join(\"\\n\");\n}\n"]}
1
+ {"version":3,"file":"view.js","sourceRoot":"","sources":["../../../src/extensions/plan/view.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAkB,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAG3F,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,gBAAgB,EAAc,MAAM,wCAAwC,CAAC;AAGtF,iEAAiE;AACjE,MAAM,UAAU,eAAe,CAAC,IAAY,EAAU;IACrD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC;IAClE,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAC5C,IAAI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OAAO,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9E,MAAM,WAAW,GAAI,OAAmC,CAAC,QAAQ,CAAC;IAElE,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,EAAE,CAAC;QACpD,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACzE,CAAC;IACD,MAAM,aAAa,GAAG,QAAQ,KAAK,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC;IACpE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,aAAa,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAChE,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAAA,CACxB","sourcesContent":["/**\n * plan/view.ts — approval-dialog context and exit_plan tool rendering.\n *\n * `exit_plan` carries the whole plan as a parameter, and the default renderer\n * echoes parameters verbatim: the row would repeat, escaped onto one line, the\n * markdown the model just printed as response text. Collapsed rows therefore\n * carry the title only, and the plan body is available on expand.\n *\n * The result text is written for the model (it restates tool-state precedence\n * rules), so the result row renders from `details` instead.\n */\nimport { type Component, Container, Markdown, Spacer, Text } from \"@earendil-works/pi-tui\";\nimport type { AgentToolResult, ExtensionContext } from \"../../core/extensions/types.ts\";\nimport type { CustomMessage } from \"../../core/messages.ts\";\nimport { keyText } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { getMarkdownTheme, type Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport type { ExitPlanDecision, ExitPlanDetails, PlanKickoffDetails } from \"./schema.ts\";\n\n/** Collapse the home prefix so a plan path stays on one line. */\nexport function shortenPlanPath(path: string): string {\n\tconst rawHome = process.env.USERPROFILE || process.env.HOME || \"\";\n\tconst home = rawHome.replace(/[\\\\/]+$/, \"\");\n\tif (!home) return path;\n\tconst normalize = (value: string) => value.replace(/\\\\/g, \"/\").toLowerCase();\n\tif (!normalize(path).startsWith(`${normalize(home)}/`)) return path;\n\treturn `~${path.slice(home.length).replace(/\\\\/g, \"/\")}`;\n}\n\n/**\n * Context pressure is the fact the compact-or-not decision turns on, so the\n * dialog states it instead of making the user leave and check the footer.\n */\nfunction formatTokenCount(tokenCount: number): string {\n\tif (tokenCount < 1000) return `${tokenCount}`;\n\tif (tokenCount < 10_000) return `${(tokenCount / 1000).toFixed(1)}k`;\n\tif (tokenCount < 1_000_000) return `${Math.round(tokenCount / 1000)}k`;\n\tif (tokenCount < 10_000_000) return `${(tokenCount / 1_000_000).toFixed(1)}M`;\n\treturn `${Math.round(tokenCount / 1_000_000)}M`;\n}\n\nexport function formatApprovalSubtitle(ctx: ExtensionContext): string | undefined {\n\tconst usage = ctx.getContextUsage();\n\tif (usage?.percent == null) return undefined;\n\tconst window = usage.contextWindow > 0 ? ` of ${formatTokenCount(usage.contextWindow)}` : \"\";\n\treturn `Context ${usage.percent.toFixed(1)}%${window}`;\n}\n\n// ---- kickoff message rendering ----------------------------------------------\n\n/**\n * The kickoff message body repeats the whole plan for the LLM, so the default\n * renderer would flood the chat with markdown the user already approved.\n * Collapsed: a two-line card (title + saved path with the expand hint).\n * Expanded: the complete kickoff markdown, styled like the exit_plan body.\n */\nexport function renderPlanKickoffMessage(\n\tmessage: CustomMessage<PlanKickoffDetails>,\n\toptions: { expanded: boolean; outputPad: number },\n\ttheme: Theme,\n): Component {\n\tconst details = (message.details ?? {}) as Partial<PlanKickoffDetails>;\n\tconst title = typeof details.title === \"string\" && details.title.trim() ? details.title.trim() : \"plan\";\n\tconst planPath = typeof details.planPath === \"string\" ? details.planPath.trim() : \"\";\n\n\tconst container = new Container();\n\tcontainer.addChild(\n\t\tnew Text(\n\t\t\t[theme.fg(\"toolTitle\", theme.bold(\"plan\")), theme.fg(\"text\", `Executing \"${title}\"`)].join(\" \"),\n\t\t\toptions.outputPad,\n\t\t\t0,\n\t\t),\n\t);\n\tconst pathText = planPath ? `${shortenPlanPath(planPath)} ` : \"\";\n\tconst secondLine = options.expanded\n\t\t? planPath\n\t\t\t? theme.fg(\"dim\", shortenPlanPath(planPath))\n\t\t\t: undefined\n\t\t: theme.fg(\"dim\", `${pathText}(${keyText(\"app.tools.expand\")} to expand)`);\n\tif (secondLine) container.addChild(new Text(secondLine, options.outputPad, 0));\n\tif (!options.expanded) return container;\n\n\tconst body =\n\t\ttypeof message.content === \"string\"\n\t\t\t? message.content\n\t\t\t: message.content\n\t\t\t\t\t.filter((block): block is { type: \"text\"; text: string } => block.type === \"text\")\n\t\t\t\t\t.map((block) => block.text)\n\t\t\t\t\t.join(\"\\n\");\n\tif (body.trim()) {\n\t\tcontainer.addChild(new Spacer(1));\n\t\tcontainer.addChild(\n\t\t\tnew Markdown(body, options.outputPad, 0, getMarkdownTheme(), {\n\t\t\t\tcolor: (text) => theme.fg(\"toolOutput\", text),\n\t\t\t}),\n\t\t);\n\t}\n\treturn container;\n}\n\n// ---- tool call rendering ----------------------------------------------------\n// Args arrive off the wire and stream in, so every field is read defensively.\n\ntype ExitPlanCallArgs = { title?: unknown; plan?: unknown; revises?: unknown };\n\nfunction callText(value: unknown): string {\n\treturn typeof value === \"string\" ? value.trim() : \"\";\n}\n\n/** Title-only headline when collapsed; Markdown plan body and revision target on expand. */\nexport function renderExitPlanCall(args: ExitPlanCallArgs | undefined, theme: Theme, expanded: boolean): Component {\n\tconst title = callText(args?.title);\n\tconst headline = [\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"exit_plan\")),\n\t\ttitle ? theme.fg(\"text\", title) : theme.fg(\"dim\", \"…\"),\n\t].join(\" \");\n\tif (!expanded) return new Text(headline, 0, 0);\n\n\tconst container = new Container();\n\tcontainer.addChild(new Text(headline, 0, 0));\n\tconst revises = callText(args?.revises);\n\tif (revises) container.addChild(new Text(theme.fg(\"dim\", `revises ${revises}`), 0, 0));\n\tconst plan = callText(args?.plan);\n\tif (plan) {\n\t\tcontainer.addChild(new Spacer(1));\n\t\tcontainer.addChild(\n\t\t\tnew Markdown(plan, 0, 0, getMarkdownTheme(), {\n\t\t\t\tcolor: (text) => theme.fg(\"toolOutput\", text),\n\t\t\t}),\n\t\t);\n\t}\n\treturn container;\n}\n\n// ---- tool result rendering --------------------------------------------------\n\nconst DECISION_SUMMARY: Record<ExitPlanDecision, string> = {\n\texecute: \"Executing with full context\",\n\tcompactAndExecute: \"Compacting context, then executing\",\n\tkeepPlanning: \"Still planning\",\n\tcancelled: \"No approval was pending\",\n};\n\nfunction isExitPlanDecision(value: unknown): value is ExitPlanDecision {\n\treturn value === \"execute\" || value === \"compactAndExecute\" || value === \"keepPlanning\" || value === \"cancelled\";\n}\n\nfunction resultText(result: AgentToolResult<ExitPlanDetails>): string {\n\treturn result.content\n\t\t.filter((block): block is { type: \"text\"; text: string } => block.type === \"text\")\n\t\t.map((block) => block.text)\n\t\t.join(\"\\n\");\n}\n\n/**\n * What the user needs to know: where the plan landed and what happens next.\n * Falls back to the model-facing text when details are missing — a thrown\n * approval (interrupt) has no details to render from.\n */\nexport function formatExitPlanResult(result: AgentToolResult<ExitPlanDetails>, theme: Theme): string {\n\tconst details = result.details as unknown;\n\tif (!details || typeof details !== \"object\" || Array.isArray(details)) {\n\t\treturn theme.fg(\"dim\", resultText(result));\n\t}\n\tconst decision = (details as Record<string, unknown>).decision;\n\tif (!isExitPlanDecision(decision)) return theme.fg(\"dim\", resultText(result));\n\tconst rawPlanPath = (details as Record<string, unknown>).planPath;\n\n\tconst lines: string[] = [];\n\tif (typeof rawPlanPath === \"string\" && rawPlanPath) {\n\t\tlines.push(theme.fg(\"dim\", `Saved to ${shortenPlanPath(rawPlanPath)}`));\n\t}\n\tconst decisionColor = decision === \"cancelled\" ? \"warning\" : \"text\";\n\tlines.push(theme.fg(decisionColor, DECISION_SUMMARY[decision]));\n\treturn lines.join(\"\\n\");\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../../src/extensions/question/dialog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,GAAG,EAAiC,MAAM,wBAAwB,CAAC;AACrH,OAAO,KAAK,EAAc,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAoB,KAAK,KAAK,EAAE,MAAM,wCAAwC,CAAC;AAYtF,OAAO,KAAK,EAAE,YAAY,EAA4B,QAAQ,EAAkC,MAAM,YAAY,CAAC;AAanH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW;;;;;;EAkoB/E","sourcesContent":["import { Editor, type EditorTheme, Markdown, type TUI, truncateToWidth, visibleWidth } from \"@earendil-works/pi-tui\";\nimport type { Keybinding, KeybindingsManager } from \"../../core/keybindings.ts\";\nimport { getMarkdownTheme, type Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport { ruleBorder, wrapWithPrefix } from \"./dialog-primitives.ts\";\nimport { QUESTION_LIMITS } from \"./limits.ts\";\nimport { PreviewLinesCache, WidthCachedRender } from \"./render-cache.ts\";\nimport {\n\tdisplayOptions,\n\tfirstUnanswered,\n\thasAnswer,\n\thasMultiAnswer,\n\tnewQuestionState,\n\torderedAnswers,\n} from \"./state.ts\";\nimport type { DialogResult, DisplayOption, InputMode, Question, QuestionAnswer, QuestionOption } from \"./types.ts\";\n\ntype DialogView = \"question\" | \"review\";\n\nconst PREVIEW_MAX_LINES = 16;\n/**\n * Estimated non-preview rows in the stacked (narrow) layout: top/bottom rules,\n * tab strip + blank, question line + blank, up to 5 option rows with\n * descriptions, the \"Chat about this\" row, warning slot, and the hint line.\n * Reserved so the preview clamps short of the terminal height.\n */\nconst PREVIEW_CHROME_ROWS = 18;\n\nexport function createQuestionDialog(questions: Question[], signal?: AbortSignal) {\n\treturn (tui: TUI, theme: Theme, keybindings: KeybindingsManager, done: (result: DialogResult) => void) => {\n\t\tlet currentIdx = 0;\n\t\tlet view: DialogView = \"question\";\n\t\tlet inputMode: InputMode;\n\t\tlet noteTarget: string | undefined;\n\t\tlet notesSnapshot: { prior: QuestionAnswer | undefined } | undefined;\n\t\tlet dialogFocused = false;\n\t\tlet footerFocused = false;\n\t\tlet finished = false;\n\t\tconst cache = new WidthCachedRender();\n\t\tconst previewCache = new PreviewLinesCache();\n\t\tconst states = questions.map(() => newQuestionState());\n\t\tconst optionsByQuestion = questions.map((question) => displayOptions(question));\n\n\t\tconst editorTheme: EditorTheme = {\n\t\t\tborderColor: (s) => theme.fg(\"accent\", s),\n\t\t\tselectList: {\n\t\t\t\tselectedPrefix: (t) => theme.fg(\"accent\", t),\n\t\t\t\tselectedText: (t) => theme.fg(\"accent\", t),\n\t\t\t\tdescription: (t) => theme.fg(\"muted\", t),\n\t\t\t\tscrollInfo: (t) => theme.fg(\"dim\", t),\n\t\t\t\tnoMatch: (t) => theme.fg(\"warning\", t),\n\t\t\t},\n\t\t};\n\t\tconst editor = new Editor(tui, editorTheme);\n\n\t\tconst currentQuestion = () => questions[currentIdx];\n\t\tconst currentState = () => states[currentIdx];\n\t\tconst currentOptions = () => optionsByQuestion[currentIdx];\n\t\tconst keyMatches = (data: string, keybinding: Keybinding) => keybindings.matches(data, keybinding);\n\t\tconst keyLabel = (keybinding: Keybinding, fallback: string) =>\n\t\t\tkeybindings.getKeys(keybinding).join(\"/\") || fallback;\n\n\t\tfunction finish(result: DialogResult): void {\n\t\t\tif (finished) return;\n\t\t\tfinished = true;\n\t\t\tdone(result);\n\t\t}\n\n\t\tconst onAbort = () => finish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\tif (signal?.aborted) onAbort();\n\t\telse signal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tfunction refresh(): void {\n\t\t\tcache.invalidate();\n\t\t\ttui.requestRender();\n\t\t}\n\n\t\tfunction syncEditorFocus(): void {\n\t\t\teditor.focused = dialogFocused && inputMode !== undefined;\n\t\t}\n\n\t\tfunction setCurrentIdx(index: number): void {\n\t\t\tcurrentIdx = Math.max(0, Math.min(questions.length - 1, index));\n\t\t\tview = \"question\";\n\t\t\tinputMode = undefined;\n\t\t\tnoteTarget = undefined;\n\t\t\tnotesSnapshot = undefined;\n\t\t\tfooterFocused = false;\n\t\t\teditor.setText(\"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction clearWarning(): void {\n\t\t\tcurrentState().warning = undefined;\n\t\t}\n\n\t\tfunction focusedOption(): DisplayOption | undefined {\n\t\t\treturn currentOptions()[currentState().optionIndex];\n\t\t}\n\n\t\tfunction showReview(): void {\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing !== undefined) {\n\t\t\t\tsetCurrentIdx(missing);\n\t\t\t\tcurrentState().warning = \"Answer this question before reviewing your answers.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tview = \"review\";\n\t\t\tinputMode = undefined;\n\t\t\tnoteTarget = undefined;\n\t\t\tfooterFocused = false;\n\t\t\teditor.setText(\"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction advanceAfterAnswer(): void {\n\t\t\tif (questions.length === 1 && !currentQuestion().multiSelect) {\n\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"answered\" });\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing === undefined) {\n\t\t\t\tshowReview();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsetCurrentIdx(missing);\n\t\t}\n\n\t\tfunction beginCustomInput(): void {\n\t\t\tconst state = currentState();\n\t\t\tinputMode = \"custom\";\n\t\t\tnoteTarget = undefined;\n\t\t\tconst existingSingle = state.singleAnswer?.kind === \"custom\" ? state.singleAnswer.answer : undefined;\n\t\t\teditor.setText(state.customAnswer?.text ?? existingSingle ?? \"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction selectSingleOption(option: QuestionOption): void {\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tstate.singleAnswer = {\n\t\t\t\tquestionIndex: currentIdx,\n\t\t\t\tquestion: question.question,\n\t\t\t\theader: question.header,\n\t\t\t\tkind: \"option\",\n\t\t\t\tanswer: option.label,\n\t\t\t\t...(option.preview ? { preview: option.preview } : {}),\n\t\t\t};\n\t\t\tstate.warning = undefined;\n\t\t}\n\n\t\tfunction beginNotesInput(): void {\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tconst option = focusedOption();\n\t\t\tif (!option || option.kind === \"other\") {\n\t\t\t\tbeginCustomInput();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (question.multiSelect && !state.multiSelected.has(option.optionIndex)) {\n\t\t\t\tstate.warning = \"Select the option first, then add notes.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!question.multiSelect) {\n\t\t\t\tnotesSnapshot = { prior: state.singleAnswer };\n\t\t\t\tselectSingleOption(option);\n\t\t\t}\n\t\t\tinputMode = \"notes\";\n\t\t\tnoteTarget = option.label;\n\t\t\teditor.setText(state.notesByOption.get(option.label) ?? \"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction recordSingle(option: DisplayOption): void {\n\t\t\tif (option.kind === \"other\") {\n\t\t\t\tif (currentState().singleAnswer?.kind === \"custom\") {\n\t\t\t\t\tadvanceAfterAnswer();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbeginCustomInput();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tselectSingleOption(option);\n\t\t\tadvanceAfterAnswer();\n\t\t}\n\n\t\tfunction recordMulti(): void {\n\t\t\tconst state = currentState();\n\t\t\tif (!hasMultiAnswer(state)) {\n\t\t\t\tstate.warning = \"Select at least one option, type a custom answer, or cancel the questions.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstate.warning = undefined;\n\t\t\tadvanceAfterAnswer();\n\t\t}\n\n\t\tfunction toggleMultiOption(option: DisplayOption): void {\n\t\t\tconst state = currentState();\n\t\t\tif (option.kind === \"other\") {\n\t\t\t\tif (state.customAnswer) {\n\t\t\t\t\tstate.customAnswer.selected = !state.customAnswer.selected;\n\t\t\t\t\tclearWarning();\n\t\t\t\t} else {\n\t\t\t\t\tstate.warning = `Press ${keyLabel(\"tui.input.tab\", \"Tab\")} to type a custom answer.`;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (state.multiSelected.has(option.optionIndex)) state.multiSelected.delete(option.optionIndex);\n\t\t\telse state.multiSelected.add(option.optionIndex);\n\t\t\tclearWarning();\n\t\t}\n\n\t\tfunction finishReview(): void {\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing !== undefined) {\n\t\t\t\tsetCurrentIdx(missing);\n\t\t\t\tcurrentState().warning = \"Answer this question before submitting.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"answered\" });\n\t\t}\n\n\t\teditor.onSubmit = (value) => {\n\t\t\tconst trimmed = value.trim();\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tif (trimmed.length > QUESTION_LIMITS.userTextChars) {\n\t\t\t\tstate.warning = `Keep notes and custom answers under ${QUESTION_LIMITS.userTextChars} characters.`;\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (inputMode === \"notes\") {\n\t\t\t\tif (noteTarget) {\n\t\t\t\t\tif (trimmed) state.notesByOption.set(noteTarget, trimmed);\n\t\t\t\t\telse state.notesByOption.delete(noteTarget);\n\t\t\t\t}\n\t\t\t\tinputMode = undefined;\n\t\t\t\tnoteTarget = undefined;\n\t\t\t\tnotesSnapshot = undefined;\n\t\t\t\teditor.setText(\"\");\n\t\t\t\tsyncEditorFocus();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (inputMode === \"custom\") {\n\t\t\t\tif (!trimmed) {\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (question.multiSelect) {\n\t\t\t\t\tstate.customAnswer = { text: trimmed, selected: true };\n\t\t\t\t\tstate.optionIndex = question.options.length;\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tstate.singleAnswer = {\n\t\t\t\t\tquestionIndex: currentIdx,\n\t\t\t\t\tquestion: question.question,\n\t\t\t\t\theader: question.header,\n\t\t\t\t\tkind: \"custom\",\n\t\t\t\t\tanswer: trimmed,\n\t\t\t\t};\n\t\t\t\tinputMode = undefined;\n\t\t\t\teditor.setText(\"\");\n\t\t\t\tsyncEditorFocus();\n\t\t\t\tadvanceAfterAnswer();\n\t\t\t}\n\t\t};\n\n\t\tfunction handleReviewInput(data: string): void {\n\t\t\tif (keyMatches(data, \"tui.editor.cursorLeft\")) {\n\t\t\t\tsetCurrentIdx(questions.length - 1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\tfinishReview();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\tsetCurrentIdx(questions.length - 1);\n\t\t\t}\n\t\t}\n\n\t\tfunction handleQuestionInput(data: string): void {\n\t\t\tif (inputMode) {\n\t\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\t\tif (inputMode === \"notes\" && notesSnapshot) {\n\t\t\t\t\t\tcurrentState().singleAnswer = notesSnapshot.prior;\n\t\t\t\t\t}\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\tnoteTarget = undefined;\n\t\t\t\t\tnotesSnapshot = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\teditor.handleInput(data);\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.editor.cursorLeft\")) {\n\t\t\t\tif (currentIdx > 0) setCurrentIdx(currentIdx - 1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.editor.cursorRight\")) {\n\t\t\t\tif (currentIdx < questions.length - 1) setCurrentIdx(currentIdx + 1);\n\t\t\t\telse if (firstUnanswered(states) === undefined) showReview();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst state = currentState();\n\t\t\tconst options = currentOptions();\n\t\t\tif (keyMatches(data, \"tui.select.up\")) {\n\t\t\t\tif (footerFocused) {\n\t\t\t\t\tfooterFocused = false;\n\t\t\t\t\tstate.optionIndex = options.length - 1;\n\t\t\t\t} else if (state.optionIndex > 0) {\n\t\t\t\t\tstate.optionIndex--;\n\t\t\t\t}\n\t\t\t\tclearWarning();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.down\")) {\n\t\t\t\tif (footerFocused) return;\n\t\t\t\tif (state.optionIndex < options.length - 1) state.optionIndex++;\n\t\t\t\telse footerFocused = true;\n\t\t\t\tclearWarning();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.input.tab\")) {\n\t\t\t\tif (!footerFocused) beginNotesInput();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (data.length === 1 && data >= \"1\" && data <= \"9\") {\n\t\t\t\tconst index = data.charCodeAt(0) - 49;\n\t\t\t\tconst target = options[index];\n\t\t\t\tif (target) {\n\t\t\t\t\tfooterFocused = false;\n\t\t\t\t\tstate.optionIndex = index;\n\t\t\t\t\tclearWarning();\n\t\t\t\t\tif (currentQuestion().multiSelect) {\n\t\t\t\t\t\ttoggleMultiOption(target);\n\t\t\t\t\t\trefresh();\n\t\t\t\t\t} else {\n\t\t\t\t\t\trecordSingle(target);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (footerFocused) {\n\t\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"needs_clarification\" });\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst question = currentQuestion();\n\t\t\tconst option = options[state.optionIndex];\n\t\t\tif (question.multiSelect && keyMatches(data, \"app.list.toggle\")) {\n\t\t\t\tif (option) toggleMultiOption(option);\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\tif (question.multiSelect) {\n\t\t\t\t\tif (option?.kind === \"other\" && !state.customAnswer) {\n\t\t\t\t\t\tbeginCustomInput();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\trecordMulti();\n\t\t\t\t} else if (option) recordSingle(option);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\t\t}\n\t\t}\n\n\t\tfunction handleInput(data: string): void {\n\t\t\tif (view === \"review\") handleReviewInput(data);\n\t\t\telse handleQuestionInput(data);\n\t\t}\n\n\t\tfunction render(width: number): string[] {\n\t\t\treturn cache.get(width, tui.terminal.rows, compute);\n\t\t}\n\n\t\tfunction renderTabs(renderWidth: number, lines: string[]): void {\n\t\t\tif (questions.length < 2) return;\n\t\t\tconst tabCount = questions.length + 1;\n\t\t\tconst headerWidth = Math.max(4, Math.floor((renderWidth - tabCount * 5) / tabCount));\n\t\t\tconst tabs = [\n\t\t\t\t...questions.map((question, index) => {\n\t\t\t\t\tconst answered = hasAnswer(states[index]);\n\t\t\t\t\tconst label = ` ${answered ? \"■\" : \"□\"} ${truncateToWidth(question.header, headerWidth)} `;\n\t\t\t\t\tif (view === \"question\" && index === currentIdx) {\n\t\t\t\t\t\treturn theme.bg(\"selectedBg\", theme.fg(answered ? \"success\" : \"text\", label));\n\t\t\t\t\t}\n\t\t\t\t\treturn theme.fg(answered ? \"success\" : \"muted\", label);\n\t\t\t\t}),\n\t\t\t\tview === \"review\"\n\t\t\t\t\t? theme.bg(\"selectedBg\", theme.fg(\"text\", \" ✓ Submit \"))\n\t\t\t\t\t: theme.fg(firstUnanswered(states) === undefined ? \"success\" : \"dim\", \" ✓ Submit \"),\n\t\t\t].join(\" \");\n\t\t\twrapWithPrefix(\" \", tabs, renderWidth, lines);\n\t\t\tlines.push(\"\");\n\t\t}\n\n\t\tfunction previewLines(previewText: string, width: number, terminalRows: number): string[] {\n\t\t\tconst maxLines = Math.max(4, Math.min(PREVIEW_MAX_LINES, terminalRows - PREVIEW_CHROME_ROWS));\n\t\t\treturn previewCache.get(previewText, width, maxLines, () => {\n\t\t\t\tconst markdown = new Markdown(previewText, 1, 0, getMarkdownTheme());\n\t\t\t\tconst rendered = markdown.render(Math.max(1, width));\n\t\t\t\tif (rendered.length <= maxLines) return rendered;\n\t\t\t\treturn [\n\t\t\t\t\t...rendered.slice(0, maxLines),\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\ttruncateToWidth(`… ${rendered.length - maxLines} preview lines hidden`, Math.max(1, width)),\n\t\t\t\t\t),\n\t\t\t\t];\n\t\t\t});\n\t\t}\n\n\t\t/** Multi-select checkbox marker; the other row shows no box until a custom answer exists. */\n\t\tfunction multiMarker(option: DisplayOption, checked: boolean, hasCustomAnswer: boolean): string {\n\t\t\tif (option.kind === \"other\" && !hasCustomAnswer) return \" \";\n\t\t\treturn checked ? theme.fg(\"success\", \"[x]\") : theme.fg(\"dim\", \"[ ]\");\n\t\t}\n\n\t\tfunction renderReview(renderWidth: number, lines: string[]): void {\n\t\t\twrapWithPrefix(\" \", theme.fg(\"accent\", \"Review answers\"), renderWidth, lines);\n\t\t\tlines.push(\"\");\n\t\t\tfor (const answer of orderedAnswers(questions, states)) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\"text\", `${answer.questionIndex + 1}. ${answer.question}`),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t\tconst response =\n\t\t\t\t\tanswer.kind === \"multi\"\n\t\t\t\t\t\t? (answer.selected?.join(\", \") ?? \"(no input)\")\n\t\t\t\t\t\t: (answer.answer ?? \"(no input)\");\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"success\", response), renderWidth, lines);\n\t\t\t\tfor (const note of answer.notes ?? []) {\n\t\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", `Note for ${note.option}: ${note.text}`), renderWidth, lines);\n\t\t\t\t}\n\t\t\t\tlines.push(\"\");\n\t\t\t}\n\t\t\twrapWithPrefix(\n\t\t\t\t\" \",\n\t\t\t\ttheme.fg(\n\t\t\t\t\t\"dim\",\n\t\t\t\t\t`${keyLabel(\"tui.select.confirm\", \"Enter\")} to submit • ${keyLabel(\"tui.select.cancel\", \"Esc\")} to edit the last question`,\n\t\t\t\t),\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t}\n\n\t\tfunction compute(width: number, terminalRows: number): string[] {\n\t\t\tconst lines: string[] = [];\n\t\t\tconst renderWidth = Math.max(1, width);\n\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\trenderTabs(renderWidth, lines);\n\n\t\t\tif (view === \"review\") {\n\t\t\t\trenderReview(renderWidth, lines);\n\t\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\t\treturn lines;\n\t\t\t}\n\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tconst options = currentOptions();\n\t\t\tconst isMulti = question.multiSelect === true;\n\t\t\twrapWithPrefix(\n\t\t\t\t\" \",\n\t\t\t\t`${theme.fg(\"accent\", question.header)} ${theme.fg(\"text\", question.question)}`,\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t\tlines.push(\"\");\n\n\t\t\tconst optionLines: string[] = [];\n\t\t\tconst hasPreview = !isMulti && options.some((option) => option.kind === \"option\" && option.preview);\n\t\t\tconst showPreviewSideBySide = hasPreview && renderWidth >= 60;\n\t\t\tconst listWidth = showPreviewSideBySide ? Math.max(20, Math.floor(renderWidth * 0.4)) : renderWidth;\n\n\t\t\tfor (let index = 0; index < options.length; index++) {\n\t\t\t\tconst option = options[index];\n\t\t\t\tconst focused = !footerFocused && index === state.optionIndex;\n\t\t\t\tconst checked =\n\t\t\t\t\tisMulti &&\n\t\t\t\t\t((option.kind === \"option\" && state.multiSelected.has(option.optionIndex)) ||\n\t\t\t\t\t\t(option.kind === \"other\" && state.customAnswer?.selected === true));\n\t\t\t\tconst selectedSingle =\n\t\t\t\t\t!isMulti &&\n\t\t\t\t\t(option.kind === \"other\"\n\t\t\t\t\t\t? state.singleAnswer?.kind === \"custom\"\n\t\t\t\t\t\t: state.singleAnswer?.answer === option.label);\n\t\t\t\tconst marker = isMulti ? multiMarker(option, checked, state.customAnswer !== undefined) : \"\";\n\t\t\t\tconst focusArrow = focused ? theme.fg(\"accent\", \"→\") : \" \";\n\t\t\t\tconst note =\n\t\t\t\t\toption.kind === \"option\" && state.notesByOption.has(option.label) ? theme.fg(\"success\", \" +note\") : \"\";\n\t\t\t\tconst customText =\n\t\t\t\t\toption.kind === \"other\"\n\t\t\t\t\t\t? (state.customAnswer?.text ??\n\t\t\t\t\t\t\t(state.singleAnswer?.kind === \"custom\" ? state.singleAnswer.answer : undefined))\n\t\t\t\t\t\t: undefined;\n\t\t\t\tconst labelText = customText ? `${option.label} ✎ ${customText}` : option.label;\n\t\t\t\tconst label = `${index + 1}. ${labelText}${selectedSingle ? \" ✓\" : \"\"}${note}`;\n\t\t\t\tconst color = focused ? \"accent\" : selectedSingle || checked ? \"success\" : \"text\";\n\t\t\t\twrapWithPrefix(`${focusArrow} ${marker} `, theme.fg(color, label), listWidth, optionLines);\n\t\t\t\tif (option.kind === \"option\" && option.description) {\n\t\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", option.description), listWidth, optionLines);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (inputMode) {\n\t\t\t\toptionLines.push(\"\");\n\t\t\t\tconst label = inputMode === \"notes\" ? `Notes for ${noteTarget ?? \"option\"}:` : \"Your answer:\";\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", label), listWidth, optionLines);\n\t\t\t\tfor (const line of editor.render(Math.max(1, listWidth - 2))) optionLines.push(` ${line}`);\n\t\t\t}\n\n\t\t\tif (showPreviewSideBySide) {\n\t\t\t\tconst gap = 2;\n\t\t\t\tconst rightWidth = renderWidth - listWidth - gap;\n\t\t\t\tconst focused = options[state.optionIndex];\n\t\t\t\tconst previewText = focused?.kind === \"option\" && focused.preview ? focused.preview : \"\";\n\t\t\t\tconst rightLines = previewText\n\t\t\t\t\t? previewLines(previewText, rightWidth, terminalRows)\n\t\t\t\t\t: [theme.fg(\"dim\", \"(no preview)\")];\n\t\t\t\tconst maxRows = Math.max(optionLines.length, rightLines.length);\n\t\t\t\tconst pad = \" \".repeat(gap);\n\t\t\t\tfor (let row = 0; row < maxRows; row++) {\n\t\t\t\t\tconst left = row < optionLines.length ? optionLines[row] : \"\";\n\t\t\t\t\tconst leftPadded = left + \" \".repeat(Math.max(0, listWidth - visibleWidth(left)));\n\t\t\t\t\tconst right = row < rightLines.length ? rightLines[row] : \"\";\n\t\t\t\t\tlines.push(`${leftPadded}${pad}${right}`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tlines.push(...optionLines);\n\t\t\t\tif (hasPreview) {\n\t\t\t\t\tconst focused = options[state.optionIndex];\n\t\t\t\t\tconst previewText = focused?.kind === \"option\" && focused.preview ? focused.preview : \"\";\n\t\t\t\t\tif (previewText) {\n\t\t\t\t\t\tlines.push(\"\");\n\t\t\t\t\t\tlines.push(ruleBorder(theme, renderWidth, \"dim\"));\n\t\t\t\t\t\tlines.push(...previewLines(previewText, renderWidth, terminalRows));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlines.push(\"\");\n\t\t\tconst chatPrefix = footerFocused ? theme.fg(\"accent\", \"→\") : \" \";\n\t\t\twrapWithPrefix(\n\t\t\t\t`${chatPrefix} `,\n\t\t\t\ttheme.fg(footerFocused ? \"accent\" : \"text\", \"Chat about this\"),\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t\tif (state.warning) {\n\t\t\t\tlines.push(\"\");\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"warning\", state.warning), renderWidth, lines);\n\t\t\t}\n\t\t\tlines.push(\"\");\n\t\t\tif (inputMode === \"notes\") {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\t`${keyLabel(\"tui.select.confirm\", \"Enter\")} to save notes • ${keyLabel(\"tui.select.cancel\", \"Esc\")} to go back`,\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (inputMode === \"custom\") {\n\t\t\t\tconst hint = isMulti ? \"to save custom answer\" : \"to continue\";\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\t`${keyLabel(\"tui.select.confirm\", \"Enter\")} ${hint} • ${keyLabel(\"tui.select.cancel\", \"Esc\")} to go back`,\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (footerFocused) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\t`${keyLabel(\"tui.select.confirm\", \"Enter\")} to discuss • ${keyLabel(\"tui.select.up\", \"Up\")} to return to options`,\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (isMulti) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\t`${keyLabel(\"app.list.toggle\", \"Space\")} to toggle • ${keyLabel(\"tui.input.tab\", \"Tab\")} for notes/custom • ${keyLabel(\"tui.select.confirm\", \"Enter\")} to continue • ${keyLabel(\"tui.editor.cursorLeft\", \"Left\")}/${keyLabel(\"tui.editor.cursorRight\", \"Right\")} questions • ${keyLabel(\"tui.select.cancel\", \"Esc\")} to cancel`,\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\t`${keyLabel(\"tui.input.tab\", \"Tab\")} for notes/custom • ${keyLabel(\"tui.select.confirm\", \"Enter\")} to select • ${keyLabel(\"tui.editor.cursorLeft\", \"Left\")}/${keyLabel(\"tui.editor.cursorRight\", \"Right\")} questions • ${keyLabel(\"tui.select.cancel\", \"Esc\")} to cancel`,\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t}\n\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\treturn lines;\n\t\t}\n\n\t\treturn {\n\t\t\tget focused() {\n\t\t\t\treturn dialogFocused;\n\t\t\t},\n\t\t\tset focused(value: boolean) {\n\t\t\t\tdialogFocused = value;\n\t\t\t\tsyncEditorFocus();\n\t\t\t},\n\t\t\trender,\n\t\t\tinvalidate: () => {\n\t\t\t\tcache.invalidate();\n\t\t\t},\n\t\t\thandleInput,\n\t\t\tdispose() {\n\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t},\n\t\t};\n\t};\n}\n"]}
1
+ {"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../../src/extensions/question/dialog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,GAAG,EAAiC,MAAM,wBAAwB,CAAC;AACrH,OAAO,KAAK,EAAc,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAEhF,OAAO,EAAoB,KAAK,KAAK,EAAE,MAAM,wCAAwC,CAAC;AAYtF,OAAO,KAAK,EAAE,YAAY,EAA4B,QAAQ,EAAkC,MAAM,YAAY,CAAC;AAanH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,QAAQ,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW;;;;;;EAiqB/E","sourcesContent":["import { Editor, type EditorTheme, Markdown, type TUI, truncateToWidth, visibleWidth } from \"@earendil-works/pi-tui\";\nimport type { Keybinding, KeybindingsManager } from \"../../core/keybindings.ts\";\nimport { keyLabel as configuredKeyLabel } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { getMarkdownTheme, type Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport { ruleBorder, wrapWithPrefix } from \"./dialog-primitives.ts\";\nimport { QUESTION_LIMITS } from \"./limits.ts\";\nimport { PreviewLinesCache, WidthCachedRender } from \"./render-cache.ts\";\nimport {\n\tdisplayOptions,\n\tfirstUnanswered,\n\thasAnswer,\n\thasMultiAnswer,\n\tnewQuestionState,\n\torderedAnswers,\n} from \"./state.ts\";\nimport type { DialogResult, DisplayOption, InputMode, Question, QuestionAnswer, QuestionOption } from \"./types.ts\";\n\ntype DialogView = \"question\" | \"review\";\n\nconst PREVIEW_MAX_LINES = 16;\n/**\n * Estimated non-preview rows in the stacked (narrow) layout: top/bottom rules,\n * tab strip + blank, question line + blank, up to 5 option rows with\n * descriptions, the \"Chat about this\" row, warning slot, and the hint line.\n * Reserved so the preview clamps short of the terminal height.\n */\nconst PREVIEW_CHROME_ROWS = 18;\n\nexport function createQuestionDialog(questions: Question[], signal?: AbortSignal) {\n\treturn (tui: TUI, theme: Theme, keybindings: KeybindingsManager, done: (result: DialogResult) => void) => {\n\t\tlet currentIdx = 0;\n\t\tlet view: DialogView = \"question\";\n\t\tlet inputMode: InputMode;\n\t\tlet noteTarget: string | undefined;\n\t\tlet notesSnapshot: { prior: QuestionAnswer | undefined } | undefined;\n\t\tlet dialogFocused = false;\n\t\tlet footerFocused = false;\n\t\tlet finished = false;\n\t\tconst cache = new WidthCachedRender();\n\t\tconst previewCache = new PreviewLinesCache();\n\t\tconst states = questions.map(() => newQuestionState());\n\t\tconst optionsByQuestion = questions.map((question) => displayOptions(question));\n\n\t\tconst editorTheme: EditorTheme = {\n\t\t\tborderColor: (s) => theme.fg(\"accent\", s),\n\t\t\tselectList: {\n\t\t\t\tselectedPrefix: (t) => theme.fg(\"accent\", t),\n\t\t\t\tselectedText: (t) => theme.fg(\"accent\", t),\n\t\t\t\tdescription: (t) => theme.fg(\"muted\", t),\n\t\t\t\tscrollInfo: (t) => theme.fg(\"dim\", t),\n\t\t\t\tnoMatch: (t) => theme.fg(\"warning\", t),\n\t\t\t},\n\t\t};\n\t\tconst editor = new Editor(tui, editorTheme);\n\n\t\tconst currentQuestion = () => questions[currentIdx];\n\t\tconst currentState = () => states[currentIdx];\n\t\tconst currentOptions = () => optionsByQuestion[currentIdx];\n\t\tconst keyMatches = (data: string, keybinding: Keybinding) => keybindings.matches(data, keybinding);\n\t\tconst keyLabel = (keybinding: Keybinding) => configuredKeyLabel(keybinding, { keybindings });\n\t\tconst keyAction = (keybinding: Keybinding, action: string) => {\n\t\t\tconst label = keyLabel(keybinding);\n\t\t\treturn label ? `${label} ${action}` : \"\";\n\t\t};\n\t\t// Editor reads the global TUI manager internally, while dialog controls use the injected manager.\n\t\tconst editorSubmitAction = (action: string) => {\n\t\t\tconst label = configuredKeyLabel(\"tui.input.submit\");\n\t\t\treturn label ? `${label} ${action}` : \"\";\n\t\t};\n\t\tconst keyGroupAction = (bindings: Keybinding[], action: string) => {\n\t\t\tconst labels = bindings.map(keyLabel).filter(Boolean).join(\"/\");\n\t\t\treturn labels ? `${labels} ${action}` : \"\";\n\t\t};\n\t\tconst joinHints = (...hints: string[]) => hints.filter(Boolean).join(\" • \");\n\n\t\tfunction finish(result: DialogResult): void {\n\t\t\tif (finished) return;\n\t\t\tfinished = true;\n\t\t\tdone(result);\n\t\t}\n\n\t\tconst onAbort = () => finish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\tif (signal?.aborted) onAbort();\n\t\telse signal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tfunction refresh(): void {\n\t\t\tcache.invalidate();\n\t\t\ttui.requestRender();\n\t\t}\n\n\t\tfunction syncEditorFocus(): void {\n\t\t\teditor.focused = dialogFocused && inputMode !== undefined;\n\t\t}\n\n\t\tfunction setCurrentIdx(index: number): void {\n\t\t\tcurrentIdx = Math.max(0, Math.min(questions.length - 1, index));\n\t\t\tview = \"question\";\n\t\t\tinputMode = undefined;\n\t\t\tnoteTarget = undefined;\n\t\t\tnotesSnapshot = undefined;\n\t\t\tfooterFocused = false;\n\t\t\teditor.setText(\"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction clearWarning(): void {\n\t\t\tcurrentState().warning = undefined;\n\t\t}\n\n\t\tfunction focusedOption(): DisplayOption | undefined {\n\t\t\treturn currentOptions()[currentState().optionIndex];\n\t\t}\n\n\t\tfunction showReview(): void {\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing !== undefined) {\n\t\t\t\tsetCurrentIdx(missing);\n\t\t\t\tcurrentState().warning = \"Answer this question before reviewing your answers.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tview = \"review\";\n\t\t\tinputMode = undefined;\n\t\t\tnoteTarget = undefined;\n\t\t\tfooterFocused = false;\n\t\t\teditor.setText(\"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction advanceAfterAnswer(): void {\n\t\t\tif (questions.length === 1 && !currentQuestion().multiSelect) {\n\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"answered\" });\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing === undefined) {\n\t\t\t\tshowReview();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsetCurrentIdx(missing);\n\t\t}\n\n\t\tfunction beginCustomInput(): void {\n\t\t\tconst state = currentState();\n\t\t\tinputMode = \"custom\";\n\t\t\tnoteTarget = undefined;\n\t\t\tconst existingSingle = state.singleAnswer?.kind === \"custom\" ? state.singleAnswer.answer : undefined;\n\t\t\teditor.setText(state.customAnswer?.text ?? existingSingle ?? \"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction selectSingleOption(option: QuestionOption): void {\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tstate.singleAnswer = {\n\t\t\t\tquestionIndex: currentIdx,\n\t\t\t\tquestion: question.question,\n\t\t\t\theader: question.header,\n\t\t\t\tkind: \"option\",\n\t\t\t\tanswer: option.label,\n\t\t\t\t...(option.preview ? { preview: option.preview } : {}),\n\t\t\t};\n\t\t\tstate.warning = undefined;\n\t\t}\n\n\t\tfunction beginNotesInput(): void {\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tconst option = focusedOption();\n\t\t\tif (!option || option.kind === \"other\") {\n\t\t\t\tbeginCustomInput();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (question.multiSelect && !state.multiSelected.has(option.optionIndex)) {\n\t\t\t\tstate.warning = \"Select the option first, then add notes.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (!question.multiSelect) {\n\t\t\t\tnotesSnapshot = { prior: state.singleAnswer };\n\t\t\t\tselectSingleOption(option);\n\t\t\t}\n\t\t\tinputMode = \"notes\";\n\t\t\tnoteTarget = option.label;\n\t\t\teditor.setText(state.notesByOption.get(option.label) ?? \"\");\n\t\t\tsyncEditorFocus();\n\t\t\trefresh();\n\t\t}\n\n\t\tfunction recordSingle(option: DisplayOption): void {\n\t\t\tif (option.kind === \"other\") {\n\t\t\t\tif (currentState().singleAnswer?.kind === \"custom\") {\n\t\t\t\t\tadvanceAfterAnswer();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbeginCustomInput();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tselectSingleOption(option);\n\t\t\tadvanceAfterAnswer();\n\t\t}\n\n\t\tfunction recordMulti(): void {\n\t\t\tconst state = currentState();\n\t\t\tif (!hasMultiAnswer(state)) {\n\t\t\t\tstate.warning = \"Select at least one option, type a custom answer, or cancel the questions.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstate.warning = undefined;\n\t\t\tadvanceAfterAnswer();\n\t\t}\n\n\t\tfunction toggleMultiOption(option: DisplayOption): void {\n\t\t\tconst state = currentState();\n\t\t\tif (option.kind === \"other\") {\n\t\t\t\tif (state.customAnswer) {\n\t\t\t\t\tstate.customAnswer.selected = !state.customAnswer.selected;\n\t\t\t\t\tclearWarning();\n\t\t\t\t} else {\n\t\t\t\t\tconst tabKey = keyLabel(\"tui.input.tab\");\n\t\t\t\t\tstate.warning = tabKey\n\t\t\t\t\t\t? `Press ${tabKey} to type a custom answer.`\n\t\t\t\t\t\t: \"Choose Type something to type a custom answer.\";\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (state.multiSelected.has(option.optionIndex)) state.multiSelected.delete(option.optionIndex);\n\t\t\telse state.multiSelected.add(option.optionIndex);\n\t\t\tclearWarning();\n\t\t}\n\n\t\tfunction finishReview(): void {\n\t\t\tconst missing = firstUnanswered(states);\n\t\t\tif (missing !== undefined) {\n\t\t\t\tsetCurrentIdx(missing);\n\t\t\t\tcurrentState().warning = \"Answer this question before submitting.\";\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"answered\" });\n\t\t}\n\n\t\teditor.onSubmit = (value) => {\n\t\t\tconst trimmed = value.trim();\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tif (trimmed.length > QUESTION_LIMITS.userTextChars) {\n\t\t\t\tstate.warning = `Keep notes and custom answers under ${QUESTION_LIMITS.userTextChars} characters.`;\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (inputMode === \"notes\") {\n\t\t\t\tif (noteTarget) {\n\t\t\t\t\tif (trimmed) state.notesByOption.set(noteTarget, trimmed);\n\t\t\t\t\telse state.notesByOption.delete(noteTarget);\n\t\t\t\t}\n\t\t\t\tinputMode = undefined;\n\t\t\t\tnoteTarget = undefined;\n\t\t\t\tnotesSnapshot = undefined;\n\t\t\t\teditor.setText(\"\");\n\t\t\t\tsyncEditorFocus();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (inputMode === \"custom\") {\n\t\t\t\tif (!trimmed) {\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (question.multiSelect) {\n\t\t\t\t\tstate.customAnswer = { text: trimmed, selected: true };\n\t\t\t\t\tstate.optionIndex = question.options.length;\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tstate.singleAnswer = {\n\t\t\t\t\tquestionIndex: currentIdx,\n\t\t\t\t\tquestion: question.question,\n\t\t\t\t\theader: question.header,\n\t\t\t\t\tkind: \"custom\",\n\t\t\t\t\tanswer: trimmed,\n\t\t\t\t};\n\t\t\t\tinputMode = undefined;\n\t\t\t\teditor.setText(\"\");\n\t\t\t\tsyncEditorFocus();\n\t\t\t\tadvanceAfterAnswer();\n\t\t\t}\n\t\t};\n\n\t\tfunction handleReviewInput(data: string): void {\n\t\t\tif (keyMatches(data, \"tui.editor.cursorLeft\")) {\n\t\t\t\tsetCurrentIdx(questions.length - 1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\tfinishReview();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\tsetCurrentIdx(questions.length - 1);\n\t\t\t}\n\t\t}\n\n\t\tfunction handleQuestionInput(data: string): void {\n\t\t\tif (inputMode) {\n\t\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\t\tif (inputMode === \"notes\" && notesSnapshot) {\n\t\t\t\t\t\tcurrentState().singleAnswer = notesSnapshot.prior;\n\t\t\t\t\t}\n\t\t\t\t\tinputMode = undefined;\n\t\t\t\t\tnoteTarget = undefined;\n\t\t\t\t\tnotesSnapshot = undefined;\n\t\t\t\t\teditor.setText(\"\");\n\t\t\t\t\tsyncEditorFocus();\n\t\t\t\t\trefresh();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\teditor.handleInput(data);\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.editor.cursorLeft\")) {\n\t\t\t\tif (currentIdx > 0) setCurrentIdx(currentIdx - 1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.editor.cursorRight\")) {\n\t\t\t\tif (currentIdx < questions.length - 1) setCurrentIdx(currentIdx + 1);\n\t\t\t\telse if (firstUnanswered(states) === undefined) showReview();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst state = currentState();\n\t\t\tconst options = currentOptions();\n\t\t\tif (keyMatches(data, \"tui.select.up\")) {\n\t\t\t\tif (footerFocused) {\n\t\t\t\t\tfooterFocused = false;\n\t\t\t\t\tstate.optionIndex = options.length - 1;\n\t\t\t\t} else if (state.optionIndex > 0) {\n\t\t\t\t\tstate.optionIndex--;\n\t\t\t\t}\n\t\t\t\tclearWarning();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.select.down\")) {\n\t\t\t\tif (footerFocused) return;\n\t\t\t\tif (state.optionIndex < options.length - 1) state.optionIndex++;\n\t\t\t\telse footerFocused = true;\n\t\t\t\tclearWarning();\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (keyMatches(data, \"tui.input.tab\")) {\n\t\t\t\tif (!footerFocused) beginNotesInput();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (data.length === 1 && data >= \"1\" && data <= \"9\") {\n\t\t\t\tconst index = data.charCodeAt(0) - 49;\n\t\t\t\tconst target = options[index];\n\t\t\t\tif (target) {\n\t\t\t\t\tfooterFocused = false;\n\t\t\t\t\tstate.optionIndex = index;\n\t\t\t\t\tclearWarning();\n\t\t\t\t\tif (currentQuestion().multiSelect) {\n\t\t\t\t\t\ttoggleMultiOption(target);\n\t\t\t\t\t\trefresh();\n\t\t\t\t\t} else {\n\t\t\t\t\t\trecordSingle(target);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (footerFocused) {\n\t\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"needs_clarification\" });\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst question = currentQuestion();\n\t\t\tconst option = options[state.optionIndex];\n\t\t\tif (question.multiSelect && keyMatches(data, \"app.list.toggle\")) {\n\t\t\t\tif (option) toggleMultiOption(option);\n\t\t\t\trefresh();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.select.confirm\")) {\n\t\t\t\tif (question.multiSelect) {\n\t\t\t\t\tif (option?.kind === \"other\" && !state.customAnswer) {\n\t\t\t\t\t\tbeginCustomInput();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\trecordMulti();\n\t\t\t\t} else if (option) recordSingle(option);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (keyMatches(data, \"tui.select.cancel\")) {\n\t\t\t\tfinish({ answers: orderedAnswers(questions, states), outcome: \"cancelled\" });\n\t\t\t}\n\t\t}\n\n\t\tfunction handleInput(data: string): void {\n\t\t\tif (view === \"review\") handleReviewInput(data);\n\t\t\telse handleQuestionInput(data);\n\t\t}\n\n\t\tfunction render(width: number): string[] {\n\t\t\treturn cache.get(width, tui.terminal.rows, compute);\n\t\t}\n\n\t\tfunction renderTabs(renderWidth: number, lines: string[]): void {\n\t\t\tif (questions.length < 2) return;\n\t\t\tconst tabCount = questions.length + 1;\n\t\t\tconst headerWidth = Math.max(4, Math.floor((renderWidth - tabCount * 5) / tabCount));\n\t\t\tconst tabs = [\n\t\t\t\t...questions.map((question, index) => {\n\t\t\t\t\tconst answered = hasAnswer(states[index]);\n\t\t\t\t\tconst label = ` ${answered ? \"■\" : \"□\"} ${truncateToWidth(question.header, headerWidth)} `;\n\t\t\t\t\tif (view === \"question\" && index === currentIdx) {\n\t\t\t\t\t\treturn theme.bg(\"selectedBg\", theme.fg(answered ? \"success\" : \"text\", label));\n\t\t\t\t\t}\n\t\t\t\t\treturn theme.fg(answered ? \"success\" : \"muted\", label);\n\t\t\t\t}),\n\t\t\t\tview === \"review\"\n\t\t\t\t\t? theme.bg(\"selectedBg\", theme.fg(\"text\", \" ✓ Submit \"))\n\t\t\t\t\t: theme.fg(firstUnanswered(states) === undefined ? \"success\" : \"dim\", \" ✓ Submit \"),\n\t\t\t].join(\" \");\n\t\t\twrapWithPrefix(\" \", tabs, renderWidth, lines);\n\t\t\tlines.push(\"\");\n\t\t}\n\n\t\tfunction previewLines(previewText: string, width: number, terminalRows: number): string[] {\n\t\t\tconst maxLines = Math.max(4, Math.min(PREVIEW_MAX_LINES, terminalRows - PREVIEW_CHROME_ROWS));\n\t\t\treturn previewCache.get(previewText, width, maxLines, () => {\n\t\t\t\tconst markdown = new Markdown(previewText, 1, 0, getMarkdownTheme());\n\t\t\t\tconst rendered = markdown.render(Math.max(1, width));\n\t\t\t\tif (rendered.length <= maxLines) return rendered;\n\t\t\t\treturn [\n\t\t\t\t\t...rendered.slice(0, maxLines),\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\ttruncateToWidth(`… ${rendered.length - maxLines} preview lines hidden`, Math.max(1, width)),\n\t\t\t\t\t),\n\t\t\t\t];\n\t\t\t});\n\t\t}\n\n\t\t/** Multi-select checkbox marker; the other row shows no box until a custom answer exists. */\n\t\tfunction multiMarker(option: DisplayOption, checked: boolean, hasCustomAnswer: boolean): string {\n\t\t\tif (option.kind === \"other\" && !hasCustomAnswer) return \" \";\n\t\t\treturn checked ? theme.fg(\"success\", \"[x]\") : theme.fg(\"dim\", \"[ ]\");\n\t\t}\n\n\t\tfunction renderReview(renderWidth: number, lines: string[]): void {\n\t\t\twrapWithPrefix(\" \", theme.fg(\"accent\", \"Review answers\"), renderWidth, lines);\n\t\t\tlines.push(\"\");\n\t\t\tfor (const answer of orderedAnswers(questions, states)) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\"text\", `${answer.questionIndex + 1}. ${answer.question}`),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t\tconst response =\n\t\t\t\t\tanswer.kind === \"multi\"\n\t\t\t\t\t\t? (answer.selected?.join(\", \") ?? \"(no input)\")\n\t\t\t\t\t\t: (answer.answer ?? \"(no input)\");\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"success\", response), renderWidth, lines);\n\t\t\t\tfor (const note of answer.notes ?? []) {\n\t\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", `Note for ${note.option}: ${note.text}`), renderWidth, lines);\n\t\t\t\t}\n\t\t\t\tlines.push(\"\");\n\t\t\t}\n\t\t\twrapWithPrefix(\n\t\t\t\t\" \",\n\t\t\t\ttheme.fg(\n\t\t\t\t\t\"dim\",\n\t\t\t\t\tjoinHints(\n\t\t\t\t\t\tkeyAction(\"tui.select.confirm\", \"submit\"),\n\t\t\t\t\t\tkeyAction(\"tui.select.cancel\", \"edit last question\"),\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t}\n\n\t\tfunction compute(width: number, terminalRows: number): string[] {\n\t\t\tconst lines: string[] = [];\n\t\t\tconst renderWidth = Math.max(1, width);\n\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\trenderTabs(renderWidth, lines);\n\n\t\t\tif (view === \"review\") {\n\t\t\t\trenderReview(renderWidth, lines);\n\t\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\t\treturn lines;\n\t\t\t}\n\n\t\t\tconst question = currentQuestion();\n\t\t\tconst state = currentState();\n\t\t\tconst options = currentOptions();\n\t\t\tconst isMulti = question.multiSelect === true;\n\t\t\tconst digitRange = `1-${Math.min(9, options.length)}`;\n\t\t\twrapWithPrefix(\n\t\t\t\t\" \",\n\t\t\t\t`${theme.fg(\"accent\", question.header)} ${theme.fg(\"text\", question.question)}`,\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t\tlines.push(\"\");\n\n\t\t\tconst optionLines: string[] = [];\n\t\t\tconst hasPreview = !isMulti && options.some((option) => option.kind === \"option\" && option.preview);\n\t\t\tconst showPreviewSideBySide = hasPreview && renderWidth >= 60;\n\t\t\tconst listWidth = showPreviewSideBySide ? Math.max(20, Math.floor(renderWidth * 0.4)) : renderWidth;\n\n\t\t\tfor (let index = 0; index < options.length; index++) {\n\t\t\t\tconst option = options[index];\n\t\t\t\tconst focused = !footerFocused && index === state.optionIndex;\n\t\t\t\tconst checked =\n\t\t\t\t\tisMulti &&\n\t\t\t\t\t((option.kind === \"option\" && state.multiSelected.has(option.optionIndex)) ||\n\t\t\t\t\t\t(option.kind === \"other\" && state.customAnswer?.selected === true));\n\t\t\t\tconst selectedSingle =\n\t\t\t\t\t!isMulti &&\n\t\t\t\t\t(option.kind === \"other\"\n\t\t\t\t\t\t? state.singleAnswer?.kind === \"custom\"\n\t\t\t\t\t\t: state.singleAnswer?.answer === option.label);\n\t\t\t\tconst marker = isMulti ? multiMarker(option, checked, state.customAnswer !== undefined) : \" \";\n\t\t\t\tconst focusArrow = focused ? theme.fg(\"accent\", \"→\") : \" \";\n\t\t\t\tconst note =\n\t\t\t\t\toption.kind === \"option\" && state.notesByOption.has(option.label) ? theme.fg(\"success\", \" +note\") : \"\";\n\t\t\t\tconst customText =\n\t\t\t\t\toption.kind === \"other\"\n\t\t\t\t\t\t? (state.customAnswer?.text ??\n\t\t\t\t\t\t\t(state.singleAnswer?.kind === \"custom\" ? state.singleAnswer.answer : undefined))\n\t\t\t\t\t\t: undefined;\n\t\t\t\tconst labelText = customText ? `${option.label} ✎ ${customText}` : option.label;\n\t\t\t\tconst label = `${index + 1}. ${labelText}${selectedSingle ? \" ✓\" : \"\"}${note}`;\n\t\t\t\tconst color = focused ? \"accent\" : selectedSingle || checked ? \"success\" : \"text\";\n\t\t\t\twrapWithPrefix(`${focusArrow} ${marker} `, theme.fg(color, label), listWidth, optionLines);\n\t\t\t\tif (option.kind === \"option\" && option.description) {\n\t\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", option.description), listWidth, optionLines);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (inputMode) {\n\t\t\t\toptionLines.push(\"\");\n\t\t\t\tconst label = inputMode === \"notes\" ? `Notes for ${noteTarget ?? \"option\"}:` : \"Your answer:\";\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"muted\", label), listWidth, optionLines);\n\t\t\t\tfor (const line of editor.render(Math.max(1, listWidth - 2))) optionLines.push(` ${line}`);\n\t\t\t}\n\n\t\t\tif (showPreviewSideBySide) {\n\t\t\t\tconst gap = 2;\n\t\t\t\tconst rightWidth = renderWidth - listWidth - gap;\n\t\t\t\tconst focused = options[state.optionIndex];\n\t\t\t\tconst previewText = focused?.kind === \"option\" && focused.preview ? focused.preview : \"\";\n\t\t\t\tconst rightLines = previewText\n\t\t\t\t\t? previewLines(previewText, rightWidth, terminalRows)\n\t\t\t\t\t: [theme.fg(\"dim\", \"(no preview)\")];\n\t\t\t\tconst maxRows = Math.max(optionLines.length, rightLines.length);\n\t\t\t\tconst pad = \" \".repeat(gap);\n\t\t\t\tfor (let row = 0; row < maxRows; row++) {\n\t\t\t\t\tconst left = row < optionLines.length ? optionLines[row] : \"\";\n\t\t\t\t\tconst leftPadded = left + \" \".repeat(Math.max(0, listWidth - visibleWidth(left)));\n\t\t\t\t\tconst right = row < rightLines.length ? rightLines[row] : \"\";\n\t\t\t\t\tlines.push(`${leftPadded}${pad}${right}`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tlines.push(...optionLines);\n\t\t\t\tif (hasPreview) {\n\t\t\t\t\tconst focused = options[state.optionIndex];\n\t\t\t\t\tconst previewText = focused?.kind === \"option\" && focused.preview ? focused.preview : \"\";\n\t\t\t\t\tif (previewText) {\n\t\t\t\t\t\tlines.push(\"\");\n\t\t\t\t\t\tlines.push(ruleBorder(theme, renderWidth, \"dim\"));\n\t\t\t\t\t\tlines.push(...previewLines(previewText, renderWidth, terminalRows));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlines.push(\"\");\n\t\t\tconst chatPrefix = footerFocused ? theme.fg(\"accent\", \"→\") : \" \";\n\t\t\twrapWithPrefix(\n\t\t\t\t`${chatPrefix} `,\n\t\t\t\ttheme.fg(footerFocused ? \"accent\" : \"text\", \"Chat about this\"),\n\t\t\t\trenderWidth,\n\t\t\t\tlines,\n\t\t\t);\n\t\t\tif (state.warning) {\n\t\t\t\tlines.push(\"\");\n\t\t\t\twrapWithPrefix(\" \", theme.fg(\"warning\", state.warning), renderWidth, lines);\n\t\t\t}\n\t\t\tlines.push(\"\");\n\t\t\tif (inputMode === \"notes\") {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\"dim\", joinHints(editorSubmitAction(\"save notes\"), keyAction(\"tui.select.cancel\", \"back\"))),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (inputMode === \"custom\") {\n\t\t\t\tconst hint = isMulti ? \"save custom answer\" : \"continue\";\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\"dim\", joinHints(editorSubmitAction(hint), keyAction(\"tui.select.cancel\", \"back\"))),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (footerFocused) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\tjoinHints(\n\t\t\t\t\t\t\tkeyAction(\"tui.select.confirm\", \"discuss\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.up\", \"return to options\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.cancel\", \"cancel\"),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else if (isMulti) {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\tjoinHints(\n\t\t\t\t\t\t\t`${digitRange} toggle`,\n\t\t\t\t\t\t\tkeyAction(\"app.list.toggle\", \"toggle focused\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.input.tab\", \"notes/custom\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.confirm\", \"continue\"),\n\t\t\t\t\t\t\tkeyGroupAction([\"tui.editor.cursorLeft\", \"tui.editor.cursorRight\"], \"questions\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.cancel\", \"cancel\"),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\twrapWithPrefix(\n\t\t\t\t\t\" \",\n\t\t\t\t\ttheme.fg(\n\t\t\t\t\t\t\"dim\",\n\t\t\t\t\t\tjoinHints(\n\t\t\t\t\t\t\t`${digitRange} select`,\n\t\t\t\t\t\t\tkeyAction(\"tui.input.tab\", \"notes/custom\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.confirm\", \"select\"),\n\t\t\t\t\t\t\tkeyGroupAction([\"tui.editor.cursorLeft\", \"tui.editor.cursorRight\"], \"questions\"),\n\t\t\t\t\t\t\tkeyAction(\"tui.select.cancel\", \"cancel\"),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t\trenderWidth,\n\t\t\t\t\tlines,\n\t\t\t\t);\n\t\t\t}\n\t\t\tlines.push(ruleBorder(theme, renderWidth));\n\t\t\treturn lines;\n\t\t}\n\n\t\treturn {\n\t\t\tget focused() {\n\t\t\t\treturn dialogFocused;\n\t\t\t},\n\t\t\tset focused(value: boolean) {\n\t\t\t\tdialogFocused = value;\n\t\t\t\tsyncEditorFocus();\n\t\t\t},\n\t\t\trender,\n\t\t\tinvalidate: () => {\n\t\t\t\tcache.invalidate();\n\t\t\t},\n\t\t\thandleInput,\n\t\t\tdispose() {\n\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t},\n\t\t};\n\t};\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  import { Editor, Markdown, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
2
+ import { keyLabel as configuredKeyLabel } from "../../modes/interactive/components/keybinding-hints.js";
2
3
  import { getMarkdownTheme } from "../../modes/interactive/theme/theme.js";
3
4
  import { ruleBorder, wrapWithPrefix } from "./dialog-primitives.js";
4
5
  import { QUESTION_LIMITS } from "./limits.js";
@@ -41,7 +42,21 @@ export function createQuestionDialog(questions, signal) {
41
42
  const currentState = () => states[currentIdx];
42
43
  const currentOptions = () => optionsByQuestion[currentIdx];
43
44
  const keyMatches = (data, keybinding) => keybindings.matches(data, keybinding);
44
- const keyLabel = (keybinding, fallback) => keybindings.getKeys(keybinding).join("/") || fallback;
45
+ const keyLabel = (keybinding) => configuredKeyLabel(keybinding, { keybindings });
46
+ const keyAction = (keybinding, action) => {
47
+ const label = keyLabel(keybinding);
48
+ return label ? `${label} ${action}` : "";
49
+ };
50
+ // Editor reads the global TUI manager internally, while dialog controls use the injected manager.
51
+ const editorSubmitAction = (action) => {
52
+ const label = configuredKeyLabel("tui.input.submit");
53
+ return label ? `${label} ${action}` : "";
54
+ };
55
+ const keyGroupAction = (bindings, action) => {
56
+ const labels = bindings.map(keyLabel).filter(Boolean).join("/");
57
+ return labels ? `${labels} ${action}` : "";
58
+ };
59
+ const joinHints = (...hints) => hints.filter(Boolean).join(" • ");
45
60
  function finish(result) {
46
61
  if (finished)
47
62
  return;
@@ -180,7 +195,10 @@ export function createQuestionDialog(questions, signal) {
180
195
  clearWarning();
181
196
  }
182
197
  else {
183
- state.warning = `Press ${keyLabel("tui.input.tab", "Tab")} to type a custom answer.`;
198
+ const tabKey = keyLabel("tui.input.tab");
199
+ state.warning = tabKey
200
+ ? `Press ${tabKey} to type a custom answer.`
201
+ : "Choose Type something to type a custom answer.";
184
202
  }
185
203
  return;
186
204
  }
@@ -441,7 +459,7 @@ export function createQuestionDialog(questions, signal) {
441
459
  }
442
460
  lines.push("");
443
461
  }
444
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("tui.select.confirm", "Enter")} to submit • ${keyLabel("tui.select.cancel", "Esc")} to edit the last question`), renderWidth, lines);
462
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(keyAction("tui.select.confirm", "submit"), keyAction("tui.select.cancel", "edit last question"))), renderWidth, lines);
445
463
  }
446
464
  function compute(width, terminalRows) {
447
465
  const lines = [];
@@ -457,6 +475,7 @@ export function createQuestionDialog(questions, signal) {
457
475
  const state = currentState();
458
476
  const options = currentOptions();
459
477
  const isMulti = question.multiSelect === true;
478
+ const digitRange = `1-${Math.min(9, options.length)}`;
460
479
  wrapWithPrefix(" ", `${theme.fg("accent", question.header)} ${theme.fg("text", question.question)}`, renderWidth, lines);
461
480
  lines.push("");
462
481
  const optionLines = [];
@@ -473,7 +492,7 @@ export function createQuestionDialog(questions, signal) {
473
492
  (option.kind === "other"
474
493
  ? state.singleAnswer?.kind === "custom"
475
494
  : state.singleAnswer?.answer === option.label);
476
- const marker = isMulti ? multiMarker(option, checked, state.customAnswer !== undefined) : "";
495
+ const marker = isMulti ? multiMarker(option, checked, state.customAnswer !== undefined) : " ";
477
496
  const focusArrow = focused ? theme.fg("accent", "→") : " ";
478
497
  const note = option.kind === "option" && state.notesByOption.has(option.label) ? theme.fg("success", " +note") : "";
479
498
  const customText = option.kind === "other"
@@ -533,20 +552,20 @@ export function createQuestionDialog(questions, signal) {
533
552
  }
534
553
  lines.push("");
535
554
  if (inputMode === "notes") {
536
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("tui.select.confirm", "Enter")} to save notes • ${keyLabel("tui.select.cancel", "Esc")} to go back`), renderWidth, lines);
555
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(editorSubmitAction("save notes"), keyAction("tui.select.cancel", "back"))), renderWidth, lines);
537
556
  }
538
557
  else if (inputMode === "custom") {
539
- const hint = isMulti ? "to save custom answer" : "to continue";
540
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("tui.select.confirm", "Enter")} ${hint} • ${keyLabel("tui.select.cancel", "Esc")} to go back`), renderWidth, lines);
558
+ const hint = isMulti ? "save custom answer" : "continue";
559
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(editorSubmitAction(hint), keyAction("tui.select.cancel", "back"))), renderWidth, lines);
541
560
  }
542
561
  else if (footerFocused) {
543
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("tui.select.confirm", "Enter")} to discuss • ${keyLabel("tui.select.up", "Up")} to return to options`), renderWidth, lines);
562
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(keyAction("tui.select.confirm", "discuss"), keyAction("tui.select.up", "return to options"), keyAction("tui.select.cancel", "cancel"))), renderWidth, lines);
544
563
  }
545
564
  else if (isMulti) {
546
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("app.list.toggle", "Space")} to toggle • ${keyLabel("tui.input.tab", "Tab")} for notes/custom • ${keyLabel("tui.select.confirm", "Enter")} to continue • ${keyLabel("tui.editor.cursorLeft", "Left")}/${keyLabel("tui.editor.cursorRight", "Right")} questions • ${keyLabel("tui.select.cancel", "Esc")} to cancel`), renderWidth, lines);
565
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(`${digitRange} toggle`, keyAction("app.list.toggle", "toggle focused"), keyAction("tui.input.tab", "notes/custom"), keyAction("tui.select.confirm", "continue"), keyGroupAction(["tui.editor.cursorLeft", "tui.editor.cursorRight"], "questions"), keyAction("tui.select.cancel", "cancel"))), renderWidth, lines);
547
566
  }
548
567
  else {
549
- wrapWithPrefix(" ", theme.fg("dim", `${keyLabel("tui.input.tab", "Tab")} for notes/custom • ${keyLabel("tui.select.confirm", "Enter")} to select • ${keyLabel("tui.editor.cursorLeft", "Left")}/${keyLabel("tui.editor.cursorRight", "Right")} questions • ${keyLabel("tui.select.cancel", "Esc")} to cancel`), renderWidth, lines);
568
+ wrapWithPrefix(" ", theme.fg("dim", joinHints(`${digitRange} select`, keyAction("tui.input.tab", "notes/custom"), keyAction("tui.select.confirm", "select"), keyGroupAction(["tui.editor.cursorLeft", "tui.editor.cursorRight"], "questions"), keyAction("tui.select.cancel", "cancel"))), renderWidth, lines);
550
569
  }
551
570
  lines.push(ruleBorder(theme, renderWidth));
552
571
  return lines;