@composable-os/sdk 0.1.0-beta.10 → 0.1.0-beta.13
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/dist/acknowledge.d.ts +38 -0
- package/dist/acknowledge.js +30 -0
- package/dist/agreements.d.ts +80 -0
- package/dist/agreements.js +109 -0
- package/dist/allowance.d.ts +47 -5
- package/dist/allowance.js +59 -4
- package/dist/automation-check.d.ts +6 -1
- package/dist/automation-check.js +93 -8
- package/dist/automation.d.ts +62 -4
- package/dist/automation.js +32 -9
- package/dist/behaviour.js +25 -6
- package/dist/calendar-entry.d.ts +97 -0
- package/dist/calendar-entry.js +180 -0
- package/dist/capacity.d.ts +69 -0
- package/dist/capacity.js +63 -2
- package/dist/checklists.d.ts +41 -0
- package/dist/checklists.js +75 -0
- package/dist/contract.d.ts +527 -26
- package/dist/contract.js +21 -3
- package/dist/discount.d.ts +135 -0
- package/dist/discount.js +281 -0
- package/dist/document.d.ts +2 -1
- package/dist/document.js +2 -1
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/formula/compile.d.ts +15 -0
- package/dist/formula/compile.js +9 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/intake.d.ts +134 -0
- package/dist/intake.js +239 -0
- package/dist/journey-signals.d.ts +89 -0
- package/dist/journey-signals.js +151 -0
- package/dist/manifest.d.ts +1060 -60
- package/dist/manifest.js +1111 -61
- package/dist/mine.d.ts +45 -0
- package/dist/mine.js +119 -0
- package/dist/occupies.d.ts +64 -0
- package/dist/occupies.js +122 -0
- package/dist/questions.d.ts +179 -0
- package/dist/questions.js +514 -0
- package/dist/recurring.d.ts +33 -4
- package/dist/recurring.js +50 -3
- package/dist/restricted-fields.d.ts +15 -0
- package/dist/restricted-fields.js +97 -0
- package/dist/rich-text.d.ts +22 -0
- package/dist/rich-text.js +91 -0
- package/dist/routine-mail.d.ts +400 -0
- package/dist/routine-mail.js +955 -0
- package/dist/runtime.d.ts +5 -11
- package/dist/runtime.js +21 -11
- package/dist/screens.d.ts +3 -0
- package/dist/screens.js +74 -0
- package/dist/sellable.d.ts +231 -2
- package/dist/sellable.js +342 -1
- package/dist/slug.d.ts +2 -0
- package/dist/slug.js +6 -2
- package/dist/staffed.d.ts +178 -0
- package/dist/staffed.js +287 -0
- package/dist/states.d.ts +74 -4
- package/dist/states.js +154 -58
- package/dist/stock.d.ts +129 -0
- package/dist/stock.js +205 -0
- package/dist/team.d.ts +28 -0
- package/dist/team.js +52 -0
- package/dist/testing.d.ts +22 -1
- package/dist/testing.js +47 -5
- package/dist/worked-on.d.ts +42 -0
- package/dist/worked-on.js +79 -0
- package/package.json +9 -1
- package/src/acknowledge.ts +100 -0
- package/src/agreements.ts +185 -0
- package/src/allowance.ts +98 -8
- package/src/automation-check.ts +120 -9
- package/src/automation.ts +78 -10
- package/src/behaviour.ts +26 -6
- package/src/calendar-entry.ts +258 -0
- package/src/capacity.ts +103 -2
- package/src/checklists.ts +118 -0
- package/src/contract.ts +514 -27
- package/src/discount.ts +408 -0
- package/src/document.ts +4 -2
- package/src/field-types.ts +5 -0
- package/src/formula/compile.ts +10 -0
- package/src/index.ts +17 -0
- package/src/intake.ts +380 -0
- package/src/journey-signals.ts +215 -0
- package/src/manifest.ts +1313 -66
- package/src/mine.ts +161 -0
- package/src/occupies.ts +192 -0
- package/src/questions.ts +681 -0
- package/src/recurring.ts +90 -9
- package/src/restricted-fields.ts +129 -0
- package/src/rich-text.ts +112 -0
- package/src/routine-mail.ts +1272 -0
- package/src/runtime.ts +25 -12
- package/src/screens.ts +91 -0
- package/src/sellable.ts +595 -1
- package/src/slug.ts +7 -2
- package/src/staffed.ts +474 -0
- package/src/states.ts +220 -64
- package/src/stock.ts +332 -0
- package/src/team.ts +83 -0
- package/src/testing.ts +70 -6
- package/src/worked-on.ts +134 -0
package/src/stock.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// The `stock` behaviour (stage 37, ADR 0680): an object declares that its records are things
|
|
2
|
+
// counted on a shelf (a variant of an item in a shop), and the host keeps how many there are, from
|
|
3
|
+
// a ledger nobody edits, never selling more than there is, for every actor. Its declaration, its
|
|
4
|
+
// manifest schema and its contract checks live here; the host's side is packages/core/src/stock/.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
7
|
+
import { moveOrigins } from "./states.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What customers and the public are told about a record's stock (staff always see the numbers):
|
|
11
|
+
* `availability` (the default), "In stock" or "Sold out"; `low`, "only N left" at or below
|
|
12
|
+
* `lowAt`; `exact`, the number available.
|
|
13
|
+
*/
|
|
14
|
+
export const STOCK_SHOW = ["exact", "low", "availability"] as const;
|
|
15
|
+
export type StockShow = (typeof STOCK_SHOW)[number];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The read-only values the host adds to every record of an object with stock: on hand (what is
|
|
19
|
+
* on the shelf, sold units gone), held (by orders being paid for) and available (on hand less
|
|
20
|
+
* held). An object with stock cannot have fields of these names.
|
|
21
|
+
*/
|
|
22
|
+
export const STOCK_VALUES = ["onHand", "held", "available"] as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The kinds of movement in the ledger (ADR 0680). The host writes `opening`, `adjusted` and
|
|
26
|
+
* `sold`; a record's `stockMoves` writes `received`, `written_off`, `used` and `counted` (ADR
|
|
27
|
+
* 0681); a return (stage 37 step 4) writes `returned`.
|
|
28
|
+
*/
|
|
29
|
+
export const STOCK_MOVEMENT_KINDS = [
|
|
30
|
+
"opening",
|
|
31
|
+
"received",
|
|
32
|
+
"sold",
|
|
33
|
+
"returned",
|
|
34
|
+
"written_off",
|
|
35
|
+
"counted",
|
|
36
|
+
"adjusted",
|
|
37
|
+
"used",
|
|
38
|
+
] as const;
|
|
39
|
+
export type StockMovementKind = (typeof STOCK_MOVEMENT_KINDS)[number];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A number of things on hand (ADR 0680). The host keeps the count from a ledger of movements
|
|
43
|
+
* (opening stock, a correction, a sale) and nobody types it: an order line holds its quantity
|
|
44
|
+
* while the order is unpaid and sells it when it is paid, refused by the database past what is
|
|
45
|
+
* available, in the order's own batch. A refund changes nothing in stock; a return does.
|
|
46
|
+
*
|
|
47
|
+
* - `reorderAt` is this object's number field holding the reorder level (a record at or below it
|
|
48
|
+
* is low); `reorderQty` the number field holding how many to order then.
|
|
49
|
+
* - `show` is what customers and the public are told (default `availability`); `lowAt` is the
|
|
50
|
+
* threshold for `low`.
|
|
51
|
+
* - `notify` is one of this extension's tool permissions: when a write takes a record's on hand to
|
|
52
|
+
* or below its reorder level, the staff who hold it are told, once until it rises above again
|
|
53
|
+
* (ADR 0681). Without it, the admins are told. It needs `reorderAt`.
|
|
54
|
+
* - The object is sellable goods (`sellable.goods`, ADR 0682), or the object a `stockMoves` of
|
|
55
|
+
* this extension moves (a part nobody sells still has stock).
|
|
56
|
+
*
|
|
57
|
+
* An object with stock is not also a capacity (its counter is the stock's), and is not bought by
|
|
58
|
+
* deposit, by subscription or by moving a record (`sellable.deposit`, `recurring`, `moves`).
|
|
59
|
+
*/
|
|
60
|
+
export interface StockSpec {
|
|
61
|
+
reorderAt?: string;
|
|
62
|
+
reorderQty?: string;
|
|
63
|
+
show?: StockShow;
|
|
64
|
+
lowAt?: number;
|
|
65
|
+
notify?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const fieldName = z.string().max(40);
|
|
69
|
+
|
|
70
|
+
export const stockManifestSchema = z
|
|
71
|
+
.object({
|
|
72
|
+
reorderAt: fieldName.optional(),
|
|
73
|
+
reorderQty: fieldName.optional(),
|
|
74
|
+
show: z.enum(STOCK_SHOW).optional(),
|
|
75
|
+
lowAt: z.number().int().min(1).max(1_000_000).optional(),
|
|
76
|
+
notify: z.string().max(80).optional(),
|
|
77
|
+
})
|
|
78
|
+
.strict();
|
|
79
|
+
export type StockManifest = z.infer<typeof stockManifestSchema>;
|
|
80
|
+
|
|
81
|
+
/** Reports problems with `objects.<name>.stock` through `contract`. */
|
|
82
|
+
export function checkObjectStock(
|
|
83
|
+
m: ExtensionManifest,
|
|
84
|
+
name: string,
|
|
85
|
+
object: ObjectManifest,
|
|
86
|
+
contract: (path: string, message: string) => void,
|
|
87
|
+
): void {
|
|
88
|
+
const stock = object.stock;
|
|
89
|
+
if (!stock) return;
|
|
90
|
+
const path = `objects.${name}.stock`;
|
|
91
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
92
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
93
|
+
for (const key of ["reorderAt", "reorderQty"] as const) {
|
|
94
|
+
const local = stock[key];
|
|
95
|
+
if (local === undefined) continue;
|
|
96
|
+
const field = fieldOf(local);
|
|
97
|
+
if (field?.type !== "number" || field.many) {
|
|
98
|
+
contract(`${path}.${key}`, `"${local}" must be a number field of ${name}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (const reserved of STOCK_VALUES) {
|
|
102
|
+
if (Object.hasOwn(object.fields, reserved)) {
|
|
103
|
+
contract(
|
|
104
|
+
`objects.${name}.fields.${reserved}`,
|
|
105
|
+
`"${reserved}" is a value the host adds to an object with stock; rename the field`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (object.capacity) {
|
|
110
|
+
contract(path, "An object has stock or a capacity, not both: stock keeps its own counter");
|
|
111
|
+
}
|
|
112
|
+
const sellable = object.sellable;
|
|
113
|
+
if (sellable) {
|
|
114
|
+
for (const key of ["deposit", "recurring", "moves"] as const) {
|
|
115
|
+
if (sellable[key] !== undefined) {
|
|
116
|
+
contract(
|
|
117
|
+
`objects.${name}.sellable.${key}`,
|
|
118
|
+
`A thing in stock is sold whole, on an order: it has no sellable.${key}`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (stock.lowAt !== undefined && stock.show !== "low") {
|
|
124
|
+
contract(`${path}.lowAt`, 'lowAt is the threshold of show: "low"');
|
|
125
|
+
}
|
|
126
|
+
if (stock.show === "low" && stock.lowAt === undefined) {
|
|
127
|
+
contract(`${path}.lowAt`, 'show: "low" needs lowAt, the number at or below which it says so');
|
|
128
|
+
}
|
|
129
|
+
if (stock.notify !== undefined) {
|
|
130
|
+
if (stock.reorderAt === undefined) {
|
|
131
|
+
contract(`${path}.notify`, "notify tells staff of a reorder level: declare reorderAt");
|
|
132
|
+
}
|
|
133
|
+
const permissions = Object.values(m.tools).flatMap((tool) =>
|
|
134
|
+
typeof tool.permission === "string" ? [tool.permission] : [...(tool.permission ?? [])],
|
|
135
|
+
);
|
|
136
|
+
if (!permissions.includes(stock.notify)) {
|
|
137
|
+
contract(
|
|
138
|
+
`${path}.notify`,
|
|
139
|
+
`"${stock.notify}" is not the permission of any of this extension's tools (tools.<name>.permission)`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const moved = Object.values(m.objects).some(
|
|
144
|
+
(other) => other.stockMoves && relationTarget(m, other, other.stockMoves.on) === name,
|
|
145
|
+
);
|
|
146
|
+
if (sellable && !sellable.goods) {
|
|
147
|
+
// A thing in stock that is sold is goods (ADR 0682): its orders have a channel and a delivery.
|
|
148
|
+
contract(
|
|
149
|
+
`objects.${name}.sellable.goods`,
|
|
150
|
+
`${name} has stock and is sold: declare sellable.goods (where it is sold)`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (!sellable && !moved) {
|
|
154
|
+
contract(
|
|
155
|
+
path,
|
|
156
|
+
`${name} has stock but is neither sellable goods nor moved by a stockMoves of this extension`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// --- stockMoves (ADR 0681) ------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
/** How a record's move changes stock: units in, units out, or on hand set to a counted number. */
|
|
164
|
+
export const STOCK_MOVES = ["add", "remove", "set"] as const;
|
|
165
|
+
export type StockMove = (typeof STOCK_MOVES)[number];
|
|
166
|
+
|
|
167
|
+
/** The ledger kinds each move may write; the first is the default. */
|
|
168
|
+
export const STOCK_MOVE_KINDS = {
|
|
169
|
+
add: ["received"],
|
|
170
|
+
remove: ["written_off", "used"],
|
|
171
|
+
set: ["counted"],
|
|
172
|
+
} as const satisfies Record<StockMove, readonly StockMovementKind[]>;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A record's state moves stock (ADR 0681): when it enters `when`, the host writes a movement of
|
|
176
|
+
* `quantity` on the record `on` points to (an object of this extension with stock), in the batch
|
|
177
|
+
* of the write that makes the move, for every actor. A receipt `received` adds; a write-off
|
|
178
|
+
* removes; a count line `set`s on hand to the number counted, the difference worked out when the
|
|
179
|
+
* batch commits.
|
|
180
|
+
*
|
|
181
|
+
* - `when` is a state of this object, or with `through`, of the record `through` points to (a
|
|
182
|
+
* relation, one record, to another object of this extension with states): that record entering
|
|
183
|
+
* `when` moves the stock of every live record of this object pointing to it, in one batch (a
|
|
184
|
+
* count applied moves each of its lines). No declared move leaves `when`: stock moves once.
|
|
185
|
+
* - `kind` is the ledger's kind: `received` for add; `written_off` (default) or `used` for
|
|
186
|
+
* remove; `counted` for set.
|
|
187
|
+
* - `unitCost` (add only) is a number field of the cost of one, in minor units; `note` a text
|
|
188
|
+
* field the ledger keeps.
|
|
189
|
+
* - Every declared move into `when` is by `["host"]`, with the `tools` (humanOnly or
|
|
190
|
+
* personApproves, with a permission) a person makes it through; nothing is created in `when`
|
|
191
|
+
* but by the host. Moving stock is a person's act: an AI's call of the tool waits for a person.
|
|
192
|
+
* - A quantity is a whole number, above 0 (add, remove) or 0 and above (set). Once moved, `on`,
|
|
193
|
+
* `quantity`, `unitCost` and `note` cannot change, and a moved record's lines cannot be added to
|
|
194
|
+
* or changed: the ledger keeps what moved.
|
|
195
|
+
*/
|
|
196
|
+
export interface StockMovesSpec {
|
|
197
|
+
move: StockMove;
|
|
198
|
+
on: string;
|
|
199
|
+
quantity: string;
|
|
200
|
+
when: string;
|
|
201
|
+
through?: string;
|
|
202
|
+
kind?: StockMovementKind;
|
|
203
|
+
unitCost?: string;
|
|
204
|
+
note?: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export const stockMovesManifestSchema = z
|
|
208
|
+
.object({
|
|
209
|
+
move: z.enum(STOCK_MOVES),
|
|
210
|
+
on: fieldName,
|
|
211
|
+
quantity: fieldName,
|
|
212
|
+
when: z.string().min(1).max(100),
|
|
213
|
+
through: fieldName.optional(),
|
|
214
|
+
kind: z.enum(STOCK_MOVEMENT_KINDS).optional(),
|
|
215
|
+
unitCost: fieldName.optional(),
|
|
216
|
+
note: fieldName.optional(),
|
|
217
|
+
})
|
|
218
|
+
.strict();
|
|
219
|
+
export type StockMovesManifest = z.infer<typeof stockMovesManifestSchema>;
|
|
220
|
+
|
|
221
|
+
/** The local name of the object of this extension a one-record relation field points to. */
|
|
222
|
+
function relationTarget(
|
|
223
|
+
m: ExtensionManifest,
|
|
224
|
+
object: ObjectManifest,
|
|
225
|
+
field: string,
|
|
226
|
+
): string | undefined {
|
|
227
|
+
const f = Object.hasOwn(object.fields, field) ? object.fields[field] : undefined;
|
|
228
|
+
if (f?.type !== "relation" || f.many || !f.to?.startsWith(`${m.id}.`)) return undefined;
|
|
229
|
+
const local = f.to.slice(m.id.length + 1);
|
|
230
|
+
return Object.hasOwn(m.objects, local) ? local : undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Reports problems with `objects.<name>.stockMoves` through `contract`. */
|
|
234
|
+
export function checkObjectStockMoves(
|
|
235
|
+
m: ExtensionManifest,
|
|
236
|
+
name: string,
|
|
237
|
+
object: ObjectManifest,
|
|
238
|
+
contract: (path: string, message: string) => void,
|
|
239
|
+
): void {
|
|
240
|
+
const moves = object.stockMoves;
|
|
241
|
+
if (!moves) return;
|
|
242
|
+
const path = `objects.${name}.stockMoves`;
|
|
243
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
244
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
245
|
+
const target = relationTarget(m, object, moves.on);
|
|
246
|
+
if (!target) {
|
|
247
|
+
contract(
|
|
248
|
+
`${path}.on`,
|
|
249
|
+
`"${moves.on}" must be a relation field (one record) to an object of this extension with stock`,
|
|
250
|
+
);
|
|
251
|
+
} else if (!m.objects[target]?.stock) {
|
|
252
|
+
contract(`${path}.on`, `${m.id}.${target} must declare stock for its stock to move`);
|
|
253
|
+
}
|
|
254
|
+
const number = (key: "quantity" | "unitCost") => {
|
|
255
|
+
const local = moves[key];
|
|
256
|
+
if (local === undefined) return;
|
|
257
|
+
const field = fieldOf(local);
|
|
258
|
+
if (field?.type !== "number" || field.many) {
|
|
259
|
+
contract(`${path}.${key}`, `"${local}" must be a number field of ${name}`);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
number("quantity");
|
|
263
|
+
number("unitCost");
|
|
264
|
+
if (moves.unitCost !== undefined && moves.move !== "add") {
|
|
265
|
+
contract(`${path}.unitCost`, "Only what comes in (add) has a unit cost");
|
|
266
|
+
}
|
|
267
|
+
if (moves.note !== undefined) {
|
|
268
|
+
const field = fieldOf(moves.note);
|
|
269
|
+
if (field?.type !== "text" || field.many) {
|
|
270
|
+
contract(`${path}.note`, `"${moves.note}" must be a text field of ${name}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const kinds: readonly string[] = STOCK_MOVE_KINDS[moves.move];
|
|
274
|
+
if (moves.kind !== undefined && !kinds.includes(moves.kind)) {
|
|
275
|
+
contract(
|
|
276
|
+
`${path}.kind`,
|
|
277
|
+
`A ${moves.move} writes ${kinds.map((k) => `"${k}"`).join(" or ")}, not "${moves.kind}"`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
// Whose state moves it: this object's, or the record `through` points to.
|
|
281
|
+
let owner: string | undefined = name;
|
|
282
|
+
if (moves.through !== undefined) {
|
|
283
|
+
owner = relationTarget(m, object, moves.through);
|
|
284
|
+
if (!owner || owner === name) {
|
|
285
|
+
contract(
|
|
286
|
+
`${path}.through`,
|
|
287
|
+
`"${moves.through}" must be a relation field (one record) to another of this extension's objects`,
|
|
288
|
+
);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const states = m.objects[owner]?.states;
|
|
293
|
+
if (!states) {
|
|
294
|
+
contract(
|
|
295
|
+
moves.through === undefined ? path : `${path}.through`,
|
|
296
|
+
`${owner} must declare states: entering "${moves.when}" moves the stock`,
|
|
297
|
+
);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const stateField = Object.hasOwn(m.objects[owner]?.fields ?? {}, states.field)
|
|
301
|
+
? m.objects[owner]?.fields[states.field]
|
|
302
|
+
: undefined;
|
|
303
|
+
if (!(stateField?.options ?? []).some((o) => o.value === moves.when)) {
|
|
304
|
+
contract(`${path}.when`, `"${moves.when}" is not a state of ${owner}`);
|
|
305
|
+
}
|
|
306
|
+
if (states.moves.some((move) => moveOrigins(move).includes(moves.when))) {
|
|
307
|
+
contract(
|
|
308
|
+
`${path}.when`,
|
|
309
|
+
`No move may leave "${moves.when}": stock moves once, when a record enters it`,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
// Moving stock is a person's act with the pack's permission (ADR 0681, the review of #422):
|
|
313
|
+
// the host's alone, and the tools a person calls (ADR 0513), so an assistant's or an MCP
|
|
314
|
+
// client's call of them waits for a person, and no customer, member or automation moves stock
|
|
315
|
+
// by moving a record.
|
|
316
|
+
states.moves.forEach((move, i) => {
|
|
317
|
+
if (move.to !== moves.when) return;
|
|
318
|
+
if (move.by.some((mover) => mover !== "host")) {
|
|
319
|
+
contract(
|
|
320
|
+
`objects.${owner}.states.moves.${i}.by`,
|
|
321
|
+
`Entering "${moves.when}" moves stock: declare it by ["host"] with the tools a person makes it through (states.moves.<n>.tools)`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
const created = states.create?.in?.[moves.when];
|
|
326
|
+
if (created?.some((mover) => mover !== "host")) {
|
|
327
|
+
contract(
|
|
328
|
+
`objects.${owner}.states.create.in.${moves.when}`,
|
|
329
|
+
`Creating a record in "${moves.when}" moves stock: only the host may; create it in another state and move it through a tool`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
}
|
package/src/team.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// A shipped pack's team (stage 56, ADR 0831; built with Programs' coach role, ADR 0889): the staff
|
|
2
|
+
// named on a live record about a Person look after that Person while the record is live. Its
|
|
3
|
+
// declaration, its manifest schema and its contract checks live here; what a pack role reaches
|
|
4
|
+
// through it (`looksAfter`) is the host's (packages/core/src/pack-role-looks-after.ts).
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Who on the staff looks after the Person a record is about (ADR 0831): the accounts in its
|
|
10
|
+
* `staff` user fields, while the record is in one of the `live` states of the object's `states`
|
|
11
|
+
* (every record not in the Trash, when the object has no states). A client's program names their
|
|
12
|
+
* coach: `{ person: "person", staff: ["coach"], live: ["generating", "active"] }`.
|
|
13
|
+
*
|
|
14
|
+
* It gives nothing by itself. A pack role that declares `looksAfter` reaches its objects' records
|
|
15
|
+
* only for the People its holder looks after this way (ADR 0889). Only shipped packs and the
|
|
16
|
+
* org's own extensions declare it: who reaches whose records is access.
|
|
17
|
+
*/
|
|
18
|
+
export interface TeamSpec {
|
|
19
|
+
/** A relation (one record) to `person`: whom the record is about. */
|
|
20
|
+
person: string;
|
|
21
|
+
/** `user` fields of this object (one account each, each `personApproves`): who looks after them. */
|
|
22
|
+
staff: readonly string[];
|
|
23
|
+
/** The states of the object's `states` in which the record counts. */
|
|
24
|
+
live?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const fieldName = z.string().max(40);
|
|
28
|
+
|
|
29
|
+
export const teamManifestSchema = z
|
|
30
|
+
.object({
|
|
31
|
+
person: fieldName,
|
|
32
|
+
staff: z.array(fieldName).min(1).max(4),
|
|
33
|
+
live: z.array(z.string().min(1).max(100)).min(1).max(20).optional(),
|
|
34
|
+
})
|
|
35
|
+
.strict();
|
|
36
|
+
export type TeamManifest = z.infer<typeof teamManifestSchema>;
|
|
37
|
+
|
|
38
|
+
/** Reports problems with `objects.<name>.team` through `contract`. */
|
|
39
|
+
export function checkObjectTeam(
|
|
40
|
+
_m: ExtensionManifest,
|
|
41
|
+
name: string,
|
|
42
|
+
object: ObjectManifest,
|
|
43
|
+
contract: (path: string, message: string) => void,
|
|
44
|
+
): void {
|
|
45
|
+
const team = object.team;
|
|
46
|
+
if (!team) return;
|
|
47
|
+
const at = `objects.${name}.team`;
|
|
48
|
+
const own = (local: string): FieldManifest | undefined =>
|
|
49
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
50
|
+
const person = own(team.person);
|
|
51
|
+
if (person?.type !== "relation" || person.to !== "person" || person.many) {
|
|
52
|
+
contract(`${at}.person`, `"${team.person}" must be a relation field (one record) to person`);
|
|
53
|
+
}
|
|
54
|
+
for (const [i, field] of team.staff.entries()) {
|
|
55
|
+
const staff = own(field);
|
|
56
|
+
if (staff?.type !== "user" || staff.many) {
|
|
57
|
+
contract(`${at}.staff.${i}`, `"${field}" must be a user field of ${name}`);
|
|
58
|
+
} else if (staff.personApproves !== true) {
|
|
59
|
+
// Naming someone in it opens a pack role's reach to the Person (ADR 0889): a person's
|
|
60
|
+
// choice, never the assistant's, an automation's or code's on its own.
|
|
61
|
+
contract(
|
|
62
|
+
`${at}.staff.${i}`,
|
|
63
|
+
`"${field}" must be a field a person approves (personApproves): who reaches whose records is a person's to change`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (new Set(team.staff).size !== team.staff.length) {
|
|
68
|
+
contract(`${at}.staff`, "Name each field once");
|
|
69
|
+
}
|
|
70
|
+
if (team.live) {
|
|
71
|
+
const states = object.states;
|
|
72
|
+
const known = new Set(
|
|
73
|
+
states ? (own(states.field)?.options ?? []).map((option) => option.value) : [],
|
|
74
|
+
);
|
|
75
|
+
if (!states) {
|
|
76
|
+
contract(`${at}.live`, `${name} has no states: leave live out (every record counts)`);
|
|
77
|
+
} else {
|
|
78
|
+
for (const [i, state] of team.live.entries()) {
|
|
79
|
+
if (!known.has(state)) contract(`${at}.live.${i}`, `"${state}" is not a state of ${name}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/testing.ts
CHANGED
|
@@ -28,12 +28,14 @@ import {
|
|
|
28
28
|
type NotificationSendInput,
|
|
29
29
|
notificationInput,
|
|
30
30
|
} from "./automation.ts";
|
|
31
|
+
import { DEFAULT_WAIT_TIMEOUT, NOTIFY_OPERATION } from "./automation-check.ts";
|
|
31
32
|
import { type AiModule, type Extension, GATEKEEPER_MODULES, type StartedBy } from "./contract.ts";
|
|
32
33
|
import { validateDocument } from "./document.ts";
|
|
33
34
|
import type { ExtensionManifest, ManifestIssue, ValidateOptions } from "./manifest.ts";
|
|
34
35
|
import { CORE_OBJECT_NAMES, toManifest, validateManifest } from "./manifest.ts";
|
|
35
36
|
import { checkGrant } from "./permissions.ts";
|
|
36
37
|
import { documentMaxBytesOf, documentSchemaOf } from "./rich-text.ts";
|
|
38
|
+
import type { RoutineSendInput, RoutineSendResult } from "./routine-mail.ts";
|
|
37
39
|
import { buildContext, type RawHostModules, runExtension } from "./runtime.ts";
|
|
38
40
|
|
|
39
41
|
type Values = Record<string, unknown> & { id: string };
|
|
@@ -63,6 +65,18 @@ export interface TestHostOptions {
|
|
|
63
65
|
string,
|
|
64
66
|
(call: { connectionId: string; resource: string; method: string; args: unknown[] }) => unknown
|
|
65
67
|
>;
|
|
68
|
+
/**
|
|
69
|
+
* What `ctx.routineMail.send` answers (ADR 0590). Every call is recorded in `routineSends`;
|
|
70
|
+
* with no fake each answers one email sent. The host decides who it goes to and what it says,
|
|
71
|
+
* so a pack's test checks which kind, record, message and values it asked for.
|
|
72
|
+
*/
|
|
73
|
+
routineMail?: (send: RoutineSendInput) => RoutineSendResult | Promise<RoutineSendResult>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A routine email an automation asked the host to send (`ctx.routineMail.send`). */
|
|
77
|
+
export interface RoutineSend extends RoutineSendInput {
|
|
78
|
+
/** The step it was asked in. */
|
|
79
|
+
step: string | null;
|
|
66
80
|
}
|
|
67
81
|
|
|
68
82
|
/** A call a tool or automation made through `ctx.gatekeeper(name).call(...)`. */
|
|
@@ -99,6 +113,13 @@ export interface AutomationRunOptions {
|
|
|
99
113
|
runId?: string;
|
|
100
114
|
/** `event.test`. Default false: the run writes to the in-memory store. */
|
|
101
115
|
test?: boolean;
|
|
116
|
+
/** `event.originalRunId`: the run a retry's chain began with (ADR 0574). */
|
|
117
|
+
originalRunId?: string;
|
|
118
|
+
/**
|
|
119
|
+
* How many times a `do` step that throws is run again, as the host's Workflow retries a step
|
|
120
|
+
* (the steps before it are not run again). Default 0: a throw fails the run.
|
|
121
|
+
*/
|
|
122
|
+
retries?: number;
|
|
102
123
|
}
|
|
103
124
|
|
|
104
125
|
export interface AutomationRunResult {
|
|
@@ -152,6 +173,8 @@ export interface TestHost {
|
|
|
152
173
|
notifications: SentNotification[];
|
|
153
174
|
/** Every call through a gatekeeper module, in order. */
|
|
154
175
|
gatekeeperCalls: GatekeeperCall[];
|
|
176
|
+
/** Every routine email asked for, in order (ADR 0590). */
|
|
177
|
+
routineSends: RoutineSend[];
|
|
155
178
|
/**
|
|
156
179
|
* Runs automation `name` in memory, start to finish: conditions checked on the trigger
|
|
157
180
|
* record, waits skipped (and recorded), steps held to the declarations.
|
|
@@ -160,7 +183,7 @@ export interface TestHost {
|
|
|
160
183
|
/**
|
|
161
184
|
* Publishes a record of a publishable object as a person would (`record.publish`, which no
|
|
162
185
|
* tool may call): its content fields become the next version, which `record.version` reads.
|
|
163
|
-
* Returns the version's number.
|
|
186
|
+
* Refused outside the object's `publishable.publishIn` states. Returns the version's number.
|
|
164
187
|
*/
|
|
165
188
|
publish(object: string, id: string): number;
|
|
166
189
|
}
|
|
@@ -221,6 +244,16 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
221
244
|
if (!content) throw new ExtensionToolError("invalid_input", `${object} is not publishable`);
|
|
222
245
|
const row = table(object).get(id);
|
|
223
246
|
if (!row) throw new ExtensionToolError("not_found", `No ${object} with id ${id}`);
|
|
247
|
+
// Only in the states the object lists (ADR 0810), as the host refuses.
|
|
248
|
+
const spec = manifest.objects[local];
|
|
249
|
+
const stateField = spec?.states?.field;
|
|
250
|
+
const publishIn = spec?.publishable?.publishIn;
|
|
251
|
+
if (publishIn && stateField && !publishIn.includes(String(row[stateField] ?? ""))) {
|
|
252
|
+
throw new ExtensionToolError(
|
|
253
|
+
"invalid_input",
|
|
254
|
+
`${object} ${id} is ${String(row[stateField] ?? "in no state")}: it is published only when ${publishIn.join(" or ")}`,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
224
257
|
const list = versions.get(`${object}|${id}`) ?? [];
|
|
225
258
|
const version = list.length + 1;
|
|
226
259
|
list.push({
|
|
@@ -483,7 +516,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
483
516
|
let observer:
|
|
484
517
|
| ((operation: string, input: Record<string, unknown>, value: unknown) => void)
|
|
485
518
|
| null = null;
|
|
486
|
-
builtIn[
|
|
519
|
+
builtIn[NOTIFY_OPERATION] = (input) => {
|
|
487
520
|
const notes: string[] = [];
|
|
488
521
|
const admins = users.filter((u) => u.role === "admin").map((u) => u.id);
|
|
489
522
|
const ids: string[] = [];
|
|
@@ -616,6 +649,27 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
616
649
|
types: async () => "",
|
|
617
650
|
};
|
|
618
651
|
}
|
|
652
|
+
const routineSends: RoutineSend[] = [];
|
|
653
|
+
if (has("routine-mail")) {
|
|
654
|
+
raw["routine-mail"] = {
|
|
655
|
+
send: async (input) => {
|
|
656
|
+
const send = structuredClone(input) as RoutineSendInput;
|
|
657
|
+
routineSends.push({ ...send, step: currentStep });
|
|
658
|
+
try {
|
|
659
|
+
const value = options.routineMail
|
|
660
|
+
? await options.routineMail(send)
|
|
661
|
+
: { sent: 1, held: 0, off: false, none: false };
|
|
662
|
+
return { ok: true, value };
|
|
663
|
+
} catch (error) {
|
|
664
|
+
const e = error as { code?: string; message?: string };
|
|
665
|
+
return {
|
|
666
|
+
ok: false,
|
|
667
|
+
error: { code: e.code ?? "internal", message: e.message ?? String(error) },
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
};
|
|
672
|
+
}
|
|
619
673
|
if (has("ai")) {
|
|
620
674
|
const ai: AiModule = {
|
|
621
675
|
summarise: async (text) => String(text).slice(0, 100),
|
|
@@ -666,6 +720,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
666
720
|
const event: AutomationEvent = {
|
|
667
721
|
automation: `${manifest.id}.${name}`,
|
|
668
722
|
runId: run.runId ?? "arn_TEST",
|
|
723
|
+
...(run.originalRunId ? { originalRunId: run.originalRunId } : {}),
|
|
669
724
|
trigger: automation.trigger.type,
|
|
670
725
|
...(run.record && row
|
|
671
726
|
? {
|
|
@@ -712,7 +767,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
712
767
|
stepName ? `step ${stepName}` : "run (outside a step)";
|
|
713
768
|
const notificationsBefore = notifications.length;
|
|
714
769
|
observer = (operation, input, value) => {
|
|
715
|
-
if (operation ===
|
|
770
|
+
if (operation === NOTIFY_OPERATION) {
|
|
716
771
|
if (!declaresNotify(currentStep)) {
|
|
717
772
|
result.undeclared.push(`${where(currentStep)} sent a notification (not in its notifies)`);
|
|
718
773
|
}
|
|
@@ -742,7 +797,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
742
797
|
const n = (notifyCounts.get(key) ?? 0) + 1;
|
|
743
798
|
notifyCounts.set(key, n);
|
|
744
799
|
const input = notificationInput(event, to, message, `${key}:${n}`);
|
|
745
|
-
const sent = await base.ops.call<{ sent: number }>(
|
|
800
|
+
const sent = await base.ops.call<{ sent: number }>(NOTIFY_OPERATION, input);
|
|
746
801
|
return { sent: sent.sent };
|
|
747
802
|
},
|
|
748
803
|
};
|
|
@@ -764,7 +819,15 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
764
819
|
const first = calls.length;
|
|
765
820
|
currentStep = stepName;
|
|
766
821
|
try {
|
|
767
|
-
|
|
822
|
+
let value!: T;
|
|
823
|
+
for (let attempt = 0; ; attempt++) {
|
|
824
|
+
try {
|
|
825
|
+
value = await fn();
|
|
826
|
+
break;
|
|
827
|
+
} catch (error) {
|
|
828
|
+
if (attempt >= (run.retries ?? 0)) throw error;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
768
831
|
result.steps.push({
|
|
769
832
|
name: stepName,
|
|
770
833
|
kind: "do",
|
|
@@ -792,7 +855,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
792
855
|
name: stepName,
|
|
793
856
|
kind: "waitForEvent",
|
|
794
857
|
value,
|
|
795
|
-
skippedMs: durationMs((step.timeout ??
|
|
858
|
+
skippedMs: durationMs((step.timeout ?? DEFAULT_WAIT_TIMEOUT) as never),
|
|
796
859
|
calls: [],
|
|
797
860
|
});
|
|
798
861
|
return value;
|
|
@@ -825,6 +888,7 @@ export function createTestHost(extension: Extension, options: TestHostOptions =
|
|
|
825
888
|
records,
|
|
826
889
|
notifications,
|
|
827
890
|
gatekeeperCalls,
|
|
891
|
+
routineSends,
|
|
828
892
|
runAutomation,
|
|
829
893
|
publish,
|
|
830
894
|
async runTool<T>(
|