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(attachments): preserve Unicode boundaries in filenames

Seth Wilson1103f29fd0b5
diff --git a/src/web/api/uploads-serve.test.ts b/src/web/api/uploads-serve.test.ts
index 36bbd7cc..7f4cca08 100644
--- a/src/web/api/uploads-serve.test.ts
+++ b/src/web/api/uploads-serve.test.ts
@@ -75,11 +75,11 @@ describe('stored uploads', () => {
expect(await response.text()).toBe(text);
});
- it('preserves the text extension in long filenames through storage, model input and retrieval', async () => {
+ it.each(['x'.repeat(79) + '.md', '😀'.repeat(40) + '.md'])('preserves long filenames through storage, model input and retrieval: %s', async (name) => {
const { default: chatRoutes } = await import('./chat.js');
- const upload = storeInlineAttachments([{ name: 'x'.repeat(79) + '.md', data: Buffer.from('Long name, real text').toString('base64') }]);
+ const upload = storeInlineAttachments([{ name, data: Buffer.from('Long name, real text').toString('base64') }]);
if (!upload.ok) throw new Error(upload.error);
- expect(upload.attachments[0].name).toHaveLength(80);
+ expect(upload.attachments[0].name!.length).toBeLessThanOrEqual(80);
expect(upload.attachments[0].name).toMatch(/\.md$/);
const message = { id: '1', channelId: 'web', channelType: 'web' as const, senderId: 'owner', content: 'Summarize', timestamp: 0, metadata: { attachments: upload.attachments } };
const out = withAttachments([{ role: 'user', content: 'Summarize' }], message, 'deepseek', 'deepseek-chat');
diff --git a/src/web/api/uploads.ts b/src/web/api/uploads.ts
index 80e11aa7..37c1a943 100644
--- a/src/web/api/uploads.ts
+++ b/src/web/api/uploads.ts
@@ -15,7 +15,7 @@ import { randomUUID } from 'node:crypto';
import { basename, dirname, extname, join, sep } from 'node:path';
import { getMercuryHome } from '../../utils/config.js';
import type { ChannelAttachment } from '../../types/channel.js';
-import { decodeTextAttachment, MAX_TEXT_BYTES, textAttachmentMime } from '../../core/text-attachments.js';
+import { decodeTextAttachment, MAX_TEXT_BYTES, textAttachmentMime, textPrefix } from '../../core/text-attachments.js';
/** Same caps as the runtime applies when it hands files to the model. */
export const MAX_FILES = 4;
@@ -88,7 +88,7 @@ function safeFileName(name: string | undefined, extension: string, text = false)
// and download route must still recognize this file after it is stored.
const supplied = extname(cleaned);
const suffix = /^\.(txt|md|markdown)$/i.test(supplied) ? supplied : extension;
- const stem = cleaned.slice(0, cleaned.length - supplied.length).slice(0, 80 - suffix.length) || `upload-${randomUUID().slice(0, 8)}`;
+ const stem = textPrefix(cleaned.slice(0, cleaned.length - supplied.length), 80 - suffix.length) || `upload-${randomUUID().slice(0, 8)}`;
return `${stem}${suffix}`.replace(/^\.+/, '_');
}
const shortened = cleaned.slice(0, 80);