{"version":3,"file":"AIUIDocument.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUIDocument.ts"],"sourcesContent":["import isEqual from 'react-fast-compare';\nimport { isRecord } from './AIUIHelpers';\n\n/**\n * The format a reply's UI is written in, such as `a2ui` or `openui`. Formats are an open set:\n * a renderer says which one it reads.\n */\nexport type AIUIFormat = 'a2ui' | 'openui' | (string & {});\n\n/**\n * The UI an agent wrote for a reply, which shows in place of the reply's text. The counterpart\n * of `AIUIDocument` in StreamChatAI.\n */\nexport type AIUIDocument = {\n  /** The document in its format's own shape: a program's text, or JSON. */\n  content: unknown;\n  /** The format it is written in. */\n  format: AIUIFormat;\n  /** Whether the agent is still writing it. */\n  isStreaming: boolean;\n  /** The format's version, when the agent names one. */\n  version?: string;\n};\n\n/**\n * A reply's `ai_ui` field as the agents router writes it. An app that reads it from its Stream\n * messages, under their `custom`, types it there with `ai_ui?: AIUIDocumentJSON` in its\n * `CustomMessageData`.\n */\nexport type AIUIDocumentJSON = {\n  /** The document in its format's own shape: usually text, such as A2UI's JSON lines. */\n  content: unknown;\n  format: AIUIFormat;\n  /** `streaming` while the agent writes it, then `complete`. */\n  status?: 'streaming' | 'complete' | (string & {});\n  /** The version of the field's own layout; a newer one than this SDK reads shows the text. */\n  v?: number;\n  version?: string;\n};\n\n/** The custom field of a message that holds its document. */\nexport const AI_UI_CUSTOM_DATA_KEY = 'ai_ui';\n/** The newest version of the `ai_ui` field this SDK reads. */\nexport const AI_UI_SUPPORTED_VERSION = 1;\n\n/**\n * Reads the document of a message's `ai_ui` field, or returns `undefined` when it carries none\n * this SDK can read: no format, no content, or a field newer than this SDK, which can lay its\n * documents out differently. The reply then shows its text.\n */\nexport const parseAIUIDocument = (field: unknown): AIUIDocument | undefined => {\n  if (!isRecord(field)) return undefined;\n  const { content, format, status, v, version } = field;\n  if (typeof format !== 'string' || format === '') return undefined;\n  if (content === undefined || content === null) return undefined;\n  if (typeof v === 'number' && v > AI_UI_SUPPORTED_VERSION) return undefined;\n  return {\n    content,\n    format,\n    isStreaming: status === 'streaming',\n    ...(typeof version === 'string' && version !== '' ? { version } : {}),\n  };\n};\n\n/** The content of a document when it is text, such as an OpenUI program or A2UI's JSON lines. */\nexport const aiUIDocumentText = (document: AIUIDocument) =>\n  typeof document.content === 'string' ? document.content : undefined;\n\n/**\n * Whether two documents are the same, content included. A message is a new object on every\n * update while a reply streams, so a document is compared by what it holds.\n */\nexport const isSameAIUIDocument = (\n  a: AIUIDocument | undefined,\n  b: AIUIDocument | undefined,\n) =>\n  a === b ||\n  (!!a &&\n    !!b &&\n    a.format === b.format &&\n    a.version === b.version &&\n    a.isStreaming === b.isStreaming &&\n    isEqual(a.content, b.content));\n"],"mappings":";;;;AAyCA,IAAa,wBAAwB;;AAErC,IAAa,0BAA0B;;;;;;AAOvC,IAAa,qBAAqB,UAA6C;CAC7E,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO,KAAA;CAC7B,MAAM,EAAE,SAAS,QAAQ,QAAQ,GAAG,YAAY;CAChD,IAAI,OAAO,WAAW,YAAY,WAAW,IAAI,OAAO,KAAA;CACxD,IAAI,YAAY,KAAA,KAAa,YAAY,MAAM,OAAO,KAAA;CACtD,IAAI,OAAO,MAAM,YAAY,IAAA,GAA6B,OAAO,KAAA;CACjE,OAAO;EACL;EACA;EACA,aAAa,WAAW;EACxB,GAAI,OAAO,YAAY,YAAY,YAAY,KAAK,EAAE,QAAQ,IAAI,CAAC;CACrE;AACF;;AAGA,IAAa,oBAAoB,aAC/B,OAAO,SAAS,YAAY,WAAW,SAAS,UAAU,KAAA;;;;;AAM5D,IAAa,sBACX,GACA,MAEA,MAAM,KACL,CAAC,CAAC,KACD,CAAC,CAAC,KACF,EAAE,WAAW,EAAE,UACf,EAAE,YAAY,EAAE,WAChB,EAAE,gBAAgB,EAAE,eACpB,UAAQ,EAAE,SAAS,EAAE,OAAO"}