-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathfile-tool-processor.ts
More file actions
199 lines (176 loc) · 5.87 KB
/
Copy pathfile-tool-processor.ts
File metadata and controls
199 lines (176 loc) · 5.87 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
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { isUserFile } from '@/lib/core/utils/user-file'
import { uploadExecutionFile, uploadFileFromRawData } from '@/lib/uploads/contexts/execution'
import { downloadFileFromUrl } from '@/lib/uploads/utils/file-utils.server'
import type { ExecutionContext, UserFile } from '@/executor/types'
import type { ToolConfig, ToolFileData } from '@/tools/types'
const logger = createLogger('FileToolProcessor')
/**
* Processes tool outputs and converts file-typed outputs to UserFile objects.
* This enables tools to return file data that gets automatically stored in the
* execution filesystem and made available as UserFile objects for workflow use.
*/
export class FileToolProcessor {
/**
* Process tool outputs and convert file-typed outputs to UserFile objects
*/
static async processToolOutputs(
toolOutput: any,
toolConfig: ToolConfig,
executionContext: ExecutionContext
): Promise<any> {
if (!toolConfig.outputs) {
return toolOutput
}
const processedOutput = { ...toolOutput }
for (const [outputKey, outputDef] of Object.entries(toolConfig.outputs)) {
if (!FileToolProcessor.isFileOutput(outputDef.type)) {
continue
}
const fileData = processedOutput[outputKey]
if (!fileData) {
logger.warn(`File-typed output '${outputKey}' is missing from tool result`)
continue
}
try {
processedOutput[outputKey] = await FileToolProcessor.processFileOutput(
fileData,
outputDef.type,
outputKey,
executionContext
)
} catch (error) {
logger.error(`Error processing file output '${outputKey}':`, error)
const errorMessage = toError(error).message
throw new Error(`Failed to process file output '${outputKey}': ${errorMessage}`)
}
}
return processedOutput
}
/**
* Check if an output type is file-related
*/
private static isFileOutput(type: string): boolean {
return type === 'file' || type === 'file[]'
}
/**
* Process a single file output (either single file or array of files)
*/
private static async processFileOutput(
fileData: any,
outputType: string,
outputKey: string,
executionContext: ExecutionContext
): Promise<UserFile | UserFile[]> {
if (outputType === 'file[]') {
return FileToolProcessor.processFileArray(fileData, outputKey, executionContext)
}
return FileToolProcessor.processFileData(fileData, executionContext)
}
/**
* Process an array of files
*/
private static async processFileArray(
fileData: any,
outputKey: string,
executionContext: ExecutionContext
): Promise<UserFile[]> {
if (!Array.isArray(fileData)) {
throw new Error(`Output '${outputKey}' is marked as file[] but is not an array`)
}
return Promise.all(
fileData.map((file, index) => FileToolProcessor.processFileData(file, executionContext))
)
}
/**
* Convert various file data formats to UserFile by storing in execution filesystem.
* If the input is already a UserFile, returns it unchanged.
*/
private static async processFileData(
fileData: ToolFileData | UserFile,
context: ExecutionContext
): Promise<UserFile> {
// If already a UserFile (e.g., from tools that handle their own file storage),
// return it directly without re-processing
if (isUserFile(fileData)) {
return fileData as UserFile
}
const data = fileData as ToolFileData
try {
let buffer: Buffer | null = null
if (Buffer.isBuffer(data.data)) {
buffer = data.data
} else if (
data.data &&
typeof data.data === 'object' &&
'type' in data.data &&
'data' in data.data
) {
const serializedBuffer = data.data as { type: string; data: number[] }
if (serializedBuffer.type === 'Buffer' && Array.isArray(serializedBuffer.data)) {
buffer = Buffer.from(serializedBuffer.data)
} else {
throw new Error(`Invalid serialized buffer format for ${data.name}`)
}
} else if (typeof data.data === 'string' && data.data) {
let base64Data = data.data
if (base64Data.includes('-') || base64Data.includes('_')) {
base64Data = base64Data.replace(/-/g, '+').replace(/_/g, '/')
}
buffer = Buffer.from(base64Data, 'base64')
}
if (!buffer && data.url) {
buffer = await downloadFileFromUrl(data.url, { userId: context.userId })
}
if (buffer) {
if (buffer.length === 0) {
throw new Error(`File '${data.name}' has zero bytes`)
}
return await uploadExecutionFile(
{
workspaceId: context.workspaceId || '',
workflowId: context.workflowId,
executionId: context.executionId || '',
},
buffer,
data.name,
data.mimeType,
context.userId
)
}
if (!data.data) {
throw new Error(
`File data for '${data.name}' must have either 'data' (Buffer/base64) or 'url' property`
)
}
return uploadFileFromRawData(
{
name: data.name,
data: data.data,
mimeType: data.mimeType,
},
{
workspaceId: context.workspaceId || '',
workflowId: context.workflowId,
executionId: context.executionId || '',
},
context.userId
)
} catch (error) {
logger.error(`Error processing file data for '${data.name}':`, error)
throw error
}
}
/**
* Check if a tool has any file-typed outputs
*/
static hasFileOutputs(toolConfig: ToolConfig): boolean {
if (!toolConfig.outputs) {
return false
}
return Object.values(toolConfig.outputs).some(
(output) => output.type === 'file' || output.type === 'file[]'
)
}
}