@wirecat/cli-core 0.618.0 → 0.620.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (335) hide show
  1. package/CHANGELOG.md +453 -1
  2. package/README.md +1 -1
  3. package/THIRD_PARTY_NOTICES +3 -0
  4. package/dist/background/deliver.d.ts +5 -2
  5. package/dist/background/deliver.js +30 -12
  6. package/dist/background/report/render.d.ts +3 -1
  7. package/dist/background/report/render.js +3 -1
  8. package/dist/calendar/cli/answers.d.ts +1 -0
  9. package/dist/calendar/cli/answers.js +6 -1
  10. package/dist/calendar/cli/index.js +2 -14
  11. package/dist/calendar/cli/people-link.d.ts +3 -0
  12. package/dist/calendar/cli/people-link.js +23 -0
  13. package/dist/calendar/cli/search-all.d.ts +1 -1
  14. package/dist/calendar/cli/search-all.js +9 -10
  15. package/dist/calendar/index.d.ts +1 -1
  16. package/dist/calendar/index.js +1 -1
  17. package/dist/calendar/mcp/index.d.ts +12 -12
  18. package/dist/calendar/prepare.js +1 -0
  19. package/dist/cli/base-stages-command.d.ts +22 -0
  20. package/dist/cli/base-stages-command.js +174 -0
  21. package/dist/cli/bot/buttons.d.ts +3 -0
  22. package/dist/cli/bot/buttons.js +24 -0
  23. package/dist/cli/bot/messages.js +14 -1
  24. package/dist/cli/bot/port.d.ts +8 -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.js +1 -0
  29. package/dist/cli/contract-templates.js +31 -2
  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 +7 -2
  33. package/dist/cli/index.js +7 -2
  34. package/dist/cli/messenger/backfill-command.js +27 -15
  35. package/dist/cli/messenger/contacts-command.js +12 -4
  36. package/dist/cli/messenger/context.js +1 -0
  37. package/dist/cli/messenger/conversations-command.js +2 -1
  38. package/dist/cli/messenger/doctor-command.js +3 -1
  39. package/dist/cli/messenger/message-items.js +1 -1
  40. package/dist/cli/messenger/models-command.js +1 -1
  41. package/dist/cli/messenger/search-answers.d.ts +5 -5
  42. package/dist/cli/messenger/search-answers.js +9 -20
  43. package/dist/cli/messenger/search-command.d.ts +2 -2
  44. package/dist/cli/messenger/search-command.js +8 -18
  45. package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
  46. package/dist/cli/messenger/store-maintenance-command.js +9 -4
  47. package/dist/cli/model-catalogue-command.d.ts +3 -1
  48. package/dist/cli/model-catalogue-command.js +48 -2
  49. package/dist/cli/profile.d.ts +8 -0
  50. package/dist/cli/profile.js +25 -0
  51. package/dist/cli/program.js +2 -0
  52. package/dist/cli/runs/search-query.d.ts +1 -1
  53. package/dist/cli/runs/search-query.js +4 -0
  54. package/dist/cli/runs/search.d.ts +5 -1
  55. package/dist/cli/runs/search.js +12 -1
  56. package/dist/cli/search-harder-consents-command.d.ts +18 -0
  57. package/dist/cli/search-harder-consents-command.js +83 -0
  58. package/dist/cli/search-left-out.d.ts +6 -1
  59. package/dist/cli/search-left-out.js +9 -4
  60. package/dist/cli/settings.d.ts +8 -0
  61. package/dist/cli/settings.js +17 -2
  62. package/dist/cli/shared-command-exports.d.ts +16 -0
  63. package/dist/cli/steps.js +0 -6
  64. package/dist/cli/store-recover.d.ts +3 -3
  65. package/dist/cli/store-recover.js +26 -26
  66. package/dist/cli/store-settings.d.ts +2 -0
  67. package/dist/cli/store-settings.js +37 -9
  68. package/dist/cli/templates-command.js +4 -4
  69. package/dist/crm/cli/index.js +40 -8
  70. package/dist/crm/cli/search-all.js +7 -6
  71. package/dist/crm/group-hint.d.ts +8 -0
  72. package/dist/crm/group-hint.js +13 -0
  73. package/dist/crm/source.d.ts +12 -1
  74. package/dist/crm/sync.d.ts +31 -2
  75. package/dist/crm/sync.js +110 -3
  76. package/dist/crm/testing/acme-scenario.d.ts +40 -0
  77. package/dist/crm/testing/acme-scenario.js +170 -0
  78. package/dist/crm/testing/index.d.ts +1 -0
  79. package/dist/crm/testing/index.js +1 -0
  80. package/dist/crm/testing/source-contract.js +12 -0
  81. package/dist/crm/testing/source.d.ts +2 -0
  82. package/dist/crm/testing/source.js +1 -0
  83. package/dist/domain/addresses.d.ts +13 -0
  84. package/dist/domain/addresses.js +63 -0
  85. package/dist/domain/categories.d.ts +3 -0
  86. package/dist/domain/categories.js +2 -0
  87. package/dist/domain/crm.d.ts +23 -0
  88. package/dist/domain/crm.js +7 -0
  89. package/dist/domain/mentions.d.ts +2 -12
  90. package/dist/domain/mentions.js +2 -19
  91. package/dist/domain/models.d.ts +2 -0
  92. package/dist/domain/orders.d.ts +8 -1
  93. package/dist/domain/orders.js +6 -2
  94. package/dist/domain/phones.d.ts +26 -0
  95. package/dist/domain/phones.js +48 -0
  96. package/dist/domain/record-types.d.ts +1 -1
  97. package/dist/domain/record-types.js +1 -0
  98. package/dist/domain/records.d.ts +5 -0
  99. package/dist/domain/ref-summaries.d.ts +39 -0
  100. package/dist/domain/ref-summaries.js +51 -3
  101. package/dist/domain/relations.d.ts +12 -2
  102. package/dist/domain/relations.js +14 -3
  103. package/dist/domain/vcard.d.ts +2 -2
  104. package/dist/domain/vcard.js +4 -10
  105. package/dist/embeddings/models.d.ts +11 -3
  106. package/dist/embeddings/models.js +28 -4
  107. package/dist/entities/business-context.d.ts +149 -0
  108. package/dist/entities/business-context.js +192 -0
  109. package/dist/entities/contact-activity.d.ts +2 -0
  110. package/dist/entities/contact-activity.js +7 -1
  111. package/dist/entities/contact-context.d.ts +160 -0
  112. package/dist/entities/contact-context.js +6 -0
  113. package/dist/entities/deal.d.ts +10 -0
  114. package/dist/entities/deal.js +2 -0
  115. package/dist/entities/hit.d.ts +9 -2
  116. package/dist/entities/hit.js +6 -2
  117. package/dist/entities/index.d.ts +32 -4
  118. package/dist/entities/index.js +5 -0
  119. package/dist/entities/mention.d.ts +4 -3
  120. package/dist/entities/mention.js +2 -1
  121. package/dist/entities/message-transfer.d.ts +4 -2
  122. package/dist/entities/message-transfer.js +2 -2
  123. package/dist/entities/model.d.ts +2 -2
  124. package/dist/entities/model.js +1 -1
  125. package/dist/entities/relationship.d.ts +4 -0
  126. package/dist/entities/relationship.js +4 -0
  127. package/dist/errors/reasons.js +39 -4
  128. package/dist/fetch/page.js +2 -1
  129. package/dist/index.d.ts +3 -2
  130. package/dist/index.js +3 -2
  131. package/dist/meetings/context-store.d.ts +2 -0
  132. package/dist/meetings/findings-rules.d.ts +4 -0
  133. package/dist/meetings/findings-rules.js +5 -2
  134. package/dist/meetings/hit-query.d.ts +1 -1
  135. package/dist/meetings/hit-query.js +2 -2
  136. package/dist/meetings/index.d.ts +1 -0
  137. package/dist/meetings/index.js +1 -0
  138. package/dist/meetings/result-items.d.ts +1 -0
  139. package/dist/meetings/result-items.js +1 -0
  140. package/dist/meetings/store.d.ts +4 -0
  141. package/dist/meetings/store.js +2 -1
  142. package/dist/models/anthropic.js +5 -0
  143. package/dist/models/index.js +22 -5
  144. package/dist/runs/events.d.ts +11 -1
  145. package/dist/runs/events.js +3 -1
  146. package/dist/runs/log-attributes.js +4 -0
  147. package/dist/runs/log-events.js +2 -0
  148. package/dist/runs/recording.d.ts +3 -0
  149. package/dist/runs/recording.js +36 -12
  150. package/dist/runs/run.d.ts +12 -3
  151. package/dist/runs/run.js +32 -13
  152. package/dist/search/account-list.d.ts +6 -0
  153. package/dist/search/account-list.js +9 -3
  154. package/dist/search/groups.d.ts +68 -0
  155. package/dist/search/groups.js +172 -0
  156. package/dist/search/question-period.d.ts +10 -0
  157. package/dist/search/question-period.js +133 -0
  158. package/dist/sends/buttons.d.ts +2 -0
  159. package/dist/sends/buttons.js +6 -1
  160. package/dist/services/account-groups.d.ts +28 -0
  161. package/dist/services/account-groups.js +89 -0
  162. package/dist/services/archive.d.ts +7 -1
  163. package/dist/services/archive.js +19 -3
  164. package/dist/services/business-context.d.ts +113 -0
  165. package/dist/services/business-context.js +156 -0
  166. package/dist/services/communications.d.ts +50 -0
  167. package/dist/services/communications.js +140 -0
  168. package/dist/services/deal-context.d.ts +90 -0
  169. package/dist/services/deal-context.js +131 -0
  170. package/dist/services/deps.d.ts +2 -0
  171. package/dist/services/enrichment.d.ts +12 -2
  172. package/dist/services/enrichment.js +47 -6
  173. package/dist/services/events-search.js +2 -1
  174. package/dist/services/group-guard.d.ts +28 -0
  175. package/dist/services/group-guard.js +131 -0
  176. package/dist/services/index.d.ts +19 -4
  177. package/dist/services/index.js +12 -3
  178. package/dist/services/ingest.d.ts +12 -0
  179. package/dist/services/ingest.js +4 -0
  180. package/dist/services/meaning-model.d.ts +7 -0
  181. package/dist/services/meaning-model.js +20 -0
  182. package/dist/services/meeting-context.d.ts +6 -1
  183. package/dist/services/meeting-context.js +12 -5
  184. package/dist/services/message-resolution.js +2 -3
  185. package/dist/services/messages-meaning.js +4 -5
  186. package/dist/services/messages-search.d.ts +5 -1
  187. package/dist/services/messages-search.js +1 -1
  188. package/dist/services/messages.d.ts +1 -4
  189. package/dist/services/messages.js +0 -2
  190. package/dist/services/metadata.d.ts +1 -1
  191. package/dist/services/model-downloads.d.ts +1 -1
  192. package/dist/services/model-downloads.js +6 -4
  193. package/dist/services/money-context.d.ts +73 -0
  194. package/dist/services/money-context.js +77 -0
  195. package/dist/services/notes-search.d.ts +4 -1
  196. package/dist/services/notes-search.js +19 -7
  197. package/dist/services/organization-context.d.ts +80 -0
  198. package/dist/services/organization-context.js +154 -0
  199. package/dist/services/people.d.ts +22 -6
  200. package/dist/services/people.js +38 -8
  201. package/dist/services/person-business.d.ts +43 -0
  202. package/dist/services/person-business.js +67 -0
  203. package/dist/services/person-context.d.ts +11 -1
  204. package/dist/services/person-context.js +25 -10
  205. package/dist/services/person-preparation.d.ts +5 -1
  206. package/dist/services/person-preparation.js +10 -3
  207. package/dist/services/person-timeline.d.ts +6 -1
  208. package/dist/services/person-timeline.js +10 -2
  209. package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
  210. package/dist/services/proposals/kinds/identity-link.js +48 -0
  211. package/dist/services/proposals/kinds/orders.js +1 -1
  212. package/dist/services/proposals/registry.js +2 -0
  213. package/dist/services/search-all-deprecated.d.ts +40 -0
  214. package/dist/services/search-all-deprecated.js +10 -0
  215. package/dist/services/search-all-meetings.d.ts +50 -52
  216. package/dist/services/search-all-meetings.js +265 -272
  217. package/dist/services/search-all.d.ts +34 -6
  218. package/dist/services/search-all.js +118 -24
  219. package/dist/services/search-harder.d.ts +64 -0
  220. package/dist/services/search-harder.js +194 -0
  221. package/dist/services/search-kind.d.ts +12 -5
  222. package/dist/services/search-kind.js +49 -10
  223. package/dist/services/text-model-choice.js +1 -1
  224. package/dist/store/accounts.d.ts +18 -0
  225. package/dist/store/accounts.js +18 -3
  226. package/dist/store/chats.d.ts +3 -1
  227. package/dist/store/chats.js +6 -0
  228. package/dist/store/damage.d.ts +43 -10
  229. package/dist/store/damage.js +119 -36
  230. package/dist/store/decisions.js +3 -1
  231. package/dist/store/evidence-findings.d.ts +14 -0
  232. package/dist/store/evidence-findings.js +2 -0
  233. package/dist/store/exports.d.ts +2 -1
  234. package/dist/store/identity-links.d.ts +92 -0
  235. package/dist/store/identity-links.js +6 -0
  236. package/dist/store/index.d.ts +11 -5
  237. package/dist/store/index.js +5 -2
  238. package/dist/store/journal.d.ts +10 -0
  239. package/dist/store/journal.js +33 -0
  240. package/dist/store/marks.d.ts +19 -1
  241. package/dist/store/marks.js +23 -1
  242. package/dist/store/people.d.ts +28 -2
  243. package/dist/store/people.js +15 -1
  244. package/dist/store/sqlite/accounts.js +2 -0
  245. package/dist/store/sqlite/added-sources.d.ts +15 -3
  246. package/dist/store/sqlite/added-sources.js +26 -5
  247. package/dist/store/sqlite/base-stages.d.ts +122 -0
  248. package/dist/store/sqlite/base-stages.js +443 -0
  249. package/dist/store/sqlite/base-suggest.d.ts +13 -0
  250. package/dist/store/sqlite/base-suggest.js +62 -0
  251. package/dist/store/sqlite/business-contracts.js +10 -1
  252. package/dist/store/sqlite/chats.d.ts +2 -0
  253. package/dist/store/sqlite/chats.js +6 -0
  254. package/dist/store/sqlite/crm-settings.d.ts +12 -2
  255. package/dist/store/sqlite/crm-settings.js +56 -13
  256. package/dist/store/sqlite/crm.d.ts +13 -1
  257. package/dist/store/sqlite/crm.js +20 -4
  258. package/dist/store/sqlite/deals.d.ts +33 -0
  259. package/dist/store/sqlite/deals.js +42 -4
  260. package/dist/store/sqlite/decision-requests.js +7 -3
  261. package/dist/store/sqlite/email-index.js +8 -4
  262. package/dist/store/sqlite/evidence-findings.js +29 -5
  263. package/dist/store/sqlite/findings-core.js +4 -2
  264. package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
  265. package/dist/store/sqlite/identity-link-rows.js +146 -0
  266. package/dist/store/sqlite/identity-proposals.d.ts +53 -0
  267. package/dist/store/sqlite/identity-proposals.js +234 -0
  268. package/dist/store/sqlite/involvements.d.ts +1 -1
  269. package/dist/store/sqlite/involvements.js +4 -2
  270. package/dist/store/sqlite/knowledge.js +2 -2
  271. package/dist/store/sqlite/link-kinds.d.ts +1 -0
  272. package/dist/store/sqlite/link-kinds.js +1 -0
  273. package/dist/store/sqlite/link-row.d.ts +6 -0
  274. package/dist/store/sqlite/link-row.js +35 -0
  275. package/dist/store/sqlite/link-source.d.ts +10 -0
  276. package/dist/store/sqlite/link-source.js +16 -0
  277. package/dist/store/sqlite/manifest.js +1 -0
  278. package/dist/store/sqlite/meeting-context-links.js +9 -6
  279. package/dist/store/sqlite/meeting-hit-query.d.ts +5 -0
  280. package/dist/store/sqlite/meeting-hit-query.js +33 -0
  281. package/dist/store/sqlite/meeting-hit-search.js +10 -8
  282. package/dist/store/sqlite/meetings.js +1 -1
  283. package/dist/store/sqlite/memories.js +9 -7
  284. package/dist/store/sqlite/mentions.d.ts +4 -1
  285. package/dist/store/sqlite/mentions.js +14 -9
  286. package/dist/store/sqlite/migrations.generated.js +8 -0
  287. package/dist/store/sqlite/notes.d.ts +8 -2
  288. package/dist/store/sqlite/notes.js +17 -34
  289. package/dist/store/sqlite/open-store.js +3 -0
  290. package/dist/store/sqlite/open.js +11 -2
  291. package/dist/store/sqlite/orders.d.ts +2 -0
  292. package/dist/store/sqlite/orders.js +9 -3
  293. package/dist/store/sqlite/organization-categories.d.ts +18 -0
  294. package/dist/store/sqlite/organization-categories.js +34 -0
  295. package/dist/store/sqlite/organizations.d.ts +5 -1
  296. package/dist/store/sqlite/organizations.js +14 -4
  297. package/dist/store/sqlite/person-links.d.ts +19 -4
  298. package/dist/store/sqlite/person-links.js +49 -11
  299. package/dist/store/sqlite/person-match-scan.d.ts +13 -0
  300. package/dist/store/sqlite/person-match-scan.js +251 -0
  301. package/dist/store/sqlite/person-matcher.d.ts +16 -0
  302. package/dist/store/sqlite/person-matcher.js +32 -0
  303. package/dist/store/sqlite/person-preparation.d.ts +7 -3
  304. package/dist/store/sqlite/person-preparation.js +20 -6
  305. package/dist/store/sqlite/proposed-actions.js +6 -0
  306. package/dist/store/sqlite/record-matching.d.ts +2 -8
  307. package/dist/store/sqlite/record-matching.js +8 -40
  308. package/dist/store/sqlite/record-projectors-phone.js +2 -1
  309. package/dist/store/sqlite/record-relationships.d.ts +52 -0
  310. package/dist/store/sqlite/record-relationships.js +243 -0
  311. package/dist/store/sqlite/record-run.d.ts +5 -0
  312. package/dist/store/sqlite/record-run.js +62 -2
  313. package/dist/store/sqlite/record-writes.d.ts +1 -1
  314. package/dist/store/sqlite/record-writes.js +4 -2
  315. package/dist/store/sqlite/records.js +27 -2
  316. package/dist/store/sqlite/ref-accounts.d.ts +13 -0
  317. package/dist/store/sqlite/ref-accounts.js +117 -0
  318. package/dist/store/sqlite/ref-summaries.js +5 -1
  319. package/dist/store/sqlite/relation-list.d.ts +4 -1
  320. package/dist/store/sqlite/relation-list.js +22 -4
  321. package/dist/store/sqlite/relation-proposals.d.ts +6 -0
  322. package/dist/store/sqlite/relation-proposals.js +3 -0
  323. package/dist/store/sqlite/relation-roles.js +12 -6
  324. package/dist/store/sqlite/reported-relations.d.ts +11 -1
  325. package/dist/store/sqlite/reported-relations.js +59 -18
  326. package/dist/store/sqlite/schema.d.ts +32 -0
  327. package/dist/store/sqlite/schema.js +3 -0
  328. package/dist/store/sqlite/transaction.js +11 -1
  329. package/dist/store/store.d.ts +3 -0
  330. package/dist/store/store.js +5 -0
  331. package/dist/store/write-events.d.ts +18 -0
  332. package/dist/store/write-events.js +88 -0
  333. package/package.json +3 -3
  334. package/parity.json +37 -16
  335. package/schemas/records-envelope.v1.schema.json +3 -0
