{"version":3,"file":"AIUIComponentDefinition.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIComponentDefinition.ts"],"sourcesContent":["import { isRecord } from './AIUIHelpers';\n\n/** A JSON Schema, as a component's parameters are described to the agent. */\nexport type AIUIJSONSchema = Record<string, unknown>;\n\n/** Something the agent fills in for a component. The counterpart of `AIUIParameter`. */\nexport type AIUIParameter = {\n  /** Whether the agent must fill it in. */\n  isRequired: boolean;\n  /** The name the agent fills it in by, such as `title`. */\n  name: string;\n  /** What it holds, as a JSON Schema, such as `AIUIParameterType.string`. */\n  type: AIUIJSONSchema;\n  /** What it is, for the agent. */\n  description?: string;\n};\n\n/** Something a component reports when the person uses it, such as tapping a place. */\nexport type AIUIComponentEvent = {\n  /** When it happens, for the agent. */\n  description: string;\n  /** The name it is reported by, such as `ask_about`. */\n  name: string;\n  /** What it carries, in order. */\n  parameters: AIUIParameter[];\n};\n\n/**\n * An app's component as the agent knows it: its name, what it shows, what the agent fills in\n * and what it reports. It is portable JSON, the same on every platform, so one description\n * (such as a `UIComponents.json` shared with the iOS and Android apps) serves them all. The\n * counterpart of `AIUIComponentDefinition` in StreamChatAI.\n */\nexport type AIUIComponentDefinition = {\n  /** What it shows and when to use it, for the agent. */\n  description: string;\n  /** What it reports when the person uses it. */\n  events: AIUIComponentEvent[];\n  /** The name the agent places it by, such as `place_card`. */\n  name: string;\n  /**\n   * What the agent fills in, in order: a format such as OpenUI Lang passes arguments by\n   * position.\n   */\n  parameters: AIUIParameter[];\n};\n\n/** The types a parameter commonly has, as JSON Schemas. */\nexport const AIUIParameterType = {\n  /** A list of values of one type. */\n  array: (items: AIUIJSONSchema): AIUIJSONSchema => ({ items, type: 'array' }),\n  boolean: { type: 'boolean' } as AIUIJSONSchema,\n  /** A day, such as `2026-11-14`. */\n  date: { format: 'date', type: 'string' } as AIUIJSONSchema,\n  /** A moment, such as `2026-11-14T09:30:00Z`. */\n  dateTime: { format: 'date-time', type: 'string' } as AIUIJSONSchema,\n  /** One of a fixed set of strings. */\n  enumeration: (values: string[]): AIUIJSONSchema => ({ enum: values, type: 'string' }),\n  integer: { type: 'integer' } as AIUIJSONSchema,\n  number: { type: 'number' } as AIUIJSONSchema,\n  /** An object with named fields, in order. */\n  object: (fields: AIUIParameter[]): AIUIJSONSchema => aiUIObjectSchema(fields),\n  string: { type: 'string' } as AIUIJSONSchema,\n  /** A link, such as an image's. */\n  url: { format: 'uri', type: 'string' } as AIUIJSONSchema,\n};\n\n/** A parameter the agent must fill in. */\nexport const requiredAIUIParameter = (\n  name: string,\n  type: AIUIJSONSchema,\n  description?: string,\n): AIUIParameter => ({ description, isRequired: true, name, type });\n\n/** A parameter the agent may leave out. */\nexport const optionalAIUIParameter = (\n  name: string,\n  type: AIUIJSONSchema,\n  description?: string,\n): AIUIParameter => ({ description, isRequired: false, name, type });\n\n// The key that keeps the order of an object's fields, which JSON objects don't have.\nconst ORDER_KEY = 'x-order';\n\nconst strings = (value: unknown) =>\n  Array.isArray(value)\n    ? value.filter((item): item is string => typeof item === 'string')\n    : [];\n\n/** Parameters as a JSON Schema object, the way a tool's are described, keeping their order. */\nexport const aiUIObjectSchema = (parameters: AIUIParameter[]): AIUIJSONSchema => ({\n  [ORDER_KEY]: parameters.map(({ name }) => name),\n  properties: Object.fromEntries(\n    parameters.map(({ description, name, type }) => [\n      name,\n      description === undefined ? type : { ...type, description },\n    ]),\n  ),\n  required: parameters.filter(({ isRequired }) => isRequired).map(({ name }) => name),\n  type: 'object',\n});\n\n/** The fields of a JSON Schema object, in the order it keeps, then by name. */\nexport const aiUIParametersOf = (schema: unknown): AIUIParameter[] => {\n  if (!isRecord(schema)) return [];\n  const properties = isRecord(schema.properties) ? schema.properties : {};\n  const required = new Set(strings(schema.required));\n  const ordered = strings(schema[ORDER_KEY]).filter((name) => name in properties);\n  const rest = Object.keys(properties)\n    .filter((name) => !ordered.includes(name))\n    .sort();\n  return [...ordered, ...rest].flatMap((name) => {\n    const field = properties[name];\n    if (!isRecord(field)) return [];\n    const { description, ...type } = field;\n    return [\n      {\n        description: typeof description === 'string' ? description : undefined,\n        isRequired: required.has(name),\n        name,\n        type,\n      },\n    ];\n  });\n};\n\n/**\n * Reads a component's portable JSON, such as one entry of a `UIComponents.json`, or returns\n * `undefined` for one without a name.\n */\nexport const parseAIUIComponentDefinition = (\n  json: unknown,\n): AIUIComponentDefinition | undefined => {\n  if (!isRecord(json) || typeof json.name !== 'string' || json.name === '') {\n    return undefined;\n  }\n  const events = Array.isArray(json.events) ? json.events : [];\n  return {\n    description: typeof json.description === 'string' ? json.description : '',\n    events: events.flatMap((event) =>\n      isRecord(event) && typeof event.name === 'string'\n        ? [\n            {\n              description: typeof event.description === 'string' ? event.description : '',\n              name: event.name,\n              parameters: aiUIParametersOf(event.parameters),\n            },\n          ]\n        : [],\n    ),\n    name: json.name,\n    parameters: aiUIParametersOf(json.parameters),\n  };\n};\n\n/** A component's portable JSON, as `parseAIUIComponentDefinition` reads it back. */\nexport const aiUIComponentDefinitionJSON = (definition: AIUIComponentDefinition) => ({\n  description: definition.description,\n  events: definition.events.map(({ description, name, parameters }) => ({\n    description,\n    name,\n    ...(parameters.length ? { parameters: aiUIObjectSchema(parameters) } : {}),\n  })),\n  name: definition.name,\n  parameters: aiUIObjectSchema(definition.parameters),\n});\n"],"mappings":";;;AAgDA,IAAa,oBAAoB;;CAE/B,QAAQ,WAA2C;EAAE;EAAO,MAAM;CAAQ;CAC1E,SAAS,EAAE,MAAM,UAAU;;CAE3B,MAAM;EAAE,QAAQ;EAAQ,MAAM;CAAS;;CAEvC,UAAU;EAAE,QAAQ;EAAa,MAAM;CAAS;;CAEhD,cAAc,YAAsC;EAAE,MAAM;EAAQ,MAAM;CAAS;CACnF,SAAS,EAAE,MAAM,UAAU;CAC3B,QAAQ,EAAE,MAAM,SAAS;;CAEzB,SAAS,WAA4C,iBAAiB,MAAM;CAC5E,QAAQ,EAAE,MAAM,SAAS;;CAEzB,KAAK;EAAE,QAAQ;EAAO,MA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