UNPKG

@copilotkit/a2ui-renderer

Version:

A2UI Renderer for CopilotKit - render A2UI surfaces in React applications

1 lines 6.24 kB
{"version":3,"file":"catalog-utils.cjs","names":["basicCatalog"],"sources":["../../src/react-renderer/catalog-utils.ts"],"sourcesContent":["import { basicCatalog } from \"./a2ui-react\";\nimport type { ComponentApi, Catalog } from \"@a2ui/web_core/v0_9\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\n\nconst BASIC_CATALOG_ID =\n \"https://a2ui.org/specification/v0_9/basic_catalog.json\";\n\n/**\n * Context description used to identify the A2UI component schema in RunAgentInput.context.\n * Must match the constant in @ag-ui/a2ui-middleware so the middleware can overwrite\n * a frontend-provided schema with a server-side one.\n */\nexport const A2UI_SCHEMA_CONTEXT_DESCRIPTION =\n \"A2UI Component Schema — available components for generating UI surfaces. Use these component names and properties when creating A2UI operations.\";\n\n/**\n * Check whether a catalog is a superset of the basic catalog\n * (i.e., it contains all basic components by name).\n */\nexport function extendsBasicCatalog(catalog: Catalog<ComponentApi>): boolean {\n for (const name of basicCatalog.components.keys()) {\n if (!catalog.components.has(name)) {\n return false;\n }\n }\n return true;\n}\n\n/**\n * Return the names of components in a catalog that are not in the basic catalog.\n */\nexport function getCustomComponentNames(\n catalog: Catalog<ComponentApi>,\n): string[] {\n const custom: string[] = [];\n for (const name of catalog.components.keys()) {\n if (!basicCatalog.components.has(name)) {\n custom.push(name);\n }\n }\n return custom;\n}\n\n/**\n * Build a context string describing the available A2UI catalog and custom components.\n * Custom components (those not in the basic catalog) are described using their\n * JSON Schema representation, matching the canonical A2UI catalog format.\n */\nexport function buildCatalogContextValue(\n catalog?: Catalog<ComponentApi>,\n): string {\n const resolved = catalog ?? basicCatalog;\n const lines: string[] = [];\n lines.push(\"Available A2UI catalog:\");\n\n if (resolved.id === BASIC_CATALOG_ID) {\n lines.push(`- ${resolved.id} (basic catalog)`);\n return lines.join(\"\\n\");\n }\n\n const isSuperset = extendsBasicCatalog(resolved);\n const customNames = getCustomComponentNames(resolved);\n\n lines.push(`- ${resolved.id}`);\n if (isSuperset) {\n lines.push(\n \" Extends the basic catalog with all standard components plus:\",\n );\n } else {\n lines.push(\" Custom catalog (does NOT include all basic components).\");\n lines.push(\" Custom components:\");\n }\n\n for (const name of customNames) {\n const comp = resolved.components.get(name);\n if (!comp) continue;\n const jsonSchema = zodToJsonSchema(comp.schema);\n lines.push(` - ${name}:`);\n lines.push(\n ` ${JSON.stringify(jsonSchema, null, 2).split(\"\\n\").join(\"\\n \")}`,\n );\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * A2UI v0.9 inline catalog format — matches the structure defined by the\n * A2UI specification (basic_catalog.json). Each component is keyed by\n * name and uses `allOf` to compose ComponentCommon with component-specific\n * properties so the schema mirrors the flat wire format the LLM must produce.\n */\nexport interface InlineCatalogSchema {\n catalogId: string;\n components: Record<string, Record<string, unknown>>;\n}\n\n/**\n * Extract component schemas from a catalog in the A2UI v0.9 inline catalog\n * format. This mirrors `generateInlineCatalog` from `@a2ui/web_core` so\n * the schema the LLM sees matches the spec and the flat wire format:\n *\n * { \"Column\": { \"allOf\": [\n * { \"$ref\": \"common_types.json#/$defs/ComponentCommon\" },\n * { \"properties\": { \"component\": {\"const\":\"Column\"}, \"gap\": ..., \"children\": ... },\n * \"required\": [\"component\"] }\n * ]}}\n *\n * When sent via `useAgentContext` with `A2UI_SCHEMA_CONTEXT_DESCRIPTION`,\n * the middleware can optionally overwrite it with a server-side schema.\n */\nexport function extractCatalogComponentSchemas(\n catalog?: Catalog<ComponentApi>,\n): InlineCatalogSchema {\n const resolved = catalog ?? basicCatalog;\n const components: Record<string, Record<string, unknown>> = {};\n\n for (const [name, comp] of resolved.components) {\n const zodSchema = zodToJsonSchema(comp.schema, {\n target: \"jsonSchema2019-09\",\n }) as { properties?: Record<string, unknown>; required?: string[] };\n\n components[name] = {\n allOf: [\n { $ref: \"common_types.json#/$defs/ComponentCommon\" },\n {\n properties: {\n component: { const: name },\n ...(zodSchema.properties ?? {}),\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