@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,77 @@
|
|
|
1
|
+
import { formatDecimal, minorUnits, scaleOf } from "../domain/money.js";
|
|
2
|
+
import { sectionOf } from "./business-context.js";
|
|
3
|
+
const invoiceLine = (invoice, linkedBy) => ({
|
|
4
|
+
ref: invoice.ref,
|
|
5
|
+
title: invoice.title,
|
|
6
|
+
direction: invoice.direction,
|
|
7
|
+
status: invoice.status,
|
|
8
|
+
review: invoice.review,
|
|
9
|
+
currency: invoice.currency,
|
|
10
|
+
total: invoice.total,
|
|
11
|
+
open: invoice.open,
|
|
12
|
+
dueDate: invoice.dueDate,
|
|
13
|
+
linkedBy: [linkedBy],
|
|
14
|
+
});
|
|
15
|
+
const paymentLine = (payment, linkedBy) => ({
|
|
16
|
+
ref: payment.ref,
|
|
17
|
+
title: payment.title,
|
|
18
|
+
direction: payment.direction,
|
|
19
|
+
review: payment.review,
|
|
20
|
+
currency: payment.currency,
|
|
21
|
+
amount: payment.amount,
|
|
22
|
+
date: payment.date,
|
|
23
|
+
linkedBy: [linkedBy],
|
|
24
|
+
});
|
|
25
|
+
/** Each row once, with every way it was reached. */
|
|
26
|
+
const merged = (rows) => {
|
|
27
|
+
const byRef = new Map();
|
|
28
|
+
for (const row of rows) {
|
|
29
|
+
const held = byRef.get(row.ref);
|
|
30
|
+
if (!held)
|
|
31
|
+
byRef.set(row.ref, { ...row, linkedBy: [...row.linkedBy] });
|
|
32
|
+
else
|
|
33
|
+
for (const way of row.linkedBy)
|
|
34
|
+
if (!held.linkedBy.includes(way))
|
|
35
|
+
held.linkedBy.push(way);
|
|
36
|
+
}
|
|
37
|
+
return [...byRef.values()];
|
|
38
|
+
};
|
|
39
|
+
const totalled = (rows) => {
|
|
40
|
+
const totals = new Map();
|
|
41
|
+
for (const row of rows) {
|
|
42
|
+
if (!row.confirmed)
|
|
43
|
+
continue;
|
|
44
|
+
const key = `${row.status}|${row.currency}`;
|
|
45
|
+
const held = totals.get(key) ?? { status: row.status, currency: row.currency, rows: 0, units: 0 };
|
|
46
|
+
held.rows += 1;
|
|
47
|
+
held.units += minorUnits(row.amount, row.currency);
|
|
48
|
+
if (row.open !== undefined)
|
|
49
|
+
held.open = (held.open ?? 0) + minorUnits(row.open, row.currency);
|
|
50
|
+
totals.set(key, held);
|
|
51
|
+
}
|
|
52
|
+
return [...totals.values()].map(({ units, open, ...total }) => ({
|
|
53
|
+
...total,
|
|
54
|
+
total: formatDecimal(units, scaleOf(total.currency)),
|
|
55
|
+
...(open === undefined ? {} : { open: formatDecimal(open, scaleOf(total.currency)) }),
|
|
56
|
+
}));
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Invoices and payments as the finance store holds them, totalled per status and currency. A payment is never
|
|
60
|
+
* inferred from an invoice, nor an invoice's state from a deal's stage: only what the store records.
|
|
61
|
+
*/
|
|
62
|
+
export const moneyOf = (rows, limit, next, complete) => {
|
|
63
|
+
const invoices = merged(rows.invoices.map(([invoice, way]) => invoiceLine(invoice, way)));
|
|
64
|
+
const payments = merged(rows.payments.map(([payment, way]) => paymentLine(payment, way)));
|
|
65
|
+
const more = { more: !complete };
|
|
66
|
+
return {
|
|
67
|
+
invoices: sectionOf(invoices, limit, next.invoices, more),
|
|
68
|
+
payments: sectionOf(payments, limit, next.payments, more),
|
|
69
|
+
...(rows.orders === undefined ? {} : { orders: sectionOf(rows.orders, limit, next.orders ?? null, more) }),
|
|
70
|
+
totals: {
|
|
71
|
+
invoices: totalled(invoices.map((one) => ({ ...one, amount: one.total, confirmed: one.review === "confirmed" }))),
|
|
72
|
+
payments: totalled(payments.map((one) => ({ ...one, status: one.direction, confirmed: one.review === "confirmed" }))),
|
|
73
|
+
},
|
|
74
|
+
unconfirmed: [...invoices, ...payments].filter((one) => one.review !== "confirmed").length,
|
|
75
|
+
complete,
|
|
76
|
+
};
|
|
77
|
+
};
|
|
@@ -61,11 +61,13 @@ export interface NotesEmbedded {
|
|
|
61
61
|
/** Chunks still without a vector, when `maxChunks` stopped the run, or records still to enrich. */
|
|
62
62
|
left: boolean;
|
|
63
63
|
/** What the owner's enrichment did first; absent when it is off. */
|
|
64
|
+
/** The run, or `failed` with no counts when enrichment threw before it could count. */
|
|
64
65
|
enrichment?: Exclude<EnrichRun, {
|
|
65
66
|
status: "off";
|
|
66
67
|
}> | {
|
|
67
68
|
status: "failed";
|
|
68
69
|
reason: string;
|
|
70
|
+
reason_id: "enrichment.error";
|
|
69
71
|
};
|
|
70
72
|
}
|
|
71
73
|
export declare const localModel: (choice: string | undefined, env: NodeJS.ProcessEnv | undefined, command: string | undefined) => {
|
|
@@ -217,8 +219,9 @@ export interface NotesFoundOf extends NotesFound {
|
|
|
217
219
|
* `search notes` with `--source`: the notes accounts the list names — `notes:internal`, `notes:<folder id>`, `notes`
|
|
218
220
|
* — and every entry naming none of them in `skipped`. Without a list, every notes account `type` leaves in.
|
|
219
221
|
*/
|
|
220
|
-
export declare const searchNotesOf: (store: MessageStore, account: AccountKey, text: string, { accounts: list, app, ...query }: Omit<NotesQuery, "folderIds" | "withInternal"> & {
|
|
222
|
+
export declare const searchNotesOf: (store: MessageStore, account: AccountKey, text: string, { accounts: list, app, env, ...query }: Omit<NotesQuery, "folderIds" | "withInternal"> & {
|
|
221
223
|
accounts?: string;
|
|
222
224
|
app?: AppIdentity;
|
|
225
|
+
env?: NodeJS.ProcessEnv;
|
|
223
226
|
}) => Promise<NotesFoundOf>;
|
|
224
227
|
export {};
|
|
@@ -5,7 +5,8 @@ import { normalizeTag } from "../domain/tags.js";
|
|
|
5
5
|
import { defaultThreads, isTextModelInstalled, openEmbedder, textModelsDirectory, } from "../embeddings/embed.js";
|
|
6
6
|
import { chosenTextModel, meaningFloor, textModel, vectorModelKey } from "../embeddings/models.js";
|
|
7
7
|
import { DEFAULT_IMAGE_MODEL, imageModel } from "../images/picture/models.js";
|
|
8
|
-
import { entriesOfList, fitting } from "../search/account-list.js";
|
|
8
|
+
import { entriesOfList, fitting, sourceOf } from "../search/account-list.js";
|
|
9
|
+
import { OWNER_NOTES } from "../search/groups.js";
|
|
9
10
|
import { fieldsOfQuery as fieldsOf, meaningWords } from "../search/lucene/meaning-parts.js";
|
|
10
11
|
import { plainQuery } from "../search/lucene/plain.js";
|
|
11
12
|
import { validateFields } from "../search/lucene/registry.js";
|
|
@@ -17,9 +18,10 @@ import { TOTAL_CAP, totalOf } from "../search/total.js";
|
|
|
17
18
|
import { openPictureEmbedderFor } from "./document-pictures.js";
|
|
18
19
|
import { documentSource } from "./document-source.js";
|
|
19
20
|
import { enrichRecords } from "./enrichment.js";
|
|
21
|
+
import { meaningModel } from "./meaning-model.js";
|
|
20
22
|
import { plainQueryWords, titleQuery, titleTiers } from "./notes-title-tier.js";
|
|
21
23
|
import { PICTURE_FLOOR, plainWords } from "./pictures.js";
|
|
22
|
-
import { listContextOf } from "./search-kind.js";
|
|
24
|
+
import { groupLockOf, listContextOf } from "./search-kind.js";
|
|
23
25
|
/**
|
|
24
26
|
* Text in the query language messages use, parsed for a store search: `date:` read in `timezone`, and the store's
|
|
25
27
|
* stemmer when a word is stemmed.
|
|
@@ -139,7 +141,11 @@ const enrichedFirst = async (store, types, options, embed) => {
|
|
|
139
141
|
enrichment = run;
|
|
140
142
|
}
|
|
141
143
|
catch (error) {
|
|
142
|
-
enrichment = {
|
|
144
|
+
enrichment = {
|
|
145
|
+
status: "failed",
|
|
146
|
+
reason: error instanceof Error ? error.message : "enrichment failed",
|
|
147
|
+
reason_id: "enrichment.error",
|
|
148
|
+
};
|
|
143
149
|
}
|
|
144
150
|
let embedded;
|
|
145
151
|
try {
|
|
@@ -175,7 +181,7 @@ export const nearestNotes = async (store, query, options) => {
|
|
|
175
181
|
const queryVector = async (store, query, { model: choice, folderIds, withInternal, source, env, threads, command, embedders }) => {
|
|
176
182
|
if (!query.trim())
|
|
177
183
|
throw new CliError("validation_error", "say what to find in the notes");
|
|
178
|
-
const { model, directory, key } = localModel(choice, env, command);
|
|
184
|
+
const { model, directory, key } = localModel(choice ?? (await meaningModel(store, env)), env, command);
|
|
179
185
|
const scope = {
|
|
180
186
|
...(folderIds ? { folderIds } : {}),
|
|
181
187
|
...(withInternal ? { withInternal: true } : {}),
|
|
@@ -556,7 +562,7 @@ export const notesScopeOf = (accounts) => {
|
|
|
556
562
|
* `search notes` with `--source`: the notes accounts the list names — `notes:internal`, `notes:<folder id>`, `notes`
|
|
557
563
|
* — and every entry naming none of them in `skipped`. Without a list, every notes account `type` leaves in.
|
|
558
564
|
*/
|
|
559
|
-
export const searchNotesOf = async (store, account, text, { accounts: list, app, ...query }) => {
|
|
565
|
+
export const searchNotesOf = async (store, account, text, { accounts: list, app, env, ...query }) => {
|
|
560
566
|
text = plainQuery(text);
|
|
561
567
|
const own = app?.command ?? query.command;
|
|
562
568
|
const what = `${own === undefined ? "" : `${own} `}search notes`;
|
|
@@ -566,9 +572,15 @@ export const searchNotesOf = async (store, account, text, { accounts: list, app,
|
|
|
566
572
|
...(await store.notes.documentsAccounts()).map(({ id }) => ({ provider: "notes", account: id })),
|
|
567
573
|
...[await store.notes.addedAccount()].flatMap((added) => (added ? [{ provider: "notes", account: added.id }] : [])),
|
|
568
574
|
];
|
|
569
|
-
const
|
|
575
|
+
const lock = groupLockOf(app, env);
|
|
576
|
+
const typedScope = list ?? (lock === undefined ? undefined : `group:${lock}`);
|
|
577
|
+
// memo's own notes are the owner's, so a group reads them beside its folders (WC-ACCT-13).
|
|
578
|
+
const notesScope = typedScope !== undefined && /(^|,)\s*group:/i.test(typedScope)
|
|
579
|
+
? `${typedScope},${sourceOf(OWNER_NOTES)}`
|
|
580
|
+
: typedScope;
|
|
581
|
+
const named = notesScope === undefined
|
|
570
582
|
? { accounts: held, skipped: [] }
|
|
571
|
-
: fitting(entriesOfList(
|
|
583
|
+
: fitting(entriesOfList(notesScope, await listContextOf(store, account, app, undefined, env)), ["notes", "documents"], what);
|
|
572
584
|
const source = query.source ?? noteSourceOfQuery(text).source;
|
|
573
585
|
const typed = named.accounts.filter(({ account }) => source === undefined || (account === INTERNAL_NOTES) === (source === "internal"));
|
|
574
586
|
if (!typed.length && list !== undefined)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type DealKind } from "../domain/deals.js";
|
|
2
|
+
import type { AccountKey, Deal, MessageStore, Organization } from "../store/index.js";
|
|
3
|
+
import { type Category, type Coverage, type Related, type Section } from "./business-context.js";
|
|
4
|
+
import { type Communications } from "./communications.js";
|
|
5
|
+
import { type MoneySection } from "./money-context.js";
|
|
6
|
+
export interface DealLine {
|
|
7
|
+
ref: string;
|
|
8
|
+
kind: Deal["kind"];
|
|
9
|
+
title: string;
|
|
10
|
+
summary: string;
|
|
11
|
+
stage: string;
|
|
12
|
+
status: Deal["status"];
|
|
13
|
+
currency: string | null;
|
|
14
|
+
amount: string | null;
|
|
15
|
+
closeDate: string | null;
|
|
16
|
+
project: string | null;
|
|
17
|
+
}
|
|
18
|
+
export interface ProjectLine {
|
|
19
|
+
ref: string;
|
|
20
|
+
name: string;
|
|
21
|
+
/** It names the organization, or one of its deals does. */
|
|
22
|
+
via: ("organization" | "deal")[];
|
|
23
|
+
}
|
|
24
|
+
export interface TaskLine {
|
|
25
|
+
ref: string;
|
|
26
|
+
key: string;
|
|
27
|
+
title: string;
|
|
28
|
+
status: string;
|
|
29
|
+
project: string;
|
|
30
|
+
dueAt: string | null;
|
|
31
|
+
}
|
|
32
|
+
export interface OrganizationContext {
|
|
33
|
+
organization: Organization & {
|
|
34
|
+
own: boolean;
|
|
35
|
+
};
|
|
36
|
+
categories: Category[];
|
|
37
|
+
/** Its members, each once with every role and the standing of each; the current ones first. */
|
|
38
|
+
people: Section<Related>;
|
|
39
|
+
/** Who is its customer. */
|
|
40
|
+
customers: Section<Related>;
|
|
41
|
+
/** Whom it is a customer of: "supplier of", read backwards. */
|
|
42
|
+
suppliers: Section<Related>;
|
|
43
|
+
partners: Section<Related>;
|
|
44
|
+
projects: Section<ProjectLine>;
|
|
45
|
+
/** Open deals and leads it is the client of. */
|
|
46
|
+
deals: Section<DealLine>;
|
|
47
|
+
money: MoneySection;
|
|
48
|
+
/** Counts only, through its own people other than the owner, in the accounts read. */
|
|
49
|
+
communications: Communications["counts"] & {
|
|
50
|
+
hasMore: boolean;
|
|
51
|
+
};
|
|
52
|
+
/** Open tasks of its projects. */
|
|
53
|
+
tasks: Section<TaskLine>;
|
|
54
|
+
coverage: Coverage;
|
|
55
|
+
}
|
|
56
|
+
export interface OrganizationContextOptions {
|
|
57
|
+
/** The accounts read: a CRM's relationships and deals, and its people's messages, mail and events. */
|
|
58
|
+
accounts?: AccountKey[];
|
|
59
|
+
/** Items per section. */
|
|
60
|
+
limit?: number;
|
|
61
|
+
/** The kinds the profile denies: none of their rows, titles or counts is answered. */
|
|
62
|
+
deniedKinds?: readonly DealKind[];
|
|
63
|
+
}
|
|
64
|
+
export declare const dealLine: (deal: Deal) => DealLine;
|
|
65
|
+
/** Open tasks of these projects, the soonest due first, and the command that lists them when it is one project's. */
|
|
66
|
+
export declare const openTasksOf: (store: MessageStore, projects: {
|
|
67
|
+
ref: string;
|
|
68
|
+
key: string;
|
|
69
|
+
}[]) => Promise<{
|
|
70
|
+
tasks: TaskLine[];
|
|
71
|
+
full: boolean;
|
|
72
|
+
next: string | null;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* One answer about an organization from the store: its people, its customers, suppliers and partners, projects,
|
|
76
|
+
* open deals, money and how much the owner talks with its people. Every relationship says its standing; a source
|
|
77
|
+
* nobody confirmed is `reported`, one that stopped is `ended`, one waiting for the owner `proposed`. A CRM's rows
|
|
78
|
+
* outside `accounts` are left out, and `coverage` names that CRM.
|
|
79
|
+
*/
|
|
80
|
+
export declare const organizationContext: (store: MessageStore, reference: string, { accounts, limit, deniedKinds }?: OrganizationContextOptions) => Promise<OrganizationContext>;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { parseLucene } from "@wirecat/cli-base/query";
|
|
3
|
+
import { DEAL_KINDS } from "../domain/deals.js";
|
|
4
|
+
import { isUsable } from "../domain/relations.js";
|
|
5
|
+
import { isClosedState } from "../tasks/index.js";
|
|
6
|
+
import { coverageFor, edgesOf, grouped, namesOf, notRejected, organizationCategories, SECTION_LIMIT, STORE_PAGE, scopeOf, sectionOf, } from "./business-context.js";
|
|
7
|
+
import { communicationsOf } from "./communications.js";
|
|
8
|
+
import { moneyOf } from "./money-context.js";
|
|
9
|
+
export const dealLine = (deal) => ({
|
|
10
|
+
ref: deal.ref,
|
|
11
|
+
kind: deal.kind,
|
|
12
|
+
title: deal.title,
|
|
13
|
+
summary: deal.summary,
|
|
14
|
+
stage: deal.stage,
|
|
15
|
+
status: deal.status,
|
|
16
|
+
currency: deal.currency,
|
|
17
|
+
amount: deal.amount,
|
|
18
|
+
closeDate: deal.closeDate,
|
|
19
|
+
project: deal.project,
|
|
20
|
+
});
|
|
21
|
+
/** Open tasks of these projects, the soonest due first, and the command that lists them when it is one project's. */
|
|
22
|
+
export const openTasksOf = async (store, projects) => {
|
|
23
|
+
const tasks = [];
|
|
24
|
+
let full = false;
|
|
25
|
+
for (const project of projects) {
|
|
26
|
+
const found = await store.tasks.find({ project: project.ref, limit: STORE_PAGE });
|
|
27
|
+
full ||= found.length >= STORE_PAGE;
|
|
28
|
+
for (const task of found)
|
|
29
|
+
if (!isClosedState(task.status))
|
|
30
|
+
tasks.push({
|
|
31
|
+
ref: task.ref,
|
|
32
|
+
key: task.key,
|
|
33
|
+
title: task.title,
|
|
34
|
+
status: task.status,
|
|
35
|
+
project: task.project,
|
|
36
|
+
dueAt: task.dueAt,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const [only] = projects;
|
|
40
|
+
return {
|
|
41
|
+
tasks: tasks.sort((a, b) => (a.dueAt ?? "~").localeCompare(b.dueAt ?? "~") || a.ref.localeCompare(b.ref)),
|
|
42
|
+
full,
|
|
43
|
+
next: projects.length === 1 && only ? `tasks list project:${only.key}` : null,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/** The one list command that holds every row, when the rows are all of one kind. */
|
|
47
|
+
const oneKind = (rows, commands) => commands.find(([fits]) => rows.every(fits))?.[1] ?? null;
|
|
48
|
+
/**
|
|
49
|
+
* One answer about an organization from the store: its people, its customers, suppliers and partners, projects,
|
|
50
|
+
* open deals, money and how much the owner talks with its people. Every relationship says its standing; a source
|
|
51
|
+
* nobody confirmed is `reported`, one that stopped is `ended`, one waiting for the owner `proposed`. A CRM's rows
|
|
52
|
+
* outside `accounts` are left out, and `coverage` names that CRM.
|
|
53
|
+
*/
|
|
54
|
+
export const organizationContext = async (store, reference, { accounts, limit = SECTION_LIMIT, deniedKinds = [] } = {}) => {
|
|
55
|
+
const id = /^(?:organization:)?(\d+)$/.exec(reference.trim())?.[1];
|
|
56
|
+
if (id === undefined)
|
|
57
|
+
throw new CliError("validation_error", "an organization is organization:<id>");
|
|
58
|
+
const ref = `organization:${id}`;
|
|
59
|
+
const [organization] = await store.knowledge.organizations(parseLucene(`id:${id}`));
|
|
60
|
+
if (!organization)
|
|
61
|
+
throw new CliError("not_found", `no ${ref} in the store`);
|
|
62
|
+
const scope = scopeOf(accounts, deniedKinds);
|
|
63
|
+
const own = (await store.knowledge.ownOrganizations()).some((one) => one.ref === ref);
|
|
64
|
+
const { edges, full } = await edgesOf(store, ref, scope);
|
|
65
|
+
const names = await namesOf(store, edges.flatMap(({ from, to }) => [from, to]));
|
|
66
|
+
const into = (kind, from) => (edge) => edge.role.kind === kind && edge.to === ref && edge.from.startsWith(from) ? edge.from : undefined;
|
|
67
|
+
const people = grouped(edges, into("member-of", "person:"), names);
|
|
68
|
+
const customers = grouped(edges, into("customer-of", ""), names);
|
|
69
|
+
const suppliers = grouped(edges, (edge) => (edge.role.kind === "customer-of" && edge.from === ref ? edge.to : undefined), names);
|
|
70
|
+
const partners = grouped(edges, (edge) => (edge.role.kind !== "partner-of" ? undefined : edge.from === ref ? edge.to : edge.from), names);
|
|
71
|
+
const deals = [];
|
|
72
|
+
let dealsFull = false;
|
|
73
|
+
for (const kind of DEAL_KINDS.filter((one) => !scope.denied.has(one))) {
|
|
74
|
+
const found = await store.deals.list({
|
|
75
|
+
kind,
|
|
76
|
+
organization: ref,
|
|
77
|
+
status: "open",
|
|
78
|
+
limit: STORE_PAGE,
|
|
79
|
+
...(accounts === undefined ? {} : { accounts }),
|
|
80
|
+
});
|
|
81
|
+
dealsFull ||= found.length >= STORE_PAGE;
|
|
82
|
+
for (const deal of found)
|
|
83
|
+
deals.push(deal);
|
|
84
|
+
}
|
|
85
|
+
const catalog = new Map((await store.knowledge.projects()).map((project) => [project.ref, project]));
|
|
86
|
+
const projects = new Map();
|
|
87
|
+
for (const project of catalog.values())
|
|
88
|
+
if (project.organizationId === organization.id)
|
|
89
|
+
projects.set(project.ref, { ref: project.ref, name: project.name, via: ["organization"] });
|
|
90
|
+
for (const deal of deals)
|
|
91
|
+
if (deal.project !== null) {
|
|
92
|
+
const held = projects.get(deal.project) ?? { ref: deal.project, name: deal.projectName ?? deal.project, via: [] };
|
|
93
|
+
if (!held.via.includes("deal"))
|
|
94
|
+
held.via.push("deal");
|
|
95
|
+
projects.set(deal.project, held);
|
|
96
|
+
}
|
|
97
|
+
const invoiceRows = await store.finance.invoices({ counterparty: ref, limit: STORE_PAGE });
|
|
98
|
+
const paymentRows = await store.finance.payments({ party: ref, limit: STORE_PAGE });
|
|
99
|
+
const invoices = notRejected(invoiceRows).map((invoice) => [invoice, "counterparty"]);
|
|
100
|
+
const payments = notRejected(paymentRows).map((payment) => [payment, "counterparty"]);
|
|
101
|
+
const orders = [];
|
|
102
|
+
let ordersFull = false;
|
|
103
|
+
for (const side of ["customer", "supplier"]) {
|
|
104
|
+
const found = await store.orders.list({ [side]: ref, limit: STORE_PAGE });
|
|
105
|
+
ordersFull ||= found.length >= STORE_PAGE;
|
|
106
|
+
for (const order of notRejected(found))
|
|
107
|
+
orders.push({
|
|
108
|
+
ref: order.ref,
|
|
109
|
+
title: order.title,
|
|
110
|
+
status: order.status,
|
|
111
|
+
review: order.review,
|
|
112
|
+
currency: order.currency,
|
|
113
|
+
total: order.total,
|
|
114
|
+
side,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const financeComplete = invoiceRows.length < STORE_PAGE && paymentRows.length < STORE_PAGE && !ordersFull;
|
|
118
|
+
const members = [];
|
|
119
|
+
for (const person of people)
|
|
120
|
+
if (person.roles.some((role) => isUsable(role.standing))) {
|
|
121
|
+
const record = await store.people.personByUid(person.ref.slice("person:".length));
|
|
122
|
+
if (record)
|
|
123
|
+
members.push(record);
|
|
124
|
+
}
|
|
125
|
+
const talks = await communicationsOf(store, members, scope);
|
|
126
|
+
const categories = (await organizationCategories(store, scope)).get(ref) ?? [];
|
|
127
|
+
const tasks = await openTasksOf(store, [...projects.keys()].flatMap((one) => {
|
|
128
|
+
const project = catalog.get(one);
|
|
129
|
+
return project ? [{ ref: one, key: project.key }] : [];
|
|
130
|
+
}));
|
|
131
|
+
const about = `about:${ref}`;
|
|
132
|
+
const more = { more: full };
|
|
133
|
+
return {
|
|
134
|
+
organization: { ...organization, own },
|
|
135
|
+
categories,
|
|
136
|
+
people: sectionOf(people, limit, `relationships list ${about} kind:member-of`, more),
|
|
137
|
+
customers: sectionOf(customers, limit, `relationships list to:${ref} kind:customer-of`, more),
|
|
138
|
+
suppliers: sectionOf(suppliers, limit, `relationships list from:${ref} kind:customer-of`, more),
|
|
139
|
+
partners: sectionOf(partners, limit, `relationships list ${about} kind:partner-of`, more),
|
|
140
|
+
projects: sectionOf([...projects.values()], limit, null),
|
|
141
|
+
deals: sectionOf(deals.map(dealLine), limit, oneKind(deals, [
|
|
142
|
+
[(deal) => deal.kind === "deal", `deals list organization:"${ref}" status:open`],
|
|
143
|
+
[(deal) => deal.kind === "lead", `leads list organization:"${ref}" status:open`],
|
|
144
|
+
]), { more: dealsFull }),
|
|
145
|
+
money: moneyOf({ invoices, payments, orders }, limit, {
|
|
146
|
+
invoices: `invoices list counterparty:"${ref}"`,
|
|
147
|
+
payments: `payments list party:"${ref}"`,
|
|
148
|
+
orders: oneKind(orders, [[(order) => order.side === "customer", `orders list customer:"${ref}"`]]),
|
|
149
|
+
}, financeComplete),
|
|
150
|
+
communications: { ...talks.counts, hasMore: talks.hasMore },
|
|
151
|
+
tasks: sectionOf(tasks.tasks, limit, tasks.next, { more: tasks.full }),
|
|
152
|
+
coverage: await coverageFor(store, talks.coverage, scope),
|
|
153
|
+
};
|
|
154
|
+
};
|
|
@@ -73,8 +73,11 @@ export interface PeopleService {
|
|
|
73
73
|
fetch?: boolean;
|
|
74
74
|
}): Promise<PersonMessages>;
|
|
75
75
|
/** `other` may name another messenger of the store: `max:Ana`. */
|
|
76
|
-
link(person: string, other: string,
|
|
77
|
-
|
|
76
|
+
link(person: string, other: string, options: LinkPeopleOptions): Promise<LinkedPeople>;
|
|
77
|
+
/** The owner's alone: an unlink separates two people, and nothing links them again on its own. */
|
|
78
|
+
unlink(person: string, options: {
|
|
79
|
+
by: string;
|
|
80
|
+
}): Promise<PersonRecord>;
|
|
78
81
|
/**
|
|
79
82
|
* Only counts and the people recognised: never a number. `IMPORT_CHUNK` numbers a request, each request
|
|
80
83
|
* through the guard, each number counted toward the hourly limit.
|
|
@@ -85,12 +88,25 @@ export interface PeopleService {
|
|
|
85
88
|
}>>;
|
|
86
89
|
}
|
|
87
90
|
export declare const peopleService: (deps: ServiceDeps) => PeopleService;
|
|
88
|
-
|
|
91
|
+
export interface LinkPeopleOptions {
|
|
92
|
+
/** `"owner"`, or the agent the run is attributed to (`actorOf`): only the owner's link is made at once. */
|
|
93
|
+
by: string;
|
|
94
|
+
/** Why an agent thinks they are one person; it is required of an agent and kept on the proposal. */
|
|
95
|
+
reason?: string | undefined;
|
|
96
|
+
method?: string;
|
|
97
|
+
}
|
|
98
|
+
/** The person as linked, or, for an agent, as still unlinked with the `identity_link` proposal waiting for the owner. */
|
|
99
|
+
export type LinkedPeople = PersonRecord & {
|
|
100
|
+
proposal?: string;
|
|
101
|
+
};
|
|
89
102
|
/**
|
|
90
|
-
*
|
|
91
|
-
* `
|
|
103
|
+
* Links two people, each read in the account's messenger unless prefixed (`max:Ana`, `email:ana@x.com`, `person:12`);
|
|
104
|
+
* `me` links the first to the owner. The owner's link is made at once; an agent's opens an `identity_link` proposal
|
|
105
|
+
* only the owner confirms, and an agent never says who the owner is. A tool without a messenger (a calendar CLI) calls
|
|
106
|
+
* it directly.
|
|
92
107
|
*/
|
|
93
|
-
export declare const linkPeople: (store: MessageStore, account: AccountKey, person: string, other: string, method
|
|
108
|
+
export declare const linkPeople: (store: MessageStore, account: AccountKey, person: string, other: string, { by, reason, method }: LinkPeopleOptions) => Promise<LinkedPeople>;
|
|
109
|
+
/** Digits, with the `+` and the spaces, dashes and brackets people type dropped. */
|
|
94
110
|
export declare const phoneOf: (typed: string) => string;
|
|
95
111
|
/** `--hide-phone`: the answer without the number, for output that will be pasted somewhere. */
|
|
96
112
|
export declare const withoutPhone: <T extends {
|
package/dist/services/people.js
CHANGED
|
@@ -109,8 +109,12 @@ export const peopleService = (deps) => {
|
|
|
109
109
|
...(detail ? { detail } : {}),
|
|
110
110
|
});
|
|
111
111
|
},
|
|
112
|
-
link: async (person, other,
|
|
113
|
-
unlink: async (person) => {
|
|
112
|
+
link: async (person, other, options) => linkPeople(await deps.store(), await deps.account(), person, other, options),
|
|
113
|
+
unlink: async (person, { by }) => {
|
|
114
|
+
if (by !== "owner")
|
|
115
|
+
throw new CliError("permission_error", "only the owner unlinks an identity; the owner runs this", {
|
|
116
|
+
reason: "identity.owner-only",
|
|
117
|
+
});
|
|
114
118
|
const store = await deps.store();
|
|
115
119
|
return store.people.unlinkIdentity(await identityIn(store, await deps.account(), person), {
|
|
116
120
|
method: "manual",
|
|
@@ -188,20 +192,46 @@ export const peopleService = (deps) => {
|
|
|
188
192
|
};
|
|
189
193
|
/** Numbers per request: one big list in one request is what a contact-scraping account sends. */
|
|
190
194
|
const IMPORT_CHUNK = 10;
|
|
191
|
-
/** Digits, with the `+` and the spaces, dashes and brackets people type dropped. */
|
|
192
195
|
/**
|
|
193
|
-
*
|
|
194
|
-
* `
|
|
196
|
+
* Links two people, each read in the account's messenger unless prefixed (`max:Ana`, `email:ana@x.com`, `person:12`);
|
|
197
|
+
* `me` links the first to the owner. The owner's link is made at once; an agent's opens an `identity_link` proposal
|
|
198
|
+
* only the owner confirms, and an agent never says who the owner is. A tool without a messenger (a calendar CLI) calls
|
|
199
|
+
* it directly.
|
|
195
200
|
*/
|
|
196
|
-
export const linkPeople = async (store, account, person, other, method = "manual") => {
|
|
201
|
+
export const linkPeople = async (store, account, person, other, { by, reason, method = "manual" }) => {
|
|
197
202
|
const one = await identityIn(store, account, person);
|
|
198
|
-
|
|
203
|
+
const owner = by === "owner";
|
|
204
|
+
if (other === "me") {
|
|
205
|
+
if (!owner)
|
|
206
|
+
throw new CliError("permission_error", "only the owner links an identity to themselves; the owner runs this", {
|
|
207
|
+
reason: "identity.owner-only",
|
|
208
|
+
});
|
|
199
209
|
return store.people.linkToOwner(one);
|
|
210
|
+
}
|
|
200
211
|
const two = await identityIn(store, account, other);
|
|
201
212
|
if (one.provider === two.provider && one.id === two.id)
|
|
202
213
|
throw new CliError("validation_error", "that is the same person twice");
|
|
203
|
-
|
|
214
|
+
if (owner)
|
|
215
|
+
return store.people.linkIdentities(one, two, { method, by });
|
|
216
|
+
const why = reason?.trim();
|
|
217
|
+
if (!why)
|
|
218
|
+
throw new CliError("validation_error", "an agent proposing a link gives a reason: --reason <why they are one>", {
|
|
219
|
+
reason: "proposal.reason-required",
|
|
220
|
+
});
|
|
221
|
+
const record = (await store.people.personOf(one));
|
|
222
|
+
if (record.identities.some(({ provider, id }) => provider === two.provider && id === two.id))
|
|
223
|
+
return record;
|
|
224
|
+
// `linkIdentities` merges `other`'s person into `person`'s; a confirmed proposal merges `identity`'s into `with`'s.
|
|
225
|
+
const proposed = await store.people.proposeLink({
|
|
226
|
+
identity: two,
|
|
227
|
+
with: one,
|
|
228
|
+
signal: "agent",
|
|
229
|
+
by: { bot: by },
|
|
230
|
+
reason: why,
|
|
231
|
+
});
|
|
232
|
+
return { ...record, proposal: proposed.id };
|
|
204
233
|
};
|
|
234
|
+
/** Digits, with the `+` and the spaces, dashes and brackets people type dropped. */
|
|
205
235
|
export const phoneOf = (typed) => {
|
|
206
236
|
const digits = typed.replace(/[\s()+-]/g, "");
|
|
207
237
|
if (!/^\d{6,15}$/.test(digits)) {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DealKind } from "../domain/deals.js";
|
|
2
|
+
import type { AccountKey, MessageStore, PersonRecord } from "../store/store.js";
|
|
3
|
+
import { type Category, type Coverage, type Related, type Scope, type Section } from "./business-context.js";
|
|
4
|
+
import { type ContextOptions, type PersonContext } from "./person-context.js";
|
|
5
|
+
/** What a person is in the owner's business, each relationship with its standing. */
|
|
6
|
+
export interface PersonBusiness {
|
|
7
|
+
organizations: Section<Related & {
|
|
8
|
+
categories: Category[];
|
|
9
|
+
}>;
|
|
10
|
+
/** Whom they report to. */
|
|
11
|
+
reportsTo: Section<Related>;
|
|
12
|
+
/** Who reports to them: `reports-to` read backwards, "manages". */
|
|
13
|
+
directReports: Section<Related>;
|
|
14
|
+
/** Deals and leads they are on, with their roles there. */
|
|
15
|
+
deals: Section<Related>;
|
|
16
|
+
projects: Section<Related>;
|
|
17
|
+
/** `client` when they are `customer-of` an own organization, and each category of an organization they belong to. */
|
|
18
|
+
categories: Category[];
|
|
19
|
+
}
|
|
20
|
+
/** `personContext` of any canonical person, with no account to ask from: every account in scope is read. */
|
|
21
|
+
export interface CanonicalPersonContext extends Omit<PersonContext, "person"> {
|
|
22
|
+
person: PersonRecord;
|
|
23
|
+
business: PersonBusiness;
|
|
24
|
+
coverage: Coverage;
|
|
25
|
+
}
|
|
26
|
+
export interface CanonicalContextOptions extends ContextOptions {
|
|
27
|
+
/** The accounts read; left out, every account in the store, and `coverage.accounts` names them. */
|
|
28
|
+
accounts?: AccountKey[];
|
|
29
|
+
/** Items per business section. */
|
|
30
|
+
limit?: number;
|
|
31
|
+
/** The kinds the profile denies: no deal or lead of theirs is named. */
|
|
32
|
+
deniedKinds?: readonly DealKind[];
|
|
33
|
+
}
|
|
34
|
+
export declare const personBusiness: (store: MessageStore, uid: string, { limit, scope }?: {
|
|
35
|
+
limit?: number;
|
|
36
|
+
scope?: Scope;
|
|
37
|
+
}) => Promise<PersonBusiness>;
|
|
38
|
+
/**
|
|
39
|
+
* What the store knows about one canonical person, by `person:<uid>`: a CRM contact, a name the owner added, or
|
|
40
|
+
* someone seen by several accounts alike. Every identity is read in every account in scope that saw it — never one
|
|
41
|
+
* picked for them (WC-ACCT-9) — and `coverage` says what was read and what was not.
|
|
42
|
+
*/
|
|
43
|
+
export declare const canonicalPersonContext: (store: MessageStore, reference: string, { accounts, limit, deniedKinds, ...options }?: CanonicalContextOptions) => Promise<CanonicalPersonContext>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { isUsable } from "../domain/relations.js";
|
|
3
|
+
import { CATEGORIES, coverageFor, edgesOf, grouped, keyOf, namesOf, organizationCategories, SECTION_LIMIT, scopeOf, sectionOf, usable, } from "./business-context.js";
|
|
4
|
+
import { calendarsOf, coverageOf, readsIn } from "./communications.js";
|
|
5
|
+
import { recordContext } from "./person-context.js";
|
|
6
|
+
export const personBusiness = async (store, uid, { limit = SECTION_LIMIT, scope = scopeOf(undefined) } = {}) => {
|
|
7
|
+
const ref = `person:${uid}`;
|
|
8
|
+
const { edges, full } = await edgesOf(store, ref, scope);
|
|
9
|
+
const names = await namesOf(store, edges.flatMap(({ from, to }) => [from, to]));
|
|
10
|
+
const outward = (kind, to) => (edge) => edge.role.kind === kind && edge.from === ref && edge.to.startsWith(to) ? edge.to : undefined;
|
|
11
|
+
const categories = await organizationCategories(store, scope);
|
|
12
|
+
const own = new Set((await store.knowledge.ownOrganizations()).map((one) => one.ref));
|
|
13
|
+
const about = `about:${ref}`;
|
|
14
|
+
const more = { more: full };
|
|
15
|
+
const organizations = grouped(edges, outward("member-of", "organization:"), names).map((one) => ({
|
|
16
|
+
...one,
|
|
17
|
+
categories: categories.get(one.ref) ?? [],
|
|
18
|
+
}));
|
|
19
|
+
const held = new Set();
|
|
20
|
+
if (edges.some((edge) => usable(edge) && edge.role.kind === "customer-of" && edge.from === ref && own.has(edge.to)))
|
|
21
|
+
held.add("client");
|
|
22
|
+
for (const one of organizations)
|
|
23
|
+
if (one.roles.some((role) => isUsable(role.standing)))
|
|
24
|
+
for (const category of one.categories)
|
|
25
|
+
held.add(category);
|
|
26
|
+
return {
|
|
27
|
+
organizations: sectionOf(organizations, limit, `relationships list ${about} kind:member-of`, more),
|
|
28
|
+
reportsTo: sectionOf(grouped(edges, outward("reports-to", "person:"), names), limit, `relationships list from:${ref} kind:reports-to`, more),
|
|
29
|
+
directReports: sectionOf(grouped(edges, (edge) => (edge.role.kind === "reports-to" && edge.to === ref ? edge.from : undefined), names), limit, `relationships list to:${ref} kind:reports-to`, more),
|
|
30
|
+
deals: sectionOf(grouped(edges, (edge) => edge.role.kind === "related-to" && edge.to === ref && /^(deal|lead):/.test(edge.from) ? edge.from : undefined, names), limit, `relationships list ${about} kind:related-to`, more),
|
|
31
|
+
projects: sectionOf(grouped(edges, outward("member-of", "project:"), names), limit, `relationships list ${about} kind:member-of`, more),
|
|
32
|
+
categories: CATEGORIES.filter((one) => held.has(one)),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* What the store knows about one canonical person, by `person:<uid>`: a CRM contact, a name the owner added, or
|
|
37
|
+
* someone seen by several accounts alike. Every identity is read in every account in scope that saw it — never one
|
|
38
|
+
* picked for them (WC-ACCT-9) — and `coverage` says what was read and what was not.
|
|
39
|
+
*/
|
|
40
|
+
export const canonicalPersonContext = async (store, reference, { accounts, limit, deniedKinds, ...options } = {}) => {
|
|
41
|
+
const uid = /^person:(\d+)$/.exec(reference.trim())?.[1];
|
|
42
|
+
if (uid === undefined)
|
|
43
|
+
throw new CliError("validation_error", "a person is person:<id>");
|
|
44
|
+
const record = await store.people.personByUid(uid);
|
|
45
|
+
if (!record)
|
|
46
|
+
throw new CliError("not_found", `no person:${uid} in the store`);
|
|
47
|
+
const scope = scopeOf(accounts, deniedKinds);
|
|
48
|
+
const stored = await store.accounts.storedAccounts();
|
|
49
|
+
// The scope is the caller's: every stored account it holds is readable, whatever its personal or work scope.
|
|
50
|
+
const readable = stored.filter((one) => scope.has(one));
|
|
51
|
+
const scopes = new Map(stored.map((one) => [keyOf(one), one.scope]));
|
|
52
|
+
const reads = readsIn(stored, record, scope).map((one) => ({
|
|
53
|
+
...one,
|
|
54
|
+
scope: scopes.get(keyOf(one.key)),
|
|
55
|
+
}));
|
|
56
|
+
const calendars = (await calendarsOf(store, stored)).filter((one) => scope.has(one));
|
|
57
|
+
const read = [...reads.map(({ key }) => keyOf(key)), ...calendars.map(keyOf)];
|
|
58
|
+
return {
|
|
59
|
+
person: record,
|
|
60
|
+
...(await recordContext(store, record, reads, readable, {
|
|
61
|
+
...options,
|
|
62
|
+
...(accounts === undefined ? {} : { calendarIds: calendars.map(({ id }) => id) }),
|
|
63
|
+
})),
|
|
64
|
+
business: await personBusiness(store, uid, { ...(limit === undefined ? {} : { limit }), scope }),
|
|
65
|
+
coverage: await coverageFor(store, coverageOf([record], read, calendars.length > 0), scope),
|
|
66
|
+
};
|
|
67
|
+
};
|