forked from simstudioai/sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-tool-processor.ts
More file actions
200 lines (178 loc) · 6.4 KB
/
Copy pathfile-tool-processor.ts
File metadata and controls
200 lines (178 loc) · 6.4 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
import { createLogger } from '@/lib/logs/console/logger'
import { uploadExecutionFile } from '@/lib/workflows/execution-file-storage'
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 }
// Process each output that's marked as file or file[]
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 = error instanceof Error ? error.message : String(error)
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, outputKey)
}
/**
* 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, `${outputKey}[${index}]`)
)
)
}
/**
* Convert various file data formats to UserFile by storing in execution filesystem
*/
private static async processFileData(
fileData: ToolFileData,
context: ExecutionContext,
outputKey: string
): Promise<UserFile> {
logger.info(`Processing file data for output '${outputKey}': ${fileData.name}`)
try {
// Convert various formats to Buffer
let buffer: Buffer
if (Buffer.isBuffer(fileData.data)) {
buffer = fileData.data
logger.info(`Using Buffer data for ${fileData.name} (${buffer.length} bytes)`)
} else if (
fileData.data &&
typeof fileData.data === 'object' &&
'type' in fileData.data &&
'data' in fileData.data
) {
// Handle serialized Buffer objects (from JSON serialization)
const serializedBuffer = fileData.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 ${fileData.name}`)
}
logger.info(
`Converted serialized Buffer to Buffer for ${fileData.name} (${buffer.length} bytes)`
)
} else if (typeof fileData.data === 'string' && fileData.data) {
// Assume base64 or base64url
let base64Data = fileData.data
// Convert base64url to base64 if needed (Gmail API format)
if (base64Data && (base64Data.includes('-') || base64Data.includes('_'))) {
base64Data = base64Data.replace(/-/g, '+').replace(/_/g, '/')
}
buffer = Buffer.from(base64Data, 'base64')
logger.info(
`Converted base64 string to Buffer for ${fileData.name} (${buffer.length} bytes)`
)
} else if (fileData.url) {
// Download from URL
logger.info(`Downloading file from URL: ${fileData.url}`)
const response = await fetch(fileData.url)
if (!response.ok) {
throw new Error(`Failed to download file from ${fileData.url}: ${response.statusText}`)
}
const arrayBuffer = await response.arrayBuffer()
buffer = Buffer.from(arrayBuffer)
logger.info(`Downloaded file from URL for ${fileData.name} (${buffer.length} bytes)`)
} else {
throw new Error(
`File data for '${fileData.name}' must have either 'data' (Buffer/base64) or 'url' property`
)
}
// Validate buffer
if (buffer.length === 0) {
throw new Error(`File '${fileData.name}' has zero bytes`)
}
// Store in execution filesystem
const userFile = await uploadExecutionFile(
{
workspaceId: context.workspaceId || '',
workflowId: context.workflowId,
executionId: context.executionId || '',
},
buffer,
fileData.name,
fileData.mimeType
)
logger.info(
`Successfully stored file '${fileData.name}' in execution filesystem with key: ${userFile.key}`
)
return userFile
} catch (error) {
logger.error(`Error processing file data for '${fileData.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[]'
)
}
}