From 6551f49d6777a0b7c1fa8c9088efeb1c7df65e92 Mon Sep 17 00:00:00 2001 From: Siddharth Ganesan Date: Tue, 17 Mar 2026 15:50:24 -0700 Subject: [PATCH 1/3] Fix files --- apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts index 299c8f0f85..74312caab0 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts @@ -132,7 +132,7 @@ function toDisplayAttachment(f: TaskStoredFileAttachment): ChatMessageAttachment media_type: f.media_type, size: f.size, previewUrl: f.media_type.startsWith('image/') - ? `/api/files/serve/${encodeURIComponent(f.key)}?context=copilot` + ? `/api/files/serve/${encodeURIComponent(f.key)}?context=mothership` : undefined, } } From 356b47d7ead7c729e74afd08ab83c4adb1c03981 Mon Sep 17 00:00:00 2001 From: Siddharth Ganesan Date: Tue, 17 Mar 2026 15:50:44 -0700 Subject: [PATCH 2/3] Fix --- .../contexts/workspace/workspace-file-manager.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts index 9c3dd16ec0..404047d80d 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts @@ -209,8 +209,9 @@ export async function uploadWorkspaceFile( /** * Track a file that was already uploaded to workspace S3 as a chat-scoped upload. - * Creates a workspaceFiles record with context='mothership' and the given chatId. - * No S3 operations -- the file is already in storage from the presigned/upload step. + * Links the existing workspaceFiles metadata record (created by the storage service + * during upload) to the chat by setting chatId and context='mothership'. + * Falls back to inserting a new record if none exists for the key. */ export async function trackChatUpload( workspaceId: string, @@ -221,6 +222,17 @@ export async function trackChatUpload( contentType: string, size: number ): Promise { + const updated = await db + .update(workspaceFiles) + .set({ chatId, context: 'mothership' }) + .where(and(eq(workspaceFiles.key, s3Key), isNull(workspaceFiles.deletedAt))) + .returning({ id: workspaceFiles.id }) + + if (updated.length > 0) { + logger.info(`Linked existing file record to chat: ${fileName} for chat ${chatId}`) + return + } + const fileId = `wf_${Date.now()}_${Math.random().toString(36).substring(2, 9)}` await db.insert(workspaceFiles).values({ From 7e10016a86a21d2f023e3159911c2b84dd165144 Mon Sep 17 00:00:00 2001 From: Siddharth Ganesan Date: Tue, 17 Mar 2026 16:11:14 -0700 Subject: [PATCH 3/3] Fix --- .../lib/uploads/contexts/workspace/workspace-file-manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts index 404047d80d..4d18638d3f 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts @@ -225,7 +225,7 @@ export async function trackChatUpload( const updated = await db .update(workspaceFiles) .set({ chatId, context: 'mothership' }) - .where(and(eq(workspaceFiles.key, s3Key), isNull(workspaceFiles.deletedAt))) + .where(and(eq(workspaceFiles.key, s3Key), eq(workspaceFiles.workspaceId, workspaceId), isNull(workspaceFiles.deletedAt))) .returning({ id: workspaceFiles.id }) if (updated.length > 0) {