@wirecat/cli-core 0.687.0 → 0.689.0

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.
Files changed (99) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/dist/background/daily-archive-timer.d.ts +1 -1
  3. package/dist/cli/documents/index.d.ts +1 -0
  4. package/dist/cli/documents/index.js +1 -0
  5. package/dist/cli/documents/library.d.ts +107 -0
  6. package/dist/cli/documents/library.js +514 -0
  7. package/dist/cli/documents/testing/source-contract.d.ts +11 -6
  8. package/dist/cli/documents/testing/source-contract.js +54 -23
  9. package/dist/cli/documents/testing/source.d.ts +11 -3
  10. package/dist/cli/documents/testing/source.js +15 -10
  11. package/dist/cli/documents/words.d.ts +1 -1
  12. package/dist/cli/envelope.js +2 -3
  13. package/dist/cli/meeting-batch.js +5 -1
  14. package/dist/cli/meeting-passage-text.d.ts +1 -1
  15. package/dist/cli/meeting-passage-text.js +3 -5
  16. package/dist/cli/messenger/conversations-command.js +4 -0
  17. package/dist/cli/messenger/messages-search-command.js +6 -1
  18. package/dist/cli/messenger/search-answers.js +2 -0
  19. package/dist/cli/search-left-out.d.ts +3 -0
  20. package/dist/cli/search-left-out.js +12 -0
  21. package/dist/cli/store-settings.js +23 -4
  22. package/dist/cli/untrusted-marks.d.ts +2 -0
  23. package/dist/cli/untrusted-marks.js +6 -0
  24. package/dist/crm/cli/files.d.ts +68 -0
  25. package/dist/crm/cli/files.js +616 -0
  26. package/dist/crm/cli/index.d.ts +1 -0
  27. package/dist/crm/cli/index.js +3 -0
  28. package/dist/crm/cli/search-all.js +3 -2
  29. package/dist/crm/file-fetch.d.ts +15 -0
  30. package/dist/crm/file-fetch.js +61 -0
  31. package/dist/crm/index.d.ts +1 -0
  32. package/dist/crm/index.js +1 -0
  33. package/dist/crm/testing/files-contract.d.ts +17 -0
  34. package/dist/crm/testing/files-contract.js +142 -0
  35. package/dist/crm/testing/index.d.ts +1 -0
  36. package/dist/crm/testing/index.js +1 -0
  37. package/dist/crm/testing/source.d.ts +5 -0
  38. package/dist/crm/testing/source.js +3 -1
  39. package/dist/documents/index.d.ts +3 -1
  40. package/dist/documents/index.js +2 -0
  41. package/dist/documents/model.d.ts +20 -6
  42. package/dist/documents/model.js +2 -1
  43. package/dist/entities/hit.d.ts +3 -3
  44. package/dist/entities/hit.js +14 -3
  45. package/dist/entities/index.d.ts +1 -1
  46. package/dist/errors/reasons.d.ts +1 -1
  47. package/dist/errors/reasons.js +23 -1
  48. package/dist/files/safe-name.d.ts +2 -1
  49. package/dist/files/safe-name.js +21 -3
  50. package/dist/meetings/mcp/passage.d.ts +6 -2
  51. package/dist/meetings/mcp/passage.js +7 -3
  52. package/dist/resolve.d.ts +1 -1
  53. package/dist/resolve.js +1 -1
  54. package/dist/search/account-list.d.ts +2 -0
  55. package/dist/search/account-list.js +32 -4
  56. package/dist/search/respell.d.ts +10 -0
  57. package/dist/search/respell.js +63 -0
  58. package/dist/search/stem.d.ts +16 -3
  59. package/dist/search/stem.js +32 -4
  60. package/dist/services/documents/file-text.d.ts +13 -0
  61. package/dist/services/documents/file-text.js +33 -0
  62. package/dist/services/documents/plan.d.ts +3 -2
  63. package/dist/services/documents/plan.js +50 -16
  64. package/dist/services/documents/ports.d.ts +2 -2
  65. package/dist/services/documents/sync.js +2 -2
  66. package/dist/services/embeddings-search.js +169 -141
  67. package/dist/services/embeddings.d.ts +8 -0
  68. package/dist/services/message-resolution.d.ts +1 -1
  69. package/dist/services/message-resolution.js +1 -1
  70. package/dist/services/messages-archive-search.js +55 -3
  71. package/dist/services/messages-search.d.ts +2 -1
  72. package/dist/services/messages-search.js +19 -7
  73. package/dist/services/messages.d.ts +8 -0
  74. package/dist/services/metadata.d.ts +1 -1
  75. package/dist/services/named-chat.d.ts +20 -0
  76. package/dist/services/named-chat.js +63 -0
  77. package/dist/services/search-all.d.ts +7 -2
  78. package/dist/services/search-all.js +35 -1
  79. package/dist/store/exports.d.ts +1 -1
  80. package/dist/store/exports.js +1 -1
  81. package/dist/store/search-index.d.ts +2 -0
  82. package/dist/store/search-index.js +1 -0
  83. package/dist/store/sqlite/crm-files.d.ts +3 -0
  84. package/dist/store/sqlite/crm-files.js +2 -0
  85. package/dist/store/sqlite/crm-settings.d.ts +42 -1
  86. package/dist/store/sqlite/crm-settings.js +46 -2
  87. package/dist/store/sqlite/lucene-fields.d.ts +2 -0
  88. package/dist/store/sqlite/lucene-fields.js +6 -2
  89. package/dist/store/sqlite/lucene.js +2 -2
  90. package/dist/store/sqlite/manifest.js +2 -0
  91. package/dist/store/sqlite/migrations.generated.js +20 -0
  92. package/dist/store/sqlite/schema.d.ts +169 -0
  93. package/dist/store/sqlite/schema.js +18 -0
  94. package/dist/store/sqlite/stems.d.ts +2 -0
  95. package/dist/store/sqlite/stems.js +5 -0
  96. package/dist/store/sqlite/vectors.d.ts +26 -1
  97. package/dist/store/sqlite/vectors.js +72 -15
  98. package/dist/store/vectors.js +6 -2
  99. package/package.json +2 -2
