@composable-os/sdk 0.0.0-stage → 0.1.0-beta.10

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 (81) hide show
  1. package/LICENSE +109 -0
  2. package/dist/allowance.d.ts +198 -0
  3. package/dist/allowance.js +374 -0
  4. package/dist/automation-check.d.ts +38 -0
  5. package/dist/automation-check.js +454 -0
  6. package/dist/automation.d.ts +767 -0
  7. package/dist/automation.js +303 -0
  8. package/dist/behaviour.d.ts +3 -0
  9. package/dist/behaviour.js +153 -0
  10. package/dist/capacity.d.ts +77 -0
  11. package/dist/capacity.js +127 -0
  12. package/dist/contract.d.ts +819 -0
  13. package/dist/contract.js +42 -0
  14. package/dist/document.d.ts +211 -0
  15. package/dist/document.js +726 -0
  16. package/dist/field-types.d.ts +11 -0
  17. package/dist/field-types.js +98 -0
  18. package/dist/formula/ast.d.ts +67 -0
  19. package/dist/formula/ast.js +22 -0
  20. package/dist/formula/compile.d.ts +37 -0
  21. package/dist/formula/compile.js +379 -0
  22. package/dist/formula/index.d.ts +3 -0
  23. package/dist/formula/index.js +4 -0
  24. package/dist/formula/parser.d.ts +13 -0
  25. package/dist/formula/parser.js +365 -0
  26. package/dist/index.d.ts +20 -0
  27. package/dist/index.js +24 -0
  28. package/dist/manifest.d.ts +1596 -0
  29. package/dist/manifest.js +1420 -0
  30. package/dist/media.d.ts +41 -0
  31. package/dist/media.js +90 -0
  32. package/dist/permissions.d.ts +17 -0
  33. package/dist/permissions.js +44 -0
  34. package/dist/recurring.d.ts +114 -0
  35. package/dist/recurring.js +173 -0
  36. package/dist/renamed.d.ts +16 -0
  37. package/dist/renamed.js +59 -0
  38. package/dist/rich-text-fixture.d.ts +121 -0
  39. package/dist/rich-text-fixture.js +67 -0
  40. package/dist/rich-text.d.ts +48 -0
  41. package/dist/rich-text.js +184 -0
  42. package/dist/runtime.d.ts +95 -0
  43. package/dist/runtime.js +312 -0
  44. package/dist/sellable.d.ts +253 -0
  45. package/dist/sellable.js +593 -0
  46. package/dist/slug.d.ts +11 -0
  47. package/dist/slug.js +23 -0
  48. package/dist/states.d.ts +238 -0
  49. package/dist/states.js +315 -0
  50. package/dist/testing.d.ts +151 -0
  51. package/dist/testing.js +679 -0
  52. package/dist/unique.d.ts +20 -0
  53. package/dist/unique.js +58 -0
  54. package/package.json +34 -4
  55. package/src/allowance.ts +522 -0
  56. package/src/automation-check.ts +534 -0
  57. package/src/automation.ts +743 -0
  58. package/src/behaviour.ts +215 -0
  59. package/src/capacity.ts +201 -0
  60. package/src/contract.ts +884 -0
  61. package/src/document.ts +935 -0
  62. package/src/field-types.ts +111 -0
  63. package/src/formula/ast.ts +72 -0
  64. package/src/formula/compile.ts +454 -0
  65. package/src/formula/index.ts +4 -0
  66. package/src/formula/parser.ts +417 -0
  67. package/src/index.ts +36 -0
  68. package/src/manifest.ts +1709 -0
  69. package/src/media.ts +108 -0
  70. package/src/permissions.ts +60 -0
  71. package/src/recurring.ts +321 -0
  72. package/src/renamed.ts +63 -0
  73. package/src/rich-text-fixture.ts +68 -0
  74. package/src/rich-text.ts +221 -0
  75. package/src/runtime.ts +410 -0
  76. package/src/sellable.ts +888 -0
  77. package/src/slug.ts +25 -0
  78. package/src/states.ts +484 -0
  79. package/src/testing.ts +847 -0
  80. package/src/unique.ts +81 -0
  81. package/README.md +0 -3
