@copilotkit/shared 1.80.0 → 1.82.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/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 +63 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +9 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1421 -20
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/telemetry-client.cjs +1 -1
- package/dist/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/telemetry/telemetry-client.d.cts.map +1 -1
- package/dist/telemetry/telemetry-client.d.mts.map +1 -1
- package/dist/telemetry/telemetry-client.mjs +1 -1
- package/dist/telemetry/telemetry-client.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.map +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 +10 -7
- package/dist/utils/inspector-learning.cjs.map +1 -1
- package/dist/utils/inspector-learning.d.cts.map +1 -1
- package/dist/utils/inspector-learning.d.mts.map +1 -1
- package/dist/utils/inspector-learning.mjs +11 -7
- 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 +1 -1
- 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/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/telemetry-client.test.ts +18 -0
- package/src/telemetry/telemetry-client.ts +3 -1
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/inspector-learning.test.ts +58 -0
- package/src/utils/inspector-learning.ts +26 -5
- package/src/utils/inspector-metadata.test.ts +2 -1
- package/src/utils/inspector-metadata.ts +3 -13
- package/src/utils.test.ts +71 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const require_is_record = require('./generative-ui/is-record.cjs');
|
|
2
|
+
const require_types = require('./generative-ui/types.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/a2ui-envelope.ts
|
|
5
|
+
/**
|
|
6
|
+
* The generative UI envelope that the client sends on every run as
|
|
7
|
+
* `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities
|
|
8
|
+
* shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.
|
|
9
|
+
* CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.
|
|
10
|
+
* The runtime reads it to pick the transport and to build the guide.
|
|
11
|
+
*/
|
|
12
|
+
/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */
|
|
13
|
+
const COPILOTKIT_FORWARDED_PROPS_KEY = "copilotkit";
|
|
14
|
+
/** Version of the envelope shape (not of the UI protocol that it carries). */
|
|
15
|
+
const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
16
|
+
function isOptionalString(value) {
|
|
17
|
+
return value === void 0 || typeof value === "string";
|
|
18
|
+
}
|
|
19
|
+
function isNonEmptyString(value) {
|
|
20
|
+
return typeof value === "string" && value !== "";
|
|
21
|
+
}
|
|
22
|
+
function isRecordOf(value, isEntry) {
|
|
23
|
+
return require_is_record.isRecord(value) && Object.values(value).every(isEntry);
|
|
24
|
+
}
|
|
25
|
+
/** A function, or an event of a component: a description and a parameter schema. */
|
|
26
|
+
function isSchemaEntry(value) {
|
|
27
|
+
return require_is_record.isRecord(value) && require_is_record.isRecord(value.parameters) && isOptionalString(value.description);
|
|
28
|
+
}
|
|
29
|
+
function isSlotKind(value) {
|
|
30
|
+
return value === "single" || value === "multiple";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `validateOperation` reads `properties` and `required` of a props schema,
|
|
34
|
+
* so those two must have the right shape when they are there.
|
|
35
|
+
*/
|
|
36
|
+
function isPropsSchema(value) {
|
|
37
|
+
return require_is_record.isRecord(value) && (value.properties === void 0 || require_is_record.isRecord(value.properties)) && (value.required === void 0 || Array.isArray(value.required) && value.required.every((name) => typeof name === "string"));
|
|
38
|
+
}
|
|
39
|
+
function isComponentManifest(value) {
|
|
40
|
+
if (!require_is_record.isRecord(value) || !isPropsSchema(value.props) || !isOptionalString(value.description) || value.events !== void 0 && !isRecordOf(value.events, isSchemaEntry) || value.slots !== void 0 && !isRecordOf(value.slots, isSlotKind)) return false;
|
|
41
|
+
const order = value.parameterOrder;
|
|
42
|
+
if (order === void 0) return true;
|
|
43
|
+
const keys = new Set([
|
|
44
|
+
...Object.keys(value.props.properties ?? {}),
|
|
45
|
+
...Object.keys(value.slots ?? {}),
|
|
46
|
+
...Object.keys(value.events ?? {})
|
|
47
|
+
]);
|
|
48
|
+
return Array.isArray(order) && new Set(order).size === order.length && order.every((key) => typeof key === "string" && keys.has(key));
|
|
49
|
+
}
|
|
50
|
+
function isInlineCatalog(value) {
|
|
51
|
+
return require_is_record.isRecord(value) && isNonEmptyString(value.catalogId) && value.protocolVersion === "1.0" && (value.components === void 0 || isRecordOf(value.components, isComponentManifest)) && isRecordOf(value.functions, isSchemaEntry) && isOptionalString(value.instructions);
|
|
52
|
+
}
|
|
53
|
+
function isCapabilities(value) {
|
|
54
|
+
return require_is_record.isRecord(value) && Array.isArray(value.supportedCatalogIds) && value.supportedCatalogIds.length > 0 && value.supportedCatalogIds.every(isNonEmptyString) && (value.inlineCatalogs === void 0 || Array.isArray(value.inlineCatalogs) && value.inlineCatalogs.every(isInlineCatalog));
|
|
55
|
+
}
|
|
56
|
+
/** A short list of component names. */
|
|
57
|
+
function isComponentNameList(value) {
|
|
58
|
+
return Array.isArray(value) && value.length <= require_types.GENERATIVE_UI_MAX_TARGET_COMPONENTS && value.every(isNonEmptyString);
|
|
59
|
+
}
|
|
60
|
+
function isTarget(value) {
|
|
61
|
+
return require_is_record.isRecord(value) && isNonEmptyString(value.id) && (value.kind === "chat" || value.kind === "inline") && isOptionalString(value.description) && (value.components === void 0 || isComponentNameList(value.components));
|
|
62
|
+
}
|
|
63
|
+
/** A short list of short strings. The summary limits keep the guide small. */
|
|
64
|
+
function isRejectionList(value) {
|
|
65
|
+
return Array.isArray(value) && value.length <= require_types.GENERATIVE_UI_MAX_SUMMARY_REJECTIONS && value.every((entry) => typeof entry === "string" && entry.length <= require_types.GENERATIVE_UI_MAX_REJECTION_LENGTH);
|
|
66
|
+
}
|
|
67
|
+
function isSurfaceEntry(value) {
|
|
68
|
+
return require_is_record.isRecord(value) && isNonEmptyString(value.target) && (value.dataModel === void 0 || require_is_record.isRecord(value.dataModel)) && (value.rejected === void 0 || isRejectionList(value.rejected)) && (value.status === void 0 || value.status === "deleted" || value.status === "failed");
|
|
69
|
+
}
|
|
70
|
+
/** Checks the `copilotkit_` keys only. Other extensions can be there too. */
|
|
71
|
+
function isExtensions(value) {
|
|
72
|
+
return require_is_record.isRecord(value) && value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT && (value.copilotkit_transport === "tool" || value.copilotkit_transport === "markdown") && Array.isArray(value.copilotkit_targets) && value.copilotkit_targets.every(isTarget) && isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) && typeof value.copilotkit_contextSent === "boolean";
|
|
73
|
+
}
|
|
74
|
+
function isEnvelope(value) {
|
|
75
|
+
return require_is_record.isRecord(value) && isCapabilities(value["v1.0"]) && require_is_record.isRecord(value.metadata) && isExtensions(value.metadata.extensions);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Reads the generative UI envelope from the `forwardedProps` of a run.
|
|
79
|
+
*
|
|
80
|
+
* The envelope comes from the client, so it is not trusted: every field is
|
|
81
|
+
* checked. Returns `undefined` when the envelope is not there, or when it is
|
|
82
|
+
* malformed (for example a wrong `copilotkit_format` or
|
|
83
|
+
* `copilotkit_transport`, or no catalog id).
|
|
84
|
+
*
|
|
85
|
+
* @param forwardedProps - The `forwardedProps` of the run input.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
90
|
+
* if (envelope?.metadata.extensions.copilotkit_transport === "markdown") {
|
|
91
|
+
* // The model writes ```a2ui fences. Add no render tool.
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
function readCopilotKitUIEnvelope(forwardedProps) {
|
|
96
|
+
if (!require_is_record.isRecord(forwardedProps)) return void 0;
|
|
97
|
+
const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];
|
|
98
|
+
if (!require_is_record.isRecord(namespace)) return void 0;
|
|
99
|
+
const envelope = namespace.ui;
|
|
100
|
+
return isEnvelope(envelope) ? envelope : void 0;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Turns an envelope into the manifest that the runtime and the prompts use.
|
|
104
|
+
*
|
|
105
|
+
* The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists
|
|
106
|
+
* all of them. The components, functions, and instructions come from the
|
|
107
|
+
* inline catalog of the first id.
|
|
108
|
+
* With no such inline catalog, there are no components and no functions.
|
|
109
|
+
*
|
|
110
|
+
* The result also has:
|
|
111
|
+
* - `transport` and `contextSent`, from the extensions.
|
|
112
|
+
* - `instructions`, when the inline catalog has them.
|
|
113
|
+
* - `hasComponents`: false when the client left the component schemas out.
|
|
114
|
+
* The runtime cannot check operations without them.
|
|
115
|
+
* - `action`: the A2UI action message of the user event that started the
|
|
116
|
+
* run, when one did.
|
|
117
|
+
*
|
|
118
|
+
* @param envelope - An envelope from `readCopilotKitUIEnvelope`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
123
|
+
* const manifest = envelope && envelopeToManifest(envelope);
|
|
124
|
+
* if (manifest?.transport === "tool" && manifest.hasComponents) {
|
|
125
|
+
* // Add the render tool and validate operations against manifest.components.
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function envelopeToManifest(envelope) {
|
|
130
|
+
const { supportedCatalogIds, inlineCatalogs } = envelope["v1.0"];
|
|
131
|
+
const catalogId = supportedCatalogIds[0];
|
|
132
|
+
const catalog = inlineCatalogs?.find((entry) => entry.catalogId === catalogId);
|
|
133
|
+
const extensions = envelope.metadata.extensions;
|
|
134
|
+
return {
|
|
135
|
+
catalogId,
|
|
136
|
+
catalogIds: supportedCatalogIds,
|
|
137
|
+
components: catalog?.components ?? {},
|
|
138
|
+
functions: catalog?.functions ?? {},
|
|
139
|
+
targets: extensions.copilotkit_targets,
|
|
140
|
+
surfaces: extensions.copilotkit_surfaces,
|
|
141
|
+
transport: extensions.copilotkit_transport,
|
|
142
|
+
contextSent: extensions.copilotkit_contextSent,
|
|
143
|
+
instructions: catalog?.instructions,
|
|
144
|
+
hasComponents: catalog?.components !== void 0,
|
|
145
|
+
...extensions.copilotkit_action === void 0 ? {} : { action: extensions.copilotkit_action }
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
exports.COPILOTKIT_FORWARDED_PROPS_KEY = COPILOTKIT_FORWARDED_PROPS_KEY;
|
|
151
|
+
exports.COPILOTKIT_UI_ENVELOPE_FORMAT = COPILOTKIT_UI_ENVELOPE_FORMAT;
|
|
152
|
+
exports.envelopeToManifest = envelopeToManifest;
|
|
153
|
+
exports.readCopilotKitUIEnvelope = readCopilotKitUIEnvelope;
|
|
154
|
+
//# sourceMappingURL=a2ui-envelope.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a2ui-envelope.cjs","names":["isRecord","GENERATIVE_UI_MAX_TARGET_COMPONENTS","GENERATIVE_UI_MAX_SUMMARY_REJECTIONS","GENERATIVE_UI_MAX_REJECTION_LENGTH"],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n const catalogId = supportedCatalogIds[0];\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n };\n}\n"],"mappings":";;;;;;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CAC3B,MAAM,yBAAyB,UAC9B,MAAM,yBAAyB,eACjC,MAAM,QAAQ,MAAM,mBAAmB,IACvC,MAAM,mBAAmB,MAAM,SAAS,IACxC,WAAW,MAAM,qBAAqB,eAAe,IACrD,OAAO,MAAM,2BAA2B;;AAI5C,SAAS,WAAW,OAA+C;AACjE,QACEA,2BAAS,MAAM,IACf,eAAe,MAAM,QAAQ,IAC7BA,2BAAS,MAAM,SAAS,IACxB,aAAa,MAAM,SAAS,WAAW;;;;;;;;;;;;;;;;;;;;AAsB3C,SAAgB,yBAAyB,gBAAyB;AAChE,KAAI,CAACA,2BAAS,eAAe,CAAE,QAAO;CACtC,MAAM,YAAY,eAAe;AACjC,KAAI,CAACA,2BAAS,UAAU,CAAE,QAAO;CACjC,MAAM,WAAW,UAAU;AAC3B,QAAO,WAAW,SAAS,GAAG,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8B3C,SAAgB,mBAAmB,UAAgC;CACjE,MAAM,EAAE,qBAAqB,mBAAmB,SAAS;CACzD,MAAM,YAAY,oBAAoB;CACtC,MAAM,UAAU,gBAAgB,MAC7B,UAAU,MAAM,cAAc,UAChC;CACD,MAAM,aAAa,SAAS,SAAS;AASrC,QAAO;EAPL;EACA,YAAY;EACZ,YAAY,SAAS,cAAc,EAAE;EACrC,WAAW,SAAS,aAAa,EAAE;EACnC,SAAS,WAAW;EACpB,UAAU,WAAW;EAIrB,WAAW,WAAW;EACtB,aAAa,WAAW;EACxB,cAAc,SAAS;EACvB,eAAe,SAAS,eAAe;EACvC,GAAI,WAAW,sBAAsB,SACjC,EAAE,GACF,EAAE,QAAQ,WAAW,mBAAmB;EAC7C"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { A2UIActionMessage, GenerativeUIComponentManifest, GenerativeUIFunctionManifest, GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./generative-ui/types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/a2ui-envelope.d.ts
|
|
4
|
+
/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */
|
|
5
|
+
declare const COPILOTKIT_FORWARDED_PROPS_KEY = "copilotkit";
|
|
6
|
+
/** Version of the envelope shape (not of the UI protocol that it carries). */
|
|
7
|
+
declare const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
8
|
+
/** What the client renders and calls, and how the model must write UI. */
|
|
9
|
+
interface CopilotKitUIEnvelope {
|
|
10
|
+
/** The A2UI v1.0 renderer capabilities. */
|
|
11
|
+
"v1.0": {
|
|
12
|
+
/**
|
|
13
|
+
* The catalog that the client renders comes first, then the catalogs of
|
|
14
|
+
* its `extends`. An operation can name any of them.
|
|
15
|
+
*/
|
|
16
|
+
supportedCatalogIds: string[];
|
|
17
|
+
inlineCatalogs?: Array<{
|
|
18
|
+
catalogId: string;
|
|
19
|
+
protocolVersion: "1.0"; /** Not there when the client is set up with `includeCatalog: false`. */
|
|
20
|
+
components?: Record<string, GenerativeUIComponentManifest>; /** The app functions that are mounted now. */
|
|
21
|
+
functions: Record<string, GenerativeUIFunctionManifest>; /** The markdown guideline. Only the markdown transport sends it. */
|
|
22
|
+
instructions?: string;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
metadata: {
|
|
26
|
+
extensions: {
|
|
27
|
+
/** Envelope shape version. */copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT; /** "tool": the model calls the render tool. "markdown": the model writes ```a2ui fences. */
|
|
28
|
+
copilotkit_transport: "tool" | "markdown";
|
|
29
|
+
copilotkit_targets: GenerativeUIManifest["targets"];
|
|
30
|
+
copilotkit_surfaces: GenerativeUIManifest["surfaces"]; /** True when the client also sent the guide as context entries on this run. */
|
|
31
|
+
copilotkit_contextSent: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* The A2UI action that started this run, when a user event did: the
|
|
34
|
+
* structured form of the user message that the event added.
|
|
35
|
+
*/
|
|
36
|
+
copilotkit_action?: A2UIActionMessage;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reads the generative UI envelope from the `forwardedProps` of a run.
|
|
42
|
+
*
|
|
43
|
+
* The envelope comes from the client, so it is not trusted: every field is
|
|
44
|
+
* checked. Returns `undefined` when the envelope is not there, or when it is
|
|
45
|
+
* malformed (for example a wrong `copilotkit_format` or
|
|
46
|
+
* `copilotkit_transport`, or no catalog id).
|
|
47
|
+
*
|
|
48
|
+
* @param forwardedProps - The `forwardedProps` of the run input.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
53
|
+
* if (envelope?.metadata.extensions.copilotkit_transport === "markdown") {
|
|
54
|
+
* // The model writes ```a2ui fences. Add no render tool.
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function readCopilotKitUIEnvelope(forwardedProps: unknown): CopilotKitUIEnvelope | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Turns an envelope into the manifest that the runtime and the prompts use.
|
|
61
|
+
*
|
|
62
|
+
* The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists
|
|
63
|
+
* all of them. The components, functions, and instructions come from the
|
|
64
|
+
* inline catalog of the first id.
|
|
65
|
+
* With no such inline catalog, there are no components and no functions.
|
|
66
|
+
*
|
|
67
|
+
* The result also has:
|
|
68
|
+
* - `transport` and `contextSent`, from the extensions.
|
|
69
|
+
* - `instructions`, when the inline catalog has them.
|
|
70
|
+
* - `hasComponents`: false when the client left the component schemas out.
|
|
71
|
+
* The runtime cannot check operations without them.
|
|
72
|
+
* - `action`: the A2UI action message of the user event that started the
|
|
73
|
+
* run, when one did.
|
|
74
|
+
*
|
|
75
|
+
* @param envelope - An envelope from `readCopilotKitUIEnvelope`.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
80
|
+
* const manifest = envelope && envelopeToManifest(envelope);
|
|
81
|
+
* if (manifest?.transport === "tool" && manifest.hasComponents) {
|
|
82
|
+
* // Add the render tool and validate operations against manifest.components.
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function envelopeToManifest(envelope: CopilotKitUIEnvelope): {
|
|
87
|
+
action?: A2UIActionMessage | undefined;
|
|
88
|
+
transport: "tool" | "markdown";
|
|
89
|
+
contextSent: boolean;
|
|
90
|
+
instructions: string | undefined;
|
|
91
|
+
hasComponents: boolean;
|
|
92
|
+
catalogId: string;
|
|
93
|
+
catalogIds?: string[];
|
|
94
|
+
components: Record<string, GenerativeUIComponentManifest>;
|
|
95
|
+
functions: Record<string, GenerativeUIFunctionManifest>;
|
|
96
|
+
targets: GenerativeUITarget[];
|
|
97
|
+
surfaces: Record<string, GenerativeUISurfaceSummary>;
|
|
98
|
+
};
|
|
99
|
+
//#endregion
|
|
100
|
+
export { COPILOTKIT_FORWARDED_PROPS_KEY, COPILOTKIT_UI_ENVELOPE_FORMAT, CopilotKitUIEnvelope, envelopeToManifest, readCopilotKitUIEnvelope };
|
|
101
|
+
//# sourceMappingURL=a2ui-envelope.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a2ui-envelope.d.cts","names":[],"sources":["../src/a2ui-envelope.ts"],"mappings":";;;;cAuBa,8BAAA;AAAb;AAAA,cAGa,6BAAA;;UAGI,oBAAA;EAN0B;EAQzC,MAAA;IALwC;;;;IAUtC,mBAAA;IACA,cAAA,GAAiB,KAAA;MACf,SAAA;MACA,eAAA,SAEa;MAAb,UAAA,GAAa,MAAA,SAAe,6BAAA,GAEjB;MAAX,SAAA,EAAW,MAAA,SAAe,4BAAA,GAQA;MAN1B,YAAA;IAAA;EAAA;EAGJ,QAAA;IACE,UAAA;MAlBF,8BAoBI,iBAAA,SAA0B,6BAAA,EAd5B;MAgBE,oBAAA;MACA,kBAAA,EAAoB,oBAAA;MACpB,mBAAA,EAAqB,oBAAA,cAdrB;MAgBA,sBAAA;MAhB4B;;;;MAqB5B,iBAAA,GAAoB,iBAAA;IAAA;EAAA;AAAA;;;;;;;;;;;;AA0L1B;;;;;AAmCA;;iBAnCgB,wBAAA,CAAyB,cAAA,YAAuB,oBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmChD,kBAAA,CAAmB,QAAA,EAAU,oBAAA"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { A2UIActionMessage, GenerativeUIComponentManifest, GenerativeUIFunctionManifest, GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./generative-ui/types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/a2ui-envelope.d.ts
|
|
4
|
+
/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */
|
|
5
|
+
declare const COPILOTKIT_FORWARDED_PROPS_KEY = "copilotkit";
|
|
6
|
+
/** Version of the envelope shape (not of the UI protocol that it carries). */
|
|
7
|
+
declare const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
8
|
+
/** What the client renders and calls, and how the model must write UI. */
|
|
9
|
+
interface CopilotKitUIEnvelope {
|
|
10
|
+
/** The A2UI v1.0 renderer capabilities. */
|
|
11
|
+
"v1.0": {
|
|
12
|
+
/**
|
|
13
|
+
* The catalog that the client renders comes first, then the catalogs of
|
|
14
|
+
* its `extends`. An operation can name any of them.
|
|
15
|
+
*/
|
|
16
|
+
supportedCatalogIds: string[];
|
|
17
|
+
inlineCatalogs?: Array<{
|
|
18
|
+
catalogId: string;
|
|
19
|
+
protocolVersion: "1.0"; /** Not there when the client is set up with `includeCatalog: false`. */
|
|
20
|
+
components?: Record<string, GenerativeUIComponentManifest>; /** The app functions that are mounted now. */
|
|
21
|
+
functions: Record<string, GenerativeUIFunctionManifest>; /** The markdown guideline. Only the markdown transport sends it. */
|
|
22
|
+
instructions?: string;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
metadata: {
|
|
26
|
+
extensions: {
|
|
27
|
+
/** Envelope shape version. */copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT; /** "tool": the model calls the render tool. "markdown": the model writes ```a2ui fences. */
|
|
28
|
+
copilotkit_transport: "tool" | "markdown";
|
|
29
|
+
copilotkit_targets: GenerativeUIManifest["targets"];
|
|
30
|
+
copilotkit_surfaces: GenerativeUIManifest["surfaces"]; /** True when the client also sent the guide as context entries on this run. */
|
|
31
|
+
copilotkit_contextSent: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* The A2UI action that started this run, when a user event did: the
|
|
34
|
+
* structured form of the user message that the event added.
|
|
35
|
+
*/
|
|
36
|
+
copilotkit_action?: A2UIActionMessage;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reads the generative UI envelope from the `forwardedProps` of a run.
|
|
42
|
+
*
|
|
43
|
+
* The envelope comes from the client, so it is not trusted: every field is
|
|
44
|
+
* checked. Returns `undefined` when the envelope is not there, or when it is
|
|
45
|
+
* malformed (for example a wrong `copilotkit_format` or
|
|
46
|
+
* `copilotkit_transport`, or no catalog id).
|
|
47
|
+
*
|
|
48
|
+
* @param forwardedProps - The `forwardedProps` of the run input.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
53
|
+
* if (envelope?.metadata.extensions.copilotkit_transport === "markdown") {
|
|
54
|
+
* // The model writes ```a2ui fences. Add no render tool.
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function readCopilotKitUIEnvelope(forwardedProps: unknown): CopilotKitUIEnvelope | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Turns an envelope into the manifest that the runtime and the prompts use.
|
|
61
|
+
*
|
|
62
|
+
* The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists
|
|
63
|
+
* all of them. The components, functions, and instructions come from the
|
|
64
|
+
* inline catalog of the first id.
|
|
65
|
+
* With no such inline catalog, there are no components and no functions.
|
|
66
|
+
*
|
|
67
|
+
* The result also has:
|
|
68
|
+
* - `transport` and `contextSent`, from the extensions.
|
|
69
|
+
* - `instructions`, when the inline catalog has them.
|
|
70
|
+
* - `hasComponents`: false when the client left the component schemas out.
|
|
71
|
+
* The runtime cannot check operations without them.
|
|
72
|
+
* - `action`: the A2UI action message of the user event that started the
|
|
73
|
+
* run, when one did.
|
|
74
|
+
*
|
|
75
|
+
* @param envelope - An envelope from `readCopilotKitUIEnvelope`.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
80
|
+
* const manifest = envelope && envelopeToManifest(envelope);
|
|
81
|
+
* if (manifest?.transport === "tool" && manifest.hasComponents) {
|
|
82
|
+
* // Add the render tool and validate operations against manifest.components.
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function envelopeToManifest(envelope: CopilotKitUIEnvelope): {
|
|
87
|
+
action?: A2UIActionMessage | undefined;
|
|
88
|
+
transport: "tool" | "markdown";
|
|
89
|
+
contextSent: boolean;
|
|
90
|
+
instructions: string | undefined;
|
|
91
|
+
hasComponents: boolean;
|
|
92
|
+
catalogId: string;
|
|
93
|
+
catalogIds?: string[];
|
|
94
|
+
components: Record<string, GenerativeUIComponentManifest>;
|
|
95
|
+
functions: Record<string, GenerativeUIFunctionManifest>;
|
|
96
|
+
targets: GenerativeUITarget[];
|
|
97
|
+
surfaces: Record<string, GenerativeUISurfaceSummary>;
|
|
98
|
+
};
|
|
99
|
+
//#endregion
|
|
100
|
+
export { COPILOTKIT_FORWARDED_PROPS_KEY, COPILOTKIT_UI_ENVELOPE_FORMAT, CopilotKitUIEnvelope, envelopeToManifest, readCopilotKitUIEnvelope };
|
|
101
|
+
//# sourceMappingURL=a2ui-envelope.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a2ui-envelope.d.mts","names":[],"sources":["../src/a2ui-envelope.ts"],"mappings":";;;;cAuBa,8BAAA;AAAb;AAAA,cAGa,6BAAA;;UAGI,oBAAA;EAN0B;EAQzC,MAAA;IALwC;;;;IAUtC,mBAAA;IACA,cAAA,GAAiB,KAAA;MACf,SAAA;MACA,eAAA,SAEa;MAAb,UAAA,GAAa,MAAA,SAAe,6BAAA,GAEjB;MAAX,SAAA,EAAW,MAAA,SAAe,4BAAA,GAQA;MAN1B,YAAA;IAAA;EAAA;EAGJ,QAAA;IACE,UAAA;MAlBF,8BAoBI,iBAAA,SAA0B,6BAAA,EAd5B;MAgBE,oBAAA;MACA,kBAAA,EAAoB,oBAAA;MACpB,mBAAA,EAAqB,oBAAA,cAdrB;MAgBA,sBAAA;MAhB4B;;;;MAqB5B,iBAAA,GAAoB,iBAAA;IAAA;EAAA;AAAA;;;;;;;;;;;;AA0L1B;;;;;AAmCA;;iBAnCgB,wBAAA,CAAyB,cAAA,YAAuB,oBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmChD,kBAAA,CAAmB,QAAA,EAAU,oBAAA"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isRecord } from "./generative-ui/is-record.mjs";
|
|
2
|
+
import { GENERATIVE_UI_MAX_REJECTION_LENGTH, GENERATIVE_UI_MAX_SUMMARY_REJECTIONS, GENERATIVE_UI_MAX_TARGET_COMPONENTS } from "./generative-ui/types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/a2ui-envelope.ts
|
|
5
|
+
/**
|
|
6
|
+
* The generative UI envelope that the client sends on every run as
|
|
7
|
+
* `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities
|
|
8
|
+
* shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.
|
|
9
|
+
* CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.
|
|
10
|
+
* The runtime reads it to pick the transport and to build the guide.
|
|
11
|
+
*/
|
|
12
|
+
/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */
|
|
13
|
+
const COPILOTKIT_FORWARDED_PROPS_KEY = "copilotkit";
|
|
14
|
+
/** Version of the envelope shape (not of the UI protocol that it carries). */
|
|
15
|
+
const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
16
|
+
function isOptionalString(value) {
|
|
17
|
+
return value === void 0 || typeof value === "string";
|
|
18
|
+
}
|
|
19
|
+
function isNonEmptyString(value) {
|
|
20
|
+
return typeof value === "string" && value !== "";
|
|
21
|
+
}
|
|
22
|
+
function isRecordOf(value, isEntry) {
|
|
23
|
+
return isRecord(value) && Object.values(value).every(isEntry);
|
|
24
|
+
}
|
|
25
|
+
/** A function, or an event of a component: a description and a parameter schema. */
|
|
26
|
+
function isSchemaEntry(value) {
|
|
27
|
+
return isRecord(value) && isRecord(value.parameters) && isOptionalString(value.description);
|
|
28
|
+
}
|
|
29
|
+
function isSlotKind(value) {
|
|
30
|
+
return value === "single" || value === "multiple";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `validateOperation` reads `properties` and `required` of a props schema,
|
|
34
|
+
* so those two must have the right shape when they are there.
|
|
35
|
+
*/
|
|
36
|
+
function isPropsSchema(value) {
|
|
37
|
+
return isRecord(value) && (value.properties === void 0 || isRecord(value.properties)) && (value.required === void 0 || Array.isArray(value.required) && value.required.every((name) => typeof name === "string"));
|
|
38
|
+
}
|
|
39
|
+
function isComponentManifest(value) {
|
|
40
|
+
if (!isRecord(value) || !isPropsSchema(value.props) || !isOptionalString(value.description) || value.events !== void 0 && !isRecordOf(value.events, isSchemaEntry) || value.slots !== void 0 && !isRecordOf(value.slots, isSlotKind)) return false;
|
|
41
|
+
const order = value.parameterOrder;
|
|
42
|
+
if (order === void 0) return true;
|
|
43
|
+
const keys = new Set([
|
|
44
|
+
...Object.keys(value.props.properties ?? {}),
|
|
45
|
+
...Object.keys(value.slots ?? {}),
|
|
46
|
+
...Object.keys(value.events ?? {})
|
|
47
|
+
]);
|
|
48
|
+
return Array.isArray(order) && new Set(order).size === order.length && order.every((key) => typeof key === "string" && keys.has(key));
|
|
49
|
+
}
|
|
50
|
+
function isInlineCatalog(value) {
|
|
51
|
+
return isRecord(value) && isNonEmptyString(value.catalogId) && value.protocolVersion === "1.0" && (value.components === void 0 || isRecordOf(value.components, isComponentManifest)) && isRecordOf(value.functions, isSchemaEntry) && isOptionalString(value.instructions);
|
|
52
|
+
}
|
|
53
|
+
function isCapabilities(value) {
|
|
54
|
+
return isRecord(value) && Array.isArray(value.supportedCatalogIds) && value.supportedCatalogIds.length > 0 && value.supportedCatalogIds.every(isNonEmptyString) && (value.inlineCatalogs === void 0 || Array.isArray(value.inlineCatalogs) && value.inlineCatalogs.every(isInlineCatalog));
|
|
55
|
+
}
|
|
56
|
+
/** A short list of component names. */
|
|
57
|
+
function isComponentNameList(value) {
|
|
58
|
+
return Array.isArray(value) && value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS && value.every(isNonEmptyString);
|
|
59
|
+
}
|
|
60
|
+
function isTarget(value) {
|
|
61
|
+
return isRecord(value) && isNonEmptyString(value.id) && (value.kind === "chat" || value.kind === "inline") && isOptionalString(value.description) && (value.components === void 0 || isComponentNameList(value.components));
|
|
62
|
+
}
|
|
63
|
+
/** A short list of short strings. The summary limits keep the guide small. */
|
|
64
|
+
function isRejectionList(value) {
|
|
65
|
+
return Array.isArray(value) && value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS && value.every((entry) => typeof entry === "string" && entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH);
|
|
66
|
+
}
|
|
67
|
+
function isSurfaceEntry(value) {
|
|
68
|
+
return isRecord(value) && isNonEmptyString(value.target) && (value.dataModel === void 0 || isRecord(value.dataModel)) && (value.rejected === void 0 || isRejectionList(value.rejected)) && (value.status === void 0 || value.status === "deleted" || value.status === "failed");
|
|
69
|
+
}
|
|
70
|
+
/** Checks the `copilotkit_` keys only. Other extensions can be there too. */
|
|
71
|
+
function isExtensions(value) {
|
|
72
|
+
return isRecord(value) && value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT && (value.copilotkit_transport === "tool" || value.copilotkit_transport === "markdown") && Array.isArray(value.copilotkit_targets) && value.copilotkit_targets.every(isTarget) && isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) && typeof value.copilotkit_contextSent === "boolean";
|
|
73
|
+
}
|
|
74
|
+
function isEnvelope(value) {
|
|
75
|
+
return isRecord(value) && isCapabilities(value["v1.0"]) && isRecord(value.metadata) && isExtensions(value.metadata.extensions);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Reads the generative UI envelope from the `forwardedProps` of a run.
|
|
79
|
+
*
|
|
80
|
+
* The envelope comes from the client, so it is not trusted: every field is
|
|
81
|
+
* checked. Returns `undefined` when the envelope is not there, or when it is
|
|
82
|
+
* malformed (for example a wrong `copilotkit_format` or
|
|
83
|
+
* `copilotkit_transport`, or no catalog id).
|
|
84
|
+
*
|
|
85
|
+
* @param forwardedProps - The `forwardedProps` of the run input.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
90
|
+
* if (envelope?.metadata.extensions.copilotkit_transport === "markdown") {
|
|
91
|
+
* // The model writes ```a2ui fences. Add no render tool.
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
function readCopilotKitUIEnvelope(forwardedProps) {
|
|
96
|
+
if (!isRecord(forwardedProps)) return void 0;
|
|
97
|
+
const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];
|
|
98
|
+
if (!isRecord(namespace)) return void 0;
|
|
99
|
+
const envelope = namespace.ui;
|
|
100
|
+
return isEnvelope(envelope) ? envelope : void 0;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Turns an envelope into the manifest that the runtime and the prompts use.
|
|
104
|
+
*
|
|
105
|
+
* The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists
|
|
106
|
+
* all of them. The components, functions, and instructions come from the
|
|
107
|
+
* inline catalog of the first id.
|
|
108
|
+
* With no such inline catalog, there are no components and no functions.
|
|
109
|
+
*
|
|
110
|
+
* The result also has:
|
|
111
|
+
* - `transport` and `contextSent`, from the extensions.
|
|
112
|
+
* - `instructions`, when the inline catalog has them.
|
|
113
|
+
* - `hasComponents`: false when the client left the component schemas out.
|
|
114
|
+
* The runtime cannot check operations without them.
|
|
115
|
+
* - `action`: the A2UI action message of the user event that started the
|
|
116
|
+
* run, when one did.
|
|
117
|
+
*
|
|
118
|
+
* @param envelope - An envelope from `readCopilotKitUIEnvelope`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
123
|
+
* const manifest = envelope && envelopeToManifest(envelope);
|
|
124
|
+
* if (manifest?.transport === "tool" && manifest.hasComponents) {
|
|
125
|
+
* // Add the render tool and validate operations against manifest.components.
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function envelopeToManifest(envelope) {
|
|
130
|
+
const { supportedCatalogIds, inlineCatalogs } = envelope["v1.0"];
|
|
131
|
+
const catalogId = supportedCatalogIds[0];
|
|
132
|
+
const catalog = inlineCatalogs?.find((entry) => entry.catalogId === catalogId);
|
|
133
|
+
const extensions = envelope.metadata.extensions;
|
|
134
|
+
return {
|
|
135
|
+
catalogId,
|
|
136
|
+
catalogIds: supportedCatalogIds,
|
|
137
|
+
components: catalog?.components ?? {},
|
|
138
|
+
functions: catalog?.functions ?? {},
|
|
139
|
+
targets: extensions.copilotkit_targets,
|
|
140
|
+
surfaces: extensions.copilotkit_surfaces,
|
|
141
|
+
transport: extensions.copilotkit_transport,
|
|
142
|
+
contextSent: extensions.copilotkit_contextSent,
|
|
143
|
+
instructions: catalog?.instructions,
|
|
144
|
+
hasComponents: catalog?.components !== void 0,
|
|
145
|
+
...extensions.copilotkit_action === void 0 ? {} : { action: extensions.copilotkit_action }
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
export { COPILOTKIT_FORWARDED_PROPS_KEY, COPILOTKIT_UI_ENVELOPE_FORMAT, envelopeToManifest, readCopilotKitUIEnvelope };
|
|
151
|
+
//# sourceMappingURL=a2ui-envelope.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"a2ui-envelope.mjs","names":[],"sources":["../src/a2ui-envelope.ts"],"sourcesContent":["/**\n * The generative UI envelope that the client sends on every run as\n * `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities\n * shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.\n * CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.\n * The runtime reads it to pick the transport and to build the guide.\n */\nimport {\n GENERATIVE_UI_MAX_REJECTION_LENGTH,\n GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,\n GENERATIVE_UI_MAX_TARGET_COMPONENTS,\n} from \"./generative-ui/types\";\nimport type {\n A2UIActionMessage,\n GenerativeUIComponentManifest,\n GenerativeUIFunctionManifest,\n GenerativeUIManifest,\n GenerativeUISurfaceSummary,\n GenerativeUITarget,\n} from \"./generative-ui/types\";\nimport { isRecord } from \"./generative-ui/is-record\";\n\n/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */\nexport const COPILOTKIT_FORWARDED_PROPS_KEY = \"copilotkit\";\n\n/** Version of the envelope shape (not of the UI protocol that it carries). */\nexport const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;\n\n/** What the client renders and calls, and how the model must write UI. */\nexport interface CopilotKitUIEnvelope {\n /** The A2UI v1.0 renderer capabilities. */\n \"v1.0\": {\n /**\n * The catalog that the client renders comes first, then the catalogs of\n * its `extends`. An operation can name any of them.\n */\n supportedCatalogIds: string[];\n inlineCatalogs?: Array<{\n catalogId: string;\n protocolVersion: \"1.0\";\n /** Not there when the client is set up with `includeCatalog: false`. */\n components?: Record<string, GenerativeUIComponentManifest>;\n /** The app functions that are mounted now. */\n functions: Record<string, GenerativeUIFunctionManifest>;\n /** The markdown guideline. Only the markdown transport sends it. */\n instructions?: string;\n }>;\n };\n metadata: {\n extensions: {\n /** Envelope shape version. */\n copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;\n /** \"tool\": the model calls the render tool. \"markdown\": the model writes ```a2ui fences. */\n copilotkit_transport: \"tool\" | \"markdown\";\n copilotkit_targets: GenerativeUIManifest[\"targets\"];\n copilotkit_surfaces: GenerativeUIManifest[\"surfaces\"];\n /** True when the client also sent the guide as context entries on this run. */\n copilotkit_contextSent: boolean;\n /**\n * The A2UI action that started this run, when a user event did: the\n * structured form of the user message that the event added.\n */\n copilotkit_action?: A2UIActionMessage;\n };\n };\n}\n\ntype InlineCatalog = NonNullable<\n CopilotKitUIEnvelope[\"v1.0\"][\"inlineCatalogs\"]\n>[number];\ntype Extensions = CopilotKitUIEnvelope[\"metadata\"][\"extensions\"];\n\nfunction isOptionalString(value: unknown) {\n return value === undefined || typeof value === \"string\";\n}\n\nfunction isNonEmptyString(value: unknown): value is string {\n return typeof value === \"string\" && value !== \"\";\n}\n\nfunction isRecordOf<T>(\n value: unknown,\n isEntry: (entry: unknown) => entry is T,\n): value is Record<string, T> {\n return isRecord(value) && Object.values(value).every(isEntry);\n}\n\n/** A function, or an event of a component: a description and a parameter schema. */\nfunction isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {\n return (\n isRecord(value) &&\n isRecord(value.parameters) &&\n isOptionalString(value.description)\n );\n}\n\nfunction isSlotKind(value: unknown): value is \"single\" | \"multiple\" {\n return value === \"single\" || value === \"multiple\";\n}\n\n/**\n * `validateOperation` reads `properties` and `required` of a props schema,\n * so those two must have the right shape when they are there.\n */\nfunction isPropsSchema(value: unknown): value is Record<string, unknown> {\n return (\n isRecord(value) &&\n (value.properties === undefined || isRecord(value.properties)) &&\n (value.required === undefined ||\n (Array.isArray(value.required) &&\n value.required.every((name) => typeof name === \"string\")))\n );\n}\n\nfunction isComponentManifest(\n value: unknown,\n): value is GenerativeUIComponentManifest {\n if (\n !isRecord(value) ||\n !isPropsSchema(value.props) ||\n !isOptionalString(value.description) ||\n (value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||\n (value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))\n )\n return false;\n const order = value.parameterOrder;\n if (order === undefined) return true;\n const keys = new Set([\n ...Object.keys(value.props.properties ?? {}),\n ...Object.keys(value.slots ?? {}),\n ...Object.keys(value.events ?? {}),\n ]);\n return (\n Array.isArray(order) &&\n new Set(order).size === order.length &&\n order.every((key) => typeof key === \"string\" && keys.has(key))\n );\n}\n\nfunction isInlineCatalog(value: unknown): value is InlineCatalog {\n return (\n isRecord(value) &&\n isNonEmptyString(value.catalogId) &&\n value.protocolVersion === \"1.0\" &&\n (value.components === undefined ||\n isRecordOf(value.components, isComponentManifest)) &&\n isRecordOf(value.functions, isSchemaEntry) &&\n isOptionalString(value.instructions)\n );\n}\n\nfunction isCapabilities(value: unknown): value is CopilotKitUIEnvelope[\"v1.0\"] {\n return (\n isRecord(value) &&\n Array.isArray(value.supportedCatalogIds) &&\n value.supportedCatalogIds.length > 0 &&\n value.supportedCatalogIds.every(isNonEmptyString) &&\n (value.inlineCatalogs === undefined ||\n (Array.isArray(value.inlineCatalogs) &&\n value.inlineCatalogs.every(isInlineCatalog)))\n );\n}\n\n/** A short list of component names. */\nfunction isComponentNameList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&\n value.every(isNonEmptyString)\n );\n}\n\nfunction isTarget(value: unknown): value is GenerativeUITarget {\n return (\n isRecord(value) &&\n isNonEmptyString(value.id) &&\n (value.kind === \"chat\" || value.kind === \"inline\") &&\n isOptionalString(value.description) &&\n (value.components === undefined || isComponentNameList(value.components))\n );\n}\n\n/** A short list of short strings. The summary limits keep the guide small. */\nfunction isRejectionList(value: unknown): value is string[] {\n return (\n Array.isArray(value) &&\n value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&\n value.every(\n (entry) =>\n typeof entry === \"string\" &&\n entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,\n )\n );\n}\n\nfunction isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {\n return (\n isRecord(value) &&\n isNonEmptyString(value.target) &&\n (value.dataModel === undefined || isRecord(value.dataModel)) &&\n (value.rejected === undefined || isRejectionList(value.rejected)) &&\n (value.status === undefined ||\n value.status === \"deleted\" ||\n value.status === \"failed\")\n );\n}\n\n/** Checks the `copilotkit_` keys only. Other extensions can be there too. */\nfunction isExtensions(value: unknown): value is Extensions {\n return (\n isRecord(value) &&\n value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&\n (value.copilotkit_transport === \"tool\" ||\n value.copilotkit_transport === \"markdown\") &&\n Array.isArray(value.copilotkit_targets) &&\n value.copilotkit_targets.every(isTarget) &&\n isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&\n typeof value.copilotkit_contextSent === \"boolean\"\n );\n}\n\nfunction isEnvelope(value: unknown): value is CopilotKitUIEnvelope {\n return (\n isRecord(value) &&\n isCapabilities(value[\"v1.0\"]) &&\n isRecord(value.metadata) &&\n isExtensions(value.metadata.extensions)\n );\n}\n\n/**\n * Reads the generative UI envelope from the `forwardedProps` of a run.\n *\n * The envelope comes from the client, so it is not trusted: every field is\n * checked. Returns `undefined` when the envelope is not there, or when it is\n * malformed (for example a wrong `copilotkit_format` or\n * `copilotkit_transport`, or no catalog id).\n *\n * @param forwardedProps - The `forwardedProps` of the run input.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * if (envelope?.metadata.extensions.copilotkit_transport === \"markdown\") {\n * // The model writes ```a2ui fences. Add no render tool.\n * }\n * ```\n */\nexport function readCopilotKitUIEnvelope(forwardedProps: unknown) {\n if (!isRecord(forwardedProps)) return undefined;\n const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];\n if (!isRecord(namespace)) return undefined;\n const envelope = namespace.ui;\n return isEnvelope(envelope) ? envelope : undefined;\n}\n\n/**\n * Turns an envelope into the manifest that the runtime and the prompts use.\n *\n * The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists\n * all of them. The components, functions, and instructions come from the\n * inline catalog of the first id.\n * With no such inline catalog, there are no components and no functions.\n *\n * The result also has:\n * - `transport` and `contextSent`, from the extensions.\n * - `instructions`, when the inline catalog has them.\n * - `hasComponents`: false when the client left the component schemas out.\n * The runtime cannot check operations without them.\n * - `action`: the A2UI action message of the user event that started the\n * run, when one did.\n *\n * @param envelope - An envelope from `readCopilotKitUIEnvelope`.\n *\n * @example\n * ```ts\n * const envelope = readCopilotKitUIEnvelope(input.forwardedProps);\n * const manifest = envelope && envelopeToManifest(envelope);\n * if (manifest?.transport === \"tool\" && manifest.hasComponents) {\n * // Add the render tool and validate operations against manifest.components.\n * }\n * ```\n */\nexport function envelopeToManifest(envelope: CopilotKitUIEnvelope) {\n const { supportedCatalogIds, inlineCatalogs } = envelope[\"v1.0\"];\n const catalogId = supportedCatalogIds[0];\n const catalog = inlineCatalogs?.find(\n (entry) => entry.catalogId === catalogId,\n );\n const extensions = envelope.metadata.extensions;\n const manifest: GenerativeUIManifest = {\n catalogId,\n catalogIds: supportedCatalogIds,\n components: catalog?.components ?? {},\n functions: catalog?.functions ?? {},\n targets: extensions.copilotkit_targets,\n surfaces: extensions.copilotkit_surfaces,\n };\n return {\n ...manifest,\n transport: extensions.copilotkit_transport,\n contextSent: extensions.copilotkit_contextSent,\n instructions: catalog?.instructions,\n hasComponents: catalog?.components !== undefined,\n ...(extensions.copilotkit_action === undefined\n ? {}\n : { action: extensions.copilotkit_action }),\n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