feat(ai-partner): client-side retrieval with sqlite-vec (#1451)#1479
Merged
Conversation
Implements the Amicus retrieval service layer that embeds a user query,
runs sqlite-vec MATCH against scripture.db, and re-ranks with profile +
context boosts.
New modules under app/src/services/amicus/:
- types.ts — shared types (RetrievedChunk, RetrievalContext,
CompressedProfile, AmicusError + AmicusErrorCode)
- embed.ts — calls proxy /ai/embed; retries once on 5xx/network;
translates proxy 401/402 to PROXY_UNAUTHORIZED,
network failure to OFFLINE
- vectorSearch.ts — packVector (little-endian float32), distance→similarity,
searchByVector executing the MATCH join on
embeddings/chunk_text/chunk_metadata. Throws
EXTENSION_NOT_LOADED if sqlite-vec isn't loaded.
- rerank.ts — pure functions: current-chapter ×1.5, preferred
scholars ×1.1, traditions ×1.05, 2-per-scholar
diversity cap, top-10
- retrieval.ts — top-level orchestrator with latency timing
- index.ts — tiny public barrel
30 Jest tests (4 suites) across the pipeline:
- rerank: boosts, diversity, ordering
- vectorSearch: packing, similarity, error on extension missing
- embed: shape validation, retry, 401/402, offline, timeout
- retrieval: end-to-end with fake fetch + mock DB
All 3213 app tests still pass; tsc --noEmit + eslint 0 warnings.
https://claude.ai/code/session_01Pht3kzgdvkn81DDfL9SnFe
Test Results✅ All tests passed
Coverage
⏱️ Duration: 61.0s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1451. Depends conceptually on #1448 and #1450.
Summary
app/src/services/amicus/service layer implementing the client retrieval pipeline defined in the plan §6.embedQuery()(proxy) →searchByVector()(sqlite-vec MATCH) →rerank()(boosts + diversity) → top-10.AmicusError.EMBED_FAILED | OFFLINE | EXTENSION_NOT_LOADED | PROXY_UNAUTHORIZED) so the UI can choose graceful fallback.Modules
types.ts—RetrievedChunk,RetrievalContext,CompressedProfile,ChapterRef,AmicusErrorembed.ts— single-retry, 5-s timeout, translates proxy status to typed errorsvectorSearch.ts— little-endian float32 pack, similarity =1/(1+distance), join tochunk_text+chunk_metadatarerank.ts— pure boosts: current chapter ×1.5, scholar ×1.1, tradition ×1.05; 2-per-scholar diversity cap; top 10retrieval.ts— orchestrator with per-phase latency timingindex.ts— public barrelTest plan
npx tsc --noEmit— 0 errorsnpx eslint src/services/amicus/— 0 warningsnpx jest --testPathPattern=amicus— 30 tests pass across rerank / vectorSearch / embed / retrievalanytypes; strict mode passesOut of scope
CompressedProfilethis code consumes./ai/chat.https://claude.ai/code/session_01Pht3kzgdvkn81DDfL9SnFe