@wirecat/cli-core 0.606.0 → 0.607.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 (209) hide show
  1. package/CHANGELOG.md +186 -1
  2. package/dist/calendar/index.d.ts +1 -0
  3. package/dist/calendar/index.js +1 -0
  4. package/dist/calendar/reads.js +5 -0
  5. package/dist/cli/app-mcp-command.d.ts +6 -2
  6. package/dist/cli/app-mcp-command.js +11 -2
  7. package/dist/cli/csv.d.ts +10 -0
  8. package/dist/cli/csv.js +22 -0
  9. package/dist/cli/early-refusal.d.ts +10 -0
  10. package/dist/cli/early-refusal.js +19 -0
  11. package/dist/cli/index.d.ts +3 -0
  12. package/dist/cli/index.js +3 -0
  13. package/dist/cli/profile-token.d.ts +29 -0
  14. package/dist/cli/profile-token.js +65 -0
  15. package/dist/cli/program.js +4 -2
  16. package/dist/crm/cli/csv.d.ts +23 -0
  17. package/dist/crm/cli/csv.js +135 -0
  18. package/dist/crm/cli/index.d.ts +1 -1
  19. package/dist/crm/cli/index.js +33 -21
  20. package/dist/crm/cli/pipeline-view.d.ts +3 -0
  21. package/dist/crm/cli/pipeline-view.js +36 -0
  22. package/dist/crm/writes.d.ts +6 -4
  23. package/dist/crm/writes.js +17 -13
  24. package/dist/documents.d.ts +2 -0
  25. package/dist/documents.js +2 -0
  26. package/dist/domain/business.d.ts +79 -0
  27. package/dist/domain/business.js +42 -0
  28. package/dist/domain/finance.d.ts +2 -0
  29. package/dist/domain/finance.js +2 -2
  30. package/dist/domain/google-documents.d.ts +2 -0
  31. package/dist/domain/google-documents.js +3 -1
  32. package/dist/domain/money.d.ts +2 -0
  33. package/dist/domain/money.js +11 -3
  34. package/dist/domain/providers.d.ts +36 -1
  35. package/dist/domain/providers.js +69 -7
  36. package/dist/domain/record-fields.d.ts +42 -0
  37. package/dist/domain/record-fields.js +122 -0
  38. package/dist/domain/record-types.d.ts +489 -16
  39. package/dist/domain/record-types.js +374 -7
  40. package/dist/domain/records.d.ts +96 -5
  41. package/dist/domain/records.js +178 -32
  42. package/dist/domain/ref-summaries.js +5 -0
  43. package/dist/domain/references.d.ts +1 -1
  44. package/dist/domain/references.js +5 -0
  45. package/dist/domain/vcard.d.ts +25 -0
  46. package/dist/domain/vcard.js +267 -0
  47. package/dist/entities/deal.d.ts +14 -0
  48. package/dist/entities/deal.js +3 -0
  49. package/dist/entities/index.d.ts +12 -0
  50. package/dist/errors/reasons.js +61 -2
  51. package/dist/fetch/addresses.d.ts +5 -0
  52. package/dist/fetch/addresses.js +76 -0
  53. package/dist/fetch/page.d.ts +64 -0
  54. package/dist/fetch/page.js +256 -0
  55. package/dist/index.d.ts +5 -3
  56. package/dist/index.js +5 -3
  57. package/dist/meetings/calendar-text.d.ts +1 -1
  58. package/dist/meetings/calendar-text.js +4 -0
  59. package/dist/meetings/event-store.d.ts +2 -2
  60. package/dist/search/account-list.d.ts +4 -0
  61. package/dist/search/account-list.js +19 -8
  62. package/dist/search/query.js +2 -2
  63. package/dist/services/events-search.js +2 -2
  64. package/dist/services/index.d.ts +1 -1
  65. package/dist/services/index.js +1 -1
  66. package/dist/services/ingest.d.ts +56 -8
  67. package/dist/services/ingest.js +95 -61
  68. package/dist/services/messages-search.js +3 -2
  69. package/dist/services/metadata.d.ts +1 -1
  70. package/dist/services/notes-search.js +1 -0
  71. package/dist/services/proposals/kinds/contracts.d.ts +4 -0
  72. package/dist/services/proposals/kinds/contracts.js +9 -0
  73. package/dist/services/proposals/kinds/finance.d.ts +4 -0
  74. package/dist/services/proposals/kinds/finance.js +14 -35
  75. package/dist/services/proposals/kinds/orders.js +7 -30
  76. package/dist/services/proposals/kinds/shared.d.ts +27 -1
  77. package/dist/services/proposals/kinds/shared.js +34 -0
  78. package/dist/services/proposals/kinds/source-review.d.ts +4 -0
  79. package/dist/services/proposals/kinds/source-review.js +45 -0
  80. package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
  81. package/dist/services/proposals/kinds/subscriptions.js +24 -0
  82. package/dist/services/proposals/kinds/transactions.d.ts +4 -0
  83. package/dist/services/proposals/kinds/transactions.js +10 -0
  84. package/dist/services/proposals/registry.js +9 -0
  85. package/dist/services/search-all-meetings.js +2 -2
  86. package/dist/services/search-all.js +4 -3
  87. package/dist/services/search-kind.js +3 -3
  88. package/dist/store/decisions.js +5 -1
  89. package/dist/store/index.d.ts +14 -3
  90. package/dist/store/index.js +3 -0
  91. package/dist/store/people.js +1 -1
  92. package/dist/store/source-review.d.ts +64 -0
  93. package/dist/store/source-review.js +7 -0
  94. package/dist/store/sqlite/accounts.d.ts +1 -1
  95. package/dist/store/sqlite/accounts.js +24 -1
  96. package/dist/store/sqlite/added-sources.d.ts +82 -0
  97. package/dist/store/sqlite/added-sources.js +237 -0
  98. package/dist/store/sqlite/business-contracts.d.ts +90 -0
  99. package/dist/store/sqlite/business-contracts.js +182 -0
  100. package/dist/store/sqlite/calendar-events.js +7 -3
  101. package/dist/store/sqlite/calendar-forget.js +3 -0
  102. package/dist/store/sqlite/calendar.d.ts +21 -1
  103. package/dist/store/sqlite/calendar.js +27 -7
  104. package/dist/store/sqlite/chat-rekey.js +7 -0
  105. package/dist/store/sqlite/chats.d.ts +3 -1
  106. package/dist/store/sqlite/chats.js +15 -1
  107. package/dist/store/sqlite/crm-rows.d.ts +44 -0
  108. package/dist/store/sqlite/crm-rows.js +195 -0
  109. package/dist/store/sqlite/crm.d.ts +2 -11
  110. package/dist/store/sqlite/crm.js +11 -166
  111. package/dist/store/sqlite/deals.d.ts +4 -8
  112. package/dist/store/sqlite/deals.js +5 -3
  113. package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
  114. package/dist/store/sqlite/emails.js +2 -0
  115. package/dist/store/sqlite/entity-types.d.ts +14 -9
  116. package/dist/store/sqlite/entity-types.js +4 -10
  117. package/dist/store/sqlite/evidence-signature.d.ts +114 -0
  118. package/dist/store/sqlite/evidence-signature.js +349 -0
  119. package/dist/store/sqlite/evidence-sources.d.ts +2 -1
  120. package/dist/store/sqlite/evidence-sources.js +7 -3
  121. package/dist/store/sqlite/facts.d.ts +7 -2
  122. package/dist/store/sqlite/facts.js +21 -5
  123. package/dist/store/sqlite/finance.d.ts +18 -105
  124. package/dist/store/sqlite/finance.js +100 -219
  125. package/dist/store/sqlite/knowledge.d.ts +8 -2
  126. package/dist/store/sqlite/knowledge.js +65 -71
  127. package/dist/store/sqlite/labelled.d.ts +3 -0
  128. package/dist/store/sqlite/labelled.js +11 -0
  129. package/dist/store/sqlite/link-kinds.d.ts +2 -0
  130. package/dist/store/sqlite/link-kinds.js +2 -0
  131. package/dist/store/sqlite/manifest.js +2 -0
  132. package/dist/store/sqlite/memories.d.ts +23 -9
  133. package/dist/store/sqlite/memories.js +41 -13
  134. package/dist/store/sqlite/messages.js +2 -0
  135. package/dist/store/sqlite/migrations.generated.js +103 -0
  136. package/dist/store/sqlite/money-lines.d.ts +48 -0
  137. package/dist/store/sqlite/money-lines.js +76 -0
  138. package/dist/store/sqlite/money-transactions.d.ts +132 -0
  139. package/dist/store/sqlite/money-transactions.js +295 -0
  140. package/dist/store/sqlite/notes.d.ts +11 -2
  141. package/dist/store/sqlite/notes.js +20 -25
  142. package/dist/store/sqlite/orders.d.ts +36 -6
  143. package/dist/store/sqlite/orders.js +126 -46
  144. package/dist/store/sqlite/payment-facts.js +1 -1
  145. package/dist/store/sqlite/private-people.js +2 -0
  146. package/dist/store/sqlite/products.d.ts +78 -0
  147. package/dist/store/sqlite/products.js +131 -0
  148. package/dist/store/sqlite/project-transfer/records.js +26 -1
  149. package/dist/store/sqlite/project-transfer/snapshot.js +5 -3
  150. package/dist/store/sqlite/proposed-actions.d.ts +1 -1
  151. package/dist/store/sqlite/proposed-actions.js +24 -7
  152. package/dist/store/sqlite/providers.d.ts +0 -4
  153. package/dist/store/sqlite/providers.js +20 -2
  154. package/dist/store/sqlite/record-matching.d.ts +80 -0
  155. package/dist/store/sqlite/record-matching.js +264 -0
  156. package/dist/store/sqlite/record-projection.d.ts +89 -0
  157. package/dist/store/sqlite/record-projection.js +50 -0
  158. package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
  159. package/dist/store/sqlite/record-projectors-direct.js +125 -0
  160. package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
  161. package/dist/store/sqlite/record-projectors-money.js +214 -0
  162. package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
  163. package/dist/store/sqlite/record-projectors-phone.js +212 -0
  164. package/dist/store/sqlite/record-projectors.d.ts +3 -44
  165. package/dist/store/sqlite/record-projectors.js +7 -67
  166. package/dist/store/sqlite/record-relations.d.ts +24 -0
  167. package/dist/store/sqlite/record-relations.js +92 -0
  168. package/dist/store/sqlite/record-remap.d.ts +12 -0
  169. package/dist/store/sqlite/record-remap.js +92 -0
  170. package/dist/store/sqlite/record-run.d.ts +71 -0
  171. package/dist/store/sqlite/record-run.js +179 -0
  172. package/dist/store/sqlite/record-settling.d.ts +9 -0
  173. package/dist/store/sqlite/record-settling.js +74 -0
  174. package/dist/store/sqlite/record-stamps.d.ts +13 -0
  175. package/dist/store/sqlite/record-stamps.js +30 -0
  176. package/dist/store/sqlite/record-writes.d.ts +43 -0
  177. package/dist/store/sqlite/record-writes.js +307 -0
  178. package/dist/store/sqlite/records.d.ts +52 -42
  179. package/dist/store/sqlite/records.js +123 -353
  180. package/dist/store/sqlite/ref-summaries.d.ts +6 -5
  181. package/dist/store/sqlite/ref-summaries.js +60 -8
  182. package/dist/store/sqlite/refunds.d.ts +31 -0
  183. package/dist/store/sqlite/refunds.js +112 -0
  184. package/dist/store/sqlite/reviewed.d.ts +109 -0
  185. package/dist/store/sqlite/reviewed.js +173 -0
  186. package/dist/store/sqlite/schema.d.ts +2840 -83
  187. package/dist/store/sqlite/schema.js +256 -1
  188. package/dist/store/sqlite/source-review-marks.d.ts +54 -0
  189. package/dist/store/sqlite/source-review-marks.js +143 -0
  190. package/dist/store/sqlite/source-review.d.ts +54 -0
  191. package/dist/store/sqlite/source-review.js +221 -0
  192. package/dist/store/sqlite/stage-amounts.d.ts +14 -0
  193. package/dist/store/sqlite/stage-amounts.js +23 -0
  194. package/dist/store/sqlite/subscriptions.d.ts +135 -0
  195. package/dist/store/sqlite/subscriptions.js +294 -0
  196. package/dist/store/sqlite/text-input.d.ts +3 -0
  197. package/dist/store/sqlite/text-input.js +13 -0
  198. package/dist/store/sqlite/things.js +2 -9
  199. package/dist/store/sqlite/transaction.d.ts +15 -0
  200. package/dist/store/sqlite/transaction.js +57 -0
  201. package/dist/store/sqlite/wallets.d.ts +88 -0
  202. package/dist/store/sqlite/wallets.js +143 -0
  203. package/dist/store/store-errors.d.ts +1 -0
  204. package/dist/store/store-errors.js +45 -0
  205. package/dist/store/store.d.ts +16 -1
  206. package/dist/store/store.js +30 -6
  207. package/package.json +5 -2
  208. package/schemas/examples/records-business.jsonl +11 -0
  209. package/schemas/records-envelope.v1.schema.json +1160 -5
