@composable-os/cli 0.0.0-stage → 0.1.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/blocks-HVTAQ5W2.js +233 -0
  4. package/dist/chunk-2ABSVMGB.js +13 -0
  5. package/dist/chunk-2P4AZTFL.js +358 -0
  6. package/dist/chunk-2S7375SC.js +802 -0
  7. package/dist/chunk-5KSXSK7Y.js +47 -0
  8. package/dist/chunk-6UWRT4VM.js +220 -0
  9. package/dist/chunk-BEKL3CA2.js +28975 -0
  10. package/dist/chunk-E5JLTLRM.js +50 -0
  11. package/dist/chunk-HDIOVFAZ.js +544 -0
  12. package/dist/chunk-KGHA67HG.js +765 -0
  13. package/dist/chunk-MAXLUQKB.js +1287 -0
  14. package/dist/chunk-MINQ46RU.js +16485 -0
  15. package/dist/chunk-STJZJIRA.js +11378 -0
  16. package/dist/chunk-VL62IMBN.js +1423 -0
  17. package/dist/chunk-X6I2LWB3.js +24319 -0
  18. package/dist/chunk-Y7PQ7NIL.js +1178 -0
  19. package/dist/chunk-ZRWF6QZS.js +2357 -0
  20. package/dist/cli-BRSACHSG.js +31393 -0
  21. package/dist/cli-XHC6QBQW.js +121 -0
  22. package/dist/cli.js +25 -0
  23. package/dist/client-repo-G55G4OLV.js +55 -0
  24. package/dist/create.js +25 -0
  25. package/dist/fields-64BQJA62.js +48 -0
  26. package/dist/invoke-5SR5ZWW7.js +26 -0
  27. package/dist/person-AZFEPZOE.js +58 -0
  28. package/dist/platform/migrations/0001_init.sql +59 -0
  29. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  30. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  31. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  32. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  33. package/dist/platform/migrations/0006_search.sql +223 -0
  34. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  35. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  36. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  37. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  38. package/dist/platform/migrations/0020_extensions.sql +373 -0
  39. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  40. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  41. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  42. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  43. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  44. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  45. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  46. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  47. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  48. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  49. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  50. package/dist/platform/migrations/0110_integrations.sql +103 -0
  51. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  52. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  53. package/dist/platform/migrations/0130_automations.sql +113 -0
  54. package/dist/platform/migrations/0131_notifications.sql +29 -0
  55. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  56. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  57. package/dist/platform/migrations/0160_releases.sql +41 -0
  58. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  59. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  60. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  61. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  62. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  63. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  64. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  65. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  66. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  67. package/dist/platform/migrations/0250_judge.sql +11 -0
  68. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  69. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  70. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  71. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  72. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  73. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  74. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  75. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  76. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  77. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  78. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  79. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  80. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  81. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  82. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  83. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  84. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  85. package/dist/platform/migrations/0365_routines.sql +38 -0
  86. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  87. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  88. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  89. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  90. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  91. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  92. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  93. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  94. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  95. package/dist/platform/migrations/0421_orders.sql +134 -0
  96. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  97. package/dist/platform/migrations/0440_capacity.sql +100 -0
  98. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  99. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  100. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  101. package/dist/platform/migrations/0460_content.sql +59 -0
  102. package/dist/platform/migrations/0461_media.sql +52 -0
  103. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  104. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  105. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  106. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  107. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  108. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  109. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  110. package/dist/platform/sdk/automation-check.ts +534 -0
  111. package/dist/platform/sdk/automation.ts +743 -0
  112. package/dist/platform/sdk/behaviour.ts +215 -0
  113. package/dist/platform/sdk/capacity.ts +112 -0
  114. package/dist/platform/sdk/contract.ts +766 -0
  115. package/dist/platform/sdk/document.ts +788 -0
  116. package/dist/platform/sdk/field-types.ts +106 -0
  117. package/dist/platform/sdk/formula/ast.ts +72 -0
  118. package/dist/platform/sdk/formula/compile.ts +454 -0
  119. package/dist/platform/sdk/formula/index.ts +4 -0
  120. package/dist/platform/sdk/formula/parser.ts +417 -0
  121. package/dist/platform/sdk/index.ts +33 -0
  122. package/dist/platform/sdk/manifest.ts +1484 -0
  123. package/dist/platform/sdk/media.ts +46 -0
  124. package/dist/platform/sdk/permissions.ts +60 -0
  125. package/dist/platform/sdk/renamed.ts +63 -0
  126. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  127. package/dist/platform/sdk/rich-text.ts +173 -0
  128. package/dist/platform/sdk/runtime.ts +410 -0
  129. package/dist/platform/sdk/sellable.ts +396 -0
  130. package/dist/platform/sdk/slug.ts +25 -0
  131. package/dist/platform/sdk/states.ts +162 -0
  132. package/dist/platform/sdk/testing.ts +836 -0
  133. package/dist/products-2H5DR232.js +11 -0
  134. package/dist/run-4JNFLYXV.js +847 -0
  135. package/dist/src-J27WXNLY.js +270 -0
  136. package/dist/src-SSVOBH6L.js +31 -0
  137. package/dist/token-MOJUA2U6.js +70 -0
  138. package/dist/token-util-TENRN4V7.js +6 -0
  139. package/package.json +33 -4
