@pencil-agent/nano-pencil 2.0.0-beta.2 → 2.0.0-beta.3

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 (179) hide show
  1. package/dist/build-meta.json +3 -3
  2. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.d.ts +17 -0
  3. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.js +60 -0
  4. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.d.ts +19 -0
  5. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.js +55 -0
  6. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.d.ts +10 -0
  7. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.js +137 -0
  8. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.d.ts +22 -0
  9. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.js +64 -0
  10. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.d.ts +26 -0
  11. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.js +913 -0
  12. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.d.ts +9 -0
  13. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.js +32 -0
  14. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.d.ts +215 -0
  15. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.js +522 -0
  16. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.d.ts +62 -0
  17. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.js +146 -0
  18. package/dist/node_modules/@pencil-agent/agent-core/dist/index.d.ts +14 -0
  19. package/dist/node_modules/@pencil-agent/agent-core/dist/index.js +19 -0
  20. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.d.ts +91 -0
  21. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.js +279 -0
  22. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.d.ts +15 -0
  23. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.js +625 -0
  24. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.d.ts +33 -0
  25. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.js +189 -0
  26. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.d.ts +35 -0
  27. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.js +319 -0
  28. package/dist/node_modules/@pencil-agent/agent-core/dist/types.d.ts +417 -0
  29. package/dist/node_modules/@pencil-agent/agent-core/dist/types.js +13 -0
  30. package/dist/node_modules/@pencil-agent/agent-core/package.json +28 -0
  31. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.d.ts +27 -0
  32. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.js +152 -0
  33. package/dist/node_modules/@pencil-agent/ai/dist/cli.d.ts +2 -0
  34. package/dist/node_modules/@pencil-agent/ai/dist/cli.js +121 -0
  35. package/dist/node_modules/@pencil-agent/ai/dist/config-path.d.ts +1 -0
  36. package/dist/node_modules/@pencil-agent/ai/dist/config-path.js +17 -0
  37. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.d.ts +94 -0
  38. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.js +218 -0
  39. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.d.ts +8 -0
  40. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.js +107 -0
  41. package/dist/node_modules/@pencil-agent/ai/dist/env.d.ts +7 -0
  42. package/dist/node_modules/@pencil-agent/ai/dist/env.js +7 -0
  43. package/dist/node_modules/@pencil-agent/ai/dist/events.d.ts +8 -0
  44. package/dist/node_modules/@pencil-agent/ai/dist/events.js +7 -0
  45. package/dist/node_modules/@pencil-agent/ai/dist/index.d.ts +27 -0
  46. package/dist/node_modules/@pencil-agent/ai/dist/index.js +20 -0
  47. package/dist/node_modules/@pencil-agent/ai/dist/json.d.ts +7 -0
  48. package/dist/node_modules/@pencil-agent/ai/dist/json.js +7 -0
  49. package/dist/node_modules/@pencil-agent/ai/dist/models.d.ts +31 -0
  50. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.d.ts +15159 -0
  51. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.js +14928 -0
  52. package/dist/node_modules/@pencil-agent/ai/dist/models.js +60 -0
  53. package/dist/node_modules/@pencil-agent/ai/dist/overflow.d.ts +7 -0
  54. package/dist/node_modules/@pencil-agent/ai/dist/overflow.js +7 -0
  55. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.d.ts +20 -0
  56. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.js +606 -0
  57. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.d.ts +38 -0
  58. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.js +737 -0
  59. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.d.ts +21 -0
  60. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.js +193 -0
  61. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.d.ts +13 -0
  62. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.js +34 -0
  63. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.d.ts +79 -0
  64. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.js +753 -0
  65. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.d.ts +70 -0
  66. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.js +311 -0
  67. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.d.ts +20 -0
  68. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.js +380 -0
  69. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.d.ts +18 -0
  70. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.js +360 -0
  71. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.d.ts +8 -0
  72. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.js +704 -0
  73. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.d.ts +20 -0
  74. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.js +870 -0
  75. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.d.ts +22 -0
  76. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.js +432 -0
  77. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.d.ts +19 -0
  78. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.js +207 -0
  79. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.d.ts +8 -0
  80. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.js +86 -0
  81. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.d.ts +13 -0
  82. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.js +40 -0
  83. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.d.ts +13 -0
  84. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.js +175 -0
  85. package/dist/node_modules/@pencil-agent/ai/dist/registry.d.ts +8 -0
  86. package/dist/node_modules/@pencil-agent/ai/dist/registry.js +8 -0
  87. package/dist/node_modules/@pencil-agent/ai/dist/schema.d.ts +10 -0
  88. package/dist/node_modules/@pencil-agent/ai/dist/schema.js +9 -0
  89. package/dist/node_modules/@pencil-agent/ai/dist/stream.d.ts +25 -0
  90. package/dist/node_modules/@pencil-agent/ai/dist/stream.js +324 -0
  91. package/dist/node_modules/@pencil-agent/ai/dist/types.d.ts +306 -0
  92. package/dist/node_modules/@pencil-agent/ai/dist/types.js +7 -0
  93. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.d.ts +12 -0
  94. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.js +7 -0
  95. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.d.ts +31 -0
  96. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.js +98 -0
  97. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.d.ts +13 -0
  98. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.js +20 -0
  99. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.d.ts +14 -0
  100. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.js +34 -0
  101. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.d.ts +22 -0
  102. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.js +109 -0
  103. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.d.ts +12 -0
  104. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.js +25 -0
  105. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.d.ts +35 -0
  106. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.js +286 -0
  107. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.d.ts +31 -0
  108. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.js +378 -0
  109. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.d.ts +31 -0
  110. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.js +483 -0
  111. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.d.ts +60 -0
  112. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.js +131 -0
  113. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.d.ts +39 -0
  114. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.js +385 -0
  115. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.d.ts +18 -0
  116. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.js +36 -0
  117. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.d.ts +52 -0
  118. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.js +7 -0
  119. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.d.ts +57 -0
  120. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.js +120 -0
  121. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.d.ts +16 -0
  122. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.js +20 -0
  123. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.d.ts +22 -0
  124. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.js +26 -0
  125. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.d.ts +23 -0
  126. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.js +78 -0
  127. package/dist/node_modules/@pencil-agent/ai/package.json +94 -0
  128. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.d.ts +62 -0
  129. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.js +624 -0
  130. package/dist/node_modules/@pencil-agent/tui/dist/components/box.d.ts +27 -0
  131. package/dist/node_modules/@pencil-agent/tui/dist/components/box.js +109 -0
  132. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.d.ts +27 -0
  133. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.js +40 -0
  134. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.d.ts +218 -0
  135. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.js +1697 -0
  136. package/dist/node_modules/@pencil-agent/tui/dist/components/image.d.ts +33 -0
  137. package/dist/node_modules/@pencil-agent/tui/dist/components/image.js +74 -0
  138. package/dist/node_modules/@pencil-agent/tui/dist/components/input.d.ts +42 -0
  139. package/dist/node_modules/@pencil-agent/tui/dist/components/input.js +438 -0
  140. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.d.ts +26 -0
  141. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.js +54 -0
  142. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.d.ts +100 -0
  143. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.js +634 -0
  144. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.d.ts +37 -0
  145. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.js +157 -0
  146. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.d.ts +55 -0
  147. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.js +190 -0
  148. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.d.ts +17 -0
  149. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.js +28 -0
  150. package/dist/node_modules/@pencil-agent/tui/dist/components/text.d.ts +24 -0
  151. package/dist/node_modules/@pencil-agent/tui/dist/components/text.js +94 -0
  152. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.d.ts +18 -0
  153. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.js +56 -0
  154. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.d.ts +51 -0
  155. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.js +7 -0
  156. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.d.ts +32 -0
  157. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.js +152 -0
  158. package/dist/node_modules/@pencil-agent/tui/dist/index.d.ts +28 -0
  159. package/dist/node_modules/@pencil-agent/tui/dist/index.js +37 -0
  160. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.d.ts +44 -0
  161. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.js +119 -0
  162. package/dist/node_modules/@pencil-agent/tui/dist/keys.d.ts +149 -0
  163. package/dist/node_modules/@pencil-agent/tui/dist/keys.js +948 -0
  164. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.d.ts +33 -0
  165. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.js +49 -0
  166. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.d.ts +38 -0
  167. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.js +307 -0
  168. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.d.ts +73 -0
  169. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.js +287 -0
  170. package/dist/node_modules/@pencil-agent/tui/dist/terminal.d.ts +86 -0
  171. package/dist/node_modules/@pencil-agent/tui/dist/terminal.js +266 -0
  172. package/dist/node_modules/@pencil-agent/tui/dist/tui.d.ts +219 -0
  173. package/dist/node_modules/@pencil-agent/tui/dist/tui.js +1001 -0
  174. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.d.ts +22 -0
  175. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.js +30 -0
  176. package/dist/node_modules/@pencil-agent/tui/dist/utils.d.ts +83 -0
  177. package/dist/node_modules/@pencil-agent/tui/dist/utils.js +811 -0
  178. package/dist/node_modules/@pencil-agent/tui/package.json +37 -0
  179. package/package.json +3 -2
