const fileAnalyzerTool = createTool({
id: 'file_analyzer',
name: 'File Analyzer',
description: 'Analyzes file content and provides statistics',
requiresPermission: false,
category: ToolCategory.READONLY,
parameters: {
path: {
type: 'string',
description: 'Path to the file to analyze'
},
includeContent: {
type: 'boolean',
description: 'Whether to include file content in analysis'
}
},
requiredParameters: ['path'],
validateArgs: (args) => {
const { path } = args;
if (typeof path !== 'string' || !path.trim()) {
return { valid: false, reason: 'Path must be a non-empty string' };
}
return { valid: true };
},
execute: async (args, context) => {
const { path, includeContent = false } = args;
const { executionAdapter, executionId, abortSignal } = context;
try {
// Check if operation was aborted
if (abortSignal?.aborted) {
throw new Error('Operation aborted');
}
// Read file content
const fileResult = await executionAdapter.readFile(
executionId,
path as string
);
if (!fileResult.ok) {
return {
ok: false,
error: `Failed to read file: ${fileResult.error}`
};
}
const content = fileResult.data.content;
const lines = content.split('\n');
const analysis = {
path,
size: fileResult.data.size,
encoding: fileResult.data.encoding,
lineCount: lines.length,
characterCount: content.length,
wordCount: content.split(/\s+/).filter(word => word.length > 0).length,
isEmpty: content.trim().length === 0,
...(includeContent && { content })
};
return {
ok: true,
data: analysis
};
} catch (error) {
return {
ok: false,
error: error.message
};
}
}
});