172 lines
5.6 KiB
JavaScript
172 lines
5.6 KiB
JavaScript
// 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,
|
|
};
|
|
|
|
/** 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.
|
|
*
|
|
* @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();
|
|
return 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}
|
|
*/
|
|
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 !== "" && /[\w/\\-]/.test(before)) return false;
|
|
if (after !== "" && /[\w/\\-]/.test(after)) return false;
|
|
if (after === "." && /[\w/\\-]/.test(afterNext)) return false;
|
|
return true;
|
|
}
|