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 299c8f0f852..74312caab0b 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, } } 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 9c3dd16ec03..4d18638d3ff 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), eq(workspaceFiles.workspaceId, workspaceId), 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({