Naalya Handbook

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

TableHoldsNotes
knowledge_basetitle, 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_chunkknowledge_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

ActionEffect
CreateEnqueues an embed job on kb-embedding
UpdateEnqueues only if content changed. A changed isPublic is copied straight onto the chunk rows, so publishing needs no re-embed
DeleteSoft-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.

RouteoperationIdNotes
POST /knowledge-basecreateKnowledgeBaseDocumentMissing content stores ''; missing isPublic is false; unknown campus is Campus not found.
GET /knowledge-baselistKnowledgeBaseDocuments
GET /knowledge-base/:idgetKnowledgeBaseDocumentKnowledge base document not found
PATCH /knowledge-base/:idupdateKnowledgeBaseDocumentA null campus makes it school-wide
DELETE /knowledge-base/:iddeleteKnowledgeBaseDocumentKnowledge 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.

KnowledgeBaseService.hybridSearch({ query, publicOnly?, limit?, campusId? }):

StepDetail
GuardBlank query returns []. No school context throws Knowledge base search requires an active school context. Unknown campus throws Campus not found.
Sizelimit 1 to 10, default 5. Each arm fetches limit × 4 rows
Vector armEmbeds the query, orders by embedding <=> query (cosine), with hnsw.iterative_scan = relaxed_order and hnsw.ef_search = 100
Keyword armcontent_tsv @@ websearch_to_tsquery('english', query), ordered by ts_rank_cd (not BM25)
Filters, both armsDoc not deleted; the school; with publicOnly, chunk and doc both public; with campusId, that campus plus school-wide docs
FusionreciprocalRankFusion scores each doc 1 / (60 + rank) and returns the top limit
ResultknowledgeBaseId, title, chunk, campusId, campusName, score

Who calls it

CallerSearchSees
Rover searchKnowledgeBasepublicOnly: false; campus from forCampusId or the pinned campusStaff and published docs. Needs list on KNOWLEDGE_BASE
Applicant searchPublicKnowledgeBasepublicOnly: true; campus from forCampusId or nonePublished docs. Same permission
Social agent RPCInside withSchool(schoolId), publicOnly: true, limit: 5; campus from the request or connectionPublished docs for that campus plus school-wide ones

On this page