MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / handleBinaryFile

Method handleBinaryFile

src/core/tools/ReadFileTool.ts:335–422  ·  view source on GitHub ↗

* Handle binary file processing (images, PDF, DOCX, etc.).

(
		task: Task,
		relPath: string,
		fullPath: string,
		supportsImages: boolean,
		maxImageFileSize: number,
		maxTotalImageSize: number,
		imageMemoryTracker: ImageMemoryTracker,
		updateFileResult: (path: string, updates: Partial<FileResult>) => void,
	)

Source from the content-addressed store, hash-verified

333 * Handle binary file processing (images, PDF, DOCX, etc.).
334 */
335 private async handleBinaryFile(
336 task: Task,
337 relPath: string,
338 fullPath: string,
339 supportsImages: boolean,
340 maxImageFileSize: number,
341 maxTotalImageSize: number,
342 imageMemoryTracker: ImageMemoryTracker,
343 updateFileResult: (path: string, updates: Partial<FileResult>) => void,
344 ): Promise<void> {
345 const fileExtension = path.extname(relPath).toLowerCase()
346 const supportedBinaryFormats = getSupportedBinaryFormats()
347
348 // Handle image files
349 if (isSupportedImageFormat(fileExtension)) {
350 try {
351 const validationResult = await validateImageForProcessing(
352 fullPath,
353 supportsImages,
354 maxImageFileSize,
355 maxTotalImageSize,
356 imageMemoryTracker.getTotalMemoryUsed(),
357 )
358
359 if (!validationResult.isValid) {
360 await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
361 updateFileResult(relPath, {
362 nativeContent: `File: ${relPath}\nNote: ${validationResult.notice}`,
363 })
364 return
365 }
366
367 const imageResult = await processImageFile(fullPath)
368 imageMemoryTracker.addMemoryUsage(imageResult.sizeInMB)
369 await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
370
371 updateFileResult(relPath, {
372 nativeContent: `File: ${relPath}\nNote: ${imageResult.notice}`,
373 imageDataUrl: imageResult.dataUrl,
374 })
375 return
376 } catch (error) {
377 const errorMsg = error instanceof Error ? error.message : String(error)
378 updateFileResult(relPath, {
379 status: "error",
380 error: `Error reading image file: ${errorMsg}`,
381 nativeContent: `File: ${relPath}\nError: ${errorMsg}`,
382 })
383 await task.say("error", `Error reading image file ${relPath}: ${errorMsg}`)
384 return
385 }
386 }
387
388 // Handle other supported binary formats (PDF, DOCX, etc.)
389 if (supportedBinaryFormats && supportedBinaryFormats.includes(fileExtension)) {
390 try {
391 const content = await extractTextFromFile(fullPath)
392 const numberedContent = addLineNumbers(content)

Callers 1

executeNewMethod · 0.95

Calls 11

isSupportedImageFormatFunction · 0.90
processImageFileFunction · 0.90
extractTextFromFileFunction · 0.90
addLineNumbersFunction · 0.90
getTotalMemoryUsedMethod · 0.80
trackFileContextMethod · 0.80
addMemoryUsageMethod · 0.80
sayMethod · 0.80
StringInterface · 0.50

Tested by

no test coverage detected