@composable-os/cli 0.0.0-stage → 0.1.0-beta.7
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/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/blocks-HVTAQ5W2.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2P4AZTFL.js +358 -0
- package/dist/chunk-2S7375SC.js +802 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-BEKL3CA2.js +28975 -0
- package/dist/chunk-E5JLTLRM.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-MAXLUQKB.js +1287 -0
- package/dist/chunk-MINQ46RU.js +16485 -0
- package/dist/chunk-STJZJIRA.js +11378 -0
- package/dist/chunk-VL62IMBN.js +1423 -0
- package/dist/chunk-X6I2LWB3.js +24319 -0
- package/dist/chunk-Y7PQ7NIL.js +1178 -0
- package/dist/chunk-ZRWF6QZS.js +2357 -0
- package/dist/cli-BRSACHSG.js +31393 -0
- package/dist/cli-XHC6QBQW.js +121 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-G55G4OLV.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-64BQJA62.js +48 -0
- package/dist/invoke-5SR5ZWW7.js +26 -0
- package/dist/person-AZFEPZOE.js +58 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +112 -0
- package/dist/platform/sdk/contract.ts +766 -0
- package/dist/platform/sdk/document.ts +788 -0
- package/dist/platform/sdk/field-types.ts +106 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +33 -0
- package/dist/platform/sdk/manifest.ts +1484 -0
- package/dist/platform/sdk/media.ts +46 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +173 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +396 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +162 -0
- package/dist/platform/sdk/testing.ts +836 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-4JNFLYXV.js +847 -0
- package/dist/src-J27WXNLY.js +270 -0
- package/dist/src-SSVOBH6L.js +31 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/package.json +33 -4
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// The `sellable` behaviour (stage 27, ADR 0435): an object's records can be put on an order. The
|
|
2
|
+
// host reads the price from the record, refuses a line outside the sale window or over the most
|
|
3
|
+
// one order may hold, and, when the order is first paid, creates what a paid line grants (a
|
|
4
|
+
// ticket per place) in the batch that marks it paid. Its declaration, its manifest schema and its
|
|
5
|
+
// contract checks live here; the host's side is packages/core/src/orders/ (sellable.ts, grants.ts).
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
8
|
+
import { moveOrigins } from "./states.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Where a granted record's field gets its value, when the host creates it:
|
|
12
|
+
*
|
|
13
|
+
* - `item`: the record that was bought (a relation to this sellable object);
|
|
14
|
+
* - `item.<field>`: a field of the record that was bought (its event, its name);
|
|
15
|
+
* - `order`: the order's id (a text field);
|
|
16
|
+
* - `order.person`: the buyer, a relation to a person (empty when the order belongs to no one
|
|
17
|
+
* yet, so the field cannot be required);
|
|
18
|
+
* - `order.email`: the address the order was placed with;
|
|
19
|
+
* - `line`: the order line's id (a text field);
|
|
20
|
+
* - `line.title`: what the line was called when it was bought;
|
|
21
|
+
* - `line.unitPrice`: what one cost, in the currency's major unit;
|
|
22
|
+
* - `code`: a long, random, unguessable code the host makes (a ticket's). A read-only text field;
|
|
23
|
+
* at most one per grant. The host keeps it unique across every granted record.
|
|
24
|
+
*/
|
|
25
|
+
export type GrantSource =
|
|
26
|
+
| "item"
|
|
27
|
+
| `item.${string}`
|
|
28
|
+
| "order"
|
|
29
|
+
| "order.person"
|
|
30
|
+
| "order.email"
|
|
31
|
+
| "line"
|
|
32
|
+
| "line.title"
|
|
33
|
+
| "line.unitPrice"
|
|
34
|
+
| "code";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* What a paid line grants: one record of `object` for each unit bought (two tickets for a line
|
|
38
|
+
* of two), created by the host, as the host, when the order is first paid (the move to `paid`,
|
|
39
|
+
* or to `part_paid` for a deposit), in the same batch. No extension code runs.
|
|
40
|
+
*
|
|
41
|
+
* - `object` is another of this extension's objects. It declares `states`; its records start in
|
|
42
|
+
* the initial state.
|
|
43
|
+
* - `set` names, for each field of the granted record the host fills, where its value comes from
|
|
44
|
+
* (see GrantSource). Every required field of the granted object must be set from a source that
|
|
45
|
+
* always has a value.
|
|
46
|
+
* - `void` is the state the host moves what was granted to when the order is refunded in full, or
|
|
47
|
+
* cancelled after a deposit. Every state but `void` and the final ones needs a `host` move into
|
|
48
|
+
* it. A record already there, or in a final state, is left alone.
|
|
49
|
+
*/
|
|
50
|
+
export interface GrantSpec {
|
|
51
|
+
object: string;
|
|
52
|
+
set: Readonly<Record<string, GrantSource>>;
|
|
53
|
+
void: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Something that can be bought (ADR 0435). An order line may point only at a record of an object
|
|
58
|
+
* that declares it; the host prices the line from the record, so a caller never sends a price.
|
|
59
|
+
* Only a shipped pack or the org's own extension may declare it: selling to the public is a
|
|
60
|
+
* reviewed change, so a runtime extension that does is refused at check and install.
|
|
61
|
+
*
|
|
62
|
+
* - `price`: this object's currency (or number) field holding the price of one, in the currency's
|
|
63
|
+
* major unit. A record with no price cannot be bought.
|
|
64
|
+
* - `currency`: what the price is in (ISO 4217). Left out, the price field's own currency, and
|
|
65
|
+
* with neither, the deployment's. A line priced in another currency than the deployment's is
|
|
66
|
+
* refused.
|
|
67
|
+
* - `saleWindow`: the date or datetime fields saying when a record is on sale, from `from` and
|
|
68
|
+
* until `until` (a date: the whole day, in UTC). Empty values leave that end open. A line
|
|
69
|
+
* outside it is refused with `{ rule: "sale_window" }`.
|
|
70
|
+
* - `maxPerOrder`: a number field holding the most of one record one order may hold. Empty means
|
|
71
|
+
* no limit beyond the host's own: 10 when the object grants, and never more than 100 grants
|
|
72
|
+
* on one order (the paid batch has a size). More is refused with `{ rule: "max_per_order" }`.
|
|
73
|
+
* - `deposit`: paying by deposit (stage 25): the field holding the deposit for one, and the date
|
|
74
|
+
* field saying when the balance is due. A record offers a deposit when both have a value.
|
|
75
|
+
* - `grants`: what a paid line grants (see GrantSpec).
|
|
76
|
+
*/
|
|
77
|
+
export interface SellableSpec {
|
|
78
|
+
price: string;
|
|
79
|
+
currency?: string;
|
|
80
|
+
saleWindow?: { from?: string; until?: string };
|
|
81
|
+
maxPerOrder?: string;
|
|
82
|
+
deposit?: { amount: string; balanceDue: string };
|
|
83
|
+
grants?: GrantSpec;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const fieldName = z.string().max(40);
|
|
87
|
+
|
|
88
|
+
export const GRANT_SOURCES = [
|
|
89
|
+
"item",
|
|
90
|
+
"order",
|
|
91
|
+
"order.person",
|
|
92
|
+
"order.email",
|
|
93
|
+
"line",
|
|
94
|
+
"line.title",
|
|
95
|
+
"line.unitPrice",
|
|
96
|
+
"code",
|
|
97
|
+
] as const;
|
|
98
|
+
|
|
99
|
+
const grantSource = z.union([
|
|
100
|
+
z.enum(GRANT_SOURCES),
|
|
101
|
+
z.string().regex(/^item\.[a-z][a-zA-Z0-9]{0,39}$/, "Use item, item.<field>, order, line or code"),
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
export const sellableManifestSchema = z
|
|
105
|
+
.object({
|
|
106
|
+
price: fieldName,
|
|
107
|
+
currency: z
|
|
108
|
+
.string()
|
|
109
|
+
.regex(/^[A-Z]{3}$/)
|
|
110
|
+
.optional(),
|
|
111
|
+
saleWindow: z
|
|
112
|
+
.object({ from: fieldName.optional(), until: fieldName.optional() })
|
|
113
|
+
.strict()
|
|
114
|
+
.optional(),
|
|
115
|
+
maxPerOrder: fieldName.optional(),
|
|
116
|
+
deposit: z
|
|
117
|
+
.object({ amount: fieldName.optional(), balanceDue: fieldName.optional() })
|
|
118
|
+
.strict()
|
|
119
|
+
.optional(),
|
|
120
|
+
grants: z
|
|
121
|
+
.object({
|
|
122
|
+
object: z.string().max(40),
|
|
123
|
+
set: z.record(fieldName, grantSource),
|
|
124
|
+
void: z.string().min(1).max(100),
|
|
125
|
+
})
|
|
126
|
+
.strict()
|
|
127
|
+
.optional(),
|
|
128
|
+
})
|
|
129
|
+
.strict();
|
|
130
|
+
export type SellableManifest = z.infer<typeof sellableManifestSchema>;
|
|
131
|
+
|
|
132
|
+
const NUMERIC = new Set(["number", "currency"]);
|
|
133
|
+
const DATES = new Set(["date", "datetime"]);
|
|
134
|
+
const TEXT = new Set(["text"]);
|
|
135
|
+
|
|
136
|
+
/** Reports problems with `objects.<name>.sellable` through `contract`. */
|
|
137
|
+
export function checkObjectSellable(
|
|
138
|
+
m: ExtensionManifest,
|
|
139
|
+
name: string,
|
|
140
|
+
object: ObjectManifest,
|
|
141
|
+
contract: (path: string, message: string) => void,
|
|
142
|
+
): void {
|
|
143
|
+
const sellable = object.sellable;
|
|
144
|
+
if (!sellable) return;
|
|
145
|
+
const path = `objects.${name}.sellable`;
|
|
146
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
147
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
148
|
+
const single = (local: string, types: Set<string>) => {
|
|
149
|
+
const field = fieldOf(local);
|
|
150
|
+
return field !== undefined && types.has(field.type) && !field.many;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
if (!single(sellable.price, NUMERIC)) {
|
|
154
|
+
contract(`${path}.price`, `"${sellable.price}" must be a currency or number field of ${name}`);
|
|
155
|
+
} else {
|
|
156
|
+
const own = fieldOf(sellable.price)?.currency;
|
|
157
|
+
if (sellable.currency && own && own !== sellable.currency) {
|
|
158
|
+
contract(
|
|
159
|
+
`${path}.currency`,
|
|
160
|
+
`The price field is in ${own}; currency says ${sellable.currency}. Declare one`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (sellable.saleWindow) {
|
|
165
|
+
const { from, until } = sellable.saleWindow;
|
|
166
|
+
if (from === undefined && until === undefined) {
|
|
167
|
+
contract(`${path}.saleWindow`, "A sale window names its from field, its until field or both");
|
|
168
|
+
}
|
|
169
|
+
for (const [key, local] of [
|
|
170
|
+
["from", from],
|
|
171
|
+
["until", until],
|
|
172
|
+
] as const) {
|
|
173
|
+
if (local !== undefined && !single(local, DATES)) {
|
|
174
|
+
contract(
|
|
175
|
+
`${path}.saleWindow.${key}`,
|
|
176
|
+
`"${local}" must be a date or datetime field of ${name}`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (sellable.maxPerOrder !== undefined && !single(sellable.maxPerOrder, new Set(["number"]))) {
|
|
182
|
+
contract(`${path}.maxPerOrder`, `"${sellable.maxPerOrder}" must be a number field of ${name}`);
|
|
183
|
+
}
|
|
184
|
+
if (sellable.deposit) {
|
|
185
|
+
const { amount, balanceDue } = sellable.deposit;
|
|
186
|
+
if (amount === undefined || balanceDue === undefined) {
|
|
187
|
+
contract(
|
|
188
|
+
`${path}.deposit`,
|
|
189
|
+
"A deposit names both fields: amount (the deposit for one) and balanceDue (when the rest is due)",
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
if (amount !== undefined && !single(amount, NUMERIC)) {
|
|
193
|
+
contract(
|
|
194
|
+
`${path}.deposit.amount`,
|
|
195
|
+
`"${amount}" must be a currency or number field of ${name}`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (balanceDue !== undefined && !single(balanceDue, DATES)) {
|
|
199
|
+
contract(
|
|
200
|
+
`${path}.deposit.balanceDue`,
|
|
201
|
+
`"${balanceDue}" must be a date or datetime field of ${name}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (sellable.grants) checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function checkGrants(
|
|
209
|
+
m: ExtensionManifest,
|
|
210
|
+
name: string,
|
|
211
|
+
item: ObjectManifest,
|
|
212
|
+
grants: NonNullable<SellableManifest["grants"]>,
|
|
213
|
+
path: string,
|
|
214
|
+
contract: (path: string, message: string) => void,
|
|
215
|
+
): void {
|
|
216
|
+
const local = grants.object.startsWith(`${m.id}.`)
|
|
217
|
+
? grants.object.slice(m.id.length + 1)
|
|
218
|
+
: grants.object;
|
|
219
|
+
const granted = Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
220
|
+
if (!granted) {
|
|
221
|
+
contract(`${path}.object`, `"${grants.object}" must be another of this extension's objects`);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (local === name) {
|
|
225
|
+
contract(`${path}.object`, "An object cannot grant records of itself");
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (granted.sellable) {
|
|
229
|
+
contract(
|
|
230
|
+
`${path}.object`,
|
|
231
|
+
`${local} is itself sellable; a grant is what was bought, not more for sale`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
const states = granted.states;
|
|
235
|
+
if (!states) {
|
|
236
|
+
contract(
|
|
237
|
+
`${path}.object`,
|
|
238
|
+
`${local} must declare states, so what was granted can be voided (to "${grants.void}")`,
|
|
239
|
+
);
|
|
240
|
+
} else {
|
|
241
|
+
const stateField = granted.fields[states.field];
|
|
242
|
+
const values = new Set((stateField?.options ?? []).map((o) => o.value));
|
|
243
|
+
const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
|
|
244
|
+
if (!values.has(grants.void)) {
|
|
245
|
+
contract(`${path}.void`, `"${grants.void}" is not a state of ${local}`);
|
|
246
|
+
} else if (grants.void === states.initial) {
|
|
247
|
+
contract(
|
|
248
|
+
`${path}.void`,
|
|
249
|
+
`"${grants.void}" is ${local}'s initial state; void is where a grant ends`,
|
|
250
|
+
);
|
|
251
|
+
} else {
|
|
252
|
+
for (const state of values) {
|
|
253
|
+
if (state === grants.void || finals.has(state)) continue;
|
|
254
|
+
const moves = states.moves.some(
|
|
255
|
+
(mv) =>
|
|
256
|
+
mv.to === grants.void && moveOrigins(mv).includes(state) && mv.by.includes("host"),
|
|
257
|
+
);
|
|
258
|
+
if (!moves) {
|
|
259
|
+
contract(
|
|
260
|
+
`${path}.void`,
|
|
261
|
+
`${local} needs a move from "${state}" to "${grants.void}" that the host may make (by: ["host", ...])`,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (Object.hasOwn(grants.set, states.field)) {
|
|
267
|
+
contract(
|
|
268
|
+
`${path}.set.${states.field}`,
|
|
269
|
+
`${states.field} is ${local}'s state: a granted record starts in "${states.initial}"`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const itemField = (f: string) => (Object.hasOwn(item.fields, f) ? item.fields[f] : undefined);
|
|
275
|
+
let codes = 0;
|
|
276
|
+
for (const [target, source] of Object.entries(grants.set)) {
|
|
277
|
+
const fp = `${path}.set.${target}`;
|
|
278
|
+
const field = Object.hasOwn(granted.fields, target) ? granted.fields[target] : undefined;
|
|
279
|
+
if (!field || field.type === "formula") {
|
|
280
|
+
contract(fp, `"${target}" must be a field of ${local} other than a formula`);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const want = (ok: boolean, what: string) => {
|
|
284
|
+
if (!ok) contract(fp, `${source} needs ${what}; "${target}" is a ${field.type} field`);
|
|
285
|
+
};
|
|
286
|
+
const one = !field.many;
|
|
287
|
+
if (source === "item") {
|
|
288
|
+
want(
|
|
289
|
+
field.type === "relation" && one && field.to === `${m.id}.${name}`,
|
|
290
|
+
`a relation (one record) to ${m.id}.${name}`,
|
|
291
|
+
);
|
|
292
|
+
} else if (source.startsWith("item.")) {
|
|
293
|
+
const from = source.slice("item.".length);
|
|
294
|
+
const own = itemField(from);
|
|
295
|
+
if (!own || own.type === "formula") {
|
|
296
|
+
contract(fp, `"${from}" is not a field of ${name}`);
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
want(
|
|
300
|
+
own.type === field.type &&
|
|
301
|
+
Boolean(own.many) === Boolean(field.many) &&
|
|
302
|
+
(own.type !== "relation" || own.to === field.to),
|
|
303
|
+
`a ${own.type} field${own.to ? ` to ${own.to}` : ""}, like ${name}.${from}`,
|
|
304
|
+
);
|
|
305
|
+
if (field.required && !own.required) {
|
|
306
|
+
contract(fp, `"${target}" is required and ${name}.${from} may be empty`);
|
|
307
|
+
}
|
|
308
|
+
} else if (source === "order.person") {
|
|
309
|
+
want(field.type === "relation" && one && field.to === "person", "a relation to one person");
|
|
310
|
+
if (field.required) {
|
|
311
|
+
contract(fp, `"${target}" cannot be required: an order may belong to no one yet`);
|
|
312
|
+
}
|
|
313
|
+
} else if (source === "order.email") {
|
|
314
|
+
want(field.type === "email" || field.type === "text", "an email or text field");
|
|
315
|
+
} else if (source === "order" || source === "line" || source === "line.title") {
|
|
316
|
+
want(TEXT.has(field.type), "a text field");
|
|
317
|
+
} else if (source === "line.unitPrice") {
|
|
318
|
+
want(NUMERIC.has(field.type), "a currency or number field");
|
|
319
|
+
} else if (source === "code") {
|
|
320
|
+
codes += 1;
|
|
321
|
+
want(TEXT.has(field.type), "a text field");
|
|
322
|
+
if (!field.readOnly) {
|
|
323
|
+
contract(
|
|
324
|
+
fp,
|
|
325
|
+
`"${target}" holds the host's code: make it readOnly, so nobody can change it`,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (codes > 1) contract(`${path}.set`, "A grant has at most one code");
|
|
331
|
+
|
|
332
|
+
// The granted object's own rules must not be able to refuse the host's insert, since that
|
|
333
|
+
// would refuse the payment. What a grant always fills: its set fields whose source always has
|
|
334
|
+
// a value, and fields with a default.
|
|
335
|
+
const always = (source: string) =>
|
|
336
|
+
source === "order.person"
|
|
337
|
+
? false
|
|
338
|
+
: source.startsWith("item.")
|
|
339
|
+
? Boolean(itemField(source.slice("item.".length))?.required)
|
|
340
|
+
: true;
|
|
341
|
+
const filled = new Set([
|
|
342
|
+
...Object.entries(grants.set)
|
|
343
|
+
.filter(([, source]) => always(source))
|
|
344
|
+
.map(([field]) => field),
|
|
345
|
+
...Object.entries(granted.fields)
|
|
346
|
+
.filter(([, field]) => field.default !== undefined)
|
|
347
|
+
.map(([name]) => name),
|
|
348
|
+
]);
|
|
349
|
+
if (granted.owner !== undefined) {
|
|
350
|
+
contract(
|
|
351
|
+
`objects.${local}.owner`,
|
|
352
|
+
`${local} is granted by ${name}: an owner would run assignment rules in the paid batch, which could refuse it. Remove owner`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
granted.requireOneOf?.forEach((group, i) => {
|
|
356
|
+
if (!group.some((field) => filled.has(field))) {
|
|
357
|
+
contract(
|
|
358
|
+
`objects.${local}.requireOneOf.${i}`,
|
|
359
|
+
`${local} is granted by ${name}: set one of ${group.join(", ")} in grants.set from a source that always has a value`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
for (const [fieldName, field] of Object.entries(granted.fields)) {
|
|
364
|
+
if (field.type !== "select") continue;
|
|
365
|
+
const source = grants.set[fieldName];
|
|
366
|
+
// The choices a granted record can hold when it is created.
|
|
367
|
+
const values =
|
|
368
|
+
source?.startsWith("item.") === true
|
|
369
|
+
? (field.options ?? []).map((o) => o.value)
|
|
370
|
+
: states && fieldName === states.field
|
|
371
|
+
? [states.initial]
|
|
372
|
+
: field.default !== undefined
|
|
373
|
+
? [String(field.default)]
|
|
374
|
+
: [];
|
|
375
|
+
for (const option of field.options ?? []) {
|
|
376
|
+
if (!values.includes(option.value)) continue;
|
|
377
|
+
const missing = (option.requires ?? []).filter((required) => !filled.has(required));
|
|
378
|
+
if (missing.length) {
|
|
379
|
+
contract(
|
|
380
|
+
`objects.${local}.fields.${fieldName}.options`,
|
|
381
|
+
`${local} is granted by ${name}: "${option.value}" needs ${missing.join(", ")}, which grants.set does not always fill`,
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
for (const [fieldName, field] of Object.entries(granted.fields)) {
|
|
387
|
+
if (!field.required || field.type === "formula" || field.default !== undefined) continue;
|
|
388
|
+
if (states && fieldName === states.field) continue;
|
|
389
|
+
if (!Object.hasOwn(grants.set, fieldName)) {
|
|
390
|
+
contract(
|
|
391
|
+
`${path}.set`,
|
|
392
|
+
`${local}.${fieldName} is required: set it from the item, the order or the line`,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The `slug` field type (stage 28, ADR 0442): a record's address in its object, such as
|
|
2
|
+
// `summer-launch` in `/blog/summer-launch`. Lowercase letters and digits in words joined by single
|
|
3
|
+
// hyphens. The host keeps it unique among the object's live records and makes one from the title
|
|
4
|
+
// when a record is created without one.
|
|
5
|
+
|
|
6
|
+
/** What a slug may be. */
|
|
7
|
+
export const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
8
|
+
/** The longest a slug may be. */
|
|
9
|
+
export const SLUG_MAX = 120;
|
|
10
|
+
/** The longest slug made from a title, leaving room for a `-<n>` that tells two apart. */
|
|
11
|
+
export const SLUG_FROM_TITLE_MAX = 80;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A slug made from a title: accents dropped, every run of anything that is not a letter or digit
|
|
15
|
+
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
16
|
+
*/
|
|
17
|
+
export function slugify(title: string): string {
|
|
18
|
+
const plain = title
|
|
19
|
+
.normalize("NFKD")
|
|
20
|
+
.replace(/[̀-ͯ]/g, "")
|
|
21
|
+
.toLowerCase()
|
|
22
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
23
|
+
.replace(/^-+|-+$/g, "");
|
|
24
|
+
return plain.slice(0, SLUG_FROM_TITLE_MAX).replace(/-+$/g, "");
|
|
25
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// The `states` behaviour (ADR 0425): an object declares a small, fixed state machine and the host
|
|
2
|
+
// holds every write to it. Its declaration, its manifest schema and its contract checks live
|
|
3
|
+
// here; the host's side is packages/core/src/records/states.ts, on the transition engine it
|
|
4
|
+
// shares with `stages`.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Who may make a move. `staff`: anyone working for the organisation (people, their assistants,
|
|
10
|
+
* MCP clients, automations and extensions acting for them). `customer`: a signed-in customer, on
|
|
11
|
+
* their own record. `host`: platform code only (a payment confirmation, an expiry); no caller of
|
|
12
|
+
* an operation can make it.
|
|
13
|
+
*/
|
|
14
|
+
export const MOVERS = ["staff", "customer", "host"] as const;
|
|
15
|
+
export type Mover = (typeof MOVERS)[number];
|
|
16
|
+
|
|
17
|
+
/** One allowed move of a state machine. */
|
|
18
|
+
export interface StateMove {
|
|
19
|
+
/** The state, or states, the record moves from. */
|
|
20
|
+
from: string | readonly string[];
|
|
21
|
+
/** The state it moves to. */
|
|
22
|
+
to: string;
|
|
23
|
+
/** Who may make it. */
|
|
24
|
+
by: readonly Mover[];
|
|
25
|
+
/** Fields that must have a value to make the move, and cannot be emptied while in `to`. */
|
|
26
|
+
requires?: readonly string[];
|
|
27
|
+
/** The move ends the lifecycle: nothing moves out of `to`. */
|
|
28
|
+
final?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A state machine (ADR 0425). The host enforces it in the record operations and the record CSV
|
|
33
|
+
* import, for every actor, and in the approval queue's dry run:
|
|
34
|
+
*
|
|
35
|
+
* - A record is created in `initial`. Creating it in another state is judged as the move from
|
|
36
|
+
* `initial` to that state.
|
|
37
|
+
* - A move that is not declared is refused, with details naming the rule, the state and the
|
|
38
|
+
* field to fix. So is a declared move made by someone its `by` does not list, and a move while
|
|
39
|
+
* one of its `requires` fields is empty.
|
|
40
|
+
* - Every move is recorded in state history (entered and left times, who) in the same batch as
|
|
41
|
+
* the write and, when the state field is tracked, posted on the timeline as "Moved to <state>".
|
|
42
|
+
*/
|
|
43
|
+
export interface StatesSpec {
|
|
44
|
+
/** This object's select field holding the state. Its options are the states. */
|
|
45
|
+
field: string;
|
|
46
|
+
/** The state a new record starts in. */
|
|
47
|
+
initial: string;
|
|
48
|
+
moves: readonly StateMove[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const fieldName = z.string().max(40);
|
|
52
|
+
const stateValue = z.string().min(1).max(100);
|
|
53
|
+
|
|
54
|
+
export const statesManifestSchema = z.object({
|
|
55
|
+
field: fieldName,
|
|
56
|
+
initial: stateValue,
|
|
57
|
+
moves: z
|
|
58
|
+
.array(
|
|
59
|
+
z.object({
|
|
60
|
+
from: z.union([stateValue, z.array(stateValue).min(1).max(50)]),
|
|
61
|
+
to: stateValue,
|
|
62
|
+
by: z.array(z.enum(MOVERS)).min(1).max(MOVERS.length),
|
|
63
|
+
requires: z.array(fieldName).max(50).optional(),
|
|
64
|
+
final: z.boolean().optional(),
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
.max(200),
|
|
68
|
+
});
|
|
69
|
+
export type StatesManifest = z.infer<typeof statesManifestSchema>;
|
|
70
|
+
|
|
71
|
+
/** The states a move leaves, as a list. */
|
|
72
|
+
export function moveOrigins(move: { from: string | readonly string[] }): readonly string[] {
|
|
73
|
+
return typeof move.from === "string" ? [move.from] : move.from;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Reports problems with `objects.<name>.states` through `contract`. */
|
|
77
|
+
export function checkObjectStates(
|
|
78
|
+
_m: ExtensionManifest,
|
|
79
|
+
name: string,
|
|
80
|
+
object: ObjectManifest,
|
|
81
|
+
contract: (path: string, message: string) => void,
|
|
82
|
+
): void {
|
|
83
|
+
const states = object.states;
|
|
84
|
+
if (!states) return;
|
|
85
|
+
const sp = `objects.${name}.states`;
|
|
86
|
+
if (object.stages) {
|
|
87
|
+
contract(sp, "An object has one lifecycle: declare stages or states, not both");
|
|
88
|
+
}
|
|
89
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
90
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
91
|
+
const field = fieldOf(states.field);
|
|
92
|
+
if (field?.type !== "select") {
|
|
93
|
+
contract(`${sp}.field`, `"${states.field}" must be a select field of ${name}`);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (field.readOnly) {
|
|
97
|
+
contract(`${sp}.field`, "The state field cannot be read-only: the moves say who may change it");
|
|
98
|
+
}
|
|
99
|
+
if (field.editableOptions) {
|
|
100
|
+
contract(
|
|
101
|
+
`${sp}.field`,
|
|
102
|
+
"The state field cannot have editableOptions: states are fixed in the declaration",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const values = new Set((field.options ?? []).map((o) => o.value));
|
|
106
|
+
const isState = (value: string) => values.has(value);
|
|
107
|
+
if (!isState(states.initial)) {
|
|
108
|
+
contract(`${sp}.initial`, `"${states.initial}" is not an option of ${states.field}`);
|
|
109
|
+
}
|
|
110
|
+
if (field.default !== undefined && field.default !== states.initial) {
|
|
111
|
+
contract(
|
|
112
|
+
`${sp}.initial`,
|
|
113
|
+
`${states.field}'s default must be the initial state "${states.initial}", or be left out`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const seen = new Set<string>();
|
|
118
|
+
const finals = new Set(states.moves.filter((move) => move.final).map((move) => move.to));
|
|
119
|
+
states.moves.forEach((move, i) => {
|
|
120
|
+
const mp = `${sp}.moves.${i}`;
|
|
121
|
+
if (!isState(move.to)) contract(`${mp}.to`, `"${move.to}" is not an option of ${states.field}`);
|
|
122
|
+
if (new Set(move.by).size !== move.by.length) contract(`${mp}.by`, "List each mover once");
|
|
123
|
+
if (finals.has(move.to) && !move.final) {
|
|
124
|
+
contract(
|
|
125
|
+
`${mp}.final`,
|
|
126
|
+
`Another move into "${move.to}" is final; every move into a state must agree`,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (move.final && move.to === states.initial) {
|
|
130
|
+
contract(`${mp}.final`, "The initial state cannot be final");
|
|
131
|
+
}
|
|
132
|
+
for (const from of moveOrigins(move)) {
|
|
133
|
+
if (!isState(from)) {
|
|
134
|
+
contract(`${mp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
135
|
+
}
|
|
136
|
+
if (from === move.to) contract(`${mp}.from`, `A move cannot go from "${from}" to itself`);
|
|
137
|
+
if (finals.has(from)) {
|
|
138
|
+
contract(`${mp}.from`, `"${from}" is final: nothing moves out of it`);
|
|
139
|
+
}
|
|
140
|
+
const key = JSON.stringify([from, move.to]);
|
|
141
|
+
if (seen.has(key)) {
|
|
142
|
+
contract(mp, `The move from "${from}" to "${move.to}" is declared twice`);
|
|
143
|
+
}
|
|
144
|
+
seen.add(key);
|
|
145
|
+
}
|
|
146
|
+
for (const local of move.requires ?? []) {
|
|
147
|
+
const required = fieldOf(local);
|
|
148
|
+
if (!required || required.type === "formula" || local === states.field) {
|
|
149
|
+
contract(
|
|
150
|
+
`${mp}.requires`,
|
|
151
|
+
`"${local}" must be a field of ${name} other than a formula or the state field`,
|
|
152
|
+
);
|
|
153
|
+
} else if (required.readOnly && move.by.some((mover) => mover !== "host")) {
|
|
154
|
+
// Only the host writes a read-only field, so only a host move can fill it.
|
|
155
|
+
contract(
|
|
156
|
+
`${mp}.requires`,
|
|
157
|
+
`"${local}" is read-only, so only a move made by the host alone can require it`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|