@copilotkit/shared 1.79.2-canary.1792172552 → 1.81.0
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/dist/a2ui-envelope.cjs +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +139 -0
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +60 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +60 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +134 -1
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/constants/index.cjs +7 -0
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +7 -1
- package/dist/constants/index.d.cts.map +1 -1
- package/dist/constants/index.d.mts +7 -1
- package/dist/constants/index.d.mts.map +1 -1
- package/dist/constants/index.mjs +7 -1
- package/dist/constants/index.mjs.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.cjs +7 -6
- package/dist/event-transforms/open-generative-ui-middleware.cjs.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.mjs +7 -6
- package/dist/event-transforms/open-generative-ui-middleware.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +177 -0
- package/dist/generative-ui/basic-functions.cjs.map +1 -0
- package/dist/generative-ui/basic-functions.d.cts +108 -0
- package/dist/generative-ui/basic-functions.d.cts.map +1 -0
- package/dist/generative-ui/basic-functions.d.mts +108 -0
- package/dist/generative-ui/basic-functions.d.mts.map +1 -0
- package/dist/generative-ui/basic-functions.mjs +167 -0
- package/dist/generative-ui/basic-functions.mjs.map +1 -0
- package/dist/generative-ui/child-refs.cjs +161 -0
- package/dist/generative-ui/child-refs.cjs.map +1 -0
- package/dist/generative-ui/child-refs.d.cts +68 -0
- package/dist/generative-ui/child-refs.d.cts.map +1 -0
- package/dist/generative-ui/child-refs.d.mts +68 -0
- package/dist/generative-ui/child-refs.d.mts.map +1 -0
- package/dist/generative-ui/child-refs.mjs +152 -0
- package/dist/generative-ui/child-refs.mjs.map +1 -0
- package/dist/generative-ui/is-record.cjs +10 -0
- package/dist/generative-ui/is-record.cjs.map +1 -0
- package/dist/generative-ui/is-record.mjs +9 -0
- package/dist/generative-ui/is-record.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +91 -0
- package/dist/generative-ui/prompt.cjs.map +1 -0
- package/dist/generative-ui/prompt.d.cts +13 -0
- package/dist/generative-ui/prompt.d.cts.map +1 -0
- package/dist/generative-ui/prompt.d.mts +13 -0
- package/dist/generative-ui/prompt.d.mts.map +1 -0
- package/dist/generative-ui/prompt.mjs +90 -0
- package/dist/generative-ui/prompt.mjs.map +1 -0
- package/dist/generative-ui/render-ui-args.cjs +160 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -0
- package/dist/generative-ui/render-ui-args.d.cts +40 -0
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
- package/dist/generative-ui/render-ui-args.d.mts +40 -0
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
- package/dist/generative-ui/render-ui-args.mjs +159 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +101 -0
- package/dist/generative-ui/types.cjs.map +1 -0
- package/dist/generative-ui/types.d.cts +270 -0
- package/dist/generative-ui/types.d.cts.map +1 -0
- package/dist/generative-ui/types.d.mts +270 -0
- package/dist/generative-ui/types.d.mts.map +1 -0
- package/dist/generative-ui/types.mjs +86 -0
- package/dist/generative-ui/types.mjs.map +1 -0
- package/dist/generative-ui/validate-operation.cjs +405 -0
- package/dist/generative-ui/validate-operation.cjs.map +1 -0
- package/dist/generative-ui/validate-operation.d.cts +86 -0
- package/dist/generative-ui/validate-operation.d.cts.map +1 -0
- package/dist/generative-ui/validate-operation.d.mts +86 -0
- package/dist/generative-ui/validate-operation.d.mts.map +1 -0
- package/dist/generative-ui/validate-operation.mjs +399 -0
- package/dist/generative-ui/validate-operation.mjs.map +1 -0
- package/dist/index.cjs +64 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +11 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +10 -3
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1466 -29
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/lambda-client.cjs.map +1 -1
- package/dist/telemetry/lambda-client.mjs.map +1 -1
- package/dist/telemetry/utils.cjs +0 -2
- package/dist/telemetry/utils.cjs.map +1 -1
- package/dist/telemetry/utils.mjs +0 -2
- package/dist/telemetry/utils.mjs.map +1 -1
- package/dist/utils/dashboard-url.cjs +19 -0
- package/dist/utils/dashboard-url.cjs.map +1 -0
- package/dist/utils/dashboard-url.mjs +18 -0
- package/dist/utils/dashboard-url.mjs.map +1 -0
- package/dist/utils/index.cjs +2 -1
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +2 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-learning.cjs +48 -16
- package/dist/utils/inspector-learning.cjs.map +1 -1
- package/dist/utils/inspector-learning.d.cts +14 -1
- package/dist/utils/inspector-learning.d.cts.map +1 -1
- package/dist/utils/inspector-learning.d.mts +14 -1
- package/dist/utils/inspector-learning.d.mts.map +1 -1
- package/dist/utils/inspector-learning.mjs +49 -16
- package/dist/utils/inspector-learning.mjs.map +1 -1
- package/dist/utils/inspector-metadata.cjs +2 -3
- package/dist/utils/inspector-metadata.cjs.map +1 -1
- package/dist/utils/inspector-metadata.d.cts.map +1 -1
- package/dist/utils/inspector-metadata.d.mts.map +1 -1
- package/dist/utils/inspector-metadata.mjs +3 -3
- package/dist/utils/inspector-metadata.mjs.map +1 -1
- package/package.json +3 -4
- package/src/__tests__/a2ui-envelope.test.ts +434 -0
- package/src/__tests__/a2ui-prompts.test.ts +362 -0
- package/src/a2ui-envelope.ts +309 -0
- package/src/a2ui-prompts.ts +207 -0
- package/src/constants/index.ts +7 -0
- package/src/event-transforms/open-generative-ui-middleware.ts +19 -10
- package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
- package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
- package/src/generative-ui/basic-functions.ts +282 -0
- package/src/generative-ui/child-refs.ts +210 -0
- package/src/generative-ui/index.ts +6 -0
- package/src/generative-ui/is-record.ts +4 -0
- package/src/generative-ui/prompt.ts +174 -0
- package/src/generative-ui/render-ui-args.ts +199 -0
- package/src/generative-ui/types.ts +314 -0
- package/src/generative-ui/validate-operation.ts +723 -0
- package/src/index.ts +15 -0
- package/src/telemetry/lambda-client.ts +3 -3
- package/src/telemetry/utils.ts +0 -28
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/inspector-learning.test.ts +210 -0
- package/src/utils/inspector-learning.ts +117 -17
- package/src/utils/inspector-metadata.test.ts +2 -1
- package/src/utils/inspector-metadata.ts +3 -13
- package/src/utils.test.ts +71 -0
- package/tsdown.config.ts +0 -1
- package/src/telemetry/scarf-client.ts +0 -39
- package/src/telemetry/security-check.ts +0 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
const require_basic_functions = require('./basic-functions.cjs');
|
|
2
|
+
const require_is_record = require('./is-record.cjs');
|
|
3
|
+
const require_child_refs = require('./child-refs.cjs');
|
|
4
|
+
const require_types = require('./types.cjs');
|
|
5
|
+
|
|
6
|
+
//#region src/generative-ui/validate-operation.ts
|
|
7
|
+
const OPERATION_KINDS = [
|
|
8
|
+
"createSurface",
|
|
9
|
+
"updateComponents",
|
|
10
|
+
"updateDataModel",
|
|
11
|
+
"deleteSurface",
|
|
12
|
+
"callRendererFunction"
|
|
13
|
+
];
|
|
14
|
+
const OK = { ok: true };
|
|
15
|
+
const fail = (reason) => ({
|
|
16
|
+
ok: false,
|
|
17
|
+
reason
|
|
18
|
+
});
|
|
19
|
+
/** Models write the v0.9 binding from habit. The reason names the v1.0 form. */
|
|
20
|
+
const V09_PATH_REASON = "bind data with {\"@path\": \"/x\"}, not {\"path\": \"/x\"}";
|
|
21
|
+
function hasOwn(record, key) {
|
|
22
|
+
return Object.prototype.hasOwnProperty.call(record, key);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reads an own property only. Model-controlled keys such as "constructor" or
|
|
26
|
+
* "toString" must not resolve to Object.prototype members.
|
|
27
|
+
*/
|
|
28
|
+
function ownValue(record, key) {
|
|
29
|
+
return record && hasOwn(record, key) ? record[key] : void 0;
|
|
30
|
+
}
|
|
31
|
+
function getOperationKind(op) {
|
|
32
|
+
if (!require_is_record.isRecord(op)) return void 0;
|
|
33
|
+
const kinds = OPERATION_KINDS.filter((kind) => kind in op);
|
|
34
|
+
return kinds.length === 1 ? kinds[0] : void 0;
|
|
35
|
+
}
|
|
36
|
+
/** The `surfaceId` of a surface operation. `callRendererFunction` has none. */
|
|
37
|
+
function getOperationSurfaceId(op) {
|
|
38
|
+
if (!require_is_record.isRecord(op)) return void 0;
|
|
39
|
+
const kind = getOperationKind(op);
|
|
40
|
+
if (!kind) return void 0;
|
|
41
|
+
const payload = op[kind];
|
|
42
|
+
return require_is_record.isRecord(payload) && typeof payload.surfaceId === "string" ? payload.surfaceId : void 0;
|
|
43
|
+
}
|
|
44
|
+
/** `{ "@path": "/x" }`: a data-model binding. */
|
|
45
|
+
function isPathBinding(value) {
|
|
46
|
+
return require_is_record.isRecord(value) && typeof value["@path"] === "string" && Object.keys(value).length === 1;
|
|
47
|
+
}
|
|
48
|
+
/** `{ "path": "/x" }`: the v0.9 binding, which v1.0 does not accept. */
|
|
49
|
+
function isV09PathBinding(value) {
|
|
50
|
+
return require_is_record.isRecord(value) && typeof value.path === "string" && Object.keys(value).length === 1;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `{ "@call": "@index" }` or `{ "@call": "@index", "args": { "offset": 1 } }`:
|
|
54
|
+
* the 0-based position of the item in a template, plus `offset`.
|
|
55
|
+
*/
|
|
56
|
+
function isIndexCall(value) {
|
|
57
|
+
if (!require_is_record.isRecord(value) || value["@call"] !== "@index") return false;
|
|
58
|
+
if (!Object.keys(value).every((key) => key === "@call" || key === "args")) return false;
|
|
59
|
+
const { args } = value;
|
|
60
|
+
if (args === void 0) return true;
|
|
61
|
+
return require_is_record.isRecord(args) && Object.keys(args).every((key) => key === "offset") && (args.offset === void 0 || Number.isFinite(args.offset));
|
|
62
|
+
}
|
|
63
|
+
function isActionBinding(value) {
|
|
64
|
+
return require_is_record.isRecord(value) && Object.keys(value).length === 1 && (require_is_record.isRecord(value.functionCall) || require_is_record.isRecord(value.event));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks one A2UI v1.0 operation against the catalog and the surfaces that
|
|
68
|
+
* exist. The operation comes from the model, so nothing in it is trusted.
|
|
69
|
+
*
|
|
70
|
+
* @param op - The operation, as parsed from JSON.
|
|
71
|
+
* @param options - The catalog, the known and created surfaces, the
|
|
72
|
+
* functions that the UI can call, and the components that the target of
|
|
73
|
+
* the surface takes.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const result = validateOperation(op, { manifest, knownSurfaces });
|
|
78
|
+
* if (!result.ok) rejected.push({ index, reason: result.reason });
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function validateOperation(op, options) {
|
|
82
|
+
if (!require_is_record.isRecord(op)) return fail("operation must be a JSON object");
|
|
83
|
+
if (op.version !== require_types.A2UI_VERSION) return fail(versionProblem(op.version));
|
|
84
|
+
const kind = getOperationKind(op);
|
|
85
|
+
if (!kind) return fail(`operation must contain exactly one of ${OPERATION_KINDS.join(", ")}`);
|
|
86
|
+
const payload = op[kind];
|
|
87
|
+
if (kind === "callRendererFunction") {
|
|
88
|
+
if (!require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS) return fail(require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON);
|
|
89
|
+
return validateRendererFunctionCall(payload, options);
|
|
90
|
+
}
|
|
91
|
+
if (!require_is_record.isRecord(payload) || typeof payload.surfaceId !== "string" || !payload.surfaceId) return fail(`${kind}.surfaceId is required`);
|
|
92
|
+
const surfaceId = payload.surfaceId;
|
|
93
|
+
if (kind === "createSurface") return validateCreateSurface(payload, surfaceId, options);
|
|
94
|
+
if (!options.knownSurfaces.has(surfaceId)) return fail(`surface "${surfaceId}" does not exist; send createSurface first`);
|
|
95
|
+
switch (kind) {
|
|
96
|
+
case "deleteSurface": return OK;
|
|
97
|
+
case "updateDataModel": return validateDataModelUpdate(payload);
|
|
98
|
+
case "updateComponents": return validateComponents(payload.components, "updateComponents.components", options);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function versionProblem(version) {
|
|
102
|
+
return typeof version === "string" ? `unsupported version "${version}"; use "${require_types.A2UI_VERSION}"` : `operation needs "version":"${require_types.A2UI_VERSION}"`;
|
|
103
|
+
}
|
|
104
|
+
/** A `catalogId` can name the catalog of the run or one of its bases. */
|
|
105
|
+
function catalogProblem(catalogId, manifest) {
|
|
106
|
+
const ids = manifest.catalogIds ?? [manifest.catalogId];
|
|
107
|
+
if (catalogId === void 0 || ids.includes(catalogId)) return;
|
|
108
|
+
const names = ids.map((id) => `"${id}"`).join(", ");
|
|
109
|
+
const use = ids.length === 1 ? names : `one of: ${names}`;
|
|
110
|
+
return `unknown catalog "${String(catalogId)}"; leave out catalogId or use ${use}`;
|
|
111
|
+
}
|
|
112
|
+
function validateCreateSurface(payload, surfaceId, options) {
|
|
113
|
+
if (options.knownSurfaces.has(surfaceId)) return fail(`surface "${surfaceId}" already exists`);
|
|
114
|
+
if (options.createdSurfaces?.has(surfaceId)) return fail(`surface id "${surfaceId}" was used before; use a new surfaceId`);
|
|
115
|
+
const catalogReason = catalogProblem(payload.catalogId, options.manifest);
|
|
116
|
+
if (catalogReason) return fail(catalogReason);
|
|
117
|
+
const { dataModel, components } = payload;
|
|
118
|
+
if (dataModel !== void 0 && !require_is_record.isRecord(dataModel)) return fail("createSurface.dataModel must be a JSON object");
|
|
119
|
+
if (components === void 0) return OK;
|
|
120
|
+
return validateComponents(components, "createSurface.components", options);
|
|
121
|
+
}
|
|
122
|
+
function validateRendererFunctionCall(payload, options) {
|
|
123
|
+
if (!(require_is_record.isRecord(payload) && typeof payload.functionCallId === "string" && payload.functionCallId !== "")) return fail("callRendererFunction.functionCallId must be a non-empty string");
|
|
124
|
+
const reason = functionCallProblem(payload.callFunction, "callRendererFunction.callFunction", options);
|
|
125
|
+
return reason ? fail(reason) : OK;
|
|
126
|
+
}
|
|
127
|
+
const FORBIDDEN_SEGMENTS = new Set([
|
|
128
|
+
"__proto__",
|
|
129
|
+
"constructor",
|
|
130
|
+
"prototype"
|
|
131
|
+
]);
|
|
132
|
+
function decodeSegment(segment) {
|
|
133
|
+
return segment.replace(/~([01])/g, (_, g) => g === "1" ? "/" : "~");
|
|
134
|
+
}
|
|
135
|
+
function validateDataModelUpdate(payload) {
|
|
136
|
+
if (payload.value === void 0) return fail("updateDataModel.value is required; use \"value\":null to delete");
|
|
137
|
+
const path = payload.path ?? "/";
|
|
138
|
+
if (typeof path !== "string" || !path.startsWith("/")) return fail("updateDataModel.path must be an absolute JSON pointer");
|
|
139
|
+
if (path === "/") {
|
|
140
|
+
if (!require_is_record.isRecord(payload.value)) return fail("a root updateDataModel value must be a JSON object");
|
|
141
|
+
return OK;
|
|
142
|
+
}
|
|
143
|
+
const reason = pathProblem(path, "updateDataModel.path");
|
|
144
|
+
return reason ? fail(reason) : OK;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Checks the segments of an absolute JSON pointer ("/a/b") or of a path
|
|
148
|
+
* relative to a template item ("a/b"). `label` names the field in the reason.
|
|
149
|
+
*/
|
|
150
|
+
function pathProblem(path, label) {
|
|
151
|
+
const segments = (path.startsWith("/") ? path.slice(1) : path).split("/");
|
|
152
|
+
if (segments.some((segment) => segment === "")) return `${label} must not contain empty segments`;
|
|
153
|
+
const forbidden = segments.map(decodeSegment).find((segment) => FORBIDDEN_SEGMENTS.has(segment));
|
|
154
|
+
if (forbidden !== void 0) return `path segment "${forbidden}" is not allowed in ${label}`;
|
|
155
|
+
}
|
|
156
|
+
function validateComponents(components, label, options) {
|
|
157
|
+
if (!Array.isArray(components) || components.length === 0) return fail(`${label} must be a non-empty array`);
|
|
158
|
+
const ids = /* @__PURE__ */ new Set();
|
|
159
|
+
for (const node of components) {
|
|
160
|
+
if (!require_is_record.isRecord(node)) return fail("component must be a JSON object");
|
|
161
|
+
const { id } = node;
|
|
162
|
+
if (typeof id !== "string" || !id) return fail("id is required");
|
|
163
|
+
const reason = nodeProblem(node, id, options);
|
|
164
|
+
if (reason) return fail(`component "${id}": ${reason}`);
|
|
165
|
+
if (ids.has(id)) return fail(`component "${id}" appears twice in one update`);
|
|
166
|
+
ids.add(id);
|
|
167
|
+
}
|
|
168
|
+
return OK;
|
|
169
|
+
}
|
|
170
|
+
function nodeProblem(node, id, options) {
|
|
171
|
+
if (typeof node.component !== "string") return "component type is required";
|
|
172
|
+
const entry = ownValue(options.manifest.components, node.component);
|
|
173
|
+
if (!entry) return `unknown component "${node.component}"`;
|
|
174
|
+
const { allowedComponents } = options;
|
|
175
|
+
if (allowedComponents && !allowedComponents.names.has(node.component)) {
|
|
176
|
+
const names = [...allowedComponents.names].join(", ") || "none";
|
|
177
|
+
return `"${node.component}" is not allowed in target "${allowedComponents.target}"; use one of: ${names}`;
|
|
178
|
+
}
|
|
179
|
+
const { properties, required } = entry.props;
|
|
180
|
+
const propNames = require_is_record.isRecord(properties) ? Object.keys(properties) : [];
|
|
181
|
+
const props = Object.entries(node);
|
|
182
|
+
for (const [key, value] of props) {
|
|
183
|
+
if (key === "id" || key === "component") continue;
|
|
184
|
+
if (key === "catalogId") {
|
|
185
|
+
const reason = catalogProblem(value, options.manifest);
|
|
186
|
+
if (reason) return `${key}: ${reason}`;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (key === "metadata") {
|
|
190
|
+
if (!(require_is_record.isRecord(value) && (value.extensions === void 0 || require_is_record.isRecord(value.extensions)))) return "metadata must be { \"extensions\": { ... } }";
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (key === "accessibility") {
|
|
194
|
+
const reason = accessibilityProblem(value);
|
|
195
|
+
if (reason) return `${key}: ${reason}`;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (ownValue(entry.events, key)) {
|
|
199
|
+
const reason = actionBindingProblem(value, options);
|
|
200
|
+
if (reason) return `${key}: ${reason}`;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const slot = ownValue(entry.slots, key);
|
|
204
|
+
if (slot) {
|
|
205
|
+
const reason = slotProblem(value, slot, id);
|
|
206
|
+
if (reason) return `${key}: ${reason}`;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (!propNames.includes(key)) return `unknown prop "${key}"`;
|
|
210
|
+
if (isV09PathBinding(value)) return `${key}: ${V09_PATH_REASON}`;
|
|
211
|
+
const call = findForbiddenCall(value, { allowValueFunctions: true });
|
|
212
|
+
if (call !== void 0) return `${key}: ${call}; a data prop can call only ${VALUE_FUNCTION_LIST}`;
|
|
213
|
+
}
|
|
214
|
+
for (const ref of childRefsOf(entry.props)) {
|
|
215
|
+
const reason = childRefProblem(node, ref, id);
|
|
216
|
+
if (reason) return reason;
|
|
217
|
+
}
|
|
218
|
+
const requiredNames = Array.isArray(required) ? required : [];
|
|
219
|
+
for (const name of requiredNames) if (typeof name === "string" && !(name in node)) return `missing required prop "${name}"`;
|
|
220
|
+
}
|
|
221
|
+
const ACCESSIBILITY_KEYS = [
|
|
222
|
+
"label",
|
|
223
|
+
"description",
|
|
224
|
+
"live",
|
|
225
|
+
"hidden"
|
|
226
|
+
];
|
|
227
|
+
const LIVE_VALUES = [
|
|
228
|
+
"off",
|
|
229
|
+
"polite",
|
|
230
|
+
"assertive"
|
|
231
|
+
];
|
|
232
|
+
/**
|
|
233
|
+
* Checks the `accessibility` of a component envelope: `label` and
|
|
234
|
+
* `description` are strings or dynamic values, `live` is one of the ARIA
|
|
235
|
+
* values, `hidden` is a boolean or a dynamic value.
|
|
236
|
+
*/
|
|
237
|
+
function accessibilityProblem(value) {
|
|
238
|
+
if (!require_is_record.isRecord(value)) return "must be { \"label\": \"...\", \"description\": \"...\", \"live\": \"polite\", \"hidden\": false }";
|
|
239
|
+
const isDynamic = (field) => isPathBinding(field) || require_basic_functions.isCallBinding(field);
|
|
240
|
+
for (const [key, field] of Object.entries(value)) {
|
|
241
|
+
if (!ACCESSIBILITY_KEYS.includes(key)) return `unknown key "${key}"`;
|
|
242
|
+
if (isV09PathBinding(field)) return `${key}: ${V09_PATH_REASON}`;
|
|
243
|
+
if (isDynamic(field)) {
|
|
244
|
+
const call = findForbiddenCall(field, { allowValueFunctions: true });
|
|
245
|
+
if (call !== void 0) return `${key}: ${call}`;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (key === "live") {
|
|
249
|
+
if (typeof field !== "string" || !LIVE_VALUES.includes(field)) return `live must be one of ${LIVE_VALUES.join(", ")}`;
|
|
250
|
+
} else if (key === "hidden") {
|
|
251
|
+
if (typeof field !== "boolean") return "hidden must be a boolean";
|
|
252
|
+
} else if (typeof field !== "string") return `${key} must be a string`;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function actionBindingProblem(value, options) {
|
|
256
|
+
if (!require_is_record.isRecord(value)) return "must be an action object";
|
|
257
|
+
const keys = Object.keys(value);
|
|
258
|
+
if (!(keys.length === 1 && (keys[0] === "functionCall" || keys[0] === "event"))) return "must be { \"event\": ... } or { \"functionCall\": ... }";
|
|
259
|
+
if (keys[0] === "functionCall") return functionCallProblem(value.functionCall, "functionCall", options);
|
|
260
|
+
return eventProblem(value.event);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Checks `{ "@call": "<function>", "args": { ... } }`. `label` names the
|
|
264
|
+
* field in the reason.
|
|
265
|
+
*/
|
|
266
|
+
function functionCallProblem(call, label, options) {
|
|
267
|
+
if (!require_is_record.isRecord(call)) return `${label} must be { "@call": "<function>", "args": { ... } }`;
|
|
268
|
+
const name = call["@call"];
|
|
269
|
+
if (typeof name !== "string" || !name) return typeof call.call === "string" ? `${label} names the function with "@call", not "call"` : `${label}["@call"] must be a function name`;
|
|
270
|
+
const { args } = call;
|
|
271
|
+
if (args !== void 0 && !require_is_record.isRecord(args)) return `${label}.args must be an object`;
|
|
272
|
+
const catalogReason = catalogProblem(call.catalogId, options.manifest);
|
|
273
|
+
if (catalogReason) return `${label}: ${catalogReason}`;
|
|
274
|
+
const argEntries = Object.entries(args ?? {});
|
|
275
|
+
for (const [key, arg] of argEntries) if (isV09PathBinding(arg)) return `${label}.args.${key}: ${V09_PATH_REASON}`;
|
|
276
|
+
const appFunctions = options.functions;
|
|
277
|
+
const declared = appFunctions !== void 0 && hasOwn(appFunctions, name) ? appFunctions[name] : void 0;
|
|
278
|
+
const builtIn = declared === void 0 && hasOwn(require_basic_functions.GENERATIVE_UI_FUNCTIONS, name) ? require_basic_functions.GENERATIVE_UI_FUNCTIONS[name] : void 0;
|
|
279
|
+
if (declared === void 0 && builtIn === void 0) return appFunctions === void 0 ? void 0 : `function "${name}" is not exposed to generative UI`;
|
|
280
|
+
if (declared === void 0 && appFunctions === void 0) return void 0;
|
|
281
|
+
return callerProblem(name, declared ?? builtIn, label.startsWith("callRendererFunction") ? "agent" : "renderer");
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Why `caller` may not call a function with these permissions, or
|
|
285
|
+
* `undefined` when it may. The dispatcher gives the same reasons at run
|
|
286
|
+
* time, for the calls that are not validated against a function list.
|
|
287
|
+
*/
|
|
288
|
+
function callerProblem(name, fn, caller) {
|
|
289
|
+
const callers = require_basic_functions.functionCallers(fn);
|
|
290
|
+
if (caller === "agent" && !callers.agent) return fn.requiresUserActivation ? `function "${name}" runs only from a user gesture; the agent cannot call it` : `function "${name}" runs only in the UI (allowedCallers "rendererOnly"); the agent cannot call it`;
|
|
291
|
+
if (caller === "renderer" && !callers.renderer) return `function "${name}" runs only for the agent (allowedCallers "agentOnly"); the UI cannot call it`;
|
|
292
|
+
}
|
|
293
|
+
function eventProblem(event) {
|
|
294
|
+
if (!require_is_record.isRecord(event) || typeof event.name !== "string" || !event.name) return "event.name is required";
|
|
295
|
+
const { userMessage, context } = event;
|
|
296
|
+
if (!(userMessage === void 0 || typeof userMessage === "string" || isPathBinding(userMessage) || require_is_record.isRecord(userMessage) && typeof userMessage["@call"] === "string" && findForbiddenCall(userMessage, { allowValueFunctions: true }) === void 0)) return isV09PathBinding(userMessage) ? `event.userMessage: ${V09_PATH_REASON}` : "event.userMessage must be a string, {\"@path\": \"/x\"} or a function value";
|
|
297
|
+
if (context === void 0) return void 0;
|
|
298
|
+
if (!require_is_record.isRecord(context)) return "event.context must be an object";
|
|
299
|
+
const entries = Object.entries(context);
|
|
300
|
+
for (const [key, entry] of entries) {
|
|
301
|
+
if (isV09PathBinding(entry)) return `event.context.${key}: ${V09_PATH_REASON}`;
|
|
302
|
+
const call = findForbiddenCall(entry, { allowValueFunctions: true });
|
|
303
|
+
if (call !== void 0) return `event.context.${key}: ${call}; a context value can call only ${VALUE_FUNCTION_LIST}`;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function slotProblem(value, kind, ownId) {
|
|
307
|
+
const isTemplate = require_is_record.isRecord(value) && "componentId" in value;
|
|
308
|
+
if (kind === "single" && isTemplate) return "a template { \"componentId\", \"path\" } is only allowed in a \"multiple\" slot";
|
|
309
|
+
if (kind === "multiple" && isTemplate) return templateProblem(value, ownId);
|
|
310
|
+
if (kind === "multiple" && !Array.isArray(value)) return "must be an array of component ids or a template { \"componentId\", \"path\" }";
|
|
311
|
+
const ids = kind === "multiple" && Array.isArray(value) ? value : [value];
|
|
312
|
+
for (const id of ids) {
|
|
313
|
+
if (typeof id !== "string" || !id) return "must reference component ids (strings)";
|
|
314
|
+
if (id === ownId) return "a component cannot be its own child";
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function templateProblem(template, ownId) {
|
|
318
|
+
if (!Object.keys(template).every((key) => key === "componentId" || key === "path")) return "a template takes only \"componentId\" and \"path\"";
|
|
319
|
+
const { componentId, path } = template;
|
|
320
|
+
if (typeof componentId !== "string" || !componentId) return "template componentId must be a component id";
|
|
321
|
+
if (componentId === ownId) return "a component cannot be its own child";
|
|
322
|
+
if (typeof path !== "string" || !path) return "template path must be a JSON pointer or a relative path";
|
|
323
|
+
return pathProblem(path, "template path");
|
|
324
|
+
}
|
|
325
|
+
const VALUE_FUNCTION_LIST = `@index and the built-in functions (${Object.keys(require_basic_functions.GENERATIVE_UI_FUNCTIONS).filter(require_basic_functions.isA2UIValueFunction).join(", ")})`;
|
|
326
|
+
/**
|
|
327
|
+
* The reason for the first function call in `value` that a data value may
|
|
328
|
+
* not make, at any depth, or `undefined`. A data value may call `@index`
|
|
329
|
+
* and, with `allowValueFunctions`, the basic functions that need no user
|
|
330
|
+
* gesture; the renderer resolves those. An app function, `openUrl`, or an
|
|
331
|
+
* event binding is not a value.
|
|
332
|
+
*/
|
|
333
|
+
function findForbiddenCall(value, options) {
|
|
334
|
+
if (Array.isArray(value)) {
|
|
335
|
+
for (const item of value) {
|
|
336
|
+
const reason = findForbiddenCall(item, options);
|
|
337
|
+
if (reason !== void 0) return reason;
|
|
338
|
+
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (!require_is_record.isRecord(value)) return void 0;
|
|
342
|
+
if (isIndexCall(value)) return void 0;
|
|
343
|
+
if (value["@call"] === "@index") return "@index takes only an offset";
|
|
344
|
+
if (typeof value.call === "string") return "name the function with \"@call\", not \"call\"";
|
|
345
|
+
if ("functionCall" in value) return "an event binding is not a value";
|
|
346
|
+
const name = value["@call"];
|
|
347
|
+
if (typeof name === "string") {
|
|
348
|
+
if (!options.allowValueFunctions || !require_basic_functions.isA2UIValueFunction(name)) return require_basic_functions.isA2UIBasicFunction(name) ? `"${name}" runs only from an event` : `"${name}" is not a basic function`;
|
|
349
|
+
const args = value.args;
|
|
350
|
+
if (args !== void 0 && !require_is_record.isRecord(args)) return `the args of "${name}" must be an object`;
|
|
351
|
+
return args === void 0 ? void 0 : findForbiddenCall(args, options);
|
|
352
|
+
}
|
|
353
|
+
for (const item of Object.values(value)) {
|
|
354
|
+
const reason = findForbiddenCall(item, options);
|
|
355
|
+
if (reason !== void 0) return reason;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const childRefCache = /* @__PURE__ */ new WeakMap();
|
|
359
|
+
/** The component references of a props schema, computed once per schema. */
|
|
360
|
+
function childRefsOf(props) {
|
|
361
|
+
let refs = childRefCache.get(props);
|
|
362
|
+
if (!refs) {
|
|
363
|
+
refs = require_child_refs.collectChildRefs(props);
|
|
364
|
+
childRefCache.set(props, refs);
|
|
365
|
+
}
|
|
366
|
+
return refs;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Checks the component ids at one reference of a node: a typed `child()`
|
|
370
|
+
* or `childList()` prop, at any depth. A missing key is fine; the props
|
|
371
|
+
* schema reports a required one.
|
|
372
|
+
*/
|
|
373
|
+
function childRefProblem(node, ref, ownId) {
|
|
374
|
+
const label = ref.path.filter((step) => step !== "*").join(".");
|
|
375
|
+
const isDynamic = (value) => isPathBinding(value) || require_is_record.isRecord(value) && "@call" in value;
|
|
376
|
+
const visit = (value, steps) => {
|
|
377
|
+
if (isDynamic(value)) return void 0;
|
|
378
|
+
if (steps.length === 0) {
|
|
379
|
+
const reason = slotProblem(value, ref.kind, ownId);
|
|
380
|
+
return reason ? `${label}: ${reason}` : void 0;
|
|
381
|
+
}
|
|
382
|
+
const [head, ...rest] = steps;
|
|
383
|
+
if (head === "*") {
|
|
384
|
+
if (!Array.isArray(value)) return void 0;
|
|
385
|
+
for (const item of value) {
|
|
386
|
+
const reason = visit(item, rest);
|
|
387
|
+
if (reason) return reason;
|
|
388
|
+
}
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (!require_is_record.isRecord(value) || !hasOwn(value, head)) return void 0;
|
|
392
|
+
return visit(value[head], rest);
|
|
393
|
+
};
|
|
394
|
+
return visit(node, ref.path);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
//#endregion
|
|
398
|
+
exports.callerProblem = callerProblem;
|
|
399
|
+
exports.getOperationKind = getOperationKind;
|
|
400
|
+
exports.getOperationSurfaceId = getOperationSurfaceId;
|
|
401
|
+
exports.isActionBinding = isActionBinding;
|
|
402
|
+
exports.isIndexCall = isIndexCall;
|
|
403
|
+
exports.isPathBinding = isPathBinding;
|
|
404
|
+
exports.validateOperation = validateOperation;
|
|
405
|
+
//# sourceMappingURL=validate-operation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-operation.cjs","names":["isRecord","A2UI_VERSION","GENERATIVE_UI_AGENT_FUNCTION_CALLS","GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON","isCallBinding","GENERATIVE_UI_FUNCTIONS","functionCallers","isA2UIValueFunction","isA2UIBasicFunction","collectChildRefs"],"sources":["../../src/generative-ui/validate-operation.ts"],"sourcesContent":["import {\n GENERATIVE_UI_FUNCTIONS,\n functionCallers,\n isA2UIBasicFunction,\n isA2UIValueFunction,\n isCallBinding,\n} from \"./basic-functions\";\nimport type { A2UIFunctionPermissions } from \"./basic-functions\";\nimport { collectChildRefs } from \"./child-refs\";\nimport type { ChildRef } from \"./child-refs\";\nimport {\n A2UI_VERSION,\n GENERATIVE_UI_AGENT_FUNCTION_CALLS,\n GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON,\n} from \"./types\";\nimport type {\n GenerativeUIActionBinding,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\nimport { isRecord } from \"./is-record\";\n\nexport type OperationValidation = { ok: true } | { ok: false; reason: string };\n\nconst OPERATION_KINDS = [\n \"createSurface\",\n \"updateComponents\",\n \"updateDataModel\",\n \"deleteSurface\",\n \"callRendererFunction\",\n] as const;\nexport type OperationKind = (typeof OPERATION_KINDS)[number];\n\nexport interface ValidateOperationOptions {\n manifest: Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"catalogIds\" | \"components\"\n >;\n /**\n * Surfaces that exist before this operation is applied. `updateComponents`,\n * `updateDataModel`, and `deleteSurface` need their surface in this set.\n */\n knownSurfaces: ReadonlySet<string>;\n /**\n * Surface ids that were created before, also the deleted ones. A surface id\n * is unique for the life of the page, so `createSurface` rejects them.\n */\n createdSurfaces?: ReadonlySet<string>;\n /**\n * UI-callable functions. When provided, `functionCall` bindings and\n * `callRendererFunction` must name one of them. The client omits it:\n * availability is checked when the function runs.\n */\n functions?: GenerativeUIManifest[\"functions\"];\n /**\n * The components that the target of the surface takes. When provided,\n * `updateComponents` and the inline components of `createSurface` can use\n * only these names. Left out, every catalog component is allowed.\n */\n allowedComponents?: AllowedComponents;\n}\n\n/** The component names that one target takes. */\nexport interface AllowedComponents {\n /** The target id, for the reason of a rejection. */\n target: string;\n names: ReadonlySet<string>;\n}\n\nconst OK: OperationValidation = { ok: true };\nconst fail = (reason: string): OperationValidation => ({ ok: false, reason });\n\n/** Models write the v0.9 binding from habit. The reason names the v1.0 form. */\nconst V09_PATH_REASON = 'bind data with {\"@path\": \"/x\"}, not {\"path\": \"/x\"}';\n\nfunction hasOwn(record: object, key: string) {\n return Object.prototype.hasOwnProperty.call(record, key);\n}\n\n/**\n * Reads an own property only. Model-controlled keys such as \"constructor\" or\n * \"toString\" must not resolve to Object.prototype members.\n */\nfunction ownValue<T>(record: Record<string, T> | undefined, key: string) {\n return record && hasOwn(record, key) ? record[key] : undefined;\n}\n\nexport function getOperationKind(op: unknown) {\n if (!isRecord(op)) return undefined;\n const kinds = OPERATION_KINDS.filter((kind) => kind in op);\n return kinds.length === 1 ? kinds[0] : undefined;\n}\n\n/** The `surfaceId` of a surface operation. `callRendererFunction` has none. */\nexport function getOperationSurfaceId(op: unknown) {\n if (!isRecord(op)) return undefined;\n const kind = getOperationKind(op);\n if (!kind) return undefined;\n const payload = op[kind];\n return isRecord(payload) && typeof payload.surfaceId === \"string\"\n ? payload.surfaceId\n : undefined;\n}\n\n/** `{ \"@path\": \"/x\" }`: a data-model binding. */\nexport function isPathBinding(value: unknown): value is { \"@path\": string } {\n return (\n isRecord(value) &&\n typeof value[\"@path\"] === \"string\" &&\n Object.keys(value).length === 1\n );\n}\n\n/** `{ \"path\": \"/x\" }`: the v0.9 binding, which v1.0 does not accept. */\nfunction isV09PathBinding(value: unknown) {\n return (\n isRecord(value) &&\n typeof value.path === \"string\" &&\n Object.keys(value).length === 1\n );\n}\n\n/**\n * `{ \"@call\": \"@index\" }` or `{ \"@call\": \"@index\", \"args\": { \"offset\": 1 } }`:\n * the 0-based position of the item in a template, plus `offset`.\n */\nexport function isIndexCall(\n value: unknown,\n): value is { \"@call\": \"@index\"; args?: { offset?: number } } {\n if (!isRecord(value) || value[\"@call\"] !== \"@index\") return false;\n const hasOnlyCallKeys = Object.keys(value).every(\n (key) => key === \"@call\" || key === \"args\",\n );\n if (!hasOnlyCallKeys) return false;\n const { args } = value;\n if (args === undefined) return true;\n return (\n isRecord(args) &&\n Object.keys(args).every((key) => key === \"offset\") &&\n (args.offset === undefined || Number.isFinite(args.offset))\n );\n}\n\nexport function isActionBinding(\n value: unknown,\n): value is GenerativeUIActionBinding {\n return (\n isRecord(value) &&\n Object.keys(value).length === 1 &&\n (isRecord(value.functionCall) || isRecord(value.event))\n );\n}\n\n/**\n * Checks one A2UI v1.0 operation against the catalog and the surfaces that\n * exist. The operation comes from the model, so nothing in it is trusted.\n *\n * @param op - The operation, as parsed from JSON.\n * @param options - The catalog, the known and created surfaces, the\n * functions that the UI can call, and the components that the target of\n * the surface takes.\n *\n * @example\n * ```ts\n * const result = validateOperation(op, { manifest, knownSurfaces });\n * if (!result.ok) rejected.push({ index, reason: result.reason });\n * ```\n */\nexport function validateOperation(\n op: unknown,\n options: ValidateOperationOptions,\n) {\n if (!isRecord(op)) return fail(\"operation must be a JSON object\");\n if (op.version !== A2UI_VERSION) return fail(versionProblem(op.version));\n const kind = getOperationKind(op);\n if (!kind) {\n return fail(\n `operation must contain exactly one of ${OPERATION_KINDS.join(\", \")}`,\n );\n }\n const payload = op[kind];\n if (kind === \"callRendererFunction\") {\n if (!GENERATIVE_UI_AGENT_FUNCTION_CALLS) {\n return fail(GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON);\n }\n return validateRendererFunctionCall(payload, options);\n }\n if (\n !isRecord(payload) ||\n typeof payload.surfaceId !== \"string\" ||\n !payload.surfaceId\n ) {\n return fail(`${kind}.surfaceId is required`);\n }\n const surfaceId = payload.surfaceId;\n if (kind === \"createSurface\") {\n return validateCreateSurface(payload, surfaceId, options);\n }\n if (!options.knownSurfaces.has(surfaceId)) {\n return fail(\n `surface \"${surfaceId}\" does not exist; send createSurface first`,\n );\n }\n switch (kind) {\n case \"deleteSurface\":\n return OK;\n case \"updateDataModel\":\n return validateDataModelUpdate(payload);\n case \"updateComponents\":\n return validateComponents(\n payload.components,\n \"updateComponents.components\",\n options,\n );\n }\n}\n\nfunction versionProblem(version: unknown) {\n return typeof version === \"string\"\n ? `unsupported version \"${version}\"; use \"${A2UI_VERSION}\"`\n : `operation needs \"version\":\"${A2UI_VERSION}\"`;\n}\n\n/** A `catalogId` can name the catalog of the run or one of its bases. */\nfunction catalogProblem(\n catalogId: unknown,\n manifest: ValidateOperationOptions[\"manifest\"],\n) {\n const ids = manifest.catalogIds ?? [manifest.catalogId];\n if (catalogId === undefined || ids.includes(catalogId as string)) {\n return undefined;\n }\n const names = ids.map((id) => `\"${id}\"`).join(\", \");\n const use = ids.length === 1 ? names : `one of: ${names}`;\n return `unknown catalog \"${String(catalogId)}\"; leave out catalogId or use ${use}`;\n}\n\nfunction validateCreateSurface(\n payload: Record<string, unknown>,\n surfaceId: string,\n options: ValidateOperationOptions,\n) {\n if (options.knownSurfaces.has(surfaceId)) {\n return fail(`surface \"${surfaceId}\" already exists`);\n }\n if (options.createdSurfaces?.has(surfaceId)) {\n return fail(\n `surface id \"${surfaceId}\" was used before; use a new surfaceId`,\n );\n }\n const catalogReason = catalogProblem(payload.catalogId, options.manifest);\n if (catalogReason) return fail(catalogReason);\n const { dataModel, components } = payload;\n if (dataModel !== undefined && !isRecord(dataModel)) {\n return fail(\"createSurface.dataModel must be a JSON object\");\n }\n if (components === undefined) return OK;\n return validateComponents(components, \"createSurface.components\", options);\n}\n\nfunction validateRendererFunctionCall(\n payload: unknown,\n options: ValidateOperationOptions,\n) {\n const hasCallId =\n isRecord(payload) &&\n typeof payload.functionCallId === \"string\" &&\n payload.functionCallId !== \"\";\n if (!hasCallId) {\n return fail(\n \"callRendererFunction.functionCallId must be a non-empty string\",\n );\n }\n const reason = functionCallProblem(\n payload.callFunction,\n \"callRendererFunction.callFunction\",\n options,\n );\n return reason ? fail(reason) : OK;\n}\n\nconst FORBIDDEN_SEGMENTS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\nfunction decodeSegment(segment: string) {\n return segment.replace(/~([01])/g, (_, g) => (g === \"1\" ? \"/\" : \"~\"));\n}\n\nfunction validateDataModelUpdate(payload: Record<string, unknown>) {\n if (payload.value === undefined) {\n return fail(\n 'updateDataModel.value is required; use \"value\":null to delete',\n );\n }\n const path = payload.path ?? \"/\";\n if (typeof path !== \"string\" || !path.startsWith(\"/\")) {\n return fail(\"updateDataModel.path must be an absolute JSON pointer\");\n }\n if (path === \"/\") {\n // Paths such as \"/items\" point into the root, so the root must be an object.\n if (!isRecord(payload.value)) {\n return fail(\"a root updateDataModel value must be a JSON object\");\n }\n return OK;\n }\n const reason = pathProblem(path, \"updateDataModel.path\");\n return reason ? fail(reason) : OK;\n}\n\n/**\n * Checks the segments of an absolute JSON pointer (\"/a/b\") or of a path\n * relative to a template item (\"a/b\"). `label` names the field in the reason.\n */\nfunction pathProblem(path: string, label: string) {\n const relative = path.startsWith(\"/\") ? path.slice(1) : path;\n const segments = relative.split(\"/\");\n if (segments.some((segment) => segment === \"\")) {\n return `${label} must not contain empty segments`;\n }\n const decoded = segments.map(decodeSegment);\n const forbidden = decoded.find((segment) => FORBIDDEN_SEGMENTS.has(segment));\n if (forbidden !== undefined) {\n return `path segment \"${forbidden}\" is not allowed in ${label}`;\n }\n return undefined;\n}\n\nfunction validateComponents(\n components: unknown,\n label: string,\n options: ValidateOperationOptions,\n) {\n if (!Array.isArray(components) || components.length === 0) {\n return fail(`${label} must be a non-empty array`);\n }\n const ids = new Set<string>();\n for (const node of components) {\n if (!isRecord(node)) return fail(\"component must be a JSON object\");\n const { id } = node;\n if (typeof id !== \"string\" || !id) return fail(\"id is required\");\n const reason = nodeProblem(node, id, options);\n if (reason) return fail(`component \"${id}\": ${reason}`);\n if (ids.has(id)) {\n return fail(`component \"${id}\" appears twice in one update`);\n }\n ids.add(id);\n }\n return OK;\n}\n\nfunction nodeProblem(\n node: Record<string, unknown>,\n id: string,\n options: ValidateOperationOptions,\n) {\n if (typeof node.component !== \"string\") return \"component type is required\";\n const entry = ownValue(options.manifest.components, node.component);\n if (!entry) return `unknown component \"${node.component}\"`;\n const { allowedComponents } = options;\n if (allowedComponents && !allowedComponents.names.has(node.component)) {\n const names = [...allowedComponents.names].join(\", \") || \"none\";\n return `\"${node.component}\" is not allowed in target \"${allowedComponents.target}\"; use one of: ${names}`;\n }\n const { properties, required } = entry.props;\n const propNames = isRecord(properties) ? Object.keys(properties) : [];\n const props = Object.entries(node);\n for (const [key, value] of props) {\n if (key === \"id\" || key === \"component\") continue;\n if (key === \"catalogId\") {\n const reason = catalogProblem(value, options.manifest);\n if (reason) return `${key}: ${reason}`;\n continue;\n }\n if (key === \"metadata\") {\n const isMetadata =\n isRecord(value) &&\n (value.extensions === undefined || isRecord(value.extensions));\n if (!isMetadata) return 'metadata must be { \"extensions\": { ... } }';\n continue;\n }\n if (key === \"accessibility\") {\n const reason = accessibilityProblem(value);\n if (reason) return `${key}: ${reason}`;\n continue;\n }\n if (ownValue(entry.events, key)) {\n const reason = actionBindingProblem(value, options);\n if (reason) return `${key}: ${reason}`;\n continue;\n }\n const slot = ownValue(entry.slots, key);\n if (slot) {\n const reason = slotProblem(value, slot, id);\n if (reason) return `${key}: ${reason}`;\n continue;\n }\n if (!propNames.includes(key)) return `unknown prop \"${key}\"`;\n if (isV09PathBinding(value)) return `${key}: ${V09_PATH_REASON}`;\n const call = findForbiddenCall(value, { allowValueFunctions: true });\n if (call !== undefined) {\n return `${key}: ${call}; a data prop can call only ${VALUE_FUNCTION_LIST}`;\n }\n }\n for (const ref of childRefsOf(entry.props)) {\n const reason = childRefProblem(node, ref, id);\n if (reason) return reason;\n }\n const requiredNames = Array.isArray(required) ? required : [];\n for (const name of requiredNames) {\n if (typeof name === \"string\" && !(name in node)) {\n return `missing required prop \"${name}\"`;\n }\n }\n return undefined;\n}\n\nconst ACCESSIBILITY_KEYS = [\"label\", \"description\", \"live\", \"hidden\"];\nconst LIVE_VALUES = [\"off\", \"polite\", \"assertive\"];\n\n/**\n * Checks the `accessibility` of a component envelope: `label` and\n * `description` are strings or dynamic values, `live` is one of the ARIA\n * values, `hidden` is a boolean or a dynamic value.\n */\nfunction accessibilityProblem(value: unknown) {\n if (!isRecord(value)) {\n return 'must be { \"label\": \"...\", \"description\": \"...\", \"live\": \"polite\", \"hidden\": false }';\n }\n const isDynamic = (field: unknown) =>\n isPathBinding(field) || isCallBinding(field);\n for (const [key, field] of Object.entries(value)) {\n if (!ACCESSIBILITY_KEYS.includes(key)) return `unknown key \"${key}\"`;\n if (isV09PathBinding(field)) return `${key}: ${V09_PATH_REASON}`;\n if (isDynamic(field)) {\n const call = findForbiddenCall(field, { allowValueFunctions: true });\n if (call !== undefined) return `${key}: ${call}`;\n continue;\n }\n if (key === \"live\") {\n if (typeof field !== \"string\" || !LIVE_VALUES.includes(field)) {\n return `live must be one of ${LIVE_VALUES.join(\", \")}`;\n }\n } else if (key === \"hidden\") {\n if (typeof field !== \"boolean\") return \"hidden must be a boolean\";\n } else if (typeof field !== \"string\") {\n return `${key} must be a string`;\n }\n }\n return undefined;\n}\n\nfunction actionBindingProblem(\n value: unknown,\n options: ValidateOperationOptions,\n) {\n if (!isRecord(value)) return \"must be an action object\";\n const keys = Object.keys(value);\n const isOneAction =\n keys.length === 1 && (keys[0] === \"functionCall\" || keys[0] === \"event\");\n if (!isOneAction) {\n return 'must be { \"event\": ... } or { \"functionCall\": ... }';\n }\n if (keys[0] === \"functionCall\") {\n return functionCallProblem(value.functionCall, \"functionCall\", options);\n }\n return eventProblem(value.event);\n}\n\n/**\n * Checks `{ \"@call\": \"<function>\", \"args\": { ... } }`. `label` names the\n * field in the reason.\n */\nfunction functionCallProblem(\n call: unknown,\n label: string,\n options: ValidateOperationOptions,\n) {\n if (!isRecord(call)) {\n return `${label} must be { \"@call\": \"<function>\", \"args\": { ... } }`;\n }\n const name = call[\"@call\"];\n if (typeof name !== \"string\" || !name) {\n return typeof call.call === \"string\"\n ? `${label} names the function with \"@call\", not \"call\"`\n : `${label}[\"@call\"] must be a function name`;\n }\n const { args } = call;\n if (args !== undefined && !isRecord(args)) {\n return `${label}.args must be an object`;\n }\n const catalogReason = catalogProblem(call.catalogId, options.manifest);\n if (catalogReason) return `${label}: ${catalogReason}`;\n const argEntries = Object.entries(args ?? {});\n for (const [key, arg] of argEntries) {\n if (isV09PathBinding(arg))\n return `${label}.args.${key}: ${V09_PATH_REASON}`;\n }\n // A function the app declares under a built-in name is the app's; the\n // restrictions of the built-in do not apply to it.\n const appFunctions = options.functions;\n const declared =\n appFunctions !== undefined && hasOwn(appFunctions, name)\n ? appFunctions[name]\n : undefined;\n const builtIn =\n declared === undefined && hasOwn(GENERATIVE_UI_FUNCTIONS, name)\n ? GENERATIVE_UI_FUNCTIONS[name]\n : undefined;\n if (declared === undefined && builtIn === undefined) {\n return appFunctions === undefined\n ? undefined\n : `function \"${name}\" is not exposed to generative UI`;\n }\n // Without the list of app functions, a built-in name may be the app's own\n // function, so the dispatcher applies the restriction when the call runs.\n if (declared === undefined && appFunctions === undefined) return undefined;\n return callerProblem(\n name,\n declared ?? builtIn!,\n label.startsWith(\"callRendererFunction\") ? \"agent\" : \"renderer\",\n );\n}\n\n/**\n * Why `caller` may not call a function with these permissions, or\n * `undefined` when it may. The dispatcher gives the same reasons at run\n * time, for the calls that are not validated against a function list.\n */\nexport function callerProblem(\n name: string,\n fn: A2UIFunctionPermissions,\n caller: \"agent\" | \"renderer\",\n) {\n const callers = functionCallers(fn);\n if (caller === \"agent\" && !callers.agent) {\n return fn.requiresUserActivation\n ? `function \"${name}\" runs only from a user gesture; the agent cannot call it`\n : `function \"${name}\" runs only in the UI (allowedCallers \"rendererOnly\"); the agent cannot call it`;\n }\n if (caller === \"renderer\" && !callers.renderer) {\n return `function \"${name}\" runs only for the agent (allowedCallers \"agentOnly\"); the UI cannot call it`;\n }\n return undefined;\n}\n\nfunction eventProblem(event: unknown) {\n if (!isRecord(event) || typeof event.name !== \"string\" || !event.name) {\n return \"event.name is required\";\n }\n const { userMessage, context } = event;\n const isUserMessage =\n userMessage === undefined ||\n typeof userMessage === \"string\" ||\n isPathBinding(userMessage) ||\n (isRecord(userMessage) &&\n typeof userMessage[\"@call\"] === \"string\" &&\n findForbiddenCall(userMessage, { allowValueFunctions: true }) ===\n undefined);\n if (!isUserMessage) {\n return isV09PathBinding(userMessage)\n ? `event.userMessage: ${V09_PATH_REASON}`\n : 'event.userMessage must be a string, {\"@path\": \"/x\"} or a function value';\n }\n if (context === undefined) return undefined;\n if (!isRecord(context)) return \"event.context must be an object\";\n const entries = Object.entries(context);\n for (const [key, entry] of entries) {\n if (isV09PathBinding(entry)) {\n return `event.context.${key}: ${V09_PATH_REASON}`;\n }\n const call = findForbiddenCall(entry, { allowValueFunctions: true });\n if (call !== undefined) {\n return `event.context.${key}: ${call}; a context value can call only ${VALUE_FUNCTION_LIST}`;\n }\n }\n return undefined;\n}\n\nfunction slotProblem(\n value: unknown,\n kind: \"single\" | \"multiple\",\n ownId: string,\n) {\n const isTemplate = isRecord(value) && \"componentId\" in value;\n if (kind === \"single\" && isTemplate) {\n return 'a template { \"componentId\", \"path\" } is only allowed in a \"multiple\" slot';\n }\n if (kind === \"multiple\" && isTemplate) {\n return templateProblem(value, ownId);\n }\n if (kind === \"multiple\" && !Array.isArray(value)) {\n return 'must be an array of component ids or a template { \"componentId\", \"path\" }';\n }\n const ids = kind === \"multiple\" && Array.isArray(value) ? value : [value];\n for (const id of ids) {\n if (typeof id !== \"string\" || !id)\n return \"must reference component ids (strings)\";\n if (id === ownId) return \"a component cannot be its own child\";\n }\n return undefined;\n}\n\nfunction templateProblem(template: Record<string, unknown>, ownId: string) {\n const hasOnlyTemplateKeys = Object.keys(template).every(\n (key) => key === \"componentId\" || key === \"path\",\n );\n if (!hasOnlyTemplateKeys) {\n return 'a template takes only \"componentId\" and \"path\"';\n }\n const { componentId, path } = template;\n if (typeof componentId !== \"string\" || !componentId) {\n return \"template componentId must be a component id\";\n }\n if (componentId === ownId) return \"a component cannot be its own child\";\n if (typeof path !== \"string\" || !path) {\n return \"template path must be a JSON pointer or a relative path\";\n }\n return pathProblem(path, \"template path\");\n}\n\nconst VALUE_FUNCTION_LIST = `@index and the built-in functions (${Object.keys(\n GENERATIVE_UI_FUNCTIONS,\n)\n .filter(isA2UIValueFunction)\n .join(\", \")})`;\n\n/**\n * The reason for the first function call in `value` that a data value may\n * not make, at any depth, or `undefined`. A data value may call `@index`\n * and, with `allowValueFunctions`, the basic functions that need no user\n * gesture; the renderer resolves those. An app function, `openUrl`, or an\n * event binding is not a value.\n */\n// The annotation is needed: the function calls itself in its return.\nfunction findForbiddenCall(\n value: unknown,\n options: { allowValueFunctions: boolean },\n): string | undefined {\n if (Array.isArray(value)) {\n for (const item of value) {\n const reason = findForbiddenCall(item, options);\n if (reason !== undefined) return reason;\n }\n return undefined;\n }\n if (!isRecord(value)) return undefined;\n if (isIndexCall(value)) return undefined;\n if (value[\"@call\"] === \"@index\") return \"@index takes only an offset\";\n if (typeof value.call === \"string\") {\n return 'name the function with \"@call\", not \"call\"';\n }\n if (\"functionCall\" in value) {\n return \"an event binding is not a value\";\n }\n const name = value[\"@call\"];\n if (typeof name === \"string\") {\n if (!options.allowValueFunctions || !isA2UIValueFunction(name)) {\n return isA2UIBasicFunction(name)\n ? `\"${name}\" runs only from an event`\n : `\"${name}\" is not a basic function`;\n }\n const args = value.args;\n if (args !== undefined && !isRecord(args)) {\n return `the args of \"${name}\" must be an object`;\n }\n return args === undefined ? undefined : findForbiddenCall(args, options);\n }\n for (const item of Object.values(value)) {\n const reason = findForbiddenCall(item, options);\n if (reason !== undefined) return reason;\n }\n return undefined;\n}\n\nconst childRefCache = new WeakMap<GenerativeUIJSONSchema, ChildRef[]>();\n\n/** The component references of a props schema, computed once per schema. */\nfunction childRefsOf(props: GenerativeUIJSONSchema) {\n let refs = childRefCache.get(props);\n if (!refs) {\n refs = collectChildRefs(props);\n childRefCache.set(props, refs);\n }\n return refs;\n}\n\n/**\n * Checks the component ids at one reference of a node: a typed `child()`\n * or `childList()` prop, at any depth. A missing key is fine; the props\n * schema reports a required one.\n */\nfunction childRefProblem(\n node: Record<string, unknown>,\n ref: ChildRef,\n ownId: string,\n): string | undefined {\n const label = ref.path.filter((step) => step !== \"*\").join(\".\");\n const isDynamic = (value: unknown) =>\n isPathBinding(value) || (isRecord(value) && \"@call\" in value);\n const visit = (\n value: unknown,\n steps: readonly string[],\n ): string | undefined => {\n // A bound or computed value, at the reference or above it, is known\n // only at render.\n if (isDynamic(value)) return undefined;\n if (steps.length === 0) {\n const reason = slotProblem(value, ref.kind, ownId);\n return reason ? `${label}: ${reason}` : undefined;\n }\n const [head, ...rest] = steps;\n if (head === \"*\") {\n if (!Array.isArray(value)) return undefined;\n for (const item of value) {\n const reason = visit(item, rest);\n if (reason) return reason;\n }\n return undefined;\n }\n if (!isRecord(value) || !hasOwn(value, head)) return undefined;\n return visit(value[head], rest);\n };\n return visit(node, ref.path);\n}\n"],"mappings":";;;;;;AAwBA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACD;AAuCD,MAAM,KAA0B,EAAE,IAAI,MAAM;AAC5C,MAAM,QAAQ,YAAyC;CAAE,IAAI;CAAO;CAAQ;;AAG5E,MAAM,kBAAkB;AAExB,SAAS,OAAO,QAAgB,KAAa;AAC3C,QAAO,OAAO,UAAU,eAAe,KAAK,QAAQ,IAAI;;;;;;AAO1D,SAAS,SAAY,QAAuC,KAAa;AACvE,QAAO,UAAU,OAAO,QAAQ,IAAI,GAAG,OAAO,OAAO;;AAGvD,SAAgB,iBAAiB,IAAa;AAC5C,KAAI,CAACA,2BAAS,GAAG,CAAE,QAAO;CAC1B,MAAM,QAAQ,gBAAgB,QAAQ,SAAS,QAAQ,GAAG;AAC1D,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;;;AAIzC,SAAgB,sBAAsB,IAAa;AACjD,KAAI,CAACA,2BAAS,GAAG,CAAE,QAAO;CAC1B,MAAM,OAAO,iBAAiB,GAAG;AACjC,KAAI,CAAC,KAAM,QAAO;CAClB,MAAM,UAAU,GAAG;AACnB,QAAOA,2BAAS,QAAQ,IAAI,OAAO,QAAQ,cAAc,WACrD,QAAQ,YACR;;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Q,MAAM,CAAE,QAAO;AAClC,QAAK,MAAM,QAAQ,OAAO;IACxB,MAAM,SAAS,MAAM,MAAM,KAAK;AAChC,QAAI,OAAQ,QAAO;;AAErB;;AAEF,MAAI,CAACA,2BAAS,MAAM,IAAI,CAAC,OAAO,OAAO,KAAK,CAAE,QAAO;AACrD,SAAO,MAAM,MAAM,OAAO,KAAK;;AAEjC,QAAO,MAAM,MAAM,IAAI,KAAK"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { A2UIFunctionPermissions } from "./basic-functions.cjs";
|
|
2
|
+
import { GenerativeUIActionBinding, GenerativeUIManifest } from "./types.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/generative-ui/validate-operation.d.ts
|
|
5
|
+
type OperationValidation = {
|
|
6
|
+
ok: true;
|
|
7
|
+
} | {
|
|
8
|
+
ok: false;
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
declare const OPERATION_KINDS: readonly ["createSurface", "updateComponents", "updateDataModel", "deleteSurface", "callRendererFunction"];
|
|
12
|
+
type OperationKind = (typeof OPERATION_KINDS)[number];
|
|
13
|
+
interface ValidateOperationOptions {
|
|
14
|
+
manifest: Pick<GenerativeUIManifest, "catalogId" | "catalogIds" | "components">;
|
|
15
|
+
/**
|
|
16
|
+
* Surfaces that exist before this operation is applied. `updateComponents`,
|
|
17
|
+
* `updateDataModel`, and `deleteSurface` need their surface in this set.
|
|
18
|
+
*/
|
|
19
|
+
knownSurfaces: ReadonlySet<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Surface ids that were created before, also the deleted ones. A surface id
|
|
22
|
+
* is unique for the life of the page, so `createSurface` rejects them.
|
|
23
|
+
*/
|
|
24
|
+
createdSurfaces?: ReadonlySet<string>;
|
|
25
|
+
/**
|
|
26
|
+
* UI-callable functions. When provided, `functionCall` bindings and
|
|
27
|
+
* `callRendererFunction` must name one of them. The client omits it:
|
|
28
|
+
* availability is checked when the function runs.
|
|
29
|
+
*/
|
|
30
|
+
functions?: GenerativeUIManifest["functions"];
|
|
31
|
+
/**
|
|
32
|
+
* The components that the target of the surface takes. When provided,
|
|
33
|
+
* `updateComponents` and the inline components of `createSurface` can use
|
|
34
|
+
* only these names. Left out, every catalog component is allowed.
|
|
35
|
+
*/
|
|
36
|
+
allowedComponents?: AllowedComponents;
|
|
37
|
+
}
|
|
38
|
+
/** The component names that one target takes. */
|
|
39
|
+
interface AllowedComponents {
|
|
40
|
+
/** The target id, for the reason of a rejection. */
|
|
41
|
+
target: string;
|
|
42
|
+
names: ReadonlySet<string>;
|
|
43
|
+
}
|
|
44
|
+
declare function getOperationKind(op: unknown): "createSurface" | "updateComponents" | "updateDataModel" | "deleteSurface" | "callRendererFunction" | undefined;
|
|
45
|
+
/** The `surfaceId` of a surface operation. `callRendererFunction` has none. */
|
|
46
|
+
declare function getOperationSurfaceId(op: unknown): string | undefined;
|
|
47
|
+
/** `{ "@path": "/x" }`: a data-model binding. */
|
|
48
|
+
declare function isPathBinding(value: unknown): value is {
|
|
49
|
+
"@path": string;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* `{ "@call": "@index" }` or `{ "@call": "@index", "args": { "offset": 1 } }`:
|
|
53
|
+
* the 0-based position of the item in a template, plus `offset`.
|
|
54
|
+
*/
|
|
55
|
+
declare function isIndexCall(value: unknown): value is {
|
|
56
|
+
"@call": "@index";
|
|
57
|
+
args?: {
|
|
58
|
+
offset?: number;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
declare function isActionBinding(value: unknown): value is GenerativeUIActionBinding;
|
|
62
|
+
/**
|
|
63
|
+
* Checks one A2UI v1.0 operation against the catalog and the surfaces that
|
|
64
|
+
* exist. The operation comes from the model, so nothing in it is trusted.
|
|
65
|
+
*
|
|
66
|
+
* @param op - The operation, as parsed from JSON.
|
|
67
|
+
* @param options - The catalog, the known and created surfaces, the
|
|
68
|
+
* functions that the UI can call, and the components that the target of
|
|
69
|
+
* the surface takes.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const result = validateOperation(op, { manifest, knownSurfaces });
|
|
74
|
+
* if (!result.ok) rejected.push({ index, reason: result.reason });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function validateOperation(op: unknown, options: ValidateOperationOptions): OperationValidation;
|
|
78
|
+
/**
|
|
79
|
+
* Why `caller` may not call a function with these permissions, or
|
|
80
|
+
* `undefined` when it may. The dispatcher gives the same reasons at run
|
|
81
|
+
* time, for the calls that are not validated against a function list.
|
|
82
|
+
*/
|
|
83
|
+
declare function callerProblem(name: string, fn: A2UIFunctionPermissions, caller: "agent" | "renderer"): string | undefined;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { AllowedComponents, OperationKind, OperationValidation, ValidateOperationOptions, callerProblem, getOperationKind, getOperationSurfaceId, isActionBinding, isIndexCall, isPathBinding, validateOperation };
|
|
86
|
+
//# sourceMappingURL=validate-operation.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-operation.d.cts","names":[],"sources":["../../src/generative-ui/validate-operation.ts"],"mappings":";;;;KAsBY,mBAAA;EAAwB,EAAA;AAAA;EAAe,EAAA;EAAW,MAAA;AAAA;AAAA,cAExD,eAAA;AAAA,KAOM,aAAA,WAAwB,eAAA;AAAA,UAEnB,wBAAA;EACf,QAAA,EAAU,IAAA,CACR,oBAAA;EAbgE;AAAW;;;EAoB7E,aAAA,EAAe,WAAA;EAZP;AACV;;;EAgBE,eAAA,GAAkB,WAAA;EAhB+B;AAEnD;;;;EAoBE,SAAA,GAAY,oBAAA;EAXG;;;;;EAiBf,iBAAA,GAAoB,iBAAA;AAAA;;UAIL,iBAAA;EArBf;EAuBA,MAAA;EACA,KAAA,EAAO,WAAA;AAAA;AAAA,iBAqBO,gBAAA,CAAiB,EAAA;;iBAOjB,qBAAA,CAAsB,EAAA;;iBAWtB,aAAA,CAAc,KAAA,YAAiB,KAAA;EAAW,OAAA;AAAA;;;;;iBAqB1C,WAAA,CACd,KAAA,YACC,KAAA;EAAW,OAAA;EAAmB,IAAA;IAAS,MAAA;EAAA;AAAA;AAAA,iBAe1B,eAAA,CACd,KAAA,YACC,KAAA,IAAS,yBAAA;;;AAnDZ;;;;;AAWA;;;;;;;;iBA+DgB,iBAAA,CACd,EAAA,WACA,OAAA,EAAS,wBAAA,GAAwB,mBAAA;AA5CnC;;;;;AAAA,iBAiZgB,aAAA,CACd,IAAA,UACA,EAAA,EAAI,uBAAA,EACJ,MAAA"}
|