@@ -1426,12 +1426,16 @@ export const invoices = sqliteTable("invoices", {
1426
1426
  createdAt: integer("created_at").notNull(),
1427
1427
  updatedAt: integer("updated_at").notNull(),
1428
1428
  deletedAt: integer("deleted_at"),
1429
+ contractId: integer("contract_id").references(() => contracts.id),
1430
+ subscriptionId: integer("subscription_id").references(() => subscriptions.id),
1429
1431
  }, (table) => [
1430
1432
  index("invoices_by_issuer_type_issuer_id").on(table.issuerType, table.issuerId),
1431
1433
  index("invoices_by_counterparty_type_counterparty_id").on(table.counterpartyType, table.counterpartyId),
1432
1434
  index("invoices_by_project_id").on(table.projectId),
1433
1435
  index("invoices_by_author_type_author_id").on(table.authorType, table.authorId),
1434
1436
  index("invoices_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1437
+ index("invoices_by_contract_id").on(table.contractId),
1438
+ index("invoices_by_subscription_id").on(table.subscriptionId),
1435
1439
  ]);
1436
1440
  export const invoiceLines = sqliteTable("invoice_lines", {
1437
1441
  id: integer("id").primaryKey(),
@@ -1446,7 +1450,11 @@ export const invoiceLines = sqliteTable("invoice_lines", {
1446
1450
  amount: integer("amount").notNull(),
1447
1451
  createdAt: integer("created_at").notNull(),
1448
1452
  updatedAt: integer("updated_at").notNull(),
1449
- }, (table) => [unique().on(table.invoiceId, table.position)]);
1453
+ catalogItemId: integer("catalog_item_id").references(() => catalogItems.id),
1454
+ }, (table) => [
1455
+ unique().on(table.invoiceId, table.position),
1456
+ index("invoice_lines_by_catalog_item_id").on(table.catalogItemId),
1457
+ ]);
1450
1458
  /** Money that moved to or from the owner, in minor units of its currency. */
1451
1459
  export const payments = sqliteTable("payments", {
1452
1460
  id: integer("id").primaryKey(),
@@ -1471,12 +1479,16 @@ export const payments = sqliteTable("payments", {
1471
1479
  createdAt: integer("created_at").notNull(),
1472
1480
  updatedAt: integer("updated_at").notNull(),
1473
1481
  deletedAt: integer("deleted_at"),
1482
+ refundOf: integer("refund_of").references(() => payments.id),
1483
+ subscriptionId: integer("subscription_id").references(() => subscriptions.id),
1474
1484
  }, (table) => [
1475
1485
  index("payments_by_payer_type_payer_id").on(table.payerType, table.payerId),
1476
1486
  index("payments_by_payee_type_payee_id").on(table.payeeType, table.payeeId),
1477
1487
  index("payments_by_project_id").on(table.projectId),
1478
1488
  index("payments_by_author_type_author_id").on(table.authorType, table.authorId),
1479
1489
  index("payments_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1490
+ index("payments_by_refund_of").on(table.refundOf),
1491
+ index("payments_by_subscription_id").on(table.subscriptionId),
1480
1492
  ]);
1481
1493
  /** How much of a payment settles an invoice; triggers keep it within both and in one currency. */
1482
1494
  export const paymentAllocations = sqliteTable("payment_allocations", {
@@ -1527,6 +1539,9 @@ export const orders = sqliteTable("orders", {
1527
1539
  updatedAt: integer("updated_at").notNull(),
1528
1540
  deletedAt: integer("deleted_at"),
1529
1541
  sourceUrl: text("source_url"),
1542
+ contractId: integer("contract_id").references(() => contracts.id),
1543
+ dealId: integer("deal_id").references(() => deals.id),
1544
+ direction: text("direction").notNull().default("purchase"),
1530
1545
  }, (table) => [
1531
1546
  index("orders_by_customer_type_customer_id").on(table.customerType, table.customerId),
1532
1547
  index("orders_by_supplier_type_supplier_id").on(table.supplierType, table.supplierId),
@@ -1535,6 +1550,8 @@ export const orders = sqliteTable("orders", {
1535
1550
  index("orders_by_project_id").on(table.projectId),
1536
1551
  index("orders_by_author_type_author_id").on(table.authorType, table.authorId),
1537
1552
  index("orders_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1553
+ index("orders_by_contract_id").on(table.contractId),
1554
+ index("orders_by_deal_id").on(table.dealId),
1538
1555
  ]);
1539
1556
  /** A sales pipeline: the stages a deal or a lead goes through, in order; the owner's own or a CRM account's. */
1540
1557
  export const pipelines = sqliteTable("pipelines", {
@@ -1680,6 +1697,241 @@ export const crmFields = sqliteTable("crm_fields", {
1680
1697
  updatedAt: integer("updated_at").notNull(),
1681
1698
  deletedAt: integer("deleted_at"),
1682
1699
  }, (table) => [unique().on(table.accountId, table.kind, table.code)]);
1700
+ /** What the owner or a supplier sells, a product or a service; a line copies its price and never reads it back. */
1701
+ export const catalogItems = sqliteTable("catalog_items", {
1702
+ id: integer("id").primaryKey(),
1703
+ kind: text("kind").notNull(),
1704
+ name: text("name").notNull(),
1705
+ nameFolded: text("name_folded").notNull(),
1706
+ sku: text("sku"),
1707
+ supplierType: text("supplier_type"),
1708
+ supplierId: integer("supplier_id"),
1709
+ unit: text("unit"),
1710
+ unitPrice: integer("unit_price"),
1711
+ currency: text("currency"),
1712
+ description: text("description"),
1713
+ archivedDate: text("archived_date"),
1714
+ authorType: text("author_type").notNull(),
1715
+ authorId: integer("author_id").notNull(),
1716
+ model: text("model"),
1717
+ source: text("source").notNull(),
1718
+ metadata: text("metadata"),
1719
+ createdAt: integer("created_at").notNull(),
1720
+ updatedAt: integer("updated_at").notNull(),
1721
+ deletedAt: integer("deleted_at"),
1722
+ }, (table) => [
1723
+ uniqueIndex("catalog_items_by_supplier_type_supplier_id_sku")
1724
+ .on(table.supplierType, table.supplierId, table.sku)
1725
+ .where(sql `supplier_id IS NOT NULL AND sku IS NOT NULL AND deleted_at IS NULL`),
1726
+ uniqueIndex("catalog_items_by_sku")
1727
+ .on(table.sku)
1728
+ .where(sql `supplier_id IS NULL AND sku IS NOT NULL AND deleted_at IS NULL`),
1729
+ index("catalog_items_by_name_folded").on(table.nameFolded),
1730
+ index("catalog_items_by_author_type_author_id").on(table.authorType, table.authorId),
1731
+ ]);
1732
+ export const orderLines = sqliteTable("order_lines", {
1733
+ id: integer("id").primaryKey(),
1734
+ orderId: integer("order_id")
1735
+ .notNull()
1736
+ .references(() => orders.id),
1737
+ position: integer("position").notNull(),
1738
+ description: text("description").notNull(),
1739
+ quantity: text("quantity").notNull(),
1740
+ unitPrice: integer("unit_price"),
1741
+ amount: integer("amount").notNull(),
1742
+ catalogItemId: integer("catalog_item_id").references(() => catalogItems.id),
1743
+ createdAt: integer("created_at").notNull(),
1744
+ updatedAt: integer("updated_at").notNull(),
1745
+ }, (table) => [
1746
+ unique().on(table.orderId, table.position),
1747
+ index("order_lines_by_catalog_item_id").on(table.catalogItemId),
1748
+ ]);
1749
+ /** A contract the owner works under; parties are relationships, the document an `attached` link. */
1750
+ export const contracts = sqliteTable("contracts", {
1751
+ id: integer("id").primaryKey(),
1752
+ name: text("name").notNull(),
1753
+ number: text("number"),
1754
+ kind: text("kind").notNull(),
1755
+ status: text("status").notNull(),
1756
+ signedDate: text("signed_date"),
1757
+ effectiveDate: text("effective_date"),
1758
+ expirationDate: text("expiration_date"),
1759
+ autoRenew: integer("auto_renew").notNull().default(0),
1760
+ renewalCount: integer("renewal_count"),
1761
+ renewalUnit: text("renewal_unit"),
1762
+ noticeDays: integer("notice_days"),
1763
+ terms: text("terms"),
1764
+ value: integer("value"),
1765
+ currency: text("currency"),
1766
+ projectId: integer("project_id").references(() => projects.id),
1767
+ dealId: integer("deal_id").references(() => deals.id),
1768
+ review: text("review").notNull(),
1769
+ authorType: text("author_type").notNull(),
1770
+ authorId: integer("author_id").notNull(),
1771
+ model: text("model"),
1772
+ confirmedByType: text("confirmed_by_type"),
1773
+ confirmedById: integer("confirmed_by_id"),
1774
+ source: text("source").notNull(),
1775
+ metadata: text("metadata"),
1776
+ createdAt: integer("created_at").notNull(),
1777
+ updatedAt: integer("updated_at").notNull(),
1778
+ deletedAt: integer("deleted_at"),
1779
+ }, (table) => [
1780
+ index("contracts_by_project_id").on(table.projectId),
1781
+ index("contracts_by_deal_id").on(table.dealId),
1782
+ index("contracts_by_author_type_author_id").on(table.authorType, table.authorId),
1783
+ index("contracts_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1784
+ ]);
1785
+ /** A subscription the owner bills or pays; its prices are `subscription_prices`, each a row of its own. */
1786
+ export const subscriptions = sqliteTable("subscriptions", {
1787
+ id: integer("id").primaryKey(),
1788
+ name: text("name").notNull(),
1789
+ direction: text("direction").notNull(),
1790
+ supplierType: text("supplier_type").notNull(),
1791
+ supplierId: integer("supplier_id").notNull(),
1792
+ customerType: text("customer_type").notNull(),
1793
+ customerId: integer("customer_id").notNull(),
1794
+ catalogItemId: integer("catalog_item_id").references(() => catalogItems.id),
1795
+ contractId: integer("contract_id").references(() => contracts.id),
1796
+ projectId: integer("project_id").references(() => projects.id),
1797
+ status: text("status").notNull(),
1798
+ intervalCount: integer("interval_count").notNull(),
1799
+ intervalUnit: text("interval_unit").notNull(),
1800
+ startDate: text("start_date").notNull(),
1801
+ trialEndDate: text("trial_end_date"),
1802
+ nextRenewalDate: text("next_renewal_date"),
1803
+ cancelledDate: text("cancelled_date"),
1804
+ endDate: text("end_date"),
1805
+ review: text("review").notNull(),
1806
+ authorType: text("author_type").notNull(),
1807
+ authorId: integer("author_id").notNull(),
1808
+ model: text("model"),
1809
+ confirmedByType: text("confirmed_by_type"),
1810
+ confirmedById: integer("confirmed_by_id"),
1811
+ source: text("source").notNull(),
1812
+ metadata: text("metadata"),
1813
+ createdAt: integer("created_at").notNull(),
1814
+ updatedAt: integer("updated_at").notNull(),
1815
+ deletedAt: integer("deleted_at"),
1816
+ }, (table) => [
1817
+ index("subscriptions_by_supplier_type_supplier_id").on(table.supplierType, table.supplierId),
1818
+ index("subscriptions_by_customer_type_customer_id").on(table.customerType, table.customerId),
1819
+ index("subscriptions_by_catalog_item_id").on(table.catalogItemId),
1820
+ index("subscriptions_by_contract_id").on(table.contractId),
1821
+ index("subscriptions_by_project_id").on(table.projectId),
1822
+ index("subscriptions_by_author_type_author_id").on(table.authorType, table.authorId),
1823
+ index("subscriptions_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1824
+ ]);
1825
+ export const subscriptionPrices = sqliteTable("subscription_prices", {
1826
+ id: integer("id").primaryKey(),
1827
+ subscriptionId: integer("subscription_id")
1828
+ .notNull()
1829
+ .references(() => subscriptions.id),
1830
+ amount: integer("amount").notNull(),
1831
+ currency: text("currency").notNull(),
1832
+ effectiveFrom: text("effective_from").notNull(),
1833
+ review: text("review").notNull(),
1834
+ authorType: text("author_type").notNull(),
1835
+ authorId: integer("author_id").notNull(),
1836
+ model: text("model"),
1837
+ confirmedByType: text("confirmed_by_type"),
1838
+ confirmedById: integer("confirmed_by_id"),
1839
+ source: text("source").notNull(),
1840
+ metadata: text("metadata"),
1841
+ createdAt: integer("created_at").notNull(),
1842
+ updatedAt: integer("updated_at").notNull(),
1843
+ }, (table) => [
1844
+ uniqueIndex("subscription_prices_by_subscription_id_effective_from")
1845
+ .on(table.subscriptionId, table.effectiveFrom)
1846
+ .where(sql `review <> 'rejected'`),
1847
+ index("subscription_prices_by_author_type_author_id").on(table.authorType, table.authorId),
1848
+ index("subscription_prices_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1849
+ ]);
1850
+ /** A bank account, card, cash box or wallet, in one currency; its balance is computed from its transactions. */
1851
+ export const financialAccounts = sqliteTable("financial_accounts", {
1852
+ id: integer("id").primaryKey(),
1853
+ name: text("name").notNull(),
1854
+ nameFolded: text("name_folded").notNull(),
1855
+ kind: text("kind").notNull(),
1856
+ currency: text("currency").notNull(),
1857
+ institution: text("institution"),
1858
+ numberHint: text("number_hint"),
1859
+ holderType: text("holder_type"),
1860
+ holderId: integer("holder_id"),
1861
+ openingBalance: integer("opening_balance"),
1862
+ openingDate: text("opening_date"),
1863
+ closedDate: text("closed_date"),
1864
+ authorType: text("author_type").notNull(),
1865
+ authorId: integer("author_id").notNull(),
1866
+ model: text("model"),
1867
+ source: text("source").notNull(),
1868
+ metadata: text("metadata"),
1869
+ createdAt: integer("created_at").notNull(),
1870
+ updatedAt: integer("updated_at").notNull(),
1871
+ deletedAt: integer("deleted_at"),
1872
+ }, (table) => [
1873
+ index("financial_accounts_by_name_folded").on(table.nameFolded),
1874
+ index("financial_accounts_by_holder_type_holder_id").on(table.holderType, table.holderId),
1875
+ index("financial_accounts_by_author_type_author_id").on(table.authorType, table.authorId),
1876
+ ]);
1877
+ /** What a statement says moved on a financial account; a payment it settles is `payment_id`. */
1878
+ export const financialTransactions = sqliteTable("financial_transactions", {
1879
+ id: integer("id").primaryKey(),
1880
+ financialAccountId: integer("financial_account_id")
1881
+ .notNull()
1882
+ .references(() => financialAccounts.id),
1883
+ direction: text("direction").notNull(),
1884
+ amount: integer("amount").notNull(),
1885
+ currency: text("currency").notNull(),
1886
+ status: text("status").notNull(),
1887
+ statementDate: text("statement_date").notNull(),
1888
+ bookedDate: text("booked_date"),
1889
+ description: text("description"),
1890
+ counterpartyName: text("counterparty_name"),
1891
+ counterpartyType: text("counterparty_type"),
1892
+ counterpartyId: integer("counterparty_id"),
1893
+ reference: text("reference"),
1894
+ paymentId: integer("payment_id").references(() => payments.id),
1895
+ projectId: integer("project_id").references(() => projects.id),
1896
+ review: text("review").notNull(),
1897
+ authorType: text("author_type").notNull(),
1898
+ authorId: integer("author_id").notNull(),
1899
+ model: text("model"),
1900
+ confirmedByType: text("confirmed_by_type"),
1901
+ confirmedById: integer("confirmed_by_id"),
1902
+ source: text("source").notNull(),
1903
+ metadata: text("metadata"),
1904
+ createdAt: integer("created_at").notNull(),
1905
+ updatedAt: integer("updated_at").notNull(),
1906
+ deletedAt: integer("deleted_at"),
1907
+ }, (table) => [
1908
+ index("financial_transactions_by_financial_account_id_booked_date").on(table.financialAccountId, table.bookedDate),
1909
+ uniqueIndex("financial_transactions_by_payment_id")
1910
+ .on(table.paymentId)
1911
+ .where(sql `payment_id IS NOT NULL AND deleted_at IS NULL`),
1912
+ index("financial_transactions_by_counterparty_type_counterparty_id").on(table.counterpartyType, table.counterpartyId),
1913
+ index("financial_transactions_by_project_id").on(table.projectId),
1914
+ index("financial_transactions_by_author_type_author_id").on(table.authorType, table.authorId),
1915
+ index("financial_transactions_by_confirmed_by_type_confirmed_by_id").on(table.confirmedByType, table.confirmedById),
1916
+ ]);
1917
+ export const transactionSplits = sqliteTable("transaction_splits", {
1918
+ id: integer("id").primaryKey(),
1919
+ transactionId: integer("transaction_id")
1920
+ .notNull()
1921
+ .references(() => financialTransactions.id),
1922
+ position: integer("position").notNull(),
1923
+ category: text("category").notNull(),
1924
+ categoryFolded: text("category_folded").notNull(),
1925
+ amount: integer("amount").notNull(),
1926
+ projectId: integer("project_id").references(() => projects.id),
1927
+ note: text("note"),
1928
+ createdAt: integer("created_at").notNull(),
1929
+ updatedAt: integer("updated_at").notNull(),
1930
+ }, (table) => [
1931
+ unique().on(table.transactionId, table.position),
1932
+ index("transaction_splits_by_category_folded").on(table.categoryFolded),
1933
+ index("transaction_splits_by_project_id").on(table.projectId),
1934
+ ]);
1683
1935
  export { decisionRequests } from "./schema/decision-requests.js";
1684
1936
  /** What a source said about one of its records, as it said it: the layer under the entity an import makes. */
1685
1937
  export const records = sqliteTable("records", {
@@ -1699,8 +1951,10 @@ export const records = sqliteTable("records", {
1699
1951
  createdAt: integer("created_at").notNull(),
1700
1952
  updatedAt: integer("updated_at").notNull(),
1701
1953
  deletedAt: integer("deleted_at"),
1954
+ createdEntity: integer("created_entity").notNull().default(0),
1702
1955
  }, (table) => [
1703
1956
  unique().on(table.accountId, table.type, table.externalId),
1957
+ index("records_by_account_id_batch_id").on(table.accountId, table.batchId),
1704
1958
  index("records_by_entity_type_entity_id").on(table.entityType, table.entityId),
1705
1959
  ]);
1706
1960
  export const recordRelations = sqliteTable("record_relations", {
@@ -1714,6 +1968,7 @@ export const recordRelations = sqliteTable("record_relations", {
1714
1968
  targetId: integer("target_id").references(() => records.id),
1715
1969
  createdAt: integer("created_at").notNull(),
1716
1970
  amount: text("amount"),
1971
+ asRole: text("as_role"),
1717
1972
  }, (table) => [
1718
1973
  unique().on(table.recordId, table.role, table.targetType, table.targetExternalId),
1719
1974
  index("record_relations_by_target_type_target_external_id").on(table.targetType, table.targetExternalId),
@@ -0,0 +1,54 @@
1
+ import type { CacheDatabase } from "../driver.js";
2
+ import { type SourceReviewChallenge, type SourceReviewMarker, type SourceReviewSource, type SourceReviewSuggestion } from "../source-review.js";
3
+ import { type Actor } from "./actors.js";
4
+ export type ReviewTarget = {
5
+ type: "memory" | "fact";
6
+ id: number;
7
+ };
8
+ export declare const HOLDING: {
9
+ readonly memory: readonly ["proposed", "confirmed"];
10
+ readonly fact: readonly ["proposed", "accepted"];
11
+ };
12
+ export declare const holdingSql: (alias: string, type: ReviewTarget["type"]) => string;
13
+ export declare const asReviewTarget: (type: unknown, id: unknown) => ReviewTarget | undefined;
14
+ export declare const holds: (database: CacheDatabase, target: ReviewTarget) => boolean;
15
+ /** The rule bot the source check writes its reviews as. */
16
+ export declare const sourceChecker: (database: CacheDatabase, at: number) => Actor;
17
+ /** The open source review of a memory or fact, through the proposals' target index. */
18
+ export declare const sourceReviewOf: (database: CacheDatabase, target: ReviewTarget) => SourceReviewMarker | null;
19
+ /**
20
+ * Opens the target's review, or folds new sources, a challenge and a suggestion into the open one, inside the caller's
21
+ * write. Its `raisedAt` moves only when the cause grows graver, so a review climbs the queue for a worse reason and
22
+ * not for the same one again.
23
+ */
24
+ export declare const raiseSourceReview: (database: CacheDatabase, target: ReviewTarget, change: {
25
+ sources?: SourceReviewSource[];
26
+ challenge?: SourceReviewChallenge;
27
+ suggestion?: SourceReviewSuggestion;
28
+ proposer: Actor;
29
+ at: number;
30
+ }) => {
31
+ id: string;
32
+ changed: boolean;
33
+ };
34
+ /**
35
+ * Closes the open review of a memory or fact that was corrected, withdrawn or removed, inside the caller's write:
36
+ * what the review asked has been answered by that decision.
37
+ */
38
+ export declare const closeSourceReviews: (database: CacheDatabase, target: ReviewTarget, close: {
39
+ actor: Actor;
40
+ at: number;
41
+ why: string;
42
+ }) => number;
43
+ /**
44
+ * A memory or fact erased takes its source reviews with it: they name it and hold what was said about it, and an
45
+ * erased row leaves nothing pointing at it.
46
+ */
47
+ export declare const eraseSourceReviews: (database: CacheDatabase, target: ReviewTarget) => number;
48
+ /**
49
+ * Rewrites every source review, open or closed, that quotes or names an erased source: a challenge or a suggestion
50
+ * citing it loses its words and references, and with `refs` a source entry naming it loses its reference.
51
+ */
52
+ export declare const scrubSourceReviews: (database: CacheDatabase, erased: (ref: string) => boolean, { refs }: {
53
+ refs: boolean;
54
+ }) => number;
@@ -0,0 +1,143 @@
1
+ import { ERASED_TEXT, graver, SOURCE_REVIEW_CAUSES, SOURCE_REVIEW_KIND, severity, } from "../source-review.js";
2
+ import { botNamed } from "./actors.js";
3
+ const TABLES = { memory: "memories", fact: "facts" };
4
+ export const HOLDING = { memory: ["proposed", "confirmed"], fact: ["proposed", "accepted"] };
5
+ const KEPT = 20;
6
+ export const holdingSql = (alias, type) => `${alias}.deleted_at IS NULL AND ${alias}.status IN (${HOLDING[type].map((status) => `'${status}'`).join(", ")})`;
7
+ export const asReviewTarget = (type, id) => type === "memory" || type === "fact" ? { type, id: Number(id) } : undefined;
8
+ export const holds = (database, target) => database
9
+ .prepare(`SELECT 1 FROM ${TABLES[target.type]} t WHERE t.id = ? AND ${holdingSql("t", target.type)}`)
10
+ .get(target.id) !== undefined;
11
+ /** The rule bot the source check writes its reviews as. */
12
+ export const sourceChecker = (database, at) => botNamed(database, "source-check", at, "rule");
13
+ /** The open source review of a memory or fact, through the proposals' target index. */
14
+ export const sourceReviewOf = (database, target) => {
15
+ const row = database
16
+ .prepare("SELECT id, CASE WHEN json_valid(payload) THEN json_extract(payload, '$.cause') END AS cause " +
17
+ "FROM proposed_actions WHERE target_type = ? AND target_id = ? AND kind = ? AND status = 'proposed' " +
18
+ "ORDER BY id LIMIT 1")
19
+ .get(target.type, target.id, SOURCE_REVIEW_KIND);
20
+ if (!row)
21
+ return null;
22
+ const cause = SOURCE_REVIEW_CAUSES.find((known) => known === row.cause) ?? "changed";
23
+ return { id: String(row.id), cause };
24
+ };
25
+ const iso = (at) => new Date(at).toISOString();
26
+ const payloadOf = (raw) => {
27
+ if (typeof raw !== "string")
28
+ return undefined;
29
+ try {
30
+ return JSON.parse(raw);
31
+ }
32
+ catch {
33
+ return undefined;
34
+ }
35
+ };
36
+ const causeOf = (payload) => payload.sources.reduce((worst, source) => graver(worst, source.cause), payload.challenges.length ? "contradicted" : "changed");
37
+ /**
38
+ * Opens the target's review, or folds new sources, a challenge and a suggestion into the open one, inside the caller's
39
+ * write. Its `raisedAt` moves only when the cause grows graver, so a review climbs the queue for a worse reason and
40
+ * not for the same one again.
41
+ */
42
+ export const raiseSourceReview = (database, target, change) => {
43
+ const ref = `${target.type}:${target.id}`;
44
+ const { at } = change;
45
+ const open = database
46
+ .prepare("SELECT id, payload FROM proposed_actions WHERE kind = ? AND status = 'proposed' AND target_type = ? " +
47
+ "AND target_id = ? ORDER BY id LIMIT 1")
48
+ .get(SOURCE_REVIEW_KIND, target.type, target.id);
49
+ const before = payloadOf(open?.payload) ?? {
50
+ target: ref,
51
+ cause: "changed",
52
+ raisedAt: iso(at),
53
+ sources: [],
54
+ challenges: [],
55
+ };
56
+ const merged = new Map(before.sources.map((source) => [source.link, source]));
57
+ for (const source of change.sources ?? []) {
58
+ const held = merged.get(source.link);
59
+ merged.set(source.link, held === undefined
60
+ ? source
61
+ : {
62
+ ...held,
63
+ ref: source.ref ?? held.ref,
64
+ cause: graver(held.cause, source.cause),
65
+ ...(source.replacement === undefined ? {} : { replacement: source.replacement }),
66
+ });
67
+ }
68
+ const suggestions = [...(before.suggestions ?? []), ...(change.suggestion ? [change.suggestion] : [])].slice(-KEPT);
69
+ const next = {
70
+ sources: [...merged.values()],
71
+ challenges: [...before.challenges, ...(change.challenge ? [change.challenge] : [])].slice(-KEPT),
72
+ };
73
+ const cause = causeOf(next);
74
+ const payload = {
75
+ target: ref,
76
+ cause,
77
+ raisedAt: open && severity(cause) <= severity(before.cause) ? before.raisedAt : iso(at),
78
+ ...next,
79
+ ...(suggestions.length ? { suggestions } : {}),
80
+ };
81
+ if (!open) {
82
+ const made = database
83
+ .prepare("INSERT INTO proposed_actions (kind, target_type, target_id, payload, status, proposed_by_type, proposed_by_id, " +
84
+ "created_at, updated_at) VALUES (?, ?, ?, ?, 'proposed', ?, ?, ?, ?) RETURNING id")
85
+ .get(SOURCE_REVIEW_KIND, target.type, target.id, JSON.stringify(payload), change.proposer.type, change.proposer.id, at, at);
86
+ return { id: String(made?.id), changed: true };
87
+ }
88
+ if (JSON.stringify(payload) === JSON.stringify(before))
89
+ return { id: String(open.id), changed: false };
90
+ database
91
+ .prepare("UPDATE proposed_actions SET payload = ?, updated_at = ? WHERE id = ?")
92
+ .run(JSON.stringify(payload), at, Number(open.id));
93
+ return { id: String(open.id), changed: true };
94
+ };
95
+ /**
96
+ * Closes the open review of a memory or fact that was corrected, withdrawn or removed, inside the caller's write:
97
+ * what the review asked has been answered by that decision.
98
+ */
99
+ export const closeSourceReviews = (database, target, close) => database
100
+ .prepare("UPDATE proposed_actions SET status = 'executed', decided_by_type = ?, decided_by_id = ?, decided_at = ?, " +
101
+ "executed_at = ?, result = ?, updated_at = ? WHERE target_type = ? AND target_id = ? AND kind = ? " +
102
+ "AND status = 'proposed'")
103
+ .run(close.actor.type, close.actor.id, close.at, close.at, JSON.stringify({ closed: close.why }), close.at, target.type, target.id, SOURCE_REVIEW_KIND).changes;
104
+ /**
105
+ * A memory or fact erased takes its source reviews with it: they name it and hold what was said about it, and an
106
+ * erased row leaves nothing pointing at it.
107
+ */
108
+ export const eraseSourceReviews = (database, target) => database
109
+ .prepare("DELETE FROM proposed_actions WHERE kind = ? AND target_type = ? AND target_id = ?")
110
+ .run(SOURCE_REVIEW_KIND, target.type, target.id).changes;
111
+ /**
112
+ * Rewrites every source review, open or closed, that quotes or names an erased source: a challenge or a suggestion
113
+ * citing it loses its words and references, and with `refs` a source entry naming it loses its reference.
114
+ */
115
+ export const scrubSourceReviews = (database, erased, { refs }) => {
116
+ const update = database.prepare("UPDATE proposed_actions SET payload = ? WHERE id = ?");
117
+ let scrubbed = 0;
118
+ for (const row of database
119
+ .prepare("SELECT id, payload FROM proposed_actions WHERE kind = ?")
120
+ .all(SOURCE_REVIEW_KIND)) {
121
+ const payload = payloadOf(row.payload);
122
+ if (!payload)
123
+ continue;
124
+ const names = (evidence) => (evidence ?? []).some(erased);
125
+ const next = {
126
+ ...payload,
127
+ sources: payload.sources.map((source) => refs && source.ref !== null && erased(source.ref)
128
+ ? { ...source, ref: null, kind: source.kind ?? source.ref.split(":")[0] }
129
+ : source),
130
+ challenges: payload.challenges.map((challenge) => names(challenge.evidence) ? { ...challenge, evidence: [], reason: ERASED_TEXT } : challenge),
131
+ ...(payload.suggestions === undefined
132
+ ? {}
133
+ : {
134
+ suggestions: payload.suggestions.map((suggestion) => names(suggestion.evidence) ? { ...suggestion, evidence: [], body: ERASED_TEXT } : suggestion),
135
+ }),
136
+ };
137
+ if (JSON.stringify(next) === JSON.stringify(payload))
138
+ continue;
139
+ update.run(JSON.stringify(next), Number(row.id));
140
+ scrubbed++;
141
+ }
142
+ return scrubbed;
143
+ };
@@ -0,0 +1,54 @@
1
+ import { type Decider } from "../decisions.js";
2
+ import { type SourceCheck } from "../source-review.js";
3
+ import { type Author } from "./memories.js";
4
+ import type { StoreContext } from "./open.js";
5
+ import { type ProposedAction } from "./proposed-actions.js";
6
+ export interface SourceReviewDecision {
7
+ /** `true`: the memory or fact still holds; `false`: dismiss the review. Either way its sources are signed anew. */
8
+ confirm: boolean;
9
+ by: Decider;
10
+ reason?: string;
11
+ }
12
+ export interface ChallengeInput {
13
+ /** What contradicts it, as references the store holds. */
14
+ evidence: string[];
15
+ reason: string;
16
+ by: Author;
17
+ /** What the challenger would say instead, for the owner to take or leave. */
18
+ suggestion?: string;
19
+ }
20
+ /**
21
+ * Evidence links of memories and facts in id order, from a cursor. The unary `+` keeps SQLite off the `from_type`
22
+ * index, which would read every memory's and fact's links and sort them; the primary key stops at `limit`.
23
+ */
24
+ export declare const SOURCE_WALK: string;
25
+ export declare const sourceReviewOver: (context: StoreContext) => {
26
+ checkSources: ({ limit }?: {
27
+ limit?: number;
28
+ }) => SourceCheck;
29
+ challenge: (reference: string, input: ChallengeInput) => ProposedAction;
30
+ decide: (id: string, decision: SourceReviewDecision) => ProposedAction;
31
+ };
32
+ /** The knowledge store's part that watches what memories and facts cite. */
33
+ export interface SourceReviewStore {
34
+ /**
35
+ * Reads the sources memories and facts that still hold cite, least recently checked first (500 links unless
36
+ * `limit` says), and opens or updates a `memory_source_review` for each one whose sources no longer back it. A
37
+ * changed source that still says the quoted words raises nothing. A link written before signatures is signed in
38
+ * silence unless its source changed or went after it was cited. Status never changes here.
39
+ */
40
+ checkSources(options?: {
41
+ limit?: number;
42
+ }): Promise<SourceCheck>;
43
+ /**
44
+ * Says that a memory or fact is contradicted, citing what contradicts it. It only opens the target's source review
45
+ * or adds to the open one; the owner decides.
46
+ */
47
+ challenge(reference: string, input: ChallengeInput): Promise<ProposedAction>;
48
+ /**
49
+ * Decides a `memory_source_review`, the owner's alone: confirm says it still holds and marks a memory verified now,
50
+ * reject dismisses the review. Both sign its sources again as they are now.
51
+ */
52
+ decideSourceReview(id: string, decision: SourceReviewDecision): Promise<ProposedAction>;
53
+ }
54
+ export declare const sourceReviewStoreOver: (context: StoreContext) => SourceReviewStore;