(
containerId: string,
fileId: string,
filename: string,
modelNodeData: INodeData,
options: ICommonObject
)
| 516 | |
| 517 | /** Downloads a file from an OpenAI container (used for file citations in responses). */ |
| 518 | export const downloadContainerFile = async ( |
| 519 | containerId: string, |
| 520 | fileId: string, |
| 521 | filename: string, |
| 522 | modelNodeData: INodeData, |
| 523 | options: ICommonObject |
| 524 | ): Promise<{ filePath: string; totalSize: number } | null> => { |
| 525 | try { |
| 526 | const credentialData = await getCredentialData(modelNodeData.credential ?? '', options) |
| 527 | const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, modelNodeData) |
| 528 | |
| 529 | if (!openAIApiKey) { |
| 530 | console.warn('No OpenAI API key available for downloading container file') |
| 531 | return null |
| 532 | } |
| 533 | |
| 534 | const response = await fetch(`https://api.openai.com/v1/containers/${containerId}/files/${fileId}/content`, { |
| 535 | method: 'GET', |
| 536 | headers: { |
| 537 | Accept: '*/*', |
| 538 | Authorization: `Bearer ${openAIApiKey}` |
| 539 | } |
| 540 | }) |
| 541 | |
| 542 | if (!response.ok) { |
| 543 | console.warn( |
| 544 | `Failed to download container file ${fileId} from container ${containerId}: ${response.status} ${response.statusText}` |
| 545 | ) |
| 546 | return null |
| 547 | } |
| 548 | |
| 549 | const data = await response.arrayBuffer() |
| 550 | const dataBuffer = Buffer.from(data) |
| 551 | const mimeType = getMimeTypeFromFilename(filename) |
| 552 | |
| 553 | const { path, totalSize } = await addSingleFileToStorage( |
| 554 | mimeType, |
| 555 | dataBuffer, |
| 556 | filename, |
| 557 | options.orgId, |
| 558 | options.chatflowid, |
| 559 | options.chatId |
| 560 | ) |
| 561 | |
| 562 | return { filePath: path, totalSize } |
| 563 | } catch (error) { |
| 564 | console.error('Error downloading container file:', error) |
| 565 | return null |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // ─── Replacing inline image data with file references in responses ─────────── |
| 570 |
no test coverage detected