@composable-os/cli 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/README.md +24 -2
- package/dist/blocks-DEELKGDC.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2S7375SC.js +802 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-E5JLTLRM.js +50 -0
- package/dist/chunk-G3ORKXVP.js +1178 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-KOKYDJ7Z.js +1423 -0
- package/dist/chunk-KTXGEXTY.js +358 -0
- package/dist/chunk-LTQDLCP7.js +16486 -0
- package/dist/chunk-MAXLUQKB.js +1287 -0
- package/dist/chunk-STJZJIRA.js +11378 -0
- package/dist/chunk-X6I2LWB3.js +24319 -0
- package/dist/chunk-XDDBLYKW.js +29154 -0
- package/dist/chunk-ZRWF6QZS.js +2357 -0
- package/dist/cli-BRSACHSG.js +31393 -0
- package/dist/cli-DTMGNDXN.js +121 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-ZVZ6KW5S.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-64BQJA62.js +48 -0
- package/dist/invoke-NHHOLLF6.js +26 -0
- package/dist/person-WMWRF2HV.js +58 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +112 -0
- package/dist/platform/sdk/contract.ts +766 -0
- package/dist/platform/sdk/document.ts +788 -0
- package/dist/platform/sdk/field-types.ts +106 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +33 -0
- package/dist/platform/sdk/manifest.ts +1484 -0
- package/dist/platform/sdk/media.ts +46 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +173 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +396 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +162 -0
- package/dist/platform/sdk/testing.ts +836 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-4JNFLYXV.js +847 -0
- package/dist/src-G6OF5SKW.js +31 -0
- package/dist/src-J27WXNLY.js +270 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/package.json +33 -4
|
@@ -0,0 +1,534 @@
|
|
|
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 {
|
|
6
|
+
type AutomationManifest,
|
|
7
|
+
durationMs,
|
|
8
|
+
type Recipient,
|
|
9
|
+
type ValueMatch,
|
|
10
|
+
} from "./automation.ts";
|
|
11
|
+
import type { ViewFilter } from "./contract.ts";
|
|
12
|
+
import type { FieldTypeName } from "./field-types.ts";
|
|
13
|
+
import type { ExtensionManifest } from "./manifest.ts";
|
|
14
|
+
|
|
15
|
+
/** Waits in one run (sleeps plus waitForEvent timeouts) add up to at most this. */
|
|
16
|
+
export const MAX_TOTAL_WAIT_DAYS = 365;
|
|
17
|
+
/** A waitForEvent without a timeout waits this long (ADR 0130). */
|
|
18
|
+
export const DEFAULT_WAIT_TIMEOUT: `${number} days` = "7 days";
|
|
19
|
+
/** Workflows event types: `^[a-zA-Z0-9_][a-zA-Z0-9-_]*$`, at most 100 characters. */
|
|
20
|
+
export const WAIT_EVENT_RE = /^[a-zA-Z0-9_][a-zA-Z0-9-_]{0,99}$/;
|
|
21
|
+
const NAME_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
22
|
+
const OPERATION_RE = /^[a-z][a-zA-Z0-9]*(\.[a-z][a-zA-Z0-9]*)+$/;
|
|
23
|
+
const DAY_MS = 86_400_000;
|
|
24
|
+
|
|
25
|
+
/** The operation an automation needs to notify anyone (`notifies`, `ctx.notify`, `onRejected`). */
|
|
26
|
+
export const NOTIFY_OPERATION = "notification.send";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Field types of the core objects automations can trigger on and notify from (the host checks
|
|
30
|
+
* this against its field registry, packages/core/test/sdk-core-fields.test.ts). `user` marks the
|
|
31
|
+
* fields that hold a user id (`{ field }` and `{ managerOf }` recipients).
|
|
32
|
+
*/
|
|
33
|
+
export const CORE_AUTOMATION_FIELDS: Record<string, Record<string, FieldTypeName>> = {
|
|
34
|
+
person: {
|
|
35
|
+
displayName: "text",
|
|
36
|
+
givenName: "text",
|
|
37
|
+
familyName: "text",
|
|
38
|
+
primaryEmail: "email",
|
|
39
|
+
primaryPhone: "phone",
|
|
40
|
+
jobTitle: "text",
|
|
41
|
+
organisationId: "relation",
|
|
42
|
+
location: "text",
|
|
43
|
+
timezone: "text",
|
|
44
|
+
ownerId: "user",
|
|
45
|
+
createdBy: "user",
|
|
46
|
+
createdAt: "datetime",
|
|
47
|
+
updatedAt: "datetime",
|
|
48
|
+
},
|
|
49
|
+
organisation: {
|
|
50
|
+
name: "text",
|
|
51
|
+
primaryDomain: "text",
|
|
52
|
+
website: "url",
|
|
53
|
+
industry: "text",
|
|
54
|
+
parentId: "relation",
|
|
55
|
+
ownerId: "user",
|
|
56
|
+
createdBy: "user",
|
|
57
|
+
createdAt: "datetime",
|
|
58
|
+
updatedAt: "datetime",
|
|
59
|
+
},
|
|
60
|
+
task: {
|
|
61
|
+
title: "text",
|
|
62
|
+
notes: "longText",
|
|
63
|
+
dueAt: "datetime",
|
|
64
|
+
status: "select",
|
|
65
|
+
completedAt: "datetime",
|
|
66
|
+
assigneeId: "user",
|
|
67
|
+
createdBy: "user",
|
|
68
|
+
createdAt: "datetime",
|
|
69
|
+
updatedAt: "datetime",
|
|
70
|
+
},
|
|
71
|
+
activity: {
|
|
72
|
+
type: "select",
|
|
73
|
+
subject: "text",
|
|
74
|
+
body: "longText",
|
|
75
|
+
occurredAt: "datetime",
|
|
76
|
+
authorUserId: "user",
|
|
77
|
+
createdAt: "datetime",
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Base records with operations of their own (stage 25's orders) that an automation may be
|
|
83
|
+
* started by (stage 27): a ticket sent when an order is first paid. They are not core objects:
|
|
84
|
+
* nothing else in a manifest may name them, and the automation reads one only through its
|
|
85
|
+
* `<object>.get`, which it must list in permissions.operations.
|
|
86
|
+
*/
|
|
87
|
+
export const BASE_AUTOMATION_FIELDS: Record<string, Record<string, FieldTypeName>> = {
|
|
88
|
+
order: {
|
|
89
|
+
state: "select",
|
|
90
|
+
email: "email",
|
|
91
|
+
personId: "relation",
|
|
92
|
+
currency: "text",
|
|
93
|
+
total: "number",
|
|
94
|
+
paid: "number",
|
|
95
|
+
balance: "number",
|
|
96
|
+
pay: "select",
|
|
97
|
+
deposit: "number",
|
|
98
|
+
balanceDueAt: "datetime",
|
|
99
|
+
paidAt: "datetime",
|
|
100
|
+
createdAt: "datetime",
|
|
101
|
+
updatedAt: "datetime",
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Fields every extension record has besides its declared ones. */
|
|
106
|
+
const RECORD_FIELDS: Record<string, FieldTypeName> = {
|
|
107
|
+
createdAt: "datetime",
|
|
108
|
+
updatedAt: "datetime",
|
|
109
|
+
createdBy: "user",
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export interface AutomationCheckContext {
|
|
113
|
+
manifest: ExtensionManifest;
|
|
114
|
+
/** Objects that exist (core, this extension's, installed ones the host knows). */
|
|
115
|
+
known: ReadonlySet<string>;
|
|
116
|
+
/** Field types of an object outside this extension, from the host; null: unknown (skip). */
|
|
117
|
+
objectFields?: (object: string) => Record<string, FieldTypeName> | null;
|
|
118
|
+
contract(path: string, message: string): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const list = (names: string[]) => (names.length ? names.join(", ") : "none");
|
|
122
|
+
|
|
123
|
+
/** Field name → type for `object`, or null when this check cannot know its fields. */
|
|
124
|
+
function fieldsOf(
|
|
125
|
+
ctx: AutomationCheckContext,
|
|
126
|
+
object: string,
|
|
127
|
+
): Record<string, FieldTypeName> | null {
|
|
128
|
+
const m = ctx.manifest;
|
|
129
|
+
// Fields this extension adds to another object, by local name and by registry name
|
|
130
|
+
// (`<ext>.<field>`, what record changes and so fieldChanged triggers use).
|
|
131
|
+
const added = Object.fromEntries(
|
|
132
|
+
Object.entries(m.fields[object] ?? {}).flatMap(([name, field]) => [
|
|
133
|
+
[name, field.type],
|
|
134
|
+
[`${m.id}.${name}`, field.type],
|
|
135
|
+
]),
|
|
136
|
+
);
|
|
137
|
+
if (object.startsWith(`${m.id}.`)) {
|
|
138
|
+
const own = m.objects[object.slice(m.id.length + 1)];
|
|
139
|
+
if (!own) return null;
|
|
140
|
+
return {
|
|
141
|
+
...RECORD_FIELDS,
|
|
142
|
+
...Object.fromEntries(Object.entries(own.fields).map(([name, f]) => [name, f.type])),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const core = CORE_AUTOMATION_FIELDS[object] ?? BASE_AUTOMATION_FIELDS[object];
|
|
146
|
+
if (core) return { ...core, ...added };
|
|
147
|
+
const host = ctx.objectFields?.(object);
|
|
148
|
+
return host ? { ...RECORD_FIELDS, ...host, ...added } : null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The object a trigger hands its run as the trigger record (null: none). */
|
|
152
|
+
export function triggerObject(trigger: AutomationManifest["trigger"]): string | null {
|
|
153
|
+
switch (trigger.type) {
|
|
154
|
+
case "record":
|
|
155
|
+
case "fieldChanged":
|
|
156
|
+
case "dateRelative":
|
|
157
|
+
case "manual":
|
|
158
|
+
return trigger.object;
|
|
159
|
+
// The synced email or meeting (an activity) is the trigger record.
|
|
160
|
+
case "syncEvent":
|
|
161
|
+
return "activity";
|
|
162
|
+
default:
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const CRON_FIELDS = [
|
|
168
|
+
{ name: "minute", min: 0, max: 59 },
|
|
169
|
+
{ name: "hour", min: 0, max: 23 },
|
|
170
|
+
{ name: "day of month", min: 1, max: 31 },
|
|
171
|
+
{
|
|
172
|
+
name: "month",
|
|
173
|
+
min: 1,
|
|
174
|
+
max: 12,
|
|
175
|
+
names: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
|
|
176
|
+
},
|
|
177
|
+
{ name: "day of week", min: 0, max: 7, names: ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] },
|
|
178
|
+
] as const;
|
|
179
|
+
|
|
180
|
+
/** Problems with a 5-field cron expression (empty when valid). */
|
|
181
|
+
export function cronIssues(cron: string): string[] {
|
|
182
|
+
const parts = cron.trim().split(/\s+/);
|
|
183
|
+
if (parts.length !== 5) {
|
|
184
|
+
return [
|
|
185
|
+
`"${cron}" has ${parts.length} fields; a schedule is 5 (minute hour day-of-month month ` +
|
|
186
|
+
'day-of-week), e.g. "0 8 * * 1-5" for 08:00 on weekdays',
|
|
187
|
+
];
|
|
188
|
+
}
|
|
189
|
+
const issues: string[] = [];
|
|
190
|
+
parts.forEach((part, i) => {
|
|
191
|
+
const spec = CRON_FIELDS[i] as (typeof CRON_FIELDS)[number];
|
|
192
|
+
const names: readonly string[] = "names" in spec ? spec.names : [];
|
|
193
|
+
const value = (text: string): number | null => {
|
|
194
|
+
const upper = text.toUpperCase();
|
|
195
|
+
const named = names.indexOf(upper);
|
|
196
|
+
if (named >= 0) return named + (spec.name === "month" ? 1 : 0);
|
|
197
|
+
if (!/^\d+$/.test(text)) return null;
|
|
198
|
+
const n = Number(text);
|
|
199
|
+
return n >= spec.min && n <= spec.max ? n : null;
|
|
200
|
+
};
|
|
201
|
+
for (const item of part.split(",")) {
|
|
202
|
+
const [range, step, extra] = item.split("/");
|
|
203
|
+
const bad = () =>
|
|
204
|
+
issues.push(
|
|
205
|
+
`Cron ${spec.name} "${part}" is not valid: use *, a number ${spec.min}-${spec.max}` +
|
|
206
|
+
`${names.length ? ` or a name (${names[0]}…)` : ""}, a range a-b, a list a,b or a step */n`,
|
|
207
|
+
);
|
|
208
|
+
if (extra !== undefined || range === undefined || range === "") {
|
|
209
|
+
bad();
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (step !== undefined && !/^[1-9]\d*$/.test(step)) {
|
|
213
|
+
bad();
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (range === "*") continue;
|
|
217
|
+
const [from, to, more] = range.split("-");
|
|
218
|
+
if (more !== undefined) {
|
|
219
|
+
bad();
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const a = value(from ?? "");
|
|
223
|
+
const b = to === undefined ? a : value(to);
|
|
224
|
+
if (a === null || b === null || b < a) bad();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return issues;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Field names a filter reads. */
|
|
231
|
+
function filterFields(filter: ViewFilter): string[] {
|
|
232
|
+
if ("and" in filter) return filter.and.flatMap(filterFields);
|
|
233
|
+
if ("or" in filter) return filter.or.flatMap(filterFields);
|
|
234
|
+
return [filter.field];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const isStageType = (match: ValueMatch | undefined): boolean =>
|
|
238
|
+
typeof match === "object" && match !== null && "stageType" in match;
|
|
239
|
+
|
|
240
|
+
/** Reports problems with `automations.<name>` through `ctx.contract`. */
|
|
241
|
+
export function checkAutomation(
|
|
242
|
+
ctx: AutomationCheckContext,
|
|
243
|
+
name: string,
|
|
244
|
+
automation: AutomationManifest,
|
|
245
|
+
): void {
|
|
246
|
+
const m = ctx.manifest;
|
|
247
|
+
const path = `automations.${name}`;
|
|
248
|
+
const { contract } = ctx;
|
|
249
|
+
const perms = m.permissions;
|
|
250
|
+
const own = (object: string) => object.startsWith(`${m.id}.`);
|
|
251
|
+
const canWrite = (object: string) => own(object) || perms.write.includes(object);
|
|
252
|
+
const canRead = (object: string) => canWrite(object) || perms.read.includes(object);
|
|
253
|
+
const trigger = automation.trigger;
|
|
254
|
+
const object = triggerObject(trigger);
|
|
255
|
+
const base = object !== null && Object.hasOwn(BASE_AUTOMATION_FIELDS, object);
|
|
256
|
+
const fields = object && (ctx.known.has(object) || base) ? fieldsOf(ctx, object) : null;
|
|
257
|
+
const ofType = (types: readonly FieldTypeName[]) =>
|
|
258
|
+
Object.entries(fields ?? {})
|
|
259
|
+
.filter(([, type]) => types.includes(type))
|
|
260
|
+
.map(([field]) => field);
|
|
261
|
+
|
|
262
|
+
if (!NAME_RE.test(name)) {
|
|
263
|
+
contract(path, `Automation name "${name}" must be camelCase letters and digits, e.g. wonDeal`);
|
|
264
|
+
}
|
|
265
|
+
// 1. The trigger.
|
|
266
|
+
if (base && !perms.operations.includes(`${object}.get`)) {
|
|
267
|
+
contract(
|
|
268
|
+
`${path}.trigger.object`,
|
|
269
|
+
`An automation started by ${object} reads it: add "${object}.get" to permissions.operations`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
if (object !== null && !ctx.known.has(object) && !base) {
|
|
273
|
+
contract(
|
|
274
|
+
`${path}.trigger.object`,
|
|
275
|
+
`Unknown object "${object}". Use a core object (person, organisation, task, activity) or ` +
|
|
276
|
+
"<extension>.<object>",
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
const requireField = (at: string, field: string, types?: readonly FieldTypeName[]) => {
|
|
280
|
+
if (!fields || !object) return;
|
|
281
|
+
const type = Object.hasOwn(fields, field) ? fields[field] : undefined;
|
|
282
|
+
if (type === undefined) {
|
|
283
|
+
contract(at, `"${field}" is not a field of ${object} (fields: ${list(Object.keys(fields))})`);
|
|
284
|
+
} else if (types && !types.includes(type)) {
|
|
285
|
+
contract(
|
|
286
|
+
at,
|
|
287
|
+
`${object}.${field} is a ${type} field; it must be ${types.join(" or ")} ` +
|
|
288
|
+
`(${types.join("/")} fields: ${list(ofType(types))})`,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
switch (trigger.type) {
|
|
293
|
+
case "record":
|
|
294
|
+
if (new Set(trigger.on).size !== trigger.on.length) {
|
|
295
|
+
contract(`${path}.trigger.on`, "List each record event once");
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
case "fieldChanged": {
|
|
299
|
+
requireField(`${path}.trigger.field`, trigger.field);
|
|
300
|
+
if (isStageType(trigger.from) || isStageType(trigger.to)) {
|
|
301
|
+
const stages = own(trigger.object)
|
|
302
|
+
? m.objects[trigger.object.slice(m.id.length + 1)]?.stages
|
|
303
|
+
: undefined;
|
|
304
|
+
if (own(trigger.object) && stages?.field !== trigger.field) {
|
|
305
|
+
contract(
|
|
306
|
+
`${path}.trigger`,
|
|
307
|
+
stages
|
|
308
|
+
? `{ stageType } compares the stage's type, so the field must be "${stages.field}" ` +
|
|
309
|
+
`(the stage field of ${trigger.object})`
|
|
310
|
+
: `${trigger.object} has no stage behaviour (objects.<name>.stages); compare ` +
|
|
311
|
+
"values instead of { stageType }",
|
|
312
|
+
);
|
|
313
|
+
} else if (!own(trigger.object) && CORE_AUTOMATION_FIELDS[trigger.object]) {
|
|
314
|
+
contract(`${path}.trigger`, `${trigger.object} has no stages; compare values instead`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (
|
|
318
|
+
trigger.from !== undefined &&
|
|
319
|
+
trigger.to !== undefined &&
|
|
320
|
+
JSON.stringify(trigger.from) === JSON.stringify(trigger.to)
|
|
321
|
+
) {
|
|
322
|
+
contract(`${path}.trigger`, "from and to are the same, so the trigger never fires");
|
|
323
|
+
}
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
case "schedule":
|
|
327
|
+
for (const issue of cronIssues(trigger.cron)) contract(`${path}.trigger.cron`, issue);
|
|
328
|
+
break;
|
|
329
|
+
case "dateRelative":
|
|
330
|
+
requireField(`${path}.trigger.field`, trigger.field, ["date", "datetime"]);
|
|
331
|
+
if (trigger.zoneField !== undefined) {
|
|
332
|
+
requireField(`${path}.trigger.zoneField`, trigger.zoneField, ["timezone"]);
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
case "manual":
|
|
336
|
+
break;
|
|
337
|
+
case "approvalDecided":
|
|
338
|
+
if (trigger.operation !== undefined && !OPERATION_RE.test(trigger.operation)) {
|
|
339
|
+
contract(
|
|
340
|
+
`${path}.trigger.operation`,
|
|
341
|
+
`"${trigger.operation}" is not an operation name, e.g. record.update`,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
if (trigger.object !== undefined && !ctx.known.has(trigger.object)) {
|
|
345
|
+
contract(`${path}.trigger.object`, `Unknown object "${trigger.object}"`);
|
|
346
|
+
}
|
|
347
|
+
break;
|
|
348
|
+
case "webhook":
|
|
349
|
+
if (trigger.events && new Set(trigger.events).size !== trigger.events.length) {
|
|
350
|
+
contract(`${path}.trigger.events`, "List each event type once");
|
|
351
|
+
}
|
|
352
|
+
break;
|
|
353
|
+
case "syncEvent":
|
|
354
|
+
if (!canRead("activity")) {
|
|
355
|
+
contract(
|
|
356
|
+
"permissions.read",
|
|
357
|
+
`Automation ${name} runs on synced activities: add "activity" to permissions.read`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 1b. Connections it needs (ADR 0170): bound by a person, reached through the gatekeeper module.
|
|
364
|
+
const slots = new Set<string>();
|
|
365
|
+
(automation.connections ?? []).forEach((connection, i) => {
|
|
366
|
+
const at = `${path}.connections.${i}`;
|
|
367
|
+
if (!NAME_RE.test(connection.id)) {
|
|
368
|
+
contract(`${at}.id`, `Connection id "${connection.id}" must be camelCase, e.g. mailbox`);
|
|
369
|
+
}
|
|
370
|
+
if (slots.has(connection.id)) {
|
|
371
|
+
contract(`${at}.id`, `Connection id "${connection.id}" is declared twice`);
|
|
372
|
+
}
|
|
373
|
+
slots.add(connection.id);
|
|
374
|
+
if (!perms.modules.includes(connection.gatekeeper)) {
|
|
375
|
+
contract(
|
|
376
|
+
`${at}.gatekeeper`,
|
|
377
|
+
`The connection uses the ${connection.gatekeeper} gatekeeper: add "${connection.gatekeeper}" ` +
|
|
378
|
+
"to permissions.modules",
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
// 2. Conditions.
|
|
384
|
+
if (automation.conditions) {
|
|
385
|
+
if (!object) {
|
|
386
|
+
contract(
|
|
387
|
+
`${path}.conditions`,
|
|
388
|
+
`conditions filter the trigger record, and a ${trigger.type} trigger has none; check ` +
|
|
389
|
+
"inside run instead (and declare a condition step for the diagram)",
|
|
390
|
+
);
|
|
391
|
+
} else if (fields) {
|
|
392
|
+
for (const field of new Set(filterFields(automation.conditions))) {
|
|
393
|
+
if (!Object.hasOwn(fields, field) && field !== "id") {
|
|
394
|
+
contract(
|
|
395
|
+
`${path}.conditions`,
|
|
396
|
+
`Condition field "${field}" is not a field of ${object} (fields: ${list(Object.keys(fields))})`,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 3. Recipients: `{ field }` / `{ managerOf }` name a user field of the trigger record.
|
|
404
|
+
let notifies = false;
|
|
405
|
+
const checkRecipient = (at: string, recipient: Recipient) => {
|
|
406
|
+
if (typeof recipient !== "object" || "userId" in recipient) return;
|
|
407
|
+
const field = "field" in recipient ? recipient.field : recipient.managerOf;
|
|
408
|
+
if ("field" in recipient && (field === "admins" || field === "actor")) {
|
|
409
|
+
contract(
|
|
410
|
+
at,
|
|
411
|
+
`Write this recipient as the plain string "${field}", not { field: "${field}" }`,
|
|
412
|
+
);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (!object) {
|
|
416
|
+
contract(
|
|
417
|
+
at,
|
|
418
|
+
`A ${trigger.type} trigger has no trigger record, so "${field}" cannot name its user; ` +
|
|
419
|
+
'use "admins" or { userId }',
|
|
420
|
+
);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
requireField(at, field, ["user"]);
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
// 4. Steps.
|
|
427
|
+
const names = new Map<string, number>();
|
|
428
|
+
let waitMs = 0;
|
|
429
|
+
automation.steps.forEach((step, i) => {
|
|
430
|
+
const at = `${path}.steps.${i}`;
|
|
431
|
+
if (!NAME_RE.test(step.name)) {
|
|
432
|
+
contract(`${at}.name`, `Step name "${step.name}" must be camelCase, e.g. createTasks`);
|
|
433
|
+
}
|
|
434
|
+
const seen = names.get(step.name);
|
|
435
|
+
if (seen !== undefined) {
|
|
436
|
+
contract(`${at}.name`, `Step name "${step.name}" is already used by step ${seen + 1}`);
|
|
437
|
+
} else {
|
|
438
|
+
names.set(step.name, i);
|
|
439
|
+
}
|
|
440
|
+
switch (step.kind) {
|
|
441
|
+
case "sleep": {
|
|
442
|
+
const ms = safeDuration(step.duration);
|
|
443
|
+
if (ms === null || ms <= 0) {
|
|
444
|
+
contract(`${at}.duration`, 'A sleep needs a positive duration, e.g. "3 days"');
|
|
445
|
+
} else {
|
|
446
|
+
waitMs += ms;
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
case "waitForEvent": {
|
|
451
|
+
if (!WAIT_EVENT_RE.test(step.event)) {
|
|
452
|
+
contract(
|
|
453
|
+
`${at}.event`,
|
|
454
|
+
`Event type "${step.event}" may only use letters, digits, - and _ (no dots), at most 100`,
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
const ms = safeDuration(step.timeout ?? DEFAULT_WAIT_TIMEOUT);
|
|
458
|
+
if (ms === null || ms <= 0) {
|
|
459
|
+
contract(`${at}.timeout`, 'A timeout needs a positive duration, e.g. "7 days"');
|
|
460
|
+
} else {
|
|
461
|
+
waitMs += ms;
|
|
462
|
+
}
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
case "condition":
|
|
466
|
+
break;
|
|
467
|
+
default: {
|
|
468
|
+
for (const read of step.reads ?? []) {
|
|
469
|
+
if (!ctx.known.has(read)) {
|
|
470
|
+
contract(`${at}.reads`, `Unknown object "${read}"`);
|
|
471
|
+
} else if (!canRead(read)) {
|
|
472
|
+
contract(`${at}.reads`, `Step reads ${read}: add "${read}" to permissions.read`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
(step.writes ?? []).forEach((write, j) => {
|
|
476
|
+
const wat = `${at}.writes.${j}`;
|
|
477
|
+
if (!ctx.known.has(write.object)) {
|
|
478
|
+
contract(`${wat}.object`, `Unknown object "${write.object}"`);
|
|
479
|
+
} else if (!canWrite(write.object)) {
|
|
480
|
+
contract(
|
|
481
|
+
`${wat}.object`,
|
|
482
|
+
`Step writes ${write.object}: add "${write.object}" to permissions.write`,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
if (write.titles && write.count !== undefined && write.titles.length > write.count) {
|
|
486
|
+
contract(
|
|
487
|
+
`${wat}.titles`,
|
|
488
|
+
`${write.titles.length} titles for count ${write.count}; give at most one title per record`,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
if (write.assignTo) checkRecipient(`${wat}.assignTo`, write.assignTo);
|
|
492
|
+
});
|
|
493
|
+
(step.notifies ?? []).forEach((notify, j) => {
|
|
494
|
+
notifies = true;
|
|
495
|
+
checkRecipient(`${at}.notifies.${j}.to`, notify.to);
|
|
496
|
+
});
|
|
497
|
+
for (const call of step.calls ?? []) {
|
|
498
|
+
if (!OPERATION_RE.test(call)) {
|
|
499
|
+
contract(`${at}.calls`, `"${call}" is not an operation name, e.g. query.sql`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
if (waitMs > MAX_TOTAL_WAIT_DAYS * DAY_MS) {
|
|
506
|
+
contract(
|
|
507
|
+
`${path}.steps`,
|
|
508
|
+
`The waits add up to ${Math.round(waitMs / DAY_MS)} days; one run may wait at most ` +
|
|
509
|
+
`${MAX_TOTAL_WAIT_DAYS} days in total (sleeps plus waitForEvent timeouts, default ` +
|
|
510
|
+
`${DEFAULT_WAIT_TIMEOUT})`,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// 5. onRejected.
|
|
515
|
+
const onRejected = automation.onRejected;
|
|
516
|
+
if (onRejected && typeof onRejected === "object") {
|
|
517
|
+
notifies = true;
|
|
518
|
+
checkRecipient(`${path}.onRejected.notify`, onRejected.notify);
|
|
519
|
+
}
|
|
520
|
+
if (notifies && !perms.operations.includes(NOTIFY_OPERATION)) {
|
|
521
|
+
contract(
|
|
522
|
+
"permissions.operations",
|
|
523
|
+
`Automation ${name} notifies people: add "${NOTIFY_OPERATION}" to permissions.operations`,
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function safeDuration(duration: number | string): number | null {
|
|
529
|
+
try {
|
|
530
|
+
return durationMs(duration as never);
|
|
531
|
+
} catch {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
}
|