package/dist/crm/sync.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
2
  import { runBudget } from "../background/budget.js";
3
3
  import { progressLine, progressOf } from "../background/checkpoints.js";
4
- import { CRM_KINDS, isItemKind } from "../domain/crm.js";
4
+ import { CRM_KINDS, isCrmUserId, isItemKind, normalCrmUserId, } from "../domain/crm.js";
5
5
  import { DEAL_KINDS } from "../domain/deals.js";
6
6
  import { isCliFailure } from "../errors/failures.js";
7
+ import { crmSettingKey } from "../store/sqlite/crm-settings.js";
8
+ import { ungroupedHint } from "./group-hint.js";
7
9
  const DAY = 86_400_000;
8
10
  /** A row the CRM changed this close to the id pass's start may be newer than the list, by the two clocks' skew. */
9
11
  const CLOCK_MARGIN = 60 * 60_000;
@@ -37,6 +39,8 @@ export const parseCrmCursor = (value) => {
37
39
  updatedAt: typeof cursor.updatedAt === "number" ? cursor.updatedAt : null,
38
40
  pass: pass ?? null,
39
41
  passAt: typeof cursor.passAt === "number" ? cursor.passAt : 0,
42
+ ...(typeof cursor.scope === "string" ? { scope: cursor.scope } : {}),
43
+ ...(cursor.narrowing === true ? { narrowing: true } : {}),
40
44
  };
