{"version":3,"file":"fromStreamMessage.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/Conversation/fromStreamMessage.ts"],"sourcesContent":["import type { Attachment, LocalMessage, MessageResponse } from 'stream-chat';\n\nimport { AI_UI_CUSTOM_DATA_KEY, parseAIUIDocument } from '../GenerativeUI/AIUIDocument';\nimport type { AIMessageAttachmentData } from '../Message/AIMessageAttachment';\nimport { parseMessageParts } from '../MessageParts/parseMessageParts';\nimport { withCustomFields } from '../utils';\nimport type { AIMessage } from './types';\n\n/** What the adapter reads of a message: a `LocalMessage` or a `MessageResponse` will do. */\ntype StreamMessage = Pick<LocalMessage | MessageResponse, 'attachments' | 'id' | 'text'>;\n\nexport type FromStreamMessageOptions<M extends StreamMessage = StreamMessage> = {\n  /** Whether the agent wrote it. Defaults to the message's `custom.ai_generated` field. */\n  isAssistant?: (message: M) => boolean;\n  /** The whole of a reasoning step's thinking, when the app has it, such as from live updates. */\n  reasoningText?: (messageId: string, stepId: string) => string | undefined;\n};\n\n// Images and files only: the steps (`ai_*`) are read as parts, and other kinds are the app's.\nconst attachmentOf = (attachment: Attachment): AIMessageAttachmentData | undefined => {\n  const name = attachment.title ?? attachment.fallback ?? undefined;\n  // stream-chat keeps the file's size and type under `custom`.\n  const { file_size, mime_type } = attachment.custom ?? {};\n  if (attachment.type === 'image') {\n    const url = attachment.image_url ?? attachment.asset_url ?? attachment.thumb_url;\n    return url ? { kind: 'image', mimeType: mime_type, name, url } : undefined;\n  }\n  if (attachment.type === 'file') {\n    const size = Number(file_size);\n    return attachment.asset_url\n      ? {\n          kind: 'file',\n          mimeType: mime_type,\n          name,\n          size: Number.isFinite(size) && size > 0 ? size : undefined,\n          url: attachment.asset_url,\n        }\n      : undefined;\n  }\n  return undefined;\n};\n\n/**\n * Adapts a Stream message for `AIConversation`: a reply is one with `custom.ai_generated: true`,\n * it is being written while `custom.generating: true`, its steps are its `ai_reasoning` and\n * `ai_tool_call` attachments, its images and files are its other attachments, and the UI the\n * agent wrote for it is its `custom.ai_ui` field. A person's message has no UI, whatever its\n * fields say.\n */\nexport const fromStreamMessage = <M extends StreamMessage>(\n  message: M,\n  { isAssistant, reasoningText }: FromStreamMessageOptions<M> = {},\n): AIMessage => {\n  const attachments = message.attachments ?? [];\n  const fields = withCustomFields(message as unknown as Record<string, unknown>);\n  const assistant = isAssistant ? isAssistant(message) : fields.ai_generated === true;\n  const uiDocument = assistant\n    ? parseAIUIDocument(fields[AI_UI_CUSTOM_DATA_KEY])\n    : undefined;\n  return {\n    attachments: attachments\n      .map(attachmentOf)\n      .filter((attachment): attachment is AIMessageAttachmentData => !!attachment),\n    id: message.id,\n    isGenerating: fields.generating === true,\n    parts: parseMessageParts(attachments),\n    reasoningText: reasoningText\n      ? (stepId: string) => reasoningText(message.id, stepId)\n      : undefined,\n    role: assistant ? 'assistant' : 'user',\n    text: message.text ?? '',\n    ...(uiDocument ? { uiDocument } : {}),\n  };\n};\n"],"mappings":";;;;AAmBA,IAAM,gBAAgB,eAAgE;CACpF,MAAM,OAAO,WAAW,SAAS,WAAW,YAAY,KAAA;CAExD,MAAM,EAAE,WAAW,cAAc,WAAW,UAAU,CAAC;CACvD,IAAI,WAAW,SAAS,SAAS;EAC/B,MAAM,MAAM,WAAW,aAAa,WAAW,aAAa,WAAW;EACvE,OAAO,MAAM;GAAE,MAAM;GAAS,UAAU;GAAW;GAAM;EAAI,IAAI,KAAA;CACnE;CACA,IAAI,WAAW,SAAS,QAAQ;EAC9B,MAAM,OAAO,OAAO,SAAS;EAC7B,OAAO,WAAW,YACd;GACE,MAAM;GACN,UAAU;GACV;GACA,MAAM,OAAO,SAAS,IAAI,KAAK,OAAO,IAAI,OAAO,KAAA;GACjD,KAAK,WAAW;EAClB,IACA,KAAA;CACN;AAEF;;;;;;;;AASA,IAAa,qBACX,SACA,EAAE,aAAa,kBAA+C,CAAC,MACjD;CACd,MAAM,cAAc,QAAQ,eAAe,CAAC;CAC5C,MAAM,SAAS,iBAAiB,OAA6C;CAC7E,MAAM,YAAY,cAAc,YAAY,OAAO,IAAI,OAAO,iBAAiB;CAC/E,MAAM,aAAa,YACf,kBAAkB,OAAO,sBAAsB,IAC/C,KAAA;CACJ,OAAO;EACL,aAAa,YACV,IAAI,YAAY,CAAC,CACjB,QAAQ,eAAsD,CAAC,CAAC,UAAU;EAC7E,IAAI,QAAQ;EACZ,cAAc,OAAO,eAAe;EACpC,OAAO,kBAAkB,WAAW;EACpC,eAAe,iBACV,WAAmB,cAAc,QAAQ,IAAI,MAAM,IACpD,KAAA;EACJ,MAAM,YAAY,cAAc;EAChC,MAAM,QAAQ,QAAQ;EACtB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;CACrC;AACF"}