|
1 | 1 | import z from "zod" |
2 | 2 | import path from "path" |
| 3 | +import { Effect, Option } from "effect" |
| 4 | +import * as Stream from "effect/Stream" |
3 | 5 | import { Tool } from "./tool" |
4 | | -import { Filesystem } from "../util/filesystem" |
5 | 6 | import DESCRIPTION from "./glob.txt" |
6 | 7 | import { Ripgrep } from "../file/ripgrep" |
7 | 8 | import { Instance } from "../project/instance" |
8 | | -import { assertExternalDirectory } from "./external-directory" |
| 9 | +import { assertExternalDirectoryEffect } from "./external-directory" |
| 10 | +import { AppFileSystem } from "../filesystem" |
9 | 11 |
|
10 | | -export const GlobTool = Tool.define("glob", { |
11 | | - description: DESCRIPTION, |
12 | | - parameters: z.object({ |
13 | | - pattern: z.string().describe("The glob pattern to match files against"), |
14 | | - path: z |
15 | | - .string() |
16 | | - .optional() |
17 | | - .describe( |
18 | | - `The directory to search in. If not specified, the current working directory will be used. IMPORTANT: Omit this field to use the default directory. DO NOT enter "undefined" or "null" - simply omit it for the default behavior. Must be a valid directory path if provided.`, |
19 | | - ), |
20 | | - }), |
21 | | - async execute(params, ctx) { |
22 | | - await ctx.ask({ |
23 | | - permission: "glob", |
24 | | - patterns: [params.pattern], |
25 | | - always: ["*"], |
26 | | - metadata: { |
27 | | - pattern: params.pattern, |
28 | | - path: params.path, |
29 | | - }, |
30 | | - }) |
| 12 | +export const GlobTool = Tool.defineEffect( |
| 13 | + "glob", |
| 14 | + Effect.gen(function* () { |
| 15 | + const rg = yield* Ripgrep.Service |
| 16 | + const fs = yield* AppFileSystem.Service |
31 | 17 |
|
32 | | - let search = params.path ?? Instance.directory |
33 | | - search = path.isAbsolute(search) ? search : path.resolve(Instance.directory, search) |
34 | | - await assertExternalDirectory(ctx, search, { kind: "directory" }) |
| 18 | + return { |
| 19 | + description: DESCRIPTION, |
| 20 | + parameters: z.object({ |
| 21 | + pattern: z.string().describe("The glob pattern to match files against"), |
| 22 | + path: z |
| 23 | + .string() |
| 24 | + .optional() |
| 25 | + .describe( |
| 26 | + `The directory to search in. If not specified, the current working directory will be used. IMPORTANT: Omit this field to use the default directory. DO NOT enter "undefined" or "null" - simply omit it for the default behavior. Must be a valid directory path if provided.`, |
| 27 | + ), |
| 28 | + }), |
| 29 | + execute: (params: { pattern: string; path?: string }, ctx: Tool.Context) => |
| 30 | + Effect.gen(function* () { |
| 31 | + yield* Effect.promise(() => |
| 32 | + ctx.ask({ |
| 33 | + permission: "glob", |
| 34 | + patterns: [params.pattern], |
| 35 | + always: ["*"], |
| 36 | + metadata: { |
| 37 | + pattern: params.pattern, |
| 38 | + path: params.path, |
| 39 | + }, |
| 40 | + }), |
| 41 | + ) |
35 | 42 |
|
36 | | - const limit = 100 |
37 | | - const files = [] |
38 | | - let truncated = false |
39 | | - for await (const file of Ripgrep.files({ |
40 | | - cwd: search, |
41 | | - glob: [params.pattern], |
42 | | - signal: ctx.abort, |
43 | | - })) { |
44 | | - if (files.length >= limit) { |
45 | | - truncated = true |
46 | | - break |
47 | | - } |
48 | | - const full = path.resolve(search, file) |
49 | | - const stats = Filesystem.stat(full)?.mtime.getTime() ?? 0 |
50 | | - files.push({ |
51 | | - path: full, |
52 | | - mtime: stats, |
53 | | - }) |
54 | | - } |
55 | | - files.sort((a, b) => b.mtime - a.mtime) |
| 43 | + let search = params.path ?? Instance.directory |
| 44 | + search = path.isAbsolute(search) ? search : path.resolve(Instance.directory, search) |
| 45 | + yield* assertExternalDirectoryEffect(ctx, search, { kind: "directory" }) |
56 | 46 |
|
57 | | - const output = [] |
58 | | - if (files.length === 0) output.push("No files found") |
59 | | - if (files.length > 0) { |
60 | | - output.push(...files.map((f) => f.path)) |
61 | | - if (truncated) { |
62 | | - output.push("") |
63 | | - output.push( |
64 | | - `(Results are truncated: showing first ${limit} results. Consider using a more specific path or pattern.)`, |
65 | | - ) |
66 | | - } |
67 | | - } |
| 47 | + const limit = 100 |
| 48 | + let truncated = false |
| 49 | + const files = yield* rg.files({ cwd: search, glob: [params.pattern] }).pipe( |
| 50 | + Stream.mapEffect((file) => |
| 51 | + Effect.gen(function* () { |
| 52 | + const full = path.resolve(search, file) |
| 53 | + const info = yield* fs.stat(full).pipe(Effect.catch(() => Effect.succeed(undefined))) |
| 54 | + const mtime = info?.mtime.pipe(Option.map((d) => d.getTime()), Option.getOrElse(() => 0)) ?? 0 |
| 55 | + return { path: full, mtime } |
| 56 | + }), |
| 57 | + ), |
| 58 | + Stream.take(limit + 1), |
| 59 | + Stream.runCollect, |
| 60 | + Effect.map((chunk) => [...chunk]), |
| 61 | + ) |
68 | 62 |
|
69 | | - return { |
70 | | - title: path.relative(Instance.worktree, search), |
71 | | - metadata: { |
72 | | - count: files.length, |
73 | | - truncated, |
74 | | - }, |
75 | | - output: output.join("\n"), |
| 63 | + if (files.length > limit) { |
| 64 | + truncated = true |
| 65 | + files.length = limit |
| 66 | + } |
| 67 | + files.sort((a, b) => b.mtime - a.mtime) |
| 68 | + |
| 69 | + const output = [] |
| 70 | + if (files.length === 0) output.push("No files found") |
| 71 | + if (files.length > 0) { |
| 72 | + output.push(...files.map((f) => f.path)) |
| 73 | + if (truncated) { |
| 74 | + output.push("") |
| 75 | + output.push( |
| 76 | + `(Results are truncated: showing first ${limit} results. Consider using a more specific path or pattern.)`, |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + title: path.relative(Instance.worktree, search), |
| 83 | + metadata: { |
| 84 | + count: files.length, |
| 85 | + truncated, |
| 86 | + }, |
| 87 | + output: output.join("\n"), |
| 88 | + } |
| 89 | + }).pipe(Effect.orDie, Effect.runPromise), |
76 | 90 | } |
77 | | - }, |
78 | | -}) |
| 91 | + }), |
| 92 | +) |
0 commit comments