@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.
Files changed (306) hide show
  1. package/CHANGELOG.md +406 -1
  2. package/README.md +1 -1
  3. package/THIRD_PARTY_NOTICES +3 -0
  4. package/dist/background/backfill.d.ts +21 -0
  5. package/dist/background/backfill.js +41 -0
  6. package/dist/background/daily-archive-timer.d.ts +1 -1
  7. package/dist/background/deliver.d.ts +5 -2
  8. package/dist/background/deliver.js +30 -12
  9. package/dist/background/report/messages.js +4 -1
  10. package/dist/background/report/render.d.ts +3 -1
  11. package/dist/background/report/render.js +3 -1
  12. package/dist/background/report/view.js +4 -1
  13. package/dist/background/schedules.d.ts +17 -0
  14. package/dist/background/schedules.js +21 -8
  15. package/dist/calendar/cli/answers.d.ts +1 -0
  16. package/dist/calendar/cli/answers.js +6 -1
  17. package/dist/calendar/cli/index.js +2 -14
  18. package/dist/calendar/cli/people-link.d.ts +3 -0
  19. package/dist/calendar/cli/people-link.js +23 -0
  20. package/dist/calendar/index.d.ts +1 -1
  21. package/dist/calendar/index.js +1 -1
  22. package/dist/calendar/mcp/index.d.ts +12 -12
  23. package/dist/cli/base-stages-command.d.ts +22 -0
  24. package/dist/cli/base-stages-command.js +174 -0
  25. package/dist/cli/command-runs.d.ts +2 -0
  26. package/dist/cli/command-runs.js +3 -1
  27. package/dist/cli/config-command.js +6 -1
  28. package/dist/cli/context.d.ts +5 -0
  29. package/dist/cli/context.js +1 -0
  30. package/dist/cli/group-lock.d.ts +42 -0
  31. package/dist/cli/group-lock.js +130 -0
  32. package/dist/cli/index.d.ts +10 -4
  33. package/dist/cli/index.js +10 -4
  34. package/dist/cli/messenger/backfill-command.js +27 -15
  35. package/dist/cli/messenger/contacts-command.js +10 -3
  36. package/dist/cli/messenger/context.js +1 -0
  37. package/dist/cli/messenger/doctor-command.js +3 -1
  38. package/dist/cli/messenger/models-command.js +1 -1
  39. package/dist/cli/messenger/serve-command.js +24 -2
  40. package/dist/cli/messenger/serve-outdated.d.ts +22 -0
  41. package/dist/cli/messenger/serve-outdated.js +100 -0
  42. package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
  43. package/dist/cli/messenger/store-maintenance-command.js +9 -4
  44. package/dist/cli/model-catalogue-command.d.ts +3 -1
  45. package/dist/cli/model-catalogue-command.js +46 -2
  46. package/dist/cli/profile.d.ts +8 -0
  47. package/dist/cli/profile.js +25 -0
  48. package/dist/cli/program.js +2 -0
  49. package/dist/cli/runs/search-query.d.ts +1 -1
  50. package/dist/cli/runs/search-query.js +4 -0
  51. package/dist/cli/runs/search.d.ts +5 -1
  52. package/dist/cli/runs/search.js +12 -1
  53. package/dist/cli/schedule/schedule-command.d.ts +9 -0
  54. package/dist/cli/schedule/schedule-command.js +13 -0
  55. package/dist/cli/search-left-out.d.ts +6 -1
  56. package/dist/cli/search-left-out.js +9 -4
  57. package/dist/cli/settings.d.ts +8 -0
  58. package/dist/cli/settings.js +17 -2
  59. package/dist/cli/shared-command-exports.d.ts +16 -0
  60. package/dist/cli/store-recover.d.ts +3 -3
  61. package/dist/cli/store-recover.js +47 -23
  62. package/dist/cli/store-settings.d.ts +2 -0
  63. package/dist/cli/store-settings.js +37 -9
  64. package/dist/crm/cli/index.js +38 -6
  65. package/dist/crm/group-hint.d.ts +8 -0
  66. package/dist/crm/group-hint.js +13 -0
  67. package/dist/crm/source.d.ts +12 -1
  68. package/dist/crm/sync.d.ts +31 -2
  69. package/dist/crm/sync.js +110 -3
  70. package/dist/crm/testing/acme-scenario.d.ts +40 -0
  71. package/dist/crm/testing/acme-scenario.js +170 -0
  72. package/dist/crm/testing/index.d.ts +1 -0
  73. package/dist/crm/testing/index.js +1 -0
  74. package/dist/crm/testing/source-contract.js +12 -0
  75. package/dist/crm/testing/source.d.ts +2 -0
  76. package/dist/crm/testing/source.js +1 -0
  77. package/dist/domain/addresses.d.ts +13 -0
  78. package/dist/domain/addresses.js +63 -0
  79. package/dist/domain/categories.d.ts +3 -0
  80. package/dist/domain/categories.js +2 -0
  81. package/dist/domain/crm.d.ts +23 -0
  82. package/dist/domain/crm.js +7 -0
  83. package/dist/domain/links.d.ts +17 -4
  84. package/dist/domain/links.js +20 -7
  85. package/dist/domain/mentions.d.ts +2 -12
  86. package/dist/domain/mentions.js +2 -19
  87. package/dist/domain/orders.d.ts +8 -1
  88. package/dist/domain/orders.js +6 -2
  89. package/dist/domain/phones.d.ts +26 -0
  90. package/dist/domain/phones.js +48 -0
  91. package/dist/domain/record-types.d.ts +1 -1
  92. package/dist/domain/record-types.js +1 -0
  93. package/dist/domain/records.d.ts +5 -0
  94. package/dist/domain/ref-summaries.d.ts +39 -0
  95. package/dist/domain/ref-summaries.js +51 -3
  96. package/dist/domain/relations.d.ts +27 -2
  97. package/dist/domain/relations.js +37 -3
  98. package/dist/domain/vcard.d.ts +2 -2
  99. package/dist/domain/vcard.js +4 -10
  100. package/dist/embeddings/models.d.ts +11 -3
  101. package/dist/embeddings/models.js +28 -4
  102. package/dist/entities/business-context.d.ts +149 -0
  103. package/dist/entities/business-context.js +192 -0
  104. package/dist/entities/contact-activity.d.ts +2 -0
  105. package/dist/entities/contact-activity.js +7 -1
  106. package/dist/entities/contact-context.d.ts +157 -0
  107. package/dist/entities/contact-context.js +2 -0
  108. package/dist/entities/deal.d.ts +10 -0
  109. package/dist/entities/deal.js +2 -0
  110. package/dist/entities/index.d.ts +23 -3
  111. package/dist/entities/index.js +5 -0
  112. package/dist/entities/mention.d.ts +4 -3
  113. package/dist/entities/mention.js +2 -1
  114. package/dist/entities/message-transfer.d.ts +4 -2
  115. package/dist/entities/message-transfer.js +2 -2
  116. package/dist/entities/model.d.ts +2 -2
  117. package/dist/entities/model.js +1 -1
  118. package/dist/entities/relationship.d.ts +4 -0
  119. package/dist/entities/relationship.js +4 -0
  120. package/dist/errors/reasons.js +44 -4
  121. package/dist/fetch/page.js +2 -1
  122. package/dist/index.d.ts +4 -3
  123. package/dist/index.js +4 -3
  124. package/dist/meetings/context-store.d.ts +2 -0
  125. package/dist/meetings/findings-rules.d.ts +4 -0
  126. package/dist/meetings/findings-rules.js +5 -2
  127. package/dist/meetings/mcp/index.d.ts +15 -15
  128. package/dist/meetings/result-items.d.ts +1 -0
  129. package/dist/meetings/result-items.js +1 -0
  130. package/dist/models/index.js +22 -5
  131. package/dist/runs/events.d.ts +11 -1
  132. package/dist/runs/events.js +3 -1
  133. package/dist/runs/log-attributes.js +4 -0
  134. package/dist/runs/log-events.js +2 -0
  135. package/dist/runs/recording.d.ts +3 -0
  136. package/dist/runs/recording.js +36 -12
  137. package/dist/runs/run.d.ts +12 -3
  138. package/dist/runs/run.js +32 -13
  139. package/dist/search/account-list.d.ts +6 -0
  140. package/dist/search/account-list.js +9 -3
  141. package/dist/search/groups.d.ts +68 -0
  142. package/dist/search/groups.js +172 -0
  143. package/dist/services/account-groups.d.ts +28 -0
  144. package/dist/services/account-groups.js +89 -0
  145. package/dist/services/archive.d.ts +7 -1
  146. package/dist/services/archive.js +19 -3
  147. package/dist/services/business-context.d.ts +113 -0
  148. package/dist/services/business-context.js +156 -0
  149. package/dist/services/communications.d.ts +50 -0
  150. package/dist/services/communications.js +140 -0
  151. package/dist/services/deal-context.d.ts +90 -0
  152. package/dist/services/deal-context.js +131 -0
  153. package/dist/services/deps.d.ts +2 -0
  154. package/dist/services/enrichment.d.ts +7 -2
  155. package/dist/services/enrichment.js +46 -5
  156. package/dist/services/events-search.js +2 -1
  157. package/dist/services/group-guard.d.ts +28 -0
  158. package/dist/services/group-guard.js +131 -0
  159. package/dist/services/index.d.ts +15 -2
  160. package/dist/services/index.js +9 -2
  161. package/dist/services/ingest.d.ts +12 -0
  162. package/dist/services/ingest.js +4 -0
  163. package/dist/services/link-scorers/chat-senders.js +39 -29
  164. package/dist/services/link-scorers/member-chats.js +4 -3
  165. package/dist/services/link-scorers/member-dialog.js +5 -7
  166. package/dist/services/link-scorers/scan.js +17 -8
  167. package/dist/services/link-scorers/shared.d.ts +6 -2
  168. package/dist/services/link-scorers/shared.js +6 -2
  169. package/dist/services/link-scorers/types.d.ts +2 -0
  170. package/dist/services/meaning-model.d.ts +7 -0
  171. package/dist/services/meaning-model.js +20 -0
  172. package/dist/services/meeting-context.d.ts +6 -1
  173. package/dist/services/meeting-context.js +12 -5
  174. package/dist/services/message-resolution.js +2 -3
  175. package/dist/services/messages-meaning.js +4 -5
  176. package/dist/services/messages-search.d.ts +5 -1
  177. package/dist/services/messages-search.js +1 -1
  178. package/dist/services/model-downloads.d.ts +1 -1
  179. package/dist/services/model-downloads.js +6 -4
  180. package/dist/services/money-context.d.ts +73 -0
  181. package/dist/services/money-context.js +77 -0
  182. package/dist/services/notes-search.d.ts +4 -1
  183. package/dist/services/notes-search.js +19 -7
  184. package/dist/services/organization-context.d.ts +80 -0
  185. package/dist/services/organization-context.js +154 -0
  186. package/dist/services/people.d.ts +22 -6
  187. package/dist/services/people.js +38 -8
  188. package/dist/services/person-business.d.ts +43 -0
  189. package/dist/services/person-business.js +67 -0
  190. package/dist/services/person-context.d.ts +9 -1
  191. package/dist/services/person-context.js +5 -3
  192. package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
  193. package/dist/services/proposals/kinds/identity-link.js +48 -0
  194. package/dist/services/proposals/kinds/orders.js +1 -1
  195. package/dist/services/proposals/registry.js +2 -0
  196. package/dist/services/search-kind.d.ts +12 -5
  197. package/dist/services/search-kind.js +49 -10
  198. package/dist/services/text-model-choice.js +1 -1
  199. package/dist/store/accounts.d.ts +18 -0
  200. package/dist/store/accounts.js +18 -3
  201. package/dist/store/damage.d.ts +43 -10
  202. package/dist/store/damage.js +119 -36
  203. package/dist/store/decisions.js +3 -1
  204. package/dist/store/evidence-findings.d.ts +14 -0
  205. package/dist/store/evidence-findings.js +2 -0
  206. package/dist/store/exports.d.ts +2 -1
  207. package/dist/store/identity-links.d.ts +92 -0
  208. package/dist/store/identity-links.js +6 -0
  209. package/dist/store/index.d.ts +11 -5
  210. package/dist/store/index.js +5 -2
  211. package/dist/store/journal.d.ts +10 -0
  212. package/dist/store/journal.js +33 -0
  213. package/dist/store/marks.d.ts +19 -1
  214. package/dist/store/marks.js +23 -1
  215. package/dist/store/migrations.d.ts +1 -1
  216. package/dist/store/migrations.js +35 -12
  217. package/dist/store/people.d.ts +22 -0
  218. package/dist/store/people.js +15 -1
  219. package/dist/store/recover.d.ts +34 -0
  220. package/dist/store/recover.js +188 -6
  221. package/dist/store/refill.d.ts +28 -0
  222. package/dist/store/refill.js +120 -0
  223. package/dist/store/sqlite/accounts.js +2 -0
  224. package/dist/store/sqlite/base-stages.d.ts +122 -0
  225. package/dist/store/sqlite/base-stages.js +443 -0
  226. package/dist/store/sqlite/base-suggest.d.ts +13 -0
  227. package/dist/store/sqlite/base-suggest.js +62 -0
  228. package/dist/store/sqlite/business-contracts.js +10 -1
  229. package/dist/store/sqlite/clients.d.ts +7 -3
  230. package/dist/store/sqlite/clients.js +11 -7
  231. package/dist/store/sqlite/crm-settings.d.ts +12 -2
  232. package/dist/store/sqlite/crm-settings.js +56 -13
  233. package/dist/store/sqlite/crm.d.ts +13 -1
  234. package/dist/store/sqlite/crm.js +20 -4
  235. package/dist/store/sqlite/deals.d.ts +33 -0
  236. package/dist/store/sqlite/deals.js +42 -4
  237. package/dist/store/sqlite/decision-requests.js +7 -3
  238. package/dist/store/sqlite/email-index.js +17 -15
  239. package/dist/store/sqlite/evidence-findings.js +29 -5
  240. package/dist/store/sqlite/findings-core.js +4 -2
  241. package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
  242. package/dist/store/sqlite/identity-link-rows.js +146 -0
  243. package/dist/store/sqlite/identity-proposals.d.ts +53 -0
  244. package/dist/store/sqlite/identity-proposals.js +234 -0
  245. package/dist/store/sqlite/knowledge.js +5 -3
  246. package/dist/store/sqlite/link-kinds.d.ts +1 -0
  247. package/dist/store/sqlite/link-kinds.js +1 -0
  248. package/dist/store/sqlite/link-row.d.ts +6 -0
  249. package/dist/store/sqlite/link-row.js +35 -0
  250. package/dist/store/sqlite/link-source.d.ts +10 -0
  251. package/dist/store/sqlite/link-source.js +16 -0
  252. package/dist/store/sqlite/manifest.js +1 -0
  253. package/dist/store/sqlite/meeting-context-links.js +9 -6
  254. package/dist/store/sqlite/memories.js +9 -7
  255. package/dist/store/sqlite/mentions.d.ts +4 -1
  256. package/dist/store/sqlite/mentions.js +14 -9
  257. package/dist/store/sqlite/migrations.generated.js +8 -0
  258. package/dist/store/sqlite/note-index.js +9 -7
  259. package/dist/store/sqlite/notes.d.ts +2 -3
  260. package/dist/store/sqlite/notes.js +6 -29
  261. package/dist/store/sqlite/open-store.js +3 -0
  262. package/dist/store/sqlite/open.js +11 -2
  263. package/dist/store/sqlite/orders.d.ts +2 -0
  264. package/dist/store/sqlite/orders.js +9 -3
  265. package/dist/store/sqlite/organization-categories.d.ts +18 -0
  266. package/dist/store/sqlite/organization-categories.js +34 -0
  267. package/dist/store/sqlite/organizations.d.ts +5 -1
  268. package/dist/store/sqlite/organizations.js +14 -4
  269. package/dist/store/sqlite/person-links.d.ts +19 -4
  270. package/dist/store/sqlite/person-links.js +49 -11
  271. package/dist/store/sqlite/person-match-scan.d.ts +13 -0
  272. package/dist/store/sqlite/person-match-scan.js +251 -0
  273. package/dist/store/sqlite/person-matcher.d.ts +16 -0
  274. package/dist/store/sqlite/person-matcher.js +32 -0
  275. package/dist/store/sqlite/proposed-actions.js +6 -0
  276. package/dist/store/sqlite/record-matching.d.ts +2 -8
  277. package/dist/store/sqlite/record-matching.js +8 -40
  278. package/dist/store/sqlite/record-projectors-phone.js +2 -1
  279. package/dist/store/sqlite/record-relationships.d.ts +52 -0
  280. package/dist/store/sqlite/record-relationships.js +243 -0
  281. package/dist/store/sqlite/record-run.d.ts +5 -0
  282. package/dist/store/sqlite/record-run.js +62 -2
  283. package/dist/store/sqlite/record-writes.d.ts +1 -1
  284. package/dist/store/sqlite/record-writes.js +4 -2
  285. package/dist/store/sqlite/records.js +27 -2
  286. package/dist/store/sqlite/ref-accounts.d.ts +13 -0
  287. package/dist/store/sqlite/ref-accounts.js +117 -0
  288. package/dist/store/sqlite/ref-summaries.js +5 -1
  289. package/dist/store/sqlite/relation-list.d.ts +4 -1
  290. package/dist/store/sqlite/relation-list.js +20 -2
  291. package/dist/store/sqlite/relation-proposals.d.ts +18 -1
  292. package/dist/store/sqlite/relation-proposals.js +53 -11
  293. package/dist/store/sqlite/relation-roles.js +12 -6
  294. package/dist/store/sqlite/reported-relations.d.ts +11 -1
  295. package/dist/store/sqlite/reported-relations.js +59 -18
  296. package/dist/store/sqlite/schema.d.ts +32 -0
  297. package/dist/store/sqlite/schema.js +3 -0
  298. package/dist/store/sqlite/transaction.d.ts +13 -0
  299. package/dist/store/sqlite/transaction.js +33 -15
  300. package/dist/store/store.d.ts +3 -0
  301. package/dist/store/store.js +5 -0
  302. package/dist/store/write-events.d.ts +18 -0
  303. package/dist/store/write-events.js +88 -0
  304. package/package.json +3 -3
  305. package/parity.json +11 -4
  306. package/schemas/records-envelope.v1.schema.json +3 -0
