aegiscode 7.0.26 → 7.0.27
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.
- package/SOURCE-OFFER.txt +9 -0
- package/package.json +4 -7
- package/scripts/check.mjs +1 -52
- package/scripts/demo.mjs +1 -94
- package/scripts/predist.mjs +1 -75
- package/scripts/runtests.mjs +1 -56
- package/scripts/seal-gate.mjs +1 -74
- package/src/agents.js +1 -139
- package/src/app.js +1 -2957
- package/src/art.js +1 -289
- package/src/chatflow.js +1 -3023
- package/src/checkpoint.js +1 -90
- package/src/clipboard.js +1 -424
- package/src/cloudsync.js +1 -528
- package/src/commands.js +1 -4089
- package/src/config.js +1 -581
- package/src/connect.js +1 -349
- package/src/credentials.js +1 -33
- package/src/deps.js +1 -232
- package/src/devrun.js +1 -187
- package/src/diff.js +1 -333
- package/src/diffview.js +1 -296
- package/src/engine.js +1 -297
- package/src/entitlement.js +1 -355
- package/src/events.js +1 -707
- package/src/export.js +1 -67
- package/src/filepreview.js +1 -153
- package/src/fingerprint.js +1 -592
- package/src/format.js +1 -259
- package/src/fuzzy.js +1 -119
- package/src/history.js +1 -423
- package/src/init.js +1 -175
- package/src/input.js +1 -139
- package/src/keys.js +1 -183
- package/src/markdown.js +1 -306
- package/src/models.js +1 -507
- package/src/naming.js +1 -57
- package/src/overlays.js +1 -462
- package/src/panels.js +1 -1418
- package/src/ping.js +1 -226
- package/src/presence.js +1 -238
- package/src/render.js +1 -516
- package/src/screen.js +1 -518
- package/src/screens.js +1 -896
- package/src/secret.js +1 -59
- package/src/securefs.js +1 -359
- package/src/settings.js +1 -155
- package/src/shared.js +1 -52
- package/src/sharedpaths.js +1 -53
- package/src/signup.js +1 -156
- package/src/summarize.js +1 -95
- package/src/sysdiag.js +1 -585
- package/src/system.js +1 -40
- package/src/theme.js +1 -570
- package/src/title.js +1 -220
- package/src/todos.js +1 -115
- package/src/tokens.js +1 -263
- package/src/topup.js +1 -210
- package/src/update.js +1 -191
- package/vendor/client/aegis.js +1 -2441
- package/vendor/client/attest.js +1 -518
- package/vendor/client/authorship-key.js +1 -410
- package/vendor/client/auto-provision.js +1 -134
- package/vendor/client/brain-catalog.js +1 -268
- package/vendor/client/byok-catalog.js +1 -199
- package/vendor/client/catalog-client.js +1 -487
- package/vendor/client/conversation-export.js +1 -177
- package/vendor/client/credentials.js +1 -494
- package/vendor/client/device-fingerprint.js +1 -141
- package/vendor/client/drive-connect.js +1 -110
- package/vendor/client/drive-upload.js +1 -234
- package/vendor/client/env-file.js +1 -1273
- package/vendor/client/fingerprint.js +1 -1064
- package/vendor/client/foreign-memory.js +1 -669
- package/vendor/client/google-signin.js +1 -133
- package/vendor/client/hooks.js +1 -430
- package/vendor/client/model-discovery.js +1 -373
- package/vendor/client/naming.js +1 -283
- package/vendor/client/platform.js +1 -937
- package/vendor/client/prefs.js +1 -351
- package/vendor/client/presence-constants.js +1 -107
- package/vendor/client/presence.js +1 -438
- package/vendor/client/record-vault.js +3 -1055
- package/vendor/client/session-store.js +1 -575
- package/vendor/client/topup.js +1 -297
- package/vendor/client/touch.js +1 -458
- package/vendor/client/update.js +1 -763
- package/vendor/client/user-models.js +1 -381
- package/vendor/desktop/lib/local/agents.js +1 -105
- package/vendor/desktop/lib/local/attachments.js +1 -276
- package/vendor/desktop/lib/local/autonomous.js +1 -1045
- package/vendor/desktop/lib/local/claude-pro.js +1 -1028
- package/vendor/desktop/lib/local/context.js +1 -125
- package/vendor/desktop/lib/local/engine.js +1 -3666
- package/vendor/desktop/lib/local/entitlement.js +1 -660
- package/vendor/desktop/lib/local/exec-plan.js +1 -269
- package/vendor/desktop/lib/local/git-scope.js +1 -228
- package/vendor/desktop/lib/local/integrity.js +1 -163
- package/vendor/desktop/lib/local/local.js +1 -2145
- package/vendor/desktop/lib/local/prompt.js +1 -280
- package/vendor/desktop/lib/local/queue.js +1 -560
- package/vendor/desktop/lib/local/session-rounds.js +1 -238
- package/vendor/desktop/lib/local/shell.js +1 -712
- package/vendor/desktop/lib/local/spill.js +1 -75
- package/vendor/desktop/lib/local/tools.js +1 -1328
- package/vendor/desktop/lib/local/turn-guard.js +1 -300
- package/vendor/desktop/lib/local/worktree-lock.js +1 -235
- package/vendor/desktop/lib/settings.js +1 -890
- package/vendor/desktop/main.js +3 -4890
- package/vendor/desktop/renderer/usage.js +1 -1239
- package/vendor/hosts/install.js +1 -638
- package/vendor/hosts/openai-shim.js +1 -515
- package/vendor/hosts/spec.js +1 -164
- package/vendor/hosts/targets.js +1 -346
- package/vendor/mcp/server.js +1 -221
- package/vendor/mcp/tools.js +1 -408
- package/vendor/one/src/child.js +1 -285
- package/vendor/one/src/permissions.js +1 -383
|
@@ -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
|
-
|
|
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};
|