Knowledge base
Staff-written school docs, embedded in a worker and searched with pgvector plus full-text, fused by reciprocal rank. Used by Rover, applicants and the social agent.
Schools write policies, FAQs and how-tos here. Rover and the public-facing agents answer questions from them. Writes are plain CRUD, embedding happens in the worker, and search reads a vector index and a full-text index together.
The two tables
| Table | Holds | Notes |
|---|---|---|
knowledge_base | title, description, content (Markdown string), isPublic (default false), optional campusId, updatedBy | @TenantScoped. No campus means school-wide. Only isPublic docs reach applicants and the social agent |
knowledge_base_chunk | knowledge_base_id, school_id, is_public, chunk_index, content, embedding vector(1536) (HNSW), content_tsv (generated, GIN) | synchronize: false: TypeORM can't express the vector and tsvector columns, so its schema is in hand-written migrations and migration:generate skips it. See The database |
Writing and embedding
| Action | Effect |
|---|---|
| Create | Enqueues an embed job on kb-embedding |
| Update | Enqueues only if content changed. A changed isPublic is copied straight onto the chunk rows, so publishing needs no re-embed |
| Delete | Soft-deletes the doc, hard-deletes its chunks |
The worker (apps/worker/src/processors/kb-embedding/) loads the doc by the job's schoolId, splits content into chunks of up to 2,000 characters, prefixes each with the title and description so a fragment keeps its context, embeds them with text-embedding-3-small (1,536 dimensions, matching the column), and replaces the doc's chunks in one transaction.
Staff routes
All under /api/v1/knowledge-base, staff only, each needing its action on KNOWLEDGE_BASE. Writes are audited without the content blob.
| Route | operationId | Notes |
|---|---|---|
POST /knowledge-base | createKnowledgeBaseDocument | Missing content stores ''; missing isPublic is false; unknown campus is Campus not found. |
GET /knowledge-base | listKnowledgeBaseDocuments | |
GET /knowledge-base/:id | getKnowledgeBaseDocument | Knowledge base document not found |
PATCH /knowledge-base/:id | updateKnowledgeBaseDocument | A null campus makes it school-wide |
DELETE /knowledge-base/:id | deleteKnowledgeBaseDocument | Knowledge base document deleted successfully |
The service stores content as given; it doesn't check for Markdown or Tiptap. There is no search route for staff; search is only reached through the callers below.
Cards in the Hub
The school knowledge-base list is cards, in src/routes/_authenticated/staff-hub/school/ai-knowledge-base/index.tsx. The title wraps to two lines. The footer shows a globe when the document is public and a lock when it is private, with the accessible name Public or Private, then the campus name when there is one. The editor's avatar sits beside "Updated" and the formatted time, and only when updatedBy is set. Public and campus badges are not in the header. Read in Hub commit 82f0d215b4, which 40067f22 contains. The later Rover-folder and proposal commits do not list this file. The screen was not opened in a browser.
Hybrid search
KnowledgeBaseService.hybridSearch({ query, publicOnly?, limit?, campusId? }):
| Step | Detail |
|---|---|
| Guard | Blank query returns []. No school context throws Knowledge base search requires an active school context. Unknown campus throws Campus not found. |
| Size | limit 1 to 10, default 5. Each arm fetches limit × 4 rows |
| Vector arm | Embeds the query, orders by embedding <=> query (cosine), with hnsw.iterative_scan = relaxed_order and hnsw.ef_search = 100 |
| Keyword arm | content_tsv @@ websearch_to_tsquery('english', query), ordered by ts_rank_cd (not BM25) |
| Filters, both arms | Doc not deleted; the school; with publicOnly, chunk and doc both public; with campusId, that campus plus school-wide docs |
| Fusion | reciprocalRankFusion scores each doc 1 / (60 + rank) and returns the top limit |
| Result | knowledgeBaseId, title, chunk, campusId, campusName, score |
Who calls it
| Caller | Search | Sees |
|---|---|---|
Rover searchKnowledgeBase | publicOnly: false; campus from forCampusId or the pinned campus | Staff and published docs. Needs list on KNOWLEDGE_BASE |
Applicant searchPublicKnowledgeBase | publicOnly: true; campus from forCampusId or none | Published docs. Same permission |
| Social agent RPC | Inside withSchool(schoolId), publicOnly: true, limit: 5; campus from the request or connection | Published docs for that campus plus school-wide ones |