@@ -0,0 +1,170 @@
1
+ import { CRM_OPENS } from "../../domain/providers.js";
2
+ import { syncCrm } from "../index.js";
3
+ import { fakeCrmSource } from "./source.js";
4
+ /** The CRM portal of the owner's own company, Northstar Consulting. */
5
+ export const ACME_PORTAL = { provider: "example-crm", account: "northstar.example" };
6
+ export const ACME_TELEGRAM = { provider: "telegram", account: "100" };
7
+ export const ACME_MAIL = { provider: "email", account: "owner@northstar.example" };
8
+ export const ACME_CALENDAR = { provider: "google-calendar", account: "owner@northstar.example" };
9
+ const DAY = 86_400_000;
10
+ /** The deal opens on this day; every message, mail and meeting falls within its first month. */
11
+ export const ACME_OPENED = Date.UTC(2026, 9, 1);
12
+ export const ACME_NOW = Date.UTC(2026, 10, 1);
13
+ const USERS = [
14
+ { externalId: "u1", name: "Emma", email: "emma@northstar.example", position: "Sales owner", departments: ["10"] },
15
+ ];
16
+ /**
17
+ * Plan §8's scenario, synthetic: Northstar Consulting is the owner's own company and its CRM portal, Acme Ltd its
18
+ * customer. Alice and Bob are Acme's CRM contacts; Emma is a portal user, Northstar's sales owner and the deal's owner.
19
+ * The deal "Acme renewal" has Alice as contact (CRM), Bob as decision-maker (the owner's) and project Atlas (the
20
+ * owner's, on the CRM copy). The owner has a Telegram dialog with Alice, a mail with Bob, a Zoom meeting with all three, an invoice to
21
+ * Acme with no payment and an open follow-up task. Everything is written through the store's and the CRM sync's own
22
+ * calls; the store is the caller's, and no real account is touched.
23
+ */
24
+ export const acmeScenario = async (store) => {
25
+ await store.accounts.registerProvider({ name: ACME_PORTAL.provider, kind: "crm", aliases: [], opens: CRM_OPENS });
26
+ const crm = fakeCrmSource({ traits: { removedInChanges: true } });
27
+ crm.put("organization", { externalId: "c1", name: "Acme Ltd" });
28
+ crm.put("organization", { externalId: "my1", name: "Northstar Consulting", own: true });
29
+ crm.put("person", {
30
+ externalId: "7",
31
+ name: "Alice",
32
+ email: "alice@acme.example",
33
+ position: "Procurement manager",
34
+ organizations: [{ externalId: "c1", primary: true }],
35
+ });
36
+ crm.put("person", { externalId: "8", name: "Bob", email: "bob@acme.example", organizations: [{ externalId: "c1" }] });
37
+ crm.put("deal", {
38
+ externalId: "d1",
39
+ title: "Acme renewal",
40
+ pipeline: "deal-1",
41
+ stage: "new",
42
+ organization: "c1",
43
+ people: [{ externalId: "7", role: "contact", primary: true }],
44
+ assignee: "u1",
45
+ createdAt: ACME_OPENED,
46
+ });
47
+ const source = Object.assign(crm, { users: async () => USERS });
48
+ const sync = () => syncCrm(source, store, ACME_PORTAL, { rescan: true, now: ACME_NOW });
49
+ await sync();
50
+ const personOf = async (email) => `person:${(await store.people.personOf({ provider: "email", id: email }))?.uid}`;
51
+ const [alice, bob, emma] = await Promise.all(["alice@acme.example", "bob@acme.example", "emma@northstar.example"].map(personOf));
52
+ const organizations = await store.knowledge.organizations();
53
+ const named = (name) => organizations.find((one) => one.name === name)?.ref;
54
+ const acme = named("Acme Ltd");
55
+ const own = named("Northstar Consulting");
56
+ const deal = (await store.crm.refOf(ACME_PORTAL, "deal", "d1"));
57
+ const owner = `person:${(await store.people.owner())?.uid}`;
58
+ const relate = (from, to, kind, role) => store.knowledge.relate(null, { from, to, kind, ...(role ? { role } : {}), by: "owner" });
59
+ await relate(acme, own, "customer-of");
60
+ await relate(owner, own, "member-of", "founder");
61
+ await relate(deal, bob, "related-to", "decision-maker");
62
+ const project = (await store.knowledge.addProject({ name: "Atlas", organization: acme })).ref;
63
+ await store.deals.setProject(deal, { project, by: "owner" });
64
+ const invoice = (await store.finance.addInvoice({
65
+ by: "owner",
66
+ direction: "receivable",
67
+ counterparty: acme,
68
+ currency: "EUR",
69
+ issueDate: "2026-10-10",
70
+ project,
71
+ lines: [{ description: "Atlas renewal", amount: "1200.00" }],
72
+ })).ref;
73
+ const task = (await store.tasks.create({ project, title: "Follow up on the Acme renewal", type: "chore" }, "owner"))
74
+ .ref;
75
+ await store.accounts.saveAccount(ACME_TELEGRAM, { name: null });
76
+ await store.chats.saveChats(ACME_TELEGRAM, [
77
+ {
78
+ id: "201",
79
+ title: "Alice",
80
+ kind: "dialog",
81
+ unreadCount: 0,
82
+ lastMessageAt: new Date(ACME_OPENED + 3 * DAY).toISOString(),
83
+ participantsCount: null,
84
+ },
85
+ ]);
86
+ await store.people.savePeople(ACME_TELEGRAM, [{ id: "201", name: "Alice" }]);
87
+ await store.messages.saveMessages(ACME_TELEGRAM, "201", [
88
+ {
89
+ id: "1",
90
+ chatId: "201",
91
+ senderId: "201",
92
+ senderName: "Alice",
93
+ timestamp: new Date(ACME_OPENED + 3 * DAY).toISOString(),
94
+ editedAt: null,
95
+ text: "Can we renew Atlas before December?",
96
+ outgoing: false,
97
+ attachments: [],
98
+ replyTo: null,
99
+ forwardedFrom: null,
100
+ reactions: null,
101
+ },
102
+ ], { via: "acme-scenario" });
103
+ await store.roster.saveMembers(ACME_TELEGRAM, "201", ["201", "100"]);
104
+ await store.people.linkIdentities({ provider: "email", id: "alice@acme.example" }, { provider: "telegram", id: "201" }, { method: "manual", by: "owner" });
105
+ const mail = await store.accounts.saveAccount(ACME_MAIL, { name: null });
106
+ await store.mail.saveThread({
107
+ accountId: mail,
108
+ externalId: "renewal-terms",
109
+ now: ACME_NOW,
110
+ emails: [
111
+ {
112
+ externalId: "<terms@acme.example>",
113
+ subject: "Renewal terms",
114
+ from: { address: "bob@acme.example", name: "Bob" },
115
+ to: [{ address: ACME_MAIL.account, name: null }],
116
+ sentAt: ACME_OPENED + 5 * DAY,
117
+ receivedAt: ACME_OPENED + 5 * DAY,
118
+ bodyText: "Here are our terms for the renewal.",
119
+ outgoing: false,
120
+ },
121
+ ],
122
+ });
123
+ await store.people.savePeople(ACME_MAIL, [{ id: "bob@acme.example", name: "Bob" }]);
124
+ const calendarAccount = await store.accounts.saveAccount(ACME_CALENDAR, { name: null });
125
+ const [calendar] = await store.calendar.saveCalendars(calendarAccount, [
126
+ {
127
+ externalId: "primary",
128
+ title: "Work",
129
+ timezone: "UTC",
130
+ accessRole: "owner",
131
+ primary: true,
132
+ syncByDefault: true,
133
+ },
134
+ ], ACME_NOW);
135
+ await store.calendar.saveEntries(calendar?.id, [
136
+ {
137
+ externalId: "renewal-call",
138
+ kind: "single",
139
+ status: "confirmed",
140
+ title: "Acme renewal call (Zoom)",
141
+ location: "https://zoom.example/j/1",
142
+ startsAt: ACME_OPENED + 7 * DAY,
143
+ endsAt: ACME_OPENED + 7 * DAY + 3_600_000,
144
+ participants: [
145
+ { email: "alice@acme.example", name: "Alice" },
146
+ { email: "bob@acme.example", name: "Bob" },
147
+ { email: "emma@northstar.example", name: "Emma" },
148
+ ],
149
+ },
150
+ ], ACME_NOW);
151
+ const [event] = await store.calendar.events({ limit: 1 });
152
+ return {
153
+ store,
154
+ crm,
155
+ sync,
156
+ refs: {
157
+ owner,
158
+ own,
159
+ acme,
160
+ alice: alice,
161
+ bob: bob,
162
+ emma: emma,
163
+ deal,
164
+ project,
165
+ invoice,
166
+ task,
167
+ event: `event:${event?.id}`,
168
+ },
169
+ };
170
+ };
@@ -1,3 +1,4 @@
1
+ export { ACME_CALENDAR, ACME_MAIL, ACME_NOW, ACME_OPENED, ACME_PORTAL, ACME_TELEGRAM, type AcmeScenario, acmeScenario, } from "./acme-scenario.js";
1
2
  export { exampleDeal, exampleLead, exampleOrganization, examplePerson, FAKE_CRM_ALIASES, type FakeCrmSource, type FakeCrmSourceOptions, type FakeCrmWrite, fakeCrmSource, LEAD_PIPELINE, SALES_PIPELINE, SOURCE_FIELD, } from "./source.js";
