@wirecat/cli-core 0.604.0 → 0.605.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 (68) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/dist/calendar/prepare.d.ts +4 -2
  3. package/dist/calendar/prepare.js +8 -8
  4. package/dist/cli/index.d.ts +1 -1
  5. package/dist/cli/index.js +1 -1
  6. package/dist/cli/messenger/contacts-command.js +3 -2
  7. package/dist/cli/messenger/doctor-command.js +14 -7
  8. package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
  9. package/dist/cli/messenger/store-maintenance-command.js +31 -4
  10. package/dist/cli/program.js +11 -2
  11. package/dist/cli/steps.js +9 -1
  12. package/dist/cli/store-recover.d.ts +10 -2
  13. package/dist/cli/store-recover.js +18 -4
  14. package/dist/domain/record-types.d.ts +61 -0
  15. package/dist/domain/record-types.js +26 -0
  16. package/dist/domain/records.d.ts +3 -5
  17. package/dist/domain/records.js +10 -13
  18. package/dist/entities/contact-activity.d.ts +4 -0
  19. package/dist/entities/contact-activity.js +1 -0
  20. package/dist/entities/contact-context.d.ts +20 -0
  21. package/dist/entities/contact-context.js +2 -0
  22. package/dist/entities/index.d.ts +22 -0
  23. package/dist/errors/failures.d.ts +17 -1
  24. package/dist/errors/failures.js +57 -1
  25. package/dist/errors/reasons.js +16 -0
  26. package/dist/mcp/tool.js +2 -1
  27. package/dist/runs/recording.js +13 -4
  28. package/dist/services/deps.js +4 -6
  29. package/dist/services/index.d.ts +1 -1
  30. package/dist/services/link-scorers/chat-senders.d.ts +3 -2
  31. package/dist/services/link-scorers/chat-senders.js +7 -4
  32. package/dist/services/link-scorers/mail-thread.js +1 -0
  33. package/dist/services/link-scorers/member-chats.js +1 -0
  34. package/dist/services/link-scorers/member-dialog.d.ts +1 -1
  35. package/dist/services/link-scorers/member-dialog.js +4 -3
  36. package/dist/services/link-scorers/scan.d.ts +5 -2
  37. package/dist/services/link-scorers/scan.js +98 -53
  38. package/dist/services/link-scorers/shared.d.ts +2 -2
  39. package/dist/services/link-scorers/shared.js +7 -4
  40. package/dist/services/link-scorers/types.d.ts +3 -0
  41. package/dist/services/metadata.d.ts +1 -1
  42. package/dist/services/person-context.d.ts +24 -7
  43. package/dist/services/person-context.js +45 -11
  44. package/dist/services/person-preparation.js +4 -5
  45. package/dist/store/damage.d.ts +3 -1
  46. package/dist/store/damage.js +19 -12
  47. package/dist/store/document-transfer/index.js +7 -0
  48. package/dist/store/index.d.ts +1 -0
  49. package/dist/store/index.js +1 -0
  50. package/dist/store/marks.d.ts +57 -0
  51. package/dist/store/marks.js +121 -0
  52. package/dist/store/project-transfer/preview.js +12 -7
  53. package/dist/store/recover.js +16 -1
  54. package/dist/store/sqlite/finance.d.ts +8 -1
  55. package/dist/store/sqlite/finance.js +10 -3
  56. package/dist/store/sqlite/open-store.js +4 -1
  57. package/dist/store/sqlite/own-identities.d.ts +1 -1
  58. package/dist/store/sqlite/own-identities.js +7 -5
  59. package/dist/store/sqlite/person-links.d.ts +5 -2
  60. package/dist/store/sqlite/person-links.js +24 -18
  61. package/dist/store/sqlite/person-separation.d.ts +5 -0
  62. package/dist/store/sqlite/person-separation.js +26 -0
  63. package/dist/store/sqlite/record-projectors.d.ts +44 -0
  64. package/dist/store/sqlite/record-projectors.js +72 -0
  65. package/dist/store/sqlite/records.js +12 -70
  66. package/dist/store/sqlite/transaction.js +2 -5
  67. package/dist/store/types.d.ts +5 -0
  68. package/package.json +3 -3
