@wirecat/cli-core 0.618.0 → 0.620.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 +453 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- 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/cli/search-all.d.ts +1 -1
- package/dist/calendar/cli/search-all.js +9 -10
- 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/calendar/prepare.js +1 -0
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -0
- package/dist/cli/bot/buttons.d.ts +3 -0
- package/dist/cli/bot/buttons.js +24 -0
- package/dist/cli/bot/messages.js +14 -1
- package/dist/cli/bot/port.d.ts +8 -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.js +1 -0
- package/dist/cli/contract-templates.js +31 -2
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +7 -2
- package/dist/cli/index.js +7 -2
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +12 -4
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/conversations-command.js +2 -1
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/message-items.js +1 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/search-answers.d.ts +5 -5
- package/dist/cli/messenger/search-answers.js +9 -20
- package/dist/cli/messenger/search-command.d.ts +2 -2
- package/dist/cli/messenger/search-command.js +8 -18
- 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 +48 -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/search-harder-consents-command.d.ts +18 -0
- package/dist/cli/search-harder-consents-command.js +83 -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/steps.js +0 -6
- package/dist/cli/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +26 -26
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/cli/templates-command.js +4 -4
- package/dist/crm/cli/index.js +40 -8
- package/dist/crm/cli/search-all.js +7 -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/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- package/dist/domain/models.d.ts +2 -0
- 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 +12 -2
- package/dist/domain/relations.js +14 -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 +160 -0
- package/dist/entities/contact-context.js +6 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/hit.d.ts +9 -2
- package/dist/entities/hit.js +6 -2
- package/dist/entities/index.d.ts +32 -4
- 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 +39 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- 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/hit-query.d.ts +1 -1
- package/dist/meetings/hit-query.js +2 -2
- package/dist/meetings/index.d.ts +1 -0
- package/dist/meetings/index.js +1 -0
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -0
- package/dist/meetings/store.d.ts +4 -0
- package/dist/meetings/store.js +2 -1
- package/dist/models/anthropic.js +5 -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/search/question-period.d.ts +10 -0
- package/dist/search/question-period.js +133 -0
- package/dist/sends/buttons.d.ts +2 -0
- package/dist/sends/buttons.js +6 -1
- 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 +12 -2
- package/dist/services/enrichment.js +47 -6
- 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 +19 -4
- package/dist/services/index.js +12 -3
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -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/messages.d.ts +1 -4
- package/dist/services/messages.js +0 -2
- package/dist/services/metadata.d.ts +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 +11 -1
- package/dist/services/person-context.js +25 -10
- package/dist/services/person-preparation.d.ts +5 -1
- package/dist/services/person-preparation.js +10 -3
- package/dist/services/person-timeline.d.ts +6 -1
- package/dist/services/person-timeline.js +10 -2
- 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-all-deprecated.d.ts +40 -0
- package/dist/services/search-all-deprecated.js +10 -0
- package/dist/services/search-all-meetings.d.ts +50 -52
- package/dist/services/search-all-meetings.js +265 -272
- package/dist/services/search-all.d.ts +34 -6
- package/dist/services/search-all.js +118 -24
- package/dist/services/search-harder.d.ts +64 -0
- package/dist/services/search-harder.js +194 -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/chats.d.ts +3 -1
- package/dist/store/chats.js +6 -0
- 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/people.d.ts +28 -2
- package/dist/store/people.js +15 -1
- package/dist/store/sqlite/accounts.js +2 -0
- package/dist/store/sqlite/added-sources.d.ts +15 -3
- package/dist/store/sqlite/added-sources.js +26 -5
- 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/chats.d.ts +2 -0
- package/dist/store/sqlite/chats.js +6 -0
- 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 +8 -4
- 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/involvements.d.ts +1 -1
- package/dist/store/sqlite/involvements.js +4 -2
- package/dist/store/sqlite/knowledge.js +2 -2
- 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/meeting-hit-query.d.ts +5 -0
- package/dist/store/sqlite/meeting-hit-query.js +33 -0
- package/dist/store/sqlite/meeting-hit-search.js +10 -8
- package/dist/store/sqlite/meetings.js +1 -1
- 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/notes.d.ts +8 -2
- package/dist/store/sqlite/notes.js +17 -34
- 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/person-preparation.d.ts +7 -3
- package/dist/store/sqlite/person-preparation.js +20 -6
- 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 +22 -4
- package/dist/store/sqlite/relation-proposals.d.ts +6 -0
- package/dist/store/sqlite/relation-proposals.js +3 -0
- 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.js +11 -1
- 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 +37 -16
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { isCrmUserId, normalCrmUserId } from "../../domain/crm.js";
|
|
2
3
|
/** `reported`: what a CRM account says is a relationship nobody confirmed; `review`: it waits as a proposal. */
|
|
3
4
|
export const CRM_LINK_MODES = ["reported", "review"];
|
|
4
5
|
/** What a CRM company type may mean: the company buys from the own company, sells to it, or partners with it. */
|
|
5
6
|
export const COMPANY_TYPE_MEANINGS = ["customer-of", "supplier", "partner-of"];
|
|
6
|
-
|
|
7
|
+
/** `mine`: only the deals and leads of the sign-in's own user and of the `unassigned` users; `all`: every one. */
|
|
8
|
+
export const CRM_SCOPES = ["mine", "all"];
|
|
9
|
+
const NAMES = ["links", "company-types", "scope", "unassigned", "self"];
|
|
10
|
+
const SETTING = new RegExp(`^crm\\.([a-z0-9-]+):(.+)\\.(${NAMES.join("|")})$`);
|
|
7
11
|
export const crmSettingKey = (key, name) => `crm.${key.provider}:${key.account}.${name}`;
|
|
8
12
|
/** The account and the setting a key names; a portal's account may hold dots, so it is read from both ends. */
|
|
9
13
|
export const parseCrmSetting = (setting) => {
|
|
10
|
-
const match =
|
|
14
|
+
const match = SETTING.exec(setting);
|
|
11
15
|
return match
|
|
12
16
|
? { key: { provider: match[1], account: match[2] }, name: match[3] }
|
|
13
17
|
: undefined;
|
|
@@ -35,13 +39,46 @@ export const crmCompanyTypes = (database, key) => {
|
|
|
35
39
|
return {};
|
|
36
40
|
return Object.fromEntries(Object.entries(value).filter((entry) => COMPANY_TYPE_MEANINGS.includes(entry[1])));
|
|
37
41
|
};
|
|
38
|
-
/**
|
|
42
|
+
/** Kept as stored, a malformed one too: the sync refuses it by name rather than copy fewer deals without a word. */
|
|
43
|
+
const userIds = (value) => Array.isArray(value) ? value.map(String) : typeof value === "string" ? [value] : [];
|
|
44
|
+
/** `mine` unless the owner chose `all`: a sync never copies other people's deals without being asked. */
|
|
45
|
+
export const crmScope = (database, key) => {
|
|
46
|
+
const scope = stored(database, crmSettingKey(key, "scope"));
|
|
47
|
+
if (scope === "all")
|
|
48
|
+
return { mode: "all" };
|
|
49
|
+
return { mode: "mine", also: userIds(stored(database, crmSettingKey(key, "unassigned"))), explicit: scope === "mine" };
|
|
50
|
+
};
|
|
51
|
+
const picked = (name, text, values) => {
|
|
52
|
+
if (!values.includes(text))
|
|
53
|
+
throw new CliError("validation_error", `a CRM account's ${name} setting is ${values.join(" or ")}`);
|
|
54
|
+
return text;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* `links`: reported or review; `scope`: mine or all; `unassigned`: the CRM's user ids, comma-separated;
|
|
58
|
+
* `company-types`: `TYPE=meaning` pairs, comma-separated.
|
|
59
|
+
*/
|
|
60
|
+
/** The CRM user whose deals the account's copy holds: the first sync's sign-in, until the owner unsets it. */
|
|
61
|
+
export const crmOwnUser = (database, key) => {
|
|
62
|
+
const value = stored(database, crmSettingKey(key, "self"));
|
|
63
|
+
return typeof value === "string" && value ? value : undefined;
|
|
64
|
+
};
|
|
39
65
|
export const parseCrmSettingValue = (name, value) => {
|
|
40
66
|
const text = value.trim();
|
|
41
|
-
if (name === "links")
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return text;
|
|
67
|
+
if (name === "links")
|
|
68
|
+
return picked(name, text, CRM_LINK_MODES);
|
|
69
|
+
if (name === "scope")
|
|
70
|
+
return picked(name, text, CRM_SCOPES);
|
|
71
|
+
if (name === "self") {
|
|
72
|
+
if (!isCrmUserId(text))
|
|
73
|
+
throw new CliError("validation_error", `self takes the CRM's numeric user id: not "${text}"`);
|
|
74
|
+
return normalCrmUserId(text);
|
|
75
|
+
}
|
|
76
|
+
if (name === "unassigned") {
|
|
77
|
+
const ids = [...new Set(text.split(",").map((one) => one.trim()))];
|
|
78
|
+
const bad = ids.find((one) => !isCrmUserId(one));
|
|
79
|
+
if (bad !== undefined)
|
|
80
|
+
throw new CliError("validation_error", `unassigned takes the CRM's numeric user ids, comma-separated: not "${bad}"`);
|
|
81
|
+
return ids;
|
|
45
82
|
}
|
|
46
83
|
const pairs = text
|
|
47
84
|
.split(",")
|
|
@@ -64,6 +101,17 @@ export const saveCrmSetting = (database, setting, value, now) => {
|
|
|
64
101
|
.prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
|
|
65
102
|
.run(setting, JSON.stringify(value), now);
|
|
66
103
|
};
|
|
104
|
+
const settingValue = (database, key, name) => {
|
|
105
|
+
if (name === "links")
|
|
106
|
+
return crmLinkMode(database, key);
|
|
107
|
+
if (name === "company-types")
|
|
108
|
+
return crmCompanyTypes(database, key);
|
|
109
|
+
if (name === "scope")
|
|
110
|
+
return crmScope(database, key).mode;
|
|
111
|
+
if (name === "self")
|
|
112
|
+
return crmOwnUser(database, key) ?? null;
|
|
113
|
+
return userIds(stored(database, crmSettingKey(key, name)));
|
|
114
|
+
};
|
|
67
115
|
export const crmSettingsOf = (database) => database
|
|
68
116
|
.prepare("SELECT key, value FROM store_settings WHERE key LIKE 'crm.%' ORDER BY key")
|
|
69
117
|
.all()
|
|
@@ -71,10 +119,5 @@ export const crmSettingsOf = (database) => database
|
|
|
71
119
|
const parsed = parseCrmSetting(String(row.key));
|
|
72
120
|
if (!parsed)
|
|
73
121
|
return [];
|
|
74
|
-
return [
|
|
75
|
-
{
|
|
76
|
-
setting: String(row.key),
|
|
77
|
-
value: parsed.name === "links" ? crmLinkMode(database, parsed.key) : crmCompanyTypes(database, parsed.key),
|
|
78
|
-
},
|
|
79
|
-
];
|
|
122
|
+
return [{ setting: String(row.key), value: settingValue(database, parsed.key, parsed.name) }];
|
|
80
123
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CrmDepartment, type CrmField, type CrmItem, type CrmKind, type CrmOrganization, type CrmPerson, type CrmPipeline, type CrmUser } from "../../domain/crm.js";
|
|
1
|
+
import { type CrmDepartment, type CrmField, type CrmItem, type CrmKind, type CrmOrganization, type CrmPerson, type CrmPipeline, type CrmScopeSetting, type CrmUser } from "../../domain/crm.js";
|
|
2
2
|
import { type DealKind } from "../../domain/deals.js";
|
|
3
3
|
import type { AccountPeople } from "../people.js";
|
|
4
4
|
import type { AccountKey } from "../types.js";
|
|
@@ -76,6 +76,18 @@ export interface CrmCopies {
|
|
|
76
76
|
removeUnlisted(key: AccountKey, kind: CrmKind, listed: string[], before: number, options?: {
|
|
77
77
|
dryRun?: boolean;
|
|
78
78
|
}): Promise<number>;
|
|
79
|
+
/** Which deals and leads the account's sync copies: its `scope` and `unassigned` settings. */
|
|
80
|
+
scope(key: AccountKey): Promise<CrmScopeSetting>;
|
|
81
|
+
/** The CRM user whose copy this is (`self` setting); none before the first sync of a source that names one. */
|
|
82
|
+
ownUser(key: AccountKey): Promise<string | undefined>;
|
|
83
|
+
saveOwnUser(key: AccountKey, userId: string): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Marks deleted every stored deal or lead of the kind assigned to a user outside `userIds`; one with no assignee
|
|
86
|
+
* stays. Reads only the store. Answers how many; `dryRun` only counts them.
|
|
87
|
+
*/
|
|
88
|
+
removeOutOfScope(key: AccountKey, kind: DealKind, userIds: readonly string[], options?: {
|
|
89
|
+
dryRun?: boolean;
|
|
90
|
+
}): Promise<number>;
|
|
79
91
|
/** How many of the kind the account holds, not deleted; every contact it ever saved. */
|
|
80
92
|
count(key: AccountKey, kind: CrmKind): Promise<number>;
|
|
81
93
|
/** The stored copy's `deal:<id>` or `lead:<id>`, deleted or not; `null` when the store holds none. */
|
package/dist/store/sqlite/crm.js
CHANGED
|
@@ -3,8 +3,9 @@ import { isItemKind, } from "../../domain/crm.js";
|
|
|
3
3
|
import { DEAL_ROLES } from "../../domain/deals.js";
|
|
4
4
|
import { recordFingerprint } from "../../domain/records.js";
|
|
5
5
|
import { accountPk, findAccountPk } from "./accounts.js";
|
|
6
|
+
import { suggestBaseStages } from "./base-suggest.js";
|
|
6
7
|
import { crmAccountPrefix, crmIdentityId, crmRowsOver, SOURCE, TABLES, textOf } from "./crm-rows.js";
|
|
7
|
-
import { crmCompanyTypes } from "./crm-settings.js";
|
|
8
|
+
import { crmCompanyTypes, crmOwnUser, crmScope, crmSettingKey, saveCrmSetting } from "./crm-settings.js";
|
|
8
9
|
import { syncReportedIn } from "./reported-relations.js";
|
|
9
10
|
import { clearState, syncStateOf, writeState } from "./sync.js";
|
|
10
11
|
import { write } from "./transaction.js";
|
|
@@ -30,8 +31,8 @@ export const crmCopiesOver = (context, people) => {
|
|
|
30
31
|
const savePipelines = (key, kind, pipelines) => write(database, () => {
|
|
31
32
|
const accountId = account(key);
|
|
32
33
|
const at = now();
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
const saved = pipelines.map((pipeline) => savePipeline(accountId, kind, pipeline, at));
|
|
35
|
+
suggestBaseStages(database, kind, saved);
|
|
35
36
|
if (pipelines.length === 0)
|
|
36
37
|
return { saved: 0, skipped: [] };
|
|
37
38
|
const listed = new Set(pipelines.map(({ externalId }) => externalId));
|
|
@@ -535,7 +536,7 @@ export const crmCopiesOver = (context, people) => {
|
|
|
535
536
|
const sourceRole = textOf(company.sourceRole, 200);
|
|
536
537
|
const metadata = {
|
|
537
538
|
...(position ? { position } : {}),
|
|
538
|
-
...(sourceRole ? { source_role: sourceRole } : {}),
|
|
539
|
+
...(sourceRole ? { source_role: [sourceRole] } : {}),
|
|
539
540
|
...(company.primary ? { primary: true } : {}),
|
|
540
541
|
};
|
|
541
542
|
return {
|
|
@@ -575,6 +576,21 @@ export const crmCopiesOver = (context, people) => {
|
|
|
575
576
|
const gone = stored(key, kind).filter((row) => !held.has(String(row.external_id)) && Number(row.updated_at) < before);
|
|
576
577
|
return dryRun ? gone.length : markDeleted(key, kind, gone);
|
|
577
578
|
}),
|
|
579
|
+
scope: async (key) => crmScope(database, key),
|
|
580
|
+
ownUser: async (key) => crmOwnUser(database, key),
|
|
581
|
+
saveOwnUser: async (key, userId) => saveCrmSetting(database, crmSettingKey(key, "self"), userId, now()),
|
|
582
|
+
removeOutOfScope: async (key, kind, userIds, { dryRun } = {}) => write(database, () => {
|
|
583
|
+
const keep = new Set(userIds.map((id) => crmIdentityId(key, id, "user")));
|
|
584
|
+
const accountId = findAccountPk(context, key);
|
|
585
|
+
if (accountId === undefined)
|
|
586
|
+
return 0;
|
|
587
|
+
const rows = database
|
|
588
|
+
.prepare(`SELECT id, external_id, json_extract(metadata, '$.assignee') AS assignee FROM ${TABLES[kind]} ` +
|
|
589
|
+
"WHERE account_id = ? AND deleted_at IS NULL")
|
|
590
|
+
.all(accountId)
|
|
591
|
+
.filter((row) => row.assignee != null && !keep.has(String(row.assignee)));
|
|
592
|
+
return dryRun ? rows.length : markDeleted(key, kind, rows);
|
|
593
|
+
}),
|
|
578
594
|
count: async (key, kind) => kind === "person"
|
|
579
595
|
? Number(database
|
|
580
596
|
.prepare("SELECT count(*) AS n FROM identities WHERE provider = ? AND substr(external_id, 1, ?) = ?")
|
|
@@ -47,6 +47,14 @@ export interface PipelineReport extends Omit<Pipeline, "stages"> {
|
|
|
47
47
|
/** `YYYY-MM-DD`: only rows made since that day are counted; `null` for all. */
|
|
48
48
|
since: string | null;
|
|
49
49
|
}
|
|
50
|
+
/** One stage a deal or a lead entered, as the store saw it: a CRM copy's change is dated by the sync that saw it. */
|
|
51
|
+
export interface StageChange {
|
|
52
|
+
/** `null` for the stage it started in. */
|
|
53
|
+
from: string | null;
|
|
54
|
+
to: string;
|
|
55
|
+
pipeline: string;
|
|
56
|
+
at: string;
|
|
57
|
+
}
|
|
50
58
|
export interface DealPerson extends Party {
|
|
51
59
|
role: DealRole;
|
|
52
60
|
}
|
|
@@ -60,6 +68,14 @@ export interface Deal {
|
|
|
60
68
|
pipeline: string;
|
|
61
69
|
pipelineName: string;
|
|
62
70
|
stage: string;
|
|
71
|
+
/**
|
|
72
|
+
* The stage of the owner's base set its stage maps to, once confirmed; a stage of the base set is its own. `null`
|
|
73
|
+
* while unmapped or only suggested: a board shows it as unsorted.
|
|
74
|
+
*/
|
|
75
|
+
baseStage: {
|
|
76
|
+
ref: string;
|
|
77
|
+
name: string;
|
|
78
|
+
} | null;
|
|
63
79
|
status: DealStatus;
|
|
64
80
|
organization: Party | null;
|
|
65
81
|
/** Both `null` when no amount is expected yet. */
|
|
@@ -129,6 +145,8 @@ export interface DealFilter {
|
|
|
129
145
|
kind?: DealKind;
|
|
130
146
|
/** Only the owner's own, or only one CRM account's copies. */
|
|
131
147
|
account?: DealScope;
|
|
148
|
+
/** Copies only from these CRM accounts; the owner's and agents' own always, as `listRelations` keeps theirs. */
|
|
149
|
+
accounts?: AccountKey[];
|
|
132
150
|
status?: DealStatus;
|
|
133
151
|
/** A stage's name, in any letter case. */
|
|
134
152
|
stage?: string;
|
|
@@ -185,9 +203,19 @@ export interface DealsStore {
|
|
|
185
203
|
add(input: DealInput): Promise<Deal>;
|
|
186
204
|
/** `deal:<id>` or `lead:<id>`; a bare id is a deal. */
|
|
187
205
|
get(reference: string): Promise<Deal>;
|
|
206
|
+
/** Every stage it entered, in the order it entered them: a CRM's own stamps may run out of order. */
|
|
207
|
+
stages(reference: string): Promise<StageChange[]>;
|
|
188
208
|
list(filter?: DealFilter): Promise<Deal[]>;
|
|
189
209
|
/** Deals and leads together, by the words of their title: the most words matched first, then the latest changed. */
|
|
190
210
|
find(search: DealSearch): Promise<Deal[]>;
|
|
211
|
+
/**
|
|
212
|
+
* The project it belongs to; `null` clears it. A CRM copy takes it too: the project is the store's own field, which
|
|
213
|
+
* a CRM has none of and its sync never writes.
|
|
214
|
+
*/
|
|
215
|
+
setProject(reference: string, input: {
|
|
216
|
+
project: string | null;
|
|
217
|
+
by: Author;
|
|
218
|
+
}): Promise<Deal>;
|
|
191
219
|
/** To another stage of its own pipeline; a stage whose outcome is won or lost closes it, an open one reopens it. */
|
|
192
220
|
move(reference: string, input: {
|
|
193
221
|
stage: string;
|
|
@@ -214,8 +242,13 @@ export declare const dealOperationsOver: (context: StoreContext) => {
|
|
|
214
242
|
}) => PipelineReport;
|
|
215
243
|
add: (input: DealInput) => Deal;
|
|
216
244
|
get: (reference: string) => Deal;
|
|
245
|
+
stages: (reference: string) => StageChange[];
|
|
217
246
|
list: (filter?: DealFilter) => Deal[];
|
|
218
247
|
find: ({ words, anyWord, accounts, kinds, limit }: DealSearch) => Deal[];
|
|
248
|
+
setProject: (reference: string, { project, by }: {
|
|
249
|
+
project: string | null;
|
|
250
|
+
by: Author;
|
|
251
|
+
}) => Deal;
|
|
219
252
|
move: (reference: string, { stage, by }: {
|
|
220
253
|
stage: string;
|
|
221
254
|
by: Author;
|
|
@@ -4,6 +4,7 @@ import { currencyOf, formatDecimal, minorUnits, scaleOf } from "../../domain/mon
|
|
|
4
4
|
import { notOverriddenSql } from "../../domain/relations.js";
|
|
5
5
|
import { fold } from "../normalize.js";
|
|
6
6
|
import { proposedBySql } from "./actors.js";
|
|
7
|
+
import { suggestBaseStages } from "./base-suggest.js";
|
|
7
8
|
import { linkKind } from "./link-kinds.js";
|
|
8
9
|
import { authorActor, authorOf, evidenceOf, linkEvidence, projectId, projectOf } from "./memories.js";
|
|
9
10
|
import { requiredThing } from "./notes.js";
|
|
@@ -12,9 +13,11 @@ import { stageAmountsOf } from "./stage-amounts.js";
|
|
|
12
13
|
import { write } from "./transaction.js";
|
|
13
14
|
const TABLES = { deal: "deals", lead: "leads" };
|
|
14
15
|
const itemsSql = (kind) => "SELECT d.*, s.name AS stage_name, s.pipeline_id, p.name AS pipeline_name, b.name AS author_name, " +
|
|
15
|
-
"b.kind AS author_kind, a.provider AS account_provider, a.external_id AS account_external_id " +
|
|
16
|
+
"b.kind AS author_kind, a.provider AS account_provider, a.external_id AS account_external_id, " +
|
|
17
|
+
"bs.id AS base_stage_ref_id, bs.name AS base_stage_name " +
|
|
16
18
|
`FROM ${TABLES[kind]} d JOIN pipeline_stages s ON s.id = d.stage_id JOIN pipelines p ON p.id = s.pipeline_id ` +
|
|
17
|
-
"LEFT JOIN bots b ON d.author_type = 'bot' AND b.id = d.author_id LEFT JOIN accounts a ON a.id = d.account_id"
|
|
19
|
+
"LEFT JOIN bots b ON d.author_type = 'bot' AND b.id = d.author_id LEFT JOIN accounts a ON a.id = d.account_id " +
|
|
20
|
+
"LEFT JOIN pipeline_stages bs ON s.base_confirmed = 1 AND bs.id = s.base_stage_id";
|
|
18
21
|
const MAX_STAGES = 30;
|
|
19
22
|
const MAX_PEOPLE = 20;
|
|
20
23
|
const iso = (value) => new Date(Number(value)).toISOString();
|
|
@@ -70,6 +73,9 @@ export const dealOperationsOver = (context) => {
|
|
|
70
73
|
pipeline: `pipeline:${row.pipeline_id}`,
|
|
71
74
|
pipelineName: String(row.pipeline_name),
|
|
72
75
|
stage: fields.stage,
|
|
76
|
+
baseStage: row.base_stage_ref_id == null
|
|
77
|
+
? null
|
|
78
|
+
: { ref: `stage:${row.base_stage_ref_id}`, name: String(row.base_stage_name) },
|
|
73
79
|
status: fields.status,
|
|
74
80
|
organization: partyOf(row.organization_id == null ? null : "organization", row.organization_id),
|
|
75
81
|
currency,
|
|
@@ -283,6 +289,7 @@ export const dealOperationsOver = (context) => {
|
|
|
283
289
|
kept.forEach((stage, index) => {
|
|
284
290
|
insert.run(made, stage.name, fold(stage.name), index + 1, stage.outcome, at, at);
|
|
285
291
|
});
|
|
292
|
+
suggestBaseStages(database, pipelineKind, [made]);
|
|
286
293
|
return made;
|
|
287
294
|
});
|
|
288
295
|
return pipelinesOf(pipelineRows(pipelineKind, undefined, { deleted: true }).filter((row) => Number(row.id) === id))[0];
|
|
@@ -348,6 +355,20 @@ export const dealOperationsOver = (context) => {
|
|
|
348
355
|
return read(`${kind}:${id}`);
|
|
349
356
|
},
|
|
350
357
|
get: (reference) => read(reference),
|
|
358
|
+
stages: (reference) => {
|
|
359
|
+
const { kind, row } = rowOf(reference);
|
|
360
|
+
return database
|
|
361
|
+
.prepare("SELECT c.pipeline_id, c.changed_at, f.name AS from_name, t.name AS to_name FROM pipeline_stage_changes c " +
|
|
362
|
+
"LEFT JOIN pipeline_stages f ON f.id = c.from_stage_id JOIN pipeline_stages t ON t.id = c.to_stage_id " +
|
|
363
|
+
"WHERE c.item_type = ? AND c.item_id = ? ORDER BY c.id")
|
|
364
|
+
.all(kind, Number(row.id))
|
|
365
|
+
.map((change) => ({
|
|
366
|
+
from: change.from_name == null ? null : String(change.from_name),
|
|
367
|
+
to: String(change.to_name),
|
|
368
|
+
pipeline: `pipeline:${change.pipeline_id}`,
|
|
369
|
+
at: iso(change.changed_at),
|
|
370
|
+
}));
|
|
371
|
+
},
|
|
351
372
|
list: (filter = {}) => {
|
|
352
373
|
const kind = kindOf(filter.kind);
|
|
353
374
|
const project = filter.project === undefined ? null : projectId(database, filter.project);
|
|
@@ -361,13 +382,17 @@ export const dealOperationsOver = (context) => {
|
|
|
361
382
|
const provider = scope === undefined || scope === "own" ? null : scope.provider;
|
|
362
383
|
const account = scope === undefined || scope === "own" ? null : scope.account;
|
|
363
384
|
const limit = limitOf(filter.limit);
|
|
385
|
+
const keys = filter.accounts?.map((one) => `${one.provider}:${one.account}`);
|
|
386
|
+
const inAccounts = keys === undefined
|
|
387
|
+
? ""
|
|
388
|
+
: `AND (d.account_id IS NULL OR a.provider || ':' || a.external_id IN (${keys.map(() => "?").join(", ") || "NULL"})) `;
|
|
364
389
|
return database
|
|
365
390
|
.prepare(`${itemsSql(kind)} WHERE d.deleted_at IS NULL AND (? IS NULL OR d.status = ?) ` +
|
|
366
391
|
"AND (? IS NULL OR d.project_id = ?) AND (? IS NULL OR s.name_folded = ?) AND (? IS NULL OR p.id = ?) " +
|
|
367
392
|
"AND (? IS NULL OR d.organization_id = ?) AND (? = 0 OR d.account_id IS NULL) " +
|
|
368
|
-
`AND (? IS NULL OR (a.provider = ? AND a.external_id = ?)) AND ${by.sql} ` +
|
|
393
|
+
`AND (? IS NULL OR (a.provider = ? AND a.external_id = ?)) ${inAccounts}AND ${by.sql} ` +
|
|
369
394
|
"ORDER BY d.created_at DESC, d.id DESC LIMIT ? OFFSET ?")
|
|
370
|
-
.all(status, status, project, project, stage, stage, pipeline, pipeline, organization?.id ?? null, organization?.id ?? null, own, provider, provider, account, ...by.params, limit, Math.max(0, Math.trunc(filter.offset ?? 0)))
|
|
395
|
+
.all(status, status, project, project, stage, stage, pipeline, pipeline, organization?.id ?? null, organization?.id ?? null, own, provider, provider, account, ...(keys ?? []), ...by.params, limit, Math.max(0, Math.trunc(filter.offset ?? 0)))
|
|
371
396
|
.map(itemOf(kind));
|
|
372
397
|
},
|
|
373
398
|
find: ({ words, anyWord, accounts, kinds = DEAL_KINDS, limit }) => {
|
|
@@ -394,6 +419,17 @@ export const dealOperationsOver = (context) => {
|
|
|
394
419
|
.slice(0, most)
|
|
395
420
|
.map(({ kind, row }) => read(`${kind}:${row.id}`));
|
|
396
421
|
},
|
|
422
|
+
setProject: (reference, { project, by }) => {
|
|
423
|
+
authorActor(context, by);
|
|
424
|
+
const { kind, row } = rowOf(reference);
|
|
425
|
+
if (row.deleted_at != null)
|
|
426
|
+
refuse(`${kind}:${row.id} is deleted; it no longer changes`, "deal.closed");
|
|
427
|
+
const id = project === null ? null : projectId(database, project);
|
|
428
|
+
write(database, () => database
|
|
429
|
+
.prepare(`UPDATE ${TABLES[kind]} SET project_id = ?, updated_at = ? WHERE id = ?`)
|
|
430
|
+
.run(id, now(), Number(row.id)));
|
|
431
|
+
return read(`${kind}:${row.id}`);
|
|
432
|
+
},
|
|
397
433
|
move: (reference, { stage, by }) => {
|
|
398
434
|
authorActor(context, by);
|
|
399
435
|
const { kind, row } = rowOf(reference);
|
|
@@ -431,8 +467,10 @@ export const dealsStoreOver = (context) => {
|
|
|
431
467
|
pipeline: async (reference, options) => operations.pipeline(reference, options),
|
|
432
468
|
add: async (input) => operations.add(input),
|
|
433
469
|
get: async (reference) => operations.get(reference),
|
|
470
|
+
stages: async (reference) => operations.stages(reference),
|
|
434
471
|
list: async (filter) => operations.list(filter),
|
|
435
472
|
find: async (search) => operations.find(search),
|
|
473
|
+
setProject: async (reference, input) => operations.setProject(reference, input),
|
|
436
474
|
move: async (reference, input) => operations.move(reference, input),
|
|
437
475
|
close: async (reference, input) => operations.close(reference, input),
|
|
438
476
|
};
|
|
@@ -101,9 +101,13 @@ export const decisionRequestStoreOver = ({ database, now }) => {
|
|
|
101
101
|
const where = [OPEN, ...filters].join(" AND ");
|
|
102
102
|
return write(database, () => Number(database.prepare(`UPDATE decision_requests SET cancelled_at=? WHERE ${where}`).run(now(), ...values).changes));
|
|
103
103
|
},
|
|
104
|
-
prune: (at = now()) =>
|
|
105
|
-
|
|
106
|
-
.
|
|
104
|
+
prune: (at = now()) => {
|
|
105
|
+
const cutoff = at - PRUNE_AFTER_MS;
|
|
106
|
+
// One `?` per use: Node 22.16's node:sqlite cannot bind a numbered `?1` by position.
|
|
107
|
+
return write(database, () => Number(database
|
|
108
|
+
.prepare(`DELETE FROM decision_requests WHERE expires_at < ? OR used_at < ? OR cancelled_at < ?`)
|
|
109
|
+
.run(cutoff, cutoff, cutoff).changes));
|
|
110
|
+
},
|
|
107
111
|
open: (message) => database
|
|
108
112
|
.prepare(`SELECT * FROM decision_requests WHERE ${SAME_MESSAGE} AND ${OPEN} AND expires_at>? ORDER BY id`)
|
|
109
113
|
.all(message.channel, message.botProfile, message.chatId, message.messageId, now()).map(requestOf),
|
|
@@ -32,9 +32,12 @@ const threadText = (database, threadId) => {
|
|
|
32
32
|
const core = mailCore(body, earlier);
|
|
33
33
|
earlier.push(body);
|
|
34
34
|
const subject = titleOf(str(row.subject));
|
|
35
|
-
const
|
|
35
|
+
const said = subject ? `${subject}\n\n${core}` : core;
|
|
36
|
+
const sender = str(row.sender);
|
|
36
37
|
at.set(String(row.id), int(row.at) ?? null);
|
|
37
|
-
|
|
38
|
+
// The sender at the end, not as "Name: " before the subject: there a model read the sender as the topic of every
|
|
39
|
+
// email they wrote (cli-testing knowledge-graph-memory/meaning-loss, Gemma in memo).
|
|
40
|
+
return { id: String(row.id), sender: null, text: sender ? `${said}\n\nFrom: ${sender}` : said };
|
|
38
41
|
});
|
|
39
42
|
return { lines, at };
|
|
40
43
|
};
|
|
@@ -97,9 +100,10 @@ export const chunkAttachments = (database, accountId, emailId, now) => {
|
|
|
97
100
|
}
|
|
98
101
|
};
|
|
99
102
|
const MAIL_CUT = "mail_pieces_cut";
|
|
100
|
-
|
|
103
|
+
// Each change to a piece's text bumps this, so every store cuts its mail again once and re-embeds it.
|
|
104
|
+
const CUT = "email, sender at the end";
|
|
101
105
|
export const drainEmailIndex = (database, stemmerFor) => {
|
|
102
|
-
// Mail was once cut without cleaning, then across the emails of a thread; a store cut
|
|
106
|
+
// Mail was once cut without cleaning, then across the emails of a thread, then with its sender before the subject; a store cut any of those ways is cut again.
|
|
103
107
|
if (database.prepare("SELECT value FROM store_settings WHERE key = ?").get(MAIL_CUT)?.value !== CUT)
|
|
104
108
|
write(database, () => {
|
|
105
109
|
const single = database
|
|
@@ -4,6 +4,7 @@ import { findingFingerprint, findingHash } from "../../meetings/index.js";
|
|
|
4
4
|
import { assertMayDecide } from "../decisions.js";
|
|
5
5
|
import { EVIDENCE_FINDINGS_KIND, validateEvidenceFindings, } from "../evidence-findings.js";
|
|
6
6
|
import { contextRecordOf } from "./context-evidence.js";
|
|
7
|
+
import { quoteKept } from "./evidence-signature.js";
|
|
7
8
|
import { sourcesOfReferences } from "./evidence-sources.js";
|
|
8
9
|
import { findingsAgent, findingsCoreOver } from "./findings-core.js";
|
|
9
10
|
import { authorActor } from "./memories.js";
|
|
@@ -47,6 +48,7 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
47
48
|
input: { schemaVersion: 1, key: input.key, extractor: input.extractor, findings: input.findings },
|
|
48
49
|
fingerprint: findingHash({ input, author: proposal.proposedBy.id }),
|
|
49
50
|
status: proposal.status === "proposed" ? "proposed" : "reviewed",
|
|
51
|
+
ownerOnly: input.ownerOnly === true || input.findings.some((finding) => finding.kind === "payment"),
|
|
50
52
|
author: proposal.proposedBy,
|
|
51
53
|
decidedBy: proposal.decidedBy,
|
|
52
54
|
decidedAt: proposal.decidedAt,
|
|
@@ -61,11 +63,18 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
61
63
|
};
|
|
62
64
|
const cite = () => {
|
|
63
65
|
let readBytes = 0;
|
|
66
|
+
const texts = new Map();
|
|
67
|
+
const quoted = (evidence) => {
|
|
68
|
+
if (evidence.quote !== undefined && !quoteKept(evidence.quote, texts.get(evidence.reference) ?? ""))
|
|
69
|
+
throw new CliError("validation_error", `the quote cited from ${evidence.reference} is not in its text`, {
|
|
70
|
+
reason: "finding.quote-not-found",
|
|
71
|
+
});
|
|
72
|
+
};
|
|
64
73
|
return (evidence, _finding, _project, seen) => {
|
|
65
74
|
if (seen.has(evidence.reference)) {
|
|
66
75
|
if (seen.get(evidence.reference) !== evidence.fingerprint)
|
|
67
76
|
return stale();
|
|
68
|
-
return;
|
|
77
|
+
return quoted(evidence);
|
|
69
78
|
}
|
|
70
79
|
const record = contextRecordOf(database, evidence.reference);
|
|
71
80
|
if (!record)
|
|
@@ -77,6 +86,8 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
77
86
|
if (fingerprint !== evidence.fingerprint)
|
|
78
87
|
return stale();
|
|
79
88
|
seen.set(evidence.reference, fingerprint);
|
|
89
|
+
texts.set(evidence.reference, record.text);
|
|
90
|
+
quoted(evidence);
|
|
80
91
|
};
|
|
81
92
|
};
|
|
82
93
|
const snapshot = (input) => core.snapshot(input, cite());
|
|
@@ -98,7 +109,7 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
98
109
|
};
|
|
99
110
|
};
|
|
100
111
|
return {
|
|
101
|
-
stage: async (raw, rawAgent) => {
|
|
112
|
+
stage: async (raw, rawAgent, { ownerOnly = false } = {}) => {
|
|
102
113
|
const input = validateEvidenceFindings(raw);
|
|
103
114
|
const projects = new Set(input.findings.flatMap((finding) => finding.project ?? []));
|
|
104
115
|
if (projects.size > 1)
|
|
@@ -109,7 +120,7 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
109
120
|
const by = findingsAgent(rawAgent);
|
|
110
121
|
return write(database, () => {
|
|
111
122
|
const { actor } = authorActor(context, by);
|
|
112
|
-
const inputHash = findingHash({ input, model: by.model ?? null });
|
|
123
|
+
const inputHash = findingHash({ input, model: by.model ?? null, ...(ownerOnly ? { ownerOnly } : {}) });
|
|
113
124
|
const held = database
|
|
114
125
|
.prepare("SELECT id FROM proposed_actions WHERE account_id IS NULL AND kind=? AND proposed_by_type=? AND proposed_by_id=? AND json_extract(payload,'$.key')=? ORDER BY id LIMIT 1")
|
|
115
126
|
.get(EVIDENCE_FINDINGS_KIND, actor.type, actor.id, input.key);
|
|
@@ -128,11 +139,24 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
128
139
|
kind: EVIDENCE_FINDINGS_KIND,
|
|
129
140
|
by,
|
|
130
141
|
...(target === undefined ? {} : { target }),
|
|
131
|
-
payload: {
|
|
142
|
+
payload: {
|
|
143
|
+
...input,
|
|
144
|
+
inputHash,
|
|
145
|
+
snapshotHash,
|
|
146
|
+
snapshotParts,
|
|
147
|
+
agentModel: by.model ?? null,
|
|
148
|
+
...(ownerOnly ? { ownerOnly: true } : {}),
|
|
149
|
+
},
|
|
132
150
|
}));
|
|
133
151
|
});
|
|
134
152
|
},
|
|
135
153
|
get: async (id) => view(load(id)),
|
|
154
|
+
citable: async (reference) => {
|
|
155
|
+
const record = contextRecordOf(database, reference);
|
|
156
|
+
return record
|
|
157
|
+
? { reference, fingerprint: contextEvidenceFingerprint(record), title: record.title, text: record.text }
|
|
158
|
+
: null;
|
|
159
|
+
},
|
|
136
160
|
sources: async (id) => sourcesOfReferences(database, view(load(id)).input.findings.flatMap((finding) => finding.evidence.map((one) => one.reference))),
|
|
137
161
|
list: async ({ project, limit = 20, offset = 0, status } = {}) => {
|
|
138
162
|
if (status !== undefined && status !== "proposed" && status !== "reviewed")
|
|
@@ -170,7 +194,7 @@ export const evidenceFindingsStoreOver = (context) => {
|
|
|
170
194
|
assertMayDecide({
|
|
171
195
|
id,
|
|
172
196
|
proposedBy: proposals.proposer(id),
|
|
173
|
-
ownerOnly: batch.
|
|
197
|
+
ownerOnly: batch.ownerOnly,
|
|
174
198
|
}, decision.by, context.decisions);
|
|
175
199
|
if (batch.receipt) {
|
|
176
200
|
if (JSON.stringify(batch.receipt.accepted) !== JSON.stringify(accepted))
|
|
@@ -72,13 +72,15 @@ export const findingsCoreOver = (context, kind) => {
|
|
|
72
72
|
};
|
|
73
73
|
const create = (finding, agent, decidedBy) => {
|
|
74
74
|
const evidence = finding.evidence.map((item) => item.reference);
|
|
75
|
+
// The quote goes into the evidence link, so a later source check knows which words the fact rests on.
|
|
76
|
+
const quoted = finding.evidence.map((item) => item.quote === undefined ? item.reference : { ref: item.reference, quote: item.quote });
|
|
75
77
|
if (finding.kind === "fact") {
|
|
76
78
|
const fact = facts.add({
|
|
77
79
|
type: finding.type,
|
|
78
80
|
statement: finding.statement,
|
|
79
81
|
...(finding.project === undefined ? {} : { project: finding.project }),
|
|
80
82
|
...(finding.subject === undefined ? {} : { subject: finding.subject }),
|
|
81
|
-
evidence,
|
|
83
|
+
evidence: quoted,
|
|
82
84
|
by: agent,
|
|
83
85
|
});
|
|
84
86
|
return facts.acceptChecked(fact.ref, decidedBy).ref;
|
|
@@ -99,7 +101,7 @@ export const findingsCoreOver = (context, kind) => {
|
|
|
99
101
|
const id = taskRowOf(database, task.id);
|
|
100
102
|
if (id === undefined)
|
|
101
103
|
throw new CliError("configuration_error", "created finding task is unavailable");
|
|
102
|
-
linkEvidence(context, { type: finding.type, id },
|
|
104
|
+
linkEvidence(context, { type: finding.type, id }, quoted, "agent");
|
|
103
105
|
if (finding.assignee)
|
|
104
106
|
tasks.assign(task.id, { to: finding.assignee, role: "assignee" }, agent);
|
|
105
107
|
return task.ref;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
2
|
+
import { type IdentityLinkPayload } from "../identity-links.js";
|
|
3
|
+
/** A stored `identity_link`, with the two people its identities are on now. */
|
|
4
|
+
export interface LinkRow {
|
|
5
|
+
id: string;
|
|
6
|
+
status: string;
|
|
7
|
+
payload: IdentityLinkPayload;
|
|
8
|
+
/** `undefined` once a purge erased an identity it names. */
|
|
9
|
+
pair: [number, number] | undefined;
|
|
10
|
+
}
|
|
11
|
+
export declare const pairOf: (a: number | string, b: number | string) => string;
|
|
12
|
+
/** The people a proposal's identities are on now; merges since may have moved either side. */
|
|
13
|
+
export declare const currentPairOf: (database: CacheDatabase, payload: IdentityLinkPayload) => [number, number] | undefined;
|
|
14
|
+
/** The proposals that still say something: waiting for the owner, or turned down by them. */
|
|
15
|
+
export declare const linkRows: (database: CacheDatabase, statuses?: readonly string[]) => LinkRow[];
|
|
16
|
+
/**
|
|
17
|
+
* What a proposal rests on: its signal and the two identities it names, in either order. The owner's rejection
|
|
18
|
+
* holds against this evidence only (NEED-2444); a new signal or identity for the same people proposes them again.
|
|
19
|
+
*/
|
|
20
|
+
export declare const evidenceKeyOf: ({ signal, identity, with: other, }: Pick<IdentityLinkPayload, "signal" | "identity" | "with">) => string;
|
|
21
|
+
/** The evidence the owner rejected for linking these two people, by any proposal that names them now. */
|
|
22
|
+
export declare const rejectedEvidence: (database: CacheDatabase, a: number, b: number) => Set<string> | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Every proposal by the pair of people it names now, read once, for a pass that checks many pairs: the open one, and
|
|
25
|
+
* the evidence the owner rejected. `merged` keeps it true after the pass joins two people itself.
|
|
26
|
+
*/
|
|
27
|
+
export declare const linkIndex: (database: CacheDatabase) => {
|
|
28
|
+
/** Whether any proposal, open or rejected, names this pair. */
|
|
29
|
+
known: (a: number, b: number) => boolean;
|
|
30
|
+
open: (a: number, b: number) => LinkRow | undefined;
|
|
31
|
+
rejected: (a: number, b: number) => ReadonlySet<string> | undefined;
|
|
32
|
+
/** Whether any proposal moves this identity, such as a meeting guest's that is already waiting. */
|
|
33
|
+
names: (identity: string) => boolean;
|
|
34
|
+
add: (row: LinkRow) => void;
|
|
35
|
+
merged: (from: number, into: number) => void;
|
|
36
|
+
};
|
|
37
|
+
export type LinkIndex = ReturnType<typeof linkIndex>;
|
|
38
|
+
export declare const sidesOf: (database: CacheDatabase, a: number, b: number) => [string, string];
|
|
39
|
+
/**
|
|
40
|
+
* Brings open proposals up to date with the people they name: one whose two people are one now, whose identity a
|
|
41
|
+
* purge erased, or that a merge made a second one for the same pair, is deleted, since nothing is left to decide; a
|
|
42
|
+
* live one takes the people and sides it names now, so the owner decides what is there. With `persons`, only the
|
|
43
|
+
* proposals that name one of them.
|
|
44
|
+
*/
|
|
45
|
+
export declare const settleLinks: (database: CacheDatabase, persons?: readonly number[]) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Deletes every `identity_link` that names an identity no longer in the store, whatever its status: its payload
|
|
48
|
+
* names the identity's phone or address, which must go with it.
|
|
49
|
+
*/
|
|
50
|
+
export declare const forgetErasedLinks: (database: CacheDatabase) => void;
|