Skip to content

Commit f6ed59b

Browse files
thdxrOpenCode
andcommitted
Refactor external tools organization and add file search API endpoint
🤖 Generated with [OpenCode](https://opencode.ai) Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 83991be commit f6ed59b

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Global } from "../global"
44
import fs from "fs/promises"
55
import { z } from "zod"
66
import { NamedError } from "../util/error"
7+
import { lazy } from "../util/lazy"
78

89
export namespace Ripgrep {
910
const PLATFORM = {
@@ -35,8 +36,10 @@ export namespace Ripgrep {
3536
}),
3637
)
3738

38-
const state = App.state("ripgrep", async () => {
39-
const filepath = path.join(
39+
const state = lazy(async () => {
40+
let filepath = Bun.which("rg")
41+
if (filepath) return { filepath }
42+
filepath = path.join(
4043
Global.Path.bin,
4144
"rg" + (process.platform === "win32" ? ".exe" : ""),
4245
)

packages/opencode/src/file/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export namespace File {
22
const glob = new Bun.Glob("**/*")
3-
export async function search(path: string) {
3+
export async function search(path: string, query: string) {
44
for await (const entry of glob.scan({
55
cwd: path,
66
onlyFiles: true,

packages/opencode/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const cli = yargs(hideBin(process.argv))
2626
describe: "Print logs to stderr",
2727
type: "boolean",
2828
})
29-
.middleware(async (args) => {
29+
.middleware(async () => {
3030
await Log.init({ print: process.argv.includes("--print-logs") })
3131
Log.Default.info("opencode", {
3232
version: VERSION,

packages/opencode/src/server/server.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { App } from "../app/app"
1212
import { Global } from "../global"
1313
import { mapValues } from "remeda"
1414
import { NamedError } from "../util/error"
15+
import { Fzf } from "../external/fzf"
1516

1617
const ERRORS = {
1718
400: {
@@ -427,6 +428,34 @@ export namespace Server {
427428
})
428429
},
429430
)
431+
.post(
432+
"/file_search",
433+
describeRoute({
434+
description: "Search for files",
435+
responses: {
436+
200: {
437+
description: "Search for files",
438+
content: {
439+
"application/json": {
440+
schema: resolver(z.string().array()),
441+
},
442+
},
443+
},
444+
},
445+
}),
446+
zValidator(
447+
"json",
448+
z.object({
449+
query: z.string(),
450+
}),
451+
),
452+
async (c) => {
453+
const body = c.req.valid("json")
454+
const app = App.info()
455+
const result = await Fzf.search(app.path.cwd, body.query)
456+
return c.json(result)
457+
},
458+
)
430459

431460
return result
432461
}

packages/opencode/src/tool/grep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from "zod"
22
import { Tool } from "./tool"
33
import { App } from "../app/app"
4-
import { Ripgrep } from "../ripgrep"
4+
import { Ripgrep } from "../external/ripgrep"
55

66
import DESCRIPTION from "./grep.txt"
77

packages/opencode/src/util/lazy.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function lazy<T>(fn: () => T) {
2+
let value: T | undefined
3+
let loaded = false
4+
5+
return (): T => {
6+
if (loaded) return value as T
7+
value = fn()
8+
return value as T
9+
}
10+
}
11+

0 commit comments

Comments
 (0)