feat(opencode): fff search tools (#27802)

Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>
This commit is contained in:
Dmitriy Kovalenko
2026-06-06 19:12:31 +05:30
committed by GitHub
co-authored by Shoubhit Dash
parent 4814ab3a3d
commit 7d3d80f840
28 changed files with 1256 additions and 138 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ import { EOL } from "os"
import { Effect } from "effect"
import { FileSystem } from "@opencode-ai/core/filesystem"
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 { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
import { effectCmd } from "../../effect-cmd"
import { cmd } from "../cmd"
@@ -68,7 +68,7 @@ const FileTreeCommand = effectCmd({
default: process.cwd(),
}),
handler: Effect.fn("Cli.debug.file.tree")(function* (args) {
const tree = yield* Effect.orDie(Ripgrep.Service.use((svc) => svc.tree({ cwd: args.dir, limit: 200 })))
const tree = yield* Effect.orDie(Search.Service.use((svc) => svc.tree({ cwd: args.dir, limit: 200 })))
console.log(JSON.stringify(tree, null, 2))
}),
})
@@ -1,6 +1,6 @@
import { EOL } from "os"
import { Effect, Stream } from "effect"
import { Ripgrep } from "@opencode-ai/core/filesystem/ripgrep"
import { Search } from "@opencode-ai/core/filesystem/search"
import { effectCmd } from "../../effect-cmd"
import { cmd } from "../cmd"
import { InstanceRef } from "@/effect/instance-ref"
@@ -22,7 +22,7 @@ const TreeCommand = effectCmd({
handler: Effect.fn("Cli.debug.rg.tree")(function* (args) {
const ctx = yield* InstanceRef
if (!ctx) return
const tree = yield* Effect.orDie(Ripgrep.Service.use((svc) => svc.tree({ cwd: ctx.directory, limit: args.limit })))
const tree = yield* Effect.orDie(Search.Service.use((svc) => svc.tree({ cwd: ctx.directory, limit: args.limit })))
process.stdout.write(tree + EOL)
}),
})
@@ -47,8 +47,8 @@ const FilesCommand = effectCmd({
handler: Effect.fn("Cli.debug.rg.files")(function* (args) {
const ctx = yield* InstanceRef
if (!ctx) return
const rg = yield* Ripgrep.Service
const files = yield* rg
const search = yield* Search.Service
const files = yield* search
.files({
cwd: ctx.directory,
glob: args.glob ? [args.glob] : undefined,
@@ -85,7 +85,7 @@ const SearchCommand = effectCmd({
const ctx = yield* InstanceRef
if (!ctx) return
const results = yield* Effect.orDie(
Ripgrep.Service.use((svc) =>
Search.Service.use((svc) =>
svc.search({
cwd: ctx.directory,
pattern: args.pattern,
@@ -345,21 +345,12 @@ export function Autocomplete(props: {
const options: AutocompleteOption[] = []
// Add file options
// Add file options. Trust the order returned by fff (frecency, fuzzy
// score, filename bonus, etc. are already factored in).
if (!result.error && result.data) {
const sortedFiles = result.data.sort((a, b) => {
const aScore = frecency.getFrecency(a)
const bScore = frecency.getFrecency(b)
if (aScore !== bScore) return bScore - aScore
const aDepth = a.split("/").length
const bDepth = b.split("/").length
if (aDepth !== bDepth) return aDepth - bDepth
return a.localeCompare(b)
})
const width = props.anchor().width - 4
options.push(
...sortedFiles.map((item): AutocompleteOption => {
...result.data.map((item): AutocompleteOption => {
const { filename, url, part } = createFilePart(item, lineRange)
const isDir = item.endsWith("/")
@@ -506,45 +497,49 @@ export function Autocomplete(props: {
const agentsValue = agents()
const referenceAliasesValue = referenceAliases()
const commandsValue = commands()
const mixed: AutocompleteOption[] =
store.visible === "@"
? referenceMatchValue
? referenceAliasesValue.filter((item) => item.display === `@${referenceMatchValue.name}`)
: [...referenceAliasesValue, ...agentsValue, ...(filesValue || []), ...mcpResources()]
: [...commandsValue]
const searchValue = search()
// @<alias>/... — narrow to the matched reference, files come from fff
// already ranked so there is no re-ranking here.
if (store.visible === "@" && referenceMatchValue) {
return referenceAliasesValue.filter((item) => item.display === `@${referenceMatchValue.name}`)
}
// Files come from fff already fuzzy ranked and filtered
// it shouldn't be additionally sorted by fuzzysort as it will loose the results
const fileOptions: AutocompleteOption[] = store.visible === "@" ? filesValue || [] : []
const nonFileOptions: AutocompleteOption[] =
store.visible === "@" ? [...referenceAliasesValue, ...agentsValue, ...mcpResources()] : [...commandsValue]
if (!searchValue) {
return mixed
return [...nonFileOptions, ...fileOptions]
}
if (files.loading && prev && prev.length > 0) {
return prev
}
if (referenceMatchValue) return mixed
const fuzziedNonFiles = fuzzysort
.go(removeLineRange(searchValue), nonFileOptions, {
keys: [
(obj) => removeLineRange((obj.value ?? obj.display).trimEnd()),
"description",
(obj) => obj.aliases?.join(" ") ?? "",
],
limit: 10,
scoreFn: (objResults) => {
const displayResult = objResults[0]
let score = objResults.score
if (displayResult && displayResult.target.startsWith(store.visible + searchValue)) {
score *= 2
}
const frecencyScore = objResults.obj.path ? frecency.getFrecency(objResults.obj.path) : 0
return score * (1 + frecencyScore)
},
})
.map((arr) => arr.obj)
const result = fuzzysort.go(removeLineRange(searchValue), mixed, {
keys: [
(obj) => removeLineRange((obj.value ?? obj.display).trimEnd()),
"description",
(obj) => obj.aliases?.join(" ") ?? "",
],
limit: 10,
scoreFn: (objResults) => {
const displayResult = objResults[0]
let score = objResults.score
if (displayResult && displayResult.target.startsWith(store.visible + searchValue)) {
score *= 2
}
const frecencyScore = objResults.obj.path ? frecency.getFrecency(objResults.obj.path) : 0
return score * (1 + frecencyScore)
},
})
return result.map((arr) => arr.obj)
return [...fuzziedNonFiles, ...fileOptions].slice(0, 10)
})
createEffect(() => {