@composable-os/cli 0.0.0-stage → 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/accounts-X4CKM3HL.js +46 -0
- package/dist/blocks-YWDHEBOK.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6GDEDOV7.js +157 -0
- package/dist/chunk-6UPUF4ML.js +29998 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-6VXHPKGG.js +1302 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/chunk-A4NTSYWV.js +11092 -0
- package/dist/chunk-CP6BXI6F.js +25825 -0
- package/dist/chunk-DU6OIFOU.js +2358 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/chunk-GYSGVT6R.js +284 -0
- package/dist/chunk-GYTEW66E.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-IFG4FRW5.js +504 -0
- package/dist/chunk-JJHHNV46.js +227 -0
- package/dist/chunk-K6T42YCH.js +704 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-LJFLENFC.js +88 -0
- package/dist/chunk-LLULPZAJ.js +365 -0
- package/dist/chunk-M7A6FR2Z.js +2586 -0
- package/dist/chunk-O2UIZHND.js +249 -0
- package/dist/chunk-Q4N55M2Z.js +552 -0
- package/dist/chunk-QGGJ2G6Z.js +349 -0
- package/dist/chunk-R4W5XAUD.js +837 -0
- package/dist/chunk-TQFGGSA4.js +748 -0
- package/dist/chunk-VY64DMQO.js +15302 -0
- package/dist/chunk-WC5LHBBA.js +426 -0
- package/dist/chunk-WO6D3XP3.js +9351 -0
- package/dist/chunk-Y7JZFSTQ.js +1425 -0
- package/dist/chunk-YJJHF65I.js +759 -0
- package/dist/chunk-Z3RMMB6S.js +668 -0
- package/dist/cli-2DFENR62.js +134 -0
- package/dist/cli-YIOVSKOD.js +24531 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-PRU3VCQQ.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-2NC7LSYE.js +50 -0
- package/dist/grants-QXMKYMGB.js +32 -0
- package/dist/invoke-I3NNNXVL.js +39 -0
- package/dist/order-PUWD65BC.js +19 -0
- package/dist/person-WIQQ7ITU.js +68 -0
- package/dist/plan-on-sale-NB774CQL.js +26 -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/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
- package/dist/platform/migrations/0581_once_per_person.sql +34 -0
- package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
- package/dist/platform/sdk/allowance.ts +522 -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 +201 -0
- package/dist/platform/sdk/contract.ts +884 -0
- package/dist/platform/sdk/document.ts +935 -0
- package/dist/platform/sdk/field-types.ts +111 -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 +36 -0
- package/dist/platform/sdk/manifest.ts +1709 -0
- package/dist/platform/sdk/media.ts +108 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/recurring.ts +321 -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 +221 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +888 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +484 -0
- package/dist/platform/sdk/testing.ts +847 -0
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-PZUCSDDL.js +508 -0
- package/dist/run-RT2HVWMK.js +648 -0
- package/dist/run-Y7T4TVOL.js +309 -0
- package/dist/src-GYS6NBQB.js +44 -0
- package/dist/src-XTOD7EXW.js +348 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/dist/visibility-DLGHU4YN.js +33 -0
- package/package.json +33 -4
|
@@ -0,0 +1,1709 @@
|
|
|
1
|
+
// The extension manifest: the JSON form of an extension (no handlers, tool schemas as JSON
|
|
2
|
+
// Schema). The host evaluates an extension in the sandbox to get its manifest, then validates,
|
|
3
|
+
// diffs and reconciles the manifest. `validateManifest` is the contract check `extension.check`
|
|
4
|
+
// runs; it returns structured issues a model can act on.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { allowanceManifestSchema, checkObjectAllowance } from "./allowance.ts";
|
|
7
|
+
import { automationManifestSchema } from "./automation.ts";
|
|
8
|
+
import { checkAutomation } from "./automation-check.ts";
|
|
9
|
+
import { checkObjectBehaviour } from "./behaviour.ts";
|
|
10
|
+
import {
|
|
11
|
+
capacityManifestSchema,
|
|
12
|
+
checkObjectCapacity,
|
|
13
|
+
checkObjectClaims,
|
|
14
|
+
claimsManifestSchema,
|
|
15
|
+
} from "./capacity.ts";
|
|
16
|
+
import {
|
|
17
|
+
type Extension,
|
|
18
|
+
HOST_MODULES,
|
|
19
|
+
type HostModuleName,
|
|
20
|
+
STAGE_TYPES,
|
|
21
|
+
SUPPORTED_CONTRACT_VERSIONS,
|
|
22
|
+
TOOL_AUDIENCES,
|
|
23
|
+
TOOL_RISKS,
|
|
24
|
+
} from "./contract.ts";
|
|
25
|
+
import {
|
|
26
|
+
checkDocumentSchema,
|
|
27
|
+
DOCUMENT_MAX_BYTES,
|
|
28
|
+
DOCUMENT_MAX_PATHS,
|
|
29
|
+
type DocumentSchema,
|
|
30
|
+
documentPathNode,
|
|
31
|
+
} from "./document.ts";
|
|
32
|
+
import { FIELD_TYPES, type FieldTypeName, formulaTypeOf } from "./field-types.ts";
|
|
33
|
+
import { checkFormula, type FormulaFieldRef } from "./formula/compile.ts";
|
|
34
|
+
import { checkObjectRecurring, recurringManifestSchema } from "./recurring.ts";
|
|
35
|
+
import { renamedMessage } from "./renamed.ts";
|
|
36
|
+
import { checkObjectSellable, sellableManifestSchema } from "./sellable.ts";
|
|
37
|
+
import { checkObjectStates, statesManifestSchema } from "./states.ts";
|
|
38
|
+
import { checkObjectUnique, uniqueManifestSchema } from "./unique.ts";
|
|
39
|
+
|
|
40
|
+
export const EXTENSION_ID_RE = /^[a-z][a-z0-9]{1,29}$/;
|
|
41
|
+
export const OBJECT_NAME_RE = /^[a-z][a-z0-9]{0,29}$/;
|
|
42
|
+
export const FIELD_NAME_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
43
|
+
export const TOOL_NAME_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
44
|
+
const SEMVER_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
45
|
+
const OPERATION_RE = /^[a-z][a-zA-Z0-9]*(\.[a-z][a-zA-Z0-9]*)+$/;
|
|
46
|
+
|
|
47
|
+
/** Core objects extension fields can target, and which extension objects may not be named. */
|
|
48
|
+
export const CORE_OBJECT_NAMES = ["person", "organisation", "activity", "task"] as const;
|
|
49
|
+
const RESERVED_OBJECT_NAMES = new Set([
|
|
50
|
+
...CORE_OBJECT_NAMES,
|
|
51
|
+
"affiliation",
|
|
52
|
+
"user",
|
|
53
|
+
"users",
|
|
54
|
+
"tag",
|
|
55
|
+
"tags",
|
|
56
|
+
"view",
|
|
57
|
+
"audit",
|
|
58
|
+
"record",
|
|
59
|
+
]);
|
|
60
|
+
const RESERVED_EXTENSION_IDS = new Set([
|
|
61
|
+
"core",
|
|
62
|
+
"ops",
|
|
63
|
+
"rec",
|
|
64
|
+
"x",
|
|
65
|
+
"sdk",
|
|
66
|
+
"system",
|
|
67
|
+
"record",
|
|
68
|
+
"extension",
|
|
69
|
+
]);
|
|
70
|
+
/** Field names every record already has. */
|
|
71
|
+
export const RESERVED_FIELD_NAMES = new Set([
|
|
72
|
+
"id",
|
|
73
|
+
"createdAt",
|
|
74
|
+
"updatedAt",
|
|
75
|
+
"deletedAt",
|
|
76
|
+
"createdBy",
|
|
77
|
+
"deletedBy",
|
|
78
|
+
"tags",
|
|
79
|
+
]);
|
|
80
|
+
/** Operation prefixes no extension may be granted. */
|
|
81
|
+
export const FORBIDDEN_OPERATION_PREFIXES = ["extension.", "policy.", "user.", "audit."] as const;
|
|
82
|
+
const TEXT_LIKE: FieldTypeName[] = ["text", "email", "phone", "url", "select"];
|
|
83
|
+
/** Objects relations may point at. */
|
|
84
|
+
export const RELATION_CORE_TARGETS = ["person", "organisation", "task"] as const;
|
|
85
|
+
|
|
86
|
+
const optionSchema = z.object({
|
|
87
|
+
value: z.string().min(1).max(100),
|
|
88
|
+
label: z.string().min(1).max(100),
|
|
89
|
+
color: z.string().max(20).optional(),
|
|
90
|
+
requires: z.array(z.string().max(80)).max(20).optional(),
|
|
91
|
+
fixed: z.boolean().optional(),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
export const fieldManifestSchema = z.object({
|
|
95
|
+
type: z.enum(FIELD_TYPES),
|
|
96
|
+
label: z.string().min(1).max(100),
|
|
97
|
+
description: z.string().max(1000).optional(),
|
|
98
|
+
track: z.literal("timeline").optional(),
|
|
99
|
+
renamedFrom: z.string().max(40).optional(),
|
|
100
|
+
required: z.boolean().optional(),
|
|
101
|
+
readOnly: z.boolean().optional(),
|
|
102
|
+
currency: z
|
|
103
|
+
.string()
|
|
104
|
+
.regex(/^[A-Z]{3}$/)
|
|
105
|
+
.optional(),
|
|
106
|
+
options: z.array(optionSchema).max(200).optional(),
|
|
107
|
+
editableOptions: z.boolean().optional(),
|
|
108
|
+
default: z.union([z.string().max(1000), z.number(), z.boolean()]).optional(),
|
|
109
|
+
defaultSetting: z.string().max(40).optional(),
|
|
110
|
+
personApproves: z.boolean().optional(),
|
|
111
|
+
to: z.string().max(80).optional(),
|
|
112
|
+
many: z.boolean().optional(),
|
|
113
|
+
expression: z.string().max(2000).optional(),
|
|
114
|
+
// Number fields' bounds (stage 41).
|
|
115
|
+
min: z.number().finite().optional(),
|
|
116
|
+
max: z.number().finite().optional(),
|
|
117
|
+
step: z.number().finite().positive().optional(),
|
|
118
|
+
// Document fields (ADR 0445): the schema is checked by checkDocumentSchema, with its paths.
|
|
119
|
+
schema: z.record(z.string(), z.unknown()).optional(),
|
|
120
|
+
maxBytes: z.number().int().optional(),
|
|
121
|
+
paths: z
|
|
122
|
+
.record(
|
|
123
|
+
z.string().max(40),
|
|
124
|
+
z
|
|
125
|
+
.object({
|
|
126
|
+
label: z.string().min(1).max(100),
|
|
127
|
+
path: z.string().min(1).max(200),
|
|
128
|
+
count: z.boolean().optional(),
|
|
129
|
+
description: z.string().max(1000).optional(),
|
|
130
|
+
})
|
|
131
|
+
.strict(),
|
|
132
|
+
)
|
|
133
|
+
.optional(),
|
|
134
|
+
});
|
|
135
|
+
export type FieldManifest = z.infer<typeof fieldManifestSchema>;
|
|
136
|
+
|
|
137
|
+
/** Whether `value` is a whole number of `step`s from `from` (with room for binary fractions). */
|
|
138
|
+
export function onStep(value: number, from: number, step: number): boolean {
|
|
139
|
+
const steps = (value - from) / step;
|
|
140
|
+
return Math.abs(steps - Math.round(steps)) < 1e-9;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Field names a publishable object's records get from the host (ADR 0446). */
|
|
144
|
+
export const PUBLISHED_FIELD_NAMES = [
|
|
145
|
+
"publishedVersion",
|
|
146
|
+
"publishedAt",
|
|
147
|
+
// Stage 28 (ADR 0441): where the record is (draft, scheduled, published, unpublished) and
|
|
148
|
+
// when a scheduled publish goes live.
|
|
149
|
+
"publishState",
|
|
150
|
+
"publishAt",
|
|
151
|
+
] as const;
|
|
152
|
+
|
|
153
|
+
const readDeclarationSchema = z
|
|
154
|
+
.object({
|
|
155
|
+
read: z.array(z.string().max(40)).min(1).max(100),
|
|
156
|
+
when: z
|
|
157
|
+
.object({
|
|
158
|
+
via: z.string().max(40).optional(),
|
|
159
|
+
field: z.string().max(40),
|
|
160
|
+
in: z.array(z.string().max(100)).min(1).max(20),
|
|
161
|
+
})
|
|
162
|
+
.strict()
|
|
163
|
+
.optional(),
|
|
164
|
+
})
|
|
165
|
+
.strict();
|
|
166
|
+
|
|
167
|
+
export const stagesManifestSchema = z.object({
|
|
168
|
+
field: z.string().max(40),
|
|
169
|
+
stage: z.object({
|
|
170
|
+
type: z.string().max(40),
|
|
171
|
+
order: z.string().max(40),
|
|
172
|
+
requiredFields: z.string().max(40).optional(),
|
|
173
|
+
probability: z.string().max(40).optional(),
|
|
174
|
+
group: z.string().max(40).optional(),
|
|
175
|
+
}),
|
|
176
|
+
group: z.string().max(40).optional(),
|
|
177
|
+
probability: z.object({ field: z.string().max(40), overridden: z.string().max(40) }).optional(),
|
|
178
|
+
closedDate: z.string().max(40).optional(),
|
|
179
|
+
status: z.string().max(40).optional(),
|
|
180
|
+
defaultGroupSetting: z.string().max(40).optional(),
|
|
181
|
+
requiredByType: z
|
|
182
|
+
.partialRecord(z.enum(STAGE_TYPES), z.array(z.string().max(40)).max(50))
|
|
183
|
+
.optional(),
|
|
184
|
+
});
|
|
185
|
+
export type StagesManifest = z.infer<typeof stagesManifestSchema>;
|
|
186
|
+
|
|
187
|
+
export const objectManifestSchema = z.object({
|
|
188
|
+
label: z.string().min(1).max(100),
|
|
189
|
+
plural: z.string().min(1).max(100),
|
|
190
|
+
icon: z.string().max(60).optional(),
|
|
191
|
+
titleField: z.string().max(40),
|
|
192
|
+
description: z.string().max(1000).optional(),
|
|
193
|
+
renamedFrom: z.string().max(40).optional(),
|
|
194
|
+
fields: z.record(z.string(), fieldManifestSchema),
|
|
195
|
+
navigation: z.enum(["main", "settings", "none"]).optional(),
|
|
196
|
+
indexes: z
|
|
197
|
+
.array(z.array(z.string().max(40)).min(1).max(4))
|
|
198
|
+
.max(16)
|
|
199
|
+
.optional(),
|
|
200
|
+
timelineLinks: z.array(z.string().max(40)).max(8).optional(),
|
|
201
|
+
stages: stagesManifestSchema.optional(),
|
|
202
|
+
capacity: capacityManifestSchema.optional(),
|
|
203
|
+
sellable: sellableManifestSchema.optional(),
|
|
204
|
+
allowance: allowanceManifestSchema.optional(),
|
|
205
|
+
states: statesManifestSchema.optional(),
|
|
206
|
+
claims: claimsManifestSchema.optional(),
|
|
207
|
+
unique: uniqueManifestSchema.optional(),
|
|
208
|
+
recurring: recurringManifestSchema.optional(),
|
|
209
|
+
owner: z.string().max(40).optional(),
|
|
210
|
+
customer: z
|
|
211
|
+
.object({
|
|
212
|
+
field: z.string().max(40),
|
|
213
|
+
read: z.array(z.string().max(40)).max(100),
|
|
214
|
+
write: z.array(z.string().max(40)).max(100),
|
|
215
|
+
requireAccess: z.array(z.string().max(40)).max(10).optional(),
|
|
216
|
+
createOnly: z.array(z.string().max(40)).max(100).optional(),
|
|
217
|
+
})
|
|
218
|
+
.strict()
|
|
219
|
+
.optional(),
|
|
220
|
+
public: readDeclarationSchema.optional(),
|
|
221
|
+
signedIn: readDeclarationSchema.optional(),
|
|
222
|
+
customerVia: z
|
|
223
|
+
.object({
|
|
224
|
+
from: z.string().max(81),
|
|
225
|
+
field: z.string().max(40),
|
|
226
|
+
read: z.array(z.string().max(40)).min(1).max(100),
|
|
227
|
+
})
|
|
228
|
+
.strict()
|
|
229
|
+
.optional(),
|
|
230
|
+
access: z
|
|
231
|
+
.object({
|
|
232
|
+
sources: z
|
|
233
|
+
.array(
|
|
234
|
+
z.union([
|
|
235
|
+
z
|
|
236
|
+
.object({
|
|
237
|
+
via: z.string().max(81),
|
|
238
|
+
field: z.string().max(40),
|
|
239
|
+
through: z.string().max(40).optional(),
|
|
240
|
+
states: z.array(z.string().max(60)).min(1).max(20).optional(),
|
|
241
|
+
until: z.string().max(40).optional(),
|
|
242
|
+
})
|
|
243
|
+
.strict(),
|
|
244
|
+
z
|
|
245
|
+
.object({
|
|
246
|
+
kind: z.string().regex(/^[a-z][a-zA-Z0-9]{0,39}$/),
|
|
247
|
+
options: z
|
|
248
|
+
.record(
|
|
249
|
+
z.string().max(40),
|
|
250
|
+
z.union([
|
|
251
|
+
z.string().max(200),
|
|
252
|
+
z.number(),
|
|
253
|
+
z.boolean(),
|
|
254
|
+
z.array(z.string().max(200)).max(20),
|
|
255
|
+
]),
|
|
256
|
+
)
|
|
257
|
+
.optional(),
|
|
258
|
+
})
|
|
259
|
+
.strict(),
|
|
260
|
+
]),
|
|
261
|
+
)
|
|
262
|
+
.min(1)
|
|
263
|
+
.max(8),
|
|
264
|
+
})
|
|
265
|
+
.strict()
|
|
266
|
+
.optional(),
|
|
267
|
+
publishable: z
|
|
268
|
+
.object({
|
|
269
|
+
content: z.array(z.string().max(40)).min(1).max(40),
|
|
270
|
+
slug: z.string().max(40).optional(),
|
|
271
|
+
date: z.string().max(40).optional(),
|
|
272
|
+
})
|
|
273
|
+
.strict()
|
|
274
|
+
.optional(),
|
|
275
|
+
navigationOrder: z.number().int().min(0).max(10_000).optional(),
|
|
276
|
+
requireOneOf: z
|
|
277
|
+
.array(z.array(z.string().max(40)).min(2).max(8))
|
|
278
|
+
.max(8)
|
|
279
|
+
.optional(),
|
|
280
|
+
titleDefault: z
|
|
281
|
+
.object({
|
|
282
|
+
from: z.array(z.string().max(40)).min(1).max(8),
|
|
283
|
+
suffix: z.string().max(40).optional(),
|
|
284
|
+
})
|
|
285
|
+
.optional(),
|
|
286
|
+
seed: z
|
|
287
|
+
.record(z.string().max(40), z.record(z.string().max(40), z.unknown()))
|
|
288
|
+
.refine((seed) => Object.keys(seed).length <= 200, "At most 200 seed records per object")
|
|
289
|
+
.optional(),
|
|
290
|
+
});
|
|
291
|
+
export type ObjectManifest = z.infer<typeof objectManifestSchema>;
|
|
292
|
+
|
|
293
|
+
const jsonSchemaObject = z.record(z.string(), z.unknown());
|
|
294
|
+
|
|
295
|
+
export const recordActionManifestSchema = z.object({
|
|
296
|
+
object: z.string().min(1).max(80),
|
|
297
|
+
label: z.string().min(1).max(60),
|
|
298
|
+
idInput: z.string().min(1).max(40),
|
|
299
|
+
confirm: z.string().max(300).optional(),
|
|
300
|
+
ask: z
|
|
301
|
+
.object({
|
|
302
|
+
input: z.string().min(1).max(40),
|
|
303
|
+
label: z.string().min(1).max(60),
|
|
304
|
+
options: z
|
|
305
|
+
.array(z.object({ value: z.string().min(1).max(100), label: z.string().min(1).max(100) }))
|
|
306
|
+
.min(1)
|
|
307
|
+
.max(50),
|
|
308
|
+
})
|
|
309
|
+
.optional(),
|
|
310
|
+
hideWhen: z
|
|
311
|
+
.object({
|
|
312
|
+
field: z.string().min(1).max(80),
|
|
313
|
+
values: z.array(z.string().min(1).max(100)).min(1).max(50),
|
|
314
|
+
})
|
|
315
|
+
.optional(),
|
|
316
|
+
});
|
|
317
|
+
export type RecordActionManifest = z.infer<typeof recordActionManifestSchema>;
|
|
318
|
+
|
|
319
|
+
export const toolManifestSchema = z.object({
|
|
320
|
+
description: z.string().min(20).max(2000),
|
|
321
|
+
risk: z.enum(TOOL_RISKS),
|
|
322
|
+
recordAction: recordActionManifestSchema.optional(),
|
|
323
|
+
audiences: z.array(z.enum(TOOL_AUDIENCES)).min(1).max(3).optional(),
|
|
324
|
+
customerFields: z.record(z.string().max(80), z.array(z.string().max(40)).max(50)).optional(),
|
|
325
|
+
/** The permission a staff caller needs (`<id>:<word>`), for the extension's roles (ADR 0436). */
|
|
326
|
+
permission: z.string().max(80).optional(),
|
|
327
|
+
/** Only a person may call it (ADR 0512). */
|
|
328
|
+
humanOnly: z.literal(true).optional(),
|
|
329
|
+
inputSchema: jsonSchemaObject,
|
|
330
|
+
outputSchema: jsonSchemaObject,
|
|
331
|
+
});
|
|
332
|
+
export type ToolManifest = z.infer<typeof toolManifestSchema>;
|
|
333
|
+
|
|
334
|
+
/** A fixed staff role (ADR 0436): see `RoleSpec`. */
|
|
335
|
+
export const roleManifestSchema = z.object({
|
|
336
|
+
label: z.string().min(1).max(60),
|
|
337
|
+
description: z.string().min(10).max(500),
|
|
338
|
+
permissions: z.array(z.string().max(80)).max(20).default([]),
|
|
339
|
+
read: z.array(z.string().max(80)).max(20).default([]),
|
|
340
|
+
});
|
|
341
|
+
export type RoleManifest = z.infer<typeof roleManifestSchema>;
|
|
342
|
+
|
|
343
|
+
/** A role's or a tool's permission: `<extension id>:<word>`. */
|
|
344
|
+
export const EXTENSION_PERMISSION_RE = /^([a-z][a-z0-9]{1,29}):([a-z][a-zA-Z0-9]{0,39})$/;
|
|
345
|
+
|
|
346
|
+
export const viewManifestSchema = z.object({
|
|
347
|
+
label: z.string().min(1).max(100),
|
|
348
|
+
object: z.string().max(80),
|
|
349
|
+
/** The view type (ADR 0056). Default table. */
|
|
350
|
+
type: z.enum(["table", "board", "calendar", "gallery"]).optional(),
|
|
351
|
+
filter: z.unknown().optional(),
|
|
352
|
+
sort: z
|
|
353
|
+
.object({ field: z.string().max(64), direction: z.enum(["asc", "desc"]).default("asc") })
|
|
354
|
+
.optional(),
|
|
355
|
+
columns: z.array(z.string().max(80)).max(100).optional(),
|
|
356
|
+
groupBy: z.string().max(80).optional(),
|
|
357
|
+
sumField: z.string().max(80).optional(),
|
|
358
|
+
dateField: z.string().max(80).optional(),
|
|
359
|
+
mode: z.enum(["month", "week", "agenda"]).optional(),
|
|
360
|
+
imageField: z.string().max(80).optional(),
|
|
361
|
+
cardFields: z.array(z.string().max(80)).max(8).optional(),
|
|
362
|
+
});
|
|
363
|
+
export type ViewManifest = z.infer<typeof viewManifestSchema>;
|
|
364
|
+
|
|
365
|
+
const RECORD_VIEW_TYPES = ["table", "board", "calendar", "gallery"] as const;
|
|
366
|
+
|
|
367
|
+
export const recordViewManifestSchema = z.object({
|
|
368
|
+
label: z.string().min(1).max(100),
|
|
369
|
+
on: z.string().min(1).max(80),
|
|
370
|
+
view: z.object({
|
|
371
|
+
type: z.enum(RECORD_VIEW_TYPES),
|
|
372
|
+
object: z.string().min(1).max(80),
|
|
373
|
+
relation: z.string().min(1).max(80),
|
|
374
|
+
filter: z.unknown().optional(),
|
|
375
|
+
sort: z
|
|
376
|
+
.object({ field: z.string().max(64), direction: z.enum(["asc", "desc"]).default("asc") })
|
|
377
|
+
.optional(),
|
|
378
|
+
columns: z.array(z.string().max(80)).max(100).optional(),
|
|
379
|
+
groupBy: z.string().max(80).optional(),
|
|
380
|
+
sumField: z.string().max(80).optional(),
|
|
381
|
+
dateField: z.string().max(80).optional(),
|
|
382
|
+
mode: z.enum(["month", "week", "agenda"]).optional(),
|
|
383
|
+
imageField: z.string().max(80).optional(),
|
|
384
|
+
cardFields: z.array(z.string().max(80)).max(8).optional(),
|
|
385
|
+
}),
|
|
386
|
+
order: z.number().int().min(0).max(10_000).default(100),
|
|
387
|
+
});
|
|
388
|
+
export type RecordViewManifest = z.infer<typeof recordViewManifestSchema>;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A dashboard an extension ships (stage 4b): read-only for everyone, listed beside the CRM's own
|
|
392
|
+
* Overview while the extension is active, copyable. `home` makes it the default Home. The host
|
|
393
|
+
* validates `filters` and `widgets` against its dashboard spec (ADR 0057) at check time.
|
|
394
|
+
*/
|
|
395
|
+
export const dashboardManifestSchema = z.object({
|
|
396
|
+
title: z.string().min(1).max(200),
|
|
397
|
+
description: z.string().max(1000).optional(),
|
|
398
|
+
home: z.boolean().optional(),
|
|
399
|
+
filters: z.array(z.record(z.string(), z.unknown())).max(8).default([]),
|
|
400
|
+
widgets: z.array(z.record(z.string(), z.unknown())).min(1).max(30),
|
|
401
|
+
});
|
|
402
|
+
export type DashboardManifest = z.infer<typeof dashboardManifestSchema>;
|
|
403
|
+
|
|
404
|
+
export const settingManifestSchema = z.object({
|
|
405
|
+
type: z.enum(["text", "number", "boolean", "select"]),
|
|
406
|
+
label: z.string().min(1).max(100),
|
|
407
|
+
description: z.string().max(1000).optional(),
|
|
408
|
+
required: z.boolean().optional(),
|
|
409
|
+
default: z.union([z.string().max(1000), z.number(), z.boolean()]).optional(),
|
|
410
|
+
min: z.number().optional(),
|
|
411
|
+
max: z.number().optional(),
|
|
412
|
+
options: z.array(optionSchema).max(200).optional(),
|
|
413
|
+
personApproves: z.boolean().optional(),
|
|
414
|
+
});
|
|
415
|
+
export type SettingManifest = z.infer<typeof settingManifestSchema>;
|
|
416
|
+
|
|
417
|
+
export const permissionsManifestSchema = z.object({
|
|
418
|
+
read: z.array(z.string().max(80)).max(100).default([]),
|
|
419
|
+
write: z.array(z.string().max(80)).max(100).default([]),
|
|
420
|
+
operations: z.array(z.string().max(100)).max(200).default([]),
|
|
421
|
+
modules: z
|
|
422
|
+
.array(
|
|
423
|
+
z.enum(HOST_MODULES, {
|
|
424
|
+
// A manifest written against the old contract is told the module's new name.
|
|
425
|
+
error: (issue) =>
|
|
426
|
+
(typeof issue.input === "string" ? renamedMessage(issue.input) : null) ??
|
|
427
|
+
`Unknown host module; one of ${HOST_MODULES.join(", ")}`,
|
|
428
|
+
}),
|
|
429
|
+
)
|
|
430
|
+
.max(HOST_MODULES.length)
|
|
431
|
+
.default([]),
|
|
432
|
+
});
|
|
433
|
+
export type PermissionsManifest = z.infer<typeof permissionsManifestSchema>;
|
|
434
|
+
|
|
435
|
+
export const manifestSchema = z.object({
|
|
436
|
+
id: z.string(),
|
|
437
|
+
version: z.string(),
|
|
438
|
+
contractVersion: z.number().int(),
|
|
439
|
+
label: z.string().max(100).optional(),
|
|
440
|
+
description: z.string().max(2000).optional(),
|
|
441
|
+
requires: z
|
|
442
|
+
.array(z.string().regex(/^[a-z][a-z0-9]*$/))
|
|
443
|
+
.max(10)
|
|
444
|
+
.optional(),
|
|
445
|
+
permissions: permissionsManifestSchema.default({
|
|
446
|
+
read: [],
|
|
447
|
+
write: [],
|
|
448
|
+
operations: [],
|
|
449
|
+
modules: [],
|
|
450
|
+
}),
|
|
451
|
+
objects: z.record(z.string(), objectManifestSchema).default({}),
|
|
452
|
+
fields: z.record(z.string(), z.record(z.string(), fieldManifestSchema)).default({}),
|
|
453
|
+
views: z.record(z.string(), viewManifestSchema).default({}),
|
|
454
|
+
recordTabs: z.record(z.string(), recordViewManifestSchema).default({}),
|
|
455
|
+
recordPanels: z.record(z.string(), recordViewManifestSchema).default({}),
|
|
456
|
+
settings: z.record(z.string(), settingManifestSchema).default({}),
|
|
457
|
+
dashboards: z.record(z.string(), dashboardManifestSchema).default({}),
|
|
458
|
+
tools: z.record(z.string(), toolManifestSchema).default({}),
|
|
459
|
+
automations: z.record(z.string(), automationManifestSchema).default({}),
|
|
460
|
+
roles: z.record(z.string(), roleManifestSchema).default({}),
|
|
461
|
+
/** Tool permissions an admin may grant one by one (ADR 0449). */
|
|
462
|
+
grantable: z.array(z.string().max(80)).max(20).optional(),
|
|
463
|
+
});
|
|
464
|
+
export type ExtensionManifest = z.infer<typeof manifestSchema>;
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* A document field's schema, size and paths (ADR 0445); and that no other type carries them.
|
|
468
|
+
* `fields` are the other fields the same extension declares on the object, which a path's name
|
|
469
|
+
* may not repeat.
|
|
470
|
+
*/
|
|
471
|
+
function checkDocumentField(
|
|
472
|
+
path: string,
|
|
473
|
+
name: string,
|
|
474
|
+
field: FieldManifest,
|
|
475
|
+
fields: Record<string, FieldManifest>,
|
|
476
|
+
known: ReadonlySet<string>,
|
|
477
|
+
contract: (path: string, message: string) => void,
|
|
478
|
+
): void {
|
|
479
|
+
if (field.type === "image" || field.type === "video") {
|
|
480
|
+
for (const key of ["schema", "maxBytes", "paths"] as const) {
|
|
481
|
+
if (field[key] !== undefined) {
|
|
482
|
+
contract(
|
|
483
|
+
`${path}.${key}`,
|
|
484
|
+
`An ${field.type} field has the platform's schema and no ${key}`,
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (field.track) contract(`${path}.track`, `An ${field.type} is not tracked on the timeline`);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (field.type === "richText") {
|
|
492
|
+
// One fixed schema (ADR 0440): a field declares at most its size.
|
|
493
|
+
for (const key of ["schema", "paths"] as const) {
|
|
494
|
+
if (field[key] !== undefined) {
|
|
495
|
+
contract(`${path}.${key}`, `A richText field has the platform's schema and no ${key}`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (field.track) contract(`${path}.track`, "Rich text is not tracked on the timeline");
|
|
499
|
+
if (
|
|
500
|
+
field.maxBytes !== undefined &&
|
|
501
|
+
(field.maxBytes < 1024 || field.maxBytes > DOCUMENT_MAX_BYTES)
|
|
502
|
+
) {
|
|
503
|
+
contract(`${path}.maxBytes`, `maxBytes is from 1024 to ${DOCUMENT_MAX_BYTES}`);
|
|
504
|
+
}
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (field.type !== "document") {
|
|
508
|
+
for (const key of ["schema", "maxBytes", "paths"] as const) {
|
|
509
|
+
if (field[key] !== undefined) {
|
|
510
|
+
contract(`${path}.${key}`, `Only document and richText fields take ${key}`);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (field.track) contract(`${path}.track`, "A document is not tracked on the timeline");
|
|
516
|
+
if (!field.schema) {
|
|
517
|
+
contract(`${path}.schema`, "A document field declares its schema");
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
// A document's refs may also name the media library (stage 42, ADR 0475): a lesson's video.
|
|
521
|
+
const problems = checkDocumentSchema(field.schema, new Set([...known, "media"]));
|
|
522
|
+
for (const [at, message] of problems) contract(`${path}.schema${at ? `.${at}` : ""}`, message);
|
|
523
|
+
if (
|
|
524
|
+
field.maxBytes !== undefined &&
|
|
525
|
+
(field.maxBytes < 1024 || field.maxBytes > DOCUMENT_MAX_BYTES)
|
|
526
|
+
) {
|
|
527
|
+
contract(`${path}.maxBytes`, `maxBytes is from 1024 to ${DOCUMENT_MAX_BYTES}`);
|
|
528
|
+
}
|
|
529
|
+
const paths = Object.entries(field.paths ?? {});
|
|
530
|
+
if (paths.length > DOCUMENT_MAX_PATHS) {
|
|
531
|
+
contract(`${path}.paths`, `A document declares at most ${DOCUMENT_MAX_PATHS} paths`);
|
|
532
|
+
}
|
|
533
|
+
for (const [pathName, spec] of paths) {
|
|
534
|
+
const at = `${path}.paths.${pathName}`;
|
|
535
|
+
if (!FIELD_NAME_RE.test(pathName) || RESERVED_FIELD_NAMES.has(pathName)) {
|
|
536
|
+
contract(at, `A path's name is a camelCase field name that is not reserved`);
|
|
537
|
+
}
|
|
538
|
+
if (Object.hasOwn(fields, pathName)) {
|
|
539
|
+
contract(at, `"${pathName}" is already a field: a path's name is a column of its own`);
|
|
540
|
+
}
|
|
541
|
+
const others = Object.entries(fields).filter(
|
|
542
|
+
([other, f]) => other !== name && f.type === "document" && f.paths && pathName in f.paths,
|
|
543
|
+
);
|
|
544
|
+
if (others.length) contract(at, `"${pathName}" is a path of ${others[0]?.[0]} too`);
|
|
545
|
+
if (!problems.length) {
|
|
546
|
+
const reached = documentPathNode(field.schema as unknown as DocumentSchema, spec);
|
|
547
|
+
if (typeof reached === "string") contract(`${at}.path`, reached);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** One problem `check` found. `path` points into the manifest, e.g. `objects.project.fields.x`. */
|
|
553
|
+
export interface ManifestIssue {
|
|
554
|
+
code:
|
|
555
|
+
| "contract"
|
|
556
|
+
| "formula_syntax"
|
|
557
|
+
| "formula_type"
|
|
558
|
+
| "formula_unknown_field"
|
|
559
|
+
| "formula_unknown_function"
|
|
560
|
+
| "formula_cycle"
|
|
561
|
+
| "formula_too_complex";
|
|
562
|
+
path: string;
|
|
563
|
+
message: string;
|
|
564
|
+
/** For formula issues: the character span in the expression. */
|
|
565
|
+
span?: { start: number; end: number };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function normaliseOptions(options: readonly unknown[] | undefined) {
|
|
569
|
+
return options?.map((option) =>
|
|
570
|
+
typeof option === "string"
|
|
571
|
+
? { value: option, label: option }
|
|
572
|
+
: {
|
|
573
|
+
...(option as { value: string; label?: string; color?: string }),
|
|
574
|
+
label: (option as { label?: string }).label ?? (option as { value: string }).value ?? "",
|
|
575
|
+
},
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function fieldToManifest(spec: Record<string, unknown>): Record<string, unknown> {
|
|
580
|
+
const { options, ...rest } = spec;
|
|
581
|
+
return {
|
|
582
|
+
...rest,
|
|
583
|
+
...(options ? { options: normaliseOptions(options as unknown[]) } : {}),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function fieldsToManifest(fields: Record<string, unknown> | undefined) {
|
|
588
|
+
return Object.fromEntries(
|
|
589
|
+
Object.entries(fields ?? {}).map(([name, spec]) => [
|
|
590
|
+
name,
|
|
591
|
+
fieldToManifest(spec as Record<string, unknown>),
|
|
592
|
+
]),
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Turns a live extension into its JSON manifest. Tool schemas become JSON Schema. Runs wherever
|
|
598
|
+
* the extension is loaded (the sandbox, or the testing harness).
|
|
599
|
+
*/
|
|
600
|
+
export function toManifest(extension: Extension): ExtensionManifest {
|
|
601
|
+
const raw = {
|
|
602
|
+
id: extension.id,
|
|
603
|
+
version: extension.version,
|
|
604
|
+
contractVersion: extension.contractVersion,
|
|
605
|
+
...(extension.label ? { label: extension.label } : {}),
|
|
606
|
+
...(extension.description ? { description: extension.description } : {}),
|
|
607
|
+
permissions: extension.permissions ?? {},
|
|
608
|
+
objects: Object.fromEntries(
|
|
609
|
+
Object.entries(extension.objects ?? {}).map(([name, object]) => [
|
|
610
|
+
name,
|
|
611
|
+
{ ...object, fields: fieldsToManifest(object.fields as Record<string, unknown>) },
|
|
612
|
+
]),
|
|
613
|
+
),
|
|
614
|
+
fields: Object.fromEntries(
|
|
615
|
+
Object.entries(extension.fields ?? {}).map(([target, fields]) => [
|
|
616
|
+
target,
|
|
617
|
+
fieldsToManifest(fields as Record<string, unknown>),
|
|
618
|
+
]),
|
|
619
|
+
),
|
|
620
|
+
views: extension.views ?? {},
|
|
621
|
+
dashboards: extension.dashboards ?? {},
|
|
622
|
+
recordTabs: extension.recordTabs ?? {},
|
|
623
|
+
recordPanels: extension.recordPanels ?? {},
|
|
624
|
+
settings: Object.fromEntries(
|
|
625
|
+
Object.entries(extension.settings ?? {}).map(([name, setting]) => [
|
|
626
|
+
name,
|
|
627
|
+
fieldToManifest(setting as unknown as Record<string, unknown>),
|
|
628
|
+
]),
|
|
629
|
+
),
|
|
630
|
+
tools: Object.fromEntries(
|
|
631
|
+
Object.entries(extension.tools ?? {}).map(([name, tool]) => [
|
|
632
|
+
name,
|
|
633
|
+
{
|
|
634
|
+
description: tool.description,
|
|
635
|
+
risk: tool.risk ?? "write",
|
|
636
|
+
...(tool.recordAction ? { recordAction: tool.recordAction } : {}),
|
|
637
|
+
...(tool.audiences ? { audiences: [...tool.audiences] } : {}),
|
|
638
|
+
...(tool.permission ? { permission: tool.permission } : {}),
|
|
639
|
+
...(tool.humanOnly ? { humanOnly: true as const } : {}),
|
|
640
|
+
...(tool.customerFields
|
|
641
|
+
? {
|
|
642
|
+
customerFields: Object.fromEntries(
|
|
643
|
+
Object.entries(tool.customerFields).map(([object, fields]) => [
|
|
644
|
+
object,
|
|
645
|
+
[...fields],
|
|
646
|
+
]),
|
|
647
|
+
),
|
|
648
|
+
}
|
|
649
|
+
: {}),
|
|
650
|
+
inputSchema: z.toJSONSchema(tool.input, { io: "input", unrepresentable: "any" }),
|
|
651
|
+
outputSchema: z.toJSONSchema(tool.output, { io: "output", unrepresentable: "any" }),
|
|
652
|
+
},
|
|
653
|
+
]),
|
|
654
|
+
),
|
|
655
|
+
automations: Object.fromEntries(
|
|
656
|
+
Object.entries(extension.automations ?? {}).map(([name, automation]) => [
|
|
657
|
+
name,
|
|
658
|
+
{
|
|
659
|
+
label: automation.label,
|
|
660
|
+
description: automation.description,
|
|
661
|
+
trigger: automation.trigger,
|
|
662
|
+
...(automation.conditions ? { conditions: automation.conditions } : {}),
|
|
663
|
+
steps: automation.steps,
|
|
664
|
+
...(automation.onRejected ? { onRejected: automation.onRejected } : {}),
|
|
665
|
+
...(automation.rails ? { rails: automation.rails } : {}),
|
|
666
|
+
...(automation.connections?.length ? { connections: automation.connections } : {}),
|
|
667
|
+
...(automation.enabled === false ? { enabled: false } : {}),
|
|
668
|
+
},
|
|
669
|
+
]),
|
|
670
|
+
),
|
|
671
|
+
...(extension.grantable ? { grantable: [...extension.grantable] } : {}),
|
|
672
|
+
...(extension.requires?.length ? { requires: [...extension.requires] } : {}),
|
|
673
|
+
roles: Object.fromEntries(
|
|
674
|
+
Object.entries(extension.roles ?? {}).map(([name, role]) => [
|
|
675
|
+
name,
|
|
676
|
+
{
|
|
677
|
+
label: role.label,
|
|
678
|
+
description: role.description,
|
|
679
|
+
permissions: [...role.permissions],
|
|
680
|
+
read: [...role.read],
|
|
681
|
+
},
|
|
682
|
+
]),
|
|
683
|
+
),
|
|
684
|
+
};
|
|
685
|
+
const parsed = manifestSchema.safeParse(raw);
|
|
686
|
+
if (!parsed.success) {
|
|
687
|
+
// Return what we have; validateManifest reports the structural issues.
|
|
688
|
+
return raw as unknown as ExtensionManifest;
|
|
689
|
+
}
|
|
690
|
+
return parsed.data;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Core fields formulas on core objects can read, by formula type. The host checks this list
|
|
695
|
+
* against its field registry (packages/core/test/sdk-core-fields.test.ts), so the SDK and the
|
|
696
|
+
* testing harness agree with the host without depending on it.
|
|
697
|
+
*/
|
|
698
|
+
export const CORE_FORMULA_FIELDS: Record<
|
|
699
|
+
(typeof CORE_OBJECT_NAMES)[number],
|
|
700
|
+
Record<string, "text" | "number" | "boolean" | "date" | "datetime">
|
|
701
|
+
> = {
|
|
702
|
+
person: {
|
|
703
|
+
id: "text",
|
|
704
|
+
displayName: "text",
|
|
705
|
+
givenName: "text",
|
|
706
|
+
familyName: "text",
|
|
707
|
+
primaryEmail: "text",
|
|
708
|
+
primaryPhone: "text",
|
|
709
|
+
jobTitle: "text",
|
|
710
|
+
organisationId: "text",
|
|
711
|
+
organisationName: "text",
|
|
712
|
+
location: "text",
|
|
713
|
+
timezone: "text",
|
|
714
|
+
ownerId: "text",
|
|
715
|
+
createdAt: "datetime",
|
|
716
|
+
updatedAt: "datetime",
|
|
717
|
+
},
|
|
718
|
+
organisation: {
|
|
719
|
+
id: "text",
|
|
720
|
+
name: "text",
|
|
721
|
+
primaryDomain: "text",
|
|
722
|
+
website: "text",
|
|
723
|
+
industry: "text",
|
|
724
|
+
parentId: "text",
|
|
725
|
+
parentName: "text",
|
|
726
|
+
ownerId: "text",
|
|
727
|
+
currentPeopleCount: "number",
|
|
728
|
+
createdAt: "datetime",
|
|
729
|
+
updatedAt: "datetime",
|
|
730
|
+
},
|
|
731
|
+
activity: {
|
|
732
|
+
id: "text",
|
|
733
|
+
type: "text",
|
|
734
|
+
subject: "text",
|
|
735
|
+
body: "text",
|
|
736
|
+
occurredAt: "datetime",
|
|
737
|
+
authorUserId: "text",
|
|
738
|
+
authorActor: "text",
|
|
739
|
+
createdAt: "datetime",
|
|
740
|
+
},
|
|
741
|
+
task: {
|
|
742
|
+
id: "text",
|
|
743
|
+
title: "text",
|
|
744
|
+
notes: "text",
|
|
745
|
+
dueAt: "datetime",
|
|
746
|
+
status: "text",
|
|
747
|
+
completedAt: "datetime",
|
|
748
|
+
assigneeId: "text",
|
|
749
|
+
createdBy: "text",
|
|
750
|
+
createdAt: "datetime",
|
|
751
|
+
updatedAt: "datetime",
|
|
752
|
+
},
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
function coreFormulaRefs(object: string): Record<string, FormulaFieldRef> {
|
|
756
|
+
const fields = (CORE_FORMULA_FIELDS as Record<string, Record<string, string>>)[object];
|
|
757
|
+
if (!fields) return {};
|
|
758
|
+
return Object.fromEntries(
|
|
759
|
+
Object.entries(fields).map(([name, type]) => [name, { type, sql: name } as FormulaFieldRef]),
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
export interface ValidateOptions {
|
|
764
|
+
/**
|
|
765
|
+
* Fields a formula on `object` may read, other than the extension's own fields on it: the
|
|
766
|
+
* host supplies core and installed fields. Default: none.
|
|
767
|
+
*/
|
|
768
|
+
baseFields?: (object: string) => Record<string, FormulaFieldRef>;
|
|
769
|
+
/** Objects that exist outside this extension (`person`, `other.thing`). Default: core only. */
|
|
770
|
+
knownObjects?: readonly string[];
|
|
771
|
+
/**
|
|
772
|
+
* Field types of objects outside this extension (installed extensions' objects), for the
|
|
773
|
+
* automation checks; null when unknown (those checks are skipped). Core objects are built in.
|
|
774
|
+
*/
|
|
775
|
+
objectFields?: (object: string) => Record<string, FieldTypeName> | null;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/** Formula environment for a set of field manifests (formula fields compile on demand). */
|
|
779
|
+
export function formulaFields(
|
|
780
|
+
fields: Record<string, FieldManifest>,
|
|
781
|
+
): Record<string, FormulaFieldRef> {
|
|
782
|
+
const env: Record<string, FormulaFieldRef> = {};
|
|
783
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
784
|
+
if (field.type === "formula") {
|
|
785
|
+
env[name] = { expression: field.expression ?? "" };
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
const type = formulaTypeOf(field.type, field.many);
|
|
789
|
+
if (type) env[name] = { type, sql: `"${name}"` };
|
|
790
|
+
}
|
|
791
|
+
return env;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* The contract check: structure (zod), names, references, permissions and formulas. Returns
|
|
796
|
+
* every issue found (empty when the manifest is valid) and the parsed manifest when it parsed.
|
|
797
|
+
*/
|
|
798
|
+
export function validateManifest(
|
|
799
|
+
input: unknown,
|
|
800
|
+
options: ValidateOptions = {},
|
|
801
|
+
): { manifest: ExtensionManifest | null; issues: ManifestIssue[] } {
|
|
802
|
+
const issues: ManifestIssue[] = [];
|
|
803
|
+
const contract = (path: string, message: string) =>
|
|
804
|
+
issues.push({ code: "contract", path, message });
|
|
805
|
+
|
|
806
|
+
const parsed = manifestSchema.safeParse(input);
|
|
807
|
+
if (!parsed.success) {
|
|
808
|
+
for (const issue of parsed.error.issues) {
|
|
809
|
+
contract(issue.path.join(".") || "(root)", issue.message);
|
|
810
|
+
}
|
|
811
|
+
return { manifest: null, issues };
|
|
812
|
+
}
|
|
813
|
+
const m = parsed.data;
|
|
814
|
+
|
|
815
|
+
if (!EXTENSION_ID_RE.test(m.id) || RESERVED_EXTENSION_IDS.has(m.id)) {
|
|
816
|
+
contract(
|
|
817
|
+
"id",
|
|
818
|
+
`Extension id "${m.id}" must be 2-30 lowercase letters or digits, start with a letter, and not be reserved`,
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
if (!SEMVER_RE.test(m.version))
|
|
822
|
+
contract("version", `Version "${m.version}" must be semver, e.g. 1.0.0`);
|
|
823
|
+
if (!SUPPORTED_CONTRACT_VERSIONS.includes(m.contractVersion)) {
|
|
824
|
+
contract(
|
|
825
|
+
"contractVersion",
|
|
826
|
+
`Contract version ${m.contractVersion} is not supported (supported: ${SUPPORTED_CONTRACT_VERSIONS.join(", ")})`,
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const ownObjects = Object.keys(m.objects).map((name) => `${m.id}.${name}`);
|
|
831
|
+
const known = new Set<string>([
|
|
832
|
+
...CORE_OBJECT_NAMES,
|
|
833
|
+
...(options.knownObjects ?? []),
|
|
834
|
+
...ownObjects,
|
|
835
|
+
]);
|
|
836
|
+
|
|
837
|
+
const checkFields = (
|
|
838
|
+
base: string,
|
|
839
|
+
object: string,
|
|
840
|
+
fields: Record<string, FieldManifest>,
|
|
841
|
+
env: Record<string, FormulaFieldRef>,
|
|
842
|
+
) => {
|
|
843
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
844
|
+
const path = `${base}.${name}`;
|
|
845
|
+
if (!FIELD_NAME_RE.test(name)) {
|
|
846
|
+
contract(
|
|
847
|
+
path,
|
|
848
|
+
`Field name "${name}" must be camelCase letters and digits, starting lowercase`,
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
if (RESERVED_FIELD_NAMES.has(name)) contract(path, `"${name}" is reserved on every record`);
|
|
852
|
+
if (field.renamedFrom !== undefined && !FIELD_NAME_RE.test(field.renamedFrom)) {
|
|
853
|
+
contract(`${path}.renamedFrom`, "renamedFrom must be a valid field name");
|
|
854
|
+
}
|
|
855
|
+
if (field.renamedFrom === name)
|
|
856
|
+
contract(`${path}.renamedFrom`, "renamedFrom equals the name");
|
|
857
|
+
if (field.type === "select" || field.type === "multiSelect") {
|
|
858
|
+
if (!field.options?.length)
|
|
859
|
+
contract(`${path}.options`, `${field.type} fields need options`);
|
|
860
|
+
const values = field.options?.map((o) => o.value) ?? [];
|
|
861
|
+
if (new Set(values).size !== values.length) {
|
|
862
|
+
contract(`${path}.options`, "Option values must be unique");
|
|
863
|
+
}
|
|
864
|
+
for (const option of field.options ?? []) {
|
|
865
|
+
if (!option.requires?.length) continue;
|
|
866
|
+
if (field.type !== "select") {
|
|
867
|
+
contract(`${path}.options`, "Only select fields take `requires`");
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
for (const required of option.requires) {
|
|
871
|
+
if (!(required in fields)) {
|
|
872
|
+
contract(
|
|
873
|
+
`${path}.options`,
|
|
874
|
+
`Option "${option.value}" requires "${required}", which is not a field here`,
|
|
875
|
+
);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (!field.editableOptions && field.options?.some((o) => o.fixed !== undefined)) {
|
|
880
|
+
contract(`${path}.options`, "Only fields with editableOptions take `fixed`");
|
|
881
|
+
}
|
|
882
|
+
} else if (field.options) {
|
|
883
|
+
contract(`${path}.options`, `Only select and multiSelect fields take options`);
|
|
884
|
+
}
|
|
885
|
+
if (
|
|
886
|
+
field.editableOptions !== undefined &&
|
|
887
|
+
field.type !== "select" &&
|
|
888
|
+
field.type !== "multiSelect"
|
|
889
|
+
) {
|
|
890
|
+
contract(
|
|
891
|
+
`${path}.editableOptions`,
|
|
892
|
+
"Only select and multiSelect fields take editableOptions",
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
if (field.type === "relation") {
|
|
896
|
+
if (!field.to) contract(`${path}.to`, "relation fields need `to`");
|
|
897
|
+
else if (
|
|
898
|
+
!(RELATION_CORE_TARGETS as readonly string[]).includes(field.to) &&
|
|
899
|
+
!(field.to.includes(".") && known.has(field.to))
|
|
900
|
+
) {
|
|
901
|
+
contract(
|
|
902
|
+
`${path}.to`,
|
|
903
|
+
`Unknown relation target "${field.to}". Use person, organisation, task or <extension>.<object>`,
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
} else if (field.to !== undefined || field.many !== undefined) {
|
|
907
|
+
contract(path, "Only relation fields take `to` and `many`");
|
|
908
|
+
}
|
|
909
|
+
if (field.type === "currency" && field.currency === undefined) {
|
|
910
|
+
// Default applied by the host; nothing to report.
|
|
911
|
+
} else if (field.type !== "currency" && field.currency !== undefined) {
|
|
912
|
+
contract(`${path}.currency`, "Only currency fields take `currency`");
|
|
913
|
+
}
|
|
914
|
+
if (field.readOnly && field.required) {
|
|
915
|
+
contract(`${path}.readOnly`, "A read-only field is set by the host and cannot be required");
|
|
916
|
+
}
|
|
917
|
+
checkDocumentField(path, name, field, fields, known, contract);
|
|
918
|
+
if (["number", "currency", "percent"].includes(field.type)) {
|
|
919
|
+
if (field.min !== undefined && field.max !== undefined && field.min > field.max) {
|
|
920
|
+
contract(`${path}.min`, "min is above max");
|
|
921
|
+
}
|
|
922
|
+
if (typeof field.default === "number") {
|
|
923
|
+
const d = field.default;
|
|
924
|
+
const step = field.step;
|
|
925
|
+
if (
|
|
926
|
+
(field.min !== undefined && d < field.min) ||
|
|
927
|
+
(field.max !== undefined && d > field.max) ||
|
|
928
|
+
(step !== undefined && !onStep(d, field.min ?? 0, step))
|
|
929
|
+
) {
|
|
930
|
+
contract(`${path}.default`, "The default is outside the field's min, max or step");
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
} else {
|
|
934
|
+
for (const key of ["min", "max", "step"] as const) {
|
|
935
|
+
if (field[key] !== undefined)
|
|
936
|
+
contract(`${path}.${key}`, `Only number fields take ${key}`);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
if (field.type === "formula") {
|
|
940
|
+
if (field.required) contract(`${path}.required`, "Formula fields cannot be required");
|
|
941
|
+
if (field.track)
|
|
942
|
+
contract(`${path}.track`, "Formula fields cannot be tracked on the timeline");
|
|
943
|
+
if (!field.expression?.trim()) {
|
|
944
|
+
contract(`${path}.expression`, "formula fields need an expression");
|
|
945
|
+
} else {
|
|
946
|
+
const result = checkFormula(field.expression, { fields: env }, name);
|
|
947
|
+
if (!result.ok) {
|
|
948
|
+
issues.push({
|
|
949
|
+
code: result.issue.code,
|
|
950
|
+
path: `${path}.expression`,
|
|
951
|
+
message: `${object}.${name}: ${result.issue.message}`,
|
|
952
|
+
span: result.issue.span,
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
} else if (field.expression !== undefined) {
|
|
957
|
+
contract(`${path}.expression`, "Only formula fields take an expression");
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
963
|
+
const path = `objects.${name}`;
|
|
964
|
+
if (!OBJECT_NAME_RE.test(name)) {
|
|
965
|
+
contract(
|
|
966
|
+
path,
|
|
967
|
+
`Object name "${name}" must be lowercase letters and digits, starting with a letter`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
if (RESERVED_OBJECT_NAMES.has(name)) contract(path, `"${name}" is a reserved object name`);
|
|
971
|
+
if (object.renamedFrom !== undefined && !OBJECT_NAME_RE.test(object.renamedFrom)) {
|
|
972
|
+
contract(`${path}.renamedFrom`, "renamedFrom must be a valid object name");
|
|
973
|
+
}
|
|
974
|
+
const title = object.fields[object.titleField];
|
|
975
|
+
if (!title) {
|
|
976
|
+
contract(`${path}.titleField`, `titleField "${object.titleField}" is not a field of ${name}`);
|
|
977
|
+
} else if (!TEXT_LIKE.includes(title.type)) {
|
|
978
|
+
contract(
|
|
979
|
+
`${path}.titleField`,
|
|
980
|
+
`titleField must be a text-like field (${TEXT_LIKE.join(", ")})`,
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
// Every record's own timestamps can be read too, as on the host (formulaEnvironment).
|
|
984
|
+
const env = {
|
|
985
|
+
createdAt: { type: "datetime", sql: '"created_at"' } as FormulaFieldRef,
|
|
986
|
+
updatedAt: { type: "datetime", sql: '"updated_at"' } as FormulaFieldRef,
|
|
987
|
+
...formulaFields(object.fields),
|
|
988
|
+
};
|
|
989
|
+
checkFields(`${path}.fields`, `${m.id}.${name}`, object.fields, env);
|
|
990
|
+
checkObjectBehaviour(m, name, object, contract);
|
|
991
|
+
checkObjectCapacity(m, name, object, contract);
|
|
992
|
+
checkObjectSellable(m, name, object, contract);
|
|
993
|
+
checkObjectAllowance(m, name, object, contract);
|
|
994
|
+
checkObjectStates(m, name, object, contract);
|
|
995
|
+
checkObjectClaims(m, name, object, contract);
|
|
996
|
+
checkObjectUnique(m, name, object, contract);
|
|
997
|
+
checkObjectRecurring(m, name, object, contract);
|
|
998
|
+
object.requireOneOf?.forEach((group, i) => {
|
|
999
|
+
for (const field of group) {
|
|
1000
|
+
if (!object.fields[field]) {
|
|
1001
|
+
contract(`${path}.requireOneOf.${i}`, `"${field}" is not a field of ${name}`);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
});
|
|
1005
|
+
for (const field of object.titleDefault?.from ?? []) {
|
|
1006
|
+
if (object.fields[field]?.type !== "relation") {
|
|
1007
|
+
contract(`${path}.titleDefault`, `"${field}" is not a relation field of ${name}`);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
for (const [fieldName, field] of Object.entries(object.fields)) {
|
|
1011
|
+
const fieldPath = `${path}.fields.${fieldName}`;
|
|
1012
|
+
if (field.default !== undefined) {
|
|
1013
|
+
const expected =
|
|
1014
|
+
field.type === "boolean"
|
|
1015
|
+
? "boolean"
|
|
1016
|
+
: ["number", "currency", "percent"].includes(field.type)
|
|
1017
|
+
? "number"
|
|
1018
|
+
: ["text", "longText", "select", "date", "email", "phone", "url"].includes(field.type)
|
|
1019
|
+
? "string"
|
|
1020
|
+
: null;
|
|
1021
|
+
if (!expected) contract(`${fieldPath}.default`, `${field.type} fields take no default`);
|
|
1022
|
+
else if (typeof field.default !== expected) {
|
|
1023
|
+
contract(`${fieldPath}.default`, `The default must be a ${expected}`);
|
|
1024
|
+
} else if (
|
|
1025
|
+
field.type === "select" &&
|
|
1026
|
+
!field.options?.some((o) => o.value === field.default)
|
|
1027
|
+
) {
|
|
1028
|
+
contract(`${fieldPath}.default`, "The default must be one of the options");
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
if (field.defaultSetting !== undefined && !m.settings[field.defaultSetting]) {
|
|
1032
|
+
contract(
|
|
1033
|
+
`${fieldPath}.defaultSetting`,
|
|
1034
|
+
`"${field.defaultSetting}" is not one of this extension's settings`,
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
if (object.owner !== undefined) {
|
|
1039
|
+
const owner = object.fields[object.owner];
|
|
1040
|
+
if (!owner) contract(`${path}.owner`, `owner "${object.owner}" is not a field of ${name}`);
|
|
1041
|
+
else if (owner.type !== "user") contract(`${path}.owner`, "owner must name a user field");
|
|
1042
|
+
}
|
|
1043
|
+
if (object.customer !== undefined) {
|
|
1044
|
+
const c = object.customer;
|
|
1045
|
+
const whose = object.fields[c.field];
|
|
1046
|
+
if (!whose) {
|
|
1047
|
+
contract(`${path}.customer.field`, `customer field "${c.field}" is not a field of ${name}`);
|
|
1048
|
+
} else if (whose.type !== "relation" || whose.to !== "person" || whose.many) {
|
|
1049
|
+
contract(`${path}.customer.field`, "customer.field must be a relation to one person");
|
|
1050
|
+
}
|
|
1051
|
+
for (const key of ["read", "write"] as const) {
|
|
1052
|
+
if (new Set(c[key]).size !== c[key].length) {
|
|
1053
|
+
contract(`${path}.customer.${key}`, `customer.${key} lists a field twice`);
|
|
1054
|
+
}
|
|
1055
|
+
for (const fieldName of c[key]) {
|
|
1056
|
+
if (!object.fields[fieldName]) {
|
|
1057
|
+
contract(
|
|
1058
|
+
`${path}.customer.${key}`,
|
|
1059
|
+
`customer.${key} lists "${fieldName}", which is not a field of ${name}`,
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
for (const fieldName of c.write) {
|
|
1065
|
+
if (fieldName === c.field) {
|
|
1066
|
+
contract(`${path}.customer.write`, "customer.write may not list the customer field");
|
|
1067
|
+
}
|
|
1068
|
+
if (!c.read.includes(fieldName)) {
|
|
1069
|
+
contract(
|
|
1070
|
+
`${path}.customer.write`,
|
|
1071
|
+
`customer.write lists "${fieldName}", which customer.read does not`,
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// What the public may read (ADR 0438): fields of the object's own, and a condition on a
|
|
1079
|
+
// state of the record or of one record of this extension it points to.
|
|
1080
|
+
// What every signed-in customer may read (ADR 0447) has the same shape and the same checks.
|
|
1081
|
+
const readDeclarations = Object.entries(m.objects).flatMap(([name, object]) =>
|
|
1082
|
+
(["public", "signedIn"] as const).flatMap((key) => {
|
|
1083
|
+
const declared = object[key];
|
|
1084
|
+
return declared ? [{ name, object, key, p: declared }] : [];
|
|
1085
|
+
}),
|
|
1086
|
+
);
|
|
1087
|
+
for (const { name, object, key, p } of readDeclarations) {
|
|
1088
|
+
const path = `objects.${name}.${key}`;
|
|
1089
|
+
if (key === "public" && object.customer) {
|
|
1090
|
+
contract(path, "An object declares customer or public, not both");
|
|
1091
|
+
}
|
|
1092
|
+
if (key === "signedIn" && object.public) {
|
|
1093
|
+
contract(
|
|
1094
|
+
path,
|
|
1095
|
+
"An object declares public or signedIn, not both: the public's is read by customers too",
|
|
1096
|
+
);
|
|
1097
|
+
}
|
|
1098
|
+
if (new Set(p.read).size !== p.read.length)
|
|
1099
|
+
contract(`${path}.read`, `${key}.read lists a field twice`);
|
|
1100
|
+
for (const fieldName of p.read) {
|
|
1101
|
+
const hostField =
|
|
1102
|
+
key === "public" &&
|
|
1103
|
+
object.publishable !== undefined &&
|
|
1104
|
+
(fieldName === "publishedAt" || fieldName === "publishedVersion");
|
|
1105
|
+
if (!object.fields[fieldName] && !hostField) {
|
|
1106
|
+
contract(
|
|
1107
|
+
`${path}.read`,
|
|
1108
|
+
`${key}.read lists "${fieldName}", which is not a field of ${name}`,
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
if (key === "signedIn" && object.customer) {
|
|
1113
|
+
// Shared records are read with the fields a customer reads of their own, and no other.
|
|
1114
|
+
for (const fieldName of p.read) {
|
|
1115
|
+
if (!object.customer.read.includes(fieldName)) {
|
|
1116
|
+
contract(
|
|
1117
|
+
`${path}.read`,
|
|
1118
|
+
`signedIn.read lists "${fieldName}", which customer.read does not: a shared record shows no more than one's own`,
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
if (!p.when) continue;
|
|
1124
|
+
let target = object;
|
|
1125
|
+
let targetName = name;
|
|
1126
|
+
if (p.when.via !== undefined) {
|
|
1127
|
+
const via = object.fields[p.when.via];
|
|
1128
|
+
const local =
|
|
1129
|
+
via?.type === "relation" && !via.many && via.to?.startsWith(`${m.id}.`)
|
|
1130
|
+
? via.to.slice(m.id.length + 1)
|
|
1131
|
+
: null;
|
|
1132
|
+
const pointed = local ? m.objects[local] : undefined;
|
|
1133
|
+
if (!pointed || !local) {
|
|
1134
|
+
contract(
|
|
1135
|
+
`${path}.when.via`,
|
|
1136
|
+
`${key}.when.via must name a relation of ${name} to one record of another of this extension's objects`,
|
|
1137
|
+
);
|
|
1138
|
+
continue;
|
|
1139
|
+
}
|
|
1140
|
+
target = pointed;
|
|
1141
|
+
targetName = local;
|
|
1142
|
+
}
|
|
1143
|
+
const field = target.fields[p.when.field];
|
|
1144
|
+
if (!field || field.type !== "select") {
|
|
1145
|
+
contract(`${path}.when.field`, `${key}.when.field must name a select field of ${targetName}`);
|
|
1146
|
+
continue;
|
|
1147
|
+
}
|
|
1148
|
+
const values = new Set((field.options ?? []).map((o) => (typeof o === "string" ? o : o.value)));
|
|
1149
|
+
for (const value of p.when.in) {
|
|
1150
|
+
if (!values.has(value)) {
|
|
1151
|
+
contract(`${path}.when.in`, `"${value}" is not an option of ${targetName}.${p.when.field}`);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// Versions (ADR 0446): the content a version keeps is fields of the object's own that a person
|
|
1157
|
+
// edits, and the host's two published fields are not taken.
|
|
1158
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1159
|
+
const p = object.publishable;
|
|
1160
|
+
if (!p) continue;
|
|
1161
|
+
const path = `objects.${name}.publishable`;
|
|
1162
|
+
if (new Set(p.content).size !== p.content.length) {
|
|
1163
|
+
contract(`${path}.content`, "publishable.content lists a field twice");
|
|
1164
|
+
}
|
|
1165
|
+
for (const fieldName of p.content) {
|
|
1166
|
+
const field = object.fields[fieldName];
|
|
1167
|
+
if (!field) {
|
|
1168
|
+
contract(`${path}.content`, `"${fieldName}" is not a field of ${name}`);
|
|
1169
|
+
} else if (field.type === "formula" || field.readOnly) {
|
|
1170
|
+
contract(
|
|
1171
|
+
`${path}.content`,
|
|
1172
|
+
`"${fieldName}" is ${field.type === "formula" ? "a formula" : "read-only"}: content is what staff edit`,
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
// The address and the date a reader is shown (stage 28, ADR 0441, ADR 0442).
|
|
1177
|
+
if (p.slug !== undefined) {
|
|
1178
|
+
const slug = object.fields[p.slug];
|
|
1179
|
+
if (slug?.type !== "slug") {
|
|
1180
|
+
contract(`${path}.slug`, `publishable.slug must name a slug field of ${name}`);
|
|
1181
|
+
} else if (p.content.includes(p.slug)) {
|
|
1182
|
+
contract(
|
|
1183
|
+
`${path}.slug`,
|
|
1184
|
+
"The slug is the record's address, not content: leave it out of content",
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (p.date !== undefined) {
|
|
1189
|
+
const date = object.fields[p.date];
|
|
1190
|
+
if (!date || (date.type !== "date" && date.type !== "datetime")) {
|
|
1191
|
+
contract(`${path}.date`, `publishable.date must name a date or datetime field of ${name}`);
|
|
1192
|
+
} else if (!p.content.includes(p.date)) {
|
|
1193
|
+
contract(`${path}.date`, "publishable.date is content: list it in content too");
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
if (object.public) {
|
|
1197
|
+
if (p.slug === undefined) {
|
|
1198
|
+
contract(`${path}.slug`, "A publishable object the public reads names its slug field");
|
|
1199
|
+
}
|
|
1200
|
+
// The public reads the published version (ADR 0442): its content, its address and when it
|
|
1201
|
+
// went live, and fields the host keeps on the working copy that are not computed from it.
|
|
1202
|
+
for (const fieldName of object.public.read) {
|
|
1203
|
+
const field = object.fields[fieldName];
|
|
1204
|
+
if (field?.type === "formula") {
|
|
1205
|
+
contract(
|
|
1206
|
+
`objects.${name}.public.read`,
|
|
1207
|
+
`public.read lists "${fieldName}", a formula: on a publishable object a formula reads the working copy`,
|
|
1208
|
+
);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
if (object.customer || object.customerVia) {
|
|
1213
|
+
contract(
|
|
1214
|
+
path,
|
|
1215
|
+
"A publishable object declares no customer or customerVia: what outsiders read of it is its published version",
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
for (const taken of PUBLISHED_FIELD_NAMES) {
|
|
1219
|
+
const clash =
|
|
1220
|
+
Object.hasOwn(object.fields, taken) ||
|
|
1221
|
+
Object.values(object.fields).some((f) => f.paths && taken in f.paths);
|
|
1222
|
+
if (clash) {
|
|
1223
|
+
contract(
|
|
1224
|
+
`objects.${name}.fields.${taken}`,
|
|
1225
|
+
`"${taken}" is the host's on a publishable object`,
|
|
1226
|
+
);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// What a customer reads of a record their own record points to (ADR 0439): fields of this
|
|
1232
|
+
// object, through a relation of another of this extension's objects that declares a customer.
|
|
1233
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1234
|
+
const v = object.customerVia;
|
|
1235
|
+
if (!v) continue;
|
|
1236
|
+
const path = `objects.${name}.customerVia`;
|
|
1237
|
+
if (object.customer) {
|
|
1238
|
+
contract(path, "An object that declares a customer cannot also declare customerVia");
|
|
1239
|
+
}
|
|
1240
|
+
if (new Set(v.read).size !== v.read.length) {
|
|
1241
|
+
contract(`${path}.read`, "customerVia.read lists a field twice");
|
|
1242
|
+
}
|
|
1243
|
+
for (const fieldName of v.read) {
|
|
1244
|
+
if (!object.fields[fieldName]) {
|
|
1245
|
+
contract(
|
|
1246
|
+
`${path}.read`,
|
|
1247
|
+
`customerVia.read lists "${fieldName}", which is not a field of ${name}`,
|
|
1248
|
+
);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
const local = v.from.startsWith(`${m.id}.`) ? v.from.slice(m.id.length + 1) : null;
|
|
1252
|
+
const from = local ? m.objects[local] : undefined;
|
|
1253
|
+
if (!from || local === name) {
|
|
1254
|
+
contract(
|
|
1255
|
+
`${path}.from`,
|
|
1256
|
+
`customerVia.from must name another of this extension's objects, as ${m.id}.<object>`,
|
|
1257
|
+
);
|
|
1258
|
+
continue;
|
|
1259
|
+
}
|
|
1260
|
+
if (!from.customer) {
|
|
1261
|
+
contract(`${path}.from`, "customerVia.from must name an object that declares a customer");
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
const pointer = from.fields[v.field];
|
|
1265
|
+
if (pointer?.type !== "relation" || pointer.many || pointer.to !== `${m.id}.${name}`) {
|
|
1266
|
+
contract(
|
|
1267
|
+
`${path}.field`,
|
|
1268
|
+
`customerVia.field must name a relation of ${local} to one ${name}`,
|
|
1269
|
+
);
|
|
1270
|
+
} else if (!from.customer.read.includes(v.field)) {
|
|
1271
|
+
contract(
|
|
1272
|
+
`${path}.field`,
|
|
1273
|
+
`customerVia.field "${v.field}" must be one ${local}'s customer may read (customer.read)`,
|
|
1274
|
+
);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
// The extensions this one needs installed first (stage 42): never itself, each once.
|
|
1279
|
+
for (const [i, required] of (m.requires ?? []).entries()) {
|
|
1280
|
+
if (required === m.id) contract(`requires.${i}`, "An extension does not require itself");
|
|
1281
|
+
if ((m.requires ?? []).indexOf(required) !== i) {
|
|
1282
|
+
contract(`requires.${i}`, `"${required}" is listed twice`);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
// Fields a customer sets only when they make the record (ADR 0507): each one of its write list.
|
|
1287
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1288
|
+
for (const fieldName of object.customer?.createOnly ?? []) {
|
|
1289
|
+
if (!object.customer?.write.includes(fieldName)) {
|
|
1290
|
+
contract(
|
|
1291
|
+
`objects.${name}.customer.createOnly`,
|
|
1292
|
+
`"${fieldName}" must be one the customer writes (customer.write)`,
|
|
1293
|
+
);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// A customer's write that needs access to what it points to (stage 42, ADR 0478): relations of
|
|
1299
|
+
// the object to one record of another of this extension's objects that declares `access`.
|
|
1300
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1301
|
+
for (const fieldName of object.customer?.requireAccess ?? []) {
|
|
1302
|
+
const path = `objects.${name}.customer.requireAccess`;
|
|
1303
|
+
const field = object.fields[fieldName];
|
|
1304
|
+
const local = field?.to?.startsWith(`${m.id}.`) ? field.to.slice(m.id.length + 1) : null;
|
|
1305
|
+
const target = local ? m.objects[local] : undefined;
|
|
1306
|
+
if (field?.type !== "relation" || field.many || !target) {
|
|
1307
|
+
contract(
|
|
1308
|
+
path,
|
|
1309
|
+
`"${fieldName}" must be a relation of ${name} to one of this extension's objects`,
|
|
1310
|
+
);
|
|
1311
|
+
} else if (!target.access) {
|
|
1312
|
+
contract(path, `"${fieldName}" points to ${local}, which declares no access`);
|
|
1313
|
+
} else if (!object.customer?.read.includes(fieldName)) {
|
|
1314
|
+
contract(path, `"${fieldName}" must be one the customer reads (customer.read)`);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
// Who holds access to a record (ADR 0477): sources of this extension's own records. A kind the
|
|
1320
|
+
// host provides is checked by the host, which knows its kinds; the subscription kind's options
|
|
1321
|
+
// (stage 32a, ADR 0488) are checked here too, as they name this extension's own plan object.
|
|
1322
|
+
for (const [name, object] of Object.entries(m.objects)) {
|
|
1323
|
+
const a = object.access;
|
|
1324
|
+
if (!a) continue;
|
|
1325
|
+
a.sources.forEach((given, i) => {
|
|
1326
|
+
const path = `objects.${name}.access.sources.${i}`;
|
|
1327
|
+
if ("kind" in given && given.kind === "subscription") {
|
|
1328
|
+
const options = (given.options ?? {}) as Record<string, unknown>;
|
|
1329
|
+
for (const key of Object.keys(options)) {
|
|
1330
|
+
if (key !== "plans" && key !== "covers") {
|
|
1331
|
+
contract(`${path}.options.${key}`, `a subscription source has no option "${key}"`);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
if (options.covers !== "all") {
|
|
1335
|
+
contract(`${path}.options.covers`, 'covers must be "all": every live record');
|
|
1336
|
+
}
|
|
1337
|
+
const plans = typeof options.plans === "string" ? options.plans : "";
|
|
1338
|
+
const planLocal = plans.startsWith(`${m.id}.`) ? plans.slice(m.id.length + 1) : null;
|
|
1339
|
+
const plan = planLocal && planLocal !== name ? m.objects[planLocal] : undefined;
|
|
1340
|
+
if (!plan) {
|
|
1341
|
+
contract(
|
|
1342
|
+
`${path}.options.plans`,
|
|
1343
|
+
`plans must name another of this extension's objects, as ${m.id}.<object>`,
|
|
1344
|
+
);
|
|
1345
|
+
} else if (!plan.sellable?.recurring) {
|
|
1346
|
+
contract(
|
|
1347
|
+
`${path}.options.plans`,
|
|
1348
|
+
"plans must name a plan: its sellable declares recurring",
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
if (!("via" in given)) return;
|
|
1354
|
+
const source = given;
|
|
1355
|
+
const local = source.via.startsWith(`${m.id}.`) ? source.via.slice(m.id.length + 1) : null;
|
|
1356
|
+
const via = local ? m.objects[local] : undefined;
|
|
1357
|
+
if (!via || local === name) {
|
|
1358
|
+
contract(
|
|
1359
|
+
`${path}.via`,
|
|
1360
|
+
`via must name another of this extension's objects, as ${m.id}.<object>`,
|
|
1361
|
+
);
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
if (!via.customer) {
|
|
1365
|
+
contract(`${path}.via`, "via must name an object that declares a customer");
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
const pointer = via.fields[source.field];
|
|
1369
|
+
if (pointer?.type !== "relation" || pointer.many || !pointer.to) {
|
|
1370
|
+
contract(`${path}.field`, `field must name a relation of ${local} to one record`);
|
|
1371
|
+
return;
|
|
1372
|
+
}
|
|
1373
|
+
if (source.through === undefined) {
|
|
1374
|
+
if (pointer.to !== `${m.id}.${name}`) {
|
|
1375
|
+
contract(`${path}.field`, `field must be a relation of ${local} to one ${name}`);
|
|
1376
|
+
}
|
|
1377
|
+
} else {
|
|
1378
|
+
const targetLocal = pointer.to.startsWith(`${m.id}.`)
|
|
1379
|
+
? pointer.to.slice(m.id.length + 1)
|
|
1380
|
+
: null;
|
|
1381
|
+
const target = targetLocal ? m.objects[targetLocal] : undefined;
|
|
1382
|
+
const list = target?.fields[source.through];
|
|
1383
|
+
const lists =
|
|
1384
|
+
(list?.type === "relation" && list.many && list.to === `${m.id}.${name}`) ||
|
|
1385
|
+
list?.type === "document";
|
|
1386
|
+
if (!target || !lists) {
|
|
1387
|
+
contract(
|
|
1388
|
+
`${path}.through`,
|
|
1389
|
+
`through must name a many relation to ${name}, or a document whose refs name it, on the object field points to`,
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
if (via.states) {
|
|
1394
|
+
const stateField = via.fields[via.states.field];
|
|
1395
|
+
const known = new Set((stateField?.options ?? []).map((o) => o.value));
|
|
1396
|
+
// What a refund moves a grant of `via` to: never a state that gives access.
|
|
1397
|
+
const voids = new Set(
|
|
1398
|
+
Object.values(m.objects).flatMap((o) => {
|
|
1399
|
+
const g = o.sellable?.grants;
|
|
1400
|
+
return g && (g.object === local || g.object === source.via) ? [g.void] : [];
|
|
1401
|
+
}),
|
|
1402
|
+
);
|
|
1403
|
+
if (!source.states?.length) {
|
|
1404
|
+
contract(`${path}.states`, `${local} has states: name the ones that give access`);
|
|
1405
|
+
} else {
|
|
1406
|
+
for (const st of source.states) {
|
|
1407
|
+
if (!known.has(st)) contract(`${path}.states`, `"${st}" is not a state of ${local}`);
|
|
1408
|
+
if (voids.has(st)) {
|
|
1409
|
+
contract(
|
|
1410
|
+
`${path}.states`,
|
|
1411
|
+
`"${st}" is where a refund moves ${local}: it gives no access`,
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
} else if (source.states !== undefined) {
|
|
1417
|
+
contract(`${path}.states`, `${local} has no states`);
|
|
1418
|
+
}
|
|
1419
|
+
if (source.until !== undefined) {
|
|
1420
|
+
const until = via.fields[source.until];
|
|
1421
|
+
if (until?.type !== "date" && until?.type !== "datetime") {
|
|
1422
|
+
contract(`${path}.until`, `until must name a date or datetime field of ${local}`);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
for (const [target, fields] of Object.entries(m.fields)) {
|
|
1429
|
+
const path = `fields.${target}`;
|
|
1430
|
+
if (!known.has(target)) {
|
|
1431
|
+
contract(path, `Unknown object "${target}". Use a core object or <extension>.<object>`);
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
if (target.startsWith(`${m.id}.`)) {
|
|
1435
|
+
contract(path, "Declare fields of your own objects in objects.<name>.fields");
|
|
1436
|
+
continue;
|
|
1437
|
+
}
|
|
1438
|
+
for (const [fieldName, field] of Object.entries(fields)) {
|
|
1439
|
+
if (field.default !== undefined || field.defaultSetting !== undefined) {
|
|
1440
|
+
contract(`${path}.${fieldName}`, "Only fields of your own objects take a default");
|
|
1441
|
+
}
|
|
1442
|
+
if (field.type === "slug") {
|
|
1443
|
+
// A slug is unique in its object's own table (ADR 0442): an object's address is its own.
|
|
1444
|
+
contract(`${path}.${fieldName}`, "Only fields of your own objects can be a slug");
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
const base = options.baseFields?.(target) ?? coreFormulaRefs(target);
|
|
1448
|
+
const env = { ...base, ...formulaFields(fields) };
|
|
1449
|
+
checkFields(path, target, fields, env);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
for (const [name, tool] of Object.entries(m.tools)) {
|
|
1453
|
+
if (!TOOL_NAME_RE.test(name)) {
|
|
1454
|
+
contract(`tools.${name}`, `Tool name "${name}" must be camelCase letters and digits`);
|
|
1455
|
+
}
|
|
1456
|
+
if (tool.inputSchema.type !== "object") {
|
|
1457
|
+
contract(`tools.${name}.input`, "A tool's input must be a z.object(...)");
|
|
1458
|
+
}
|
|
1459
|
+
const open = (tool.audiences ?? []).filter((audience) => audience !== "staff");
|
|
1460
|
+
if (tool.audiences && new Set(tool.audiences).size !== tool.audiences.length) {
|
|
1461
|
+
contract(`tools.${name}.audiences`, "audiences lists one twice");
|
|
1462
|
+
}
|
|
1463
|
+
if (tool.humanOnly && open.length) {
|
|
1464
|
+
contract(
|
|
1465
|
+
`tools.${name}.humanOnly`,
|
|
1466
|
+
`A tool only a person on the staff may call cannot be open to ${open.join(" and ")} callers`,
|
|
1467
|
+
);
|
|
1468
|
+
}
|
|
1469
|
+
if (open.length && tool.risk !== "read" && tool.risk !== "write") {
|
|
1470
|
+
contract(
|
|
1471
|
+
`tools.${name}.risk`,
|
|
1472
|
+
`A tool open to ${open.join(" and ")} callers must have risk read or write, not ${tool.risk}`,
|
|
1473
|
+
);
|
|
1474
|
+
}
|
|
1475
|
+
if (tool.customerFields) {
|
|
1476
|
+
if (!tool.audiences?.includes("customer")) {
|
|
1477
|
+
contract(
|
|
1478
|
+
`tools.${name}.customerFields`,
|
|
1479
|
+
"customerFields is only for a tool open to customer",
|
|
1480
|
+
);
|
|
1481
|
+
}
|
|
1482
|
+
for (const [objectName, fieldNames] of Object.entries(tool.customerFields)) {
|
|
1483
|
+
const local = objectName.startsWith(`${m.id}.`) ? objectName.slice(m.id.length + 1) : null;
|
|
1484
|
+
const own = local ? m.objects[local] : undefined;
|
|
1485
|
+
if (!own) {
|
|
1486
|
+
contract(
|
|
1487
|
+
`tools.${name}.customerFields.${objectName}`,
|
|
1488
|
+
`customerFields names "${objectName}", which is not an object of this extension`,
|
|
1489
|
+
);
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
if (!own.customer) {
|
|
1493
|
+
contract(
|
|
1494
|
+
`tools.${name}.customerFields.${objectName}`,
|
|
1495
|
+
`${objectName} declares no customer, so a customer has no record of it to change`,
|
|
1496
|
+
);
|
|
1497
|
+
continue;
|
|
1498
|
+
}
|
|
1499
|
+
for (const fieldName of fieldNames) {
|
|
1500
|
+
if (fieldName === "id" || fieldName === own.customer.field) {
|
|
1501
|
+
contract(
|
|
1502
|
+
`tools.${name}.customerFields.${objectName}`,
|
|
1503
|
+
`A tool may never set "${fieldName}": whose record it is, and its id, are the host's`,
|
|
1504
|
+
);
|
|
1505
|
+
} else if (!own.fields[fieldName]) {
|
|
1506
|
+
contract(
|
|
1507
|
+
`tools.${name}.customerFields.${objectName}`,
|
|
1508
|
+
`"${fieldName}" is not a field of ${objectName}`,
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
if (tool.permission !== undefined) {
|
|
1515
|
+
const match = EXTENSION_PERMISSION_RE.exec(tool.permission);
|
|
1516
|
+
if (!match || match[1] !== m.id) {
|
|
1517
|
+
contract(
|
|
1518
|
+
`tools.${name}.permission`,
|
|
1519
|
+
`A tool's permission is named ${m.id}:<word> (camelCase), not "${tool.permission}"`,
|
|
1520
|
+
);
|
|
1521
|
+
}
|
|
1522
|
+
if (tool.audiences && !tool.audiences.includes("staff")) {
|
|
1523
|
+
contract(
|
|
1524
|
+
`tools.${name}.permission`,
|
|
1525
|
+
"A permission is for staff callers; this tool is not open to staff",
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
if (tool.recordAction) {
|
|
1530
|
+
const action = tool.recordAction;
|
|
1531
|
+
if (!known.has(action.object)) {
|
|
1532
|
+
contract(`tools.${name}.recordAction.object`, `Unknown object "${action.object}"`);
|
|
1533
|
+
}
|
|
1534
|
+
const properties = (tool.inputSchema.properties ?? {}) as Record<string, { type?: unknown }>;
|
|
1535
|
+
if (properties[action.idInput]?.type !== "string") {
|
|
1536
|
+
contract(
|
|
1537
|
+
`tools.${name}.recordAction.idInput`,
|
|
1538
|
+
`idInput "${action.idInput}" must be a string field of the tool's input`,
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
if (action.ask && !properties[action.ask.input]) {
|
|
1542
|
+
contract(
|
|
1543
|
+
`tools.${name}.recordAction.ask`,
|
|
1544
|
+
`"${action.ask.input}" is not a field of the tool's input`,
|
|
1545
|
+
);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// Grantable permissions (ADR 0449): permissions this extension's tools declare, for staff.
|
|
1551
|
+
(m.grantable ?? []).forEach((permission, i) => {
|
|
1552
|
+
const tool = Object.values(m.tools).find((t) => t.permission === permission);
|
|
1553
|
+
if (!tool) {
|
|
1554
|
+
contract(
|
|
1555
|
+
`grantable.${i}`,
|
|
1556
|
+
`"${permission}" is not the permission of any of this extension's tools (tools.<name>.permission)`,
|
|
1557
|
+
);
|
|
1558
|
+
}
|
|
1559
|
+
});
|
|
1560
|
+
|
|
1561
|
+
// Roles (ADR 0436): each holds permissions this extension's tools declare and reads objects
|
|
1562
|
+
// of its own, and nothing else, so a role can never reach past the extension that declares it.
|
|
1563
|
+
const declaredPermissions = new Set(
|
|
1564
|
+
Object.values(m.tools).flatMap((tool) => (tool.permission ? [tool.permission] : [])),
|
|
1565
|
+
);
|
|
1566
|
+
for (const [name, role] of Object.entries(m.roles)) {
|
|
1567
|
+
const path = `roles.${name}`;
|
|
1568
|
+
if (!FIELD_NAME_RE.test(name)) contract(path, `Role name "${name}" must be camelCase`);
|
|
1569
|
+
if (!role.permissions.length && !role.read.length) {
|
|
1570
|
+
contract(path, "A role holds at least one permission or reads at least one object");
|
|
1571
|
+
}
|
|
1572
|
+
for (const permission of role.permissions) {
|
|
1573
|
+
if (!declaredPermissions.has(permission)) {
|
|
1574
|
+
contract(
|
|
1575
|
+
`${path}.permissions`,
|
|
1576
|
+
`"${permission}" is not the permission of any of this extension's tools (tools.<name>.permission)`,
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
for (const object of role.read) {
|
|
1581
|
+
const local = object.startsWith(`${m.id}.`) ? object.slice(m.id.length + 1) : null;
|
|
1582
|
+
if (!local || !m.objects[local]) {
|
|
1583
|
+
contract(
|
|
1584
|
+
`${path}.read`,
|
|
1585
|
+
`"${object}" is not an object of this extension: a role reads ${m.id}.<object> only`,
|
|
1586
|
+
);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
for (const [name, view] of Object.entries(m.views)) {
|
|
1592
|
+
if (!FIELD_NAME_RE.test(name)) contract(`views.${name}`, "View names must be camelCase");
|
|
1593
|
+
if (!known.has(view.object))
|
|
1594
|
+
contract(`views.${name}.object`, `Unknown object "${view.object}"`);
|
|
1595
|
+
if (view.type === "board" && !view.groupBy) {
|
|
1596
|
+
contract(`views.${name}.groupBy`, "board views need groupBy");
|
|
1597
|
+
}
|
|
1598
|
+
if (view.type === "calendar" && !view.dateField) {
|
|
1599
|
+
contract(`views.${name}.dateField`, "calendar views need dateField");
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
const homes = Object.entries(m.dashboards).filter(([, d]) => d.home);
|
|
1604
|
+
for (const [name] of Object.entries(m.dashboards)) {
|
|
1605
|
+
if (!FIELD_NAME_RE.test(name))
|
|
1606
|
+
contract(`dashboards.${name}`, "Dashboard names must be camelCase");
|
|
1607
|
+
}
|
|
1608
|
+
if (homes.length > 1) contract("dashboards", "At most one dashboard can be the default Home");
|
|
1609
|
+
|
|
1610
|
+
for (const [kind, entries] of [
|
|
1611
|
+
["recordTabs", m.recordTabs],
|
|
1612
|
+
["recordPanels", m.recordPanels],
|
|
1613
|
+
] as const) {
|
|
1614
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
1615
|
+
const path = `${kind}.${name}`;
|
|
1616
|
+
if (!FIELD_NAME_RE.test(name)) contract(path, `${kind} names must be camelCase`);
|
|
1617
|
+
if (!known.has(entry.on)) contract(`${path}.on`, `Unknown object "${entry.on}"`);
|
|
1618
|
+
if (!known.has(entry.view.object)) {
|
|
1619
|
+
contract(`${path}.view.object`, `Unknown object "${entry.view.object}"`);
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
// The extension's own objects can be checked here; the host checks the rest.
|
|
1623
|
+
const own = entry.view.object.startsWith(`${m.id}.`)
|
|
1624
|
+
? m.objects[entry.view.object.slice(m.id.length + 1)]
|
|
1625
|
+
: undefined;
|
|
1626
|
+
if (own) {
|
|
1627
|
+
const relation = own.fields[entry.view.relation];
|
|
1628
|
+
if (!relation) {
|
|
1629
|
+
contract(
|
|
1630
|
+
`${path}.view.relation`,
|
|
1631
|
+
`${entry.view.object} has no field "${entry.view.relation}"`,
|
|
1632
|
+
);
|
|
1633
|
+
} else if (relation.type !== "relation" || relation.to !== entry.on) {
|
|
1634
|
+
contract(
|
|
1635
|
+
`${path}.view.relation`,
|
|
1636
|
+
`${entry.view.object}.${entry.view.relation} must be a relation to ${entry.on}`,
|
|
1637
|
+
);
|
|
1638
|
+
}
|
|
1639
|
+
for (const key of ["groupBy", "sumField", "dateField", "imageField"] as const) {
|
|
1640
|
+
const field = entry.view[key];
|
|
1641
|
+
if (field && !own.fields[field]) {
|
|
1642
|
+
contract(`${path}.view.${key}`, `${entry.view.object} has no field "${field}"`);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
if (entry.view.type === "board" && !entry.view.groupBy) {
|
|
1647
|
+
contract(`${path}.view.groupBy`, "board views need groupBy");
|
|
1648
|
+
}
|
|
1649
|
+
if (entry.view.type === "calendar" && !entry.view.dateField) {
|
|
1650
|
+
contract(`${path}.view.dateField`, "calendar views need dateField");
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
for (const [name, setting] of Object.entries(m.settings)) {
|
|
1656
|
+
const path = `settings.${name}`;
|
|
1657
|
+
if (!FIELD_NAME_RE.test(name)) contract(path, "Setting names must be camelCase");
|
|
1658
|
+
if (setting.type === "select") {
|
|
1659
|
+
if (!setting.options?.length) contract(`${path}.options`, "select settings need options");
|
|
1660
|
+
if (
|
|
1661
|
+
setting.default !== undefined &&
|
|
1662
|
+
!setting.options?.some((o) => o.value === setting.default)
|
|
1663
|
+
) {
|
|
1664
|
+
contract(`${path}.default`, "The default must be one of the options");
|
|
1665
|
+
}
|
|
1666
|
+
} else if (setting.options) {
|
|
1667
|
+
contract(`${path}.options`, "Only select settings take options");
|
|
1668
|
+
}
|
|
1669
|
+
const expected = { text: "string", select: "string", number: "number", boolean: "boolean" };
|
|
1670
|
+
if (setting.default !== undefined && typeof setting.default !== expected[setting.type]) {
|
|
1671
|
+
contract(`${path}.default`, `The default must be a ${expected[setting.type]}`);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
for (const [name, automation] of Object.entries(m.automations)) {
|
|
1676
|
+
checkAutomation(
|
|
1677
|
+
{ manifest: m, known, objectFields: options.objectFields, contract },
|
|
1678
|
+
name,
|
|
1679
|
+
automation,
|
|
1680
|
+
);
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
const perms = m.permissions;
|
|
1684
|
+
for (const [key, list] of [
|
|
1685
|
+
["read", perms.read],
|
|
1686
|
+
["write", perms.write],
|
|
1687
|
+
] as const) {
|
|
1688
|
+
list.forEach((object, i) => {
|
|
1689
|
+
if (!known.has(object)) contract(`permissions.${key}.${i}`, `Unknown object "${object}"`);
|
|
1690
|
+
});
|
|
1691
|
+
}
|
|
1692
|
+
perms.operations.forEach((operation, i) => {
|
|
1693
|
+
if (!OPERATION_RE.test(operation)) {
|
|
1694
|
+
contract(`permissions.operations.${i}`, `"${operation}" is not an operation name`);
|
|
1695
|
+
} else if (FORBIDDEN_OPERATION_PREFIXES.some((prefix) => operation.startsWith(prefix))) {
|
|
1696
|
+
contract(`permissions.operations.${i}`, `Extensions cannot be granted ${operation}`);
|
|
1697
|
+
}
|
|
1698
|
+
});
|
|
1699
|
+
const usesOps = perms.read.length > 0 || perms.write.length > 0 || perms.operations.length > 0;
|
|
1700
|
+
if (usesOps && !perms.modules.includes("ops")) {
|
|
1701
|
+
contract("permissions.modules", 'Object or operation permissions need the "ops" module');
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
return { manifest: m, issues };
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
export function declaredModules(manifest: ExtensionManifest): HostModuleName[] {
|
|
1708
|
+
return manifest.permissions.modules;
|
|
1709
|
+
}
|