glob.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. export * as GlobTool from "./glob"
  2. import { ToolFailure } from "@opencode-ai/llm"
  3. import { Effect, Layer, Schema } from "effect"
  4. import path from "path"
  5. import { makeLocationNode } from "../effect/app-node"
  6. import { FileSystem } from "../filesystem"
  7. import { Location } from "../location"
  8. import { Ripgrep } from "../ripgrep"
  9. import { RelativePath } from "../schema"
  10. import { PermissionV2 } from "../permission"
  11. import { ToolRegistry } from "./registry"
  12. import { Tool } from "./tool"
  13. import { Tools } from "./tools"
  14. export const name = "glob"
  15. export const Input = Schema.Struct({
  16. pattern: FileSystem.GlobInput.fields.pattern.annotate({ description: "Glob pattern to match files against" }),
  17. path: RelativePath.pipe(Schema.optional).annotate({
  18. description: "Relative directory to search. Defaults to the active Location.",
  19. }),
  20. limit: FileSystem.GlobInput.fields.limit.annotate({
  21. description: "Maximum results to return",
  22. }),
  23. })
  24. export const Output = Schema.Array(FileSystem.Entry)
  25. type ModelOutput = typeof Output.Encoded
  26. /** Format raw search results into the concise line-oriented output models expect. */
  27. export const toModelOutput = (output: ModelOutput) => {
  28. const lines = output.length === 0 ? ["No files found"] : output.map((item) => item.path)
  29. return lines.join("\n")
  30. }
  31. /** Glob leaf that defaults its filesystem root to the active Location. */
  32. const layer = Layer.effectDiscard(
  33. Effect.gen(function* () {
  34. const tools = yield* Tools.Service
  35. const ripgrep = yield* Ripgrep.Service
  36. const location = yield* Location.Service
  37. const permission = yield* PermissionV2.Service
  38. yield* tools
  39. .register({
  40. [name]: Tool.make({
  41. description:
  42. "Find files by glob pattern within the active Location. Returns concise relative file resources. Use a relative path to narrow the search and limit to bound the result count.",
  43. input: Input,
  44. output: Output,
  45. toModelOutput: ({ output }) => [
  46. {
  47. type: "text",
  48. text: toModelOutput(
  49. output.map((entry) => ({ ...entry, path: path.resolve(location.directory, entry.path) })),
  50. ),
  51. },
  52. ],
  53. execute: (input, context) =>
  54. Effect.gen(function* () {
  55. yield* permission.assert({
  56. action: name,
  57. resources: [input.pattern],
  58. save: ["*"],
  59. metadata: {
  60. root: input.path ?? ".",
  61. path: input.path,
  62. limit: input.limit,
  63. },
  64. sessionID: context.sessionID,
  65. agent: context.agent,
  66. source: { type: "tool", messageID: context.assistantMessageID, callID: context.toolCallID },
  67. })
  68. const cwd = path.resolve(location.directory, input.path ?? ".")
  69. return yield* ripgrep
  70. .glob({
  71. cwd,
  72. pattern: input.pattern,
  73. limit: input.limit ?? Number.MAX_SAFE_INTEGER,
  74. })
  75. .pipe(
  76. Effect.map((result) =>
  77. result.map((entry) =>
  78. FileSystem.Entry.make({
  79. ...entry,
  80. path: RelativePath.make(path.relative(location.directory, path.resolve(cwd, entry.path))),
  81. }),
  82. ),
  83. ),
  84. )
  85. }).pipe(
  86. Effect.mapError(() => new ToolFailure({ message: `Unable to find files matching ${input.pattern}` })),
  87. ),
  88. }),
  89. })
  90. .pipe(Effect.orDie)
  91. }),
  92. )
  93. export const node = makeLocationNode({
  94. name: "tool/glob",
  95. layer,
  96. deps: [ToolRegistry.node, Ripgrep.node, Location.node, PermissionV2.node],
  97. })