@wirecat/cli-core 0.640.0 → 0.641.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 (36) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/dist/cli/messenger/backfill-command.js +16 -1
  3. package/dist/cli/messenger/search-answers.js +2 -0
  4. package/dist/cli/store-settings.d.ts +4 -1
  5. package/dist/cli/store-settings.js +26 -9
  6. package/dist/crm/cli/csv.js +2 -0
  7. package/dist/crm/cli/index.js +8 -2
  8. package/dist/crm/cli/search-all.js +4 -4
  9. package/dist/crm/stage-tasks.d.ts +8 -0
  10. package/dist/crm/stage-tasks.js +42 -0
  11. package/dist/domain/crm.d.ts +6 -0
  12. package/dist/entities/deal.d.ts +6 -0
  13. package/dist/entities/deal.js +3 -0
  14. package/dist/entities/index.d.ts +4 -0
  15. package/dist/runs/events.d.ts +9 -1
  16. package/dist/runs/events.js +5 -1
  17. package/dist/runs/log-events.js +2 -0
  18. package/dist/services/deal-context.js +17 -0
  19. package/dist/services/message-resolution.d.ts +1 -1
  20. package/dist/services/message-resolution.js +2 -2
  21. package/dist/services/messages-search.js +12 -5
  22. package/dist/services/search-all.d.ts +10 -3
  23. package/dist/services/search-all.js +10 -4
  24. package/dist/store/index.d.ts +1 -1
  25. package/dist/store/sqlite/crm-rows.js +2 -0
  26. package/dist/store/sqlite/crm-settings.d.ts +26 -1
  27. package/dist/store/sqlite/crm-settings.js +104 -1
  28. package/dist/store/sqlite/crm.d.ts +12 -0
  29. package/dist/store/sqlite/crm.js +4 -1
  30. package/dist/store/sqlite/deals.d.ts +28 -5
  31. package/dist/store/sqlite/deals.js +158 -14
  32. package/dist/store/sqlite/lucene-fields.js +56 -3
  33. package/dist/store/sqlite/tasks.js +9 -8
  34. package/dist/tasks/model.d.ts +6 -2
  35. package/dist/tasks/service.d.ts +2 -0
  36. package/package.json +1 -1
@@ -61,12 +61,14 @@ export const wordMatch = (corpus, match) => ({
61
61
  fts: match,
62
62
  });
63
63
  // The vocabulary is the whole store's, so a chat or date filter does not shorten it.
