@wirecat/cli-core 0.617.0 → 0.619.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. package/CHANGELOG.md +406 -1
  2. package/README.md +1 -1
  3. package/THIRD_PARTY_NOTICES +3 -0
  4. package/dist/background/backfill.d.ts +21 -0
  5. package/dist/background/backfill.js +41 -0
  6. package/dist/background/daily-archive-timer.d.ts +1 -1
  7. package/dist/background/deliver.d.ts +5 -2
  8. package/dist/background/deliver.js +30 -12
  9. package/dist/background/report/messages.js +4 -1
  10. package/dist/background/report/render.d.ts +3 -1
  11. package/dist/background/report/render.js +3 -1
  12. package/dist/background/report/view.js +4 -1
  13. package/dist/background/schedules.d.ts +17 -0
  14. package/dist/background/schedules.js +21 -8
  15. package/dist/calendar/cli/answers.d.ts +1 -0
  16. package/dist/calendar/cli/answers.js +6 -1
  17. package/dist/calendar/cli/index.js +2 -14
  18. package/dist/calendar/cli/people-link.d.ts +3 -0
  19. package/dist/calendar/cli/people-link.js +23 -0
  20. package/dist/calendar/index.d.ts +1 -1
  21. package/dist/calendar/index.js +1 -1
  22. package/dist/calendar/mcp/index.d.ts +12 -12
  23. package/dist/cli/base-stages-command.d.ts +22 -0
  24. package/dist/cli/base-stages-command.js +174 -0
  25. package/dist/cli/command-runs.d.ts +2 -0
  26. package/dist/cli/command-runs.js +3 -1
  27. package/dist/cli/config-command.js +6 -1
  28. package/dist/cli/context.d.ts +5 -0
  29. package/dist/cli/context.js +1 -0
  30. package/dist/cli/group-lock.d.ts +42 -0
  31. package/dist/cli/group-lock.js +130 -0
  32. package/dist/cli/index.d.ts +10 -4
  33. package/dist/cli/index.js +10 -4
  34. package/dist/cli/messenger/backfill-command.js +27 -15
  35. package/dist/cli/messenger/contacts-command.js +10 -3
  36. package/dist/cli/messenger/context.js +1 -0
  37. package/dist/cli/messenger/doctor-command.js +3 -1
  38. package/dist/cli/messenger/models-command.js +1 -1
  39. package/dist/cli/messenger/serve-command.js +24 -2
  40. package/dist/cli/messenger/serve-outdated.d.ts +22 -0
  41. package/dist/cli/messenger/serve-outdated.js +100 -0
  42. package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
  43. package/dist/cli/messenger/store-maintenance-command.js +9 -4
  44. package/dist/cli/model-catalogue-command.d.ts +3 -1
  45. package/dist/cli/model-catalogue-command.js +46 -2
  46. package/dist/cli/profile.d.ts +8 -0
  47. package/dist/cli/profile.js +25 -0
  48. package/dist/cli/program.js +2 -0
  49. package/dist/cli/runs/search-query.d.ts +1 -1
  50. package/dist/cli/runs/search-query.js +4 -0
  51. package/dist/cli/runs/search.d.ts +5 -1
  52. package/dist/cli/runs/search.js +12 -1
  53. package/dist/cli/schedule/schedule-command.d.ts +9 -0
  54. package/dist/cli/schedule/schedule-command.js +13 -0
  55. package/dist/cli/search-left-out.d.ts +6 -1
  56. package/dist/cli/search-left-out.js +9 -4
  57. package/dist/cli/settings.d.ts +8 -0
  58. package/dist/cli/settings.js +17 -2
  59. package/dist/cli/shared-command-exports.d.ts +16 -0
  60. package/dist/cli/store-recover.d.ts +3 -3
  61. package/dist/cli/store-recover.js +47 -23
  62. package/dist/cli/store-settings.d.ts +2 -0
  63. package/dist/cli/store-settings.js +37 -9
  64. package/dist/crm/cli/index.js +38 -6
  65. package/dist/crm/group-hint.d.ts +8 -0
  66. package/dist/crm/group-hint.js +13 -0
  67. package/dist/crm/source.d.ts +12 -1
  68. package/dist/crm/sync.d.ts +31 -2
  69. package/dist/crm/sync.js +110 -3
  70. package/dist/crm/testing/acme-scenario.d.ts +40 -0
  71. package/dist/crm/testing/acme-scenario.js +170 -0
  72. package/dist/crm/testing/index.d.ts +1 -0
  73. package/dist/crm/testing/index.js +1 -0
  74. package/dist/crm/testing/source-contract.js +12 -0
  75. package/dist/crm/testing/source.d.ts +2 -0
  76. package/dist/crm/testing/source.js +1 -0
  77. package/dist/domain/addresses.d.ts +13 -0
  78. package/dist/domain/addresses.js +63 -0
  79. package/dist/domain/categories.d.ts +3 -0
  80. package/dist/domain/categories.js +2 -0
  81. package/dist/domain/crm.d.ts +23 -0
  82. package/dist/domain/crm.js +7 -0
  83. package/dist/domain/links.d.ts +17 -4
  84. package/dist/domain/links.js +20 -7
  85. package/dist/domain/mentions.d.ts +2 -12
  86. package/dist/domain/mentions.js +2 -19
  87. package/dist/domain/orders.d.ts +8 -1
  88. package/dist/domain/orders.js +6 -2
  89. package/dist/domain/phones.d.ts +26 -0
  90. package/dist/domain/phones.js +48 -0
  91. package/dist/domain/record-types.d.ts +1 -1
  92. package/dist/domain/record-types.js +1 -0
  93. package/dist/domain/records.d.ts +5 -0
  94. package/dist/domain/ref-summaries.d.ts +39 -0
  95. package/dist/domain/ref-summaries.js +51 -3
  96. package/dist/domain/relations.d.ts +27 -2
  97. package/dist/domain/relations.js +37 -3
  98. package/dist/domain/vcard.d.ts +2 -2
  99. package/dist/domain/vcard.js +4 -10
  100. package/dist/embeddings/models.d.ts +11 -3
  101. package/dist/embeddings/models.js +28 -4
  102. package/dist/entities/business-context.d.ts +149 -0
  103. package/dist/entities/business-context.js +192 -0
  104. package/dist/entities/contact-activity.d.ts +2 -0
  105. package/dist/entities/contact-activity.js +7 -1
  106. package/dist/entities/contact-context.d.ts +157 -0
  107. package/dist/entities/contact-context.js +2 -0
  108. package/dist/entities/deal.d.ts +10 -0
  109. package/dist/entities/deal.js +2 -0
  110. package/dist/entities/index.d.ts +23 -3
  111. package/dist/entities/index.js +5 -0
  112. package/dist/entities/mention.d.ts +4 -3
  113. package/dist/entities/mention.js +2 -1
  114. package/dist/entities/message-transfer.d.ts +4 -2
  115. package/dist/entities/message-transfer.js +2 -2
  116. package/dist/entities/model.d.ts +2 -2
  117. package/dist/entities/model.js +1 -1
  118. package/dist/entities/relationship.d.ts +4 -0
  119. package/dist/entities/relationship.js +4 -0
  120. package/dist/errors/reasons.js +44 -4
  121. package/dist/fetch/page.js +2 -1
  122. package/dist/index.d.ts +4 -3
  123. package/dist/index.js +4 -3
  124. package/dist/meetings/context-store.d.ts +2 -0
  125. package/dist/meetings/findings-rules.d.ts +4 -0
  126. package/dist/meetings/findings-rules.js +5 -2
  127. package/dist/meetings/mcp/index.d.ts +15 -15
  128. package/dist/meetings/result-items.d.ts +1 -0
  129. package/dist/meetings/result-items.js +1 -0
  130. package/dist/models/index.js +22 -5
  131. package/dist/runs/events.d.ts +11 -1
  132. package/dist/runs/events.js +3 -1
  133. package/dist/runs/log-attributes.js +4 -0
  134. package/dist/runs/log-events.js +2 -0
  135. package/dist/runs/recording.d.ts +3 -0
  136. package/dist/runs/recording.js +36 -12
  137. package/dist/runs/run.d.ts +12 -3
  138. package/dist/runs/run.js +32 -13
  139. package/dist/search/account-list.d.ts +6 -0
  140. package/dist/search/account-list.js +9 -3
  141. package/dist/search/groups.d.ts +68 -0
  142. package/dist/search/groups.js +172 -0
  143. package/dist/services/account-groups.d.ts +28 -0
  144. package/dist/services/account-groups.js +89 -0
  145. package/dist/services/archive.d.ts +7 -1
  146. package/dist/services/archive.js +19 -3
  147. package/dist/services/business-context.d.ts +113 -0
  148. package/dist/services/business-context.js +156 -0
  149. package/dist/services/communications.d.ts +50 -0
  150. package/dist/services/communications.js +140 -0
  151. package/dist/services/deal-context.d.ts +90 -0
  152. package/dist/services/deal-context.js +131 -0
  153. package/dist/services/deps.d.ts +2 -0
  154. package/dist/services/enrichment.d.ts +7 -2
  155. package/dist/services/enrichment.js +46 -5
  156. package/dist/services/events-search.js +2 -1
  157. package/dist/services/group-guard.d.ts +28 -0
  158. package/dist/services/group-guard.js +131 -0
  159. package/dist/services/index.d.ts +15 -2
  160. package/dist/services/index.js +9 -2
  161. package/dist/services/ingest.d.ts +12 -0
  162. package/dist/services/ingest.js +4 -0
  163. package/dist/services/link-scorers/chat-senders.js +39 -29
  164. package/dist/services/link-scorers/member-chats.js +4 -3
  165. package/dist/services/link-scorers/member-dialog.js +5 -7
  166. package/dist/services/link-scorers/scan.js +17 -8
  167. package/dist/services/link-scorers/shared.d.ts +6 -2
  168. package/dist/services/link-scorers/shared.js +6 -2
  169. package/dist/services/link-scorers/types.d.ts +2 -0
  170. package/dist/services/meaning-model.d.ts +7 -0
  171. package/dist/services/meaning-model.js +20 -0
  172. package/dist/services/meeting-context.d.ts +6 -1
  173. package/dist/services/meeting-context.js +12 -5
  174. package/dist/services/message-resolution.js +2 -3
  175. package/dist/services/messages-meaning.js +4 -5
  176. package/dist/services/messages-search.d.ts +5 -1
  177. package/dist/services/messages-search.js +1 -1
  178. package/dist/services/model-downloads.d.ts +1 -1
  179. package/dist/services/model-downloads.js +6 -4
  180. package/dist/services/money-context.d.ts +73 -0
  181. package/dist/services/money-context.js +77 -0
  182. package/dist/services/notes-search.d.ts +4 -1
  183. package/dist/services/notes-search.js +19 -7
  184. package/dist/services/organization-context.d.ts +80 -0
  185. package/dist/services/organization-context.js +154 -0
  186. package/dist/services/people.d.ts +22 -6
  187. package/dist/services/people.js +38 -8
  188. package/dist/services/person-business.d.ts +43 -0
  189. package/dist/services/person-business.js +67 -0
  190. package/dist/services/person-context.d.ts +9 -1
  191. package/dist/services/person-context.js +5 -3
  192. package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
  193. package/dist/services/proposals/kinds/identity-link.js +48 -0
  194. package/dist/services/proposals/kinds/orders.js +1 -1
  195. package/dist/services/proposals/registry.js +2 -0
  196. package/dist/services/search-kind.d.ts +12 -5
  197. package/dist/services/search-kind.js +49 -10
  198. package/dist/services/text-model-choice.js +1 -1
  199. package/dist/store/accounts.d.ts +18 -0
  200. package/dist/store/accounts.js +18 -3
  201. package/dist/store/damage.d.ts +43 -10
  202. package/dist/store/damage.js +119 -36
  203. package/dist/store/decisions.js +3 -1
  204. package/dist/store/evidence-findings.d.ts +14 -0
  205. package/dist/store/evidence-findings.js +2 -0
  206. package/dist/store/exports.d.ts +2 -1
  207. package/dist/store/identity-links.d.ts +92 -0
  208. package/dist/store/identity-links.js +6 -0
  209. package/dist/store/index.d.ts +11 -5
  210. package/dist/store/index.js +5 -2
  211. package/dist/store/journal.d.ts +10 -0
  212. package/dist/store/journal.js +33 -0
  213. package/dist/store/marks.d.ts +19 -1
  214. package/dist/store/marks.js +23 -1
  215. package/dist/store/migrations.d.ts +1 -1
  216. package/dist/store/migrations.js +35 -12
  217. package/dist/store/people.d.ts +22 -0
  218. package/dist/store/people.js +15 -1
  219. package/dist/store/recover.d.ts +34 -0
  220. package/dist/store/recover.js +188 -6
  221. package/dist/store/refill.d.ts +28 -0
  222. package/dist/store/refill.js +120 -0
  223. package/dist/store/sqlite/accounts.js +2 -0
  224. package/dist/store/sqlite/base-stages.d.ts +122 -0
  225. package/dist/store/sqlite/base-stages.js +443 -0
  226. package/dist/store/sqlite/base-suggest.d.ts +13 -0
  227. package/dist/store/sqlite/base-suggest.js +62 -0
  228. package/dist/store/sqlite/business-contracts.js +10 -1
  229. package/dist/store/sqlite/clients.d.ts +7 -3
  230. package/dist/store/sqlite/clients.js +11 -7
  231. package/dist/store/sqlite/crm-settings.d.ts +12 -2
  232. package/dist/store/sqlite/crm-settings.js +56 -13
  233. package/dist/store/sqlite/crm.d.ts +13 -1
  234. package/dist/store/sqlite/crm.js +20 -4
  235. package/dist/store/sqlite/deals.d.ts +33 -0
  236. package/dist/store/sqlite/deals.js +42 -4
  237. package/dist/store/sqlite/decision-requests.js +7 -3
  238. package/dist/store/sqlite/email-index.js +17 -15
  239. package/dist/store/sqlite/evidence-findings.js +29 -5
  240. package/dist/store/sqlite/findings-core.js +4 -2
  241. package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
  242. package/dist/store/sqlite/identity-link-rows.js +146 -0
  243. package/dist/store/sqlite/identity-proposals.d.ts +53 -0
  244. package/dist/store/sqlite/identity-proposals.js +234 -0
  245. package/dist/store/sqlite/knowledge.js +5 -3
  246. package/dist/store/sqlite/link-kinds.d.ts +1 -0
  247. package/dist/store/sqlite/link-kinds.js +1 -0
  248. package/dist/store/sqlite/link-row.d.ts +6 -0
  249. package/dist/store/sqlite/link-row.js +35 -0
  250. package/dist/store/sqlite/link-source.d.ts +10 -0
  251. package/dist/store/sqlite/link-source.js +16 -0
  252. package/dist/store/sqlite/manifest.js +1 -0
  253. package/dist/store/sqlite/meeting-context-links.js +9 -6
  254. package/dist/store/sqlite/memories.js +9 -7
  255. package/dist/store/sqlite/mentions.d.ts +4 -1
  256. package/dist/store/sqlite/mentions.js +14 -9
  257. package/dist/store/sqlite/migrations.generated.js +8 -0
  258. package/dist/store/sqlite/note-index.js +9 -7
  259. package/dist/store/sqlite/notes.d.ts +2 -3
  260. package/dist/store/sqlite/notes.js +6 -29
  261. package/dist/store/sqlite/open-store.js +3 -0
  262. package/dist/store/sqlite/open.js +11 -2
  263. package/dist/store/sqlite/orders.d.ts +2 -0
  264. package/dist/store/sqlite/orders.js +9 -3
  265. package/dist/store/sqlite/organization-categories.d.ts +18 -0
  266. package/dist/store/sqlite/organization-categories.js +34 -0
  267. package/dist/store/sqlite/organizations.d.ts +5 -1
  268. package/dist/store/sqlite/organizations.js +14 -4
  269. package/dist/store/sqlite/person-links.d.ts +19 -4
  270. package/dist/store/sqlite/person-links.js +49 -11
  271. package/dist/store/sqlite/person-match-scan.d.ts +13 -0
  272. package/dist/store/sqlite/person-match-scan.js +251 -0
  273. package/dist/store/sqlite/person-matcher.d.ts +16 -0
  274. package/dist/store/sqlite/person-matcher.js +32 -0
  275. package/dist/store/sqlite/proposed-actions.js +6 -0
  276. package/dist/store/sqlite/record-matching.d.ts +2 -8
  277. package/dist/store/sqlite/record-matching.js +8 -40
  278. package/dist/store/sqlite/record-projectors-phone.js +2 -1
  279. package/dist/store/sqlite/record-relationships.d.ts +52 -0
  280. package/dist/store/sqlite/record-relationships.js +243 -0
  281. package/dist/store/sqlite/record-run.d.ts +5 -0
  282. package/dist/store/sqlite/record-run.js +62 -2
  283. package/dist/store/sqlite/record-writes.d.ts +1 -1
  284. package/dist/store/sqlite/record-writes.js +4 -2
  285. package/dist/store/sqlite/records.js +27 -2
  286. package/dist/store/sqlite/ref-accounts.d.ts +13 -0
  287. package/dist/store/sqlite/ref-accounts.js +117 -0
  288. package/dist/store/sqlite/ref-summaries.js +5 -1
  289. package/dist/store/sqlite/relation-list.d.ts +4 -1
  290. package/dist/store/sqlite/relation-list.js +20 -2
  291. package/dist/store/sqlite/relation-proposals.d.ts +18 -1
  292. package/dist/store/sqlite/relation-proposals.js +53 -11
  293. package/dist/store/sqlite/relation-roles.js +12 -6
  294. package/dist/store/sqlite/reported-relations.d.ts +11 -1
  295. package/dist/store/sqlite/reported-relations.js +59 -18
  296. package/dist/store/sqlite/schema.d.ts +32 -0
  297. package/dist/store/sqlite/schema.js +3 -0
  298. package/dist/store/sqlite/transaction.d.ts +13 -0
  299. package/dist/store/sqlite/transaction.js +33 -15
  300. package/dist/store/store.d.ts +3 -0
  301. package/dist/store/store.js +5 -0
  302. package/dist/store/write-events.d.ts +18 -0
  303. package/dist/store/write-events.js +88 -0
  304. package/package.json +3 -3
  305. package/parity.json +11 -4
  306. package/schemas/records-envelope.v1.schema.json +3 -0
