@composable-os/sdk 0.1.0-beta.11 → 0.1.0-beta.14

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 (104) hide show
  1. package/dist/acknowledge.d.ts +38 -0
  2. package/dist/acknowledge.js +30 -0
  3. package/dist/agreements.d.ts +80 -0
  4. package/dist/agreements.js +109 -0
  5. package/dist/allowance.d.ts +47 -5
  6. package/dist/allowance.js +59 -4
  7. package/dist/automation-check.d.ts +6 -1
  8. package/dist/automation-check.js +93 -8
  9. package/dist/automation.d.ts +62 -4
  10. package/dist/automation.js +32 -9
  11. package/dist/behaviour.js +25 -6
  12. package/dist/calendar-entry.d.ts +97 -0
  13. package/dist/calendar-entry.js +180 -0
  14. package/dist/capacity.js +1 -2
  15. package/dist/checklists.d.ts +41 -0
  16. package/dist/checklists.js +75 -0
  17. package/dist/contract.d.ts +495 -26
  18. package/dist/contract.js +21 -3
  19. package/dist/discount.d.ts +135 -0
  20. package/dist/discount.js +281 -0
  21. package/dist/document.d.ts +2 -1
  22. package/dist/document.js +2 -1
  23. package/dist/field-types.d.ts +1 -1
  24. package/dist/field-types.js +5 -0
  25. package/dist/formula/compile.d.ts +15 -0
  26. package/dist/formula/compile.js +9 -0
  27. package/dist/index.d.ts +17 -0
  28. package/dist/index.js +17 -0
  29. package/dist/intake.d.ts +134 -0
  30. package/dist/intake.js +239 -0
  31. package/dist/journey-signals.d.ts +89 -0
  32. package/dist/journey-signals.js +151 -0
  33. package/dist/manifest.d.ts +944 -56
  34. package/dist/manifest.js +1036 -60
  35. package/dist/mine.d.ts +45 -0
  36. package/dist/mine.js +119 -0
  37. package/dist/occupies.d.ts +64 -0
  38. package/dist/occupies.js +122 -0
  39. package/dist/questions.d.ts +179 -0
  40. package/dist/questions.js +514 -0
  41. package/dist/recurring.d.ts +33 -4
  42. package/dist/recurring.js +50 -3
  43. package/dist/restricted-fields.d.ts +15 -0
  44. package/dist/restricted-fields.js +97 -0
  45. package/dist/rich-text.d.ts +22 -0
  46. package/dist/rich-text.js +91 -0
  47. package/dist/routine-mail.d.ts +400 -0
  48. package/dist/routine-mail.js +955 -0
  49. package/dist/runtime.d.ts +5 -11
  50. package/dist/runtime.js +21 -11
  51. package/dist/screens.d.ts +3 -0
  52. package/dist/screens.js +74 -0
  53. package/dist/sellable.d.ts +153 -2
  54. package/dist/sellable.js +188 -0
  55. package/dist/slug.d.ts +2 -0
  56. package/dist/slug.js +6 -2
  57. package/dist/staffed.d.ts +178 -0
  58. package/dist/staffed.js +287 -0
  59. package/dist/states.d.ts +17 -1
  60. package/dist/states.js +48 -20
  61. package/dist/stock.d.ts +129 -0
  62. package/dist/stock.js +205 -0
  63. package/dist/team.d.ts +28 -0
  64. package/dist/team.js +52 -0
  65. package/dist/testing.d.ts +22 -1
  66. package/dist/testing.js +47 -5
  67. package/dist/worked-on.d.ts +42 -0
  68. package/dist/worked-on.js +79 -0
  69. package/package.json +9 -1
  70. package/src/acknowledge.ts +100 -0
  71. package/src/agreements.ts +185 -0
  72. package/src/allowance.ts +98 -8
  73. package/src/automation-check.ts +120 -9
  74. package/src/automation.ts +78 -10
  75. package/src/behaviour.ts +26 -6
  76. package/src/calendar-entry.ts +258 -0
  77. package/src/capacity.ts +1 -2
  78. package/src/checklists.ts +118 -0
  79. package/src/contract.ts +489 -27
  80. package/src/discount.ts +408 -0
  81. package/src/document.ts +4 -2
  82. package/src/field-types.ts +5 -0
  83. package/src/formula/compile.ts +10 -0
  84. package/src/index.ts +17 -0
  85. package/src/intake.ts +380 -0
  86. package/src/journey-signals.ts +215 -0
  87. package/src/manifest.ts +1236 -65
  88. package/src/mine.ts +161 -0
  89. package/src/occupies.ts +192 -0
  90. package/src/questions.ts +681 -0
  91. package/src/recurring.ts +90 -9
  92. package/src/restricted-fields.ts +129 -0
  93. package/src/rich-text.ts +112 -0
  94. package/src/routine-mail.ts +1272 -0
  95. package/src/runtime.ts +25 -12
  96. package/src/screens.ts +91 -0
  97. package/src/sellable.ts +344 -0
  98. package/src/slug.ts +7 -2
  99. package/src/staffed.ts +474 -0
  100. package/src/states.ts +68 -26
  101. package/src/stock.ts +332 -0
  102. package/src/team.ts +83 -0
  103. package/src/testing.ts +70 -6
  104. package/src/worked-on.ts +134 -0
