Repository navigation
Expand file tree
/
Copy pathparser.ts
More file actions
398 lines (350 loc) · 12.8 KB
/
Copy pathparser.ts
File metadata and controls
398 lines (350 loc) · 12.8 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
import { createLogger } from '@sim/logger'
import { isRecordLike } from '@sim/utils/object'
import { inferContextFromKey } from '@/lib/uploads/utils/file-utils'
import type { UserFile } from '@/executor/types'
import type {
FileParseResult,
FileParserInput,
FileParserOutput,
FileParserOutputData,
FileParserV3Output,
FileParserV3OutputData,
FileUploadInput,
} from '@/tools/file/types'
import { transformTable } from '@/tools/shared/table'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('FileParserTool')
const isUserFile = (value: unknown): value is UserFile =>
isRecordLike(value) &&
typeof value.id === 'string' &&
typeof value.name === 'string' &&
typeof value.url === 'string' &&
typeof value.size === 'number' &&
typeof value.type === 'string' &&
typeof value.key === 'string'
const isFileParseResult = (value: unknown): value is FileParseResult =>
isRecordLike(value) &&
typeof value.content === 'string' &&
typeof value.fileType === 'string' &&
typeof value.size === 'number' &&
typeof value.name === 'string' &&
typeof value.binary === 'boolean'
const normalizeHeaders = (headers: FileParserInput['headers']): Record<string, string> => {
const transformed = transformTable(headers ?? null)
return Object.entries(transformed).reduce(
(acc, [key, value]) => {
const headerName = key.trim()
if (headerName && value !== undefined && value !== null) {
acc[headerName] = String(value)
}
return acc
},
{} as Record<string, string>
)
}
const normalizeFileParseResult = (value: unknown): FileParseResult => {
if (isRecordLike(value) && isFileParseResult(value.output)) {
return value.output
}
if (isFileParseResult(value)) {
return value
}
const record = isRecordLike(value) ? value : {}
const file = isUserFile(record.file) ? record.file : undefined
const metadata = isRecordLike(record.metadata) ? record.metadata : undefined
const fallback: FileParseResult = {
content: typeof record.content === 'string' ? record.content : '',
fileType: typeof record.fileType === 'string' ? record.fileType : '',
size: typeof record.size === 'number' ? record.size : 0,
name: typeof record.name === 'string' ? record.name : 'unknown',
binary: typeof record.binary === 'boolean' ? record.binary : false,
...(metadata && { metadata }),
...(file && { file }),
}
return Object.assign({}, record, fallback)
}
interface ToolBodyParams extends Partial<FileParserInput> {
files?: FileUploadInput[]
_context?: {
workspaceId?: string
workflowId?: string
executionId?: string
}
}
const parseFileParserResponse = async (response: Response): Promise<FileParserOutput> => {
logger.info('Received response status:', response.status)
const result: unknown = await response.json()
logger.info('Response parsed successfully')
// Handle multiple files response
if (isRecordLike(result) && Array.isArray(result.results)) {
logger.info('Processing multiple files response')
const failedResults = result.results.filter(
(fileResult) => isRecordLike(fileResult) && fileResult.success === false
)
if (failedResults.length === result.results.length) {
const firstError = failedResults.find(
(fileResult) => isRecordLike(fileResult) && typeof fileResult.error === 'string'
)
return {
success: false,
output: {
files: [],
combinedContent: '',
},
error:
isRecordLike(firstError) && typeof firstError.error === 'string'
? firstError.error
: 'Failed to parse files',
}
}
// Extract individual file results
const fileResults: FileParseResult[] = result.results
.filter((fileResult) => !(isRecordLike(fileResult) && fileResult.success === false))
.map((fileResult) => normalizeFileParseResult(fileResult))
const processedFiles = fileResults.flatMap((file) => (file.file ? [file.file] : []))
// Combine all file contents with clear dividers
const combinedContent = fileResults
.map((file, index) => {
const divider = `\n${'='.repeat(80)}\n`
return file.content + (index < fileResults.length - 1 ? divider : '')
})
.join('\n')
// Create the base output
const output: FileParserOutputData = {
files: fileResults,
combinedContent,
...(processedFiles.length > 0 && { processedFiles }),
}
const error = typeof result.error === 'string' ? result.error : undefined
return {
success: true,
output,
...(error ? { error } : {}),
}
}
if (isRecordLike(result) && result.success === false) {
return {
success: false,
output: {
files: [],
combinedContent: '',
},
error: typeof result.error === 'string' ? result.error : 'Failed to parse file',
}
}
// Handle single file response
const fileOutput = normalizeFileParseResult(result)
logger.info('Successfully parsed file:', fileOutput.name || 'unknown')
// For a single file, create the output with just array format
const output: FileParserOutputData = {
files: [fileOutput],
combinedContent:
fileOutput.content ||
(isRecordLike(result) && typeof result.content === 'string' ? result.content : ''),
...(fileOutput.file && { processedFiles: [fileOutput.file] }),
}
return {
success: true,
output,
}
}
export const fileParserTool: ToolConfig<FileParserInput, FileParserOutput> = {
id: 'file_parser',
name: 'File Parser',
description: 'Parse one or more uploaded files or files from URLs (text, PDF, CSV, images, etc.)',
version: '1.0.0',
params: {
filePath: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'Path to the file(s). Can be a single path, URL, or an array of paths.',
},
file: {
type: 'file',
required: false,
visibility: 'user-only',
description: 'Uploaded file(s) to parse',
},
fileType: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'Type of file to parse (auto-detected if not specified)',
},
},
request: {
url: '/api/files/parse',
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
}),
body: (params: ToolBodyParams) => {
logger.info('Request parameters received by tool body:', params)
if (!params) {
logger.error('Tool body received no parameters')
throw new Error('No parameters provided to tool body')
}
let determinedFilePath: string | string[] | null = null
const determinedFileType: string | undefined = params.fileType
const resolveFilePath = (fileInput: unknown): string | null => {
if (!isRecordLike(fileInput)) return null
if (typeof fileInput.path === 'string') {
return fileInput.path
}
if (typeof fileInput.url === 'string') {
return fileInput.url
}
if (typeof fileInput.key === 'string') {
const key = fileInput.key
const context =
typeof fileInput.context === 'string' ? fileInput.context : inferContextFromKey(key)
return `/api/files/serve/${encodeURIComponent(key)}?context=${context}`
}
return null
}
// Determine the file path(s) based on input parameters.
// Precedence: direct filePath > file array > single file object > legacy files array
// 1. Check for direct filePath (URL or single path from upload)
if (params.filePath) {
logger.info('Tool body found direct filePath:', params.filePath)
determinedFilePath = params.filePath
}
// 2. Check for file upload (array)
else if (params.file && Array.isArray(params.file) && params.file.length > 0) {
logger.info('Tool body processing file array upload')
const filePaths = params.file
.map((file) => resolveFilePath(file))
.filter(Boolean) as string[]
if (filePaths.length !== params.file.length) {
throw new Error('Invalid file input: One or more files are missing path or URL')
}
determinedFilePath = filePaths
}
// 3. Check for file upload (single object)
else if (params.file && !Array.isArray(params.file)) {
logger.info('Tool body processing single file object upload')
const resolvedPath = resolveFilePath(params.file)
if (!resolvedPath) {
throw new Error('Invalid file input: Missing path or URL')
}
determinedFilePath = resolvedPath
}
// 4. Check for deprecated multiple files case (from older blocks?)
else if (params.files && Array.isArray(params.files)) {
logger.info('Tool body processing legacy files array:', params.files.length)
if (params.files.length > 0) {
const filePaths = params.files
.map((file) => resolveFilePath(file))
.filter(Boolean) as string[]
if (filePaths.length !== params.files.length) {
throw new Error('Invalid file input: One or more files are missing path or URL')
}
determinedFilePath = filePaths
} else {
logger.warn('Legacy files array provided but is empty')
}
}
// Final check if filePath was determined
if (!determinedFilePath) {
logger.error('Tool body could not determine filePath from parameters:', params)
throw new Error('Missing required parameter: filePath')
}
logger.info('Tool body determined filePath:', determinedFilePath)
const headers = normalizeHeaders(params.headers)
return {
filePath: determinedFilePath,
fileType: determinedFileType,
...(Object.keys(headers).length > 0 && { headers }),
workspaceId: params.workspaceId || params._context?.workspaceId,
workflowId: params._context?.workflowId,
executionId: params._context?.executionId,
}
},
},
transformResponse: parseFileParserResponse,
outputs: {
files: { type: 'array', description: 'Array of parsed files with content and metadata' },
combinedContent: { type: 'string', description: 'Combined content of all parsed files' },
processedFiles: { type: 'file[]', description: 'Array of UserFile objects for downstream use' },
},
}
export const fileParserV2Tool: ToolConfig<FileParserInput, FileParserOutput> = {
id: 'file_parser_v2',
name: 'File Parser',
description: 'Parse one or more uploaded files or files from URLs (text, PDF, CSV, images, etc.)',
version: '2.0.0',
params: fileParserTool.params,
request: fileParserTool.request,
transformResponse: parseFileParserResponse,
outputs: {
files: {
type: 'array',
description: 'Array of parsed files with content, metadata, and file properties',
},
combinedContent: {
type: 'string',
description: 'All file contents merged into a single text string',
},
},
}
const parseFileParserV3Response = async (response: Response): Promise<FileParserV3Output> => {
const parsed = await parseFileParserResponse(response)
if (!parsed.success) {
return {
success: false,
output: {
files: [],
combinedContent: '',
},
error: parsed.error,
}
}
const output = parsed.output as FileParserOutputData
const files =
Array.isArray(output.processedFiles) && output.processedFiles.length > 0
? output.processedFiles
: []
const cleanedOutput: FileParserV3OutputData = {
files,
combinedContent: output.combinedContent,
}
return {
success: true,
output: cleanedOutput,
}
}
export const fileParserV3Tool: ToolConfig<FileParserInput, FileParserV3Output> = {
id: 'file_parser_v3',
name: 'File Parser',
description: 'Parse one or more uploaded files or files from URLs (text, PDF, CSV, images, etc.)',
version: '3.0.0',
params: fileParserTool.params,
request: fileParserTool.request,
transformResponse: parseFileParserV3Response,
outputs: {
files: { type: 'file[]', description: 'Parsed files as UserFile objects' },
combinedContent: { type: 'string', description: 'Combined content of all parsed files' },
},
}
export const fileFetchTool: ToolConfig<FileParserInput, FileParserV3Output> = {
id: 'file_fetch',
name: 'File Fetch',
description: 'Fetch and parse a file from a URL with optional custom headers.',
version: '1.0.0',
params: {
...fileParserTool.params,
headers: {
type: 'object',
required: false,
visibility: 'user-or-llm',
description: 'HTTP headers to include when fetching URL-based files.',
},
},
request: fileParserTool.request,
transformResponse: parseFileParserV3Response,
outputs: {
files: { type: 'file[]', description: 'Fetched files as UserFile objects' },
combinedContent: { type: 'string', description: 'Combined content of all fetched files' },
},
}