@@ -1,6 +1,8 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { CliError } from "@wirecat/cli-base";
3
3
  import { sqliteCode } from "../../errors/failures.js";
4
+ import { checkJournal } from "../journal.js";
5
+ import { committedWrites, droppedWrites } from "../write-events.js";
4
6
  import { drainInvolvementQueue } from "./involvement-queue.js";
5
7
  import { drainMentions } from "./mention-drain.js";
6
8
  import { drainStems, stemmerCache } from "./stems.js";
@@ -12,17 +14,18 @@ export const isBusy = (error) => sqliteCode(error) === SQLITE_BUSY;
12
14
  * Takes the write lock, waiting as long as the connection's `busy_timeout`. SQLite's own wait sleeps 1 ms, then up to
13
15
  * 100 ms between tries, so a process that commits and begins again at once is never seen free and the waiter fails
14
16
  * with "database is locked" after the full timeout. Trying every millisecond sees a gap of a fraction of a
15
- * millisecond between two writes; a writer that leaves none still starves it, hence `writeBatch`'s pause.
17
+ * millisecond between two writes; a writer that leaves none still starves it, hence the pause after a write that
18
+ * waited. Answers whether it had to wait.
16
19
  */
17
- const beginImmediate = (database) => {
20
+ export const beginImmediate = (database) => {
18
21
  const timeout = Number(database.prepare("PRAGMA busy_timeout").get()?.timeout ?? 0);
19
22
  const deadline = Date.now() + timeout;
20
23
  database.exec("PRAGMA busy_timeout = 0");
21
24
  try {
22
- for (;;) {
25
+ for (let waited = false;; waited = true) {
23
26
  try {
24
27
  database.exec("BEGIN IMMEDIATE");
25
- return;
28
+ return waited;
26
29
  }
27
30
  catch (error) {
28
31
  if (!isBusy(error) || Date.now() >= deadline)
@@ -35,6 +38,16 @@ const beginImmediate = (database) => {
35
38
  database.exec(`PRAGMA busy_timeout = ${timeout}`);
36
39
  }
37
40
  };
41
+ /**
42
+ * On some errors (a full disk, an I/O error) SQLite has already rolled the transaction back, and the ROLLBACK then
43
+ * fails with "no transaction is active". That failure must not take the place of the error that ended it.
44
+ */
45
+ export const rollBack = (database, sql = "ROLLBACK") => {
46
+ try {
47
+ database.exec(sql);
48
+ }
49
+ catch { }
50
+ };
38
51
  const connections = new WeakMap();
39
52
  const connectionOf = (database) => {
40
53
  let connection = connections.get(database);
@@ -57,13 +70,7 @@ export const write = (database, body, { beforeCommit } = {}) => transact(databas
57
70
  * the next `write`. The outermost pauses 2 ms after it commits: a loop that begins again at once leaves another
58
71
  * process's `write` a gap of microseconds, and it misses that gap until its 5 s wait runs out.
59
72
  */
60
- export const writeBatch = (database, body) => {
61
- const outer = connectionOf(database).depth === 0;
62
- const result = transact(database, body, false);
63
- if (outer)
64
- Atomics.wait(pause, 0, 0, BATCH_PAUSE_MS);
65
- return result;
66
- };
73
+ export const writeBatch = (database, body) => transact(database, body, false);
67
74
  const BATCH_PAUSE_MS = 2;
68
75
  const transact = (database, body, drain, beforeCommit) => {
69
76
  const connection = connectionOf(database);
@@ -71,8 +78,9 @@ const transact = (database, body, drain, beforeCommit) => {
71
78
  throw new Error("a store write cannot run inside a read");
72
79
  const outer = connection.depth === 0;
73
80
  const name = `w${connection.depth + 1}`;
81
+ const waited = outer && beginImmediate(database);
74
82
  if (outer)
75
- beginImmediate(database);
83
+ droppedWrites(database);
76
84
  else
77
85
  database.exec(`SAVEPOINT ${name}`);
78
86
  connection.depth += 1;
@@ -84,11 +92,21 @@ const transact = (database, body, drain, beforeCommit) => {
84
92
  drainInvolvementQueue(database);
85
93
  }
86
94
  beforeCommit?.();
95
+ if (outer)
96
+ checkJournal(database);
87
97
  database.exec(outer ? "COMMIT" : `RELEASE ${name}`);
98
+ if (outer)
99
+ committedWrites(database);
100
+ // A write that had to wait has others waiting behind it, and its caller's next write would begin before they see
101
+ // the lock free. Pausing only then costs an uncontended write nothing.
102
+ if (outer && (waited || !drain))
103
+ Atomics.wait(pause, 0, 0, BATCH_PAUSE_MS);
88
104
  return result;
89
105
  }
90
106
  catch (error) {
91
- database.exec(outer ? "ROLLBACK" : `ROLLBACK TO ${name}; RELEASE ${name}`);
107
+ rollBack(database, outer ? "ROLLBACK" : `ROLLBACK TO ${name}; RELEASE ${name}`);
108
+ if (outer)
109
+ droppedWrites(database);
92
110
  throw error;
93
111
  }
94
112
  finally {
@@ -146,7 +164,7 @@ export const rehearse = async ({ database, client }, body) => {
146
164
  else
147
165
  delete target[name];
148
166
  connection.depth -= 1;
149
- database.exec("ROLLBACK");
167
+ rollBack(database);
150
168
  }
151
169
  };
152
170
  /** Several queries that must see one state of the file. Never drains: that is the outer write's work, or nobody's. */
@@ -164,7 +182,7 @@ export const read = (database, body) => {
164
182
  return result;
165
183
  }
166
184
  catch (error) {
167
- database.exec(`ROLLBACK TO ${name}; RELEASE ${name}`);
185
+ rollBack(database, `ROLLBACK TO ${name}; RELEASE ${name}`);
168
186
  throw error;
169
187
  }
170
188
  finally {
@@ -20,6 +20,7 @@ import { type RosterStore } from "./roster.js";
20
20
  import { type SearchIndexStore } from "./search-index.js";
21
21
  import { type SearchesStore } from "./searches.js";
22
22
  import { type AgentActionsStore } from "./sqlite/agent-actions.js";
23
+ import { type BaseStagesStore } from "./sqlite/base-stages.js";
23
24
  import { type BotUpdateStore } from "./sqlite/bot-updates.js";
24
25
  import { type ContractsStore } from "./sqlite/business-contracts.js";
25
26
  import { type BusinessSearchStore } from "./sqlite/business-search.js";
@@ -119,6 +120,8 @@ export interface MessageStore {
119
120
  readonly orders: OrdersStore;
120
121
  /** The owner's sales pipelines and the deals in them, written directly by the owner and his agents. */
121
122
  readonly deals: DealsStore;
123
+ /** The owner's base stages of deals and of leads, and every other pipeline's stages mapped onto them. */
124
+ readonly baseStages: BaseStagesStore;
122
125
  /** What the owner or a supplier sells, with its price; written directly. */
123
126
  readonly products: ProductsStore;
124
127
  /** The contracts the owner works under; one with a value from an agent or an import waits for the owner. */
@@ -16,6 +16,7 @@ import * as accounts from "./sqlite/accounts.js";
16
16
  import { addedSourcesOver } from "./sqlite/added-sources.js";
17
17
  import { agentActionsStoreOver } from "./sqlite/agent-actions.js";
18
18
  import * as attachmentTexts from "./sqlite/attachment-texts.js";
19
+ import { baseStagesStoreOver } from "./sqlite/base-stages.js";
19
20
  import { botUpdateStoreOver } from "./sqlite/bot-updates.js";
20
21
  import { contractsStoreOver } from "./sqlite/business-contracts.js";
21
22
  import { businessSearchOver } from "./sqlite/business-search.js";
@@ -128,6 +129,7 @@ const storeOver = (context) => {
128
129
  const finance = once(() => financeStoreOver(context));
129
130
  const orders = once(() => ordersStoreOver(context));
130
131
  const deals = once(() => dealsStoreOver(context));
132
+ const baseStages = once(() => baseStagesStoreOver(context));
131
133
  const products = once(() => productsStoreOver(context));
132
134
  const contracts = once(() => contractsStoreOver(context));
133
135
  const subscriptions = once(() => subscriptionsStoreOver(context));
@@ -255,6 +257,9 @@ const storeOver = (context) => {
255
257
  get deals() {
256
258
  return deals();
257
259
  },
260
+ get baseStages() {
261
+ return baseStages();
262
+ },
258
263
  get products() {
259
264
  return products();
260
265
  },
@@ -0,0 +1,18 @@
1
+ import type { StoreEvent } from "../runs/events.js";
2
+ type Target = {
3
+ table: string;
4
+ op: StoreEvent["op"];
5
+ };
6
+ /** The table a statement writes and how, read from its text; a read, DDL or a temp table answers undefined. */
7
+ export declare const writeTarget: (sql: string) => Target | undefined;
8
+ /**
9
+ * Counts the rows each write statement changes, per table, on a writable store connection: `methods` are the client's
10
+ * prepare calls, which both the `CacheDatabase` seam and Drizzle go through. A transaction's counts become run events
11
+ * when `write` commits it; a statement outside one is its own event.
12
+ */
13
+ export declare const countWrites: <C extends object>(client: C, methods: readonly string[], inTransaction: () => boolean) => C;
14
+ /** The seam built over a counted client shares its counts. */
15
+ export declare const shareCounts: (from: object, to: object) => void;
16
+ export declare const committedWrites: (database: object) => void;
17
+ export declare const droppedWrites: (database: object) => void;
18
+ export {};
@@ -0,0 +1,88 @@
1
+ import { runEvent } from "../runs/scope.js";
2
+ const WRITE = /^\s*(?:WITH\b[\s\S]*?\)\s*)?(INSERT|REPLACE|UPDATE|DELETE)\b(?:\s+OR\s+[A-Za-z]+)?\s+(?:INTO\s+|FROM\s+)?(?:("?)(\w+)\2\.)?["`[]?(\w+)/i;
3
+ /** The table a statement writes and how, read from its text; a read, DDL or a temp table answers undefined. */
4
+ export const writeTarget = (sql) => {
5
+ const match = WRITE.exec(sql);
6
+ if (!match || match[3]?.toLowerCase() === "temp")
7
+ return undefined;
8
+ const verb = match[1].toUpperCase();
9
+ return { table: match[4], op: verb === "UPDATE" ? "update" : verb === "DELETE" ? "delete" : "insert" };
10
+ };
11
+ const tallies = new WeakMap();
12
+ const wrapped = new WeakSet();
13
+ const emit = ({ table, op, n }) => runEvent({ event: "store", operation: "store.write", kind: "store", table, op, n });
14
+ const note = (tally, target, n) => {
15
+ if (n <= 0)
16
+ return;
17
+ // Outside a transaction the statement committed itself.
18
+ if (!tally.inTransaction())
19
+ return emit({ ...target, n });
20
+ const key = `${target.table} ${target.op}`;
21
+ const seen = tally.pending.get(key);
22
+ if (seen)
23
+ seen.n += n;
24
+ else
25
+ tally.pending.set(key, { ...target, n });
26
+ };
27
+ const count = (statement, target, tally) => {
28
+ if (wrapped.has(statement))
29
+ return;
30
+ wrapped.add(statement);
31
+ const { run, all, get } = statement;
32
+ statement.run = (...parameters) => {
33
+ const result = run.apply(statement, parameters);
34
+ note(tally, target, Number(result.changes));
35
+ return result;
36
+ };
37
+ statement.all = (...parameters) => {
38
+ const rows = all.apply(statement, parameters);
39
+ note(tally, target, rows.length);
40
+ return rows;
41
+ };
42
+ statement.get = (...parameters) => {
43
+ const row = get.apply(statement, parameters);
44
+ note(tally, target, row === undefined || row === null ? 0 : 1);
45
+ return row;
46
+ };
47
+ };
48
+ /**
49
+ * Counts the rows each write statement changes, per table, on a writable store connection: `methods` are the client's
50
+ * prepare calls, which both the `CacheDatabase` seam and Drizzle go through. A transaction's counts become run events
51
+ * when `write` commits it; a statement outside one is its own event.
52
+ */
53
+ export const countWrites = (client, methods, inTransaction) => {
54
+ const tally = { pending: new Map(), inTransaction };
55
+ const target = client;
56
+ const shared = Object.getPrototypeOf(client);
57
+ for (const name of methods) {
58
+ const own = Object.hasOwn(target, name) ? target[name] : undefined;
59
+ if (typeof (own ?? shared[name]) !== "function")
60
+ continue;
61
+ // Looked up on each call, so a spy on the prototype still sees it.
62
+ target[name] = (sql, ...rest) => {
63
+ const prepare = (own ?? shared[name]);
64
+ const statement = prepare.call(client, sql, ...rest);
65
+ const writes = typeof sql === "string" ? writeTarget(sql) : undefined;
66
+ if (writes)
67
+ count(statement, writes, tally);
68
+ return statement;
69
+ };
70
+ }
71
+ tallies.set(client, tally);
72
+ return client;
73
+ };
74
+ /** The seam built over a counted client shares its counts. */
75
+ export const shareCounts = (from, to) => {
76
+ const tally = tallies.get(from);
77
+ if (tally)
78
+ tallies.set(to, tally);
79
+ };
80
+ export const committedWrites = (database) => {
81
+ const tally = tallies.get(database);
82
+ if (!tally || tally.pending.size === 0)
83
+ return;
84
+ for (const counted of tally.pending.values())
85
+ emit(counted);
86
+ tally.pending.clear();
87
+ };
88
+ export const droppedWrites = (database) => tallies.get(database)?.pending.clear();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.617.0",
3
+ "version": "0.619.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",
@@ -15,7 +15,7 @@
15
15
  "type": "module",
16
16
  "wirecat": {
17
17
  "store": {
18
- "speaks": 59,
18
+ "speaks": 60,
19
19
  "minCompatible": 56
20
20
  }
21
21
  },
@@ -278,7 +278,7 @@
278
278
  "@types/node": "^22.20.5",
279
279
  "@types/pngjs": "6.0.5",
280
280
  "@vitest/coverage-v8": "^5.0.5",
281
- "@wirecat/cli-base": "0.90.0",
281
+ "@wirecat/cli-base": "0.93.0",
282
282
  "@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
283
283
  "ajv": "8.17.1",
284
284
  "commander": "15.0.0",
package/parity.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "meaning": "who sees the about text: everyone, contacts or nobody"
7
7
  },
8
8
  "--accept-terms": {
9
- "meaning": "accept the model's licence terms, for a model that has its own"
9
+ "meaning": "no longer needed; kept so older scripts still run"
10
10
  },
11
11
  "--add": {
12
12
  "value": "<chat>",
@@ -880,7 +880,7 @@
880
880
  },
881
881
  "--reason": {
882
882
  "value": "<text>",
883
- "meaning": "why a task was closed, kept with it — no-reply-needed, for example"
883
+ "meaning": "why, kept with what it records: a task's close (no-reply-needed, for example), an agent's proposed link"
884
884
  },
885
885
  "--record": {
886
886
  "meaning": "keep this run under `runs`, with the command as typed and secrets hidden; record_input false leaves the typed words out",
@@ -1154,6 +1154,9 @@
1154
1154
  "default": "me",
1155
1155
  "meaning": "whose replies are timed: the owner's (me) or the other side's (others)"
1156
1156
  },
1157
+ "--with-muted": {
1158
+ "meaning": "with --all: also read the chats the owner muted, as history_muted true does"
1159
+ },
1157
1160
  "--with-notes": {
1158
1161
  "meaning": "include your private notes, subject to contacts.notes.list permission"
1159
1162
  },
@@ -2884,7 +2887,10 @@
2884
2887
  "in": "all"
2885
2888
  },
2886
2889
  "contacts link": {
2887
- "in": "all"
2890
+ "in": "all",
2891
+ "options": {
2892
+ "--reason": "all"
2893
+ }
2888
2894
  },
2889
2895
  "contacts list": {
2890
2896
  "in": "all",
@@ -3581,7 +3587,8 @@
3581
3587
  "--no-catch-up": "all",
3582
3588
  "--page-size": "all",
3583
3589
  "--pause": "all",
3584
- "--since-time": "all"
3590
+ "--since-time": "all",
3591
+ "--with-muted": "all"
3585
3592
  }
3586
3593
  },
3587
3594
  "messages transcribe": {
@@ -537,6 +537,9 @@
537
537
  },
538
538
  "phone": {
539
539
  "description": "Phone numbers, one or a list: an exact one, read to E.164, links like an e-mail"
540
+ },
541
+ "position": {
542
+ "description": "The job title, kept beside each member-of as its position: never the role"
540
543
  }
541
544
  },
542
545
  "additionalProperties": true