@hasna/conversations 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/hook.js CHANGED
@@ -191,6 +191,43 @@ function getDb() {
191
191
  if (!colNames2.includes("attachments")) {
192
192
  db.exec("ALTER TABLE messages ADD COLUMN attachments TEXT");
193
193
  }
194
+ if (!colNames2.includes("reply_to")) {
195
+ db.exec("ALTER TABLE messages ADD COLUMN reply_to INTEGER REFERENCES messages(id)");
196
+ db.exec("CREATE INDEX IF NOT EXISTS idx_messages_reply_to ON messages(reply_to)");
197
+ }
198
+ const ftsExists = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='messages_fts'").get();
199
+ if (!ftsExists) {
200
+ db.exec(`
201
+ CREATE VIRTUAL TABLE messages_fts USING fts5(
202
+ content, from_agent, to_agent, space,
203
+ content_rowid='id', content='messages'
204
+ )
205
+ `);
206
+ db.exec(`
207
+ INSERT INTO messages_fts(rowid, content, from_agent, to_agent, space)
208
+ SELECT id, content, from_agent, to_agent, space FROM messages
209
+ `);
210
+ db.exec(`
211
+ CREATE TRIGGER IF NOT EXISTS messages_fts_insert AFTER INSERT ON messages BEGIN
212
+ INSERT INTO messages_fts(rowid, content, from_agent, to_agent, space)
213
+ VALUES (new.id, new.content, new.from_agent, new.to_agent, new.space);
214
+ END
215
+ `);
216
+ db.exec(`
217
+ CREATE TRIGGER IF NOT EXISTS messages_fts_delete AFTER DELETE ON messages BEGIN
218
+ INSERT INTO messages_fts(messages_fts, rowid, content, from_agent, to_agent, space)
219
+ VALUES ('delete', old.id, old.content, old.from_agent, old.to_agent, old.space);
220
+ END
221
+ `);
222
+ db.exec(`
223
+ CREATE TRIGGER IF NOT EXISTS messages_fts_update AFTER UPDATE OF content ON messages BEGIN
224
+ INSERT INTO messages_fts(messages_fts, rowid, content, from_agent, to_agent, space)
225
+ VALUES ('delete', old.id, old.content, old.from_agent, old.to_agent, old.space);
226
+ INSERT INTO messages_fts(rowid, content, from_agent, to_agent, space)
227
+ VALUES (new.id, new.content, new.from_agent, new.to_agent, new.space);
228
+ END
229
+ `);
230
+ }
194
231
  return db;
195
232
  }
196
233
  function closeDb() {