import { Tool } from '@agenite/tool';
import { readFile } from 'fs/promises';
interface FileReadInput {
path: string;
format?: 'text' | 'base64';
}
const fileReaderTool = new Tool<FileReadInput>({
name: 'file-reader',
description: 'Read the contents of a file',
version: '1.0.0',
inputSchema: {
type: 'object',
properties: {
path: { type: 'string' },
format: {
type: 'string',
enum: ['text', 'base64'],
default: 'text',
},
},
required: ['path'],
},
execute: async ({ input }) => {
try {
const content = await readFile(input.path);
if (input.format === 'base64' && content.toString().endsWith('.jpg')) {
// Return image data for JPG files when base64 format is requested
return {
isError: false,
data: [
{
type: 'image',
image: {
type: 'base64',
media_type: 'image/jpeg',
data: content.toString('base64'),
},
},
{
type: 'text',
text: 'File read successfully',
},
],
};
}
// Default to text response
return {
isError: false,
data: content.toString('utf-8'),
};
} catch (error) {
return {
isError: true,
data: `Could not read file: ${error.message}`,
error: {
code: 'FILE_ERROR',
message: error.message,
},
};
}
},
});