2
3
  export { type CrmSourceFixture, crmSourceContract } from "./source-contract.js";
3
4
  export { CRM_TRAIT_COMBINATIONS, type CrmSyncFixture, crmSyncContract } from "./sync-contract.js";
@@ -1,3 +1,4 @@
1
+ export { ACME_CALENDAR, ACME_MAIL, ACME_NOW, ACME_OPENED, ACME_PORTAL, ACME_TELEGRAM, acmeScenario, } from "./acme-scenario.js";
1
2
  export { exampleDeal, exampleLead, exampleOrganization, examplePerson, FAKE_CRM_ALIASES, fakeCrmSource, LEAD_PIPELINE, SALES_PIPELINE, SOURCE_FIELD, } from "./source.js";
2
3
  export { crmSourceContract } from "./source-contract.js";
3
4
  export { CRM_TRAIT_COMBINATIONS, crmSyncContract } from "./sync-contract.js";
@@ -28,6 +28,18 @@ const listAll = async (source, kind, cursor) => {
28
28
  /** What every CRM adapter answers, whatever the CRM: run each case in the adapter's tests. */
29
29
  export const crmSourceContract = (make) => {
30
30
  const cases = [
31
+ {
32
+ name: "self: a source that names its own user gives it as a user id, and every deal and lead an assignee",
33
+ async run() {
34
+ const { source } = await make();
35
+ if (!source.self)
36
+ return;
37
+ assert.match(String(await source.self()), /^\d{1,20}$/, "self() answers a numeric user id");
38
+ for (const kind of DEAL_KINDS.filter((one) => source.traits.kinds.includes(one)))
39
+ for (const item of (await listAll(source, kind, null)).items)
40
+ assert.ok(item.assignee, `${kind} ${item.externalId} has an assignee: mine would keep it as nobody's`);
41
+ },
42
+ },
31
43
  {
32
44
  name: "pipelines: unique ids in each kind, stages with a known outcome and unique ids",
33
45
  async run() {
@@ -8,6 +8,8 @@ export interface FakeCrmSourceOptions {
8
8
  pageSize?: number;
9
9
  /** What the write side takes: every verb on both kinds, not idempotent, when absent. */
10
10
  capabilities?: Partial<CrmWriteCapabilities>;
11
+ /** The sign-in's own user, as `self()` answers it; absent, the fake names none and is copied whole. */
12
+ self?: string;
11
13
  }
12
14
  /** One write the fake CRM was sent, in order. */
13
15
  export interface FakeCrmWrite {
@@ -145,6 +145,7 @@ export const fakeCrmSource = (options = {}) => {
145
145
  const fake = {
146
146
  provider: "example-crm",
147
147
  traits,
148
+ ...(options.self === undefined ? {} : { self: () => options.self }),
148
149
  calls,
149
150
  capabilities,
150
151
  writes,
@@ -0,0 +1,13 @@
1
+ /** One reading of an email address everywhere a person is matched by one: a local part, an `@` and a dotted domain. */
2
+ export declare const EMAIL_ADDRESS: RegExp;
3
+ /** Whether a lower-cased, trimmed value is an email address. */
4
+ export declare const isEmailAddress: (value: string) => boolean;
5
+ /** Local parts that name a mailbox, not a person; the no-reply family names no one at all. */
6
+ export declare const ROLE_ADDRESS_WORDS: {
7
+ readonly noReply: readonly ["noreply", "no-reply", "donotreply", "do-not-reply", "mailer-daemon", "bounce", "bounces"];
8
+ readonly shared: readonly ["info", "hello", "hi", "contact", "contacts", "support", "help", "helpdesk", "service", "sales", "office", "admin", "team", "billing", "accounts", "accounting", "invoices", "finance", "hr", "jobs", "careers", "press", "media", "marketing", "partners", "legal", "privacy", "security", "abuse", "postmaster", "webmaster", "mail", "inbox", "enquiries", "inquiries", "booking", "reservations", "orders", "feedback", "newsletter", "notifications", "alerts"];
9
+ };
10
+ /** Whether the whole local part, `+tag` and `.`/`-`/`_` aside, is a role word: `info@`, `Hello+news@`, `no_reply@`. */
11
+ export declare const isRoleAddress: (address: string) => boolean;
12
+ /** Whether the address is a no-reply one, which belongs to no person. */
13
+ export declare const isNoReplyAddress: (address: string) => boolean;
@@ -0,0 +1,63 @@
1
+ /** One reading of an email address everywhere a person is matched by one: a local part, an `@` and a dotted domain. */
2
+ export const EMAIL_ADDRESS = /^[^\s@]+@[^\s@]+\.[^\s@]+$/u;
3
+ /** Whether a lower-cased, trimmed value is an email address. */
4
+ export const isEmailAddress = (value) => EMAIL_ADDRESS.test(value);
5
+ /** Local parts that name a mailbox, not a person; the no-reply family names no one at all. */
6
+ export const ROLE_ADDRESS_WORDS = {
7
+ noReply: ["noreply", "no-reply", "donotreply", "do-not-reply", "mailer-daemon", "bounce", "bounces"],
8
+ shared: [
9
+ "info",
10
+ "hello",
11
+ "hi",
12
+ "contact",
13
+ "contacts",
14
+ "support",
15
+ "help",
16
+ "helpdesk",
17
+ "service",
18
+ "sales",
19
+ "office",
20
+ "admin",
21
+ "team",
22
+ "billing",
23
+ "accounts",
24
+ "accounting",
25
+ "invoices",
26
+ "finance",
27
+ "hr",
28
+ "jobs",
29
+ "careers",
30
+ "press",
31
+ "media",
32
+ "marketing",
33
+ "partners",
34
+ "legal",
35
+ "privacy",
36
+ "security",
37
+ "abuse",
38
+ "postmaster",
39
+ "webmaster",
40
+ "mail",
41
+ "inbox",
42
+ "enquiries",
43
+ "inquiries",
44
+ "booking",
45
+ "reservations",
46
+ "orders",
47
+ "feedback",
48
+ "newsletter",
49
+ "notifications",
50
+ "alerts",
51
+ ],
52
+ };
53
+ const bare = (word) => word.replace(/[.\-_]/gu, "");
54
+ const NO_REPLY = new Set(ROLE_ADDRESS_WORDS.noReply.map(bare));
55
+ const ROLE = new Set([...ROLE_ADDRESS_WORDS.shared, ...ROLE_ADDRESS_WORDS.noReply].map(bare));
56
+ const localWord = (address) => {
57
+ const local = address.trim().toLowerCase().split("@")[0] ?? "";
58
+ return bare(local.split("+")[0] ?? "");
59
+ };
60
+ /** Whether the whole local part, `+tag` and `.`/`-`/`_` aside, is a role word: `info@`, `Hello+news@`, `no_reply@`. */
61
+ export const isRoleAddress = (address) => ROLE.has(localWord(address));
62
+ /** Whether the address is a no-reply one, which belongs to no person. */
63
+ export const isNoReplyAddress = (address) => NO_REPLY.has(localWord(address));
@@ -0,0 +1,3 @@
1
+ /** What an organization is to the owner, derived from relationships at read time and never stored (NEED-2377). */
2
+ export declare const CATEGORIES: readonly ["client", "supplier", "partner", "employer"];
3
+ export type Category = (typeof CATEGORIES)[number];
@@ -0,0 +1,2 @@
1
+ /** What an organization is to the owner, derived from relationships at read time and never stored (NEED-2377). */
2
+ export const CATEGORIES = ["client", "supplier", "partner", "employer"];
@@ -145,4 +145,27 @@ export interface CrmRecords {
145
145
  person: CrmPerson;
146
146
  organization: CrmOrganization;
147
147
  }
148
+ /** The account's scope settings: `also` are the users whose deals the owner counts as nobody's. */
149
+ export type CrmScopeSetting = {
150
+ mode: "all";
151
+ } | {
152
+ mode: "mine";
153
+ also: readonly string[];
154
+ explicit: boolean;
155
+ };
156
+ /**
157
+ * Which deals and leads a sync copies: all, or those with no assignee or assigned to the sign-in's own user (`self`)
158
+ * or to one of `also`. Contacts and companies are always copied whole. Ids are normalised (`normalCrmUserId`).
159
+ */
160
+ export type CrmScope = {
161
+ mode: "all";
162
+ } | {
163
+ mode: "mine";
164
+ self: string;
165
+ also: readonly string[];
166
+ };
167
+ /** A CRM's user id as `unassigned` takes it: digits, as every CRM core syncs numbers its users. */
168
+ export declare const isCrmUserId: (id: string) => boolean;
169
+ /** One spelling per user id, so `015`, ` 15` and `15` are one user wherever they come from. */
170
+ export declare const normalCrmUserId: (id: string) => string;
148
171
  export declare const isItemKind: (kind: CrmKind) => kind is DealKind;
@@ -1,3 +1,10 @@
1
1
  /** What a CRM holds that the store copies: its deals and leads, and the contacts and companies on them. */
2
2
  export const CRM_KINDS = ["organization", "person", "deal", "lead"];
3
+ /** A CRM's user id as `unassigned` takes it: digits, as every CRM core syncs numbers its users. */
4
+ export const isCrmUserId = (id) => /^\d{1,20}$/.test(id);
5
+ /** One spelling per user id, so `015`, ` 15` and `15` are one user wherever they come from. */
6
+ export const normalCrmUserId = (id) => {
7
+ const text = id.trim();
8
+ return /^\d+$/.test(text) ? text.replace(/^0+(?=\d)/, "") : text;
9
+ };
3
10
  export const isItemKind = (kind) => kind === "deal" || kind === "lead";
@@ -35,6 +35,14 @@ export type DeepLinkTarget = {
35
35
  type: "sync";
36
36
  tool: string;
37
37
  account?: string;
38
+ } | {
39
+ type: "person";
40
+ id: string;
41
+ }
42
+ /** A project's store id or its key, as `project:<key>` names it. */
43
+ | {
44
+ type: "project";
45
+ id: string;
38
46
  };
39
47
  export type LinkForm = "web" | "app";
40
48
  export interface LinkConfig {
@@ -52,10 +60,15 @@ export declare const formatLink: (target: DeepLinkTarget) => string;
52
60
  */
53
61
  export declare const parseLink: (text: string) => DeepLinkTarget | null;
54
62
  /**
55
- * The target a ref names, or `null` when it names nothing a link opens. A store reference (`msg:`, `chat:`, `task:`)
56
- * is read by its own grammar; the kinds that are no reference are written as their path with a colon after the
57
- * kind: `proposal:fact%3A512`, `tasks:overdue`, `tasks:new/2026-11-07T08%3A00%3A00Z`, `digest:2026-11-08`,
58
- * `sync:telegram/1001`.
63
+ * The refs in the order an item's one link is chosen from: a person or a project names who or what an item is about,
64
+ * not where it happened, so it opens only when nothing else does.
65
+ */
66
+ export declare const refsForLink: (refs: readonly string[]) => string[];
67
+ /**
68
+ * The target a ref names, or `null` when it names nothing a link opens. A store reference (`msg:`, `chat:`, `task:`,
69
+ * `person:`, `project:`) is read by its own grammar; the kinds that are no reference are written as their path with a
70
+ * colon after the kind: `proposal:fact%3A512`, `tasks:overdue`, `tasks:new/2026-11-07T08%3A00%3A00Z`,
71
+ * `digest:2026-11-08`, `sync:telegram/1001`.
59
72
  */
60
73
  export declare const linkTargetOf: (ref: string) => DeepLinkTarget | null;
61
74
  /** The `links` section of the shared config, with the default for what is missing or not a usable value. */
@@ -15,7 +15,9 @@ const partsOf = (target) => {
15
15
  case "chat":
16
16
  return ["chat", target.provider, target.account, target.chat];
17
17
  case "task":
18
- return ["task", target.id];
18
+ case "person":
19
+ case "project":
20
+ return [target.type, target.id];
19
21
  case "tasks":
20
22
  return target.filter === "new" ? ["tasks", "new", target.since] : ["tasks", target.filter];
21
23
  case "proposal":
@@ -37,7 +39,9 @@ const targetOfParts = (parts) => {
37
39
  case "chat":
38
40
  return rest.length === 3 ? { type: "chat", provider: a, account: b, chat: c } : null;
39
41
  case "task":
40
- return rest.length === 1 ? { type: "task", id: a } : null;
42
+ case "person":
43
+ case "project":
44
+ return rest.length === 1 ? { type: kind, id: a } : null;
41
45
  case "tasks":
42
46
  if (rest.length === 1 && TASK_FILTERS.includes(a))
43
47
  return { type: "tasks", filter: a };
@@ -90,12 +94,21 @@ export const parseLink = (text) => {
90
94
  return targetOfPath(rest);
91
95
  };
92
96
  const LINK_ONLY = new Set(["tasks", "proposal", "digest", "sync"]);
93
- const FROM_REFERENCE = new Set(["message", "chat", "task"]);
97
+ const FROM_REFERENCE = new Set(["message", "chat", "task", "person", "project"]);
98
+ const ENTITY_KINDS = new Set(["person", "project"]);
94
99
  /**
95
- * The target a ref names, or `null` when it names nothing a link opens. A store reference (`msg:`, `chat:`, `task:`)
96
- * is read by its own grammar; the kinds that are no reference are written as their path with a colon after the
97
- * kind: `proposal:fact%3A512`, `tasks:overdue`, `tasks:new/2026-11-07T08%3A00%3A00Z`, `digest:2026-11-08`,
98
- * `sync:telegram/1001`.
100
+ * The refs in the order an item's one link is chosen from: a person or a project names who or what an item is about,
101
+ * not where it happened, so it opens only when nothing else does.
102
+ */
103
+ export const refsForLink = (refs) => [
104
+ ...refs.filter((ref) => !ENTITY_KINDS.has(ref.slice(0, ref.indexOf(":")))),
105
+ ...refs.filter((ref) => ENTITY_KINDS.has(ref.slice(0, ref.indexOf(":")))),
106
+ ];
107
+ /**
108
+ * The target a ref names, or `null` when it names nothing a link opens. A store reference (`msg:`, `chat:`, `task:`,
109
+ * `person:`, `project:`) is read by its own grammar; the kinds that are no reference are written as their path with a
110
+ * colon after the kind: `proposal:fact%3A512`, `tasks:overdue`, `tasks:new/2026-11-07T08%3A00%3A00Z`,
111
+ * `digest:2026-11-08`, `sync:telegram/1001`.
99
112
  */
100
113
  export const linkTargetOf = (ref) => {
101
114
  const text = ref.trim();
@@ -1,3 +1,4 @@
1
+ import { type PhoneOptions } from "./phones.js";
1
2
  export declare const MENTION_KINDS: readonly ["phone", "email", "url", "document", "person-name", "place"];
2
3
  export type MentionKind = (typeof MENTION_KINDS)[number];
3
4
  /** Who decides, and for a name whether every undecided place of it in the same chat follows. */
@@ -29,10 +30,7 @@ export interface FoundMention {
29
30
  }
30
31
  /** The site a host belongs to, from the public suffix list: `docs.google.com` → `google.com`. */
31
32
  export declare const siteOf: (host: string) => string;
32
- export interface PhoneOptions {
33
- /** Where a number written without `+` is read, first match first: the store's `phone_countries`. */
34
- phoneCountries?: readonly string[];
35
- }
33
+ export { type PhoneOptions, typedPhone } from "./phones.js";
36
34
  /** Every phone, e-mail address, link, Google document and address in the text, in the order they are written. */
37
35
  export declare const findMentions: (text: string, { phoneCountries }?: PhoneOptions) => FoundMention[];
38
36
  /**
@@ -47,11 +45,3 @@ export declare const oneTrackedLinkPerSite: <T extends {
47
45
  export declare const trackedOrigin: (value: string) => string | undefined;
48
46
  /** A value as a person types it, in the form mentions store: `8 900 000-00-01` → `+79000000001`. */
49
47
  export declare const normalizedValue: (typed: string, options?: PhoneOptions) => string;
50
- /**
51
- * A number typed into a search, read as if a phone word stood before it, and the national number every stored
52
- * spelling of it ends with — at least 8 digits, so a short tail does not match unrelated numbers.
53
- */
54
- export declare const typedPhone: (typed: string, { phoneCountries }?: PhoneOptions) => {
55
- value: string;
56
- national: string | null;
57
- } | undefined;
@@ -2,7 +2,7 @@
2
2
  import { domainToUnicode } from "node:url";
3
3
  import psl from "psl";
4
4
  import { googleDocumentId, trimUrl } from "./google-documents.js";
5
- import { DEFAULT_PHONE_COUNTRIES, nationalPhone } from "./phones.js";
5
+ import { DEFAULT_PHONE_COUNTRIES, nationalPhone, typedPhone } from "./phones.js";
6
6
  import { findPlaces } from "./places.js";
7
7
  export const MENTION_KINDS = ["phone", "email", "url", "document", "person-name", "place"];
8
8
  /** Bumped when a kind's rule changes, so `mentions rebuild` can redo that kind alone. */
@@ -121,6 +121,7 @@ const DATE = /^\d{1,2}[./-]\d{1,2}[./-]\d{2,4}$|^\d{4}[./-]\d{1,2}[./-]\d{1,2}$/
121
121
  const IP = /^\d{1,3}(?:\.\d{1,3}){3}$/;
122
122
  const RUSSIAN_GROUPS = /\d{3}[ \u00a0\-–]\d{2}[ \u00a0\-–]\d{2}$/;
123
123
  const SPANISH_GROUPS = /^[6-9]\d{2}[ \u00a0\-–]\d{3}[ \u00a0\-–]\d{3}$/;
124
+ export { typedPhone } from "./phones.js";
124
125
  /**
125
126
  * Looks like a phone, not a checked one: a wrong guess costs one `dismissed`, a miss a search that finds nothing.
126
127
  * `+` or `00` makes it international. Without them the number is read in the store's countries: grouped or after
@@ -213,21 +214,3 @@ const exactlyOne = (text, options) => {
213
214
  const [one] = findMentions(text, options);
214
215
  return one && one.start === 0 && one.end === text.length ? one.value : undefined;
215
216
  };
216
- /**
217
- * A number typed into a search, read as if a phone word stood before it, and the national number every stored
218
- * spelling of it ends with — at least 8 digits, so a short tail does not match unrelated numbers.
219
- */
220
- export const typedPhone = (typed, { phoneCountries = DEFAULT_PHONE_COUNTRIES } = {}) => {
221
- const text = typed.trim();
222
- if (!/^\+?[\d\s().\-–]+$/.test(text))
223
- return undefined;
224
- const digits = text.replace(/\(0\)/, "").replace(/\D/g, "").replace(/^00/, "");
225
- if (digits.length < 7 || digits.length > 15)
226
- return undefined;
227
- const national = nationalPhone(digits, phoneCountries, { anyType: true });
228
- const tail = national?.national ?? digits.replace(/^0+/, "");
229
- return {
230
- value: text.startsWith("+") || /^00/.test(text) ? `+${digits}` : (national?.value ?? digits),
231
- national: tail.length >= 8 ? tail : null,
232
- };
233
- };
@@ -1,3 +1,4 @@
1
+ import type { OrderDirection } from "./business.js";
1
2
  import type { FinanceReview } from "./finance.js";
2
3
  export declare const ORDER_STATUSES: readonly ["placed", "shipped", "delivered", "cancelled", "returned"];
3
4
  /** Why it was ordered: for the owner himself, for his business, for a client, or anything else. */
@@ -8,12 +9,18 @@ export interface OrderTitleFields {
8
9
  id: number;
9
10
  number: string | null;
10
11
  supplierName: string;
12
+ direction: OrderDirection;
13
+ /** A sale's title names its customer; the supplier is then the owner. */
14
+ customerName: string | null;
11
15
  total: number | null;
12
16
  currency: string | null;
13
17
  status: OrderStatus;
14
18
  review: FinanceReview;
15
19
  }
16
- /** "12345 · Ozon, 2 499.00 RUB (shipped)": a ref summary's title, which `refLabel` prefixes. */
20
+ /**
21
+ * "12345 · Ozon, 2 499.00 RUB (shipped)", or for a sale "O-55 · to ООО Пример, …": a ref summary's title, which
22
+ * `refLabel` prefixes.
23
+ */
17
24
  export declare const orderLine: (order: OrderTitleFields) => string;
18
25
  /** "Order 12345 · Ozon, 2 499.00 RUB (shipped)". */
19
26
  export declare const orderTitle: (order: OrderTitleFields) => string;
@@ -2,11 +2,15 @@ import { moneyLabel } from "./money.js";
2
2
  export const ORDER_STATUSES = ["placed", "shipped", "delivered", "cancelled", "returned"];
3
3
  /** Why it was ordered: for the owner himself, for his business, for a client, or anything else. */
4
4
  export const ORDER_PURPOSES = ["personal", "business", "client", "other"];
5
- /** "12345 · Ozon, 2 499.00 RUB (shipped)": a ref summary's title, which `refLabel` prefixes. */
5
+ /**
6
+ * "12345 · Ozon, 2 499.00 RUB (shipped)", or for a sale "O-55 · to ООО Пример, …": a ref summary's title, which
7
+ * `refLabel` prefixes.
8
+ */
6
9
  export const orderLine = (order) => {
10
+ const party = order.direction === "sale" && order.customerName !== null ? `to ${order.customerName}` : order.supplierName;
7
11
  const money = order.total === null || order.currency === null ? "" : `, ${moneyLabel(order.total, order.currency)}`;
8
12
  const notes = [order.status, order.review === "confirmed" ? null : order.review].filter(Boolean).join(", ");
9
- return `${order.number ?? `#${order.id}`} · ${order.supplierName}${money} (${notes})`;
13
+ return `${order.number ?? `#${order.id}`} · ${party}${money} (${notes})`;
10
14
  };
11
15
  /** "Order 12345 · Ozon, 2 499.00 RUB (shipped)". */
12
16
  export const orderTitle = (order) => `Order ${orderLine(order)}`;
@@ -20,3 +20,29 @@ export interface NationalPhone {
20
20
  export declare const nationalPhone: (digits: string, countries: readonly string[], { anyType }: {
21
21
  anyType: boolean;
22
22
  }) => NationalPhone | undefined;
23
+ export interface PhoneOptions {
24
+ /** Where a number written without `+` is read, first match first: the store's `phone_countries`. */
25
+ phoneCountries?: readonly string[];
26
+ }
27
+ /**
28
+ * A number typed into a search, read as if a phone word stood before it, and the national number every stored
29
+ * spelling of it ends with — at least 8 digits, so a short tail does not match unrelated numbers.
30
+ */
31
+ export declare const typedPhone: (typed: string, { phoneCountries }?: PhoneOptions) => {
32
+ value: string;
33
+ national: string | null;
34
+ } | undefined;
35
+ /**
36
+ * A phone as a source typed it, as E.164: `null` for a number written without `+` or `00` when no region is given,
37
+ * nothing when it is no phone. Only the source says which country a national number is from: the store's
38
+ * `phone_countries` setting reads `4155550100` as Russian, so it never decides an identifier.
39
+ */
40
+ export declare const e164Of: (phone: string, region: string | undefined) => string | null | undefined;
41
+ /** The messengers that keep a contact's number as bare international digits, the `+` left off. */
42
+ export declare const BARE_INTERNATIONAL_PROVIDERS: readonly string[];
43
+ /**
44
+ * The E.164 number a stored phone column stands for. A number written with `+` or `00` is read however it is spaced;
45
+ * a `phone` identity keeps it whole. Bare digits are international only from a messenger that stores them so; from a
46
+ * CRM or a card they are a national number, whose country nothing names, so they match nothing.
47
+ */
48
+ export declare const storedPhone: (raw: string, provider: string) => string | undefined;
@@ -50,3 +50,51 @@ export const nationalPhone = (digits, countries, { anyType }) => {
50
50
  }
51
51
  return undefined;
52
52
  };
53
+ /**
54
+ * A number typed into a search, read as if a phone word stood before it, and the national number every stored
55
+ * spelling of it ends with — at least 8 digits, so a short tail does not match unrelated numbers.
56
+ */
57
+ export const typedPhone = (typed, { phoneCountries = DEFAULT_PHONE_COUNTRIES } = {}) => {
58
+ const text = typed.trim();
59
+ if (!/^\+?[\d\s().\-–]+$/.test(text))
60
+ return undefined;
61
+ const digits = text.replace(/\(0\)/, "").replace(/\D/g, "").replace(/^00/, "");
62
+ if (digits.length < 7 || digits.length > 15)
63
+ return undefined;
64
+ const national = nationalPhone(digits, phoneCountries, { anyType: true });
65
+ const tail = national?.national ?? digits.replace(/^0+/, "");
66
+ return {
67
+ value: text.startsWith("+") || /^00/.test(text) ? `+${digits}` : (national?.value ?? digits),
68
+ national: tail.length >= 8 ? tail : null,
69
+ };
70
+ };
71
+ /**
72
+ * A phone as a source typed it, as E.164: `null` for a number written without `+` or `00` when no region is given,
73
+ * nothing when it is no phone. Only the source says which country a national number is from: the store's
74
+ * `phone_countries` setting reads `4155550100` as Russian, so it never decides an identifier.
75
+ */
76
+ export const e164Of = (phone, region) => {
77
+ if (/^\s*(\+|00)/.test(phone))
78
+ return typedPhone(phone)?.value;
79
+ if (typedPhone(phone) === undefined)
80
+ return undefined;
81
+ if (region === undefined)
82
+ return null;
83
+ const digits = phone.replace(/\(0\)/, "").replace(/\D/g, "");
84
+ return nationalPhone(digits, [region], { anyType: true })?.value;
85
+ };
86
+ /** The messengers that keep a contact's number as bare international digits, the `+` left off. */
87
+ export const BARE_INTERNATIONAL_PROVIDERS = ["telegram", "max", "whatsapp"];
88
+ /**
89
+ * The E.164 number a stored phone column stands for. A number written with `+` or `00` is read however it is spaced;
90
+ * a `phone` identity keeps it whole. Bare digits are international only from a messenger that stores them so; from a
91
+ * CRM or a card they are a national number, whose country nothing names, so they match nothing.
92
+ */
93
+ export const storedPhone = (raw, provider) => {
94
+ if (/^\s*(\+|00)/.test(raw))
95
+ return typedPhone(raw)?.value;
96
+ if (!BARE_INTERNATIONAL_PROVIDERS.includes(provider))
97
+ return undefined;
98
+ const found = /^([1-9]\d{6,14})$/.exec(raw.trim());
99
+ return found ? `+${found[1]}` : undefined;
100
+ };
@@ -10,7 +10,7 @@ export interface RecordRoleSpec {
10
10
  amount?: boolean;
11
11
  /** A business relationship between entities, by its kind: the relation may carry `as`, the part the target plays. */
12
12
  relationship?: RelationKind;
13
- /** Kept in `record_relations` only, never read to make the entity, so it never makes a record wait (PR 5). */
13
+ /** Never read to make the entity, so it never makes a record wait. */
14
14
  kept?: boolean;
15
15
  /**
16
16
  * A role v1 already had: `fields.<role>` may name a store ref instead of a relation, and its target type is not