-
-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Expand file tree
/
Copy pathutils.ts
More file actions
895 lines (791 loc) · 37.7 KB
/
Copy pathutils.ts
File metadata and controls
895 lines (791 loc) · 37.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
import { BaseMessage, MessageContentImageUrl, AIMessageChunk, MessageContentComplex } from '@langchain/core/messages'
import type { ContentBlock } from 'langchain'
import { getImageUploads } from '../../src/multiModalUtils'
import { addSingleFileToStorage, getFileFromStorage } from '../../src/storageUtils'
import { ICommonObject, IFileUpload, INodeData } from '../../src/Interface'
import { BaseMessageLike } from '@langchain/core/messages'
import {
IFlowState,
IImageFileRef,
IArtifact,
IFileAnnotation,
ISavedImageResult,
ISavedInlineImage,
IResponseMetadata,
IChatMessage,
IImageArtifact,
IMultimodalContentItem,
IMessageAdditionalKwargs
} from './Interface.Agentflow'
import { getCredentialData, getCredentialParam, handleEscapeCharacters, mapMimeTypeToInputField } from '../../src/utils'
import { sanitizeFileName } from '../../src/validator'
import fetch from 'node-fetch'
// ─── Constants ───────────────────────────────────────────────────────────────
const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp']
const MIME_TYPES: Record<string, string> = {
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
pdf: 'application/pdf',
txt: 'text/plain',
csv: 'text/csv',
json: 'application/json',
html: 'text/html',
xml: 'application/xml'
}
const ARTIFACT_TYPES: Record<string, string> = {
png: 'png',
jpg: 'jpeg',
jpeg: 'jpeg',
html: 'html',
htm: 'html',
md: 'markdown',
markdown: 'markdown',
json: 'json',
js: 'javascript',
javascript: 'javascript',
tex: 'latex',
latex: 'latex',
txt: 'text',
csv: 'text',
pdf: 'text'
}
const DEFAULT_TOKEN_COUNT_TIMEOUT_MS = 1500
type TokenCountingModel = {
getNumTokens(text: string): Promise<number>
}
// ─── Shared helpers (used across multiple functions) ─────────────────────────
const isTruthyEnv = (value?: string): boolean => ['1', 'true', 'yes'].includes((value || '').toLowerCase())
const getTokenCountTimeoutMs = (): number => {
const timeout = Number(process.env.TIKTOKEN_TIMEOUT)
return Number.isFinite(timeout) && timeout > 0 ? timeout : DEFAULT_TOKEN_COUNT_TIMEOUT_MS
}
const getNumTokensWithTimeout = async (llm: TokenCountingModel, text: string, timeoutMs: number): Promise<number> => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error(`Token counting timed out after ${timeoutMs}ms`)), timeoutMs)
let tokenCountPromise: Promise<number>
try {
tokenCountPromise = Promise.resolve(llm.getNumTokens(text))
} catch (error) {
clearTimeout(timeout)
reject(error)
return
}
tokenCountPromise.then(
(count) => {
clearTimeout(timeout)
resolve(count)
},
(error) => {
clearTimeout(timeout)
reject(error)
}
)
})
}
export const getApproximateTokenCount = (text: string): number => Math.ceil((text || '').length / 4)
export const createTokenCounter = (llm?: TokenCountingModel | null): ((text: string) => Promise<number>) => {
let useApproximateCount =
isTruthyEnv(process.env.DISABLE_TIKTOKEN) ||
isTruthyEnv(process.env.USE_APPROXIMATE_TOKENS) ||
typeof llm?.getNumTokens !== 'function'
return async (text: string): Promise<number> => {
if (useApproximateCount) {
return getApproximateTokenCount(text)
}
try {
return await getNumTokensWithTimeout(llm, text, getTokenCountTimeoutMs())
} catch (error) {
useApproximateCount = true
console.warn('Failed to calculate number of tokens, falling back to approximate count', error)
return getApproximateTokenCount(text)
}
}
}
/** Reads a file from storage and returns a base64 data-URL string. */
const storedFileToBase64 = async (fileName: string, mime: string, options: ICommonObject): Promise<string> => {
const contents = await getFileFromStorage(fileName, options.orgId, options.chatflowid, options.chatId)
return 'data:' + mime + ';base64,' + contents.toString('base64')
}
/** Saves raw base64 data to storage as a file. Returns the file path, name, and size. */
const saveImageToStorage = async (
base64Data: string,
mimeType: string,
fileName: string,
options: ICommonObject
): Promise<ISavedImageResult> => {
const imageBuffer = Buffer.from(base64Data, 'base64')
const { path, totalSize } = await addSingleFileToStorage(
mimeType,
imageBuffer,
fileName,
options.orgId,
options.chatflowid,
options.chatId
)
return { filePath: path, fileName, totalSize }
}
// ─── Processing stored-file references into base64 for model invocation ──────
/**
* Converts stored-file references in user messages to base64 image_url content
* so LLM providers can process them. Returns both the updated messages and copies
* of the originals that were transformed (for later reverting).
*
* The base64 content is only needed during model invocation — after the call,
* use `revertBase64ImagesToFileRefs` to restore lightweight file references.
*/
export const processMessagesWithImages = async (
messages: BaseMessageLike[],
options: ICommonObject
): Promise<{
updatedMessages: BaseMessageLike[]
transformedMessages: BaseMessageLike[]
}> => {
if (!messages || !options.chatflowid || !options.chatId) {
return {
updatedMessages: messages,
transformedMessages: []
}
}
// Create a deep copy of the messages to avoid mutating the original
const updatedMessages: IChatMessage[] = JSON.parse(JSON.stringify(messages))
// Track which messages were transformed
const transformedMessages: BaseMessageLike[] = []
// Scan through all messages looking for stored-file references
for (let i = 0; i < updatedMessages.length; i++) {
const message = updatedMessages[i]
// Skip non-user messages or messages without content
if (message.role !== 'user' || !Array.isArray(message.content)) continue
const imageContents: MessageContentImageUrl[] = []
const fileRefs: IImageFileRef[] = []
let hasImageReferences = false
// Find stored-file image items and convert them to base64 image_url items
for (const item of message.content as IMultimodalContentItem[]) {
if (item.type === 'stored-file' && item.name && item.mime?.startsWith('image/')) {
hasImageReferences = true
try {
const fileName = sanitizeFileName(item.name)
const base64Url = await storedFileToBase64(fileName, item.mime, options)
// Track which content index maps to which file, so we can revert later
fileRefs.push({ index: imageContents.length, fileName, mime: item.mime })
imageContents.push({
type: 'image_url',
image_url: {
url: base64Url
}
})
} catch (error) {
console.error(`Failed to load image ${item.name}:`, error)
}
}
}
if (imageContents.length > 0) {
// Save a copy of the original message before we replace its content
if (hasImageReferences) {
transformedMessages.push(JSON.parse(JSON.stringify(messages[i])))
}
updatedMessages[i].content = imageContents
// Store file refs in additional_kwargs (not sent to the LLM API)
if (fileRefs.length > 0) {
if (!updatedMessages[i].additional_kwargs) updatedMessages[i].additional_kwargs = {}
updatedMessages[i].additional_kwargs!._imageFileRefs = fileRefs
}
}
}
return { updatedMessages, transformedMessages }
}
// ─── Reverting base64 back to stored-file references ─────────────────────────
/**
* After model invocation, reverts base64 image_url items back to lightweight
* stored-file references using the `_imageFileRefs` metadata in additional_kwargs.
* This keeps chat history storage efficient (no base64 blobs).
*/
export const revertBase64ImagesToFileRefs = (messages: BaseMessageLike[]): BaseMessageLike[] => {
const updatedMessages: IChatMessage[] = JSON.parse(JSON.stringify(messages))
for (const message of updatedMessages) {
const fileRefs: IImageFileRef[] = message.additional_kwargs?._imageFileRefs || []
if (message.content && Array.isArray(message.content) && fileRefs.length > 0) {
const contentArray = message.content as MessageContentComplex[]
// Replace each image_url item with its stored-file equivalent
for (const ref of fileRefs) {
const item = contentArray[ref.index] as IMultimodalContentItem | undefined
if (item && ref.index < contentArray.length && item.type === 'image_url') {
contentArray[ref.index] = {
type: 'stored-file',
name: ref.fileName,
mime: ref.mime
} as ContentBlock
}
}
// Clean up the temporary tracking metadata
delete message.additional_kwargs!._imageFileRefs
if (message.additional_kwargs && Object.keys(message.additional_kwargs).length === 0) {
delete message.additional_kwargs
}
}
}
return updatedMessages
}
/**
* Converts LangChain message/chunk instances into plain JSON objects for clean DB storage.
* This avoids persisting large `{ lc, type, kwargs }` blobs and keeps execution-details UI readable.
*/
export const normalizeMessagesForStorage = (messages: BaseMessageLike[]): BaseMessageLike[] => {
return (messages || []).map((msg: any) => {
if (msg?.lc_namespace || typeof msg?._getType === 'function') {
const rawType = typeof msg?._getType === 'function' ? msg._getType() : msg?.type
const role =
rawType === 'ai'
? 'assistant'
: rawType === 'human'
? 'user'
: rawType === 'system'
? 'system'
: rawType === 'tool'
? 'tool'
: msg?.role || 'assistant'
const plain: Record<string, any> = {
role,
content: msg?.content ?? ''
}
if (msg?.name) plain.name = msg.name
if (msg?.tool_call_id) plain.tool_call_id = msg.tool_call_id
if (Array.isArray(msg?.tool_calls) && msg.tool_calls.length > 0) plain.tool_calls = msg.tool_calls
if (msg?.additional_kwargs && Object.keys(msg.additional_kwargs).length > 0) {
plain.additional_kwargs = msg.additional_kwargs
}
if (msg?.usage_metadata) plain.usage_metadata = msg.usage_metadata
if (msg?.id) plain.id = msg.id
return plain
}
return msg
})
}
// ─── Handling new image uploads ──────────────────────────────────────────────
/**
* Builds unique image messages from the current upload payload.
* Returns two versions:
* - `imageMessageWithFileRef`: lightweight stored-file references (for chat history)
* - `imageMessageWithBase64`: base64 data URLs (for model invocation)
* Returns undefined if no new unique images are found.
*/
export const getUniqueImageMessages = async (
options: ICommonObject,
messages: BaseMessageLike[],
modelConfig?: ICommonObject
): Promise<{ imageMessageWithFileRef: BaseMessageLike; imageMessageWithBase64: BaseMessageLike } | undefined> => {
if (!options.uploads) return undefined
// Get images from uploads
const images = await _addImagesToMessages(options, modelConfig?.allowImageUploads ?? false)
const imageUploads = getImageUploads(options.uploads)
// Collect (fileName, mime) already present in messages via _imageFileRefs
const alreadyPresentRefs = new Set<string>()
for (const msg of messages) {
const refs = (msg as IChatMessage).additional_kwargs?._imageFileRefs
if (refs) {
for (const r of refs) {
alreadyPresentRefs.add(`${sanitizeFileName(r.fileName)}:${r.mime}`)
}
}
}
// Filter out images already present in previous messages to avoid duplicates; keep (image, upload) pairs so indices stay aligned
const uniquePairs: { image: MessageContentImageUrl; upload: IFileUpload }[] = []
images.forEach((image, index) => {
const upload = imageUploads[index]
if (upload && alreadyPresentRefs.has(`${sanitizeFileName(upload.name)}:${upload.mime}`)) {
return
}
const alreadyInContent = messages.some((msg) => {
const chatMsg = msg as IChatMessage
if (Array.isArray(chatMsg.content)) {
return chatMsg.content.some(
(item) =>
(item as IMultimodalContentItem).type === 'image_url' &&
image.type === 'image_url' &&
JSON.stringify(item) === JSON.stringify(image)
)
}
return JSON.stringify(chatMsg.content) === JSON.stringify(image)
})
if (!alreadyInContent) uniquePairs.push({ image, upload })
})
const uniqueImages = uniquePairs.map((p) => p.image)
if (uniqueImages.length === 0) return undefined
// File-ref version: lightweight references for storage (only unique uploads)
const imageMessageWithFileRef: IChatMessage = {
role: 'user',
content: uniquePairs.map(({ upload }) => ({
type: upload.type,
name: sanitizeFileName(upload.name),
mime: upload.mime
})) as ContentBlock[]
}
// Build _imageFileRefs tracking from uploads (stored-file types only)
const fileRefs: IImageFileRef[] = []
uniquePairs.forEach(({ upload }, i) => {
if (upload && upload.type === 'stored-file') {
fileRefs.push({ index: i, fileName: sanitizeFileName(upload.name), mime: upload.mime })
}
})
// Base64 version: full image data for model invocation
const imageMessageWithBase64: IChatMessage = { role: 'user', content: uniqueImages }
if (fileRefs.length > 0) {
imageMessageWithBase64.additional_kwargs = { _imageFileRefs: fileRefs }
}
return {
imageMessageWithFileRef,
imageMessageWithBase64
}
}
// ─── Reconstructing past chat history with images ────────────────────────────
/**
* Processes past chat history messages, loading file uploads and converting
* stored images to base64 for model consumption. Also preserves additional_kwargs
* metadata (artifacts, file annotations, used tools) on each message.
*/
export const getPastChatHistoryImageMessages = async (
pastChatHistory: BaseMessageLike[],
options: ICommonObject
): Promise<{ updatedPastMessages: BaseMessageLike[]; transformedPastMessages: BaseMessageLike[] }> => {
const chatHistory: IChatMessage[] = []
const transformedPastMessages: IChatMessage[] = []
for (let i = 0; i < pastChatHistory.length; i++) {
const message = pastChatHistory[i] as BaseMessage & { role: string }
const messageRole = message.role || 'user'
// Collect non-empty additional_kwargs (artifacts, fileAnnotations, usedTools)
const collectKwargs = (source: Record<string, unknown>): IMessageAdditionalKwargs | undefined => {
const result: IMessageAdditionalKwargs = {}
let found = false
for (const key of ['artifacts', 'fileAnnotations', 'usedTools'] as const) {
const val = source[key]
if (val && Array.isArray(val) && val.length > 0) {
result[key] = val
found = true
}
}
return found ? result : undefined
}
if (message.additional_kwargs && message.additional_kwargs.fileUploads) {
const { fileUploads, artifacts, fileAnnotations, usedTools } = message.additional_kwargs
try {
let messageWithFileUploads = ''
const uploads: IFileUpload[] = typeof fileUploads === 'string' ? JSON.parse(fileUploads) : fileUploads
const imageContents: MessageContentImageUrl[] = []
const fileRefs: IImageFileRef[] = []
for (const upload of uploads as IFileUpload[]) {
if (upload.type === 'stored-file' && upload.mime.startsWith('image/')) {
// Convert stored images to base64 for model consumption
const fileName = sanitizeFileName(upload.name)
const base64Url = await storedFileToBase64(fileName, upload.mime, options)
fileRefs.push({ index: imageContents.length, fileName, mime: upload.mime })
imageContents.push({
type: 'image_url',
image_url: {
url: base64Url
}
})
} else if (upload.type === 'url' && upload.mime.startsWith('image') && upload.data) {
// URL-based images can be passed through directly
imageContents.push({
type: 'image_url',
image_url: {
url: upload.data
}
})
} else if (upload.type === 'stored-file:full') {
// Full document uploads: load and inline as XML-wrapped text
const safeFileName = sanitizeFileName(upload.name)
const fileLoaderNodeModule = await import('../../nodes/documentloaders/File/File')
// @ts-ignore
const fileLoaderNodeInstance = new fileLoaderNodeModule.nodeClass()
const nodeData = {
inputs: {
[mapMimeTypeToInputField(upload.mime)]: `FILE-STORAGE::${JSON.stringify([safeFileName])}`
}
}
const documents: string = await fileLoaderNodeInstance.init(nodeData, '', {
retrieveAttachmentChatId: true,
chatflowid: options.chatflowid,
chatId: options.chatId,
orgId: options.orgId
})
messageWithFileUploads += `<doc name='${safeFileName}'>${handleEscapeCharacters(documents, true)}</doc>\n\n`
}
}
const extraKwargs = collectKwargs({ artifacts, fileAnnotations, usedTools } as Record<string, unknown>)
// Add image message if we found any images
if (imageContents.length > 0) {
const imageMsg: IChatMessage = {
role: messageRole,
content: imageContents
}
if (fileRefs.length > 0) {
imageMsg.additional_kwargs = { _imageFileRefs: fileRefs }
}
if (extraKwargs) {
imageMsg.additional_kwargs = { ...imageMsg.additional_kwargs, ...extraKwargs }
}
chatHistory.push(imageMsg)
// Keep a copy of the original file uploads for potential reverting
const rawFileUploads = (pastChatHistory[i] as BaseMessage).additional_kwargs.fileUploads as string
transformedPastMessages.push({
role: messageRole,
content: [...JSON.parse(rawFileUploads)] as ContentBlock[]
})
}
// Add the text content (with any inlined document uploads prepended)
const messageContent = messageWithFileUploads
? `${messageWithFileUploads}\n\n${message.content}`
: (message.content as string)
const textMsg: IChatMessage = { role: messageRole, content: messageContent }
if (extraKwargs) textMsg.additional_kwargs = extraKwargs
chatHistory.push(textMsg)
} catch (e) {
// Fallback: just use the text content with any available kwargs
const extraKwargs = collectKwargs({
artifacts: message.additional_kwargs.artifacts,
fileAnnotations: message.additional_kwargs.fileAnnotations,
usedTools: message.additional_kwargs.usedTools
} as Record<string, unknown>)
const msg: IChatMessage = { role: messageRole, content: message.content as string }
if (extraKwargs) msg.additional_kwargs = extraKwargs
chatHistory.push(msg)
}
} else if (message.additional_kwargs) {
const extraKwargs = collectKwargs(message.additional_kwargs as Record<string, unknown>)
const msg: IChatMessage = { role: messageRole, content: message.content as string }
if (extraKwargs) msg.additional_kwargs = extraKwargs
chatHistory.push(msg)
} else {
chatHistory.push({ role: messageRole, content: message.content as string })
}
}
return { updatedPastMessages: chatHistory, transformedPastMessages }
}
// ─── MIME type and artifact type lookups ──────────────────────────────────────
/** Returns the MIME type for a filename based on its extension. */
export const getMimeTypeFromFilename = (filename: string): string => {
const extension = filename.toLowerCase().split('.').pop()
return MIME_TYPES[extension || ''] || 'application/octet-stream'
}
/** Returns the artifact type (for UI rendering) based on a filename's extension. */
export const getArtifactTypeFromFilename = (filename: string): string => {
const extension = filename.toLowerCase().split('.').pop()
return ARTIFACT_TYPES[extension || ''] || 'text'
}
// ─── Saving generated images to storage ──────────────────────────────────────
/** Saves base64 image data to storage and returns file information */
export const saveBase64Image = async (
outputItem: { result?: string; id?: string; output_format?: string },
options: ICommonObject
): Promise<ISavedImageResult | null> => {
try {
if (!outputItem.result) return null
const outputFormat = outputItem.output_format || 'png'
const fileName = `generated_image_${outputItem.id || Date.now()}.${outputFormat}`
const mimeType = outputFormat === 'png' ? 'image/png' : 'image/jpeg'
return await saveImageToStorage(outputItem.result, mimeType, fileName, options)
} catch (error) {
console.error('Error saving base64 image:', error)
return null
}
}
/** Saves a Gemini inline image to storage. */
export const saveGeminiInlineImage = async (
inlineItem: { data?: string; mimeType?: string },
options: ICommonObject
): Promise<ISavedImageResult | null> => {
try {
if (!inlineItem.data || !inlineItem.mimeType) return null
// Derive file extension from MIME type
const mime = inlineItem.mimeType
const extension =
mime.includes('jpeg') || mime.includes('jpg') ? 'jpg' : mime.includes('gif') ? 'gif' : mime.includes('webp') ? 'webp' : 'png'
const fileName = `gemini_generated_image_${Date.now()}.${extension}`
return await saveImageToStorage(inlineItem.data, inlineItem.mimeType, fileName, options)
} catch (error) {
console.error('Error saving Gemini inline image:', error)
return null
}
}
// ─── Downloading container files from OpenAI ─────────────────────────────────
/** Downloads a file from an OpenAI container (used for file citations in responses). */
export const downloadContainerFile = async (
containerId: string,
fileId: string,
filename: string,
modelNodeData: INodeData,
options: ICommonObject
): Promise<{ filePath: string; totalSize: number } | null> => {
try {
const credentialData = await getCredentialData(modelNodeData.credential ?? '', options)
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, modelNodeData)
if (!openAIApiKey) {
console.warn('No OpenAI API key available for downloading container file')
return null
}
const response = await fetch(`https://api.openai.com/v1/containers/${containerId}/files/${fileId}/content`, {
method: 'GET',
headers: {
Accept: '*/*',
Authorization: `Bearer ${openAIApiKey}`
}
})
if (!response.ok) {
console.warn(
`Failed to download container file ${fileId} from container ${containerId}: ${response.status} ${response.statusText}`
)
return null
}
const data = await response.arrayBuffer()
const dataBuffer = Buffer.from(data)
const mimeType = getMimeTypeFromFilename(filename)
const { path, totalSize } = await addSingleFileToStorage(
mimeType,
dataBuffer,
filename,
options.orgId,
options.chatflowid,
options.chatId
)
return { filePath: path, totalSize }
} catch (error) {
console.error('Error downloading container file:', error)
return null
}
}
// ─── Replacing inline image data with file references in responses ───────────
/**
* Replaces Gemini inlineData content items in a response with stored-file references,
* so the response content doesn't contain raw base64 data.
*/
export const replaceInlineDataWithFileReferences = (response: AIMessageChunk, savedInlineImages: ISavedInlineImage[]): void => {
if (!Array.isArray(response.content)) return
let savedImageIndex = 0
for (let i = 0; i < response.content.length; i++) {
const contentItem = response.content[i]
if (
typeof contentItem === 'object' &&
(contentItem as IMultimodalContentItem).type === 'inlineData' &&
(contentItem as Record<string, unknown>).inlineData &&
savedImageIndex < savedInlineImages.length
) {
const savedImage = savedInlineImages[savedImageIndex]
response.content[i] = {
type: 'stored-file',
name: savedImage.fileName,
mime: savedImage.mimeType,
path: savedImage.filePath
} as ContentBlock
savedImageIndex++
}
}
if (response.response_metadata?.inlineData) {
delete response.response_metadata.inlineData
}
}
// ─── Extracting artifacts from LLM response metadata ─────────────────────────
/**
* Processes response metadata from LLM providers to extract:
* - Image artifacts (OpenAI image generation, Gemini inline data)
* - File annotations (OpenAI container file citations)
* Saves generated images to storage and returns metadata for the UI.
*/
export const extractArtifactsFromResponse = async (
responseMetadata: IResponseMetadata | undefined,
modelNodeData: INodeData,
options: ICommonObject
): Promise<{
artifacts: IArtifact[]
fileAnnotations: IFileAnnotation[]
savedInlineImages?: ISavedInlineImage[]
}> => {
const artifacts: IArtifact[] = []
const fileAnnotations: IFileAnnotation[] = []
const savedInlineImages: ISavedInlineImage[] = []
// --- Gemini inline data (image generation) ---
if (responseMetadata?.inlineData && Array.isArray(responseMetadata.inlineData)) {
for (const inlineItem of responseMetadata.inlineData) {
if (inlineItem.type === 'gemini_inline_data' && inlineItem.data && inlineItem.mimeType) {
try {
const savedImageResult = await saveGeminiInlineImage(inlineItem, options)
if (savedImageResult) {
artifacts.push({
type: getArtifactTypeFromFilename(savedImageResult.fileName),
data: savedImageResult.filePath
})
savedInlineImages.push({
filePath: savedImageResult.filePath,
fileName: savedImageResult.fileName,
mimeType: inlineItem.mimeType
})
}
} catch (error) {
console.error('Error processing Gemini inline image artifact:', error)
}
}
}
}
if (!responseMetadata?.output || !Array.isArray(responseMetadata.output)) {
return { artifacts, fileAnnotations, savedInlineImages: savedInlineImages.length > 0 ? savedInlineImages : undefined }
}
for (const outputItem of responseMetadata.output) {
// --- Container file citations (OpenAI responses API) ---
if (outputItem.type === 'message' && outputItem.content && Array.isArray(outputItem.content)) {
for (const contentItem of outputItem.content) {
if (contentItem.annotations && Array.isArray(contentItem.annotations)) {
for (const annotation of contentItem.annotations) {
if (annotation.type === 'container_file_citation' && annotation.file_id && annotation.filename) {
try {
const downloadResult = await downloadContainerFile(
annotation.container_id,
annotation.file_id,
annotation.filename,
modelNodeData,
options
)
if (downloadResult) {
const fileType = getArtifactTypeFromFilename(annotation.filename)
if (fileType === 'png' || fileType === 'jpeg' || fileType === 'jpg') {
artifacts.push({ type: fileType, data: downloadResult.filePath })
} else {
fileAnnotations.push({
filePath: downloadResult.filePath,
fileName: annotation.filename
})
}
}
} catch (error) {
console.error('Error processing annotation:', error)
}
}
}
}
}
}
// --- OpenAI built-in tool artifacts (image generation) ---
if (outputItem.type === 'image_generation_call' && outputItem.result) {
try {
const savedImageResult = await saveBase64Image(outputItem, options)
if (savedImageResult) {
outputItem.result = savedImageResult.filePath
artifacts.push({
type: getArtifactTypeFromFilename(savedImageResult.fileName),
data: savedImageResult.filePath
})
}
} catch (error) {
console.error('Error processing image generation artifact:', error)
}
}
}
return { artifacts, fileAnnotations, savedInlineImages: savedInlineImages.length > 0 ? savedInlineImages : undefined }
}
// ─── Injecting image artifacts as temporary messages for model context ────────
/**
* Scans assistant messages for image artifacts and inserts temporary user messages
* containing the base64 image data right after each assistant message. This allows
* the model to "see" previously generated images in follow-up turns.
*
* These temporary messages are marked with `_isTemporaryImageMessage: true` so they
* can be stripped out after model invocation (they shouldn't be persisted).
*/
export const addImageArtifactsToMessages = async (messages: BaseMessageLike[], options: ICommonObject): Promise<void> => {
const messagesToInsert: Array<{ index: number; base64Message: IChatMessage }> = []
for (let i = 0; i < messages.length; i++) {
const message = messages[i] as IChatMessage
if (
(message.role !== 'assistant' && message.role !== 'ai') ||
!message.additional_kwargs?.artifacts ||
!Array.isArray(message.additional_kwargs.artifacts)
) {
continue
}
// Find image-type artifacts in this assistant message
const imageArtifacts: IImageArtifact[] = []
for (const artifact of message.additional_kwargs.artifacts) {
if (artifact.type && artifact.data && IMAGE_EXTENSIONS.includes(artifact.type.toLowerCase())) {
imageArtifacts.push({
name: sanitizeFileName(artifact.data),
mime: `image/${artifact.type.toLowerCase()}`
})
}
}
if (imageArtifacts.length === 0) continue
// Skip if the next message already contains these image artifacts
const nextMessage = messages[i + 1] as IChatMessage | undefined
const alreadyPresent =
nextMessage &&
nextMessage.role === 'user' &&
Array.isArray(nextMessage.content) &&
(nextMessage.content as IMultimodalContentItem[]).some(
(item) =>
(item.type === 'stored-file' || item.type === 'image_url') &&
imageArtifacts.some((artifact) => {
const artifactName = artifact.name.replace('FILE-STORAGE::', '')
const itemName = item.name?.replace('FILE-STORAGE::', '') || ''
return artifactName === itemName
})
)
if (alreadyPresent) continue
// Build base64 content for each image artifact
const base64Contents: MessageContentImageUrl[] = []
const fileRefs: IImageFileRef[] = []
for (const artifact of imageArtifacts) {
try {
const fileName = sanitizeFileName(artifact.name)
const base64Url = await storedFileToBase64(fileName, artifact.mime, options)
fileRefs.push({ index: base64Contents.length, fileName, mime: artifact.mime })
base64Contents.push({
type: 'image_url',
image_url: { url: base64Url }
})
} catch (error) {
console.error(`Failed to load artifact image ${artifact.name}:`, error)
}
}
if (base64Contents.length > 0) {
const base64Message: IChatMessage = { role: 'user', content: base64Contents, _isTemporaryImageMessage: true }
if (fileRefs.length > 0) {
base64Message.additional_kwargs = { _imageFileRefs: fileRefs }
}
messagesToInsert.push({ index: i + 1, base64Message })
}
}
// Insert in reverse order so indices remain valid
for (let i = messagesToInsert.length - 1; i >= 0; i--) {
const { index, base64Message } = messagesToInsert[i]
messages.splice(index, 0, base64Message as unknown as BaseMessageLike)
}
}
// ─── Flow state management ───────────────────────────────────────────────────
/** Merges new key-value pairs into the flow state. */
export const updateFlowState = (state: ICommonObject, updateState: IFlowState[]): ICommonObject => {
const newFlowState: Record<string, string> = {}
for (const s of updateState) {
newFlowState[s.key] = s.value
}
return { ...state, ...newFlowState }
}
// ─── Private: converting uploads to base64 image content ─────────────────────
/** Converts image uploads to base64 image_url content items for model consumption. */
const _addImagesToMessages = async (options: ICommonObject, allowImageUploads: boolean): Promise<MessageContentImageUrl[]> => {
const imageContent: MessageContentImageUrl[] = []
if (!allowImageUploads || !options?.uploads?.length) return imageContent
const imageUploads = getImageUploads(options.uploads)
for (const upload of imageUploads) {
let url = upload.data
if (upload.type === 'stored-file') {
const fileName = sanitizeFileName(upload.name)
url = await storedFileToBase64(fileName, upload.mime, options)
}
if (url) {
imageContent.push({
type: 'image_url',
image_url: { url }
})
}
}
return imageContent
}