Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 3.68 KB

local-folder-search.md

File metadata and controls

55 lines (42 loc) · 3.68 KB

Local Folder Search

Co-do can build an opt-in, on-device search index of the folder you have opened, so both you (Ctrl/⌘+K) and the agent (search_folder_semantic) can find code and documents by meaning and keywords.

Privacy model

  • Off by default. Nothing is indexed until you press Ctrl/⌘+K → Enable local index.
  • Never uploaded. Chunking, embedding and querying all happen in the page. The index lives in IndexedDB (co-do-db → folder-index store) on this device, scoped per workspace.
  • No model download. The default embedding is a deterministic hashed TF + character-trigram vector (512 dims), so it works offline straight away.
  • Permission-gated for the agent. search_folder_semantic goes through the normal tool permission system (default Ask Each Time, under Tool Permissions → Local Search). Only the returned snippets (paths, line ranges and up to ~300 characters per hit) are shared with your AI provider, the same as grep or cat output.
  • Deletable. Delete index & disable removes every stored chunk for the current folder and switches the feature off.

How it works

flowchart LR
  Files["Opened folder<br/>(File System Access)"] --> Filter["Filter<br/>skip node_modules, .git, dist,<br/>binaries, lockfiles, &gt;256 KB"]
  Filter --> Chunk["40-line chunks<br/>10-line overlap"]
  Chunk --> Embed["EmbeddingProvider<br/>(local TF + trigram)"]
  Embed --> Store[("VectorDB<br/>IndexedDB folder-index")]
  Query["Query"] --> QEmbed["Embed query"] --> Store
  Store --> Rerank["Hybrid rerank<br/>0.65 cosine + 0.35 keyword<br/>max 2 hits per file"]
  Rerank --> Hits["path · lines · snippet · score"]
Loading
  • Incremental refresh. Each chunk records its file's size:lastModified signature and the embedding provider id. Rebuilds only re-read changed files, drop deleted ones, and re-embed everything if the provider changes. The agent tool refreshes the index automatically at most every 30 s.
  • Tokenisation splits on punctuation and camelCase/PascalCase boundaries, so saveApiKey matches "api key".
  • Ranking combines cosine similarity (which captures sub-word overlap through trigrams) with the fraction of query terms found in the chunk or its path.

Code map

Module Purpose
src/folder-search/embedding.ts EmbeddingProvider interface, tokenizer, LocalHashedEmbeddingProvider, cosine helpers
src/folder-search/vector-store.ts VectorDB with an idb-vector-style contract (insert / update / delete / query(vector, { limit }) → { object, key, similarity }) and pluggable backends
src/folder-search/folder-index.ts Chunking, filtering, incremental build(), hybrid search() (environment-agnostic, unit-tested)
src/folder-search/service.ts Browser glue: File System Access source, IndexedDB backend, opt-in flag, workspace scoping
src/folder-search-ui.ts Ctrl/⌘+K panel (combobox + listbox, arrow-key navigation, DOM/textContent rendering)
src/tools.ts → search_folder_semantic Agent tool

Plugging in a real embedding model

folderSearchService.setEmbeddingProvider(provider) accepts anything implementing:

interface EmbeddingProvider {
  readonly id: string;          // persisted; changing it triggers a re-embed
  readonly dimensions: number;
  embed(texts: string[]): Promise<Float32Array[]>;
}

For example a WebGPU/WASM sentence-transformer or a browser built-in AI embedding API. The index, ranking, storage and tool stay unchanged.

Co-do ships a small (~150-line) store that matches the idb-vector contract instead of adding a dependency. Swapping in the package later only means implementing VectorBackend on top of it.