@aexol/spectral 0.7.7 → 0.8.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 (221) hide show
  1. package/dist/agent/agents.js +4 -4
  2. package/dist/agent/index.js +24 -148
  3. package/dist/cli.js +25 -220
  4. package/dist/commands/serve.js +1 -1
  5. package/dist/extensions/spectral-vision-fallback.js +225 -0
  6. package/dist/mcp/agent-dir.js +1 -1
  7. package/dist/mcp/config.js +3 -3
  8. package/dist/mcp/sampling-handler.js +1 -1
  9. package/dist/mcp/server-manager.js +5 -1
  10. package/dist/memory/commands/status.js +6 -6
  11. package/dist/memory/commands/view.js +16 -14
  12. package/dist/memory/compaction.js +33 -5
  13. package/dist/memory/config.js +3 -3
  14. package/dist/memory/debug-log.js +1 -1
  15. package/dist/memory/observer.js +2 -2
  16. package/dist/memory/prompts.js +5 -5
  17. package/dist/memory/tokens.js +1 -1
  18. package/dist/memory/tools/read-project-observations.js +2 -2
  19. package/dist/memory/tools/recall-observation.js +4 -4
  20. package/dist/relay/auto-research.js +23 -23
  21. package/dist/relay/dispatcher.js +28 -2
  22. package/dist/relay/models-fetch.js +15 -3
  23. package/dist/{pi → sdk}/coding-agent/cli/args.js +4 -4
  24. package/dist/{pi → sdk}/coding-agent/config.js +9 -20
  25. package/dist/{pi → sdk}/coding-agent/core/agent-session.js +5 -17
  26. package/dist/{pi → sdk}/coding-agent/core/compaction/compaction.js +161 -5
  27. package/dist/{pi → sdk}/coding-agent/core/extensions/loader.js +0 -6
  28. package/dist/{pi → sdk}/coding-agent/core/extensions/runner.js +7 -1
  29. package/dist/{pi → sdk}/coding-agent/core/keybindings.js +129 -2
  30. package/dist/{pi → sdk}/coding-agent/core/model-registry.js +11 -4
  31. package/dist/{pi → sdk}/coding-agent/core/package-manager.js +5 -5
  32. package/dist/{pi → sdk}/coding-agent/core/sdk.js +1 -1
  33. package/dist/{pi → sdk}/coding-agent/core/session-manager.js +4 -4
  34. package/dist/{pi → sdk}/coding-agent/core/settings-manager.js +20 -0
  35. package/dist/{pi → sdk}/coding-agent/core/telemetry.js +1 -1
  36. package/dist/{pi → sdk}/coding-agent/core/tools/bash.js +17 -63
  37. package/dist/{pi → sdk}/coding-agent/core/tools/edit.js +4 -141
  38. package/dist/{pi → sdk}/coding-agent/core/tools/find.js +0 -11
  39. package/dist/{pi → sdk}/coding-agent/core/tools/grep.js +0 -11
  40. package/dist/{pi → sdk}/coding-agent/core/tools/ls.js +0 -11
  41. package/dist/{pi → sdk}/coding-agent/core/tools/read.js +0 -12
  42. package/dist/{pi → sdk}/coding-agent/core/tools/render-utils.js +1 -14
  43. package/dist/{pi → sdk}/coding-agent/core/tools/write.js +2 -97
  44. package/dist/{pi → sdk}/coding-agent/migrations.js +3 -3
  45. package/dist/{pi → sdk}/coding-agent/modes/interactive/components/keybinding-hints.js +1 -1
  46. package/dist/sdk/coding-agent/modes/interactive/components/visual-truncate.js +26 -0
  47. package/dist/{pi → sdk}/coding-agent/modes/interactive/theme/theme.js +1 -2
  48. package/dist/{pi → sdk}/coding-agent/utils/tools-manager.js +1 -1
  49. package/dist/{pi → sdk}/coding-agent/utils/version-check.js +2 -2
  50. package/dist/{pi → sdk}/coding-agent/utils/windows-self-update.js +1 -1
  51. package/dist/server/{pi-bridge.js → agent-bridge.js} +158 -89
  52. package/dist/server/handlers/sessions.js +21 -0
  53. package/dist/server/session-stream.js +12 -6
  54. package/package.json +6 -3
  55. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +0 -248
  56. package/dist/pi/coding-agent/core/export-html/index.js +0 -225
  57. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +0 -107
  58. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +0 -32
  59. package/dist/pi/tui/autocomplete.js +0 -631
  60. package/dist/pi/tui/components/box.js +0 -103
  61. package/dist/pi/tui/components/cancellable-loader.js +0 -34
  62. package/dist/pi/tui/components/editor.js +0 -1915
  63. package/dist/pi/tui/components/image.js +0 -88
  64. package/dist/pi/tui/components/input.js +0 -425
  65. package/dist/pi/tui/components/loader.js +0 -68
  66. package/dist/pi/tui/components/markdown.js +0 -633
  67. package/dist/pi/tui/components/select-list.js +0 -158
  68. package/dist/pi/tui/components/settings-list.js +0 -184
  69. package/dist/pi/tui/components/spacer.js +0 -22
  70. package/dist/pi/tui/components/text.js +0 -88
  71. package/dist/pi/tui/components/truncated-text.js +0 -50
  72. package/dist/pi/tui/editor-component.js +0 -1
  73. package/dist/pi/tui/fuzzy.js +0 -109
  74. package/dist/pi/tui/index.js +0 -31
  75. package/dist/pi/tui/keybindings.js +0 -173
  76. package/dist/pi/tui/keys.js +0 -1172
  77. package/dist/pi/tui/kill-ring.js +0 -43
  78. package/dist/pi/tui/stdin-buffer.js +0 -360
  79. package/dist/pi/tui/terminal-image.js +0 -335
  80. package/dist/pi/tui/terminal.js +0 -324
  81. package/dist/pi/tui/tui.js +0 -1076
  82. package/dist/pi/tui/undo-stack.js +0 -24
  83. package/dist/pi/tui/utils.js +0 -1016
  84. /package/dist/{pi → sdk}/agent-core/agent-loop.js +0 -0
  85. /package/dist/{pi → sdk}/agent-core/agent.js +0 -0
  86. /package/dist/{pi → sdk}/agent-core/harness/agent-harness.js +0 -0
  87. /package/dist/{pi → sdk}/agent-core/harness/compaction/branch-summarization.js +0 -0
  88. /package/dist/{pi → sdk}/agent-core/harness/compaction/compaction.js +0 -0
  89. /package/dist/{pi → sdk}/agent-core/harness/compaction/utils.js +0 -0
  90. /package/dist/{pi → sdk}/agent-core/harness/env/nodejs.js +0 -0
  91. /package/dist/{pi → sdk}/agent-core/harness/messages.js +0 -0
  92. /package/dist/{pi → sdk}/agent-core/harness/prompt-templates.js +0 -0
  93. /package/dist/{pi → sdk}/agent-core/harness/session/jsonl-repo.js +0 -0
  94. /package/dist/{pi → sdk}/agent-core/harness/session/jsonl-storage.js +0 -0
  95. /package/dist/{pi → sdk}/agent-core/harness/session/memory-repo.js +0 -0
  96. /package/dist/{pi → sdk}/agent-core/harness/session/memory-storage.js +0 -0
  97. /package/dist/{pi → sdk}/agent-core/harness/session/repo-utils.js +0 -0
  98. /package/dist/{pi → sdk}/agent-core/harness/session/session.js +0 -0
  99. /package/dist/{pi → sdk}/agent-core/harness/session/uuid.js +0 -0
  100. /package/dist/{pi → sdk}/agent-core/harness/skills.js +0 -0
  101. /package/dist/{pi → sdk}/agent-core/harness/system-prompt.js +0 -0
  102. /package/dist/{pi → sdk}/agent-core/harness/types.js +0 -0
  103. /package/dist/{pi → sdk}/agent-core/harness/utils/shell-output.js +0 -0
  104. /package/dist/{pi → sdk}/agent-core/harness/utils/truncate.js +0 -0
  105. /package/dist/{pi → sdk}/agent-core/index.js +0 -0
  106. /package/dist/{pi → sdk}/agent-core/node.js +0 -0
  107. /package/dist/{pi → sdk}/agent-core/proxy.js +0 -0
  108. /package/dist/{pi → sdk}/agent-core/types.js +0 -0
  109. /package/dist/{pi → sdk}/ai/api-registry.js +0 -0
  110. /package/dist/{pi → sdk}/ai/cli.js +0 -0
  111. /package/dist/{pi → sdk}/ai/env-api-keys.js +0 -0
  112. /package/dist/{pi → sdk}/ai/image-models.generated.js +0 -0
  113. /package/dist/{pi → sdk}/ai/image-models.js +0 -0
  114. /package/dist/{pi → sdk}/ai/images-api-registry.js +0 -0
  115. /package/dist/{pi → sdk}/ai/images.js +0 -0
  116. /package/dist/{pi → sdk}/ai/index.js +0 -0
  117. /package/dist/{pi → sdk}/ai/models.generated.js +0 -0
  118. /package/dist/{pi → sdk}/ai/models.js +0 -0
  119. /package/dist/{pi → sdk}/ai/oauth.js +0 -0
  120. /package/dist/{pi → sdk}/ai/providers/anthropic.js +0 -0
  121. /package/dist/{pi → sdk}/ai/providers/faux.js +0 -0
  122. /package/dist/{pi → sdk}/ai/providers/github-copilot-headers.js +0 -0
  123. /package/dist/{pi → sdk}/ai/providers/openai-completions.js +0 -0
  124. /package/dist/{pi → sdk}/ai/providers/openai-prompt-cache.js +0 -0
  125. /package/dist/{pi → sdk}/ai/providers/register-builtins.js +0 -0
  126. /package/dist/{pi → sdk}/ai/providers/simple-options.js +0 -0
  127. /package/dist/{pi → sdk}/ai/providers/transform-messages.js +0 -0
  128. /package/dist/{pi → sdk}/ai/session-resources.js +0 -0
  129. /package/dist/{pi → sdk}/ai/stream.js +0 -0
  130. /package/dist/{pi → sdk}/ai/types.js +0 -0
  131. /package/dist/{pi → sdk}/ai/utils/diagnostics.js +0 -0
  132. /package/dist/{pi → sdk}/ai/utils/event-stream.js +0 -0
  133. /package/dist/{pi → sdk}/ai/utils/hash.js +0 -0
  134. /package/dist/{pi → sdk}/ai/utils/headers.js +0 -0
  135. /package/dist/{pi → sdk}/ai/utils/json-parse.js +0 -0
  136. /package/dist/{pi → sdk}/ai/utils/node-http-proxy.js +0 -0
  137. /package/dist/{pi → sdk}/ai/utils/oauth/anthropic.js +0 -0
  138. /package/dist/{pi → sdk}/ai/utils/oauth/device-code.js +0 -0
  139. /package/dist/{pi → sdk}/ai/utils/oauth/github-copilot.js +0 -0
  140. /package/dist/{pi → sdk}/ai/utils/oauth/index.js +0 -0
  141. /package/dist/{pi → sdk}/ai/utils/oauth/oauth-page.js +0 -0
  142. /package/dist/{pi → sdk}/ai/utils/oauth/openai-codex.js +0 -0
  143. /package/dist/{pi → sdk}/ai/utils/oauth/pkce.js +0 -0
  144. /package/dist/{pi → sdk}/ai/utils/oauth/types.js +0 -0
  145. /package/dist/{pi → sdk}/ai/utils/overflow.js +0 -0
  146. /package/dist/{pi → sdk}/ai/utils/sanitize-unicode.js +0 -0
  147. /package/dist/{pi → sdk}/ai/utils/typebox-helpers.js +0 -0
  148. /package/dist/{pi → sdk}/ai/utils/validation.js +0 -0
  149. /package/dist/{pi → sdk}/coding-agent/bun/cli.js +0 -0
  150. /package/dist/{pi → sdk}/coding-agent/bun/restore-sandbox-env.js +0 -0
  151. /package/dist/{pi → sdk}/coding-agent/cli/file-processor.js +0 -0
  152. /package/dist/{pi → sdk}/coding-agent/cli/initial-message.js +0 -0
  153. /package/dist/{pi → sdk}/coding-agent/cli.js +0 -0
  154. /package/dist/{pi → sdk}/coding-agent/core/agent-session-runtime.js +0 -0
  155. /package/dist/{pi → sdk}/coding-agent/core/agent-session-services.js +0 -0
  156. /package/dist/{pi → sdk}/coding-agent/core/auth-guidance.js +0 -0
  157. /package/dist/{pi → sdk}/coding-agent/core/auth-storage.js +0 -0
  158. /package/dist/{pi → sdk}/coding-agent/core/bash-executor.js +0 -0
  159. /package/dist/{pi → sdk}/coding-agent/core/compaction/branch-summarization.js +0 -0
  160. /package/dist/{pi → sdk}/coding-agent/core/compaction/index.js +0 -0
  161. /package/dist/{pi → sdk}/coding-agent/core/compaction/utils.js +0 -0
  162. /package/dist/{pi → sdk}/coding-agent/core/defaults.js +0 -0
  163. /package/dist/{pi → sdk}/coding-agent/core/diagnostics.js +0 -0
  164. /package/dist/{pi → sdk}/coding-agent/core/event-bus.js +0 -0
  165. /package/dist/{pi → sdk}/coding-agent/core/exec.js +0 -0
  166. /package/dist/{pi → sdk}/coding-agent/core/extensions/index.js +0 -0
  167. /package/dist/{pi → sdk}/coding-agent/core/extensions/types.js +0 -0
  168. /package/dist/{pi → sdk}/coding-agent/core/extensions/wrapper.js +0 -0
  169. /package/dist/{pi → sdk}/coding-agent/core/footer-data-provider.js +0 -0
  170. /package/dist/{pi → sdk}/coding-agent/core/http-dispatcher.js +0 -0
  171. /package/dist/{pi → sdk}/coding-agent/core/index.js +0 -0
  172. /package/dist/{pi → sdk}/coding-agent/core/messages.js +0 -0
  173. /package/dist/{pi → sdk}/coding-agent/core/model-resolver.js +0 -0
  174. /package/dist/{pi → sdk}/coding-agent/core/output-guard.js +0 -0
  175. /package/dist/{pi → sdk}/coding-agent/core/prompt-templates.js +0 -0
  176. /package/dist/{pi → sdk}/coding-agent/core/provider-display-names.js +0 -0
  177. /package/dist/{pi → sdk}/coding-agent/core/resolve-config-value.js +0 -0
  178. /package/dist/{pi → sdk}/coding-agent/core/resource-loader.js +0 -0
  179. /package/dist/{pi → sdk}/coding-agent/core/session-cwd.js +0 -0
  180. /package/dist/{pi → sdk}/coding-agent/core/skills.js +0 -0
  181. /package/dist/{pi → sdk}/coding-agent/core/slash-commands.js +0 -0
  182. /package/dist/{pi → sdk}/coding-agent/core/source-info.js +0 -0
  183. /package/dist/{pi → sdk}/coding-agent/core/system-prompt.js +0 -0
  184. /package/dist/{pi → sdk}/coding-agent/core/timings.js +0 -0
  185. /package/dist/{pi → sdk}/coding-agent/core/tools/edit-diff.js +0 -0
  186. /package/dist/{pi → sdk}/coding-agent/core/tools/file-mutation-queue.js +0 -0
  187. /package/dist/{pi → sdk}/coding-agent/core/tools/index.js +0 -0
  188. /package/dist/{pi → sdk}/coding-agent/core/tools/output-accumulator.js +0 -0
  189. /package/dist/{pi → sdk}/coding-agent/core/tools/path-utils.js +0 -0
  190. /package/dist/{pi → sdk}/coding-agent/core/tools/tool-definition-wrapper.js +0 -0
  191. /package/dist/{pi → sdk}/coding-agent/core/tools/truncate.js +0 -0
  192. /package/dist/{pi → sdk}/coding-agent/index.js +0 -0
  193. /package/dist/{pi → sdk}/coding-agent/main.js +0 -0
  194. /package/dist/{pi → sdk}/coding-agent/modes/index.js +0 -0
  195. /package/dist/{pi → sdk}/coding-agent/modes/interactive/components/diff.js +0 -0
  196. /package/dist/{pi → sdk}/coding-agent/modes/interactive/interactive-mode.js +0 -0
  197. /package/dist/{pi → sdk}/coding-agent/modes/print-mode.js +0 -0
  198. /package/dist/{pi → sdk}/coding-agent/modes/rpc/jsonl.js +0 -0
  199. /package/dist/{pi → sdk}/coding-agent/modes/rpc/rpc-client.js +0 -0
  200. /package/dist/{pi → sdk}/coding-agent/modes/rpc/rpc-mode.js +0 -0
  201. /package/dist/{pi → sdk}/coding-agent/modes/rpc/rpc-types.js +0 -0
  202. /package/dist/{pi → sdk}/coding-agent/utils/ansi.js +0 -0
  203. /package/dist/{pi → sdk}/coding-agent/utils/changelog.js +0 -0
  204. /package/dist/{pi → sdk}/coding-agent/utils/child-process.js +0 -0
  205. /package/dist/{pi → sdk}/coding-agent/utils/clipboard-image.js +0 -0
  206. /package/dist/{pi → sdk}/coding-agent/utils/clipboard-native.js +0 -0
  207. /package/dist/{pi → sdk}/coding-agent/utils/clipboard.js +0 -0
  208. /package/dist/{pi → sdk}/coding-agent/utils/exif-orientation.js +0 -0
  209. /package/dist/{pi → sdk}/coding-agent/utils/frontmatter.js +0 -0
  210. /package/dist/{pi → sdk}/coding-agent/utils/fs-watch.js +0 -0
  211. /package/dist/{pi → sdk}/coding-agent/utils/git.js +0 -0
  212. /package/dist/{pi → sdk}/coding-agent/utils/html.js +0 -0
  213. /package/dist/{pi → sdk}/coding-agent/utils/image-convert.js +0 -0
  214. /package/dist/{pi → sdk}/coding-agent/utils/image-resize.js +0 -0
  215. /package/dist/{pi → sdk}/coding-agent/utils/mime.js +0 -0
  216. /package/dist/{pi → sdk}/coding-agent/utils/paths.js +0 -0
  217. /package/dist/{pi → sdk}/coding-agent/utils/photon.js +0 -0
  218. /package/dist/{pi → sdk}/coding-agent/utils/pi-user-agent.js +0 -0
  219. /package/dist/{pi → sdk}/coding-agent/utils/shell.js +0 -0
  220. /package/dist/{pi → sdk}/coding-agent/utils/sleep.js +0 -0
  221. /package/dist/{pi → sdk}/coding-agent/utils/syntax-highlight.js +0 -0
