@visual-z/xeno 0.2.0 → 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 (259) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +106 -26
  3. package/docs/install.md +52 -9
  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/cli.ts +10 -11
  8. package/src/apps/commands.ts +38 -5
  9. package/src/apps/completions.ts +73 -19
  10. package/src/apps/image-draft.ts +46 -0
  11. package/src/apps/project-trust.ts +2 -42
  12. package/src/apps/provider-connect.ts +346 -0
  13. package/src/apps/render.ts +22 -47
  14. package/src/apps/resources.ts +5 -2
  15. package/src/apps/shell.ts +298 -1526
  16. package/src/apps/startup-input.ts +130 -0
  17. package/src/apps/startup.ts +143 -115
  18. package/src/apps/ui/app.ts +1383 -0
  19. package/src/apps/ui/extension-host.ts +194 -0
  20. package/src/apps/ui/image-cache.ts +78 -0
  21. package/src/apps/ui/palette.ts +88 -0
  22. package/src/apps/ui/project.ts +127 -0
  23. package/src/apps/ui/standalone.ts +146 -0
  24. package/src/apps/ui/status-model.ts +68 -0
  25. package/src/apps/ui/surface.ts +84 -0
  26. package/src/apps/ui/theme.ts +86 -0
  27. package/src/core/acceptance.ts +107 -0
  28. package/src/core/action-defaults.ts +47 -295
  29. package/src/core/benchmark.ts +2 -2
  30. package/src/core/branch-receipt.ts +24 -0
  31. package/src/core/checkpoints.ts +64 -0
  32. package/src/core/churn.ts +2 -0
  33. package/src/core/command-artifacts.ts +94 -0
  34. package/src/core/command-contract.ts +35 -0
  35. package/src/core/completion-review.ts +21 -5
  36. package/src/core/context-detail.ts +37 -0
  37. package/src/core/context-policy.ts +74 -1
  38. package/src/core/continuation.ts +17 -2
  39. package/src/core/derive.ts +29 -3
  40. package/src/core/draft-images.ts +56 -0
  41. package/src/core/environment.ts +145 -0
  42. package/src/core/evaluation-coverage.ts +74 -0
  43. package/src/core/event-validation.ts +80 -5
  44. package/src/core/events.ts +97 -3
  45. package/src/core/evidence-links.ts +7 -0
  46. package/src/core/evidence.ts +13 -3
  47. package/src/core/execution.ts +8 -2
  48. package/src/core/experiment.ts +133 -0
  49. package/src/core/export.ts +9 -0
  50. package/src/core/failure-recovery.ts +173 -0
  51. package/src/core/file-precondition.ts +72 -0
  52. package/src/core/handoff.ts +65 -4
  53. package/src/core/history-query.ts +38 -2
  54. package/src/core/history.ts +38 -1
  55. package/src/core/integration-health.ts +34 -0
  56. package/src/core/keymap.ts +8 -20
  57. package/src/core/model-picker.ts +64 -0
  58. package/src/core/native-eval.ts +25 -0
  59. package/src/core/note-guidance.ts +34 -0
  60. package/src/core/observation-view.ts +49 -8
  61. package/src/core/permissions.ts +24 -2
  62. package/src/core/process-receipt.ts +105 -0
  63. package/src/core/process-state.ts +37 -0
  64. package/src/core/project.ts +45 -18
  65. package/src/core/prompt-policy.ts +37 -0
  66. package/src/core/query-eval-contract.ts +14 -0
  67. package/src/core/read-observation.ts +82 -0
  68. package/src/core/recovery-corpus.ts +17 -0
  69. package/src/core/recovery-evaluation.ts +60 -0
  70. package/src/core/recovery-sources.ts +74 -0
  71. package/src/core/runtime-target.ts +94 -0
  72. package/src/core/search-observation.ts +97 -0
  73. package/src/core/session-tree.ts +147 -0
  74. package/src/core/settings.ts +26 -10
  75. package/src/core/skills.ts +44 -0
  76. package/src/core/sql-query.ts +211 -0
  77. package/src/core/theme.ts +36 -58
  78. package/src/core/tool-contract.ts +51 -2
  79. package/src/core/tool-discovery.ts +87 -0
  80. package/src/core/tool-failure.ts +60 -0
  81. package/src/core/tool-mode-protocol.ts +25 -0
  82. package/src/core/transcript.ts +19 -4
  83. package/src/core/trial-lifecycle.ts +14 -0
  84. package/src/core/tui-mode.ts +29 -0
  85. package/src/core/update-check.ts +97 -0
  86. package/src/core/version.ts +55 -0
  87. package/src/core/work-state.ts +49 -3
  88. package/src/core/working-set.ts +91 -0
  89. package/src/runtime/auth.ts +2 -2
  90. package/src/runtime/branch-jobs.ts +36 -0
  91. package/src/runtime/clipboard.ts +15 -4
  92. package/src/runtime/command-artifacts.ts +64 -0
  93. package/src/runtime/environment-tools.ts +60 -0
  94. package/src/runtime/evidence-notes.ts +4 -2
  95. package/src/runtime/exec-sessions.ts +206 -61
  96. package/src/runtime/execution-control.ts +1 -0
  97. package/src/runtime/extension-ui.ts +22 -30
  98. package/src/runtime/extensions.ts +95 -16
  99. package/src/runtime/harness-comparison.ts +27 -7
  100. package/src/runtime/harness-eval.ts +460 -0
  101. package/src/runtime/history-cache.ts +57 -0
  102. package/src/runtime/history-tools.ts +62 -12
  103. package/src/runtime/integrations.ts +210 -0
  104. package/src/runtime/loop.ts +1021 -268
  105. package/src/runtime/model-discovery.ts +208 -0
  106. package/src/runtime/native-eval-fixtures.ts +33 -0
  107. package/src/runtime/native-eval.ts +143 -0
  108. package/src/runtime/oauth.ts +1 -1
  109. package/src/runtime/provider-connect.ts +109 -0
  110. package/src/runtime/provider-presets.ts +87 -0
  111. package/src/runtime/query-eval-verifier.ts +28 -0
  112. package/src/runtime/recovery-coverage.ts +71 -0
  113. package/src/runtime/recovery-evidence.ts +76 -0
  114. package/src/runtime/recovery-fixtures.ts +116 -0
  115. package/src/runtime/runtime-target.ts +149 -0
  116. package/src/runtime/tool-broker.ts +137 -0
  117. package/src/runtime/tool-discovery.ts +57 -0
  118. package/src/runtime/tool-mode-eval.ts +146 -0
  119. package/src/runtime/tool-queue.ts +77 -0
  120. package/src/runtime/tools.ts +628 -183
  121. package/src/runtime/trial-control.ts +45 -0
  122. package/src/runtime/update-check.ts +47 -0
  123. package/src/runtime/window-trial.ts +2 -1
  124. package/src/runtime/working-notes.ts +33 -3
  125. package/src/runtime/workspace-lifecycle.ts +97 -0
  126. package/src/store/acceptance.ts +15 -0
  127. package/src/store/checkpoint-history.ts +43 -0
  128. package/src/store/command-artifacts.ts +96 -0
  129. package/src/store/command.ts +65 -37
  130. package/src/store/file-checkpoints.ts +104 -0
  131. package/src/store/file-index.ts +13 -1
  132. package/src/store/git-checkpoints.ts +67 -0
  133. package/src/store/observations.ts +61 -20
  134. package/src/store/process-receipts.ts +34 -0
  135. package/src/store/query-source.ts +13 -8
  136. package/src/store/recovery.ts +2 -3
  137. package/src/store/replay.ts +16 -4
  138. package/src/store/runtime-target.ts +64 -0
  139. package/src/store/search.ts +171 -132
  140. package/src/store/session-archive.ts +4 -0
  141. package/src/store/source-cancellation.ts +6 -0
  142. package/src/store/update-cache.ts +36 -0
  143. package/src/store/workspace-observation.ts +113 -0
  144. package/src/tui/ansi-spans.ts +126 -0
  145. package/src/tui/style.ts +19 -16
  146. package/src/tui/ui/atomic-draft.ts +121 -0
  147. package/src/tui/ui/canvas.ts +96 -0
  148. package/src/tui/ui/colors.ts +70 -0
  149. package/src/tui/ui/composer.ts +199 -0
  150. package/src/tui/ui/dialog.ts +379 -0
  151. package/src/tui/ui/draft.ts +23 -0
  152. package/src/tui/ui/edge.ts +186 -0
  153. package/src/tui/ui/image-size.ts +40 -0
  154. package/src/tui/ui/ink.ts +210 -0
  155. package/src/tui/ui/items.ts +40 -0
  156. package/src/tui/ui/keys.ts +40 -0
  157. package/src/tui/ui/layout.ts +74 -0
  158. package/src/tui/ui/list-model.ts +86 -0
  159. package/src/tui/ui/list-view.ts +172 -0
  160. package/src/tui/ui/mascot.ts +67 -0
  161. package/src/tui/ui/messages.ts +518 -0
  162. package/src/tui/ui/motion.ts +28 -0
  163. package/src/tui/ui/paths.ts +10 -0
  164. package/src/tui/ui/pictures.ts +78 -0
  165. package/src/tui/ui/screen.ts +421 -0
  166. package/src/tui/ui/status.ts +271 -0
  167. package/src/tui/ui/styled.ts +85 -0
  168. package/src/tui/ui/testing.ts +5 -0
  169. package/src/tui/ui/tool-card.ts +150 -0
  170. package/src/apps/extension-ui.ts +0 -330
  171. package/src/apps/resource-browser.ts +0 -75
  172. package/src/apps/session-browser.ts +0 -113
  173. package/src/apps/session-picker.ts +0 -55
  174. package/src/tui/ansi.ts +0 -67
  175. package/src/tui/autocomplete.ts +0 -29
  176. package/src/tui/chat-viewport.ts +0 -47
  177. package/src/tui/complete.ts +0 -87
  178. package/src/tui/editor.ts +0 -940
  179. package/src/tui/host.ts +0 -264
  180. package/src/tui/input.ts +0 -75
  181. package/src/tui/keys.ts +0 -378
  182. package/src/tui/markdown.ts +0 -17
  183. package/src/tui/render.ts +0 -153
  184. package/src/tui/select.ts +0 -258
  185. package/src/tui/settings.ts +0 -44
  186. package/src/tui/statusline.ts +0 -162
  187. package/src/tui/system-theme.ts +0 -700
  188. package/src/tui/terminal.ts +0 -30
  189. package/src/tui/text.ts +0 -52
  190. package/src/tui/toolview.ts +0 -386
  191. package/src/tui/transcript.ts +0 -123
  192. package/src/tui/tree.ts +0 -471
  193. package/src/vendor/pi-tui/LICENSE.upstream +0 -21
  194. package/src/vendor/pi-tui/UPSTREAM.json +0 -50
  195. package/src/vendor/pi-tui/VENDOR.md +0 -25
  196. package/src/vendor/pi-tui/alt-screen-search.ts +0 -327
  197. package/src/vendor/pi-tui/autocomplete.ts +0 -860
  198. package/src/vendor/pi-tui/colors.ts +0 -367
  199. package/src/vendor/pi-tui/components/alt-screen-flash.ts +0 -51
  200. package/src/vendor/pi-tui/components/box.ts +0 -166
  201. package/src/vendor/pi-tui/components/cancellable-loader.ts +0 -40
  202. package/src/vendor/pi-tui/components/editor.ts +0 -2472
  203. package/src/vendor/pi-tui/components/h-stack.ts +0 -44
  204. package/src/vendor/pi-tui/components/image.ts +0 -127
  205. package/src/vendor/pi-tui/components/input.ts +0 -494
  206. package/src/vendor/pi-tui/components/loader.ts +0 -101
  207. package/src/vendor/pi-tui/components/markdown.ts +0 -1021
  208. package/src/vendor/pi-tui/components/mouse-region.ts +0 -33
  209. package/src/vendor/pi-tui/components/scroll-view.ts +0 -224
  210. package/src/vendor/pi-tui/components/select-list.ts +0 -273
  211. package/src/vendor/pi-tui/components/settings-list.ts +0 -328
  212. package/src/vendor/pi-tui/components/spacer.ts +0 -28
  213. package/src/vendor/pi-tui/components/stack.ts +0 -154
  214. package/src/vendor/pi-tui/components/text.ts +0 -107
  215. package/src/vendor/pi-tui/components/truncated-text.ts +0 -65
  216. package/src/vendor/pi-tui/components/v-stack.ts +0 -33
  217. package/src/vendor/pi-tui/editor-component.ts +0 -74
  218. package/src/vendor/pi-tui/fuzzy.ts +0 -138
  219. package/src/vendor/pi-tui/index.ts +0 -186
  220. package/src/vendor/pi-tui/keybindings.ts +0 -320
  221. package/src/vendor/pi-tui/keys.ts +0 -1401
  222. package/src/vendor/pi-tui/kill-ring.ts +0 -46
  223. package/src/vendor/pi-tui/latex.ts +0 -1506
  224. package/src/vendor/pi-tui/layout-node.ts +0 -51
  225. package/src/vendor/pi-tui/layout.ts +0 -449
  226. package/src/vendor/pi-tui/native/UPSTREAM.json +0 -23
  227. package/src/vendor/pi-tui/native/clipboard.h +0 -208
  228. package/src/vendor/pi-tui/native/darwin/README.md +0 -22
  229. package/src/vendor/pi-tui/native/darwin/build.sh +0 -69
  230. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  231. package/src/vendor/pi-tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  232. package/src/vendor/pi-tui/native/darwin/src/darwin-platform.m +0 -132
  233. package/src/vendor/pi-tui/native/linux/README.md +0 -23
  234. package/src/vendor/pi-tui/native/linux/build.sh +0 -65
  235. package/src/vendor/pi-tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  236. package/src/vendor/pi-tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  237. package/src/vendor/pi-tui/native/linux/src/clipboard-worker.h +0 -70
  238. package/src/vendor/pi-tui/native/linux/src/linux-platform-x11.c +0 -274
  239. package/src/vendor/pi-tui/native/napi.h +0 -91
  240. package/src/vendor/pi-tui/native/win32/README.md +0 -31
  241. package/src/vendor/pi-tui/native/win32/build.mjs +0 -232
  242. package/src/vendor/pi-tui/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  243. package/src/vendor/pi-tui/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  244. package/src/vendor/pi-tui/native/win32/src/win32-platform.c +0 -241
  245. package/src/vendor/pi-tui/native-modifiers.ts +0 -13
  246. package/src/vendor/pi-tui/native-module-path.ts +0 -31
  247. package/src/vendor/pi-tui/native-platform.ts +0 -65
  248. package/src/vendor/pi-tui/oklab.ts +0 -233
  249. package/src/vendor/pi-tui/stdin-buffer.ts +0 -444
  250. package/src/vendor/pi-tui/terminal-colors.ts +0 -91
  251. package/src/vendor/pi-tui/terminal-image.ts +0 -731
  252. package/src/vendor/pi-tui/terminal.ts +0 -554
  253. package/src/vendor/pi-tui/tui-alt-screen.ts +0 -1750
  254. package/src/vendor/pi-tui/tui-main-screen.ts +0 -655
  255. package/src/vendor/pi-tui/tui.ts +0 -1493
  256. package/src/vendor/pi-tui/undo-stack.ts +0 -28
  257. package/src/vendor/pi-tui/utils.ts +0 -1385
  258. package/src/vendor/pi-tui/wheel-scroll.ts +0 -82
  259. package/src/vendor/pi-tui/word-navigation.ts +0 -117
