{"version":3,"file":"A2UIStream.mjs","names":[],"sources":["../../../../src/plugins/A2UI/A2UIStream.ts"],"sourcesContent":["import type { AIUIDocument } from '../AIComponents/GenerativeUI/AIUIDocument';\nimport { isRecord } from '../AIComponents/GenerativeUI/AIUIHelpers';\n\n/** The version of A2UI this renderer reads. */\nexport const A2UI_VERSION = '1.0';\n\n// The spellings of 1.0 an agent may write, such as from instructions written before its\n// release, or by copying the fence's `ui:a2ui@1.0` in another form.\nconst VERSION_SPELLINGS = new Set(['1', '1.0', '1.0.0', 'v1', 'v1.0']);\n\n/** Whether a reply's A2UI of `version`, or of no version, is one this renderer reads. */\nexport const supportsA2UIVersion = (version: string | undefined) =>\n  version === undefined || VERSION_SPELLINGS.has(version);\n\n/**\n * The complete A2UI messages in `text`, as written: every top-level JSON object, in order.\n * One object per line, a pretty-printed one or a JSON array of them all read the same, and\n * words around them are skipped. An object still being written is left out until it closes, so\n * a reply's UI shows each message as it completes.\n */\nexport const a2uiMessagesIn = (text: string): string[] => {\n  const messages: string[] = [];\n  let depth = 0;\n  let start = 0;\n  let inString = false;\n  let escaped = false;\n  for (let index = 0; index < text.length; index++) {\n    const char = text[index];\n    if (inString) {\n      if (escaped) escaped = false;\n      else if (char === '\\\\') escaped = true;\n      else if (char === '\"') inString = false;\n      continue;\n    }\n    if (char === '\"' && depth > 0) {\n      inString = true;\n    } else if (char === '{') {\n      if (depth === 0) start = index;\n      depth++;\n    } else if (char === '}' && depth > 0) {\n      depth--;\n      if (depth === 0) messages.push(text.slice(start, index + 1));\n    }\n  }\n  return messages;\n};\n\n/**\n * The messages of a document as text: as the agent wrote them, or written out when a backend\n * sent them as JSON. `undefined` for content that holds no messages.\n */\nexport const a2uiSourceOf = (document: AIUIDocument): string | undefined => {\n  const { content } = document;\n  if (typeof content === 'string') return content;\n  if (typeof content === 'object' && content !== null) {\n    try {\n      return JSON.stringify(content);\n    } catch {\n      return undefined;\n    }\n  }\n  return undefined;\n};\n\n/**\n * A message as the processor reads it: 1.0 in its own spelling, and a surface the agent made\n * without naming a catalog made with `catalogId`, the catalog with the app's components.\n */\nexport const normalizeA2UIMessage = (\n  message: Record<string, unknown>,\n  catalogId: string,\n): Record<string, unknown> => {\n  let normalized = message;\n  if (typeof message.version === 'string' && VERSION_SPELLINGS.has(message.version)) {\n    normalized = { ...normalized, version: A2UI_VERSION };\n  }\n  const { createSurface } = normalized;\n  if (isRecord(createSurface) && createSurface.catalogId === undefined) {\n    normalized = { ...normalized, createSurface: { ...createSurface, catalogId } };\n  }\n  return normalized;\n};\n"],"mappings":";;;AAIA,IAAa,eAAe;AAI5B,IAAM,oCAAoB,IAAI,IAAI;CAAC;CAAK;CAAO;CAAS;CAAM;AAAM,CAAC;;AAGrE,IAAa,uBAAuB,YAClC,YAAY,KAAA,KAAa,kBAAkB,IAAI,OAAO;;;;;;;AAQxD,IAAa,kBAAkB,SAA2B;CACxD,MAAM,WAAqB,CAAC;CAC5B,IAAI,QAAQ;CACZ,IAAI,QAAQ;CACZ,IAAI,WAAW;CACf,IAAI,UAAU;CACd,KAAK,IAAI,QAAQ,GAAG,QAAQ,KAAK,QAAQ,SAAS;EAChD,MAAM,OAAO,KAAK;EAClB,IAAI,UAAU;GACZ,IAAI,SAAS,UAAU;QAClB,IAAI,SAAS,MAAM,UAAU;QAC7B,IAAI,SAAS,MAAK,WAAW;GAClC;EACF;EACA,IAAI,SAAS,QAAO,QAAQ,GAC1B,WAAW;OACN,IAAI,SAAS,KAAK;GACvB,IAAI,UAAU,GAAG,QAAQ;GACzB;EACF,OAAO,IAAI,SAAS,OAAO,QAAQ,GAAG;GACpC;GACA,IAAI,UAAU,GAAG,SAAS,KAAK,KAAK,MAAM,OAAO,QAAQ,CAAC,CAAC;EAC7D;CACF;CACA,OAAO;AACT;;;;;AAMA,IAAa,gBAAgB,aAA+C;CAC1E,MAAM,EAAE,YAAY;CACpB,IAAI,OAAO,YAAY,UAAU,OAAO;CACxC,IAAI,OAAO,YAAY,YAAY,YAAY,MAC7C,IAA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