@visual-z/xeno 0.2.1 → 0.3.0

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 (258) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +106 -26
  3. package/docs/install.md +51 -13
  4. package/docs/providers.md +11 -0
  5. package/package.json +5 -4
  6. package/src/apps/approve.ts +3 -3
  7. package/src/apps/commands.ts +38 -5
  8. package/src/apps/completions.ts +73 -19
  9. package/src/apps/image-draft.ts +46 -0
  10. package/src/apps/project-trust.ts +2 -42
  11. package/src/apps/provider-connect.ts +346 -0
  12. package/src/apps/render.ts +22 -47
  13. package/src/apps/resources.ts +5 -2
  14. package/src/apps/shell.ts +298 -1526
  15. package/src/apps/startup-input.ts +45 -9
  16. package/src/apps/startup.ts +128 -86
  17. package/src/apps/ui/app.ts +1383 -0
  18. package/src/apps/ui/extension-host.ts +194 -0
  19. package/src/apps/ui/image-cache.ts +78 -0
  20. package/src/apps/ui/palette.ts +88 -0
  21. package/src/apps/ui/project.ts +127 -0
  22. package/src/apps/ui/standalone.ts +146 -0
  23. package/src/apps/ui/status-model.ts +68 -0
  24. package/src/apps/ui/surface.ts +84 -0
  25. package/src/apps/ui/theme.ts +86 -0
  26. package/src/core/acceptance.ts +107 -0
  27. package/src/core/action-defaults.ts +47 -295
  28. package/src/core/benchmark.ts +2 -2
  29. package/src/core/branch-receipt.ts +24 -0
  30. package/src/core/checkpoints.ts +64 -0
  31. package/src/core/churn.ts +2 -0
  32. package/src/core/command-artifacts.ts +94 -0
  33. package/src/core/command-contract.ts +35 -0
  34. package/src/core/completion-review.ts +21 -5
  35. package/src/core/context-detail.ts +37 -0
  36. package/src/core/context-policy.ts +74 -1
  37. package/src/core/continuation.ts +17 -2
  38. package/src/core/derive.ts +29 -3
  39. package/src/core/draft-images.ts +56 -0
  40. package/src/core/environment.ts +145 -0
  41. package/src/core/evaluation-coverage.ts +74 -0
  42. package/src/core/event-validation.ts +80 -5
  43. package/src/core/events.ts +97 -3
  44. package/src/core/evidence-links.ts +7 -0
  45. package/src/core/evidence.ts +13 -3
  46. package/src/core/execution.ts +8 -2
  47. package/src/core/experiment.ts +133 -0
  48. package/src/core/export.ts +9 -0
  49. package/src/core/failure-recovery.ts +173 -0
  50. package/src/core/file-precondition.ts +72 -0
  51. package/src/core/handoff.ts +65 -4
  52. package/src/core/history-query.ts +38 -2
  53. package/src/core/history.ts +38 -1
  54. package/src/core/integration-health.ts +34 -0
  55. package/src/core/keymap.ts +8 -20
  56. package/src/core/model-picker.ts +64 -0
  57. package/src/core/native-eval.ts +25 -0
  58. package/src/core/note-guidance.ts +34 -0
  59. package/src/core/observation-view.ts +49 -8
  60. package/src/core/permissions.ts +24 -2
  61. package/src/core/process-receipt.ts +105 -0
  62. package/src/core/process-state.ts +37 -0
  63. package/src/core/project.ts +45 -18
  64. package/src/core/prompt-policy.ts +37 -0
  65. package/src/core/query-eval-contract.ts +14 -0
  66. package/src/core/read-observation.ts +82 -0
  67. package/src/core/recovery-corpus.ts +17 -0
  68. package/src/core/recovery-evaluation.ts +60 -0
  69. package/src/core/recovery-sources.ts +74 -0
  70. package/src/core/runtime-target.ts +94 -0
  71. package/src/core/search-observation.ts +97 -0
  72. package/src/core/session-tree.ts +147 -0
  73. package/src/core/settings.ts +26 -10
  74. package/src/core/skills.ts +44 -0
  75. package/src/core/sql-query.ts +211 -0
  76. package/src/core/theme.ts +36 -58
  77. package/src/core/tool-contract.ts +51 -2
  78. package/src/core/tool-discovery.ts +87 -0
  79. package/src/core/tool-failure.ts +60 -0
  80. package/src/core/tool-mode-protocol.ts +25 -0
  81. package/src/core/transcript.ts +19 -4
  82. package/src/core/trial-lifecycle.ts +14 -0
  83. package/src/core/tui-mode.ts +29 -0
  84. package/src/core/update-check.ts +97 -0
  85. package/src/core/version.ts +55 -0
  86. package/src/core/work-state.ts +49 -3
  87. package/src/core/working-set.ts +91 -0
  88. package/src/runtime/auth.ts +2 -2
  89. package/src/runtime/branch-jobs.ts +36 -0
  90. package/src/runtime/clipboard.ts +15 -4
  91. package/src/runtime/command-artifacts.ts +64 -0
  92. package/src/runtime/environment-tools.ts +60 -0
  93. package/src/runtime/evidence-notes.ts +4 -2
  94. package/src/runtime/exec-sessions.ts +206 -61
  95. package/src/runtime/execution-control.ts +1 -0
  96. package/src/runtime/extension-ui.ts +22 -30
  97. package/src/runtime/extensions.ts +95 -16
  98. package/src/runtime/harness-comparison.ts +27 -7
  99. package/src/runtime/harness-eval.ts +460 -0
  100. package/src/runtime/history-cache.ts +57 -0
  101. package/src/runtime/history-tools.ts +62 -12
  102. package/src/runtime/integrations.ts +210 -0
  103. package/src/runtime/loop.ts +1021 -268
  104. package/src/runtime/model-discovery.ts +208 -0
  105. package/src/runtime/native-eval-fixtures.ts +33 -0
  106. package/src/runtime/native-eval.ts +143 -0
  107. package/src/runtime/oauth.ts +1 -1
  108. package/src/runtime/provider-connect.ts +109 -0
  109. package/src/runtime/provider-presets.ts +87 -0
  110. package/src/runtime/query-eval-verifier.ts +28 -0
  111. package/src/runtime/recovery-coverage.ts +71 -0
  112. package/src/runtime/recovery-evidence.ts +76 -0
  113. package/src/runtime/recovery-fixtures.ts +116 -0
  114. package/src/runtime/runtime-target.ts +149 -0
  115. package/src/runtime/tool-broker.ts +137 -0
  116. package/src/runtime/tool-discovery.ts +57 -0
  117. package/src/runtime/tool-mode-eval.ts +146 -0
  118. package/src/runtime/tool-queue.ts +77 -0
  119. package/src/runtime/tools.ts +628 -183
  120. package/src/runtime/trial-control.ts +45 -0
  121. package/src/runtime/update-check.ts +47 -0
  122. package/src/runtime/window-trial.ts +2 -1
  123. package/src/runtime/working-notes.ts +33 -3
  124. package/src/runtime/workspace-lifecycle.ts +97 -0
  125. package/src/store/acceptance.ts +15 -0
  126. package/src/store/checkpoint-history.ts +43 -0
  127. package/src/store/command-artifacts.ts +96 -0
  128. package/src/store/command.ts +65 -37
  129. package/src/store/file-checkpoints.ts +104 -0
  130. package/src/store/file-index.ts +13 -1
  131. package/src/store/git-checkpoints.ts +67 -0
  132. package/src/store/observations.ts +61 -20
  133. package/src/store/process-receipts.ts +34 -0
  134. package/src/store/query-source.ts +13 -8
  135. package/src/store/recovery.ts +2 -3
  136. package/src/store/replay.ts +16 -4
  137. package/src/store/runtime-target.ts +64 -0
  138. package/src/store/search.ts +171 -132
  139. package/src/store/session-archive.ts +4 -0
  140. package/src/store/source-cancellation.ts +6 -0
  141. package/src/store/update-cache.ts +36 -0
  142. package/src/store/workspace-observation.ts +113 -0
  143. package/src/tui/ansi-spans.ts +126 -0
  144. package/src/tui/style.ts +19 -16
  145. package/src/tui/ui/atomic-draft.ts +121 -0
  146. package/src/tui/ui/canvas.ts +96 -0
  147. package/src/tui/ui/colors.ts +70 -0
  148. package/src/tui/ui/composer.ts +199 -0
  149. package/src/tui/ui/dialog.ts +379 -0
  150. package/src/tui/ui/draft.ts +23 -0
  151. package/src/tui/ui/edge.ts +186 -0
  152. package/src/tui/ui/image-size.ts +40 -0
  153. package/src/tui/ui/ink.ts +210 -0
  154. package/src/tui/ui/items.ts +40 -0
  155. package/src/tui/ui/keys.ts +40 -0
  156. package/src/tui/ui/layout.ts +74 -0
  157. package/src/tui/ui/list-model.ts +86 -0
  158. package/src/tui/ui/list-view.ts +172 -0
  159. package/src/tui/ui/mascot.ts +67 -0
  160. package/src/tui/ui/messages.ts +518 -0
  161. package/src/tui/ui/motion.ts +28 -0
  162. package/src/tui/ui/paths.ts +10 -0
  163. package/src/tui/ui/pictures.ts +78 -0
  164. package/src/tui/ui/screen.ts +421 -0
  165. package/src/tui/ui/status.ts +271 -0
  166. package/src/tui/ui/styled.ts +85 -0
  167. package/src/tui/ui/testing.ts +5 -0
  168. package/src/tui/ui/tool-card.ts +150 -0
  169. package/src/apps/extension-ui.ts +0 -330
  170. package/src/apps/resource-browser.ts +0 -75
  171. package/src/apps/session-browser.ts +0 -113
  172. package/src/apps/session-picker.ts +0 -55
  173. package/src/tui/ansi.ts +0 -67
  174. package/src/tui/autocomplete.ts +0 -29
  175. package/src/tui/chat-viewport.ts +0 -47
  176. package/src/tui/complete.ts +0 -87
  177. package/src/tui/editor.ts +0 -940
  178. package/src/tui/host.ts +0 -264
  179. package/src/tui/input.ts +0 -75
  180. package/src/tui/keys.ts +0 -378
  181. package/src/tui/markdown.ts +0 -17
  182. package/src/tui/render.ts +0 -153
  183. package/src/tui/select.ts +0 -258
  184. package/src/tui/settings.ts +0 -44
  185. package/src/tui/statusline.ts +0 -162
  186. package/src/tui/system-theme.ts +0 -700
  187. package/src/tui/terminal.ts +0 -30
  188. package/src/tui/text.ts +0 -52
  189. package/src/tui/toolview.ts +0 -386
  190. package/src/tui/transcript.ts +0 -123
  191. package/src/tui/tree.ts +0 -471
  192. package/src/vendor/pi-tui/LICENSE.upstream +0 -21
  193. package/src/vendor/pi-tui/UPSTREAM.json +0 -50
  194. package/src/vendor/pi-tui/VENDOR.md +0 -25
  195. package/src/vendor/pi-tui/alt-screen-search.ts +0 -327
  196. package/src/vendor/pi-tui/autocomplete.ts +0 -860
  197. package/src/vendor/pi-tui/colors.ts +0 -367
  198. package/src/vendor/pi-tui/components/alt-screen-flash.ts +0 -51
  199. package/src/vendor/pi-tui/components/box.ts +0 -166
  200. package/src/vendor/pi-tui/components/cancellable-loader.ts +0 -40
  201. package/src/vendor/pi-tui/components/editor.ts +0 -2472
  202. package/src/vendor/pi-tui/components/h-stack.ts +0 -44
  203. package/src/vendor/pi-tui/components/image.ts +0 -127
  204. package/src/vendor/pi-tui/components/input.ts +0 -494
  205. package/src/vendor/pi-tui/components/loader.ts +0 -101
  206. package/src/vendor/pi-tui/components/markdown.ts +0 -1021
  207. package/src/vendor/pi-tui/components/mouse-region.ts +0 -33
  208. package/src/vendor/pi-tui/components/scroll-view.ts +0 -224
  209. package/src/vendor/pi-tui/components/select-list.ts +0 -273
  210. package/src/vendor/pi-tui/components/settings-list.ts +0 -328
  211. package/src/vendor/pi-tui/components/spacer.ts +0 -28
  212. package/src/vendor/pi-tui/components/stack.ts +0 -154
  213. package/src/vendor/pi-tui/components/text.ts +0 -107
  214. package/src/vendor/pi-tui/components/truncated-text.ts +0 -65
  215. package/src/vendor/pi-tui/components/v-stack.ts +0 -33
  216. package/src/vendor/pi-tui/editor-component.ts +0 -74
  217. package/src/vendor/pi-tui/fuzzy.ts +0 -138
  218. package/src/vendor/pi-tui/index.ts +0 -186
  219. package/src/vendor/pi-tui/keybindings.ts +0 -320
  220. package/src/vendor/pi-tui/keys.ts +0 -1401
  221. package/src/vendor/pi-tui/kill-ring.ts +0 -46
  222. package/src/vendor/pi-tui/latex.ts +0 -1506
  223. package/src/vendor/pi-tui/layout-node.ts +0 -51
  224. package/src/vendor/pi-tui/layout.ts +0 -449
  225. package/src/vendor/pi-tui/native/UPSTREAM.json +0 -23
  226. package/src/vendor/pi-tui/native/clipboard.h +0 -208
  227. package/src/vendor/pi-tui/native/darwin/README.md +0 -22
  228. package/src/vendor/pi-tui/native/darwin/build.sh +0 -69
  229. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  230. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  231. package/src/vendor/pi-tui/native/darwin/src/darwin-platform.m +0 -132
  232. package/src/vendor/pi-tui/native/linux/README.md +0 -23
  233. package/src/vendor/pi-tui/native/linux/build.sh +0 -65
  234. package/src/vendor/pi-tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  235. package/src/vendor/pi-tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  236. package/src/vendor/pi-tui/native/linux/src/clipboard-worker.h +0 -70
  237. package/src/vendor/pi-tui/native/linux/src/linux-platform-x11.c +0 -274
  238. package/src/vendor/pi-tui/native/napi.h +0 -91
  239. package/src/vendor/pi-tui/native/win32/README.md +0 -31
  240. package/src/vendor/pi-tui/native/win32/build.mjs +0 -232
  241. package/src/vendor/pi-tui/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  242. package/src/vendor/pi-tui/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  243. package/src/vendor/pi-tui/native/win32/src/win32-platform.c +0 -241
  244. package/src/vendor/pi-tui/native-modifiers.ts +0 -13
  245. package/src/vendor/pi-tui/native-module-path.ts +0 -31
  246. package/src/vendor/pi-tui/native-platform.ts +0 -65
  247. package/src/vendor/pi-tui/oklab.ts +0 -233
  248. package/src/vendor/pi-tui/stdin-buffer.ts +0 -444
  249. package/src/vendor/pi-tui/terminal-colors.ts +0 -91
  250. package/src/vendor/pi-tui/terminal-image.ts +0 -731
  251. package/src/vendor/pi-tui/terminal.ts +0 -554
  252. package/src/vendor/pi-tui/tui-alt-screen.ts +0 -1750
  253. package/src/vendor/pi-tui/tui-main-screen.ts +0 -655
  254. package/src/vendor/pi-tui/tui.ts +0 -1493
  255. package/src/vendor/pi-tui/undo-stack.ts +0 -28
  256. package/src/vendor/pi-tui/utils.ts +0 -1385
  257. package/src/vendor/pi-tui/wheel-scroll.ts +0 -82
  258. package/src/vendor/pi-tui/word-navigation.ts +0 -117
