@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,554 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import { setKittyProtocolActive } from "./keys.ts";
4
- import { isNativeModifierPressed } from "./native-modifiers.ts";
5
- import { getNativePlatformHelper } from "./native-platform.ts";
6
- import { StdinBuffer } from "./stdin-buffer.ts";
7
-
8
- const TERMINAL_PROGRESS_KEEPALIVE_MS = 1000;
9
- const TERMINAL_PROGRESS_ACTIVE_SEQUENCE = "\x1b]9;4;3\x07";
10
- const TERMINAL_PROGRESS_CLEAR_SEQUENCE = "\x1b]9;4;0\x07";
11
- const NATIVE_SHIFT_ENTER_SEQUENCE = "\x1b[13;2u";
12
- const DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS = 7;
13
- const KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS = 150;
14
- const KITTY_KEYBOARD_PROTOCOL_QUERY = `\x1b[>${DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS}u\x1b[?u\x1b[c`;
15
-
16
- export type KeyboardProtocolNegotiationSequence =
17
- | { type: "kitty-flags"; flags: number }
18
- | { type: "device-attributes" };
19
-
20
- export function parseKeyboardProtocolNegotiationSequence(
21
- sequence: string,
22
- ): KeyboardProtocolNegotiationSequence | undefined {
23
- const kittyFlags = sequence.match(/^\x1b\[\?(\d+)u$/);
24
- if (kittyFlags) {
25
- return { type: "kitty-flags", flags: Number.parseInt(kittyFlags[1]!, 10) };
26
- }
27
- if (/^\x1b\[\?[\d;]*c$/.test(sequence)) {
28
- return { type: "device-attributes" };
29
- }
30
- return undefined;
31
- }
32
-
33
- function isKeyboardProtocolNegotiationSequencePrefix(sequence: string): boolean {
34
- return sequence === "\x1b[" || /^\x1b\[\?[\d;]*$/.test(sequence);
35
- }
36
-
37
- export function isAppleTerminalSession(): boolean {
38
- return process.platform === "darwin" && process.env.TERM_PROGRAM === "Apple_Terminal";
39
- }
40
-
41
- /**
42
- * Refresh terminal dimensions on POSIX platforms by sending SIGWINCH to this process.
43
- * Best-effort: some environments (restricted seccomp or LSM policies) return EACCES
44
- * for `kill(2)`; in that case the dimensions refresh is skipped rather than crashing.
45
- */
46
- export function refreshTerminalDimensions(): void {
47
- if (process.platform === "win32" || process.pid <= 0) return;
48
- try {
49
- process.kill(process.pid, "SIGWINCH");
50
- } catch {
51
- // Signal delivery not permitted in this environment; ignore.
52
- }
53
- }
54
-
55
- export function normalizeNativeShiftEnterInput(
56
- data: string,
57
- shouldDetectNativeShiftEnter: boolean,
58
- isShiftPressed: boolean,
59
- ): string {
60
- if (shouldDetectNativeShiftEnter && data === "\r" && isShiftPressed) return NATIVE_SHIFT_ENTER_SEQUENCE;
61
- return data;
62
- }
63
-
64
- export function normalizeAppleTerminalInput(data: string, isAppleTerminal: boolean, isShiftPressed: boolean): string {
65
- return normalizeNativeShiftEnterInput(data, isAppleTerminal, isShiftPressed);
66
- }
67
-
68
- /**
69
- * Minimal terminal interface for TUI
70
- */
71
- export interface Terminal {
72
- // Start the terminal with input and resize handlers
73
- start(onInput: (data: string) => void, onResize: () => void): void;
74
-
75
- // Stop the terminal and restore state
76
- stop(): void;
77
-
78
- /**
79
- * Drain stdin before exiting to prevent Kitty key release events from
80
- * leaking to the parent shell over slow SSH connections.
81
- * @param maxMs - Maximum time to drain (default: 1000ms)
82
- * @param idleMs - Exit early if no input arrives within this time (default: 50ms)
83
- */
84
- drainInput(maxMs?: number, idleMs?: number): Promise<void>;
85
-
86
- // Write output to terminal
87
- write(data: string): void;
88
-
89
- // Get terminal dimensions
90
- get columns(): number;
91
- get rows(): number;
92
-
93
- // Whether Kitty keyboard protocol is active
94
- get kittyProtocolActive(): boolean;
95
-
96
- // Cursor positioning (relative to current position)
97
- moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines
98
-
99
- // Cursor visibility
100
- hideCursor(): void; // Hide the cursor
101
- showCursor(): void; // Show the cursor
102
-
103
- // Clear operations
104
- clearLine(): void; // Clear current line
105
- clearFromCursor(): void; // Clear from cursor to end of screen
106
- clearScreen(): void; // Clear entire screen and move cursor to (0,0)
107
-
108
- // Title operations
109
- setTitle(title: string): void; // Set terminal window title
110
-
111
- // Progress indicator (OSC 9;4)
112
- setProgress(active: boolean): void;
113
- }
114
-
115
- const DEFAULT_ESCAPE_TIMEOUT_MS = 10;
116
- const DEFAULT_SSH_ESCAPE_TIMEOUT_MS = 100;
117
-
118
- /**
119
- * Resolve how long to wait for the rest of an escape sequence before
120
- * dispatching a lone ESC as the Escape key. Legacy Alt+key input is ESC plus
121
- * another byte, so high-latency transports need a longer reassembly window.
122
- */
123
- export function resolveEscapeTimeoutMs(env: NodeJS.ProcessEnv = process.env): number {
124
- const configured = Number(env.PI_TUI_ESC_TIMEOUT);
125
- if (Number.isFinite(configured) && configured > 0) {
126
- return configured;
127
- }
128
- if (env.SSH_CONNECTION || env.SSH_TTY) {
129
- return DEFAULT_SSH_ESCAPE_TIMEOUT_MS;
130
- }
131
- return DEFAULT_ESCAPE_TIMEOUT_MS;
132
- }
133
-
134
- /**
135
- * Real terminal using process.stdin/stdout
136
- */
137
- export class ProcessTerminal implements Terminal {
138
- private wasRaw = false;
139
- private inputHandler?: (data: string) => void;
140
- private resizeHandler?: () => void;
141
- private _kittyProtocolActive = false;
142
- private _modifyOtherKeysActive = false;
143
- private keyboardProtocolPushed = false;
144
- /** DA1 replies owed to keyboard protocol queries. Later DA1 replies answer other queries and are forwarded. */
145
- private pendingKeyboardProtocolDeviceAttributes = 0;
146
- private keyboardProtocolNegotiationBuffer = "";
147
- private keyboardProtocolBufferFlushTimer?: ReturnType<typeof setTimeout>;
148
- private stdinBuffer?: StdinBuffer;
149
- private stdinDataHandler?: (data: string) => void;
150
- private progressInterval?: ReturnType<typeof setInterval>;
151
- private writeLogPath = (() => {
152
- const env = process.env.PI_TUI_WRITE_LOG || "";
153
- if (!env) return "";
154
- try {
155
- if (fs.statSync(env).isDirectory()) {
156
- const now = new Date();
157
- const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}_${String(now.getHours()).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}-${String(now.getSeconds()).padStart(2, "0")}`;
158
- return path.join(env, `tui-${ts}-${process.pid}.log`);
159
- }
160
- } catch {
161
- // Not an existing directory - use as-is (file path)
162
- }
163
- return env;
164
- })();
165
-
166
- get kittyProtocolActive(): boolean {
167
- return this._kittyProtocolActive;
168
- }
169
-
170
- get modifyOtherKeysActive(): boolean {
171
- return this._modifyOtherKeysActive;
172
- }
173
-
174
- start(onInput: (data: string) => void, onResize: () => void): void {
175
- this.inputHandler = onInput;
176
- this.resizeHandler = onResize;
177
-
178
- // Save previous state and enable raw mode
179
- this.wasRaw = process.stdin.isRaw || false;
180
- if (process.stdin.setRawMode) {
181
- process.stdin.setRawMode(true);
182
- }
183
- process.stdin.setEncoding("utf8");
184
- process.stdin.resume();
185
-
186
- // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
187
- process.stdout.write("\x1b[?2004h");
188
-
189
- // Set up resize handler immediately
190
- process.stdout.on("resize", this.resizeHandler);
191
-
192
- // Refresh terminal dimensions - they may be stale after suspend/resume
193
- // (SIGWINCH is lost while process is stopped). Unix only, best-effort.
194
- refreshTerminalDimensions();
195
-
196
- // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
197
- // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
198
- // events that lose modifier information. Must run AFTER setRawMode(true)
199
- // since that resets console mode flags.
200
- this.enableWindowsVTInput();
201
-
202
- // Query Kitty keyboard protocol and fall back to modifyOtherKeys when DA confirms no Kitty response.
203
- // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
204
- this.queryAndEnableKittyProtocol();
205
- }
206
-
207
- /**
208
- * Set up StdinBuffer to split batched input into individual sequences.
209
- * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
210
- *
211
- * Also watches for Kitty protocol response and enables it when detected.
212
- * This is done here (after stdinBuffer parsing) rather than on raw stdin
213
- * to handle the case where the response arrives split across multiple events.
214
- */
215
- private setupStdinBuffer(): void {
216
- this.stdinBuffer = new StdinBuffer({ escapeTimeout: resolveEscapeTimeoutMs() });
217
-
218
- // Forward individual sequences to the input handler
219
- this.stdinBuffer.on("data", (sequence) => {
220
- const negotiation = this.readKeyboardProtocolNegotiationSequence(sequence);
221
- if (negotiation === "pending") {
222
- this.scheduleKeyboardProtocolNegotiationBufferFlush();
223
- return; // Wait briefly for the rest of a split Kitty response.
224
- }
225
- if (negotiation && this.handleKeyboardProtocolNegotiationSequence(negotiation.parsed)) {
226
- return;
227
- }
228
-
229
- this.forwardInputSequence(negotiation?.sequence ?? sequence);
230
- });
231
-
232
- // Re-wrap paste content with bracketed paste markers for existing editor handling
233
- this.stdinBuffer.on("paste", (content) => {
234
- if (this.inputHandler) {
235
- this.inputHandler(`\x1b[200~${content}\x1b[201~`);
236
- }
237
- });
238
-
239
- // Handler that pipes stdin data through the buffer
240
- this.stdinDataHandler = (data: string) => {
241
- this.stdinBuffer!.process(data);
242
- };
243
- }
244
-
245
- /**
246
- * Query terminal for Kitty keyboard protocol support and enable it if available.
247
- *
248
- * Kitty's progressive enhancement detection requires requesting the desired
249
- * flags before querying them. The trailing DA query is a sentinel supported by
250
- * terminals that do not know Kitty keyboard protocol; receiving DA before a
251
- * Kitty response enables modifyOtherKeys fallback without a startup timeout.
252
- *
253
- * The requested flags are:
254
- * - 1 = disambiguate escape codes
255
- * - 2 = report event types (press/repeat/release)
256
- * - 4 = report alternate keys (shifted key, base layout key)
257
- */
258
- private queryAndEnableKittyProtocol(): void {
259
- this.setupStdinBuffer();
260
- process.stdin.on("data", this.stdinDataHandler!);
261
- this.keyboardProtocolPushed = true;
262
- this.pendingKeyboardProtocolDeviceAttributes += 1;
263
- this.clearKeyboardProtocolNegotiationBuffer();
264
- process.stdout.write(KITTY_KEYBOARD_PROTOCOL_QUERY);
265
- }
266
-
267
- private handleKeyboardProtocolNegotiationSequence(
268
- negotiationSequence: KeyboardProtocolNegotiationSequence,
269
- ): boolean {
270
- this.clearKeyboardProtocolNegotiationBuffer();
271
- if (negotiationSequence.type === "device-attributes") {
272
- if (this.pendingKeyboardProtocolDeviceAttributes === 0) return false;
273
- this.pendingKeyboardProtocolDeviceAttributes -= 1;
274
- }
275
- if (negotiationSequence.type === "kitty-flags") {
276
- if (negotiationSequence.flags !== 0) {
277
- this.disableModifyOtherKeys();
278
- if (!this._kittyProtocolActive) {
279
- this._kittyProtocolActive = true;
280
- setKittyProtocolActive(true);
281
- }
282
- } else {
283
- this.enableModifyOtherKeys();
284
- }
285
- return true;
286
- }
287
-
288
- if (!this._kittyProtocolActive) {
289
- this.enableModifyOtherKeys();
290
- }
291
- return true;
292
- }
293
-
294
- /** Returns the parsed negotiation reply with its full (possibly reassembled) sequence. */
295
- private readKeyboardProtocolNegotiationSequence(
296
- sequence: string,
297
- ): { parsed: KeyboardProtocolNegotiationSequence; sequence: string } | "pending" | undefined {
298
- if (this.keyboardProtocolNegotiationBuffer) {
299
- const bufferedSequence = this.keyboardProtocolNegotiationBuffer + sequence;
300
- const negotiationSequence = parseKeyboardProtocolNegotiationSequence(bufferedSequence);
301
- if (negotiationSequence) {
302
- this.clearKeyboardProtocolNegotiationBuffer();
303
- return { parsed: negotiationSequence, sequence: bufferedSequence };
304
- }
305
- if (isKeyboardProtocolNegotiationSequencePrefix(bufferedSequence)) {
306
- this.setKeyboardProtocolNegotiationBuffer(bufferedSequence);
307
- return "pending";
308
- }
309
- this.flushKeyboardProtocolNegotiationBufferAsInput();
310
- }
311
-
312
- const negotiationSequence = parseKeyboardProtocolNegotiationSequence(sequence);
313
- if (negotiationSequence) return { parsed: negotiationSequence, sequence };
314
- if (isKeyboardProtocolNegotiationSequencePrefix(sequence)) {
315
- this.setKeyboardProtocolNegotiationBuffer(sequence);
316
- return "pending";
317
- }
318
- return undefined;
319
- }
320
-
321
- private setKeyboardProtocolNegotiationBuffer(sequence: string): void {
322
- this.clearKeyboardProtocolNegotiationBufferFlushTimer();
323
- this.keyboardProtocolNegotiationBuffer = sequence;
324
- }
325
-
326
- private clearKeyboardProtocolNegotiationBuffer(): void {
327
- this.clearKeyboardProtocolNegotiationBufferFlushTimer();
328
- this.keyboardProtocolNegotiationBuffer = "";
329
- }
330
-
331
- private flushKeyboardProtocolNegotiationBufferAsInput(): void {
332
- if (!this.keyboardProtocolNegotiationBuffer) return;
333
- const sequence = this.keyboardProtocolNegotiationBuffer;
334
- this.clearKeyboardProtocolNegotiationBuffer();
335
- this.forwardInputSequence(sequence);
336
- }
337
-
338
- private scheduleKeyboardProtocolNegotiationBufferFlush(): void {
339
- if (!this.keyboardProtocolNegotiationBuffer || this.keyboardProtocolBufferFlushTimer) return;
340
- this.keyboardProtocolBufferFlushTimer = setTimeout(() => {
341
- this.keyboardProtocolBufferFlushTimer = undefined;
342
- this.flushKeyboardProtocolNegotiationBufferAsInput();
343
- }, KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS);
344
- }
345
-
346
- private clearKeyboardProtocolNegotiationBufferFlushTimer(): void {
347
- if (!this.keyboardProtocolBufferFlushTimer) return;
348
- clearTimeout(this.keyboardProtocolBufferFlushTimer);
349
- this.keyboardProtocolBufferFlushTimer = undefined;
350
- }
351
-
352
- private forwardInputSequence(sequence: string): void {
353
- if (!this.inputHandler) return;
354
- const shouldDetectNativeShiftEnter =
355
- sequence === "\r" && (isAppleTerminalSession() || process.platform === "win32");
356
- const input = normalizeNativeShiftEnterInput(
357
- sequence,
358
- shouldDetectNativeShiftEnter,
359
- shouldDetectNativeShiftEnter && isNativeModifierPressed("shift"),
360
- );
361
- this.inputHandler(input);
362
- }
363
-
364
- private enableModifyOtherKeys(): void {
365
- if (this._kittyProtocolActive || this._modifyOtherKeysActive) return;
366
- process.stdout.write("\x1b[>4;2m");
367
- this._modifyOtherKeysActive = true;
368
- }
369
-
370
- private disableModifyOtherKeys(): void {
371
- if (!this._modifyOtherKeysActive) return;
372
- process.stdout.write("\x1b[>4;0m");
373
- this._modifyOtherKeysActive = false;
374
- }
375
-
376
- /**
377
- * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
378
- * console handle so the terminal sends VT sequences for modified keys
379
- * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
380
- * discards modifier state and Shift+Tab arrives as plain \t.
381
- */
382
- private enableWindowsVTInput(): void {
383
- if (process.platform !== "win32") return;
384
- try {
385
- getNativePlatformHelper()?.enableVirtualTerminalInput?.();
386
- } catch {
387
- // Native helper not available — Shift+Tab won't be distinguishable from Tab.
388
- }
389
- }
390
-
391
- async drainInput(maxMs = 1000, idleMs = 50): Promise<void> {
392
- const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive;
393
- this.clearKeyboardProtocolNegotiationBuffer();
394
- if (shouldDisableKittyProtocol) {
395
- // Disable Kitty keyboard protocol first so any late key releases
396
- // do not generate new Kitty escape sequences.
397
- process.stdout.write("\x1b[<u");
398
- this.keyboardProtocolPushed = false;
399
- this._kittyProtocolActive = false;
400
- setKittyProtocolActive(false);
401
- }
402
- this.disableModifyOtherKeys();
403
-
404
- const previousHandler = this.inputHandler;
405
- this.inputHandler = undefined;
406
-
407
- let lastDataTime = Date.now();
408
- const onData = () => {
409
- lastDataTime = Date.now();
410
- };
411
-
412
- process.stdin.on("data", onData);
413
- const endTime = Date.now() + maxMs;
414
-
415
- try {
416
- while (true) {
417
- const now = Date.now();
418
- const timeLeft = endTime - now;
419
- if (timeLeft <= 0) break;
420
- if (now - lastDataTime >= idleMs) break;
421
- await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
422
- }
423
- } finally {
424
- process.stdin.removeListener("data", onData);
425
- this.inputHandler = previousHandler;
426
- }
427
- }
428
-
429
- stop(): void {
430
- if (this.clearProgressInterval()) {
431
- process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
432
- }
433
-
434
- // Disable bracketed paste mode
435
- process.stdout.write("\x1b[?2004l");
436
-
437
- const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive;
438
- this.clearKeyboardProtocolNegotiationBuffer();
439
-
440
- // Disable Kitty keyboard protocol if not already done by drainInput()
441
- if (shouldDisableKittyProtocol) {
442
- process.stdout.write("\x1b[<u");
443
- this.keyboardProtocolPushed = false;
444
- this._kittyProtocolActive = false;
445
- setKittyProtocolActive(false);
446
- }
447
- this.disableModifyOtherKeys();
448
-
449
- // Clean up StdinBuffer
450
- if (this.stdinBuffer) {
451
- this.stdinBuffer.destroy();
452
- this.stdinBuffer = undefined;
453
- }
454
-
455
- // Remove event handlers
456
- if (this.stdinDataHandler) {
457
- process.stdin.removeListener("data", this.stdinDataHandler);
458
- this.stdinDataHandler = undefined;
459
- }
460
- this.inputHandler = undefined;
461
- if (this.resizeHandler) {
462
- process.stdout.removeListener("resize", this.resizeHandler);
463
- this.resizeHandler = undefined;
464
- }
465
-
466
- // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
467
- // re-interpreted after raw mode is disabled. This fixes a race condition
468
- // where Ctrl+D could close the parent shell over SSH.
469
- process.stdin.pause();
470
-
471
- // Restore raw mode state
472
- if (process.stdin.setRawMode) {
473
- process.stdin.setRawMode(this.wasRaw);
474
- }
475
- }
476
-
477
- write(data: string): void {
478
- process.stdout.write(data);
479
- if (this.writeLogPath) {
480
- try {
481
- fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
482
- } catch {
483
- // Ignore logging errors
484
- }
485
- }
486
- }
487
-
488
- get columns(): number {
489
- return process.stdout.columns || Number(process.env.COLUMNS) || 80;
490
- }
491
-
492
- get rows(): number {
493
- return process.stdout.rows || Number(process.env.LINES) || 24;
494
- }
495
-
496
- moveBy(lines: number): void {
497
- if (lines > 0) {
498
- // Move down
499
- process.stdout.write(`\x1b[${lines}B`);
500
- } else if (lines < 0) {
501
- // Move up
502
- process.stdout.write(`\x1b[${-lines}A`);
503
- }
504
- // lines === 0: no movement
505
- }
506
-
507
- hideCursor(): void {
508
- process.stdout.write("\x1b[?25l");
509
- }
510
-
511
- showCursor(): void {
512
- process.stdout.write("\x1b[?25h");
513
- }
514
-
515
- clearLine(): void {
516
- process.stdout.write("\x1b[K");
517
- }
518
-
519
- clearFromCursor(): void {
520
- process.stdout.write("\x1b[J");
521
- }
522
-
523
- clearScreen(): void {
524
- process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
525
- }
526
-
527
- setTitle(title: string): void {
528
- // OSC 0;title BEL - set terminal window title
529
- process.stdout.write(`\x1b]0;${title}\x07`);
530
- }
531
-
532
- setProgress(active: boolean): void {
533
- if (active) {
534
- // OSC 9;4;3 - indeterminate progress
535
- process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
536
- if (!this.progressInterval) {
537
- this.progressInterval = setInterval(() => {
538
- process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
539
- }, TERMINAL_PROGRESS_KEEPALIVE_MS);
540
- }
541
- } else {
542
- this.clearProgressInterval();
543
- // OSC 9;4;0 - clear progress
544
- process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
545
- }
546
- }
547
-
548
- private clearProgressInterval(): boolean {
549
- if (!this.progressInterval) return false;
550
- clearInterval(this.progressInterval);
551
- this.progressInterval = undefined;
552
- return true;
553
- }
554
- }