import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
import { resolveToCwd } from "../../utils/helpers.js";
import path from "path";
import { existsSync } from "fs";
import { Type } from "@sinclair/typebox";
import { globSync } from "glob";
//#region src/agent/tools/find.ts
const findSchema = Type.Object({
	pattern: Type.String({ description: "Glob pattern to match files, e.g. '*.ts', '**/*.json', or 'src/**/*.spec.ts'" }),
	path: Type.Optional(Type.String({ description: "Directory to search in (default: current directory)" })),
	limit: Type.Optional(Type.Number({ description: "Maximum number of results (default: 1000)" }))
});
const DEFAULT_LIMIT = 1e3;
/**
* Find tool - Search for files by glob pattern
*/
function createFindTool(cwd) {
	return {
		name: "find",
		label: "📁 find",
		description: "Find files by glob pattern (e.g., *.ts, **/*.json).",
		parameters: findSchema,
		execute: async (_toolCallId, params, signal) => {
			try {
				const p = params;
				const searchPath = resolveToCwd(p.path || ".", cwd);
				const effectiveLimit = p.limit || DEFAULT_LIMIT;
				if (!existsSync(searchPath)) return {
					content: [{
						type: "text",
						text: `Error: Path not found: ${searchPath}`
					}],
					details: {}
				};
				if (signal?.aborted) return {
					content: [{
						type: "text",
						text: "Operation aborted"
					}],
					details: {}
				};
				const filePaths = globSync(p.pattern, {
					cwd: searchPath,
					dot: true,
					ignore: ["**/node_modules/**", "**/.git/**"]
				});
				if (filePaths.length === 0) return {
					content: [{
						type: "text",
						text: "No files found matching pattern"
					}],
					details: {}
				};
				const relativized = filePaths.map((p) => {
					if (p.startsWith(searchPath)) return p.slice(searchPath.length + 1);
					return path.relative(searchPath, p);
				});
				relativized.sort();
				const resultLimitReached = relativized.length >= effectiveLimit;
				const truncation = truncateHead((resultLimitReached ? relativized.slice(0, effectiveLimit) : relativized).join("\n"), { maxLines: Number.MAX_SAFE_INTEGER });
				let resultOutput = truncation.content;
				const details = {};
				const notices = [];
				if (resultLimitReached) {
					notices.push(`${effectiveLimit} results limit reached`);
					details.resultLimitReached = effectiveLimit;
				}
				if (truncation.truncated) {
					notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
					details.truncation = truncation;
				}
				if (notices.length > 0) resultOutput += `\n\n[${notices.join(". ")}]`;
				return {
					content: [{
						type: "text",
						text: resultOutput
					}],
					details: Object.keys(details).length > 0 ? details : void 0
				};
			} catch (error) {
				return {
					content: [{
						type: "text",
						text: `Error during find: ${error instanceof Error ? error.message : String(error)}`
					}],
					details: {}
				};
			}
		}
	};
}
/** Default find tool using process.cwd() */
const findTool = createFindTool(process.cwd());
//#endregion
export { createFindTool, findTool };

//# sourceMappingURL=find.js.map