fix: file sizes from PowerShell, quoted mentions, dedupe and BOM
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
761a80004a
commit
1ed04fe9be
5 files changed
+339
-94
No files matched your search
+119
-50
@@ -40,12 +40,14 @@ import { keyIntent } from "./keys.js";
|
||||
import { decide, shouldDrain, shouldStartCountdown, sendNow as sendToTerminal } from "./delivery.js";
|
||||
import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js";
|
||||
import * as catalog from "./catalog.js";
|
||||
import { LIMITS, validateMany, mention, removeMention, kindOf } from "./attachments.js";
|
||||
import { LIMITS, validate, validateMany, mention, removeMention, kindOf } from "./attachments.js";
|
||||
import {
|
||||
clipboardScript,
|
||||
parseClipboardResult,
|
||||
pickerScript,
|
||||
parsePickerResult,
|
||||
sizeScript,
|
||||
parseSizeResult,
|
||||
staleFiles,
|
||||
supportsImages,
|
||||
} from "./clipboard.js";
|
||||
@@ -83,6 +85,12 @@ import {
|
||||
/** The placeholder of the field. */
|
||||
const PLACEHOLDER = "Napisz do agenta…";
|
||||
|
||||
/** What the composer says when a size cannot be read (the file does not go on a guess). */
|
||||
const SIZE_ERROR = "Nie można odczytać rozmiaru pliku";
|
||||
|
||||
/** Folders are not carried - only files. */
|
||||
const DIR_NOTICE = "Foldery nie są obsługiwane — dodaj pliki.";
|
||||
|
||||
/** How many rows the menu of the suggestions shows at most. */
|
||||
const MENU_LIMIT = 20;
|
||||
|
||||
@@ -419,6 +427,8 @@ export default class Composer extends View {
|
||||
const next = this.textarea.value();
|
||||
const previous = this.value;
|
||||
this.value = next;
|
||||
// The question was about that text - another text, another question.
|
||||
if (this.confirmImage && this.confirmImage.text !== next) this.confirmImage = null;
|
||||
|
||||
// Enter puts a newline into the field before the composer sends it - the
|
||||
// newline is not part of the message.
|
||||
@@ -1119,7 +1129,10 @@ export default class Composer extends View {
|
||||
this.state = reduce(this.state, { type: "draft", agent: key, text: result.value });
|
||||
}
|
||||
|
||||
/** "Ponów": the tile that failed is sent to the agent again. */
|
||||
/**
|
||||
* "Ponów": the tile that failed is sent to the agent again. A file whose
|
||||
* size was never read is measured first - it does not go on a guess.
|
||||
*/
|
||||
retryFile(agent, id, cx) {
|
||||
const key = agentKey(agent);
|
||||
const file = (this.state.attachments[key] ?? []).find((entry) => entry.id === id);
|
||||
@@ -1128,7 +1141,24 @@ export default class Composer extends View {
|
||||
this.state = reduce(this.state, { type: "attachment_update", agent: key, id, patch: { state: "adding" } });
|
||||
cx.notify();
|
||||
cx.spawn(async (cx) => {
|
||||
await this.putAttachment(agent, file, cx);
|
||||
let current = file;
|
||||
if (!Number.isFinite(file.bytes)) {
|
||||
const measured = (await this.measure([file.path])).files[0];
|
||||
const check = measured && Number.isFinite(measured.bytes) ? validate({ ...file, bytes: measured.bytes }) : null;
|
||||
if (!check || !check.ok) {
|
||||
this.state = reduce(this.state, {
|
||||
type: "attachment_update",
|
||||
agent: key,
|
||||
id,
|
||||
patch: { state: "error", error: check ? check.reason : SIZE_ERROR },
|
||||
});
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
current = { ...file, bytes: measured.bytes };
|
||||
this.state = reduce(this.state, { type: "attachment_update", agent: key, id, patch: { bytes: measured.bytes } });
|
||||
}
|
||||
await this.putAttachment(agent, current, cx);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1309,9 +1339,30 @@ export default class Composer extends View {
|
||||
|
||||
// --- the attachments -----------------------------------------------------
|
||||
|
||||
/** Files were dropped on the composer. */
|
||||
/** Files were dropped on the composer: measured in one call, then the pipeline. */
|
||||
drop(event, cx) {
|
||||
this.addFiles(Array.isArray(event.paths) ? event.paths : [], "drop", cx);
|
||||
const paths = Array.isArray(event.paths) ? event.paths.filter((p) => typeof p === "string" && p !== "") : [];
|
||||
if (!paths.length) return;
|
||||
cx.spawn(async (cx) => {
|
||||
const measured = await this.measure(paths);
|
||||
if (measured.error) {
|
||||
// The sizes are unknown: the files get a tile with the reason, they
|
||||
// do not go through on a guess.
|
||||
this.addFiles(paths.map((path) => ({ path, bytes: null })), "drop", cx);
|
||||
return;
|
||||
}
|
||||
if (measured.dirs.length) this.notice(DIR_NOTICE, "error");
|
||||
this.addFiles(measured.files, "drop", cx);
|
||||
});
|
||||
}
|
||||
|
||||
/** PowerShell measures a batch of paths (the mods have no `stat`). Never throws. */
|
||||
async measure(paths) {
|
||||
try {
|
||||
return parseSizeResult(await exec("powershell", ["-NoProfile", "-STA", "-Command", sizeScript(paths)]));
|
||||
} catch (err) {
|
||||
return { files: [], dirs: [], missing: [], error: errorText(err) };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1319,46 +1370,72 @@ export default class Composer extends View {
|
||||
*
|
||||
* The files are checked (size, count, type), get a tile in the state
|
||||
* `adding`, go to the agent with `hc put`, and only then - `ready` - the
|
||||
* mention with the path the agent sees goes into the message.
|
||||
* mention with the path the agent sees goes into the message. A file whose
|
||||
* size is unknown (`bytes` null) gets an `error` tile and goes nowhere. A
|
||||
* file that is attached already is not added a second time.
|
||||
*
|
||||
* @param {string[]} paths
|
||||
* @param {{path:string, bytes:number|null}[]} entries
|
||||
* @param {"drop"|"paste"|"picker"} origin
|
||||
*/
|
||||
addFiles(paths, origin, cx) {
|
||||
addFiles(entries, origin, cx) {
|
||||
const agent = this.target();
|
||||
if (!agent) {
|
||||
this.notice("Wybierz agenta — nie ma do kogo dodać pliku.", "error", cx);
|
||||
return;
|
||||
}
|
||||
const list = [...new Set((Array.isArray(paths) ? paths : []).filter((path) => typeof path === "string" && path !== ""))];
|
||||
if (!list.length) return;
|
||||
|
||||
const key = agentKey(agent);
|
||||
|
||||
const have = new Set((this.state.attachments[key] ?? []).map((file) => file.path.toLowerCase()));
|
||||
const fresh = [];
|
||||
for (const entry of Array.isArray(entries) ? entries : []) {
|
||||
if (!entry || typeof entry.path !== "string" || entry.path === "") continue;
|
||||
const id = entry.path.toLowerCase();
|
||||
if (have.has(id)) continue;
|
||||
have.add(id);
|
||||
fresh.push(entry);
|
||||
}
|
||||
if (!fresh.length) {
|
||||
if ((entries ?? []).length) this.notice("Ten plik jest już dodany.", "error", cx);
|
||||
return;
|
||||
}
|
||||
|
||||
const files = fresh.map((entry) => {
|
||||
const name = nameOf(entry.path);
|
||||
return {
|
||||
id: this.newId(),
|
||||
name,
|
||||
path: entry.path,
|
||||
bytes: Number.isFinite(entry.bytes) ? entry.bytes : undefined,
|
||||
kind: kindOf({ name }),
|
||||
origin,
|
||||
};
|
||||
});
|
||||
const known = files.filter((file) => file.bytes !== undefined);
|
||||
|
||||
const check = validateMany(known);
|
||||
if (!check.ok) {
|
||||
this.notice(check.reason, "error", cx);
|
||||
return;
|
||||
}
|
||||
if ((this.state.attachments[key] ?? []).length + files.length > LIMITS.filesPerAction) {
|
||||
this.notice(`Za dużo plików (limit ${LIMITS.filesPerAction})`, "error", cx);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
const unknown = file.bytes === undefined;
|
||||
this.state = reduce(this.state, {
|
||||
type: "attach",
|
||||
agent: key,
|
||||
file: unknown ? { ...file, state: "error", error: SIZE_ERROR } : { ...file, state: "adding" },
|
||||
});
|
||||
}
|
||||
if (files.some((file) => file.bytes === undefined)) this.notice(SIZE_ERROR, "error");
|
||||
this.scheduleDraftSave(cx);
|
||||
cx.notify();
|
||||
|
||||
cx.spawn(async (cx) => {
|
||||
const files = [];
|
||||
for (const path of list) {
|
||||
const name = nameOf(path);
|
||||
files.push({ id: this.newId(), name, path, bytes: await sizeOf(path), kind: kindOf({ name }), origin });
|
||||
}
|
||||
|
||||
const check = validateMany(files);
|
||||
if (!check.ok) {
|
||||
this.notice(check.reason, "error");
|
||||
return cx.notify();
|
||||
}
|
||||
const total = (this.state.attachments[key] ?? []).length + files.length;
|
||||
if (total > LIMITS.filesPerAction) {
|
||||
this.notice(`Za dużo plików (limit ${LIMITS.filesPerAction})`, "error");
|
||||
return cx.notify();
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
this.state = reduce(this.state, { type: "attach", agent: key, file: { ...file, state: "adding" } });
|
||||
}
|
||||
this.scheduleDraftSave(cx);
|
||||
cx.notify();
|
||||
|
||||
for (const file of files) await this.putAttachment(agent, file, cx);
|
||||
for (const file of known) await this.putAttachment(agent, file, cx);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1383,8 +1460,11 @@ export default class Composer extends View {
|
||||
this.notice("Schowek nie zawiera tekstu ani obrazu, który można wkleić.", "error");
|
||||
} else if (result.kind === "error") {
|
||||
this.notice(`Nie udało się odczytać schowka: ${result.message}`, "error");
|
||||
} else if (result.kind === "dirs") {
|
||||
this.notice(DIR_NOTICE, "error");
|
||||
} else {
|
||||
this.addFiles(result.paths, "paste", cx);
|
||||
if (result.dirs.length) this.notice(DIR_NOTICE, "error");
|
||||
this.addFiles(result.files, "paste", cx);
|
||||
}
|
||||
cx.notify();
|
||||
});
|
||||
@@ -1400,8 +1480,8 @@ export default class Composer extends View {
|
||||
const res = await exec("powershell", ["-NoProfile", "-STA", "-Command", pickerScript()], {
|
||||
timeout: 600000,
|
||||
});
|
||||
const paths = parsePickerResult(res);
|
||||
if (paths.length) this.addFiles(paths, "picker", cx);
|
||||
const picked = parsePickerResult(res);
|
||||
if (picked.length) this.addFiles(picked, "picker", cx);
|
||||
} catch (err) {
|
||||
this.notice(`Nie udało się otworzyć okna wyboru plików: ${errorText(err)}`, "error");
|
||||
} finally {
|
||||
@@ -1759,7 +1839,7 @@ export default class Composer extends View {
|
||||
)
|
||||
.child(
|
||||
this.renderButton(t, "Wyślij mimo to", "image-send", (event, cx) =>
|
||||
this.submit(ask.text, cx, ask.mode, true),
|
||||
this.submit(this.value, cx, ask.mode, true),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -2034,14 +2114,3 @@ function landedPath(payload, stdout) {
|
||||
.filter(Boolean);
|
||||
return lines.length ? lines[lines.length - 1].trim() : "";
|
||||
}
|
||||
|
||||
/** The size of a file, when it can be measured. */
|
||||
async function sizeOf(path) {
|
||||
try {
|
||||
const info = await fs.stat(path);
|
||||
return Number.isFinite(info?.size) ? info.size : undefined;
|
||||
} catch {
|
||||
// A size that could not be measured is not a size that broke the limit.
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user