feat(assistant): an attached file stays with its conversation

A file attached in the trip chat lived only as a buffer for the one request
that read it, so the assistant could not look at it again and nothing could
keep it in Files. Now its bytes go to storage before the turn writes anything
(under the trip files' category, which is never served by path) and a row in
the new assistant_attachments table keeps its name, type, size, hash and the
text TREK read out of it, written in the turn's opening transaction. The
message's attachment part carries the row's id; old messages without one still
read.

The text is read the way the AI import reads it, through a new public
LlmParseService.readText, and a file whose text cannot be read is kept all the
same. A storage that refuses the bytes refuses the turn before any row exists.
Deleting a thread takes its files along, and the nightly retention sweep drops
files older than seven days and bytes no row names any more.
This commit is contained in:
Maurice
2026-10-24 21:11:35 +02:00
parent 487f8dd886
commit 6a77496438
27 changed files with 1088 additions and 26 deletions
@@ -0,0 +1,12 @@
import { z } from 'zod';
/**
* A file the user attached in the trip chat, as the conversation knows it.
*
* The server keeps the file with the thread under a random id, which the
* message's `attachment` part carries. The id never reaches anything but the
* thread it was attached in: every read answers an id of another thread or
* another user the way it answers one that does not exist.
*/
export const assistantAttachmentIdSchema = z.uuid();
export type AssistantAttachmentId = z.infer<typeof assistantAttachmentIdSchema>;
@@ -180,6 +180,15 @@ describe('assistantMessageSchema', () => {
const negative = { ...attachment, size: -1 };
expect(assistantMessageSchema.safeParse({ ...message, parts: [negative] }).success).toBe(false);
});
it('reads the id a kept attachment goes by, and only a uuid as one', () => {
const message = { id: '14', role: 'user', status: 'complete', createdAt: '2026-10-21T08:00:00.000Z' };
const attachment = { type: 'attachment', name: 'ticket.pdf', mime: 'application/pdf', size: 2048 };
const kept = { ...attachment, attachmentId: '0b7c2f0e-6f6d-4a8e-9b1a-3d2c1e0f9a87' };
expect(assistantMessageSchema.parse({ ...message, parts: [kept] }).parts[0]).toEqual(kept);
const forged = { ...attachment, attachmentId: '../other-thread' };
expect(assistantMessageSchema.safeParse({ ...message, parts: [forged] }).success).toBe(false);
});
});
describe('uiStreamChunkSchema', () => {
+4 -1
View File
@@ -1,5 +1,6 @@
import { bookElementIdSchema, bookSpreadIdSchema } from '../book/book-ops.schema';
import { idParamSchema, idSchema } from '../common/primitives.schema';
import { assistantAttachmentIdSchema } from './assistant-attachments.schema';
import { assistantPageRenderSchema, assistantPageViewSchema } from './assistant-vision.schema';
import {
assistantCardSchema,
@@ -648,12 +649,14 @@ function messagePartSchema<
>(change: Change, applied: Applied, card: Card) {
return z.discriminatedUnion('type', [
z.object({ type: z.literal('text'), text: z.string() }),
// A file the user handed the assistant. Only its description is kept, never its content.
// A file the user handed the assistant. Only its description is kept here, never its content; the
// server keeps the file under `attachmentId` (left out on messages from before it kept files).
z.object({
type: z.literal('attachment'),
name: z.string(),
mime: z.string(),
size: z.number().int().nonnegative(),
attachmentId: assistantAttachmentIdSchema.optional(),
}),
z.object({
type: z.literal('tool-call'),
+1
View File
@@ -232,6 +232,7 @@ export * from './oauth/oauth.schema';
export * from './admin/admin.schema';
export * from './ai/ai-connections.schema';
export * from './assistant/assistant.schema';
export * from './assistant/assistant-attachments.schema';
export * from './assistant/cards';
export * from './assistant/assistant-share.schema';
export * from './assistant/assistant-scope.schema';