@wirecat/cli-core 0.688.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.
- package/CHANGELOG.md +40 -0
- package/dist/cli/documents/index.d.ts +1 -0
- package/dist/cli/documents/index.js +1 -0
- package/dist/cli/documents/library.d.ts +107 -0
- package/dist/cli/documents/library.js +514 -0
- package/dist/cli/documents/testing/source-contract.d.ts +11 -6
- package/dist/cli/documents/testing/source-contract.js +54 -23
- package/dist/cli/documents/testing/source.d.ts +11 -3
- package/dist/cli/documents/testing/source.js +15 -10
- package/dist/documents/index.d.ts +3 -1
- package/dist/documents/index.js +2 -0
- package/dist/documents/model.d.ts +20 -6
- package/dist/documents/model.js +2 -1
- package/dist/errors/reasons.d.ts +1 -1
- package/dist/errors/reasons.js +11 -0
- package/dist/resolve.d.ts +1 -1
- package/dist/resolve.js +1 -1
- package/dist/search/account-list.d.ts +2 -0
- package/dist/search/account-list.js +32 -4
- package/dist/services/documents/file-text.d.ts +13 -0
- package/dist/services/documents/file-text.js +33 -0
- package/dist/services/documents/plan.d.ts +3 -2
- package/dist/services/documents/plan.js +50 -16
- package/dist/services/documents/ports.d.ts +2 -2
- package/dist/services/documents/sync.js +2 -2
- package/dist/services/message-resolution.d.ts +1 -1
- package/dist/services/message-resolution.js +1 -1
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +20 -0
- package/dist/store/sqlite/schema.d.ts +169 -0
- package/dist/store/sqlite/schema.js +18 -0
- package/dist/store/sqlite/vectors.d.ts +26 -1
- package/dist/store/sqlite/vectors.js +72 -15
- package/dist/store/vectors.js +6 -2
- package/package.json +2 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { CliError } from "@wirecat/cli-base";
|
|
3
|
+
import { feedOf, } from "../../documents/model.js";
|
|
3
4
|
import { inScope, within } from "../../documents/scope.js";
|
|
4
5
|
// Without a query or pages the hash stays what it was, so a profile that never set them keeps its checkpoint. Turning
|
|
5
6
|
// pages on changes it, so the next sync walks again and imports the pages an incremental one would never revisit.
|
|
@@ -12,6 +13,9 @@ export const changedSince = (previous, file) => previous.version !== file.versio
|
|
|
12
13
|
previous.canDownload !== file.canDownload ||
|
|
13
14
|
JSON.stringify(previous.parents) !== JSON.stringify(file.parents);
|
|
14
15
|
const SAVE_EVERY_MS = 2000;
|
|
16
|
+
// What a pass of a source without a change feed records as its cursor: the next pass is not a first one, so it saves
|
|
17
|
+
// only what changed.
|
|
18
|
+
export const WALKED = "walked";
|
|
15
19
|
// A round of changes is saved under its own scope: a CLI that validates the walk file keeps only the fields it knows,
|
|
16
20
|
// and these say both what the walk is and which cursor it started from.
|
|
17
21
|
const changesScope = (scope, cursor) => `${scope}:changes:${cursor}`;
|
|
@@ -47,8 +51,11 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
47
51
|
if (query?.mode === "include" && !query.list)
|
|
48
52
|
throw new CliError("configuration_error", "a query that includes files must list them");
|
|
49
53
|
const initial = !state.cursor || scope !== state.scope;
|
|
50
|
-
|
|
51
|
-
const
|
|
54
|
+
// Without a feed every pass walks the whole tree again; only a first one saves every file.
|
|
55
|
+
const feed = feedOf(source);
|
|
56
|
+
const full = initial || !feed;
|
|
57
|
+
const resumed = walks?.read(full ? scope : changesScope(scope, state.cursor));
|
|
58
|
+
const folders = full ? {} : structuredClone(state.folders);
|
|
52
59
|
const files = new Map(Object.values(state.documents).map((doc) => [doc.metadata.id, doc.metadata]));
|
|
53
60
|
const changed = new Set();
|
|
54
61
|
// What the walk saves: every file of a full walk, or what a round of changes touched, a removed file as trashed.
|
|
@@ -56,11 +63,17 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
56
63
|
for (const skipped of state.skipped)
|
|
57
64
|
if (skipped.file)
|
|
58
65
|
files.set(skipped.id, skipped.file);
|
|
59
|
-
if (
|
|
66
|
+
if (full)
|
|
60
67
|
files.clear();
|
|
68
|
+
// A pass without a feed lists every file again; only one new or changed since the last pass is work, so a file kept
|
|
69
|
+
// by name only is not read again each sync. A changed path (a folder renamed above it) counts as a change.
|
|
70
|
+
const prior = (id) => state.documents[id]?.metadata ?? state.skipped.find((item) => item.id === id)?.file;
|
|
71
|
+
const moved = (before, file) => JSON.stringify(before.providerMetadata ?? null) !== JSON.stringify(file.providerMetadata ?? null);
|
|
61
72
|
const keep = (file) => {
|
|
62
73
|
files.set(file.id, file);
|
|
63
|
-
|
|
74
|
+
const before = feed || initial ? undefined : prior(file.id);
|
|
75
|
+
if (!before || changedSince(before, file) || moved(before, file))
|
|
76
|
+
changed.add(file.id);
|
|
64
77
|
delta.set(file.id, file);
|
|
65
78
|
};
|
|
66
79
|
const drop = (id) => {
|
|
@@ -75,7 +88,7 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
75
88
|
};
|
|
76
89
|
// A folder the last finished pass walked keeps its subtree: a round lists only its direct children, and the
|
|
77
90
|
// source's changes name what changed below them. A new, restored or moved-in folder is walked in full.
|
|
78
|
-
const walked = (id) => !
|
|
91
|
+
const walked = (id) => !full && state.folders[id] !== undefined && !state.folders[id]?.trashed;
|
|
79
92
|
let walk = resumed;
|
|
80
93
|
let cursor = state.cursor ?? "";
|
|
81
94
|
// Each read takes a request from the budget, the start token and a root's own metadata as much as a listing: a run
|
|
@@ -84,11 +97,11 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
84
97
|
const taken = options.signal?.aborted ? "cancelled" : budget?.take();
|
|
85
98
|
return taken === "deadline" && budget?.first?.() ? null : (taken ?? null);
|
|
86
99
|
};
|
|
87
|
-
if (!walk &&
|
|
100
|
+
if (!walk && full) {
|
|
88
101
|
const before = charge();
|
|
89
102
|
if (before)
|
|
90
103
|
return { walk: { folders_left: roots.length, entries: 0, listed: 0, stopped: before } };
|
|
91
|
-
const start = await
|
|
104
|
+
const start = feed ? await feed.startToken() : WALKED;
|
|
92
105
|
walk = {
|
|
93
106
|
scope,
|
|
94
107
|
cursor: start,
|
|
@@ -105,7 +118,7 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
105
118
|
const before = charge();
|
|
106
119
|
if (before)
|
|
107
120
|
return { walk: { folders_left: 0, entries: 0, listed: 0, stopped: before } };
|
|
108
|
-
const page = await
|
|
121
|
+
const page = await feed.changes(cursor);
|
|
109
122
|
const latest = new Map(page.items.map((change) => [change.id, change]));
|
|
110
123
|
const entering = [];
|
|
111
124
|
// Apply all ancestry changes before classifying document scope.
|
|
@@ -129,7 +142,7 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
129
142
|
for (const root of roots) {
|
|
130
143
|
const previous = folders[root];
|
|
131
144
|
try {
|
|
132
|
-
const current = await source.metadata(root);
|
|
145
|
+
const current = await source.metadata(root, folders[root]);
|
|
133
146
|
if (current.id !== root || !isFolder(current))
|
|
134
147
|
throw new CliError("invalid_response", "selected folder metadata is inconsistent");
|
|
135
148
|
folders[root] = current;
|
|
@@ -160,9 +173,9 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
160
173
|
else
|
|
161
174
|
cursor = page.cursor;
|
|
162
175
|
}
|
|
163
|
-
const vanished = async (id) => {
|
|
176
|
+
const vanished = async (id, known) => {
|
|
164
177
|
try {
|
|
165
|
-
return (await source.metadata(id)).trashed;
|
|
178
|
+
return (await source.metadata(id, known)).trashed;
|
|
166
179
|
}
|
|
167
180
|
catch (error) {
|
|
168
181
|
return errorOf(error).code === "not_found";
|
|
@@ -210,7 +223,24 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
210
223
|
if (stopped)
|
|
211
224
|
break;
|
|
212
225
|
if (!step.folder) {
|
|
213
|
-
const
|
|
226
|
+
const known = state.folders[step.id];
|
|
227
|
+
let root;
|
|
228
|
+
try {
|
|
229
|
+
root = await source.metadata(step.id, known);
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
// Without a feed, a folder the last pass held and the provider no longer finds is skipped as unavailable,
|
|
233
|
+
// as a round of changes does, so a moved or deleted root does not fail every sync. A source with a feed
|
|
234
|
+
// walks in full only on a rescan, where a root it cannot read still fails and keeps what it holds.
|
|
235
|
+
if (feed || !known || errorOf(error).code !== "not_found")
|
|
236
|
+
throw error;
|
|
237
|
+
walk.stack.pop();
|
|
238
|
+
visited.add(step.id);
|
|
239
|
+
folders[step.id] = { ...known, trashed: true };
|
|
240
|
+
spent++;
|
|
241
|
+
listed++;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
214
244
|
if (!isFolder(root) || root.trashed)
|
|
215
245
|
throw new CliError("validation_error", "configured root is not an accessible folder");
|
|
216
246
|
container(root);
|
|
@@ -225,9 +255,9 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
225
255
|
let more;
|
|
226
256
|
try {
|
|
227
257
|
if (source.childrenPage)
|
|
228
|
-
({ files: children, cursor: more } = await source.childrenPage(folder.id, step.listing));
|
|
258
|
+
({ files: children, cursor: more } = await source.childrenPage(folder.id, step.listing, folder));
|
|
229
259
|
else
|
|
230
|
-
children = await source.children(folder.id);
|
|
260
|
+
children = await source.children(folder.id, folder);
|
|
231
261
|
}
|
|
232
262
|
catch (error) {
|
|
233
263
|
if (errorOf(error).code !== "not_found")
|
|
@@ -243,7 +273,7 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
243
273
|
// Deleted or unshared since the walk saved it: gone, with what is below it, instead of failing every sync.
|
|
244
274
|
// A listing's 404 alone can be passing, so the folder is read again and is gone only when that read is a
|
|
245
275
|
// 404 too or finds it trashed; on any doubt the call fails and the next sync lists it again.
|
|
246
|
-
if (!(await vanished(folder.id)))
|
|
276
|
+
if (!(await vanished(folder.id, folder)))
|
|
247
277
|
throw error;
|
|
248
278
|
walk.stack.pop();
|
|
249
279
|
visited.add(folder.id);
|
|
@@ -336,7 +366,11 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
336
366
|
continue;
|
|
337
367
|
}
|
|
338
368
|
if (changed.has(id) || retry || known?.notRead) {
|
|
339
|
-
if (!known ||
|
|
369
|
+
if (!known ||
|
|
370
|
+
known.notRead ||
|
|
371
|
+
changedSince(known.metadata, file) ||
|
|
372
|
+
initial ||
|
|
373
|
+
(!feed && moved(known.metadata, file)))
|
|
340
374
|
jobs.push({ id, kind: "save", file });
|
|
341
375
|
}
|
|
342
376
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import type { Deadline } from "../../background/budget.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { FeedlessDocumentSource, SourceFile, SourceIndex, SourceSkip, SourceStatePort } from "../../documents/model.js";
|
|
4
4
|
import type { SourceQuery } from "../../documents/scope.js";
|
|
5
5
|
export interface SyncOptions<M extends SourceFile> {
|
|
6
|
-
source:
|
|
6
|
+
source: FeedlessDocumentSource<M> | undefined;
|
|
7
7
|
index: SourceIndex<M>;
|
|
8
8
|
state: SourceStatePort<M>;
|
|
9
9
|
principal: string;
|
|
@@ -135,7 +135,7 @@ export const syncDocuments = async (options) => {
|
|
|
135
135
|
const stop = ask();
|
|
136
136
|
if (stop)
|
|
137
137
|
return stop;
|
|
138
|
-
const bytes = await source.bytes(file.id);
|
|
138
|
+
const bytes = await source.bytes(file.id, file);
|
|
139
139
|
if (bytes.length > MAX_PICTURE_BYTES) {
|
|
140
140
|
entry.picture = "too-large";
|
|
141
141
|
result.pictures_too_large = (result.pictures_too_large ?? 0) + 1;
|
|
@@ -187,7 +187,7 @@ export const syncDocuments = async (options) => {
|
|
|
187
187
|
result.stopped = stop;
|
|
188
188
|
break;
|
|
189
189
|
}
|
|
190
|
-
const content = await source.content(job.id, limit);
|
|
190
|
+
const content = await source.content(job.id, limit, job.file);
|
|
191
191
|
const listed = query?.mode === "include" && (pending.matched?.includes(job.id) ?? query.matches(content.metadata));
|
|
192
192
|
if (!listed && !inScope(content.metadata, profile.roots, pending.folders, new Set(), query)) {
|
|
193
193
|
await index.remove(profile.principal, job.id);
|
|
@@ -36,7 +36,7 @@ export declare const senderAmong: (store: MessageStore, accounts: AccountKey[],
|
|
|
36
36
|
}>;
|
|
37
37
|
/** The most people or chats one name pattern may stand for in a search. */
|
|
38
38
|
export declare const MOST_NAME_MATCHES = 50;
|
|
39
|
-
/** Every sender a name pattern (`from
|
|
39
|
+
/** Every sender a name pattern (`from:Станислав*`) fits, through the names each messenger's accounts have seen. */
|
|
40
40
|
export declare const sendersMatching: (store: MessageStore, accounts: AccountKey[], field: string, pattern: string) => Promise<{
|
|
41
41
|
provider: Provider;
|
|
42
42
|
id: Id;
|
|
@@ -198,7 +198,7 @@ const patternRefused = (field, pattern, what, found, near) => {
|
|
|
198
198
|
}
|
|
199
199
|
throw new CliError("validation_error", `${field}:${pattern} fits ${found} ${what}, more than ${MOST_NAME_MATCHES} — write more of the name`, { reason: "query.too-large", budget: "name matches", limit: MOST_NAME_MATCHES, complete: false });
|
|
200
200
|
};
|
|
201
|
-
/** Every sender a name pattern (`from
|
|
201
|
+
/** Every sender a name pattern (`from:Станислав*`) fits, through the names each messenger's accounts have seen. */
|
|
202
202
|
export const sendersMatching = async (store, accounts, field, pattern) => {
|
|
203
203
|
const providers = [...new Set(accounts.map(({ provider }) => provider))];
|
|
204
204
|
const seen = await Promise.all(providers.map(async (provider) => {
|
|
@@ -134,6 +134,8 @@ export const MANIFEST = [
|
|
|
134
134
|
{ name: "20261113154357_version-67-fact-removals-call-fields", version: 67, minCompatible: 56 },
|
|
135
135
|
{ name: "20261116204138_version-68-crm-files", version: 68, minCompatible: 56 },
|
|
136
136
|
{ name: "20261116204204_version-68-crm-file-words", version: 68, minCompatible: 56 },
|
|
137
|
+
{ name: "20261117015456_version-69-conversation-readiness", version: 69, minCompatible: 56 },
|
|
138
|
+
{ name: "20261117015457_version-69-conversation-readiness-triggers", version: 69, minCompatible: 56 },
|
|
137
139
|
];
|
|
138
140
|
export const generatedMigrations = (manifest = MANIFEST, generated = GENERATED) => {
|
|
139
141
|
const migrations = [];
|
|
@@ -1361,5 +1361,25 @@ export const GENERATED = [
|
|
|
1361
1361
|
"CREATE TRIGGER crm_file_words_au AFTER UPDATE OF normalized_text ON crm_files\n WHEN old.normalized_text IS NOT new.normalized_text BEGIN\n DELETE FROM crm_file_words WHERE rowid = old.id;\n INSERT INTO crm_file_words (rowid, normalized_text)\n SELECT new.id, new.normalized_text WHERE new.normalized_text <> '';\nEND;",
|
|
1362
1362
|
"CREATE TRIGGER crm_file_words_ad AFTER DELETE ON crm_files BEGIN\n DELETE FROM crm_file_words WHERE rowid = old.id;\nEND;"
|
|
1363
1363
|
]
|
|
1364
|
+
},
|
|
1365
|
+
{
|
|
1366
|
+
"name": "20261117015456_version-69-conversation-readiness",
|
|
1367
|
+
"statements": [
|
|
1368
|
+
"CREATE TABLE `conversation_readiness` (\n\t`chat_id` integer NOT NULL,\n\t`model` text NOT NULL,\n\t`new_count` integer NOT NULL,\n\t`edited_count` integer NOT NULL,\n\t`deleted_count` integer NOT NULL,\n\t`chunks` integer NOT NULL,\n\t`embedded_count` integer NOT NULL,\n\t`current` integer NOT NULL,\n\t`stale` integer NOT NULL,\n\t`counted_at` integer NOT NULL,\n\tCONSTRAINT `conversation_readiness_pk` PRIMARY KEY(`chat_id`, `model`),\n\tCONSTRAINT `fk_conversation_readiness_chat_id_chats_id_fk` FOREIGN KEY (`chat_id`) REFERENCES `chats`(`id`) ON DELETE CASCADE\n);",
|
|
1369
|
+
"CREATE INDEX `conversation_readiness_by_model` ON `conversation_readiness` (`model`);"
|
|
1370
|
+
]
|
|
1371
|
+
},
|
|
1372
|
+
{
|
|
1373
|
+
"name": "20261117015457_version-69-conversation-readiness-triggers",
|
|
1374
|
+
"statements": [
|
|
1375
|
+
"-- A stored readiness row stays only while nothing it counts has changed; any such write drops it, and the next\n-- search counts that chat again.\nCREATE TRIGGER conversation_readiness_message_ai AFTER INSERT ON messages BEGIN\n DELETE FROM conversation_readiness WHERE chat_id = NEW.chat_id;\nEND;",
|
|
1376
|
+
"CREATE TRIGGER conversation_readiness_message_au AFTER UPDATE OF chat_id, deleted_at ON messages\nWHEN OLD.chat_id IS NOT NEW.chat_id OR OLD.deleted_at IS NOT NEW.deleted_at BEGIN\n DELETE FROM conversation_readiness WHERE chat_id IN (OLD.chat_id, NEW.chat_id);\nEND;",
|
|
1377
|
+
"CREATE TRIGGER conversation_readiness_message_ad AFTER DELETE ON messages BEGIN\n DELETE FROM conversation_readiness WHERE chat_id = OLD.chat_id;\nEND;",
|
|
1378
|
+
"CREATE TRIGGER conversation_readiness_revision_ai AFTER INSERT ON message_revisions BEGIN\n DELETE FROM conversation_readiness WHERE chat_id = (SELECT chat_id FROM messages WHERE id = NEW.message_id);\nEND;",
|
|
1379
|
+
"CREATE TRIGGER conversation_readiness_state_ai AFTER INSERT ON conversation_state BEGIN\n DELETE FROM conversation_readiness WHERE chat_id = NEW.chat_id;\nEND;",
|
|
1380
|
+
"CREATE TRIGGER conversation_readiness_state_au AFTER UPDATE ON conversation_state BEGIN\n DELETE FROM conversation_readiness WHERE chat_id IN (OLD.chat_id, NEW.chat_id);\nEND;",
|
|
1381
|
+
"-- A chat whose every chunk text already has a vector cannot gain from a new one: (model, content_hash) is unique.\nCREATE TRIGGER conversation_readiness_embedding_ai AFTER INSERT ON embeddings BEGIN\n DELETE FROM conversation_readiness WHERE model = NEW.model AND embedded_count < chunks;\nEND;",
|
|
1382
|
+
"CREATE TRIGGER conversation_readiness_embedding_ad AFTER DELETE ON embeddings BEGIN\n DELETE FROM conversation_readiness WHERE model = OLD.model AND embedded_count > 0;\nEND;"
|
|
1383
|
+
]
|
|
1364
1384
|
}
|
|
1365
1385
|
];
|
|
@@ -23391,6 +23391,175 @@ export declare const vectorIndexRedo: import("drizzle-orm/sqlite-core").SQLiteTa
|
|
|
23391
23391
|
dialect: 'sqlite';
|
|
23392
23392
|
isAlias: false;
|
|
23393
23393
|
}>;
|
|
23394
|
+
/** One built chat's readiness for one model, counted once; triggers drop the row when what it counts changes. */
|
|
23395
|
+
export declare const conversationReadiness: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
23396
|
+
name: "conversation_readiness";
|
|
23397
|
+
schema: undefined;
|
|
23398
|
+
columns: {
|
|
23399
|
+
chatId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23400
|
+
name: string;
|
|
23401
|
+
tableName: "conversation_readiness";
|
|
23402
|
+
dataType: "number int53";
|
|
23403
|
+
data: number;
|
|
23404
|
+
driverParam: number;
|
|
23405
|
+
notNull: true;
|
|
23406
|
+
hasDefault: false;
|
|
23407
|
+
isPrimaryKey: false;
|
|
23408
|
+
isAutoincrement: false;
|
|
23409
|
+
hasRuntimeDefault: false;
|
|
23410
|
+
enumValues: undefined;
|
|
23411
|
+
baseColumn: never;
|
|
23412
|
+
identity: undefined;
|
|
23413
|
+
generated: undefined;
|
|
23414
|
+
}, {}>;
|
|
23415
|
+
model: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23416
|
+
name: string;
|
|
23417
|
+
tableName: "conversation_readiness";
|
|
23418
|
+
dataType: "string";
|
|
23419
|
+
data: string;
|
|
23420
|
+
driverParam: string;
|
|
23421
|
+
notNull: true;
|
|
23422
|
+
hasDefault: false;
|
|
23423
|
+
isPrimaryKey: false;
|
|
23424
|
+
isAutoincrement: false;
|
|
23425
|
+
hasRuntimeDefault: false;
|
|
23426
|
+
enumValues: [string, ...string[]];
|
|
23427
|
+
baseColumn: never;
|
|
23428
|
+
identity: undefined;
|
|
23429
|
+
generated: undefined;
|
|
23430
|
+
}, {}>;
|
|
23431
|
+
newCount: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23432
|
+
name: string;
|
|
23433
|
+
tableName: "conversation_readiness";
|
|
23434
|
+
dataType: "number int53";
|
|
23435
|
+
data: number;
|
|
23436
|
+
driverParam: number;
|
|
23437
|
+
notNull: true;
|
|
23438
|
+
hasDefault: false;
|
|
23439
|
+
isPrimaryKey: false;
|
|
23440
|
+
isAutoincrement: false;
|
|
23441
|
+
hasRuntimeDefault: false;
|
|
23442
|
+
enumValues: undefined;
|
|
23443
|
+
baseColumn: never;
|
|
23444
|
+
identity: undefined;
|
|
23445
|
+
generated: undefined;
|
|
23446
|
+
}, {}>;
|
|
23447
|
+
editedCount: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23448
|
+
name: string;
|
|
23449
|
+
tableName: "conversation_readiness";
|
|
23450
|
+
dataType: "number int53";
|
|
23451
|
+
data: number;
|
|
23452
|
+
driverParam: number;
|
|
23453
|
+
notNull: true;
|
|
23454
|
+
hasDefault: false;
|
|
23455
|
+
isPrimaryKey: false;
|
|
23456
|
+
isAutoincrement: false;
|
|
23457
|
+
hasRuntimeDefault: false;
|
|
23458
|
+
enumValues: undefined;
|
|
23459
|
+
baseColumn: never;
|
|
23460
|
+
identity: undefined;
|
|
23461
|
+
generated: undefined;
|
|
23462
|
+
}, {}>;
|
|
23463
|
+
deletedCount: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23464
|
+
name: string;
|
|
23465
|
+
tableName: "conversation_readiness";
|
|
23466
|
+
dataType: "number int53";
|
|
23467
|
+
data: number;
|
|
23468
|
+
driverParam: number;
|
|
23469
|
+
notNull: true;
|
|
23470
|
+
hasDefault: false;
|
|
23471
|
+
isPrimaryKey: false;
|
|
23472
|
+
isAutoincrement: false;
|
|
23473
|
+
hasRuntimeDefault: false;
|
|
23474
|
+
enumValues: undefined;
|
|
23475
|
+
baseColumn: never;
|
|
23476
|
+
identity: undefined;
|
|
23477
|
+
generated: undefined;
|
|
23478
|
+
}, {}>;
|
|
23479
|
+
chunks: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23480
|
+
name: string;
|
|
23481
|
+
tableName: "conversation_readiness";
|
|
23482
|
+
dataType: "number int53";
|
|
23483
|
+
data: number;
|
|
23484
|
+
driverParam: number;
|
|
23485
|
+
notNull: true;
|
|
23486
|
+
hasDefault: false;
|
|
23487
|
+
isPrimaryKey: false;
|
|
23488
|
+
isAutoincrement: false;
|
|
23489
|
+
hasRuntimeDefault: false;
|
|
23490
|
+
enumValues: undefined;
|
|
23491
|
+
baseColumn: never;
|
|
23492
|
+
identity: undefined;
|
|
23493
|
+
generated: undefined;
|
|
23494
|
+
}, {}>;
|
|
23495
|
+
embeddedCount: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23496
|
+
name: string;
|
|
23497
|
+
tableName: "conversation_readiness";
|
|
23498
|
+
dataType: "number int53";
|
|
23499
|
+
data: number;
|
|
23500
|
+
driverParam: number;
|
|
23501
|
+
notNull: true;
|
|
23502
|
+
hasDefault: false;
|
|
23503
|
+
isPrimaryKey: false;
|
|
23504
|
+
isAutoincrement: false;
|
|
23505
|
+
hasRuntimeDefault: false;
|
|
23506
|
+
enumValues: undefined;
|
|
23507
|
+
baseColumn: never;
|
|
23508
|
+
identity: undefined;
|
|
23509
|
+
generated: undefined;
|
|
23510
|
+
}, {}>;
|
|
23511
|
+
current: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23512
|
+
name: string;
|
|
23513
|
+
tableName: "conversation_readiness";
|
|
23514
|
+
dataType: "number int53";
|
|
23515
|
+
data: number;
|
|
23516
|
+
driverParam: number;
|
|
23517
|
+
notNull: true;
|
|
23518
|
+
hasDefault: false;
|
|
23519
|
+
isPrimaryKey: false;
|
|
23520
|
+
isAutoincrement: false;
|
|
23521
|
+
hasRuntimeDefault: false;
|
|
23522
|
+
enumValues: undefined;
|
|
23523
|
+
baseColumn: never;
|
|
23524
|
+
identity: undefined;
|
|
23525
|
+
generated: undefined;
|
|
23526
|
+
}, {}>;
|
|
23527
|
+
stale: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23528
|
+
name: string;
|
|
23529
|
+
tableName: "conversation_readiness";
|
|
23530
|
+
dataType: "number int53";
|
|
23531
|
+
data: number;
|
|
23532
|
+
driverParam: number;
|
|
23533
|
+
notNull: true;
|
|
23534
|
+
hasDefault: false;
|
|
23535
|
+
isPrimaryKey: false;
|
|
23536
|
+
isAutoincrement: false;
|
|
23537
|
+
hasRuntimeDefault: false;
|
|
23538
|
+
enumValues: undefined;
|
|
23539
|
+
baseColumn: never;
|
|
23540
|
+
identity: undefined;
|
|
23541
|
+
generated: undefined;
|
|
23542
|
+
}, {}>;
|
|
23543
|
+
countedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
23544
|
+
name: string;
|
|
23545
|
+
tableName: "conversation_readiness";
|
|
23546
|
+
dataType: "number int53";
|
|
23547
|
+
data: number;
|
|
23548
|
+
driverParam: number;
|
|
23549
|
+
notNull: true;
|
|
23550
|
+
hasDefault: false;
|
|
23551
|
+
isPrimaryKey: false;
|
|
23552
|
+
isAutoincrement: false;
|
|
23553
|
+
hasRuntimeDefault: false;
|
|
23554
|
+
enumValues: undefined;
|
|
23555
|
+
baseColumn: never;
|
|
23556
|
+
identity: undefined;
|
|
23557
|
+
generated: undefined;
|
|
23558
|
+
}, {}>;
|
|
23559
|
+
};
|
|
23560
|
+
dialect: 'sqlite';
|
|
23561
|
+
isAlias: false;
|
|
23562
|
+
}>;
|
|
23394
23563
|
/** Settings of the store file itself, shared by every profile, tg and MAX — unlike a profile's config file. */
|
|
23395
23564
|
export declare const storeSettings: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
23396
23565
|
name: "store_settings";
|
|
@@ -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(),
|
|
@@ -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
|
-
|
|
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
|
-
*
|
|
291
|
-
*
|
|
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 = (
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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. */
|
package/dist/store/vectors.js
CHANGED
|
@@ -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
|
-
|
|
245
|
-
|
|
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.
|
|
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":
|
|
18
|
+
"speaks": 69,
|
|
19
19
|
"minCompatible": 56
|
|
20
20
|
}
|
|
21
21
|
},
|