@@ -1,335 +0,0 @@
1
- let cachedCapabilities = null;
2
- // Default cell dimensions - updated by TUI when terminal responds to query
3
- let cellDimensions = { widthPx: 9, heightPx: 18 };
4
- export function getCellDimensions() {
5
- return cellDimensions;
6
- }
7
- export function setCellDimensions(dims) {
8
- cellDimensions = dims;
9
- }
10
- export function detectCapabilities() {
11
- const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || "";
12
- const term = process.env.TERM?.toLowerCase() || "";
13
- const colorTerm = process.env.COLORTERM?.toLowerCase() || "";
14
- const hasTrueColorHint = colorTerm === "truecolor" || colorTerm === "24bit";
15
- // tmux and screen swallow OSC 8 by default (passthrough is opt-in and wraps
16
- // sequences differently). Force hyperlinks off whenever we detect them, even
17
- // when the outer terminal would otherwise support OSC 8. Image protocols are
18
- // also unreliable under tmux/screen, so leave `images: null` for safety.
19
- const inTmuxOrScreen = !!process.env.TMUX || term.startsWith("tmux") || term.startsWith("screen");
20
- if (inTmuxOrScreen) {
21
- return { images: null, trueColor: hasTrueColorHint, hyperlinks: false };
22
- }
23
- if (process.env.KITTY_WINDOW_ID || termProgram === "kitty") {
24
- return { images: "kitty", trueColor: true, hyperlinks: true };
25
- }
26
- if (termProgram === "ghostty" || term.includes("ghostty") || process.env.GHOSTTY_RESOURCES_DIR) {
27
- return { images: "kitty", trueColor: true, hyperlinks: true };
28
- }
29
- if (process.env.WEZTERM_PANE || termProgram === "wezterm") {
30
- return { images: "kitty", trueColor: true, hyperlinks: true };
31
- }
32
- if (process.env.ITERM_SESSION_ID || termProgram === "iterm.app") {
33
- return { images: "iterm2", trueColor: true, hyperlinks: true };
34
- }
35
- if (termProgram === "vscode") {
36
- return { images: null, trueColor: true, hyperlinks: true };
37
- }
38
- if (termProgram === "alacritty") {
39
- return { images: null, trueColor: true, hyperlinks: true };
40
- }
41
- // Unknown terminal: be conservative. OSC 8 is rendered invisibly as "just
42
- // text" on terminals that swallow it, which means the URL disappears from
43
- // the rendered output. Default to the legacy `text (url)` behavior unless we
44
- // have positively identified a hyperlink-capable terminal above.
45
- return { images: null, trueColor: hasTrueColorHint || !!process.env.WT_SESSION, hyperlinks: false };
46
- }
47
- export function getCapabilities() {
48
- if (!cachedCapabilities) {
49
- cachedCapabilities = detectCapabilities();
50
- }
51
- return cachedCapabilities;
52
- }
53
- export function resetCapabilitiesCache() {
54
- cachedCapabilities = null;
55
- }
56
- /** Override the cached capabilities. Useful in tests to exercise both code paths. */
57
- export function setCapabilities(caps) {
58
- cachedCapabilities = caps;
59
- }
60
- const KITTY_PREFIX = "\x1b_G";
61
- const ITERM2_PREFIX = "\x1b]1337;File=";
62
- export function isImageLine(line) {
63
- // Fast path: sequence at line start (single-row images)
64
- if (line.startsWith(KITTY_PREFIX) || line.startsWith(ITERM2_PREFIX)) {
65
- return true;
66
- }
67
- // Slow path: sequence elsewhere (multi-row images have cursor-up prefix)
68
- return line.includes(KITTY_PREFIX) || line.includes(ITERM2_PREFIX);
69
- }
70
- /**
71
- * Generate a random image ID for Kitty graphics protocol.
72
- * Uses random IDs to avoid collisions between different module instances
73
- * (e.g., main app vs extensions).
74
- */
75
- export function allocateImageId() {
76
- // Use random ID in range [1, 0xffffffff] to avoid collisions
77
- return Math.floor(Math.random() * 0xfffffffe) + 1;
78
- }
79
- export function encodeKitty(base64Data, options = {}) {
80
- const CHUNK_SIZE = 4096;
81
- const params = ["a=T", "f=100", "q=2"];
82
- if (options.moveCursor === false)
83
- params.push("C=1");
84
- if (options.columns)
85
- params.push(`c=${options.columns}`);
86
- if (options.rows)
87
- params.push(`r=${options.rows}`);
88
- if (options.imageId)
89
- params.push(`i=${options.imageId}`);
90
- if (base64Data.length <= CHUNK_SIZE) {
91
- return `\x1b_G${params.join(",")};${base64Data}\x1b\\`;
92
- }
93
- const chunks = [];
94
- let offset = 0;
95
- let isFirst = true;
96
- while (offset < base64Data.length) {
97
- const chunk = base64Data.slice(offset, offset + CHUNK_SIZE);
98
- const isLast = offset + CHUNK_SIZE >= base64Data.length;
99
- if (isFirst) {
100
- chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`);
101
- isFirst = false;
102
- }
103
- else if (isLast) {
104
- chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`);
105
- }
106
- else {
107
- chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`);
108
- }
109
- offset += CHUNK_SIZE;
110
- }
111
- return chunks.join("");
112
- }
113
- /**
114
- * Delete a Kitty graphics image by ID.
115
- * Uses uppercase 'I' to also free the image data.
116
- */
117
- export function deleteKittyImage(imageId) {
118
- return `\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`;
119
- }
120
- /**
121
- * Delete all visible Kitty graphics images.
122
- * Uses uppercase 'A' to also free the image data.
123
- */
124
- export function deleteAllKittyImages() {
125
- return "\x1b_Ga=d,d=A,q=2\x1b\\";
126
- }
127
- export function encodeITerm2(base64Data, options = {}) {
128
- const params = [`inline=${options.inline !== false ? 1 : 0}`];
129
- if (options.width !== undefined)
130
- params.push(`width=${options.width}`);
131
- if (options.height !== undefined)
132
- params.push(`height=${options.height}`);
133
- if (options.name) {
134
- const nameBase64 = Buffer.from(options.name).toString("base64");
135
- params.push(`name=${nameBase64}`);
136
- }
137
- if (options.preserveAspectRatio === false) {
138
- params.push("preserveAspectRatio=0");
139
- }
140
- return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`;
141
- }
142
- export function calculateImageCellSize(imageDimensions, maxWidthCells, maxHeightCells, cellDimensions = { widthPx: 9, heightPx: 18 }) {
143
- const maxWidth = Math.max(1, Math.floor(maxWidthCells));
144
- const maxHeight = maxHeightCells === undefined ? undefined : Math.max(1, Math.floor(maxHeightCells));
145
- const imageWidth = Math.max(1, imageDimensions.widthPx);
146
- const imageHeight = Math.max(1, imageDimensions.heightPx);
147
- const widthScale = (maxWidth * cellDimensions.widthPx) / imageWidth;
148
- const heightScale = maxHeight === undefined ? widthScale : (maxHeight * cellDimensions.heightPx) / imageHeight;
149
- const scale = Math.min(widthScale, heightScale);
150
- const scaledWidthPx = imageWidth * scale;
151
- const scaledHeightPx = imageHeight * scale;
152
- const columns = Math.ceil(scaledWidthPx / cellDimensions.widthPx);
153
- const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx);
154
- return {
155
- columns: Math.max(1, Math.min(maxWidth, columns)),
156
- rows: Math.max(1, maxHeight === undefined ? rows : Math.min(maxHeight, rows)),
157
- };
158
- }
159
- export function calculateImageRows(imageDimensions, targetWidthCells, cellDimensions = { widthPx: 9, heightPx: 18 }) {
160
- return calculateImageCellSize(imageDimensions, targetWidthCells, undefined, cellDimensions).rows;
161
- }
162
- export function getPngDimensions(base64Data) {
163
- try {
164
- const buffer = Buffer.from(base64Data, "base64");
165
- if (buffer.length < 24) {
166
- return null;
167
- }
168
- if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) {
169
- return null;
170
- }
171
- const width = buffer.readUInt32BE(16);
172
- const height = buffer.readUInt32BE(20);
173
- return { widthPx: width, heightPx: height };
174
- }
175
- catch {
176
- return null;
177
- }
178
- }
179
- export function getJpegDimensions(base64Data) {
180
- try {
181
- const buffer = Buffer.from(base64Data, "base64");
182
- if (buffer.length < 2) {
183
- return null;
184
- }
185
- if (buffer[0] !== 0xff || buffer[1] !== 0xd8) {
186
- return null;
187
- }
188
- let offset = 2;
189
- while (offset < buffer.length - 9) {
190
- if (buffer[offset] !== 0xff) {
191
- offset++;
192
- continue;
193
- }
194
- const marker = buffer[offset + 1];
195
- if (marker >= 0xc0 && marker <= 0xc2) {
196
- const height = buffer.readUInt16BE(offset + 5);
197
- const width = buffer.readUInt16BE(offset + 7);
198
- return { widthPx: width, heightPx: height };
199
- }
200
- if (offset + 3 >= buffer.length) {
201
- return null;
202
- }
203
- const length = buffer.readUInt16BE(offset + 2);
204
- if (length < 2) {
205
- return null;
206
- }
207
- offset += 2 + length;
208
- }
209
- return null;
210
- }
211
- catch {
212
- return null;
213
- }
214
- }
215
- export function getGifDimensions(base64Data) {
216
- try {
217
- const buffer = Buffer.from(base64Data, "base64");
218
- if (buffer.length < 10) {
219
- return null;
220
- }
221
- const sig = buffer.slice(0, 6).toString("ascii");
222
- if (sig !== "GIF87a" && sig !== "GIF89a") {
223
- return null;
224
- }
225
- const width = buffer.readUInt16LE(6);
226
- const height = buffer.readUInt16LE(8);
227
- return { widthPx: width, heightPx: height };
228
- }
229
- catch {
230
- return null;
231
- }
232
- }
233
- export function getWebpDimensions(base64Data) {
234
- try {
235
- const buffer = Buffer.from(base64Data, "base64");
236
- if (buffer.length < 30) {
237
- return null;
238
- }
239
- const riff = buffer.slice(0, 4).toString("ascii");
240
- const webp = buffer.slice(8, 12).toString("ascii");
241
- if (riff !== "RIFF" || webp !== "WEBP") {
242
- return null;
243
- }
244
- const chunk = buffer.slice(12, 16).toString("ascii");
245
- if (chunk === "VP8 ") {
246
- if (buffer.length < 30)
247
- return null;
248
- const width = buffer.readUInt16LE(26) & 0x3fff;
249
- const height = buffer.readUInt16LE(28) & 0x3fff;
250
- return { widthPx: width, heightPx: height };
251
- }
252
- else if (chunk === "VP8L") {
253
- if (buffer.length < 25)
254
- return null;
255
- const bits = buffer.readUInt32LE(21);
256
- const width = (bits & 0x3fff) + 1;
257
- const height = ((bits >> 14) & 0x3fff) + 1;
258
- return { widthPx: width, heightPx: height };
259
- }
260
- else if (chunk === "VP8X") {
261
- if (buffer.length < 30)
262
- return null;
263
- const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
264
- const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
265
- return { widthPx: width, heightPx: height };
266
- }
267
- return null;
268
- }
269
- catch {
270
- return null;
271
- }
272
- }
273
- export function getImageDimensions(base64Data, mimeType) {
274
- if (mimeType === "image/png") {
275
- return getPngDimensions(base64Data);
276
- }
277
- if (mimeType === "image/jpeg") {
278
- return getJpegDimensions(base64Data);
279
- }
280
- if (mimeType === "image/gif") {
281
- return getGifDimensions(base64Data);
282
- }
283
- if (mimeType === "image/webp") {
284
- return getWebpDimensions(base64Data);
285
- }
286
- return null;
287
- }
288
- export function renderImage(base64Data, imageDimensions, options = {}) {
289
- const caps = getCapabilities();
290
- if (!caps.images) {
291
- return null;
292
- }
293
- const maxWidth = options.maxWidthCells ?? 80;
294
- const size = calculateImageCellSize(imageDimensions, maxWidth, options.maxHeightCells, getCellDimensions());
295
- if (caps.images === "kitty") {
296
- const sequence = encodeKitty(base64Data, {
297
- columns: size.columns,
298
- rows: size.rows,
299
- imageId: options.imageId,
300
- moveCursor: options.moveCursor,
301
- });
302
- return { sequence, rows: size.rows, imageId: options.imageId };
303
- }
304
- if (caps.images === "iterm2") {
305
- const sequence = encodeITerm2(base64Data, {
306
- width: size.columns,
307
- height: "auto",
308
- preserveAspectRatio: options.preserveAspectRatio ?? true,
309
- });
310
- return { sequence, rows: size.rows };
311
- }
312
- return null;
313
- }
314
- /**
315
- * Wrap text in an OSC 8 hyperlink sequence.
316
- * The text is rendered as a clickable hyperlink in terminals that support OSC 8
317
- * (Ghostty, Kitty, WezTerm, iTerm2, VSCode, and others).
318
- * In terminals that do not support OSC 8, the escape sequences are ignored
319
- * and only the plain text is displayed.
320
- *
321
- * @param text - The visible text to display
322
- * @param url - The URL to link to
323
- */
324
- export function hyperlink(text, url) {
325
- return `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`;
326
- }
327
- export function imageFallback(mimeType, dimensions, filename) {
328
- const parts = [];
329
- if (filename)
330
- parts.push(filename);
331
- parts.push(`[${mimeType}]`);
332
- if (dimensions)
333
- parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`);
334
- return `[Image: ${parts.join(" ")}]`;
335
- }
@@ -1,324 +0,0 @@
1
- import * as fs from "node:fs";
2
- import { createRequire } from "node:module";
3
- import * as path from "node:path";
4
- import { fileURLToPath } from "node:url";
5
- import { setKittyProtocolActive } from "./keys.js";
6
- import { StdinBuffer } from "./stdin-buffer.js";
7
- const cjsRequire = createRequire(import.meta.url);
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
- /**
12
- * Real terminal using process.stdin/stdout
13
- */
14
- export class ProcessTerminal {
15
- wasRaw = false;
16
- inputHandler;
17
- resizeHandler;
18
- _kittyProtocolActive = false;
19
- _modifyOtherKeysActive = false;
20
- stdinBuffer;
21
- stdinDataHandler;
22
- progressInterval;
23
- writeLogPath = (() => {
24
- const env = process.env.PI_TUI_WRITE_LOG || "";
25
- if (!env)
26
- return "";
27
- try {
28
- if (fs.statSync(env).isDirectory()) {
29
- const now = new Date();
30
- 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")}`;
31
- return path.join(env, `tui-${ts}-${process.pid}.log`);
32
- }
33
- }
34
- catch {
35
- // Not an existing directory - use as-is (file path)
36
- }
37
- return env;
38
- })();
39
- get kittyProtocolActive() {
40
- return this._kittyProtocolActive;
41
- }
42
- start(onInput, onResize) {
43
- this.inputHandler = onInput;
44
- this.resizeHandler = onResize;
45
- // Save previous state and enable raw mode
46
- this.wasRaw = process.stdin.isRaw || false;
47
- if (process.stdin.setRawMode) {
48
- process.stdin.setRawMode(true);
49
- }
50
- process.stdin.setEncoding("utf8");
51
- process.stdin.resume();
52
- // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
53
- process.stdout.write("\x1b[?2004h");
54
- // Set up resize handler immediately
55
- process.stdout.on("resize", this.resizeHandler);
56
- // Refresh terminal dimensions - they may be stale after suspend/resume
57
- // (SIGWINCH is lost while process is stopped). Unix only.
58
- if (process.platform !== "win32") {
59
- process.kill(process.pid, "SIGWINCH");
60
- }
61
- // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
62
- // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
63
- // events that lose modifier information. Must run AFTER setRawMode(true)
64
- // since that resets console mode flags.
65
- this.enableWindowsVTInput();
66
- // Query and enable Kitty keyboard protocol
67
- // The query handler intercepts input temporarily, then installs the user's handler
68
- // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
69
- this.queryAndEnableKittyProtocol();
70
- }
71
- /**
72
- * Set up StdinBuffer to split batched input into individual sequences.
73
- * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
74
- *
75
- * Also watches for Kitty protocol response and enables it when detected.
76
- * This is done here (after stdinBuffer parsing) rather than on raw stdin
77
- * to handle the case where the response arrives split across multiple events.
78
- */
79
- setupStdinBuffer() {
80
- this.stdinBuffer = new StdinBuffer({ timeout: 10 });
81
- // Kitty protocol response pattern: \x1b[?<flags>u
82
- const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
83
- // Forward individual sequences to the input handler
84
- this.stdinBuffer.on("data", (sequence) => {
85
- // Check for Kitty protocol response (only if not already enabled)
86
- if (!this._kittyProtocolActive) {
87
- const match = sequence.match(kittyResponsePattern);
88
- if (match) {
89
- this._kittyProtocolActive = true;
90
- setKittyProtocolActive(true);
91
- // Enable Kitty keyboard protocol (push flags)
92
- // Flag 1 = disambiguate escape codes
93
- // Flag 2 = report event types (press/repeat/release)
94
- // Flag 4 = report alternate keys (shifted key, base layout key)
95
- // Base layout key enables shortcuts to work with non-Latin keyboard layouts
96
- process.stdout.write("\x1b[>7u");
97
- return; // Don't forward protocol response to TUI
98
- }
99
- }
100
- if (this.inputHandler) {
101
- this.inputHandler(sequence);
102
- }
103
- });
104
- // Re-wrap paste content with bracketed paste markers for existing editor handling
105
- this.stdinBuffer.on("paste", (content) => {
106
- if (this.inputHandler) {
107
- this.inputHandler(`\x1b[200~${content}\x1b[201~`);
108
- }
109
- });
110
- // Handler that pipes stdin data through the buffer
111
- this.stdinDataHandler = (data) => {
112
- this.stdinBuffer.process(data);
113
- };
114
- }
115
- /**
116
- * Query terminal for Kitty keyboard protocol support and enable if available.
117
- *
118
- * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
119
- * it supports the protocol and we enable it with CSI > 1 u.
120
- *
121
- * If no Kitty response arrives shortly after startup, fall back to enabling
122
- * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward
123
- * modified enter keys as CSI-u when extended-keys is enabled, but may not
124
- * answer the Kitty protocol query.
125
- *
126
- * The response is detected in setupStdinBuffer's data handler, which properly
127
- * handles the case where the response arrives split across multiple stdin events.
128
- */
129
- queryAndEnableKittyProtocol() {
130
- this.setupStdinBuffer();
131
- process.stdin.on("data", this.stdinDataHandler);
132
- process.stdout.write("\x1b[?u");
133
- setTimeout(() => {
134
- if (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {
135
- process.stdout.write("\x1b[>4;2m");
136
- this._modifyOtherKeysActive = true;
137
- }
138
- }, 150);
139
- }
140
- /**
141
- * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
142
- * console handle so the terminal sends VT sequences for modified keys
143
- * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
144
- * discards modifier state and Shift+Tab arrives as plain \t.
145
- */
146
- enableWindowsVTInput() {
147
- if (process.platform !== "win32")
148
- return;
149
- try {
150
- const arch = process.arch;
151
- if (arch !== "x64" && arch !== "arm64")
152
- return;
153
- // Dynamic require so non-Windows and bundled/browser paths never load the
154
- // native helper. In the npm package native/ is next to dist/; in compiled
155
- // binary archives native/ is copied next to the executable.
156
- const moduleDir = path.dirname(fileURLToPath(import.meta.url));
157
- const nativePath = path.join("native", "win32", "prebuilds", `win32-${arch}`, "win32-console-mode.node");
158
- const candidates = [
159
- path.join(moduleDir, "..", nativePath),
160
- path.join(moduleDir, nativePath),
161
- path.join(path.dirname(process.execPath), nativePath),
162
- ];
163
- for (const modulePath of candidates) {
164
- try {
165
- const helper = cjsRequire(modulePath);
166
- helper.enableVirtualTerminalInput?.();
167
- return;
168
- }
169
- catch {
170
- // Try the next possible packaging location.
171
- }
172
- }
173
- }
174
- catch {
175
- // Native helper not available — Shift+Tab won't be distinguishable from Tab.
176
- }
177
- }
178
- async drainInput(maxMs = 1000, idleMs = 50) {
179
- if (this._kittyProtocolActive) {
180
- // Disable Kitty keyboard protocol first so any late key releases
181
- // do not generate new Kitty escape sequences.
182
- process.stdout.write("\x1b[<u");
183
- this._kittyProtocolActive = false;
184
- setKittyProtocolActive(false);
185
- }
186
- if (this._modifyOtherKeysActive) {
187
- process.stdout.write("\x1b[>4;0m");
188
- this._modifyOtherKeysActive = false;
189
- }
190
- const previousHandler = this.inputHandler;
191
- this.inputHandler = undefined;
192
- let lastDataTime = Date.now();
193
- const onData = () => {
194
- lastDataTime = Date.now();
195
- };
196
- process.stdin.on("data", onData);
197
- const endTime = Date.now() + maxMs;
198
- try {
199
- while (true) {
200
- const now = Date.now();
201
- const timeLeft = endTime - now;
202
- if (timeLeft <= 0)
203
- break;
204
- if (now - lastDataTime >= idleMs)
205
- break;
206
- await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
207
- }
208
- }
209
- finally {
210
- process.stdin.removeListener("data", onData);
211
- this.inputHandler = previousHandler;
212
- }
213
- }
214
- stop() {
215
- if (this.clearProgressInterval()) {
216
- process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
217
- }
218
- // Disable bracketed paste mode
219
- process.stdout.write("\x1b[?2004l");
220
- // Disable Kitty keyboard protocol if not already done by drainInput()
221
- if (this._kittyProtocolActive) {
222
- process.stdout.write("\x1b[<u");
223
- this._kittyProtocolActive = false;
224
- setKittyProtocolActive(false);
225
- }
226
- if (this._modifyOtherKeysActive) {
227
- process.stdout.write("\x1b[>4;0m");
228
- this._modifyOtherKeysActive = false;
229
- }
230
- // Clean up StdinBuffer
231
- if (this.stdinBuffer) {
232
- this.stdinBuffer.destroy();
233
- this.stdinBuffer = undefined;
234
- }
235
- // Remove event handlers
236
- if (this.stdinDataHandler) {
237
- process.stdin.removeListener("data", this.stdinDataHandler);
238
- this.stdinDataHandler = undefined;
239
- }
240
- this.inputHandler = undefined;
241
- if (this.resizeHandler) {
242
- process.stdout.removeListener("resize", this.resizeHandler);
243
- this.resizeHandler = undefined;
244
- }
245
- // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
246
- // re-interpreted after raw mode is disabled. This fixes a race condition
247
- // where Ctrl+D could close the parent shell over SSH.
248
- process.stdin.pause();
249
- // Restore raw mode state
250
- if (process.stdin.setRawMode) {
251
- process.stdin.setRawMode(this.wasRaw);
252
- }
253
- }
254
- write(data) {
255
- process.stdout.write(data);
256
- if (this.writeLogPath) {
257
- try {
258
- fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
259
- }
260
- catch {
261
- // Ignore logging errors
262
- }
263
- }
264
- }
265
- get columns() {
266
- return process.stdout.columns || Number(process.env.COLUMNS) || 80;
267
- }
268
- get rows() {
269
- return process.stdout.rows || Number(process.env.LINES) || 24;
270
- }
271
- moveBy(lines) {
272
- if (lines > 0) {
273
- // Move down
274
- process.stdout.write(`\x1b[${lines}B`);
275
- }
276
- else if (lines < 0) {
277
- // Move up
278
- process.stdout.write(`\x1b[${-lines}A`);
279
- }
280
- // lines === 0: no movement
281
- }
282
- hideCursor() {
283
- process.stdout.write("\x1b[?25l");
284
- }
285
- showCursor() {
286
- process.stdout.write("\x1b[?25h");
287
- }
288
- clearLine() {
289
- process.stdout.write("\x1b[K");
290
- }
291
- clearFromCursor() {
292
- process.stdout.write("\x1b[J");
293
- }
294
- clearScreen() {
295
- process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
296
- }
297
- setTitle(title) {
298
- // OSC 0;title BEL - set terminal window title
299
- process.stdout.write(`\x1b]0;${title}\x07`);
300
- }
301
- setProgress(active) {
302
- if (active) {
303
- // OSC 9;4;3 - indeterminate progress
304
- process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
305
- if (!this.progressInterval) {
306
- this.progressInterval = setInterval(() => {
307
- process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
308
- }, TERMINAL_PROGRESS_KEEPALIVE_MS);
309
- }
310
- }
311
- else {
312
- this.clearProgressInterval();
313
- // OSC 9;4;0 - clear progress
314
- process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
315
- }
316
- }
317
- clearProgressInterval() {
318
- if (!this.progressInterval)
319
- return false;
320
- clearInterval(this.progressInterval);
321
- this.progressInterval = undefined;
322
- return true;
323
- }
324
- }