|
1 | 1 | import { createLogger } from '@sim/logger' |
2 | 2 | import { getUserSubscriptionState } from '@/lib/billing/core/subscription' |
3 | | -import { processFileAttachments } from '@/lib/copilot/chat-context' |
4 | 3 | import { getCopilotToolDescription } from '@/lib/copilot/tool-descriptions' |
5 | 4 | import { isHosted } from '@/lib/core/config/feature-flags' |
6 | 5 | import { createMcpToolId } from '@/lib/mcp/utils' |
| 6 | +import { trackChatUpload } from '@/lib/uploads/contexts/workspace/workspace-file-manager' |
7 | 7 | import { getWorkflowById } from '@/lib/workflows/utils' |
8 | 8 | import { tools } from '@/tools/registry' |
9 | 9 | import { getLatestVersionTools, stripVersionSuffix } from '@/tools/utils' |
@@ -126,7 +126,47 @@ export async function buildCopilotRequestPayload( |
126 | 126 | const effectiveMode = mode === 'agent' ? 'build' : mode |
127 | 127 | const transportMode = effectiveMode === 'build' ? 'agent' : effectiveMode |
128 | 128 |
|
129 | | - const processedFileContents = await processFileAttachments(fileAttachments ?? [], userId) |
| 129 | + // Track uploaded files in the DB and build context tags instead of base64 inlining |
| 130 | + const uploadContexts: Array<{ type: string; content: string }> = [] |
| 131 | + if (chatId && params.workspaceId && fileAttachments && fileAttachments.length > 0) { |
| 132 | + for (const f of fileAttachments) { |
| 133 | + const filename = (f.filename ?? f.name ?? 'file') as string |
| 134 | + const mediaType = (f.media_type ?? f.mimeType ?? 'application/octet-stream') as string |
| 135 | + try { |
| 136 | + await trackChatUpload( |
| 137 | + params.workspaceId, |
| 138 | + userId, |
| 139 | + chatId, |
| 140 | + f.key, |
| 141 | + filename, |
| 142 | + mediaType, |
| 143 | + f.size |
| 144 | + ) |
| 145 | + const lines = [ |
| 146 | + `File "${filename}" (${mediaType}, ${f.size} bytes) uploaded.`, |
| 147 | + `Read with: read("uploads/${filename}")`, |
| 148 | + `To save permanently: materialize_file(fileName: "${filename}")`, |
| 149 | + ] |
| 150 | + if (filename.endsWith('.json')) { |
| 151 | + lines.push( |
| 152 | + `To import as a workflow: materialize_file(fileName: "${filename}", operation: "import")` |
| 153 | + ) |
| 154 | + } |
| 155 | + uploadContexts.push({ |
| 156 | + type: 'uploaded_file', |
| 157 | + content: lines.join('\n'), |
| 158 | + }) |
| 159 | + } catch (err) { |
| 160 | + logger.warn('Failed to track chat upload', { |
| 161 | + filename, |
| 162 | + chatId, |
| 163 | + error: err instanceof Error ? err.message : String(err), |
| 164 | + }) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + const allContexts = [...(contexts ?? []), ...uploadContexts] |
130 | 170 |
|
131 | 171 | let integrationTools: ToolSchema[] = [] |
132 | 172 |
|
@@ -170,11 +210,10 @@ export async function buildCopilotRequestPayload( |
170 | 210 | ...(provider ? { provider } : {}), |
171 | 211 | mode: transportMode, |
172 | 212 | messageId: userMessageId, |
173 | | - ...(contexts && contexts.length > 0 ? { context: contexts } : {}), |
| 213 | + ...(allContexts.length > 0 ? { context: allContexts } : {}), |
174 | 214 | ...(chatId ? { chatId } : {}), |
175 | 215 | ...(typeof prefetch === 'boolean' ? { prefetch } : {}), |
176 | 216 | ...(implicitFeedback ? { implicitFeedback } : {}), |
177 | | - ...(processedFileContents.length > 0 ? { fileAttachments: processedFileContents } : {}), |
178 | 217 | ...(integrationTools.length > 0 ? { integrationTools } : {}), |
179 | 218 | ...(commands && commands.length > 0 ? { commands } : {}), |
180 | 219 | ...(params.workspaceContext ? { workspaceContext: params.workspaceContext } : {}), |
|
0 commit comments