@@ -3,11 +3,16 @@ import { toError } from '@sim/utils/errors'
33import { generateId } from '@sim/utils/id'
44import { isExecutionCancelled , isRedisCancellationEnabled } from '@/lib/execution/cancellation'
55import { readUserFileContent } from '@/lib/execution/payloads/materialization.server'
6- import { createFileContent , type MessageContent } from '@/lib/uploads/utils/file-utils'
6+ import {
7+ createFileContentFromBase64 ,
8+ type MessageContent ,
9+ processSingleFileToUserFile ,
10+ type RawFileInput ,
11+ } from '@/lib/uploads/utils/file-utils'
712import type { BlockOutput } from '@/blocks/types'
813import { normalizeFileInput } from '@/blocks/utils'
914import { BlockType } from '@/executor/constants'
10- import type { BlockHandler , ExecutionContext , UserFile } from '@/executor/types'
15+ import type { BlockHandler , ExecutionContext } from '@/executor/types'
1116import { buildAPIUrl , buildAuthHeaders , extractAPIErrorMessage } from '@/executor/utils/http'
1217import type { SerializedBlock } from '@/serializer/types'
1318
@@ -19,35 +24,6 @@ type MothershipFileAttachment = MessageContent & {
1924 filename ?: string
2025}
2126
22- function toUserFile ( file : object ) : UserFile {
23- const candidate = file as Record < string , unknown >
24- const key = typeof candidate . key === 'string' ? candidate . key : ''
25- const name = typeof candidate . name === 'string' ? candidate . name : ''
26- const url =
27- typeof candidate . url === 'string'
28- ? candidate . url
29- : typeof candidate . path === 'string'
30- ? candidate . path
31- : key
32- const size = typeof candidate . size === 'number' ? candidate . size : Number ( candidate . size )
33- const type = typeof candidate . type === 'string' ? candidate . type : ''
34- const id = typeof candidate . id === 'string' ? candidate . id : key
35-
36- if ( ! id || ! key || ! name || ! url || ! Number . isFinite ( size ) || ! type ) {
37- throw new Error ( 'Mothership attachment must include file name, key, url/path, size, and type.' )
38- }
39-
40- return {
41- id,
42- key,
43- name,
44- url,
45- size,
46- type,
47- ...( typeof candidate . context === 'string' ? { context : candidate . context } : { } ) ,
48- }
49- }
50-
5127async function buildMothershipFileAttachments (
5228 filesInput : unknown ,
5329 ctx : ExecutionContext ,
@@ -64,7 +40,7 @@ async function buildMothershipFileAttachments(
6440
6541 const attachments : MothershipFileAttachment [ ] = [ ]
6642 for ( const file of files ) {
67- const userFile = toUserFile ( file )
43+ const userFile = processSingleFileToUserFile ( file as RawFileInput , requestId , logger )
6844 const base64 = await readUserFileContent ( userFile , {
6945 encoding : 'base64' ,
7046 userId : ctx . userId ,
@@ -79,7 +55,7 @@ async function buildMothershipFileAttachments(
7955 maxSourceBytes : MAX_MOTHERSHIP_ATTACHMENT_BYTES ,
8056 } )
8157
82- const content = createFileContent ( Buffer . from ( base64 , 'base64' ) , userFile . type )
58+ const content = createFileContentFromBase64 ( base64 , userFile . type )
8359 if ( ! content ) {
8460 throw new Error ( `File type is not supported for Mothership attachments: ${ userFile . name } ` )
8561 }
0 commit comments