@@ -1,444 +0,0 @@
1
- /**
2
- * StdinBuffer buffers input and emits complete sequences.
3
- *
4
- * This is necessary because stdin data events can arrive in partial chunks,
5
- * especially for escape sequences like mouse events. Without buffering,
6
- * partial sequences can be misinterpreted as regular keypresses.
7
- *
8
- * For example, the mouse SGR sequence `\x1b[<35;20;5m` might arrive as:
9
- * - Event 1: `\x1b`
10
- * - Event 2: `[<35`
11
- * - Event 3: `;20;5m`
12
- *
13
- * The buffer accumulates these until a complete sequence is detected.
14
- * Call the `process()` method to feed input data.
15
- *
16
- * Based on code from OpenTUI (https://github.com/anomalyco/opentui)
17
- * MIT License - Copyright (c) 2025 opentui
18
- */
19
-
20
- import { EventEmitter } from "events";
21
-
22
- const ESC = "\x1b";
23
- const DEFAULT_SEQUENCE_TIMEOUT_MS = 50;
24
- const DEFAULT_ESCAPE_TIMEOUT_MS = 10;
25
- const BRACKETED_PASTE_START = "\x1b[200~";
26
- const BRACKETED_PASTE_END = "\x1b[201~";
27
-
28
- /**
29
- * Check if a string is a complete escape sequence or needs more data
30
- */
31
- function isCompleteSequence(data: string): "complete" | "incomplete" | "not-escape" {
32
- if (!data.startsWith(ESC)) {
33
- return "not-escape";
34
- }
35
-
36
- if (data.length === 1) {
37
- return "incomplete";
38
- }
39
-
40
- const afterEsc = data.slice(1);
41
-
42
- // CSI sequences: ESC [
43
- if (afterEsc.startsWith("[")) {
44
- // Check for old-style mouse sequence: ESC[M + 3 bytes
45
- if (afterEsc.startsWith("[M")) {
46
- // Old-style mouse needs ESC[M + 3 bytes = 6 total
47
- return data.length >= 6 ? "complete" : "incomplete";
48
- }
49
- return isCompleteCsiSequence(data);
50
- }
51
-
52
- // OSC sequences: ESC ]
53
- if (afterEsc.startsWith("]")) {
54
- return isCompleteOscSequence(data);
55
- }
56
-
57
- // DCS sequences: ESC P ... ESC \ (includes XTVersion responses)
58
- if (afterEsc.startsWith("P")) {
59
- return isCompleteDcsSequence(data);
60
- }
61
-
62
- // APC sequences: ESC _ ... ESC \ (includes Kitty graphics responses)
63
- if (afterEsc.startsWith("_")) {
64
- return isCompleteApcSequence(data);
65
- }
66
-
67
- // SS3 sequences: ESC O
68
- if (afterEsc.startsWith("O")) {
69
- // ESC O followed by a single character
70
- return afterEsc.length >= 2 ? "complete" : "incomplete";
71
- }
72
-
73
- // Meta key sequences: ESC followed by a single character
74
- if (afterEsc.length === 1) {
75
- return "complete";
76
- }
77
-
78
- // Unknown escape sequence - treat as complete
79
- return "complete";
80
- }
81
-
82
- /**
83
- * Check if CSI sequence is complete
84
- * CSI sequences: ESC [ ... followed by a final byte (0x40-0x7E)
85
- */
86
- function isCompleteCsiSequence(data: string): "complete" | "incomplete" {
87
- if (!data.startsWith(`${ESC}[`)) {
88
- return "complete";
89
- }
90
-
91
- // Need at least ESC [ and one more character
92
- if (data.length < 3) {
93
- return "incomplete";
94
- }
95
-
96
- const payload = data.slice(2);
97
-
98
- // CSI sequences end with a byte in the range 0x40-0x7E (@-~)
99
- // This includes all letters and several special characters
100
- const lastChar = payload[payload.length - 1];
101
- const lastCharCode = lastChar.charCodeAt(0);
102
-
103
- if (lastCharCode >= 0x40 && lastCharCode <= 0x7e) {
104
- // Special handling for SGR mouse sequences
105
- // Format: ESC[<B;X;Ym or ESC[<B;X;YM
106
- if (payload.startsWith("<")) {
107
- // Must have format: <digits;digits;digits[Mm]
108
- const mouseMatch = /^<\d+;\d+;\d+[Mm]$/.test(payload);
109
- if (mouseMatch) {
110
- return "complete";
111
- }
112
- // If it ends with M or m but doesn't match the pattern, still incomplete
113
- if (lastChar === "M" || lastChar === "m") {
114
- // Check if we have the right structure
115
- const parts = payload.slice(1, -1).split(";");
116
- if (parts.length === 3 && parts.every((p) => /^\d+$/.test(p))) {
117
- return "complete";
118
- }
119
- }
120
-
121
- return "incomplete";
122
- }
123
-
124
- return "complete";
125
- }
126
-
127
- return "incomplete";
128
- }
129
-
130
- /**
131
- * Check if OSC sequence is complete
132
- * OSC sequences: ESC ] ... ST (where ST is ESC \ or BEL)
133
- */
134
- function isCompleteOscSequence(data: string): "complete" | "incomplete" {
135
- if (!data.startsWith(`${ESC}]`)) {
136
- return "complete";
137
- }
138
-
139
- // OSC sequences end with ST (ESC \) or BEL (\x07)
140
- if (data.endsWith(`${ESC}\\`) || data.endsWith("\x07")) {
141
- return "complete";
142
- }
143
-
144
- return "incomplete";
145
- }
146
-
147
- /**
148
- * Check if DCS (Device Control String) sequence is complete
149
- * DCS sequences: ESC P ... ST (where ST is ESC \)
150
- * Used for XTVersion responses like ESC P >| ... ESC \
151
- */
152
- function isCompleteDcsSequence(data: string): "complete" | "incomplete" {
153
- if (!data.startsWith(`${ESC}P`)) {
154
- return "complete";
155
- }
156
-
157
- // DCS sequences end with ST (ESC \)
158
- if (data.endsWith(`${ESC}\\`)) {
159
- return "complete";
160
- }
161
-
162
- return "incomplete";
163
- }
164
-
165
- /**
166
- * Check if APC (Application Program Command) sequence is complete
167
- * APC sequences: ESC _ ... ST (where ST is ESC \)
168
- * Used for Kitty graphics responses like ESC _ G ... ESC \
169
- */
170
- function isCompleteApcSequence(data: string): "complete" | "incomplete" {
171
- if (!data.startsWith(`${ESC}_`)) {
172
- return "complete";
173
- }
174
-
175
- // APC sequences end with ST (ESC \)
176
- if (data.endsWith(`${ESC}\\`)) {
177
- return "complete";
178
- }
179
-
180
- return "incomplete";
181
- }
182
-
183
- /**
184
- * Split accumulated buffer into complete sequences
185
- */
186
- function parseUnmodifiedKittyPrintableCodepoint(sequence: string): number | undefined {
187
- const match = sequence.match(/^\x1b\[(\d+)(?::\d*)?(?::\d+)?u$/);
188
- if (!match) return undefined;
189
-
190
- const codepoint = parseInt(match[1]!, 10);
191
- return codepoint >= 32 ? codepoint : undefined;
192
- }
193
-
194
- function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } {
195
- const sequences: string[] = [];
196
- let pos = 0;
197
-
198
- while (pos < buffer.length) {
199
- const remaining = buffer.slice(pos);
200
-
201
- // Try to extract a sequence starting at this position
202
- if (remaining.startsWith(ESC)) {
203
- // Find the end of this escape sequence
204
- let seqEnd = 1;
205
- while (seqEnd <= remaining.length) {
206
- const candidate = remaining.slice(0, seqEnd);
207
- const status = isCompleteSequence(candidate);
208
-
209
- if (status === "complete") {
210
- // WezTerm with enable_kitty_keyboard sends the Escape key press as a
211
- // raw '\x1b' byte (simple text path in encode_kitty, ignoring
212
- // DISAMBIGUATE_ESCAPE_CODES) and the release as a full Kitty CSI-u
213
- // sequence. These arrive concatenated as '\x1b\x1b[27;...u'.
214
- // The buffer would normally treat '\x1b\x1b' as a complete meta-key
215
- // sequence (ESC + single char), leaving '[27;...u' to be typed as
216
- // plain text. If the character immediately following '\x1b\x1b'
217
- // would begin a new escape sequence, emit only the first ESC and
218
- // restart from the second.
219
- if (candidate === "\x1b\x1b") {
220
- const nextChar = remaining[seqEnd];
221
- if (
222
- nextChar === "[" || // CSI
223
- nextChar === "]" || // OSC
224
- nextChar === "O" || // SS3
225
- nextChar === "P" || // DCS
226
- nextChar === "_" // APC
227
- ) {
228
- sequences.push(ESC);
229
- pos += 1;
230
- break;
231
- }
232
- }
233
- sequences.push(candidate);
234
- pos += seqEnd;
235
- break;
236
- } else if (status === "incomplete") {
237
- seqEnd++;
238
- } else {
239
- // Should not happen when starting with ESC
240
- sequences.push(candidate);
241
- pos += seqEnd;
242
- break;
243
- }
244
- }
245
-
246
- if (seqEnd > remaining.length) {
247
- return { sequences, remainder: remaining };
248
- }
249
- } else {
250
- // Not an escape sequence - take a single character
251
- sequences.push(remaining[0]!);
252
- pos++;
253
- }
254
- }
255
-
256
- return { sequences, remainder: "" };
257
- }
258
-
259
- export type StdinBufferOptions = {
260
- /**
261
- * Maximum time to wait for an incomplete sequence such as CSI or mouse
262
- * (default: 50ms).
263
- */
264
- timeout?: number;
265
- /**
266
- * Maximum time to wait after a lone ESC before treating it as Escape
267
- * (default: 10ms). Increase for high-latency Alt+key input (SSH).
268
- */
269
- escapeTimeout?: number;
270
- };
271
-
272
- export type StdinBufferEventMap = {
273
- data: [string];
274
- paste: [string];
275
- };
276
-
277
- /**
278
- * Buffers stdin input and emits complete sequences via the 'data' event.
279
- * Handles partial escape sequences that arrive across multiple chunks.
280
- */
281
- export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
282
- private buffer: string = "";
283
- private timeout: ReturnType<typeof setTimeout> | null = null;
284
- private readonly timeoutMs: number;
285
- private readonly escapeTimeoutMs: number;
286
- private pasteMode: boolean = false;
287
- private pasteBuffer: string = "";
288
- private pendingKittyPrintableCodepoint: number | undefined;
289
-
290
- constructor(options: StdinBufferOptions = {}) {
291
- super();
292
- this.timeoutMs = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT_MS;
293
- this.escapeTimeoutMs = options.escapeTimeout ?? DEFAULT_ESCAPE_TIMEOUT_MS;
294
- }
295
-
296
- public process(data: string | Buffer): void {
297
- // Clear any pending timeout
298
- if (this.timeout) {
299
- clearTimeout(this.timeout);
300
- this.timeout = null;
301
- }
302
-
303
- // Handle high-byte conversion (for compatibility with parseKeypress)
304
- // If buffer has single byte > 127, convert to ESC + (byte - 128)
305
- let str: string;
306
- if (Buffer.isBuffer(data)) {
307
- if (data.length === 1 && data[0]! > 127) {
308
- const byte = data[0]! - 128;
309
- str = `\x1b${String.fromCharCode(byte)}`;
310
- } else {
311
- str = data.toString();
312
- }
313
- } else {
314
- str = data;
315
- }
316
-
317
- if (str.length === 0 && this.buffer.length === 0) {
318
- this.emitDataSequence("");
319
- return;
320
- }
321
-
322
- this.buffer += str;
323
-
324
- if (this.pasteMode) {
325
- this.pasteBuffer += this.buffer;
326
- this.buffer = "";
327
-
328
- const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
329
- if (endIndex !== -1) {
330
- const pastedContent = this.pasteBuffer.slice(0, endIndex);
331
- const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
332
-
333
- this.pasteMode = false;
334
- this.pasteBuffer = "";
335
- this.pendingKittyPrintableCodepoint = undefined;
336
-
337
- this.emit("paste", pastedContent);
338
-
339
- if (remaining.length > 0) {
340
- this.process(remaining);
341
- }
342
- }
343
- return;
344
- }
345
-
346
- const startIndex = this.buffer.indexOf(BRACKETED_PASTE_START);
347
- if (startIndex !== -1) {
348
- if (startIndex > 0) {
349
- const beforePaste = this.buffer.slice(0, startIndex);
350
- const result = extractCompleteSequences(beforePaste);
351
- for (const sequence of result.sequences) {
352
- this.emitDataSequence(sequence);
353
- }
354
- }
355
-
356
- this.pendingKittyPrintableCodepoint = undefined;
357
- this.buffer = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length);
358
- this.pasteMode = true;
359
- this.pasteBuffer = this.buffer;
360
- this.buffer = "";
361
-
362
- const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
363
- if (endIndex !== -1) {
364
- const pastedContent = this.pasteBuffer.slice(0, endIndex);
365
- const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
366
-
367
- this.pasteMode = false;
368
- this.pasteBuffer = "";
369
- this.pendingKittyPrintableCodepoint = undefined;
370
-
371
- this.emit("paste", pastedContent);
372
-
373
- if (remaining.length > 0) {
374
- this.process(remaining);
375
- }
376
- }
377
- return;
378
- }
379
-
380
- const result = extractCompleteSequences(this.buffer);
381
- this.buffer = result.remainder;
382
-
383
- for (const sequence of result.sequences) {
384
- this.emitDataSequence(sequence);
385
- }
386
-
387
- if (this.buffer.length > 0) {
388
- const timeoutMs = this.buffer === ESC ? this.escapeTimeoutMs : this.timeoutMs;
389
- this.timeout = setTimeout(() => {
390
- const flushed = this.flush();
391
-
392
- for (const sequence of flushed) {
393
- this.emitDataSequence(sequence);
394
- }
395
- }, timeoutMs);
396
- }
397
- }
398
-
399
- private emitDataSequence(sequence: string): void {
400
- const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : undefined;
401
- if (rawCodepoint !== undefined && rawCodepoint === this.pendingKittyPrintableCodepoint) {
402
- this.pendingKittyPrintableCodepoint = undefined;
403
- return;
404
- }
405
-
406
- this.pendingKittyPrintableCodepoint = parseUnmodifiedKittyPrintableCodepoint(sequence);
407
- this.emit("data", sequence);
408
- }
409
-
410
- flush(): string[] {
411
- if (this.timeout) {
412
- clearTimeout(this.timeout);
413
- this.timeout = null;
414
- }
415
-
416
- if (this.buffer.length === 0) {
417
- return [];
418
- }
419
-
420
- const sequences = [this.buffer];
421
- this.buffer = "";
422
- this.pendingKittyPrintableCodepoint = undefined;
423
- return sequences;
424
- }
425
-
426
- clear(): void {
427
- if (this.timeout) {
428
- clearTimeout(this.timeout);
429
- this.timeout = null;
430
- }
431
- this.buffer = "";
432
- this.pasteMode = false;
433
- this.pasteBuffer = "";
434
- this.pendingKittyPrintableCodepoint = undefined;
435
- }
436
-
437
- getBuffer(): string {
438
- return this.buffer;
439
- }
440
-
441
- destroy(): void {
442
- this.clear();
443
- }
444
- }
@@ -1,91 +0,0 @@
1
- export interface RgbColor {
2
- r: number;
3
- g: number;
4
- b: number;
5
- }
6
-
7
- export type TerminalColorScheme = "dark" | "light";
8
-
9
- /** Colors the terminal reports for its current theme. */
10
- export interface TerminalColors {
11
- /** Default foreground (OSC 10). */
12
- foreground?: RgbColor;
13
- /** Default background (OSC 11). */
14
- background?: RgbColor;
15
- /** ANSI colors 0-15 (OSC 4). Only set when the terminal reported all 16. */
16
- palette?: RgbColor[];
17
- }
18
-
19
- function hexToRgb(hex: string): RgbColor {
20
- const normalized = hex.startsWith("#") ? hex.slice(1) : hex;
21
- const r = parseInt(normalized.slice(0, 2), 16);
22
- const g = parseInt(normalized.slice(2, 4), 16);
23
- const b = parseInt(normalized.slice(4, 6), 16);
24
- return { r, g, b };
25
- }
26
-
27
- function parseOscHexChannel(channel: string): number | undefined {
28
- if (!/^[0-9a-f]+$/i.test(channel)) {
29
- return undefined;
30
- }
31
- const max = 16 ** channel.length - 1;
32
- if (max <= 0) {
33
- return undefined;
34
- }
35
- return Math.round((parseInt(channel, 16) / max) * 255);
36
- }
37
-
38
- /** What an OSC color reply reports: the default foreground (OSC 10), background (OSC 11), or a palette index (OSC 4). */
39
- export type OscColorTarget = "foreground" | "background" | number;
40
-
41
- const OSC_COLOR_RESPONSE_PATTERN = /^\x1b\](?:(1[01])|4;(\d{1,3}));([^\x07\x1b]*)(?:\x07|\x1b\\)$/i;
42
- const COLOR_SCHEME_REPORT_PATTERN = /^(?:\x1b\[\?997;(1|2)n)+$/;
43
-
44
- /**
45
- * Parse an OSC 10, 11, or 4 color reply. Returns undefined when `data` is not such a reply;
46
- * `rgb` is undefined when it is a reply with an unparseable color.
47
- */
48
- export function parseOscColorResponse(data: string): { target: OscColorTarget; rgb: RgbColor | undefined } | undefined {
49
- const match = data.match(OSC_COLOR_RESPONSE_PATTERN);
50
- if (!match) {
51
- return undefined;
52
- }
53
- const target: OscColorTarget =
54
- match[1] === "10" ? "foreground" : match[1] === "11" ? "background" : Number.parseInt(match[2], 10);
55
- return { target, rgb: parseOscColorValue(match[3]) };
56
- }
57
-
58
- function parseOscColorValue(rawValue: string): RgbColor | undefined {
59
- const value = rawValue.trim();
60
- if (value.startsWith("#")) {
61
- const hex = value.slice(1);
62
- if (/^[0-9a-f]{6}$/i.test(hex)) {
63
- return hexToRgb(value);
64
- }
65
- if (/^[0-9a-f]{12}$/i.test(hex)) {
66
- const r = parseOscHexChannel(hex.slice(0, 4));
67
- const g = parseOscHexChannel(hex.slice(4, 8));
68
- const b = parseOscHexChannel(hex.slice(8, 12));
69
- return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined;
70
- }
71
- return undefined;
72
- }
73
-
74
- const rgbValue = value.replace(/^rgba?:/i, "");
75
- const [red, green, blue] = rgbValue.split("/");
76
- if (red === undefined || green === undefined || blue === undefined) {
77
- return undefined;
78
- }
79
- const r = parseOscHexChannel(red);
80
- const g = parseOscHexChannel(green);
81
- const b = parseOscHexChannel(blue);
82
- return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined;
83
- }
84
-
85
- export function parseTerminalColorSchemeReport(data: string): TerminalColorScheme | undefined {
86
- const match = data.match(COLOR_SCHEME_REPORT_PATTERN);
87
- if (!match) {
88
- return undefined;
89
- }
90
- return match[1] === "2" ? "light" : "dark";
91
- }