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

+37 -2
View File
@@ -12,11 +12,12 @@ import { onRestart } from "./delivery.js";
/**
* @typedef {"image"|"file"} AttachmentKind
* @typedef {"ready"|"error"} AttachmentState
* @typedef {"adding"|"ready"|"error"} AttachmentState
* @typedef {object} Attachment
* @property {string} id
* @property {string} name
* @property {string} path
* @property {string} [remotePath] - the path the agent sees (`hc put`)
* @property {number} bytes
* @property {AttachmentKind} kind
* @property {AttachmentState} state
@@ -133,6 +134,9 @@ export function reduce(state, action) {
},
};
case "attachment_update":
return attachmentUpdate(state, action);
case "removeAttachment":
return {
...state,
@@ -177,6 +181,30 @@ export function reduce(state, action) {
}
}
/**
* One attachment changes: it was added to the agent (`ready` with the path
* the agent sees), it failed (`error`), or it is tried again (`adding`). An
* `error` text belongs to the `error` state only.
*/
function attachmentUpdate(state, action) {
const files = list(state.attachments, action.agent);
const file = files.find((entry) => entry.id === action.id);
if (!file) return state;
const { error, ...rest } = file;
const next = { ...rest, ...action.patch };
if (next.state === "error") next.error = action.patch?.error ?? error ?? "";
else delete next.error;
return {
...state,
attachments: {
...state.attachments,
[action.agent]: files.map((entry) => (entry.id === action.id ? next : entry)),
},
};
}
/**
* The draft of an agent comes back from the disk.
*
@@ -207,7 +235,13 @@ function restoreDraft(state, action) {
carets: { ...(state.carets ?? {}), [agent]: Number.isInteger(draft.caret) ? draft.caret : draft.text.length },
attachments: {
...state.attachments,
[agent]: (draft.attachments ?? []).map((file) => ({ ...file })),
// A file that was still being added when the app went down is not on
// the agent: the tile says so and can be tried again.
[agent]: (draft.attachments ?? []).map((file) =>
file.state === "adding"
? { ...file, state: "error", error: "Dodawanie przerwane — ponów." }
: { ...file },
),
},
};
}
@@ -426,6 +460,7 @@ function toAttachment(file) {
bytes: file.bytes,
kind: file.kind ?? "file",
state: file.state ?? "ready",
...(file.remotePath !== undefined ? { remotePath: file.remotePath } : {}),
...(file.error !== undefined ? { error: file.error } : {}),
};
}