fix: speed up fff file search (#31366)
This commit is contained in:
parent
1772e8ee6e
commit
b1a6c40ad0
7 changed files with 68 additions and 39 deletions
|
|
@ -368,19 +368,6 @@ export function createPromptState(input: PromptInput): PromptState {
|
|||
const next = extractLineRange(value)
|
||||
const list = await input.findFiles(next.base)
|
||||
return list
|
||||
.sort((a, b) => {
|
||||
const dir = Number(b.endsWith("/")) - Number(a.endsWith("/"))
|
||||
if (dir !== 0) {
|
||||
return dir
|
||||
}
|
||||
|
||||
const depth = a.split("/").length - b.split("/").length
|
||||
if (depth !== 0) {
|
||||
return depth
|
||||
}
|
||||
|
||||
return a.localeCompare(b)
|
||||
})
|
||||
.map((item): Auto => {
|
||||
const url = pathToFileURL(path.resolve(input.directory, item))
|
||||
let filename = item
|
||||
|
|
@ -472,8 +459,21 @@ export function createPromptState(input: PromptInput): PromptState {
|
|||
return mixed
|
||||
}
|
||||
|
||||
const next = removeLineRange(query())
|
||||
if (mode() === "mention") {
|
||||
return [
|
||||
...fuzzysort
|
||||
.go(next, agents(), { keys: ["value", "display", "description"] })
|
||||
.map((item) => item.obj),
|
||||
...files(),
|
||||
...fuzzysort
|
||||
.go(next, resources(), { keys: ["value", "display", "description"] })
|
||||
.map((item) => item.obj),
|
||||
]
|
||||
}
|
||||
|
||||
return fuzzysort
|
||||
.go(removeLineRange(query()), mixed, {
|
||||
.go(next, mixed, {
|
||||
keys: [(item) => (item.kind === "mention" ? item.value : item.name).trimEnd(), "display", "description"],
|
||||
})
|
||||
.map((item) => item.obj)
|
||||
|
|
|
|||
|
|
@ -4,12 +4,15 @@ import { LocationServiceMap } from "@opencode-ai/core/location-layer"
|
|||
import { Ripgrep } from "@opencode-ai/core/filesystem/ripgrep"
|
||||
import { Search } from "@opencode-ai/core/filesystem/search"
|
||||
import { FSUtil } from "@opencode-ai/core/fs-util"
|
||||
import { Log } from "@opencode-ai/core/util/log"
|
||||
import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
|
||||
import { Effect, Layer } from "effect"
|
||||
import path from "path"
|
||||
import { HttpApiBuilder } from "effect/unstable/httpapi"
|
||||
import { InstanceHttpApi } from "../api"
|
||||
|
||||
const log = Log.create({ service: "server.file" })
|
||||
|
||||
export const fileHandlers = HttpApiBuilder.group(InstanceHttpApi, "file", (handlers) =>
|
||||
Effect.gen(function* () {
|
||||
const ripgrep = yield* Ripgrep.Service
|
||||
|
|
@ -34,11 +37,23 @@ export const fileHandlers = HttpApiBuilder.group(InstanceHttpApi, "file", (handl
|
|||
const directory = (yield* InstanceState.context).directory
|
||||
const limit = ctx.query.limit ?? 10
|
||||
const kind = ctx.query.type ?? (ctx.query.dirs === "false" ? "file" : "all")
|
||||
const started = performance.now()
|
||||
// Prefer fff (frecency + fuzzy ranking) and trust its ordering. Fall back
|
||||
// to the ripgrep-backed FileSystem.find when fff is unavailable.
|
||||
const fff = yield* search.file({ cwd: directory, query: ctx.query.query, limit, kind }).pipe(Effect.orDie)
|
||||
if (fff !== undefined) return fff
|
||||
return (yield* filesystem(
|
||||
if (fff !== undefined) {
|
||||
log.info("find file", {
|
||||
engine: "fff",
|
||||
query: ctx.query.query,
|
||||
kind,
|
||||
directory,
|
||||
limit,
|
||||
results: fff.length,
|
||||
duration: Math.round(performance.now() - started),
|
||||
})
|
||||
return fff
|
||||
}
|
||||
const fallback = (yield* filesystem(
|
||||
FileSystem.Service.use((fs) =>
|
||||
fs.find({
|
||||
query: ctx.query.query,
|
||||
|
|
@ -47,6 +62,16 @@ export const fileHandlers = HttpApiBuilder.group(InstanceHttpApi, "file", (handl
|
|||
}),
|
||||
),
|
||||
)).map((item) => item.path)
|
||||
log.info("find file", {
|
||||
engine: "ripgrep",
|
||||
query: ctx.query.query,
|
||||
kind,
|
||||
directory,
|
||||
limit,
|
||||
results: fallback.length,
|
||||
duration: Math.round(performance.now() - started),
|
||||
})
|
||||
return fallback
|
||||
})
|
||||
|
||||
const findSymbol = Effect.fn("FileHttpApi.findSymbol")(function* () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue