@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.
- package/CHANGELOG.md +61 -0
- package/dist/calendar/prepare.d.ts +4 -2
- package/dist/calendar/prepare.js +8 -8
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/messenger/contacts-command.js +3 -2
- package/dist/cli/messenger/doctor-command.js +14 -7
- package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
- package/dist/cli/messenger/store-maintenance-command.js +31 -4
- package/dist/cli/program.js +11 -2
- package/dist/cli/steps.js +9 -1
- package/dist/cli/store-recover.d.ts +10 -2
- package/dist/cli/store-recover.js +18 -4
- package/dist/domain/record-types.d.ts +61 -0
- package/dist/domain/record-types.js +26 -0
- package/dist/domain/records.d.ts +3 -5
- package/dist/domain/records.js +10 -13
- package/dist/entities/contact-activity.d.ts +4 -0
- package/dist/entities/contact-activity.js +1 -0
- package/dist/entities/contact-context.d.ts +20 -0
- package/dist/entities/contact-context.js +2 -0
- package/dist/entities/index.d.ts +22 -0
- package/dist/errors/failures.d.ts +17 -1
- package/dist/errors/failures.js +57 -1
- package/dist/errors/reasons.js +16 -0
- package/dist/mcp/tool.js +2 -1
- package/dist/runs/recording.js +13 -4
- package/dist/services/deps.js +4 -6
- package/dist/services/index.d.ts +1 -1
- package/dist/services/link-scorers/chat-senders.d.ts +3 -2
- package/dist/services/link-scorers/chat-senders.js +7 -4
- package/dist/services/link-scorers/mail-thread.js +1 -0
- package/dist/services/link-scorers/member-chats.js +1 -0
- package/dist/services/link-scorers/member-dialog.d.ts +1 -1
- package/dist/services/link-scorers/member-dialog.js +4 -3
- package/dist/services/link-scorers/scan.d.ts +5 -2
- package/dist/services/link-scorers/scan.js +98 -53
- package/dist/services/link-scorers/shared.d.ts +2 -2
- package/dist/services/link-scorers/shared.js +7 -4
- package/dist/services/link-scorers/types.d.ts +3 -0
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/person-context.d.ts +24 -7
- package/dist/services/person-context.js +45 -11
- package/dist/services/person-preparation.js +4 -5
- package/dist/store/damage.d.ts +3 -1
- package/dist/store/damage.js +19 -12
- package/dist/store/document-transfer/index.js +7 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +1 -0
- package/dist/store/marks.d.ts +57 -0
- package/dist/store/marks.js +121 -0
- package/dist/store/project-transfer/preview.js +12 -7
- package/dist/store/recover.js +16 -1
- package/dist/store/sqlite/finance.d.ts +8 -1
- package/dist/store/sqlite/finance.js +10 -3
- package/dist/store/sqlite/open-store.js +4 -1
- package/dist/store/sqlite/own-identities.d.ts +1 -1
- package/dist/store/sqlite/own-identities.js +7 -5
- package/dist/store/sqlite/person-links.d.ts +5 -2
- package/dist/store/sqlite/person-links.js +24 -18
- package/dist/store/sqlite/person-separation.d.ts +5 -0
- package/dist/store/sqlite/person-separation.js +26 -0
- package/dist/store/sqlite/record-projectors.d.ts +44 -0
- package/dist/store/sqlite/record-projectors.js +72 -0
- package/dist/store/sqlite/records.js +12 -70
- package/dist/store/sqlite/transaction.js +2 -5
- package/dist/store/types.d.ts +5 -0
- 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
|
|
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
|
|
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 (!
|
|
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
|
|
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
|
|
68
|
+
for (const name of FIELD_REFERENCE_ROLES)
|
|
125
69
|
stringField(fields, name);
|
|
126
|
-
const ref = type
|
|
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
|
-
|
|
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
|
-
|
|
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
|
package/dist/store/types.d.ts
CHANGED
|
@@ -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.
|
|
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.
|
|
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.
|
|
299
|
+
"@wirecat/cli-base": ">=0.80.7 <0.83.0",
|
|
300
300
|
"commander": ">=15"
|
|
301
301
|
},
|
|
302
302
|
"bin": {
|