A Second Brain That Shows Its Sources

I read in a dozen formats and I forget where I read things. A paragraph I need might be in an EPUB, a PDF, a Wikipedia dump, or one of my own notes. I wanted one place to ask “where did I read about X” and get an actual answer, not a list of files. That’s Grimoire: a desktop app that imports what you read, indexes it twice, and answers questions by pointing back at the books it pulled from.
Getting text out of everything
Import has to turn very different file types into plain text first. EPUB and HTML get their markup stripped, PDFs go through lopdf, and everything lands as one text blob per item, capped at 800,000 characters so a huge file can’t blow up the rest of the pipeline.
The more unusual case is Wikipedia and Kiwix-style ZIM archives, compressed article dumps. I didn’t want a dependency on an external reader, so Grimoire has its own pure-Rust ZIM reader that decompresses zstd and lzma clusters directly. Articles are served through a custom zimres:// scheme, and the handler injects a <base> tag so the relative links inside an article actually resolve.
Two indexes, one file
Once there’s text, Grimoire builds two different indexes, and both live in the same SQLite file.
The first is a FTS5 keyword index with the porter unicode61 tokenizer, built right at import time. That’s what powers the plain library search box, instant and exact.
The second is a semantic index, built on demand since it needs an embedding model and that costs time and maybe money. Before it can run, the text has to be chunked.
Chunking is word-based: windows of 200 words with a 40-word overlap, so each new chunk starts 160 words after the last one began. The overlap matters because an idea that straddles a chunk boundary shouldn’t disappear from both halves.
// 200-word windows; each one shares its first 40 words with the previous chunk
let chunks = embed::chunk_text(&body, 200, 40);
Words are a rough stand-in for tokens, but they’re predictable and need no tokenizer. A small unit test pins the overlap down: the last word of one chunk has to be the 40th word of the next.
Each chunk gets embedded, default is Ollama’s nomic-embed-text on localhost, with presets for OpenAI, Voyage, NVIDIA and custom endpoints.
Vectors in SQLite, no separate database
The vectors are stored with the sqlite-vec extension, as a vec0 virtual table whose rowid matches the chunk’s id in the regular chunks table. It’s statically linked into the binary and registered once per process with sqlite3_auto_extension, so there’s no separate vector database to run, just more tables in the same file.
Switching embedding models is the one delicate part, because a different model usually means a different vector dimension, and you can’t mix 768-dimensional and 1536-dimensional vectors in one vec0 table. So when the dimension changes, Grimoire drops the vector table, clears the chunks, and marks every item for re-indexing, rather than ever comparing incompatible vectors.
Retrieval, and its one awkward corner
A question becomes an embedding, and that embedding runs a KNN search in sqlite-vec using L2 distance, pulling the top 6 chunks. The UI shows a score as 1 / (1 + distance), turning “smaller is better” into a number that reads like a percentage.
SELECT c.item_id, c.text, v.distance
FROM vec_chunks v
JOIN chunks c ON c.id = v.rowid
WHERE v.embedding MATCH ?1 AND k = ?2
ORDER BY v.distance
The awkward corner is scoping a question to a chosen subset of books. sqlite-vec can’t filter by item inside the MATCH clause, so Grimoire over-fetches, 48 neighbors instead of 6, and filters down to the chosen books in Rust afterward. A scoped question can come back with fewer than 6 chunks as a result, but that’s better than padding the answer with chunks from books you didn’t ask about.
Making the model show its work
The 6 passages go into the prompt numbered [1] through [6], each labeled with its book title. The system prompt asks the model to cite those titles, and just as importantly, to say plainly when the passages don’t answer the question, and fall back to general knowledge while flagging that it isn’t from the library.
The UI backs that up visually: every retrieved chunk is its own source card, book title, the first 220 characters, and the score. Click one and it opens the book. Sources are saved alongside the chat, so reopening an old answer shows the same sources it was built from, not a fresh retrieval.
One backend, three request shapes
Grimoire talks to Claude, OpenRouter, DeepSeek, Kimi, GLM and NVIDIA in the cloud, plus Ollama, LM Studio and llama.cpp locally. That sounds like a lot of integrations, but underneath it’s just three request shapes: Anthropic’s, OpenAI-compatible, and Ollama’s. Every provider maps onto one of the three, and Rust streams tokens back to the UI as Tauri events. API keys for chat providers sit in the OS keychain, never in the SQLite file.
The frontend never talks to any of this directly, it talks to one backend object. In a plain browser that object serves mock data with fake latency, so I can build and test UI without Tauri running at all. Inside the app, the same calls go to roughly 60 typed Tauri commands, with serde structs on the Rust side mirrored by camelCase types in TypeScript.
The local-first trade-off, stated plainly
Local-first is a real property here, not a slogan. The library database, extracted text, chunks, vectors, notes and chats all stay on disk. Run Ollama for both embeddings and chat, and nothing leaves your machine.
The moment you choose a cloud model, that changes in a specific, bounded way. With a cloud chat model, each question, the conversation history, and the 6 retrieved passages go to that provider. With a cloud embedder, every chunk of every book you index gets sent at index time. That’s the honest shape of the trade-off: local by default, and exactly as networked as the provider you pick.
A single-instance plugin is registered first, before anything else starts, so launching Grimoire a second time just focuses the existing window. Combined with autostart and close-to-tray, it behaves like something meant to sit in the background permanently, not something you reopen from scratch each time.
What it doesn’t do yet
I’d rather say this directly than let the feature list imply more than it delivers. Citations point at a book title, not a page number or a passage location, so “cited” means “attributed to the right book,” not “footnoted precisely.” The model is asked to cite titles, but nothing checks that it cited the right one. There’s no similarity threshold on retrieval, so the top 6 chunks are always sent even when none of them are actually relevant. Grounding lives entirely in the system prompt, not in any code that refuses to answer when retrieval comes up empty.
The next real improvements are page- or passage-level jumps instead of book-level ones, a relevance threshold so weak matches get dropped instead of forwarded, and hybrid search that combines FTS5 keyword matching with the vector index instead of choosing one per search box. None of that is built yet.
Takeaways
- Put both indexes in one file. Keyword and vector search don’t need separate infrastructure, they need separate tables.
- Test the boring invariant. A unit test that just proves chunk overlap exists is cheap insurance against silent regressions.
- Changing a model’s shape should invalidate old data, not blend with it. Dropping and re-indexing beats quietly mixing vector dimensions.
- Show the sources, not just the answer. A citation nobody can click is just a claim.
- Say what “grounded” actually means. Prompt-level instruction isn’t the same as enforced refusal, and the difference is worth stating out loud.