@@ -0,0 +1,312 @@
1
+ // The sandbox side of the runtime. The host generates a small entry module per run that imports
2
+ // the extension and the host modules it declared, and calls `runExtension`. Tool handlers get a
3
+ // `ctx` built from those modules; a module the extension did not declare is absent, and using it
4
+ // throws.
5
+ import { durationMs, notificationInput, } from "./automation.js";
6
+ import { toManifest } from "./manifest.js";
7
+ /** Thrown by `ctx.ops.call` when the operation fails; `code` is the pipeline's error code. */
8
+ export class OpsCallError extends Error {
9
+ code;
10
+ details;
11
+ name = "OpsCallError";
12
+ constructor(code, message, details) {
13
+ super(message);
14
+ this.code = code;
15
+ this.details = details;
16
+ }
17
+ }
18
+ function missing(name) {
19
+ throw new Error(`Host module "${name}" is not declared. Add "${name}" to permissions.modules in defineExtension.`);
20
+ }
21
+ function absent(name) {
22
+ return new Proxy({}, {
23
+ get(_target, prop) {
24
+ if (prop === "then")
25
+ return undefined;
26
+ return () => missing(name);
27
+ },
28
+ });
29
+ }
30
+ /** Builds the `ctx` tools receive from raw host modules. Exported for the testing harness. */
31
+ export function buildContext(extension, raw, pending = [], caller) {
32
+ const ops = raw.ops
33
+ ? {
34
+ call: async (operation, input) => {
35
+ // The host answers with an envelope so error codes survive the isolate boundary.
36
+ const reply = (await raw.ops?.call(operation, input ?? {}));
37
+ if (reply.ok)
38
+ return reply.value;
39
+ throw new OpsCallError(reply.error.code, reply.error.message, reply.error.details);
40
+ },
41
+ }
42
+ : absent("ops");
43
+ const storage = raw.storage
44
+ ? {
45
+ get: (key) => raw.storage?.get(key),
46
+ set: async (key, value) => {
47
+ await raw.storage?.set(key, value);
48
+ },
49
+ delete: async (key) => {
50
+ await raw.storage?.del(key);
51
+ },
52
+ list: (prefix) => raw.storage?.list(prefix ?? ""),
53
+ }
54
+ : absent("storage");
55
+ const write = (level) => (message, data) => {
56
+ const call = raw.log?.write(level, String(message), data ?? null);
57
+ if (call)
58
+ pending.push(call.catch(() => undefined));
59
+ };
60
+ const log = raw.log
61
+ ? { info: write("info"), warn: write("warn"), error: write("error") }
62
+ : absent("log");
63
+ const ai = raw.ai
64
+ ? {
65
+ summarise: (text, options) => raw.ai?.summarise(text, options ?? {}),
66
+ extract: (text, fields) => raw.ai?.extract(text, fields),
67
+ classify: (text, labels) => raw.ai?.classify(text, labels),
68
+ }
69
+ : absent("ai");
70
+ const gatekeeper = (name) => {
71
+ const mod = raw[name];
72
+ if (!mod?.call)
73
+ return absent(name);
74
+ return {
75
+ connections: () => mod.connections?.(),
76
+ call: async (connectionId, resource, method, ...args) => {
77
+ const reply = (await mod.call?.(connectionId, resource, method, args));
78
+ if (reply.ok)
79
+ return reply.value;
80
+ throw new OpsCallError(reply.error.code, reply.error.message, reply.error.details);
81
+ },
82
+ types: (connectionId, resource) => mod.types?.(connectionId, resource),
83
+ };
84
+ };
85
+ return {
86
+ extension: { id: extension.id, version: extension.version },
87
+ ops,
88
+ storage,
89
+ log,
90
+ ai,
91
+ gatekeeper: gatekeeper,
92
+ ...(caller ? { caller: Object.freeze({ ...caller }) } : {}),
93
+ };
94
+ }
95
+ function toError(error) {
96
+ if (error && typeof error === "object") {
97
+ const e = error;
98
+ return {
99
+ code: typeof e.code === "string" ? e.code : "extension_error",
100
+ message: typeof e.message === "string" ? e.message : String(error),
101
+ ...(e.details === undefined ? {} : { details: e.details }),
102
+ };
103
+ }
104
+ return { code: "extension_error", message: String(error) };
105
+ }
106
+ /** Plain JSON only crosses back to the host. */
107
+ function plain(value) {
108
+ return value === undefined ? null : JSON.parse(JSON.stringify(value));
109
+ }
110
+ /** Dispatches one run request inside the sandbox. */
111
+ export async function runExtension(extension, request, raw) {
112
+ const pending = [];
113
+ try {
114
+ if (request.kind === "manifest")
115
+ return { ok: true, value: plain(toManifest(extension)) };
116
+ const tool = extension.tools?.[request.tool];
117
+ if (!tool) {
118
+ return {
119
+ ok: false,
120
+ error: { code: "unknown_tool", message: `No tool named ${request.tool}` },
121
+ };
122
+ }
123
+ const parsed = tool.input.safeParse(request.input);
124
+ if (!parsed.success) {
125
+ return {
126
+ ok: false,
127
+ error: {
128
+ code: "invalid_input",
129
+ message: `Invalid input for ${request.tool}`,
130
+ details: plain(parsed.error.issues),
131
+ },
132
+ };
133
+ }
134
+ const ctx = buildContext(extension, raw, pending, request.caller);
135
+ const result = await tool.handler(parsed.data, ctx);
136
+ const checked = tool.output.safeParse(result);
137
+ await Promise.all(pending);
138
+ if (!checked.success) {
139
+ return {
140
+ ok: false,
141
+ error: {
142
+ code: "invalid_output",
143
+ message: `${request.tool} returned an invalid result`,
144
+ details: plain(checked.error.issues),
145
+ },
146
+ };
147
+ }
148
+ return { ok: true, value: plain(checked.data) };
149
+ }
150
+ catch (error) {
151
+ await Promise.all(pending);
152
+ return { ok: false, error: toError(error) };
153
+ }
154
+ }
155
+ /** Thrown to unwind `run` once the advance has done its one step. Never reaches the host. */
156
+ class AdvanceStop extends Error {
157
+ name = "AdvanceStop";
158
+ }
159
+ /** A step call that does not match the declarations (the declarations are the contract). */
160
+ export class AutomationStepError extends Error {
161
+ name = "AutomationStepError";
162
+ code = "undeclared_step";
163
+ }
164
+ /**
165
+ * Builds the automation `ctx`: the extension context plus `notify`, which calls
166
+ * `notification.send` with `notificationInput`. Its dedupe key is `<step>:<n>` (the n-th notify
167
+ * of that step), so a step retried or replayed never notifies twice.
168
+ */
169
+ export function buildAutomationContext(extension, raw, event, pending = [], currentStep = () => null) {
170
+ const base = buildContext(extension, raw, pending);
171
+ const counts = new Map();
172
+ return {
173
+ ...base,
174
+ async notify(to, message) {
175
+ const step = currentStep() ?? "run";
176
+ const n = (counts.get(step) ?? 0) + 1;
177
+ counts.set(step, n);
178
+ const input = notificationInput(event, to, message, `${step}:${n}`);
179
+ const out = (await base.ops.call("notification.send", input));
180
+ return { sent: typeof out?.sent === "number" ? out.sent : 0 };
181
+ },
182
+ };
183
+ }
184
+ /** Runs one advance (or a whole test run) of an automation inside the sandbox. */
185
+ export async function runAutomation(extension, request, raw) {
186
+ const spec = extension.automations?.[request.automation];
187
+ if (!spec) {
188
+ return {
189
+ ok: false,
190
+ error: { code: "unknown_automation", message: `No automation named ${request.automation}` },
191
+ };
192
+ }
193
+ const test = request.mode === "test";
194
+ const journal = request.journal ?? [];
195
+ const declared = new Map(spec.steps.map((s) => [s.name, s]));
196
+ const testSteps = [];
197
+ let cursor = 0;
198
+ let stop = null;
199
+ const declaredStep = (name, kind) => {
200
+ const step = declared.get(name);
201
+ if (!step) {
202
+ throw new AutomationStepError(`step.${kind}("${name}") is not a declared step of ${request.automation}; add it to steps`);
203
+ }
204
+ const declaredKind = step.kind ?? "do";
205
+ if (declaredKind !== kind) {
206
+ throw new AutomationStepError(`"${name}" is declared as a ${declaredKind} step but was called with step.${kind}`);
207
+ }
208
+ return step;
209
+ };
210
+ /** The journaled result of the next step, or undefined when it is new. */
211
+ const replayed = (name, kind) => {
212
+ if (stop)
213
+ throw new AdvanceStop();
214
+ const entry = journal[cursor];
215
+ if (!entry)
216
+ return undefined;
217
+ if (entry.name !== name || entry.kind !== kind) {
218
+ throw new AutomationStepError(`The run took a different path on replay: step ${cursor + 1} was "${entry.name}" ` +
219
+ `(${entry.kind}) and is now "${name}" (${kind}). Steps must run in the same order ` +
220
+ "every time; base branches on step results, not on the clock or randomness.");
221
+ }
222
+ cursor += 1;
223
+ return { value: entry.value };
224
+ };
225
+ const halt = (advance) => {
226
+ stop = advance;
227
+ throw new AdvanceStop();
228
+ };
229
+ const pending = [];
230
+ // Every ops call carries the step it ran in, so the host can file it under that step (and
231
+ // flag writes the step did not declare).
232
+ let current = null;
233
+ const ops = raw.ops;
234
+ const tagged = ops
235
+ ? { ...raw, ops: { call: (operation, input) => ops.call(operation, input, { step: current }) } }
236
+ : raw;
237
+ const ctx = buildAutomationContext(extension, tagged, request.event, pending, () => current);
238
+ const step = {
239
+ async do(name, a, b) {
240
+ const fn = (typeof a === "function" ? a : b);
241
+ const declaredDo = declaredStep(name, "do");
242
+ if (a && typeof a === "object") {
243
+ const intent = a;
244
+ const allowed = new Set((declaredDo.writes ?? []).map((w) => w.object));
245
+ const extra = (intent.writes ?? []).find((w) => !allowed.has(w.object));
246
+ if (extra) {
247
+ throw new AutomationStepError(`step.do("${name}") writes ${extra.object}, which its declaration does not list`);
248
+ }
249
+ }
250
+ const r = replayed(name, "do");
251
+ if (r)
252
+ return r.value;
253
+ current = name;
254
+ let value;
255
+ try {
256
+ value = plain(await fn());
257
+ }
258
+ finally {
259
+ current = null;
260
+ }
261
+ if (test) {
262
+ testSteps.push({ name, kind: "do", value });
263
+ return value;
264
+ }
265
+ return halt({ kind: "do", name, value });
266
+ },
267
+ async sleep(name, duration) {
268
+ declaredStep(name, "sleep");
269
+ if (replayed(name, "sleep"))
270
+ return;
271
+ const ms = durationMs(duration);
272
+ if (test) {
273
+ testSteps.push({ name, kind: "sleep", skippedMs: ms });
274
+ return;
275
+ }
276
+ halt({ kind: "sleep", name, ms });
277
+ },
278
+ async waitForEvent(name) {
279
+ const declaredWait = declaredStep(name, "waitForEvent");
280
+ const r = replayed(name, "waitForEvent");
281
+ if (r)
282
+ return r.value ?? null;
283
+ const timeoutMs = durationMs(declaredWait.timeout ?? "7 days");
284
+ if (test) {
285
+ testSteps.push({ name, kind: "waitForEvent", skippedMs: timeoutMs });
286
+ return null;
287
+ }
288
+ return halt({ kind: "waitForEvent", name, event: declaredWait.event, timeoutMs });
289
+ },
290
+ };
291
+ try {
292
+ const value = await spec.run(request.event, step, ctx);
293
+ await Promise.all(pending);
294
+ if (stop)
295
+ return { ok: true, value: plain(stop) };
296
+ const done = test
297
+ ? { kind: "test", steps: testSteps, value: plain(value) }
298
+ : { kind: "done", value: plain(value) };
299
+ return { ok: true, value: plain(done) };
300
+ }
301
+ catch (error) {
302
+ await Promise.all(pending);
303
+ // The stop unwinds `run` by throwing; code that catches and rethrows it changes nothing.
304
+ if (stop)
305
+ return { ok: true, value: plain(stop) };
306
+ // Test mode keeps the steps that ran before the failure, for the report.
307
+ if (test) {
308
+ return { ok: false, error: { ...toError(error), details: { steps: plain(testSteps) } } };
309
+ }
310
+ return { ok: false, error: toError(error) };
311
+ }
312
+ }
@@ -0,0 +1,253 @@
1
+ import { z } from "zod";
2
+ import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
3
+ /**
4
+ * Where a granted record's field gets its value, when the host creates it:
5
+ *
6
+ * - `item`: the record that was bought (a relation to this sellable object);
7
+ * - `item.<field>`: a field of the record that was bought (its event, its name);
8
+ * - `order`: the order's id (a text field);
9
+ * - `order.person`: the buyer, a relation to a person (empty when the order belongs to no one
10
+ * yet, so the field cannot be required);
11
+ * - `order.email`: the address the order was placed with;
12
+ * - `line`: the order line's id (a text field);
13
+ * - `line.title`: what the line was called when it was bought;
14
+ * - `line.unitPrice`: what one cost, in the currency's major unit;
15
+ * - `code`: a long, random, unguessable code the host makes (a ticket's). A read-only text field;
16
+ * at most one per grant. The host keeps it unique across every granted record.
17
+ */
18
+ export type GrantSource = "item" | `item.${string}` | "order" | "order.person" | "order.email" | "line" | "line.title" | "line.unitPrice" | "code";
19
+ /**
20
+ * What a paid line grants: one record of `object` for each unit bought (two tickets for a line
21
+ * of two), created by the host, as the host, when the order is first paid (the move to `paid`,
22
+ * or to `part_paid` for a deposit), in the same batch. No extension code runs.
23
+ *
24
+ * - `object` is another of this extension's objects. It declares `states`; its records start in
25
+ * the initial state.
26
+ * - `set` names, for each field of the granted record the host fills, where its value comes from
27
+ * (see GrantSource). Every required field of the granted object must be set from a source that
28
+ * always has a value.
29
+ * - `void` is the state the host moves what was granted to when the order is refunded in full, or
30
+ * cancelled after a deposit. Every state but `void` and the final ones needs a `host` move into
31
+ * it. A record already there, or in a final state, is left alone.
32
+ */
33
+ export interface GrantSpec {
34
+ object: string;
35
+ set: Readonly<Record<string, GrantSource>>;
36
+ void: string;
37
+ /**
38
+ * When what was granted is valid (stage 32b, ADR 0511): the host writes `from` (the moment it
39
+ * is granted) and `until` (that moment plus `days` local days in the organisation's zone; for a
40
+ * date field, the day before that, since a date includes its whole day), each a date or
41
+ * datetime field of the granted object. `days` is `item.<number field>` of the record bought (a
42
+ * class pack's "valid for 30 days"); empty there, `until` is left empty. A pass's expiry.
43
+ */
44
+ valid?: {
45
+ from?: string;
46
+ until?: string;
47
+ days?: `item.${string}`;
48
+ };
49
+ }
50
+ /**
51
+ * Something that can be bought (ADR 0435). An order line may point only at a record of an object
52
+ * that declares it; the host prices the line from the record, so a caller never sends a price.
53
+ * Only a shipped pack or the org's own extension may declare it: selling to the public is a
54
+ * reviewed change, so a runtime extension that does is refused at check and install.
55
+ *
56
+ * - `price`: this object's currency (or number) field holding the price of one, in the currency's
57
+ * major unit. A record with no price cannot be bought.
58
+ * - `currency`: what the price is in (ISO 4217). Left out, the price field's own currency, and
59
+ * with neither, the deployment's. A line priced in another currency than the deployment's is
60
+ * refused.
61
+ * - `saleWindow`: the date or datetime fields saying when a record is on sale, from `from` and
62
+ * until `until` (a date: the whole day, in UTC). Empty values leave that end open. A line
63
+ * outside it is refused with `{ rule: "sale_window" }`.
64
+ * - `maxPerOrder`: a number field holding the most of one record one order may hold. Empty means
65
+ * no limit beyond the host's own: 10 when the object grants, and never more than 100 grants
66
+ * on one order (the paid batch has a size). More is refused with `{ rule: "max_per_order" }`.
67
+ * - `deposit`: paying by deposit (stage 25): the field holding the deposit for one, and the date
68
+ * field saying when the balance is due. A record offers a deposit when both have a value.
69
+ * - `grants`: what a paid line grants (see GrantSpec).
70
+ * - `tax`: tax at checkout (ADR 0479), see TaxSpec. A line is taxed only when this is declared and
71
+ * a human admin has turned tax on for the deployment.
72
+ * - `recurring`: a plan, paid for every period (stage 32a, ADR 0485), see RecurringSpec. Its records
73
+ * are subscribed to, never put on an order: `price` is the price of one period. Not with
74
+ * `deposit`, `maxPerOrder` or `grants` (32b adds what a subscription grants).
75
+ */
76
+ export interface SellableSpec {
77
+ price: string;
78
+ currency?: string;
79
+ saleWindow?: {
80
+ from?: string;
81
+ until?: string;
82
+ };
83
+ maxPerOrder?: string;
84
+ deposit?: {
85
+ amount: string;
86
+ balanceDue: string;
87
+ };
88
+ grants?: GrantSpec;
89
+ tax?: TaxSpec;
90
+ recurring?: RecurringSpec;
91
+ /**
92
+ * A boolean field: while it is true on a record, a Person may buy it once, across every record
93
+ * of this object with it true (an intro offer: one per Person, ADR 0512). The host holds it from
94
+ * the order's start, uses it when the order is paid, and gives it back only when the order
95
+ * expires or is cancelled unpaid; the database refuses a second hold, so two purchases at once
96
+ * give one. Only a buyer who proved who they are buys it (a signed-in customer, or staff for a
97
+ * Person): a guest who typed an address is refused (`rule: "once_per_person"`, `reason:
98
+ * "sign_in"`). Such a line holds one, and an order at most one such line. Not on a plan.
99
+ */
100
+ onePerPerson?: string;
101
+ }
102
+ /**
103
+ * How a plan recurs (ADR 0485). The payment provider charges every `interval` on its own schedule;
104
+ * the host learns of each paid period from its verified events and writes a paid order for it.
105
+ *
106
+ * - `interval`: `week`, `month` or `year`.
107
+ * - `trialDays`: a number field of the plan holding the days of a free first period (the card is
108
+ * taken at the start, the first charge is when it ends). Empty or zero: no trial.
109
+ */
110
+ export interface RecurringSpec {
111
+ interval: "week" | "month" | "year";
112
+ trialDays?: string;
113
+ /**
114
+ * What a subscription to the plan grants (stage 32b, ADR 0510): one record of `object` for the
115
+ * subscription's holder, made by the host when the provider first reports it running, then kept
116
+ * in step with it, as the host, in each verified event's batch: its state follows the
117
+ * subscription's (`states` maps each of the host's subscription states to one of the granted
118
+ * object's), and `until` is when it stops giving (the end of its last period when it is set to
119
+ * end, 30 days of failing while past due, the moment it ended). No extension code runs.
120
+ *
121
+ * - `object`: another of this extension's objects, declaring `states`.
122
+ * - `set`: fields filled once, when it is granted: `item` (a relation to the plan),
123
+ * `item.<field>` (the plan's terms as they are at the start: its uses, its class types),
124
+ * `subscription` (a text field, the subscription's id), `subscription.holder` and
125
+ * `subscription.payer` (relations to person). The holder always has a value.
126
+ * - `from`, `until`: datetime fields the host keeps.
127
+ * - `states`: the granted object's state for each subscription state that grants
128
+ * (`trialing`, `active`, `past_due`, `unpaid`, `paused`, `ended`); `ended`'s is final.
129
+ *
130
+ * Not the `recurring` behaviour (a weekly timetable rule, ADR 0500): this is the plan's own
131
+ * `sellable.recurring`, and its `grants` is what the subscription gives while it runs.
132
+ */
133
+ grants?: RecurringGrantSpec;
134
+ }
135
+ /** Where a field of what a subscription grants gets its value (see RecurringSpec.grants). */
136
+ export type RecurringGrantSource = "item" | `item.${string}` | "subscription" | "subscription.holder" | "subscription.payer";
137
+ /** The host's subscription states a grant follows (ADR 0485). */
138
+ export declare const SUBSCRIPTION_GRANT_STATES: readonly ["trialing", "active", "past_due", "unpaid", "paused", "ended"];
139
+ export type SubscriptionGrantState = (typeof SUBSCRIPTION_GRANT_STATES)[number];
140
+ export interface RecurringGrantSpec {
141
+ object: string;
142
+ set: Readonly<Record<string, RecurringGrantSource>>;
143
+ from?: string;
144
+ until?: string;
145
+ states: Readonly<Record<SubscriptionGrantState, string>>;
146
+ }
147
+ /**
148
+ * How the payment provider taxes a line of this object (ADR 0479). Prices stay what the buyer
149
+ * pays: the provider works the tax out inside them.
150
+ *
151
+ * - `code`: the provider's product tax code (Stripe's list: `txcd_` and eight digits). A human
152
+ * admin may override it per object in Settings → Orders.
153
+ * - `place`: for admission to an in-person event, where it is held, so the provider taxes it
154
+ * there and not where the buyer lives. Text fields of this object, or of the record its
155
+ * relation `via` points at (a ticket type's event): `country` (two letters, ISO 3166-1) and,
156
+ * when given, `region`, `postcode`, `city` and `line1`.
157
+ */
158
+ export interface TaxSpec {
159
+ code: string;
160
+ place?: {
161
+ via?: string;
162
+ country: string;
163
+ region?: string;
164
+ postcode?: string;
165
+ city?: string;
166
+ line1?: string;
167
+ };
168
+ }
169
+ /** A product tax code as Stripe's list has them. */
170
+ export declare const TAX_CODE_PATTERN: RegExp;
171
+ export declare const GRANT_SOURCES: readonly ["item", "order", "order.person", "order.email", "line", "line.title", "line.unitPrice", "code"];
172
+ export declare const sellableManifestSchema: z.ZodObject<{
173
+ price: z.ZodString;
174
+ currency: z.ZodOptional<z.ZodString>;
175
+ saleWindow: z.ZodOptional<z.ZodObject<{
176
+ from: z.ZodOptional<z.ZodString>;
177
+ until: z.ZodOptional<z.ZodString>;
178
+ }, z.core.$strict>>;
179
+ maxPerOrder: z.ZodOptional<z.ZodString>;
180
+ deposit: z.ZodOptional<z.ZodObject<{
181
+ amount: z.ZodOptional<z.ZodString>;
182
+ balanceDue: z.ZodOptional<z.ZodString>;
183
+ }, z.core.$strict>>;
184
+ grants: z.ZodOptional<z.ZodObject<{
185
+ object: z.ZodString;
186
+ set: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodEnum<{
187
+ item: "item";
188
+ order: "order";
189
+ "order.person": "order.person";
190
+ "order.email": "order.email";
191
+ line: "line";
192
+ "line.title": "line.title";
193
+ "line.unitPrice": "line.unitPrice";
194
+ code: "code";
195
+ }>, z.ZodString]>>;
196
+ void: z.ZodString;
197
+ valid: z.ZodOptional<z.ZodObject<{
198
+ from: z.ZodOptional<z.ZodString>;
199
+ until: z.ZodOptional<z.ZodString>;
200
+ days: z.ZodOptional<z.ZodType<`item.${string}`, unknown, z.core.$ZodTypeInternals<`item.${string}`, unknown>>>;
201
+ }, z.core.$strict>>;
202
+ }, z.core.$strict>>;
203
+ onePerPerson: z.ZodOptional<z.ZodString>;
204
+ tax: z.ZodOptional<z.ZodObject<{
205
+ code: z.ZodString;
206
+ place: z.ZodOptional<z.ZodObject<{
207
+ via: z.ZodOptional<z.ZodString>;
208
+ country: z.ZodString;
209
+ region: z.ZodOptional<z.ZodString>;
210
+ postcode: z.ZodOptional<z.ZodString>;
211
+ city: z.ZodOptional<z.ZodString>;
212
+ line1: z.ZodOptional<z.ZodString>;
213
+ }, z.core.$strict>>;
214
+ }, z.core.$strict>>;
215
+ recurring: z.ZodOptional<z.ZodObject<{
216
+ interval: z.ZodEnum<{
217
+ week: "week";
218
+ month: "month";
219
+ year: "year";
220
+ }>;
221
+ trialDays: z.ZodOptional<z.ZodString>;
222
+ grants: z.ZodOptional<z.ZodObject<{
223
+ object: z.ZodString;
224
+ set: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodEnum<{
225
+ item: "item";
226
+ subscription: "subscription";
227
+ "subscription.holder": "subscription.holder";
228
+ "subscription.payer": "subscription.payer";
229
+ }>, z.ZodType<`item.${string}`, unknown, z.core.$ZodTypeInternals<`item.${string}`, unknown>>]>>;
230
+ from: z.ZodOptional<z.ZodString>;
231
+ until: z.ZodOptional<z.ZodString>;
232
+ states: z.ZodObject<{
233
+ trialing: z.ZodString;
234
+ active: z.ZodString;
235
+ past_due: z.ZodString;
236
+ unpaid: z.ZodString;
237
+ paused: z.ZodString;
238
+ ended: z.ZodString;
239
+ }, z.core.$strict>;
240
+ }, z.core.$strict>>;
241
+ }, z.core.$strict>>;
242
+ }, z.core.$strict>;
243
+ export type SellableManifest = z.infer<typeof sellableManifestSchema>;
244
+ /** Reports problems with `objects.<name>.sellable` through `contract`. */
245
+ export declare function checkObjectSellable(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
246
+ /**
247
+ * A granted holder (a pass a paid line grants, a membership a subscription grants) keeps the
248
+ * terms it was granted (ADR 0513): its Person, uses, period, dates, zone and the fields its
249
+ * conditions compare with are read-only and never a customer's to write, and nobody but the host
250
+ * (or a tool only a person with a permission may call) moves it into a state it can be spent in.
251
+ * Otherwise a staff write, an assistant or an automation could give someone uses nobody paid for.
252
+ */
253
+ export declare function checkGrantedHolder(m: ExtensionManifest, local: string, granted: ObjectManifest, contract: (path: string, message: string) => void): void;