@wirecat/hubspot-cli 0.0.0-stage → 0.2.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 (261) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/LICENSE +204 -0
  3. package/README.md +280 -2
  4. package/contracts/wirecat.json +3270 -0
  5. package/dist/bin/hubspot.d.ts +3 -0
  6. package/dist/bin/hubspot.d.ts.map +1 -0
  7. package/dist/bin/hubspot.js +40 -0
  8. package/dist/bin/hubspot.js.map +1 -0
  9. package/dist/bundle/all-profiles-64JAUYW4.js +279 -0
  10. package/dist/bundle/anthropic-VJOS4OUM.js +91 -0
  11. package/dist/bundle/app-server-GXV2OFML.js +245 -0
  12. package/dist/bundle/bun-M3J2QTR3.js +169 -0
  13. package/dist/bundle/bun-sqlite-IX3XYA5F.js +76 -0
  14. package/dist/bundle/chunk-24ERVFJP.js +73 -0
  15. package/dist/bundle/chunk-2QZK5BEP.js +392 -0
  16. package/dist/bundle/chunk-2UOJ7DLZ.js +46 -0
  17. package/dist/bundle/chunk-2VGD63G6.js +604 -0
  18. package/dist/bundle/chunk-2VGZRYEH.js +334 -0
  19. package/dist/bundle/chunk-36ITRDYY.js +63116 -0
  20. package/dist/bundle/chunk-3WLYWRGG.js +1395 -0
  21. package/dist/bundle/chunk-43EKCUBJ.js +72 -0
  22. package/dist/bundle/chunk-4W6BMH45.js +731 -0
  23. package/dist/bundle/chunk-5A6ZYNAQ.js +2003 -0
  24. package/dist/bundle/chunk-5D4AEUVA.js +24783 -0
  25. package/dist/bundle/chunk-5GBUCHRU.js +274 -0
  26. package/dist/bundle/chunk-5K7QLJMM.js +4072 -0
  27. package/dist/bundle/chunk-65TWQ7BV.js +54 -0
  28. package/dist/bundle/chunk-7ELMWE3P.js +12066 -0
  29. package/dist/bundle/chunk-ADLS6FVG.js +24600 -0
  30. package/dist/bundle/chunk-B3MANLNK.js +602 -0
  31. package/dist/bundle/chunk-C32AKHTY.js +2310 -0
  32. package/dist/bundle/chunk-CLAN3FES.js +1246 -0
  33. package/dist/bundle/chunk-CWXOXIWS.js +46 -0
  34. package/dist/bundle/chunk-D72URWJE.js +532 -0
  35. package/dist/bundle/chunk-DB2XKHI4.js +72 -0
  36. package/dist/bundle/chunk-DBEQI5YM.js +100 -0
  37. package/dist/bundle/chunk-DFCHHR5M.js +38 -0
  38. package/dist/bundle/chunk-DFDN6AUB.js +21 -0
  39. package/dist/bundle/chunk-DRWWJLI6.js +188 -0
  40. package/dist/bundle/chunk-DSP36YHQ.js +29 -0
  41. package/dist/bundle/chunk-E52JAS35.js +1028 -0
  42. package/dist/bundle/chunk-E5WLE7BM.js +78 -0
  43. package/dist/bundle/chunk-ECEZOLOD.js +230 -0
  44. package/dist/bundle/chunk-EH6ZEJGT.js +932 -0
  45. package/dist/bundle/chunk-FBCCBCFH.js +288 -0
  46. package/dist/bundle/chunk-FKA7DOCK.js +53 -0
  47. package/dist/bundle/chunk-FOZYMDRL.js +259 -0
  48. package/dist/bundle/chunk-FPUGMDJV.js +7 -0
  49. package/dist/bundle/chunk-FTFV3HFW.js +721 -0
  50. package/dist/bundle/chunk-GPYCBYX5.js +218 -0
  51. package/dist/bundle/chunk-H3TNVEQX.js +782 -0
  52. package/dist/bundle/chunk-HC5ZWE5X.js +193 -0
  53. package/dist/bundle/chunk-HCAZV5NP.js +95 -0
  54. package/dist/bundle/chunk-HZRQLAHO.js +16 -0
  55. package/dist/bundle/chunk-IJADZW42.js +195 -0
  56. package/dist/bundle/chunk-IMJQRPLN.js +92 -0
  57. package/dist/bundle/chunk-JLDBMEKZ.js +106 -0
  58. package/dist/bundle/chunk-LOBVOGOE.js +66 -0
  59. package/dist/bundle/chunk-LXZ66MCH.js +1328 -0
  60. package/dist/bundle/chunk-MFNMKHML.js +26462 -0
  61. package/dist/bundle/chunk-MVA5HVKL.js +1392 -0
  62. package/dist/bundle/chunk-N2XADGL4.js +76 -0
  63. package/dist/bundle/chunk-NHHJHXMQ.js +58 -0
  64. package/dist/bundle/chunk-NUU4WZVL.js +52 -0
  65. package/dist/bundle/chunk-NWHEHV3K.js +133 -0
  66. package/dist/bundle/chunk-NXERIDPJ.js +319 -0
  67. package/dist/bundle/chunk-O3DYRCIT.js +23 -0
  68. package/dist/bundle/chunk-O55RWNSH.js +161 -0
  69. package/dist/bundle/chunk-P5BHKKBI.js +206 -0
  70. package/dist/bundle/chunk-PPHBRESD.js +82 -0
  71. package/dist/bundle/chunk-PVKF24ZW.js +364 -0
  72. package/dist/bundle/chunk-Q2OLOMHL.js +4045 -0
  73. package/dist/bundle/chunk-QGFX5MXL.js +460 -0
  74. package/dist/bundle/chunk-QJW35N5V.js +761 -0
  75. package/dist/bundle/chunk-QU25DA5N.js +360 -0
  76. package/dist/bundle/chunk-RMIYKDUL.js +1068 -0
  77. package/dist/bundle/chunk-RR5MX5TM.js +142 -0
  78. package/dist/bundle/chunk-SDQGB4KB.js +185 -0
  79. package/dist/bundle/chunk-TBJYLC5K.js +2788 -0
  80. package/dist/bundle/chunk-TI46WQXQ.js +1197 -0
  81. package/dist/bundle/chunk-TKVL2C3B.js +170 -0
  82. package/dist/bundle/chunk-TLKEPUAV.js +786 -0
  83. package/dist/bundle/chunk-TOSCB4SO.js +1227 -0
  84. package/dist/bundle/chunk-TP7GDWVF.js +6848 -0
  85. package/dist/bundle/chunk-URX6YDUL.js +9393 -0
  86. package/dist/bundle/chunk-UUMUL4L3.js +72 -0
  87. package/dist/bundle/chunk-VOH7S5M4.js +40963 -0
  88. package/dist/bundle/chunk-W53EJOFQ.js +63 -0
  89. package/dist/bundle/chunk-W7HM2VGO.js +24 -0
  90. package/dist/bundle/chunk-WE4UFASG.js +88 -0
  91. package/dist/bundle/chunk-X2M6NIQG.js +18 -0
  92. package/dist/bundle/chunk-XRREFEIW.js +372 -0
  93. package/dist/bundle/chunk-Y5E2WNWB.js +35 -0
  94. package/dist/bundle/chunk-YMCY7VRE.js +1846 -0
  95. package/dist/bundle/chunk-Z73OL5HV.js +41 -0
  96. package/dist/bundle/chunk-ZVVMGPBJ.js +50 -0
  97. package/dist/bundle/command-contract-C6CBFXZF.js +40 -0
  98. package/dist/bundle/command-server-entry-DJKVVZCQ.js +169 -0
  99. package/dist/bundle/decode-FC65Y4IV.js +297 -0
  100. package/dist/bundle/definitions-MVPICAWV.js +2991 -0
  101. package/dist/bundle/dispatch-KOISQ5QJ.js +411 -0
  102. package/dist/bundle/dist-7TGU56B2.js +7577 -0
  103. package/dist/bundle/echarts-FVTUDXDU.js +42501 -0
  104. package/dist/bundle/embed-VNBZFPYH.js +38 -0
  105. package/dist/bundle/embedder-6U4NOFRO.js +111 -0
  106. package/dist/bundle/failure-FZ6VYKEQ.js +25 -0
  107. package/dist/bundle/first-run-UWAFSINQ.js +29 -0
  108. package/dist/bundle/first-screen-RTVMGHAE.js +40 -0
  109. package/dist/bundle/hubspot.js +198 -0
  110. package/dist/bundle/instructions-HET7BCYK.js +11 -0
  111. package/dist/bundle/mcp-RGNRW77S.js +167 -0
  112. package/dist/bundle/meeting-combined-L5AGBPYX.js +25 -0
  113. package/dist/bundle/meeting-model-NTB4RK3F.js +25 -0
  114. package/dist/bundle/meeting-semantic-JY3OGCBS.js +22 -0
  115. package/dist/bundle/node-CXZIFHZC.js +176 -0
  116. package/dist/bundle/node-P4E63KXA.js +2340 -0
  117. package/dist/bundle/node-TLZSIHIJ.js +21 -0
  118. package/dist/bundle/node-sqlite-P3O7XN7Q.js +74 -0
  119. package/dist/bundle/oauth-HNCY4B2S.js +20 -0
  120. package/dist/bundle/open-F4P6YV54.js +23 -0
  121. package/dist/bundle/openai-4AICLIK3.js +238 -0
  122. package/dist/bundle/process-5RUXWGYS.js +91 -0
  123. package/dist/bundle/program-XA4C44Z4.js +9374 -0
  124. package/dist/bundle/prompt-NFMDVBZ4.js +17 -0
  125. package/dist/bundle/recognize-DKN3RY4V.js +26 -0
  126. package/dist/bundle/remote-6QHAGQWY.js +31 -0
  127. package/dist/bundle/rerank-M4KHEHUZ.js +52 -0
  128. package/dist/bundle/schemas-JRL3DLMP.js +952 -0
  129. package/dist/bundle/serve-DNU43ND6.js +199 -0
  130. package/dist/bundle/server-ATG5KY7K.js +207 -0
  131. package/dist/bundle/server-VNWVVH7A.js +77 -0
  132. package/dist/bundle/source-54HR4IWG.js +661 -0
  133. package/dist/bundle/sqlite-runtime-EMQAC63N.js +20 -0
  134. package/dist/bundle/state-T45CX7VY.js +36 -0
  135. package/dist/bundle/store-6LCEVBAZ.js +176 -0
  136. package/dist/bundle/store-6TZ2FCVA.js +295 -0
  137. package/dist/bundle/store-DXWYNVVB.js +32 -0
  138. package/dist/bundle/store-maintenance-command-J57HCJYY.js +68 -0
  139. package/dist/bundle/writer-NLLI5YRM.js +79 -0
  140. package/dist/commands/account.d.ts +3 -0
  141. package/dist/commands/account.d.ts.map +1 -0
  142. package/dist/commands/account.js +87 -0
  143. package/dist/commands/account.js.map +1 -0
  144. package/dist/commands/api.d.ts +9 -0
  145. package/dist/commands/api.d.ts.map +1 -0
  146. package/dist/commands/api.js +70 -0
  147. package/dist/commands/api.js.map +1 -0
  148. package/dist/commands/crm-shared.d.ts +8 -0
  149. package/dist/commands/crm-shared.d.ts.map +1 -0
  150. package/dist/commands/crm-shared.js +109 -0
  151. package/dist/commands/crm-shared.js.map +1 -0
  152. package/dist/commands/schedule.d.ts +3 -0
  153. package/dist/commands/schedule.d.ts.map +1 -0
  154. package/dist/commands/schedule.js +9 -0
  155. package/dist/commands/schedule.js.map +1 -0
  156. package/dist/commands/session.d.ts +14 -0
  157. package/dist/commands/session.d.ts.map +1 -0
  158. package/dist/commands/session.js +164 -0
  159. package/dist/commands/session.js.map +1 -0
  160. package/dist/config.d.ts +60 -0
  161. package/dist/config.d.ts.map +1 -0
  162. package/dist/config.js +153 -0
  163. package/dist/config.js.map +1 -0
  164. package/dist/context.d.ts +88 -0
  165. package/dist/context.d.ts.map +1 -0
  166. package/dist/context.js +137 -0
  167. package/dist/context.js.map +1 -0
  168. package/dist/crm/activities.d.ts +24 -0
  169. package/dist/crm/activities.d.ts.map +1 -0
  170. package/dist/crm/activities.js +214 -0
  171. package/dist/crm/activities.js.map +1 -0
  172. package/dist/crm/associations.d.ts +20 -0
  173. package/dist/crm/associations.d.ts.map +1 -0
  174. package/dist/crm/associations.js +47 -0
  175. package/dist/crm/associations.js.map +1 -0
  176. package/dist/crm/records.d.ts +63 -0
  177. package/dist/crm/records.d.ts.map +1 -0
  178. package/dist/crm/records.js +333 -0
  179. package/dist/crm/records.js.map +1 -0
  180. package/dist/crm/search.d.ts +57 -0
  181. package/dist/crm/search.d.ts.map +1 -0
  182. package/dist/crm/search.js +47 -0
  183. package/dist/crm/search.js.map +1 -0
  184. package/dist/crm/source.d.ts +37 -0
  185. package/dist/crm/source.d.ts.map +1 -0
  186. package/dist/crm/source.js +366 -0
  187. package/dist/crm/source.js.map +1 -0
  188. package/dist/crm/writer.d.ts +15 -0
  189. package/dist/crm/writer.d.ts.map +1 -0
  190. package/dist/crm/writer.js +199 -0
  191. package/dist/crm/writer.js.map +1 -0
  192. package/dist/generated/definitions.d.ts +3 -0
  193. package/dist/generated/definitions.d.ts.map +1 -0
  194. package/dist/generated/definitions.js +2987 -0
  195. package/dist/generated/definitions.js.map +1 -0
  196. package/dist/generated/manifest.d.ts +6 -0
  197. package/dist/generated/manifest.d.ts.map +1 -0
  198. package/dist/generated/manifest.js +6504 -0
  199. package/dist/generated/manifest.js.map +1 -0
  200. package/dist/generated/schemas.d.ts +205 -0
  201. package/dist/generated/schemas.d.ts.map +1 -0
  202. package/dist/generated/schemas.js +810 -0
  203. package/dist/generated/schemas.js.map +1 -0
  204. package/dist/generated/types.d.ts +1114 -0
  205. package/dist/generated/types.d.ts.map +1 -0
  206. package/dist/generated/types.js +5 -0
  207. package/dist/generated/types.js.map +1 -0
  208. package/dist/hubspot/account.d.ts +12 -0
  209. package/dist/hubspot/account.d.ts.map +1 -0
  210. package/dist/hubspot/account.js +23 -0
  211. package/dist/hubspot/account.js.map +1 -0
  212. package/dist/hubspot/transport.d.ts +81 -0
  213. package/dist/hubspot/transport.d.ts.map +1 -0
  214. package/dist/hubspot/transport.js +354 -0
  215. package/dist/hubspot/transport.js.map +1 -0
  216. package/dist/identity.d.ts +7 -0
  217. package/dist/identity.d.ts.map +1 -0
  218. package/dist/identity.js +32 -0
  219. package/dist/identity.js.map +1 -0
  220. package/dist/index.d.ts +3 -0
  221. package/dist/index.d.ts.map +1 -0
  222. package/dist/index.js +3 -0
  223. package/dist/index.js.map +1 -0
  224. package/dist/program.d.ts +6 -0
  225. package/dist/program.d.ts.map +1 -0
  226. package/dist/program.js +78 -0
  227. package/dist/program.js.map +1 -0
  228. package/dist/reasons.d.ts +10 -0
  229. package/dist/reasons.d.ts.map +1 -0
  230. package/dist/reasons.js +33 -0
  231. package/dist/reasons.js.map +1 -0
  232. package/dist/setup/flow.d.ts +2 -0
  233. package/dist/setup/flow.d.ts.map +1 -0
  234. package/dist/setup/flow.js +97 -0
  235. package/dist/setup/flow.js.map +1 -0
  236. package/dist/setup/texts.en.json +19 -0
  237. package/dist/setup/texts.es.json +19 -0
  238. package/dist/setup/texts.ru.json +19 -0
  239. package/dist/store.d.ts +16 -0
  240. package/dist/store.d.ts.map +1 -0
  241. package/dist/store.js +34 -0
  242. package/dist/store.js.map +1 -0
  243. package/dist/version.d.ts +2 -0
  244. package/dist/version.d.ts.map +1 -0
  245. package/dist/version.js +2 -0
  246. package/dist/version.js.map +1 -0
  247. package/dist/wcat.d.ts +3 -0
  248. package/dist/wcat.d.ts.map +1 -0
  249. package/dist/wcat.js +4 -0
  250. package/dist/wcat.js.map +1 -0
  251. package/dist/whoami.d.ts +19 -0
  252. package/dist/whoami.d.ts.map +1 -0
  253. package/dist/whoami.js +15 -0
  254. package/dist/whoami.js.map +1 -0
  255. package/package.json +98 -4
  256. package/skills/hubspot-cli/SKILL.md +102 -0
  257. package/skills/hubspot-cli/references/api/api-advanced.md +51 -0
  258. package/skills/hubspot-cli/references/api/api-basic.md +1064 -0
  259. package/skills/hubspot-cli/references/api/api-batch.md +724 -0
  260. package/skills/hubspot-cli/references/api/api-search.md +147 -0
  261. package/skills/hubspot-cli/references/api.md +16 -0
