@useavalon/caelence-agent 0.2.2 → 0.2.3-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,80 @@ function resolveToolPath(cwd: string, rel: string, extra: ReadonlySet<string>):
48
48
  return resolveUnderCwd(cwd, rel);
49
49
  }
50
50
 
51
+ function toolError(err: unknown): ReturnType<typeof errorResult> {
52
+ return errorResult(err instanceof Error ? err.message : String(err));
53
+ }
54
+
55
+ function skipsPath(rel: string): boolean {
56
+ return rel.split("/").some((part) => shouldSkipDir(part));
57
+ }
58
+
59
+ async function globProjectFiles(
60
+ cwd: string,
61
+ pattern: string,
62
+ ): Promise<{ matches: string[]; truncated: boolean }> {
63
+ const glob = new Glob(pattern);
64
+ const matches: string[] = [];
65
+ for await (const path of glob.scan({ cwd, onlyFiles: true, dot: false })) {
66
+ if (skipsPath(path)) continue;
67
+ matches.push(path);
68
+ if (matches.length >= 500) break;
69
+ }
70
+ matches.sort((left, right) => left.localeCompare(right));
71
+ return { matches, truncated: matches.length >= 500 };
72
+ }
73
+
74
+ function lineMatches(line: string, pattern: string, re: RegExp | null): boolean {
75
+ return re ? re.test(line) : line.includes(pattern);
76
+ }
77
+
78
+ function collectLineHits(
79
+ rel: string,
80
+ content: string,
81
+ pattern: string,
82
+ re: RegExp | null,
83
+ hits: Array<{ path: string; line: number; text: string }>,
84
+ maxMatches: number,
85
+ ): boolean {
86
+ const lines = content.split("\n");
87
+ for (let i = 0; i < lines.length; i++) {
88
+ const line = lines[i] ?? "";
89
+ if (!lineMatches(line, pattern, re)) continue;
90
+ hits.push({ path: rel, line: i + 1, text: line.slice(0, 240) });
91
+ if (hits.length >= maxMatches) return true;
92
+ }
93
+ return false;
94
+ }
95
+
96
+ async function readFileIfPresent(abs: string): Promise<string | undefined> {
97
+ try {
98
+ return await readFile(abs, "utf8");
99
+ } catch {
100
+ return undefined;
101
+ }
102
+ }
103
+
104
+ async function grepProjectFiles(
105
+ cwd: string,
106
+ pattern: string,
107
+ fileGlob: string,
108
+ useRegex: boolean,
109
+ maxMatches: number,
110
+ ): Promise<{ hits: Array<{ path: string; line: number; text: string }>; truncated: boolean }> {
111
+ const re = useRegex ? new RegExp(pattern) : null;
112
+ const hits: Array<{ path: string; line: number; text: string }> = [];
113
+ const glob = new Glob(fileGlob);
114
+ for await (const rel of glob.scan({ cwd, onlyFiles: true, dot: false })) {
115
+ if (skipsPath(rel)) continue;
116
+ const content = await readFileIfPresent(join(cwd, rel));
117
+ if (content === undefined) continue;
118
+ if (collectLineHits(rel, content, pattern, re, hits, maxMatches)) {
119
+ return { hits, truncated: true };
120
+ }
121
+ }
122
+ return { hits, truncated: false };
123
+ }
124
+
51
125
  export function createLocalTools(options: LocalToolsOptions): McpServer {
52
126
  const cwd = options.cwd;
53
127
  const extras = (): ReadonlySet<string> =>
@@ -185,18 +259,10 @@ export function createLocalTools(options: LocalToolsOptions): McpServer {
185
259
  async handler(raw) {
186
260
  try {
187
261
  const pattern = asString(raw.pattern) || "**/*";
188
- const glob = new Glob(pattern);
189
- const matches: string[] = [];
190
- for await (const path of glob.scan({ cwd, onlyFiles: true, dot: false })) {
191
- const parts = path.split("/");
192
- if (parts.some((p) => shouldSkipDir(p))) continue;
193
- matches.push(path);
194
- if (matches.length >= 500) break;
195
- }
196
- matches.sort();
197
- return jsonResult({ pattern, matches, truncated: matches.length >= 500 });
262
+ const found = await globProjectFiles(cwd, pattern);
263
+ return jsonResult({ pattern, matches: found.matches, truncated: found.truncated });
198
264
  } catch (err) {
199
- return errorResult(err instanceof Error ? err.message : String(err));
265
+ return toolError(err);
200
266
  }
201
267
  },
202
268
  },
@@ -218,35 +284,16 @@ export function createLocalTools(options: LocalToolsOptions): McpServer {
218
284
  try {
219
285
  const pattern = asString(raw.pattern);
220
286
  if (!pattern) return errorResult("pattern is empty");
221
- const fileGlob = asString(raw.glob) || "**/*";
222
- const maxMatches = typeof raw.max_matches === "number" ? raw.max_matches : 100;
223
- const useRegex = raw.regex === true;
224
- const re = useRegex ? new RegExp(pattern) : null;
225
- const hits: Array<{ path: string; line: number; text: string }> = [];
226
- const glob = new Glob(fileGlob);
227
- for await (const rel of glob.scan({ cwd, onlyFiles: true, dot: false })) {
228
- const parts = rel.split("/");
229
- if (parts.some((p) => shouldSkipDir(p))) continue;
230
- let content: string;
231
- try {
232
- content = await readFile(join(cwd, rel), "utf8");
233
- } catch {
234
- continue;
235
- }
236
- const lines = content.split("\n");
237
- for (let i = 0; i < lines.length; i++) {
238
- const line = lines[i] ?? "";
239
- const ok = re ? re.test(line) : line.includes(pattern);
240
- if (!ok) continue;
241
- hits.push({ path: rel, line: i + 1, text: line.slice(0, 240) });
242
- if (hits.length >= maxMatches) {
243
- return jsonResult({ pattern, hits, truncated: true });
244
- }
245
- }
246
- }
247
- return jsonResult({ pattern, hits, truncated: false });
287
+ const found = await grepProjectFiles(
288
+ cwd,
289
+ pattern,
290
+ asString(raw.glob) || "**/*",
291
+ raw.regex === true,
292
+ typeof raw.max_matches === "number" ? raw.max_matches : 100,
293
+ );
294
+ return jsonResult({ pattern, hits: found.hits, truncated: found.truncated });
248
295
  } catch (err) {
249
- return errorResult(err instanceof Error ? err.message : String(err));
296
+ return toolError(err);
250
297
  }
251
298
  },
252
299
  },