@aexol/spectral 0.7.1 → 0.7.5

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 (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/agent/agents.js +1 -1
  3. package/dist/agent/index.js +199 -184
  4. package/dist/commands/serve.js +0 -3
  5. package/dist/designer/data/systems/renault/DESIGN.md +1 -1
  6. package/dist/designer/philosophies.js +668 -0
  7. package/dist/mcp/sampling-handler.js +1 -1
  8. package/dist/memory/commands/status.js +1 -1
  9. package/dist/memory/compaction.js +2 -2
  10. package/dist/memory/config.js +1 -1
  11. package/dist/memory/debug-log.js +1 -1
  12. package/dist/memory/hooks/compaction-hook.js +29 -0
  13. package/dist/memory/index.js +2 -0
  14. package/dist/memory/observer.js +2 -2
  15. package/dist/memory/project-observations-store.js +14 -0
  16. package/dist/memory/tokens.js +1 -1
  17. package/dist/memory/tools/read-project-observations.js +82 -0
  18. package/dist/memory/tools/recall-observation.js +2 -2
  19. package/dist/pi/agent-core/agent-loop.js +501 -0
  20. package/dist/pi/agent-core/agent.js +401 -0
  21. package/dist/pi/agent-core/harness/agent-harness.js +899 -0
  22. package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
  23. package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
  24. package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
  25. package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
  26. package/dist/pi/agent-core/harness/messages.js +101 -0
  27. package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
  28. package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
  29. package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
  30. package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
  31. package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
  32. package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
  33. package/dist/pi/agent-core/harness/session/session.js +196 -0
  34. package/dist/pi/agent-core/harness/session/uuid.js +49 -0
  35. package/dist/pi/agent-core/harness/skills.js +310 -0
  36. package/dist/pi/agent-core/harness/system-prompt.js +29 -0
  37. package/dist/pi/agent-core/harness/types.js +93 -0
  38. package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
  39. package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
  40. package/dist/pi/agent-core/index.js +24 -0
  41. package/dist/pi/agent-core/node.js +2 -0
  42. package/dist/pi/agent-core/proxy.js +277 -0
  43. package/dist/pi/agent-core/types.js +1 -0
  44. package/dist/pi/ai/api-registry.js +43 -0
  45. package/dist/pi/ai/cli.js +120 -0
  46. package/dist/pi/ai/env-api-keys.js +169 -0
  47. package/dist/pi/ai/image-models.generated.js +441 -0
  48. package/dist/pi/ai/image-models.js +22 -0
  49. package/dist/pi/ai/images-api-registry.js +21 -0
  50. package/dist/pi/ai/images.js +13 -0
  51. package/dist/pi/ai/index.js +18 -0
  52. package/dist/pi/ai/models.generated.js +16220 -0
  53. package/dist/pi/ai/models.js +70 -0
  54. package/dist/pi/ai/oauth.js +1 -0
  55. package/dist/pi/ai/providers/anthropic.js +945 -0
  56. package/dist/pi/ai/providers/faux.js +367 -0
  57. package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
  58. package/dist/pi/ai/providers/openai-completions.js +945 -0
  59. package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
  60. package/dist/pi/ai/providers/register-builtins.js +97 -0
  61. package/dist/pi/ai/providers/simple-options.js +40 -0
  62. package/dist/pi/ai/providers/transform-messages.js +183 -0
  63. package/dist/pi/ai/session-resources.js +21 -0
  64. package/dist/pi/ai/stream.js +26 -0
  65. package/dist/pi/ai/types.js +1 -0
  66. package/dist/pi/ai/utils/diagnostics.js +24 -0
  67. package/dist/pi/ai/utils/event-stream.js +80 -0
  68. package/dist/pi/ai/utils/hash.js +13 -0
  69. package/dist/pi/ai/utils/headers.js +7 -0
  70. package/dist/pi/ai/utils/json-parse.js +112 -0
  71. package/dist/pi/ai/utils/node-http-proxy.js +96 -0
  72. package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
  73. package/dist/pi/ai/utils/oauth/device-code.js +54 -0
  74. package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
  75. package/dist/pi/ai/utils/oauth/index.js +121 -0
  76. package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
  77. package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
  78. package/dist/pi/ai/utils/oauth/pkce.js +30 -0
  79. package/dist/pi/ai/utils/oauth/types.js +1 -0
  80. package/dist/pi/ai/utils/overflow.js +150 -0
  81. package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
  82. package/dist/pi/ai/utils/typebox-helpers.js +20 -0
  83. package/dist/pi/ai/utils/validation.js +280 -0
  84. package/dist/pi/coding-agent/bun/cli.js +7 -0
  85. package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
  86. package/dist/pi/coding-agent/cli/args.js +340 -0
  87. package/dist/pi/coding-agent/cli/file-processor.js +82 -0
  88. package/dist/pi/coding-agent/cli/initial-message.js +21 -0
  89. package/dist/pi/coding-agent/cli.js +17 -0
  90. package/dist/pi/coding-agent/config.js +414 -0
  91. package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
  92. package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
  93. package/dist/pi/coding-agent/core/agent-session.js +2498 -0
  94. package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
  95. package/dist/pi/coding-agent/core/auth-storage.js +441 -0
  96. package/dist/pi/coding-agent/core/bash-executor.js +110 -0
  97. package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
  98. package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
  99. package/dist/pi/coding-agent/core/compaction/index.js +6 -0
  100. package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
  101. package/dist/pi/coding-agent/core/defaults.js +1 -0
  102. package/dist/pi/coding-agent/core/diagnostics.js +1 -0
  103. package/dist/pi/coding-agent/core/event-bus.js +24 -0
  104. package/dist/pi/coding-agent/core/exec.js +74 -0
  105. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
  106. package/dist/pi/coding-agent/core/export-html/index.js +225 -0
  107. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
  108. package/dist/pi/coding-agent/core/extensions/index.js +8 -0
  109. package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
  110. package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
  111. package/dist/pi/coding-agent/core/extensions/types.js +44 -0
  112. package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
  113. package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
  114. package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
  115. package/dist/pi/coding-agent/core/index.js +11 -0
  116. package/dist/pi/coding-agent/core/keybindings.js +294 -0
  117. package/dist/pi/coding-agent/core/messages.js +122 -0
  118. package/dist/pi/coding-agent/core/model-registry.js +728 -0
  119. package/dist/pi/coding-agent/core/model-resolver.js +494 -0
  120. package/dist/pi/coding-agent/core/output-guard.js +58 -0
  121. package/dist/pi/coding-agent/core/package-manager.js +2020 -0
  122. package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
  123. package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
  124. package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
  125. package/dist/pi/coding-agent/core/resource-loader.js +733 -0
  126. package/dist/pi/coding-agent/core/sdk.js +282 -0
  127. package/dist/pi/coding-agent/core/session-cwd.js +37 -0
  128. package/dist/pi/coding-agent/core/session-manager.js +1146 -0
  129. package/dist/pi/coding-agent/core/settings-manager.js +794 -0
  130. package/dist/pi/coding-agent/core/skills.js +386 -0
  131. package/dist/pi/coding-agent/core/slash-commands.js +24 -0
  132. package/dist/pi/coding-agent/core/source-info.js +18 -0
  133. package/dist/pi/coding-agent/core/system-prompt.js +122 -0
  134. package/dist/pi/coding-agent/core/telemetry.js +8 -0
  135. package/dist/pi/coding-agent/core/timings.js +30 -0
  136. package/dist/pi/coding-agent/core/tools/bash.js +341 -0
  137. package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
  138. package/dist/pi/coding-agent/core/tools/edit.js +324 -0
  139. package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
  140. package/dist/pi/coding-agent/core/tools/find.js +297 -0
  141. package/dist/pi/coding-agent/core/tools/grep.js +303 -0
  142. package/dist/pi/coding-agent/core/tools/index.js +111 -0
  143. package/dist/pi/coding-agent/core/tools/ls.js +168 -0
  144. package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
  145. package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
  146. package/dist/pi/coding-agent/core/tools/read.js +288 -0
  147. package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
  148. package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
  149. package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
  150. package/dist/pi/coding-agent/core/tools/write.js +212 -0
  151. package/dist/pi/coding-agent/index.js +41 -0
  152. package/dist/pi/coding-agent/main.js +5 -0
  153. package/dist/pi/coding-agent/migrations.js +280 -0
  154. package/dist/pi/coding-agent/modes/index.js +7 -0
  155. package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
  156. package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
  157. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
  158. package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
  159. package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
  160. package/dist/pi/coding-agent/modes/print-mode.js +130 -0
  161. package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
  162. package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
  163. package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
  164. package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
  165. package/dist/pi/coding-agent/utils/ansi.js +51 -0
  166. package/dist/pi/coding-agent/utils/changelog.js +86 -0
  167. package/dist/pi/coding-agent/utils/child-process.js +87 -0
  168. package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
  169. package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
  170. package/dist/pi/coding-agent/utils/clipboard.js +116 -0
  171. package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
  172. package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
  173. package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
  174. package/dist/pi/coding-agent/utils/git.js +162 -0
  175. package/dist/pi/coding-agent/utils/html.js +39 -0
  176. package/dist/pi/coding-agent/utils/image-convert.js +38 -0
  177. package/dist/pi/coding-agent/utils/image-resize.js +136 -0
  178. package/dist/pi/coding-agent/utils/mime.js +68 -0
  179. package/dist/pi/coding-agent/utils/paths.js +91 -0
  180. package/dist/pi/coding-agent/utils/photon.js +120 -0
  181. package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
  182. package/dist/pi/coding-agent/utils/shell.js +194 -0
  183. package/dist/pi/coding-agent/utils/sleep.js +16 -0
  184. package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
  185. package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
  186. package/dist/pi/coding-agent/utils/version-check.js +81 -0
  187. package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
  188. package/dist/pi/tui/autocomplete.js +631 -0
  189. package/dist/pi/tui/components/box.js +103 -0
  190. package/dist/pi/tui/components/cancellable-loader.js +34 -0
  191. package/dist/pi/tui/components/editor.js +1915 -0
  192. package/dist/pi/tui/components/image.js +88 -0
  193. package/dist/pi/tui/components/input.js +425 -0
  194. package/dist/pi/tui/components/loader.js +68 -0
  195. package/dist/pi/tui/components/markdown.js +633 -0
  196. package/dist/pi/tui/components/select-list.js +158 -0
  197. package/dist/pi/tui/components/settings-list.js +184 -0
  198. package/dist/pi/tui/components/spacer.js +22 -0
  199. package/dist/pi/tui/components/text.js +88 -0
  200. package/dist/pi/tui/components/truncated-text.js +50 -0
  201. package/dist/pi/tui/editor-component.js +1 -0
  202. package/dist/pi/tui/fuzzy.js +109 -0
  203. package/dist/pi/tui/index.js +31 -0
  204. package/dist/pi/tui/keybindings.js +173 -0
  205. package/dist/pi/tui/keys.js +1172 -0
  206. package/dist/pi/tui/kill-ring.js +43 -0
  207. package/dist/pi/tui/stdin-buffer.js +360 -0
  208. package/dist/pi/tui/terminal-image.js +335 -0
  209. package/dist/pi/tui/terminal.js +324 -0
  210. package/dist/pi/tui/tui.js +1076 -0
  211. package/dist/pi/tui/undo-stack.js +24 -0
  212. package/dist/pi/tui/utils.js +1016 -0
  213. package/dist/relay/dispatcher.js +30 -0
  214. package/dist/server/handlers/queue.js +52 -0
  215. package/dist/server/pi-bridge.js +9 -1
  216. package/dist/server/session-stream.js +76 -111
  217. package/dist/server/storage.js +154 -2
  218. package/dist/server/title-generator.js +14 -153
  219. package/package.json +24 -6
@@ -0,0 +1,157 @@
1
+ function readOrientationFromTiff(bytes, tiffStart) {
2
+ if (tiffStart + 8 > bytes.length)
3
+ return 1;
4
+ const byteOrder = (bytes[tiffStart] << 8) | bytes[tiffStart + 1];
5
+ const le = byteOrder === 0x4949;
6
+ const read16 = (pos) => {
7
+ if (le)
8
+ return bytes[pos] | (bytes[pos + 1] << 8);
9
+ return (bytes[pos] << 8) | bytes[pos + 1];
10
+ };
11
+ const read32 = (pos) => {
12
+ if (le)
13
+ return bytes[pos] | (bytes[pos + 1] << 8) | (bytes[pos + 2] << 16) | (bytes[pos + 3] << 24);
14
+ return ((bytes[pos] << 24) | (bytes[pos + 1] << 16) | (bytes[pos + 2] << 8) | bytes[pos + 3]) >>> 0;
15
+ };
16
+ const ifdOffset = read32(tiffStart + 4);
17
+ const ifdStart = tiffStart + ifdOffset;
18
+ if (ifdStart + 2 > bytes.length)
19
+ return 1;
20
+ const entryCount = read16(ifdStart);
21
+ for (let i = 0; i < entryCount; i++) {
22
+ const entryPos = ifdStart + 2 + i * 12;
23
+ if (entryPos + 12 > bytes.length)
24
+ return 1;
25
+ if (read16(entryPos) === 0x0112) {
26
+ const value = read16(entryPos + 8);
27
+ return value >= 1 && value <= 8 ? value : 1;
28
+ }
29
+ }
30
+ return 1;
31
+ }
32
+ function findJpegTiffOffset(bytes) {
33
+ let offset = 2;
34
+ while (offset < bytes.length - 1) {
35
+ if (bytes[offset] !== 0xff)
36
+ return -1;
37
+ const marker = bytes[offset + 1];
38
+ if (marker === 0xff) {
39
+ offset++;
40
+ continue;
41
+ }
42
+ if (marker === 0xe1) {
43
+ if (offset + 4 >= bytes.length)
44
+ return -1;
45
+ const segmentStart = offset + 4;
46
+ if (segmentStart + 6 > bytes.length)
47
+ return -1;
48
+ if (!hasExifHeader(bytes, segmentStart))
49
+ return -1;
50
+ return segmentStart + 6;
51
+ }
52
+ if (offset + 4 > bytes.length)
53
+ return -1;
54
+ const length = (bytes[offset + 2] << 8) | bytes[offset + 3];
55
+ offset += 2 + length;
56
+ }
57
+ return -1;
58
+ }
59
+ function findWebpTiffOffset(bytes) {
60
+ let offset = 12;
61
+ while (offset + 8 <= bytes.length) {
62
+ const chunkId = String.fromCharCode(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3]);
63
+ const chunkSize = bytes[offset + 4] | (bytes[offset + 5] << 8) | (bytes[offset + 6] << 16) | (bytes[offset + 7] << 24);
64
+ const dataStart = offset + 8;
65
+ if (chunkId === "EXIF") {
66
+ if (dataStart + chunkSize > bytes.length)
67
+ return -1;
68
+ // Some WebP files have "Exif\0\0" prefix before the TIFF header
69
+ const tiffStart = chunkSize >= 6 && hasExifHeader(bytes, dataStart) ? dataStart + 6 : dataStart;
70
+ return tiffStart;
71
+ }
72
+ // RIFF chunks are padded to even size
73
+ offset = dataStart + chunkSize + (chunkSize % 2);
74
+ }
75
+ return -1;
76
+ }
77
+ function hasExifHeader(bytes, offset) {
78
+ return (bytes[offset] === 0x45 &&
79
+ bytes[offset + 1] === 0x78 &&
80
+ bytes[offset + 2] === 0x69 &&
81
+ bytes[offset + 3] === 0x66 &&
82
+ bytes[offset + 4] === 0x00 &&
83
+ bytes[offset + 5] === 0x00);
84
+ }
85
+ function getExifOrientation(bytes) {
86
+ let tiffOffset = -1;
87
+ // JPEG: starts with FF D8
88
+ if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) {
89
+ tiffOffset = findJpegTiffOffset(bytes);
90
+ }
91
+ // WebP: starts with RIFF....WEBP
92
+ else if (bytes.length >= 12 &&
93
+ bytes[0] === 0x52 &&
94
+ bytes[1] === 0x49 &&
95
+ bytes[2] === 0x46 &&
96
+ bytes[3] === 0x46 &&
97
+ bytes[8] === 0x57 &&
98
+ bytes[9] === 0x45 &&
99
+ bytes[10] === 0x42 &&
100
+ bytes[11] === 0x50) {
101
+ tiffOffset = findWebpTiffOffset(bytes);
102
+ }
103
+ if (tiffOffset === -1)
104
+ return 1;
105
+ return readOrientationFromTiff(bytes, tiffOffset);
106
+ }
107
+ function rotate90(photon, image, dstIndex) {
108
+ const w = image.get_width();
109
+ const h = image.get_height();
110
+ const src = image.get_raw_pixels();
111
+ const dst = new Uint8Array(src.length);
112
+ for (let y = 0; y < h; y++) {
113
+ for (let x = 0; x < w; x++) {
114
+ const srcIdx = (y * w + x) * 4;
115
+ const dstIdx = dstIndex(x, y, w, h) * 4;
116
+ dst[dstIdx] = src[srcIdx];
117
+ dst[dstIdx + 1] = src[srcIdx + 1];
118
+ dst[dstIdx + 2] = src[srcIdx + 2];
119
+ dst[dstIdx + 3] = src[srcIdx + 3];
120
+ }
121
+ }
122
+ return new photon.PhotonImage(dst, h, w);
123
+ }
124
+ // Flip orientations mutate in-place. Rotations return a new image (caller must free the old one if different).
125
+ export function applyExifOrientation(photon, image, originalBytes) {
126
+ const orientation = getExifOrientation(originalBytes);
127
+ if (orientation === 1)
128
+ return image;
129
+ switch (orientation) {
130
+ case 2:
131
+ photon.fliph(image);
132
+ return image;
133
+ case 3:
134
+ photon.fliph(image);
135
+ photon.flipv(image);
136
+ return image;
137
+ case 4:
138
+ photon.flipv(image);
139
+ return image;
140
+ case 5: {
141
+ const rotated = rotate90(photon, image, (x, y, _w, h) => x * h + (h - 1 - y));
142
+ photon.fliph(rotated);
143
+ return rotated;
144
+ }
145
+ case 6:
146
+ return rotate90(photon, image, (x, y, _w, h) => x * h + (h - 1 - y));
147
+ case 7: {
148
+ const rotated = rotate90(photon, image, (x, y, w, h) => (w - 1 - x) * h + y);
149
+ photon.fliph(rotated);
150
+ return rotated;
151
+ }
152
+ case 8:
153
+ return rotate90(photon, image, (x, y, w, h) => (w - 1 - x) * h + y);
154
+ default:
155
+ return image;
156
+ }
157
+ }
@@ -0,0 +1,25 @@
1
+ import { parse } from "yaml";
2
+ const normalizeNewlines = (value) => value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
3
+ const extractFrontmatter = (content) => {
4
+ const normalized = normalizeNewlines(content);
5
+ if (!normalized.startsWith("---")) {
6
+ return { yamlString: null, body: normalized };
7
+ }
8
+ const endIndex = normalized.indexOf("\n---", 3);
9
+ if (endIndex === -1) {
10
+ return { yamlString: null, body: normalized };
11
+ }
12
+ return {
13
+ yamlString: normalized.slice(4, endIndex),
14
+ body: normalized.slice(endIndex + 4).trim(),
15
+ };
16
+ };
17
+ export const parseFrontmatter = (content) => {
18
+ const { yamlString, body } = extractFrontmatter(content);
19
+ if (!yamlString) {
20
+ return { frontmatter: {}, body };
21
+ }
22
+ const parsed = parse(yamlString);
23
+ return { frontmatter: (parsed ?? {}), body };
24
+ };
25
+ export const stripFrontmatter = (content) => parseFrontmatter(content).body;
@@ -0,0 +1,24 @@
1
+ import { watch } from "node:fs";
2
+ export const FS_WATCH_RETRY_DELAY_MS = 5000;
3
+ export function closeWatcher(watcher) {
4
+ if (!watcher) {
5
+ return;
6
+ }
7
+ try {
8
+ watcher.close();
9
+ }
10
+ catch {
11
+ // Ignore watcher close errors
12
+ }
13
+ }
14
+ export function watchWithErrorHandler(path, listener, onError) {
15
+ try {
16
+ const watcher = watch(path, listener);
17
+ watcher.on("error", onError);
18
+ return watcher;
19
+ }
20
+ catch {
21
+ onError();
22
+ return null;
23
+ }
24
+ }
@@ -0,0 +1,162 @@
1
+ import hostedGitInfo from "hosted-git-info";
2
+ function splitRef(url) {
3
+ const scpLikeMatch = url.match(/^git@([^:]+):(.+)$/);
4
+ if (scpLikeMatch) {
5
+ const pathWithMaybeRef = scpLikeMatch[2] ?? "";
6
+ const refSeparator = pathWithMaybeRef.indexOf("@");
7
+ if (refSeparator < 0)
8
+ return { repo: url };
9
+ const repoPath = pathWithMaybeRef.slice(0, refSeparator);
10
+ const ref = pathWithMaybeRef.slice(refSeparator + 1);
11
+ if (!repoPath || !ref)
12
+ return { repo: url };
13
+ return {
14
+ repo: `git@${scpLikeMatch[1] ?? ""}:${repoPath}`,
15
+ ref,
16
+ };
17
+ }
18
+ if (url.includes("://")) {
19
+ try {
20
+ const parsed = new URL(url);
21
+ const pathWithMaybeRef = parsed.pathname.replace(/^\/+/, "");
22
+ const refSeparator = pathWithMaybeRef.indexOf("@");
23
+ if (refSeparator < 0)
24
+ return { repo: url };
25
+ const repoPath = pathWithMaybeRef.slice(0, refSeparator);
26
+ const ref = pathWithMaybeRef.slice(refSeparator + 1);
27
+ if (!repoPath || !ref)
28
+ return { repo: url };
29
+ parsed.pathname = `/${repoPath}`;
30
+ return {
31
+ repo: parsed.toString().replace(/\/$/, ""),
32
+ ref,
33
+ };
34
+ }
35
+ catch {
36
+ return { repo: url };
37
+ }
38
+ }
39
+ const slashIndex = url.indexOf("/");
40
+ if (slashIndex < 0) {
41
+ return { repo: url };
42
+ }
43
+ const host = url.slice(0, slashIndex);
44
+ const pathWithMaybeRef = url.slice(slashIndex + 1);
45
+ const refSeparator = pathWithMaybeRef.indexOf("@");
46
+ if (refSeparator < 0) {
47
+ return { repo: url };
48
+ }
49
+ const repoPath = pathWithMaybeRef.slice(0, refSeparator);
50
+ const ref = pathWithMaybeRef.slice(refSeparator + 1);
51
+ if (!repoPath || !ref) {
52
+ return { repo: url };
53
+ }
54
+ return {
55
+ repo: `${host}/${repoPath}`,
56
+ ref,
57
+ };
58
+ }
59
+ function parseGenericGitUrl(url) {
60
+ const { repo: repoWithoutRef, ref } = splitRef(url);
61
+ let repo = repoWithoutRef;
62
+ let host = "";
63
+ let path = "";
64
+ const scpLikeMatch = repoWithoutRef.match(/^git@([^:]+):(.+)$/);
65
+ if (scpLikeMatch) {
66
+ host = scpLikeMatch[1] ?? "";
67
+ path = scpLikeMatch[2] ?? "";
68
+ }
69
+ else if (repoWithoutRef.startsWith("https://") ||
70
+ repoWithoutRef.startsWith("http://") ||
71
+ repoWithoutRef.startsWith("ssh://") ||
72
+ repoWithoutRef.startsWith("git://")) {
73
+ try {
74
+ const parsed = new URL(repoWithoutRef);
75
+ host = parsed.hostname;
76
+ path = parsed.pathname.replace(/^\/+/, "");
77
+ }
78
+ catch {
79
+ return null;
80
+ }
81
+ }
82
+ else {
83
+ const slashIndex = repoWithoutRef.indexOf("/");
84
+ if (slashIndex < 0) {
85
+ return null;
86
+ }
87
+ host = repoWithoutRef.slice(0, slashIndex);
88
+ path = repoWithoutRef.slice(slashIndex + 1);
89
+ if (!host.includes(".") && host !== "localhost") {
90
+ return null;
91
+ }
92
+ repo = `https://${repoWithoutRef}`;
93
+ }
94
+ const normalizedPath = path.replace(/\.git$/, "").replace(/^\/+/, "");
95
+ if (!host || !normalizedPath || normalizedPath.split("/").length < 2) {
96
+ return null;
97
+ }
98
+ return {
99
+ type: "git",
100
+ repo,
101
+ host,
102
+ path: normalizedPath,
103
+ ref,
104
+ pinned: Boolean(ref),
105
+ };
106
+ }
107
+ /**
108
+ * Parse git source into a GitSource.
109
+ *
110
+ * Rules:
111
+ * - With git: prefix, accept all historical shorthand forms.
112
+ * - Without git: prefix, only accept explicit protocol URLs.
113
+ */
114
+ export function parseGitUrl(source) {
115
+ const trimmed = source.trim();
116
+ const hasGitPrefix = trimmed.startsWith("git:");
117
+ const url = hasGitPrefix ? trimmed.slice(4).trim() : trimmed;
118
+ if (!hasGitPrefix && !/^(https?|ssh|git):\/\//i.test(url)) {
119
+ return null;
120
+ }
121
+ const split = splitRef(url);
122
+ const hostedCandidates = [split.ref ? `${split.repo}#${split.ref}` : undefined, url].filter((value) => Boolean(value));
123
+ for (const candidate of hostedCandidates) {
124
+ const info = hostedGitInfo.fromUrl(candidate);
125
+ if (info) {
126
+ if (split.ref && info.project?.includes("@")) {
127
+ continue;
128
+ }
129
+ const useHttpsPrefix = !split.repo.startsWith("http://") &&
130
+ !split.repo.startsWith("https://") &&
131
+ !split.repo.startsWith("ssh://") &&
132
+ !split.repo.startsWith("git://") &&
133
+ !split.repo.startsWith("git@");
134
+ return {
135
+ type: "git",
136
+ repo: useHttpsPrefix ? `https://${split.repo}` : split.repo,
137
+ host: info.domain || "",
138
+ path: `${info.user}/${info.project}`.replace(/\.git$/, ""),
139
+ ref: info.committish || split.ref || undefined,
140
+ pinned: Boolean(info.committish || split.ref),
141
+ };
142
+ }
143
+ }
144
+ const httpsCandidates = [split.ref ? `https://${split.repo}#${split.ref}` : undefined, `https://${url}`].filter((value) => Boolean(value));
145
+ for (const candidate of httpsCandidates) {
146
+ const info = hostedGitInfo.fromUrl(candidate);
147
+ if (info) {
148
+ if (split.ref && info.project?.includes("@")) {
149
+ continue;
150
+ }
151
+ return {
152
+ type: "git",
153
+ repo: `https://${split.repo}`,
154
+ host: info.domain || "",
155
+ path: `${info.user}/${info.project}`.replace(/\.git$/, ""),
156
+ ref: info.committish || split.ref || undefined,
157
+ pinned: Boolean(info.committish || split.ref),
158
+ };
159
+ }
160
+ }
161
+ return parseGenericGitUrl(url);
162
+ }
@@ -0,0 +1,39 @@
1
+ function decodeCodePoint(codePoint) {
2
+ if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
3
+ return undefined;
4
+ }
5
+ return String.fromCodePoint(codePoint);
6
+ }
7
+ export function decodeHtmlEntity(entity) {
8
+ switch (entity) {
9
+ case "amp":
10
+ return "&";
11
+ case "lt":
12
+ return "<";
13
+ case "gt":
14
+ return ">";
15
+ case "quot":
16
+ return '"';
17
+ case "apos":
18
+ return "'";
19
+ }
20
+ if (entity.startsWith("#x") || entity.startsWith("#X")) {
21
+ return decodeCodePoint(Number.parseInt(entity.slice(2), 16));
22
+ }
23
+ if (entity.startsWith("#")) {
24
+ return decodeCodePoint(Number.parseInt(entity.slice(1), 10));
25
+ }
26
+ return undefined;
27
+ }
28
+ export function decodeHtmlEntityAt(html, index) {
29
+ const semicolonIndex = html.indexOf(";", index + 1);
30
+ if (semicolonIndex === -1 || semicolonIndex - index > 16) {
31
+ return undefined;
32
+ }
33
+ const entity = html.slice(index + 1, semicolonIndex);
34
+ const decoded = decodeHtmlEntity(entity);
35
+ if (decoded === undefined) {
36
+ return undefined;
37
+ }
38
+ return { text: decoded, length: semicolonIndex - index + 1 };
39
+ }
@@ -0,0 +1,38 @@
1
+ import { applyExifOrientation } from "./exif-orientation.js";
2
+ import { loadPhoton } from "./photon.js";
3
+ /**
4
+ * Convert image to PNG format for terminal display.
5
+ * Kitty graphics protocol requires PNG format (f=100).
6
+ */
7
+ export async function convertToPng(base64Data, mimeType) {
8
+ // Already PNG, no conversion needed
9
+ if (mimeType === "image/png") {
10
+ return { data: base64Data, mimeType };
11
+ }
12
+ const photon = await loadPhoton();
13
+ if (!photon) {
14
+ // Photon not available, can't convert
15
+ return null;
16
+ }
17
+ try {
18
+ const bytes = new Uint8Array(Buffer.from(base64Data, "base64"));
19
+ const rawImage = photon.PhotonImage.new_from_byteslice(bytes);
20
+ const image = applyExifOrientation(photon, rawImage, bytes);
21
+ if (image !== rawImage)
22
+ rawImage.free();
23
+ try {
24
+ const pngBuffer = image.get_bytes();
25
+ return {
26
+ data: Buffer.from(pngBuffer).toString("base64"),
27
+ mimeType: "image/png",
28
+ };
29
+ }
30
+ finally {
31
+ image.free();
32
+ }
33
+ }
34
+ catch {
35
+ // Conversion failed
36
+ return null;
37
+ }
38
+ }
@@ -0,0 +1,136 @@
1
+ import { applyExifOrientation } from "./exif-orientation.js";
2
+ import { loadPhoton } from "./photon.js";
3
+ // 4.5MB of base64 payload. Provides headroom below Anthropic's 5MB limit.
4
+ const DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;
5
+ const DEFAULT_OPTIONS = {
6
+ maxWidth: 2000,
7
+ maxHeight: 2000,
8
+ maxBytes: DEFAULT_MAX_BYTES,
9
+ jpegQuality: 80,
10
+ };
11
+ function encodeCandidate(buffer, mimeType) {
12
+ const data = Buffer.from(buffer).toString("base64");
13
+ return {
14
+ data,
15
+ encodedSize: Buffer.byteLength(data, "utf-8"),
16
+ mimeType,
17
+ };
18
+ }
19
+ /**
20
+ * Resize an image to fit within the specified max dimensions and encoded file size.
21
+ * Returns null if the image cannot be resized below maxBytes.
22
+ *
23
+ * Uses Photon (Rust/WASM) for image processing. If Photon is not available,
24
+ * returns null.
25
+ *
26
+ * Strategy for staying under maxBytes:
27
+ * 1. First resize to maxWidth/maxHeight
28
+ * 2. Try both PNG and JPEG formats, pick the smaller one
29
+ * 3. If still too large, try JPEG with decreasing quality
30
+ * 4. If still too large, progressively reduce dimensions until 1x1
31
+ */
32
+ export async function resizeImage(img, options) {
33
+ const opts = { ...DEFAULT_OPTIONS, ...options };
34
+ const inputBuffer = Buffer.from(img.data, "base64");
35
+ const inputBase64Size = Buffer.byteLength(img.data, "utf-8");
36
+ const photon = await loadPhoton();
37
+ if (!photon) {
38
+ return null;
39
+ }
40
+ let image;
41
+ try {
42
+ const inputBytes = new Uint8Array(inputBuffer);
43
+ const rawImage = photon.PhotonImage.new_from_byteslice(inputBytes);
44
+ image = applyExifOrientation(photon, rawImage, inputBytes);
45
+ if (image !== rawImage)
46
+ rawImage.free();
47
+ const originalWidth = image.get_width();
48
+ const originalHeight = image.get_height();
49
+ const format = img.mimeType?.split("/")[1] ?? "png";
50
+ // Check if already within all limits (dimensions AND encoded size)
51
+ if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && inputBase64Size < opts.maxBytes) {
52
+ return {
53
+ data: img.data,
54
+ mimeType: img.mimeType ?? `image/${format}`,
55
+ originalWidth,
56
+ originalHeight,
57
+ width: originalWidth,
58
+ height: originalHeight,
59
+ wasResized: false,
60
+ };
61
+ }
62
+ // Calculate initial dimensions respecting max limits
63
+ let targetWidth = originalWidth;
64
+ let targetHeight = originalHeight;
65
+ if (targetWidth > opts.maxWidth) {
66
+ targetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);
67
+ targetWidth = opts.maxWidth;
68
+ }
69
+ if (targetHeight > opts.maxHeight) {
70
+ targetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);
71
+ targetHeight = opts.maxHeight;
72
+ }
73
+ function tryEncodings(width, height, jpegQualities) {
74
+ const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
75
+ try {
76
+ const candidates = [encodeCandidate(resized.get_bytes(), "image/png")];
77
+ for (const quality of jpegQualities) {
78
+ candidates.push(encodeCandidate(resized.get_bytes_jpeg(quality), "image/jpeg"));
79
+ }
80
+ return candidates;
81
+ }
82
+ finally {
83
+ resized.free();
84
+ }
85
+ }
86
+ const qualitySteps = Array.from(new Set([opts.jpegQuality, 85, 70, 55, 40]));
87
+ let currentWidth = targetWidth;
88
+ let currentHeight = targetHeight;
89
+ while (true) {
90
+ const candidates = tryEncodings(currentWidth, currentHeight, qualitySteps);
91
+ for (const candidate of candidates) {
92
+ if (candidate.encodedSize < opts.maxBytes) {
93
+ return {
94
+ data: candidate.data,
95
+ mimeType: candidate.mimeType,
96
+ originalWidth,
97
+ originalHeight,
98
+ width: currentWidth,
99
+ height: currentHeight,
100
+ wasResized: true,
101
+ };
102
+ }
103
+ }
104
+ if (currentWidth === 1 && currentHeight === 1) {
105
+ break;
106
+ }
107
+ const nextWidth = currentWidth === 1 ? 1 : Math.max(1, Math.floor(currentWidth * 0.75));
108
+ const nextHeight = currentHeight === 1 ? 1 : Math.max(1, Math.floor(currentHeight * 0.75));
109
+ if (nextWidth === currentWidth && nextHeight === currentHeight) {
110
+ break;
111
+ }
112
+ currentWidth = nextWidth;
113
+ currentHeight = nextHeight;
114
+ }
115
+ return null;
116
+ }
117
+ catch {
118
+ return null;
119
+ }
120
+ finally {
121
+ if (image) {
122
+ image.free();
123
+ }
124
+ }
125
+ }
126
+ /**
127
+ * Format a dimension note for resized images.
128
+ * This helps the model understand the coordinate mapping.
129
+ */
130
+ export function formatDimensionNote(result) {
131
+ if (!result.wasResized) {
132
+ return undefined;
133
+ }
134
+ const scale = result.originalWidth / result.width;
135
+ return `[Image: original ${result.originalWidth}x${result.originalHeight}, displayed at ${result.width}x${result.height}. Multiply coordinates by ${scale.toFixed(2)} to map to original image.]`;
136
+ }
@@ -0,0 +1,68 @@
1
+ import { open } from "node:fs/promises";
2
+ const IMAGE_TYPE_SNIFF_BYTES = 4100;
3
+ const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
4
+ export function detectSupportedImageMimeType(buffer) {
5
+ if (startsWith(buffer, [0xff, 0xd8, 0xff])) {
6
+ return buffer[3] === 0xf7 ? null : "image/jpeg";
7
+ }
8
+ if (startsWith(buffer, PNG_SIGNATURE)) {
9
+ return isPng(buffer) && !isAnimatedPng(buffer) ? "image/png" : null;
10
+ }
11
+ if (startsWithAscii(buffer, 0, "GIF")) {
12
+ return "image/gif";
13
+ }
14
+ if (startsWithAscii(buffer, 0, "RIFF") && startsWithAscii(buffer, 8, "WEBP")) {
15
+ return "image/webp";
16
+ }
17
+ return null;
18
+ }
19
+ export async function detectSupportedImageMimeTypeFromFile(filePath) {
20
+ const fileHandle = await open(filePath, "r");
21
+ try {
22
+ const buffer = Buffer.alloc(IMAGE_TYPE_SNIFF_BYTES);
23
+ const { bytesRead } = await fileHandle.read(buffer, 0, IMAGE_TYPE_SNIFF_BYTES, 0);
24
+ return detectSupportedImageMimeType(buffer.subarray(0, bytesRead));
25
+ }
26
+ finally {
27
+ await fileHandle.close();
28
+ }
29
+ }
30
+ function isPng(buffer) {
31
+ return (buffer.length >= 16 && readUint32BE(buffer, PNG_SIGNATURE.length) === 13 && startsWithAscii(buffer, 12, "IHDR"));
32
+ }
33
+ function isAnimatedPng(buffer) {
34
+ let offset = PNG_SIGNATURE.length;
35
+ while (offset + 8 <= buffer.length) {
36
+ const chunkLength = readUint32BE(buffer, offset);
37
+ const chunkTypeOffset = offset + 4;
38
+ if (startsWithAscii(buffer, chunkTypeOffset, "acTL"))
39
+ return true;
40
+ if (startsWithAscii(buffer, chunkTypeOffset, "IDAT"))
41
+ return false;
42
+ const nextOffset = offset + 8 + chunkLength + 4;
43
+ if (nextOffset <= offset || nextOffset > buffer.length)
44
+ return false;
45
+ offset = nextOffset;
46
+ }
47
+ return false;
48
+ }
49
+ function readUint32BE(buffer, offset) {
50
+ return ((buffer[offset] ?? 0) * 0x1000000 +
51
+ ((buffer[offset + 1] ?? 0) << 16) +
52
+ ((buffer[offset + 2] ?? 0) << 8) +
53
+ (buffer[offset + 3] ?? 0));
54
+ }
55
+ function startsWith(buffer, bytes) {
56
+ if (buffer.length < bytes.length)
57
+ return false;
58
+ return bytes.every((byte, index) => buffer[index] === byte);
59
+ }
60
+ function startsWithAscii(buffer, offset, text) {
61
+ if (buffer.length < offset + text.length)
62
+ return false;
63
+ for (let index = 0; index < text.length; index++) {
64
+ if (buffer[offset + index] !== text.charCodeAt(index))
65
+ return false;
66
+ }
67
+ return true;
68
+ }