Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(chat): name caption-less attachments; drop files of a failed send (#228)

Salman Qureshi63f0246ab062
diff --git a/src/web/api/chat-attachments-route.test.ts b/src/web/api/chat-attachments-route.test.ts
index 3d468c8e..00f837ca 100644
--- a/src/web/api/chat-attachments-route.test.ts
+++ b/src/web/api/chat-attachments-route.test.ts
@@ -68,10 +68,11 @@ describe('POST /api/chat/send with attachments', () => {
it('accepts a photo with no text at all', async () => {
const { seen } = recordingChannel();
- const res = await send({ content: ' ', attachments: [{ data: PNG_BASE64 }] });
+ const res = await send({ content: ' ', attachments: [{ name: 'dot.png', data: PNG_BASE64 }] });
expect(res.status).toBe(200);
- expect(seen[0].content).toBe('');
+ // An empty text block is rejected by models: the files are named instead.
+ expect(seen[0].content).toBe('(Attached: dot.png)');
expect((seen[0].metadata?.attachments as unknown[]).length).toBe(1);
});
@@ -105,4 +106,17 @@ describe('POST /api/chat/send with attachments', () => {
expect(seen[0].content).toBe('hello');
expect(seen[0].metadata?.attachments).toBeUndefined();
});
+
+ it('removes the stored files when the message cannot be sent', async () => {
+ const { channel } = recordingChannel();
+ channel.setRestrictUser(true);
+ const { readdirSync, existsSync } = await import('node:fs');
+ const { uploadsDir } = await import('./uploads.js');
+ const before = existsSync(uploadsDir()) ? readdirSync(uploadsDir()).length : 0;
+
+ const res = await send({ content: 'look', attachments: [{ data: PNG_BASE64 }] });
+
+ expect(res.status).toBe(500);
+ expect(readdirSync(uploadsDir()).length).toBe(before);
+ });
});
diff --git a/src/web/api/chat.ts b/src/web/api/chat.ts
index bbce7bb2..0bd79213 100644
--- a/src/web/api/chat.ts
+++ b/src/web/api/chat.ts
@@ -2,7 +2,7 @@ import { Hono } from 'hono';
import { storeInlineAttachments } 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 } from 'node:fs';
+import { existsSync, readFileSync, writeFileSync, readdirSync, statSync, rmSync } from 'node:fs';
import { join, extname, basename, relative, resolve } from 'node:path';
import { getMercuryHome, loadConfig, getActiveProviders } from '../../utils/config.js';
import type { SessionRepository } from '../../sessions/index.js';
@@ -188,13 +188,17 @@ chat.post('/api/chat/send', async (c) => {
}
}
session ??= sessions.getOrCreateBound('web', 'web:default');
- webChannel.emitMessageInThread(content, session.id, session.id, undefined, undefined, undefined, upload.attachments);
+ // Models reject an empty text block, so a photo sent alone says what it is.
+ const text = content || `(Attached: ${upload.attachments.map(a => a.name).join(', ')})`;
+ webChannel.emitMessageInThread(text, session.id, session.id, undefined, undefined, undefined, upload.attachments);
return c.json({
sent: true,
sessionId: session.id,
...(upload.attachments.length > 0 ? { attachments: upload.attachments.map(a => ({ name: a.name, kind: a.kind })) } : {}),
});
} catch (err: any) {
+ // The message never reached the runtime: don't leave its files behind.
+ for (const a of upload.attachments) rmSync(a.path, { force: true });
return c.json({ error: err.message }, 500);
}
});
diff --git a/src/web/api/uploads.ts b/src/web/api/uploads.ts
index c3ca618b..b62dbb92 100644
--- a/src/web/api/uploads.ts
+++ b/src/web/api/uploads.ts
@@ -90,7 +90,8 @@ function decode(data: string): Buffer | null {
const payload = data.startsWith('data:') ? data.slice(data.indexOf(',') + 1) : data;
if (!payload) return null;
const bytes = Buffer.from(payload, 'base64');
- // Buffer.from is forgiving: re-encoding catches truncated or mis-typed input.
+ // Buffer.from is forgiving and skips bad characters; the magic-byte check
+ // in sniff() rejects anything that did not decode to a real file.
if (bytes.length === 0) return null;
return bytes;
}