{"version":3,"file":"AIUIAction.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIAction.ts"],"sourcesContent":["import type { AIUIFormat } from './AIUIDocument';\n\n/**\n * What the person did in a reply's UI, such as tapping a place or submitting a form. It goes to\n * the agent as their next message: `label` is its text and `aiUIActionJSON(action)` what it\n * carries. The counterpart of `AIUIAction` in StreamChatAI.\n */\nexport type AIUIAction = {\n  /** The format of the UI it came from. */\n  format: AIUIFormat;\n  /** What the person did, in their words, such as \"Tell me more about St. Naum\". */\n  label: string;\n  /** The reply whose UI it came from. */\n  messageId: string;\n  /**\n   * What happened, such as an event of an app's component (`ask_about`) or OpenUI's\n   * `continue_conversation`.\n   */\n  name: string;\n  /** The details in the format's own shape, such as what was chosen and a form's values. */\n  payload: Record<string, unknown>;\n};\n\n/** The action as the JSON object an agent's backend reads. */\nexport type AIUIActionJSON = {\n  format: AIUIFormat;\n  message_id: string;\n  name: string;\n  payload: Record<string, unknown>;\n  v: 1;\n};\n\n/** The custom field of the person's message that holds the action. */\nexport const AI_UI_ACTION_CUSTOM_DATA_KEY = 'ai_ui_action';\n/**\n * The most an action may take as JSON. The agents router refuses a larger one, and its\n * refusal would lose the person's message with it.\n */\nexport const AI_UI_ACTION_MAX_BYTES = 8 * 1024;\n\n/**\n * The action as the JSON object an agent's backend reads, such as the `uiAction` of\n * `session.responses.create(action.label, { uiAction })`. The label is not in it: it is the\n * text of the person's message.\n */\nexport const aiUIActionJSON = (action: AIUIAction): AIUIActionJSON => ({\n  format: action.format,\n  message_id: action.messageId,\n  name: action.name,\n  payload: action.payload,\n  v: 1,\n});\n\n/**\n * Why the agent's backend would refuse the action, or `undefined` when it would take it: it\n * names its reply, its format and what happened, in at most `AI_UI_ACTION_MAX_BYTES` of JSON.\n */\nexport const aiUIActionProblem = (action: AIUIAction): string | undefined => {\n  if (!action.messageId) return 'an action names the reply it came from';\n  if (!action.format) return 'an action names the format of its UI';\n  if (!action.name) return 'an action names what happened';\n  let json: string;\n  try {\n    json = JSON.stringify(aiUIActionJSON(action));\n  } catch {\n    return 'an action carries only JSON';\n  }\n  const bytes = new TextEncoder().encode(json).length;\n  return bytes > AI_UI_ACTION_MAX_BYTES\n    ? `an action takes at most ${AI_UI_ACTION_MAX_BYTES} bytes as JSON, and this one takes ${bytes}`\n    : undefined;\n};\n"],"mappings":";;AAiCA,IAAa,+BAA+B;;;;;AAK5C,IAAa,yBAAyB,IAAI;;;;;;AAO1C,IAAa,kBAAkB,YAAwC;CACrE,QAAQ,OAAO;CACf,YAAY,OAAO;CACnB,MAAM,OAAO;CACb,SAAS,OAAO;CAChB,GAAG;AACL;;;;;AAMA,IAAa,qBAAqB,WAA2C;CAC3E,IAAI,CAAC,OAAO,WAAW,OAAO;CAC9B,IAAI,CAAC,OAAO,QAAQ,OAAO;CAC3B,IAAI,CAAC,OAAO,MAAM,OAAO;CACzB,IAAI;CACJ,IAAI;EACF,OAAO,KAAK,UAAU,eAAe,MAAM,CAAC;CAC9C,QAAQ;EACN,OAAO;CACT;CACA,MAAM,QAAQ,IAAI,YAAY,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC;CAC7C,OAAO,QAAA,OACH,2BAA2B,uBAAuB,qCAAqC,UACvF,KAAA;AACN"}