41
45
  }
42
46
  catch {
@@ -205,12 +209,67 @@ const savePage = async (run, kind, page) => {
205
209
  run.report.issues.push({ kind, ...one });
206
210
  return { saved, stop };
207
211
  };
212
+ const scopeIds = (scope) => scope.mode === "all" ? null : [...new Set([scope.self, ...scope.also])].sort();
213
+ const scopeKey = (scope) => {
214
+ const ids = scopeIds(scope);
215
+ return ids ? `mine:${ids.join(",")}` : "all";
216
+ };
217
+ const idsOfKey = (key) => (key.startsWith("mine:") ? key.slice(5).split(",") : null);
218
+ /** The scope new pages are read under; `null` before the first sync. A held narrowing is `held`, not this. */
219
+ const appliedScope = (cursor) => {
220
+ if (cursor === null)
221
+ return null;
222
+ const users = idsOfKey(cursor.scope ?? "all");
223
+ return users === null ? { mode: "all" } : { mode: "mine", users };
224
+ };
225
+ /** Under `mine`, a row with no assignee is nobody's, so it is in scope. */
226
+ const inScope = (ids, item) => ids === null || item.assignee == null || ids.includes(item.assignee);
227
+ const normalAssignee = (item) => item.assignee == null ? item : { ...item, assignee: normalCrmUserId(item.assignee) };
208
228
  const syncKind = async (run, kind, options, result) => {
209
229
  const { source, store, account, now, budget, signal } = run;
210
230
  const name = cursorName(kind);
211
231
  let cursor = parseCrmCursor((await store.sync.syncState(account, name))?.value) ?? fresh();
212
232
  if (options.rescan)
213
233
  cursor = { ...cursor, round: { from: null, page: null, reset: false }, done: 0, pass: null, passAt: 0 };
234
+ const scope = scopeKey(run.scope);
235
+ const ids = isItemKind(kind) ? scopeIds(run.scope) : null;
236
+ const listedUnder = cursor.scope ?? "all";
237
+ const before = idsOfKey(listedUnder);
238
+ const changed = isItemKind(kind) && listedUnder !== scope;
239
+ let held = false;
240
+ // Narrowing is done here, by each stored row's assignee, so no list from the CRM can empty the copy.
241
+ if (isItemKind(kind) &&
242
+ ids &&
243
+ (cursor.narrowing === true || (changed && (before === null || before.some((id) => !ids.includes(id)))))) {
244
+ const leaving = await store.crm.removeOutOfScope(account, kind, ids, { dryRun: true });
245
+ const stored = await store.crm.count(account, kind);
246
+ // A default `mine` whose own user holds few of the copy's rows is more likely a wrong sign-in than the owner's
247
+ // wish: the stored rows wait for the owner to set the scope. New pages are still read under `mine`.
248
+ held = !run.scopeSet && leaving > stored * MOST_UNLISTED;
249
+ if (held) {
250
+ result.held = true;
251
+ run.report.issues.push({
252
+ kind,
253
+ reason: "crm.narrowing-held",
254
+ message: `scope mine keeps the ${kind}s of user ${ids.join(", ")}: ${leaving} of the ${stored} stored would be ` +
255
+ `marked deleted; none was, and none will be until the owner sets ${crmSettingKey(account, "scope")} ` +
256
+ "to mine or all",
257
+ });
258
+ }
259
+ else
260
+ result.outOfScope += await store.crm.removeOutOfScope(account, kind, ids);
261
+ }
262
+ if (changed || cursor.narrowing) {
263
+ // The cursor takes the new scope even while held: pages are read under it, so a later wider scope lists in full.
264
+ const widened = changed && before !== null && (ids === null || ids.some((id) => !before.includes(id)));
265
+ const { narrowing: _done, ...rest } = cursor;
266
+ cursor = {
267
+ ...rest,
268
+ ...(widened ? { round: { from: null, page: null, reset: false }, done: 0 } : {}),
269
+ scope,
270
+ ...(held ? { narrowing: true } : {}),
271
+ };
272
+ }
214
273
  let round = cursor.round ?? { from: cursor.token, page: null, reset: false };
215
274
  if (cursor.round === null)
216
275
  cursor = { ...cursor, done: 0 };
@@ -244,11 +303,17 @@ const syncKind = async (run, kind, options, result) => {
244
303
  await save();
245
304
  continue;
246
305
  }
247
- const { saved, stop } = await savePage(run, kind, page);
306
+ const items = isItemKind(kind) ? page.items.map(normalAssignee) : page.items;
307
+ const kept = isItemKind(kind) ? items.filter((item) => inScope(ids, item)) : items;
308
+ const { saved, stop } = await savePage(run, kind, { ...page, items: kept });
248
309
  result.saved += saved;
249
310
  cursor = { ...cursor, done: cursor.done + saved };
250
311
  if (page.removed?.length)
251
312
  result.removed += await store.crm.remove(account, kind, page.removed);
313
+ if (kept.length < items.length && !held) {
314
+ const left = items.filter((item) => !inScope(ids, item)).map(({ externalId }) => externalId);
315
+ result.outOfScope += await store.crm.remove(account, kind, left);
316
+ }
252
317
  run.moved();
253
318
  if (page.nextPage === undefined) {
254
319
  listed = true;
@@ -287,7 +352,8 @@ const syncKind = async (run, kind, options, result) => {
287
352
  const before = pass.startedAt - CLOCK_MARGIN;
288
353
  const stored = await store.crm.count(account, kind);
289
354
  const unlisted = await store.crm.removeUnlisted(account, kind, pass.seen, before, { dryRun: true });
290
- if (!options.rescan && stored >= GUARDED_FROM && unlisted > stored * MOST_UNLISTED) {
355
+ const emptied = pass.seen.length === 0 && unlisted > 0;
356
+ if (!options.rescan && (emptied || (stored >= GUARDED_FROM && unlisted > stored * MOST_UNLISTED))) {
291
357
  result.pass = "held";
292
358
  run.report.issues.push({
293
359
  kind,
@@ -316,9 +382,28 @@ const syncKind = async (run, kind, options, result) => {
316
382
  export const syncCrm = async (source, store, account, options = {}) => {
317
383
  const now = options.now ?? Date.now();
318
384
  const budget = options.budget ?? runBudget();
385
+ const setting = await store.crm.scope(account);
386
+ const self = source.self ? normalCrmUserId(await source.self()) : undefined;
387
+ if (self !== undefined && !isCrmUserId(self))
388
+ throw new CliError("invalid_response", `${source.provider} named its sign-in's user as "${self}", not a user id`);
389
+ if (self !== undefined) {
390
+ const owner = await store.crm.ownUser(account);
391
+ if (owner === undefined)
392
+ await store.crm.saveOwnUser(account, self);
393
+ else if (owner !== self) {
394
+ const setting = crmSettingKey(account, "self");
395
+ throw new CliError("validation_error", `this account's copy is user ${owner}'s, and this sign-in is user ${self}: one copy holds one user's ` +
396
+ `deals, under scope all too, since ${setting} decides whose deals a later scope mine keeps; ` +
397
+ `"config unset ${setting}" gives the copy to this sign-in`, { setting });
398
+ }
399
+ }
400
+ const scope = self !== undefined && setting.mode === "mine"
401
+ ? { mode: "mine", self, also: setting.also.map(normalCrmUserId) }
402
+ : { mode: "all" };
319
403
  const report = {
320
404
  provider: source.provider,
321
405
  account: account.account,
406
+ scope,
322
407
  pipelines: 0,
323
408
  skippedOwnerRejected: 0,
324
409
  synced: [],
@@ -330,6 +415,8 @@ export const syncCrm = async (source, store, account, options = {}) => {
330
415
  let steps = 0;
331
416
  const run = {
332
417
  source,
418
+ scope,
419
+ scopeSet: setting.mode === "mine" && setting.explicit,
333
420
  store,
334
421
  account,
335
422
  now,
@@ -352,11 +439,15 @@ export const syncCrm = async (source, store, account, options = {}) => {
352
439
  throw new BudgetSpent(why);
353
440
  },
354
441
  gap: ({ kind, message, scope }) => run.gaps.push({ kind, reason: "partial", message, ...(scope ? { scope } : {}) }),
442
+ scope,
355
443
  },
356
444
  unlistedUsers: 0,
357
445
  droppedPending: 0,
358
446
  };
359
447
  const kinds = CRM_KINDS.filter((kind) => source.traits.kinds.includes(kind));
448
+ const cursors = await Promise.all(kinds.map((kind) => store.sync.syncState(account, cursorName(kind))));
449
+ const first = cursors.every((cursor) => cursor === undefined);
450
+ const malformed = scope.mode === "mine" && setting.mode === "mine" ? setting.also.find((id) => !isCrmUserId(id)) : undefined;
360
451
  let stopped = null;
361
452
  for (const kind of DEAL_KINDS)
362
453
  if (!stopped && kinds.includes(kind)) {
@@ -386,10 +477,18 @@ export const syncCrm = async (source, store, account, options = {}) => {
386
477
  saved: 0,
387
478
  removed: 0,
388
479
  unlisted: 0,
480
+ outOfScope: 0,
389
481
  };
390
482
  report.synced.push(result);
391
483
  if (stopped)
392
484
  continue;
485
+ if (malformed !== undefined && isItemKind(kind)) {
486
+ Object.assign(result, {
487
+ error: `unassigned holds "${malformed}", not a numeric user id: no ${kind} was read`,
488
+ setting: crmSettingKey(account, "unassigned"),
489
+ });
490
+ continue;
491
+ }
393
492
  try {
394
493
  stopped = await syncKind(run, kind, options, result);
395
494
  // After every company of the run is stored, so one listed before the own company still gets its meaning.
@@ -439,10 +538,15 @@ export const syncCrm = async (source, store, account, options = {}) => {
439
538
  report.complete = false;
440
539
  report.hint = CONTINUE;
441
540
  }
541
+ const groupHint = first ? await ungroupedHint(store, account) : undefined;
542
+ if (groupHint)
543
+ report.groupHint = groupHint;
442
544
  return report;
443
545
  };
444
546
  /** Each kind's sync in the WC-LIFE-14 shape: `cursor_age_ms` growing on a `partial` one means nothing moves it. */
445
547
  export const crmStatus = async (store, account, { profile, now, kinds = CRM_KINDS }) => {
548
+ const saved = await store.crm.scope(account);
549
+ const setting = { setting: saved.mode, explicit: saved.mode === "all" || saved.explicit };
446
550
  const items = [];
447
551
  for (const kind of kinds) {
448
552
  const cursor = parseCrmCursor((await store.sync.syncState(account, cursorName(kind)))?.value);
@@ -459,6 +563,9 @@ export const crmStatus = async (store, account, { profile, now, kinds = CRM_KIND
459
563
  round: cursor?.round ? (cursor.round.from === null ? "full" : "changes") : null,
460
564
  id_pass: cursor?.pass ? "partial" : cursor?.passAt ? new Date(cursor.passAt).toISOString() : null,
461
565
  stored: await store.crm.count(account, kind),
566
+ ...(isItemKind(kind)
567
+ ? { scope: { ...setting, applied: appliedScope(cursor), ...(cursor?.narrowing ? { held: true } : {}) } }
568
+ : {}),
462
569
  ...progress,
463
570
  line: progressLine(progress),
464
571
  });
@@ -0,0 +1,40 @@
1
+ import type { AccountKey, MessageStore } from "../../store/store.js";
2
+ import { type CrmSyncReport } from "../index.js";
3
+ import { type FakeCrmSource } from "./source.js";
4
+ /** The CRM portal of the owner's own company, Northstar Consulting. */
5
+ export declare const ACME_PORTAL: AccountKey;
6
+ export declare const ACME_TELEGRAM: AccountKey;
7
+ export declare const ACME_MAIL: AccountKey;
8
+ export declare const ACME_CALENDAR: AccountKey;
9
+ /** The deal opens on this day; every message, mail and meeting falls within its first month. */
10
+ export declare const ACME_OPENED: number;
11
+ export declare const ACME_NOW: number;
12
+ export interface AcmeScenario {
13
+ store: MessageStore;
14
+ /** The portal: change a row with `crm.put` or `crm.delete`, then `sync()` again. */
15
+ crm: FakeCrmSource;
16
+ sync: () => Promise<CrmSyncReport>;
17
+ refs: {
18
+ owner: string;
19
+ /** Northstar Consulting, the owner's own company. */
20
+ own: string;
21
+ acme: string;
22
+ alice: string;
23
+ bob: string;
24
+ emma: string;
25
+ deal: string;
26
+ project: string;
27
+ invoice: string;
28
+ task: string;
29
+ event: string;
30
+ };
31
+ }
32
+ /**
33
+ * Plan §8's scenario, synthetic: Northstar Consulting is the owner's own company and its CRM portal, Acme Ltd its
34
+ * customer. Alice and Bob are Acme's CRM contacts; Emma is a portal user, Northstar's sales owner and the deal's owner.
35
+ * The deal "Acme renewal" has Alice as contact (CRM), Bob as decision-maker (the owner's) and project Atlas (the
36
+ * 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
37
+ * Acme with no payment and an open follow-up task. Everything is written through the store's and the CRM sync's own
38
+ * calls; the store is the caller's, and no real account is touched.
39
+ */
40
+ export declare const acmeScenario: (store: MessageStore) => Promise<AcmeScenario>;
@@ -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";
@@ -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;