( files: UserFile[] | undefined, providerId: ProviderId | string )
| 333 | } |
| 334 | |
| 335 | export function prepareProviderAttachments( |
| 336 | files: UserFile[] | undefined, |
| 337 | providerId: ProviderId | string |
| 338 | ): PreparedProviderAttachment[] { |
| 339 | if (!files || files.length === 0) return [] |
| 340 | |
| 341 | const provider = getAttachmentProvider(providerId) |
| 342 | if (!provider) { |
| 343 | throw new Error(`File attachments are not supported for provider "${providerId}"`) |
| 344 | } |
| 345 | |
| 346 | if (UNSUPPORTED_FILE_PROVIDERS.has(provider)) { |
| 347 | throw new Error( |
| 348 | `File attachments are not supported for provider "${providerId}" in the current adapter. Supported attachments: ${PROVIDER_SUPPORTED_LABELS[provider]}.` |
| 349 | ) |
| 350 | } |
| 351 | |
| 352 | return files.map((file) => { |
| 353 | const declaredMimeType = inferAttachmentMimeType(file) |
| 354 | const contentType = getAttachmentContentType(declaredMimeType) |
| 355 | |
| 356 | if (!contentType) { |
| 357 | throw new Error( |
| 358 | `File "${file.name}" has MIME type "${declaredMimeType}", which is not supported by provider "${providerId}". Supported attachments: ${PROVIDER_SUPPORTED_LABELS[provider]}.` |
| 359 | ) |
| 360 | } |
| 361 | |
| 362 | const maxBytes = getProviderAttachmentMaxBytes(providerId) |
| 363 | if (Number.isFinite(file.size) && file.size > maxBytes) { |
| 364 | const sizeMB = (file.size / (1024 * 1024)).toFixed(2) |
| 365 | const maxMB = (maxBytes / (1024 * 1024)).toFixed(0) |
| 366 | throw new Error( |
| 367 | `File "${file.name}" (${sizeMB}MB) exceeds the ${maxMB}MB agent attachment limit for provider "${providerId}"` |
| 368 | ) |
| 369 | } |
| 370 | |
| 371 | const providerFileId = file.providerFileId |
| 372 | const providerFileUri = file.providerFileUri |
| 373 | const remoteUrl = file.remoteUrl |
| 374 | const hasHandle = Boolean(providerFileId || providerFileUri || remoteUrl) |
| 375 | |
| 376 | if (!file.base64 && !hasHandle) { |
| 377 | throw new Error(`File "${file.name}" could not be read for provider "${providerId}"`) |
| 378 | } |
| 379 | |
| 380 | const sniffedImageMimeType = |
| 381 | contentType === 'image' && file.base64 ? sniffImageMimeType(file.base64) : '' |
| 382 | if (contentType === 'image' && file.base64 && !sniffedImageMimeType) { |
| 383 | throw new Error( |
| 384 | `Image bytes in "${file.name}" are not a supported model image format (declared MIME type "${declaredMimeType}"). Supported image formats: image/jpeg, image/png, image/gif, image/webp.` |
| 385 | ) |
| 386 | } |
| 387 | |
| 388 | const mimeType = sniffedImageMimeType || declaredMimeType |
| 389 | const extension = getAttachmentExtension(file, mimeType) |
| 390 | const attachment = { |
| 391 | file, |
| 392 | filename: file.name, |
no test coverage detected