@wirecat/cli-core 0.605.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.
- package/CHANGELOG.md +284 -1
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/cli/store-settings.js +38 -4
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +36 -22
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/index.d.ts +2 -2
- package/dist/crm/index.js +1 -1
- package/dist/crm/source.d.ts +35 -5
- package/dist/crm/sync.d.ts +15 -7
- package/dist/crm/sync.js +169 -11
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/crm.d.ts +41 -2
- package/dist/domain/deals.d.ts +5 -2
- package/dist/domain/deals.js +5 -2
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/relations.d.ts +125 -0
- package/dist/domain/relations.js +107 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +16 -2
- package/dist/entities/deal.js +5 -1
- package/dist/entities/index.d.ts +29 -13
- package/dist/entities/relationship.d.ts +8 -2
- package/dist/entities/relationship.js +8 -2
- package/dist/entities/task-results.d.ts +10 -10
- package/dist/errors/reasons.js +73 -2
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +6 -3
- package/dist/mail/cli/review.js +20 -6
- package/dist/mail/index.d.ts +1 -1
- package/dist/mail/marks.d.ts +10 -5
- package/dist/mail/marks.js +16 -8
- package/dist/mail/review.d.ts +5 -0
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/link-scorers/scan.js +2 -1
- package/dist/services/link-scorers/shared.js +2 -1
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/project-status/index.js +2 -1
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/services/stored-project-gathering.js +4 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/index.d.ts +20 -4
- package/dist/store/index.js +4 -0
- package/dist/store/people.js +1 -1
- package/dist/store/project-transfer/request.d.ts +4 -1
- package/dist/store/project-transfer/schema.d.ts +9 -2
- package/dist/store/project-transfer/schema.js +4 -0
- package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm-settings.d.ts +28 -0
- package/dist/store/sqlite/crm-settings.js +80 -0
- package/dist/store/sqlite/crm.d.ts +42 -11
- package/dist/store/sqlite/crm.js +402 -196
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +8 -5
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/involvement-queue.js +4 -3
- package/dist/store/sqlite/knowledge.d.ts +14 -4
- package/dist/store/sqlite/knowledge.js +108 -102
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +4 -3
- package/dist/store/sqlite/link-kinds.js +6 -2
- package/dist/store/sqlite/manifest.js +7 -0
- package/dist/store/sqlite/memories.d.ts +24 -10
- package/dist/store/sqlite/memories.js +73 -19
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +134 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +14 -4
- package/dist/store/sqlite/notes.js +43 -31
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/organizations.d.ts +8 -0
- package/dist/store/sqlite/organizations.js +14 -0
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/person-preparation.d.ts +1 -1
- package/dist/store/sqlite/person-preparation.js +3 -2
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +39 -3
- package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +11 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/relation-list.d.ts +43 -0
- package/dist/store/sqlite/relation-list.js +133 -0
- package/dist/store/sqlite/relation-proposals.d.ts +28 -6
- package/dist/store/sqlite/relation-proposals.js +134 -10
- package/dist/store/sqlite/relation-reads.d.ts +8 -0
- package/dist/store/sqlite/relation-reads.js +7 -0
- package/dist/store/sqlite/relation-roles.d.ts +26 -0
- package/dist/store/sqlite/relation-roles.js +91 -0
- package/dist/store/sqlite/reported-relations.d.ts +63 -0
- package/dist/store/sqlite/reported-relations.js +132 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
- package/dist/store/sqlite/schema/link-revisions.js +11 -0
- package/dist/store/sqlite/schema/links.d.ts +360 -0
- package/dist/store/sqlite/schema/links.js +32 -0
- package/dist/store/sqlite/schema.d.ts +2769 -306
- package/dist/store/sqlite/schema.js +258 -25
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +19 -1
- package/dist/store/store.js +35 -6
- package/package.json +6 -3
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
|
@@ -473,6 +473,7 @@ export const pollVotes = sqliteTable("poll_votes", {
|
|
|
473
473
|
unique().on(table.chatId, table.messageExternalId, table.identityId),
|
|
474
474
|
index("poll_votes_by_identity_id").on(table.identityId),
|
|
475
475
|
]);
|
|
476
|
+
export { linkRevisions } from "./schema/link-revisions.js";
|
|
476
477
|
/**
|
|
477
478
|
* One row per field a chat changed, written by a trigger on `chats` in the changing statement's transaction. No foreign
|
|
478
479
|
* keys: an older build purges chats without knowing this table, and a trigger drops a purged chat's rows instead.
|
|
@@ -1425,12 +1426,16 @@ export const invoices = sqliteTable("invoices", {
|
|
|
1425
1426
|
createdAt: integer("created_at").notNull(),
|
|
1426
1427
|
updatedAt: integer("updated_at").notNull(),
|
|
1427
1428
|
deletedAt: integer("deleted_at"),
|
|
1429
|
+
contractId: integer("contract_id").references(() => contracts.id),
|
|
1430
|
+
subscriptionId: integer("subscription_id").references(() => subscriptions.id),
|
|
1428
1431
|
}, (table) => [
|
|
1429
1432
|
index("invoices_by_issuer_type_issuer_id").on(table.issuerType, table.issuerId),
|
|
1430
1433
|
index("invoices_by_counterparty_type_counterparty_id").on(table.counterpartyType, table.counterpartyId),
|
|
1431
1434
|
index("invoices_by_project_id").on(table.projectId),
|
|
1432
1435
|
index("invoices_by_author_type_author_id").on(table.authorType, table.authorId),
|
|
1433
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),
|
|
1434
1439
|
]);
|
|
1435
1440
|
export const invoiceLines = sqliteTable("invoice_lines", {
|
|
1436
1441
|
id: integer("id").primaryKey(),
|
|
@@ -1445,7 +1450,11 @@ export const invoiceLines = sqliteTable("invoice_lines", {
|
|
|
1445
1450
|
amount: integer("amount").notNull(),
|
|
1446
1451
|
createdAt: integer("created_at").notNull(),
|
|
1447
1452
|
updatedAt: integer("updated_at").notNull(),
|
|
1448
|
-
|
|
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
|
+
]);
|
|
1449
1458
|
/** Money that moved to or from the owner, in minor units of its currency. */
|
|
1450
1459
|
export const payments = sqliteTable("payments", {
|
|
1451
1460
|
id: integer("id").primaryKey(),
|
|
@@ -1470,12 +1479,16 @@ export const payments = sqliteTable("payments", {
|
|
|
1470
1479
|
createdAt: integer("created_at").notNull(),
|
|
1471
1480
|
updatedAt: integer("updated_at").notNull(),
|
|
1472
1481
|
deletedAt: integer("deleted_at"),
|
|
1482
|
+
refundOf: integer("refund_of").references(() => payments.id),
|
|
1483
|
+
subscriptionId: integer("subscription_id").references(() => subscriptions.id),
|
|
1473
1484
|
}, (table) => [
|
|
1474
1485
|
index("payments_by_payer_type_payer_id").on(table.payerType, table.payerId),
|
|
1475
1486
|
index("payments_by_payee_type_payee_id").on(table.payeeType, table.payeeId),
|
|
1476
1487
|
index("payments_by_project_id").on(table.projectId),
|
|
1477
1488
|
index("payments_by_author_type_author_id").on(table.authorType, table.authorId),
|
|
1478
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),
|
|
1479
1492
|
]);
|
|
1480
1493
|
/** How much of a payment settles an invoice; triggers keep it within both and in one currency. */
|
|
1481
1494
|
export const paymentAllocations = sqliteTable("payment_allocations", {
|
|
@@ -1526,6 +1539,9 @@ export const orders = sqliteTable("orders", {
|
|
|
1526
1539
|
updatedAt: integer("updated_at").notNull(),
|
|
1527
1540
|
deletedAt: integer("deleted_at"),
|
|
1528
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"),
|
|
1529
1545
|
}, (table) => [
|
|
1530
1546
|
index("orders_by_customer_type_customer_id").on(table.customerType, table.customerId),
|
|
1531
1547
|
index("orders_by_supplier_type_supplier_id").on(table.supplierType, table.supplierId),
|
|
@@ -1534,6 +1550,8 @@ export const orders = sqliteTable("orders", {
|
|
|
1534
1550
|
index("orders_by_project_id").on(table.projectId),
|
|
1535
1551
|
index("orders_by_author_type_author_id").on(table.authorType, table.authorId),
|
|
1536
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),
|
|
1537
1555
|
]);
|
|
1538
1556
|
/** A sales pipeline: the stages a deal or a lead goes through, in order; the owner's own or a CRM account's. */
|
|
1539
1557
|
export const pipelines = sqliteTable("pipelines", {
|
|
@@ -1679,6 +1697,241 @@ export const crmFields = sqliteTable("crm_fields", {
|
|
|
1679
1697
|
updatedAt: integer("updated_at").notNull(),
|
|
1680
1698
|
deletedAt: integer("deleted_at"),
|
|
1681
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
|
+
]);
|
|
1682
1935
|
export { decisionRequests } from "./schema/decision-requests.js";
|
|
1683
1936
|
/** What a source said about one of its records, as it said it: the layer under the entity an import makes. */
|
|
1684
1937
|
export const records = sqliteTable("records", {
|
|
@@ -1698,8 +1951,10 @@ export const records = sqliteTable("records", {
|
|
|
1698
1951
|
createdAt: integer("created_at").notNull(),
|
|
1699
1952
|
updatedAt: integer("updated_at").notNull(),
|
|
1700
1953
|
deletedAt: integer("deleted_at"),
|
|
1954
|
+
createdEntity: integer("created_entity").notNull().default(0),
|
|
1701
1955
|
}, (table) => [
|
|
1702
1956
|
unique().on(table.accountId, table.type, table.externalId),
|
|
1957
|
+
index("records_by_account_id_batch_id").on(table.accountId, table.batchId),
|
|
1703
1958
|
index("records_by_entity_type_entity_id").on(table.entityType, table.entityId),
|
|
1704
1959
|
]);
|
|
1705
1960
|
export const recordRelations = sqliteTable("record_relations", {
|
|
@@ -1713,6 +1968,7 @@ export const recordRelations = sqliteTable("record_relations", {
|
|
|
1713
1968
|
targetId: integer("target_id").references(() => records.id),
|
|
1714
1969
|
createdAt: integer("created_at").notNull(),
|
|
1715
1970
|
amount: text("amount"),
|
|
1971
|
+
asRole: text("as_role"),
|
|
1716
1972
|
}, (table) => [
|
|
1717
1973
|
unique().on(table.recordId, table.role, table.targetType, table.targetExternalId),
|
|
1718
1974
|
index("record_relations_by_target_type_target_external_id").on(table.targetType, table.targetExternalId),
|
|
@@ -1781,30 +2037,7 @@ export const canonicalAutoTagClaims = sqliteTable("canonical_auto_tag_claims", {
|
|
|
1781
2037
|
primaryKey({ columns: [table.chatId, table.tagId] }),
|
|
1782
2038
|
index("canonical_auto_tag_claims_by_tag_id").on(table.tagId),
|
|
1783
2039
|
]);
|
|
1784
|
-
export
|
|
1785
|
-
id: integer("id").primaryKey(),
|
|
1786
|
-
fromType: text("from_type").notNull(),
|
|
1787
|
-
fromId: integer("from_id").notNull(),
|
|
1788
|
-
toType: text("to_type"),
|
|
1789
|
-
toId: integer("to_id"),
|
|
1790
|
-
kind: text("kind").notNull(),
|
|
1791
|
-
anchor: text("anchor"),
|
|
1792
|
-
source: text("source").notNull(),
|
|
1793
|
-
targetText: text("target_text"),
|
|
1794
|
-
targetFolded: text("target_folded"),
|
|
1795
|
-
role: text("role"),
|
|
1796
|
-
evidence: text("evidence"),
|
|
1797
|
-
metadata: text("metadata"),
|
|
1798
|
-
confirmed: integer("confirmed").notNull().default(1),
|
|
1799
|
-
createdAt: integer("created_at").notNull(),
|
|
1800
|
-
author: text("author"),
|
|
1801
|
-
updatedAt: integer("updated_at").notNull(),
|
|
1802
|
-
purpose: text("purpose"),
|
|
1803
|
-
}, (table) => [
|
|
1804
|
-
index("links_unresolved").on(table.targetFolded).where(sql `to_id IS NULL`),
|
|
1805
|
-
index("links_by_from_type_from_id").on(table.fromType, table.fromId),
|
|
1806
|
-
index("links_by_to_type_to_id").on(table.toType, table.toId),
|
|
1807
|
-
]);
|
|
2040
|
+
export { links } from "./schema/links.js";
|
|
1808
2041
|
/**
|
|
1809
2042
|
* Every run of `search messages` and `stats messages show`, with the parameters as the caller gave them — never
|
|
1810
2043
|
* a message or a result. A row with a name is a saved search; an identical unnamed run counts on its row.
|
|
@@ -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;
|