Files
cobble/src/functions/backend/db/knowledgebases/reindex-knowledgebase.ts
T
tbenandClaude Opus 5.5 e7b23b7454 Add knowledgebases
Knowledgebases give bots RAG over documents, scoped like extensions: to
particular bots, to a workspace, or to every bot, with their own button
beside Tools in the composer. Documents are chunked and embedded by the
file-ingest worker with the install's embedding model, which is now an
install-wide setting rather than a per-bot one.

- Word search runs on an FTS5 index ranked by BM25, with a scored LIKE
  fallback for scripts written without spaces.
- Changing the embedding model re-indexes documents automatically.
- Ingest jobs wait without spending attempts while every embedding model
  rests, fail at once on errors retrying can't fix, and keep passages
  searchable by words when the model refuses.
- Settings > Knowledgebases shows each knowledgebase's and document's
  index state: spinner, warning, grey check (words only) or green check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-26 16:41:01 +01:00

33 lines
1.1 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import enqueueDocumentIngest from "@/src/file-ingest/enqueue-document-ingest";
import grabKnowledgebase from "./grab-knowledgebase";
type Params = {
knowledgebase_id: number | string;
};
/**
* Reads every document of a knowledgebase again (after the install's
* embedding model changed, or one failed). Each keeps its old passages
* until its new ones are in. Returns how many were queued.
*/
export default async function reindexKnowledgebase({ knowledgebase_id }: Params) {
const knowledgebase = await grabKnowledgebase({ knowledgebase_id });
const documents = await BunSQLite.sql<{ id: number }>({
sql: `SELECT id FROM knowledgebase_documents WHERE knowledgebase_id = ?`,
values: [knowledgebase.id],
});
await BunSQLite.sql({
sql: `UPDATE knowledgebase_documents SET ingest_error = NULL WHERE knowledgebase_id = ?`,
values: [knowledgebase.id],
});
for (const document of documents.payload || []) {
await enqueueDocumentIngest({ document_id: document.id });
}
return documents.payload?.length || 0;
}