@@ -2355,6 +2355,24 @@ export const vectorIndexes = sqliteTable("vector_indexes", {
2355
2355
  export const vectorIndexRedo = sqliteTable("vector_index_redo", {
2356
2356
  chunkId: integer("chunk_id").notNull(),
2357
2357
  });
2358
+ /** One built chat's readiness for one model, counted once; triggers drop the row when what it counts changes. */
2359
+ export const conversationReadiness = sqliteTable("conversation_readiness", {
2360
+ chatId: integer("chat_id")
2361
+ .notNull()
2362
+ .references(() => chats.id, { onDelete: "cascade" }),
2363
+ model: text("model").notNull(),
2364
+ newCount: integer("new_count").notNull(),
2365
+ editedCount: integer("edited_count").notNull(),
2366
+ deletedCount: integer("deleted_count").notNull(),
2367
+ chunks: integer("chunks").notNull(),
2368
+ embeddedCount: integer("embedded_count").notNull(),
2369
+ current: integer("current").notNull(),
2370
+ stale: integer("stale").notNull(),
2371
+ countedAt: integer("counted_at").notNull(),
2372
+ }, (table) => [
2373
+ primaryKey({ columns: [table.chatId, table.model] }),
2374
+ index("conversation_readiness_by_model").on(table.model),
2375
+ ]);
2358
2376
  /** Settings of the store file itself, shared by every profile, tg and MAX — unlike a profile's config file. */
2359
2377
  export const storeSettings = sqliteTable("store_settings", {
2360
2378
  key: text("key").primaryKey(),
@@ -45,6 +45,8 @@ export declare const stemmersOrigin: (database: CacheDatabase) => {
45
45
  origin: "default";
46
46
  defaults: number;
47
47
  } | undefined;
48
+ /** Of these stems, those some message's words were stemmed to. */
49
+ export declare const heldStems: (database: CacheDatabase, stems: string[]) => Set<string>;
48
50
  /** `undefined` on a file before version 15. */
49
51
  export declare const stemsState: (database: CacheDatabase) => StemsState | undefined;
50
52
  /**
@@ -54,6 +54,11 @@ export const stemmersOrigin = (database) => {
54
54
  return { origin: "owner" };
55
55
  }
56
56
  };
57
+ /** Of these stems, those some message's words were stemmed to. */
58
+ export const heldStems = (database, stems) => {
59
+ const lookup = database.prepare(`SELECT 1 FROM ${INDEX} WHERE ${INDEX} MATCH ? LIMIT 1`);
60
+ return new Set(stems.filter((stem) => lookup.get(`stems : "${stem.replaceAll('"', '""')}"`) !== undefined));
61
+ };
57
62
  /** `undefined` on a file before version 15. */
58
63
  export const stemsState = (database) => {
59
64
  const row = indexRow(database, INDEX);
@@ -100,7 +100,20 @@ export declare const unbuiltGroups: ({ orm }: StoreContext, accountPk: number) =
100
100
  * deleted messages; an edit is known only by its revision's time, so one in the build's first millisecond
101
101
  * counts as pending. A changed sender name leaves no trace and is not counted.
102
102
  */
103
- export declare const readiness: ({ orm }: StoreContext, chatKey: number, model: string) => {
103
+ type Counts = {
104
+ new: number;
105
+ edited: number;
106
+ deleted: number;
107
+ chunks: number;
108
+ embedded: number;
109
+ current: number;
110
+ stale: number;
111
+ };
112
+ /**
113
+ * A built chat's readiness for one model. The counts read every message and chunk of the chat, a second or more in a
114
+ * large group, so they are kept in `conversation_readiness` once counted; triggers drop the row when they change.
115
+ */
116
+ export declare const readiness: (context: StoreContext, chatKey: number, model: string, counted: Counted[]) => {
104
117
  builtAt: number;
105
118
  algorithmVersion: number | null;
106
119
  pending: {
@@ -116,5 +129,17 @@ export declare const readiness: ({ orm }: StoreContext, chatKey: number, model:
116
129
  missing: number;
117
130
  };
118
131
  } | undefined;
132
+ type Counted = {
133
+ chatKey: number;
134
+ counts: Counts;
135
+ };
136
+ /** The commits other connections made to the file so far; equal twice means nothing changed in between. */
137
+ export declare const dataVersion: ({ database }: StoreContext) => number;
138
+ /**
139
+ * Keeps what `readiness` counted, in one write. Skipped, never waited for, when another process holds the write lock
140
+ * or committed anything since `since`: the counts may then be out of date, and the next search counts them again.
141
+ */
142
+ export declare const saveReadiness: (context: StoreContext, model: string, counted: Counted[], since: number) => void;
119
143
  /** The primary key starts with `model`, so each count reads only that model's rows. */
120
144
  export declare const vectorCounts: ({ orm }: StoreContext, models: string[]) => Map<string, number>;
145
+ export {};
@@ -1,5 +1,7 @@
1
1
  import { chunkHash, chunkTextOf } from "../../conversations/chunks.js";
2
+ import { isQueryOnly } from "../driver.js";
2
3
  import { sql } from "./drizzle/core.js";
4
+ import { isBusy, rollBack } from "./transaction.js";
3
5
  import { ensureVectorIndex, searchIndex, unitVector } from "./vector-index.js";
4
6
  /** No planner statistics in the store: without CROSS JOIN SQLite reads every chunk of the store, of every chat. */
5
7
  const currentChunks = (chatKey) => sql `SELECT k.content_hash, k.chunkable_id AS conversation_id, r.first_message_id,
@@ -287,16 +289,75 @@ export const unbuiltGroups = ({ orm }, accountPk) => orm
287
289
  ORDER BY (SELECT max(m.sent_at) FROM messages m WHERE m.chat_id = ch.id) DESC, ch.external_id`)
288
290
  .map(({ id }) => id);
289
291
  /**
290
- * What the chat's current build has not seen, and its chunks for `model`. Membership is exact for new and
291
- * deleted messages; an edit is known only by its revision's time, so one in the build's first millisecond
292
- * counts as pending. A changed sender name leaves no trace and is not counted.
292
+ * A built chat's readiness for one model. The counts read every message and chunk of the chat, a second or more in a
293
+ * large group, so they are kept in `conversation_readiness` once counted; triggers drop the row when they change.
293
294
  */
294
- export const readiness = ({ orm }, chatKey, model) => {
295
+ export const readiness = (context, chatKey, model, counted) => {
296
+ const { orm } = context;
295
297
  const state = orm.get(sql `SELECT built_at AS builtAt, algorithm_version AS algorithmVersion, current_build AS build
296
298
  FROM conversation_state WHERE chat_id = ${chatKey}`);
297
299
  if (!state || state.builtAt === null || state.build === null)
298
300
  return undefined;
299
301
  const { builtAt, build } = state;
302
+ const saved = orm.get(sql `SELECT new_count AS new, edited_count AS edited, deleted_count AS deleted, chunks,
303
+ embedded_count AS embedded, current, stale FROM conversation_readiness
304
+ WHERE chat_id = ${chatKey} AND model = ${model}`);
305
+ const counts = saved ?? countReadiness(context, chatKey, model, builtAt, build);
306
+ if (!saved)
307
+ counted.push({ chatKey, counts });
308
+ return {
309
+ builtAt,
310
+ algorithmVersion: state.algorithmVersion,
311
+ pending: { new: counts.new, edited: counts.edited, deleted: counts.deleted },
312
+ vectors: {
313
+ chunks: counts.chunks,
314
+ embedded: counts.embedded,
315
+ current: counts.current,
316
+ stale: counts.stale,
317
+ missing: counts.chunks - counts.current - counts.stale,
318
+ },
319
+ };
320
+ };
321
+ /** The commits other connections made to the file so far; equal twice means nothing changed in between. */
322
+ export const dataVersion = ({ database }) => Number(Object.values(database.prepare("PRAGMA data_version").get() ?? {})[0]);
323
+ /**
324
+ * Keeps what `readiness` counted, in one write. Skipped, never waited for, when another process holds the write lock
325
+ * or committed anything since `since`: the counts may then be out of date, and the next search counts them again.
326
+ */
327
+ export const saveReadiness = (context, model, counted, since) => {
328
+ const { database } = context;
329
+ if (counted.length === 0 || isQueryOnly(database))
330
+ return;
331
+ const timeout = Number(database.prepare("PRAGMA busy_timeout").get()?.timeout ?? 0);
332
+ database.exec("PRAGMA busy_timeout = 0");
333
+ try {
334
+ database.exec("BEGIN IMMEDIATE");
335
+ }
336
+ catch (error) {
337
+ if (isBusy(error))
338
+ return;
339
+ throw error;
340
+ }
341
+ finally {
342
+ database.exec(`PRAGMA busy_timeout = ${timeout}`);
343
+ }
344
+ try {
345
+ if (dataVersion(context) === since) {
346
+ const insert = database.prepare(`INSERT OR REPLACE INTO conversation_readiness
347
+ (chat_id, model, new_count, edited_count, deleted_count, chunks, embedded_count, current, stale, counted_at)
348
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
349
+ const at = Date.now();
350
+ for (const { chatKey, counts: c } of counted)
351
+ insert.run(chatKey, model, c.new, c.edited, c.deleted, c.chunks, c.embedded, c.current, c.stale, at);
352
+ }
353
+ database.exec("COMMIT");
354
+ }
355
+ catch (error) {
356
+ rollBack(database);
357
+ throw error;
358
+ }
359
+ };
360
+ const countReadiness = ({ orm }, chatKey, model, builtAt, build) => {
300
361
  const member = (pk) => sql `EXISTS (SELECT 1 FROM conversation_messages cm
301
362
  JOIN conversations c ON c.id = cm.conversation_id WHERE cm.message_id = ${pk} AND c.build = ${build})`;
302
363
  const editedSince = (pk) => sql `EXISTS (SELECT 1 FROM message_revisions r WHERE r.message_id = ${pk} AND r.created_at >= ${builtAt})`;
@@ -320,18 +381,14 @@ export const readiness = ({ orm }, chatKey, model) => {
320
381
  FROM (${currentChunks(chatKey)}) h)
321
382
  SELECT count(*) AS chunks, sum(vector) AS embedded, sum(vector AND NOT stale) AS current, sum(stale) AS stale
322
383
  FROM hashes`);
323
- const chunks = Number(vectors?.chunks ?? 0);
324
- const current = Number(vectors?.current ?? 0);
325
- const stale = Number(vectors?.stale ?? 0);
326
384
  return {
327
- builtAt,
328
- algorithmVersion: state.algorithmVersion,
329
- pending: {
330
- new: Number(pending?.new ?? 0),
331
- edited: Number(pending?.edited ?? 0),
332
- deleted: Number(pending?.deleted ?? 0),
333
- },
334
- vectors: { chunks, embedded: Number(vectors?.embedded ?? 0), current, stale, missing: chunks - current - stale },
385
+ new: Number(pending?.new ?? 0),
386
+ edited: Number(pending?.edited ?? 0),
387
+ deleted: Number(pending?.deleted ?? 0),
388
+ chunks: Number(vectors?.chunks ?? 0),
389
+ embedded: Number(vectors?.embedded ?? 0),
390
+ current: Number(vectors?.current ?? 0),
391
+ stale: Number(vectors?.stale ?? 0),
335
392
  };
336
393
  };
337
394
  /** The primary key starts with `model`, so each count reads only that model's rows. */
@@ -241,8 +241,10 @@ export const accountVectorsOver = (context, mail) => {
241
241
  const chats = chatId === undefined
242
242
  ? vectors.builtChats(context, accountPk, searchableOnly)
243
243
  : [{ chatKey: chatKeyOf(key, chatId), id: chatId }].flatMap(({ chatKey, id }) => chatKey === undefined ? [] : [{ chatKey, id }]);
244
- return chats.map(({ chatKey, id }) => {
245
- const found = vectors.readiness(context, chatKey, model);
244
+ const since = vectors.dataVersion(context);
245
+ const counted = [];
246
+ const found = chats.map(({ chatKey, id }) => {
247
+ const found = vectors.readiness(context, chatKey, model, counted);
246
248
  return found
247
249
  ? { chatId: id, ...found, builtAt: new Date(found.builtAt).toISOString() }
248
250
  : {
@@ -253,6 +255,8 @@ export const accountVectorsOver = (context, mail) => {
253
255
  vectors: { chunks: 0, embedded: 0, current: 0, stale: 0, missing: 0 },
254
256
  };
255
257
  });
258
+ vectors.saveReadiness(context, model, counted, since);
259
+ return found;
256
260
  },
257
261
  unbuiltGroups: async (key) => {
258
262
  const accountPk = findAccountPk(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.687.0",
3
+ "version": "0.689.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",
@@ -15,7 +15,7 @@
15
15
  "type": "module",
16
16
  "wirecat": {
17
17
  "store": {
18
- "speaks": 68,
18
+ "speaks": 69,
19
19
  "minCompatible": 56
20
20
  }
21
21
  },