{"version":3,"file":"catalogDocument.mjs","names":[],"sources":["../../../../src/plugins/A2UI/catalogDocument.ts"],"sourcesContent":["// The app's components as A2UI knows them. Free of the A2UI packages, so the renderer and what\n// writes an agent's instructions, which may run on a server, both read them.\nimport type { AIUIComponent } from '../AIComponents/GenerativeUI/AIUIComponent';\nimport type {\n  AIUIComponentDefinition,\n  AIUIJSONSchema,\n} from '../AIComponents/GenerativeUI/AIUIComponentDefinition';\nimport { aiUISentence, aiUITypeName } from '../AIComponents/GenerativeUI/AIUIHelpers';\nimport { A2UI_VERSION } from './A2UIStream';\n\n/**\n * The id of the catalog with the app's components, unless the renderer is given another. The\n * agent writes it in every surface it makes, so it names no domain: a message's link enrichment\n * would show a domain's preview under every reply with A2UI.\n */\nexport const A2UI_DEFAULT_CATALOG_ID = 'stream-app-catalog';\n\n/**\n * The default catalog id before 14.17, which A2UI already in a conversation's history names. A\n * renderer with the default id shows its surfaces too.\n */\nexport const A2UI_LEGACY_CATALOG_ID = 'getstream.io:app-catalog';\n\nconst definitionOf = (component: AIUIComponent | AIUIComponentDefinition) =>\n  'definition' in component ? component.definition : component;\n\n/** The A2UI type of one of the app's components, named as the basic catalog names its own. */\nexport const a2uiTypeName = aiUITypeName;\n\n// A parameter as the agent may write it: the value its schema describes, bound to the data\n// model, or worked out by a function.\nconst dynamic = (schema: AIUIJSONSchema) => ({\n  anyOf: [\n    schema,\n    { $ref: 'common_types.json#/$defs/DataBinding' },\n    { $ref: 'common_types.json#/$defs/FunctionCall' },\n  ],\n});\n\n/**\n * The app's components as an A2UI catalog document: each under its A2UI type, its parameters\n * as its properties and what its events do in its description, since it reports them itself\n * rather than through an A2UI action the agent writes. The same as StreamChatA2UI's\n * `PortableComponent`, so an agent writes for both alike. With `bindings`, as the renderer reads\n * them, a parameter may also be bound to the data model or worked out by a function; without,\n * as an agent is told them, it is the value its schema describes.\n */\nexport const a2uiAppCatalogDocument = (\n  catalogId: string,\n  components: (AIUIComponent | AIUIComponentDefinition)[],\n  { bindings = true }: { bindings?: boolean } = {},\n) => ({\n  catalogId,\n  components: Object.fromEntries(\n    components.map(definitionOf).map((definition) => {\n      const type = a2uiTypeName(definition.name);\n      const notes = [\n        definition.description,\n        ...definition.events.map((e) => e.description),\n      ]\n        .filter((note) => note.trim() !== '')\n        .map(aiUISentence);\n      return [\n        type,\n        {\n          ...(notes.length ? { description: notes.join(' ') } : {}),\n          properties: {\n            component: { const: type },\n            ...Object.fromEntries(\n              definition.parameters.map(({ description, name, type: schema }) => [\n                name,\n                (bindings ? dynamic : (plain: AIUIJSONSchema) => plain)(\n                  description === undefined ? schema : { ...schema, description },\n                ),\n              ]),\n            ),\n          },\n          // Sorted, as StreamChatA2UI writes it, so both platforms describe it alike.\n          required: [\n            'component',\n            ...definition.parameters\n              .filter(({ isRequired }) => isRequired)\n              .map(({ name }) => name)\n              .sort(),\n          ],\n          type: 'object',\n        },\n      ];\n    }),\n  ),\n  protocolVersion: A2UI_VERSION,\n});\n"],"mappings":";;;;;;;;AAeA,IAAa,0BAA0B;;;;;AAMvC,IAAa,yBAAyB;AAEtC,IAAM,gBAAgB,cACpB,gBAAgB,YAAY,UAAU,aAAa;;AAGrD,IAAa,eAAe;AAI5B,IAAM,WAAW,YAA4B,EAC3C,OAAO;CACL;CACA,EAAE,MAAM,uCAAuC;CAC/C,EAAE,MAAM,wCAAwC;AAClD,EACF;;;;;;;;;AAUA,IAAa,0BACX,WACA,YACA,EAAE,WAAW,SAAiC,CAAC,OAC3C;CACJ;CACA,YAAY,OAAO,YACjB,WAAW,IAAI,YAAY,CAAC,CAAC,KAAK,eAAe;EAC/C,MAAM,OAAO,aAAa,WAAW,IAAI;EACzC,MAAM,QAAQ,CACZ,WAAW,aACX,GAAG,WAAW,OAAO,KAAK,MAAM,EAAE,WAAW,CAC/C,CAAC,CACE,QAAQ,SAAS,KAAK,KAAK,MAAM,EAAE,CAAC,CACpC,IAAI,YAAY;EACnB,OAAO,CACL,MACA;GACE,GAAI,MAAM,SAAS,EAAE,aAAa,MAAM,KAAK,GAAG,EAAE,IAAI,CAAC;GACvD,YAAY;IACV,WAAW,EAAE,OAAO,KAAK;IACzB,GAAG,OAAO,YACR,WAAW,WAAW,KAAK,EAAE,aAAa,MAAM,MAAM,aAAa,CACjE,OACC,WAAW,WAAW,UAA0B,MAAA,CAC/C,gBAAgB,KAAA,IAAY,SAAS;KAAE,GAAG;KAAQ;IAAY,CAChE,CACF,CAAC,CACH;GACF;GAEA,UAAU,CACR,aACA,GAAG,WAAW,WACX,QAAQ,EAAE,iBAAiB,UAAU,CAAC,CACtC,KAAK,EAAE,WAAW,IAAI,CAAC,CACvB,KAAK,CACV;GACA,MAAM;EACR,CACF;CACF,CAAC,CACH;CACA,iBAAA;AACF"}