mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-07 19:13:24 +00:00
144 lines
5.5 KiB
TypeScript
144 lines
5.5 KiB
TypeScript
export * as GrepTool from "./grep"
|
|
|
|
import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
|
|
import { ToolFailure } from "@opencode-ai/llm"
|
|
import { Effect, Schema } from "effect"
|
|
import path from "path"
|
|
import { FileSystem } from "../filesystem"
|
|
import { FSUtil } from "../fs-util"
|
|
import { Location } from "../location"
|
|
import { PermissionV2 } from "../permission"
|
|
import { Ripgrep } from "../ripgrep"
|
|
import { RelativePath } from "../schema"
|
|
import { Tool } from "./tool"
|
|
|
|
export const name = "grep"
|
|
|
|
export const Input = Schema.Struct({
|
|
pattern: FileSystem.GrepInput.fields.pattern.annotate({
|
|
description: "Regex pattern to search for in file contents",
|
|
}),
|
|
path: RelativePath.pipe(Schema.optional).annotate({
|
|
description: "Relative directory to search. Defaults to the active Location.",
|
|
}),
|
|
include: FileSystem.GrepInput.fields.include.annotate({
|
|
description: 'File glob to include in the search (for example, "*.js" or "*.{ts,tsx}")',
|
|
}),
|
|
limit: FileSystem.GrepInput.fields.limit.annotate({
|
|
description: "Maximum matches to return",
|
|
}),
|
|
})
|
|
|
|
export const Output = Schema.Array(FileSystem.Match)
|
|
type ModelOutput = typeof Output.Encoded
|
|
|
|
/** Format raw search matches into the familiar concise model output. */
|
|
export const toModelOutput = (output: ModelOutput) => {
|
|
const lines = output.length === 0 ? ["No files found"] : [`Found ${output.length} matches`]
|
|
let current = ""
|
|
for (const match of output) {
|
|
if (current !== match.entry.path) {
|
|
if (current) lines.push("")
|
|
current = match.entry.path
|
|
lines.push(`${match.entry.path}:`)
|
|
}
|
|
lines.push(` Line ${match.line}: ${match.text}`)
|
|
}
|
|
return lines.join("\n")
|
|
}
|
|
|
|
/** Grep leaf that defaults its filesystem root to the active Location. */
|
|
export const Plugin = {
|
|
id: "opencode.tool.grep",
|
|
effect: Effect.fn("GrepTool.Plugin")(function* (ctx: PluginContext) {
|
|
const fs = yield* FSUtil.Service
|
|
const ripgrep = yield* Ripgrep.Service
|
|
const location = yield* Location.Service
|
|
const permission = yield* PermissionV2.Service
|
|
|
|
yield* ctx.tool
|
|
.transform((draft) =>
|
|
draft.add(
|
|
name,
|
|
Tool.make({
|
|
description:
|
|
"Search file contents by regular expression within the active Location or an absolute managed tool-output file. Use a path to narrow the search, include to filter files by glob, and limit to bound the match count. Returns concise file resources, line numbers, and bounded line previews.",
|
|
input: Input,
|
|
output: Output,
|
|
toModelOutput: ({ output }) => [
|
|
{
|
|
type: "text",
|
|
text: toModelOutput(
|
|
output.map((match) => ({
|
|
...match,
|
|
entry: { ...match.entry, path: path.resolve(location.directory, match.entry.path) },
|
|
})),
|
|
),
|
|
},
|
|
],
|
|
execute: (input, context) =>
|
|
Effect.gen(function* () {
|
|
yield* permission.assert({
|
|
action: name,
|
|
resources: [input.pattern],
|
|
save: ["*"],
|
|
metadata: {
|
|
root: ".",
|
|
path: input.path,
|
|
include: input.include,
|
|
limit: input.limit,
|
|
},
|
|
sessionID: context.sessionID,
|
|
agent: context.agent,
|
|
source: { type: "tool", messageID: context.assistantMessageID, callID: context.toolCallID },
|
|
})
|
|
const target = path.resolve(location.directory, input.path ?? ".")
|
|
const info = yield* fs
|
|
.stat(target)
|
|
.pipe(
|
|
Effect.catchReason("PlatformError", "NotFound", () =>
|
|
Effect.fail(new ToolFailure({ message: `Search path does not exist: ${input.path ?? "."}` })),
|
|
),
|
|
)
|
|
return yield* ripgrep
|
|
.grep({
|
|
cwd: info?.type === "Directory" ? target : path.dirname(target),
|
|
pattern: input.pattern,
|
|
file: info?.type === "File" ? path.basename(target) : undefined,
|
|
include: input.include,
|
|
limit: input.limit ?? Number.MAX_SAFE_INTEGER,
|
|
})
|
|
.pipe(
|
|
Effect.map((result) =>
|
|
result.map((match) =>
|
|
FileSystem.Match.make({
|
|
...match,
|
|
entry: FileSystem.Entry.make({
|
|
...match.entry,
|
|
path: RelativePath.make(
|
|
path.relative(
|
|
location.directory,
|
|
path.resolve(
|
|
info?.type === "Directory" ? target : path.dirname(target),
|
|
match.entry.path,
|
|
),
|
|
),
|
|
),
|
|
}),
|
|
}),
|
|
),
|
|
),
|
|
)
|
|
}).pipe(
|
|
Effect.mapError((error) =>
|
|
error instanceof ToolFailure
|
|
? error
|
|
: new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error }),
|
|
),
|
|
),
|
|
}),
|
|
),
|
|
)
|
|
.pipe(Effect.orDie)
|
|
}),
|
|
}
|