@@ -0,0 +1,948 @@
1
+ /**
2
+ * [WHO]: Keyboard input handling - setKittyProtocolActive, isKittyProtocolActive, Key, matchesKey, parseKey
3
+ * [FROM]: No external dependencies
4
+ * [TO]: Consumed by core/lib/tui/src/index.ts, TUI components
5
+ * [HERE]: core/lib/tui/src/keys.ts - keyboard input handling for terminal applications
6
+ *
7
+ * Supports both legacy terminal sequences and Kitty keyboard protocol.
8
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
9
+ */
10
+ // =============================================================================
11
+ // Global Kitty Protocol State
12
+ // =============================================================================
13
+ let _kittyProtocolActive = false;
14
+ /**
15
+ * Set the global Kitty keyboard protocol state.
16
+ * Called by ProcessTerminal after detecting protocol support.
17
+ */
18
+ export function setKittyProtocolActive(active) {
19
+ _kittyProtocolActive = active;
20
+ }
21
+ /**
22
+ * Query whether Kitty keyboard protocol is currently active.
23
+ */
24
+ export function isKittyProtocolActive() {
25
+ return _kittyProtocolActive;
26
+ }
27
+ /**
28
+ * Helper object for creating typed key identifiers with autocomplete.
29
+ *
30
+ * Usage:
31
+ * - Key.escape, Key.enter, Key.tab, etc. for special keys
32
+ * - Key.backtick, Key.comma, Key.period, etc. for symbol keys
33
+ * - Key.ctrl("c"), Key.alt("x") for single modifier
34
+ * - Key.ctrlShift("p"), Key.ctrlAlt("x") for combined modifiers
35
+ */
36
+ export const Key = {
37
+ // Special keys
38
+ escape: "escape",
39
+ esc: "esc",
40
+ enter: "enter",
41
+ return: "return",
42
+ tab: "tab",
43
+ space: "space",
44
+ backspace: "backspace",
45
+ delete: "delete",
46
+ insert: "insert",
47
+ clear: "clear",
48
+ home: "home",
49
+ end: "end",
50
+ pageUp: "pageUp",
51
+ pageDown: "pageDown",
52
+ up: "up",
53
+ down: "down",
54
+ left: "left",
55
+ right: "right",
56
+ f1: "f1",
57
+ f2: "f2",
58
+ f3: "f3",
59
+ f4: "f4",
60
+ f5: "f5",
61
+ f6: "f6",
62
+ f7: "f7",
63
+ f8: "f8",
64
+ f9: "f9",
65
+ f10: "f10",
66
+ f11: "f11",
67
+ f12: "f12",
68
+ // Symbol keys
69
+ backtick: "`",
70
+ hyphen: "-",
71
+ equals: "=",
72
+ leftbracket: "[",
73
+ rightbracket: "]",
74
+ backslash: "\\",
75
+ semicolon: ";",
76
+ quote: "'",
77
+ comma: ",",
78
+ period: ".",
79
+ slash: "/",
80
+ exclamation: "!",
81
+ at: "@",
82
+ hash: "#",
83
+ dollar: "$",
84
+ percent: "%",
85
+ caret: "^",
86
+ ampersand: "&",
87
+ asterisk: "*",
88
+ leftparen: "(",
89
+ rightparen: ")",
90
+ underscore: "_",
91
+ plus: "+",
92
+ pipe: "|",
93
+ tilde: "~",
94
+ leftbrace: "{",
95
+ rightbrace: "}",
96
+ colon: ":",
97
+ lessthan: "<",
98
+ greaterthan: ">",
99
+ question: "?",
100
+ // Single modifiers
101
+ ctrl: (key) => `ctrl+${key}`,
102
+ shift: (key) => `shift+${key}`,
103
+ alt: (key) => `alt+${key}`,
104
+ // Combined modifiers
105
+ ctrlShift: (key) => `ctrl+shift+${key}`,
106
+ shiftCtrl: (key) => `shift+ctrl+${key}`,
107
+ ctrlAlt: (key) => `ctrl+alt+${key}`,
108
+ altCtrl: (key) => `alt+ctrl+${key}`,
109
+ shiftAlt: (key) => `shift+alt+${key}`,
110
+ altShift: (key) => `alt+shift+${key}`,
111
+ // Triple modifiers
112
+ ctrlShiftAlt: (key) => `ctrl+shift+alt+${key}`,
113
+ };
114
+ // =============================================================================
115
+ // Constants
116
+ // =============================================================================
117
+ const SYMBOL_KEYS = new Set([
118
+ "`",
119
+ "-",
120
+ "=",
121
+ "[",
122
+ "]",
123
+ "\\",
124
+ ";",
125
+ "'",
126
+ ",",
127
+ ".",
128
+ "/",
129
+ "!",
130
+ "@",
131
+ "#",
132
+ "$",
133
+ "%",
134
+ "^",
135
+ "&",
136
+ "*",
137
+ "(",
138
+ ")",
139
+ "_",
140
+ "+",
141
+ "|",
142
+ "~",
143
+ "{",
144
+ "}",
145
+ ":",
146
+ "<",
147
+ ">",
148
+ "?",
149
+ ]);
150
+ const MODIFIERS = {
151
+ shift: 1,
152
+ alt: 2,
153
+ ctrl: 4,
154
+ };
155
+ const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock
156
+ const CODEPOINTS = {
157
+ escape: 27,
158
+ tab: 9,
159
+ enter: 13,
160
+ space: 32,
161
+ backspace: 127,
162
+ kpEnter: 57414, // Numpad Enter (Kitty protocol)
163
+ };
164
+ const ARROW_CODEPOINTS = {
165
+ up: -1,
166
+ down: -2,
167
+ right: -3,
168
+ left: -4,
169
+ };
170
+ const FUNCTIONAL_CODEPOINTS = {
171
+ delete: -10,
172
+ insert: -11,
173
+ pageUp: -12,
174
+ pageDown: -13,
175
+ home: -14,
176
+ end: -15,
177
+ };
178
+ const LEGACY_KEY_SEQUENCES = {
179
+ up: ["\x1b[A", "\x1bOA"],
180
+ down: ["\x1b[B", "\x1bOB"],
181
+ right: ["\x1b[C", "\x1bOC"],
182
+ left: ["\x1b[D", "\x1bOD"],
183
+ home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"],
184
+ end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"],
185
+ insert: ["\x1b[2~"],
186
+ delete: ["\x1b[3~"],
187
+ pageUp: ["\x1b[5~", "\x1b[[5~"],
188
+ pageDown: ["\x1b[6~", "\x1b[[6~"],
189
+ clear: ["\x1b[E", "\x1bOE"],
190
+ f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"],
191
+ f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"],
192
+ f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"],
193
+ f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"],
194
+ f5: ["\x1b[15~", "\x1b[[E"],
195
+ f6: ["\x1b[17~"],
196
+ f7: ["\x1b[18~"],
197
+ f8: ["\x1b[19~"],
198
+ f9: ["\x1b[20~"],
199
+ f10: ["\x1b[21~"],
200
+ f11: ["\x1b[23~"],
201
+ f12: ["\x1b[24~"],
202
+ };
203
+ const LEGACY_SHIFT_SEQUENCES = {
204
+ up: ["\x1b[a"],
205
+ down: ["\x1b[b"],
206
+ right: ["\x1b[c"],
207
+ left: ["\x1b[d"],
208
+ clear: ["\x1b[e"],
209
+ insert: ["\x1b[2$"],
210
+ delete: ["\x1b[3$"],
211
+ pageUp: ["\x1b[5$"],
212
+ pageDown: ["\x1b[6$"],
213
+ home: ["\x1b[7$"],
214
+ end: ["\x1b[8$"],
215
+ };
216
+ const LEGACY_CTRL_SEQUENCES = {
217
+ up: ["\x1bOa"],
218
+ down: ["\x1bOb"],
219
+ right: ["\x1bOc"],
220
+ left: ["\x1bOd"],
221
+ clear: ["\x1bOe"],
222
+ insert: ["\x1b[2^"],
223
+ delete: ["\x1b[3^"],
224
+ pageUp: ["\x1b[5^"],
225
+ pageDown: ["\x1b[6^"],
226
+ home: ["\x1b[7^"],
227
+ end: ["\x1b[8^"],
228
+ };
229
+ const LEGACY_SEQUENCE_KEY_IDS = {
230
+ "\x1bOA": "up",
231
+ "\x1bOB": "down",
232
+ "\x1bOC": "right",
233
+ "\x1bOD": "left",
234
+ "\x1bOH": "home",
235
+ "\x1bOF": "end",
236
+ "\x1b[E": "clear",
237
+ "\x1bOE": "clear",
238
+ "\x1bOe": "ctrl+clear",
239
+ "\x1b[e": "shift+clear",
240
+ "\x1b[2~": "insert",
241
+ "\x1b[2$": "shift+insert",
242
+ "\x1b[2^": "ctrl+insert",
243
+ "\x1b[3$": "shift+delete",
244
+ "\x1b[3^": "ctrl+delete",
245
+ "\x1b[[5~": "pageUp",
246
+ "\x1b[[6~": "pageDown",
247
+ "\x1b[a": "shift+up",
248
+ "\x1b[b": "shift+down",
249
+ "\x1b[c": "shift+right",
250
+ "\x1b[d": "shift+left",
251
+ "\x1bOa": "ctrl+up",
252
+ "\x1bOb": "ctrl+down",
253
+ "\x1bOc": "ctrl+right",
254
+ "\x1bOd": "ctrl+left",
255
+ "\x1b[5$": "shift+pageUp",
256
+ "\x1b[6$": "shift+pageDown",
257
+ "\x1b[7$": "shift+home",
258
+ "\x1b[8$": "shift+end",
259
+ "\x1b[5^": "ctrl+pageUp",
260
+ "\x1b[6^": "ctrl+pageDown",
261
+ "\x1b[7^": "ctrl+home",
262
+ "\x1b[8^": "ctrl+end",
263
+ "\x1bOP": "f1",
264
+ "\x1bOQ": "f2",
265
+ "\x1bOR": "f3",
266
+ "\x1bOS": "f4",
267
+ "\x1b[11~": "f1",
268
+ "\x1b[12~": "f2",
269
+ "\x1b[13~": "f3",
270
+ "\x1b[14~": "f4",
271
+ "\x1b[[A": "f1",
272
+ "\x1b[[B": "f2",
273
+ "\x1b[[C": "f3",
274
+ "\x1b[[D": "f4",
275
+ "\x1b[[E": "f5",
276
+ "\x1b[15~": "f5",
277
+ "\x1b[17~": "f6",
278
+ "\x1b[18~": "f7",
279
+ "\x1b[19~": "f8",
280
+ "\x1b[20~": "f9",
281
+ "\x1b[21~": "f10",
282
+ "\x1b[23~": "f11",
283
+ "\x1b[24~": "f12",
284
+ "\x1bb": "alt+left",
285
+ "\x1bf": "alt+right",
286
+ "\x1bp": "alt+up",
287
+ "\x1bn": "alt+down",
288
+ };
289
+ const matchesLegacySequence = (data, sequences) => sequences.includes(data);
290
+ const matchesLegacyModifierSequence = (data, key, modifier) => {
291
+ if (modifier === MODIFIERS.shift) {
292
+ return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]);
293
+ }
294
+ if (modifier === MODIFIERS.ctrl) {
295
+ return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]);
296
+ }
297
+ return false;
298
+ };
299
+ // Store the last parsed event type for isKeyRelease() to query
300
+ let _lastEventType = "press";
301
+ /**
302
+ * Check if the last parsed key event was a key release.
303
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
304
+ */
305
+ export function isKeyRelease(data) {
306
+ // Don't treat bracketed paste content as key release, even if it contains
307
+ // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5").
308
+ // Terminal.ts re-wraps paste content with bracketed paste markers before
309
+ // passing to TUI, so pasted data will always contain \x1b[200~.
310
+ if (data.includes("\x1b[200~")) {
311
+ return false;
312
+ }
313
+ // Quick check: release events with flag 2 contain ":3"
314
+ // Format: \x1b[<codepoint>;<modifier>:3u
315
+ if (data.includes(":3u") ||
316
+ data.includes(":3~") ||
317
+ data.includes(":3A") ||
318
+ data.includes(":3B") ||
319
+ data.includes(":3C") ||
320
+ data.includes(":3D") ||
321
+ data.includes(":3H") ||
322
+ data.includes(":3F")) {
323
+ return true;
324
+ }
325
+ return false;
326
+ }
327
+ /**
328
+ * Check if the last parsed key event was a key repeat.
329
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
330
+ */
331
+ export function isKeyRepeat(data) {
332
+ // Don't treat bracketed paste content as key repeat, even if it contains
333
+ // patterns like ":2F". See isKeyRelease() for details.
334
+ if (data.includes("\x1b[200~")) {
335
+ return false;
336
+ }
337
+ if (data.includes(":2u") ||
338
+ data.includes(":2~") ||
339
+ data.includes(":2A") ||
340
+ data.includes(":2B") ||
341
+ data.includes(":2C") ||
342
+ data.includes(":2D") ||
343
+ data.includes(":2H") ||
344
+ data.includes(":2F")) {
345
+ return true;
346
+ }
347
+ return false;
348
+ }
349
+ function parseEventType(eventTypeStr) {
350
+ if (!eventTypeStr)
351
+ return "press";
352
+ const eventType = parseInt(eventTypeStr, 10);
353
+ if (eventType === 2)
354
+ return "repeat";
355
+ if (eventType === 3)
356
+ return "release";
357
+ return "press";
358
+ }
359
+ function parseKittySequence(data) {
360
+ // CSI u format with alternate keys (flag 4):
361
+ // \x1b[<codepoint>u
362
+ // \x1b[<codepoint>;<mod>u
363
+ // \x1b[<codepoint>;<mod>:<event>u
364
+ // \x1b[<codepoint>:<shifted>;<mod>u
365
+ // \x1b[<codepoint>:<shifted>:<base>;<mod>u
366
+ // \x1b[<codepoint>::<base>;<mod>u (no shifted key, only base)
367
+ //
368
+ // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release
369
+ // With flag 4, alternate keys are appended after codepoint with colons
370
+ const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/);
371
+ if (csiUMatch) {
372
+ const codepoint = parseInt(csiUMatch[1], 10);
373
+ const shiftedKey = csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined;
374
+ const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined;
375
+ const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1;
376
+ const eventType = parseEventType(csiUMatch[5]);
377
+ _lastEventType = eventType;
378
+ return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType };
379
+ }
380
+ // Arrow keys with modifier: \x1b[1;<mod>A/B/C/D or \x1b[1;<mod>:<event>A/B/C/D
381
+ const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/);
382
+ if (arrowMatch) {
383
+ const modValue = parseInt(arrowMatch[1], 10);
384
+ const eventType = parseEventType(arrowMatch[2]);
385
+ const arrowCodes = { A: -1, B: -2, C: -3, D: -4 };
386
+ _lastEventType = eventType;
387
+ return { codepoint: arrowCodes[arrowMatch[3]], modifier: modValue - 1, eventType };
388
+ }
389
+ // Functional keys: \x1b[<num>~ or \x1b[<num>;<mod>~ or \x1b[<num>;<mod>:<event>~
390
+ const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/);
391
+ if (funcMatch) {
392
+ const keyNum = parseInt(funcMatch[1], 10);
393
+ const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
394
+ const eventType = parseEventType(funcMatch[3]);
395
+ const funcCodes = {
396
+ 2: FUNCTIONAL_CODEPOINTS.insert,
397
+ 3: FUNCTIONAL_CODEPOINTS.delete,
398
+ 5: FUNCTIONAL_CODEPOINTS.pageUp,
399
+ 6: FUNCTIONAL_CODEPOINTS.pageDown,
400
+ 7: FUNCTIONAL_CODEPOINTS.home,
401
+ 8: FUNCTIONAL_CODEPOINTS.end,
402
+ };
403
+ const codepoint = funcCodes[keyNum];
404
+ if (codepoint !== undefined) {
405
+ _lastEventType = eventType;
406
+ return { codepoint, modifier: modValue - 1, eventType };
407
+ }
408
+ }
409
+ // Home/End with modifier: \x1b[1;<mod>H/F or \x1b[1;<mod>:<event>H/F
410
+ const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/);
411
+ if (homeEndMatch) {
412
+ const modValue = parseInt(homeEndMatch[1], 10);
413
+ const eventType = parseEventType(homeEndMatch[2]);
414
+ const codepoint = homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
415
+ _lastEventType = eventType;
416
+ return { codepoint, modifier: modValue - 1, eventType };
417
+ }
418
+ return null;
419
+ }
420
+ function matchesKittySequence(data, expectedCodepoint, expectedModifier) {
421
+ const parsed = parseKittySequence(data);
422
+ if (!parsed)
423
+ return false;
424
+ const actualMod = parsed.modifier & ~LOCK_MASK;
425
+ const expectedMod = expectedModifier & ~LOCK_MASK;
426
+ // Check if modifiers match
427
+ if (actualMod !== expectedMod)
428
+ return false;
429
+ // Primary match: codepoint matches directly
430
+ if (parsed.codepoint === expectedCodepoint)
431
+ return true;
432
+ // Alternate match: use base layout key for non-Latin keyboard layouts.
433
+ // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports
434
+ // the base layout key (the key in standard PC-101 layout).
435
+ //
436
+ // Only fall back to base layout key when the codepoint is NOT already a
437
+ // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.).
438
+ // When the codepoint is a recognized key, it is authoritative regardless
439
+ // of physical key position. This prevents remapped layouts (Dvorak, Colemak,
440
+ // xremap, etc.) from causing false matches: both letters and symbols move
441
+ // to different physical positions, so Ctrl+K could falsely match Ctrl+V
442
+ // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping)
443
+ // if the base layout key were always considered.
444
+ if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) {
445
+ const cp = parsed.codepoint;
446
+ const isLatinLetter = cp >= 97 && cp <= 122; // a-z
447
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp));
448
+ if (!isLatinLetter && !isKnownSymbol)
449
+ return true;
450
+ }
451
+ return false;
452
+ }
453
+ /**
454
+ * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~
455
+ * This is used by terminals when Kitty protocol is not enabled.
456
+ * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc.
457
+ */
458
+ function matchesModifyOtherKeys(data, expectedKeycode, expectedModifier) {
459
+ const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/);
460
+ if (!match)
461
+ return false;
462
+ const modValue = parseInt(match[1], 10);
463
+ const keycode = parseInt(match[2], 10);
464
+ // Convert from 1-indexed xterm format to our 0-indexed format
465
+ const actualMod = modValue - 1;
466
+ return keycode === expectedKeycode && actualMod === expectedModifier;
467
+ }
468
+ // =============================================================================
469
+ // Generic Key Matching
470
+ // =============================================================================
471
+ /**
472
+ * Get the control character for a key.
473
+ * Uses the universal formula: code & 0x1f (mask to lower 5 bits)
474
+ *
475
+ * Works for:
476
+ * - Letters a-z → 1-26
477
+ * - Symbols [\]_ → 27, 28, 29, 31
478
+ * - Also maps - to same as _ (same physical key on US keyboards)
479
+ */
480
+ function rawCtrlChar(key) {
481
+ const char = key.toLowerCase();
482
+ const code = char.charCodeAt(0);
483
+ if ((code >= 97 && code <= 122) || char === "[" || char === "\\" || char === "]" || char === "_") {
484
+ return String.fromCharCode(code & 0x1f);
485
+ }
486
+ // Handle - as _ (same physical key on US keyboards)
487
+ if (char === "-") {
488
+ return String.fromCharCode(31); // Same as Ctrl+_
489
+ }
490
+ return null;
491
+ }
492
+ function parseKeyId(keyId) {
493
+ const parts = keyId.toLowerCase().split("+");
494
+ const key = parts[parts.length - 1];
495
+ if (!key)
496
+ return null;
497
+ return {
498
+ key,
499
+ ctrl: parts.includes("ctrl"),
500
+ shift: parts.includes("shift"),
501
+ alt: parts.includes("alt"),
502
+ };
503
+ }
504
+ /**
505
+ * Match input data against a key identifier string.
506
+ *
507
+ * Supported key identifiers:
508
+ * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space"
509
+ * - Arrow keys: "up", "down", "left", "right"
510
+ * - Ctrl combinations: "ctrl+c", "ctrl+z", etc.
511
+ * - Shift combinations: "shift+tab", "shift+enter"
512
+ * - Alt combinations: "alt+enter", "alt+backspace"
513
+ * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x"
514
+ *
515
+ * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p")
516
+ *
517
+ * @param data - Raw input data from terminal
518
+ * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
519
+ */
520
+ export function matchesKey(data, keyId) {
521
+ const parsed = parseKeyId(keyId);
522
+ if (!parsed)
523
+ return false;
524
+ const { key, ctrl, shift, alt } = parsed;
525
+ let modifier = 0;
526
+ if (shift)
527
+ modifier |= MODIFIERS.shift;
528
+ if (alt)
529
+ modifier |= MODIFIERS.alt;
530
+ if (ctrl)
531
+ modifier |= MODIFIERS.ctrl;
532
+ switch (key) {
533
+ case "escape":
534
+ case "esc":
535
+ if (modifier !== 0)
536
+ return false;
537
+ return data === "\x1b" || matchesKittySequence(data, CODEPOINTS.escape, 0);
538
+ case "space":
539
+ if (!_kittyProtocolActive) {
540
+ if (ctrl && !alt && !shift && data === "\x00") {
541
+ return true;
542
+ }
543
+ if (alt && !ctrl && !shift && data === "\x1b ") {
544
+ return true;
545
+ }
546
+ }
547
+ if (modifier === 0) {
548
+ return data === " " || matchesKittySequence(data, CODEPOINTS.space, 0);
549
+ }
550
+ return matchesKittySequence(data, CODEPOINTS.space, modifier);
551
+ case "tab":
552
+ if (shift && !ctrl && !alt) {
553
+ return data === "\x1b[Z" || matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift);
554
+ }
555
+ if (modifier === 0) {
556
+ return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0);
557
+ }
558
+ return matchesKittySequence(data, CODEPOINTS.tab, modifier);
559
+ case "enter":
560
+ case "return":
561
+ if (shift && !ctrl && !alt) {
562
+ // CSI u sequences (standard Kitty protocol)
563
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) ||
564
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift)) {
565
+ return true;
566
+ }
567
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
568
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) {
569
+ return true;
570
+ }
571
+ // When Kitty protocol is active, legacy sequences are custom terminal mappings
572
+ // \x1b\r = Kitty's "map shift+enter send_text all \e\r"
573
+ // \n = Ghostty's "keybind = shift+enter=text:\n"
574
+ if (_kittyProtocolActive) {
575
+ return data === "\x1b\r" || data === "\n";
576
+ }
577
+ return false;
578
+ }
579
+ if (alt && !ctrl && !shift) {
580
+ // CSI u sequences (standard Kitty protocol)
581
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) ||
582
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt)) {
583
+ return true;
584
+ }
585
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
586
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) {
587
+ return true;
588
+ }
589
+ // \x1b\r is alt+enter only in legacy mode (no Kitty protocol)
590
+ // When Kitty protocol is active, alt+enter comes as CSI u sequence
591
+ if (!_kittyProtocolActive) {
592
+ return data === "\x1b\r";
593
+ }
594
+ return false;
595
+ }
596
+ if (modifier === 0) {
597
+ return (data === "\r" ||
598
+ (!_kittyProtocolActive && data === "\n") ||
599
+ data === "\x1bOM" || // SS3 M (numpad enter in some terminals)
600
+ matchesKittySequence(data, CODEPOINTS.enter, 0) ||
601
+ matchesKittySequence(data, CODEPOINTS.kpEnter, 0));
602
+ }
603
+ return (matchesKittySequence(data, CODEPOINTS.enter, modifier) ||
604
+ matchesKittySequence(data, CODEPOINTS.kpEnter, modifier));
605
+ case "backspace":
606
+ if (alt && !ctrl && !shift) {
607
+ if (data === "\x1b\x7f" || data === "\x1b\b") {
608
+ return true;
609
+ }
610
+ return matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt);
611
+ }
612
+ if (modifier === 0) {
613
+ return data === "\x7f" || data === "\x08" || matchesKittySequence(data, CODEPOINTS.backspace, 0);
614
+ }
615
+ return matchesKittySequence(data, CODEPOINTS.backspace, modifier);
616
+ case "insert":
617
+ if (modifier === 0) {
618
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) ||
619
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0));
620
+ }
621
+ if (matchesLegacyModifierSequence(data, "insert", modifier)) {
622
+ return true;
623
+ }
624
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier);
625
+ case "delete":
626
+ if (modifier === 0) {
627
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) ||
628
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0));
629
+ }
630
+ if (matchesLegacyModifierSequence(data, "delete", modifier)) {
631
+ return true;
632
+ }
633
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier);
634
+ case "clear":
635
+ if (modifier === 0) {
636
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear);
637
+ }
638
+ return matchesLegacyModifierSequence(data, "clear", modifier);
639
+ case "home":
640
+ if (modifier === 0) {
641
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) ||
642
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0));
643
+ }
644
+ if (matchesLegacyModifierSequence(data, "home", modifier)) {
645
+ return true;
646
+ }
647
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier);
648
+ case "end":
649
+ if (modifier === 0) {
650
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) ||
651
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0));
652
+ }
653
+ if (matchesLegacyModifierSequence(data, "end", modifier)) {
654
+ return true;
655
+ }
656
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier);
657
+ case "pageup":
658
+ if (modifier === 0) {
659
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) ||
660
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0));
661
+ }
662
+ if (matchesLegacyModifierSequence(data, "pageUp", modifier)) {
663
+ return true;
664
+ }
665
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier);
666
+ case "pagedown":
667
+ if (modifier === 0) {
668
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) ||
669
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0));
670
+ }
671
+ if (matchesLegacyModifierSequence(data, "pageDown", modifier)) {
672
+ return true;
673
+ }
674
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier);
675
+ case "up":
676
+ if (alt && !ctrl && !shift) {
677
+ return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt);
678
+ }
679
+ if (modifier === 0) {
680
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) ||
681
+ matchesKittySequence(data, ARROW_CODEPOINTS.up, 0));
682
+ }
683
+ if (matchesLegacyModifierSequence(data, "up", modifier)) {
684
+ return true;
685
+ }
686
+ return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier);
687
+ case "down":
688
+ if (alt && !ctrl && !shift) {
689
+ return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt);
690
+ }
691
+ if (modifier === 0) {
692
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) ||
693
+ matchesKittySequence(data, ARROW_CODEPOINTS.down, 0));
694
+ }
695
+ if (matchesLegacyModifierSequence(data, "down", modifier)) {
696
+ return true;
697
+ }
698
+ return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier);
699
+ case "left":
700
+ if (alt && !ctrl && !shift) {
701
+ return (data === "\x1b[1;3D" ||
702
+ (!_kittyProtocolActive && data === "\x1bB") ||
703
+ data === "\x1bb" ||
704
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt));
705
+ }
706
+ if (ctrl && !alt && !shift) {
707
+ return (data === "\x1b[1;5D" ||
708
+ matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) ||
709
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl));
710
+ }
711
+ if (modifier === 0) {
712
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) ||
713
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, 0));
714
+ }
715
+ if (matchesLegacyModifierSequence(data, "left", modifier)) {
716
+ return true;
717
+ }
718
+ return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier);
719
+ case "right":
720
+ if (alt && !ctrl && !shift) {
721
+ return (data === "\x1b[1;3C" ||
722
+ (!_kittyProtocolActive && data === "\x1bF") ||
723
+ data === "\x1bf" ||
724
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt));
725
+ }
726
+ if (ctrl && !alt && !shift) {
727
+ return (data === "\x1b[1;5C" ||
728
+ matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) ||
729
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl));
730
+ }
731
+ if (modifier === 0) {
732
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) ||
733
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, 0));
734
+ }
735
+ if (matchesLegacyModifierSequence(data, "right", modifier)) {
736
+ return true;
737
+ }
738
+ return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier);
739
+ case "f1":
740
+ case "f2":
741
+ case "f3":
742
+ case "f4":
743
+ case "f5":
744
+ case "f6":
745
+ case "f7":
746
+ case "f8":
747
+ case "f9":
748
+ case "f10":
749
+ case "f11":
750
+ case "f12": {
751
+ if (modifier !== 0) {
752
+ return false;
753
+ }
754
+ const functionKey = key;
755
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]);
756
+ }
757
+ }
758
+ // Handle single letter keys (a-z) and some symbols
759
+ if (key.length === 1 && ((key >= "a" && key <= "z") || SYMBOL_KEYS.has(key))) {
760
+ const codepoint = key.charCodeAt(0);
761
+ const rawCtrl = rawCtrlChar(key);
762
+ if (ctrl && alt && !shift && !_kittyProtocolActive && rawCtrl) {
763
+ // Legacy: ctrl+alt+key is ESC followed by the control character
764
+ return data === `\x1b${rawCtrl}`;
765
+ }
766
+ if (alt && !ctrl && !shift && !_kittyProtocolActive && key >= "a" && key <= "z") {
767
+ // Legacy: alt+letter is ESC followed by the letter
768
+ if (data === `\x1b${key}`)
769
+ return true;
770
+ }
771
+ if (ctrl && !shift && !alt) {
772
+ // Legacy: ctrl+key sends the control character
773
+ if (rawCtrl && data === rawCtrl)
774
+ return true;
775
+ return matchesKittySequence(data, codepoint, MODIFIERS.ctrl);
776
+ }
777
+ if (ctrl && shift && !alt) {
778
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl);
779
+ }
780
+ if (shift && !ctrl && !alt) {
781
+ // Legacy: shift+letter produces uppercase
782
+ if (data === key.toUpperCase())
783
+ return true;
784
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift);
785
+ }
786
+ if (modifier !== 0) {
787
+ return matchesKittySequence(data, codepoint, modifier);
788
+ }
789
+ // Check both raw char and Kitty sequence (needed for release events)
790
+ return data === key || matchesKittySequence(data, codepoint, 0);
791
+ }
792
+ return false;
793
+ }
794
+ /**
795
+ * Parse input data and return the key identifier if recognized.
796
+ *
797
+ * @param data - Raw input data from terminal
798
+ * @returns Key identifier string (e.g., "ctrl+c") or undefined
799
+ */
800
+ export function parseKey(data) {
801
+ const kitty = parseKittySequence(data);
802
+ if (kitty) {
803
+ const { codepoint, baseLayoutKey, modifier } = kitty;
804
+ const mods = [];
805
+ const effectiveMod = modifier & ~LOCK_MASK;
806
+ if (effectiveMod & MODIFIERS.shift)
807
+ mods.push("shift");
808
+ if (effectiveMod & MODIFIERS.ctrl)
809
+ mods.push("ctrl");
810
+ if (effectiveMod & MODIFIERS.alt)
811
+ mods.push("alt");
812
+ // Use base layout key only when codepoint is not a recognized Latin
813
+ // letter (a-z) or symbol (/, -, [, ;, etc.). For those, the codepoint
814
+ // is authoritative regardless of physical key position. This prevents
815
+ // remapped layouts (Dvorak, Colemak, xremap, etc.) from reporting the
816
+ // wrong key name based on the QWERTY physical position.
817
+ const isLatinLetter = codepoint >= 97 && codepoint <= 122; // a-z
818
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(codepoint));
819
+ const effectiveCodepoint = isLatinLetter || isKnownSymbol ? codepoint : (baseLayoutKey ?? codepoint);
820
+ let keyName;
821
+ if (effectiveCodepoint === CODEPOINTS.escape)
822
+ keyName = "escape";
823
+ else if (effectiveCodepoint === CODEPOINTS.tab)
824
+ keyName = "tab";
825
+ else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter)
826
+ keyName = "enter";
827
+ else if (effectiveCodepoint === CODEPOINTS.space)
828
+ keyName = "space";
829
+ else if (effectiveCodepoint === CODEPOINTS.backspace)
830
+ keyName = "backspace";
831
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete)
832
+ keyName = "delete";
833
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert)
834
+ keyName = "insert";
835
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home)
836
+ keyName = "home";
837
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end)
838
+ keyName = "end";
839
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp)
840
+ keyName = "pageUp";
841
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown)
842
+ keyName = "pageDown";
843
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.up)
844
+ keyName = "up";
845
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.down)
846
+ keyName = "down";
847
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.left)
848
+ keyName = "left";
849
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.right)
850
+ keyName = "right";
851
+ else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122)
852
+ keyName = String.fromCharCode(effectiveCodepoint);
853
+ else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint)))
854
+ keyName = String.fromCharCode(effectiveCodepoint);
855
+ if (keyName) {
856
+ return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName;
857
+ }
858
+ }
859
+ // Mode-aware legacy sequences
860
+ // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings:
861
+ // - \x1b\r = shift+enter (Kitty mapping), not alt+enter
862
+ // - \n = shift+enter (Ghostty mapping)
863
+ if (_kittyProtocolActive) {
864
+ if (data === "\x1b\r" || data === "\n")
865
+ return "shift+enter";
866
+ }
867
+ const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data];
868
+ if (legacySequenceKeyId)
869
+ return legacySequenceKeyId;
870
+ // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences)
871
+ if (data === "\x1b")
872
+ return "escape";
873
+ if (data === "\x1c")
874
+ return "ctrl+\\";
875
+ if (data === "\x1d")
876
+ return "ctrl+]";
877
+ if (data === "\x1f")
878
+ return "ctrl+-";
879
+ if (data === "\x1b\x1b")
880
+ return "ctrl+alt+[";
881
+ if (data === "\x1b\x1c")
882
+ return "ctrl+alt+\\";
883
+ if (data === "\x1b\x1d")
884
+ return "ctrl+alt+]";
885
+ if (data === "\x1b\x1f")
886
+ return "ctrl+alt+-";
887
+ if (data === "\t")
888
+ return "tab";
889
+ if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM")
890
+ return "enter";
891
+ if (data === "\x00")
892
+ return "ctrl+space";
893
+ if (data === " ")
894
+ return "space";
895
+ if (data === "\x7f" || data === "\x08")
896
+ return "backspace";
897
+ if (data === "\x1b[Z")
898
+ return "shift+tab";
899
+ if (!_kittyProtocolActive && data === "\x1b\r")
900
+ return "alt+enter";
901
+ if (!_kittyProtocolActive && data === "\x1b ")
902
+ return "alt+space";
903
+ if (data === "\x1b\x7f" || data === "\x1b\b")
904
+ return "alt+backspace";
905
+ if (!_kittyProtocolActive && data === "\x1bB")
906
+ return "alt+left";
907
+ if (!_kittyProtocolActive && data === "\x1bF")
908
+ return "alt+right";
909
+ if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") {
910
+ const code = data.charCodeAt(1);
911
+ if (code >= 1 && code <= 26) {
912
+ return `ctrl+alt+${String.fromCharCode(code + 96)}`;
913
+ }
914
+ // Legacy alt+letter (ESC followed by letter a-z)
915
+ if (code >= 97 && code <= 122) {
916
+ return `alt+${String.fromCharCode(code)}`;
917
+ }
918
+ }
919
+ if (data === "\x1b[A")
920
+ return "up";
921
+ if (data === "\x1b[B")
922
+ return "down";
923
+ if (data === "\x1b[C")
924
+ return "right";
925
+ if (data === "\x1b[D")
926
+ return "left";
927
+ if (data === "\x1b[H" || data === "\x1bOH")
928
+ return "home";
929
+ if (data === "\x1b[F" || data === "\x1bOF")
930
+ return "end";
931
+ if (data === "\x1b[3~")
932
+ return "delete";
933
+ if (data === "\x1b[5~")
934
+ return "pageUp";
935
+ if (data === "\x1b[6~")
936
+ return "pageDown";
937
+ // Raw Ctrl+letter
938
+ if (data.length === 1) {
939
+ const code = data.charCodeAt(0);
940
+ if (code >= 1 && code <= 26) {
941
+ return `ctrl+${String.fromCharCode(code + 96)}`;
942
+ }
943
+ if (code >= 32 && code <= 126) {
944
+ return data;
945
+ }
946
+ }
947
+ return undefined;
948
+ }