64
- const tooManyWords = ({ field, operator, value, span }, alone, prefix) => {
64
+ const tooManyWords = ({ field, operator, value, span }, alone, prefix, mail) => {
65
65
  const term = `${field}:${operator === "regex" ? `/${value}/` : value}`;
66
66
  const spelled = operator === "wildcard" ? value.replace(/[*?]+/gu, " ").trim().split(/\s+/u)[0] : "";
67
67
  // Measured on a synthetic 1M-word vocabulary: reading every word takes 1-2 s, the whole search's time.
68
68
  const instead = !prefix && spelled
69
- ? `a pattern that starts with * reads every indexed word — write the start of the word: ${spelled}* (it also finds the word's other forms)`
69
+ ? mail
70
+ ? `a pattern that starts with * reads every indexed word — write the start of the word: ${spelled}* (it also finds the word's other forms)`
71
+ : `a pattern without three letters in a row reads every indexed word — write three or more between the stars, or the start of the word: ${spelled}*`
70
72
  : "use a longer prefix, or body:/…/ inside one chat";
71
73
  throw new CliError("validation_error", `search: ${term}${alone ? " matches" : " and the patterns before it match"} more than ${QUERY_LIMITS.expansions} indexed words, the term expansions budget — ${instead}`, {
72
74
  reason: "query.too-large",
@@ -164,6 +166,52 @@ const stemmedText = (node, scope) => {
164
166
  })
165
167
  : bound("0"));
166
168
  };
169
+ /** A wildcard value's literal parts, split at its unescaped `*` and `?`. */
170
+ const literalRuns = (value) => {
171
+ const runs = [""];
172
+ const chars = [...value];
173
+ for (let at = 0; at < chars.length; at++) {
174
+ const c = chars[at];
175
+ if (c === "\\")
176
+ runs[runs.length - 1] += chars[++at] ?? "";
177
+ else if (c === "*" || c === "?")
178
+ runs.push("");
179
+ else
180
+ runs[runs.length - 1] += c;
181
+ }
182
+ return runs.filter(Boolean);
183
+ };
184
+ const MOST_YO_SPELLINGS = 64;
185
+ /** Every spelling of `run` with each е or ё as either: the trigram index keeps the text as written. */
186
+ const yoSpellings = (run) => {
187
+ let spellings = [""];
188
+ for (const c of run) {
189
+ spellings = spellings.flatMap((start) => (c === "е" || c === "ё" ? [`${start}е`, `${start}ё`] : [`${start}${c}`]));
190
+ if (spellings.length > MOST_YO_SPELLINGS)
191
+ return undefined;
192
+ }
193
+ return spellings;
194
+ };
195
+ /**
196
+ * A pattern with no start to read the word list from (`*договор*`, `*ость`), in chat messages: the trigram index of
197
+ * their text finds where its longest part occurs, so a large store does not refuse it: the whole search took 0.5–1 s at
198
+ * 1M messages (cli-testing performance/search/infix-chat-text). `*part*` is answered by the index alone; any other shape is
199
+ * checked word by word. It folds case, not accents: the part matches as typed, ё and е either way.
200
+ */
201
+ const insideWords = (node, automaton, scope) => {
202
+ const typed = node.value.normalize("NFC").toLowerCase();
203
+ const [longest = ""] = literalRuns(typed).sort((a, b) => [...b].length - [...a].length);
204
+ const spellings = [...longest].length >= 3 ? yoSpellings(longest) : undefined;
205
+ if (!spellings)
206
+ return undefined;
207
+ const fragment = bound("m.id IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH ?)", spellings.map(quoted).join(" OR "));
208
+ if (/^\*+[\p{L}\p{N}]+\*+$/u.test(typed))
209
+ return always(fragment);
210
+ return {
211
+ fragment: { ...fragment, exact: false },
212
+ test: (text) => (normalize(text).match(/[\p{L}\p{N}]+/gu) ?? []).some((word) => automaton.test(word, scope.budget)),
213
+ };
214
+ };
167
215
  const words = (node, scope) => {
168
216
  const { operator, value } = node;
169
217
  const { corpus } = scope;
@@ -187,11 +235,16 @@ const words = (node, scope) => {
187
235
  if (cached)
188
236
  return { node, fragment: widened(cached) };
189
237
  const prefix = prefixOf(pattern);
238
+ if (!scope.mail && operator === "wildcard" && [...prefix].length < 3) {
239
+ const inside = insideWords(node, automaton, scope);
240
+ if (inside)
241
+ return inside;
242
+ }
190
243
  const terms = scope.database
191
244
  .prepare(`SELECT term FROM ${corpus.vocab} WHERE col='normalized_text'${prefix ? " AND term>=? AND term<=?" : ""} ORDER BY term LIMIT ?`)
192
245
  .all(...(prefix ? [prefix, `${prefix}\u{10ffff}`] : []), QUERY_LIMITS.expansions + 1);
193
246
  if (scope.expand(terms.length) > QUERY_LIMITS.expansions)
194
- tooManyWords(node, terms.length > QUERY_LIMITS.expansions, prefix);
247
+ tooManyWords(node, terms.length > QUERY_LIMITS.expansions, prefix, scope.mail);
195
248
  const matches = terms.flatMap(({ term }) => {
196
249
  scope.check();
197
250
  return automaton.test(String(term), scope.budget) ? [quoted(String(term))] : [];
@@ -178,17 +178,18 @@ export const taskStoreOver = (context) => {
178
178
  const key = typedKey(database, task.kind);
179
179
  const author = actorOfOrigin(database, task.origin);
180
180
  const closedBy = task.closedBy ? actorOfOrigin(database, task.closedBy) : undefined;
181
- const title = displayTitleOf(database, {
182
- type: task.kind,
183
- title: `${task.kind} — ${task.source}`,
184
- source_locator: task.source,
185
- });
181
+ const title = task.title ??
182
+ displayTitleOf(database, {
183
+ type: task.kind,
184
+ title: `${task.kind} — ${task.source}`,
185
+ source_locator: task.source,
186
+ });
186
187
  const made = database
187
188
  .prepare("INSERT INTO tasks (project_id, number, key, title, type, status, due_at, closed_at, closed_by_type, closed_by_id, " +
188
189
  "close_reason, author_type, author_id, source, package_id, source_locator, source_kind, source_group, created_at, updated_at, " +
189
- "title_folded, close_reason_folded, category) " +
190
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
191
- .get(project, number, key, title, task.kind, task.state, time(task.dueAt), time(task.closedAt), closedBy?.type ?? null, closedBy?.id ?? null, task.reason ?? null, author.type, author.id, task.origin, task.id, task.source, task.sourceKind, task.group, task.createdAt.getTime(), now(), folded(title), folded(task.reason), task.category ?? null);
190
+ "title_folded, close_reason_folded, category, description) " +
191
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
192
+ .get(project, number, key, title, task.kind, task.state, time(task.dueAt), time(task.closedAt), closedBy?.type ?? null, closedBy?.id ?? null, task.reason ?? null, author.type, author.id, task.origin, task.id, task.source, task.sourceKind, task.group, task.createdAt.getTime(), now(), folded(title), folded(task.reason), task.category ?? null, task.description ?? null);
192
193
  recordTaskEvent(database, Number(made?.id), author, "created", null, task.createdAt.getTime());
193
194
  });
194
195
  },
@@ -11,8 +11,9 @@ export type TaskVerdict = (typeof TASK_VERDICTS)[number];
11
11
  export type ClosedState = (typeof CLOSED_STATES)[number];
12
12
  export declare const isClosedState: (state: string) => state is ClosedState;
13
13
  /**
14
- * A task points at what it is about and never holds its text: `source` is a locator the host
15
- * resolves (`msg:…` for a message), so a deleted message leaves nothing behind here.
14
+ * A task points at what it is about and never copies its source's text: `source` is a locator the host resolves
15
+ * (`msg:…` for a message), so a deleted message leaves nothing behind here. Only a rule that wrote its own `title` and
16
+ * `description`, such as an owner's stage rule naming a deal, stores text with it.
16
17
  */
17
18
  export interface Task {
18
19
  id: string;
@@ -32,6 +33,9 @@ export interface Task {
32
33
  key?: string;
33
34
  /** One name from the host's category list; a host without one leaves it unset. */
34
35
  category?: string;
36
+ /** Given by a rule that knows what to do, such as an owner's stage rule; a found task's comes from its source. */
37
+ title?: string;
38
+ description?: string;
35
39
  /** Who works on it now, until `claimedUntil`; a lapsed claim holds nothing. */
36
40
  claimedBy?: string;
37
41
  claimedUntil?: Date;
@@ -9,6 +9,8 @@ export interface NewTask {
9
9
  origin: TaskOrigin;
10
10
  dueAt?: Date;
11
11
  category?: string;
12
+ title?: string;
13
+ description?: string;
12
14
  }
13
15
  export interface CloseOptions {
14
16
  as: ClosedState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.640.0",
3
+ "version": "0.641.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",