@@ -0,0 +1,38 @@
1
+ import type { DocumentSchema } from "./document.ts";
2
+ /** What an entry of `roles` may be: a staff role, `<pack>.<role>` or `<pack>:<permission>`. */
3
+ export declare const MUST_READ_ENTRY_RE: RegExp;
4
+ /** The most entries one "must read" lists. */
5
+ export declare const MUST_READ_MAX = 50;
6
+ /** The one schema of an acknowledged object's "must read" field. */
7
+ export declare const MUST_READ_SCHEMA: DocumentSchema;
8
+ /** One notice's words: `{record}` in either stands for the record's title. */
9
+ export interface AcknowledgeNotice {
10
+ title: string;
11
+ body?: string;
12
+ }
13
+ /** The notices the host sends for an acknowledged object (ADR 0706). */
14
+ export interface AcknowledgeNotices {
15
+ /** A version that asks everyone goes live: to everyone who must read it, once per version. */
16
+ asked?: AcknowledgeNotice;
17
+ /** Still to read `afterDays` after it became theirs to read: to that person, once per version. */
18
+ reminder?: AcknowledgeNotice & {
19
+ afterDays: number;
20
+ };
21
+ /** Each week while anyone has not confirmed: to the record's `owner`. */
22
+ outstanding?: AcknowledgeNotice;
23
+ /** A new staff account with records to read: to that person, once, listing them. */
24
+ welcome?: AcknowledgeNotice;
25
+ }
26
+ /** `publishable.acknowledge`: see the comment above and ADRs 0702 and 0706. */
27
+ export interface AcknowledgeSpec {
28
+ who: string;
29
+ manage: string;
30
+ managed?: readonly string[];
31
+ owner?: string;
32
+ notices?: AcknowledgeNotices;
33
+ }
34
+ /** The longest a notice's title and body may be. */
35
+ export declare const ACKNOWLEDGE_NOTICE_TITLE_MAX = 120;
36
+ export declare const ACKNOWLEDGE_NOTICE_BODY_MAX = 600;
37
+ /** The longest a reminder may wait, in days. */
38
+ export declare const ACKNOWLEDGE_REMINDER_MAX_DAYS = 60;
@@ -0,0 +1,30 @@
1
+ /** What an entry of `roles` may be: a staff role, `<pack>.<role>` or `<pack>:<permission>`. */
2
+ export const MUST_READ_ENTRY_RE = /^(?:admin|member|viewer|[a-z][a-z0-9]{1,29}[.:][a-zA-Z0-9]{1,40})$/;
3
+ /** The most entries one "must read" lists. */
4
+ export const MUST_READ_MAX = 50;
5
+ /** The one schema of an acknowledged object's "must read" field. */
6
+ export const MUST_READ_SCHEMA = {
7
+ root: {
8
+ type: "object",
9
+ fields: {
10
+ everyone: { type: "boolean", description: "Every staff account must read it" },
11
+ roles: {
12
+ type: "array",
13
+ max: MUST_READ_MAX,
14
+ optional: true,
15
+ description: "Staff roles, pack roles and granted pack permissions that must read it",
16
+ of: {
17
+ type: "text",
18
+ min: 1,
19
+ max: 71,
20
+ chars: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:",
21
+ },
22
+ },
23
+ },
24
+ },
25
+ };
26
+ /** The longest a notice's title and body may be. */
27
+ export const ACKNOWLEDGE_NOTICE_TITLE_MAX = 120;
28
+ export const ACKNOWLEDGE_NOTICE_BODY_MAX = 600;
29
+ /** The longest a reminder may wait, in days. */
30
+ export const ACKNOWLEDGE_REMINDER_MAX_DAYS = 60;
@@ -0,0 +1,80 @@
1
+ import { z } from "zod";
2
+ import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
3
+ /** Kinds of agreement document a business writes (`care` is the platform's own, stage 43). */
4
+ export declare const AGREEMENT_KINDS: readonly ["terms", "waiver", "health", "guardian", "other"];
5
+ export type AgreementKind = (typeof AGREEMENT_KINDS)[number];
6
+ /** A point's name: a lowercase word, shared by objects that mean the same place. */
7
+ export declare const AGREEMENT_POINT_RE: RegExp;
8
+ /**
9
+ * An agreement point (ADR 0641).
10
+ *
11
+ * - `point`: the point's name. Objects may share one (a membership plan and a pass type are both
12
+ * "buying a membership or a pass").
13
+ * - `label`: what the admin shows when attaching documents.
14
+ * - `suggests`: kinds of document the admin offers first. A suggestion, never a requirement.
15
+ * - `enter` and `person`: for a record a caller makes (a booking), the states whose entry is checked
16
+ * and the relation to the Person it is for. Left out, the point is checked where the object is
17
+ * bought (a `sellable` object).
18
+ * - `keeps`: with `enter`, for an object without `claims`, the states a record keeps its entry in
19
+ * (a ticket valid or used at the door), as `claims` says which states hold a place: a record
20
+ * created in one is asked, and a move between two of them is the same entry and is not (staff
21
+ * undoing a check-in). Left out, only `enter`'s states count.
22
+ * - `importMovesOver`: with `enter`, a record the record import makes (a person's door:
23
+ * `cos courses import`) is someone moving over from another system, and is not asked (stage 44,
24
+ * issue #281). A later import that gives it to another Person or makes it count again is asked.
25
+ * Left out, an import is asked as any caller's write.
26
+ * - `inlineStep`: the pack's own screens (its blocks that buy or book this object) show what is
27
+ * due and let a person agree on the way (stage 44 step 5). Left out, nothing on the pack's
28
+ * screens can agree, so a document attached there refuses every purchase or booking until the
29
+ * person agrees elsewhere (My agreements, the desk, paper): the admin is warned when attaching.
30
+ *
31
+ * In stage 44 step 1 the declaration is inert: it is checked and listed, and documents can be
32
+ * attached to it; the host asks it from step 3.
33
+ */
34
+ export interface AgreementsSpec {
35
+ point: string;
36
+ label: string;
37
+ suggests?: readonly AgreementKind[];
38
+ enter?: readonly string[];
39
+ person?: string;
40
+ keeps?: readonly string[];
41
+ importMovesOver?: boolean;
42
+ inlineStep?: boolean;
43
+ }
44
+ export declare const agreementsManifestSchema: z.ZodObject<{
45
+ point: z.ZodString;
46
+ label: z.ZodString;
47
+ suggests: z.ZodOptional<z.ZodArray<z.ZodEnum<{
48
+ other: "other";
49
+ terms: "terms";
50
+ waiver: "waiver";
51
+ health: "health";
52
+ guardian: "guardian";
53
+ }>>>;
54
+ enter: z.ZodOptional<z.ZodArray<z.ZodString>>;
55
+ person: z.ZodOptional<z.ZodString>;
56
+ keeps: z.ZodOptional<z.ZodArray<z.ZodString>>;
57
+ importMovesOver: z.ZodOptional<z.ZodBoolean>;
58
+ inlineStep: z.ZodOptional<z.ZodBoolean>;
59
+ }, z.core.$strip>;
60
+ export type AgreementsManifest = z.infer<typeof agreementsManifestSchema>;
61
+ /** The contract checks of an object's `agreements` declaration. */
62
+ export declare function checkObjectAgreements(_m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
63
+ /**
64
+ * A health-note list (stage 44, ADR 0643): records of this object make a list a person on the
65
+ * staff reads (a class's bookings), and the host may mark, beside each Person on it, a yes on
66
+ * their current health form, nothing else. `list` is the relation to the list's own record (the
67
+ * class), `person` the relation to the Person. The host reads the People itself; pack code names
68
+ * only the list's record (`agreement.listHealthNotes`), inside a tool a person called.
69
+ */
70
+ export interface HealthNotesSpec {
71
+ list: string;
72
+ person: string;
73
+ }
74
+ export declare const healthNotesManifestSchema: z.ZodObject<{
75
+ list: z.ZodString;
76
+ person: z.ZodString;
77
+ }, z.core.$strip>;
78
+ export type HealthNotesManifest = z.infer<typeof healthNotesManifestSchema>;
79
+ /** The contract checks of an object's `healthNotes` declaration. */
80
+ export declare function checkObjectHealthNotes(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
@@ -0,0 +1,109 @@
1
+ // The `agreements` behaviour (stage 44, ADR 0641): an object marks an agreement point, a place a
2
+ // business may need a person to have agreed to its documents first (buying a membership, making a
3
+ // booking). The pack names the point; the business attaches its own documents to it in the admin.
4
+ // Nothing is needed anywhere until a person publishes a document and attaches it. Its declaration,
5
+ // its manifest schema and its contract checks live here; the host's side is
6
+ // packages/core/src/agreements/.
7
+ import { z } from "zod";
8
+ import { moveOrigins } from "./states.js";
9
+ /** Kinds of agreement document a business writes (`care` is the platform's own, stage 43). */
10
+ export const AGREEMENT_KINDS = ["terms", "waiver", "health", "guardian", "other"];
11
+ /** A point's name: a lowercase word, shared by objects that mean the same place. */
12
+ export const AGREEMENT_POINT_RE = /^[a-z][a-z0-9_]{1,39}$/;
13
+ export const agreementsManifestSchema = z.object({
14
+ point: z.string().max(40),
15
+ label: z.string().min(1).max(80),
16
+ suggests: z.array(z.enum(AGREEMENT_KINDS)).max(AGREEMENT_KINDS.length).optional(),
17
+ enter: z.array(z.string().max(40)).min(1).max(20).optional(),
18
+ person: z.string().max(40).optional(),
19
+ keeps: z.array(z.string().max(40)).min(1).max(20).optional(),
20
+ importMovesOver: z.boolean().optional(),
21
+ inlineStep: z.boolean().optional(),
22
+ });
23
+ /** The states an object's `states` declaration names. */
24
+ function stateNames(object) {
25
+ const states = object.states;
26
+ if (!states)
27
+ return new Set();
28
+ return new Set([
29
+ states.initial,
30
+ ...states.moves.flatMap((move) => [...moveOrigins(move), move.to]),
31
+ ]);
32
+ }
33
+ /** The contract checks of an object's `agreements` declaration. */
34
+ export function checkObjectAgreements(_m, name, object, contract) {
35
+ const declared = object.agreements;
36
+ if (!declared)
37
+ return;
38
+ const path = `objects.${name}.agreements`;
39
+ if (!AGREEMENT_POINT_RE.test(declared.point)) {
40
+ contract(`${path}.point`, "point must be a lowercase word (letters, digits, _), 2 to 40 long");
41
+ }
42
+ if (declared.suggests && new Set(declared.suggests).size !== declared.suggests.length) {
43
+ contract(`${path}.suggests`, "suggests lists each kind once");
44
+ }
45
+ if (!declared.enter) {
46
+ if (declared.person !== undefined) {
47
+ contract(`${path}.person`, "person is for a point checked when a record enters a state");
48
+ }
49
+ if (declared.keeps !== undefined) {
50
+ contract(`${path}.keeps`, "keeps is for a point checked when a record enters a state");
51
+ }
52
+ if (declared.importMovesOver !== undefined) {
53
+ contract(`${path}.importMovesOver`, "importMovesOver is for a point checked when a record enters a state");
54
+ }
55
+ if (!object.sellable) {
56
+ contract(path, `${name} is not sellable: a point on it needs enter (the states whose entry is checked) and person`);
57
+ }
58
+ return;
59
+ }
60
+ const states = stateNames(object);
61
+ if (!object.states) {
62
+ contract(`${path}.enter`, `enter names states, and ${name} declares none`);
63
+ }
64
+ else {
65
+ for (const [i, state] of declared.enter.entries()) {
66
+ if (!states.has(state))
67
+ contract(`${path}.enter.${i}`, `"${state}" is not a state of ${name}`);
68
+ }
69
+ for (const [i, state] of (declared.keeps ?? []).entries()) {
70
+ if (!states.has(state))
71
+ contract(`${path}.keeps.${i}`, `"${state}" is not a state of ${name}`);
72
+ }
73
+ }
74
+ if (declared.keeps && object.claims) {
75
+ contract(`${path}.keeps`, `${name} holds places (claims): its claims say which states keep it`);
76
+ }
77
+ const person = declared.person;
78
+ const field = person !== undefined && Object.hasOwn(object.fields, person)
79
+ ? object.fields[person]
80
+ : undefined;
81
+ if (field?.type !== "relation" || field.many || field.to !== "person") {
82
+ contract(`${path}.person`, `person must be a relation field (one record) to person`);
83
+ }
84
+ }
85
+ export const healthNotesManifestSchema = z.object({
86
+ list: z.string().max(40),
87
+ person: z.string().max(40),
88
+ });
89
+ /** The contract checks of an object's `healthNotes` declaration. */
90
+ export function checkObjectHealthNotes(m, name, object, contract) {
91
+ const declared = object.healthNotes;
92
+ if (!declared)
93
+ return;
94
+ const path = `objects.${name}.healthNotes`;
95
+ const one = (field) => Object.hasOwn(object.fields, field) ? object.fields[field] : undefined;
96
+ const list = one(declared.list);
97
+ const self = `${m.id}.${name}`;
98
+ if (list?.type !== "relation" ||
99
+ list.many ||
100
+ list.to === "person" ||
101
+ list.to === self ||
102
+ list.to === name) {
103
+ contract(`${path}.list`, "list must be a relation field (one record) to another object");
104
+ }
105
+ const person = one(declared.person);
106
+ if (person?.type !== "relation" || person.many || person.to !== "person") {
107
+ contract(`${path}.person`, "person must be a relation field (one record) to person");
108
+ }
109
+ }
@@ -21,12 +21,19 @@ export declare const ALLOWANCE_VALUES: readonly ["used", "left"];
21
21
  * 16:00, `below: 960`). An empty holder field sets no bound.
22
22
  *
23
23
  * A spender with no value for `field` meets no condition that sets a limit.
24
+ *
25
+ * - `value` (stage 49, ADR 0764), in place of `field`: what every record of the spender is, a fixed
26
+ * word compared with `in` ("sessions": an appointment, never a class). No caller sets it.
27
+ * - `empty` (stage 49, ADR 0764): `"none"` makes an empty holder field for `in` allow nothing
28
+ * instead of anything: a pass that names no services covers no appointment. Left out, `"any"`.
24
29
  */
25
30
  export interface AllowanceCondition {
26
- field: string;
31
+ field?: string;
32
+ value?: string;
27
33
  in?: string | readonly string[];
28
34
  atLeast?: string | number;
29
35
  below?: string | number;
36
+ empty?: "any" | "none";
30
37
  }
31
38
  /** What a holder may be spent on: an object that declares `allowance.spends`, and conditions. */
32
39
  export interface AllowanceSpendsOn {
@@ -83,7 +90,8 @@ export interface AllowanceHolderSpec {
83
90
  * be live then. Left out: the time of the spend.
84
91
  * - `copy`: read-only fields of its own the host fills, at create and at each spend, from one
85
92
  * relation away (`{ classType: "class.classType" }`), so a holder's `when` can read them and no
86
- * caller can set them.
93
+ * caller can set them. `<relation>.id` is the related record's own id (`{ serviceId:
94
+ * "service.id" }`, ADR 0764): an appointment's service, which its kind and length follow.
87
95
  * - `local`: read-only number fields of its own the host fills from `at` in `zone` (a timezone
88
96
  * field of its own or one relation away; the organisation's when left out or empty):
89
97
  * `weekday` (1 Monday to 7 Sunday) and `minute` (minutes since local midnight).
@@ -91,6 +99,12 @@ export interface AllowanceHolderSpec {
91
99
  * A record created into a spend state with it set takes the use of the record it names: in the
92
100
  * same batch the host moves that record to `state` (a return state) and spends from the same
93
101
  * holder, so a move never fails for want of a use the member already had.
102
+ * - `fixed` (ADR 0764): fields of its own that change only by such a move, never by an update,
103
+ * for every caller but the host (an appointment's times and service): a session a late cancel
104
+ * used never comes back by moving its time first.
105
+ * - `anchor` (ADR 0764): `{ field, from }`, a read-only datetime field the host sets at create:
106
+ * the moved record's, or else its own `from` (its start). A window may count from it, so a
107
+ * moved record keeps the late point of the one it replaced. It is fixed too.
94
108
  * - `paidBy`: holders of other extensions that may pay for it, by object (`memberships.pass`) or
95
109
  * by extension (`memberships`). Left out, only this extension's holders. A holder naming a
96
110
  * spender of another extension that does not list it is refused at install.
@@ -99,6 +113,12 @@ export interface AllowanceHolderSpec {
99
113
  * its order). Only the host and this extension's own code set it, never a customer, and it
100
114
  * cannot change while the record is in a spend state.
101
115
  *
116
+ * - `released` (stage 44 step 7, ADR 0647): a state of `returnIn` the host moves a record into
117
+ * when the holder it spent from is ended early by its payer's cooling off and the record's time
118
+ * is still ahead (a class not yet started): its use is returned and its place freed, as any move
119
+ * into a return state does. Each state a record holds a use in (`spendIn`) needs a move into it
120
+ * by the host. Left out, such records are kept.
121
+ *
102
122
  * Every field a holder's condition reads must be one the host fills (`copy` or `local`), so no
103
123
  * caller (a customer, staff, an AI) sets what pays for it.
104
124
  */
@@ -117,9 +137,15 @@ export interface AllowanceSpenderSpec {
117
137
  movedFrom?: {
118
138
  field: string;
119
139
  state: string;
140
+ fixed?: readonly string[];
141
+ anchor?: {
142
+ field: string;
143
+ from: string;
144
+ };
120
145
  };
121
146
  paidBy?: readonly string[];
122
147
  waived?: string;
148
+ released?: string;
123
149
  }
124
150
  /** The allowance behaviour: an object holds uses or spends them, never both. */
125
151
  export type AllowanceSpec = {
@@ -132,27 +158,37 @@ export declare const allowanceManifestSchema: z.ZodUnion<readonly [z.ZodObject<{
132
158
  person: z.ZodString;
133
159
  uses: z.ZodString;
134
160
  per: z.ZodUnion<readonly [z.ZodEnum<{
161
+ total: "total";
135
162
  week: "week";
136
163
  month: "month";
137
- total: "total";
138
164
  }>, z.ZodObject<{
139
165
  field: z.ZodString;
140
166
  }, z.core.$strict>]>;
141
167
  spendsOn: z.ZodUnion<readonly [z.ZodObject<{
142
168
  object: z.ZodString;
143
169
  when: z.ZodOptional<z.ZodArray<z.ZodObject<{
144
- field: z.ZodString;
170
+ field: z.ZodOptional<z.ZodString>;
171
+ value: z.ZodOptional<z.ZodString>;
145
172
  in: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
146
173
  atLeast: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
147
174
  below: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
175
+ empty: z.ZodOptional<z.ZodEnum<{
176
+ any: "any";
177
+ none: "none";
178
+ }>>;
148
179
  }, z.core.$strict>>>;
149
180
  }, z.core.$strict>, z.ZodArray<z.ZodObject<{
150
181
  object: z.ZodString;
151
182
  when: z.ZodOptional<z.ZodArray<z.ZodObject<{
152
- field: z.ZodString;
183
+ field: z.ZodOptional<z.ZodString>;
184
+ value: z.ZodOptional<z.ZodString>;
153
185
  in: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
154
186
  atLeast: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
155
187
  below: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
188
+ empty: z.ZodOptional<z.ZodEnum<{
189
+ any: "any";
190
+ none: "none";
191
+ }>>;
156
192
  }, z.core.$strict>>>;
157
193
  }, z.core.$strict>>]>;
158
194
  from: z.ZodOptional<z.ZodString>;
@@ -177,9 +213,15 @@ export declare const allowanceManifestSchema: z.ZodUnion<readonly [z.ZodObject<{
177
213
  movedFrom: z.ZodOptional<z.ZodObject<{
178
214
  field: z.ZodString;
179
215
  state: z.ZodString;
216
+ fixed: z.ZodOptional<z.ZodArray<z.ZodString>>;
217
+ anchor: z.ZodOptional<z.ZodObject<{
218
+ field: z.ZodString;
219
+ from: z.ZodString;
220
+ }, z.core.$strict>>;
180
221
  }, z.core.$strict>>;
181
222
  paidBy: z.ZodOptional<z.ZodArray<z.ZodString>>;
182
223
  waived: z.ZodOptional<z.ZodString>;
224
+ released: z.ZodOptional<z.ZodString>;
183
225
  }, z.core.$strict>;
184
226
  }, z.core.$strict>]>;
185
227
  export type AllowanceManifest = z.infer<typeof allowanceManifestSchema>;
package/dist/allowance.js CHANGED
@@ -4,6 +4,7 @@
4
4
  // Its declaration, its manifest schema and its contract checks live here; the host's side is
5
5
  // packages/core/src/allowance/.
6
6
  import { z } from "zod";
7
+ import { moveOrigins } from "./states.js";
7
8
  /** What a holder's uses are counted over. Weeks start on Monday; both in the holder's zone. */
8
9
  export const ALLOWANCE_PERIODS = ["total", "week", "month"];
9
10
  /**
@@ -17,10 +18,12 @@ const path = z.string().max(81);
17
18
  const stateValue = z.string().min(1).max(100);
18
19
  const conditionSchema = z
19
20
  .object({
20
- field: fieldName,
21
+ field: fieldName.optional(),
22
+ value: z.string().min(1).max(100).optional(),
21
23
  in: z.union([fieldName, z.array(z.string().max(200)).min(1).max(100)]).optional(),
22
24
  atLeast: z.union([fieldName, z.number()]).optional(),
23
25
  below: z.union([fieldName, z.number()]).optional(),
26
+ empty: z.enum(["any", "none"]).optional(),
24
27
  })
25
28
  .strict();
26
29
  const spendsOnSchema = z
@@ -62,9 +65,18 @@ export const allowanceManifestSchema = z.union([
62
65
  })
63
66
  .strict()
64
67
  .optional(),
65
- movedFrom: z.object({ field: fieldName, state: stateValue }).strict().optional(),
68
+ movedFrom: z
69
+ .object({
70
+ field: fieldName,
71
+ state: stateValue,
72
+ fixed: z.array(fieldName).min(1).max(10).optional(),
73
+ anchor: z.object({ field: fieldName, from: fieldName }).strict().optional(),
74
+ })
75
+ .strict()
76
+ .optional(),
66
77
  paidBy: z.array(path).min(1).max(20).optional(),
67
78
  waived: fieldName.optional(),
79
+ released: stateValue.optional(),
68
80
  })
69
81
  .strict(),
70
82
  })
@@ -221,8 +233,21 @@ export function checkObjectAllowance(m, name, object, contract) {
221
233
  if (ops.length !== 1) {
222
234
  contract(cp, "A condition compares with exactly one of in, atLeast and below");
223
235
  }
224
- const field = fieldOf(spender, clause.field);
225
- if (!field || field.type === "formula") {
236
+ if ((clause.field === undefined) === (clause.value === undefined)) {
237
+ contract(cp, "A condition reads exactly one of field and value");
238
+ }
239
+ if (clause.value !== undefined && clause.in === undefined) {
240
+ contract(`${cp}.value`, "A fixed value is compared with in");
241
+ }
242
+ if (clause.empty !== undefined && clause.in === undefined) {
243
+ contract(`${cp}.empty`, "empty says what an empty in allows: it needs in");
244
+ }
245
+ // A fixed word (`value`) is what every record of the spender is: only the holder's is read.
246
+ const field = clause.field === undefined ? null : fieldOf(spender, clause.field);
247
+ if (clause.field === undefined) {
248
+ // Nothing of the spender to check.
249
+ }
250
+ else if (!field || field.type === "formula") {
226
251
  contract(`${cp}.field`, `"${clause.field}" must be a field of ${spenderName}`);
227
252
  }
228
253
  else if (!filledBy.has(clause.field)) {
@@ -289,6 +314,9 @@ export function checkObjectAllowance(m, name, object, contract) {
289
314
  const other = ownObject(relation.to);
290
315
  if (!other)
291
316
  return null; // Another extension's object or a core one: the host checks.
317
+ // The related record's own id (ADR 0764): what it is, which only the relation sets.
318
+ if (target === "id")
319
+ return { type: "text" };
292
320
  const field = fieldOf(other[1], target);
293
321
  if (!field)
294
322
  contract(p, `"${target}" is not a field of ${other[0]}`);
@@ -356,11 +384,38 @@ export function checkObjectAllowance(m, name, object, contract) {
356
384
  }
357
385
  notCustomerWritten(s.waived, `${sp}.waived`, "whether it spends a use");
358
386
  }
387
+ if (s.released !== undefined) {
388
+ if (!s.returnIn.includes(s.released)) {
389
+ contract(`${sp}.released`, `"${s.released}" must be one of returnIn: a released record gives its use back`);
390
+ }
391
+ else if (object.states) {
392
+ for (const from of s.spendIn) {
393
+ const ok = object.states.moves.some((move) => move.to === s.released && moveOrigins(move).includes(from) && move.by.includes("host"));
394
+ if (!ok) {
395
+ contract(`${sp}.released`, `A move from "${from}" into "${s.released}" by the host must be declared: the host releases it`);
396
+ }
397
+ }
398
+ }
399
+ }
359
400
  if (s.movedFrom) {
360
401
  const rel = fieldOf(object, s.movedFrom.field);
361
402
  if (rel?.type !== "relation" || rel.many || (rel.to !== `${m.id}.${name}` && rel.to !== name)) {
362
403
  contract(`${sp}.movedFrom.field`, `"${s.movedFrom.field}" must be a relation field (one record) to ${m.id}.${name}`);
363
404
  }
405
+ for (const f of s.movedFrom.fixed ?? []) {
406
+ if (!fieldOf(object, f))
407
+ contract(`${sp}.movedFrom.fixed`, `"${f}" is not a field of ${name}`);
408
+ }
409
+ if (s.movedFrom.anchor) {
410
+ const { field, from } = s.movedFrom.anchor;
411
+ const a = fieldOf(object, field);
412
+ if (a?.type !== "datetime" || a.many || !a.readOnly) {
413
+ contract(`${sp}.movedFrom.anchor.field`, `"${field}" must be a read-only datetime field of ${name}: the host sets it`);
414
+ }
415
+ if (fieldOf(object, from)?.type !== "datetime") {
416
+ contract(`${sp}.movedFrom.anchor.from`, `"${from}" must be a datetime field of ${name}`);
417
+ }
418
+ }
364
419
  if (!s.returnIn.includes(s.movedFrom.state)) {
365
420
  contract(`${sp}.movedFrom.state`, `"${s.movedFrom.state}" must be one of returnIn: the moved record gives its use back`);
366
421
  }
@@ -1,6 +1,6 @@
1
1
  import { type AutomationManifest } from "./automation.ts";
2
2
  import type { FieldTypeName } from "./field-types.ts";
3
- import type { ExtensionManifest } from "./manifest.ts";
3
+ import { type ExtensionManifest } from "./manifest.ts";
4
4
  /** Waits in one run (sleeps plus waitForEvent timeouts) add up to at most this. */
5
5
  export declare const MAX_TOTAL_WAIT_DAYS = 365;
6
6
  /** A waitForEvent without a timeout waits this long (ADR 0130). */
@@ -34,5 +34,10 @@ export interface AutomationCheckContext {
34
34
  export declare function triggerObject(trigger: AutomationManifest["trigger"]): string | null;
35
35
  /** Problems with a 5-field cron expression (empty when valid). */
36
36
  export declare function cronIssues(cron: string): string[];
37
+ /**
38
+ * True when `object` is of an extension in `onlyWith` that is not installed (no object of it is
39
+ * `known`): what an automation running only with it names of it is checked once it is (ADR 0812).
40
+ */
41
+ export declare function laterObject(onlyWith: readonly string[], known: ReadonlySet<string>, object: string): boolean;
37
42
  /** Reports problems with `automations.<name>` through `ctx.contract`. */
38
43
  export declare function checkAutomation(ctx: AutomationCheckContext, name: string, automation: AutomationManifest): void;