@@ -1,22 +1,14 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
2
  import { formatDecimal, minorUnits, scaleOf } from "../../domain/money.js";
3
+ import { rolesOf } from "../../domain/record-types.js";
3
4
  import { pointerOf, recordFingerprint, } from "../../domain/records.js";
4
5
  import { accountPk, findAccountPk } from "./accounts.js";
5
6
  import { financeOperationsOver } from "./finance.js";
6
7
  import { requiredThing } from "./notes.js";
8
+ import { FIELD_REFERENCE_ROLES, IMPORTER, isProjected, PROJECTED_TYPES, RECORD_PROJECTORS, refusal, stringField, } from "./record-projectors.js";
7
9
  import { referenceOfThing, storedThing } from "./things.js";
8
10
  import { read, write } from "./transaction.js";
9
- const PROJECTED = {
10
- invoice: { roles: ["issuer", "counterparty", "project"], many: [] },
11
- payment: { roles: ["payer", "payee", "project", "settles"], many: ["settles"] },
12
- };
13
- const isProjected = (type) => type === "invoice" || type === "payment";
14
- const IMPORTER = { bot: "import" };
15
- const REFERENCE_FIELDS = ["issuer", "counterparty", "project", "payer", "payee"];
16
- /** `at` names the record's member at fault; a refused entity is the fields' fault. */
17
- const refusal = (message, code, at) => {
18
- throw new CliError("validation_error", message, { reason: code, ...(at === undefined ? {} : { at }) });
19
- };
11
+ const PROJECTED_TYPES_SQL = PROJECTED_TYPES.map((type) => `'${type}'`).join(", ");
20
12
  const codeOf = (error) => {
21
13
  const reason = error.details?.reason;
22
14
  if (typeof reason === "string")
@@ -26,56 +18,6 @@ const codeOf = (error) => {
26
18
  /** A record's fingerprint without its settles relations: what an import may not change on a payment it proposed. */
27
19
  const unsettled = (fields, relations) => recordFingerprint(fields, relations.filter((relation) => relation.role !== "settles"));
28
20
  const iso = (value) => new Date(Number(value)).toISOString();
29
- const stringField = (fields, name) => {
30
- const value = fields[name];
31
- if (value === undefined || value === null)
32
- return undefined;
33
- if (typeof value !== "string")
34
- return refusal(`fields.${name} is text`, "record.invalid");
35
- return value;
36
- };
37
- const invoiceOf = (fields, related, source) => {
38
- if (!Array.isArray(fields.lines) || fields.lines.some((line) => typeof line !== "object" || line === null))
39
- return refusal("an invoice's fields.lines is a list of lines", "record.invalid");
40
- const one = (role) => related.get(role)?.[0] ?? stringField(fields, role);
41
- return {
42
- ...written(source),
43
- direction: fields.direction,
44
- number: stringField(fields, "number"),
45
- issuer: one("issuer"),
46
- counterparty: one("counterparty"),
47
- project: one("project"),
48
- currency: fields.currency,
49
- issueDate: stringField(fields, "issue_date"),
50
- dueDate: stringField(fields, "due_date"),
51
- total: fields.total,
52
- lines: fields.lines.map((line) => ({
53
- description: line.description,
54
- quantity: line.quantity,
55
- unitPrice: line.unit_price,
56
- amount: line.amount,
57
- })),
58
- };
59
- };
60
- const paymentOf = (fields, related, source) => {
61
- const one = (role) => related.get(role)?.[0] ?? stringField(fields, role);
62
- return {
63
- ...written(source),
64
- direction: fields.direction,
65
- payer: one("payer"),
66
- payee: one("payee"),
67
- project: one("project"),
68
- amount: fields.amount,
69
- currency: fields.currency,
70
- date: fields.date,
71
- reference: stringField(fields, "reference"),
72
- };
73
- };
74
- const written = (source) => ({
75
- by: IMPORTER,
76
- source: `import:${source.provider}`,
77
- reason: `imported from ${source.provider} account ${source.account}, batch ${source.batch_id}`,
78
- });
79
21
  export const recordStoreOver = (context) => {
80
22
  const { database, now } = context;
81
23
  const finance = financeOperationsOver(context);
@@ -91,7 +33,9 @@ export const recordStoreOver = (context) => {
91
33
  const entityRef = (type, id) => type == null ? null : (referenceOfThing(database, storedThing(type, id)) ?? `${String(type)}:${String(id)}`);
92
34
  /** Roles a projection needs whose target has no entity yet, the entities of the rest, and what each invoice gets. */
93
35
  const relatedOf = (recordId, type) => {
94
- const { roles, many } = PROJECTED[type];
36
+ const roles = rolesOf(type);
37
+ const known = roles.map(([name]) => name);
38
+ const many = roles.flatMap(([name, role]) => (role.many ? [name] : []));
95
39
  const rows = sql("SELECT rr.role, rr.amount, t.entity_type, t.entity_id FROM record_relations rr " +
96
40
  "LEFT JOIN records t ON t.id = rr.target_id AND t.deleted_at IS NULL WHERE rr.record_id = ? ORDER BY rr.id").all(recordId);
97
41
  const related = new Map();
@@ -99,7 +43,7 @@ export const recordStoreOver = (context) => {
99
43
  const missing = new Set();
100
44
  for (const row of rows) {
101
45
  const role = String(row.role);
102
- if (!roles.includes(role))
46
+ if (!known.includes(role))
103
47
  continue;
104
48
  const ref = entityRef(row.entity_type, row.entity_id);
105
49
  if (ref === null)
@@ -114,18 +58,16 @@ export const recordStoreOver = (context) => {
114
58
  refusal(`a record has one ${role}`, "record.relation-ambiguous");
115
59
  return { related, shares, missing: [...missing] };
116
60
  };
117
- /** Makes the proposal a waiting invoice or payment record stands for, once every reference it names has an entity. */
61
+ /** Makes the proposal a waiting record stands for, once every reference it names has an entity. */
118
62
  const project = (row, source) => {
119
63
  const type = row.type;
120
64
  const { related, shares, missing } = relatedOf(Number(row.id), type);
121
65
  if (missing.length > 0)
122
66
  return { missing };
123
67
  const fields = JSON.parse(String(row.fields));
124
- for (const name of REFERENCE_FIELDS)
68
+ for (const name of FIELD_REFERENCE_ROLES)
125
69
  stringField(fields, name);
126
- const ref = type === "invoice"
127
- ? finance.addInvoice(invoiceOf(fields, related, source)).ref
128
- : settle(finance.addPayment(paymentOf(fields, related, source)), shares);
70
+ const ref = RECORD_PROJECTORS[type](fields, { related, shares }, source, { finance, settle });
129
71
  const entity = requiredThing(database, ref);
130
72
  sql("UPDATE records SET entity_type = ?, entity_id = ?, projected_fingerprint = fingerprint WHERE id = ?").run(entity.type, entity.id, Number(row.id));
131
73
  return { proposed: ref, missing: [] };
@@ -211,7 +153,7 @@ export const recordStoreOver = (context) => {
211
153
  const queue = [target];
212
154
  while (queue.length > 0) {
213
155
  const rows = sql("SELECT DISTINCT r.* FROM record_relations rr JOIN records r ON r.id = rr.record_id WHERE rr.target_id = ? " +
214
- "AND r.entity_id IS NULL AND r.deleted_at IS NULL AND r.type IN ('invoice', 'payment')").all(queue.shift());
156
+ `AND r.entity_id IS NULL AND r.deleted_at IS NULL AND r.type IN (${PROJECTED_TYPES_SQL})`).all(queue.shift());
215
157
  for (const row of rows) {
216
158
  try {
217
159
  const done = write(database, () => project(row, source));
@@ -434,7 +376,7 @@ export const recordStoreOver = (context) => {
434
376
  return [];
435
377
  const rows = database
436
378
  .prepare(`${SELECT}WHERE a.provider = ? AND a.external_id = ? AND (? IS NULL OR r.type = ?) ` +
437
- "AND (? IS NULL OR (r.entity_id IS NULL AND r.deleted_at IS NULL AND r.type IN ('invoice', 'payment'))) " +
379
+ `AND (? IS NULL OR (r.entity_id IS NULL AND r.deleted_at IS NULL AND r.type IN (${PROJECTED_TYPES_SQL}))) ` +
438
380
  "AND (? IS NULL OR (r.entity_id IS NOT NULL AND (r.deleted_at IS NOT NULL " +
439
381
  "OR r.projected_fingerprint IS NOT r.fingerprint))) ORDER BY r.updated_at DESC, r.id DESC LIMIT ?")
440
382
  .all(provider, account, type ?? null, type ?? null, waiting ? 1 : null, flagged ? 1 : null, limit);
@@ -1,13 +1,10 @@
1
+ import { sqliteCode } from "../../errors/failures.js";
1
2
  import { drainInvolvementQueue } from "./involvement-queue.js";
2
3
  import { drainMentions } from "./mention-drain.js";
3
4
  import { drainStems, stemmerCache } from "./stems.js";
4
5
  const SQLITE_BUSY = 5;
5
6
  const pause = new Int32Array(new SharedArrayBuffer(4));
6
- export const isBusy = (error) => {
7
- const { errcode, errno } = (error ?? {});
8
- const code = errcode ?? errno;
9
- return typeof code === "number" && (code & 0xff) === SQLITE_BUSY;
10
- };
7
+ export const isBusy = (error) => sqliteCode(error) === SQLITE_BUSY;
11
8
  /**
12
9
  * Takes the write lock, waiting as long as the connection's `busy_timeout`. SQLite's own wait sleeps 1 ms, then up to
13
10
  * 100 ms between tries, so a process that commits and begins again at once is never seen free and the waiter fails
@@ -53,6 +53,11 @@ export interface LinkedIdentity extends IdentityRef {
53
53
  isBot: boolean | null;
54
54
  /** The accounts in this store that have seen it. */
55
55
  accounts: Id[];
56
+ /** The same accounts with their scope: a person joined by email may be seen by personal and work accounts. */
57
+ seenIn: {
58
+ account: Id;
59
+ scope: StoredAccount["scope"];
60
+ }[];
56
61
  method: string;
57
62
  linkedBy: string;
58
63
  linkedAt: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.604.0",
3
+ "version": "0.605.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",
@@ -276,7 +276,7 @@
276
276
  "@types/node": "^22.20.5",
277
277
  "@types/pngjs": "6.0.5",
278
278
  "@vitest/coverage-v8": "^5.0.5",
279
- "@wirecat/cli-base": "0.80.7",
279
+ "@wirecat/cli-base": "0.82.0",
280
280
  "@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
281
281
  "commander": "15.0.0",
282
282
  "cspell": "10.4.0",
@@ -296,7 +296,7 @@
296
296
  },
297
297
  "peerDependencies": {
298
298
  "@napi-rs/canvas": ">=0.1.69 <2",
299
- "@wirecat/cli-base": ">=0.80.7 <0.81.0",
299
+ "@wirecat/cli-base": ">=0.80.7 <0.83.0",
300
300
  "commander": ">=15"
301
301
  },
302
302
  "bin": {