aegisone 1.0.34 → 1.0.35

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 (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -5
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -976
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -383
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/package.json +3 -7
  17. package/vendor/cli/scripts/check.mjs +1 -52
  18. package/vendor/cli/scripts/demo.mjs +1 -94
  19. package/vendor/cli/scripts/predist.mjs +1 -75
  20. package/vendor/cli/scripts/runtests.mjs +1 -56
  21. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  22. package/vendor/cli/src/agents.js +1 -139
  23. package/vendor/cli/src/app.js +1 -2957
  24. package/vendor/cli/src/art.js +1 -289
  25. package/vendor/cli/src/chatflow.js +1 -3023
  26. package/vendor/cli/src/checkpoint.js +1 -90
  27. package/vendor/cli/src/clipboard.js +1 -424
  28. package/vendor/cli/src/cloudsync.js +1 -528
  29. package/vendor/cli/src/commands.js +1 -4089
  30. package/vendor/cli/src/config.js +1 -581
  31. package/vendor/cli/src/connect.js +1 -349
  32. package/vendor/cli/src/credentials.js +1 -33
  33. package/vendor/cli/src/deps.js +1 -232
  34. package/vendor/cli/src/devrun.js +1 -187
  35. package/vendor/cli/src/diff.js +1 -333
  36. package/vendor/cli/src/diffview.js +1 -296
  37. package/vendor/cli/src/engine.js +1 -297
  38. package/vendor/cli/src/entitlement.js +1 -355
  39. package/vendor/cli/src/events.js +1 -707
  40. package/vendor/cli/src/export.js +1 -67
  41. package/vendor/cli/src/filepreview.js +1 -153
  42. package/vendor/cli/src/fingerprint.js +1 -592
  43. package/vendor/cli/src/format.js +1 -259
  44. package/vendor/cli/src/fuzzy.js +1 -119
  45. package/vendor/cli/src/history.js +1 -423
  46. package/vendor/cli/src/init.js +1 -175
  47. package/vendor/cli/src/input.js +1 -139
  48. package/vendor/cli/src/keys.js +1 -183
  49. package/vendor/cli/src/markdown.js +1 -306
  50. package/vendor/cli/src/models.js +1 -507
  51. package/vendor/cli/src/naming.js +1 -57
  52. package/vendor/cli/src/overlays.js +1 -462
  53. package/vendor/cli/src/panels.js +1 -1418
  54. package/vendor/cli/src/ping.js +1 -226
  55. package/vendor/cli/src/presence.js +1 -238
  56. package/vendor/cli/src/render.js +1 -516
  57. package/vendor/cli/src/screen.js +1 -518
  58. package/vendor/cli/src/screens.js +1 -896
  59. package/vendor/cli/src/secret.js +1 -59
  60. package/vendor/cli/src/securefs.js +1 -359
  61. package/vendor/cli/src/settings.js +1 -155
  62. package/vendor/cli/src/shared.js +1 -52
  63. package/vendor/cli/src/sharedpaths.js +1 -53
  64. package/vendor/cli/src/signup.js +1 -156
  65. package/vendor/cli/src/summarize.js +1 -95
  66. package/vendor/cli/src/sysdiag.js +1 -585
  67. package/vendor/cli/src/system.js +1 -40
  68. package/vendor/cli/src/theme.js +1 -570
  69. package/vendor/cli/src/title.js +1 -220
  70. package/vendor/cli/src/todos.js +1 -115
  71. package/vendor/cli/src/tokens.js +1 -263
  72. package/vendor/cli/src/topup.js +1 -210
  73. package/vendor/cli/src/update.js +1 -191
  74. package/vendor/cli/vendor/client/aegis.js +1 -2441
  75. package/vendor/cli/vendor/client/attest.js +1 -518
  76. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  77. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  78. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  79. package/vendor/cli/vendor/client/byok-catalog.js +1 -199
  80. package/vendor/cli/vendor/client/catalog-client.js +1 -487
  81. package/vendor/cli/vendor/client/conversation-export.js +1 -177
  82. package/vendor/cli/vendor/client/credentials.js +1 -494
  83. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  84. package/vendor/cli/vendor/client/drive-connect.js +1 -110
  85. package/vendor/cli/vendor/client/drive-upload.js +1 -234
  86. package/vendor/cli/vendor/client/env-file.js +1 -1273
  87. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  88. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  89. package/vendor/cli/vendor/client/google-signin.js +1 -133
  90. package/vendor/cli/vendor/client/hooks.js +1 -430
  91. package/vendor/cli/vendor/client/model-discovery.js +1 -373
  92. package/vendor/cli/vendor/client/naming.js +1 -283
  93. package/vendor/cli/vendor/client/platform.js +1 -937
  94. package/vendor/cli/vendor/client/prefs.js +1 -351
  95. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  96. package/vendor/cli/vendor/client/presence.js +1 -438
  97. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  98. package/vendor/cli/vendor/client/session-store.js +1 -575
  99. package/vendor/cli/vendor/client/topup.js +1 -297
  100. package/vendor/cli/vendor/client/touch.js +1 -458
  101. package/vendor/cli/vendor/client/update.js +1 -763
  102. package/vendor/cli/vendor/client/user-models.js +1 -381
  103. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  104. package/vendor/cli/vendor/desktop/lib/local/attachments.js +1 -276
  105. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  106. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -1028
  107. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -125
  108. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3666
  109. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -660
  110. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  111. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2145
  112. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -280
  113. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  114. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -238
  115. package/vendor/cli/vendor/desktop/lib/local/spill.js +1 -75
  116. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  117. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  118. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  119. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  120. package/vendor/cli/vendor/hosts/install.js +1 -638
  121. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  122. package/vendor/cli/vendor/hosts/spec.js +1 -164
  123. package/vendor/cli/vendor/hosts/targets.js +1 -346
  124. package/vendor/cli/vendor/mcp/server.js +1 -221
  125. package/vendor/cli/vendor/mcp/tools.js +1 -408
  126. package/vendor/cli/vendor/one/src/child.js +1 -285
  127. package/vendor/cli/vendor/one/src/permissions.js +1 -383
  128. package/vendor/desktop/bin/aegis.js +1 -32
  129. package/vendor/desktop/lib/account-key.js +1 -56
  130. package/vendor/desktop/lib/auto.js +1 -461
  131. package/vendor/desktop/lib/avatar/companion.js +1 -698
  132. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  133. package/vendor/desktop/lib/avatar/events.js +1 -344
  134. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  135. package/vendor/desktop/lib/avatar/holders.js +1 -612
  136. package/vendor/desktop/lib/avatar/identity.js +1 -641
  137. package/vendor/desktop/lib/avatar/level.js +1 -368
  138. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  139. package/vendor/desktop/lib/avatar/persona.js +1 -388
  140. package/vendor/desktop/lib/avatar/profile.js +1 -977
  141. package/vendor/desktop/lib/avatar/register.js +1 -307
  142. package/vendor/desktop/lib/avatar/store.js +1 -472
  143. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  144. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  145. package/vendor/desktop/lib/avatar/turn.js +1 -216
  146. package/vendor/desktop/lib/avatar/voice.js +1 -328
  147. package/vendor/desktop/lib/avatar/xp.js +1 -404
  148. package/vendor/desktop/lib/deep-link.js +1 -120
  149. package/vendor/desktop/lib/fingerprint.js +2 -347
  150. package/vendor/desktop/lib/google-connect.js +1 -323
  151. package/vendor/desktop/lib/local/agents.js +1 -105
  152. package/vendor/desktop/lib/local/attachments.js +1 -276
  153. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  154. package/vendor/desktop/lib/local/claude-pro.js +1 -1028
  155. package/vendor/desktop/lib/local/context.js +1 -125
  156. package/vendor/desktop/lib/local/engine.js +1 -3666
  157. package/vendor/desktop/lib/local/entitlement.js +1 -660
  158. package/vendor/desktop/lib/local/git-scope.js +1 -228
  159. package/vendor/desktop/lib/local/local.js +1 -2145
  160. package/vendor/desktop/lib/local/prompt.js +1 -280
  161. package/vendor/desktop/lib/local/queue.js +1 -560
  162. package/vendor/desktop/lib/local/session-rounds.js +1 -238
  163. package/vendor/desktop/lib/local/spill.js +1 -75
  164. package/vendor/desktop/lib/local/telemetry.js +1 -470
  165. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  166. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  167. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  168. package/vendor/desktop/lib/quick-launcher.js +1 -84
  169. package/vendor/desktop/lib/settings.js +1 -890
  170. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  171. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  172. package/vendor/desktop/lib/sync/sessions.js +1 -106
  173. package/vendor/desktop/lib/window-state.js +1 -66
  174. package/vendor/desktop/package.json +2 -4
  175. package/vendor/desktop/preload.js +1 -679
  176. package/vendor/desktop/renderer/app.js +1 -7250
  177. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  178. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  179. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  180. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  181. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  182. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  183. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  184. package/vendor/desktop/renderer/budget.js +1 -172
  185. package/vendor/desktop/renderer/companion.js +1 -223
  186. package/vendor/desktop/renderer/diff.js +1 -359
  187. package/vendor/desktop/renderer/diffview.js +1 -336
  188. package/vendor/desktop/renderer/filepreview.js +1 -211
  189. package/vendor/desktop/renderer/fingerprint.js +3 -789
  190. package/vendor/desktop/renderer/markdown.js +1 -168
  191. package/vendor/desktop/renderer/provider-mark.js +1 -311
  192. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  193. package/vendor/desktop/renderer/providers.js +1 -259
  194. package/vendor/desktop/renderer/quick.js +1 -285
  195. package/vendor/desktop/renderer/stream-policy.js +1 -387
  196. package/vendor/desktop/renderer/transcript-view.js +1 -323
  197. package/vendor/desktop/renderer/usage.js +1 -1239
  198. package/vendor/desktop/vendor/aegis.js +1 -2441
  199. package/vendor/desktop/vendor/attest.js +1 -518
  200. package/vendor/desktop/vendor/authorship-key.js +1 -410
  201. package/vendor/desktop/vendor/auto-provision.js +1 -134
  202. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  203. package/vendor/desktop/vendor/byok-catalog.js +1 -199
  204. package/vendor/desktop/vendor/catalog-client.js +1 -487
  205. package/vendor/desktop/vendor/conversation-export.js +1 -177
  206. package/vendor/desktop/vendor/credentials.js +1 -494
  207. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  208. package/vendor/desktop/vendor/drive-connect.js +1 -110
  209. package/vendor/desktop/vendor/drive-upload.js +1 -234
  210. package/vendor/desktop/vendor/env-file.js +1 -1273
  211. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  212. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  213. package/vendor/desktop/vendor/google-signin.js +1 -133
  214. package/vendor/desktop/vendor/hooks.js +1 -430
  215. package/vendor/desktop/vendor/model-discovery.js +1 -373
  216. package/vendor/desktop/vendor/naming.js +1 -283
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -575
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -763
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,278 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * attachments.js — drag-and-drop attachments for the desktop composer.
10
- *
11
- * Pure Node, no Electron imports: the one privileged operation this needs
12
- * (downscaling an oversized image) is *injected* by main.js as `resize`, so
13
- * this module stays unit-testable in plain Node and the renderer never gets
14
- * a handle on `nativeImage`.
15
- *
16
- * Classification is by MAGIC BYTES, never by extension. A dropped `.png` that
17
- * is actually a 40 MB ZIP must not be base64-inlined into a provider request,
18
- * and `path.extname` is attacker-controlled in exactly the case that matters
19
- * (a file someone else named). Anything we cannot positively identify is
20
- * treated as binary and refused rather than guessed at.
21
- *
22
- * The three drop behaviours the composer supports all land here:
23
- * 1. path insertion — `describe()` gives the chip label, and the renderer
24
- * puts the absolute path in the prompt text.
25
- * 2. text inlining — `kind: 'text'` returns the file's contents (capped).
26
- * 3. vision — `kind: 'image'` returns a data: URL for
27
- * `{ type: 'image_url' }` content parts.
28
- */
29
-
30
- // ── Caps ─────────────────────────────────────────────────────────────────────
31
- // Every one of these is a refusal, not a truncation, except where noted. An
32
- // attachment that silently shrank would be worse than one that said no.
33
-
34
- /** Largest single file we will ever read off disk for an attachment. */
35
- const MAX_FILE_BYTES = 32 * 1024 * 1024;
36
- /** A text file is inlined into the prompt, so it is capped far lower — this
37
- * lands in the token budget, and 512 KB of source is already ~128k tokens. */
38
- const MAX_TEXT_BYTES = 512 * 1024;
39
- /** Images above this are downscaled (not refused) when a resize hook exists. */
40
- const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
41
- /** How many attachments one turn may carry. Each image is a real token cost. */
42
- const MAX_ATTACHMENTS = 8;
43
-
44
- /** Fixed token cost charged per image by the context trimmer. A vision model
45
- * bills a tile-based amount that depends on provider and resolution; this is
46
- * the conservative flat figure the trimmer uses so an image turn is not
47
- * counted as free (see messageTokens in context.js). */
48
- const IMAGE_TOKEN_COST = 850;
49
-
50
- // ── Magic-byte sniffing ──────────────────────────────────────────────────────
51
-
52
- const PNG_SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
53
- const JPEG_SIG = Buffer.from([0xff, 0xd8, 0xff]);
54
- const GIF87 = Buffer.from('GIF87a', 'ascii');
55
- const GIF89 = Buffer.from('GIF89a', 'ascii');
56
- const PDF_SIG = Buffer.from('%PDF-', 'ascii');
57
- const BMP_SIG = Buffer.from('BM', 'ascii');
58
-
59
- function startsWith(buf, sig) {
60
- if (!Buffer.isBuffer(buf) || buf.length < sig.length) return false;
61
- return buf.compare(sig, 0, sig.length, 0, sig.length) === 0;
62
- }
63
-
64
- /** RIFF....WEBP — the tag sits at offset 8, not 0. */
65
- function isWebp(buf) {
66
- return (
67
- Buffer.isBuffer(buf) &&
68
- buf.length >= 12 &&
69
- buf.toString('ascii', 0, 4) === 'RIFF' &&
70
- buf.toString('ascii', 8, 12) === 'WEBP'
71
- );
72
- }
73
-
74
- /**
75
- * Heuristic text test: no NUL bytes in the probe window, and valid UTF-8.
76
- * A UTF-16/32 file reads as "binary" here on purpose — it would decode to
77
- * mojibake in the prompt, and a wrong guess is the failure this avoids.
78
- *
79
- * @param {Buffer} buf
80
- * @param {number} [probe=8192]
81
- */
82
- function looksLikeText(buf, probe = 8192) {
83
- if (!Buffer.isBuffer(buf) || buf.length === 0) return false;
84
- const window = buf.subarray(0, Math.min(buf.length, probe));
85
- if (window.includes(0)) return false;
86
- const dec = new TextDecoder('utf-8', { fatal: true });
87
- try {
88
- dec.decode(window);
89
- } catch {
90
- return false;
91
- }
92
- return true;
93
- }
94
-
95
- /**
96
- * Identify a buffer.
97
- *
98
- * @param {Buffer} buf
99
- * @returns {{kind:'image'|'pdf'|'text'|'binary', mime:string, ext:string}}
100
- */
101
- function sniff(buf) {
102
- if (startsWith(buf, PNG_SIG)) return { kind: 'image', mime: 'image/png', ext: 'png' };
103
- if (startsWith(buf, JPEG_SIG)) return { kind: 'image', mime: 'image/jpeg', ext: 'jpg' };
104
- if (startsWith(buf, GIF87) || startsWith(buf, GIF89)) {
105
- return { kind: 'image', mime: 'image/gif', ext: 'gif' };
106
- }
107
- if (isWebp(buf)) return { kind: 'image', mime: 'image/webp', ext: 'webp' };
108
- if (startsWith(buf, PDF_SIG)) return { kind: 'pdf', mime: 'application/pdf', ext: 'pdf' };
109
- // BMP last of the signatures: 'BM' is only two bytes and is the likeliest
110
- // false positive, so it is tested after every longer signature.
111
- if (startsWith(buf, BMP_SIG) && buf.length > 14) {
112
- return { kind: 'image', mime: 'image/bmp', ext: 'bmp' };
113
- }
114
- if (looksLikeText(buf)) return { kind: 'text', mime: 'text/plain', ext: 'txt' };
115
- return { kind: 'binary', mime: 'application/octet-stream', ext: '' };
116
- }
117
-
118
- /** True for the image MIME types a vision provider will accept. */
119
- function isVisionMime(mime) {
120
- return mime === 'image/png' || mime === 'image/jpeg' || mime === 'image/webp' || mime === 'image/gif';
121
- }
122
-
123
- // ── Content-part construction (OpenAI chat format) ───────────────────────────
124
-
125
- /** `data:<mime>;base64,<...>` — the only image form a provider will take from
126
- * a sandboxed renderer, which can never expose a local path to the model. */
127
- function toDataUrl(buf, mime) {
128
- if (!Buffer.isBuffer(buf)) return null;
129
- return `data:${mime || 'application/octet-stream'};base64,${buf.toString('base64')}`;
130
- }
131
-
132
- /** One vision part. */
133
- function imagePart(buf, mime) {
134
- const url = toDataUrl(buf, mime);
135
- if (!url) return null;
136
- return { type: 'image_url', image_url: { url } };
137
- }
138
-
139
- /**
140
- * Flatten message `content` to its text, for token estimation and for the
141
- * transcript paths that must stay string-only. Images contribute a marker, not
142
- * their base64 — counting a 1 MB data URL as ~250k tokens is how a vision turn
143
- * would get its own history trimmed away.
144
- *
145
- * @param {string|Array|null|undefined} content
146
- * @returns {string}
147
- */
148
- function flattenContent(content) {
149
- if (content == null) return '';
150
- if (typeof content === 'string') return content;
151
- if (!Array.isArray(content)) return String(content);
152
- const out = [];
153
- for (const part of content) {
154
- if (!part || typeof part !== 'object') continue;
155
- if (typeof part.text === 'string') out.push(part.text);
156
- else if (part.type === 'image_url') out.push('[image]');
157
- }
158
- return out.join('\n');
159
- }
160
-
161
- /** Count of image parts in a content value. */
162
- function imageCount(content) {
163
- if (!Array.isArray(content)) return 0;
164
- let n = 0;
165
- for (const part of content) {
166
- if (part && part.type === 'image_url') n += 1;
167
- }
168
- return n;
169
- }
170
-
171
- /**
172
- * Turn a prompt plus ingested attachments into wire `content`.
173
- *
174
- * Returns a plain string when there is nothing structured to send — that is
175
- * the overwhelmingly common turn, and keeping it a string means no provider,
176
- * transcript or replay path changes behaviour for users who never drop a file.
177
- *
178
- * @param {string} prompt
179
- * @param {Array<object>} attachments from ingest()
180
- * @returns {string|Array<object>}
181
- */
182
- /** The MIME an attachment actually carries: its own `mime`, or the one encoded
183
- * in its data URL when the field is absent. `ingestAttachment` always sets
184
- * both, but an attachment assembled elsewhere (or a fixture) may only have the
185
- * data URL, and refusing those would break turns that used to work. */
186
- function effectiveMime(a) {
187
- if (a && typeof a.mime === 'string' && a.mime) return a.mime;
188
- const url = a && typeof a.dataUrl === 'string' ? a.dataUrl : '';
189
- const m = /^data:([^;,]+)[;,]/.exec(url);
190
- return m ? m[1].toLowerCase() : '';
191
- }
192
-
193
- function buildContent(prompt, attachments) {
194
- const list = Array.isArray(attachments) ? attachments.filter(Boolean) : [];
195
- if (!list.length) return typeof prompt === 'string' ? prompt : '';
196
- const parts = [];
197
- const text = typeof prompt === 'string' ? prompt : '';
198
- if (text.trim()) parts.push({ type: 'text', text });
199
- for (const a of list) {
200
- if (a.kind === 'image' && a.dataUrl && isVisionMime(effectiveMime(a))) {
201
- parts.push({ type: 'image_url', image_url: { url: a.dataUrl } });
202
- } else if (a.kind === 'image') {
203
- // An image format no vision model accepts — `sniff` identifies BMP, and
204
- // BMP is on no provider's accepted list. Sending it as an `image_url`
205
- // fails the WHOLE turn with a 400 rather than degrading, so it travels
206
- // as its path instead and the model can still reach it with a tool.
207
- const mime = effectiveMime(a) || 'unknown image type';
208
- const note = a.path
209
- ? `[attached image, not inline (${mime} is not a vision format): ${a.path}]\nThe file is not attached inline; use a tool to inspect it.`
210
- : `[attached image, not inline (${mime} is not a vision format)]`;
211
- parts.push({ type: 'text', text: note });
212
- } else if (a.kind === 'text' && typeof a.text === 'string') {
213
- parts.push({ type: 'text', text: `--- ${a.name} ---\n${a.text}` });
214
- } else if (a.kind === 'pdf') {
215
- // No PDF text extraction in a zero-dep shell: the path is what the model
216
- // can actually act on (read it with a tool), so that is what it gets.
217
- parts.push({ type: 'text', text: `[attached PDF: ${a.path}]\nUse a tool to read this file.` });
218
- } else if (a.path) {
219
- parts.push({ type: 'text', text: `[attached file: ${a.path}]` });
220
- }
221
- }
222
- // A single text part is not worth the array form; send the string so every
223
- // downstream string-shaped assumption keeps holding.
224
- if (parts.length === 1 && parts[0].type === 'text') return parts[0].text;
225
- if (!parts.length) return text;
226
- return parts;
227
- }
228
-
229
- /**
230
- * The absolute paths an attachment set contributes to the prompt for the
231
- * "path insertion" behaviour: one per line, ready to append to the composer
232
- * text. Quoted when the path contains whitespace, so it can be pasted into a
233
- * shell command unchanged.
234
- *
235
- * @param {Array<{path:string}>} attachments
236
- * @returns {string}
237
- */
238
- function pathLines(attachments) {
239
- const list = Array.isArray(attachments) ? attachments : [];
240
- return list
241
- .map((a) => (a && a.path ? (/[\s"']/.test(a.path) ? `"${a.path}"` : a.path) : ''))
242
- .filter(Boolean)
243
- .join('\n');
244
- }
245
-
246
- /** Chip label: name plus a human size, e.g. `photo.png · 240 KB`. */
247
- function describe(a) {
248
- if (!a || typeof a !== 'object') return '';
249
- const name = String(a.name || a.path || 'file');
250
- const size = Number(a.size) || 0;
251
- return `${name} · ${humanBytes(size)}`;
252
- }
253
-
254
- function humanBytes(n) {
255
- const bytes = Number(n) || 0;
256
- if (bytes < 1024) return `${bytes} B`;
257
- if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
258
- return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
259
- }
260
-
261
- module.exports = {
262
- MAX_FILE_BYTES,
263
- MAX_TEXT_BYTES,
264
- MAX_IMAGE_BYTES,
265
- MAX_ATTACHMENTS,
266
- IMAGE_TOKEN_COST,
267
- sniff,
268
- looksLikeText,
269
- isVisionMime,
270
- toDataUrl,
271
- imagePart,
272
- flattenContent,
273
- imageCount,
274
- buildContent,
275
- pathLines,
276
- describe,
277
- humanBytes,
278
- };
3
+ "use strict";const MAX_FILE_BYTES=33554432,MAX_TEXT_BYTES=524288,MAX_IMAGE_BYTES=5242880,MAX_ATTACHMENTS=8,IMAGE_TOKEN_COST=850,PNG_SIG=Buffer.from([137,80,78,71,13,10,26,10]),JPEG_SIG=Buffer.from([255,216,255]),GIF87=Buffer.from("GIF87a","ascii"),GIF89=Buffer.from("GIF89a","ascii"),PDF_SIG=Buffer.from("%PDF-","ascii"),BMP_SIG=Buffer.from("BM","ascii");function startsWith(t,e){return!(!Buffer.isBuffer(t)||t.length<e.length)&&0===t.compare(e,0,e.length,0,e.length)}function isWebp(t){return Buffer.isBuffer(t)&&t.length>=12&&"RIFF"===t.toString("ascii",0,4)&&"WEBP"===t.toString("ascii",8,12)}function looksLikeText(t,e=8192){if(!Buffer.isBuffer(t)||0===t.length)return!1;const i=t.subarray(0,Math.min(t.length,e));if(i.includes(0))return!1;const n=new TextDecoder("utf-8",{fatal:!0});try{n.decode(i)}catch{return!1}return!0}function sniff(t){return startsWith(t,PNG_SIG)?{kind:"image",mime:"image/png",ext:"png"}:startsWith(t,JPEG_SIG)?{kind:"image",mime:"image/jpeg",ext:"jpg"}:startsWith(t,GIF87)||startsWith(t,GIF89)?{kind:"image",mime:"image/gif",ext:"gif"}:isWebp(t)?{kind:"image",mime:"image/webp",ext:"webp"}:startsWith(t,PDF_SIG)?{kind:"pdf",mime:"application/pdf",ext:"pdf"}:startsWith(t,BMP_SIG)&&t.length>14?{kind:"image",mime:"image/bmp",ext:"bmp"}:looksLikeText(t)?{kind:"text",mime:"text/plain",ext:"txt"}:{kind:"binary",mime:"application/octet-stream",ext:""}}function isVisionMime(t){return"image/png"===t||"image/jpeg"===t||"image/webp"===t||"image/gif"===t}function toDataUrl(t,e){return Buffer.isBuffer(t)?`data:${e||"application/octet-stream"};base64,${t.toString("base64")}`:null}function imagePart(t,e){const i=toDataUrl(t,e);return i?{type:"image_url",image_url:{url:i}}:null}function flattenContent(t){if(null==t)return"";if("string"==typeof t)return t;if(!Array.isArray(t))return String(t);const e=[];for(const i of t)i&&"object"==typeof i&&("string"==typeof i.text?e.push(i.text):"image_url"===i.type&&e.push("[image]"));return e.join("\n")}function imageCount(t){if(!Array.isArray(t))return 0;let e=0;for(const i of t)i&&"image_url"===i.type&&(e+=1);return e}function effectiveMime(t){if(t&&"string"==typeof t.mime&&t.mime)return t.mime;const e=t&&"string"==typeof t.dataUrl?t.dataUrl:"",i=/^data:([^;,]+)[;,]/.exec(e);return i?i[1].toLowerCase():""}function buildContent(t,e){const i=Array.isArray(e)?e.filter(Boolean):[];if(!i.length)return"string"==typeof t?t:"";const n=[],r="string"==typeof t?t:"";r.trim()&&n.push({type:"text",text:r});for(const t of i)if("image"===t.kind&&t.dataUrl&&isVisionMime(effectiveMime(t)))n.push({type:"image_url",image_url:{url:t.dataUrl}});else if("image"===t.kind){const e=effectiveMime(t)||"unknown image type",i=t.path?`[attached image, not inline (${e} is not a vision format): ${t.path}]\nThe file is not attached inline; use a tool to inspect it.`:`[attached image, not inline (${e} is not a vision format)]`;n.push({type:"text",text:i})}else"text"===t.kind&&"string"==typeof t.text?n.push({type:"text",text:`--- ${t.name} ---\n${t.text}`}):"pdf"===t.kind?n.push({type:"text",text:`[attached PDF: ${t.path}]\nUse a tool to read this file.`}):t.path&&n.push({type:"text",text:`[attached file: ${t.path}]`});return 1===n.length&&"text"===n[0].type?n[0].text:n.length?n:r}function pathLines(t){return(Array.isArray(t)?t:[]).map((t=>t&&t.path?/[\s"']/.test(t.path)?`"${t.path}"`:t.path:"")).filter(Boolean).join("\n")}function describe(t){return t&&"object"==typeof t?`${String(t.name||t.path||"file")} · ${humanBytes(Number(t.size)||0)}`:""}function humanBytes(t){const e=Number(t)||0;return e<1024?`${e} B`:e<1048576?`${Math.round(e/1024)} KB`:`${(e/1048576).toFixed(1)} MB`}module.exports={MAX_FILE_BYTES:33554432,MAX_TEXT_BYTES:524288,MAX_IMAGE_BYTES:5242880,MAX_ATTACHMENTS:8,IMAGE_TOKEN_COST:850,sniff:sniff,looksLikeText:looksLikeText,isVisionMime:isVisionMime,toDataUrl:toDataUrl,imagePart:imagePart,flattenContent:flattenContent,imageCount:imageCount,buildContent:buildContent,pathLines:pathLines,describe:describe,humanBytes:humanBytes};