@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.
Files changed (260) hide show
  1. package/CHANGELOG.md +284 -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 +4 -1
  12. package/dist/cli/index.js +4 -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/cli/store-settings.js +38 -4
  17. package/dist/crm/cli/csv.d.ts +23 -0
  18. package/dist/crm/cli/csv.js +135 -0
  19. package/dist/crm/cli/index.d.ts +1 -1
  20. package/dist/crm/cli/index.js +36 -22
  21. package/dist/crm/cli/pipeline-view.d.ts +3 -0
  22. package/dist/crm/cli/pipeline-view.js +36 -0
  23. package/dist/crm/index.d.ts +2 -2
  24. package/dist/crm/index.js +1 -1
  25. package/dist/crm/source.d.ts +35 -5
  26. package/dist/crm/sync.d.ts +15 -7
  27. package/dist/crm/sync.js +169 -11
  28. package/dist/crm/writes.d.ts +6 -4
  29. package/dist/crm/writes.js +17 -13
  30. package/dist/documents.d.ts +2 -0
  31. package/dist/documents.js +2 -0
  32. package/dist/domain/business.d.ts +79 -0
  33. package/dist/domain/business.js +42 -0
  34. package/dist/domain/crm.d.ts +41 -2
  35. package/dist/domain/deals.d.ts +5 -2
  36. package/dist/domain/deals.js +5 -2
  37. package/dist/domain/finance.d.ts +2 -0
  38. package/dist/domain/finance.js +2 -2
  39. package/dist/domain/google-documents.d.ts +2 -0
  40. package/dist/domain/google-documents.js +3 -1
  41. package/dist/domain/money.d.ts +2 -0
  42. package/dist/domain/money.js +11 -3
  43. package/dist/domain/providers.d.ts +36 -1
  44. package/dist/domain/providers.js +69 -7
  45. package/dist/domain/record-fields.d.ts +42 -0
  46. package/dist/domain/record-fields.js +122 -0
  47. package/dist/domain/record-types.d.ts +489 -16
  48. package/dist/domain/record-types.js +374 -7
  49. package/dist/domain/records.d.ts +96 -5
  50. package/dist/domain/records.js +178 -32
  51. package/dist/domain/ref-summaries.js +5 -0
  52. package/dist/domain/references.d.ts +1 -1
  53. package/dist/domain/references.js +5 -0
  54. package/dist/domain/relations.d.ts +125 -0
  55. package/dist/domain/relations.js +107 -0
  56. package/dist/domain/vcard.d.ts +25 -0
  57. package/dist/domain/vcard.js +267 -0
  58. package/dist/entities/deal.d.ts +16 -2
  59. package/dist/entities/deal.js +5 -1
  60. package/dist/entities/index.d.ts +29 -13
  61. package/dist/entities/relationship.d.ts +8 -2
  62. package/dist/entities/relationship.js +8 -2
  63. package/dist/entities/task-results.d.ts +10 -10
  64. package/dist/errors/reasons.js +73 -2
  65. package/dist/fetch/addresses.d.ts +5 -0
  66. package/dist/fetch/addresses.js +76 -0
  67. package/dist/fetch/page.d.ts +64 -0
  68. package/dist/fetch/page.js +256 -0
  69. package/dist/index.d.ts +7 -4
  70. package/dist/index.js +6 -3
  71. package/dist/mail/cli/review.js +20 -6
  72. package/dist/mail/index.d.ts +1 -1
  73. package/dist/mail/marks.d.ts +10 -5
  74. package/dist/mail/marks.js +16 -8
  75. package/dist/mail/review.d.ts +5 -0
  76. package/dist/meetings/calendar-text.d.ts +1 -1
  77. package/dist/meetings/calendar-text.js +4 -0
  78. package/dist/meetings/event-store.d.ts +2 -2
  79. package/dist/search/account-list.d.ts +4 -0
  80. package/dist/search/account-list.js +19 -8
  81. package/dist/search/query.js +2 -2
  82. package/dist/services/events-search.js +2 -2
  83. package/dist/services/index.d.ts +1 -1
  84. package/dist/services/index.js +1 -1
  85. package/dist/services/ingest.d.ts +56 -8
  86. package/dist/services/ingest.js +95 -61
  87. package/dist/services/link-scorers/scan.js +2 -1
  88. package/dist/services/link-scorers/shared.js +2 -1
  89. package/dist/services/messages-search.js +3 -2
  90. package/dist/services/metadata.d.ts +1 -1
  91. package/dist/services/notes-search.js +1 -0
  92. package/dist/services/project-status/index.js +2 -1
  93. package/dist/services/proposals/kinds/contracts.d.ts +4 -0
  94. package/dist/services/proposals/kinds/contracts.js +9 -0
  95. package/dist/services/proposals/kinds/finance.d.ts +4 -0
  96. package/dist/services/proposals/kinds/finance.js +14 -35
  97. package/dist/services/proposals/kinds/orders.js +7 -30
  98. package/dist/services/proposals/kinds/shared.d.ts +27 -1
  99. package/dist/services/proposals/kinds/shared.js +34 -0
  100. package/dist/services/proposals/kinds/source-review.d.ts +4 -0
  101. package/dist/services/proposals/kinds/source-review.js +45 -0
  102. package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
  103. package/dist/services/proposals/kinds/subscriptions.js +24 -0
  104. package/dist/services/proposals/kinds/transactions.d.ts +4 -0
  105. package/dist/services/proposals/kinds/transactions.js +10 -0
  106. package/dist/services/proposals/registry.js +9 -0
  107. package/dist/services/search-all-meetings.js +2 -2
  108. package/dist/services/search-all.js +4 -3
  109. package/dist/services/search-kind.js +3 -3
  110. package/dist/services/stored-project-gathering.js +4 -3
  111. package/dist/store/decisions.js +5 -1
  112. package/dist/store/exports.d.ts +1 -0
  113. package/dist/store/exports.js +1 -0
  114. package/dist/store/index.d.ts +20 -4
  115. package/dist/store/index.js +4 -0
  116. package/dist/store/people.js +1 -1
  117. package/dist/store/project-transfer/request.d.ts +4 -1
  118. package/dist/store/project-transfer/schema.d.ts +9 -2
  119. package/dist/store/project-transfer/schema.js +4 -0
  120. package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
  121. package/dist/store/source-review.d.ts +64 -0
  122. package/dist/store/source-review.js +7 -0
  123. package/dist/store/sqlite/accounts.d.ts +1 -1
  124. package/dist/store/sqlite/accounts.js +24 -1
  125. package/dist/store/sqlite/added-sources.d.ts +82 -0
  126. package/dist/store/sqlite/added-sources.js +237 -0
  127. package/dist/store/sqlite/business-contracts.d.ts +90 -0
  128. package/dist/store/sqlite/business-contracts.js +182 -0
  129. package/dist/store/sqlite/calendar-events.js +7 -3
  130. package/dist/store/sqlite/calendar-forget.js +3 -0
  131. package/dist/store/sqlite/calendar.d.ts +21 -1
  132. package/dist/store/sqlite/calendar.js +27 -7
  133. package/dist/store/sqlite/chat-rekey.js +7 -0
  134. package/dist/store/sqlite/chats.d.ts +3 -1
  135. package/dist/store/sqlite/chats.js +15 -1
  136. package/dist/store/sqlite/crm-rows.d.ts +44 -0
  137. package/dist/store/sqlite/crm-rows.js +195 -0
  138. package/dist/store/sqlite/crm-settings.d.ts +28 -0
  139. package/dist/store/sqlite/crm-settings.js +80 -0
  140. package/dist/store/sqlite/crm.d.ts +42 -11
  141. package/dist/store/sqlite/crm.js +402 -196
  142. package/dist/store/sqlite/deals.d.ts +4 -8
  143. package/dist/store/sqlite/deals.js +8 -5
  144. package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
  145. package/dist/store/sqlite/emails.js +2 -0
  146. package/dist/store/sqlite/entity-types.d.ts +14 -9
  147. package/dist/store/sqlite/entity-types.js +4 -10
  148. package/dist/store/sqlite/evidence-signature.d.ts +114 -0
  149. package/dist/store/sqlite/evidence-signature.js +349 -0
  150. package/dist/store/sqlite/evidence-sources.d.ts +2 -1
  151. package/dist/store/sqlite/evidence-sources.js +7 -3
  152. package/dist/store/sqlite/facts.d.ts +7 -2
  153. package/dist/store/sqlite/facts.js +21 -5
  154. package/dist/store/sqlite/finance.d.ts +18 -105
  155. package/dist/store/sqlite/finance.js +100 -219
  156. package/dist/store/sqlite/involvement-queue.js +4 -3
  157. package/dist/store/sqlite/knowledge.d.ts +14 -4
  158. package/dist/store/sqlite/knowledge.js +108 -102
  159. package/dist/store/sqlite/labelled.d.ts +3 -0
  160. package/dist/store/sqlite/labelled.js +11 -0
  161. package/dist/store/sqlite/link-kinds.d.ts +4 -3
  162. package/dist/store/sqlite/link-kinds.js +6 -2
  163. package/dist/store/sqlite/manifest.js +7 -0
  164. package/dist/store/sqlite/memories.d.ts +24 -10
  165. package/dist/store/sqlite/memories.js +73 -19
  166. package/dist/store/sqlite/messages.js +2 -0
  167. package/dist/store/sqlite/migrations.generated.js +134 -0
  168. package/dist/store/sqlite/money-lines.d.ts +48 -0
  169. package/dist/store/sqlite/money-lines.js +76 -0
  170. package/dist/store/sqlite/money-transactions.d.ts +132 -0
  171. package/dist/store/sqlite/money-transactions.js +295 -0
  172. package/dist/store/sqlite/notes.d.ts +14 -4
  173. package/dist/store/sqlite/notes.js +43 -31
  174. package/dist/store/sqlite/orders.d.ts +36 -6
  175. package/dist/store/sqlite/orders.js +126 -46
  176. package/dist/store/sqlite/organizations.d.ts +8 -0
  177. package/dist/store/sqlite/organizations.js +14 -0
  178. package/dist/store/sqlite/payment-facts.js +1 -1
  179. package/dist/store/sqlite/person-preparation.d.ts +1 -1
  180. package/dist/store/sqlite/person-preparation.js +3 -2
  181. package/dist/store/sqlite/private-people.js +2 -0
  182. package/dist/store/sqlite/products.d.ts +78 -0
  183. package/dist/store/sqlite/products.js +131 -0
  184. package/dist/store/sqlite/project-transfer/records.js +39 -3
  185. package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
  186. package/dist/store/sqlite/project-transfer/snapshot.js +11 -3
  187. package/dist/store/sqlite/proposed-actions.d.ts +1 -1
  188. package/dist/store/sqlite/proposed-actions.js +24 -7
  189. package/dist/store/sqlite/providers.d.ts +0 -4
  190. package/dist/store/sqlite/providers.js +20 -2
  191. package/dist/store/sqlite/record-matching.d.ts +80 -0
  192. package/dist/store/sqlite/record-matching.js +264 -0
  193. package/dist/store/sqlite/record-projection.d.ts +89 -0
  194. package/dist/store/sqlite/record-projection.js +50 -0
  195. package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
  196. package/dist/store/sqlite/record-projectors-direct.js +125 -0
  197. package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
  198. package/dist/store/sqlite/record-projectors-money.js +214 -0
  199. package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
  200. package/dist/store/sqlite/record-projectors-phone.js +212 -0
  201. package/dist/store/sqlite/record-projectors.d.ts +3 -44
  202. package/dist/store/sqlite/record-projectors.js +7 -67
  203. package/dist/store/sqlite/record-relations.d.ts +24 -0
  204. package/dist/store/sqlite/record-relations.js +92 -0
  205. package/dist/store/sqlite/record-remap.d.ts +12 -0
  206. package/dist/store/sqlite/record-remap.js +92 -0
  207. package/dist/store/sqlite/record-run.d.ts +71 -0
  208. package/dist/store/sqlite/record-run.js +179 -0
  209. package/dist/store/sqlite/record-settling.d.ts +9 -0
  210. package/dist/store/sqlite/record-settling.js +74 -0
  211. package/dist/store/sqlite/record-stamps.d.ts +13 -0
  212. package/dist/store/sqlite/record-stamps.js +30 -0
  213. package/dist/store/sqlite/record-writes.d.ts +43 -0
  214. package/dist/store/sqlite/record-writes.js +307 -0
  215. package/dist/store/sqlite/records.d.ts +52 -42
  216. package/dist/store/sqlite/records.js +123 -353
  217. package/dist/store/sqlite/ref-summaries.d.ts +6 -5
  218. package/dist/store/sqlite/ref-summaries.js +60 -8
  219. package/dist/store/sqlite/refunds.d.ts +31 -0
  220. package/dist/store/sqlite/refunds.js +112 -0
  221. package/dist/store/sqlite/relation-list.d.ts +43 -0
  222. package/dist/store/sqlite/relation-list.js +133 -0
  223. package/dist/store/sqlite/relation-proposals.d.ts +28 -6
  224. package/dist/store/sqlite/relation-proposals.js +134 -10
  225. package/dist/store/sqlite/relation-reads.d.ts +8 -0
  226. package/dist/store/sqlite/relation-reads.js +7 -0
  227. package/dist/store/sqlite/relation-roles.d.ts +26 -0
  228. package/dist/store/sqlite/relation-roles.js +91 -0
  229. package/dist/store/sqlite/reported-relations.d.ts +63 -0
  230. package/dist/store/sqlite/reported-relations.js +132 -0
  231. package/dist/store/sqlite/reviewed.d.ts +109 -0
  232. package/dist/store/sqlite/reviewed.js +173 -0
  233. package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
  234. package/dist/store/sqlite/schema/link-revisions.js +11 -0
  235. package/dist/store/sqlite/schema/links.d.ts +360 -0
  236. package/dist/store/sqlite/schema/links.js +32 -0
  237. package/dist/store/sqlite/schema.d.ts +2769 -306
  238. package/dist/store/sqlite/schema.js +258 -25
  239. package/dist/store/sqlite/source-review-marks.d.ts +54 -0
  240. package/dist/store/sqlite/source-review-marks.js +143 -0
  241. package/dist/store/sqlite/source-review.d.ts +54 -0
  242. package/dist/store/sqlite/source-review.js +221 -0
  243. package/dist/store/sqlite/stage-amounts.d.ts +14 -0
  244. package/dist/store/sqlite/stage-amounts.js +23 -0
  245. package/dist/store/sqlite/subscriptions.d.ts +135 -0
  246. package/dist/store/sqlite/subscriptions.js +294 -0
  247. package/dist/store/sqlite/text-input.d.ts +3 -0
  248. package/dist/store/sqlite/text-input.js +13 -0
  249. package/dist/store/sqlite/things.js +2 -9
  250. package/dist/store/sqlite/transaction.d.ts +15 -0
  251. package/dist/store/sqlite/transaction.js +57 -0
  252. package/dist/store/sqlite/wallets.d.ts +88 -0
  253. package/dist/store/sqlite/wallets.js +143 -0
  254. package/dist/store/store-errors.d.ts +1 -0
  255. package/dist/store/store-errors.js +45 -0
  256. package/dist/store/store.d.ts +19 -1
  257. package/dist/store/store.js +35 -6
  258. package/package.json +6 -3
  259. package/schemas/examples/records-business.jsonl +11 -0
  260. package/schemas/records-envelope.v1.schema.json +1160 -5
