Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

069_ThreadMessageSearch.ts

90 lines3.6 KB

1import * as Effect from "effect/Effect";
2import * as Predicate from "effect/Predicate";
3import * as SqlClient from "effect/sql/SqlClient";
4​
5import {
6 indexedMessageCondition,
7 indexedMessageText,
8} from "../../orchestration-v2/messageSearchIndex.ts";
9​
10/**
11 * Trigrams match 3+ character substrings without a scan, ignoring case and accents.
12 * Generated user-role notices would flood results and appear as "You".
13 * VACUUM can change rowids, so `t3 doctor` must rebuild this contentless index afterward.
14 * Index only finished messages, folding apostrophes as queries do; scan if the index is missing.
15 */
16export default Effect.gen(function* () {
17 const sql = yield* SqlClient.SqlClient;
18​
19 // Older system SQLite builds may lack these FTS5 options; search can scan messages instead.
20 // Only SQLITE_ERROR signals unsupported options. Disk and I/O failures must fail the migration
21 // so the next startup retries creating the index.
22 const created = yield* sql`
23 CREATE VIRTUAL TABLE orchestration_v2_message_search USING fts5(
24 text,
25 content = '',
26 contentless_delete = 1,
27 tokenize = 'trigram remove_diacritics 1'
28 )
29 `.pipe(
30 Effect.as(true),
31 Effect.catchIf(
32 (cause) =>
33 Predicate.hasProperty(cause.reason.cause, "errcode") && cause.reason.cause.errcode === 1,
34 (cause) =>
35 Effect.logWarning(
36 "Message search index unavailable; search will scan messages.",
37 cause,
38 ).pipe(Effect.as(false)),
39 ),
40 );
41 if (!created) return;
42 yield* sql`
43 CREATE TRIGGER orchestration_v2_message_search_insert
44 AFTER INSERT ON orchestration_v2_projection_messages
45 WHEN ${indexedMessageCondition(sql, "new")}
46 BEGIN
47 INSERT INTO orchestration_v2_message_search (rowid, text)
48 SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")}
49 WHERE json_extract(new.payload_json, '$.text') IS NOT NULL;
50 END
51 `;
52 // Streaming deltas rewrite the row many times a second; skip them until the
53 // message settles. Upserts rewrite every column, so a finished message
54 // re-indexes only when what the index reads changed. Deleting a rowid the
55 // index never held is a no-op.
56 yield* sql`
57 CREATE TRIGGER orchestration_v2_message_search_update
58 AFTER UPDATE OF payload_json, streaming, role ON orchestration_v2_projection_messages
59 WHEN (old.streaming = 0 OR new.streaming = 0)
60 AND (
61 old.streaming IS NOT new.streaming
62 OR old.role IS NOT new.role
63 OR json_extract(old.payload_json, '$.text') IS NOT json_extract(new.payload_json, '$.text')
64 OR json_extract(old.payload_json, '$.createdBy')
65 IS NOT json_extract(new.payload_json, '$.createdBy')
66 )
67 BEGIN
68 DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid AND old.streaming = 0;
69 INSERT INTO orchestration_v2_message_search (rowid, text)
70 SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")}
71 WHERE ${indexedMessageCondition(sql, "new")}
72 AND json_extract(new.payload_json, '$.text') IS NOT NULL;
73 END
74 `;
75 yield* sql`
76 CREATE TRIGGER orchestration_v2_message_search_delete
77 AFTER DELETE ON orchestration_v2_projection_messages
78 WHEN old.streaming = 0
79 BEGIN
80 DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid;
81 END
82 `;
83 yield* sql`
84 INSERT INTO orchestration_v2_message_search (rowid, text)
85 SELECT messages.rowid, ${indexedMessageText(sql, "messages.payload_json")}
86 FROM orchestration_v2_projection_messages AS messages
87 WHERE ${indexedMessageCondition(sql)}
88 AND json_extract(messages.payload_json, '$.text') IS NOT NULL
89 `;
90});