@wirecat/cli-core 0.735.0 → 0.736.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 +64 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/background/report/messages.d.ts +4 -0
- package/dist/background/report/messages.js +2 -0
- package/dist/background/report/templates/schedule-failed.email.en.liquid +2 -1
- package/dist/background/report/templates/schedule-failed.email.ru.liquid +2 -1
- package/dist/background/report/templates/schedule-failed.sample.json +2 -1
- package/dist/background/report/templates/schedule-failed.telegram.en.liquid +2 -1
- package/dist/background/report/templates/schedule-failed.telegram.ru.liquid +2 -1
- package/dist/background/report/templates/schedule-failed.text.en.liquid +2 -1
- package/dist/background/report/templates/schedule-failed.text.ru.liquid +2 -1
- package/dist/background/schedules.js +11 -7
- package/dist/cli/documents/file-edit.d.ts +4 -4
- package/dist/cli/documents/file-edit.js +69 -5
- package/dist/cli/enrichment-consents-command.js +2 -1
- package/dist/cli/messenger/catch-up-answers.js +1 -0
- package/dist/cli/messenger/download-command.js +3 -0
- package/dist/cli/messenger/inbox.js +4 -1
- package/dist/cli/messenger/review.js +4 -1
- package/dist/cli/messenger/search-answers.js +1 -0
- package/dist/cli/schedule/schedule-command.js +10 -1
- package/dist/cli/search-harder-consents-command.js +2 -1
- package/dist/cli/templates-command.js +7 -6
- package/dist/crm/cli/records.js +16 -1
- package/dist/domain/models.d.ts +4 -0
- package/dist/domain/providers.d.ts +7 -0
- package/dist/domain/providers.js +15 -2
- package/dist/entities/hit.d.ts +2 -0
- package/dist/entities/hit.js +2 -0
- package/dist/entities/index.d.ts +1 -0
- package/dist/errors/reasons.js +9 -2
- package/dist/office/create.d.ts +34 -0
- package/dist/office/create.js +216 -0
- package/dist/office/index.d.ts +1 -0
- package/dist/office/index.js +1 -0
- package/dist/office/package.d.ts +1 -0
- package/dist/office/package.js +1 -1
- package/dist/ports/messenger.d.ts +5 -2
- package/dist/search/word-match.d.ts +16 -0
- package/dist/search/word-match.js +28 -0
- package/dist/sends/upload.d.ts +3 -0
- package/dist/sends/upload.js +2 -2
- package/dist/services/documents/ingestion.js +4 -2
- package/dist/services/file-download.js +4 -2
- package/dist/services/inbox.js +5 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/search-all.d.ts +2 -0
- package/dist/services/search-all.js +47 -17
- package/dist/store/sqlite/crm-rows.d.ts +4 -0
- package/dist/store/sqlite/crm-rows.js +30 -0
- package/dist/store/sqlite/crm.d.ts +2 -0
- package/dist/store/sqlite/crm.js +3 -10
- package/dist/store/sqlite/deals.js +1 -24
- package/dist/store/sqlite/tracker-chunks.d.ts +24 -8
- package/dist/store/sqlite/tracker-chunks.js +137 -38
- package/dist/store/sqlite/tracker.d.ts +5 -0
- package/dist/store/sqlite/tracker.js +118 -17
- package/dist/tracker/cli/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { trackerBodyOf, trackerParentOf, trackerTitleOf, } from "../../domain/tracker.js";
|
|
2
|
+
import { START_LETTERS } from "../../search/stem.js";
|
|
3
|
+
import { clauseIn, probeOf, tokensOf, wordMatches, } from "../../search/word-match.js";
|
|
2
4
|
import { fold, normalize } from "../normalize.js";
|
|
3
5
|
import { accountPk, findAccountPk } from "./accounts.js";
|
|
4
|
-
import { excerptOf
|
|
6
|
+
import { excerptOf } from "./crm-files.js";
|
|
5
7
|
import { dropTrackerChunks, isChunkedTrackerKind, nearestTrackerItems, refreshTrackerChunks, trackerChunksLeft, trackerChunksToEmbed, trackerChunkWriter, trackerEmbedded, } from "./tracker-chunks.js";
|
|
6
8
|
import { write } from "./transaction.js";
|
|
7
9
|
import { dropUnusedVectors } from "./vector-index.js";
|
|
8
10
|
const MOST = 500;
|
|
11
|
+
/** How many rows the index may offer one search; each is read and its words checked. */
|
|
12
|
+
const CANDIDATES = 2_000;
|
|
13
|
+
const PAGE = 200;
|
|
9
14
|
const itemOf = (row) => ({
|
|
10
15
|
kind: String(row.kind),
|
|
11
16
|
record: JSON.parse(String(row.data)),
|
|
@@ -33,7 +38,40 @@ export const resetTrackerItemWords = (database) => {
|
|
|
33
38
|
return Number(changes);
|
|
34
39
|
});
|
|
35
40
|
};
|
|
41
|
+
const keyOf = (data) => data == null ? undefined : JSON.parse(String(data)).key;
|
|
36
42
|
const ISSUE_FIELDS = { assignee: "assignee", project: "project", state: "state" };
|
|
43
|
+
/**
|
|
44
|
+
* The words as clauses for `clauseIn`, and the index expression that finds the rows they could match: a word, a word
|
|
45
|
+
* it begins, or a word that begins with its stem. A Snowball stem begins its word, but for the English `y` it turns to
|
|
46
|
+
* `i` (`compani`), so a stem ending in `i` is looked up without it.
|
|
47
|
+
*/
|
|
48
|
+
const queryOf = (words, exact) => {
|
|
49
|
+
const seen = new Set();
|
|
50
|
+
const clauses = words
|
|
51
|
+
.map((word) => tokensOf(word).filter((token) => fold(token)))
|
|
52
|
+
.filter((tokens) => {
|
|
53
|
+
const key = tokens.map(fold).join(" ");
|
|
54
|
+
if (!tokens.length || seen.has(key))
|
|
55
|
+
return false;
|
|
56
|
+
seen.add(key);
|
|
57
|
+
return true;
|
|
58
|
+
})
|
|
59
|
+
.map((tokens) => tokens.map((token) => [probeOf(token)]));
|
|
60
|
+
if (!clauses.length)
|
|
61
|
+
return undefined;
|
|
62
|
+
const termOf = ({ literal, stems }) => {
|
|
63
|
+
const terms = new Set([exact || literal.length < START_LETTERS ? `"${literal}"` : `"${literal}"*`]);
|
|
64
|
+
if (!exact)
|
|
65
|
+
for (const stem of stems) {
|
|
66
|
+
const start = stem.endsWith("i") ? stem.slice(0, -1) : stem;
|
|
67
|
+
if (start.length >= 3)
|
|
68
|
+
terms.add(`"${start}"*`);
|
|
69
|
+
}
|
|
70
|
+
return terms.size > 1 ? `(${[...terms].join(" OR ")})` : [...terms].join("");
|
|
71
|
+
};
|
|
72
|
+
const expressions = clauses.map((clause) => `(${clause.flatMap((word) => word.map(termOf)).join(" AND ")})`);
|
|
73
|
+
return { clauses, match: (anyWord) => expressions.join(anyWord ? " OR " : " AND ") };
|
|
74
|
+
};
|
|
37
75
|
export const trackerStoreOver = (context) => {
|
|
38
76
|
const { database } = context;
|
|
39
77
|
const save = (key, kind, records, now) => write(database, () => {
|
|
@@ -43,6 +81,8 @@ export const trackerStoreOver = (context) => {
|
|
|
43
81
|
const chunked = isChunkedTrackerKind(kind);
|
|
44
82
|
const chunk = trackerChunkWriter(database);
|
|
45
83
|
const released = [];
|
|
84
|
+
const comments = database.prepare("SELECT id, title, body, coalesce(source_updated_at, synced_at) AS at FROM tracker_items " +
|
|
85
|
+
"WHERE account_id = ? AND kind = 'comment' AND parent_id = ? AND deleted_at IS NULL");
|
|
46
86
|
const upsert = database.prepare("INSERT INTO tracker_items (account_id, kind, external_id, parent_id, title, title_folded, body, " +
|
|
47
87
|
"normalized_text, data, source_created_at, source_updated_at, archived_at, deleted_at, synced_at) " +
|
|
48
88
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?) " +
|
|
@@ -63,6 +103,26 @@ export const trackerStoreOver = (context) => {
|
|
|
63
103
|
const saved = upsert.get(accountId, kind, merged.externalId, trackerParentOf(kind, merged) ?? null, title, fold(title), body, normalize([title, body].filter(Boolean).join("\n")), JSON.stringify(merged), merged.createdAt ?? null, merged.updatedAt ?? null, merged.archivedAt ?? null, now);
|
|
64
104
|
// Most rows of a sync come back with the text they had; their chunks stay, and only a revived row needs new.
|
|
65
105
|
const same = before?.deleted_at == null && before?.title === title && (before?.body ?? null) === body;
|
|
106
|
+
// A comment is embedded after its issue's key and title, so a new, revived, renamed or moved issue cuts its
|
|
107
|
+
// comments again.
|
|
108
|
+
if (kind === "issue" &&
|
|
109
|
+
(before?.deleted_at !== null ||
|
|
110
|
+
before.title !== title ||
|
|
111
|
+
keyOf(before.data) !== merged.key))
|
|
112
|
+
for (const row of comments.all(accountId, merged.externalId)) {
|
|
113
|
+
const freed = chunk({
|
|
114
|
+
id: Number(row.id),
|
|
115
|
+
accountId,
|
|
116
|
+
kind: "comment",
|
|
117
|
+
title: String(row.title),
|
|
118
|
+
body: row.body == null ? null : String(row.body),
|
|
119
|
+
parent: merged.externalId,
|
|
120
|
+
occurredAt: Number(row.at),
|
|
121
|
+
scope: scope == null ? null : String(scope),
|
|
122
|
+
}, now);
|
|
123
|
+
for (const hash of freed?.released ?? [])
|
|
124
|
+
released.push(hash);
|
|
125
|
+
}
|
|
66
126
|
if (!chunked || same)
|
|
67
127
|
continue;
|
|
68
128
|
const freed = chunk({
|
|
@@ -71,10 +131,11 @@ export const trackerStoreOver = (context) => {
|
|
|
71
131
|
kind,
|
|
72
132
|
title,
|
|
73
133
|
body,
|
|
134
|
+
parent: trackerParentOf(kind, merged) ?? null,
|
|
74
135
|
occurredAt: merged.updatedAt ?? now,
|
|
75
136
|
scope: scope == null ? null : String(scope),
|
|
76
137
|
}, now);
|
|
77
|
-
for (const hash of freed ?? [])
|
|
138
|
+
for (const hash of freed?.released ?? [])
|
|
78
139
|
released.push(hash);
|
|
79
140
|
}
|
|
80
141
|
dropUnusedVectors(database, released);
|
|
@@ -149,16 +210,15 @@ export const trackerStoreOver = (context) => {
|
|
|
149
210
|
return { items: rows.slice(0, limit).map((row) => itemOf(row)), hasMore: rows.length > limit };
|
|
150
211
|
};
|
|
151
212
|
const find = ({ words, anyWord = false, exact = false, accounts, kinds, limit, offset = 0 }) => {
|
|
152
|
-
const
|
|
153
|
-
if (!
|
|
213
|
+
const query = queryOf(words, exact);
|
|
214
|
+
if (!query)
|
|
154
215
|
return { items: [], hasMore: false };
|
|
155
216
|
const most = Math.min(Math.max(limit ?? 100, 1), MOST);
|
|
156
217
|
const scoped = accounts?.map((one) => findAccountPk(context, one)).filter((one) => one !== undefined) ?? [];
|
|
157
218
|
if (accounts !== undefined && !scoped.length)
|
|
158
219
|
return { items: [], hasMore: false };
|
|
159
|
-
const params = [match];
|
|
160
|
-
let sql =
|
|
161
|
-
"JOIN accounts a ON a.id = t.account_id " +
|
|
220
|
+
const params = [query.match(anyWord)];
|
|
221
|
+
let sql = "SELECT t.id FROM tracker_items t " +
|
|
162
222
|
"JOIN (SELECT rowid, rank FROM tracker_item_words WHERE tracker_item_words MATCH ?) w ON w.rowid = t.id " +
|
|
163
223
|
"WHERE t.deleted_at IS NULL";
|
|
164
224
|
if (accounts !== undefined) {
|
|
@@ -171,16 +231,57 @@ export const trackerStoreOver = (context) => {
|
|
|
171
231
|
for (const kind of kinds)
|
|
172
232
|
params.push(kind);
|
|
173
233
|
}
|
|
174
|
-
sql += " ORDER BY w.rank, t.source_updated_at DESC, t.id DESC LIMIT ?
|
|
175
|
-
|
|
176
|
-
|
|
234
|
+
sql += " ORDER BY w.rank, t.source_updated_at DESC, t.id DESC LIMIT ?";
|
|
235
|
+
// One read of the index for the ids, then the rows in pages: paging the ranked query by OFFSET ranks it again for
|
|
236
|
+
// every page.
|
|
237
|
+
const ids = database
|
|
238
|
+
.prepare(sql)
|
|
239
|
+
.all(...params, CANDIDATES + 1)
|
|
240
|
+
.map(({ id }) => Number(id));
|
|
241
|
+
const capped = ids.length > CANDIDATES;
|
|
242
|
+
const rowsOf = database.prepare(`SELECT ${COLUMNS}, t.id, t.title, t.body, a.provider, a.external_id AS account FROM tracker_items t ` +
|
|
243
|
+
"JOIN accounts a ON a.id = t.account_id WHERE t.id IN (SELECT value FROM json_each(?))");
|
|
244
|
+
const wanted = offset + most + 1;
|
|
245
|
+
const found = [];
|
|
246
|
+
let read = 0;
|
|
247
|
+
// Each candidate's own words decide. All words matched rank by the index alone, so the reading stops once the page
|
|
248
|
+
// is full; some of them rank by how many matched first, over every candidate.
|
|
249
|
+
for (let at = 0; at < Math.min(ids.length, CANDIDATES); at += PAGE) {
|
|
250
|
+
if (!anyWord && found.length >= wanted)
|
|
251
|
+
break;
|
|
252
|
+
const batch = ids.slice(at, Math.min(at + PAGE, CANDIDATES));
|
|
253
|
+
const byId = new Map(rowsOf.all(JSON.stringify(batch)).map((row) => [Number(row.id), row]));
|
|
254
|
+
for (const id of batch) {
|
|
255
|
+
const row = byId.get(id);
|
|
256
|
+
if (!row)
|
|
257
|
+
continue;
|
|
258
|
+
read += 1;
|
|
259
|
+
const held = tokensOf([row.title, row.body].filter(Boolean).join("\n")).map((token) => ({
|
|
260
|
+
token,
|
|
261
|
+
literal: fold(token),
|
|
262
|
+
}));
|
|
263
|
+
const hit = query.clauses.filter((clause) => clauseIn(clause, held, exact));
|
|
264
|
+
if (anyWord ? hit.length > 0 : hit.length === query.clauses.length)
|
|
265
|
+
found.push({ row, held, hit, score: hit.length, at: read });
|
|
266
|
+
if (!anyWord && found.length >= wanted)
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const rows = found.sort((a, b) => b.score - a.score || a.at - b.at).slice(offset, wanted);
|
|
177
271
|
return {
|
|
178
|
-
items: rows.slice(0, most).map((row) =>
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
272
|
+
items: rows.slice(0, most).map(({ row, held, hit }) => {
|
|
273
|
+
const matched = held
|
|
274
|
+
.filter((word) => hit.some((clause) => clause.some((one) => one.some((probe) => wordMatches(probe, word, exact)))))
|
|
275
|
+
.map(({ token }) => token);
|
|
276
|
+
return {
|
|
277
|
+
account: { provider: String(row.provider), account: String(row.account) },
|
|
278
|
+
item: itemOf(row),
|
|
279
|
+
excerpt: excerptOf([row.title, row.body].filter(Boolean).join("\n"), [...words, ...matched]),
|
|
280
|
+
};
|
|
281
|
+
}),
|
|
282
|
+
// A capped search may hold more past what was read; an empty page ends the paging.
|
|
283
|
+
hasMore: rows.length > most || (capped && rows.length > 0),
|
|
284
|
+
...(capped ? { capped: true } : {}),
|
|
184
285
|
};
|
|
185
286
|
};
|
|
186
287
|
const accountIdsOf = (accounts) => accounts.flatMap((one) => {
|
|
@@ -248,7 +349,7 @@ export const trackerStoreOver = (context) => {
|
|
|
248
349
|
}));
|
|
249
350
|
},
|
|
250
351
|
refreshChunks: async ({ until } = {}) => refreshTrackerChunks(database, context.now(), until),
|
|
251
|
-
chunksToEmbed: async (model, options) => trackerChunksToEmbed(database, model, options),
|
|
352
|
+
chunksToEmbed: async (model, options) => trackerChunksToEmbed(database, model, options, context.now()),
|
|
252
353
|
chunksLeft: async (model) => trackerChunksLeft(database, model),
|
|
253
354
|
embedded: async (model, accounts) => trackerEmbedded(database, model, accountIdsOf(accounts).map(({ id }) => id)),
|
|
254
355
|
nearest: async (model, query, { limit, accounts }) => nearest(model, query, limit, accounts),
|
|
@@ -445,7 +445,7 @@ const addTrackerCommandsDefinition = (program, options) => {
|
|
|
445
445
|
});
|
|
446
446
|
example(paging(answerEntity(annotate(issues.command("search"), { effects: LOCAL_READ }), "tracker_hit", trackerView)
|
|
447
447
|
.description("find issues, projects and comments in the copy by their words")
|
|
448
|
-
.argument("<words...>", "every word must match;
|
|
448
|
+
.argument("<words...>", "every word must match, in any form; a word of four letters or more finds the words it begins")), 'issues search "invoice currency"').action(async (words, opts, command) => {
|
|
449
449
|
await within(command, true, async (run) => {
|
|
450
450
|
const found = await run.store.tracker.find({
|
|
451
451
|
words,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.736.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",
|