@copilotkit/shared 1.84.2-canary.1793809041 → 1.85.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/README.md +1 -1
- package/dist/a2ui-catalog-schema.cjs +14 -5
- package/dist/a2ui-catalog-schema.cjs.map +1 -1
- package/dist/a2ui-catalog-schema.d.cts +4 -2
- package/dist/a2ui-catalog-schema.d.cts.map +1 -1
- package/dist/a2ui-catalog-schema.d.mts +4 -2
- package/dist/a2ui-catalog-schema.d.mts.map +1 -1
- package/dist/a2ui-catalog-schema.mjs +14 -5
- package/dist/a2ui-catalog-schema.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/catalog/catalog-export.cjs +329 -0
- package/dist/generative-ui/catalog/catalog-export.cjs.map +1 -0
- package/dist/generative-ui/catalog/catalog-export.d.cts +92 -0
- package/dist/generative-ui/catalog/catalog-export.d.cts.map +1 -0
- package/dist/generative-ui/catalog/catalog-export.d.mts +92 -0
- package/dist/generative-ui/catalog/catalog-export.d.mts.map +1 -0
- package/dist/generative-ui/catalog/catalog-export.mjs +328 -0
- package/dist/generative-ui/catalog/catalog-export.mjs.map +1 -0
- package/dist/generative-ui/catalog/catalog.cjs +314 -0
- package/dist/generative-ui/catalog/catalog.cjs.map +1 -0
- package/dist/generative-ui/catalog/catalog.d.cts +407 -0
- package/dist/generative-ui/catalog/catalog.d.cts.map +1 -0
- package/dist/generative-ui/catalog/catalog.d.mts +407 -0
- package/dist/generative-ui/catalog/catalog.d.mts.map +1 -0
- package/dist/generative-ui/catalog/catalog.mjs +311 -0
- package/dist/generative-ui/catalog/catalog.mjs.map +1 -0
- package/dist/generative-ui/catalog/errors.cjs +13 -0
- package/dist/generative-ui/catalog/errors.cjs.map +1 -0
- package/dist/generative-ui/catalog/errors.d.cts +46 -0
- package/dist/generative-ui/catalog/errors.d.cts.map +1 -0
- package/dist/generative-ui/catalog/errors.d.mts +46 -0
- package/dist/generative-ui/catalog/errors.d.mts.map +1 -0
- package/dist/generative-ui/catalog/errors.mjs +12 -0
- package/dist/generative-ui/catalog/errors.mjs.map +1 -0
- package/dist/generative-ui/catalog/index.d.mts +6 -0
- package/dist/generative-ui/catalog/json-schema.cjs +19 -0
- package/dist/generative-ui/catalog/json-schema.cjs.map +1 -0
- package/dist/generative-ui/catalog/json-schema.d.cts +13 -0
- package/dist/generative-ui/catalog/json-schema.d.cts.map +1 -0
- package/dist/generative-ui/catalog/json-schema.d.mts +13 -0
- package/dist/generative-ui/catalog/json-schema.d.mts.map +1 -0
- package/dist/generative-ui/catalog/json-schema.mjs +18 -0
- package/dist/generative-ui/catalog/json-schema.mjs.map +1 -0
- package/dist/generative-ui/catalog/refs.cjs +20 -0
- package/dist/generative-ui/catalog/refs.cjs.map +1 -0
- package/dist/generative-ui/catalog/refs.d.cts +54 -0
- package/dist/generative-ui/catalog/refs.d.cts.map +1 -0
- package/dist/generative-ui/catalog/refs.d.mts +54 -0
- package/dist/generative-ui/catalog/refs.d.mts.map +1 -0
- package/dist/generative-ui/catalog/refs.mjs +18 -0
- package/dist/generative-ui/catalog/refs.mjs.map +1 -0
- package/dist/generative-ui/catalog/schema.cjs +74 -0
- package/dist/generative-ui/catalog/schema.cjs.map +1 -0
- package/dist/generative-ui/catalog/schema.d.cts +43 -0
- package/dist/generative-ui/catalog/schema.d.cts.map +1 -0
- package/dist/generative-ui/catalog/schema.d.mts +43 -0
- package/dist/generative-ui/catalog/schema.d.mts.map +1 -0
- package/dist/generative-ui/catalog/schema.mjs +68 -0
- package/dist/generative-ui/catalog/schema.mjs.map +1 -0
- package/dist/generative-ui/index.cjs +247 -0
- package/dist/generative-ui/index.d.cts +33 -0
- package/dist/generative-ui/index.d.mts +35 -0
- package/dist/generative-ui/index.mjs +34 -0
- package/dist/generative-ui/is-record.cjs +6 -1
- package/dist/generative-ui/is-record.cjs.map +1 -1
- package/dist/generative-ui/is-record.d.cts +6 -0
- package/dist/generative-ui/is-record.d.cts.map +1 -0
- package/dist/generative-ui/is-record.d.mts +6 -0
- package/dist/generative-ui/is-record.d.mts.map +1 -0
- package/dist/generative-ui/is-record.mjs +6 -2
- package/dist/generative-ui/is-record.mjs.map +1 -1
- package/dist/generative-ui/standard/chart.cjs +56 -0
- package/dist/generative-ui/standard/chart.cjs.map +1 -0
- package/dist/generative-ui/standard/chart.d.cts +142 -0
- package/dist/generative-ui/standard/chart.d.cts.map +1 -0
- package/dist/generative-ui/standard/chart.d.mts +142 -0
- package/dist/generative-ui/standard/chart.d.mts.map +1 -0
- package/dist/generative-ui/standard/chart.mjs +54 -0
- package/dist/generative-ui/standard/chart.mjs.map +1 -0
- package/dist/generative-ui/standard/checks.cjs +74 -0
- package/dist/generative-ui/standard/checks.cjs.map +1 -0
- package/dist/generative-ui/standard/checks.d.cts +74 -0
- package/dist/generative-ui/standard/checks.d.cts.map +1 -0
- package/dist/generative-ui/standard/checks.d.mts +74 -0
- package/dist/generative-ui/standard/checks.d.mts.map +1 -0
- package/dist/generative-ui/standard/checks.mjs +71 -0
- package/dist/generative-ui/standard/checks.mjs.map +1 -0
- package/dist/generative-ui/standard/components.cjs +23 -0
- package/dist/generative-ui/standard/components.cjs.map +1 -0
- package/dist/generative-ui/standard/components.d.cts +24 -0
- package/dist/generative-ui/standard/components.d.cts.map +1 -0
- package/dist/generative-ui/standard/components.d.mts +24 -0
- package/dist/generative-ui/standard/components.d.mts.map +1 -0
- package/dist/generative-ui/standard/components.mjs +22 -0
- package/dist/generative-ui/standard/components.mjs.map +1 -0
- package/dist/generative-ui/standard/composites.cjs +143 -0
- package/dist/generative-ui/standard/composites.cjs.map +1 -0
- package/dist/generative-ui/standard/composites.d.cts +500 -0
- package/dist/generative-ui/standard/composites.d.cts.map +1 -0
- package/dist/generative-ui/standard/composites.d.mts +500 -0
- package/dist/generative-ui/standard/composites.d.mts.map +1 -0
- package/dist/generative-ui/standard/composites.mjs +132 -0
- package/dist/generative-ui/standard/composites.mjs.map +1 -0
- package/dist/generative-ui/standard/data.cjs +176 -0
- package/dist/generative-ui/standard/data.cjs.map +1 -0
- package/dist/generative-ui/standard/data.d.cts +745 -0
- package/dist/generative-ui/standard/data.d.cts.map +1 -0
- package/dist/generative-ui/standard/data.d.mts +745 -0
- package/dist/generative-ui/standard/data.d.mts.map +1 -0
- package/dist/generative-ui/standard/data.mjs +167 -0
- package/dist/generative-ui/standard/data.mjs.map +1 -0
- package/dist/generative-ui/standard/declarations.cjs +40 -0
- package/dist/generative-ui/standard/declarations.cjs.map +1 -0
- package/dist/generative-ui/standard/declarations.d.cts +4604 -0
- package/dist/generative-ui/standard/declarations.d.cts.map +1 -0
- package/dist/generative-ui/standard/declarations.d.mts +4605 -0
- package/dist/generative-ui/standard/declarations.d.mts.map +1 -0
- package/dist/generative-ui/standard/declarations.mjs +39 -0
- package/dist/generative-ui/standard/declarations.mjs.map +1 -0
- package/dist/generative-ui/standard/feedback.cjs +97 -0
- package/dist/generative-ui/standard/feedback.cjs.map +1 -0
- package/dist/generative-ui/standard/feedback.d.cts +227 -0
- package/dist/generative-ui/standard/feedback.d.cts.map +1 -0
- package/dist/generative-ui/standard/feedback.d.mts +227 -0
- package/dist/generative-ui/standard/feedback.d.mts.map +1 -0
- package/dist/generative-ui/standard/feedback.mjs +90 -0
- package/dist/generative-ui/standard/feedback.mjs.map +1 -0
- package/dist/generative-ui/standard/index.d.mts +16 -0
- package/dist/generative-ui/standard/inputs.cjs +333 -0
- package/dist/generative-ui/standard/inputs.cjs.map +1 -0
- package/dist/generative-ui/standard/inputs.d.cts +2658 -0
- package/dist/generative-ui/standard/inputs.d.cts.map +1 -0
- package/dist/generative-ui/standard/inputs.d.mts +2658 -0
- package/dist/generative-ui/standard/inputs.d.mts.map +1 -0
- package/dist/generative-ui/standard/inputs.mjs +318 -0
- package/dist/generative-ui/standard/inputs.mjs.map +1 -0
- package/dist/generative-ui/standard/layout.cjs +289 -0
- package/dist/generative-ui/standard/layout.cjs.map +1 -0
- package/dist/generative-ui/standard/layout.d.cts +2854 -0
- package/dist/generative-ui/standard/layout.d.cts.map +1 -0
- package/dist/generative-ui/standard/layout.d.mts +2854 -0
- package/dist/generative-ui/standard/layout.d.mts.map +1 -0
- package/dist/generative-ui/standard/layout.mjs +269 -0
- package/dist/generative-ui/standard/layout.mjs.map +1 -0
- package/dist/generative-ui/standard/map.cjs +27 -0
- package/dist/generative-ui/standard/map.cjs.map +1 -0
- package/dist/generative-ui/standard/map.d.cts +93 -0
- package/dist/generative-ui/standard/map.d.cts.map +1 -0
- package/dist/generative-ui/standard/map.d.mts +93 -0
- package/dist/generative-ui/standard/map.d.mts.map +1 -0
- package/dist/generative-ui/standard/map.mjs +25 -0
- package/dist/generative-ui/standard/map.mjs.map +1 -0
- package/dist/generative-ui/standard/media.cjs +215 -0
- package/dist/generative-ui/standard/media.cjs.map +1 -0
- package/dist/generative-ui/standard/media.d.cts +968 -0
- package/dist/generative-ui/standard/media.d.cts.map +1 -0
- package/dist/generative-ui/standard/media.d.mts +968 -0
- package/dist/generative-ui/standard/media.d.mts.map +1 -0
- package/dist/generative-ui/standard/media.mjs +205 -0
- package/dist/generative-ui/standard/media.mjs.map +1 -0
- package/dist/generative-ui/standard/parameter-order.cjs +231 -0
- package/dist/generative-ui/standard/parameter-order.cjs.map +1 -0
- package/dist/generative-ui/standard/parameter-order.d.cts +84 -0
- package/dist/generative-ui/standard/parameter-order.d.cts.map +1 -0
- package/dist/generative-ui/standard/parameter-order.d.mts +84 -0
- package/dist/generative-ui/standard/parameter-order.d.mts.map +1 -0
- package/dist/generative-ui/standard/parameter-order.mjs +230 -0
- package/dist/generative-ui/standard/parameter-order.mjs.map +1 -0
- package/dist/generative-ui/standard/styles.cjs +62 -0
- package/dist/generative-ui/standard/styles.cjs.map +1 -0
- package/dist/generative-ui/standard/styles.d.cts +17 -0
- package/dist/generative-ui/standard/styles.d.cts.map +1 -0
- package/dist/generative-ui/standard/styles.d.mts +17 -0
- package/dist/generative-ui/standard/styles.d.mts.map +1 -0
- package/dist/generative-ui/standard/styles.mjs +60 -0
- package/dist/generative-ui/standard/styles.mjs.map +1 -0
- package/dist/generative-ui/standard/text.cjs +226 -0
- package/dist/generative-ui/standard/text.cjs.map +1 -0
- package/dist/generative-ui/standard/text.d.cts +687 -0
- package/dist/generative-ui/standard/text.d.cts.map +1 -0
- package/dist/generative-ui/standard/text.d.mts +687 -0
- package/dist/generative-ui/standard/text.d.mts.map +1 -0
- package/dist/generative-ui/standard/text.mjs +209 -0
- package/dist/generative-ui/standard/text.mjs.map +1 -0
- package/dist/generative-ui/standard/theme.cjs +115 -0
- package/dist/generative-ui/standard/theme.cjs.map +1 -0
- package/dist/generative-ui/standard/theme.d.cts +60 -0
- package/dist/generative-ui/standard/theme.d.cts.map +1 -0
- package/dist/generative-ui/standard/theme.d.mts +60 -0
- package/dist/generative-ui/standard/theme.d.mts.map +1 -0
- package/dist/generative-ui/standard/theme.mjs +112 -0
- package/dist/generative-ui/standard/theme.mjs.map +1 -0
- package/dist/generative-ui/standard/tokens.cjs +336 -0
- package/dist/generative-ui/standard/tokens.cjs.map +1 -0
- package/dist/generative-ui/standard/tokens.d.cts +76 -0
- package/dist/generative-ui/standard/tokens.d.cts.map +1 -0
- package/dist/generative-ui/standard/tokens.d.mts +76 -0
- package/dist/generative-ui/standard/tokens.d.mts.map +1 -0
- package/dist/generative-ui/standard/tokens.mjs +305 -0
- package/dist/generative-ui/standard/tokens.mjs.map +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -10
- package/dist/index.d.mts +10 -10
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +16 -7
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +7 -3
- package/src/__tests__/a2ui-catalog-schema.test.ts +26 -0
- package/src/a2ui-catalog-schema.ts +14 -5
- package/src/event-transforms/open-generative-ui-middleware.ts +19 -10
- package/src/generative-ui/base.ts +9 -0
- package/src/generative-ui/catalog/catalog-export.ts +478 -0
- package/src/generative-ui/catalog/catalog.ts +1166 -0
- package/src/generative-ui/catalog/errors.ts +102 -0
- package/src/generative-ui/catalog/index.ts +6 -0
- package/src/generative-ui/catalog/json-schema.ts +23 -0
- package/src/generative-ui/catalog/refs.ts +86 -0
- package/src/generative-ui/catalog/schema.ts +107 -0
- package/src/generative-ui/index.ts +7 -0
- package/src/generative-ui/is-record.ts +6 -1
- package/src/generative-ui/standard/__tests__/theme.test.ts +61 -0
- package/src/generative-ui/standard/__tests__/tokens.test.ts +139 -0
- package/src/generative-ui/standard/chart.ts +79 -0
- package/src/generative-ui/standard/checks.ts +98 -0
- package/src/generative-ui/standard/components.ts +35 -0
- package/src/generative-ui/standard/composites.ts +158 -0
- package/src/generative-ui/standard/data.ts +213 -0
- package/src/generative-ui/standard/declarations.ts +57 -0
- package/src/generative-ui/standard/feedback.ts +105 -0
- package/src/generative-ui/standard/index.ts +19 -0
- package/src/generative-ui/standard/inputs.ts +423 -0
- package/src/generative-ui/standard/layout.ts +312 -0
- package/src/generative-ui/standard/map.ts +32 -0
- package/src/generative-ui/standard/media.ts +265 -0
- package/src/generative-ui/standard/parameter-order.ts +155 -0
- package/src/generative-ui/standard/styles.ts +79 -0
- package/src/generative-ui/standard/text.ts +210 -0
- package/src/generative-ui/standard/theme.ts +198 -0
- package/src/generative-ui/standard/tokens.ts +317 -0
- package/src/index.ts +1 -1
- package/tsdown.config.ts +1 -0
package/dist/package.cjs
CHANGED
package/dist/package.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.85.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"import": "./dist/event-transforms/index.mjs",
|
|
37
37
|
"require": "./dist/event-transforms/index.cjs"
|
|
38
38
|
},
|
|
39
|
+
"./generative-ui": {
|
|
40
|
+
"import": "./dist/generative-ui/index.mjs",
|
|
41
|
+
"require": "./dist/generative-ui/index.cjs"
|
|
42
|
+
},
|
|
39
43
|
"./telemetry": {
|
|
40
44
|
"import": "./dist/telemetry/index.mjs",
|
|
41
45
|
"require": "./dist/telemetry/index.cjs"
|
|
@@ -57,7 +61,7 @@
|
|
|
57
61
|
},
|
|
58
62
|
"dependencies": {
|
|
59
63
|
"@ag-ui/a2ui-middleware": "0.0.10",
|
|
60
|
-
"@ag-ui/client": "1.0.
|
|
64
|
+
"@ag-ui/client": "1.0.2",
|
|
61
65
|
"@copilotkit/license-verifier": "~0.5.0",
|
|
62
66
|
"@segment/analytics-node": "^2.1.2",
|
|
63
67
|
"@standard-schema/spec": "^1.0.0",
|
|
@@ -70,7 +74,7 @@
|
|
|
70
74
|
"zod-to-json-schema": "^3.23.5"
|
|
71
75
|
},
|
|
72
76
|
"devDependencies": {
|
|
73
|
-
"@ag-ui/core": "1.0.
|
|
77
|
+
"@ag-ui/core": "1.0.2",
|
|
74
78
|
"@types/clarinet": "^0.12.3",
|
|
75
79
|
"@types/uuid": "^10.0.0",
|
|
76
80
|
"@valibot/to-json-schema": "^1.5.0",
|
|
@@ -93,6 +93,32 @@ describe("extractA2UICatalogSchemas", () => {
|
|
|
93
93
|
expect(schema).not.toHaveProperty(["common_types.json"]);
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
+
it("writes a local #/$defs REF marker as a common_types.json ref", () => {
|
|
97
|
+
// web_core 0.15's v0.9 common types mark themselves REF:#/$defs/<Name>.
|
|
98
|
+
const id = z.string().describe("REF:#/$defs/ComponentId|Child id");
|
|
99
|
+
const schema = extractA2UICatalogSchemas(
|
|
100
|
+
catalog({
|
|
101
|
+
Card: z.object({
|
|
102
|
+
child: id,
|
|
103
|
+
other: z
|
|
104
|
+
.string()
|
|
105
|
+
.describe("REF:common_types.json#/$defs/ComponentId"),
|
|
106
|
+
}),
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(props(schema, "Card")).toEqual({
|
|
111
|
+
component: { const: "Card" },
|
|
112
|
+
child: {
|
|
113
|
+
$ref: "common_types.json#/$defs/ComponentId",
|
|
114
|
+
description: "Child id",
|
|
115
|
+
},
|
|
116
|
+
other: { $ref: "common_types.json#/$defs/ComponentId" },
|
|
117
|
+
});
|
|
118
|
+
expect(commonTypes(schema)).toEqual({ ComponentId: { type: "string" } });
|
|
119
|
+
expect(JSON.stringify(schema)).not.toContain('"$ref":"#/');
|
|
120
|
+
});
|
|
121
|
+
|
|
96
122
|
it("keeps a use whose shape differs from the stored type inline", () => {
|
|
97
123
|
const schema = extractA2UICatalogSchemas(
|
|
98
124
|
catalog({
|
|
@@ -71,8 +71,10 @@ export type A2UICommonTypes = Record<string, JsonSchema>;
|
|
|
71
71
|
*
|
|
72
72
|
* - Reused Zod schemas are inlined rather than emitted as local `$ref`s, which
|
|
73
73
|
* would dangle once properties move under `allOf[1]`.
|
|
74
|
-
* - A `REF:<ref>|<description>` marker becomes a bare `$ref`, as upstream
|
|
75
|
-
*
|
|
74
|
+
* - A `REF:<ref>|<description>` marker becomes a bare `$ref`, as upstream.
|
|
75
|
+
* Upstream writes a common type as a local `#/$defs/<Name>` ref, which an
|
|
76
|
+
* inline catalog without `$defs` leaves dangling; here it is always
|
|
77
|
+
* `common_types.json#/$defs/<Name>`, and the shape it replaced is kept once under
|
|
76
78
|
* `"common_types.json": { "$defs": { <Name>: ... } }`. Every
|
|
77
79
|
* `common_types.json#/$defs/<Name>` ref therefore resolves within the
|
|
78
80
|
* payload, both for agents that only see it as prompt text and for SDKs
|
|
@@ -287,7 +289,14 @@ const SUBSCHEMA_MAP_KEYWORDS = [
|
|
|
287
289
|
"definitions",
|
|
288
290
|
];
|
|
289
291
|
|
|
290
|
-
|
|
292
|
+
/**
|
|
293
|
+
* A reference to a whole common type, in `common_types.json` or local. From
|
|
294
|
+
* `@a2ui/web_core` 0.15 the v0.9 common types mark themselves with local
|
|
295
|
+
* `REF:#/$defs/<Name>` refs, the layout of a bundled catalog document; both
|
|
296
|
+
* forms name the same type, which this payload writes as
|
|
297
|
+
* `common_types.json#/$defs/<Name>`.
|
|
298
|
+
*/
|
|
299
|
+
const COMMON_TYPE_REF = /^(?:common_types\.json)?#\/\$defs\/([^/]+)$/;
|
|
291
300
|
|
|
292
301
|
function isObject(value: unknown): value is JsonSchema {
|
|
293
302
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -358,11 +367,11 @@ function resolveRefMarker(
|
|
|
358
367
|
if (!marker) return node;
|
|
359
368
|
|
|
360
369
|
const { description: _marker, ...shape } = node;
|
|
370
|
+
const name = COMMON_TYPE_REF.exec(marker.ref)?.[1];
|
|
361
371
|
const ref: JsonSchema = {
|
|
362
|
-
$ref: marker.ref
|
|
372
|
+
$ref: name === undefined ? marker.ref : `common_types.json#/$defs/${name}`,
|
|
363
373
|
...(marker.description ? { description: marker.description } : {}),
|
|
364
374
|
};
|
|
365
|
-
const name = COMMON_TYPE_REF.exec(marker.ref)?.[1];
|
|
366
375
|
if (name !== undefined) {
|
|
367
376
|
const known = commonTypes[name];
|
|
368
377
|
if (known === undefined) {
|
|
@@ -251,16 +251,23 @@ export class ArgsParser {
|
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Extract EventWithState type from Middleware.runNextWithState return type
|
|
256
|
+
*/
|
|
257
|
+
type ExtractObservableType<T> = T extends Observable<infer U> ? U : never;
|
|
258
|
+
type RunNextWithStateReturn = ReturnType<Middleware["runNextWithState"]>;
|
|
259
|
+
type EventWithState = ExtractObservableType<RunNextWithStateReturn>;
|
|
260
|
+
|
|
254
261
|
export class OpenGenerativeUIMiddleware extends Middleware {
|
|
255
262
|
run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent> {
|
|
256
|
-
|
|
257
|
-
// messages and state on every event for a consumer that never looks.
|
|
258
|
-
return this.processStream(this.runNext(input, next));
|
|
263
|
+
return this.processStream(this.runNextWithState(input, next));
|
|
259
264
|
}
|
|
260
265
|
|
|
261
|
-
private processStream(
|
|
266
|
+
private processStream(
|
|
267
|
+
source: Observable<EventWithState>,
|
|
268
|
+
): Observable<BaseEvent> {
|
|
262
269
|
return new Observable<BaseEvent>((subscriber) => {
|
|
263
|
-
let heldRunFinished:
|
|
270
|
+
let heldRunFinished: EventWithState | null = null;
|
|
264
271
|
// Track active generateSandboxedUi tool call IDs → their streaming parser
|
|
265
272
|
const activeParsers = new Map<string, ArgsParser>();
|
|
266
273
|
// Hold genui tool call events until the first activity event is emitted
|
|
@@ -280,14 +287,16 @@ export class OpenGenerativeUIMiddleware extends Middleware {
|
|
|
280
287
|
};
|
|
281
288
|
|
|
282
289
|
const subscription = source.subscribe({
|
|
283
|
-
next: (
|
|
290
|
+
next: (eventWithState) => {
|
|
291
|
+
const event = eventWithState.event;
|
|
292
|
+
|
|
284
293
|
if (heldRunFinished) {
|
|
285
|
-
subscriber.next(heldRunFinished);
|
|
294
|
+
subscriber.next(heldRunFinished.event);
|
|
286
295
|
heldRunFinished = null;
|
|
287
296
|
}
|
|
288
297
|
|
|
289
298
|
if (event.type === EventType.RUN_FINISHED) {
|
|
290
|
-
heldRunFinished =
|
|
299
|
+
heldRunFinished = eventWithState;
|
|
291
300
|
return;
|
|
292
301
|
}
|
|
293
302
|
|
|
@@ -357,7 +366,7 @@ export class OpenGenerativeUIMiddleware extends Middleware {
|
|
|
357
366
|
heldToolCallEvents.clear();
|
|
358
367
|
|
|
359
368
|
if (heldRunFinished) {
|
|
360
|
-
subscriber.next(heldRunFinished);
|
|
369
|
+
subscriber.next(heldRunFinished.event);
|
|
361
370
|
heldRunFinished = null;
|
|
362
371
|
}
|
|
363
372
|
subscriber.error(err);
|
|
@@ -369,7 +378,7 @@ export class OpenGenerativeUIMiddleware extends Middleware {
|
|
|
369
378
|
});
|
|
370
379
|
|
|
371
380
|
if (heldRunFinished) {
|
|
372
|
-
subscriber.next(heldRunFinished);
|
|
381
|
+
subscriber.next(heldRunFinished.event);
|
|
373
382
|
heldRunFinished = null;
|
|
374
383
|
}
|
|
375
384
|
activeParsers.clear();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./types";
|
|
2
|
+
export * from "./validate-operation";
|
|
3
|
+
export * from "./render-ui-args";
|
|
4
|
+
export * from "./basic-functions";
|
|
5
|
+
export * from "./child-refs";
|
|
6
|
+
export * from "./prompt";
|
|
7
|
+
export { buildCompactGenerativeUISchema } from "./compact-schema";
|
|
8
|
+
export * from "./prompt-spec";
|
|
9
|
+
export * from "./schema-render";
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import {
|
|
2
|
+
A2UI_CHILD_LIST_REF,
|
|
3
|
+
A2UI_COMPONENT_ID_REF,
|
|
4
|
+
GENERATIVE_UI_FUNCTIONS,
|
|
5
|
+
childRefKind,
|
|
6
|
+
functionCallers,
|
|
7
|
+
} from "../base";
|
|
8
|
+
import type {
|
|
9
|
+
A2UIAllowedCallers,
|
|
10
|
+
A2UIFunctionPermissions,
|
|
11
|
+
GenerativeUIComponentManifest,
|
|
12
|
+
GenerativeUIJSONSchema,
|
|
13
|
+
} from "../base";
|
|
14
|
+
import type { GenerativeUI } from "./catalog";
|
|
15
|
+
import { toJsonSchema } from "./json-schema";
|
|
16
|
+
import { hasOwnKey, isRecord } from "../is-record";
|
|
17
|
+
|
|
18
|
+
const COMMON = "common_types.json#/$defs/";
|
|
19
|
+
const DATA_BINDING_REF = `${COMMON}DataBinding`;
|
|
20
|
+
const FUNCTION_CALL_REF = `${COMMON}FunctionCall`;
|
|
21
|
+
const ACTION_REF = `${COMMON}Action`;
|
|
22
|
+
const DYNAMIC_REFS: Record<string, string> = {
|
|
23
|
+
string: `${COMMON}DynamicString`,
|
|
24
|
+
number: `${COMMON}DynamicNumber`,
|
|
25
|
+
boolean: `${COMMON}DynamicBoolean`,
|
|
26
|
+
"string[]": `${COMMON}DynamicStringList`,
|
|
27
|
+
unknown: `${COMMON}DynamicValue`,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** What `exportA2UICatalog` adds at the top of the catalog. */
|
|
31
|
+
export interface ExportA2UICatalogOptions {
|
|
32
|
+
/** The title of the catalog. */
|
|
33
|
+
title?: string;
|
|
34
|
+
/** What the catalog is for. */
|
|
35
|
+
description?: string;
|
|
36
|
+
/** Markdown design guidelines for a model that writes this catalog. */
|
|
37
|
+
instructions?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The A2UI 1.0 catalog definition of a generative UI, as a JSON value that
|
|
42
|
+
* follows `catalog_definition.json`: one `components` entry per component,
|
|
43
|
+
* with `component` as a `const`, `functions` for the built-ins and the
|
|
44
|
+
* declared functions, and `$defs.anyComponent` / `$defs.anyFunction`. Other
|
|
45
|
+
* A2UI tools (a validator, another renderer, a prompt builder) read it.
|
|
46
|
+
*
|
|
47
|
+
* - A prop that `child()` or `childList()` marks is a `ComponentId` or a
|
|
48
|
+
* `ChildList` reference; so is a slot. An event prop is an `Action`.
|
|
49
|
+
* - Every other value, at any depth of a prop or of the args of a declared
|
|
50
|
+
* function, takes its literal schema, a `DataBinding` or a `FunctionCall`,
|
|
51
|
+
* because the renderer resolves dynamic values at any depth.
|
|
52
|
+
* - Every function states `returnType` and `allowedCallers`: the built-ins
|
|
53
|
+
* are `rendererOnly`, and so is a declared function without
|
|
54
|
+
* `allowedCallers`, as in A2UI 1.0.
|
|
55
|
+
*
|
|
56
|
+
* @param ui - The generative UI to describe.
|
|
57
|
+
* @param options - The title, description and instructions of the catalog.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const catalog = exportA2UICatalog(shopUI, { title: "Acme shop" });
|
|
62
|
+
* fs.writeFileSync("shop-catalog.json", JSON.stringify(catalog, null, 2));
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function exportA2UICatalog(
|
|
66
|
+
ui: GenerativeUI,
|
|
67
|
+
options: ExportA2UICatalogOptions = {},
|
|
68
|
+
) {
|
|
69
|
+
const components = Object.fromEntries(
|
|
70
|
+
Object.entries(ui.manifestComponents).map(([name, manifest]) => [
|
|
71
|
+
name,
|
|
72
|
+
toDraft2020(
|
|
73
|
+
rebaseRefs(
|
|
74
|
+
componentDefinition(name, manifest),
|
|
75
|
+
`#/components/${pointerSegment(name)}`,
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
]),
|
|
79
|
+
);
|
|
80
|
+
const functions: Record<string, GenerativeUIJSONSchema> = {};
|
|
81
|
+
for (const [name, info] of Object.entries(GENERATIVE_UI_FUNCTIONS)) {
|
|
82
|
+
// A declared function of the same name is the app's.
|
|
83
|
+
if (ui.functions.has(name)) continue;
|
|
84
|
+
functions[name] = functionEntry(name, info, {
|
|
85
|
+
description: info.description,
|
|
86
|
+
returnType: info.returnType,
|
|
87
|
+
args: argsSchemaFromSignature(info.parameters),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
for (const [name, fn] of ui.functions) {
|
|
91
|
+
functions[name] = functionEntry(name, fn, {
|
|
92
|
+
description: fn.description,
|
|
93
|
+
returnType: fn.returnType ?? "any",
|
|
94
|
+
args: toDraft2020(
|
|
95
|
+
rebaseRefs(
|
|
96
|
+
dynamicArgs(toJsonSchema(fn.parameters)),
|
|
97
|
+
`#/functions/${pointerSegment(name)}/properties/args`,
|
|
98
|
+
),
|
|
99
|
+
),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const { title, description, instructions } = options;
|
|
103
|
+
return {
|
|
104
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
105
|
+
protocolVersion: "1.0",
|
|
106
|
+
catalogId: ui.id,
|
|
107
|
+
...(title === undefined ? {} : { title }),
|
|
108
|
+
...(description === undefined ? {} : { description }),
|
|
109
|
+
...(instructions === undefined ? {} : { instructions }),
|
|
110
|
+
components,
|
|
111
|
+
functions,
|
|
112
|
+
$defs: {
|
|
113
|
+
anyComponent: {
|
|
114
|
+
...oneOfRefs("components", Object.keys(components)),
|
|
115
|
+
discriminator: { propertyName: "component" },
|
|
116
|
+
},
|
|
117
|
+
anyFunction: oneOfRefs("functions", Object.keys(functions)),
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* `oneOf` over the entries of a section. JSON Schema wants at least one
|
|
124
|
+
* variant, so an empty section (a UI with no components) matches nothing.
|
|
125
|
+
*/
|
|
126
|
+
function oneOfRefs(section: string, names: string[]) {
|
|
127
|
+
if (names.length === 0) return { not: {} };
|
|
128
|
+
return {
|
|
129
|
+
oneOf: names.map((name) => ({
|
|
130
|
+
$ref: `#/${section}/${pointerSegment(name)}`,
|
|
131
|
+
})),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A name as a JSON Pointer segment in a URI fragment: `acme/Card` is `acme~1Card`. */
|
|
136
|
+
function pointerSegment(name: string) {
|
|
137
|
+
return encodeURIComponent(name.replace(/~/g, "~0").replace(/\//g, "~1"));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Keywords whose value is data, not a schema. */
|
|
141
|
+
const LITERAL_KEYWORDS = new Set(["default", "const", "enum", "examples"]);
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A schema in the draft 2020-12 dialect that the catalog declares. The
|
|
145
|
+
* converter writes draft-07, where a tuple is `items: [...]` with
|
|
146
|
+
* `additionalItems`; 2020-12 spells that `prefixItems` and `items`.
|
|
147
|
+
*/
|
|
148
|
+
function toDraft2020(schema: GenerativeUIJSONSchema): GenerativeUIJSONSchema {
|
|
149
|
+
return mapSchema(schema, (node) => {
|
|
150
|
+
const { items, additionalItems, ...rest } = node;
|
|
151
|
+
if (!Array.isArray(items)) return node;
|
|
152
|
+
return {
|
|
153
|
+
...rest,
|
|
154
|
+
prefixItems: items,
|
|
155
|
+
...(additionalItems === undefined ? {} : { items: additionalItems }),
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** One `components` entry: the props as the model sets them, with the A2UI common types. */
|
|
161
|
+
function componentDefinition(
|
|
162
|
+
name: string,
|
|
163
|
+
manifest: GenerativeUIComponentManifest,
|
|
164
|
+
): GenerativeUIJSONSchema {
|
|
165
|
+
const { properties, required, description, $defs, definitions } =
|
|
166
|
+
manifest.props;
|
|
167
|
+
const props = Object.fromEntries(
|
|
168
|
+
Object.entries(isRecord(properties) ? properties : {}).map(
|
|
169
|
+
([key, schema]) => [key, dynamicProp(schema)],
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
const events = Object.fromEntries(
|
|
173
|
+
Object.entries(manifest.events ?? {}).map(([key, event]) => [
|
|
174
|
+
key,
|
|
175
|
+
{
|
|
176
|
+
$ref: ACTION_REF,
|
|
177
|
+
...(event.description ? { description: event.description } : {}),
|
|
178
|
+
},
|
|
179
|
+
]),
|
|
180
|
+
);
|
|
181
|
+
const slots = Object.fromEntries(
|
|
182
|
+
Object.entries(manifest.slots ?? {}).map(([key, kind]) => [
|
|
183
|
+
key,
|
|
184
|
+
{ $ref: kind === "single" ? A2UI_COMPONENT_ID_REF : A2UI_CHILD_LIST_REF },
|
|
185
|
+
]),
|
|
186
|
+
);
|
|
187
|
+
const fields = { ...props, ...events, ...slots };
|
|
188
|
+
const orderedFields =
|
|
189
|
+
manifest.parameterOrder === undefined
|
|
190
|
+
? fields
|
|
191
|
+
: Object.fromEntries(
|
|
192
|
+
[...new Set([...manifest.parameterOrder, ...Object.keys(fields)])]
|
|
193
|
+
.filter((key) => hasOwnKey(fields, key))
|
|
194
|
+
.map((key) => [key, fields[key]]),
|
|
195
|
+
);
|
|
196
|
+
const text = manifest.description ?? description;
|
|
197
|
+
return {
|
|
198
|
+
type: "object",
|
|
199
|
+
...(typeof text === "string" ? { description: text } : {}),
|
|
200
|
+
properties: { component: { const: name }, ...orderedFields },
|
|
201
|
+
required: [
|
|
202
|
+
"component",
|
|
203
|
+
...(Array.isArray(required) ? required.filter(isString) : []),
|
|
204
|
+
],
|
|
205
|
+
// The definitions of the props schema, when its converter made some:
|
|
206
|
+
// their values are dynamic like the props, and the references to them
|
|
207
|
+
// are rebased onto this entry.
|
|
208
|
+
...withDynamicChildren({
|
|
209
|
+
...(isRecord($defs) ? { $defs } : {}),
|
|
210
|
+
...(isRecord(definitions) ? { definitions } : {}),
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The schema with its local references (`#/...`) pointing into the place
|
|
217
|
+
* it is embedded at. A converter that writes `$defs` refers to them from
|
|
218
|
+
* the root of its own document; inside the catalog, that root is `base`.
|
|
219
|
+
*/
|
|
220
|
+
function rebaseRefs(
|
|
221
|
+
schema: GenerativeUIJSONSchema,
|
|
222
|
+
base: string,
|
|
223
|
+
): GenerativeUIJSONSchema {
|
|
224
|
+
return mapSchema(schema, (node) => {
|
|
225
|
+
const ref = node.$ref;
|
|
226
|
+
// "#" is the root of the embedded schema (a recursive one refers to it).
|
|
227
|
+
if (ref === "#") return { ...node, $ref: base };
|
|
228
|
+
if (typeof ref === "string" && ref.startsWith("#/")) {
|
|
229
|
+
return { ...node, $ref: `${base}${ref.slice(1)}` };
|
|
230
|
+
}
|
|
231
|
+
return node;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Keywords whose value is a map from a name to a schema. */
|
|
236
|
+
const SCHEMA_MAP_KEYWORDS = new Set([
|
|
237
|
+
"properties",
|
|
238
|
+
"patternProperties",
|
|
239
|
+
"$defs",
|
|
240
|
+
"definitions",
|
|
241
|
+
"dependentSchemas",
|
|
242
|
+
]);
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Walks a JSON Schema and rebuilds each schema node with `transform`, the
|
|
246
|
+
* children first. The names in `properties` and the definition maps are
|
|
247
|
+
* names, not keywords: a prop called `default` is a schema, while the
|
|
248
|
+
* `default` of a schema is data and stays as it is.
|
|
249
|
+
*/
|
|
250
|
+
function mapSchema(
|
|
251
|
+
schema: GenerativeUIJSONSchema,
|
|
252
|
+
transform: (node: Record<string, unknown>) => Record<string, unknown>,
|
|
253
|
+
): GenerativeUIJSONSchema {
|
|
254
|
+
const visitValue = (value: unknown): unknown => {
|
|
255
|
+
if (Array.isArray(value)) return value.map(visitValue);
|
|
256
|
+
return isRecord(value) ? visitNode(value) : value;
|
|
257
|
+
};
|
|
258
|
+
const visitNode = (
|
|
259
|
+
node: Record<string, unknown>,
|
|
260
|
+
): Record<string, unknown> => {
|
|
261
|
+
const result: Record<string, unknown> = {};
|
|
262
|
+
for (const [key, value] of Object.entries(node)) {
|
|
263
|
+
if (LITERAL_KEYWORDS.has(key)) result[key] = value;
|
|
264
|
+
else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
|
|
265
|
+
result[key] = Object.fromEntries(
|
|
266
|
+
Object.entries(value).map(([name, entry]) => [
|
|
267
|
+
name,
|
|
268
|
+
isRecord(entry) ? visitNode(entry) : entry,
|
|
269
|
+
]),
|
|
270
|
+
);
|
|
271
|
+
} else result[key] = visitValue(value);
|
|
272
|
+
}
|
|
273
|
+
return transform(result);
|
|
274
|
+
};
|
|
275
|
+
return visitNode(schema) as GenerativeUIJSONSchema;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const isString = (value: unknown): value is string => typeof value === "string";
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* A prop as the catalog states it. A component reference stays one. Any
|
|
282
|
+
* other value takes its literal schema, a data binding or a function call,
|
|
283
|
+
* and so does every value inside it: the properties of an object, the
|
|
284
|
+
* values of a record and the items of a list, because the renderer
|
|
285
|
+
* resolves dynamic values at any depth. The variants of a union are
|
|
286
|
+
* transformed in place.
|
|
287
|
+
*/
|
|
288
|
+
function dynamicProp(schema: unknown): GenerativeUIJSONSchema {
|
|
289
|
+
if (!isRecord(schema)) return { $ref: DYNAMIC_REFS.unknown };
|
|
290
|
+
if (childRefKind(schema)) return schema;
|
|
291
|
+
const { description, ...literal } = withDynamicChildren(schema);
|
|
292
|
+
return {
|
|
293
|
+
...(typeof description === "string" ? { description } : {}),
|
|
294
|
+
anyOf: [literal, { $ref: DATA_BINDING_REF }, { $ref: FUNCTION_CALL_REF }],
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The schema with its nested values made dynamic, itself unchanged. */
|
|
299
|
+
function withDynamicChildren(
|
|
300
|
+
schema: Record<string, unknown>,
|
|
301
|
+
): Record<string, unknown> {
|
|
302
|
+
const result = { ...schema };
|
|
303
|
+
if (isRecord(schema.properties)) {
|
|
304
|
+
result.properties = Object.fromEntries(
|
|
305
|
+
Object.entries(schema.properties).map(([key, value]) => [
|
|
306
|
+
key,
|
|
307
|
+
dynamicProp(value),
|
|
308
|
+
]),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if (isRecord(schema.items)) result.items = dynamicProp(schema.items);
|
|
312
|
+
// A tuple, as the converter writes it; the catalog spells it prefixItems.
|
|
313
|
+
if (Array.isArray(schema.items)) {
|
|
314
|
+
result.items = schema.items.map((item) =>
|
|
315
|
+
isRecord(item) ? dynamicProp(item) : item,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
if (isRecord(schema.additionalItems)) {
|
|
319
|
+
result.additionalItems = dynamicProp(schema.additionalItems);
|
|
320
|
+
}
|
|
321
|
+
// The definitions a converter wrote: referenced from value positions.
|
|
322
|
+
for (const key of ["$defs", "definitions"] as const) {
|
|
323
|
+
const defs = schema[key];
|
|
324
|
+
if (isRecord(defs)) {
|
|
325
|
+
result[key] = Object.fromEntries(
|
|
326
|
+
Object.entries(defs).map(([name, def]) => [
|
|
327
|
+
name,
|
|
328
|
+
isRecord(def) ? withDynamicChildren(def) : def,
|
|
329
|
+
]),
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (Array.isArray(schema.allOf)) {
|
|
334
|
+
result.allOf = schema.allOf.map((part) =>
|
|
335
|
+
isRecord(part) ? withDynamicChildren(part) : part,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
// The values of a record, by any key or by key pattern.
|
|
339
|
+
if (isRecord(schema.additionalProperties)) {
|
|
340
|
+
result.additionalProperties = dynamicProp(schema.additionalProperties);
|
|
341
|
+
}
|
|
342
|
+
if (isRecord(schema.patternProperties)) {
|
|
343
|
+
result.patternProperties = Object.fromEntries(
|
|
344
|
+
Object.entries(schema.patternProperties).map(([pattern, value]) => [
|
|
345
|
+
pattern,
|
|
346
|
+
dynamicProp(value),
|
|
347
|
+
]),
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
for (const key of ["anyOf", "oneOf"] as const) {
|
|
351
|
+
const variants = schema[key];
|
|
352
|
+
if (Array.isArray(variants)) {
|
|
353
|
+
result[key] = variants.map((variant) =>
|
|
354
|
+
isRecord(variant) && !childRefKind(variant)
|
|
355
|
+
? withDynamicChildren(variant)
|
|
356
|
+
: variant,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return result;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* The args of a declared function as the catalog states them: the object
|
|
365
|
+
* stays an object, each arg takes a value, a binding or a function call.
|
|
366
|
+
*/
|
|
367
|
+
function dynamicArgs(schema: GenerativeUIJSONSchema): GenerativeUIJSONSchema {
|
|
368
|
+
return withDynamicChildren(schema);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** One `functions` entry, in the A2UI `FunctionDefinition` shape. */
|
|
372
|
+
function functionEntry(
|
|
373
|
+
name: string,
|
|
374
|
+
permissions: A2UIFunctionPermissions,
|
|
375
|
+
parts: {
|
|
376
|
+
description: string | undefined;
|
|
377
|
+
returnType: string;
|
|
378
|
+
args: GenerativeUIJSONSchema;
|
|
379
|
+
},
|
|
380
|
+
): GenerativeUIJSONSchema {
|
|
381
|
+
const callers = functionCallers(permissions);
|
|
382
|
+
const allowedCallers: A2UIAllowedCallers =
|
|
383
|
+
callers.renderer && callers.agent
|
|
384
|
+
? "rendererOrAgent"
|
|
385
|
+
: callers.agent
|
|
386
|
+
? "agentOnly"
|
|
387
|
+
: "rendererOnly";
|
|
388
|
+
// The metaschema's activation rule has no `required`, so it also matches
|
|
389
|
+
// an entry without the key: the value is always written out.
|
|
390
|
+
// `args` is required only when an arg is.
|
|
391
|
+
const argsRequired = hasRequiredArgs(parts.args);
|
|
392
|
+
return {
|
|
393
|
+
type: "object",
|
|
394
|
+
...(parts.description ? { description: parts.description } : {}),
|
|
395
|
+
returnType: parts.returnType,
|
|
396
|
+
allowedCallers,
|
|
397
|
+
requiresUserActivation: permissions.requiresUserActivation === true,
|
|
398
|
+
properties: { "@call": { const: name }, args: parts.args },
|
|
399
|
+
required: argsRequired ? ["@call", "args"] : ["@call"],
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* True when an empty args object fails the schema: a key is required at
|
|
405
|
+
* its top, in an `allOf` part, or in every variant of an `anyOf` / `oneOf`.
|
|
406
|
+
*/
|
|
407
|
+
function hasRequiredArgs(schema: unknown): boolean {
|
|
408
|
+
if (!isRecord(schema)) return false;
|
|
409
|
+
if (Array.isArray(schema.required) && schema.required.length > 0) return true;
|
|
410
|
+
if (Array.isArray(schema.allOf) && schema.allOf.some(hasRequiredArgs)) {
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
for (const key of ["anyOf", "oneOf"] as const) {
|
|
414
|
+
const variants = schema[key];
|
|
415
|
+
if (Array.isArray(variants) && variants.length > 0) {
|
|
416
|
+
if (variants.every(hasRequiredArgs)) return true;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* The JSON Schema of the args of a built-in function, from its short
|
|
424
|
+
* TypeScript-like signature (`{ value: number; decimals?: number }`). Each
|
|
425
|
+
* arg takes a literal, a binding or a function call, so a type maps to the
|
|
426
|
+
* A2UI dynamic type; `[name: string]: T` becomes `additionalProperties`.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```ts
|
|
430
|
+
* argsSchemaFromSignature("{ value: string; min?: number }");
|
|
431
|
+
* // { type: "object", properties: { value: { $ref: ".../DynamicString" }, min: { $ref: ".../DynamicNumber" } }, required: ["value"], additionalProperties: false }
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
export function argsSchemaFromSignature(
|
|
435
|
+
signature: string,
|
|
436
|
+
): GenerativeUIJSONSchema {
|
|
437
|
+
const body = signature.trim().replace(/^\{/, "").replace(/\}$/, "");
|
|
438
|
+
const properties: Record<string, GenerativeUIJSONSchema> = {};
|
|
439
|
+
const required: string[] = [];
|
|
440
|
+
let additionalProperties: GenerativeUIJSONSchema | false = false;
|
|
441
|
+
for (const part of body.split(";")) {
|
|
442
|
+
const field = part.trim();
|
|
443
|
+
if (field === "") continue;
|
|
444
|
+
const index = field.match(/^\[\w+: string\]:\s*(.+)$/);
|
|
445
|
+
if (index) {
|
|
446
|
+
additionalProperties = typeSchema(index[1]);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const named = field.match(/^(\w+)(\?)?:\s*(.+)$/);
|
|
450
|
+
if (!named) continue;
|
|
451
|
+
properties[named[1]] = typeSchema(named[3]);
|
|
452
|
+
if (!named[2]) required.push(named[1]);
|
|
453
|
+
}
|
|
454
|
+
return {
|
|
455
|
+
type: "object",
|
|
456
|
+
properties,
|
|
457
|
+
...(required.length > 0 ? { required } : {}),
|
|
458
|
+
additionalProperties,
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function typeSchema(type: string): GenerativeUIJSONSchema {
|
|
463
|
+
const trimmed = type.trim();
|
|
464
|
+
const alternatives = trimmed.split("|").map((part) => part.trim());
|
|
465
|
+
if (alternatives.length > 1) return { anyOf: alternatives.map(typeSchema) };
|
|
466
|
+
if (trimmed in DYNAMIC_REFS) return { $ref: DYNAMIC_REFS[trimmed] };
|
|
467
|
+
if (trimmed.endsWith("[]")) {
|
|
468
|
+
// The whole list can be bound or computed, like any other arg.
|
|
469
|
+
return {
|
|
470
|
+
anyOf: [
|
|
471
|
+
{ type: "array", items: typeSchema(trimmed.slice(0, -2)) },
|
|
472
|
+
{ $ref: DATA_BINDING_REF },
|
|
473
|
+
{ $ref: FUNCTION_CALL_REF },
|
|
474
|
+
],
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
return { $ref: DYNAMIC_REFS.unknown };
|
|
478
|
+
}
|