@visual-z/xeno 0.1.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 (180) hide show
  1. package/README.md +498 -0
  2. package/docs/install.md +130 -0
  3. package/package.json +43 -0
  4. package/src/apps/approve.ts +49 -0
  5. package/src/apps/cli.ts +37 -0
  6. package/src/apps/commands.ts +226 -0
  7. package/src/apps/completions.ts +28 -0
  8. package/src/apps/input.ts +297 -0
  9. package/src/apps/render.ts +207 -0
  10. package/src/apps/shell.ts +2353 -0
  11. package/src/apps/startup.ts +1302 -0
  12. package/src/core/benchmark.ts +195 -0
  13. package/src/core/boundary.ts +174 -0
  14. package/src/core/churn.ts +59 -0
  15. package/src/core/completion-review.ts +66 -0
  16. package/src/core/context-policy.ts +60 -0
  17. package/src/core/context-window.ts +54 -0
  18. package/src/core/continuation.ts +114 -0
  19. package/src/core/cycle.ts +17 -0
  20. package/src/core/data-query.ts +201 -0
  21. package/src/core/deadline.ts +39 -0
  22. package/src/core/derive.ts +489 -0
  23. package/src/core/edit-plan.ts +200 -0
  24. package/src/core/environment.ts +29 -0
  25. package/src/core/event-validation.ts +99 -0
  26. package/src/core/events.ts +486 -0
  27. package/src/core/evidence-links.ts +71 -0
  28. package/src/core/evidence.ts +89 -0
  29. package/src/core/execution.ts +12 -0
  30. package/src/core/exhaustive.ts +18 -0
  31. package/src/core/experiment.ts +40 -0
  32. package/src/core/export.ts +169 -0
  33. package/src/core/file-references.ts +35 -0
  34. package/src/core/frontmatter.ts +213 -0
  35. package/src/core/handoff.ts +397 -0
  36. package/src/core/harness-comparison.ts +90 -0
  37. package/src/core/history-query.ts +103 -0
  38. package/src/core/history.ts +100 -0
  39. package/src/core/json-event.ts +135 -0
  40. package/src/core/json-pointer.ts +46 -0
  41. package/src/core/keymap.ts +241 -0
  42. package/src/core/observation-view.ts +87 -0
  43. package/src/core/outcome.ts +45 -0
  44. package/src/core/overflow.ts +59 -0
  45. package/src/core/permissions.ts +182 -0
  46. package/src/core/policy-evaluation.ts +55 -0
  47. package/src/core/progress.ts +101 -0
  48. package/src/core/project.ts +372 -0
  49. package/src/core/prompts.ts +83 -0
  50. package/src/core/queue.ts +60 -0
  51. package/src/core/read-bounds.ts +15 -0
  52. package/src/core/recall-analysis.ts +90 -0
  53. package/src/core/recall-trial.ts +153 -0
  54. package/src/core/response-recovery.ts +46 -0
  55. package/src/core/scheduling.ts +8 -0
  56. package/src/core/settings.ts +174 -0
  57. package/src/core/skills.ts +121 -0
  58. package/src/core/stall.ts +48 -0
  59. package/src/core/text-normalize.ts +81 -0
  60. package/src/core/theme.ts +179 -0
  61. package/src/core/tokens.ts +149 -0
  62. package/src/core/tool-contract.ts +102 -0
  63. package/src/core/tool-input.ts +38 -0
  64. package/src/core/transcript.ts +60 -0
  65. package/src/core/window-trial.ts +65 -0
  66. package/src/core/work-state.ts +103 -0
  67. package/src/core/yaml.ts +282 -0
  68. package/src/index.ts +10 -0
  69. package/src/runtime/auth.ts +255 -0
  70. package/src/runtime/benchmark.ts +187 -0
  71. package/src/runtime/catalog.ts +365 -0
  72. package/src/runtime/evidence-notes.ts +33 -0
  73. package/src/runtime/exec-sessions.ts +214 -0
  74. package/src/runtime/execution-control.ts +39 -0
  75. package/src/runtime/harness-comparison.ts +356 -0
  76. package/src/runtime/history-tools.ts +108 -0
  77. package/src/runtime/loop.ts +1983 -0
  78. package/src/runtime/model.ts +614 -0
  79. package/src/runtime/models-fetch.ts +155 -0
  80. package/src/runtime/oauth-device.ts +86 -0
  81. package/src/runtime/oauth.ts +759 -0
  82. package/src/runtime/openai-responses.ts +286 -0
  83. package/src/runtime/openai.ts +675 -0
  84. package/src/runtime/provider.ts +498 -0
  85. package/src/runtime/recall-trial.ts +226 -0
  86. package/src/runtime/resume.ts +66 -0
  87. package/src/runtime/retry.ts +297 -0
  88. package/src/runtime/tools.ts +1193 -0
  89. package/src/runtime/usage.ts +225 -0
  90. package/src/runtime/window-trial.ts +140 -0
  91. package/src/runtime/working-notes.ts +30 -0
  92. package/src/store/atomic.ts +31 -0
  93. package/src/store/auth-store.ts +50 -0
  94. package/src/store/blobs.ts +142 -0
  95. package/src/store/command.ts +75 -0
  96. package/src/store/config.ts +69 -0
  97. package/src/store/context-files.ts +356 -0
  98. package/src/store/file-index.ts +81 -0
  99. package/src/store/git-branch.ts +35 -0
  100. package/src/store/history-search.ts +19 -0
  101. package/src/store/log.ts +642 -0
  102. package/src/store/models.ts +41 -0
  103. package/src/store/observations.ts +181 -0
  104. package/src/store/query-source.ts +44 -0
  105. package/src/store/recovery.ts +39 -0
  106. package/src/store/replay.ts +63 -0
  107. package/src/store/resources.ts +361 -0
  108. package/src/store/restore.ts +302 -0
  109. package/src/store/search.ts +644 -0
  110. package/src/store/serial.ts +13 -0
  111. package/src/store/sessions.ts +463 -0
  112. package/src/store/trust.ts +69 -0
  113. package/src/store/workspace.ts +256 -0
  114. package/src/store/worktree.ts +233 -0
  115. package/src/tui/ansi.ts +67 -0
  116. package/src/tui/autocomplete.ts +29 -0
  117. package/src/tui/complete.ts +87 -0
  118. package/src/tui/diff.ts +160 -0
  119. package/src/tui/editor.ts +893 -0
  120. package/src/tui/exhaustive.ts +14 -0
  121. package/src/tui/fuzzy.ts +75 -0
  122. package/src/tui/host.ts +223 -0
  123. package/src/tui/input.ts +69 -0
  124. package/src/tui/keys.ts +378 -0
  125. package/src/tui/markdown.ts +17 -0
  126. package/src/tui/render.ts +153 -0
  127. package/src/tui/select.ts +240 -0
  128. package/src/tui/settings.ts +40 -0
  129. package/src/tui/statusline.ts +162 -0
  130. package/src/tui/style.ts +205 -0
  131. package/src/tui/terminal.ts +30 -0
  132. package/src/tui/text.ts +52 -0
  133. package/src/tui/toolview.ts +386 -0
  134. package/src/tui/transcript.ts +71 -0
  135. package/src/tui/tree.ts +405 -0
  136. package/src/tui/width.ts +567 -0
  137. package/src/vendor/pi-tui/LICENSE.upstream +21 -0
  138. package/src/vendor/pi-tui/VENDOR.md +23 -0
  139. package/src/vendor/pi-tui/alt-screen-search.ts +327 -0
  140. package/src/vendor/pi-tui/autocomplete.ts +835 -0
  141. package/src/vendor/pi-tui/components/alt-screen-flash.ts +51 -0
  142. package/src/vendor/pi-tui/components/box.ts +167 -0
  143. package/src/vendor/pi-tui/components/cancellable-loader.ts +40 -0
  144. package/src/vendor/pi-tui/components/editor.ts +2470 -0
  145. package/src/vendor/pi-tui/components/h-stack.ts +44 -0
  146. package/src/vendor/pi-tui/components/image.ts +127 -0
  147. package/src/vendor/pi-tui/components/input.ts +494 -0
  148. package/src/vendor/pi-tui/components/loader.ts +101 -0
  149. package/src/vendor/pi-tui/components/markdown.ts +1015 -0
  150. package/src/vendor/pi-tui/components/mouse-region.ts +33 -0
  151. package/src/vendor/pi-tui/components/scroll-view.ts +224 -0
  152. package/src/vendor/pi-tui/components/select-list.ts +273 -0
  153. package/src/vendor/pi-tui/components/settings-list.ts +328 -0
  154. package/src/vendor/pi-tui/components/spacer.ts +28 -0
  155. package/src/vendor/pi-tui/components/stack.ts +154 -0
  156. package/src/vendor/pi-tui/components/text.ts +107 -0
  157. package/src/vendor/pi-tui/components/truncated-text.ts +65 -0
  158. package/src/vendor/pi-tui/components/v-stack.ts +33 -0
  159. package/src/vendor/pi-tui/editor-component.ts +74 -0
  160. package/src/vendor/pi-tui/fuzzy.ts +138 -0
  161. package/src/vendor/pi-tui/index.ts +156 -0
  162. package/src/vendor/pi-tui/keybindings.ts +320 -0
  163. package/src/vendor/pi-tui/keys.ts +1401 -0
  164. package/src/vendor/pi-tui/kill-ring.ts +46 -0
  165. package/src/vendor/pi-tui/latex.ts +1506 -0
  166. package/src/vendor/pi-tui/layout-node.ts +51 -0
  167. package/src/vendor/pi-tui/layout.ts +449 -0
  168. package/src/vendor/pi-tui/native-modifiers.ts +13 -0
  169. package/src/vendor/pi-tui/native-module-path.ts +31 -0
  170. package/src/vendor/pi-tui/native-platform.ts +63 -0
  171. package/src/vendor/pi-tui/stdin-buffer.ts +444 -0
  172. package/src/vendor/pi-tui/terminal-colors.ts +73 -0
  173. package/src/vendor/pi-tui/terminal-image.ts +696 -0
  174. package/src/vendor/pi-tui/terminal.ts +547 -0
  175. package/src/vendor/pi-tui/tui-alt-screen.ts +1742 -0
  176. package/src/vendor/pi-tui/tui-main-screen.ts +655 -0
  177. package/src/vendor/pi-tui/tui.ts +1456 -0
  178. package/src/vendor/pi-tui/undo-stack.ts +28 -0
  179. package/src/vendor/pi-tui/utils.ts +1345 -0
  180. package/src/vendor/pi-tui/word-navigation.ts +117 -0
