UNPKG

@copilotkit/a2ui-renderer

Version:

A2UI Renderer for CopilotKit - render A2UI surfaces in React applications

1 lines 11.1 kB
{"version":3,"file":"create-catalog.mjs","names":[],"sources":["../../src/web-components/create-catalog.ts"],"sourcesContent":["import type { z } from \"zod\";\nimport type { ZodObject, ZodRawShape, ZodTypeAny } from \"zod\";\nimport { Catalog } from \"@a2ui/web_core/v0_9\";\nimport type { ComponentApi } from \"@a2ui/web_core/v0_9\";\nimport { zodToJsonSchema } from \"zod-to-json-schema\";\nimport { basicCatalog } from \"./catalog/basic\";\nimport { createLitComponent } from \"./adapter\";\nimport type {\n CatalogComponentDefinition,\n CatalogDefinitions,\n CatalogRenderers,\n ComponentRenderer,\n LitComponentImplementation,\n RendererProps,\n} from \"./types\";\n\nconst BASIC_CATALOG_ID =\n \"https://a2ui.org/specification/v0_9/basic_catalog.json\";\n\ntype CatalogContextComponent = {\n schema: unknown;\n};\n\ntype CatalogContextValue = {\n id: string;\n components: ReadonlyMap<string, CatalogContextComponent>;\n};\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\nexport type {\n CatalogComponentDefinition,\n CatalogDefinitions,\n CatalogRenderers,\n ComponentRenderer,\n RendererProps,\n} from \"./types\";\n\nexport function createCatalog<D extends CatalogDefinitions>(\n definitions: D,\n renderers: CatalogRenderers<D>,\n options?: {\n catalogId?: string;\n includeBasicCatalog?: boolean;\n },\n): Catalog<LitComponentImplementation> {\n const catalogId = options?.catalogId ?? \"copilotkit://custom-catalog\";\n const customComponents: LitComponentImplementation[] = [];\n\n for (const [name, def] of Object.entries(definitions)) {\n const api: ComponentApi = {\n name,\n schema: def.props,\n };\n const renderer = (renderers as Record<string, ComponentRenderer<any>>)[\n name\n ];\n if (renderer === undefined) {\n throw new Error(`Missing renderer for component \"${name}\"`);\n }\n customComponents.push(\n createLitComponent(api, ({ props, buildChild, context }) =>\n renderer({\n props,\n children: buildChild,\n dispatch: (action: unknown) => context.dispatchAction(action),\n }),\n ),\n );\n }\n\n const components =\n options?.includeBasicCatalog === true\n ? [...Array.from(basicCatalog.components.values()), ...customComponents]\n : customComponents;\n const functions =\n options?.includeBasicCatalog === true\n ? Array.from(basicCatalog.functions.values())\n : [];\n\n return new Catalog<LitComponentImplementation>(\n catalogId,\n components,\n functions,\n );\n}\n\nexport function extractSchema(definitions: CatalogDefinitions): Array<{\n name: string;\n description?: string;\n props?: Record<string, unknown>;\n}> {\n return Object.entries(definitions).map(([name, def]) => ({\n name,\n description: def.description,\n props: zodSchemaToSimpleObject(def.props),\n }));\n}\n\nfunction zodSchemaToSimpleObject(\n schema: ZodObject<any>,\n): Record<string, unknown> {\n const shape = schema.shape;\n const properties: Record<string, { type: string; description?: string }> = {};\n for (const [key, value] of Object.entries(shape)) {\n const zodValue = value as any;\n properties[key] = {\n type: zodValue._def?.typeName ?? \"unknown\",\n ...(zodValue.description ? { description: zodValue.description } : {}),\n };\n }\n return { type: \"object\", properties };\n}\n\nexport interface A2UIComponentDefinition<T extends ZodRawShape = ZodRawShape> {\n props: ZodObject<T>;\n description?: string;\n render: (props: RendererProps<z.infer<ZodObject<T>>>) => unknown;\n}\n\nexport type A2UIComponentMap = Record<string, A2UIComponentDefinition<any>>;\n\nexport function createA2UICatalog(\n components: A2UIComponentMap,\n options?: {\n catalogId?: string;\n includeBasicCatalog?: boolean;\n },\n): Catalog<LitComponentImplementation> {\n const definitions: CatalogDefinitions = {};\n const renderers: Record<string, ComponentRenderer<any>> = {};\n\n for (const [name, def] of Object.entries(components)) {\n definitions[name] = { props: def.props, description: def.description };\n renderers[name] = def.render as ComponentRenderer<any>;\n }\n\n return createCatalog(definitions, renderers as any, options);\n}\n\nexport function extractA2UISchema(components: A2UIComponentMap): Array<{\n name: string;\n description?: string;\n props?: Record<string, unknown>;\n}> {\n const definitions: CatalogDefinitions = {};\n for (const [name, def] of Object.entries(components)) {\n definitions[name] = { props: def.props, description: def.description };\n }\n return extractSchema(definitions);\n}\n\nfunction isCatalogContextValue(value: unknown): value is CatalogContextValue {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"id\" in value &&\n typeof value.id === \"string\" &&\n \"components\" in value &&\n value.components instanceof Map\n );\n}\n\nfunction resolveCatalog(catalog?: unknown): CatalogContextValue {\n return isCatalogContextValue(catalog) ? catalog : basicCatalog;\n}\n\nfunction toJsonSchema(\n schema: unknown,\n options?: Parameters<typeof zodToJsonSchema>[1],\n): ReturnType<typeof zodToJsonSchema> {\n return zodToJsonSchema(schema as ZodTypeAny, options);\n}\n\nfunction extendsBasicCatalog(catalog: CatalogContextValue): 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\nfunction getCustomComponentNames(catalog: CatalogContextValue): 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\nexport function buildCatalogContextValue(catalog?: unknown): string {\n const resolved = resolveCatalog(catalog);\n const lines: string[] = [\"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 component = resolved.components.get(name);\n if (!component) continue;\n const jsonSchema = toJsonSchema(component.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\nexport interface InlineCatalogSchema {\n catalogId: string;\n components: Record<string, Record<string, unknown>>;\n}\n\nexport function extractCatalogComponentSchemas(\n catalog?: unknown,\n): InlineCatalogSchema {\n const resolved = resolveCatalog(catalog);\n const components: Record<string, Record<string, unknown>> = {};\n for (const [name, comp] of resolved.components) {\n const zodSchema = toJsonSchema(comp.schema, {\n target: \"jsonSchema2019-09\",\n }) as { properties?: Record<string, unknown>; required?: string[] };\n components[name] = {\n allOf: [\n { $ref: \"common_types.json#/$defs/ComponentCommon\" },\n {\n properties: {\n component: { const: name },\n ...zodSchema.properties,\n },\n required: [\"component\", ...(zodSchema.required ?? [])],\n },\n ],\n };\n }\n return { catalogId: resolved.id, components };\n}\n"],"mappings":";;;;;;AAgBA,MAAM,mBACJ;;;;;;AAgBF,MAAa,kCACX;AAUF,SAAgB,cACd,aACA,WACA,SAIqC;CACrC,MAAM,YAAY,SAAS,aAAa;CACxC,MAAM,mBAAiD,EAAE;AAEzD,MAAK,MAAM,CAAC,MAAM,QAAQ,OAAO,QAAQ,YAAY,EAAE;EACrD,MAAM,MAAoB;GACxB;GACA,QAAQ,IAAI;GACb;EACD,MAAM,WAAY,UAChB;AAEF,MAAI,aAAa,OACf,OAAM,IAAI,MAAM,mCAAmC,KAAK,GAAG;AAE7D,mBAAiB,KACf,mBAAmB,MAAM,EAAE,OAAO,YAAY,cAC5C,SAAS;GACP;GACA,UAAU;GACV,WAAW,WAAoB,QAAQ,eAAe,OAAO;GAC9D,CAAC,CACH,CACF;;AAYH,QAAO,IAAI,QACT,WATA,SAAS,wBAAwB,OAC7B,CAAC,GAAG,MAAM,KAAK,aAAa,WAAW,QAAQ,CAAC,EAAE,GAAG,iBAAiB,GACtE,kBAEJ,SAAS,wBAAwB,OAC7B,MAAM,KAAK,aAAa,UAAU,QAAQ,CAAC,GAC3C,EAAE,CAMP;;AAGH,SAAgB,cAAc,aAI3B;AACD,QAAO,OAAO,Q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