@@ -0,0 +1,46 @@
1
+ // What a picture of the media library may be (stage 28, ADR 0444), as data and pure functions so
2
+ // the host and `cos content import` check one rule: the type is the file's first bytes, never its
3
+ // name or a client's word; only raster images the host recognises (JPEG, PNG, GIF, WebP, AVIF);
4
+ // no SVG, which can carry script; at most 10 MB.
5
+
6
+ /** The types the media library takes. */
7
+ export const MEDIA_TYPES = [
8
+ "image/jpeg",
9
+ "image/png",
10
+ "image/gif",
11
+ "image/webp",
12
+ "image/avif",
13
+ ] as const;
14
+ export type MediaType = (typeof MEDIA_TYPES)[number];
15
+
16
+ /** The most an upload may be, in bytes. */
17
+ export const MEDIA_MAX_BYTES = 10 * 1024 * 1024;
18
+
19
+ const starts = (bytes: Uint8Array, at: number, ...values: number[]) =>
20
+ values.every((v, i) => bytes[at + i] === v);
21
+ const ascii = (bytes: Uint8Array, at: number, text: string) =>
22
+ [...text].every((c, i) => bytes[at + i] === c.charCodeAt(0));
23
+
24
+ /** The type the bytes are, or null when they are not a picture the library takes. */
25
+ export function sniffMediaType(bytes: Uint8Array): MediaType | null {
26
+ if (starts(bytes, 0, 0xff, 0xd8, 0xff)) return "image/jpeg";
27
+ if (starts(bytes, 0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) return "image/png";
28
+ if (ascii(bytes, 0, "GIF87a") || ascii(bytes, 0, "GIF89a")) return "image/gif";
29
+ if (ascii(bytes, 0, "RIFF") && ascii(bytes, 8, "WEBP")) return "image/webp";
30
+ if (ascii(bytes, 4, "ftyp") && (ascii(bytes, 8, "avif") || ascii(bytes, 8, "avis"))) {
31
+ return "image/avif";
32
+ }
33
+ return null;
34
+ }
35
+
36
+ /** Why `bytes` cannot be uploaded, or null when they can. */
37
+ export function mediaProblem(bytes: Uint8Array): string | null {
38
+ if (bytes.byteLength === 0) return "The file is empty";
39
+ if (bytes.byteLength > MEDIA_MAX_BYTES) {
40
+ return `The file is ${bytes.byteLength} bytes; a picture is at most ${MEDIA_MAX_BYTES}`;
41
+ }
42
+ if (!sniffMediaType(bytes)) {
43
+ return "This is not a picture the library takes: JPEG, PNG, GIF, WebP or AVIF";
44
+ }
45
+ return null;
46
+ }
@@ -0,0 +1,60 @@
1
+ // Declared permissions (ADR 0025): what an extension may call through `codemode:ops`. The host
2
+ // enforces this in the invocation pipeline; the testing harness uses the same rules.
3
+ import type { PermissionsManifest } from "./manifest.ts";
4
+ import { FORBIDDEN_OPERATION_PREFIXES } from "./manifest.ts";
5
+
6
+ export interface GrantRequest {
7
+ /** Operation name, e.g. `organisation.get`, `record.create`. */
8
+ operation: string;
9
+ /** The operation's risk class. */
10
+ risk: string;
11
+ /** The object the call reads or writes, when the operation is about one (else null). */
12
+ object: string | null;
13
+ }
14
+
15
+ export type GrantDecision = { ok: true } | { ok: false; reason: string };
16
+
17
+ /** Decides whether extension `id` with `permissions` may make `request`. */
18
+ export function checkGrant(
19
+ id: string,
20
+ permissions: PermissionsManifest,
21
+ request: GrantRequest,
22
+ ): GrantDecision {
23
+ const { operation, risk, object } = request;
24
+ if (FORBIDDEN_OPERATION_PREFIXES.some((prefix) => operation.startsWith(prefix))) {
25
+ return { ok: false, reason: `Extensions cannot call ${operation}` };
26
+ }
27
+ if (operation.startsWith(`x.${id}.`)) {
28
+ return { ok: false, reason: "An extension cannot call its own tools through codemode:ops" };
29
+ }
30
+ if (permissions.operations.includes(operation)) return { ok: true };
31
+ if (operation.startsWith("x.")) {
32
+ return { ok: false, reason: `${operation} is not in permissions.operations` };
33
+ }
34
+ if (!object) {
35
+ return {
36
+ ok: false,
37
+ reason: `${operation} is not in permissions.operations of extension ${id}`,
38
+ };
39
+ }
40
+ if (object.startsWith(`${id}.`)) return { ok: true };
41
+ if (risk === "read") {
42
+ if (permissions.read.includes(object) || permissions.write.includes(object))
43
+ return { ok: true };
44
+ return {
45
+ ok: false,
46
+ reason: `Extension ${id} may not read ${object} (add it to permissions.read)`,
47
+ };
48
+ }
49
+ if (risk === "write" || risk === "bulk-write") {
50
+ if (permissions.write.includes(object)) return { ok: true };
51
+ return {
52
+ ok: false,
53
+ reason: `Extension ${id} may not write ${object} (add it to permissions.write)`,
54
+ };
55
+ }
56
+ return {
57
+ ok: false,
58
+ reason: `${operation} is ${risk}; list it in permissions.operations to allow it`,
59
+ };
60
+ }
@@ -0,0 +1,63 @@
1
+ // The names the extension contract had before stage 20 (ADR 0400), when it still said `crm`.
2
+ //
3
+ // None of them is accepted: no release was installed under them, so there is nothing to keep
4
+ // working. They are listed so that code written against the old contract is refused with a
5
+ // message that names the new form, by the extension build, Code Mode, the manifest check and
6
+ // query.sql. This file is the only place the old names are written.
7
+
8
+ const OLD_PREFIX = "crm";
9
+
10
+ /** The host module extension code calls operations through, and what it was called. */
11
+ export const OPS_MODULE = "ops";
12
+ export const OLD_OPS_MODULE = OLD_PREFIX;
13
+
14
+ /** The module specifier Code Mode scripts import, and what it was. */
15
+ export const OPS_SPECIFIER = `codemode:${OPS_MODULE}`;
16
+ export const OLD_OPS_SPECIFIER = `codemode:${OLD_OPS_MODULE}`;
17
+
18
+ /** The prefix of every read view, and what it was. */
19
+ export const VIEW_PREFIX = "rec_";
20
+ export const OLD_VIEW_PREFIX = `${OLD_PREFIX}_`;
21
+
22
+ /** The core views, which were named in the plural: old suffix → new suffix. */
23
+ const RENAMED_CORE_VIEWS: Readonly<Record<string, string>> = {
24
+ people: "person",
25
+ organisations: "organisation",
26
+ affiliations: "affiliation",
27
+ activities: "activity",
28
+ activity_links: "activity_link",
29
+ tasks: "task",
30
+ task_links: "task_link",
31
+ record_tags: "record_tag",
32
+ users: "user",
33
+ stage_history: "stage_history",
34
+ };
35
+
36
+ /**
37
+ * What a view named under the old contract is called now: `rec_person` for the old people view,
38
+ * `rec_x_deals_deal` for an extension object's. Null when `name` is not an old view name.
39
+ */
40
+ export function renamedView(name: string): string | null {
41
+ const lower = name.toLowerCase();
42
+ if (!lower.startsWith(OLD_VIEW_PREFIX)) return null;
43
+ const suffix = lower.slice(OLD_VIEW_PREFIX.length);
44
+ if (suffix.startsWith("x_")) return `${VIEW_PREFIX}${suffix}`;
45
+ const core = RENAMED_CORE_VIEWS[suffix];
46
+ return core ? `${VIEW_PREFIX}${core}` : null;
47
+ }
48
+
49
+ /** Why a use of an old contract name is refused, naming the new form. Null for any other name. */
50
+ export function renamedMessage(name: string): string | null {
51
+ if (name === OLD_OPS_SPECIFIER) {
52
+ return `"${OLD_OPS_SPECIFIER}" was renamed: import { ${OPS_MODULE} } from "${OPS_SPECIFIER}" and call ${OPS_MODULE}.call(…)`;
53
+ }
54
+ if (name === OLD_OPS_MODULE) {
55
+ return `The "${OLD_OPS_MODULE}" module was renamed "${OPS_MODULE}": declare "${OPS_MODULE}" in permissions.modules and call ctx.${OPS_MODULE}`;
56
+ }
57
+ const view = renamedView(name);
58
+ if (view) return `${name} was renamed ${view}; query the ${VIEW_PREFIX}* views`;
59
+ if (name.toLowerCase().startsWith(OLD_VIEW_PREFIX)) {
60
+ return `${name} is not a view: the ${OLD_VIEW_PREFIX}* views were renamed ${VIEW_PREFIX}*`;
61
+ }
62
+ return null;
63
+ }
@@ -0,0 +1,68 @@
1
+ // One rich text document with every block type and every mark (stage 28, ADR 0440): what the
2
+ // host's tests store and what both renderers (web and native, stage 28 step 6) must draw in full.
3
+ // The picture names a media item by an id of the right shape; the host refuses one that is not a
4
+ // live media item, so a test that stores this replaces it (or leaves the image out).
5
+ /** A media id in the fixture; replace it with a real one before storing. */
6
+ export const RICH_TEXT_FIXTURE_MEDIA = "med_01FIXTURE0000000000000000";
7
+
8
+ export const RICH_TEXT_FIXTURE = {
9
+ blocks: [
10
+ { type: "heading", level: 2, content: [{ type: "text", text: "Summer at the hall" }] },
11
+ {
12
+ type: "paragraph",
13
+ content: [
14
+ { type: "text", text: "Doors open at " },
15
+ { type: "text", text: "seven", bold: true },
16
+ { type: "text", text: ", the bar a little " },
17
+ { type: "text", text: "earlier", italic: true },
18
+ { type: "text", text: ". Bring " },
19
+ { type: "text", text: "ID", code: true },
20
+ { type: "break" },
21
+ { type: "text", text: "Tickets are on the ", bold: true, italic: true },
22
+ { type: "text", text: "events page", href: "/events" },
23
+ { type: "text", text: " or by " },
24
+ { type: "text", text: "email", href: "mailto:hello@example.com" },
25
+ { type: "text", text: "." },
26
+ ],
27
+ },
28
+ {
29
+ type: "list",
30
+ style: "bullet",
31
+ items: [
32
+ { content: [{ type: "text", text: "Friday: open mic" }] },
33
+ {
34
+ content: [{ type: "text", text: "Saturday: two bands" }],
35
+ list: {
36
+ style: "ordered",
37
+ items: [
38
+ { content: [{ type: "text", text: "The first" }] },
39
+ { content: [{ type: "text", text: "The second" }] },
40
+ ],
41
+ },
42
+ },
43
+ ],
44
+ },
45
+ {
46
+ type: "quote",
47
+ content: [{ type: "text", text: "Best night of the year." }],
48
+ attribution: "A regular",
49
+ },
50
+ {
51
+ type: "image",
52
+ media: RICH_TEXT_FIXTURE_MEDIA,
53
+ alt: "The hall from the stage, full",
54
+ caption: [
55
+ { type: "text", text: "Last summer, " },
56
+ { type: "text", text: "sold out", bold: true },
57
+ ],
58
+ },
59
+ { type: "divider" },
60
+ { type: "embed", url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", caption: "The trailer" },
61
+ {
62
+ type: "callout",
63
+ tone: "warning",
64
+ content: [{ type: "text", text: "Under 18s with an adult only." }],
65
+ },
66
+ { type: "button", label: "Get tickets", href: "https://tickets.example.com/summer" },
67
+ ],
68
+ } as const;
@@ -0,0 +1,173 @@
1
+ // The `richText` field type (stage 28, ADR 0440): a document field (ADR 0445) with one fixed
2
+ // schema the platform owns. A post's body is one; a client renders it with its own components,
3
+ // block by block, and never as HTML.
4
+ //
5
+ // body: { type: "richText", label: "Body" }
6
+ //
7
+ // The value is `{ blocks: [...] }`. A block is one of paragraph, heading, list, quote, image,
8
+ // divider, embed, callout and button, told apart by `type`. Text sits in inline runs: a run of
9
+ // text with its marks (bold, italic, code, a link) or a line break. Every text is `noMarkup` and
10
+ // every link is checked by scheme, so no HTML, script or `javascript:` address can be stored, by
11
+ // any actor. A block type, mark or key the schema does not name is refused like any other fault.
12
+ //
13
+ // The schema is data like any declared document schema: the host, `cos ext check`, the SDK's test
14
+ // host and both renderers read this one declaration. Changing it changes what every product
15
+ // stores; a block type added here must be added to both renderers (stage 28, step 6).
16
+ import type { DocumentNode, DocumentObjectNode, DocumentSchema } from "./document.ts";
17
+
18
+ /** The block types, in the order a renderer lists them. */
19
+ export const RICH_TEXT_BLOCKS = [
20
+ "paragraph",
21
+ "heading",
22
+ "list",
23
+ "quote",
24
+ "image",
25
+ "divider",
26
+ "embed",
27
+ "callout",
28
+ "button",
29
+ ] as const;
30
+ export type RichTextBlockType = (typeof RICH_TEXT_BLOCKS)[number];
31
+
32
+ /** The marks a run of text may carry. A link is `href`. */
33
+ export const RICH_TEXT_MARKS = ["bold", "italic", "code", "href"] as const;
34
+
35
+ /** A richText field that declares no `maxBytes`. At most DOCUMENT_MAX_BYTES. */
36
+ export const RICH_TEXT_DEFAULT_BYTES = 131_072;
37
+
38
+ /** The object a picture's `media` names: the media library's (stage 28, step 4). */
39
+ export const RICH_TEXT_MEDIA_OBJECT = "media";
40
+
41
+ const prose = (max: number, extra: Partial<DocumentNode> = {}): DocumentNode =>
42
+ ({ type: "text", max, noMarkup: true, ...extra }) as DocumentNode;
43
+ const optional = (node: DocumentNode): DocumentNode => ({ ...node, optional: true });
44
+ const inlines: DocumentNode = { type: "use", def: "inlines" };
45
+ const object = (fields: Record<string, DocumentNode>): DocumentObjectNode => ({
46
+ type: "object",
47
+ fields,
48
+ });
49
+
50
+ const listShape = (): DocumentObjectNode =>
51
+ object({
52
+ style: { type: "choice", options: ["bullet", "ordered"] },
53
+ items: { type: "array", min: 1, max: 200, of: { type: "use", def: "listItem" } },
54
+ });
55
+
56
+ const blocks: Record<RichTextBlockType, DocumentObjectNode> = {
57
+ paragraph: object({ content: inlines }),
58
+ heading: object({
59
+ level: { type: "number", integer: true, min: 1, max: 6 },
60
+ content: inlines,
61
+ }),
62
+ list: listShape(),
63
+ quote: object({ content: inlines, attribution: optional(prose(300)) }),
64
+ image: object({
65
+ media: { type: "ref", to: RICH_TEXT_MEDIA_OBJECT, description: "The picture, from media" },
66
+ alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
67
+ caption: optional(inlines),
68
+ }),
69
+ divider: object({}),
70
+ embed: object({
71
+ url: {
72
+ type: "text",
73
+ max: 2000,
74
+ href: "web",
75
+ description: "A video or post to show (YouTube, Vimeo): the client decides how",
76
+ },
77
+ caption: optional(prose(500)),
78
+ }),
79
+ callout: object({
80
+ tone: { type: "choice", options: ["info", "note", "success", "warning"] },
81
+ content: inlines,
82
+ }),
83
+ button: object({
84
+ label: prose(80, { min: 1 }),
85
+ href: { type: "text", max: 2000, href: "link" },
86
+ }),
87
+ };
88
+
89
+ /** The one schema of every richText field. */
90
+ export const RICH_TEXT_SCHEMA: DocumentSchema = {
91
+ root: object({
92
+ blocks: {
93
+ type: "array",
94
+ max: 1000,
95
+ of: {
96
+ type: "union",
97
+ tag: "type",
98
+ variants: blocks,
99
+ },
100
+ },
101
+ }),
102
+ defs: {
103
+ inlines: {
104
+ type: "array",
105
+ max: 500,
106
+ of: {
107
+ type: "union",
108
+ tag: "type",
109
+ variants: {
110
+ text: object({
111
+ text: prose(5000, { min: 1 }),
112
+ bold: optional({ type: "boolean" }),
113
+ italic: optional({ type: "boolean" }),
114
+ code: optional({ type: "boolean" }),
115
+ href: optional({ type: "text", max: 2000, href: "link" }),
116
+ }),
117
+ break: object({}),
118
+ },
119
+ },
120
+ },
121
+ listItem: object({ content: inlines, list: optional({ type: "use", def: "list" }) }),
122
+ list: listShape(),
123
+ },
124
+ };
125
+
126
+ /**
127
+ * The `image` field type (stage 28, ADR 0444): a picture from the media library, with its alt
128
+ * text and the point to keep in view when a client crops it (0 to 1 from the left and the top).
129
+ */
130
+ export const IMAGE_SCHEMA: DocumentSchema = {
131
+ root: object({
132
+ media: { type: "ref", to: RICH_TEXT_MEDIA_OBJECT, description: "The picture, from media" },
133
+ alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
134
+ focal: optional(
135
+ object({
136
+ x: { type: "number", min: 0, max: 1 },
137
+ y: { type: "number", min: 0, max: 1 },
138
+ }),
139
+ ),
140
+ }),
141
+ };
142
+
143
+ /** The most an image field's value may be, in bytes of its JSON. */
144
+ export const IMAGE_MAX_BYTES = 2048;
145
+
146
+ /** The field types whose value is a document (ADR 0445): `document`, `richText` and `image`. */
147
+ export function isDocumentType(type: string | undefined): boolean {
148
+ return type === "document" || type === "richText" || type === "image";
149
+ }
150
+
151
+ /**
152
+ * The schema a document-valued field is checked against: its declared one, or richText's. Not a
153
+ * document type: undefined.
154
+ */
155
+ export function documentSchemaOf(field: {
156
+ type?: string | undefined;
157
+ schema?: unknown;
158
+ }): DocumentSchema | undefined {
159
+ if (field.type === "richText") return RICH_TEXT_SCHEMA;
160
+ if (field.type === "image") return IMAGE_SCHEMA;
161
+ if (field.type === "document") return field.schema as DocumentSchema | undefined;
162
+ return undefined;
163
+ }
164
+
165
+ /** The size limit of a document-valued field: its declared one, or its type's default. */
166
+ export function documentMaxBytesOf(field: {
167
+ type?: string | undefined;
168
+ maxBytes?: number | undefined;
169
+ }): number | undefined {
170
+ if (field.type === "image") return IMAGE_MAX_BYTES;
171
+ if (field.maxBytes !== undefined) return field.maxBytes;
172
+ return field.type === "richText" ? RICH_TEXT_DEFAULT_BYTES : undefined;
173
+ }