Forráskód Böngészése

enhance ripgrep files function with query filtering and limit support

🤖 Generated with [opencode](https://opencode.ai)

Co-Authored-By: opencode <[email protected]>
Dax Raad 8 hónapja
szülő
commit
fe05edaa79

+ 12 - 5
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)
   }
 }

+ 2 - 1
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,

+ 6 - 4
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 [