Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

refactor: 拆出 Bot 文本索引 SQL

Zhehui Bi285812c1a61d
diff --git a/server/bot-library.ts b/server/bot-library.ts
index b0cdf89dfa..d266e2d316 100644
--- a/server/bot-library.ts
+++ b/server/bot-library.ts
@@ -4,7 +4,8 @@
import { fromMarkdown } from "mdast-util-from-markdown";
import { BOT_LIBRARY_MAX, type BotLibrary, type BotLibraryApp, type BotLibraryFileMessage, type BotLibraryPage } from "../shared/bot-library.ts";
import { windowsPathDestinations } from "../shared/markdown-windows-paths.ts";
-import { LIBRARY_FILE_REPLY_FILTER, prepared } from "./message-db.ts";
+import { prepared } from "./message-db.ts";
+import { LIBRARY_FILE_REPLY_FILTER } from "./message-db-bot-text-indexes.ts";
import type { PublishedPageRecord } from "./pages/store.ts";
/** Replies read for file links, newest first; most replies share nothing, so this reads well past the files shown. */
diff --git a/server/message-db-bot-text-indexes.ts b/server/message-db-bot-text-indexes.ts
new file mode 100644
index 0000000000..8cbdc46c5a
--- /dev/null
+++ b/server/message-db-bot-text-indexes.ts
@@ -0,0 +1,6 @@
+export const LIBRARY_FILE_REPLY_FILTER = `role = 'bot' AND kind = 'text'
+ AND (instr(text, '](') > 0 OR instr(text, ']:') > 0 OR instr(text, '<file:') > 0 OR json_extract(json, '$.attachments') IS NOT NULL)`;
+
+export const BOT_TEXT_MESSAGE_INDEXES_SQL = `
+ CREATE INDEX IF NOT EXISTS messages_thread_bot_text ON messages(thread_id, at) WHERE kind = 'text' AND role = 'bot' AND text IS NOT NULL;
+ CREATE INDEX IF NOT EXISTS messages_library_file_replies ON messages(thread_id, role) WHERE ${LIBRARY_FILE_REPLY_FILTER};`;
diff --git a/server/message-db.ts b/server/message-db.ts
index 455eee0c74..aab59dec8d 100644
--- a/server/message-db.ts
+++ b/server/message-db.ts
@@ -8,6 +8,7 @@ import { peerProvenanceAuthor, type WorkOrigin } from "./peer-provenance.ts";
import type { ResolvedSender, SteerQueueReason } from "../shared/wire.ts";
import type { Message } from "./store.ts";
import type { UsageTrigger } from "./usage-ledger.ts";
+import { BOT_TEXT_MESSAGE_INDEXES_SQL } from "./message-db-bot-text-indexes.ts";
import { ensureMemoryIndex } from "./message-db-memory-index.ts";
import { cjkSearchText, cjkTokenParts, ensureSearchIndex, ftsPhrase, hasCjk, withoutSearchMarks } from "./cjk-search.ts";
import { attentionSelect } from "./message-db-attention.ts";
@@ -22,8 +23,6 @@ const DB_FILE = () => join(DATA_DIR, "messages.db");
const WORK_KINDS = "kind IN ('activity', 'digest', 'options')";
const CARD_KINDS = "kind IN ('secret', 'signin', 'connector')";
const FAILED_CALL = "kind = 'activity' AND instr(json, '\"ok\":false') > 0";
-export const LIBRARY_FILE_REPLY_FILTER = `role = 'bot' AND kind = 'text'
- AND (instr(text, '](') > 0 OR instr(text, ']:') > 0 OR instr(text, '<file:') > 0 OR json_extract(json, '$.attachments') IS NOT NULL)`;
/** The rows that order the thread list and the time each one counts at. SQLite uses messages_thread_order only for a
* statement whose WHERE and MAX repeat these texts exactly. */
const ORDERING_ROW = "((role = 'user' AND json_extract(json, '$.peerAsk') IS NULL) OR (role = 'bot' AND kind = 'text' AND (json_extract(json, '$.turnTerminal') = 1 OR json_extract(json, '$.turnId') IS NULL)))";
@@ -64,8 +63,7 @@ function open(): DatabaseSync {
-- The primary key has no time column, so newest-per-thread reads need this index.
CREATE INDEX IF NOT EXISTS messages_thread_at ON messages(thread_id, at);
-- Partial, so no other query's plan can move onto them: only a statement that repeats the WHERE may use one.
- CREATE INDEX IF NOT EXISTS messages_thread_bot_text ON messages(thread_id, at) WHERE kind = 'text' AND role = 'bot' AND text IS NOT NULL;
- CREATE INDEX IF NOT EXISTS messages_library_file_replies ON messages(thread_id, role) WHERE ${LIBRARY_FILE_REPLY_FILTER};
+ ${BOT_TEXT_MESSAGE_INDEXES_SQL}
CREATE INDEX IF NOT EXISTS messages_thread_order ON messages(thread_id, ${ORDERING_AT}) WHERE ${ORDERING_ROW};
CREATE INDEX IF NOT EXISTS messages_work_at ON messages(kind, at) WHERE ${WORK_KINDS};
CREATE INDEX IF NOT EXISTS messages_cards_at ON messages(kind, at) WHERE ${CARD_KINDS};