@composable-os/sdk 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.
- package/LICENSE +109 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +51 -0
- package/dist/capacity.js +68 -0
- package/dist/contract.d.ts +706 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +165 -0
- package/dist/document.js +636 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +93 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +21 -0
- package/dist/manifest.d.ts +1131 -0
- package/dist/manifest.js +1229 -0
- package/dist/media.d.ts +9 -0
- package/dist/media.js +43 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +34 -0
- package/dist/rich-text.js +138 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +103 -0
- package/dist/sellable.js +258 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +64 -0
- package/dist/states.js +101 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +670 -0
- package/package.json +34 -4
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +112 -0
- package/src/contract.ts +766 -0
- package/src/document.ts +788 -0
- package/src/field-types.ts +106 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +33 -0
- package/src/manifest.ts +1484 -0
- package/src/media.ts +46 -0
- package/src/permissions.ts +60 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +173 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +396 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +162 -0
- package/src/testing.ts +836 -0
- package/README.md +0 -3
package/dist/media.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The types the media library takes. */
|
|
2
|
+
export declare const MEDIA_TYPES: readonly ["image/jpeg", "image/png", "image/gif", "image/webp", "image/avif"];
|
|
3
|
+
export type MediaType = (typeof MEDIA_TYPES)[number];
|
|
4
|
+
/** The most an upload may be, in bytes. */
|
|
5
|
+
export declare const MEDIA_MAX_BYTES: number;
|
|
6
|
+
/** The type the bytes are, or null when they are not a picture the library takes. */
|
|
7
|
+
export declare function sniffMediaType(bytes: Uint8Array): MediaType | null;
|
|
8
|
+
/** Why `bytes` cannot be uploaded, or null when they can. */
|
|
9
|
+
export declare function mediaProblem(bytes: Uint8Array): string | null;
|
package/dist/media.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
/** The types the media library takes. */
|
|
6
|
+
export const MEDIA_TYPES = [
|
|
7
|
+
"image/jpeg",
|
|
8
|
+
"image/png",
|
|
9
|
+
"image/gif",
|
|
10
|
+
"image/webp",
|
|
11
|
+
"image/avif",
|
|
12
|
+
];
|
|
13
|
+
/** The most an upload may be, in bytes. */
|
|
14
|
+
export const MEDIA_MAX_BYTES = 10 * 1024 * 1024;
|
|
15
|
+
const starts = (bytes, at, ...values) => values.every((v, i) => bytes[at + i] === v);
|
|
16
|
+
const ascii = (bytes, at, text) => [...text].every((c, i) => bytes[at + i] === c.charCodeAt(0));
|
|
17
|
+
/** The type the bytes are, or null when they are not a picture the library takes. */
|
|
18
|
+
export function sniffMediaType(bytes) {
|
|
19
|
+
if (starts(bytes, 0, 0xff, 0xd8, 0xff))
|
|
20
|
+
return "image/jpeg";
|
|
21
|
+
if (starts(bytes, 0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
|
|
22
|
+
return "image/png";
|
|
23
|
+
if (ascii(bytes, 0, "GIF87a") || ascii(bytes, 0, "GIF89a"))
|
|
24
|
+
return "image/gif";
|
|
25
|
+
if (ascii(bytes, 0, "RIFF") && ascii(bytes, 8, "WEBP"))
|
|
26
|
+
return "image/webp";
|
|
27
|
+
if (ascii(bytes, 4, "ftyp") && (ascii(bytes, 8, "avif") || ascii(bytes, 8, "avis"))) {
|
|
28
|
+
return "image/avif";
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
/** Why `bytes` cannot be uploaded, or null when they can. */
|
|
33
|
+
export function mediaProblem(bytes) {
|
|
34
|
+
if (bytes.byteLength === 0)
|
|
35
|
+
return "The file is empty";
|
|
36
|
+
if (bytes.byteLength > MEDIA_MAX_BYTES) {
|
|
37
|
+
return `The file is ${bytes.byteLength} bytes; a picture is at most ${MEDIA_MAX_BYTES}`;
|
|
38
|
+
}
|
|
39
|
+
if (!sniffMediaType(bytes)) {
|
|
40
|
+
return "This is not a picture the library takes: JPEG, PNG, GIF, WebP or AVIF";
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PermissionsManifest } from "./manifest.ts";
|
|
2
|
+
export interface GrantRequest {
|
|
3
|
+
/** Operation name, e.g. `organisation.get`, `record.create`. */
|
|
4
|
+
operation: string;
|
|
5
|
+
/** The operation's risk class. */
|
|
6
|
+
risk: string;
|
|
7
|
+
/** The object the call reads or writes, when the operation is about one (else null). */
|
|
8
|
+
object: string | null;
|
|
9
|
+
}
|
|
10
|
+
export type GrantDecision = {
|
|
11
|
+
ok: true;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
};
|
|
16
|
+
/** Decides whether extension `id` with `permissions` may make `request`. */
|
|
17
|
+
export declare function checkGrant(id: string, permissions: PermissionsManifest, request: GrantRequest): GrantDecision;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FORBIDDEN_OPERATION_PREFIXES } from "./manifest.js";
|
|
2
|
+
/** Decides whether extension `id` with `permissions` may make `request`. */
|
|
3
|
+
export function checkGrant(id, permissions, request) {
|
|
4
|
+
const { operation, risk, object } = request;
|
|
5
|
+
if (FORBIDDEN_OPERATION_PREFIXES.some((prefix) => operation.startsWith(prefix))) {
|
|
6
|
+
return { ok: false, reason: `Extensions cannot call ${operation}` };
|
|
7
|
+
}
|
|
8
|
+
if (operation.startsWith(`x.${id}.`)) {
|
|
9
|
+
return { ok: false, reason: "An extension cannot call its own tools through codemode:ops" };
|
|
10
|
+
}
|
|
11
|
+
if (permissions.operations.includes(operation))
|
|
12
|
+
return { ok: true };
|
|
13
|
+
if (operation.startsWith("x.")) {
|
|
14
|
+
return { ok: false, reason: `${operation} is not in permissions.operations` };
|
|
15
|
+
}
|
|
16
|
+
if (!object) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
reason: `${operation} is not in permissions.operations of extension ${id}`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (object.startsWith(`${id}.`))
|
|
23
|
+
return { ok: true };
|
|
24
|
+
if (risk === "read") {
|
|
25
|
+
if (permissions.read.includes(object) || permissions.write.includes(object))
|
|
26
|
+
return { ok: true };
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
reason: `Extension ${id} may not read ${object} (add it to permissions.read)`,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (risk === "write" || risk === "bulk-write") {
|
|
33
|
+
if (permissions.write.includes(object))
|
|
34
|
+
return { ok: true };
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
reason: `Extension ${id} may not write ${object} (add it to permissions.write)`,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
ok: false,
|
|
42
|
+
reason: `${operation} is ${risk}; list it in permissions.operations to allow it`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The host module extension code calls operations through, and what it was called. */
|
|
2
|
+
export declare const OPS_MODULE = "ops";
|
|
3
|
+
export declare const OLD_OPS_MODULE = "crm";
|
|
4
|
+
/** The module specifier Code Mode scripts import, and what it was. */
|
|
5
|
+
export declare const OPS_SPECIFIER = "codemode:ops";
|
|
6
|
+
export declare const OLD_OPS_SPECIFIER = "codemode:crm";
|
|
7
|
+
/** The prefix of every read view, and what it was. */
|
|
8
|
+
export declare const VIEW_PREFIX = "rec_";
|
|
9
|
+
export declare const OLD_VIEW_PREFIX = "crm_";
|
|
10
|
+
/**
|
|
11
|
+
* What a view named under the old contract is called now: `rec_person` for the old people view,
|
|
12
|
+
* `rec_x_deals_deal` for an extension object's. Null when `name` is not an old view name.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renamedView(name: string): string | null;
|
|
15
|
+
/** Why a use of an old contract name is refused, naming the new form. Null for any other name. */
|
|
16
|
+
export declare function renamedMessage(name: string): string | null;
|
package/dist/renamed.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
const OLD_PREFIX = "crm";
|
|
8
|
+
/** The host module extension code calls operations through, and what it was called. */
|
|
9
|
+
export const OPS_MODULE = "ops";
|
|
10
|
+
export const OLD_OPS_MODULE = OLD_PREFIX;
|
|
11
|
+
/** The module specifier Code Mode scripts import, and what it was. */
|
|
12
|
+
export const OPS_SPECIFIER = `codemode:${OPS_MODULE}`;
|
|
13
|
+
export const OLD_OPS_SPECIFIER = `codemode:${OLD_OPS_MODULE}`;
|
|
14
|
+
/** The prefix of every read view, and what it was. */
|
|
15
|
+
export const VIEW_PREFIX = "rec_";
|
|
16
|
+
export const OLD_VIEW_PREFIX = `${OLD_PREFIX}_`;
|
|
17
|
+
/** The core views, which were named in the plural: old suffix → new suffix. */
|
|
18
|
+
const RENAMED_CORE_VIEWS = {
|
|
19
|
+
people: "person",
|
|
20
|
+
organisations: "organisation",
|
|
21
|
+
affiliations: "affiliation",
|
|
22
|
+
activities: "activity",
|
|
23
|
+
activity_links: "activity_link",
|
|
24
|
+
tasks: "task",
|
|
25
|
+
task_links: "task_link",
|
|
26
|
+
record_tags: "record_tag",
|
|
27
|
+
users: "user",
|
|
28
|
+
stage_history: "stage_history",
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* What a view named under the old contract is called now: `rec_person` for the old people view,
|
|
32
|
+
* `rec_x_deals_deal` for an extension object's. Null when `name` is not an old view name.
|
|
33
|
+
*/
|
|
34
|
+
export function renamedView(name) {
|
|
35
|
+
const lower = name.toLowerCase();
|
|
36
|
+
if (!lower.startsWith(OLD_VIEW_PREFIX))
|
|
37
|
+
return null;
|
|
38
|
+
const suffix = lower.slice(OLD_VIEW_PREFIX.length);
|
|
39
|
+
if (suffix.startsWith("x_"))
|
|
40
|
+
return `${VIEW_PREFIX}${suffix}`;
|
|
41
|
+
const core = RENAMED_CORE_VIEWS[suffix];
|
|
42
|
+
return core ? `${VIEW_PREFIX}${core}` : null;
|
|
43
|
+
}
|
|
44
|
+
/** Why a use of an old contract name is refused, naming the new form. Null for any other name. */
|
|
45
|
+
export function renamedMessage(name) {
|
|
46
|
+
if (name === OLD_OPS_SPECIFIER) {
|
|
47
|
+
return `"${OLD_OPS_SPECIFIER}" was renamed: import { ${OPS_MODULE} } from "${OPS_SPECIFIER}" and call ${OPS_MODULE}.call(…)`;
|
|
48
|
+
}
|
|
49
|
+
if (name === OLD_OPS_MODULE) {
|
|
50
|
+
return `The "${OLD_OPS_MODULE}" module was renamed "${OPS_MODULE}": declare "${OPS_MODULE}" in permissions.modules and call ctx.${OPS_MODULE}`;
|
|
51
|
+
}
|
|
52
|
+
const view = renamedView(name);
|
|
53
|
+
if (view)
|
|
54
|
+
return `${name} was renamed ${view}; query the ${VIEW_PREFIX}* views`;
|
|
55
|
+
if (name.toLowerCase().startsWith(OLD_VIEW_PREFIX)) {
|
|
56
|
+
return `${name} is not a view: the ${OLD_VIEW_PREFIX}* views were renamed ${VIEW_PREFIX}*`;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** A media id in the fixture; replace it with a real one before storing. */
|
|
2
|
+
export declare const RICH_TEXT_FIXTURE_MEDIA = "med_01FIXTURE0000000000000000";
|
|
3
|
+
export declare const RICH_TEXT_FIXTURE: {
|
|
4
|
+
readonly blocks: readonly [{
|
|
5
|
+
readonly type: "heading";
|
|
6
|
+
readonly level: 2;
|
|
7
|
+
readonly content: readonly [{
|
|
8
|
+
readonly type: "text";
|
|
9
|
+
readonly text: "Summer at the hall";
|
|
10
|
+
}];
|
|
11
|
+
}, {
|
|
12
|
+
readonly type: "paragraph";
|
|
13
|
+
readonly content: readonly [{
|
|
14
|
+
readonly type: "text";
|
|
15
|
+
readonly text: "Doors open at ";
|
|
16
|
+
}, {
|
|
17
|
+
readonly type: "text";
|
|
18
|
+
readonly text: "seven";
|
|
19
|
+
readonly bold: true;
|
|
20
|
+
}, {
|
|
21
|
+
readonly type: "text";
|
|
22
|
+
readonly text: ", the bar a little ";
|
|
23
|
+
}, {
|
|
24
|
+
readonly type: "text";
|
|
25
|
+
readonly text: "earlier";
|
|
26
|
+
readonly italic: true;
|
|
27
|
+
}, {
|
|
28
|
+
readonly type: "text";
|
|
29
|
+
readonly text: ". Bring ";
|
|
30
|
+
}, {
|
|
31
|
+
readonly type: "text";
|
|
32
|
+
readonly text: "ID";
|
|
33
|
+
readonly code: true;
|
|
34
|
+
}, {
|
|
35
|
+
readonly type: "break";
|
|
36
|
+
}, {
|
|
37
|
+
readonly type: "text";
|
|
38
|
+
readonly text: "Tickets are on the ";
|
|
39
|
+
readonly bold: true;
|
|
40
|
+
readonly italic: true;
|
|
41
|
+
}, {
|
|
42
|
+
readonly type: "text";
|
|
43
|
+
readonly text: "events page";
|
|
44
|
+
readonly href: "/events";
|
|
45
|
+
}, {
|
|
46
|
+
readonly type: "text";
|
|
47
|
+
readonly text: " or by ";
|
|
48
|
+
}, {
|
|
49
|
+
readonly type: "text";
|
|
50
|
+
readonly text: "email";
|
|
51
|
+
readonly href: "mailto:hello@example.com";
|
|
52
|
+
}, {
|
|
53
|
+
readonly type: "text";
|
|
54
|
+
readonly text: ".";
|
|
55
|
+
}];
|
|
56
|
+
}, {
|
|
57
|
+
readonly type: "list";
|
|
58
|
+
readonly style: "bullet";
|
|
59
|
+
readonly items: readonly [{
|
|
60
|
+
readonly content: readonly [{
|
|
61
|
+
readonly type: "text";
|
|
62
|
+
readonly text: "Friday: open mic";
|
|
63
|
+
}];
|
|
64
|
+
}, {
|
|
65
|
+
readonly content: readonly [{
|
|
66
|
+
readonly type: "text";
|
|
67
|
+
readonly text: "Saturday: two bands";
|
|
68
|
+
}];
|
|
69
|
+
readonly list: {
|
|
70
|
+
readonly style: "ordered";
|
|
71
|
+
readonly items: readonly [{
|
|
72
|
+
readonly content: readonly [{
|
|
73
|
+
readonly type: "text";
|
|
74
|
+
readonly text: "The first";
|
|
75
|
+
}];
|
|
76
|
+
}, {
|
|
77
|
+
readonly content: readonly [{
|
|
78
|
+
readonly type: "text";
|
|
79
|
+
readonly text: "The second";
|
|
80
|
+
}];
|
|
81
|
+
}];
|
|
82
|
+
};
|
|
83
|
+
}];
|
|
84
|
+
}, {
|
|
85
|
+
readonly type: "quote";
|
|
86
|
+
readonly content: readonly [{
|
|
87
|
+
readonly type: "text";
|
|
88
|
+
readonly text: "Best night of the year.";
|
|
89
|
+
}];
|
|
90
|
+
readonly attribution: "A regular";
|
|
91
|
+
}, {
|
|
92
|
+
readonly type: "image";
|
|
93
|
+
readonly media: "med_01FIXTURE0000000000000000";
|
|
94
|
+
readonly alt: "The hall from the stage, full";
|
|
95
|
+
readonly caption: readonly [{
|
|
96
|
+
readonly type: "text";
|
|
97
|
+
readonly text: "Last summer, ";
|
|
98
|
+
}, {
|
|
99
|
+
readonly type: "text";
|
|
100
|
+
readonly text: "sold out";
|
|
101
|
+
readonly bold: true;
|
|
102
|
+
}];
|
|
103
|
+
}, {
|
|
104
|
+
readonly type: "divider";
|
|
105
|
+
}, {
|
|
106
|
+
readonly type: "embed";
|
|
107
|
+
readonly url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
|
|
108
|
+
readonly caption: "The trailer";
|
|
109
|
+
}, {
|
|
110
|
+
readonly type: "callout";
|
|
111
|
+
readonly tone: "warning";
|
|
112
|
+
readonly content: readonly [{
|
|
113
|
+
readonly type: "text";
|
|
114
|
+
readonly text: "Under 18s with an adult only.";
|
|
115
|
+
}];
|
|
116
|
+
}, {
|
|
117
|
+
readonly type: "button";
|
|
118
|
+
readonly label: "Get tickets";
|
|
119
|
+
readonly href: "https://tickets.example.com/summer";
|
|
120
|
+
}];
|
|
121
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
export const RICH_TEXT_FIXTURE = {
|
|
8
|
+
blocks: [
|
|
9
|
+
{ type: "heading", level: 2, content: [{ type: "text", text: "Summer at the hall" }] },
|
|
10
|
+
{
|
|
11
|
+
type: "paragraph",
|
|
12
|
+
content: [
|
|
13
|
+
{ type: "text", text: "Doors open at " },
|
|
14
|
+
{ type: "text", text: "seven", bold: true },
|
|
15
|
+
{ type: "text", text: ", the bar a little " },
|
|
16
|
+
{ type: "text", text: "earlier", italic: true },
|
|
17
|
+
{ type: "text", text: ". Bring " },
|
|
18
|
+
{ type: "text", text: "ID", code: true },
|
|
19
|
+
{ type: "break" },
|
|
20
|
+
{ type: "text", text: "Tickets are on the ", bold: true, italic: true },
|
|
21
|
+
{ type: "text", text: "events page", href: "/events" },
|
|
22
|
+
{ type: "text", text: " or by " },
|
|
23
|
+
{ type: "text", text: "email", href: "mailto:hello@example.com" },
|
|
24
|
+
{ type: "text", text: "." },
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: "list",
|
|
29
|
+
style: "bullet",
|
|
30
|
+
items: [
|
|
31
|
+
{ content: [{ type: "text", text: "Friday: open mic" }] },
|
|
32
|
+
{
|
|
33
|
+
content: [{ type: "text", text: "Saturday: two bands" }],
|
|
34
|
+
list: {
|
|
35
|
+
style: "ordered",
|
|
36
|
+
items: [
|
|
37
|
+
{ content: [{ type: "text", text: "The first" }] },
|
|
38
|
+
{ content: [{ type: "text", text: "The second" }] },
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: "quote",
|
|
46
|
+
content: [{ type: "text", text: "Best night of the year." }],
|
|
47
|
+
attribution: "A regular",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: "image",
|
|
51
|
+
media: RICH_TEXT_FIXTURE_MEDIA,
|
|
52
|
+
alt: "The hall from the stage, full",
|
|
53
|
+
caption: [
|
|
54
|
+
{ type: "text", text: "Last summer, " },
|
|
55
|
+
{ type: "text", text: "sold out", bold: true },
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
{ type: "divider" },
|
|
59
|
+
{ type: "embed", url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", caption: "The trailer" },
|
|
60
|
+
{
|
|
61
|
+
type: "callout",
|
|
62
|
+
tone: "warning",
|
|
63
|
+
content: [{ type: "text", text: "Under 18s with an adult only." }],
|
|
64
|
+
},
|
|
65
|
+
{ type: "button", label: "Get tickets", href: "https://tickets.example.com/summer" },
|
|
66
|
+
],
|
|
67
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { DocumentSchema } from "./document.ts";
|
|
2
|
+
/** The block types, in the order a renderer lists them. */
|
|
3
|
+
export declare const RICH_TEXT_BLOCKS: readonly ["paragraph", "heading", "list", "quote", "image", "divider", "embed", "callout", "button"];
|
|
4
|
+
export type RichTextBlockType = (typeof RICH_TEXT_BLOCKS)[number];
|
|
5
|
+
/** The marks a run of text may carry. A link is `href`. */
|
|
6
|
+
export declare const RICH_TEXT_MARKS: readonly ["bold", "italic", "code", "href"];
|
|
7
|
+
/** A richText field that declares no `maxBytes`. At most DOCUMENT_MAX_BYTES. */
|
|
8
|
+
export declare const RICH_TEXT_DEFAULT_BYTES = 131072;
|
|
9
|
+
/** The object a picture's `media` names: the media library's (stage 28, step 4). */
|
|
10
|
+
export declare const RICH_TEXT_MEDIA_OBJECT = "media";
|
|
11
|
+
/** The one schema of every richText field. */
|
|
12
|
+
export declare const RICH_TEXT_SCHEMA: DocumentSchema;
|
|
13
|
+
/**
|
|
14
|
+
* The `image` field type (stage 28, ADR 0444): a picture from the media library, with its alt
|
|
15
|
+
* text and the point to keep in view when a client crops it (0 to 1 from the left and the top).
|
|
16
|
+
*/
|
|
17
|
+
export declare const IMAGE_SCHEMA: DocumentSchema;
|
|
18
|
+
/** The most an image field's value may be, in bytes of its JSON. */
|
|
19
|
+
export declare const IMAGE_MAX_BYTES = 2048;
|
|
20
|
+
/** The field types whose value is a document (ADR 0445): `document`, `richText` and `image`. */
|
|
21
|
+
export declare function isDocumentType(type: string | undefined): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The schema a document-valued field is checked against: its declared one, or richText's. Not a
|
|
24
|
+
* document type: undefined.
|
|
25
|
+
*/
|
|
26
|
+
export declare function documentSchemaOf(field: {
|
|
27
|
+
type?: string | undefined;
|
|
28
|
+
schema?: unknown;
|
|
29
|
+
}): DocumentSchema | undefined;
|
|
30
|
+
/** The size limit of a document-valued field: its declared one, or its type's default. */
|
|
31
|
+
export declare function documentMaxBytesOf(field: {
|
|
32
|
+
type?: string | undefined;
|
|
33
|
+
maxBytes?: number | undefined;
|
|
34
|
+
}): number | undefined;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/** The block types, in the order a renderer lists them. */
|
|
2
|
+
export const RICH_TEXT_BLOCKS = [
|
|
3
|
+
"paragraph",
|
|
4
|
+
"heading",
|
|
5
|
+
"list",
|
|
6
|
+
"quote",
|
|
7
|
+
"image",
|
|
8
|
+
"divider",
|
|
9
|
+
"embed",
|
|
10
|
+
"callout",
|
|
11
|
+
"button",
|
|
12
|
+
];
|
|
13
|
+
/** The marks a run of text may carry. A link is `href`. */
|
|
14
|
+
export const RICH_TEXT_MARKS = ["bold", "italic", "code", "href"];
|
|
15
|
+
/** A richText field that declares no `maxBytes`. At most DOCUMENT_MAX_BYTES. */
|
|
16
|
+
export const RICH_TEXT_DEFAULT_BYTES = 131_072;
|
|
17
|
+
/** The object a picture's `media` names: the media library's (stage 28, step 4). */
|
|
18
|
+
export const RICH_TEXT_MEDIA_OBJECT = "media";
|
|
19
|
+
const prose = (max, extra = {}) => ({ type: "text", max, noMarkup: true, ...extra });
|
|
20
|
+
const optional = (node) => ({ ...node, optional: true });
|
|
21
|
+
const inlines = { type: "use", def: "inlines" };
|
|
22
|
+
const object = (fields) => ({
|
|
23
|
+
type: "object",
|
|
24
|
+
fields,
|
|
25
|
+
});
|
|
26
|
+
const listShape = () => object({
|
|
27
|
+
style: { type: "choice", options: ["bullet", "ordered"] },
|
|
28
|
+
items: { type: "array", min: 1, max: 200, of: { type: "use", def: "listItem" } },
|
|
29
|
+
});
|
|
30
|
+
const blocks = {
|
|
31
|
+
paragraph: object({ content: inlines }),
|
|
32
|
+
heading: object({
|
|
33
|
+
level: { type: "number", integer: true, min: 1, max: 6 },
|
|
34
|
+
content: inlines,
|
|
35
|
+
}),
|
|
36
|
+
list: listShape(),
|
|
37
|
+
quote: object({ content: inlines, attribution: optional(prose(300)) }),
|
|
38
|
+
image: object({
|
|
39
|
+
media: { type: "ref", to: RICH_TEXT_MEDIA_OBJECT, description: "The picture, from media" },
|
|
40
|
+
alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
|
|
41
|
+
caption: optional(inlines),
|
|
42
|
+
}),
|
|
43
|
+
divider: object({}),
|
|
44
|
+
embed: object({
|
|
45
|
+
url: {
|
|
46
|
+
type: "text",
|
|
47
|
+
max: 2000,
|
|
48
|
+
href: "web",
|
|
49
|
+
description: "A video or post to show (YouTube, Vimeo): the client decides how",
|
|
50
|
+
},
|
|
51
|
+
caption: optional(prose(500)),
|
|
52
|
+
}),
|
|
53
|
+
callout: object({
|
|
54
|
+
tone: { type: "choice", options: ["info", "note", "success", "warning"] },
|
|
55
|
+
content: inlines,
|
|
56
|
+
}),
|
|
57
|
+
button: object({
|
|
58
|
+
label: prose(80, { min: 1 }),
|
|
59
|
+
href: { type: "text", max: 2000, href: "link" },
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
/** The one schema of every richText field. */
|
|
63
|
+
export const RICH_TEXT_SCHEMA = {
|
|
64
|
+
root: object({
|
|
65
|
+
blocks: {
|
|
66
|
+
type: "array",
|
|
67
|
+
max: 1000,
|
|
68
|
+
of: {
|
|
69
|
+
type: "union",
|
|
70
|
+
tag: "type",
|
|
71
|
+
variants: blocks,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
}),
|
|
75
|
+
defs: {
|
|
76
|
+
inlines: {
|
|
77
|
+
type: "array",
|
|
78
|
+
max: 500,
|
|
79
|
+
of: {
|
|
80
|
+
type: "union",
|
|
81
|
+
tag: "type",
|
|
82
|
+
variants: {
|
|
83
|
+
text: object({
|
|
84
|
+
text: prose(5000, { min: 1 }),
|
|
85
|
+
bold: optional({ type: "boolean" }),
|
|
86
|
+
italic: optional({ type: "boolean" }),
|
|
87
|
+
code: optional({ type: "boolean" }),
|
|
88
|
+
href: optional({ type: "text", max: 2000, href: "link" }),
|
|
89
|
+
}),
|
|
90
|
+
break: object({}),
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
listItem: object({ content: inlines, list: optional({ type: "use", def: "list" }) }),
|
|
95
|
+
list: listShape(),
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* The `image` field type (stage 28, ADR 0444): a picture from the media library, with its alt
|
|
100
|
+
* text and the point to keep in view when a client crops it (0 to 1 from the left and the top).
|
|
101
|
+
*/
|
|
102
|
+
export const IMAGE_SCHEMA = {
|
|
103
|
+
root: object({
|
|
104
|
+
media: { type: "ref", to: RICH_TEXT_MEDIA_OBJECT, description: "The picture, from media" },
|
|
105
|
+
alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
|
|
106
|
+
focal: optional(object({
|
|
107
|
+
x: { type: "number", min: 0, max: 1 },
|
|
108
|
+
y: { type: "number", min: 0, max: 1 },
|
|
109
|
+
})),
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
/** The most an image field's value may be, in bytes of its JSON. */
|
|
113
|
+
export const IMAGE_MAX_BYTES = 2048;
|
|
114
|
+
/** The field types whose value is a document (ADR 0445): `document`, `richText` and `image`. */
|
|
115
|
+
export function isDocumentType(type) {
|
|
116
|
+
return type === "document" || type === "richText" || type === "image";
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The schema a document-valued field is checked against: its declared one, or richText's. Not a
|
|
120
|
+
* document type: undefined.
|
|
121
|
+
*/
|
|
122
|
+
export function documentSchemaOf(field) {
|
|
123
|
+
if (field.type === "richText")
|
|
124
|
+
return RICH_TEXT_SCHEMA;
|
|
125
|
+
if (field.type === "image")
|
|
126
|
+
return IMAGE_SCHEMA;
|
|
127
|
+
if (field.type === "document")
|
|
128
|
+
return field.schema;
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
/** The size limit of a document-valued field: its declared one, or its type's default. */
|
|
132
|
+
export function documentMaxBytesOf(field) {
|
|
133
|
+
if (field.type === "image")
|
|
134
|
+
return IMAGE_MAX_BYTES;
|
|
135
|
+
if (field.maxBytes !== undefined)
|
|
136
|
+
return field.maxBytes;
|
|
137
|
+
return field.type === "richText" ? RICH_TEXT_DEFAULT_BYTES : undefined;
|
|
138
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type AutomationAdvance, type AutomationContext, type AutomationEvent, type AutomationRunRequest, type AutomationTestStep } from "./automation.ts";
|
|
2
|
+
import type { Extension, ExtensionContext, StartedBy } from "./contract.ts";
|
|
3
|
+
type HostFn = (...args: unknown[]) => Promise<unknown>;
|
|
4
|
+
/** The raw `codemode:*` modules, as imported in the entry module. */
|
|
5
|
+
export interface RawHostModules {
|
|
6
|
+
ops?: {
|
|
7
|
+
call: HostFn;
|
|
8
|
+
};
|
|
9
|
+
storage?: {
|
|
10
|
+
get: HostFn;
|
|
11
|
+
set: HostFn;
|
|
12
|
+
del: HostFn;
|
|
13
|
+
list: HostFn;
|
|
14
|
+
};
|
|
15
|
+
log?: {
|
|
16
|
+
write: HostFn;
|
|
17
|
+
};
|
|
18
|
+
ai?: {
|
|
19
|
+
summarise: HostFn;
|
|
20
|
+
extract: HostFn;
|
|
21
|
+
classify: HostFn;
|
|
22
|
+
};
|
|
23
|
+
/** `codemode:<gatekeeper>` modules, by gatekeeper name. */
|
|
24
|
+
[gatekeeper: string]: Record<string, HostFn> | undefined;
|
|
25
|
+
}
|
|
26
|
+
export type RunRequest = {
|
|
27
|
+
kind: "manifest";
|
|
28
|
+
} | {
|
|
29
|
+
kind: "tool";
|
|
30
|
+
tool: string;
|
|
31
|
+
input: unknown;
|
|
32
|
+
version?: string;
|
|
33
|
+
/** Who started the tool, from the host (ADR 0436). */
|
|
34
|
+
caller?: StartedBy;
|
|
35
|
+
};
|
|
36
|
+
export type RunResponse = {
|
|
37
|
+
ok: true;
|
|
38
|
+
value: unknown;
|
|
39
|
+
} | {
|
|
40
|
+
ok: false;
|
|
41
|
+
error: {
|
|
42
|
+
code: string;
|
|
43
|
+
message: string;
|
|
44
|
+
details?: unknown;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/** What `codemode:ops`'s `call` returns. */
|
|
48
|
+
export type OpsReply = {
|
|
49
|
+
ok: true;
|
|
50
|
+
value: unknown;
|
|
51
|
+
} | {
|
|
52
|
+
ok: false;
|
|
53
|
+
error: {
|
|
54
|
+
code: string;
|
|
55
|
+
message: string;
|
|
56
|
+
details?: unknown;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/** Thrown by `ctx.ops.call` when the operation fails; `code` is the pipeline's error code. */
|
|
60
|
+
export declare class OpsCallError extends Error {
|
|
61
|
+
readonly code: string;
|
|
62
|
+
readonly details?: unknown | undefined;
|
|
63
|
+
name: string;
|
|
64
|
+
constructor(code: string, message: string, details?: unknown | undefined);
|
|
65
|
+
}
|
|
66
|
+
/** Builds the `ctx` tools receive from raw host modules. Exported for the testing harness. */
|
|
67
|
+
export declare function buildContext(extension: {
|
|
68
|
+
id: string;
|
|
69
|
+
version: string;
|
|
70
|
+
}, raw: RawHostModules, pending?: Promise<unknown>[], caller?: StartedBy): ExtensionContext;
|
|
71
|
+
/** Dispatches one run request inside the sandbox. */
|
|
72
|
+
export declare function runExtension(extension: Extension, request: RunRequest, raw: RawHostModules): Promise<RunResponse>;
|
|
73
|
+
/** What `runAutomation` returns in `value`: an advance, or (test mode) every step. */
|
|
74
|
+
export type AutomationRunValue = AutomationAdvance | {
|
|
75
|
+
kind: "test";
|
|
76
|
+
steps: AutomationTestStep[];
|
|
77
|
+
value: unknown;
|
|
78
|
+
};
|
|
79
|
+
/** A step call that does not match the declarations (the declarations are the contract). */
|
|
80
|
+
export declare class AutomationStepError extends Error {
|
|
81
|
+
name: string;
|
|
82
|
+
readonly code = "undeclared_step";
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Builds the automation `ctx`: the extension context plus `notify`, which calls
|
|
86
|
+
* `notification.send` with `notificationInput`. Its dedupe key is `<step>:<n>` (the n-th notify
|
|
87
|
+
* of that step), so a step retried or replayed never notifies twice.
|
|
88
|
+
*/
|
|
89
|
+
export declare function buildAutomationContext(extension: {
|
|
90
|
+
id: string;
|
|
91
|
+
version: string;
|
|
92
|
+
}, raw: RawHostModules, event: AutomationEvent, pending?: Promise<unknown>[], currentStep?: () => string | null): AutomationContext;
|
|
93
|
+
/** Runs one advance (or a whole test run) of an automation inside the sandbox. */
|
|
94
|
+
export declare function runAutomation(extension: Extension, request: AutomationRunRequest, raw: RawHostModules): Promise<RunResponse>;
|
|
95
|
+
export {};
|