The open source coding agent.
TypeScript
enhance ripgrep files function with query filtering and limit support
Dax Raadfe05edaa7958
diff --git a/packages/opencode/src/external/ripgrep.ts b/packages/opencode/src/external/ripgrep.tsindex 0c77673e3be..5d627dba588 100644--- a/packages/opencode/src/external/ripgrep.ts+++ b/packages/opencode/src/external/ripgrep.ts@@ -6,6 +6,7 @@ import { z } from "zod"import { NamedError } from "../util/error"import { lazy } from "../util/lazy"import { $ } from "bun"+import { Fzf } from "./fzf"export namespace Ripgrep {const PLATFORM = {@@ -113,11 +114,17 @@ export namespace Ripgrep {return filepath}- export async function files(cwd: string) {- const result =- await $`${await filepath()} --files --hidden --glob '!.git/*'`- .cwd(cwd)- .text()+ export async function files(input: {+ cwd: string+ query?: string+ limit?: number+ }) {+ const commands = [`${await filepath()} --files --hidden --glob='!.git/*'`]+ if (input.query)+ commands.push(`${await Fzf.filepath()} --filter=${input.query}`)+ if (input.limit) commands.push(`head -n ${input.limit}`)+ const joined = commands.join(" | ")+ const result = await $`${{ raw: joined }}`.cwd(input.cwd).text()return result.split("\n").filter(Boolean)}}diff --git a/packages/opencode/src/server/server.ts b/packages/opencode/src/server/server.tsindex eb50f7496ee..275a0e2ba5f 100644--- a/packages/opencode/src/server/server.ts+++ b/packages/opencode/src/server/server.ts@@ -14,6 +14,7 @@ import { mapValues } from "remeda"import { NamedError } from "../util/error"import { Fzf } from "../external/fzf"import { ModelsDev } from "../provider/models"+import { Ripgrep } from "../external/ripgrep"const ERRORS = {400: {@@ -457,7 +458,7 @@ export namespace Server {async (c) => {const body = c.req.valid("json")const app = App.info()- const result = await Fzf.search({+ const result = await Ripgrep.files({cwd: app.path.cwd,query: body.query,limit: 10,diff --git a/packages/opencode/src/session/system.ts b/packages/opencode/src/session/system.tsindex 9b124e41f1d..de128134fe6 100644--- a/packages/opencode/src/session/system.ts+++ b/packages/opencode/src/session/system.ts@@ -1,7 +1,5 @@import { App } from "../app/app"-import { Fzf } from "../external/fzf"import { Ripgrep } from "../external/ripgrep"-import { ListTool } from "../tool/ls"import { Filesystem } from "../util/filesystem"import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"@@ -28,7 +26,9 @@ export namespace SystemPrompt {const app = App.info()const tree = async () => {- const files = await Ripgrep.files(app.path.cwd)+ const files = await Ripgrep.files({+ cwd: app.path.cwd,+ })type Node = {children: Record<string, Node>}@@ -52,6 +52,7 @@ export namespace SystemPrompt {}function render(path: string[], node: Node): string {+ // if (path.length === 3) return "\t".repeat(path.length) + "..."const lines: string[] = []const entries = Object.entries(node.children).sort(([a], [b]) =>a.localeCompare(b),@@ -68,7 +69,8 @@ export namespace SystemPrompt {return lines.join("\n")}- return render([], root)+ const result = render([], root)+ return result}return [