@visual-z/xeno 0.2.1 → 0.4.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 (282) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +126 -34
  3. package/docs/app-server.md +84 -0
  4. package/docs/install.md +51 -13
  5. package/docs/providers.md +24 -3
  6. package/package.json +7 -5
  7. package/src/apps/app-server.ts +28 -0
  8. package/src/apps/approve.ts +3 -3
  9. package/src/apps/cli.ts +25 -10
  10. package/src/apps/commands.ts +45 -8
  11. package/src/apps/completions.ts +78 -22
  12. package/src/apps/control-transport.ts +89 -0
  13. package/src/apps/image-draft.ts +46 -0
  14. package/src/apps/model-catalog.ts +2 -1
  15. package/src/apps/project-trust.ts +2 -42
  16. package/src/apps/provider-connect.ts +346 -0
  17. package/src/apps/render.ts +22 -47
  18. package/src/apps/resources.ts +5 -2
  19. package/src/apps/shell.ts +333 -1542
  20. package/src/apps/startup-input.ts +45 -9
  21. package/src/apps/startup.ts +149 -94
  22. package/src/apps/ui/app.ts +1553 -0
  23. package/src/apps/ui/control-model.ts +26 -0
  24. package/src/apps/ui/extension-host.ts +194 -0
  25. package/src/apps/ui/image-cache.ts +78 -0
  26. package/src/apps/ui/palette.ts +88 -0
  27. package/src/apps/ui/project.ts +127 -0
  28. package/src/apps/ui/standalone.ts +146 -0
  29. package/src/apps/ui/status-model.ts +68 -0
  30. package/src/apps/ui/surface.ts +86 -0
  31. package/src/apps/ui/theme.ts +86 -0
  32. package/src/core/acceptance.ts +107 -0
  33. package/src/core/action-defaults.ts +47 -295
  34. package/src/core/benchmark.ts +2 -2
  35. package/src/core/branch-receipt.ts +24 -0
  36. package/src/core/checkpoints.ts +64 -0
  37. package/src/core/churn.ts +2 -0
  38. package/src/core/command-artifacts.ts +94 -0
  39. package/src/core/command-contract.ts +35 -0
  40. package/src/core/completion-action.ts +20 -0
  41. package/src/core/completion-review.ts +37 -21
  42. package/src/core/context-detail.ts +37 -0
  43. package/src/core/context-policy.ts +74 -1
  44. package/src/core/continuation.ts +17 -2
  45. package/src/core/control-protocol.ts +62 -0
  46. package/src/core/control-view.ts +42 -0
  47. package/src/core/derive.ts +29 -3
  48. package/src/core/draft-images.ts +56 -0
  49. package/src/core/environment.ts +145 -0
  50. package/src/core/evaluation-coverage.ts +74 -0
  51. package/src/core/event-validation.ts +81 -6
  52. package/src/core/events.ts +99 -3
  53. package/src/core/evidence-links.ts +7 -0
  54. package/src/core/evidence.ts +13 -3
  55. package/src/core/execution.ts +8 -2
  56. package/src/core/experiment.ts +133 -0
  57. package/src/core/export.ts +9 -0
  58. package/src/core/failure-recovery.ts +173 -0
  59. package/src/core/file-precondition.ts +72 -0
  60. package/src/core/handoff.ts +65 -4
  61. package/src/core/harness-diagnostics.ts +206 -0
  62. package/src/core/history-query.ts +38 -2
  63. package/src/core/history.ts +38 -1
  64. package/src/core/integration-health.ts +34 -0
  65. package/src/core/keymap.ts +8 -20
  66. package/src/core/model-picker.ts +64 -0
  67. package/src/core/native-eval.ts +25 -0
  68. package/src/core/note-guidance.ts +34 -0
  69. package/src/core/oauth-fields.ts +44 -0
  70. package/src/core/observation-view.ts +49 -8
  71. package/src/core/permissions.ts +24 -2
  72. package/src/core/process-receipt.ts +105 -0
  73. package/src/core/process-state.ts +37 -0
  74. package/src/core/program-status.ts +31 -0
  75. package/src/core/project.ts +70 -19
  76. package/src/core/prompt-policy.ts +37 -0
  77. package/src/core/query-eval-contract.ts +14 -0
  78. package/src/core/read-observation.ts +82 -0
  79. package/src/core/recovery-corpus.ts +17 -0
  80. package/src/core/recovery-evaluation.ts +60 -0
  81. package/src/core/recovery-sources.ts +74 -0
  82. package/src/core/request-progress.ts +87 -0
  83. package/src/core/request-state.ts +114 -0
  84. package/src/core/runtime-target.ts +94 -0
  85. package/src/core/search-observation.ts +97 -0
  86. package/src/core/session-tree.ts +147 -0
  87. package/src/core/settings.ts +33 -10
  88. package/src/core/skills.ts +44 -0
  89. package/src/core/sql-query.ts +211 -0
  90. package/src/core/theme.ts +36 -58
  91. package/src/core/tool-contract.ts +51 -2
  92. package/src/core/tool-discovery.ts +87 -0
  93. package/src/core/tool-failure.ts +60 -0
  94. package/src/core/tool-input.ts +6 -0
  95. package/src/core/tool-mode-protocol.ts +25 -0
  96. package/src/core/transcript.ts +19 -4
  97. package/src/core/trial-lifecycle.ts +14 -0
  98. package/src/core/tui-mode.ts +29 -0
  99. package/src/core/update-check.ts +97 -0
  100. package/src/core/version.ts +55 -0
  101. package/src/core/work-state.ts +49 -3
  102. package/src/core/working-set.ts +91 -0
  103. package/src/runtime/auth.ts +11 -4
  104. package/src/runtime/branch-jobs.ts +36 -0
  105. package/src/runtime/catalog.ts +13 -3
  106. package/src/runtime/clipboard.ts +15 -4
  107. package/src/runtime/command-artifacts.ts +64 -0
  108. package/src/runtime/control-connection.ts +104 -0
  109. package/src/runtime/environment-tools.ts +60 -0
  110. package/src/runtime/evidence-notes.ts +4 -2
  111. package/src/runtime/exec-sessions.ts +206 -61
  112. package/src/runtime/execution-control.ts +1 -0
  113. package/src/runtime/extension-ui.ts +22 -30
  114. package/src/runtime/extensions.ts +95 -16
  115. package/src/runtime/harness-comparison.ts +27 -7
  116. package/src/runtime/harness-eval.ts +462 -0
  117. package/src/runtime/history-cache.ts +57 -0
  118. package/src/runtime/history-tools.ts +62 -12
  119. package/src/runtime/integrations.ts +210 -0
  120. package/src/runtime/loop.ts +1088 -282
  121. package/src/runtime/model-discovery.ts +208 -0
  122. package/src/runtime/model.ts +9 -0
  123. package/src/runtime/native-eval-fixtures.ts +33 -0
  124. package/src/runtime/native-eval.ts +146 -0
  125. package/src/runtime/oauth.ts +80 -3
  126. package/src/runtime/pi-provider.ts +169 -12
  127. package/src/runtime/program-status.ts +38 -0
  128. package/src/runtime/provider-connect.ts +109 -0
  129. package/src/runtime/provider-presets.ts +87 -0
  130. package/src/runtime/provider-request.ts +13 -2
  131. package/src/runtime/provider.ts +23 -9
  132. package/src/runtime/query-eval-verifier.ts +28 -0
  133. package/src/runtime/recovery-coverage.ts +71 -0
  134. package/src/runtime/recovery-evidence.ts +76 -0
  135. package/src/runtime/recovery-fixtures.ts +116 -0
  136. package/src/runtime/runtime-target.ts +149 -0
  137. package/src/runtime/session-control.ts +198 -0
  138. package/src/runtime/tool-broker.ts +137 -0
  139. package/src/runtime/tool-discovery.ts +57 -0
  140. package/src/runtime/tool-mode-eval.ts +146 -0
  141. package/src/runtime/tool-queue.ts +77 -0
  142. package/src/runtime/tools.ts +628 -183
  143. package/src/runtime/trial-control.ts +45 -0
  144. package/src/runtime/update-check.ts +47 -0
  145. package/src/runtime/window-trial.ts +2 -1
  146. package/src/runtime/working-notes.ts +33 -3
  147. package/src/runtime/workspace-lifecycle.ts +97 -0
  148. package/src/store/acceptance.ts +15 -0
  149. package/src/store/checkpoint-history.ts +43 -0
  150. package/src/store/command-artifacts.ts +96 -0
  151. package/src/store/command.ts +65 -37
  152. package/src/store/file-checkpoints.ts +104 -0
  153. package/src/store/file-index.ts +13 -1
  154. package/src/store/git-checkpoints.ts +67 -0
  155. package/src/store/observations.ts +61 -20
  156. package/src/store/process-receipts.ts +34 -0
  157. package/src/store/query-source.ts +13 -8
  158. package/src/store/recovery.ts +2 -3
  159. package/src/store/replay.ts +16 -4
  160. package/src/store/runtime-target.ts +64 -0
  161. package/src/store/search.ts +171 -132
  162. package/src/store/session-archive.ts +4 -0
  163. package/src/store/source-cancellation.ts +6 -0
  164. package/src/store/update-cache.ts +36 -0
  165. package/src/store/workspace-observation.ts +113 -0
  166. package/src/tui/ansi-spans.ts +126 -0
  167. package/src/tui/style.ts +19 -16
  168. package/src/tui/ui/atomic-draft.ts +121 -0
  169. package/src/tui/ui/canvas.ts +96 -0
  170. package/src/tui/ui/colors.ts +70 -0
  171. package/src/tui/ui/composer.ts +200 -0
  172. package/src/tui/ui/control-panel.ts +23 -0
  173. package/src/tui/ui/dialog.ts +379 -0
  174. package/src/tui/ui/draft.ts +23 -0
  175. package/src/tui/ui/edge.ts +186 -0
  176. package/src/tui/ui/image-size.ts +40 -0
  177. package/src/tui/ui/ink.ts +210 -0
  178. package/src/tui/ui/items.ts +40 -0
  179. package/src/tui/ui/keys.ts +40 -0
  180. package/src/tui/ui/layout.ts +74 -0
  181. package/src/tui/ui/list-model.ts +86 -0
  182. package/src/tui/ui/list-view.ts +172 -0
  183. package/src/tui/ui/mascot.ts +67 -0
  184. package/src/tui/ui/messages.ts +518 -0
  185. package/src/tui/ui/motion.ts +28 -0
  186. package/src/tui/ui/paths.ts +10 -0
  187. package/src/tui/ui/pictures.ts +78 -0
  188. package/src/tui/ui/screen.ts +421 -0
  189. package/src/tui/ui/status.ts +279 -0
  190. package/src/tui/ui/styled.ts +85 -0
  191. package/src/tui/ui/testing.ts +5 -0
  192. package/src/tui/ui/tool-card.ts +150 -0
  193. package/src/apps/extension-ui.ts +0 -330
  194. package/src/apps/resource-browser.ts +0 -75
  195. package/src/apps/session-browser.ts +0 -113
  196. package/src/apps/session-picker.ts +0 -55
  197. package/src/tui/ansi.ts +0 -67
  198. package/src/tui/autocomplete.ts +0 -29
  199. package/src/tui/chat-viewport.ts +0 -47
  200. package/src/tui/complete.ts +0 -87
  201. package/src/tui/editor.ts +0 -940
  202. package/src/tui/host.ts +0 -264
  203. package/src/tui/input.ts +0 -75
  204. package/src/tui/keys.ts +0 -378
  205. package/src/tui/markdown.ts +0 -17
  206. package/src/tui/render.ts +0 -153
  207. package/src/tui/select.ts +0 -258
  208. package/src/tui/settings.ts +0 -44
  209. package/src/tui/statusline.ts +0 -162
  210. package/src/tui/system-theme.ts +0 -700
  211. package/src/tui/terminal.ts +0 -30
  212. package/src/tui/text.ts +0 -52
  213. package/src/tui/toolview.ts +0 -386
  214. package/src/tui/transcript.ts +0 -123
  215. package/src/tui/tree.ts +0 -471
  216. package/src/vendor/pi-tui/LICENSE.upstream +0 -21
  217. package/src/vendor/pi-tui/UPSTREAM.json +0 -50
  218. package/src/vendor/pi-tui/VENDOR.md +0 -25
  219. package/src/vendor/pi-tui/alt-screen-search.ts +0 -327
  220. package/src/vendor/pi-tui/autocomplete.ts +0 -860
  221. package/src/vendor/pi-tui/colors.ts +0 -367
  222. package/src/vendor/pi-tui/components/alt-screen-flash.ts +0 -51
  223. package/src/vendor/pi-tui/components/box.ts +0 -166
  224. package/src/vendor/pi-tui/components/cancellable-loader.ts +0 -40
  225. package/src/vendor/pi-tui/components/editor.ts +0 -2472
  226. package/src/vendor/pi-tui/components/h-stack.ts +0 -44
  227. package/src/vendor/pi-tui/components/image.ts +0 -127
  228. package/src/vendor/pi-tui/components/input.ts +0 -494
  229. package/src/vendor/pi-tui/components/loader.ts +0 -101
  230. package/src/vendor/pi-tui/components/markdown.ts +0 -1021
  231. package/src/vendor/pi-tui/components/mouse-region.ts +0 -33
  232. package/src/vendor/pi-tui/components/scroll-view.ts +0 -224
  233. package/src/vendor/pi-tui/components/select-list.ts +0 -273
  234. package/src/vendor/pi-tui/components/settings-list.ts +0 -328
  235. package/src/vendor/pi-tui/components/spacer.ts +0 -28
  236. package/src/vendor/pi-tui/components/stack.ts +0 -154
  237. package/src/vendor/pi-tui/components/text.ts +0 -107
  238. package/src/vendor/pi-tui/components/truncated-text.ts +0 -65
  239. package/src/vendor/pi-tui/components/v-stack.ts +0 -33
  240. package/src/vendor/pi-tui/editor-component.ts +0 -74
  241. package/src/vendor/pi-tui/fuzzy.ts +0 -138
  242. package/src/vendor/pi-tui/index.ts +0 -186
  243. package/src/vendor/pi-tui/keybindings.ts +0 -320
  244. package/src/vendor/pi-tui/keys.ts +0 -1401
  245. package/src/vendor/pi-tui/kill-ring.ts +0 -46
  246. package/src/vendor/pi-tui/latex.ts +0 -1506
  247. package/src/vendor/pi-tui/layout-node.ts +0 -51
  248. package/src/vendor/pi-tui/layout.ts +0 -449
  249. package/src/vendor/pi-tui/native/UPSTREAM.json +0 -23
  250. package/src/vendor/pi-tui/native/clipboard.h +0 -208
  251. package/src/vendor/pi-tui/native/darwin/README.md +0 -22
  252. package/src/vendor/pi-tui/native/darwin/build.sh +0 -69
  253. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  254. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  255. package/src/vendor/pi-tui/native/darwin/src/darwin-platform.m +0 -132
  256. package/src/vendor/pi-tui/native/linux/README.md +0 -23
  257. package/src/vendor/pi-tui/native/linux/build.sh +0 -65
  258. package/src/vendor/pi-tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  259. package/src/vendor/pi-tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  260. package/src/vendor/pi-tui/native/linux/src/clipboard-worker.h +0 -70
  261. package/src/vendor/pi-tui/native/linux/src/linux-platform-x11.c +0 -274
  262. package/src/vendor/pi-tui/native/napi.h +0 -91
  263. package/src/vendor/pi-tui/native/win32/README.md +0 -31
  264. package/src/vendor/pi-tui/native/win32/build.mjs +0 -232
  265. package/src/vendor/pi-tui/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  266. package/src/vendor/pi-tui/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  267. package/src/vendor/pi-tui/native/win32/src/win32-platform.c +0 -241
  268. package/src/vendor/pi-tui/native-modifiers.ts +0 -13
  269. package/src/vendor/pi-tui/native-module-path.ts +0 -31
  270. package/src/vendor/pi-tui/native-platform.ts +0 -65
  271. package/src/vendor/pi-tui/oklab.ts +0 -233
  272. package/src/vendor/pi-tui/stdin-buffer.ts +0 -444
  273. package/src/vendor/pi-tui/terminal-colors.ts +0 -91
  274. package/src/vendor/pi-tui/terminal-image.ts +0 -731
  275. package/src/vendor/pi-tui/terminal.ts +0 -554
  276. package/src/vendor/pi-tui/tui-alt-screen.ts +0 -1750
  277. package/src/vendor/pi-tui/tui-main-screen.ts +0 -655
  278. package/src/vendor/pi-tui/tui.ts +0 -1493
  279. package/src/vendor/pi-tui/undo-stack.ts +0 -28
  280. package/src/vendor/pi-tui/utils.ts +0 -1385
  281. package/src/vendor/pi-tui/wheel-scroll.ts +0 -82
  282. 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
- }