@wirecat/cli-core 0.617.0 → 0.619.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 +406 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/backfill.d.ts +21 -0
- package/dist/background/backfill.js +41 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/messages.js +4 -1
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- package/dist/background/report/view.js +4 -1
- package/dist/background/schedules.d.ts +17 -0
- package/dist/background/schedules.js +21 -8
- package/dist/calendar/cli/answers.d.ts +1 -0
- package/dist/calendar/cli/answers.js +6 -1
- package/dist/calendar/cli/index.js +2 -14
- package/dist/calendar/cli/people-link.d.ts +3 -0
- package/dist/calendar/cli/people-link.js +23 -0
- package/dist/calendar/index.d.ts +1 -1
- package/dist/calendar/index.js +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -0
- package/dist/cli/command-runs.d.ts +2 -0
- package/dist/cli/command-runs.js +3 -1
- package/dist/cli/config-command.js +6 -1
- package/dist/cli/context.d.ts +5 -0
- package/dist/cli/context.js +1 -0
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +10 -4
- package/dist/cli/index.js +10 -4
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +10 -3
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/serve-command.js +24 -2
- package/dist/cli/messenger/serve-outdated.d.ts +22 -0
- package/dist/cli/messenger/serve-outdated.js +100 -0
- package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
- package/dist/cli/messenger/store-maintenance-command.js +9 -4
- package/dist/cli/model-catalogue-command.d.ts +3 -1
- package/dist/cli/model-catalogue-command.js +46 -2
- package/dist/cli/profile.d.ts +8 -0
- package/dist/cli/profile.js +25 -0
- package/dist/cli/program.js +2 -0
- package/dist/cli/runs/search-query.d.ts +1 -1
- package/dist/cli/runs/search-query.js +4 -0
- package/dist/cli/runs/search.d.ts +5 -1
- package/dist/cli/runs/search.js +12 -1
- package/dist/cli/schedule/schedule-command.d.ts +9 -0
- package/dist/cli/schedule/schedule-command.js +13 -0
- package/dist/cli/search-left-out.d.ts +6 -1
- package/dist/cli/search-left-out.js +9 -4
- package/dist/cli/settings.d.ts +8 -0
- package/dist/cli/settings.js +17 -2
- package/dist/cli/shared-command-exports.d.ts +16 -0
- package/dist/cli/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +47 -23
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/crm/cli/index.js +38 -6
- package/dist/crm/group-hint.d.ts +8 -0
- package/dist/crm/group-hint.js +13 -0
- package/dist/crm/source.d.ts +12 -1
- package/dist/crm/sync.d.ts +31 -2
- package/dist/crm/sync.js +110 -3
- package/dist/crm/testing/acme-scenario.d.ts +40 -0
- package/dist/crm/testing/acme-scenario.js +170 -0
- package/dist/crm/testing/index.d.ts +1 -0
- package/dist/crm/testing/index.js +1 -0
- package/dist/crm/testing/source-contract.js +12 -0
- package/dist/crm/testing/source.d.ts +2 -0
- package/dist/crm/testing/source.js +1 -0
- package/dist/domain/addresses.d.ts +13 -0
- package/dist/domain/addresses.js +63 -0
- package/dist/domain/categories.d.ts +3 -0
- package/dist/domain/categories.js +2 -0
- package/dist/domain/crm.d.ts +23 -0
- package/dist/domain/crm.js +7 -0
- package/dist/domain/links.d.ts +17 -4
- package/dist/domain/links.js +20 -7
- package/dist/domain/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- package/dist/domain/orders.d.ts +8 -1
- package/dist/domain/orders.js +6 -2
- package/dist/domain/phones.d.ts +26 -0
- package/dist/domain/phones.js +48 -0
- package/dist/domain/record-types.d.ts +1 -1
- package/dist/domain/record-types.js +1 -0
- package/dist/domain/records.d.ts +5 -0
- package/dist/domain/ref-summaries.d.ts +39 -0
- package/dist/domain/ref-summaries.js +51 -3
- package/dist/domain/relations.d.ts +27 -2
- package/dist/domain/relations.js +37 -3
- package/dist/domain/vcard.d.ts +2 -2
- package/dist/domain/vcard.js +4 -10
- package/dist/embeddings/models.d.ts +11 -3
- package/dist/embeddings/models.js +28 -4
- package/dist/entities/business-context.d.ts +149 -0
- package/dist/entities/business-context.js +192 -0
- package/dist/entities/contact-activity.d.ts +2 -0
- package/dist/entities/contact-activity.js +7 -1
- package/dist/entities/contact-context.d.ts +157 -0
- package/dist/entities/contact-context.js +2 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/index.d.ts +23 -3
- package/dist/entities/index.js +5 -0
- package/dist/entities/mention.d.ts +4 -3
- package/dist/entities/mention.js +2 -1
- package/dist/entities/message-transfer.d.ts +4 -2
- package/dist/entities/message-transfer.js +2 -2
- package/dist/entities/model.d.ts +2 -2
- package/dist/entities/model.js +1 -1
- package/dist/entities/relationship.d.ts +4 -0
- package/dist/entities/relationship.js +4 -0
- package/dist/errors/reasons.js +44 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/meetings/context-store.d.ts +2 -0
- package/dist/meetings/findings-rules.d.ts +4 -0
- package/dist/meetings/findings-rules.js +5 -2
- package/dist/meetings/mcp/index.d.ts +15 -15
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -0
- package/dist/models/index.js +22 -5
- package/dist/runs/events.d.ts +11 -1
- package/dist/runs/events.js +3 -1
- package/dist/runs/log-attributes.js +4 -0
- package/dist/runs/log-events.js +2 -0
- package/dist/runs/recording.d.ts +3 -0
- package/dist/runs/recording.js +36 -12
- package/dist/runs/run.d.ts +12 -3
- package/dist/runs/run.js +32 -13
- package/dist/search/account-list.d.ts +6 -0
- package/dist/search/account-list.js +9 -3
- package/dist/search/groups.d.ts +68 -0
- package/dist/search/groups.js +172 -0
- package/dist/services/account-groups.d.ts +28 -0
- package/dist/services/account-groups.js +89 -0
- package/dist/services/archive.d.ts +7 -1
- package/dist/services/archive.js +19 -3
- package/dist/services/business-context.d.ts +113 -0
- package/dist/services/business-context.js +156 -0
- package/dist/services/communications.d.ts +50 -0
- package/dist/services/communications.js +140 -0
- package/dist/services/deal-context.d.ts +90 -0
- package/dist/services/deal-context.js +131 -0
- package/dist/services/deps.d.ts +2 -0
- package/dist/services/enrichment.d.ts +7 -2
- package/dist/services/enrichment.js +46 -5
- package/dist/services/events-search.js +2 -1
- package/dist/services/group-guard.d.ts +28 -0
- package/dist/services/group-guard.js +131 -0
- package/dist/services/index.d.ts +15 -2
- package/dist/services/index.js +9 -2
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -0
- package/dist/services/link-scorers/chat-senders.js +39 -29
- package/dist/services/link-scorers/member-chats.js +4 -3
- package/dist/services/link-scorers/member-dialog.js +5 -7
- package/dist/services/link-scorers/scan.js +17 -8
- package/dist/services/link-scorers/shared.d.ts +6 -2
- package/dist/services/link-scorers/shared.js +6 -2
- package/dist/services/link-scorers/types.d.ts +2 -0
- package/dist/services/meaning-model.d.ts +7 -0
- package/dist/services/meaning-model.js +20 -0
- package/dist/services/meeting-context.d.ts +6 -1
- package/dist/services/meeting-context.js +12 -5
- package/dist/services/message-resolution.js +2 -3
- package/dist/services/messages-meaning.js +4 -5
- package/dist/services/messages-search.d.ts +5 -1
- package/dist/services/messages-search.js +1 -1
- package/dist/services/model-downloads.d.ts +1 -1
- package/dist/services/model-downloads.js +6 -4
- package/dist/services/money-context.d.ts +73 -0
- package/dist/services/money-context.js +77 -0
- package/dist/services/notes-search.d.ts +4 -1
- package/dist/services/notes-search.js +19 -7
- package/dist/services/organization-context.d.ts +80 -0
- package/dist/services/organization-context.js +154 -0
- package/dist/services/people.d.ts +22 -6
- package/dist/services/people.js +38 -8
- package/dist/services/person-business.d.ts +43 -0
- package/dist/services/person-business.js +67 -0
- package/dist/services/person-context.d.ts +9 -1
- package/dist/services/person-context.js +5 -3
- package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
- package/dist/services/proposals/kinds/identity-link.js +48 -0
- package/dist/services/proposals/kinds/orders.js +1 -1
- package/dist/services/proposals/registry.js +2 -0
- package/dist/services/search-kind.d.ts +12 -5
- package/dist/services/search-kind.js +49 -10
- package/dist/services/text-model-choice.js +1 -1
- package/dist/store/accounts.d.ts +18 -0
- package/dist/store/accounts.js +18 -3
- package/dist/store/damage.d.ts +43 -10
- package/dist/store/damage.js +119 -36
- package/dist/store/decisions.js +3 -1
- package/dist/store/evidence-findings.d.ts +14 -0
- package/dist/store/evidence-findings.js +2 -0
- package/dist/store/exports.d.ts +2 -1
- package/dist/store/identity-links.d.ts +92 -0
- package/dist/store/identity-links.js +6 -0
- package/dist/store/index.d.ts +11 -5
- package/dist/store/index.js +5 -2
- package/dist/store/journal.d.ts +10 -0
- package/dist/store/journal.js +33 -0
- package/dist/store/marks.d.ts +19 -1
- package/dist/store/marks.js +23 -1
- package/dist/store/migrations.d.ts +1 -1
- package/dist/store/migrations.js +35 -12
- package/dist/store/people.d.ts +22 -0
- package/dist/store/people.js +15 -1
- package/dist/store/recover.d.ts +34 -0
- package/dist/store/recover.js +188 -6
- package/dist/store/refill.d.ts +28 -0
- package/dist/store/refill.js +120 -0
- package/dist/store/sqlite/accounts.js +2 -0
- package/dist/store/sqlite/base-stages.d.ts +122 -0
- package/dist/store/sqlite/base-stages.js +443 -0
- package/dist/store/sqlite/base-suggest.d.ts +13 -0
- package/dist/store/sqlite/base-suggest.js +62 -0
- package/dist/store/sqlite/business-contracts.js +10 -1
- package/dist/store/sqlite/clients.d.ts +7 -3
- package/dist/store/sqlite/clients.js +11 -7
- package/dist/store/sqlite/crm-settings.d.ts +12 -2
- package/dist/store/sqlite/crm-settings.js +56 -13
- package/dist/store/sqlite/crm.d.ts +13 -1
- package/dist/store/sqlite/crm.js +20 -4
- package/dist/store/sqlite/deals.d.ts +33 -0
- package/dist/store/sqlite/deals.js +42 -4
- package/dist/store/sqlite/decision-requests.js +7 -3
- package/dist/store/sqlite/email-index.js +17 -15
- package/dist/store/sqlite/evidence-findings.js +29 -5
- package/dist/store/sqlite/findings-core.js +4 -2
- package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
- package/dist/store/sqlite/identity-link-rows.js +146 -0
- package/dist/store/sqlite/identity-proposals.d.ts +53 -0
- package/dist/store/sqlite/identity-proposals.js +234 -0
- package/dist/store/sqlite/knowledge.js +5 -3
- package/dist/store/sqlite/link-kinds.d.ts +1 -0
- package/dist/store/sqlite/link-kinds.js +1 -0
- package/dist/store/sqlite/link-row.d.ts +6 -0
- package/dist/store/sqlite/link-row.js +35 -0
- package/dist/store/sqlite/link-source.d.ts +10 -0
- package/dist/store/sqlite/link-source.js +16 -0
- package/dist/store/sqlite/manifest.js +1 -0
- package/dist/store/sqlite/meeting-context-links.js +9 -6
- package/dist/store/sqlite/memories.js +9 -7
- package/dist/store/sqlite/mentions.d.ts +4 -1
- package/dist/store/sqlite/mentions.js +14 -9
- package/dist/store/sqlite/migrations.generated.js +8 -0
- package/dist/store/sqlite/note-index.js +9 -7
- package/dist/store/sqlite/notes.d.ts +2 -3
- package/dist/store/sqlite/notes.js +6 -29
- package/dist/store/sqlite/open-store.js +3 -0
- package/dist/store/sqlite/open.js +11 -2
- package/dist/store/sqlite/orders.d.ts +2 -0
- package/dist/store/sqlite/orders.js +9 -3
- package/dist/store/sqlite/organization-categories.d.ts +18 -0
- package/dist/store/sqlite/organization-categories.js +34 -0
- package/dist/store/sqlite/organizations.d.ts +5 -1
- package/dist/store/sqlite/organizations.js +14 -4
- package/dist/store/sqlite/person-links.d.ts +19 -4
- package/dist/store/sqlite/person-links.js +49 -11
- package/dist/store/sqlite/person-match-scan.d.ts +13 -0
- package/dist/store/sqlite/person-match-scan.js +251 -0
- package/dist/store/sqlite/person-matcher.d.ts +16 -0
- package/dist/store/sqlite/person-matcher.js +32 -0
- package/dist/store/sqlite/proposed-actions.js +6 -0
- package/dist/store/sqlite/record-matching.d.ts +2 -8
- package/dist/store/sqlite/record-matching.js +8 -40
- package/dist/store/sqlite/record-projectors-phone.js +2 -1
- package/dist/store/sqlite/record-relationships.d.ts +52 -0
- package/dist/store/sqlite/record-relationships.js +243 -0
- package/dist/store/sqlite/record-run.d.ts +5 -0
- package/dist/store/sqlite/record-run.js +62 -2
- package/dist/store/sqlite/record-writes.d.ts +1 -1
- package/dist/store/sqlite/record-writes.js +4 -2
- package/dist/store/sqlite/records.js +27 -2
- package/dist/store/sqlite/ref-accounts.d.ts +13 -0
- package/dist/store/sqlite/ref-accounts.js +117 -0
- package/dist/store/sqlite/ref-summaries.js +5 -1
- package/dist/store/sqlite/relation-list.d.ts +4 -1
- package/dist/store/sqlite/relation-list.js +20 -2
- package/dist/store/sqlite/relation-proposals.d.ts +18 -1
- package/dist/store/sqlite/relation-proposals.js +53 -11
- package/dist/store/sqlite/relation-roles.js +12 -6
- package/dist/store/sqlite/reported-relations.d.ts +11 -1
- package/dist/store/sqlite/reported-relations.js +59 -18
- package/dist/store/sqlite/schema.d.ts +32 -0
- package/dist/store/sqlite/schema.js +3 -0
- package/dist/store/sqlite/transaction.d.ts +13 -0
- package/dist/store/sqlite/transaction.js +33 -15
- package/dist/store/store.d.ts +3 -0
- package/dist/store/store.js +5 -0
- package/dist/store/write-events.d.ts +18 -0
- package/dist/store/write-events.js +88 -0
- package/package.json +3 -3
- package/parity.json +11 -4
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { IDENTITY_LINK_KIND } from "../identity-links.js";
|
|
2
|
+
import { currentPerson } from "./merged-persons.js";
|
|
3
|
+
export const pairOf = (a, b) => [Number(a), Number(b)].sort((x, y) => x - y).join("-");
|
|
4
|
+
const pkOf = (database, ref) => {
|
|
5
|
+
const at = ref.indexOf(":");
|
|
6
|
+
const row = database
|
|
7
|
+
.prepare("SELECT id FROM identities WHERE provider = ? AND external_id = ?")
|
|
8
|
+
.get(ref.slice(0, at), ref.slice(at + 1));
|
|
9
|
+
return row ? Number(row.id) : undefined;
|
|
10
|
+
};
|
|
11
|
+
const personOf = (database, identity) => {
|
|
12
|
+
if (identity === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const row = database.prepare("SELECT person_id FROM identity_links WHERE identity_id = ?").get(identity);
|
|
15
|
+
return row ? Number(row.person_id) : undefined;
|
|
16
|
+
};
|
|
17
|
+
/** The people a proposal's identities are on now; merges since may have moved either side. */
|
|
18
|
+
export const currentPairOf = (database, payload) => {
|
|
19
|
+
const from = personOf(database, pkOf(database, payload.identity));
|
|
20
|
+
const to = payload.with === null
|
|
21
|
+
? currentPerson(database, Number(payload.persons[1]))
|
|
22
|
+
: personOf(database, pkOf(database, payload.with));
|
|
23
|
+
return from === undefined || to === undefined ? undefined : [from, to];
|
|
24
|
+
};
|
|
25
|
+
/** The proposals that still say something: waiting for the owner, or turned down by them. */
|
|
26
|
+
export const linkRows = (database, statuses = ["proposed", "rejected"]) => database
|
|
27
|
+
.prepare("SELECT id, status, payload FROM proposed_actions WHERE kind = ? " +
|
|
28
|
+
"AND status IN (SELECT value FROM json_each(?)) ORDER BY id")
|
|
29
|
+
.all(IDENTITY_LINK_KIND, JSON.stringify(statuses))
|
|
30
|
+
.map((row) => {
|
|
31
|
+
const payload = JSON.parse(String(row.payload));
|
|
32
|
+
return { id: String(row.id), status: String(row.status), payload, pair: currentPairOf(database, payload) };
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* What a proposal rests on: its signal and the two identities it names, in either order. The owner's rejection
|
|
36
|
+
* holds against this evidence only (NEED-2444); a new signal or identity for the same people proposes them again.
|
|
37
|
+
*/
|
|
38
|
+
export const evidenceKeyOf = ({ signal, identity, with: other, }) => `${signal} ${[identity, other ?? ""].sort().join(" ")}`;
|
|
39
|
+
/** The evidence the owner rejected for linking these two people, by any proposal that names them now. */
|
|
40
|
+
export const rejectedEvidence = (database, a, b) => {
|
|
41
|
+
if (database
|
|
42
|
+
.prepare("SELECT 1 FROM proposed_actions WHERE kind = ? AND status = 'rejected' LIMIT 1")
|
|
43
|
+
.get(IDENTITY_LINK_KIND) === undefined)
|
|
44
|
+
return undefined;
|
|
45
|
+
const keys = linkRows(database, ["rejected"])
|
|
46
|
+
.filter(({ pair }) => pair !== undefined && pairOf(...pair) === pairOf(a, b))
|
|
47
|
+
.map(({ payload }) => evidenceKeyOf(payload));
|
|
48
|
+
return keys.length === 0 ? undefined : new Set(keys);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Every proposal by the pair of people it names now, read once, for a pass that checks many pairs: the open one, and
|
|
52
|
+
* the evidence the owner rejected. `merged` keeps it true after the pass joins two people itself.
|
|
53
|
+
*/
|
|
54
|
+
export const linkIndex = (database) => {
|
|
55
|
+
const rows = linkRows(database);
|
|
56
|
+
const open = new Map();
|
|
57
|
+
const rejected = new Map();
|
|
58
|
+
const byIdentity = new Set();
|
|
59
|
+
const place = (row) => {
|
|
60
|
+
if (row.pair === undefined)
|
|
61
|
+
return;
|
|
62
|
+
const key = pairOf(...row.pair);
|
|
63
|
+
if (row.status !== "rejected") {
|
|
64
|
+
if (!open.has(key))
|
|
65
|
+
open.set(key, row);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const keys = rejected.get(key) ?? new Set();
|
|
69
|
+
keys.add(evidenceKeyOf(row.payload));
|
|
70
|
+
rejected.set(key, keys);
|
|
71
|
+
};
|
|
72
|
+
for (const row of rows) {
|
|
73
|
+
place(row);
|
|
74
|
+
byIdentity.add(row.payload.identity);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
/** Whether any proposal, open or rejected, names this pair. */
|
|
78
|
+
known: (a, b) => open.has(pairOf(a, b)) || rejected.has(pairOf(a, b)),
|
|
79
|
+
open: (a, b) => open.get(pairOf(a, b)),
|
|
80
|
+
rejected: (a, b) => rejected.get(pairOf(a, b)),
|
|
81
|
+
/** Whether any proposal moves this identity, such as a meeting guest's that is already waiting. */
|
|
82
|
+
names: (identity) => byIdentity.has(identity),
|
|
83
|
+
add: (row) => {
|
|
84
|
+
rows.push(row);
|
|
85
|
+
place(row);
|
|
86
|
+
byIdentity.add(row.payload.identity);
|
|
87
|
+
},
|
|
88
|
+
merged: (from, into) => {
|
|
89
|
+
open.clear();
|
|
90
|
+
rejected.clear();
|
|
91
|
+
for (const row of rows) {
|
|
92
|
+
if (row.pair !== undefined)
|
|
93
|
+
row.pair = row.pair.map((person) => (person === from ? into : person));
|
|
94
|
+
place(row);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
const sideOf = (database, person) => database
|
|
100
|
+
.prepare("SELECT identity_id FROM identity_links WHERE person_id = ? ORDER BY identity_id")
|
|
101
|
+
.all(person)
|
|
102
|
+
.map(({ identity_id }) => String(identity_id))
|
|
103
|
+
.join(",");
|
|
104
|
+
export const sidesOf = (database, a, b) => [
|
|
105
|
+
sideOf(database, a),
|
|
106
|
+
sideOf(database, b),
|
|
107
|
+
];
|
|
108
|
+
/**
|
|
109
|
+
* Brings open proposals up to date with the people they name: one whose two people are one now, whose identity a
|
|
110
|
+
* purge erased, or that a merge made a second one for the same pair, is deleted, since nothing is left to decide; a
|
|
111
|
+
* live one takes the people and sides it names now, so the owner decides what is there. With `persons`, only the
|
|
112
|
+
* proposals that name one of them.
|
|
113
|
+
*/
|
|
114
|
+
export const settleLinks = (database, persons) => {
|
|
115
|
+
const named = persons === undefined ? undefined : new Set(persons.map(String));
|
|
116
|
+
const rows = linkRows(database, ["proposed"]);
|
|
117
|
+
const first = new Map();
|
|
118
|
+
for (const row of rows)
|
|
119
|
+
if (row.pair !== undefined && !first.has(pairOf(...row.pair)))
|
|
120
|
+
first.set(pairOf(...row.pair), row.id);
|
|
121
|
+
for (const row of rows) {
|
|
122
|
+
if (named !== undefined && !row.payload.persons.some((person) => named.has(person)))
|
|
123
|
+
continue;
|
|
124
|
+
if (row.pair === undefined || row.pair[0] === row.pair[1] || first.get(pairOf(...row.pair)) !== row.id) {
|
|
125
|
+
database.prepare("DELETE FROM proposed_actions WHERE id = ?").run(Number(row.id));
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const persons = [String(row.pair[0]), String(row.pair[1])];
|
|
129
|
+
const sides = sidesOf(database, ...row.pair);
|
|
130
|
+
if (persons.join() === row.payload.persons.join() && sides.join("|") === row.payload.sides.join("|"))
|
|
131
|
+
continue;
|
|
132
|
+
database
|
|
133
|
+
.prepare("UPDATE proposed_actions SET payload = ? WHERE id = ?")
|
|
134
|
+
.run(JSON.stringify({ ...row.payload, persons, sides }), Number(row.id));
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Deletes every `identity_link` that names an identity no longer in the store, whatever its status: its payload
|
|
139
|
+
* names the identity's phone or address, which must go with it.
|
|
140
|
+
*/
|
|
141
|
+
export const forgetErasedLinks = (database) => {
|
|
142
|
+
for (const row of linkRows(database, ["proposed", "approved", "rejected", "executed", "failed"]))
|
|
143
|
+
if (pkOf(database, row.payload.identity) === undefined ||
|
|
144
|
+
(row.payload.with !== null && pkOf(database, row.payload.with) === undefined))
|
|
145
|
+
database.prepare("DELETE FROM proposed_actions WHERE id = ?").run(Number(row.id));
|
|
146
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type IdentityLinkDecision, type IdentityLinkFilter, type IdentityLinkInput, type IdentityLinkPayload, type IdentityLinkRow, type IdentityLinkSignal } from "../identity-links.js";
|
|
2
|
+
import type { IdentityRef, LinkOptions } from "../types.js";
|
|
3
|
+
import { type LinkIndex } from "./identity-link-rows.js";
|
|
4
|
+
import type { Author } from "./memories.js";
|
|
5
|
+
import type { StoreContext } from "./open.js";
|
|
6
|
+
import { type ProposedAction } from "./proposed-actions.js";
|
|
7
|
+
export interface Opening {
|
|
8
|
+
identity: number;
|
|
9
|
+
/** An identity of the person it would join, when that person has one. */
|
|
10
|
+
with: number | null;
|
|
11
|
+
from: number;
|
|
12
|
+
to: number;
|
|
13
|
+
joins: IdentityLinkPayload["joins"];
|
|
14
|
+
signal: IdentityLinkSignal;
|
|
15
|
+
by: Author;
|
|
16
|
+
reason?: string | undefined;
|
|
17
|
+
score?: number | undefined;
|
|
18
|
+
evidence?: string[] | undefined;
|
|
19
|
+
candidates?: number[] | undefined;
|
|
20
|
+
}
|
|
21
|
+
export type Opened = {
|
|
22
|
+
outcome: "proposed" | "open";
|
|
23
|
+
action: ProposedAction;
|
|
24
|
+
} | {
|
|
25
|
+
outcome: "skipped";
|
|
26
|
+
reason: "identity.same" | "identity.separated" | "identity.rejected";
|
|
27
|
+
};
|
|
28
|
+
/** `provider:id`; the provider never holds a colon, an id may. */
|
|
29
|
+
export declare const parseIdentityRef: (ref: string) => IdentityRef;
|
|
30
|
+
/**
|
|
31
|
+
* Opens an `identity_link` proposal for two people, unless the owner separated them, one is already open for them, or
|
|
32
|
+
* the owner rejected a proposal on this same evidence: then it says which. At most one proposal is open per pair.
|
|
33
|
+
*/
|
|
34
|
+
export declare const openIdentityLink: (context: StoreContext, opening: Opening, index?: LinkIndex) => Opened;
|
|
35
|
+
/** Proposes that `with`'s person and `identity`'s person are one; the owner links directly instead. */
|
|
36
|
+
export declare const proposeIdentityLink: (context: StoreContext, input: IdentityLinkInput) => ProposedAction;
|
|
37
|
+
/**
|
|
38
|
+
* Moves the identity to the person when the owner says so; anyone else's move waits as an `identity_link` proposal,
|
|
39
|
+
* so an agent's mention or meeting association never replaces a link. Refused over the owner's separation, as
|
|
40
|
+
* `linkIdentityToPerson` refuses. Gives the proposal's id when it opened or met one.
|
|
41
|
+
*/
|
|
42
|
+
export declare const linkOrPropose: (context: StoreContext, identity: number, target: number, options: LinkOptions) => string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Decides an `identity_link`, the owner's alone. Confirm links the two people as the owner's link, refused with
|
|
45
|
+
* `identity.stale` when either person changed since it was proposed; reject keeps them apart, and only new evidence
|
|
46
|
+
* proposes the pair again.
|
|
47
|
+
*/
|
|
48
|
+
export declare const decideIdentityLink: (context: StoreContext, id: string, decision: IdentityLinkDecision) => ProposedAction;
|
|
49
|
+
/** The `identity_link` proposals, newest first, each naming its people as they are now; a page and whether more follow. */
|
|
50
|
+
export declare const identityLinkRows: (context: StoreContext, { person, status, limit, offset }: IdentityLinkFilter) => {
|
|
51
|
+
items: IdentityLinkRow[];
|
|
52
|
+
hasMore: boolean;
|
|
53
|
+
};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
3
|
+
import { IDENTITY_LINK_KIND, } from "../identity-links.js";
|
|
4
|
+
import { ensurePerson } from "./identities.js";
|
|
5
|
+
import { evidenceKeyOf, linkIndex, linkRows, settleLinks, sidesOf } from "./identity-link-rows.js";
|
|
6
|
+
import { currentPerson } from "./merged-persons.js";
|
|
7
|
+
import { linkIdentityToPerson, mergePerson, move, recordOf, required, survivorOf } from "./person-links.js";
|
|
8
|
+
import { resolvePersonLinks } from "./person-resolution.js";
|
|
9
|
+
import { identitySeparated, separated } from "./person-separation.js";
|
|
10
|
+
import { proposalOperationsOver, proposerOfAction } from "./proposed-actions.js";
|
|
11
|
+
import { write } from "./transaction.js";
|
|
12
|
+
/** The method an owner's confirmed proposal links by. */
|
|
13
|
+
const CONFIRMED = "proposal";
|
|
14
|
+
const refOf = ({ database }, pk) => {
|
|
15
|
+
const row = database.prepare("SELECT provider, external_id FROM identities WHERE id = ?").get(pk);
|
|
16
|
+
return `${String(row?.provider)}:${String(row?.external_id)}`;
|
|
17
|
+
};
|
|
18
|
+
/** `provider:id`; the provider never holds a colon, an id may. */
|
|
19
|
+
export const parseIdentityRef = (ref) => {
|
|
20
|
+
const at = ref.indexOf(":");
|
|
21
|
+
if (at < 1 || at === ref.length - 1)
|
|
22
|
+
throw new CliError("validation_error", `${ref} is not an identity; it looks like telegram:12 or email:a@b.example`);
|
|
23
|
+
return { provider: ref.slice(0, at), id: ref.slice(at + 1) };
|
|
24
|
+
};
|
|
25
|
+
const personOfIdentity = ({ database }, pk) => {
|
|
26
|
+
const row = database.prepare("SELECT person_id FROM identity_links WHERE identity_id = ?").get(pk);
|
|
27
|
+
return row ? Number(row.person_id) : undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Opens an `identity_link` proposal for two people, unless the owner separated them, one is already open for them, or
|
|
31
|
+
* the owner rejected a proposal on this same evidence: then it says which. At most one proposal is open per pair.
|
|
32
|
+
*/
|
|
33
|
+
export const openIdentityLink = (context, opening, index) => write(context.database, () => {
|
|
34
|
+
const { from, to } = opening;
|
|
35
|
+
if (from === to)
|
|
36
|
+
return { outcome: "skipped", reason: "identity.same" };
|
|
37
|
+
const apart = opening.joins === "identity" ? identitySeparated(context, opening.identity, to) : separated(context, from, to);
|
|
38
|
+
if (apart)
|
|
39
|
+
return { outcome: "skipped", reason: "identity.separated" };
|
|
40
|
+
const operations = proposalOperationsOver(context);
|
|
41
|
+
const links = index ?? linkIndex(context.database);
|
|
42
|
+
const open = links.open(from, to);
|
|
43
|
+
if (open)
|
|
44
|
+
return { outcome: "open", action: operations.get(open.id) };
|
|
45
|
+
const identity = refOf(context, opening.identity);
|
|
46
|
+
const other = opening.with === null ? null : refOf(context, opening.with);
|
|
47
|
+
if (links.rejected(from, to)?.has(evidenceKeyOf({ signal: opening.signal, identity, with: other })))
|
|
48
|
+
return { outcome: "skipped", reason: "identity.rejected" };
|
|
49
|
+
const payload = {
|
|
50
|
+
identity,
|
|
51
|
+
with: other,
|
|
52
|
+
persons: [String(from), String(to)],
|
|
53
|
+
sides: sidesOf(context.database, from, to),
|
|
54
|
+
joins: opening.joins,
|
|
55
|
+
signal: opening.signal,
|
|
56
|
+
score: opening.score ?? null,
|
|
57
|
+
...(opening.candidates && opening.candidates.length > 2 ? { candidates: opening.candidates.map(String) } : {}),
|
|
58
|
+
evidence: opening.evidence ?? [],
|
|
59
|
+
};
|
|
60
|
+
const action = operations.propose({
|
|
61
|
+
kind: IDENTITY_LINK_KIND,
|
|
62
|
+
by: opening.by,
|
|
63
|
+
payload,
|
|
64
|
+
...(opening.reason?.trim() ? { reason: opening.reason.trim() } : {}),
|
|
65
|
+
});
|
|
66
|
+
index?.add({ id: action.id, status: "proposed", payload, pair: [from, to] });
|
|
67
|
+
return { outcome: "proposed", action };
|
|
68
|
+
});
|
|
69
|
+
const refusalOf = (reason) => reason === "identity.same"
|
|
70
|
+
? new CliError("validation_error", "those identities are one person already", { reason })
|
|
71
|
+
: reason === "identity.separated"
|
|
72
|
+
? new CliError("conflict", "the owner separated these people; only the owner links them again", { reason })
|
|
73
|
+
: new CliError("conflict", "the owner rejected linking these people on this evidence; only new evidence proposes them again", { reason });
|
|
74
|
+
const firstIdentityOf = ({ database }, person) => {
|
|
75
|
+
const row = database
|
|
76
|
+
.prepare("SELECT identity_id FROM identity_links WHERE person_id = ? ORDER BY identity_id LIMIT 1")
|
|
77
|
+
.get(person);
|
|
78
|
+
return row ? Number(row.identity_id) : null;
|
|
79
|
+
};
|
|
80
|
+
/** Proposes that `with`'s person and `identity`'s person are one; the owner links directly instead. */
|
|
81
|
+
export const proposeIdentityLink = (context, input) => {
|
|
82
|
+
const identity = required(context, input.identity);
|
|
83
|
+
const other = required(context, input.with);
|
|
84
|
+
const opened = openIdentityLink(context, {
|
|
85
|
+
identity: identity.pk,
|
|
86
|
+
with: other.pk,
|
|
87
|
+
from: identity.personPk,
|
|
88
|
+
to: other.personPk,
|
|
89
|
+
joins: "person",
|
|
90
|
+
signal: input.signal,
|
|
91
|
+
by: input.by,
|
|
92
|
+
reason: input.reason,
|
|
93
|
+
score: input.score,
|
|
94
|
+
evidence: input.evidence,
|
|
95
|
+
});
|
|
96
|
+
if (opened.outcome === "skipped")
|
|
97
|
+
throw refusalOf(opened.reason);
|
|
98
|
+
return opened.action;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Moves the identity to the person when the owner says so; anyone else's move waits as an `identity_link` proposal,
|
|
102
|
+
* so an agent's mention or meeting association never replaces a link. Refused over the owner's separation, as
|
|
103
|
+
* `linkIdentityToPerson` refuses. Gives the proposal's id when it opened or met one.
|
|
104
|
+
*/
|
|
105
|
+
export const linkOrPropose = (context, identity, target, options) => {
|
|
106
|
+
if (options.by === "owner") {
|
|
107
|
+
linkIdentityToPerson(context, identity, target, options);
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const held = personOfIdentity(context, identity);
|
|
111
|
+
if (held === target)
|
|
112
|
+
return undefined;
|
|
113
|
+
if (identitySeparated(context, identity, target))
|
|
114
|
+
throw refusalOf("identity.separated");
|
|
115
|
+
return write(context.database, () => {
|
|
116
|
+
if (held === undefined) {
|
|
117
|
+
const named = context.database.prepare("SELECT name FROM identities WHERE id = ?").get(identity);
|
|
118
|
+
ensurePerson(context.orm, identity, named?.name == null ? null : String(named.name), context.now());
|
|
119
|
+
}
|
|
120
|
+
const opened = openIdentityLink(context, {
|
|
121
|
+
identity,
|
|
122
|
+
with: firstIdentityOf(context, target),
|
|
123
|
+
from: personOfIdentity(context, identity),
|
|
124
|
+
to: target,
|
|
125
|
+
joins: "identity",
|
|
126
|
+
signal: "agent",
|
|
127
|
+
by: { bot: options.by },
|
|
128
|
+
});
|
|
129
|
+
if (opened.outcome === "skipped")
|
|
130
|
+
throw refusalOf(opened.reason);
|
|
131
|
+
return opened.action.id;
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
/** The mentions an agent's proposal left `proposed` take the owner's answer: linked, or undecided again. */
|
|
135
|
+
const settleMentions = ({ database, now }, payload, to) => {
|
|
136
|
+
const { provider, id } = parseIdentityRef(payload.identity);
|
|
137
|
+
const identity = database
|
|
138
|
+
.prepare("SELECT id FROM identities WHERE provider = ? AND external_id = ?")
|
|
139
|
+
.get(provider, id);
|
|
140
|
+
if (!identity)
|
|
141
|
+
return;
|
|
142
|
+
const cleared = to === "found" ? ", target_type = NULL, target_id = NULL" : "";
|
|
143
|
+
database
|
|
144
|
+
.prepare(`UPDATE mentions SET status = ?${cleared}, updated_at = ? WHERE status = 'proposed' AND target_type = 'identity' ` +
|
|
145
|
+
"AND target_id = ?")
|
|
146
|
+
.run(to, now(), Number(identity.id));
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Decides an `identity_link`, the owner's alone. Confirm links the two people as the owner's link, refused with
|
|
150
|
+
* `identity.stale` when either person changed since it was proposed; reject keeps them apart, and only new evidence
|
|
151
|
+
* proposes the pair again.
|
|
152
|
+
*/
|
|
153
|
+
export const decideIdentityLink = (context, id, decision) => write(context.database, () => {
|
|
154
|
+
const operations = proposalOperationsOver(context);
|
|
155
|
+
const proposal = operations.get(id);
|
|
156
|
+
if (proposal.kind !== IDENTITY_LINK_KIND)
|
|
157
|
+
throw new CliError("validation_error", `${id} is a ${proposal.kind} proposal, not an identity link`);
|
|
158
|
+
assertMayDecide({ id, kind: IDENTITY_LINK_KIND, ownerOnly: true, proposedBy: proposerOfAction(proposal) }, decision.by, context.decisions);
|
|
159
|
+
const author = deciderAuthor(decision.by);
|
|
160
|
+
const reason = decision.reason?.trim() || undefined;
|
|
161
|
+
const noted = reason === undefined ? {} : { reason };
|
|
162
|
+
if (!decision.confirm) {
|
|
163
|
+
const rejected = operations.reject(id, author, { dismissed: true, ...noted });
|
|
164
|
+
settleMentions(context, proposal.payload, "found");
|
|
165
|
+
return rejected;
|
|
166
|
+
}
|
|
167
|
+
if (proposal.status !== "proposed")
|
|
168
|
+
throw new CliError("validation_error", `${id} is already ${proposal.status}`, { reason: "proposal.wrong-state" });
|
|
169
|
+
const payload = proposal.payload;
|
|
170
|
+
const identity = required(context, parseIdentityRef(payload.identity));
|
|
171
|
+
const from = identity.personPk;
|
|
172
|
+
const to = payload.with === null
|
|
173
|
+
? currentPerson(context.database, Number(payload.persons[1]))
|
|
174
|
+
: required(context, parseIdentityRef(payload.with)).personPk;
|
|
175
|
+
const [wasFrom, wasTo] = payload.persons;
|
|
176
|
+
if (String(from) !== wasFrom ||
|
|
177
|
+
String(to) !== wasTo ||
|
|
178
|
+
sidesOf(context.database, from, to).join("|") !== payload.sides.join("|"))
|
|
179
|
+
throw new CliError("conflict", `the people ${id} names changed since it was proposed; propose it again`, {
|
|
180
|
+
reason: "identity.stale",
|
|
181
|
+
});
|
|
182
|
+
const options = { method: CONFIRMED, by: "owner" };
|
|
183
|
+
const kept = payload.joins === "person" ? survivorOf(context, from, to) : to;
|
|
184
|
+
// Approved first: the merge settles the proposals naming these people, and this one is decided, not moot.
|
|
185
|
+
const approved = operations.approve(id, { person: String(kept), ...noted }, author);
|
|
186
|
+
if (payload.joins === "person")
|
|
187
|
+
mergePerson(context, from, to, options);
|
|
188
|
+
else {
|
|
189
|
+
move(context, identity.pk, from, to, options);
|
|
190
|
+
settleLinks(context.database, [from, to]);
|
|
191
|
+
}
|
|
192
|
+
resolvePersonLinks(context.database, recordOf(context, kept).identities.flatMap(({ name, username }) => [name, username]));
|
|
193
|
+
settleMentions(context, payload, "linked");
|
|
194
|
+
return approved;
|
|
195
|
+
});
|
|
196
|
+
const SHOWN = {
|
|
197
|
+
proposed: "proposed",
|
|
198
|
+
rejected: "rejected",
|
|
199
|
+
approved: "confirmed",
|
|
200
|
+
executed: "confirmed",
|
|
201
|
+
failed: "confirmed",
|
|
202
|
+
};
|
|
203
|
+
/** The `identity_link` proposals, newest first, each naming its people as they are now; a page and whether more follow. */
|
|
204
|
+
export const identityLinkRows = (context, { person, status = "proposed", limit, offset }) => {
|
|
205
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 500)
|
|
206
|
+
throw new CliError("validation_error", "a list of link proposals takes 1–500 items");
|
|
207
|
+
const asked = person === undefined ? undefined : currentPerson(context.database, Number(person));
|
|
208
|
+
const operations = proposalOperationsOver(context);
|
|
209
|
+
const rows = linkRows(context.database, ["proposed", "rejected", "approved", "executed", "failed"])
|
|
210
|
+
.filter((row) => status === "all" || SHOWN[row.status] === status)
|
|
211
|
+
.filter((row) => asked === undefined || (row.pair?.includes(asked) ?? false))
|
|
212
|
+
.reverse();
|
|
213
|
+
const items = rows.slice(offset, offset + limit).map((row) => {
|
|
214
|
+
const action = operations.get(row.id);
|
|
215
|
+
const payload = row.payload;
|
|
216
|
+
const [from, to] = row.pair ?? payload.persons.map(Number);
|
|
217
|
+
return {
|
|
218
|
+
id: row.id,
|
|
219
|
+
status: SHOWN[row.status] ?? "proposed",
|
|
220
|
+
persons: [`person:${from}`, `person:${to}`],
|
|
221
|
+
identity: payload.identity,
|
|
222
|
+
with: payload.with,
|
|
223
|
+
joins: payload.joins,
|
|
224
|
+
signal: payload.signal,
|
|
225
|
+
score: payload.score,
|
|
226
|
+
candidates: (payload.candidates ?? []).map((uid) => `person:${uid}`),
|
|
227
|
+
reason: action.reason,
|
|
228
|
+
evidence: payload.evidence,
|
|
229
|
+
proposedBy: proposerOfAction(action),
|
|
230
|
+
createdAt: action.createdAt,
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
return { items, hasMore: rows.length > offset + limit };
|
|
234
|
+
};
|
|
@@ -3,7 +3,7 @@ import { walkQuery } from "@wirecat/cli-base/query";
|
|
|
3
3
|
import { parseLocator } from "../../domain/locator.js";
|
|
4
4
|
import { isBusinessType } from "../../domain/record-types.js";
|
|
5
5
|
import { formatReference, parseReference } from "../../domain/references.js";
|
|
6
|
-
import { checkEnds, isSymmetric, notOverriddenSql } from "../../domain/relations.js";
|
|
6
|
+
import { checkEnds, checkEngagement, isSymmetric, notOverriddenSql } from "../../domain/relations.js";
|
|
7
7
|
import { normalizeTag } from "../../domain/tags.js";
|
|
8
8
|
import { normalizeVocabularyName } from "../../domain/vocabulary.js";
|
|
9
9
|
import { formatMeetingReference } from "../../meetings/index.js";
|
|
@@ -14,7 +14,7 @@ import { ulid } from "../ulid.js";
|
|
|
14
14
|
import { ownerPerson } from "./actors.js";
|
|
15
15
|
import { forgetEvidenceToRow } from "./evidence-signature.js";
|
|
16
16
|
import { LABELLED } from "./labelled.js";
|
|
17
|
-
import { linkKind, RELATION_KINDS } from "./link-kinds.js";
|
|
17
|
+
import { linkKind, RELATION_KINDS, REPORTER_SQL } from "./link-kinds.js";
|
|
18
18
|
import { meetingProjectsStoreOver } from "./meeting-projects.js";
|
|
19
19
|
import { idsOf } from "./merged-persons.js";
|
|
20
20
|
import { holdingText } from "./note-index.js";
|
|
@@ -215,7 +215,7 @@ export const knowledgeStoreOver = (context) => {
|
|
|
215
215
|
});
|
|
216
216
|
const proposals = relationProposalsOver(context);
|
|
217
217
|
const relationOf = (link) => relationAt(link, now());
|
|
218
|
-
const RELATIONS = `SELECT
|
|
218
|
+
const RELATIONS = `SELECT *, ${REPORTER_SQL} FROM links WHERE kind IN (${RELATION_KINDS.map(linkKind).join(",")}) AND to_id IS NOT NULL`;
|
|
219
219
|
const relation = (id) => {
|
|
220
220
|
const row = /^\d+$/.test(id) ? database.prepare(`${RELATIONS} AND id = ?`).get(Number(id)) : undefined;
|
|
221
221
|
if (!row)
|
|
@@ -231,6 +231,8 @@ export const knowledgeStoreOver = (context) => {
|
|
|
231
231
|
checkEnds(input.kind, from.type, to.type);
|
|
232
232
|
if (input.role !== undefined)
|
|
233
233
|
checkStoredRole(database, input.kind, from.type, to.type, input.role.trim());
|
|
234
|
+
if (input.engagement !== undefined)
|
|
235
|
+
checkEngagement(input.kind, from.type, to.type, input.engagement);
|
|
234
236
|
let ends = { from: input.from, to: input.to };
|
|
235
237
|
if (isSymmetric(input.kind) && to.id < from.id)
|
|
236
238
|
[from, to, ends] = [to, from, { from: input.to, to: input.from }];
|
|
@@ -13,3 +13,4 @@ export { RELATION_KINDS } from "../../domain/relations.js";
|
|
|
13
13
|
export declare const linkKind: (kind: LinkKind) => string;
|
|
14
14
|
/** The `role` of the `related-to` link from a phone call's event to the person on the other end. */
|
|
15
15
|
export declare const COUNTERPART_ROLE = "counterpart";
|
|
16
|
+
export { REPORTER_SQL } from "./link-source.js";
|
|
@@ -29,3 +29,4 @@ export { RELATION_KINDS } from "../../domain/relations.js";
|
|
|
29
29
|
export const linkKind = (kind) => `'${kind}'`;
|
|
30
30
|
/** The `role` of the `related-to` link from a phone call's event to the person on the other end. */
|
|
31
31
|
export const COUNTERPART_ROLE = "counterpart";
|
|
32
|
+
export { REPORTER_SQL } from "./link-source.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
2
|
+
import type { Link } from "./notes.js";
|
|
3
|
+
type Row = Record<string, unknown>;
|
|
4
|
+
export declare const thingRef: (database: CacheDatabase, type: unknown, id: unknown) => string | null;
|
|
5
|
+
export declare const linkOf: (database: CacheDatabase, row: Row) => Link;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isEngagement } from "../../domain/relations.js";
|
|
2
|
+
import { sourceOf } from "./link-source.js";
|
|
3
|
+
import { referenceOfThing, storedThing } from "./things.js";
|
|
4
|
+
const iso = (value) => new Date(Number(value)).toISOString();
|
|
5
|
+
export const thingRef = (database, type, id) => type == null || id == null ? null : (referenceOfThing(database, storedThing(type, id)) ?? null);
|
|
6
|
+
export const linkOf = (database, row) => {
|
|
7
|
+
const metadata = row.metadata == null ? {} : JSON.parse(String(row.metadata));
|
|
8
|
+
// An older row holds one word; every reader gets a list.
|
|
9
|
+
const sourceRoles = [metadata.source_role].flat().filter((word) => typeof word === "string");
|
|
10
|
+
return {
|
|
11
|
+
id: String(row.id),
|
|
12
|
+
from: thingRef(database, row.from_type, row.from_id),
|
|
13
|
+
to: thingRef(database, row.to_type, row.to_id),
|
|
14
|
+
kind: row.kind,
|
|
15
|
+
anchor: row.anchor == null ? null : String(row.anchor),
|
|
16
|
+
origin: row.source,
|
|
17
|
+
targetText: row.target_text == null ? null : String(row.target_text),
|
|
18
|
+
role: row.role == null ? null : String(row.role),
|
|
19
|
+
purpose: row.purpose == null ? null : String(row.purpose),
|
|
20
|
+
evidence: row.evidence == null ? null : String(row.evidence),
|
|
21
|
+
provenance: typeof metadata.provenance === "string" ? metadata.provenance : null,
|
|
22
|
+
confirmed: Number(row.confirmed) === 1,
|
|
23
|
+
createdAt: iso(row.created_at),
|
|
24
|
+
...(row.account_id == null ? {} : { accountId: Number(row.account_id) }),
|
|
25
|
+
...(row.starts_at == null ? {} : { startsAt: Number(row.starts_at) }),
|
|
26
|
+
...(row.ends_at == null ? {} : { endsAt: Number(row.ends_at) }),
|
|
27
|
+
...(row.deleted_at == null ? {} : { deletedAt: Number(row.deleted_at) }),
|
|
28
|
+
...(typeof metadata.position === "string" ? { position: metadata.position } : {}),
|
|
29
|
+
...(typeof metadata.roleNote === "string" ? { roleNote: metadata.roleNote } : {}),
|
|
30
|
+
...(isEngagement(metadata.engagement) ? { engagement: metadata.engagement } : {}),
|
|
31
|
+
...(typeof metadata.overrides === "number" ? { overrides: metadata.overrides } : {}),
|
|
32
|
+
...(sourceRoles.length ? { sourceRoles } : {}),
|
|
33
|
+
...sourceOf(database, row, metadata),
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
2
|
+
type Row = Record<string, unknown>;
|
|
3
|
+
/** A relationships read selects its reporter with this, so a page of links costs no lookup per row. */
|
|
4
|
+
export declare const REPORTER_SQL = "(SELECT provider || ':' || external_id FROM accounts r WHERE r.id = links.account_id) AS reporter";
|
|
5
|
+
/** What a source said about a relationship link: the account that reported it and how it was derived. */
|
|
6
|
+
export declare const sourceOf: (database: CacheDatabase, row: Row, metadata: Record<string, unknown>) => {
|
|
7
|
+
account?: string;
|
|
8
|
+
derived?: string;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A relationships read selects its reporter with this, so a page of links costs no lookup per row. */
|
|
2
|
+
export const REPORTER_SQL = "(SELECT provider || ':' || external_id FROM accounts r WHERE r.id = links.account_id) AS reporter";
|
|
3
|
+
/** What a source said about a relationship link: the account that reported it and how it was derived. */
|
|
4
|
+
export const sourceOf = (database, row, metadata) => {
|
|
5
|
+
const reporter = row.account_id == null
|
|
6
|
+
? undefined
|
|
7
|
+
: "reporter" in row
|
|
8
|
+
? row.reporter
|
|
9
|
+
: database
|
|
10
|
+
.prepare("SELECT provider || ':' || external_id AS key FROM accounts WHERE id = ?")
|
|
11
|
+
.get(Number(row.account_id))?.key;
|
|
12
|
+
return {
|
|
13
|
+
...(typeof reporter === "string" ? { account: reporter } : {}),
|
|
14
|
+
...(typeof metadata.derived === "string" ? { derived: metadata.derived } : {}),
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -123,6 +123,7 @@ export const MANIFEST = [
|
|
|
123
123
|
{ name: "20261108104659_version-58-enrichments", version: 58, minCompatible: 56 },
|
|
124
124
|
{ name: "20261108104701_version-58-enrichments-deletes", version: 58, minCompatible: 56 },
|
|
125
125
|
{ name: "20261108165309_version-59-templates", version: 59, minCompatible: 56 },
|
|
126
|
+
{ name: "20261108190101_version-60-base-stages", version: 60, minCompatible: 56 },
|
|
126
127
|
];
|
|
127
128
|
export const generatedMigrations = (manifest = MANIFEST, generated = GENERATED) => {
|
|
128
129
|
const migrations = [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { linkOrPropose } from "./identity-proposals.js";
|
|
2
3
|
import { checkMeetingCancelled } from "./meeting-operation.js";
|
|
3
4
|
import { positiveReadInteger } from "./meeting-reads.js";
|
|
4
|
-
import { linkIdentityToPerson } from "./person-links.js";
|
|
5
5
|
import { read, write } from "./transaction.js";
|
|
6
6
|
const missing = () => {
|
|
7
7
|
throw new CliError("not_found", "Meeting, participant or selected target not found");
|
|
@@ -114,12 +114,14 @@ export function meetingContextLinksOver(context) {
|
|
|
114
114
|
.prepare("SELECT 1 FROM identity_links l JOIN account_identities a ON a.identity_id=l.identity_id WHERE l.person_id=? AND a.account_id IN (SELECT value FROM json_each(?)) LIMIT 1")
|
|
115
115
|
.get(target, JSON.stringify(selected)))
|
|
116
116
|
return missing();
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
const moving = found.personUid !== input.personUid;
|
|
118
|
+
const proposal = moving
|
|
119
|
+
? linkOrPropose({ ...context, now: () => input.now }, found.identityId, target, {
|
|
120
120
|
method: "manual",
|
|
121
121
|
by: input.author,
|
|
122
|
-
})
|
|
122
|
+
})
|
|
123
|
+
: undefined;
|
|
124
|
+
const changed = moving && proposal === undefined;
|
|
123
125
|
database
|
|
124
126
|
.prepare("INSERT INTO account_identities(account_id,identity_id,created_at,updated_at) VALUES(?,?,?,?) ON CONFLICT(account_id,identity_id) DO NOTHING")
|
|
125
127
|
.run(input.accountId, found.identityId, input.now, input.now);
|
|
@@ -127,9 +129,10 @@ export function meetingContextLinksOver(context) {
|
|
|
127
129
|
return {
|
|
128
130
|
meetingId: input.meetingId,
|
|
129
131
|
participantId: input.participantId,
|
|
130
|
-
personUid: input.personUid,
|
|
132
|
+
personUid: proposal === undefined ? input.personUid : (found.personUid ?? input.personUid),
|
|
131
133
|
previousPersonUid: found.personUid,
|
|
132
134
|
changed,
|
|
135
|
+
...(proposal === undefined ? {} : { proposal }),
|
|
133
136
|
};
|
|
134
137
|
}, { beforeCommit: () => checkMeetingCancelled(input.signal) });
|
|
135
138
|
},
|
|
@@ -182,12 +182,14 @@ export const memoriesStoreOver = (context) => {
|
|
|
182
182
|
return row;
|
|
183
183
|
};
|
|
184
184
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
185
|
+
* Another correction retired the memory once it left `proposed`: confirmed, and maybe since then stale or corrected
|
|
186
|
+
* itself. Not `last_verified_at`: the owner's source review sets it on a proposal too. An older store let an agent's
|
|
187
|
+
* proposal retire the memory at once, so a memory superseded only by proposals is not refused.
|
|
187
188
|
*/
|
|
188
|
-
const retiredByAnother = (replaced, by) =>
|
|
189
|
-
|
|
190
|
-
|
|
189
|
+
const retiredByAnother = (replaced, by) => replaced.status === "superseded" &&
|
|
190
|
+
database
|
|
191
|
+
.prepare("SELECT 1 FROM memories WHERE supersedes_id = ? AND id <> ? AND status <> 'proposed' LIMIT 1")
|
|
192
|
+
.get(Number(replaced.id), by) !== undefined;
|
|
191
193
|
const setStatus = (reference, status, verified) => {
|
|
192
194
|
const row = rowOf(reference);
|
|
193
195
|
const at = now();
|
|
@@ -247,9 +249,9 @@ export const memoriesStoreOver = (context) => {
|
|
|
247
249
|
const confirmNow = (reference, by) => write(database, () => {
|
|
248
250
|
const row = rowOf(reference);
|
|
249
251
|
const replaced = row.status === "confirmed" || row.supersedes_id == null ? undefined : rowOf(`memory:${row.supersedes_id}`);
|
|
250
|
-
decided(reference, by, replaced?.status === "confirmed");
|
|
252
|
+
decided(reference, by, replaced?.status === "confirmed" || replaced?.status === "superseded");
|
|
251
253
|
if (replaced !== undefined) {
|
|
252
|
-
if (retiredByAnother(
|
|
254
|
+
if (retiredByAnother(replaced, Number(row.id)))
|
|
253
255
|
throw alreadySuperseded(replaced.id);
|
|
254
256
|
database
|
|
255
257
|
.prepare("UPDATE memories SET status = 'superseded', updated_at = ? WHERE id = ?")
|