package/dist/config.js ADDED
@@ -0,0 +1,153 @@
1
+ import { commandPathKey, commandPermissions, serviceApiPermissions, settingsFor, } from "@wirecat/cli-core/cli";
2
+ import { CRM_ASSIGN_OTHERS_KEY, CRM_WRITE_DEFAULTS, CRM_WRITE_KEYS, crmAlsoChecks, } from "@wirecat/cli-core/crm";
3
+ import * as manifest from "./generated/manifest.js";
4
+ import { SERVICE } from "./hubspot/transport.js";
5
+ import { IDENTITY } from "./identity.js";
6
+ const firstWord = (command) => command.split("-")[0] ?? command;
7
+ /**
8
+ * The group of an operation's keys, as the generator named its delete key (`contacts-delete` → `people.delete`), so
9
+ * `people.write` sits beside `people.delete` and both under `people`.
10
+ */
11
+ const GROUPS = new Map(manifest.operations.flatMap((operation) => (operation.permissions ?? []).flatMap(({ key }) => key.endsWith(".delete") ? [[firstWord(operation.command), key.slice(0, -".delete".length)]] : [])));
12
+ /**
13
+ * The CRM command each raw operation does the same as, so `deals.update: deny` also stops `api deals-update` and its
14
+ * `--dry-run` names the key (WC-PERM-22). Deletes need no entry: the generator gave them `deals.delete` already.
15
+ */
16
+ const COMMAND_KEYS = Object.fromEntries([
17
+ ["deals", "deals"],
18
+ ["contacts", "people"],
19
+ ["companies", "organizations"],
20
+ ].flatMap(([type, noun]) => [
21
+ [`${type}-create`, [`${noun}.create`]],
22
+ [`${type}-batch-create`, [`${noun}.create`]],
23
+ [`${type}-update`, [`${noun}.update`]],
24
+ [`${type}-batch-update`, [`${noun}.update`]],
25
+ [`${type}-batch-upsert`, [`${noun}.create`, `${noun}.update`]],
26
+ ]));
27
+ /** Every operation the spec has but one that answers with a credential: the CLI signs in through `session`. */
28
+ const offered = (operation) => !operation.response?.sensitive;
29
+ /**
30
+ * Erases a contact and everything on it for good, and blocks its e-mail address from coming back: a key of its own,
31
+ * so the owner can deny it and still allow ordinary deletes. `ask`, not `deny`: cli-core's `--dry-run` refuses a
32
+ * command whose key is denied, and the deny contract needs each write's preview.
33
+ */
34
+ const ERASE_KEY = "people.erase";
35
+ const ERASES = "contacts-gdpr-delete";
36
+ /** A write's own `<group>.write`, `ask` by default like each key of the api group, and its command's keys. */
37
+ const keysOf = (operation) => {
38
+ const group = GROUPS.get(firstWord(operation.command));
39
+ const own = operation.effect === "write" && group ? [{ key: `${group}.write` }] : [];
40
+ const erases = operation.command === ERASES ? [{ key: ERASE_KEY }] : [];
41
+ return [...own, ...erases, ...(COMMAND_KEYS[operation.command] ?? []).map((key) => ({ key }))];
42
+ };
43
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
44
+ /** The properties of each record a body writes: one under `properties`, a batch's under `inputs[].properties`. */
45
+ const propertiesOf = (body) => {
46
+ if (!isRecord(body))
47
+ return [];
48
+ const records = Array.isArray(body.inputs) ? body.inputs.filter(isRecord) : [body];
49
+ return records.flatMap((record) => (isRecord(record.properties) ? [record.properties] : []));
50
+ };
51
+ /** A value the body sets, not one it clears: an empty value takes the owner away, it gives the record to nobody. */
52
+ const sets = (body, name) => propertiesOf(body).some((properties) => {
53
+ const value = properties[name];
54
+ return value !== undefined && value !== null && String(value).trim() !== "";
55
+ });
56
+ /**
57
+ * A key the body implies through a nested property, which a manifest flag cannot name: a flag is a top-level body
58
+ * field. A service key belongs to the account, not a user, so any owner a body names is someone else (as
59
+ * `deals create --owner` checks `crm.assign-others`), and a deal's stage or pipeline is a move.
60
+ */
61
+ const implied = (key, names) => {
62
+ const paths = names.map((name) => `properties.${name}`);
63
+ return {
64
+ /** For `--dry-run` and discovery: a flag the manifest names is a top-level field, so this one never applies. */
65
+ named: { key, body: paths },
66
+ checked: {
67
+ key,
68
+ when: `with ${paths.join(" or ")} set`,
69
+ applies: (body) => names.some((name) => sets(body, name)),
70
+ },
71
+ };
72
+ };
73
+ const ASSIGNS = implied(CRM_ASSIGN_OTHERS_KEY, ["hubspot_owner_id"]);
74
+ const MOVES = implied("deals.move", ["dealstage", "pipeline"]);
75
+ /** A won or lost stage is a close; which stage is closed needs the pipelines, so any stage checks it, as stricter. */
76
+ const CLOSES = implied("deals.close", ["dealstage"]);
77
+ const OWNED = new Set([
78
+ "contacts",
79
+ "companies",
80
+ "deals",
81
+ "tickets",
82
+ "leads",
83
+ "notes",
84
+ "calls",
85
+ "emails",
86
+ "meetings",
87
+ "tasks",
88
+ ]);
89
+ const RECORD_WRITES = ["create", "update", "batch-create", "batch-update", "batch-upsert"];
90
+ const bodyKeysOf = (operation) => {
91
+ const type = firstWord(operation.command);
92
+ if (!OWNED.has(type) || !RECORD_WRITES.includes(operation.command.slice(type.length + 1)))
93
+ return [];
94
+ return [ASSIGNS, ...(type === "deals" ? [MOVES, CLOSES] : [])];
95
+ };
96
+ const bodyPermissions = Object.fromEntries(manifest.operations.filter(offered).flatMap((operation) => {
97
+ const keys = bodyKeysOf(operation).map(({ checked }) => checked);
98
+ return keys.length ? [[operation.command, keys]] : [];
99
+ }));
100
+ /**
101
+ * Every operation of HubSpot's CRM specs: reads, writes and deletes. A write checks `api.<command>`, its group's
102
+ * `.write` and the CRM command it does the same as; a delete its group's `.delete`.
103
+ */
104
+ export const API = {
105
+ manifest: {
106
+ ...manifest,
107
+ operations: manifest.operations.filter(offered).map(({ security, ...operation }) => ({
108
+ ...operation,
109
+ // The object specs mark 115 operations `security: []`, which reads as "no sign-in", and the generator's `auth`
110
+ // keeps it: such an operation went out without the token and HubSpot answered 401. Left out, the manifest's
111
+ // bearer token applies, as on every other operation.
112
+ ...(security?.length ? { security } : {}),
113
+ permissions: [
114
+ ...(operation.permissions ?? []),
115
+ ...keysOf(operation),
116
+ // cli-core takes a body key only when the manifest names it; the run checks it through `bodyPermissions`.
117
+ ...bodyKeysOf(operation).map(({ named }) => named),
118
+ ],
119
+ })),
120
+ },
121
+ service: SERVICE,
122
+ bodyPermissions,
123
+ };
124
+ /** The api key each CRM write command also checks, so `api.deals-update: deny` stops `deals update` too. */
125
+ const CRM_TYPES = { deal: "deals", person: "contacts", organization: "companies" };
126
+ export const crmApiKeys = (kind, verb) => {
127
+ const type = CRM_TYPES[kind];
128
+ // A delete shares `deals.delete` with `api deals-delete` already; `api.deals-delete` would ask a second time.
129
+ if (!type || verb === "remove")
130
+ return [];
131
+ return [`api.${type}-${verb === "create" ? "create" : "update"}`];
132
+ };
133
+ // Without `bodyPermissions`: the manifest names each of their keys already, with the same condition.
134
+ const api = serviceApiPermissions({ manifest: API.manifest });
135
+ /** `api list` and `api show` never reach the account. */
136
+ const keyOf = (path) => path[0] === "api" && ["list", "show"].includes(path[1] ?? "") ? null : commandPathKey(path);
137
+ /** The CRM commands' keys keep the commands' levels on the api path too: an ordinary change goes ahead. */
138
+ const COMMAND_DEFAULTS = Object.fromEntries([...new Set([...Object.values(COMMAND_KEYS).flat(), MOVES.named.key, CLOSES.named.key])].map((key) => [
139
+ key,
140
+ "allow",
141
+ ]));
142
+ export const permissions = commandPermissions({
143
+ keyOf,
144
+ extra: [...new Set([...api.extra, ...CRM_WRITE_KEYS])],
145
+ alsoChecks: (path) => [...api.alsoChecks(path), ...crmAlsoChecks(crmApiKeys)(path)],
146
+ defaults: {
147
+ ...api.defaults,
148
+ ...COMMAND_DEFAULTS,
149
+ ...CRM_WRITE_DEFAULTS,
150
+ },
151
+ });
152
+ export const configuration = settingsFor(IDENTITY, { permissions });
153
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,WAAW,GACZ,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,cAAc,EAEd,aAAa,GACd,MAAM,uBAAuB,CAAA;AAE9B,OAAO,KAAK,QAAQ,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,OAAO,EA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E,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC;IACnF,QAAQ,EAAE;QACR,GAAG,GAAG,CAAC,QAAQ;QACf,GAAG,gBAAgB;QACnB,GAAG,kBAAkB;KACtB;CACF,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,aAAa,GAAkB,WAAW,CAAC,QAAQ,EAAE,EAAE,WAAW,EAAE,CAAC,CAAA"}
@@ -0,0 +1,88 @@
1
+ import { type SleepLike } from "@wirecat/cli-base";
2
+ import { type BaseEnvironment, type EventSink, type GlobalFlags, ProfileTokenStore } from "@wirecat/cli-core/cli";
3
+ import { Pacer } from "@wirecat/cli-core/sends";
4
+ import type { Command } from "commander";
5
+ import { type HubSpotTransport, type HubSpotWriteTransport, PACE } from "./hubspot/transport.js";
6
+ export { PACE };
7
+ /** What a test hands `run()` in place of the account and real waiting (WC-ARCH-10). */
8
+ export interface HubspotEnvironment extends BaseEnvironment {
9
+ fetch?: typeof fetch;
10
+ sleep?: SleepLike;
11
+ /** The clock the pace waits on; a test moves it with its `sleep`, so no wait is real. */
12
+ now?: () => number;
13
+ }
14
+ /**
15
+ * Settings, keyring and the run come from cli-core's `baseContext` and nowhere else: a command that resolves settings
16
+ * on its own skips `--timeout`, the run record and the profile's permissions (WC-LIFE-5, WC-LIFE-15).
17
+ */
18
+ export declare const contextOf: (command: Command) => {
19
+ settings: import("@wirecat/cli-core/cli").Settings;
20
+ renderer: import("@wirecat/cli-base").Renderer;
21
+ format: import("@wirecat/cli-base").RenderFormat;
22
+ color: boolean;
23
+ streams: import("@wirecat/cli-base").Streams;
24
+ compact?: boolean;
25
+ full?: boolean;
26
+ env: NodeJS.ProcessEnv;
27
+ keyring?: import("@wirecat/cli-base").KeyringStore;
28
+ run: <T>(body: (events: EventSink) => Promise<T>, options?: {
29
+ unbounded?: boolean;
30
+ name?: string;
31
+ }) => Promise<T>;
32
+ track: (closeable: import("@wirecat/cli-core/cli").Closeable) => void;
33
+ environment: HubspotEnvironment;
34
+ flags: GlobalFlags;
35
+ profile: string;
36
+ tokens: ProfileTokenStore;
37
+ };
38
+ /**
39
+ * What a profile keeps: the token and the account `session start` proved it against. The account is the CRM's key in
40
+ * the shared store, so a read offline knows it without asking HubSpot.
41
+ */
42
+ export interface SignIn {
43
+ token: string;
44
+ portalId: string;
45
+ uiDomain: string | null;
46
+ /** The account's IANA time zone: HubSpot's daily allowance comes back at its midnight. */
47
+ timeZone?: string;
48
+ }
49
+ export declare const encodeSignIn: (signIn: SignIn) => string;
50
+ export declare const decodeSignIn: (secret: string) => SignIn;
51
+ export declare const ACCOUNT_VARIABLE: string;
52
+ /** The account `WCHUBSPOT_ACCOUNT` names, checked for its shape only; `undefined` when it is not set. */
53
+ export declare const environmentAccount: (env: NodeJS.ProcessEnv) => string | undefined;
54
+ /**
55
+ * `WCHUBSPOT_TOKEN` first, as for every token CLI, with `WCHUBSPOT_ACCOUNT` naming its account (the portal id), as
56
+ * amo's token needs `AMO_HOST`; then the saved sign-in. Read with no network: whether the two belong together is
57
+ * `refuseAccountMismatch`'s question. A token from the environment has no time zone, so its daily limit names the
58
+ * earliest UTC time it can be back.
59
+ */
60
+ export declare const storedSignIn: (command: Command) => SignIn | undefined;
61
+ /** Whether this run's key comes from `WCHUBSPOT_TOKEN`, whose account nobody has checked yet. */
62
+ export declare const fromEnvironment: (command: Command) => boolean;
63
+ /**
64
+ * A token's own account against the one `WCHUBSPOT_ACCOUNT` names. A wrong id would file this account's CRM under
65
+ * another's in the shared store, so a mismatch is refused before anything is written. Names both ids, never the token.
66
+ */
67
+ export declare const refuseAccountMismatch: (named: string, actual: string) => void;
68
+ /** The sign-in a request uses, or cli-core's `session.missing` refusal that names the command to sign in. */
69
+ export declare const signInOf: (command: Command) => SignIn;
70
+ /** The bare token, read before any request; the api group sends it as `Authorization: Bearer`. */
71
+ export declare const tokenOf: (command: Command) => string;
72
+ /**
73
+ * HubSpot counts its limits per account, so every process on one account waits in one queue, whichever profile or
74
+ * `WCHUBSPOT_TOKEN` it reads with. Only a first sign-in, whose account is not known yet, paces by the profile.
75
+ */
76
+ export declare const pacerFor: (command: Command, portalId: string | undefined) => Pacer;
77
+ /** The transport for one run, paced with the api group by the account. */
78
+ export declare const hubFor: (command: Command, { token, portalId, timeZone }: {
79
+ token: string;
80
+ portalId?: string;
81
+ timeZone?: string | undefined;
82
+ }, events?: EventSink) => HubSpotWriteTransport;
83
+ /**
84
+ * Every remote call goes through here: inside cli-core's run, so `--timeout` bounds it, a failure or `--record` keeps
85
+ * a run named after the command, and the transport's requests land in that run (WC-LIFE-5, WC-LIFE-15).
86
+ */
87
+ export declare const withHub: <T>(command: Command, body: (hub: HubSpotTransport) => Promise<T>) => Promise<T>;
88
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,EACL,KAAK,eAAe,EAEpB,KAAK,SAAS,EAEd,KAAK,WAAW,EAChB,iBAAiB,EAElB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,KAAK,EAAe,MAAM,yBAAyB,CAAA;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAE1B,IAAI,EAEL,MAAM,wBAAwB,CAAA;AAG/B,OAAO,EAAE,IAAI,EAAE,CAAA;AAEf,uFAAuF;AACvF,MAAM,WAAW,kBAAmB,SAAQ,eAAe;IACzD,KAAK,CAAC,EAAE,OAAO,KAAK,CAAA;IACpB,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,yFAAyF;IACzF,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;CACnB;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C,WAAW,OAAO,QAAQ,CAAC,GAAG,EAAE,gBAAgB,KAAK,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,CAGnG,CAAA"}
@@ -0,0 +1,137 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ import { baseContext, environmentOf, ProfileTokenStore, refuseOffline, } from "@wirecat/cli-core/cli";
3
+ import { Pacer, pacePathFor } from "@wirecat/cli-core/sends";
4
+ import { configuration } from "./config.js";
5
+ import { hubspotTransport, PACE, SERVICE, } from "./hubspot/transport.js";
6
+ import { IDENTITY } from "./identity.js";
7
+ export { PACE };
8
+ /**
9
+ * Settings, keyring and the run come from cli-core's `baseContext` and nowhere else: a command that resolves settings
10
+ * on its own skips `--timeout`, the run record and the profile's permissions (WC-LIFE-5, WC-LIFE-15).
11
+ */
12
+ export const contextOf = (command) => {
13
+ const base = baseContext(command, configuration.resolveSettings);
14
+ return {
15
+ ...base,
16
+ environment: environmentOf(command),
17
+ flags: command.optsWithGlobals(),
18
+ profile: base.settings.profile,
19
+ tokens: new ProfileTokenStore({
20
+ app: IDENTITY,
21
+ env: base.env,
22
+ ...(base.keyring ? { keyring: base.keyring } : {}),
23
+ warn: base.renderer.warn,
24
+ }),
25
+ };
26
+ };
27
+ export const encodeSignIn = (signIn) => JSON.stringify(signIn);
28
+ export const decodeSignIn = (secret) => {
29
+ let parsed;
30
+ try {
31
+ parsed = JSON.parse(secret);
32
+ }
33
+ catch {
34
+ parsed = undefined;
35
+ }
36
+ const found = typeof parsed === "object" && parsed !== null ? parsed : {};
37
+ if (typeof found.token !== "string" || !found.token || typeof found.portalId !== "string" || !found.portalId)
38
+ throw new CliError("configuration_error", "the saved sign-in is not one hubspot wrote; run session start again", {
39
+ reason: "session.unreadable",
40
+ });
41
+ return {
42
+ token: found.token,
43
+ portalId: found.portalId,
44
+ uiDomain: typeof found.uiDomain === "string" ? found.uiDomain : null,
45
+ // An entry written before the time zone was kept holds `utcOffsetMs`, which is ignored.
46
+ ...(typeof found.timeZone === "string" && found.timeZone ? { timeZone: found.timeZone } : {}),
47
+ };
48
+ };
49
+ export const ACCOUNT_VARIABLE = `${IDENTITY.envPrefix}_ACCOUNT`;
50
+ /** The account `WCHUBSPOT_ACCOUNT` names, checked for its shape only; `undefined` when it is not set. */
51
+ export const environmentAccount = (env) => {
52
+ const portalId = env[ACCOUNT_VARIABLE]?.trim();
53
+ if (!portalId)
54
+ return undefined;
55
+ if (!/^\d+$/.test(portalId))
56
+ throw new CliError("configuration_error", `${ACCOUNT_VARIABLE} must be the account's id, digits only`, {
57
+ reason: "account.id-malformed",
58
+ });
59
+ return portalId;
60
+ };
61
+ /**
62
+ * `WCHUBSPOT_TOKEN` first, as for every token CLI, with `WCHUBSPOT_ACCOUNT` naming its account (the portal id), as
63
+ * amo's token needs `AMO_HOST`; then the saved sign-in. Read with no network: whether the two belong together is
64
+ * `refuseAccountMismatch`'s question. A token from the environment has no time zone, so its daily limit names the
65
+ * earliest UTC time it can be back.
66
+ */
67
+ export const storedSignIn = (command) => {
68
+ const { env, profile, tokens } = contextOf(command);
69
+ const stored = tokens.read(profile);
70
+ if (!stored)
71
+ return undefined;
72
+ if (stored.source !== "environment")
73
+ return decodeSignIn(stored.secret);
74
+ const portalId = environmentAccount(env);
75
+ if (!portalId)
76
+ throw new CliError("configuration_error", `${tokens.variable} needs ${ACCOUNT_VARIABLE}, the HubSpot account's id: ` +
77
+ `\`${IDENTITY.command} ${profile} session status --check\` with the token alone prints it`, { reason: "account.id-missing" });
78
+ return { token: stored.secret, portalId, uiDomain: null };
79
+ };
80
+ /** Whether this run's key comes from `WCHUBSPOT_TOKEN`, whose account nobody has checked yet. */
81
+ export const fromEnvironment = (command) => {
82
+ const { profile, tokens } = contextOf(command);
83
+ return tokens.read(profile)?.source === "environment";
84
+ };
85
+ /**
86
+ * A token's own account against the one `WCHUBSPOT_ACCOUNT` names. A wrong id would file this account's CRM under
87
+ * another's in the shared store, so a mismatch is refused before anything is written. Names both ids, never the token.
88
+ */
89
+ export const refuseAccountMismatch = (named, actual) => {
90
+ if (named === actual)
91
+ return;
92
+ throw new CliError("configuration_error", `${ACCOUNT_VARIABLE} names account ${named}, but the token belongs to account ${actual}: ` +
93
+ `set ${ACCOUNT_VARIABLE}=${actual}`, { reason: "account.mismatch", account: actual });
94
+ };
95
+ /** The sign-in a request uses, or cli-core's `session.missing` refusal that names the command to sign in. */
96
+ export const signInOf = (command) => {
97
+ refuseOffline(command, SERVICE);
98
+ const found = storedSignIn(command);
99
+ if (found)
100
+ return found;
101
+ const { profile, tokens } = contextOf(command);
102
+ tokens.need(profile);
103
+ throw new CliError("authentication_error", `profile ${profile} is not signed in`);
104
+ };
105
+ /** The bare token, read before any request; the api group sends it as `Authorization: Bearer`. */
106
+ export const tokenOf = (command) => signInOf(command).token;
107
+ /**
108
+ * HubSpot counts its limits per account, so every process on one account waits in one queue, whichever profile or
109
+ * `WCHUBSPOT_TOKEN` it reads with. Only a first sign-in, whose account is not known yet, paces by the profile.
110
+ */
111
+ export const pacerFor = (command, portalId) => {
112
+ const { environment, env, profile } = contextOf(command);
113
+ return new Pacer(pacePathFor(IDENTITY, portalId ? `account-${portalId}` : profile, env), PACE, environment.now);
114
+ };
115
+ /** The transport for one run, paced with the api group by the account. */
116
+ export const hubFor = (command, { token, portalId, timeZone }, events) => {
117
+ const { environment } = contextOf(command);
118
+ return hubspotTransport({
119
+ token,
120
+ ...(timeZone ? { timeZone } : {}),
121
+ ...(events ? { events } : {}),
122
+ signal: environment.signal,
123
+ ...(environment.fetch ? { fetch: environment.fetch } : {}),
124
+ ...(environment.sleep ? { sleep: environment.sleep } : {}),
125
+ ...(environment.now ? { now: environment.now } : {}),
126
+ pacer: pacerFor(command, portalId),
127
+ });
128
+ };
129
+ /**
130
+ * Every remote call goes through here: inside cli-core's run, so `--timeout` bounds it, a failure or `--record` keeps
131
+ * a run named after the command, and the transport's requests land in that run (WC-LIFE-5, WC-LIFE-15).
132
+ */
133
+ export const withHub = (command, body) => {
134
+ const signIn = signInOf(command);
135
+ return contextOf(command).run((events) => body(hubFor(command, signIn, events)));
136
+ };
137
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAkB,MAAM,mBAAmB,CAAA;AAC5D,OAAO,EAEL,WAAW,EAEX,aAAa,EAEb,iBAAiB,EACjB,aAAa,GACd,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAGL,gBAAgB,EAChB,IAAI,EACJ,OAAO,GACR,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,OAAO,EAAE,IAAI,EAAE,CAAA;AAUf;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,OAAgB,EAAE,EAAE;IAC5C,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,EAAE,aAAa,CAAC,eAAe,CAAC,CAAA;IAChE,OAAO;QACL,GAAG,IAAI;QACP,WAAW,EAAE,aAAa,CAAqB,OAAO,CAAC;QACvD,KAAK,EAAE,OAAO,CAAC,eAAe,EAAe;QAC7C,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO;QAC9B,MAAM,EAAE,IAAI,iBAAiB,CAAC;YAC5B,GAAG,EAAE,QAAQ;YACb,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAClD,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI;SACzB,CAAC;KACH,CAAA;AACH,CAAC,CAAA;AAcD,MAAM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A;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAI,OAAgB,EAAE,IAA2C,EAAc,EAAE;IACtG,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAA;IAChC,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAA;AAClF,CAAC,CAAA"}
@@ -0,0 +1,24 @@
1
+ import type { CrmActivity } from "@wirecat/cli-core";
2
+ import { type CrmCallContext, type CrmPage } from "@wirecat/cli-core/crm";
3
+ import type { HubSpotTransport } from "../hubspot/transport.js";
4
+ /**
5
+ * A call not over yet, of the `hs_call_status` values BUSY, CALLING_CRM_USER, CANCELED, COMPLETED, CONNECTING, FAILED,
6
+ * IN_PROGRESS, NO_ANSWER, QUEUED and RINGING (https://developers.hubspot.com/docs/api-reference/latest/crm/activities/calls/guide).
7
+ */
8
+ export declare const CALL_OPEN: string[];
9
+ export interface ActivityPages {
10
+ /** Rows a search asks per page (search guide: 200 at most). */
11
+ search: number;
12
+ /** Rows a list asks per page (100 at most). */
13
+ list: number;
14
+ }
15
+ /**
16
+ * One page of one engagement type changed since `since`, the types one after another: notes, calls, e-mails,
17
+ * meetings, tasks. A type the token's scopes refuse is a gap naming the scope, and the next type is read.
18
+ *
19
+ * More than the search cap changed at one moment (an import): the type is then listed in full, newest change or not,
20
+ * and only rows changed since the window's start are kept. Not a stop: cli-core resumes a failed activity round
21
+ * from the page it saved, which would hit the same cap on every run.
22
+ */
23
+ export declare const readActivities: (caller: HubSpotTransport, since: number, page: string | undefined, context: CrmCallContext | undefined, now: () => number, sizes?: ActivityPages) => Promise<CrmPage<CrmActivity>>;
24
+ //# sourceMappingURL=activities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"activities.d.ts","sourceRoot":"","sources":["../../src/crm/activities.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAA4B,MAAM,mBAAmB,CAAA;AAC9E,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,OAAO,EAAa,MAAM,uBAAuB,CAAA;AACpF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAyB/D;;;GAGG;AACH,eAAO,MAAM,SAAS,UAAyE,CAAA;AAuI/F,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,MAAM,EAAE,MAAM,CAAA;IACd,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,gBAAgB,SACjB,MAAM,QACP,MAAM,GAAG,SAAS,WACf,cAAc,GAAG,SAAS,OAC9B,MAAM,MAAM,UACV,aAAa,KACnB,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,CA6D9B,CAAA"}
@@ -0,0 +1,214 @@
1
+ import { isCliError } from "@wirecat/cli-base";
2
+ import { crmValues } from "@wirecat/cli-core/crm";
3
+ import { associationsOf } from "./associations.js";
4
+ import { msOf, nextOf, propertiesOf, resultsOf } from "./records.js";
5
+ import { modifiedSince, nextSpot, SEARCH_PAGE, search } from "./search.js";
6
+ const { id, text } = crmValues;
7
+ const OVERLAP_MS = 10 * 60_000;
8
+ /**
9
+ * A call not over yet, of the `hs_call_status` values BUSY, CALLING_CRM_USER, CANCELED, COMPLETED, CONNECTING, FAILED,
10
+ * IN_PROGRESS, NO_ANSWER, QUEUED and RINGING (https://developers.hubspot.com/docs/api-reference/latest/crm/activities/calls/guide).
11
+ */
12
+ export const CALL_OPEN = ["CALLING_CRM_USER", "CONNECTING", "IN_PROGRESS", "QUEUED", "RINGING"];
13
+ /**
14
+ * An e-mail not sent yet, of BOUNCED, FAILED, SCHEDULED, SENDING and SENT
15
+ * (https://developers.hubspot.com/docs/api-reference/latest/crm/activities/emails/guide).
16
+ */
17
+ const EMAIL_OPEN = ["SCHEDULED", "SENDING"];
18
+ const COMMON = ["hubspot_owner_id", "hs_timestamp", "hs_createdate", "hs_lastmodifieddate"];
19
+ /** HTML as plain text: notes, meetings and tasks keep rich text. */
20
+ const plain = (value) => {
21
+ const html = text(value);
22
+ if (html === null)
23
+ return null;
24
+ return text(html
25
+ .replace(/<br\s*\/?>|<\/p>|<\/div>|<\/li>/gi, "\n")
26
+ .replace(/<[^>]*>/g, "")
27
+ .replace(/&nbsp;/g, " ")
28
+ .replace(/&lt;/g, "<")
29
+ .replace(/&gt;/g, ">")
30
+ .replace(/&quot;/g, '"')
31
+ .replace(/&#39;/g, "'")
32
+ .replace(/&amp;/g, "&"));
33
+ };
34
+ // HubSpot has no notes, calls, meetings or tasks scope; a key with the contacts scope reads them (account 149800303,
35
+ // 2026-11-17). Emails ask for sales-email-read.
36
+ const CONTACTS_SCOPE = "crm.objects.contacts.read";
37
+ const ENGAGEMENTS = [
38
+ {
39
+ type: "notes",
40
+ kind: "comment",
41
+ scope: CONTACTS_SCOPE,
42
+ properties: ["hs_note_body", ...COMMON],
43
+ subject: () => "",
44
+ body: (properties) => plain(properties.hs_note_body),
45
+ dueAt: () => null,
46
+ done: () => true,
47
+ },
48
+ {
49
+ type: "calls",
50
+ kind: "call",
51
+ scope: CONTACTS_SCOPE,
52
+ properties: ["hs_call_title", "hs_call_direction", "hs_call_status", ...COMMON],
53
+ subject: (properties) => text(properties.hs_call_title) ??
54
+ (text(properties.hs_call_direction) === "INBOUND" ? "incoming call" : "outgoing call"),
55
+ body: () => null,
56
+ dueAt: () => null,
57
+ done: (properties) => !CALL_OPEN.includes(text(properties.hs_call_status) ?? ""),
58
+ },
59
+ {
60
+ type: "emails",
61
+ kind: "email",
62
+ scope: "sales-email-read",
63
+ properties: ["hs_email_subject", "hs_email_text", "hs_email_direction", "hs_email_status", ...COMMON],
64
+ subject: (properties) => text(properties.hs_email_subject),
65
+ body: (properties) => text(properties.hs_email_text),
66
+ dueAt: () => null,
67
+ done: (properties) => !EMAIL_OPEN.includes(text(properties.hs_email_status) ?? ""),
68
+ },
69
+ {
70
+ type: "meetings",
71
+ kind: "meeting",
72
+ scope: CONTACTS_SCOPE,
73
+ properties: ["hs_meeting_title", "hs_meeting_body", "hs_meeting_start_time", "hs_meeting_outcome", ...COMMON],
74
+ subject: (properties) => text(properties.hs_meeting_title),
75
+ body: (properties) => plain(properties.hs_meeting_body),
76
+ dueAt: (properties) => msOf(properties.hs_meeting_start_time),
77
+ // "scheduled, completed, rescheduled, no show, and canceled", in prose and lower case
78
+ // (https://developers.hubspot.com/docs/api-reference/latest/crm/activities/meetings/guide).
79
+ done: (properties) => text(properties.hs_meeting_outcome)?.toUpperCase() === "COMPLETED",
80
+ },
81
+ {
82
+ type: "tasks",
83
+ kind: "task",
84
+ scope: CONTACTS_SCOPE,
85
+ properties: ["hs_task_subject", "hs_task_body", "hs_task_status", ...COMMON],
86
+ subject: (properties) => text(properties.hs_task_subject),
87
+ body: (properties) => plain(properties.hs_task_body),
88
+ dueAt: (properties) => msOf(properties.hs_timestamp),
89
+ // COMPLETED or NOT_STARTED (https://developers.hubspot.com/docs/api-reference/latest/crm/activities/tasks/guide).
90
+ done: (properties) => text(properties.hs_task_status) === "COMPLETED",
91
+ },
92
+ ];
93
+ /** What an engagement is on, by the associations API's names; a list the token may not read leaves its kind out. */
94
+ const ON = [
95
+ ["deals", "deal"],
96
+ ["leads", "lead"],
97
+ ["contacts", "person"],
98
+ ["companies", "organization"],
99
+ ];
100
+ const toActivity = (engagement, raw, on) => {
101
+ const externalId = id(raw.id);
102
+ const properties = propertiesOf(raw);
103
+ const updatedAt = msOf(properties.hs_lastmodifieddate) ?? msOf(raw.updatedAt);
104
+ if (!externalId || updatedAt === null)
105
+ return null;
106
+ return {
107
+ // One id space for every kind: a note and a call may share a number.
108
+ externalId: `${engagement.type}:${externalId}`,
109
+ kind: engagement.kind,
110
+ subject: engagement.subject(properties) ?? "",
111
+ body: engagement.body(properties),
112
+ dueAt: engagement.dueAt(properties),
113
+ done: engagement.done(properties),
114
+ assignee: id(properties.hubspot_owner_id),
115
+ createdAt: msOf(properties.hs_createdate) ?? msOf(raw.createdAt) ?? updatedAt,
116
+ updatedAt,
117
+ on,
118
+ };
119
+ };
120
+ /**
121
+ * `<engagement index>.<from>.<offset>@<round start>` reads by search, keyset by modified time as the record changes
122
+ * are; `<engagement index>.<from>.L<after>@<round start>` lists the type in full, for a moment past the search cap.
123
+ * HubSpot's `after` is opaque, so the round's start is read after the last `@`.
124
+ */
125
+ const pageOf = (page, since, now) => {
126
+ const fresh = { at: 0, spot: { from: since - OVERLAP_MS, offset: 0 }, after: undefined, started: now };
127
+ const cut = (page ?? "").lastIndexOf("@");
128
+ const started = Number(page?.slice(cut + 1));
129
+ const found = /^(\d+)\.(-?\d+)\.(?:(\d+)|L(.*))$/s.exec(page?.slice(0, cut) ?? "");
130
+ if (cut < 0 || !found || !Number.isSafeInteger(started))
131
+ return fresh;
132
+ return {
133
+ at: Number(found[1]),
134
+ spot: { from: Number(found[2]), offset: Number(found[3] ?? 0) },
135
+ after: found[4] === undefined ? undefined : found[4] || "",
136
+ started,
137
+ };
138
+ };
139
+ /**
140
+ * One page of one engagement type changed since `since`, the types one after another: notes, calls, e-mails,
141
+ * meetings, tasks. A type the token's scopes refuse is a gap naming the scope, and the next type is read.
142
+ *
143
+ * More than the search cap changed at one moment (an import): the type is then listed in full, newest change or not,
144
+ * and only rows changed since the window's start are kept. Not a stop: cli-core resumes a failed activity round
145
+ * from the page it saved, which would hit the same cap on every run.
146
+ */
147
+ export const readActivities = async (caller, since, page, context, now, sizes = { search: SEARCH_PAGE, list: 100 }) => {
148
+ const { at, spot, after, started } = pageOf(page, since, now());
149
+ const engagement = ENGAGEMENTS[at];
150
+ const last = { hasMore: false, cursor: String(started) };
151
+ if (!engagement)
152
+ return { items: [], ...last };
153
+ const following = (items, next) => {
154
+ if (next)
155
+ return { items, hasMore: true, nextCursor: `${at}.${next}@${started}` };
156
+ if (at + 1 >= ENGAGEMENTS.length)
157
+ return { items, ...last };
158
+ return { items, hasMore: true, nextCursor: `${at + 1}.${since - OVERLAP_MS}.0@${started}` };
159
+ };
160
+ let rows;
161
+ let more;
162
+ let listedNext = null;
163
+ try {
164
+ if (after === undefined)
165
+ ({ rows, more } = await search(caller.post, engagement.type, modifiedSince("hs_lastmodifieddate", spot, sizes.search, engagement.properties)));
166
+ else {
167
+ const body = await caller.get(`/crm/objects/2026-03/${engagement.type}`, {
168
+ limit: sizes.list,
169
+ properties: engagement.properties.join(","),
170
+ archived: false,
171
+ ...(after ? { after } : {}),
172
+ });
173
+ listedNext = nextOf(body);
174
+ more = listedNext !== null;
175
+ rows = resultsOf(body).filter((raw) => {
176
+ const changed = msOf(propertiesOf(raw).hs_lastmodifieddate) ?? msOf(raw.updatedAt);
177
+ return changed === null || changed >= spot.from;
178
+ });
179
+ }
180
+ }
181
+ catch (error) {
182
+ if (!isCliError(error) || error.code !== "permission_error")
183
+ throw error;
184
+ context?.gap({
185
+ kind: "activities",
186
+ scope: engagement.scope,
187
+ message: `HubSpot refused the ${engagement.type} (${engagement.scope} scope missing); the other activities are read`,
188
+ });
189
+ return following([]);
190
+ }
191
+ const ids = rows.flatMap((raw) => id(raw.id) ?? []);
192
+ const on = new Map();
193
+ for (const [to, kind] of ON) {
194
+ let found;
195
+ try {
196
+ found = await associationsOf(caller.post, engagement.type, to, ids);
197
+ }
198
+ catch (error) {
199
+ if (isCliError(error) && error.code === "permission_error")
200
+ continue;
201
+ throw error;
202
+ }
203
+ for (const [owner, linked] of found)
204
+ on.set(owner, [...(on.get(owner) ?? []), ...linked.map((one) => ({ kind, externalId: one.id }))]);
205
+ }
206
+ const items = rows.flatMap((raw) => toActivity(engagement, raw, on.get(String(raw.id)) ?? []) ?? []);
207
+ if (!more)
208
+ return following(items);
209
+ if (after !== undefined)
210
+ return following(items, `${spot.from}.L${listedNext}`);
211
+ const next = nextSpot(spot, rows, "hs_lastmodifieddate", sizes.search);
212
+ return following(items, next ? `${next.from}.${next.offset}` : `${spot.from}.L`);
213
+ };
214
+ //# sourceMappingURL=activities.js.map