@composable-os/sdk 0.0.0-stage → 0.1.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +51 -0
- package/dist/capacity.js +68 -0
- package/dist/contract.d.ts +706 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +165 -0
- package/dist/document.js +636 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +93 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +21 -0
- package/dist/manifest.d.ts +1131 -0
- package/dist/manifest.js +1229 -0
- package/dist/media.d.ts +9 -0
- package/dist/media.js +43 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +34 -0
- package/dist/rich-text.js +138 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +103 -0
- package/dist/sellable.js +258 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +64 -0
- package/dist/states.js +101 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +670 -0
- package/package.json +34 -4
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +112 -0
- package/src/contract.ts +766 -0
- package/src/document.ts +788 -0
- package/src/field-types.ts +106 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +33 -0
- package/src/manifest.ts +1484 -0
- package/src/media.ts +46 -0
- package/src/permissions.ts +60 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +173 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +396 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +162 -0
- package/src/testing.ts +836 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
> This licence covers the repository except the directories listed in NOTICE. In particular, the
|
|
4
|
+
> assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
|
|
5
|
+
> licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
|
|
6
|
+
|
|
7
|
+
## Abbreviation
|
|
8
|
+
|
|
9
|
+
FSL-1.1-ALv2
|
|
10
|
+
|
|
11
|
+
## Notice
|
|
12
|
+
|
|
13
|
+
Copyright 2026 Tim O'Connell
|
|
14
|
+
|
|
15
|
+
## Terms and Conditions
|
|
16
|
+
|
|
17
|
+
### Licensor ("We")
|
|
18
|
+
|
|
19
|
+
The party offering the Software under these Terms and Conditions.
|
|
20
|
+
|
|
21
|
+
### The Software
|
|
22
|
+
|
|
23
|
+
The "Software" is each version of the software that we make available under
|
|
24
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
25
|
+
Conditions with the Software.
|
|
26
|
+
|
|
27
|
+
### License Grant
|
|
28
|
+
|
|
29
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
30
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
31
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
32
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
33
|
+
|
|
34
|
+
### Permitted Purpose
|
|
35
|
+
|
|
36
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
37
|
+
means making the Software available to others in a commercial product or
|
|
38
|
+
service that:
|
|
39
|
+
|
|
40
|
+
1. substitutes for the Software;
|
|
41
|
+
|
|
42
|
+
2. substitutes for any other product or service we offer using the Software
|
|
43
|
+
that exists as of the date we make the Software available; or
|
|
44
|
+
|
|
45
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
46
|
+
|
|
47
|
+
Permitted Purposes specifically include using the Software:
|
|
48
|
+
|
|
49
|
+
1. for your internal use and access;
|
|
50
|
+
|
|
51
|
+
2. for non-commercial education;
|
|
52
|
+
|
|
53
|
+
3. for non-commercial research; and
|
|
54
|
+
|
|
55
|
+
4. in connection with professional services that you provide to a licensee
|
|
56
|
+
using the Software in accordance with these Terms and Conditions.
|
|
57
|
+
|
|
58
|
+
### Patents
|
|
59
|
+
|
|
60
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
61
|
+
patents, the license grant above includes a license under our patents. If you
|
|
62
|
+
make a claim against any party that the Software infringes or contributes to
|
|
63
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
64
|
+
immediately.
|
|
65
|
+
|
|
66
|
+
### Redistribution
|
|
67
|
+
|
|
68
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
69
|
+
the Software.
|
|
70
|
+
|
|
71
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
72
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
73
|
+
remove any copyright notices provided in or with the Software.
|
|
74
|
+
|
|
75
|
+
### Disclaimer
|
|
76
|
+
|
|
77
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
78
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
79
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
80
|
+
|
|
81
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
82
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
83
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
84
|
+
|
|
85
|
+
### Trademarks
|
|
86
|
+
|
|
87
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
88
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
89
|
+
trademarks, trade names, service marks or product names.
|
|
90
|
+
|
|
91
|
+
## Grant of Future License
|
|
92
|
+
|
|
93
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
94
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
95
|
+
the date we make the Software available. On or after that date, you may use the
|
|
96
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
97
|
+
will apply:
|
|
98
|
+
|
|
99
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
100
|
+
this file except in compliance with the License.
|
|
101
|
+
|
|
102
|
+
You may obtain a copy of the License at
|
|
103
|
+
|
|
104
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
105
|
+
|
|
106
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
107
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
108
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
109
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type AutomationManifest } from "./automation.ts";
|
|
2
|
+
import type { FieldTypeName } from "./field-types.ts";
|
|
3
|
+
import type { ExtensionManifest } from "./manifest.ts";
|
|
4
|
+
/** Waits in one run (sleeps plus waitForEvent timeouts) add up to at most this. */
|
|
5
|
+
export declare const MAX_TOTAL_WAIT_DAYS = 365;
|
|
6
|
+
/** A waitForEvent without a timeout waits this long (ADR 0130). */
|
|
7
|
+
export declare const DEFAULT_WAIT_TIMEOUT: `${number} days`;
|
|
8
|
+
/** Workflows event types: `^[a-zA-Z0-9_][a-zA-Z0-9-_]*$`, at most 100 characters. */
|
|
9
|
+
export declare const WAIT_EVENT_RE: RegExp;
|
|
10
|
+
/** The operation an automation needs to notify anyone (`notifies`, `ctx.notify`, `onRejected`). */
|
|
11
|
+
export declare const NOTIFY_OPERATION = "notification.send";
|
|
12
|
+
/**
|
|
13
|
+
* Field types of the core objects automations can trigger on and notify from (the host checks
|
|
14
|
+
* this against its field registry, packages/core/test/sdk-core-fields.test.ts). `user` marks the
|
|
15
|
+
* fields that hold a user id (`{ field }` and `{ managerOf }` recipients).
|
|
16
|
+
*/
|
|
17
|
+
export declare const CORE_AUTOMATION_FIELDS: Record<string, Record<string, FieldTypeName>>;
|
|
18
|
+
/**
|
|
19
|
+
* Base records with operations of their own (stage 25's orders) that an automation may be
|
|
20
|
+
* started by (stage 27): a ticket sent when an order is first paid. They are not core objects:
|
|
21
|
+
* nothing else in a manifest may name them, and the automation reads one only through its
|
|
22
|
+
* `<object>.get`, which it must list in permissions.operations.
|
|
23
|
+
*/
|
|
24
|
+
export declare const BASE_AUTOMATION_FIELDS: Record<string, Record<string, FieldTypeName>>;
|
|
25
|
+
export interface AutomationCheckContext {
|
|
26
|
+
manifest: ExtensionManifest;
|
|
27
|
+
/** Objects that exist (core, this extension's, installed ones the host knows). */
|
|
28
|
+
known: ReadonlySet<string>;
|
|
29
|
+
/** Field types of an object outside this extension, from the host; null: unknown (skip). */
|
|
30
|
+
objectFields?: (object: string) => Record<string, FieldTypeName> | null;
|
|
31
|
+
contract(path: string, message: string): void;
|
|
32
|
+
}
|
|
33
|
+
/** The object a trigger hands its run as the trigger record (null: none). */
|
|
34
|
+
export declare function triggerObject(trigger: AutomationManifest["trigger"]): string | null;
|
|
35
|
+
/** Problems with a 5-field cron expression (empty when valid). */
|
|
36
|
+
export declare function cronIssues(cron: string): string[];
|
|
37
|
+
/** Reports problems with `automations.<name>` through `ctx.contract`. */
|
|
38
|
+
export declare function checkAutomation(ctx: AutomationCheckContext, name: string, automation: AutomationManifest): void;
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
// Contract checks for `automations` (ADR 0028, ADR 0131). Called by validateManifest for every
|
|
2
|
+
// declared automation, after the zod structure check. Every message names the problem and the fix
|
|
3
|
+
// in terms a model can act on: the field that does not exist and the ones that do, the permission
|
|
4
|
+
// to add, the step name that is repeated.
|
|
5
|
+
import { durationMs, } from "./automation.js";
|
|
6
|
+
/** Waits in one run (sleeps plus waitForEvent timeouts) add up to at most this. */
|
|
7
|
+
export const MAX_TOTAL_WAIT_DAYS = 365;
|
|
8
|
+
/** A waitForEvent without a timeout waits this long (ADR 0130). */
|
|
9
|
+
export const DEFAULT_WAIT_TIMEOUT = "7 days";
|
|
10
|
+
/** Workflows event types: `^[a-zA-Z0-9_][a-zA-Z0-9-_]*$`, at most 100 characters. */
|
|
11
|
+
export const WAIT_EVENT_RE = /^[a-zA-Z0-9_][a-zA-Z0-9-_]{0,99}$/;
|
|
12
|
+
const NAME_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
13
|
+
const OPERATION_RE = /^[a-z][a-zA-Z0-9]*(\.[a-z][a-zA-Z0-9]*)+$/;
|
|
14
|
+
const DAY_MS = 86_400_000;
|
|
15
|
+
/** The operation an automation needs to notify anyone (`notifies`, `ctx.notify`, `onRejected`). */
|
|
16
|
+
export const NOTIFY_OPERATION = "notification.send";
|
|
17
|
+
/**
|
|
18
|
+
* Field types of the core objects automations can trigger on and notify from (the host checks
|
|
19
|
+
* this against its field registry, packages/core/test/sdk-core-fields.test.ts). `user` marks the
|
|
20
|
+
* fields that hold a user id (`{ field }` and `{ managerOf }` recipients).
|
|
21
|
+
*/
|
|
22
|
+
export const CORE_AUTOMATION_FIELDS = {
|
|
23
|
+
person: {
|
|
24
|
+
displayName: "text",
|
|
25
|
+
givenName: "text",
|
|
26
|
+
familyName: "text",
|
|
27
|
+
primaryEmail: "email",
|
|
28
|
+
primaryPhone: "phone",
|
|
29
|
+
jobTitle: "text",
|
|
30
|
+
organisationId: "relation",
|
|
31
|
+
location: "text",
|
|
32
|
+
timezone: "text",
|
|
33
|
+
ownerId: "user",
|
|
34
|
+
createdBy: "user",
|
|
35
|
+
createdAt: "datetime",
|
|
36
|
+
updatedAt: "datetime",
|
|
37
|
+
},
|
|
38
|
+
organisation: {
|
|
39
|
+
name: "text",
|
|
40
|
+
primaryDomain: "text",
|
|
41
|
+
website: "url",
|
|
42
|
+
industry: "text",
|
|
43
|
+
parentId: "relation",
|
|
44
|
+
ownerId: "user",
|
|
45
|
+
createdBy: "user",
|
|
46
|
+
createdAt: "datetime",
|
|
47
|
+
updatedAt: "datetime",
|
|
48
|
+
},
|
|
49
|
+
task: {
|
|
50
|
+
title: "text",
|
|
51
|
+
notes: "longText",
|
|
52
|
+
dueAt: "datetime",
|
|
53
|
+
status: "select",
|
|
54
|
+
completedAt: "datetime",
|
|
55
|
+
assigneeId: "user",
|
|
56
|
+
createdBy: "user",
|
|
57
|
+
createdAt: "datetime",
|
|
58
|
+
updatedAt: "datetime",
|
|
59
|
+
},
|
|
60
|
+
activity: {
|
|
61
|
+
type: "select",
|
|
62
|
+
subject: "text",
|
|
63
|
+
body: "longText",
|
|
64
|
+
occurredAt: "datetime",
|
|
65
|
+
authorUserId: "user",
|
|
66
|
+
createdAt: "datetime",
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Base records with operations of their own (stage 25's orders) that an automation may be
|
|
71
|
+
* started by (stage 27): a ticket sent when an order is first paid. They are not core objects:
|
|
72
|
+
* nothing else in a manifest may name them, and the automation reads one only through its
|
|
73
|
+
* `<object>.get`, which it must list in permissions.operations.
|
|
74
|
+
*/
|
|
75
|
+
export const BASE_AUTOMATION_FIELDS = {
|
|
76
|
+
order: {
|
|
77
|
+
state: "select",
|
|
78
|
+
email: "email",
|
|
79
|
+
personId: "relation",
|
|
80
|
+
currency: "text",
|
|
81
|
+
total: "number",
|
|
82
|
+
paid: "number",
|
|
83
|
+
balance: "number",
|
|
84
|
+
pay: "select",
|
|
85
|
+
deposit: "number",
|
|
86
|
+
balanceDueAt: "datetime",
|
|
87
|
+
paidAt: "datetime",
|
|
88
|
+
createdAt: "datetime",
|
|
89
|
+
updatedAt: "datetime",
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
/** Fields every extension record has besides its declared ones. */
|
|
93
|
+
const RECORD_FIELDS = {
|
|
94
|
+
createdAt: "datetime",
|
|
95
|
+
updatedAt: "datetime",
|
|
96
|
+
createdBy: "user",
|
|
97
|
+
};
|
|
98
|
+
const list = (names) => (names.length ? names.join(", ") : "none");
|
|
99
|
+
/** Field name → type for `object`, or null when this check cannot know its fields. */
|
|
100
|
+
function fieldsOf(ctx, object) {
|
|
101
|
+
const m = ctx.manifest;
|
|
102
|
+
// Fields this extension adds to another object, by local name and by registry name
|
|
103
|
+
// (`<ext>.<field>`, what record changes and so fieldChanged triggers use).
|
|
104
|
+
const added = Object.fromEntries(Object.entries(m.fields[object] ?? {}).flatMap(([name, field]) => [
|
|
105
|
+
[name, field.type],
|
|
106
|
+
[`${m.id}.${name}`, field.type],
|
|
107
|
+
]));
|
|
108
|
+
if (object.startsWith(`${m.id}.`)) {
|
|
109
|
+
const own = m.objects[object.slice(m.id.length + 1)];
|
|
110
|
+
if (!own)
|
|
111
|
+
return null;
|
|
112
|
+
return {
|
|
113
|
+
...RECORD_FIELDS,
|
|
114
|
+
...Object.fromEntries(Object.entries(own.fields).map(([name, f]) => [name, f.type])),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const core = CORE_AUTOMATION_FIELDS[object] ?? BASE_AUTOMATION_FIELDS[object];
|
|
118
|
+
if (core)
|
|
119
|
+
return { ...core, ...added };
|
|
120
|
+
const host = ctx.objectFields?.(object);
|
|
121
|
+
return host ? { ...RECORD_FIELDS, ...host, ...added } : null;
|
|
122
|
+
}
|
|
123
|
+
/** The object a trigger hands its run as the trigger record (null: none). */
|
|
124
|
+
export function triggerObject(trigger) {
|
|
125
|
+
switch (trigger.type) {
|
|
126
|
+
case "record":
|
|
127
|
+
case "fieldChanged":
|
|
128
|
+
case "dateRelative":
|
|
129
|
+
case "manual":
|
|
130
|
+
return trigger.object;
|
|
131
|
+
// The synced email or meeting (an activity) is the trigger record.
|
|
132
|
+
case "syncEvent":
|
|
133
|
+
return "activity";
|
|
134
|
+
default:
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const CRON_FIELDS = [
|
|
139
|
+
{ name: "minute", min: 0, max: 59 },
|
|
140
|
+
{ name: "hour", min: 0, max: 23 },
|
|
141
|
+
{ name: "day of month", min: 1, max: 31 },
|
|
142
|
+
{
|
|
143
|
+
name: "month",
|
|
144
|
+
min: 1,
|
|
145
|
+
max: 12,
|
|
146
|
+
names: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
|
|
147
|
+
},
|
|
148
|
+
{ name: "day of week", min: 0, max: 7, names: ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] },
|
|
149
|
+
];
|
|
150
|
+
/** Problems with a 5-field cron expression (empty when valid). */
|
|
151
|
+
export function cronIssues(cron) {
|
|
152
|
+
const parts = cron.trim().split(/\s+/);
|
|
153
|
+
if (parts.length !== 5) {
|
|
154
|
+
return [
|
|
155
|
+
`"${cron}" has ${parts.length} fields; a schedule is 5 (minute hour day-of-month month ` +
|
|
156
|
+
'day-of-week), e.g. "0 8 * * 1-5" for 08:00 on weekdays',
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
const issues = [];
|
|
160
|
+
parts.forEach((part, i) => {
|
|
161
|
+
const spec = CRON_FIELDS[i];
|
|
162
|
+
const names = "names" in spec ? spec.names : [];
|
|
163
|
+
const value = (text) => {
|
|
164
|
+
const upper = text.toUpperCase();
|
|
165
|
+
const named = names.indexOf(upper);
|
|
166
|
+
if (named >= 0)
|
|
167
|
+
return named + (spec.name === "month" ? 1 : 0);
|
|
168
|
+
if (!/^\d+$/.test(text))
|
|
169
|
+
return null;
|
|
170
|
+
const n = Number(text);
|
|
171
|
+
return n >= spec.min && n <= spec.max ? n : null;
|
|
172
|
+
};
|
|
173
|
+
for (const item of part.split(",")) {
|
|
174
|
+
const [range, step, extra] = item.split("/");
|
|
175
|
+
const bad = () => issues.push(`Cron ${spec.name} "${part}" is not valid: use *, a number ${spec.min}-${spec.max}` +
|
|
176
|
+
`${names.length ? ` or a name (${names[0]}…)` : ""}, a range a-b, a list a,b or a step */n`);
|
|
177
|
+
if (extra !== undefined || range === undefined || range === "") {
|
|
178
|
+
bad();
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (step !== undefined && !/^[1-9]\d*$/.test(step)) {
|
|
182
|
+
bad();
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (range === "*")
|
|
186
|
+
continue;
|
|
187
|
+
const [from, to, more] = range.split("-");
|
|
188
|
+
if (more !== undefined) {
|
|
189
|
+
bad();
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const a = value(from ?? "");
|
|
193
|
+
const b = to === undefined ? a : value(to);
|
|
194
|
+
if (a === null || b === null || b < a)
|
|
195
|
+
bad();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
return issues;
|
|
199
|
+
}
|
|
200
|
+
/** Field names a filter reads. */
|
|
201
|
+
function filterFields(filter) {
|
|
202
|
+
if ("and" in filter)
|
|
203
|
+
return filter.and.flatMap(filterFields);
|
|
204
|
+
if ("or" in filter)
|
|
205
|
+
return filter.or.flatMap(filterFields);
|
|
206
|
+
return [filter.field];
|
|
207
|
+
}
|
|
208
|
+
const isStageType = (match) => typeof match === "object" && match !== null && "stageType" in match;
|
|
209
|
+
/** Reports problems with `automations.<name>` through `ctx.contract`. */
|
|
210
|
+
export function checkAutomation(ctx, name, automation) {
|
|
211
|
+
const m = ctx.manifest;
|
|
212
|
+
const path = `automations.${name}`;
|
|
213
|
+
const { contract } = ctx;
|
|
214
|
+
const perms = m.permissions;
|
|
215
|
+
const own = (object) => object.startsWith(`${m.id}.`);
|
|
216
|
+
const canWrite = (object) => own(object) || perms.write.includes(object);
|
|
217
|
+
const canRead = (object) => canWrite(object) || perms.read.includes(object);
|
|
218
|
+
const trigger = automation.trigger;
|
|
219
|
+
const object = triggerObject(trigger);
|
|
220
|
+
const base = object !== null && Object.hasOwn(BASE_AUTOMATION_FIELDS, object);
|
|
221
|
+
const fields = object && (ctx.known.has(object) || base) ? fieldsOf(ctx, object) : null;
|
|
222
|
+
const ofType = (types) => Object.entries(fields ?? {})
|
|
223
|
+
.filter(([, type]) => types.includes(type))
|
|
224
|
+
.map(([field]) => field);
|
|
225
|
+
if (!NAME_RE.test(name)) {
|
|
226
|
+
contract(path, `Automation name "${name}" must be camelCase letters and digits, e.g. wonDeal`);
|
|
227
|
+
}
|
|
228
|
+
// 1. The trigger.
|
|
229
|
+
if (base && !perms.operations.includes(`${object}.get`)) {
|
|
230
|
+
contract(`${path}.trigger.object`, `An automation started by ${object} reads it: add "${object}.get" to permissions.operations`);
|
|
231
|
+
}
|
|
232
|
+
if (object !== null && !ctx.known.has(object) && !base) {
|
|
233
|
+
contract(`${path}.trigger.object`, `Unknown object "${object}". Use a core object (person, organisation, task, activity) or ` +
|
|
234
|
+
"<extension>.<object>");
|
|
235
|
+
}
|
|
236
|
+
const requireField = (at, field, types) => {
|
|
237
|
+
if (!fields || !object)
|
|
238
|
+
return;
|
|
239
|
+
const type = Object.hasOwn(fields, field) ? fields[field] : undefined;
|
|
240
|
+
if (type === undefined) {
|
|
241
|
+
contract(at, `"${field}" is not a field of ${object} (fields: ${list(Object.keys(fields))})`);
|
|
242
|
+
}
|
|
243
|
+
else if (types && !types.includes(type)) {
|
|
244
|
+
contract(at, `${object}.${field} is a ${type} field; it must be ${types.join(" or ")} ` +
|
|
245
|
+
`(${types.join("/")} fields: ${list(ofType(types))})`);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
switch (trigger.type) {
|
|
249
|
+
case "record":
|
|
250
|
+
if (new Set(trigger.on).size !== trigger.on.length) {
|
|
251
|
+
contract(`${path}.trigger.on`, "List each record event once");
|
|
252
|
+
}
|
|
253
|
+
break;
|
|
254
|
+
case "fieldChanged": {
|
|
255
|
+
requireField(`${path}.trigger.field`, trigger.field);
|
|
256
|
+
if (isStageType(trigger.from) || isStageType(trigger.to)) {
|
|
257
|
+
const stages = own(trigger.object)
|
|
258
|
+
? m.objects[trigger.object.slice(m.id.length + 1)]?.stages
|
|
259
|
+
: undefined;
|
|
260
|
+
if (own(trigger.object) && stages?.field !== trigger.field) {
|
|
261
|
+
contract(`${path}.trigger`, stages
|
|
262
|
+
? `{ stageType } compares the stage's type, so the field must be "${stages.field}" ` +
|
|
263
|
+
`(the stage field of ${trigger.object})`
|
|
264
|
+
: `${trigger.object} has no stage behaviour (objects.<name>.stages); compare ` +
|
|
265
|
+
"values instead of { stageType }");
|
|
266
|
+
}
|
|
267
|
+
else if (!own(trigger.object) && CORE_AUTOMATION_FIELDS[trigger.object]) {
|
|
268
|
+
contract(`${path}.trigger`, `${trigger.object} has no stages; compare values instead`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (trigger.from !== undefined &&
|
|
272
|
+
trigger.to !== undefined &&
|
|
273
|
+
JSON.stringify(trigger.from) === JSON.stringify(trigger.to)) {
|
|
274
|
+
contract(`${path}.trigger`, "from and to are the same, so the trigger never fires");
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case "schedule":
|
|
279
|
+
for (const issue of cronIssues(trigger.cron))
|
|
280
|
+
contract(`${path}.trigger.cron`, issue);
|
|
281
|
+
break;
|
|
282
|
+
case "dateRelative":
|
|
283
|
+
requireField(`${path}.trigger.field`, trigger.field, ["date", "datetime"]);
|
|
284
|
+
if (trigger.zoneField !== undefined) {
|
|
285
|
+
requireField(`${path}.trigger.zoneField`, trigger.zoneField, ["timezone"]);
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
case "manual":
|
|
289
|
+
break;
|
|
290
|
+
case "approvalDecided":
|
|
291
|
+
if (trigger.operation !== undefined && !OPERATION_RE.test(trigger.operation)) {
|
|
292
|
+
contract(`${path}.trigger.operation`, `"${trigger.operation}" is not an operation name, e.g. record.update`);
|
|
293
|
+
}
|
|
294
|
+
if (trigger.object !== undefined && !ctx.known.has(trigger.object)) {
|
|
295
|
+
contract(`${path}.trigger.object`, `Unknown object "${trigger.object}"`);
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
case "webhook":
|
|
299
|
+
if (trigger.events && new Set(trigger.events).size !== trigger.events.length) {
|
|
300
|
+
contract(`${path}.trigger.events`, "List each event type once");
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
case "syncEvent":
|
|
304
|
+
if (!canRead("activity")) {
|
|
305
|
+
contract("permissions.read", `Automation ${name} runs on synced activities: add "activity" to permissions.read`);
|
|
306
|
+
}
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
// 1b. Connections it needs (ADR 0170): bound by a person, reached through the gatekeeper module.
|
|
310
|
+
const slots = new Set();
|
|
311
|
+
(automation.connections ?? []).forEach((connection, i) => {
|
|
312
|
+
const at = `${path}.connections.${i}`;
|
|
313
|
+
if (!NAME_RE.test(connection.id)) {
|
|
314
|
+
contract(`${at}.id`, `Connection id "${connection.id}" must be camelCase, e.g. mailbox`);
|
|
315
|
+
}
|
|
316
|
+
if (slots.has(connection.id)) {
|
|
317
|
+
contract(`${at}.id`, `Connection id "${connection.id}" is declared twice`);
|
|
318
|
+
}
|
|
319
|
+
slots.add(connection.id);
|
|
320
|
+
if (!perms.modules.includes(connection.gatekeeper)) {
|
|
321
|
+
contract(`${at}.gatekeeper`, `The connection uses the ${connection.gatekeeper} gatekeeper: add "${connection.gatekeeper}" ` +
|
|
322
|
+
"to permissions.modules");
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
// 2. Conditions.
|
|
326
|
+
if (automation.conditions) {
|
|
327
|
+
if (!object) {
|
|
328
|
+
contract(`${path}.conditions`, `conditions filter the trigger record, and a ${trigger.type} trigger has none; check ` +
|
|
329
|
+
"inside run instead (and declare a condition step for the diagram)");
|
|
330
|
+
}
|
|
331
|
+
else if (fields) {
|
|
332
|
+
for (const field of new Set(filterFields(automation.conditions))) {
|
|
333
|
+
if (!Object.hasOwn(fields, field) && field !== "id") {
|
|
334
|
+
contract(`${path}.conditions`, `Condition field "${field}" is not a field of ${object} (fields: ${list(Object.keys(fields))})`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// 3. Recipients: `{ field }` / `{ managerOf }` name a user field of the trigger record.
|
|
340
|
+
let notifies = false;
|
|
341
|
+
const checkRecipient = (at, recipient) => {
|
|
342
|
+
if (typeof recipient !== "object" || "userId" in recipient)
|
|
343
|
+
return;
|
|
344
|
+
const field = "field" in recipient ? recipient.field : recipient.managerOf;
|
|
345
|
+
if ("field" in recipient && (field === "admins" || field === "actor")) {
|
|
346
|
+
contract(at, `Write this recipient as the plain string "${field}", not { field: "${field}" }`);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (!object) {
|
|
350
|
+
contract(at, `A ${trigger.type} trigger has no trigger record, so "${field}" cannot name its user; ` +
|
|
351
|
+
'use "admins" or { userId }');
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
requireField(at, field, ["user"]);
|
|
355
|
+
};
|
|
356
|
+
// 4. Steps.
|
|
357
|
+
const names = new Map();
|
|
358
|
+
let waitMs = 0;
|
|
359
|
+
automation.steps.forEach((step, i) => {
|
|
360
|
+
const at = `${path}.steps.${i}`;
|
|
361
|
+
if (!NAME_RE.test(step.name)) {
|
|
362
|
+
contract(`${at}.name`, `Step name "${step.name}" must be camelCase, e.g. createTasks`);
|
|
363
|
+
}
|
|
364
|
+
const seen = names.get(step.name);
|
|
365
|
+
if (seen !== undefined) {
|
|
366
|
+
contract(`${at}.name`, `Step name "${step.name}" is already used by step ${seen + 1}`);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
names.set(step.name, i);
|
|
370
|
+
}
|
|
371
|
+
switch (step.kind) {
|
|
372
|
+
case "sleep": {
|
|
373
|
+
const ms = safeDuration(step.duration);
|
|
374
|
+
if (ms === null || ms <= 0) {
|
|
375
|
+
contract(`${at}.duration`, 'A sleep needs a positive duration, e.g. "3 days"');
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
waitMs += ms;
|
|
379
|
+
}
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
case "waitForEvent": {
|
|
383
|
+
if (!WAIT_EVENT_RE.test(step.event)) {
|
|
384
|
+
contract(`${at}.event`, `Event type "${step.event}" may only use letters, digits, - and _ (no dots), at most 100`);
|
|
385
|
+
}
|
|
386
|
+
const ms = safeDuration(step.timeout ?? DEFAULT_WAIT_TIMEOUT);
|
|
387
|
+
if (ms === null || ms <= 0) {
|
|
388
|
+
contract(`${at}.timeout`, 'A timeout needs a positive duration, e.g. "7 days"');
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
waitMs += ms;
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "condition":
|
|
396
|
+
break;
|
|
397
|
+
default: {
|
|
398
|
+
for (const read of step.reads ?? []) {
|
|
399
|
+
if (!ctx.known.has(read)) {
|
|
400
|
+
contract(`${at}.reads`, `Unknown object "${read}"`);
|
|
401
|
+
}
|
|
402
|
+
else if (!canRead(read)) {
|
|
403
|
+
contract(`${at}.reads`, `Step reads ${read}: add "${read}" to permissions.read`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
(step.writes ?? []).forEach((write, j) => {
|
|
407
|
+
const wat = `${at}.writes.${j}`;
|
|
408
|
+
if (!ctx.known.has(write.object)) {
|
|
409
|
+
contract(`${wat}.object`, `Unknown object "${write.object}"`);
|
|
410
|
+
}
|
|
411
|
+
else if (!canWrite(write.object)) {
|
|
412
|
+
contract(`${wat}.object`, `Step writes ${write.object}: add "${write.object}" to permissions.write`);
|
|
413
|
+
}
|
|
414
|
+
if (write.titles && write.count !== undefined && write.titles.length > write.count) {
|
|
415
|
+
contract(`${wat}.titles`, `${write.titles.length} titles for count ${write.count}; give at most one title per record`);
|
|
416
|
+
}
|
|
417
|
+
if (write.assignTo)
|
|
418
|
+
checkRecipient(`${wat}.assignTo`, write.assignTo);
|
|
419
|
+
});
|
|
420
|
+
(step.notifies ?? []).forEach((notify, j) => {
|
|
421
|
+
notifies = true;
|
|
422
|
+
checkRecipient(`${at}.notifies.${j}.to`, notify.to);
|
|
423
|
+
});
|
|
424
|
+
for (const call of step.calls ?? []) {
|
|
425
|
+
if (!OPERATION_RE.test(call)) {
|
|
426
|
+
contract(`${at}.calls`, `"${call}" is not an operation name, e.g. query.sql`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
if (waitMs > MAX_TOTAL_WAIT_DAYS * DAY_MS) {
|
|
433
|
+
contract(`${path}.steps`, `The waits add up to ${Math.round(waitMs / DAY_MS)} days; one run may wait at most ` +
|
|
434
|
+
`${MAX_TOTAL_WAIT_DAYS} days in total (sleeps plus waitForEvent timeouts, default ` +
|
|
435
|
+
`${DEFAULT_WAIT_TIMEOUT})`);
|
|
436
|
+
}
|
|
437
|
+
// 5. onRejected.
|
|
438
|
+
const onRejected = automation.onRejected;
|
|
439
|
+
if (onRejected && typeof onRejected === "object") {
|
|
440
|
+
notifies = true;
|
|
441
|
+
checkRecipient(`${path}.onRejected.notify`, onRejected.notify);
|
|
442
|
+
}
|
|
443
|
+
if (notifies && !perms.operations.includes(NOTIFY_OPERATION)) {
|
|
444
|
+
contract("permissions.operations", `Automation ${name} notifies people: add "${NOTIFY_OPERATION}" to permissions.operations`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function safeDuration(duration) {
|
|
448
|
+
try {
|
|
449
|
+
return durationMs(duration);
|
|
450
|
+
}
|
|
451
|
+
catch {
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
}
|