import fg from 'fast-glob';
import { z } from 'zod';
const schema = z.object({
    pattern: z.string().min(1),
    cwd: z.string().optional(),
});
export const globTool = {
    name: 'glob',
    description: 'Find files matching a glob pattern (e.g. "src/**/*.ts", "**/package.json"). ' +
        'Returns matching paths, newest first. node_modules and .git are always excluded.',
    schema,
    jsonSchema: {
        type: 'object',
        properties: {
            pattern: { type: 'string', description: 'Glob pattern to match' },
            cwd: { type: 'string', description: 'Directory to search from (default: working directory)' },
        },
        required: ['pattern'],
    },
    readOnly: true,
    summarize: (i) => i.pattern,
    run: async (input, ctx) => {
        const entries = await fg(input.pattern, {
            cwd: input.cwd ?? ctx.cwd,
            ignore: ['**/node_modules/**', '**/.git/**'],
            dot: true,
            onlyFiles: true,
            stats: true,
            suppressErrors: true,
        });
        if (entries.length === 0)
            return 'No files matched.';
        const sorted = entries
            .sort((a, b) => (b.stats?.mtimeMs ?? 0) - (a.stats?.mtimeMs ?? 0))
            .slice(0, 200)
            .map((e) => e.path);
        const more = entries.length > 200 ? `\n... (${entries.length - 200} more)` : '';
        return sorted.join('\n') + more;
    },
};
