Files
cobble/src/functions/backend/db/harness/grab-harness-setting.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

150 lines
4.4 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_BOT_HARNESS_SETTINGS,
BUN_SQLITE_COBBLE_HARNESS_SETTINGS,
BUN_SQLITE_COBBLE_WORKSPACE_HARNESS_SETTINGS,
} from "@/db/types/db";
import { isInstallWideHarness, type HarnessKind } from "@/src/dict/harness-dict";
import parseJobJson from "@/src/bots-queue/parse-job-json";
import grabDefaultHarnessProviderId from "./grab-default-harness-provider-id";
export type HarnessSetting = {
harness: HarnessKind;
/**
* The picked `harness_models` row; undefined picks automatically.
*/
model_id?: number;
/**
* The `harness_providers` row the harness is limited to (its pick
* and its automatic picks); undefined: any provider.
*/
provider_id?: number;
fallback: boolean;
enabled: boolean;
options: Record<string, unknown>;
/**
* `workspace` when the call's workspace sets this harness, `bot` when a
* worker bot overrides the default for it.
*/
source: "default" | "bot" | "workspace";
};
type Params = {
harness: HarnessKind;
/**
* The bot the call is for. A worker bot's own setting wins over the
* default; the coordinator always uses the default.
*/
bot_id?: number | string;
/**
* The workspace the call is in. Its setting wins over the bot's, for
* the coordinator too.
*/
workspace_id?: number | string | null;
};
type SettingRow = Pick<
BUN_SQLITE_COBBLE_HARNESS_SETTINGS,
"model_id" | "provider_id" | "fallback" | "enabled" | "options"
>;
/**
* A harness's setting for a call: the workspace's, else the bot's, else
* the default. With no row it is automatic on the default provider
* (OpenRouter), on, with fallback. Text is always on. An install-wide
* harness (embeddings) always takes the default: rows a bot or workspace
* saved before it was one are ignored.
*/
export default async function grabHarnessSetting({
harness,
bot_id,
workspace_id,
}: Params): Promise<HarnessSetting> {
const install_wide = isInstallWideHarness({ harness });
const workspace_row = workspace_id && !install_wide
? await grabWorkspaceOverride({ harness, workspace_id })
: undefined;
if (workspace_row) {
return toSetting({ harness, row: workspace_row, source: "workspace" });
}
const bot_row = bot_id && !install_wide
? await grabBotOverride({ harness, bot_id })
: undefined;
if (bot_row) {
return toSetting({ harness, row: bot_row, source: "bot" });
}
const row = (
await BunSQLite.sql<BUN_SQLITE_COBBLE_HARNESS_SETTINGS>({
sql: `SELECT * FROM harness_settings WHERE harness = ? LIMIT 1`,
values: [harness],
})
).payload?.[0];
if (!row) {
return {
...toSetting({ harness, source: "default" }),
provider_id: await grabDefaultHarnessProviderId(),
};
}
return toSetting({ harness, row, source: "default" });
}
async function grabWorkspaceOverride({
harness,
workspace_id,
}: {
harness: HarnessKind;
workspace_id: number | string;
}) {
return (
await BunSQLite.sql<BUN_SQLITE_COBBLE_WORKSPACE_HARNESS_SETTINGS>({
sql: `SELECT * FROM workspace_harness_settings WHERE workspace_id = ? AND harness = ? LIMIT 1`,
values: [workspace_id, harness],
})
).payload?.[0];
}
async function grabBotOverride({
harness,
bot_id,
}: {
harness: HarnessKind;
bot_id: number | string;
}) {
return (
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_HARNESS_SETTINGS>({
sql: `SELECT s.* FROM bot_harness_settings s
JOIN bots b ON b.id = s.bot_id
WHERE s.bot_id = ? AND s.harness = ? AND b.kind != 'coordinator'
LIMIT 1`,
values: [bot_id, harness],
})
).payload?.[0];
}
function toSetting({
harness,
row,
source,
}: {
harness: HarnessKind;
row?: SettingRow;
source: HarnessSetting["source"];
}): HarnessSetting {
return {
harness,
model_id: typeof row?.model_id == "number" ? row.model_id : undefined,
provider_id: typeof row?.provider_id == "number" ? row.provider_id : undefined,
fallback: row ? row.fallback != 0 : true,
enabled: harness == "text" || !row || row.enabled != 0,
options: parseJobJson({ text: row?.options }) || {},
source,
};
}