@@ -0,0 +1,109 @@
1
+ import type { FinanceReview } from "../../domain/finance.js";
2
+ import { type DecidedTable, type DecidedType } from "../../domain/record-types.js";
3
+ import { type Decider } from "../decisions.js";
4
+ import { type Actor } from "./actors.js";
5
+ import { type Author, type Memory } from "./memories.js";
6
+ import type { StoreContext } from "./open.js";
7
+ type Row = Record<string, unknown>;
8
+ /** Who wrote a row: `name` is a bot's handle and `kind` whether it is an agent or a rule; both `null` for a person. */
9
+ export type AuthorInfo = Memory["author"];
10
+ /** A `person:` or `organization:` on an invoice or a payment, with its name so a list needs no lookup. */
11
+ export interface Party {
12
+ ref: string;
13
+ name: string | null;
14
+ /** The owner's own person row. */
15
+ owner: boolean;
16
+ }
17
+ export interface Reviewed {
18
+ review: FinanceReview;
19
+ /** `name` is a bot's handle and `kind` whether it is an agent or a rule; both `null` for a person. */
20
+ author: AuthorInfo;
21
+ model: string | null;
22
+ confirmedBy: {
23
+ type: Actor["type"];
24
+ id: string;
25
+ } | null;
26
+ source: string;
27
+ /** Why an agent or an import proposed it. */
28
+ reason: string | null;
29
+ /** What it was read from: the messages, documents or notes an agent or the owner cited. */
30
+ evidence: string[];
31
+ project: string | null;
32
+ projectName: string | null;
33
+ createdAt: string;
34
+ updatedAt: string;
35
+ }
36
+ export interface Written {
37
+ /** The owner's entry is confirmed at once; a bot's waits as proposed for the owner. */
38
+ by: Author;
39
+ project?: string;
40
+ /** Where a bot's row came from, such as `agent` or an importer's name; the owner's is always `owner`. */
41
+ source?: string;
42
+ /** Why a bot proposes it, kept for the reviewer. */
43
+ reason?: string;
44
+ /** References it was read from, linked as `evidence`. */
45
+ evidence?: string[];
46
+ }
47
+ /** One article, for the first prefix only: `an invoice: or payment:`. */
48
+ export declare const prefixesText: (types: readonly string[]) => string;
49
+ export declare const refuse: (message: string, reason: string, code?: "validation_error" | "permission_error") => never;
50
+ export declare const oneOf: <T extends string>(value: unknown, allowed: readonly T[], what: string) => T;
51
+ export declare const text: (value: unknown, what: string, limit: number, reason?: string) => string | null;
52
+ export declare const isoDate: (value: unknown, what: string) => string | null;
53
+ export declare const iso: (value: unknown) => string;
54
+ export declare const reasonOf: (metadata: unknown) => string | null;
55
+ /** A list's page size: 1–500, 100 when left out. */
56
+ export declare const limitOf: (limit: number | undefined) => number;
57
+ export interface Decision {
58
+ by: Decider;
59
+ reason?: string;
60
+ confirm: boolean;
61
+ }
62
+ /** A reviewed row the owner decides; its `author_name` comes from the bots join every reviewed query makes. */
63
+ export declare const decisionsOver: (context: StoreContext) => {
64
+ keepDecision: (table: DecidedTable, id: number, by: Decider, reason?: string) => void;
65
+ decideRow: (type: DecidedType, row: Row, { by, reason, confirm }: Decision, within?: {
66
+ before?: () => void;
67
+ after?: (confirmer: Actor | null) => void;
68
+ }) => void;
69
+ mayChangeParts: (row: Row, by: Author, message: string, reason?: string) => void;
70
+ };
71
+ /** What invoices, payments and orders share: their parties, who wrote them, and how the owner decides them. */
72
+ export declare const reviewedRowsOver: (context: StoreContext) => {
73
+ keepDecision: (table: DecidedTable, id: number, by: Decider, reason?: string) => void;
74
+ decideRow: (type: DecidedType, row: Row, { by, reason, confirm }: Decision, within?: {
75
+ before?: () => void;
76
+ after?: (confirmer: Actor | null) => void;
77
+ }) => void;
78
+ mayChangeParts: (row: Row, by: Author, message: string, reason?: string) => void;
79
+ partyOf: (type: unknown, id: unknown) => Party | null;
80
+ partyThing: (reference: string | undefined, what: string) => import("./things.js").Thing | undefined;
81
+ reviewedOf: (row: Row, type: DecidedType | "deal") => Reviewed;
82
+ written: (input: Written) => {
83
+ owner: boolean;
84
+ actor: Actor;
85
+ model: string | null;
86
+ source: string;
87
+ review: string;
88
+ confirmedType: "bot" | "person" | null;
89
+ confirmedId: number | null;
90
+ metadata: string | null;
91
+ project: number | null;
92
+ evidence: string[];
93
+ };
94
+ keepEvidence: (type: DecidedType | "deal", id: number, by: ReturnType<(input: Written) => {
95
+ owner: boolean;
96
+ actor: Actor;
97
+ model: string | null;
98
+ source: string;
99
+ review: string;
100
+ confirmedType: "bot" | "person" | null;
101
+ confirmedId: number | null;
102
+ metadata: string | null;
103
+ project: number | null;
104
+ evidence: string[];
105
+ }>) => void;
106
+ ownerOnly: (by: Author, what: string) => void;
107
+ linkedId: <T extends string>(reference: string | undefined, type: T, reason: string) => number | null;
108
+ };
109
+ export {};
@@ -0,0 +1,173 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ import { article, DECIDED_TABLES } from "../../domain/record-types.js";
3
+ import { assertMayDecide, botKey, deciderAuthor } from "../decisions.js";
4
+ import { proposerOf } from "./actors.js";
5
+ import { authorActor, authorOf, evidenceOf, linkEvidence, projectId, projectOf, subjectOf, } from "./memories.js";
6
+ import { requiredThing } from "./notes.js";
7
+ import { write } from "./transaction.js";
8
+ /** One article, for the first prefix only: `an invoice: or payment:`. */
9
+ export const prefixesText = (types) => `${article(types[0] ?? "")} ${types.map((type) => `${type}:`).join(" or ")}`;
10
+ export const refuse = (message, reason, code = "validation_error") => {
11
+ throw new CliError(code, message, { reason });
12
+ };
13
+ export const oneOf = (value, allowed, what) => {
14
+ if (typeof value !== "string" || !allowed.includes(value))
15
+ return refuse(`${what} is one of ${allowed.join(", ")}`, "finance.field");
16
+ return value;
17
+ };
18
+ export const text = (value, what, limit, reason = "finance.field") => {
19
+ if (value === undefined || value === null)
20
+ return null;
21
+ if (typeof value !== "string" || !value.trim() || value.length > limit || value.includes("\0"))
22
+ return refuse(`${what} takes 1–${limit} characters`, reason);
23
+ return value.trim();
24
+ };
25
+ export const isoDate = (value, what) => {
26
+ if (value === undefined || value === null)
27
+ return null;
28
+ const date = typeof value === "string" ? value.trim() : "";
29
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || new Date(`${date}T00:00:00Z`).toISOString().slice(0, 10) !== date)
30
+ return refuse(`${what} is a date such as 2026-11-15`, "finance.field");
31
+ return date;
32
+ };
33
+ export const iso = (value) => new Date(Number(value)).toISOString();
34
+ const evidenceList = (value) => {
35
+ if (value === undefined)
36
+ return [];
37
+ if (!Array.isArray(value) || value.length > 8 || value.some((one) => typeof one !== "string" || !one.trim()))
38
+ return refuse("evidence is a list of at most 8 references", "finance.field");
39
+ return [...new Set(value.map((one) => one.trim()))];
40
+ };
41
+ export const reasonOf = (metadata) => {
42
+ if (metadata == null)
43
+ return null;
44
+ try {
45
+ const { reason } = JSON.parse(String(metadata));
46
+ return typeof reason === "string" ? reason : null;
47
+ }
48
+ catch {
49
+ return null;
50
+ }
51
+ };
52
+ /** A list's page size: 1–500, 100 when left out. */
53
+ export const limitOf = (limit) => Math.min(Math.max(limit ?? 100, 1), 500);
54
+ /** A reviewed row the owner decides; its `author_name` comes from the bots join every reviewed query makes. */
55
+ export const decisionsOver = (context) => {
56
+ const { database, now } = context;
57
+ const keepDecision = (table, id, by, reason) => {
58
+ const why = [by === "owner" ? undefined : by.reason.trim(), reason?.trim() || undefined].filter(Boolean).join(": ");
59
+ if (by === "owner" && !why)
60
+ return;
61
+ const decision = by === "owner" ? { by: "owner", reason: why } : { by: "agent", bot: by.bot, reason: why };
62
+ database
63
+ .prepare(`UPDATE ${table} SET metadata = json_set(coalesce(metadata, '{}'), '$.decision', json(?)) WHERE id = ?`)
64
+ .run(JSON.stringify(decision), id);
65
+ };
66
+ /**
67
+ * Confirms or rejects a proposed row; only the owner decides money. `before` runs the checks that may still refuse
68
+ * and `after` what follows from the decision, both in its transaction.
69
+ */
70
+ const decideRow = (type, row, { by, reason, confirm }, within = {}) => {
71
+ const ref = `${type}:${row.id}`;
72
+ assertMayDecide({ id: ref, proposedBy: proposerOf(database, row.author_type, row.author_id), ownerOnly: true }, by, context.decisions);
73
+ if (row.review !== "proposed")
74
+ throw new CliError("validation_error", `${ref} is already ${String(row.review)}`, {
75
+ reason: "proposal.wrong-state",
76
+ });
77
+ const table = DECIDED_TABLES[type];
78
+ const id = Number(row.id);
79
+ write(database, () => {
80
+ within.before?.();
81
+ const at = now();
82
+ const confirmer = confirm ? authorActor(context, deciderAuthor(by)).actor : null;
83
+ if (confirmer)
84
+ database
85
+ .prepare(`UPDATE ${table} SET review = 'confirmed', confirmed_by_type = ?, confirmed_by_id = ?, updated_at = ? WHERE id = ?`)
86
+ .run(confirmer.type, confirmer.id, at, id);
87
+ else
88
+ database.prepare(`UPDATE ${table} SET review = 'rejected', updated_at = ? WHERE id = ?`).run(at, id);
89
+ keepDecision(table, id, by, reason);
90
+ within.after?.(confirmer);
91
+ });
92
+ };
93
+ /**
94
+ * The one rule for changing a reviewed row or its lines, allocations and splits: the owner changes any; a bot only
95
+ * a row it proposed that is still proposed, as its own draft. A confirmed row is the owner's.
96
+ */
97
+ const mayChangeParts = (row, by, message, reason = "finance.owner-only") => {
98
+ if (by === "owner")
99
+ return;
100
+ const own = row.author_type === "bot" && row.author_name != null && botKey(String(row.author_name)) === botKey(by.bot);
101
+ if (row.review !== "proposed" || !own)
102
+ throw new CliError("permission_error", message, { reason });
103
+ };
104
+ return { keepDecision, decideRow, mayChangeParts };
105
+ };
106
+ /** What invoices, payments and orders share: their parties, who wrote them, and how the owner decides them. */
107
+ export const reviewedRowsOver = (context) => {
108
+ const { database } = context;
109
+ const partyOf = (type, id) => {
110
+ if (type == null)
111
+ return null;
112
+ const { subject, subjectName, subjectIsOwner } = subjectOf(database, { subject_type: type, subject_id: id });
113
+ return subject === null ? null : { ref: subject, name: subjectName, owner: subjectIsOwner };
114
+ };
115
+ const partyThing = (reference, what) => {
116
+ if (reference === undefined)
117
+ return undefined;
118
+ const thing = requiredThing(database, reference, what);
119
+ if (thing.type !== "person" && thing.type !== "organization")
120
+ return refuse(`${what} is a person: or organization: reference, not ${reference}`, "finance.party");
121
+ return thing;
122
+ };
123
+ const reviewedOf = (row, type) => ({
124
+ review: row.review,
125
+ author: authorOf(row),
126
+ model: row.model == null ? null : String(row.model),
127
+ confirmedBy: row.confirmed_by_type == null
128
+ ? null
129
+ : { type: row.confirmed_by_type, id: String(row.confirmed_by_id) },
130
+ source: String(row.source),
131
+ reason: reasonOf(row.metadata),
132
+ evidence: evidenceOf(context, { type, id: Number(row.id) }),
133
+ ...projectOf(database, row),
134
+ createdAt: iso(row.created_at),
135
+ updatedAt: iso(row.updated_at),
136
+ });
137
+ const written = (input) => {
138
+ const owner = input.by === "owner";
139
+ const { actor, model } = authorActor(context, input.by);
140
+ const source = owner ? "owner" : (text(input.source, "a source", 80) ?? "agent");
141
+ const reason = owner ? null : text(input.reason, "a reason", 2000);
142
+ return {
143
+ owner,
144
+ actor,
145
+ model,
146
+ source,
147
+ review: owner ? "confirmed" : "proposed",
148
+ confirmedType: owner ? actor.type : null,
149
+ confirmedId: owner ? actor.id : null,
150
+ metadata: reason === null ? null : JSON.stringify({ reason }),
151
+ project: input.project === undefined ? null : projectId(database, input.project),
152
+ evidence: evidenceList(input.evidence),
153
+ };
154
+ };
155
+ const keepEvidence = (type, id, by) => {
156
+ if (by.evidence.length)
157
+ linkEvidence(context, { type, id }, by.evidence, by.owner ? "owner" : "agent");
158
+ };
159
+ const ownerOnly = (by, what) => {
160
+ if (by !== "owner")
161
+ refuse(`only the owner ${what}`, "finance.owner-only", "permission_error");
162
+ };
163
+ /** A typed ref of one kind, as its row id; `undefined` passes through. */
164
+ const linkedId = (reference, type, reason) => {
165
+ if (reference === undefined)
166
+ return null;
167
+ const thing = requiredThing(database, reference, `the ${type}`);
168
+ if (thing.type !== type)
169
+ return refuse(`${reference} is not ${prefixesText([type])} reference`, reason);
170
+ return thing.id;
171
+ };
172
+ return { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly, linkedId, ...decisionsOver(context) };
173
+ };
@@ -0,0 +1,136 @@
1
+ export declare const linkRevisions: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
2
+ name: "link_revisions";
3
+ schema: undefined;
4
+ columns: {
5
+ id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
6
+ name: string;
7
+ tableName: "link_revisions";
8
+ dataType: "number int53";
9
+ data: number;
10
+ driverParam: number;
11
+ notNull: true;
12
+ hasDefault: true;
13
+ isPrimaryKey: true;
14
+ isAutoincrement: false;
15
+ hasRuntimeDefault: false;
16
+ enumValues: undefined;
17
+ baseColumn: never;
18
+ identity: undefined;
19
+ generated: undefined;
20
+ }, {}>;
21
+ linkId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
22
+ name: string;
23
+ tableName: "link_revisions";
24
+ dataType: "number int53";
25
+ data: number;
26
+ driverParam: number;
27
+ notNull: true;
28
+ hasDefault: false;
29
+ isPrimaryKey: false;
30
+ isAutoincrement: false;
31
+ hasRuntimeDefault: false;
32
+ enumValues: undefined;
33
+ baseColumn: never;
34
+ identity: undefined;
35
+ generated: undefined;
36
+ }, {}>;
37
+ change: import("drizzle-orm/sqlite-core").SQLiteColumn<{
38
+ name: string;
39
+ tableName: "link_revisions";
40
+ dataType: "string";
41
+ data: string;
42
+ driverParam: string;
43
+ notNull: true;
44
+ hasDefault: false;
45
+ isPrimaryKey: false;
46
+ isAutoincrement: false;
47
+ hasRuntimeDefault: false;
48
+ enumValues: [string, ...string[]];
49
+ baseColumn: never;
50
+ identity: undefined;
51
+ generated: undefined;
52
+ }, {}>;
53
+ before: import("drizzle-orm/sqlite-core").SQLiteColumn<{
54
+ name: string;
55
+ tableName: "link_revisions";
56
+ dataType: "string";
57
+ data: string;
58
+ driverParam: string;
59
+ notNull: false;
60
+ hasDefault: false;
61
+ isPrimaryKey: false;
62
+ isAutoincrement: false;
63
+ hasRuntimeDefault: false;
64
+ enumValues: [string, ...string[]];
65
+ baseColumn: never;
66
+ identity: undefined;
67
+ generated: undefined;
68
+ }, {}>;
69
+ after: import("drizzle-orm/sqlite-core").SQLiteColumn<{
70
+ name: string;
71
+ tableName: "link_revisions";
72
+ dataType: "string";
73
+ data: string;
74
+ driverParam: string;
75
+ notNull: false;
76
+ hasDefault: false;
77
+ isPrimaryKey: false;
78
+ isAutoincrement: false;
79
+ hasRuntimeDefault: false;
80
+ enumValues: [string, ...string[]];
81
+ baseColumn: never;
82
+ identity: undefined;
83
+ generated: undefined;
84
+ }, {}>;
85
+ author: import("drizzle-orm/sqlite-core").SQLiteColumn<{
86
+ name: string;
87
+ tableName: "link_revisions";
88
+ dataType: "string";
89
+ data: string;
90
+ driverParam: string;
91
+ notNull: false;
92
+ hasDefault: false;
93
+ isPrimaryKey: false;
94
+ isAutoincrement: false;
95
+ hasRuntimeDefault: false;
96
+ enumValues: [string, ...string[]];
97
+ baseColumn: never;
98
+ identity: undefined;
99
+ generated: undefined;
100
+ }, {}>;
101
+ accountId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
102
+ name: string;
103
+ tableName: "link_revisions";
104
+ dataType: "number int53";
105
+ data: number;
106
+ driverParam: number;
107
+ notNull: false;
108
+ hasDefault: false;
109
+ isPrimaryKey: false;
110
+ isAutoincrement: false;
111
+ hasRuntimeDefault: false;
112
+ enumValues: undefined;
113
+ baseColumn: never;
114
+ identity: undefined;
115
+ generated: undefined;
116
+ }, {}>;
117
+ createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
118
+ name: string;
119
+ tableName: "link_revisions";
120
+ dataType: "number int53";
121
+ data: number;
122
+ driverParam: number;
123
+ notNull: true;
124
+ hasDefault: false;
125
+ isPrimaryKey: false;
126
+ isAutoincrement: false;
127
+ hasRuntimeDefault: false;
128
+ enumValues: undefined;
129
+ baseColumn: never;
130
+ identity: undefined;
131
+ generated: undefined;
132
+ }, {}>;
133
+ };
134
+ dialect: 'sqlite';
135
+ isAlias: false;
136
+ }>;
@@ -0,0 +1,11 @@
1
+ import { index, integer, sqliteTable, text } from "../drizzle/core.js";
2
+ export const linkRevisions = sqliteTable("link_revisions", {
3
+ id: integer("id").primaryKey(),
4
+ linkId: integer("link_id").notNull(),
5
+ change: text("change").notNull(),
6
+ before: text("before"),
7
+ after: text("after"),
8
+ author: text("author"),
9
+ accountId: integer("account_id"),
10
+ createdAt: integer("created_at").notNull(),
11
+ }, (table) => [index("link_revisions_by_link_id_created_at").on(table.linkId, table.createdAt)]);