Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-file-mentions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

Keep file mention suggestions current and scoped to the active workspace while preserving instant cached results.
55 changes: 34 additions & 21 deletions packages/kilo-vscode/src/kilo-provider/file-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path"
import * as vscode from "vscode"
import type { KiloClient } from "@kilocode/sdk/v2/client"
import { mergeFileSearchResults } from "./file-search-results"
import { mergeFileSearchItems } from "./file-search-items"
import { mergeFileSearchItems, type FileSearchItem } from "./file-search-items"

type Message = {
query: string
Expand All @@ -20,6 +20,35 @@ type Input = {
post: (message: unknown) => void
}

async function fetchBackend(client: KiloClient, dir: string, query: string): Promise<[string[], string[]]> {
if (!client?.find?.files) return [[], []]
const [fileRes, folderRes] = await Promise.allSettled([
client.find.files({ query, directory: dir, type: "file", limit: 50 }, { throwOnError: true }),
client.find.files({ query, directory: dir, type: "directory", limit: 50 }, { throwOnError: true }),
])
return [settled(fileRes, "file"), settled(folderRes, "folder")]
}

function assemble(
query: string,
dir: string,
files: string[],
folders: string[],
open: Set<string>,
): { paths: string[]; items: FileSearchItem[] } {
const uri = vscode.window.activeTextEditor?.document.uri
const rel = uri?.scheme === "file" && dir ? path.relative(dir, uri.fsPath) : undefined
const active = rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? rel.replaceAll("\\", "/") : undefined
const paths = mergeFileSearchResults({ query, backend: files, open, active })
const items = mergeFileSearchItems({
query,
files: paths,
folders,
open: new Set(active ? [active, ...open] : open),
})
return { paths, items }
}

export async function handleFileSearch(input: Input): Promise<void> {
const client = input.client
if (!client) {
Expand All @@ -29,27 +58,11 @@ export async function handleFileSearch(input: Input): Promise<void> {

const id = input.message.sessionID ?? input.current ?? input.context
const dir = input.dir(id)
const open = dir ? await input.open(dir) : new Set<string>()

const query = input.message.query
void Promise.allSettled([
client.find.files({ query, directory: dir, type: "file", limit: 50 }, { throwOnError: true }),
client.find.files({ query, directory: dir, type: "directory", limit: 50 }, { throwOnError: true }),
]).then(([fileRes, folderRes]) => {
const files = settled(fileRes, "file")
const folders = settled(folderRes, "folder")
const uri = vscode.window.activeTextEditor?.document.uri
const rel = uri?.scheme === "file" && dir ? path.relative(dir, uri.fsPath) : undefined
const active = rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? rel.replaceAll("\\", "/") : undefined
const result = mergeFileSearchResults({ query, backend: files, open, active })
const items = mergeFileSearchItems({
query,
files: result,
folders,
open: new Set(active ? [active, ...open] : open),
})
input.post({ type: "fileSearchResult", paths: result, items, dir, requestId: input.message.requestId })
})
const [files, folders] = await fetchBackend(client, dir, query)
const open = dir ? await input.open(dir) : new Set<string>()
const { paths, items } = assemble(query, dir, files, folders, open)
input.post({ type: "fileSearchResult", paths, items, dir, requestId: input.message.requestId })
}

function settled(result: PromiseSettledResult<{ data: string[] }>, kind: "file" | "folder"): string[] {
Expand Down
74 changes: 74 additions & 0 deletions packages/kilo-vscode/tests/unit/file-search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from "bun:test"
import { handleFileSearch } from "../../src/kilo-provider/file-search"

type Query = { query: string; directory: string; type: "file" | "directory"; limit: number }

function client(data: { files: string[]; folders: string[] }) {
const calls: Query[] = []
return {
calls,
value: {
find: {
files: async (query: Query) => {
calls.push(query)
return { data: query.type === "file" ? data.files : data.folders }
},
},
},
}
}

describe("handleFileSearch", () => {
it("posts one fresh response for each request", async () => {
const api = client({ files: ["src/a.ts"], folders: ["src"] })
const posted: unknown[] = []

await handleFileSearch({
client: api.value as never,
message: { query: "", requestId: "request-1", sessionID: "session-1" },
dir: (id) => (id === "session-1" ? "/repo" : ""),
open: async () => new Set(["src/open.ts"]),
post: (message) => posted.push(message),
})

expect(api.calls).toEqual([
{ query: "", directory: "/repo", type: "file", limit: 50 },
{ query: "", directory: "/repo", type: "directory", limit: 50 },
])
expect(posted).toHaveLength(1)
expect(posted[0]).toEqual({
type: "fileSearchResult",
requestId: "request-1",
dir: "/repo",
paths: ["src/open.ts", "src/a.ts"],
items: [
{ path: "src/open.ts", type: "opened-file" },
{ path: "src/a.ts", type: "file" },
{ path: "src", type: "folder" },
],
})
})

it("returns an empty fresh response when files were deleted", async () => {
const api = client({ files: [], folders: [] })
const posted: unknown[] = []

await handleFileSearch({
client: api.value as never,
message: { query: "", requestId: "request-empty" },
dir: () => "/repo",
open: async () => new Set(),
post: (message) => posted.push(message),
})

expect(posted).toEqual([
{
type: "fileSearchResult",
requestId: "request-empty",
dir: "/repo",
paths: [],
items: [],
},
])
})
})
Loading
Loading