( content: string | null | undefined, files: UserFile[] | undefined, providerId: ProviderId | string )
| 466 | } |
| 467 | |
| 468 | export function buildAnthropicMessageContent( |
| 469 | content: string | null | undefined, |
| 470 | files: UserFile[] | undefined, |
| 471 | providerId: ProviderId | string |
| 472 | ): Anthropic.Messages.ContentBlockParam[] { |
| 473 | const parts: Anthropic.Messages.ContentBlockParam[] = [] |
| 474 | if (content) { |
| 475 | parts.push({ type: 'text', text: content } satisfies Anthropic.Messages.TextBlockParam) |
| 476 | } |
| 477 | |
| 478 | for (const attachment of prepareProviderAttachments(files, providerId)) { |
| 479 | if (attachment.contentType === 'image') { |
| 480 | parts.push({ |
| 481 | type: 'image', |
| 482 | source: attachment.remoteUrl |
| 483 | ? ({ type: 'url', url: attachment.remoteUrl } satisfies Anthropic.Messages.URLImageSource) |
| 484 | : ({ |
| 485 | type: 'base64', |
| 486 | media_type: attachment.providerMimeType as AnthropicImageMediaType, |
| 487 | data: attachment.base64 ?? '', |
| 488 | } satisfies Anthropic.Messages.Base64ImageSource), |
| 489 | } satisfies Anthropic.Messages.ImageBlockParam) |
| 490 | } else if (attachment.remoteUrl) { |
| 491 | if (attachment.mimeType !== PDF_MIME_TYPE) { |
| 492 | throw new Error( |
| 493 | `Document "${attachment.filename}" (${attachment.mimeType}) is too large to send to provider "${providerId}". Only PDFs and images are supported above the inline limit — convert it to PDF or reduce its size.` |
| 494 | ) |
| 495 | } |
| 496 | parts.push({ |
| 497 | type: 'document', |
| 498 | source: { type: 'url', url: attachment.remoteUrl }, |
| 499 | title: attachment.filename, |
| 500 | } satisfies Anthropic.Messages.DocumentBlockParam) |
| 501 | } else if (attachment.text) { |
| 502 | parts.push({ |
| 503 | type: 'document', |
| 504 | source: { |
| 505 | type: 'text', |
| 506 | media_type: 'text/plain', |
| 507 | data: attachment.text, |
| 508 | }, |
| 509 | title: attachment.filename, |
| 510 | } satisfies Anthropic.Messages.DocumentBlockParam) |
| 511 | } else { |
| 512 | parts.push({ |
| 513 | type: 'document', |
| 514 | source: { |
| 515 | type: 'base64', |
| 516 | media_type: 'application/pdf', |
| 517 | data: attachment.base64 ?? '', |
| 518 | }, |
| 519 | title: attachment.filename, |
| 520 | } satisfies Anthropic.Messages.DocumentBlockParam) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return parts |
| 525 | } |
no test coverage detected