// attachments.js - what may be attached to a message and how it is named. // // Pure: no gpui-kit, no gpui-base, no herdr. The limits and the mention rules // live here, so the view (Task 8) has one place to ask and the tests // (tests/attachments.test.mjs) can check them in plain Node. // // The mention holds the path on the AGENT's machine - the path `hc put` // printed, the file as it landed there - and not the local path of the file // that was dropped. For a remote machine the local path does not even exist. /** The limits the composer keeps to (the same numbers Ovis uses). */ export const LIMITS = { fileBytes: 32 * 1024 * 1024, filesPerAction: 100, messageChars: 20000, }; /** A character that can be part of a path (letters of any alphabet too: "ęą"). */ const PATH_CHAR = /[\p{L}\p{N}_/\\-]/u; /** The extensions that make a file an image (the design's §13.8 list). */ const IMAGE_EXTENSIONS = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp"]); /** * May this file be attached? * * Two rules: the file may not be bigger than 32 MiB, and the composer has to * know what it is - a file it can carry or an image it can show a thumbnail * of. A size that could not be measured is not a size that broke the limit, * so it passes; a size that is not a number at all is a broken file. * * @param {{name?:string, path?:string, bytes?:number, kind?:string}} file * @returns {{ok:true} | {ok:false, reason:string}} */ export function validate(file) { if (!file || typeof file !== "object" || Array.isArray(file)) { return { ok: false, reason: "Nieznany plik" }; } const bytes = file.bytes; if (bytes !== undefined && bytes !== null && !Number.isFinite(bytes)) { return { ok: false, reason: "Nieznany rozmiar pliku" }; } if (Number.isFinite(bytes) && bytes > LIMITS.fileBytes) { return { ok: false, reason: `Plik przekracza limit ${LIMITS.fileBytes / 1024 / 1024} MiB` }; } const kind = kindOf(file); if (kind !== "file" && kind !== "image") { return { ok: false, reason: "Nieobsługiwany typ pliku" }; } return { ok: true }; } /** * May this many files be attached in one go? * * One action - one drop, one send - carries at most 100 files, and every one * of them has to pass on its own. * * @param {object[]} files * @returns {{ok:true} | {ok:false, reason:string}} */ export function validateMany(files) { const list = Array.isArray(files) ? files : []; if (list.length > LIMITS.filesPerAction) { return { ok: false, reason: `Za dużo plików (limit ${LIMITS.filesPerAction})` }; } for (const file of list) { const result = validate(file); if (!result.ok) return result; } return { ok: true }; } /** * The mention of an attachment - the path as the agent sees it. * * A path with whitespace in it is quoted - `@"/a b/c.png"` - or the agent * would read only the part up to the first space; any other path stays bare * (`@/a/c.png`). `removeMention` takes out exactly the form built here. * * @param {string} path - the path on the agent's machine * @returns {string} the mention, or nothing when there is no path */ export function mention(path) { if (typeof path !== "string") return ""; const clean = path.trim(); if (clean === "") return ""; return /\s/.test(clean) ? `@"${clean}"` : `@${clean}`; } /** * Take the mention of an attachment out of the text. * * Only the mention itself goes: a longer word that starts with the same * characters (`@src/a.rsx`) is left alone. The space that separated the * mention from the rest of the sentence goes with it, so the text does not * keep a hole where the mention was. * * @param {string} text * @param {string} path - the agent-side path, the one `mention` builds * @returns {string} */ export function removeMention(text, path) { if (typeof text !== "string") return ""; const token = mention(path); if (token === "" || text === "") return text; const kept = []; let rest = text; for (;;) { const at = rest.indexOf(token); if (at < 0) break; const before = at === 0 ? "" : rest[at - 1]; const after = rest[at + token.length] ?? ""; const afterNext = rest[at + token.length + 1] ?? ""; if (!standalone(before, after, afterNext)) { // a longer word that starts with the same characters - not our mention kept.push(rest.slice(0, at + token.length)); rest = rest.slice(at + token.length); continue; } kept.push(rest.slice(0, at)); rest = rest.slice(at + token.length).replace(/^[ \t]+/, ""); // the mention ended the line - the space before it goes too const tail = kept[kept.length - 1]; if (/[ \t]$/.test(tail) && (rest === "" || rest.startsWith("\n"))) { kept[kept.length - 1] = tail.replace(/[ \t]+$/, ""); } } kept.push(rest); return kept.join(""); } /** * What the file is: what the caller says it is, or - when it says nothing - * what the name says. Anything else than a file or an image is a type the * composer cannot carry. * * @param {{name?:string, kind?:string}} file * @returns {string} */ export function kindOf(file) { if (typeof file.kind === "string" && file.kind !== "") return file.kind.toLowerCase(); const name = typeof file.name === "string" ? file.name.toLowerCase() : ""; const dot = name.lastIndexOf("."); if (dot < 0) return "file"; const extension = name.slice(dot + 1); return IMAGE_EXTENSIONS.has(extension) ? "image" : "file"; } /** * Is the mention a word of its own? A word may not be glued to the mention * on either side, and a dot that continues the path (`@src/a.rs.txt`) makes * it a different file. * * @param {string} before - the character in front of the mention * @param {string} after - the character behind the mention * @param {string} afterNext - the character behind that one * @returns {boolean} */ function standalone(before, after, afterNext) { if (before !== "" && PATH_CHAR.test(before)) return false; if (after !== "" && PATH_CHAR.test(after)) return false; if (after === "." && PATH_CHAR.test(afterNext)) return false; return true; } /** * A size for a tile: `512 B`, `1,5 KB`, `5 MB`. * * @param {number|undefined|null} bytes * @returns {string} nothing when the size is not known */ export function formatBytes(bytes) { if (!Number.isFinite(bytes) || bytes < 0) return ""; const unit = (value, name) => `${String(Math.round(value * 10) / 10).replace(".", ",")} ${name}`; if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return unit(bytes / 1024, "KB"); if (bytes < 1024 * 1024 * 1024) return unit(bytes / 1024 / 1024, "MB"); return unit(bytes / 1024 / 1024 / 1024, "GB"); } /** * The path of a file relative to a folder - or nothing when the file is not * inside it. * * `image()` draws only files of the application's own folder, and wants their * path relative to it (forward slashes). Windows paths compare without regard * to case and to the kind of slash. * * @param {string} base - the folder (`appHome()`) * @param {string} path - the file * @returns {string|null} */ export function relativeTo(base, path) { if (typeof base !== "string" || typeof path !== "string" || base === "" || path === "") return null; const clean = (text) => text.replace(/\\/g, "/").replace(/\/+$/, ""); const root = clean(base); const file = clean(path); if (file.length <= root.length + 1) return null; if (file.slice(0, root.length).toLowerCase() !== root.toLowerCase()) return null; if (file[root.length] !== "/") return null; return file.slice(root.length + 1); }