@wirecat/cli-core 0.603.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 +92 -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/search-answers.js +8 -1
- package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
- package/dist/cli/messenger/store-maintenance-command.js +31 -4
- package/dist/cli/model-catalogue-command.js +28 -2
- 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/search/lucene/plain.d.ts +2 -1
- package/dist/search/lucene/plain.js +15 -1
- 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/model-downloads.d.ts +5 -3
- package/dist/services/model-downloads.js +6 -6
- package/dist/services/notes-search.js +4 -2
- 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/services/search-all.d.ts +6 -0
- package/dist/services/search-all.js +16 -8
- package/dist/speech/install.d.ts +6 -7
- package/dist/speech/install.js +17 -10
- 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/note-dates.d.ts +6 -0
- package/dist/store/sqlite/note-dates.js +23 -0
- package/dist/store/sqlite/notes.d.ts +13 -0
- package/dist/store/sqlite/notes.js +6 -1
- 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
- package/parity.json +3 -1
|
@@ -13,6 +13,7 @@ import { PREFERRED_FILE, syncFile } from "./files.js";
|
|
|
13
13
|
import { importedDocumentWhere } from "./imported-documents.js";
|
|
14
14
|
import { LINK_KINDS, LINK_ORIGINS, linkKind } from "./link-kinds.js";
|
|
15
15
|
import { reviewLink } from "./link-reviews.js";
|
|
16
|
+
import { dateFileNotes } from "./note-dates.js";
|
|
16
17
|
import { holdingText } from "./note-index.js";
|
|
17
18
|
import { addTags, ensureTag, labelsOf } from "./tags.js";
|
|
18
19
|
import { referenceOfThing, storedThing, thingOf } from "./things.js";
|
|
@@ -46,6 +47,7 @@ export const documentOf = (row) => ({
|
|
|
46
47
|
contentHash: row.content_hash == null ? null : String(row.content_hash),
|
|
47
48
|
revision: Number(row.revision),
|
|
48
49
|
exportPath: row.export_path == null ? null : String(row.export_path),
|
|
50
|
+
modifiedAt: optionalIso(row.external_updated_at),
|
|
49
51
|
createdAt: iso(row.created_at),
|
|
50
52
|
updatedAt: iso(row.updated_at),
|
|
51
53
|
deletedAt: optionalIso(row.deleted_at),
|
|
@@ -62,6 +64,7 @@ export const noteOf = (row) => ({
|
|
|
62
64
|
contentHash: null,
|
|
63
65
|
revision: Number(row.revision),
|
|
64
66
|
exportPath: null,
|
|
67
|
+
modifiedAt: null,
|
|
65
68
|
createdAt: iso(row.created_at),
|
|
66
69
|
updatedAt: iso(row.updated_at),
|
|
67
70
|
deletedAt: optionalIso(row.deleted_at),
|
|
@@ -484,6 +487,7 @@ export const notesStoreOver = (context) => {
|
|
|
484
487
|
});
|
|
485
488
|
return noteAt(id, folder, target);
|
|
486
489
|
}),
|
|
490
|
+
dateFileNotes: async (folderId, dates) => write(database, () => dateFileNotes(database, Number(folderRow(folderId).id), dates)),
|
|
487
491
|
deleteFileNotes: async (folderId, paths) => write(database, () => {
|
|
488
492
|
const folder = Number(folderRow(folderId).id);
|
|
489
493
|
let deleted = 0;
|
|
@@ -557,7 +561,8 @@ export const notesStoreOver = (context) => {
|
|
|
557
561
|
(folder === null
|
|
558
562
|
? "SELECT d.* FROM documents d WHERE d.deleted_at IS NULL AND ? IS NULL AND ? IS NULL "
|
|
559
563
|
: "SELECT d.*, f.account_id AS account_id, CASE WHEN a.provider IN ('local', 'obsidian') " +
|
|
560
|
-
"THEN f.external_id ELSE d.external_id END AS external_id
|
|
564
|
+
"THEN f.external_id ELSE d.external_id END AS external_id, " +
|
|
565
|
+
"coalesce(f.external_updated_at, d.external_updated_at) AS external_updated_at FROM files f " +
|
|
561
566
|
"JOIN documents d ON d.id = f.document_id JOIN accounts a ON a.id = f.account_id " +
|
|
562
567
|
"WHERE f.deleted_at IS NULL AND d.deleted_at IS NULL AND f.account_id = ? AND ? IS NOT NULL ") +
|
|
563
568
|
"AND coalesce(json_extract(d.metadata, '$.catalog.active'), 1)=1 " +
|
|
@@ -2,6 +2,7 @@ import { accessSync, constants, existsSync, mkdirSync, statSync, writeFileSync }
|
|
|
2
2
|
import { dirname } from "node:path";
|
|
3
3
|
import { CliError } from "@wirecat/cli-base";
|
|
4
4
|
import { DEFAULT_DECISION_POLICY } from "../decisions.js";
|
|
5
|
+
import { refuseDamagedStore } from "../marks.js";
|
|
5
6
|
import { migrate } from "../migrations.js";
|
|
6
7
|
import { storeCapable } from "../open.js";
|
|
7
8
|
import { refuseOwnerStoreInTests, storePath } from "../path.js";
|
|
@@ -78,8 +79,10 @@ export const openStoreContext = async (options = {}) => {
|
|
|
78
79
|
if (!statSync(file, { throwIfNoEntry: false })?.isFile())
|
|
79
80
|
throw new CliError("not_found", "The store does not exist as a file; read-only opening does not create it");
|
|
80
81
|
}
|
|
81
|
-
else
|
|
82
|
+
else {
|
|
82
83
|
createWritable(file);
|
|
84
|
+
refuseDamagedStore(file, command);
|
|
85
|
+
}
|
|
83
86
|
const opened = readOnly ? await openReadable(file, options) : await openSqlite(file, { readOnly });
|
|
84
87
|
const database = opened.database;
|
|
85
88
|
try {
|
|
@@ -3,7 +3,7 @@ import type { StoreContext } from "./open.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* An account's own identity (same provider and id as the account) is the owner. Messengers save it like anyone
|
|
5
5
|
* else's, as a person of its own, so the owner's name counted as a contact's. Links it, and whoever was linked
|
|
6
|
-
* with it, to the owner; nothing to do once done.
|
|
6
|
+
* with it, to the owner, unless the owner separated that person from themselves; nothing to do once done.
|
|
7
7
|
*/
|
|
8
8
|
export declare const linkOwnIdentities: (context: StoreContext) => number;
|
|
9
9
|
/** `contacts link <person> me`: an identity the account id does not name, a Zoom user for one, is the owner. */
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { ownerPerson } from "./actors.js";
|
|
3
3
|
import { mergePerson, personOf } from "./person-links.js";
|
|
4
|
+
import { separated } from "./person-separation.js";
|
|
4
5
|
import { write } from "./transaction.js";
|
|
5
6
|
/**
|
|
6
7
|
* An account's own identity (same provider and id as the account) is the owner. Messengers save it like anyone
|
|
7
8
|
* else's, as a person of its own, so the owner's name counted as a contact's. Links it, and whoever was linked
|
|
8
|
-
* with it, to the owner; nothing to do once done.
|
|
9
|
+
* with it, to the owner, unless the owner separated that person from themselves; nothing to do once done.
|
|
9
10
|
*/
|
|
10
11
|
export const linkOwnIdentities = (context) => {
|
|
11
12
|
const owner = ownerPerson(context.database).id;
|
|
@@ -15,11 +16,12 @@ export const linkOwnIdentities = (context) => {
|
|
|
15
16
|
.all(owner);
|
|
16
17
|
if (strays.length === 0)
|
|
17
18
|
return 0;
|
|
18
|
-
write(context.database, () => {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
return write(context.database, () => {
|
|
20
|
+
const joining = strays.map(({ person }) => Number(person)).filter((person) => !separated(context, person, owner));
|
|
21
|
+
for (const person of joining)
|
|
22
|
+
mergePerson(context, person, owner, { method: "account", by: "rules" });
|
|
23
|
+
return joining.length;
|
|
21
24
|
});
|
|
22
|
-
return strays.length;
|
|
23
25
|
};
|
|
24
26
|
/** `contacts link <person> me`: an identity the account id does not name, a Zoom user for one, is the owner. */
|
|
25
27
|
export const linkToOwner = (context, ref) => {
|
|
@@ -5,7 +5,10 @@ import type { StoreContext } from "./open.js";
|
|
|
5
5
|
export declare const personByUid: (context: StoreContext, uid: string) => PersonRecord | undefined;
|
|
6
6
|
/** The person an identity belongs to, with every identity linked to them. */
|
|
7
7
|
export declare const personOf: (context: StoreContext, ref: IdentityRef) => PersonRecord | undefined;
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* A reviewed meeting participant association moves only its selected identity. Only the owner may move it back
|
|
10
|
+
* over a separation the owner made.
|
|
11
|
+
*/
|
|
9
12
|
export declare const linkIdentityToPerson: (context: StoreContext, identityPk: number, target: number, options: LinkOptions) => void;
|
|
10
13
|
/**
|
|
11
14
|
* Makes `other` and everyone already linked to it the same person as `person`. Never inferred from a
|
|
@@ -17,7 +20,7 @@ export declare const mergePerson: (context: StoreContext, from: number, to: numb
|
|
|
17
20
|
/**
|
|
18
21
|
* Joins the identity to the person of the mail contact with this exact address, making that contact when mail has
|
|
19
22
|
* not seen it yet, so whichever import comes second lands on one person. The contact is seen by no account, so it is
|
|
20
|
-
* no one's mail correspondent. An owner's unlink wins: two
|
|
23
|
+
* no one's mail correspondent. An owner's unlink wins: two people the owner separated stay apart.
|
|
21
24
|
*/
|
|
22
25
|
export declare const joinByAddress: (context: StoreContext, ref: IdentityRef, address: string, by: string) => void;
|
|
23
26
|
/** Gives the identity a person of its own again; the ones left keep theirs. */
|
|
@@ -2,6 +2,7 @@ import { CliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { and, eq, inArray, isNull, sql } from "./drizzle/core.js";
|
|
3
3
|
import { identityOf } from "./identities.js";
|
|
4
4
|
import { resolvePersonLinks } from "./person-resolution.js";
|
|
5
|
+
import { identitySeparated, separated } from "./person-separation.js";
|
|
5
6
|
import { accountIdentities, accounts, chats, identities, identityLinkEvents, identityLinks, messages, persons, } from "./schema.js";
|
|
6
7
|
import { hitsWhere } from "./search.js";
|
|
7
8
|
const identityRow = ({ orm }, { provider, id }) => orm
|
|
@@ -38,17 +39,23 @@ const recordOf = ({ orm }, personPk) => {
|
|
|
38
39
|
.orderBy(identities.provider, identities.externalId)
|
|
39
40
|
.all();
|
|
40
41
|
const seen = orm
|
|
41
|
-
.select({ identityPk: accountIdentities.identityId, account: accounts.externalId })
|
|
42
|
+
.select({ identityPk: accountIdentities.identityId, account: accounts.externalId, scope: accounts.scope })
|
|
42
43
|
.from(accountIdentities)
|
|
43
44
|
.innerJoin(accounts, eq(accounts.id, accountIdentities.accountId))
|
|
44
45
|
.where(inArray(accountIdentities.identityId, rows.map((row) => row.pk)))
|
|
45
46
|
.all();
|
|
46
|
-
const identitiesOf = rows.map(({ pk, isBot, linkedAt, ...row }) =>
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
const identitiesOf = rows.map(({ pk, isBot, linkedAt, ...row }) => {
|
|
48
|
+
const seenIn = seen
|
|
49
|
+
.filter((one) => one.identityPk === pk)
|
|
50
|
+
.map(({ account, scope }) => ({ account, scope: scope }));
|
|
51
|
+
return {
|
|
52
|
+
...row,
|
|
53
|
+
isBot: isBot === null ? null : isBot === 1,
|
|
54
|
+
accounts: seenIn.map((one) => one.account),
|
|
55
|
+
seenIn,
|
|
56
|
+
linkedAt: new Date(linkedAt).toISOString(),
|
|
57
|
+
};
|
|
58
|
+
});
|
|
52
59
|
return { uid: String(person.id), name: person.name, identities: identitiesOf };
|
|
53
60
|
};
|
|
54
61
|
/** A person by the uid a `person:` reference holds, with every identity linked to them. */
|
|
@@ -75,13 +82,20 @@ const move = (context, identityPk, from, to, { method, by }) => {
|
|
|
75
82
|
.values({ identityId: identityPk, fromPersonId: from, toPersonId: to, method, createdAt: at, author: by })
|
|
76
83
|
.run();
|
|
77
84
|
};
|
|
78
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* A reviewed meeting participant association moves only its selected identity. Only the owner may move it back
|
|
87
|
+
* over a separation the owner made.
|
|
88
|
+
*/
|
|
79
89
|
export const linkIdentityToPerson = (context, identityPk, target, options) => {
|
|
80
90
|
const found = context.orm
|
|
81
91
|
.select({ personId: identityLinks.personId })
|
|
82
92
|
.from(identityLinks)
|
|
83
93
|
.where(eq(identityLinks.identityId, identityPk))
|
|
84
94
|
.get();
|
|
95
|
+
if (options.by !== "owner" && found?.personId !== target && identitySeparated(context, identityPk, target))
|
|
96
|
+
throw new CliError("conflict", "the owner separated this identity from that person; only the owner links it again", {
|
|
97
|
+
reason: "identity.separated",
|
|
98
|
+
});
|
|
85
99
|
if (found) {
|
|
86
100
|
move(context, identityPk, found.personId, target, options);
|
|
87
101
|
return;
|
|
@@ -136,18 +150,10 @@ export const mergePerson = (context, from, to, options) => {
|
|
|
136
150
|
for (const { identityPk } of together)
|
|
137
151
|
move(context, identityPk, from, to, options);
|
|
138
152
|
};
|
|
139
|
-
/** The owner once moved one of the two identities away from a person the other belonged to. */
|
|
140
|
-
const separatedByOwner = ({ database }, one, other) => database
|
|
141
|
-
.prepare(`SELECT 1 FROM identity_link_events moved
|
|
142
|
-
JOIN identity_link_events held ON held.to_person_id = moved.from_person_id
|
|
143
|
-
WHERE moved.author = 'owner' AND ((moved.identity_id = ? AND held.identity_id = ?)
|
|
144
|
-
OR (moved.identity_id = ? AND held.identity_id = ?))
|
|
145
|
-
LIMIT 1`)
|
|
146
|
-
.get(one, other, other, one) !== undefined;
|
|
147
153
|
/**
|
|
148
154
|
* Joins the identity to the person of the mail contact with this exact address, making that contact when mail has
|
|
149
155
|
* not seen it yet, so whichever import comes second lands on one person. The contact is seen by no account, so it is
|
|
150
|
-
* no one's mail correspondent. An owner's unlink wins: two
|
|
156
|
+
* no one's mail correspondent. An owner's unlink wins: two people the owner separated stay apart.
|
|
151
157
|
*/
|
|
152
158
|
export const joinByAddress = (context, ref, address, by) => {
|
|
153
159
|
const key = address.trim().toLowerCase();
|
|
@@ -162,7 +168,7 @@ export const joinByAddress = (context, ref, address, by) => {
|
|
|
162
168
|
if (!mail)
|
|
163
169
|
identityOf(context, "email", key, null);
|
|
164
170
|
const contact = required(context, { provider: "email", id: mail ? String(mail.external_id) : key });
|
|
165
|
-
if (contact.personPk === found.personPk ||
|
|
171
|
+
if (contact.personPk === found.personPk || separated(context, found.personPk, contact.personPk))
|
|
166
172
|
return;
|
|
167
173
|
mergePerson(context, found.personPk, contact.personPk, { method: "same-email", by });
|
|
168
174
|
resolvePersonLinks(context.database, recordOf(context, contact.personPk).identities.flatMap(({ name, username }) => [name, username]));
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StoreContext } from "./open.js";
|
|
2
|
+
/** Whether the owner separated anyone of `one` from anyone of `other`; nothing automatic may join them again. */
|
|
3
|
+
export declare const separated: (context: StoreContext, one: number, other: number) => boolean;
|
|
4
|
+
/** Whether the owner separated this one identity from `person` or from anyone of it. */
|
|
5
|
+
export declare const identitySeparated: (context: StoreContext, identity: number, person: number) => boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const identitiesOf = ({ database }, person) => database
|
|
2
|
+
.prepare("SELECT identity_id AS pk FROM identity_links WHERE person_id = ?")
|
|
3
|
+
.all(person)
|
|
4
|
+
.map(({ pk }) => Number(pk));
|
|
5
|
+
const MOVED_AWAY = `moved.identity_id IN (SELECT value FROM json_each(?))
|
|
6
|
+
AND (moved.from_person_id IN (SELECT value FROM json_each(?))
|
|
7
|
+
OR EXISTS (SELECT 1 FROM identity_link_events held WHERE held.to_person_id = moved.from_person_id
|
|
8
|
+
AND held.identity_id IN (SELECT value FROM json_each(?))))`;
|
|
9
|
+
/**
|
|
10
|
+
* The owner once moved an identity of one side out of the other side's person, or out of a person that held an
|
|
11
|
+
* identity of the other side. Checked over whole people because a merge moves whole people: a pair check lets a
|
|
12
|
+
* third identity carry two separated people back together.
|
|
13
|
+
*/
|
|
14
|
+
const apart = ({ database }, one, other) => {
|
|
15
|
+
const ids = (side) => JSON.stringify(side.identities);
|
|
16
|
+
const person = (side) => JSON.stringify(side.person === undefined ? [] : [side.person]);
|
|
17
|
+
return (database
|
|
18
|
+
.prepare(`SELECT 1 FROM identity_link_events moved WHERE moved.author = 'owner'
|
|
19
|
+
AND ((${MOVED_AWAY}) OR (${MOVED_AWAY})) LIMIT 1`)
|
|
20
|
+
.get(ids(one), person(other), ids(other), ids(other), person(one), ids(one)) !== undefined);
|
|
21
|
+
};
|
|
22
|
+
/** Whether the owner separated anyone of `one` from anyone of `other`; nothing automatic may join them again. */
|
|
23
|
+
export const separated = (context, one, other) => one !== other &&
|
|
24
|
+
apart(context, { identities: identitiesOf(context, one), person: one }, { identities: identitiesOf(context, other), person: other });
|
|
25
|
+
/** Whether the owner separated this one identity from `person` or from anyone of it. */
|
|
26
|
+
export const identitySeparated = (context, identity, person) => apart(context, { identities: [identity] }, { identities: identitiesOf(context, person).filter((pk) => pk !== identity), person });
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type RecordEntityType } from "../../domain/record-types.js";
|
|
2
|
+
import type { EnvelopeSource } from "../../domain/records.js";
|
|
3
|
+
import type { financeOperationsOver, PaymentDetail } from "./finance.js";
|
|
4
|
+
export type Row = Record<string, unknown>;
|
|
5
|
+
export interface Share {
|
|
6
|
+
invoice: string;
|
|
7
|
+
/** As the source gave it; `null` settles as far as both allow. */
|
|
8
|
+
amount: string | null;
|
|
9
|
+
}
|
|
10
|
+
/** The entities a record's relations resolved to, by role, and what each invoice a payment settles gets. */
|
|
11
|
+
export interface Related {
|
|
12
|
+
related: Map<string, string[]>;
|
|
13
|
+
shares: Share[];
|
|
14
|
+
}
|
|
15
|
+
export interface ProjectionTools {
|
|
16
|
+
finance: ReturnType<typeof financeOperationsOver>;
|
|
17
|
+
settle: (made: PaymentDetail, shares: Share[]) => string;
|
|
18
|
+
}
|
|
19
|
+
/** Makes the proposal a record stands for and returns its ref. */
|
|
20
|
+
export type Projector = (fields: Row, related: Related, source: EnvelopeSource, tools: ProjectionTools) => string;
|
|
21
|
+
export declare const IMPORTER: {
|
|
22
|
+
readonly bot: "import";
|
|
23
|
+
};
|
|
24
|
+
/** `at` names the record's member at fault; a refused entity is the fields' fault. */
|
|
25
|
+
export declare const refusal: (message: string, code: string, at?: string | (string | number)[]) => never;
|
|
26
|
+
export declare const stringField: (fields: Row, name: string) => string | undefined;
|
|
27
|
+
/** `null` for a type whose entity only the owner's `map` gives. */
|
|
28
|
+
export declare const RECORD_PROJECTORS: {
|
|
29
|
+
readonly invoice: (fields: Row, { related }: Related, source: EnvelopeSource, { finance }: ProjectionTools) => string;
|
|
30
|
+
readonly payment: (fields: Row, { related, shares }: Related, source: EnvelopeSource, { finance, settle }: ProjectionTools) => string;
|
|
31
|
+
readonly person: null;
|
|
32
|
+
readonly organization: null;
|
|
33
|
+
readonly project: null;
|
|
34
|
+
};
|
|
35
|
+
export type ProjectedType = {
|
|
36
|
+
[K in RecordEntityType]: (typeof RECORD_PROJECTORS)[K] extends null ? never : K;
|
|
37
|
+
}[RecordEntityType];
|
|
38
|
+
export declare const PROJECTED_TYPES: ProjectedType[];
|
|
39
|
+
export declare const isProjected: (type: unknown) => type is ProjectedType;
|
|
40
|
+
/**
|
|
41
|
+
* Single roles a record may also name in `fields.<role>`. Pooled across the projected types on purpose: an invoice
|
|
42
|
+
* whose `fields.payer` is not text is refused, as before the registry.
|
|
43
|
+
*/
|
|
44
|
+
export declare const FIELD_REFERENCE_ROLES: string[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { RECORD_ENTITY_TYPES, roleNames } from "../../domain/record-types.js";
|
|
3
|
+
export const IMPORTER = { bot: "import" };
|
|
4
|
+
/** `at` names the record's member at fault; a refused entity is the fields' fault. */
|
|
5
|
+
export const refusal = (message, code, at) => {
|
|
6
|
+
throw new CliError("validation_error", message, { reason: code, ...(at === undefined ? {} : { at }) });
|
|
7
|
+
};
|
|
8
|
+
export const stringField = (fields, name) => {
|
|
9
|
+
const value = fields[name];
|
|
10
|
+
if (value === undefined || value === null)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (typeof value !== "string")
|
|
13
|
+
return refusal(`fields.${name} is text`, "record.invalid");
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
const written = (source) => ({
|
|
17
|
+
by: IMPORTER,
|
|
18
|
+
source: `import:${source.provider}`,
|
|
19
|
+
reason: `imported from ${source.provider} account ${source.account}, batch ${source.batch_id}`,
|
|
20
|
+
});
|
|
21
|
+
const invoiceOf = (fields, related, source) => {
|
|
22
|
+
if (!Array.isArray(fields.lines) || fields.lines.some((line) => typeof line !== "object" || line === null))
|
|
23
|
+
return refusal("an invoice's fields.lines is a list of lines", "record.invalid");
|
|
24
|
+
const one = (role) => related.get(role)?.[0] ?? stringField(fields, role);
|
|
25
|
+
return {
|
|
26
|
+
...written(source),
|
|
27
|
+
direction: fields.direction,
|
|
28
|
+
number: stringField(fields, "number"),
|
|
29
|
+
issuer: one("issuer"),
|
|
30
|
+
counterparty: one("counterparty"),
|
|
31
|
+
project: one("project"),
|
|
32
|
+
currency: fields.currency,
|
|
33
|
+
issueDate: stringField(fields, "issue_date"),
|
|
34
|
+
dueDate: stringField(fields, "due_date"),
|
|
35
|
+
total: fields.total,
|
|
36
|
+
lines: fields.lines.map((line) => ({
|
|
37
|
+
description: line.description,
|
|
38
|
+
quantity: line.quantity,
|
|
39
|
+
unitPrice: line.unit_price,
|
|
40
|
+
amount: line.amount,
|
|
41
|
+
})),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const paymentOf = (fields, related, source) => {
|
|
45
|
+
const one = (role) => related.get(role)?.[0] ?? stringField(fields, role);
|
|
46
|
+
return {
|
|
47
|
+
...written(source),
|
|
48
|
+
direction: fields.direction,
|
|
49
|
+
payer: one("payer"),
|
|
50
|
+
payee: one("payee"),
|
|
51
|
+
project: one("project"),
|
|
52
|
+
amount: fields.amount,
|
|
53
|
+
currency: fields.currency,
|
|
54
|
+
date: fields.date,
|
|
55
|
+
reference: stringField(fields, "reference"),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** `null` for a type whose entity only the owner's `map` gives. */
|
|
59
|
+
export const RECORD_PROJECTORS = {
|
|
60
|
+
invoice: (fields, { related }, source, { finance }) => finance.addInvoice(invoiceOf(fields, related, source)).ref,
|
|
61
|
+
payment: (fields, { related, shares }, source, { finance, settle }) => settle(finance.addPayment(paymentOf(fields, related, source)), shares),
|
|
62
|
+
person: null,
|
|
63
|
+
organization: null,
|
|
64
|
+
project: null,
|
|
65
|
+
};
|
|
66
|
+
export const PROJECTED_TYPES = RECORD_ENTITY_TYPES.filter((type) => RECORD_PROJECTORS[type] !== null);
|
|
67
|
+
export const isProjected = (type) => PROJECTED_TYPES.includes(type);
|
|
68
|
+
/**
|
|
69
|
+
* Single roles a record may also name in `fields.<role>`. Pooled across the projected types on purpose: an invoice
|
|
70
|
+
* whose `fields.payer` is not text is refused, as before the registry.
|
|
71
|
+
*/
|
|
72
|
+
export const FIELD_REFERENCE_ROLES = roleNames(PROJECTED_TYPES, (role) => !role.many);
|
|
@@ -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": {
|
package/parity.json
CHANGED
|
@@ -4369,7 +4369,9 @@
|
|
|
4369
4369
|
"options": {
|
|
4370
4370
|
"--answer-stdin": {
|
|
4371
4371
|
"in": ["tg", "max"],
|
|
4372
|
-
"
|
|
4372
|
+
"planned": {
|
|
4373
|
+
"wa": "whatsapp-cli's next cli-core bump: its phone link asks the number through signInSteps"
|
|
4374
|
+
}
|
|
4373
4375
|
},
|
|
4374
4376
|
"--app": {
|
|
4375
4377
|
"in": ["tg"],
|