66 lines
3.1 KiB
JavaScript
66 lines
3.1 KiB
JavaScript
import { z } from "zod";
|
|
import { handleToolError, validateRequired } from "../helpers/errorHandler.js";
|
|
import { getCurrentProjectInfo, callLocalFileEndpoint, buildLocalFileErrorResponse } from "./helpers.js";
|
|
|
|
export function registerAcaiGrepTool(server) {
|
|
server.tool(
|
|
"acai-grep",
|
|
"Search text inside project files with compact line-level results. Supports optional glob filtering.",
|
|
{
|
|
pattern: z.string().describe("Text or regex pattern to search for"),
|
|
base_path: z.string().optional().describe("Optional base directory relative to the project root"),
|
|
glob: z.string().optional().describe("Optional file path glob filter, e.g. '**/index-base.tpl'"),
|
|
limit: z.number().int().positive().max(100).optional().describe("Maximum number of matches to return"),
|
|
case_sensitive: z.boolean().optional().describe("Whether matching should be case-sensitive"),
|
|
regex: z.boolean().optional().describe("Treat pattern as a regular expression"),
|
|
},
|
|
{ readOnlyHint: true, destructiveHint: false },
|
|
async ({ pattern, base_path, glob, limit, case_sensitive, regex }) => {
|
|
try {
|
|
const validationError = validateRequired({ pattern }, ["pattern"], "acai-grep");
|
|
if (validationError) return validationError;
|
|
|
|
const { projectSlug, projectDir } = getCurrentProjectInfo();
|
|
const result = await callLocalFileEndpoint("POST", "/api/files/grep", {
|
|
project: projectSlug,
|
|
projectDir,
|
|
pattern,
|
|
basePath: base_path,
|
|
glob,
|
|
limit,
|
|
caseSensitive: case_sensitive,
|
|
regex,
|
|
});
|
|
if (!result.data?.success) {
|
|
return buildLocalFileErrorResponse("acai-grep", result, { pattern, base_path, glob });
|
|
}
|
|
const data = result.data;
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: true,
|
|
pattern: data.pattern,
|
|
base_path: data.basePath,
|
|
glob: data.glob,
|
|
regex: data.regex,
|
|
case_sensitive: data.caseSensitive,
|
|
files_scanned: data.filesScanned,
|
|
matches: data.matches.map((match) => ({
|
|
file_path: match.filePath,
|
|
line: match.line,
|
|
match_preview: match.matchPreview,
|
|
})),
|
|
total_matches: data.totalMatches,
|
|
truncated: data.truncated,
|
|
}, null, 2),
|
|
}],
|
|
};
|
|
} catch (error) {
|
|
return handleToolError(error, "acai-grep", { pattern, base_path, glob });
|
|
}
|
|
}
|
|
);
|
|
}
|