package/src/tui/select.ts DELETED
@@ -1,258 +0,0 @@
1
- /**
2
- * A pickable list: applySelectKey(state, key) is pure, renderSelect paints.
3
- *
4
- * The visible window follows the selection so a /model list of dozens
5
- * does not dump every row into the live region. An empty list must say
6
- * why it is empty -- a blank box looks like a render bug.
7
- */
8
-
9
- import { assertNever } from "./exhaustive.ts";
10
- import type { Key } from "./keys.ts";
11
- import type { Component } from "./text.ts";
12
- import { displayWidth } from "./width.ts";
13
- import { PLAIN_ATTRS, paint, type Style } from "./style.ts";
14
- import { Input } from "../vendor/pi-tui/components/input.ts";
15
- import { SelectList as PiSelectList } from "../vendor/pi-tui/components/select-list.ts";
16
- import { fuzzyFilter as piFuzzyFilter } from "../vendor/pi-tui/fuzzy.ts";
17
- import { truncateToWidth } from "../vendor/pi-tui/utils.ts";
18
- import { getKeybindings } from "../vendor/pi-tui/keybindings.ts";
19
- import { encodeKey } from "./input.ts";
20
-
21
- /**
22
- * Default visible rows. Assumed, not measured: enough to scan a /model
23
- * list without eating the whole scrollback. Callers pass maxVisible.
24
- */
25
- export const DEFAULT_MAX_VISIBLE = 10;
26
-
27
- export interface SelectItem {
28
- readonly value: string;
29
- readonly label: string;
30
- readonly description?: string;
31
- }
32
-
33
- export interface CreateSelectOptions {
34
- readonly maxVisible?: number;
35
- readonly emptyMessage?: string;
36
- readonly style?: Style;
37
- readonly title?: string;
38
- readonly selectedValue?: string;
39
- readonly search?: boolean;
40
- readonly hint?: string;
41
- readonly filter?: (items: readonly SelectItem[], query: string) => readonly SelectItem[];
42
- }
43
-
44
- export interface SelectState {
45
- readonly items: readonly SelectItem[];
46
- readonly selected: number;
47
- readonly maxVisible: number;
48
- readonly confirmed: SelectItem | null;
49
- readonly cancelled: boolean;
50
- readonly emptyMessage: string;
51
- }
52
-
53
- export function createSelect(
54
- items: readonly SelectItem[],
55
- opts: CreateSelectOptions = {},
56
- ): SelectState {
57
- return {
58
- items,
59
- selected: 0,
60
- maxVisible: opts.maxVisible ?? DEFAULT_MAX_VISIBLE,
61
- confirmed: null,
62
- cancelled: false,
63
- emptyMessage: opts.emptyMessage ?? "No matching items",
64
- };
65
- }
66
-
67
- export function applySelectKey(state: SelectState, key: Key): SelectState {
68
- switch (key.type) {
69
- case "up":
70
- return move(state, -1);
71
- case "down":
72
- return move(state, 1);
73
- case "enter":
74
- return confirm(state);
75
- case "escape":
76
- return { ...state, cancelled: true, confirmed: null };
77
- case "char":
78
- case "backspace":
79
- case "delete":
80
- case "tab":
81
- case "left":
82
- case "right":
83
- case "home":
84
- case "end":
85
- case "pageup":
86
- case "pagedown":
87
- case "ctrl":
88
- case "alt":
89
- return state;
90
- default:
91
- return assertNever(key, "applySelectKey");
92
- }
93
- }
94
-
95
- function move(state: SelectState, dir: -1 | 1): SelectState {
96
- const n = state.items.length;
97
- if (n === 0) return state;
98
- const selected = (state.selected + dir + n) % n;
99
- return { ...state, selected, confirmed: null, cancelled: false };
100
- }
101
-
102
- function confirm(state: SelectState): SelectState {
103
- const item = state.items[state.selected];
104
- if (item === undefined) return state;
105
- return { ...state, confirmed: item, cancelled: false };
106
- }
107
-
108
- interface Window {
109
- readonly start: number;
110
- readonly end: number;
111
- }
112
-
113
- function visibleWindow(state: SelectState): Window {
114
- const n = state.items.length;
115
- const cap = state.maxVisible;
116
- if (n <= cap) return { start: 0, end: n };
117
- const start = Math.max(
118
- 0,
119
- Math.min(state.selected - Math.floor(cap / 2), n - cap),
120
- );
121
- return { start, end: Math.min(start + cap, n) };
122
- }
123
-
124
- /**
125
- * Truncate on display columns. Slicing with s.length would cut a CJK
126
- * label in half and the next cell would paint garbage.
127
- */
128
- function truncate(s: string, width: number): string {
129
- if (width <= 0) return "";
130
- if (displayWidth(s) <= width) return s;
131
- let out = "";
132
- let w = 0;
133
- for (const ch of s) {
134
- const cw = displayWidth(ch);
135
- if (w + cw > width) break;
136
- out += ch;
137
- w += cw;
138
- }
139
- return out;
140
- }
141
-
142
- export function renderSelect(state: SelectState, width: number, style?: Style): string[] {
143
- if (state.items.length === 0) return [state.emptyMessage];
144
-
145
- const { start, end } = visibleWindow(state);
146
- const lines: string[] = [];
147
- for (let i = start; i < end; i++) {
148
- const item = state.items[i];
149
- if (item === undefined) continue;
150
- const selected = i === state.selected;
151
- const prefix = selected ? "→ " : " ";
152
- const budget = width - displayWidth(prefix);
153
- let body = item.label;
154
- const desc = item.description;
155
- if (desc !== undefined && desc.length > 0) body = `${item.label} ${desc}`;
156
- const line = prefix + truncate(body, budget);
157
- lines.push(
158
- selected && style !== undefined ? paint(line, { ...PLAIN_ATTRS, selected: true }, style) : line,
159
- );
160
- }
161
-
162
- const hidden = state.items.length - (end - start);
163
- // Card copy is Chinese on purpose: this is the one user-visible string
164
- // the completion definition named. English would silently relax it.
165
- if (hidden > 0) lines.push(`还有 ${hidden} 条`);
166
- return lines;
167
- }
168
-
169
- export class SelectList implements Component {
170
- private current: SelectState;
171
- private style: Style | undefined;
172
- private items: readonly SelectItem[];
173
- private readonly options: CreateSelectOptions;
174
- private readonly search = new Input({ prompt: "Search: " });
175
- private list: PiSelectList;
176
- onInput: ((data: string) => void) | undefined;
177
-
178
- constructor(items: readonly SelectItem[], opts: CreateSelectOptions = {}) {
179
- this.current = createSelect(items, opts);
180
- this.items = items;
181
- this.options = opts;
182
- this.style = opts.style;
183
- this.list = this.makeList();
184
- this.list.setSelectedIndex(Math.max(0, items.findIndex((item) => item.value === opts.selectedValue)));
185
- }
186
-
187
- get state(): SelectState {
188
- const selected = this.list.getSelectedItem();
189
- return { ...this.current, selected: Math.max(0, this.current.items.findIndex((item) => item.value === selected?.value)) };
190
- }
191
-
192
- applyKey(key: Key): SelectState {
193
- return this.acceptInput(encodeKey(key));
194
- }
195
-
196
- get focused(): boolean { return this.search.focused; }
197
- get query(): string { return this.search.getValue(); }
198
- set focused(value: boolean) { this.search.focused = value; }
199
- invalidate(): void { this.search.invalidate(); this.list.invalidate(); }
200
- handleInput(data: string): void { if (this.onInput) this.onInput(data); else this.acceptInput(data); }
201
-
202
- setStyle(style: Style): void {
203
- if (this.style === style) return;
204
- const selected = this.state.selected;
205
- this.style = style;
206
- this.list = this.makeList();
207
- this.list.setSelectedIndex(selected);
208
- }
209
-
210
- setItems(items: readonly SelectItem[]): void {
211
- const selected = this.state.items[this.state.selected]?.value;
212
- this.items = items;
213
- const filtered = this.filter(items, this.search.getValue());
214
- this.current = { ...this.current, items: filtered, selected: Math.max(0, filtered.findIndex(item => item.value === selected)) };
215
- this.list = this.makeList();
216
- this.list.setSelectedIndex(this.current.selected);
217
- }
218
-
219
- acceptInput(data: string): SelectState {
220
- this.current = { ...this.current, confirmed: null, cancelled: false };
221
- const kb = getKeybindings();
222
- if ((["tui.select.up", "tui.select.down", "tui.select.confirm", "tui.select.cancel"] as const).some(key => kb.matches(data, key))) this.list.handleInput(data);
223
- else if (kb.matches(data, "tui.select.pageUp") || kb.matches(data, "tui.select.pageDown")) {
224
- this.list.setSelectedIndex(this.state.selected + (kb.matches(data, "tui.select.pageUp") ? -1 : 1) * this.current.maxVisible);
225
- } else if (this.options.search !== false) {
226
- const before = this.search.getValue();
227
- this.search.handleInput(data);
228
- if (before === this.search.getValue()) return this.state;
229
- const filtered = this.filter(this.items, this.search.getValue());
230
- this.current = { ...this.current, items: filtered, selected: 0 };
231
- this.list = this.makeList();
232
- }
233
- return this.state;
234
- }
235
-
236
- private makeList(): PiSelectList {
237
- const selected = (text: string) => this.style ? paint(text, { ...PLAIN_ATTRS, selected: true }, this.style) : text;
238
- const dim = (text: string) => this.style ? paint(text, { ...PLAIN_ATTRS, dim: true }, this.style) : text;
239
- const list = new PiSelectList([...this.current.items], this.current.maxVisible, {
240
- selectedPrefix: selected, selectedText: selected, description: dim, scrollInfo: dim, noMatch: dim,
241
- });
242
- list.onSelect = (item) => { this.current = { ...this.current, confirmed: item }; };
243
- list.onCancel = () => { this.current = { ...this.current, cancelled: true }; };
244
- return list;
245
- }
246
- private filter(items: readonly SelectItem[], query: string): readonly SelectItem[] {
247
- return this.options.filter?.(items, query) ?? piFuzzyFilter([...items], query, item => `${item.label} ${item.description ?? ""}`);
248
- }
249
-
250
- render(width: number): string[] {
251
- if (width <= 0) return [""];
252
- const lines = [this.options.title ?? "Select"];
253
- if (this.options.search !== false) lines.push(...this.search.render(width));
254
- lines.push(...(this.current.items.length ? this.list.render(Math.max(4, width)) : [this.current.emptyMessage]));
255
- lines.push(this.options.hint ?? "↑↓ select · Enter confirm · Esc cancel");
256
- return lines.map((line) => truncateToWidth(line, width, ""));
257
- }
258
- }
@@ -1,44 +0,0 @@
1
- import { SettingsList, type SettingItem } from "../vendor/pi-tui/components/settings-list.ts";
2
- import { truncateToWidth } from "../vendor/pi-tui/utils.ts";
3
- import { PLAIN_ATTRS, paint, type Style } from "./style.ts";
4
-
5
- /** Pi owns settings navigation and cycling; apps persists the returned changes. */
6
- export class Settings {
7
- private readonly items: SettingItem[];
8
- private list: SettingsList;
9
- private style: Style;
10
- private changes: { id: string; value: string }[] = [];
11
- cancelled = false;
12
- onInput: ((data: string) => void) | undefined;
13
- constructor(items: SettingItem[], style: Style) {
14
- this.items = items;
15
- this.style = style;
16
- this.list = this.createList();
17
- }
18
- private createList(): SettingsList {
19
- return new SettingsList(this.items, 8, {
20
- label: (text, selected) => paint(text, { ...PLAIN_ATTRS, selected }, this.style),
21
- value: (text) => paint(text, { ...PLAIN_ATTRS, status: true }, this.style),
22
- description: (text) => paint(text, { ...PLAIN_ATTRS, dim: true }, this.style),
23
- cursor: "→ ", hint: (text) => paint(text, { ...PLAIN_ATTRS, dim: true }, this.style),
24
- }, (id, value) => { this.changes.push({ id, value }); }, () => { this.cancelled = true; }, { enableSearch: true });
25
- }
26
- setStyle(style: Style): void { this.style = style; }
27
- handleInput(data: string): void { if (this.onInput) this.onInput(data); else this.acceptInput(data); }
28
- acceptInput(data: string): { id: string; value: string }[] {
29
- this.changes = [];
30
- this.list.handleInput(data);
31
- return this.changes;
32
- }
33
- updateValue(id: string, value: string): void { this.list.updateValue(id, value); }
34
- updateValues(id: string, values: string[]): void {
35
- const item = this.items.find(item => item.id === id);
36
- if (item) item.values = values;
37
- }
38
- invalidate(): void { this.list.invalidate(); }
39
- render(width: number): string[] {
40
- if (width <= 0) return [""];
41
- return ["Settings · changes apply immediately", ...this.list.render(Math.max(8, width))]
42
- .map((line) => truncateToWidth(line, width, ""));
43
- }
44
- }
@@ -1,162 +0,0 @@
1
- /**
2
- * Footer: pi's two-line shape, xeno's cut budget.
3
- *
4
- * Line 1 is cwd (with git branch) and session name. Line 2 is usage
5
- * (↑ ↓ R W CH), cost, the context gauge, and the model. The gauge's
6
- * denominator is shouldCut's triggerTokens, not a model context window —
7
- * that number is the reason this project has a TUI at all.
8
- *
9
- * State in, string[] out. No terminal, no clock, no git.
10
- */
11
- import { isAbsolute, relative, resolve, sep } from "node:path";
12
- import type { Component } from "./text.ts";
13
- import { displayWidth } from "./width.ts";
14
- import { PLAIN_ATTRS, paint, type Style } from "./style.ts";
15
-
16
- export interface StatusState {
17
- cwd: string;
18
- branch: string | null;
19
- tokens: number;
20
- triggerTokens: number;
21
- model: string;
22
- thinking: string;
23
- /**
24
- * From shouldCut in apps, not inferred from the percentage. TUI must not
25
- * recompute minNodeWorkTokens -- that floor lives in core.
26
- */
27
- cut: boolean;
28
- reason: string;
29
- sessionName?: string;
30
- home?: string;
31
- inputTokens?: number;
32
- outputTokens?: number;
33
- cacheRead?: number;
34
- cacheWrite?: number;
35
- cacheHitRate?: number;
36
- cost?: number;
37
- /** Follow-ups waiting; painted in the stats line so it is not truncated off a padded row. */
38
- queued?: number;
39
- }
40
-
41
- /** Kept so /hotkeys and the queue count can still name the bindings. */
42
- export const STATUS_HINT = "escape interrupt · / commands · /quit · ctrl+o tools · ctrl+t think";
43
-
44
- const ELLIPSIS = "...";
45
-
46
- /**
47
- * Compact token counts the way pi's footer does. 30_000 → "30k" so the
48
- * spelling matches the number shouldCut compares.
49
- */
50
- export function formatTokens(count: number): string {
51
- if (count < 1000) return count.toString();
52
- if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
53
- if (count < 1000000) return `${Math.round(count / 1000)}k`;
54
- if (count < 10000000) return `${(count / 1000000).toFixed(1)}M`;
55
- return `${Math.round(count / 1000000)}M`;
56
- }
57
-
58
- export function formatCwdForFooter(cwd: string, home: string | undefined): string {
59
- if (!home) return cwd;
60
- const resolvedCwd = resolve(cwd);
61
- const resolvedHome = resolve(home);
62
- const relativeToHome = relative(resolvedHome, resolvedCwd);
63
- const isInsideHome =
64
- relativeToHome === "" ||
65
- (relativeToHome !== ".." && !relativeToHome.startsWith(`..${sep}`) && !isAbsolute(relativeToHome));
66
- if (!isInsideHome) return cwd;
67
- return relativeToHome === "" ? "~" : `~${sep}${relativeToHome}`;
68
- }
69
-
70
- export function renderStatus(state: StatusState, width: number, style?: Style): string[] {
71
- const w = Math.max(0, width);
72
- const pwd = pwdLine(state, w);
73
- const stats = statsLine(state, w);
74
- const lines = [pwd, stats];
75
- if (style === undefined) return lines;
76
- const attrs = { ...PLAIN_ATTRS, status: true, dim: true };
77
- return lines.map((line) => paint(line, attrs, style));
78
- }
79
-
80
- export function withStatus(body: Component, state: StatusState): Component {
81
- return {
82
- render(width: number): string[] {
83
- return [...body.render(width), ...renderStatus(state, width)];
84
- },
85
- };
86
- }
87
-
88
- function pwdLine(state: StatusState, width: number): string {
89
- let pwd = formatCwdForFooter(state.cwd, state.home);
90
- if (state.branch) pwd = `${pwd} (${state.branch})`;
91
- if (state.sessionName) pwd = `${pwd} • ${state.sessionName}`;
92
- return truncateToWidth(pwd, width);
93
- }
94
-
95
- function statsLine(state: StatusState, width: number): string {
96
- const parts: string[] = [];
97
- if (state.inputTokens) parts.push(`↑${formatTokens(state.inputTokens)}`);
98
- if (state.outputTokens) parts.push(`↓${formatTokens(state.outputTokens)}`);
99
- if (state.cacheRead) parts.push(`R${formatTokens(state.cacheRead)}`);
100
- if (state.cacheWrite) parts.push(`W${formatTokens(state.cacheWrite)}`);
101
- if (
102
- ((state.cacheRead ?? 0) > 0 || (state.cacheWrite ?? 0) > 0) &&
103
- state.cacheHitRate !== undefined
104
- ) {
105
- parts.push(`CH${state.cacheHitRate.toFixed(1)}%`);
106
- }
107
- if (state.cost !== undefined && state.cost > 0) {
108
- parts.push(`$${state.cost.toFixed(3)}`);
109
- }
110
- if (state.queued !== undefined && state.queued > 0) {
111
- parts.push(`${state.queued} queued`);
112
- }
113
-
114
- // Floor, not round: 29999/30000 must not read as 100% while shouldCut
115
- // is still below_trigger. Pi's shape is N%/Mk — the Mk is our trigger.
116
- const trigger = state.triggerTokens;
117
- const pct = trigger <= 0 ? 0 : Math.floor((100 * state.tokens) / trigger);
118
- parts.push(`${pct}%/${formatTokens(trigger)}`);
119
-
120
- let statsLeft = parts.join(" ");
121
- if (displayWidth(statsLeft) > width) {
122
- statsLeft = truncateToWidth(statsLeft, width);
123
- }
124
-
125
- let right = state.model;
126
- if (state.thinking.length > 0) {
127
- right = `${state.model} • ${state.thinking}`;
128
- }
129
- const leftW = displayWidth(statsLeft);
130
- const rightW = displayWidth(right);
131
- const minPadding = 2;
132
- if (leftW + minPadding + rightW <= width) {
133
- return statsLeft + " ".repeat(width - leftW - rightW) + right;
134
- }
135
- const availableForRight = width - leftW - minPadding;
136
- if (availableForRight > 0) {
137
- const truncatedRight = truncateToWidth(right, availableForRight);
138
- const pad = Math.max(0, width - leftW - displayWidth(truncatedRight));
139
- return statsLeft + " ".repeat(pad) + truncatedRight;
140
- }
141
- return statsLeft;
142
- }
143
-
144
- function truncateToWidth(s: string, width: number): string {
145
- if (width <= 0) return "";
146
- if (displayWidth(s) <= width) return s;
147
- const ew = displayWidth(ELLIPSIS);
148
- if (width <= ew) return takePrefix(s, width);
149
- return takePrefix(s, width - ew) + ELLIPSIS;
150
- }
151
-
152
- function takePrefix(s: string, width: number): string {
153
- let out = "";
154
- let w = 0;
155
- for (const ch of s) {
156
- const cw = displayWidth(ch);
157
- if (w + cw > width) break;
158
- out += ch;
159
- w += cw;
160
- }
161
- return out;
162
- }