feat: unified attachment pipeline with clipboard image and file picker

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:36:00 +01:00
1 parent d63af25fc1
commit 761a80004a
7 files changed
+597 -57

No files matched your search

+284 -51
View File
@@ -27,6 +27,7 @@ import {
theme,
exec,
hc,
home,
shared,
setShared,
notify,
@@ -39,7 +40,15 @@ 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 } from "./attachments.js";
import { LIMITS, validateMany, mention, removeMention, kindOf } from "./attachments.js";
import {
clipboardScript,
parseClipboardResult,
pickerScript,
parsePickerResult,
staleFiles,
supportsImages,
} from "./clipboard.js";
import * as storage from "./storage.js";
import { record } from "./history.js";
import {
@@ -113,6 +122,12 @@ export default class Composer extends View {
this.draftLoading = new Set();
this.draftTimer = null;
// Attachments: the message that waits for the answer to "this agent may
// not take images" (null when nothing waits), and whether the file dialog
// is open.
this.confirmImage = null;
this.picking = false;
// The menu of suggestions: what it shows, which row is picked.
this.menu = null;
this.debounce = null;
@@ -132,6 +147,9 @@ export default class Composer extends View {
this.textarea.set_soft_wrap(true);
this.textarea.on("change", (event, cx) => this.changed(cx));
this.textarea.on("submit", (event, cx) => this.submitted(event, cx));
// A paste that found no text (an image, files): the clipboard is read by
// PowerShell and goes through the same pipeline as a drop.
this.textarea.on("paste_other", (event, cx) => this.pasteOther(cx));
this.textarea.on("focus", () => {
this.hasKeyboard = true;
});
@@ -170,6 +188,7 @@ export default class Composer extends View {
// Focus follows the click: nothing here takes the keyboard on its own.
this.cleanupPaste();
this.syncAgent(cx);
// The agent under the focus can change without the composer being
// touched - the sidebar, another pane. The draft that goes with it has to
@@ -220,6 +239,7 @@ export default class Composer extends View {
// The draft of the agent we leave goes to the disk at once.
this.saveDraftNow();
this.confirmImage = null;
this.currentKey = key;
if (!key) return;
@@ -598,7 +618,7 @@ export default class Composer extends View {
// --- sending -------------------------------------------------------------
/** The message goes out, or it waits in the queue. */
submit(text, cx, mode = "auto") {
submit(text, cx, mode = "auto", force = false) {
const agent = this.target();
if (!agent) {
this.notice("Composer nie ma do kogo pisać — brak agenta.", "error", cx);
@@ -619,33 +639,32 @@ export default class Composer extends View {
if (broken) {
// A file that did not make it to the agent is still in the way - the
// person removes it or fixes it first.
this.notice(`Plik ${broken.name} nie dotarł do agenta — usuń go albo wyślij go jeszcze raz.`, "error", cx);
this.notice(`Plik ${broken.name} nie dotarł do agenta — kliknij „Ponów” albo usuń go.`, "error", cx);
return;
}
if (attached.some((file) => file.state === "adding")) {
this.notice("Pliki są jeszcze dodawane do agenta — poczekaj chwilę.", "error", cx);
return;
}
const files = attached.filter((file) => file.state === "ready");
// An image for a program that may not take it: the person decides.
if (!force && files.some((file) => file.kind === "image") && !supportsImages(agent.app)) {
this.confirmImage = { text, mode, program: agent.program?.name ?? agentName(agent) };
cx.notify();
return;
}
this.confirmImage = null;
// The field is free again - the message is on its way.
this.clearField(cx);
for (const file of files) {
this.state = reduce(this.state, { type: "removeAttachment", agent: key, id: file.id });
}
// The files go to the agent's machine first; the mention in the text
// points at the path the agent sees, not at the path on this machine.
cx.spawn(async (cx) => {
let message = body;
for (const file of files) {
const landed = await this.putFile(agent, file);
if (!landed) {
// The agent cannot see that file - the message does not talk about
// it, and the tile stays with the reason.
message = removeMention(message, file.path);
continue;
}
message = message.split(mention(file.path)).join(mention(landed));
}
this.finish(agent, message, cx, mode);
});
// The files are on the agent's machine already (`addFiles`) and the text
// mentions them by the path the agent sees.
this.finish(agent, body, cx, mode);
}
/** The field is empty again. */
@@ -1041,30 +1060,75 @@ export default class Composer extends View {
cx.notify();
}
/** The file lands beside the agent's terminal; the path it got is the one the agent sees. */
async putFile(agent, file) {
/**
* The file lands beside the agent's terminal (`hc put`); the path it got is
* the one the agent sees, and the mention in the text uses it. A tile that
* failed stays, with the reason and a "Ponów" button.
*/
async putAttachment(agent, file, cx) {
const key = agentKey(agent);
let remote = "";
let reason = "";
try {
const res = await hc(["put", agent.address, file.path, "--json"]);
const payload = parseJson(res.stdout);
if (res.code !== 0 || (payload && payload.error)) {
this.putFailed(agent, file, resultError(res, payload));
return "";
reason = resultError(res, payload);
} else {
remote = landedPath(payload, res.stdout);
if (!remote) reason = "Agent nie podał ścieżki pliku.";
}
return landedPath(payload, res.stdout);
} catch (err) {
this.putFailed(agent, file, errorText(err));
return "";
reason = errorText(err);
}
// The tile was removed while the file was on its way.
if (!(this.state.attachments[key] ?? []).some((entry) => entry.id === file.id)) return;
if (reason) {
this.state = reduce(this.state, {
type: "attachment_update",
agent: key,
id: file.id,
patch: { state: "error", error: reason },
});
this.notice(`Nie udało się dostarczyć pliku ${file.name}: ${reason}`, "error");
} else {
this.state = reduce(this.state, {
type: "attachment_update",
agent: key,
id: file.id,
patch: { state: "ready", remotePath: remote },
});
this.addMention(key, remote);
}
this.scheduleDraftSave(cx);
cx.notify();
}
/** The file did not make it to the agent: the tile stays, with the reason. */
putFailed(agent, file, reason) {
/** The mention of a file that reached the agent goes into the message of that agent. */
addMention(key, remote) {
const token = mention(remote);
if (key === this.currentKey) {
const result = insertMentions(this.value, [token]);
this.textarea.set_value(result.value);
this.value = result.value;
return;
}
const result = insertMentions(this.state.drafts[key] ?? "", [token]);
this.state = reduce(this.state, { type: "draft", agent: key, text: result.value });
}
/** "Ponów": the tile that failed is sent to the agent again. */
retryFile(agent, id, cx) {
const key = agentKey(agent);
this.notice(`Nie udało się dostarczyć pliku ${file.name}: ${reason}`, "error");
this.state = reduce(this.state, {
type: "attach",
agent: key,
file: { ...file, state: "error", error: reason },
const file = (this.state.attachments[key] ?? []).find((entry) => entry.id === id);
if (!file || file.state !== "error") return;
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);
});
}
@@ -1247,15 +1311,34 @@ export default class Composer extends View {
/** Files were dropped on the composer. */
drop(event, cx) {
const agent = this.target();
if (!agent) return;
const paths = Array.isArray(event.paths) ? event.paths : [];
if (!paths.length) return;
this.addFiles(Array.isArray(event.paths) ? event.paths : [], "drop", cx);
}
/**
* The one way a file gets attached - dropped, pasted or picked with "+".
*
* 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.
*
* @param {string[]} paths
* @param {"drop"|"paste"|"picker"} origin
*/
addFiles(paths, 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);
cx.spawn(async (cx) => {
const files = [];
for (const path of paths) {
files.push({ id: this.newId(), name: nameOf(path), path, bytes: await sizeOf(path) });
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);
@@ -1263,24 +1346,102 @@ export default class Composer extends View {
this.notice(check.reason, "error");
return cx.notify();
}
const key = agentKey(agent);
for (const file of files) {
this.state = reduce(this.state, { type: "attach", agent: key, file });
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();
}
// The mention of the file goes into the message.
const result = insertMentions(
this.value,
files.map((file) => mention(file.path)),
);
this.textarea.set_value(result.value);
this.value = result.value;
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);
});
}
/** A paste found no text: the clipboard has files, an image - or nothing. */
pasteOther(cx) {
if (!this.target()) return;
cx.spawn(async (cx) => {
let result;
try {
const res = await exec("powershell", [
"-NoProfile",
"-STA",
"-Command",
clipboardScript(this.pasteDir(), String(Date.now())),
]);
result = parseClipboardResult(res);
} catch (err) {
result = { kind: "error", message: errorText(err) };
}
if (result.kind === "none") {
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 {
this.addFiles(result.paths, "paste", cx);
}
cx.notify();
});
}
/** The "+" button: the file dialog, then the same pipeline. */
pickFiles(cx) {
if (this.picking || !this.target()) return;
this.picking = true;
cx.spawn(async (cx) => {
try {
// The person may take their time with the dialog.
const res = await exec("powershell", ["-NoProfile", "-STA", "-Command", pickerScript()], {
timeout: 600000,
});
const paths = parsePickerResult(res);
if (paths.length) this.addFiles(paths, "picker", cx);
} catch (err) {
this.notice(`Nie udało się otworzyć okna wyboru plików: ${errorText(err)}`, "error");
} finally {
this.picking = false;
cx.notify();
}
});
}
/** Where pasted images are saved before they go to the agent. */
pasteDir() {
return [home(), ".config", "herdr-app", "data", "ovis-composer", "paste"].join("\\");
}
/**
* The pasted images older than 7 days go. The moment is in the name
* (`paste-<ms>.png`) - the file API of the mods has no `stat` for mtime -
* and only files of that shape are ever touched.
*/
async cleanupPaste() {
try {
const dir = this.pasteDir();
if (!(await fs.exists(dir))) return;
const entries = [];
for (const name of await fs.readdir(dir)) {
const match = /^paste-(\d+)\.png$/.exec(name);
if (match) entries.push({ path: `${dir}\\${name}`, mtimeMs: Number(match[1]) });
}
for (const path of staleFiles(entries, Date.now())) {
try {
await fs.unlink(path);
} catch {
// A file that cannot be removed stays; the next start tries again.
}
}
} catch {
// No paste folder, nothing to clean.
}
}
/** The file is not wanted any more - the mention goes with it. */
removeFile(agent, id, cx) {
const key = agentKey(agent);
@@ -1289,7 +1450,7 @@ export default class Composer extends View {
this.state = reduce(this.state, { type: "removeAttachment", agent: key, id });
const text = removeMention(this.value, file.path);
const text = file.remotePath ? removeMention(this.value, file.remotePath) : this.value;
if (text !== this.value) {
this.textarea.set_value(text);
this.value = text;
@@ -1393,6 +1554,7 @@ export default class Composer extends View {
[
this.renderMenu(t),
this.renderQueue(t, agent, key),
this.renderImageConfirm(t),
this.renderNotice(t),
this.renderCard(t, agent, key, status),
this.renderContext(t, agent),
@@ -1568,6 +1730,41 @@ export default class Composer extends View {
.child(text);
}
/** The question about an image for a program that may not take it. */
renderImageConfirm(t) {
const ask = this.confirmImage;
if (!ask) return null;
return v_flex()
.id("composer-image-confirm")
.gap(6)
.px(10)
.py(6)
.rounded(8)
.bg(t.chrome)
.child(
div()
.text_size(11.5)
.text_color(t.foreground)
.child(`${ask.program} może nie obsługiwać obrazów — obraz trafi do agenta tylko jako ścieżka w tekście.`),
)
.child(
h_flex()
.gap(6)
.child(
this.renderButton(t, "Anuluj", "image-cancel", (event, cx) => {
this.confirmImage = null;
cx.notify();
}),
)
.child(
this.renderButton(t, "Wyślij mimo to", "image-send", (event, cx) =>
this.submit(ask.text, cx, ask.mode, true),
),
),
);
}
/** What the composer says under the card. */
renderNotice(t) {
const notice = this.state.notice;
@@ -1615,6 +1812,7 @@ export default class Composer extends View {
h_flex()
.gap(8)
.items_center()
.child(this.renderPlus(t, agent))
.child(this.renderChip(t, agent, status))
.child(div().flex_1())
.child(stop ? this.renderStop(t, agent) : this.renderSend(t, agent, over)),
@@ -1622,6 +1820,25 @@ export default class Composer extends View {
);
}
/** The "+" button: pick files with the system dialog. */
renderPlus(t, agent) {
return div()
.id("composer-plus")
.size(28)
.rounded(999)
.flex_none()
.flex()
.items_center()
.justify_center()
.bg(t.chrome)
.text_size(15)
.text_color(agent ? t.muted : t.dim)
.cursor_pointer()
.hover((s) => s.bg(t.hover).text_color(t.foreground))
.on_click((event, cx) => this.pickFiles(cx))
.child("+");
}
/** The tiles of the files that go with the message. */
renderFiles(t, agent) {
const key = agentKey(agent);
@@ -1648,6 +1865,22 @@ export default class Composer extends View {
.truncate()
.child(file.name),
)
.when(file.state === "adding", (el) =>
el.child(div().text_size(10.5).text_color(t.dim).child("dodawanie…")),
)
.when(file.state === "error", (el) =>
el.child(
div()
.id(`file-retry-${file.id}`)
.px(4)
.text_size(11)
.text_color(t.blocked)
.cursor_pointer()
.hover((s) => s.text_color(t.foreground))
.on_click((event, cx) => this.retryFile(agent, file.id, cx))
.child("Ponów"),
),
)
.child(
div()
.id(`file-x-${file.id}`)