@@ -0,0 +1,444 @@
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
+ }
@@ -0,0 +1,73 @@
1
+ export interface RgbColor {
2
+ r: number;
3
+ g: number;
4
+ b: number;
5
+ }
6
+
7
+ export type TerminalColorScheme = "dark" | "light";
8
+
9
+ function hexToRgb(hex: string): RgbColor {
10
+ const normalized = hex.startsWith("#") ? hex.slice(1) : hex;
11
+ const r = parseInt(normalized.slice(0, 2), 16);
12
+ const g = parseInt(normalized.slice(2, 4), 16);
13
+ const b = parseInt(normalized.slice(4, 6), 16);
14
+ return { r, g, b };
15
+ }
16
+
17
+ function parseOscHexChannel(channel: string): number | undefined {
18
+ if (!/^[0-9a-f]+$/i.test(channel)) {
19
+ return undefined;
20
+ }
21
+ const max = 16 ** channel.length - 1;
22
+ if (max <= 0) {
23
+ return undefined;
24
+ }
25
+ return Math.round((parseInt(channel, 16) / max) * 255);
26
+ }
27
+
28
+ const OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN = /^\x1b\]11;([^\x07\x1b]*)(?:\x07|\x1b\\)$/i;
29
+ const COLOR_SCHEME_REPORT_PATTERN = /^(?:\x1b\[\?997;(1|2)n)+$/;
30
+
31
+ export function isOsc11BackgroundColorResponse(data: string): boolean {
32
+ return OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN.test(data);
33
+ }
34
+
35
+ export function parseOsc11BackgroundColor(data: string): RgbColor | undefined {
36
+ const match = data.match(OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN);
37
+ if (!match) {
38
+ return undefined;
39
+ }
40
+
41
+ const value = match[1].trim();
42
+ if (value.startsWith("#")) {
43
+ const hex = value.slice(1);
44
+ if (/^[0-9a-f]{6}$/i.test(hex)) {
45
+ return hexToRgb(value);
46
+ }
47
+ if (/^[0-9a-f]{12}$/i.test(hex)) {
48
+ const r = parseOscHexChannel(hex.slice(0, 4));
49
+ const g = parseOscHexChannel(hex.slice(4, 8));
50
+ const b = parseOscHexChannel(hex.slice(8, 12));
51
+ return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined;
52
+ }
53
+ return undefined;
54
+ }
55
+
56
+ const rgbValue = value.replace(/^rgba?:/i, "");
57
+ const [red, green, blue] = rgbValue.split("/");
58
+ if (red === undefined || green === undefined || blue === undefined) {
59
+ return undefined;
60
+ }
61
+ const r = parseOscHexChannel(red);
62
+ const g = parseOscHexChannel(green);
63
+ const b = parseOscHexChannel(blue);
64
+ return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined;
65
+ }
66
+
67
+ export function parseTerminalColorSchemeReport(data: string): TerminalColorScheme | undefined {
68
+ const match = data.match(COLOR_SCHEME_REPORT_PATTERN);
69
+ if (!match) {
70
+ return undefined;
71
+ }
72
+ return match[1] === "2" ? "light" : "dark";
73
+ }