@copilotkit/shared 1.80.0 → 1.81.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a2ui-envelope.cjs +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +139 -0
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +60 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +60 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +134 -1
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/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/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/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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inspector-metadata.mjs","names":[],"sources":["../../src/utils/inspector-metadata.ts"],"sourcesContent":["/**\n * Inspector metadata supplied by a trusted CopilotKit runtime.\n *\n * Each optional module is independent so clients can render partial metadata\n * from runtimes that do not expose every module.\n */\nexport interface InspectorMetadataV1 {\n readonly schemaVersion: 1;\n readonly identity?: {\n readonly organizationName: string;\n readonly projectName: string;\n };\n readonly plan?: {\n readonly code: string;\n readonly label: string;\n };\n readonly license?: {\n readonly state: \"valid\" | \"none\" | \"expired\" | \"unknown\";\n };\n readonly action?:\n | { readonly kind: \"manage_plan\"; readonly url: string }\n | { readonly kind: \"renew\"; readonly url: string }\n | { readonly kind: \"enable_intelligence\"; readonly url: string };\n readonly usage?: {\n readonly used: number;\n readonly limit:\n | { readonly kind: \"finite\"; readonly value: number }\n | { readonly kind: \"unlimited\" }\n | { readonly kind: \"unknown\" };\n readonly expiringSoonCount?: number;\n };\n}\n\ntype UnknownRecord = Record<string, unknown>;\ntype InspectorIdentity = NonNullable<InspectorMetadataV1[\"identity\"]>;\ntype InspectorPlan = NonNullable<InspectorMetadataV1[\"plan\"]>;\ntype InspectorLicense = NonNullable<InspectorMetadataV1[\"license\"]>;\ntype InspectorAction = NonNullable<InspectorMetadataV1[\"action\"]>;\ntype InspectorUsage = NonNullable<InspectorMetadataV1[\"usage\"]>;\ntype InspectorUsageLimit = InspectorUsage[\"limit\"];\n\nfunction isRecord(value: unknown): value is UnknownRecord {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n\n try {\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n } catch {\n return false;\n }\n}\n\nfunction readOwnDataProperty(value: UnknownRecord, key: string): unknown {\n try {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n return descriptor !== undefined && \"value\" in descriptor\n ? descriptor.value\n : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction parseNonBlankString(value: unknown): string | undefined {\n if (typeof value !== \"string\") {\n return undefined;\n }\n\n const parsed = value.trim();\n return parsed.length > 0 ? parsed : undefined;\n}\n\nfunction parseIdentity(value: unknown): InspectorIdentity | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const organizationName = parseNonBlankString(value.organizationName);\n const projectName = parseNonBlankString(value.projectName);\n if (organizationName === undefined || projectName === undefined) {\n return undefined;\n }\n\n return { organizationName, projectName };\n}\n\nfunction parsePlan(value: unknown): InspectorPlan | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const code = parseNonBlankString(value.code);\n const label = parseNonBlankString(value.label);\n if (code === undefined || label === undefined) {\n return undefined;\n }\n\n return { code, label };\n}\n\nfunction parseLicense(value: unknown): InspectorLicense | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n switch (value.state) {\n case \"valid\":\n case \"none\":\n case \"expired\":\n case \"unknown\":\n return { state: value.state };\n default:\n return undefined;\n }\n}\n\nfunction parseActionKind(value: unknown): InspectorAction[\"kind\"] | undefined {\n switch (value) {\n case \"manage_plan\":\n case \"renew\":\n case \"enable_intelligence\":\n return value;\n default:\n return undefined;\n }\n}\n\nfunction parseSafeActionUrl(value: unknown): string | undefined {\n const url = parseNonBlankString(value);\n if (url === undefined || url.includes(\"?\") || url.includes(\"#\")) {\n return undefined;\n }\n\n const authorityStart = url.indexOf(\"://\");\n if (authorityStart < 1) {\n return undefined;\n }\n\n const authorityAndPath = url.slice(authorityStart + 3);\n const pathStart = authorityAndPath.indexOf(\"/\");\n const authority =\n pathStart === -1 ? authorityAndPath : authorityAndPath.slice(0, pathStart);\n if (authority.includes(\"@\")) {\n return undefined;\n }\n\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n return undefined;\n }\n\n if (parsed.hostname.length === 0 || parsed.username || parsed.password) {\n return undefined;\n }\n\n if (parsed.protocol === \"https:\") {\n return url;\n }\n\n const isLoopbackHost =\n parsed.hostname === \"localhost\" ||\n parsed.hostname === \"127.0.0.1\" ||\n parsed.hostname === \"[::1]\";\n if (parsed.protocol === \"http:\" && isLoopbackHost) {\n return url;\n }\n\n return undefined;\n}\n\nfunction parseAction(value: unknown): InspectorAction | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const kind = parseActionKind(value.kind);\n const url = parseSafeActionUrl(value.url);\n if (kind === undefined || url === undefined) {\n return undefined;\n }\n\n return { kind, url };\n}\n\nfunction isFiniteNonnegativeInteger(value: unknown): value is number {\n return typeof value === \"number\" && Number.isSafeInteger(value) && value >= 0;\n}\n\nfunction parseUsageLimit(value: unknown): InspectorUsageLimit | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n if (value.kind === \"finite\") {\n if (\n typeof value.value !== \"number\" ||\n !Number.isSafeInteger(value.value) ||\n value.value < 1\n ) {\n return undefined;\n }\n\n return { kind: \"finite\", value: value.value };\n }\n\n if (value.kind === \"unlimited\") {\n return { kind: \"unlimited\" };\n }\n\n if (value.kind === \"unknown\") {\n return { kind: \"unknown\" };\n }\n\n return undefined;\n}\n\nfunction parseUsage(value: unknown): InspectorUsage | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const limit = parseUsageLimit(value.limit);\n const used = value.used;\n if (!isFiniteNonnegativeInteger(used) || limit === undefined) {\n return undefined;\n }\n\n const rawExpiringSoonCount = readOwnDataProperty(value, \"expiringSoonCount\");\n const expiringSoonCount = isFiniteNonnegativeInteger(rawExpiringSoonCount)\n ? rawExpiringSoonCount\n : undefined;\n\n return {\n used,\n limit,\n ...(expiringSoonCount === undefined ? {} : { expiringSoonCount }),\n };\n}\n\n/**\n * Parses untrusted inspector metadata without letting one invalid optional\n * module hide the other valid modules.\n *\n * @param value - The decoded runtime response body.\n * @returns Normalized version 1 metadata, or `undefined` for an unsupported\n * top-level payload.\n */\nexport function parseInspectorMetadataV1(\n value: unknown,\n): InspectorMetadataV1 | undefined {\n if (!isRecord(value) || value.schemaVersion !== 1) {\n return undefined;\n }\n\n const identity = parseIdentity(value.identity);\n const plan = parsePlan(value.plan);\n const license = parseLicense(value.license);\n const action = parseAction(value.action);\n const usage = parseUsage(value.usage);\n\n return {\n schemaVersion: 1,\n ...(identity === undefined ? {} : { identity }),\n ...(plan === undefined ? {} : { plan }),\n ...(license === undefined ? {} : { license }),\n ...(action === undefined ? {} : { action }),\n ...(usage === undefined ? {} : { usage }),\n };\n}\n"],"mappings":";AAyCA,SAAS,SAAS,OAAwC;AACxD,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,MAAM,CACrE,QAAO;AAGT,KAAI;EACF,MAAM,YAAY,OAAO,eAAe,MAAM;AAC9C,SAAO,cAAc,OAAO,aAAa,cAAc;SACjD;AACN,SAAO;;;AAIX,SAAS,oBAAoB,OAAsB,KAAsB;AACvE,KAAI;EACF,MAAM,aAAa,OAAO,yBAAyB,OAAO,IAAI;AAC9D,SAAO,eAAe,UAAa,WAAW,aAC1C,WAAW,QACX;SACE;AACN;;;AAIJ,SAAS,oBAAoB,OAAoC;AAC/D,KAAI,OAAO,UAAU,SACnB;CAGF,MAAM,SAAS,MAAM,MAAM;AAC3B,QAAO,OAAO,SAAS,IAAI,SAAS;;AAGtC,SAAS,cAAc,OAA+C;AACpE,KAAI,CAAC,SAAS,MAAM,CAClB;CAGF,MAAM,mBAAmB,oBAAoB,MAAM,iBAAiB;CACpE,MAAM,cAAc,oBAAoB,MAAM,YAAY;AAC1D,KAAI,qBAAqB,UAAa,gBAAgB,OACpD;AAGF,QAAO;EAAE;EAAkB;EAAa;;AAG1C,SAAS,UAAU,OAA2C;AAC5D,KAAI,CAAC,SAAS,MAAM,CAClB;CAGF,MAAM,OAAO,oBAAo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AAY,SAAY,EAAE,GAAG,EAAE,SAAS;EAC5C,GAAI,WAAW,SAAY,EAAE,GAAG,EAAE,QAAQ;EAC1C,GAAI,UAAU,SAAY,EAAE,GAAG,EAAE,OAAO;EACzC"}
|
|
1
|
+
{"version":3,"file":"inspector-metadata.mjs","names":[],"sources":["../../src/utils/inspector-metadata.ts"],"sourcesContent":["import { isSafeDashboardUrl } from \"./dashboard-url\";\n\n/**\n * Inspector metadata supplied by a trusted CopilotKit runtime.\n *\n * Each optional module is independent so clients can render partial metadata\n * from runtimes that do not expose every module.\n */\nexport interface InspectorMetadataV1 {\n readonly schemaVersion: 1;\n readonly identity?: {\n readonly organizationName: string;\n readonly projectName: string;\n };\n readonly plan?: {\n readonly code: string;\n readonly label: string;\n };\n readonly license?: {\n readonly state: \"valid\" | \"none\" | \"expired\" | \"unknown\";\n };\n readonly action?:\n | { readonly kind: \"manage_plan\"; readonly url: string }\n | { readonly kind: \"renew\"; readonly url: string }\n | { readonly kind: \"enable_intelligence\"; readonly url: string };\n readonly usage?: {\n readonly used: number;\n readonly limit:\n | { readonly kind: \"finite\"; readonly value: number }\n | { readonly kind: \"unlimited\" }\n | { readonly kind: \"unknown\" };\n readonly expiringSoonCount?: number;\n };\n}\n\ntype UnknownRecord = Record<string, unknown>;\ntype InspectorIdentity = NonNullable<InspectorMetadataV1[\"identity\"]>;\ntype InspectorPlan = NonNullable<InspectorMetadataV1[\"plan\"]>;\ntype InspectorLicense = NonNullable<InspectorMetadataV1[\"license\"]>;\ntype InspectorAction = NonNullable<InspectorMetadataV1[\"action\"]>;\ntype InspectorUsage = NonNullable<InspectorMetadataV1[\"usage\"]>;\ntype InspectorUsageLimit = InspectorUsage[\"limit\"];\n\nfunction isRecord(value: unknown): value is UnknownRecord {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n\n try {\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n } catch {\n return false;\n }\n}\n\nfunction readOwnDataProperty(value: UnknownRecord, key: string): unknown {\n try {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n return descriptor !== undefined && \"value\" in descriptor\n ? descriptor.value\n : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction parseNonBlankString(value: unknown): string | undefined {\n if (typeof value !== \"string\") {\n return undefined;\n }\n\n const parsed = value.trim();\n return parsed.length > 0 ? parsed : undefined;\n}\n\nfunction parseIdentity(value: unknown): InspectorIdentity | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const organizationName = parseNonBlankString(value.organizationName);\n const projectName = parseNonBlankString(value.projectName);\n if (organizationName === undefined || projectName === undefined) {\n return undefined;\n }\n\n return { organizationName, projectName };\n}\n\nfunction parsePlan(value: unknown): InspectorPlan | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const code = parseNonBlankString(value.code);\n const label = parseNonBlankString(value.label);\n if (code === undefined || label === undefined) {\n return undefined;\n }\n\n return { code, label };\n}\n\nfunction parseLicense(value: unknown): InspectorLicense | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n switch (value.state) {\n case \"valid\":\n case \"none\":\n case \"expired\":\n case \"unknown\":\n return { state: value.state };\n default:\n return undefined;\n }\n}\n\nfunction parseActionKind(value: unknown): InspectorAction[\"kind\"] | undefined {\n switch (value) {\n case \"manage_plan\":\n case \"renew\":\n case \"enable_intelligence\":\n return value;\n default:\n return undefined;\n }\n}\n\nfunction parseSafeActionUrl(value: unknown): string | undefined {\n const url = parseNonBlankString(value);\n if (url === undefined || url.includes(\"?\") || url.includes(\"#\")) {\n return undefined;\n }\n\n const authorityStart = url.indexOf(\"://\");\n if (authorityStart < 1) {\n return undefined;\n }\n\n const authorityAndPath = url.slice(authorityStart + 3);\n const pathStart = authorityAndPath.indexOf(\"/\");\n const authority =\n pathStart === -1 ? authorityAndPath : authorityAndPath.slice(0, pathStart);\n if (authority.includes(\"@\")) {\n return undefined;\n }\n\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n return undefined;\n }\n\n if (parsed.hostname.length === 0 || parsed.username || parsed.password) {\n return undefined;\n }\n\n return isSafeDashboardUrl(parsed) ? url : undefined;\n}\n\nfunction parseAction(value: unknown): InspectorAction | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const kind = parseActionKind(value.kind);\n const url = parseSafeActionUrl(value.url);\n if (kind === undefined || url === undefined) {\n return undefined;\n }\n\n return { kind, url };\n}\n\nfunction isFiniteNonnegativeInteger(value: unknown): value is number {\n return typeof value === \"number\" && Number.isSafeInteger(value) && value >= 0;\n}\n\nfunction parseUsageLimit(value: unknown): InspectorUsageLimit | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n if (value.kind === \"finite\") {\n if (\n typeof value.value !== \"number\" ||\n !Number.isSafeInteger(value.value) ||\n value.value < 1\n ) {\n return undefined;\n }\n\n return { kind: \"finite\", value: value.value };\n }\n\n if (value.kind === \"unlimited\") {\n return { kind: \"unlimited\" };\n }\n\n if (value.kind === \"unknown\") {\n return { kind: \"unknown\" };\n }\n\n return undefined;\n}\n\nfunction parseUsage(value: unknown): InspectorUsage | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n\n const limit = parseUsageLimit(value.limit);\n const used = value.used;\n if (!isFiniteNonnegativeInteger(used) || limit === undefined) {\n return undefined;\n }\n\n const rawExpiringSoonCount = readOwnDataProperty(value, \"expiringSoonCount\");\n const expiringSoonCount = isFiniteNonnegativeInteger(rawExpiringSoonCount)\n ? rawExpiringSoonCount\n : undefined;\n\n return {\n used,\n limit,\n ...(expiringSoonCount === undefined ? {} : { expiringSoonCount }),\n };\n}\n\n/**\n * Parses untrusted inspector metadata without letting one invalid optional\n * module hide the other valid modules.\n *\n * @param value - The decoded runtime response body.\n * @returns Normalized version 1 metadata, or `undefined` for an unsupported\n * top-level payload.\n */\nexport function parseInspectorMetadataV1(\n value: unknown,\n): InspectorMetadataV1 | undefined {\n if (!isRecord(value) || value.schemaVersion !== 1) {\n return undefined;\n }\n\n const identity = parseIdentity(value.identity);\n const plan = parsePlan(value.plan);\n const license = parseLicense(value.license);\n const action = parseAction(value.action);\n const usage = parseUsage(value.usage);\n\n return {\n schemaVersion: 1,\n ...(identity === undefined ? {} : { identity }),\n ...(plan === undefined ? {} : { plan }),\n ...(license === undefined ? {} : { license }),\n ...(action === undefined ? {} : { action }),\n ...(usage === undefined ? {} : { usage }),\n };\n}\n"],"mappings":";;;AA2CA,SAAS,SAAS,OAAwC;AACxD,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,MAAM,CACrE,QAAO;AAGT,KAAI;EACF,MAAM,YAAY,OAAO,eAAe,MAAM;AAC9C,SAAO,cAAc,OAAO,aAAa,cAAc;SACjD;AACN,SAAO;;;AAIX,SAAS,oBAAoB,OAAsB,KAAsB;AACvE,KAAI;EACF,MAAM,aAAa,OAAO,yBAAyB,OAAO,IAAI;AAC9D,SAAO,eAAe,UAAa,WAAW,aAC1C,WAAW,QACX;SACE;AACN;;;AAIJ,SAAS,oBAAoB,OAAoC;AAC/D,KAAI,OAAO,UAAU,SACnB;CAGF,MAAM,SAAS,MAAM,MAAM;AAC3B,QAAO,OAAO,SAAS,IAAI,SAAS;;AAGtC,SAAS,cAAc,OAA+C;AACpE,KAAI,CAAC,SAAS,MAAM,CAClB;CAGF,MAAM,mBAAmB,oBAAoB,MAAM,iBAAiB;CACpE,MAAM,cAAc,oBAAoB,MAAM,YAAY;AAC1D,KAAI,qBAAqB,UAAa,gBAAgB,OACpD;AAGF,QAAO;EAAE;EAAkB;EAAa;;AAG1C,SAAS,UAAU,OAA2C;AAC5D,KAAI,CAAC,SAAS,MAAM,CAClB;CAGF,MAAM,OAAO,oBAAoB,MAAM,KAAK;CAC5C,MAAM,QAAQ,oBAAoB,MAAM,MAAM;AAC9C,KAAI,SAAS,UAAa,UAAU,OAClC;AAGF,QAAO;EAAE;EAAM;EAAO;;AAGxB,SAAS,aAAa,OAA8C;AAClE,KAAI,CAAC,SAAS,MAAM,CAClB;AAG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Q,EAEd;AAGF,SAAO;GAAE,MAAM;GAAU,OAAO,MAAM;GAAO;;AAG/C,KAAI,MAAM,SAAS,YACjB,QAAO,EAAE,MAAM,aAAa;AAG9B,KAAI,MAAM,SAAS,UACjB,QAAO,EAAE,MAAM,WAAW;;AAM9B,SAAS,WAAW,OAA4C;AAC9D,KAAI,CAAC,SAAS,MAAM,CAClB;CAGF,MAAM,QAAQ,gBAAgB,MAAM,MAAM;CAC1C,MAAM,OAAO,MAAM;AACnB,KAAI,CAAC,2BAA2B,KAAK,IAAI,UAAU,OACjD;CAGF,MAAM,uBAAuB,oBAAoB,OAAO,oBAAoB;CAC5E,MAAM,oBAAoB,2BAA2B,qBAAqB,GACtE,uBACA;AAEJ,QAAO;EACL;EACA;EACA,GAAI,sBAAsB,SAAY,EAAE,GAAG,EAAE,mBAAmB;EACjE;;;;;;;;;;AAWH,SAAgB,yBACd,OACiC;AACjC,KAAI,CAAC,SAAS,MAAM,IAAI,MAAM,kBAAkB,EAC9C;CAGF,MAAM,WAAW,cAAc,MAAM,SAAS;CAC9C,MAAM,OAAO,UAAU,MAAM,KAAK;CAClC,MAAM,UAAU,aAAa,MAAM,QAAQ;CAC3C,MAAM,SAAS,YAAY,MAAM,OAAO;CACxC,MAAM,QAAQ,WAAW,MAAM,MAAM;AAErC,QAAO;EACL,eAAe;EACf,GAAI,aAAa,SAAY,EAAE,GAAG,EAAE,UAAU;EAC9C,GAAI,SAAS,SAAY,EAAE,GAAG,EAAE,MAAM;EACtC,GAAI,YAAY,SAAY,EAAE,GAAG,EAAE,SAAS;EAC5C,GAAI,WAAW,SAAY,EAAE,GAAG,EAAE,QAAQ;EAC1C,GAAI,UAAU,SAAY,EAAE,GAAG,EAAE,OAAO;EACzC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { envelopeToManifest, readCopilotKitUIEnvelope } from "../a2ui-envelope";
|
|
3
|
+
import type { CopilotKitUIEnvelope } from "../a2ui-envelope";
|
|
4
|
+
|
|
5
|
+
type InlineCatalog = NonNullable<
|
|
6
|
+
CopilotKitUIEnvelope["v1.0"]["inlineCatalogs"]
|
|
7
|
+
>[number];
|
|
8
|
+
type Extensions = CopilotKitUIEnvelope["metadata"]["extensions"];
|
|
9
|
+
|
|
10
|
+
const productGrid = {
|
|
11
|
+
description: "A grid of products.",
|
|
12
|
+
props: {
|
|
13
|
+
type: "object",
|
|
14
|
+
properties: { title: { type: "string" } },
|
|
15
|
+
required: ["title"],
|
|
16
|
+
},
|
|
17
|
+
events: { onSelect: { parameters: { type: "object" } } },
|
|
18
|
+
slots: { children: "multiple" as const },
|
|
19
|
+
};
|
|
20
|
+
const addToCart = { description: "Add.", parameters: { type: "object" } };
|
|
21
|
+
const targets: Extensions["copilotkit_targets"] = [
|
|
22
|
+
{ id: "chat", kind: "chat" },
|
|
23
|
+
{
|
|
24
|
+
id: "sidebar",
|
|
25
|
+
kind: "inline",
|
|
26
|
+
description: "Right column.",
|
|
27
|
+
components: ["ProductGrid"],
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
const surfaces: Extensions["copilotkit_surfaces"] = {
|
|
31
|
+
cart: { target: "sidebar" },
|
|
32
|
+
form: {
|
|
33
|
+
target: "chat",
|
|
34
|
+
dataModel: { "/form/email": "me@x.io" },
|
|
35
|
+
rejected: ["operation 2: unknown component Foo"],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function createCatalog(overrides?: Partial<InlineCatalog>): InlineCatalog {
|
|
40
|
+
return {
|
|
41
|
+
catalogId: "acme/app@1",
|
|
42
|
+
protocolVersion: "1.0",
|
|
43
|
+
components: { ProductGrid: productGrid },
|
|
44
|
+
functions: { addToCart },
|
|
45
|
+
instructions: "Write fences.",
|
|
46
|
+
...overrides,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function createEnvelope(options?: {
|
|
51
|
+
catalog?: Partial<InlineCatalog>;
|
|
52
|
+
extensions?: Partial<Extensions>;
|
|
53
|
+
}): CopilotKitUIEnvelope {
|
|
54
|
+
return {
|
|
55
|
+
"v1.0": {
|
|
56
|
+
supportedCatalogIds: ["acme/app@1"],
|
|
57
|
+
inlineCatalogs: [createCatalog(options?.catalog)],
|
|
58
|
+
},
|
|
59
|
+
metadata: {
|
|
60
|
+
extensions: {
|
|
61
|
+
copilotkit_format: 3,
|
|
62
|
+
copilotkit_transport: "markdown",
|
|
63
|
+
copilotkit_targets: targets,
|
|
64
|
+
copilotkit_surfaces: surfaces,
|
|
65
|
+
copilotkit_contextSent: true,
|
|
66
|
+
...options?.extensions,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Reads `ui` from `forwardedProps.copilotkit.ui`. */
|
|
73
|
+
const readUI = (ui: unknown) =>
|
|
74
|
+
readCopilotKitUIEnvelope({ copilotkit: { ui }, other: 1 });
|
|
75
|
+
|
|
76
|
+
/** An envelope with one key of the inline catalog replaced by any value. */
|
|
77
|
+
const withCatalogField = (key: string, value: unknown) => {
|
|
78
|
+
const envelope = createEnvelope();
|
|
79
|
+
return {
|
|
80
|
+
...envelope,
|
|
81
|
+
"v1.0": {
|
|
82
|
+
...envelope["v1.0"],
|
|
83
|
+
inlineCatalogs: [{ ...createCatalog(), [key]: value }],
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** An envelope with one key of the capabilities replaced by any value. */
|
|
89
|
+
const withCapability = (key: string, value: unknown) => {
|
|
90
|
+
const envelope = createEnvelope();
|
|
91
|
+
return { ...envelope, "v1.0": { ...envelope["v1.0"], [key]: value } };
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** An envelope with one CopilotKit extension replaced by any value. */
|
|
95
|
+
const withExtension = (key: string, value: unknown) => {
|
|
96
|
+
const envelope = createEnvelope();
|
|
97
|
+
return {
|
|
98
|
+
...envelope,
|
|
99
|
+
metadata: {
|
|
100
|
+
extensions: { ...envelope.metadata.extensions, [key]: value },
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
describe("readCopilotKitUIEnvelope", () => {
|
|
106
|
+
it("reads a markdown envelope from forwardedProps.copilotkit.ui", () => {
|
|
107
|
+
const envelope = createEnvelope();
|
|
108
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("reads a tool envelope without instructions and without components", () => {
|
|
112
|
+
const envelope: CopilotKitUIEnvelope = {
|
|
113
|
+
"v1.0": {
|
|
114
|
+
supportedCatalogIds: ["acme/app@1"],
|
|
115
|
+
inlineCatalogs: [
|
|
116
|
+
{ catalogId: "acme/app@1", protocolVersion: "1.0", functions: {} },
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
metadata: {
|
|
120
|
+
extensions: {
|
|
121
|
+
copilotkit_format: 3,
|
|
122
|
+
copilotkit_transport: "tool",
|
|
123
|
+
copilotkit_targets: [],
|
|
124
|
+
copilotkit_surfaces: {},
|
|
125
|
+
copilotkit_contextSent: false,
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("reads an envelope without inline catalogs", () => {
|
|
133
|
+
const envelope: CopilotKitUIEnvelope = {
|
|
134
|
+
...createEnvelope(),
|
|
135
|
+
"v1.0": { supportedCatalogIds: ["acme/app@1"] },
|
|
136
|
+
};
|
|
137
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("reads an envelope that carries other extensions too", () => {
|
|
141
|
+
const envelope = withExtension("a2ui_trace", { id: "t1" });
|
|
142
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("reads a target with 100 component names", () => {
|
|
146
|
+
const envelope = createEnvelope({
|
|
147
|
+
extensions: {
|
|
148
|
+
copilotkit_targets: [
|
|
149
|
+
{ id: "x", kind: "inline", components: Array(100).fill("Card") },
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("reads a surface with 20 rejections of 500 characters", () => {
|
|
157
|
+
const envelope = createEnvelope({
|
|
158
|
+
extensions: {
|
|
159
|
+
copilotkit_surfaces: {
|
|
160
|
+
cart: { target: "chat", rejected: Array(20).fill("x".repeat(500)) },
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("reads a deleted surface and a surface whose create failed", () => {
|
|
168
|
+
const envelope = createEnvelope({
|
|
169
|
+
extensions: {
|
|
170
|
+
copilotkit_surfaces: {
|
|
171
|
+
old: { target: "chat", status: "deleted" },
|
|
172
|
+
bad: { target: "chat", status: "failed", rejected: ["operation 0"] },
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
expect(readUI(envelope)).toEqual(envelope);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("returns undefined when there is no envelope", () => {
|
|
180
|
+
expect(readCopilotKitUIEnvelope(undefined)).toBeUndefined();
|
|
181
|
+
expect(readCopilotKitUIEnvelope("copilotkit")).toBeUndefined();
|
|
182
|
+
expect(readCopilotKitUIEnvelope({})).toBeUndefined();
|
|
183
|
+
expect(readCopilotKitUIEnvelope({ copilotkit: null })).toBeUndefined();
|
|
184
|
+
expect(readCopilotKitUIEnvelope({ copilotkit: {} })).toBeUndefined();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it.each([
|
|
188
|
+
[
|
|
189
|
+
"the format 2 envelope",
|
|
190
|
+
{
|
|
191
|
+
method: "a2ui",
|
|
192
|
+
version: "v0.9",
|
|
193
|
+
format: 2,
|
|
194
|
+
transport: "tool",
|
|
195
|
+
catalog: { id: "acme/app", version: "1" },
|
|
196
|
+
functions: {},
|
|
197
|
+
targets: [],
|
|
198
|
+
surfaces: {},
|
|
199
|
+
contextSent: false,
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
["format 2 in the extensions", withExtension("copilotkit_format", 2)],
|
|
203
|
+
["an unknown transport", withExtension("copilotkit_transport", "fence")],
|
|
204
|
+
["no capabilities", { ...createEnvelope(), "v1.0": undefined }],
|
|
205
|
+
["no metadata", { ...createEnvelope(), metadata: undefined }],
|
|
206
|
+
["no extensions", { ...createEnvelope(), metadata: {} }],
|
|
207
|
+
["no catalog id", withCapability("supportedCatalogIds", [])],
|
|
208
|
+
["an empty catalog id", withCapability("supportedCatalogIds", [""])],
|
|
209
|
+
[
|
|
210
|
+
"inline catalogs that are not a list",
|
|
211
|
+
withCapability("inlineCatalogs", {}),
|
|
212
|
+
],
|
|
213
|
+
[
|
|
214
|
+
"an inline catalog without an id",
|
|
215
|
+
withCatalogField("catalogId", undefined),
|
|
216
|
+
],
|
|
217
|
+
[
|
|
218
|
+
"an inline catalog of protocol 0.9",
|
|
219
|
+
withCatalogField("protocolVersion", "0.9"),
|
|
220
|
+
],
|
|
221
|
+
[
|
|
222
|
+
"a component without a props schema",
|
|
223
|
+
withCatalogField("components", { Card: { description: "x" } }),
|
|
224
|
+
],
|
|
225
|
+
[
|
|
226
|
+
"a component whose required list is not strings",
|
|
227
|
+
withCatalogField("components", {
|
|
228
|
+
Card: { props: { type: "object", required: 5 } },
|
|
229
|
+
}),
|
|
230
|
+
],
|
|
231
|
+
[
|
|
232
|
+
"an unknown slot kind",
|
|
233
|
+
withCatalogField("components", {
|
|
234
|
+
Card: { props: {}, slots: { body: "many" } },
|
|
235
|
+
}),
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
"an event without parameters",
|
|
239
|
+
withCatalogField("components", {
|
|
240
|
+
Card: { props: {}, events: { onClick: {} } },
|
|
241
|
+
}),
|
|
242
|
+
],
|
|
243
|
+
[
|
|
244
|
+
"a function without parameters",
|
|
245
|
+
withCatalogField("functions", { add: {} }),
|
|
246
|
+
],
|
|
247
|
+
[
|
|
248
|
+
"an inline catalog without functions",
|
|
249
|
+
withCatalogField("functions", undefined),
|
|
250
|
+
],
|
|
251
|
+
["instructions that are not text", withCatalogField("instructions", ["a"])],
|
|
252
|
+
[
|
|
253
|
+
"targets that are not a list",
|
|
254
|
+
withExtension("copilotkit_targets", { chat: "chat" }),
|
|
255
|
+
],
|
|
256
|
+
[
|
|
257
|
+
"a target with an unknown kind",
|
|
258
|
+
withExtension("copilotkit_targets", [{ id: "x", kind: "modal" }]),
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
"a surface without a target",
|
|
262
|
+
withExtension("copilotkit_surfaces", { cart: {} }),
|
|
263
|
+
],
|
|
264
|
+
[
|
|
265
|
+
"a surface data model that is not an object",
|
|
266
|
+
withExtension("copilotkit_surfaces", {
|
|
267
|
+
cart: { target: "chat", dataModel: ["a"] },
|
|
268
|
+
}),
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
"surface rejections that are not strings",
|
|
272
|
+
withExtension("copilotkit_surfaces", {
|
|
273
|
+
cart: { target: "chat", rejected: [1] },
|
|
274
|
+
}),
|
|
275
|
+
],
|
|
276
|
+
[
|
|
277
|
+
"more than 20 surface rejections",
|
|
278
|
+
withExtension("copilotkit_surfaces", {
|
|
279
|
+
cart: { target: "chat", rejected: Array(21).fill("operation 0: x") },
|
|
280
|
+
}),
|
|
281
|
+
],
|
|
282
|
+
[
|
|
283
|
+
"an unknown surface status",
|
|
284
|
+
withExtension("copilotkit_surfaces", {
|
|
285
|
+
cart: { target: "chat", status: "closed" },
|
|
286
|
+
}),
|
|
287
|
+
],
|
|
288
|
+
[
|
|
289
|
+
"a surface rejection longer than 500 characters",
|
|
290
|
+
withExtension("copilotkit_surfaces", {
|
|
291
|
+
cart: { target: "chat", rejected: ["x".repeat(501)] },
|
|
292
|
+
}),
|
|
293
|
+
],
|
|
294
|
+
[
|
|
295
|
+
"target components that are not a list",
|
|
296
|
+
withExtension("copilotkit_targets", [
|
|
297
|
+
{ id: "x", kind: "inline", components: "ProductGrid" },
|
|
298
|
+
]),
|
|
299
|
+
],
|
|
300
|
+
[
|
|
301
|
+
"a target component name that is empty",
|
|
302
|
+
withExtension("copilotkit_targets", [
|
|
303
|
+
{ id: "x", kind: "inline", components: ["ProductGrid", ""] },
|
|
304
|
+
]),
|
|
305
|
+
],
|
|
306
|
+
[
|
|
307
|
+
"more than 100 target component names",
|
|
308
|
+
withExtension("copilotkit_targets", [
|
|
309
|
+
{ id: "x", kind: "inline", components: Array(101).fill("ProductGrid") },
|
|
310
|
+
]),
|
|
311
|
+
],
|
|
312
|
+
["no contextSent flag", withExtension("copilotkit_contextSent", undefined)],
|
|
313
|
+
])("returns undefined for %s", (_case, ui) => {
|
|
314
|
+
expect(readUI(ui)).toBeUndefined();
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe("envelopeToManifest", () => {
|
|
319
|
+
it("returns the manifest, the transport, the context flag, and the instructions", () => {
|
|
320
|
+
expect(envelopeToManifest(createEnvelope())).toEqual({
|
|
321
|
+
catalogId: "acme/app@1",
|
|
322
|
+
catalogIds: ["acme/app@1"],
|
|
323
|
+
components: { ProductGrid: productGrid },
|
|
324
|
+
functions: { addToCart },
|
|
325
|
+
targets,
|
|
326
|
+
surfaces,
|
|
327
|
+
transport: "markdown",
|
|
328
|
+
contextSent: true,
|
|
329
|
+
instructions: "Write fences.",
|
|
330
|
+
hasComponents: true,
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("gives the action of the user event that started the run", () => {
|
|
335
|
+
const action = {
|
|
336
|
+
version: "v1.0" as const,
|
|
337
|
+
action: {
|
|
338
|
+
name: "buy",
|
|
339
|
+
surfaceId: "s1",
|
|
340
|
+
sourceComponentId: "button",
|
|
341
|
+
timestamp: "2026-10-20T10:00:00.000Z",
|
|
342
|
+
userMessage: "Buy now",
|
|
343
|
+
context: { sku: "a" },
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
const envelope = createEnvelope({
|
|
347
|
+
extensions: { copilotkit_action: action },
|
|
348
|
+
});
|
|
349
|
+
expect(readCopilotKitUIEnvelope({ copilotkit: { ui: envelope } })).toEqual(
|
|
350
|
+
envelope,
|
|
351
|
+
);
|
|
352
|
+
expect(envelopeToManifest(envelope).action).toEqual(action);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("reports no components when the client left them out", () => {
|
|
356
|
+
const manifest = envelopeToManifest(
|
|
357
|
+
createEnvelope({
|
|
358
|
+
catalog: { components: undefined, instructions: undefined },
|
|
359
|
+
extensions: { copilotkit_transport: "tool" },
|
|
360
|
+
}),
|
|
361
|
+
);
|
|
362
|
+
expect(manifest).toMatchObject({
|
|
363
|
+
components: {},
|
|
364
|
+
functions: { addToCart },
|
|
365
|
+
transport: "tool",
|
|
366
|
+
hasComponents: false,
|
|
367
|
+
});
|
|
368
|
+
expect(manifest.instructions).toBeUndefined();
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("lists every supported catalog id, the first one as the catalog of the run", () => {
|
|
372
|
+
const envelope: CopilotKitUIEnvelope = {
|
|
373
|
+
...createEnvelope(),
|
|
374
|
+
"v1.0": {
|
|
375
|
+
supportedCatalogIds: ["acme/app@1", "copilotkit/standard@1"],
|
|
376
|
+
inlineCatalogs: [createCatalog()],
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
expect(envelopeToManifest(envelope)).toMatchObject({
|
|
380
|
+
catalogId: "acme/app@1",
|
|
381
|
+
catalogIds: ["acme/app@1", "copilotkit/standard@1"],
|
|
382
|
+
components: { ProductGrid: productGrid },
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("uses only the inline catalog of the first supported catalog id", () => {
|
|
387
|
+
const envelope: CopilotKitUIEnvelope = {
|
|
388
|
+
...createEnvelope(),
|
|
389
|
+
"v1.0": {
|
|
390
|
+
supportedCatalogIds: ["acme/app@1"],
|
|
391
|
+
inlineCatalogs: [createCatalog({ catalogId: "other/catalog" })],
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
expect(envelopeToManifest(envelope)).toMatchObject({
|
|
395
|
+
catalogId: "acme/app@1",
|
|
396
|
+
components: {},
|
|
397
|
+
functions: {},
|
|
398
|
+
hasComponents: false,
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
describe("component parameter order", () => {
|
|
404
|
+
it("preserves ordering through the capability envelope", () => {
|
|
405
|
+
const component = {
|
|
406
|
+
...productGrid,
|
|
407
|
+
parameterOrder: ["children", "onSelect", "title"],
|
|
408
|
+
};
|
|
409
|
+
const envelope = createEnvelope({
|
|
410
|
+
catalog: { components: { ProductGrid: component } },
|
|
411
|
+
});
|
|
412
|
+
const parsed = readUI(JSON.parse(JSON.stringify(envelope)))!;
|
|
413
|
+
expect(
|
|
414
|
+
envelopeToManifest(parsed).components.ProductGrid.parameterOrder,
|
|
415
|
+
).toEqual(component.parameterOrder);
|
|
416
|
+
});
|
|
417
|
+
it.each(["title", ["title", "title"], ["unknown"], [42], ["__proto__"]])(
|
|
418
|
+
"rejects invalid metadata %j",
|
|
419
|
+
(parameterOrder) => {
|
|
420
|
+
const envelope = createEnvelope();
|
|
421
|
+
const catalog = envelope["v1.0"].inlineCatalogs![0];
|
|
422
|
+
const invalid = {
|
|
423
|
+
...catalog,
|
|
424
|
+
components: { ProductGrid: { ...productGrid, parameterOrder } },
|
|
425
|
+
};
|
|
426
|
+
expect(
|
|
427
|
+
readUI({
|
|
428
|
+
...envelope,
|
|
429
|
+
"v1.0": { ...envelope["v1.0"], inlineCatalogs: [invalid] },
|
|
430
|
+
}),
|
|
431
|
+
).toBeUndefined();
|
|
432
|
+
},
|
|
433
|
+
);
|
|
434
|
+
});
|