Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat(chat): sent images and PDFs stay visible in the conversation
Salman Qureshiad46771995f4
diff --git a/apps/desktop/src/api/types.ts b/apps/desktop/src/api/types.tsindex 64829669..9ff53f6f 100644--- a/apps/desktop/src/api/types.ts+++ b/apps/desktop/src/api/types.ts@@ -49,6 +49,16 @@ export interface ChatMessage {content: string;timestamp: string;steps?: ToolStep[];+ /** Files sent with this message. `url` is an API path (auth required); `src` is a local preview. */+ attachments?: MessageAttachment[];+}++export interface MessageAttachment {+ name: string;+ kind: "image" | "document" | string;+ mime?: string;+ url?: string;+ src?: string;}export type ReasoningLevel = "off" | "low" | "medium" | "high";diff --git a/apps/desktop/src/app/events.ts b/apps/desktop/src/app/events.tsindex 5240bcf0..0e2d6e6d 100644--- a/apps/desktop/src/app/events.ts+++ b/apps/desktop/src/app/events.ts@@ -157,7 +157,7 @@ export async function openThread(id: string | null): Promise<void> {}}-export async function sendMessage(content: string, attachments: Array<{ name: string; data: string }> = []): Promise<void> {+export async function sendMessage(content: string, attachments: Array<{ name: string; data: string; mime?: string }> = []): Promise<void> {const chat = useChat.getState();const rt = useRuntimeState.getState();let id = chat.activeId;@@ -168,10 +168,18 @@ export async function sendMessage(content: string, attachments: Array<{ name: stchat.setMessages([]);}const shown = content || `(Attached: ${attachments.map((a) => a.name).join(", ")})`;- const optimistic: ChatMessage = { id: crypto.randomUUID(), role: "user", content: shown, timestamp: new Date().toISOString() };+ const optimistic: ChatMessage = {+ id: crypto.randomUUID(),+ role: "user",+ content: shown,+ timestamp: new Date().toISOString(),+ ...(attachments.length+ ? { attachments: attachments.map((a) => ({ name: a.name, kind: a.mime?.startsWith("image/") ? "image" : "document", mime: a.mime, src: a.mime?.startsWith("image/") ? `data:${a.mime};base64,${a.data}` : undefined })) }+ : {}),+ };useChat.getState().addMessage(optimistic);rt.setAgent("thinking");- await api.chat.send(content, id, attachments);+ await api.chat.send(content, id, attachments.map(({ name, data }) => ({ name, data })));void refreshThreads();}diff --git a/apps/desktop/src/components/Attachments.tsx b/apps/desktop/src/components/Attachments.tsxnew file mode 100644index 00000000..579931ed--- /dev/null+++ b/apps/desktop/src/components/Attachments.tsx@@ -0,0 +1,80 @@+import { FileText } from "lucide-react";+import { useEffect, useState } from "react";+import * as RadixDialog from "@radix-ui/react-dialog";+import { apiUrl, authHeaders } from "~/api/client";+import type { MessageAttachment } from "~/api/types";+import { cx } from "~/lib/cx";++/**+ * Files on a message. Images are thumbnails that open full size; PDFs are a+ * file chip. Stored files come from the runtime with the Bearer token (an+ * <img> cannot send one), so they are fetched once into blob URLs.+ */++const blobs = new Map<string, Promise<string | null>>();++function useAuthedSrc(att: MessageAttachment): string | null {+ const [src, setSrc] = useState<string | null>(att.src ?? null);+ useEffect(() => {+ if (att.src || !att.url) return;+ let live = true;+ let hit = blobs.get(att.url);+ if (!hit) {+ hit = fetch(apiUrl(att.url), { headers: authHeaders() })+ .then(async (r) => (r.ok ? URL.createObjectURL(await r.blob()) : null))+ .catch(() => null);+ blobs.set(att.url, hit);+ }+ void hit.then((u) => { if (live) setSrc(u); });+ return () => { live = false; };+ }, [att.src, att.url]);+ return src;+}++export function Attachments({ items, align = "start" }: { items: MessageAttachment[]; align?: "start" | "end" }) {+ const images = items.filter((a) => a.kind === "image");+ const docs = items.filter((a) => a.kind !== "image");+ return (+ <div className={cx("flex flex-col gap-2", align === "end" && "items-end")}>+ {images.length > 0 && (+ <div className={cx("grid gap-2", images.length === 1 ? "grid-cols-1" : "grid-cols-2")} style={{ maxWidth: images.length === 1 ? 360 : 440 }}>+ {images.map((a, i) => <ImageThumb key={`${a.url ?? a.name}-${i}`} att={a} single={images.length === 1} />)}+ </div>+ )}+ {docs.map((a, i) => (+ <span key={`${a.url ?? a.name}-${i}`} className="inline-flex max-w-[320px] items-center gap-2.5 rounded-lg border border-line bg-surface px-2.5 py-2">+ <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-red/10 text-red"><FileText size={17} /></span>+ <span className="min-w-0">+ <span className="block truncate text-sm text-ink">{a.name}</span>+ <span className="block text-2xs uppercase text-ink4">{a.mime === "application/pdf" || a.name.toLowerCase().endsWith(".pdf") ? "PDF" : "File"}</span>+ </span>+ </span>+ ))}+ </div>+ );+}++function ImageThumb({ att, single }: { att: MessageAttachment; single: boolean }) {+ const src = useAuthedSrc(att);+ const [open, setOpen] = useState(false);+ return (+ <RadixDialog.Root open={open} onOpenChange={setOpen}>+ <RadixDialog.Trigger asChild>+ <button className={cx("group relative overflow-hidden rounded-xl border border-line bg-raised", single ? "max-h-[320px]" : "aspect-square")} aria-label={`Open ${att.name}`}>+ {src ? (+ <img src={src} alt={att.name} className={cx("block h-full w-full transition-transform duration-200 group-hover:scale-[1.02]", single ? "max-h-[320px] object-contain" : "object-cover")} draggable={false} />+ ) : (+ <span className="flex h-40 w-full items-center justify-center text-xs text-ink4">{att.url ? "Loading…" : att.name}</span>+ )}+ </button>+ </RadixDialog.Trigger>+ <RadixDialog.Portal>+ <RadixDialog.Overlay className="fixed inset-0 z-40 bg-black/80 animate-fade-in" />+ <RadixDialog.Content className="fixed inset-0 z-50 flex items-center justify-center p-10 outline-none animate-fade-in" onClick={() => setOpen(false)}>+ <RadixDialog.Title className="sr-only">{att.name}</RadixDialog.Title>+ {src && <img src={src} alt={att.name} className="max-h-full max-w-full rounded-lg object-contain shadow-2xl" />}+ </RadixDialog.Content>+ </RadixDialog.Portal>+ </RadixDialog.Root>+ );+}diff --git a/apps/desktop/src/views/chat/Composer.tsx b/apps/desktop/src/views/chat/Composer.tsxindex b58f3bbe..a4e1ae08 100644--- a/apps/desktop/src/views/chat/Composer.tsx+++ b/apps/desktop/src/views/chat/Composer.tsx@@ -18,6 +18,7 @@ import { Tooltip } from "~/components/ui/Tooltip";interface Pending {id: string;name: string;+ mime: string;size: number;data: string;preview: string | null;@@ -67,7 +68,7 @@ export function Composer({ mode }: { mode: "chat" | "code" }) {const limit = isImage ? MAX_IMAGE_BYTES : MAX_PDF_BYTES;if (f.size > limit) { setError(`${f.name} is ${Math.round(f.size / 1048576)} MB; the limit is ${limit / 1048576} MB.`); continue; }const data = await readBase64(f);- accepted.push({ id: crypto.randomUUID(), name: f.name, size: f.size, data, preview: isImage ? URL.createObjectURL(f) : null });+ accepted.push({ id: crypto.randomUUID(), name: f.name, mime: isImage ? f.type || "image/png" : "application/pdf", size: f.size, data, preview: isImage ? URL.createObjectURL(f) : null });}if (accepted.length) setFiles((cur) => [...cur, ...accepted]);ref.current?.focus();@@ -87,7 +88,7 @@ export function Composer({ mode }: { mode: "chat" | "code" }) {setSending(true);setError(null);try {- await sendMessage(content, files.map((f) => ({ name: f.name, data: f.data })));+ await sendMessage(content, files.map((f) => ({ name: f.name, data: f.data, mime: f.mime })));setText("");for (const f of files) if (f.preview) URL.revokeObjectURL(f.preview);setFiles([]);diff --git a/apps/desktop/src/views/chat/Message.tsx b/apps/desktop/src/views/chat/Message.tsxindex ca3c33a3..6f338ca2 100644--- a/apps/desktop/src/views/chat/Message.tsx+++ b/apps/desktop/src/views/chat/Message.tsx@@ -1,10 +1,11 @@-import { ChevronRight, Wrench } from "lucide-react";+import { Check, ChevronRight, Copy, Wrench } from "lucide-react";import { memo, useState } from "react";import type { ChatMessage } from "~/api/types";import { Mark } from "~/components/Logo";import { Markdown } from "~/components/Markdown";import { StreamCaret } from "~/components/indicators/Thinking";import { ActionResult } from "~/components/indicators/Activity";+import { Attachments } from "~/components/Attachments";import { cx } from "~/lib/cx";/**@@ -17,9 +18,14 @@ import { cx } from "~/lib/cx";*/export const Message = memo(function Message({ message }: { message: ChatMessage }) {if (message.role === "user") {+ const files = message.attachments ?? [];+ // "(Attached: a.png)" is the runtime's text for a files-only turn; the files say it better.+ const text = files.length > 0 && /^\(Attached: .*\)$/.test(message.content.trim()) ? "" : message.content;return (- <div className="pl-10 pt-2">- <p className="whitespace-pre-wrap text-lg font-medium leading-[1.55] tracking-[-0.01em] text-ink" data-selectable>{message.content}</p>+ <div className="group flex flex-col gap-3 pl-10 pt-2">+ {files.length > 0 && <Attachments items={files} />}+ {text && <p className="whitespace-pre-wrap text-lg font-medium leading-[1.55] tracking-[-0.01em] text-ink" data-selectable>{text}</p>}+ <MessageMeta timestamp={message.timestamp} /></div>);}@@ -34,7 +40,7 @@ export const Message = memo(function Message({ message }: { message: ChatMessage);}return (- <div className="flex gap-4">+ <div className="group flex gap-4"><span className="mt-1 flex h-6 w-6 shrink-0 items-center justify-center" aria-hidden="true"><Mark size={22} /></span><div className="min-w-0 flex-1">{message.steps && message.steps.length > 0 && (@@ -43,11 +49,41 @@ export const Message = memo(function Message({ message }: { message: ChatMessage</div>)}<Markdown text={message.content} />+ <MessageMeta timestamp={message.timestamp} copy={message.content} /></div></div>);});+/** Time and copy, shown on hover so the conversation stays quiet. */+function MessageMeta({ timestamp, copy }: { timestamp: string; copy?: string }) {+ const [copied, setCopied] = useState(false);+ const time = formatTime(timestamp);+ return (+ <div className="-mb-2 mt-2 flex h-6 items-center gap-1 opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100">+ {copy && (+ <button+ onClick={() => { void navigator.clipboard.writeText(copy).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1200); }); }}+ className="flex h-6 items-center gap-1 rounded-md px-1.5 text-xs text-ink4 hover:bg-ink/[0.06] hover:text-ink"+ aria-label="Copy answer"+ >+ {copied ? <Check size={13} className="text-accent" /> : <Copy size={13} />}+ {copied ? "Copied" : "Copy"}+ </button>+ )}+ {time && <span className="px-1.5 text-xs text-ink4">{time}</span>}+ </div>+ );+}++function formatTime(iso: string): string {+ const d = new Date(iso);+ if (Number.isNaN(d.getTime())) return "";+ const today = new Date();+ const sameDay = d.toDateString() === today.toDateString();+ return sameDay ? d.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" }) : d.toLocaleString([], { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" });+}+interface ToolLine {tool: string;args: string;diff --git a/src/core/agent.ts b/src/core/agent.tsindex 32a5fa7e..58af55f0 100644--- a/src/core/agent.ts+++ b/src/core/agent.ts@@ -98,6 +98,7 @@ import { buildFileChangePreview } from '../utils/file-preview.js';import { whatsNewText } from '../utils/whats-new.js';import { handleBotsCommand as handleBotsCommandImpl } from './commands/bots-command.js';import { modelSeesImages, withAttachments } from './attachments.js';+import { attachmentRefs } from '../web/api/uploads.js';import { AlertGate } from './alert-gate.js';import { setPluginImagesAllowed } from '../plugins/tool-bridge.js';import { handleCallCommand } from '../plugins/call-schedule.js';@@ -2725,7 +2726,14 @@ export class Agent {content: msg.content,timestamp: msg.timestamp,externalMessageId: msg.id,- metadata: { channelType: msg.channelType, channelId: msg.channelId, workCwd: this.capabilities.getCwd(), ...(requestId ? { requestId } : {}) },+ metadata: {+ channelType: msg.channelType,+ channelId: msg.channelId,+ workCwd: this.capabilities.getCwd(),+ ...(requestId ? { requestId } : {}),+ // Files the owner sent with this turn, as refs a client can fetch back.+ ...(() => { const refs = attachmentRefs(msg.metadata?.attachments); return refs.length ? { attachments: refs } : {}; })(),+ },});// One read of the session per turn (it used to be re-read three times).const activeSession = this.sessions.get(canonicalSession.id);diff --git a/src/web/api/chat.ts b/src/web/api/chat.tsindex 0bd79213..0352df5c 100644--- a/src/web/api/chat.ts+++ b/src/web/api/chat.ts@@ -1,5 +1,5 @@import { Hono } from 'hono';-import { storeInlineAttachments } from './uploads.js';+import { storeInlineAttachments, attachmentRefs, resolveUploadRef, uploadMime } from './uploads.js';import type { WebChannel } from '../../channels/web.js';import type { ProgrammingMode, ProgrammingModeState } from '../../core/programming-mode.js';import { existsSync, readFileSync, writeFileSync, readdirSync, statSync, rmSync } from 'node:fs';@@ -43,6 +43,9 @@ function toThread(session: ReturnType<SessionRepository['get']>, includeMessagestimestamp: new Date(message.timestamp).toISOString(),kind: message.kind,sequence: message.sequence,+ ...(Array.isArray(message.metadata?.attachments) && message.metadata.attachments.length > 0+ ? { attachments: (message.metadata.attachments as Array<{ name: string; kind: string; mime?: string; ref: string }>).map((a) => ({ name: a.name, kind: a.kind, mime: a.mime, url: `/api/chat/uploads/${a.ref}` })) }+ : {}),})) : [],};}@@ -194,7 +197,7 @@ chat.post('/api/chat/send', async (c) => {return c.json({sent: true,sessionId: session.id,- ...(upload.attachments.length > 0 ? { attachments: upload.attachments.map(a => ({ name: a.name, kind: a.kind })) } : {}),+ ...(upload.attachments.length > 0 ? { attachments: attachmentRefs(upload.attachments).map((a) => ({ name: a.name, kind: a.kind, mime: a.mime, url: `/api/chat/uploads/${a.ref}` })) } : {}),});} catch (err: any) {// The message never reached the runtime: don't leave its files behind.@@ -203,6 +206,25 @@ chat.post('/api/chat/send', async (c) => {}});+// ─── Uploads (files the owner sent, served back to the owner) ──++chat.get('/api/chat/uploads/:month/:file', (c) => {+ const path = resolveUploadRef(c.req.param('month'), c.req.param('file'));+ if (!path) return c.json({ error: 'Not found' }, 404);+ const bytes = readFileSync(path);+ const mime = uploadMime(bytes);+ if (!mime) return c.json({ error: 'Not found' }, 404);+ return new Response(bytes, {+ headers: {+ 'Content-Type': mime,+ 'Content-Length': String(bytes.length),+ 'X-Content-Type-Options': 'nosniff',+ 'Cache-Control': 'private, max-age=31536000, immutable',+ 'Content-Disposition': `inline; filename="${c.req.param('file').slice(37).replace(/["\\\r\n]/g, '_')}"`,+ },+ });+});+// ─── Settings ────────────────────────────────────────────────chat.get('/api/chat/settings', async (c) => {diff --git a/src/web/api/uploads-serve.test.ts b/src/web/api/uploads-serve.test.tsnew file mode 100644index 00000000..de752870--- /dev/null+++ b/src/web/api/uploads-serve.test.ts@@ -0,0 +1,61 @@+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { mkdtempSync, rmSync, writeFileSync, mkdirSync, symlinkSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { storeInlineAttachments, attachmentRefs, resolveUploadRef, uploadRef } from './uploads.js';++// 1×1 PNG.+const PNG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';++/**+ * Sent files are recorded on their message as refs and served back through+ * /api/chat/uploads/:month/:file. A ref must never reach anything but a+ * stored upload.+ */+describe('stored uploads', () => {+ let home: string;+ const previous = process.env.MERCURY_HOME;+ beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'mercury-uploads-')); process.env.MERCURY_HOME = home; });+ afterEach(() => { if (previous === undefined) delete process.env.MERCURY_HOME; else process.env.MERCURY_HOME = previous; rmSync(home, { recursive: true, force: true }); });++ it('round-trips: stored file → ref → resolved path', () => {+ const upload = storeInlineAttachments([{ name: 'cat.png', data: PNG }], new Date('2026-10-26T00:00:00Z'));+ expect(upload.ok).toBe(true);+ const refs = attachmentRefs(upload.attachments);+ expect(refs).toHaveLength(1);+ expect(refs[0].ref).toMatch(/^2026-10\/[0-9a-f-]{36}-cat\.png$/);+ expect(refs[0].kind).toBe('image');+ const [month, file] = refs[0].ref.split('/');+ expect(resolveUploadRef(month, file)).toBeTruthy();+ });++ it('refuses paths outside the uploads folder, traversal and odd names', () => {+ expect(uploadRef('/etc/passwd')).toBeNull();+ expect(resolveUploadRef('..', 'x')).toBeNull();+ expect(resolveUploadRef('2026-10', '../../mercury.yaml')).toBeNull();+ expect(resolveUploadRef('2026-10', 'mercury.yaml')).toBeNull();+ expect(attachmentRefs([{ kind: 'image', path: join(home, 'elsewhere.png') }])).toEqual([]);+ });++ it('refuses a symlink planted in the month folder', () => {+ const dir = join(home, 'uploads', '2026-10');+ mkdirSync(dir, { recursive: true });+ const secret = join(home, 'secret.txt');+ writeFileSync(secret, 'nope');+ const name = '00000000-0000-0000-0000-000000000000-x.png';+ symlinkSync(secret, join(dir, name));+ expect(resolveUploadRef('2026-10', name)).toBeNull();+ });++ it('serves the bytes with their real type, and 404s anything else', async () => {+ const { default: chatRoutes } = await import('./chat.js');+ const upload = storeInlineAttachments([{ name: 'cat.png', data: PNG }]);+ const [ref] = attachmentRefs(upload.attachments);+ const ok = await chatRoutes.request(`http://127.0.0.1:6174/api/chat/uploads/${ref.ref}`);+ expect(ok.status).toBe(200);+ expect(ok.headers.get('Content-Type')).toBe('image/png');+ expect(ok.headers.get('X-Content-Type-Options')).toBe('nosniff');+ const missing = await chatRoutes.request('http://127.0.0.1:6174/api/chat/uploads/2026-10/00000000-0000-0000-0000-000000000000-none.png');+ expect(missing.status).toBe(404);+ });+});diff --git a/src/web/api/uploads.ts b/src/web/api/uploads.tsindex b62dbb92..22c40fe8 100644--- a/src/web/api/uploads.ts+++ b/src/web/api/uploads.ts@@ -10,9 +10,9 @@* Only bytes are accepted, never a server path: a client must not be able to make* Mercury read a file it could not otherwise reach.*/-import { mkdirSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, realpathSync, writeFileSync } from 'node:fs';import { randomUUID } from 'node:crypto';-import { extname, join } from 'node:path';+import { basename, dirname, extname, join, sep } from 'node:path';import { getMercuryHome } from '../../utils/config.js';import type { ChannelAttachment } from '../../types/channel.js';@@ -151,3 +151,65 @@ export function storeInlineAttachments(input: unknown, now: Date = new Date()):}),};}+++// ── Serving stored uploads back to the owner ─────────────────────────────+//+// A sent file is recorded on its message as a REF: "<yyyy-mm>/<file>",+// relative to the uploads root, never an absolute path. Clients fetch it+// through GET /api/chat/uploads/:month/:file (authenticated like every API+// route). Both parts are validated by shape and the resolved file must sit+// directly in that month's folder, so a ref can never reach anything else.++const MONTH_RE = /^\d{4}-\d{2}$/;+const FILE_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-[^/\\]{1,200}$/i;++export interface AttachmentRef {+ name: string;+ kind: 'image' | 'document';+ mime?: string;+ ref: string;+}++/** The ref of a stored upload path, or null when the path is not one. */+export function uploadRef(path: string): string | null {+ const file = basename(path);+ const month = basename(dirname(path));+ if (!MONTH_RE.test(month) || !FILE_RE.test(file)) return null;+ if (dirname(dirname(path)) !== join(getMercuryHome(), 'uploads')) return null;+ return `${month}/${file}`;+}++/** Message metadata for the attachments of one turn (only real uploads). */+export function attachmentRefs(attachments: unknown): AttachmentRef[] {+ if (!Array.isArray(attachments)) return [];+ const out: AttachmentRef[] = [];+ for (const a of attachments as ChannelAttachment[]) {+ if (!a || typeof a.path !== 'string') continue;+ const ref = uploadRef(a.path);+ if (!ref) continue;+ out.push({ name: a.name ?? basename(a.path), kind: a.kind, ...(a.mime ? { mime: a.mime } : {}), ref });+ }+ return out;+}++/** Resolve a ref from a request to the file on disk, or null. */+export function resolveUploadRef(month: string, file: string): string | null {+ if (!MONTH_RE.test(month) || !FILE_RE.test(file)) return null;+ const root = join(getMercuryHome(), 'uploads', month);+ const path = join(root, file);+ if (!existsSync(path)) return null;+ try {+ const real = realpathSync(path);+ const realRoot = realpathSync(root);+ if (dirname(real) !== realRoot || real.includes(`..${sep}`)) return null;+ return real;+ } catch {+ return null;+ }+}++/** The served type of a stored upload, from its bytes; null for anything else. */+export function uploadMime(bytes: Buffer): string | null {+ return sniff(bytes)?.mime ?? null;+}diff --git a/vitest.config.ts b/vitest.config.tsindex aa77fa0b..c8276d92 100644--- a/vitest.config.ts+++ b/vitest.config.ts@@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url';// ADR-017: tests run against the same vendored, patched ink the bundle// ships (vendor/ink), never the stock copy in node_modules.export default defineConfig({+ // apps/* run their own test config (their own aliases and environment).+ test: { exclude: ['**/node_modules/**', '**/dist/**', 'apps/**'] },resolve: {alias: [{ find: /^ink$/, replacement: fileURLToPath(new URL('./vendor/ink/build/index.js', import.meta.url)) },