@cossistant/react 0.0.25 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/api.d.ts +1 -1
  2. package/api.d.ts.map +1 -1
  3. package/checks.d.ts +1 -1
  4. package/checks.d.ts.map +1 -1
  5. package/clsx.d.ts +1 -1
  6. package/clsx.d.ts.map +1 -1
  7. package/coerce.d.ts +1 -1
  8. package/coerce.d.ts.map +1 -1
  9. package/conversation.d.ts +3 -0
  10. package/conversation.d.ts.map +1 -1
  11. package/core.d.ts +1 -1
  12. package/core.d.ts.map +1 -1
  13. package/errors.d.ts +1 -1
  14. package/errors.d.ts.map +1 -1
  15. package/errors2.d.ts +1 -1
  16. package/errors2.d.ts.map +1 -1
  17. package/hooks/index.d.ts +2 -1
  18. package/hooks/index.js +6 -5
  19. package/hooks/private/store/use-website-store.js +2 -1
  20. package/hooks/private/store/use-website-store.js.map +1 -1
  21. package/hooks/private/use-client-query.d.ts +6 -0
  22. package/hooks/private/use-client-query.d.ts.map +1 -1
  23. package/hooks/private/use-client-query.js +26 -3
  24. package/hooks/private/use-client-query.js.map +1 -1
  25. package/hooks/private/use-multimodal-input.d.ts.map +1 -1
  26. package/hooks/private/use-multimodal-input.js +7 -5
  27. package/hooks/private/use-multimodal-input.js.map +1 -1
  28. package/hooks/private/use-visitor-typing-reporter.d.ts +18 -1
  29. package/hooks/private/use-visitor-typing-reporter.d.ts.map +1 -1
  30. package/hooks/private/use-visitor-typing-reporter.js +34 -4
  31. package/hooks/private/use-visitor-typing-reporter.js.map +1 -1
  32. package/hooks/use-conversation-page.d.ts +1 -0
  33. package/hooks/use-conversation-page.d.ts.map +1 -1
  34. package/hooks/use-conversation-page.js +6 -1
  35. package/hooks/use-conversation-page.js.map +1 -1
  36. package/hooks/use-conversation-preview.d.ts +2 -1
  37. package/hooks/use-conversation-preview.d.ts.map +1 -1
  38. package/hooks/use-conversation-preview.js +1 -1
  39. package/hooks/use-conversation-preview.js.map +1 -1
  40. package/hooks/use-conversation-timeline-items.js +2 -1
  41. package/hooks/use-conversation-timeline-items.js.map +1 -1
  42. package/hooks/use-conversation.js +2 -1
  43. package/hooks/use-conversation.js.map +1 -1
  44. package/hooks/use-conversations.js +1 -0
  45. package/hooks/use-conversations.js.map +1 -1
  46. package/hooks/use-file-upload.d.ts +55 -0
  47. package/hooks/use-file-upload.d.ts.map +1 -0
  48. package/hooks/use-file-upload.js +100 -0
  49. package/hooks/use-file-upload.js.map +1 -0
  50. package/hooks/use-message-composer.d.ts +11 -0
  51. package/hooks/use-message-composer.d.ts.map +1 -1
  52. package/hooks/use-message-composer.js +7 -3
  53. package/hooks/use-message-composer.js.map +1 -1
  54. package/hooks/use-realtime-support.d.ts.map +1 -1
  55. package/hooks/use-send-message.d.ts +1 -0
  56. package/hooks/use-send-message.d.ts.map +1 -1
  57. package/hooks/use-send-message.js +63 -11
  58. package/hooks/use-send-message.js.map +1 -1
  59. package/index.d.ts +6 -3
  60. package/index.js +13 -10
  61. package/openapi30.d.ts +1 -1
  62. package/openapi30.d.ts.map +1 -1
  63. package/openapi31.d.ts +1 -1
  64. package/openapi31.d.ts.map +1 -1
  65. package/package.json +4 -3
  66. package/parse.d.ts +1 -1
  67. package/parse.d.ts.map +1 -1
  68. package/primitives/conversation-timeline.d.ts.map +1 -1
  69. package/primitives/conversation-timeline.js +10 -5
  70. package/primitives/conversation-timeline.js.map +1 -1
  71. package/primitives/index.d.ts +4 -3
  72. package/primitives/index.js +12 -5
  73. package/primitives/index.parts.d.ts +3 -2
  74. package/primitives/index.parts.js +4 -3
  75. package/primitives/timeline-item-attachments.d.ts +100 -0
  76. package/primitives/timeline-item-attachments.d.ts.map +1 -0
  77. package/primitives/timeline-item-attachments.js +151 -0
  78. package/primitives/timeline-item-attachments.js.map +1 -0
  79. package/primitives/trigger.d.ts +91 -0
  80. package/primitives/trigger.d.ts.map +1 -0
  81. package/primitives/trigger.js +74 -0
  82. package/primitives/trigger.js.map +1 -0
  83. package/primitives/window.d.ts +22 -1
  84. package/primitives/window.d.ts.map +1 -1
  85. package/primitives/window.js +91 -5
  86. package/primitives/window.js.map +1 -1
  87. package/provider.d.ts.map +1 -1
  88. package/provider.js +8 -3
  89. package/provider.js.map +1 -1
  90. package/realtime/index.js +1 -1
  91. package/realtime/provider.js +1 -1
  92. package/realtime/support-provider.js +1 -1
  93. package/realtime/support-provider.js.map +1 -1
  94. package/realtime-events.d.ts +39 -0
  95. package/realtime-events.d.ts.map +1 -1
  96. package/registries.d.ts +1 -1
  97. package/registries.d.ts.map +1 -1
  98. package/schemas.d.ts +1 -1
  99. package/schemas.d.ts.map +1 -1
  100. package/schemas2.d.ts +1 -1
  101. package/schemas2.d.ts.map +1 -1
  102. package/schemas3.d.ts +1 -0
  103. package/schemas3.d.ts.map +1 -1
  104. package/specification-extension.d.ts +1 -1
  105. package/specification-extension.d.ts.map +1 -1
  106. package/standard-schema.d.ts +1 -1
  107. package/standard-schema.d.ts.map +1 -1
  108. package/support/components/button.d.ts +1 -1
  109. package/support/components/content.d.ts +30 -0
  110. package/support/components/content.d.ts.map +1 -0
  111. package/support/components/content.js +282 -0
  112. package/support/components/content.js.map +1 -0
  113. package/support/components/conversation-button-link.js +1 -1
  114. package/support/components/conversation-timeline.js +3 -3
  115. package/support/components/conversation-timeline.js.map +1 -1
  116. package/support/components/header.js +1 -1
  117. package/support/components/image-lightbox.d.ts +49 -0
  118. package/support/components/image-lightbox.d.ts.map +1 -0
  119. package/support/components/image-lightbox.js +142 -0
  120. package/support/components/image-lightbox.js.map +1 -0
  121. package/support/components/index.d.ts +5 -4
  122. package/support/components/index.js +4 -4
  123. package/support/components/multimodal-input.d.ts +4 -1
  124. package/support/components/multimodal-input.d.ts.map +1 -1
  125. package/support/components/multimodal-input.js +71 -45
  126. package/support/components/multimodal-input.js.map +1 -1
  127. package/support/components/navigation-tab.js +1 -1
  128. package/support/components/root.d.ts +23 -0
  129. package/support/components/root.d.ts.map +1 -0
  130. package/support/components/root.js +36 -0
  131. package/support/components/root.js.map +1 -0
  132. package/support/components/timeline-message-item.d.ts.map +1 -1
  133. package/support/components/timeline-message-item.js +82 -18
  134. package/support/components/timeline-message-item.js.map +1 -1
  135. package/support/components/trigger.d.ts +14 -0
  136. package/support/components/trigger.d.ts.map +1 -0
  137. package/support/components/{bubble.js → trigger.js} +16 -12
  138. package/support/components/trigger.js.map +1 -0
  139. package/support/components/typing-indicator.d.ts.map +1 -1
  140. package/support/components/typing-indicator.js +1 -0
  141. package/support/components/typing-indicator.js.map +1 -1
  142. package/support/context/controlled-state.d.ts +46 -0
  143. package/support/context/controlled-state.d.ts.map +1 -0
  144. package/support/context/controlled-state.js +34 -0
  145. package/support/context/controlled-state.js.map +1 -0
  146. package/support/context/events.d.ts +103 -0
  147. package/support/context/events.d.ts.map +1 -0
  148. package/support/context/events.js +139 -0
  149. package/support/context/events.js.map +1 -0
  150. package/support/context/handle.d.ts +90 -0
  151. package/support/context/handle.d.ts.map +1 -0
  152. package/support/context/handle.js +79 -0
  153. package/support/context/handle.js.map +1 -0
  154. package/support/context/positioning.d.ts +17 -0
  155. package/support/context/positioning.d.ts.map +1 -0
  156. package/support/context/positioning.js +26 -0
  157. package/support/context/positioning.js.map +1 -0
  158. package/support/context/slots.d.ts +85 -0
  159. package/support/context/slots.d.ts.map +1 -0
  160. package/support/context/slots.js +115 -0
  161. package/support/context/slots.js.map +1 -0
  162. package/support/context/websocket.d.ts +8 -1
  163. package/support/context/websocket.d.ts.map +1 -1
  164. package/support/context/websocket.js +8 -1
  165. package/support/context/websocket.js.map +1 -1
  166. package/support/index.d.ts +239 -54
  167. package/support/index.d.ts.map +1 -1
  168. package/support/index.js +254 -33
  169. package/support/index.js.map +1 -1
  170. package/support/pages/articles.d.ts.map +1 -1
  171. package/support/pages/articles.js +3 -4
  172. package/support/pages/articles.js.map +1 -1
  173. package/support/pages/conversation-history.js +2 -2
  174. package/support/pages/conversation.js +6 -5
  175. package/support/pages/conversation.js.map +1 -1
  176. package/support/pages/home.js +2 -2
  177. package/support/router.d.ts +52 -12
  178. package/support/router.d.ts.map +1 -1
  179. package/support/router.js +78 -30
  180. package/support/router.js.map +1 -1
  181. package/support/store/index.d.ts +2 -2
  182. package/support/store/support-store.d.ts +26 -20
  183. package/support/store/support-store.d.ts.map +1 -1
  184. package/support/store/support-store.js +47 -6
  185. package/support/store/support-store.js.map +1 -1
  186. package/support/{support-D2EgfIts.css → support-C7Xaw-N6.css} +1 -2
  187. package/support/support-C7Xaw-N6.css.map +1 -0
  188. package/support/text/index.js.map +1 -1
  189. package/support/types.d.ts +75 -12
  190. package/support/types.d.ts.map +1 -1
  191. package/support.css +2 -2
  192. package/tailwind.css +0 -1
  193. package/timeline-item.d.ts +68 -2
  194. package/timeline-item.d.ts.map +1 -1
  195. package/util.d.ts +1 -1
  196. package/util.d.ts.map +1 -1
  197. package/utils/index.d.ts +2 -1
  198. package/utils/index.js +2 -1
  199. package/utils/merge-refs.d.ts +30 -0
  200. package/utils/merge-refs.d.ts.map +1 -0
  201. package/utils/merge-refs.js +46 -0
  202. package/utils/merge-refs.js.map +1 -0
  203. package/utils/use-render-element.d.ts.map +1 -1
  204. package/utils/use-render-element.js +20 -7
  205. package/utils/use-render-element.js.map +1 -1
  206. package/versions.d.ts +1 -1
  207. package/versions.d.ts.map +1 -1
  208. package/zod-extensions.d.ts +1 -1
  209. package/zod-extensions.d.ts.map +1 -1
  210. package/primitives/bubble.d.ts +0 -38
  211. package/primitives/bubble.d.ts.map +0 -1
  212. package/primitives/bubble.js +0 -57
  213. package/primitives/bubble.js.map +0 -1
  214. package/support/components/bubble.d.ts +0 -10
  215. package/support/components/bubble.d.ts.map +0 -1
  216. package/support/components/bubble.js.map +0 -1
  217. package/support/components/container.d.ts +0 -13
  218. package/support/components/container.d.ts.map +0 -1
  219. package/support/components/container.js +0 -109
  220. package/support/components/container.js.map +0 -1
  221. package/support/components/support-content.d.ts +0 -22
  222. package/support/components/support-content.d.ts.map +0 -1
  223. package/support/components/support-content.js +0 -48
  224. package/support/components/support-content.js.map +0 -1
  225. package/support/support-D2EgfIts.css.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"use-conversation-preview.js","names":["senderImage: string | null","typingState: ConversationPreviewTypingState"],"sources":["../../src/hooks/use-conversation-preview.ts"],"sourcesContent":["import type { Conversation } from \"@cossistant/types\";\nimport type { TimelineItem } from \"@cossistant/types/api/timeline-item\";\nimport { useMemo } from \"react\";\n\nimport { useSupport } from \"../provider\";\nimport { useSupportText } from \"../support/text\";\nimport { formatTimeAgo } from \"../support/utils/time\";\nimport {\n\tmapTypingEntriesToPreviewParticipants,\n\ttype PreviewTypingParticipant,\n} from \"./private/typing\";\nimport { useConversationTimelineItems } from \"./use-conversation-timeline-items\";\nimport { useConversationTyping } from \"./use-conversation-typing\";\n\nexport type ConversationPreviewLastMessage = {\n\tcontent: string;\n\ttime: string;\n\tisFromVisitor: boolean;\n\tsenderName?: string;\n\tsenderImage?: string | null;\n};\n\nexport type ConversationPreviewAssignedAgent = {\n\tname: string;\n\timage: string | null;\n\ttype: \"human\" | \"ai\" | \"fallback\";\n};\n\nexport type ConversationPreviewTypingParticipant = PreviewTypingParticipant;\n\nexport type ConversationPreviewTypingState = {\n\tparticipants: ConversationPreviewTypingParticipant[];\n\tprimaryParticipant: ConversationPreviewTypingParticipant | null;\n\tlabel: string | null;\n\tisTyping: boolean;\n};\n\nexport type UseConversationPreviewOptions = {\n\tconversation: Conversation;\n\t/**\n\t * Whether the hook should fetch timeline items for the conversation.\n\t * Enabled by default.\n\t */\n\tincludeTimelineItems?: boolean;\n\t/**\n\t * Optional timeline items to merge with the live ones (e.g. optimistic items).\n\t */\n\tinitialTimelineItems?: TimelineItem[];\n\t/**\n\t * Typing state configuration (mainly exclusions for the current visitor).\n\t */\n\ttyping?: {\n\t\texcludeVisitorId?: string | null;\n\t\texcludeUserId?: string | null;\n\t\texcludeAiAgentId?: string | null;\n\t};\n};\n\nexport type UseConversationPreviewReturn = {\n\tconversation: Conversation;\n\ttitle: string;\n\tlastMessage: ConversationPreviewLastMessage | null;\n\tassignedAgent: ConversationPreviewAssignedAgent;\n\ttyping: ConversationPreviewTypingState;\n\ttimeline: ReturnType<typeof useConversationTimelineItems>;\n};\n\nfunction resolveLastTimelineMessage(\n\titems: TimelineItem[],\n\tfallback: TimelineItem | null\n) {\n\tfor (let index = items.length - 1; index >= 0; index--) {\n\t\tconst item = items[index];\n\n\t\tif (item?.type === \"message\") {\n\t\t\treturn item;\n\t\t}\n\t}\n\n\tif (fallback?.type === \"message\") {\n\t\treturn fallback;\n\t}\n\n\treturn null;\n}\n\n/**\n * Composes conversation metadata including derived titles, last message\n * snippets and typing state for use in lists.\n */\nexport function useConversationPreview(\n\toptions: UseConversationPreviewOptions\n): UseConversationPreviewReturn {\n\tconst {\n\t\tconversation,\n\t\tincludeTimelineItems = true,\n\t\tinitialTimelineItems = [],\n\t\ttyping,\n\t} = options;\n\tconst { availableHumanAgents, availableAIAgents, visitor } = useSupport();\n\tconst text = useSupportText();\n\n\tconst timeline = useConversationTimelineItems(conversation.id, {\n\t\tenabled: includeTimelineItems,\n\t});\n\n\tconst mergedTimelineItems = useMemo(() => {\n\t\tif (timeline.items.length > 0) {\n\t\t\treturn timeline.items;\n\t\t}\n\n\t\tif (initialTimelineItems.length > 0) {\n\t\t\treturn initialTimelineItems;\n\t\t}\n\n\t\treturn [] as TimelineItem[];\n\t}, [timeline.items, initialTimelineItems]);\n\n\tconst knownTimelineItems = useMemo(() => {\n\t\tconst items = [...mergedTimelineItems];\n\n\t\tif (\n\t\t\tconversation.lastTimelineItem &&\n\t\t\t!items.some((item) => item.id === conversation.lastTimelineItem?.id)\n\t\t) {\n\t\t\titems.push(conversation.lastTimelineItem);\n\t\t}\n\n\t\treturn items;\n\t}, [mergedTimelineItems, conversation.lastTimelineItem]);\n\n\tconst lastTimelineMessage = useMemo(\n\t\t() =>\n\t\t\tresolveLastTimelineMessage(\n\t\t\t\tmergedTimelineItems,\n\t\t\t\tconversation.lastTimelineItem ?? null\n\t\t\t),\n\t\t[mergedTimelineItems, conversation.lastTimelineItem]\n\t);\n\n\tconst lastMessage = useMemo<ConversationPreviewLastMessage | null>(() => {\n\t\tif (!lastTimelineMessage) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst isFromVisitor = lastTimelineMessage.visitorId !== null;\n\n\t\tlet senderName = text(\"common.fallbacks.unknown\");\n\t\tlet senderImage: string | null = null;\n\n\t\tif (isFromVisitor) {\n\t\t\tsenderName = text(\"common.fallbacks.you\");\n\t\t} else if (lastTimelineMessage.userId) {\n\t\t\tconst agent = availableHumanAgents.find(\n\t\t\t\t(a) => a.id === lastTimelineMessage.userId\n\t\t\t);\n\t\t\tif (agent) {\n\t\t\t\tsenderName = agent.name;\n\t\t\t\tsenderImage = agent.image;\n\t\t\t} else {\n\t\t\t\tsenderName = text(\"common.fallbacks.supportTeam\");\n\t\t\t}\n\t\t} else if (lastTimelineMessage.aiAgentId) {\n\t\t\tconst aiAgent = availableAIAgents.find(\n\t\t\t\t(agent) => agent.id === lastTimelineMessage.aiAgentId\n\t\t\t);\n\t\t\tif (aiAgent) {\n\t\t\t\tsenderName = aiAgent.name;\n\t\t\t\tsenderImage = aiAgent.image;\n\t\t\t} else {\n\t\t\t\tsenderName = text(\"common.fallbacks.aiAssistant\");\n\t\t\t}\n\t\t} else {\n\t\t\tsenderName = text(\"common.fallbacks.supportTeam\");\n\t\t}\n\n\t\treturn {\n\t\t\tcontent: lastTimelineMessage.text || \"\",\n\t\t\ttime: formatTimeAgo(lastTimelineMessage.createdAt),\n\t\t\tisFromVisitor,\n\t\t\tsenderName,\n\t\t\tsenderImage,\n\t\t};\n\t}, [lastTimelineMessage, availableHumanAgents, availableAIAgents, text]);\n\n\tconst assignedAgent = useMemo<ConversationPreviewAssignedAgent>(() => {\n\t\tconst supportFallbackName = text(\"common.fallbacks.supportTeam\");\n\t\tconst aiFallbackName = text(\"common.fallbacks.aiAssistant\");\n\n\t\tconst lastAgentItem = [...knownTimelineItems]\n\t\t\t.reverse()\n\t\t\t.find((item) => item.userId !== null || item.aiAgentId !== null);\n\n\t\tif (lastAgentItem?.userId) {\n\t\t\tconst human = availableHumanAgents.find(\n\t\t\t\t(agent) => agent.id === lastAgentItem.userId\n\t\t\t);\n\n\t\t\tif (human) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"human\" as const,\n\t\t\t\t\tname: human.name,\n\t\t\t\t\timage: human.image ?? null,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"human\" as const,\n\t\t\t\tname: supportFallbackName,\n\t\t\t\timage: null,\n\t\t\t};\n\t\t}\n\n\t\tif (lastAgentItem?.aiAgentId) {\n\t\t\tconst ai = availableAIAgents.find(\n\t\t\t\t(agent) => agent.id === lastAgentItem.aiAgentId\n\t\t\t);\n\n\t\t\tif (ai) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"ai\" as const,\n\t\t\t\t\tname: ai.name,\n\t\t\t\t\timage: ai.image ?? null,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"ai\" as const,\n\t\t\t\tname: aiFallbackName,\n\t\t\t\timage: null,\n\t\t\t};\n\t\t}\n\n\t\tconst fallbackHuman = availableHumanAgents[0];\n\t\tif (fallbackHuman) {\n\t\t\treturn {\n\t\t\t\ttype: \"human\" as const,\n\t\t\t\tname: fallbackHuman.name,\n\t\t\t\timage: fallbackHuman.image ?? null,\n\t\t\t};\n\t\t}\n\n\t\tconst fallbackAi = availableAIAgents[0];\n\t\tif (fallbackAi) {\n\t\t\treturn {\n\t\t\t\ttype: \"ai\" as const,\n\t\t\t\tname: fallbackAi.name,\n\t\t\t\timage: fallbackAi.image ?? null,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"fallback\" as const,\n\t\t\tname: supportFallbackName,\n\t\t\timage: null,\n\t\t};\n\t}, [knownTimelineItems, availableHumanAgents, availableAIAgents, text]);\n\n\tconst typingEntries = useConversationTyping(conversation.id, {\n\t\texcludeVisitorId: typing?.excludeVisitorId ?? visitor?.id ?? null,\n\t\texcludeUserId: typing?.excludeUserId ?? null,\n\t\texcludeAiAgentId: typing?.excludeAiAgentId ?? null,\n\t});\n\n\tconst typingParticipants = useMemo(\n\t\t() =>\n\t\t\tmapTypingEntriesToPreviewParticipants(typingEntries, {\n\t\t\t\tavailableHumanAgents,\n\t\t\t\tavailableAIAgents,\n\t\t\t\ttext,\n\t\t\t}),\n\t\t[typingEntries, availableHumanAgents, availableAIAgents, text]\n\t);\n\n\tconst primaryTypingParticipant = typingParticipants[0] ?? null;\n\n\tconst typingLabel = useMemo(() => {\n\t\tif (!primaryTypingParticipant) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn text(\"component.conversationButtonLink.typing\", {\n\t\t\tname: primaryTypingParticipant.name,\n\t\t});\n\t}, [primaryTypingParticipant, text]);\n\n\tconst typingState: ConversationPreviewTypingState = useMemo(\n\t\t() => ({\n\t\t\tparticipants: typingParticipants,\n\t\t\tprimaryParticipant: primaryTypingParticipant,\n\t\t\tlabel: typingLabel,\n\t\t\tisTyping: typingParticipants.length > 0,\n\t\t}),\n\t\t[typingParticipants, primaryTypingParticipant, typingLabel]\n\t);\n\n\tconst title = useMemo(() => {\n\t\tif (conversation.title) {\n\t\t\treturn conversation.title;\n\t\t}\n\n\t\tif (lastMessage?.content) {\n\t\t\treturn lastMessage.content;\n\t\t}\n\n\t\treturn text(\"component.conversationButtonLink.fallbackTitle\");\n\t}, [conversation.title, lastMessage?.content, text]);\n\n\treturn {\n\t\tconversation,\n\t\ttitle,\n\t\tlastMessage,\n\t\tassignedAgent,\n\t\ttyping: typingState,\n\t\ttimeline,\n\t};\n}\n"],"mappings":";;;;;;;;;AAmEA,SAAS,2BACR,OACA,UACC;AACD,MAAK,IAAI,QAAQ,MAAM,SAAS,GAAG,SAAS,GAAG,SAAS;EACvD,MAAM,OAAO,MAAM;AAEnB,MAAI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AEvE,MAAM,gBAAgB,sBAAsB,aAAa,IAAI;EAC5D,kBAAkB,QAAQ,oBAAoB,SAAS,MAAM;EAC7D,eAAe,QAAQ,iBAAiB;EACxC,kBAAkB,QAAQ,oBAAoB;EAC9C,CAAC;CAEF,MAAM,qBAAqB,cAEzB,sCAAsC,eAAe;EACpD;EACA;EACA;EACA,CAAC,EACH;EAAC;EAAe;EAAsB;EAAmB;EAAK,CAC9D;CAED,MAAM,2BAA2B,mBAAmB,MAAM;CAE1D,MAAM,cAAc,cAAc;AACjC,MAAI,CAAC,yBACJ,QAAO;AAGR,SAAO,KAAK,2CAA2C,EACtD,MAAM,yBAAyB,MAC/B,CAAC;IACA,CAAC,0BAA0B,KAAK,CAAC;CAEpC,MAAMC,cAA8C,eAC5C;EACN,cAAc;EACd,oBAAoB;EACpB,OAAO;EACP,UAAU,mBAAmB,SAAS;EACtC,GACD;EAAC;EAAoB;EAA0B;EAAY,CAC3D;AAcD,QAAO;EACN;EACA,OAda,cAAc;AAC3B,OAAI,aAAa,MAChB,QAAO,aAAa;AAGrB,OAAI,aAAa,QAChB,QAAO,YAAY;AAGpB,UAAO,KAAK,iDAAiD;KAC3D;GAAC,aAAa;GAAO,aAAa;GAAS;GAAK,CAAC;EAKnD;EACA;EACA,QAAQ;EACR;EACA"}
1
+ {"version":3,"file":"use-conversation-preview.js","names":["senderImage: string | null","typingState: ConversationPreviewTypingState"],"sources":["../../src/hooks/use-conversation-preview.ts"],"sourcesContent":["import type { Conversation } from \"@cossistant/types\";\nimport type { TimelineItem } from \"@cossistant/types/api/timeline-item\";\nimport { useMemo } from \"react\";\n\nimport { useSupport } from \"../provider\";\nimport { useSupportText } from \"../support/text\";\nimport { formatTimeAgo } from \"../support/utils/time\";\nimport {\n\tmapTypingEntriesToPreviewParticipants,\n\ttype PreviewTypingParticipant,\n} from \"./private/typing\";\nimport { useConversationTimelineItems } from \"./use-conversation-timeline-items\";\nimport { useConversationTyping } from \"./use-conversation-typing\";\n\nexport type ConversationPreviewLastMessage = {\n\tcontent: string;\n\ttime: string;\n\tisFromVisitor: boolean;\n\tsenderName?: string;\n\tsenderImage?: string | null;\n};\n\nexport type ConversationPreviewAssignedAgent = {\n\tname: string;\n\timage: string | null;\n\ttype: \"human\" | \"ai\" | \"fallback\";\n};\n\nexport type ConversationPreviewTypingParticipant = PreviewTypingParticipant;\n\nexport type ConversationPreviewTypingState = {\n\tparticipants: ConversationPreviewTypingParticipant[];\n\tprimaryParticipant: ConversationPreviewTypingParticipant | null;\n\tlabel: string | null;\n\tisTyping: boolean;\n};\n\nexport type UseConversationPreviewOptions = {\n\tconversation: Conversation;\n\t/**\n\t * Whether the hook should fetch timeline items for the conversation.\n\t * Disabled by default to reduce API calls - conversation.lastTimelineItem\n\t * is typically sufficient for previews.\n\t */\n\tincludeTimelineItems?: boolean;\n\t/**\n\t * Optional timeline items to merge with the live ones (e.g. optimistic items).\n\t */\n\tinitialTimelineItems?: TimelineItem[];\n\t/**\n\t * Typing state configuration (mainly exclusions for the current visitor).\n\t */\n\ttyping?: {\n\t\texcludeVisitorId?: string | null;\n\t\texcludeUserId?: string | null;\n\t\texcludeAiAgentId?: string | null;\n\t};\n};\n\nexport type UseConversationPreviewReturn = {\n\tconversation: Conversation;\n\ttitle: string;\n\tlastMessage: ConversationPreviewLastMessage | null;\n\tassignedAgent: ConversationPreviewAssignedAgent;\n\ttyping: ConversationPreviewTypingState;\n\ttimeline: ReturnType<typeof useConversationTimelineItems>;\n};\n\nfunction resolveLastTimelineMessage(\n\titems: TimelineItem[],\n\tfallback: TimelineItem | null\n) {\n\tfor (let index = items.length - 1; index >= 0; index--) {\n\t\tconst item = items[index];\n\n\t\tif (item?.type === \"message\") {\n\t\t\treturn item;\n\t\t}\n\t}\n\n\tif (fallback?.type === \"message\") {\n\t\treturn fallback;\n\t}\n\n\treturn null;\n}\n\n/**\n * Composes conversation metadata including derived titles, last message\n * snippets and typing state for use in lists.\n */\nexport function useConversationPreview(\n\toptions: UseConversationPreviewOptions\n): UseConversationPreviewReturn {\n\tconst {\n\t\tconversation,\n\t\tincludeTimelineItems = false,\n\t\tinitialTimelineItems = [],\n\t\ttyping,\n\t} = options;\n\tconst { availableHumanAgents, availableAIAgents, visitor } = useSupport();\n\tconst text = useSupportText();\n\n\tconst timeline = useConversationTimelineItems(conversation.id, {\n\t\tenabled: includeTimelineItems,\n\t});\n\n\tconst mergedTimelineItems = useMemo(() => {\n\t\tif (timeline.items.length > 0) {\n\t\t\treturn timeline.items;\n\t\t}\n\n\t\tif (initialTimelineItems.length > 0) {\n\t\t\treturn initialTimelineItems;\n\t\t}\n\n\t\treturn [] as TimelineItem[];\n\t}, [timeline.items, initialTimelineItems]);\n\n\tconst knownTimelineItems = useMemo(() => {\n\t\tconst items = [...mergedTimelineItems];\n\n\t\tif (\n\t\t\tconversation.lastTimelineItem &&\n\t\t\t!items.some((item) => item.id === conversation.lastTimelineItem?.id)\n\t\t) {\n\t\t\titems.push(conversation.lastTimelineItem);\n\t\t}\n\n\t\treturn items;\n\t}, [mergedTimelineItems, conversation.lastTimelineItem]);\n\n\tconst lastTimelineMessage = useMemo(\n\t\t() =>\n\t\t\tresolveLastTimelineMessage(\n\t\t\t\tmergedTimelineItems,\n\t\t\t\tconversation.lastTimelineItem ?? null\n\t\t\t),\n\t\t[mergedTimelineItems, conversation.lastTimelineItem]\n\t);\n\n\tconst lastMessage = useMemo<ConversationPreviewLastMessage | null>(() => {\n\t\tif (!lastTimelineMessage) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst isFromVisitor = lastTimelineMessage.visitorId !== null;\n\n\t\tlet senderName = text(\"common.fallbacks.unknown\");\n\t\tlet senderImage: string | null = null;\n\n\t\tif (isFromVisitor) {\n\t\t\tsenderName = text(\"common.fallbacks.you\");\n\t\t} else if (lastTimelineMessage.userId) {\n\t\t\tconst agent = availableHumanAgents.find(\n\t\t\t\t(a) => a.id === lastTimelineMessage.userId\n\t\t\t);\n\t\t\tif (agent) {\n\t\t\t\tsenderName = agent.name;\n\t\t\t\tsenderImage = agent.image;\n\t\t\t} else {\n\t\t\t\tsenderName = text(\"common.fallbacks.supportTeam\");\n\t\t\t}\n\t\t} else if (lastTimelineMessage.aiAgentId) {\n\t\t\tconst aiAgent = availableAIAgents.find(\n\t\t\t\t(agent) => agent.id === lastTimelineMessage.aiAgentId\n\t\t\t);\n\t\t\tif (aiAgent) {\n\t\t\t\tsenderName = aiAgent.name;\n\t\t\t\tsenderImage = aiAgent.image;\n\t\t\t} else {\n\t\t\t\tsenderName = text(\"common.fallbacks.aiAssistant\");\n\t\t\t}\n\t\t} else {\n\t\t\tsenderName = text(\"common.fallbacks.supportTeam\");\n\t\t}\n\n\t\treturn {\n\t\t\tcontent: lastTimelineMessage.text || \"\",\n\t\t\ttime: formatTimeAgo(lastTimelineMessage.createdAt),\n\t\t\tisFromVisitor,\n\t\t\tsenderName,\n\t\t\tsenderImage,\n\t\t};\n\t}, [lastTimelineMessage, availableHumanAgents, availableAIAgents, text]);\n\n\tconst assignedAgent = useMemo<ConversationPreviewAssignedAgent>(() => {\n\t\tconst supportFallbackName = text(\"common.fallbacks.supportTeam\");\n\t\tconst aiFallbackName = text(\"common.fallbacks.aiAssistant\");\n\n\t\tconst lastAgentItem = [...knownTimelineItems]\n\t\t\t.reverse()\n\t\t\t.find((item) => item.userId !== null || item.aiAgentId !== null);\n\n\t\tif (lastAgentItem?.userId) {\n\t\t\tconst human = availableHumanAgents.find(\n\t\t\t\t(agent) => agent.id === lastAgentItem.userId\n\t\t\t);\n\n\t\t\tif (human) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"human\" as const,\n\t\t\t\t\tname: human.name,\n\t\t\t\t\timage: human.image ?? null,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"human\" as const,\n\t\t\t\tname: supportFallbackName,\n\t\t\t\timage: null,\n\t\t\t};\n\t\t}\n\n\t\tif (lastAgentItem?.aiAgentId) {\n\t\t\tconst ai = availableAIAgents.find(\n\t\t\t\t(agent) => agent.id === lastAgentItem.aiAgentId\n\t\t\t);\n\n\t\t\tif (ai) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"ai\" as const,\n\t\t\t\t\tname: ai.name,\n\t\t\t\t\timage: ai.image ?? null,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"ai\" as const,\n\t\t\t\tname: aiFallbackName,\n\t\t\t\timage: null,\n\t\t\t};\n\t\t}\n\n\t\tconst fallbackHuman = availableHumanAgents[0];\n\t\tif (fallbackHuman) {\n\t\t\treturn {\n\t\t\t\ttype: \"human\" as const,\n\t\t\t\tname: fallbackHuman.name,\n\t\t\t\timage: fallbackHuman.image ?? null,\n\t\t\t};\n\t\t}\n\n\t\tconst fallbackAi = availableAIAgents[0];\n\t\tif (fallbackAi) {\n\t\t\treturn {\n\t\t\t\ttype: \"ai\" as const,\n\t\t\t\tname: fallbackAi.name,\n\t\t\t\timage: fallbackAi.image ?? null,\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"fallback\" as const,\n\t\t\tname: supportFallbackName,\n\t\t\timage: null,\n\t\t};\n\t}, [knownTimelineItems, availableHumanAgents, availableAIAgents, text]);\n\n\tconst typingEntries = useConversationTyping(conversation.id, {\n\t\texcludeVisitorId: typing?.excludeVisitorId ?? visitor?.id ?? null,\n\t\texcludeUserId: typing?.excludeUserId ?? null,\n\t\texcludeAiAgentId: typing?.excludeAiAgentId ?? null,\n\t});\n\n\tconst typingParticipants = useMemo(\n\t\t() =>\n\t\t\tmapTypingEntriesToPreviewParticipants(typingEntries, {\n\t\t\t\tavailableHumanAgents,\n\t\t\t\tavailableAIAgents,\n\t\t\t\ttext,\n\t\t\t}),\n\t\t[typingEntries, availableHumanAgents, availableAIAgents, text]\n\t);\n\n\tconst primaryTypingParticipant = typingParticipants[0] ?? null;\n\n\tconst typingLabel = useMemo(() => {\n\t\tif (!primaryTypingParticipant) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn text(\"component.conversationButtonLink.typing\", {\n\t\t\tname: primaryTypingParticipant.name,\n\t\t});\n\t}, [primaryTypingParticipant, text]);\n\n\tconst typingState: ConversationPreviewTypingState = useMemo(\n\t\t() => ({\n\t\t\tparticipants: typingParticipants,\n\t\t\tprimaryParticipant: primaryTypingParticipant,\n\t\t\tlabel: typingLabel,\n\t\t\tisTyping: typingParticipants.length > 0,\n\t\t}),\n\t\t[typingParticipants, primaryTypingParticipant, typingLabel]\n\t);\n\n\tconst title = useMemo(() => {\n\t\tif (conversation.title) {\n\t\t\treturn conversation.title;\n\t\t}\n\n\t\tif (lastMessage?.content) {\n\t\t\treturn lastMessage.content;\n\t\t}\n\n\t\treturn text(\"component.conversationButtonLink.fallbackTitle\");\n\t}, [conversation.title, lastMessage?.content, text]);\n\n\treturn {\n\t\tconversation,\n\t\ttitle,\n\t\tlastMessage,\n\t\tassignedAgent,\n\t\ttyping: typingState,\n\t\ttimeline,\n\t};\n}\n"],"mappings":";;;;;;;;;AAoEA,SAAS,2BACR,OACA,UACC;AACD,MAAK,IAAI,QAAQ,MAAM,SAAS,GAAG,SAAS,GAAG,SAAS;EACvD,MAAM,OAAO,MAAM;AAEnB,MAAI,MAAM,SAAS,UAClB,QAAO;;AAIT,KAAI,UAAU,SAAS,UACtB,QAAO;AAGR,QAAO;;;;;;AAOR,SAAgB,uBACf,SAC+B;CAC/B,MAAM,EACL,cACA,uBAAuB,OACvB,uBAAuB,EAAE,EACzB,WACG;CACJ,MAAM,EAAE,sBAAsB,mBAAmB,YAAY,YAAY;CACzE,MAAM,OAAO,gBAAgB;CAE7B,MAAM,WAAW,6BAA6B,aAAa,IAAI,EAC9D,SAAS,sBACT,CAAC;CAEF,MAAM,sBAAsB,cAAc;AACzC,MAAI,SAAS,MAAM,SAAS,EAC3B,QAAO,SAAS;AAGjB,MAAI,qBAAqB,SAAS,EACjC,QAAO;AAGR,SAAO,EAAE;IACP,CAAC,SAAS,OAAO,qBAAqB,CAAC;CAE1C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cAAc;EACd,oBAAoB;EACpB,OAAO;EACP,UAAU,mBAAmB,SAAS;EACtC,GACD;EAAC;EAAoB;EAA0B;EAAY,CAC3D;AAcD,QAAO;EACN;EACA,OAda,cAAc;AAC3B,OAAI,aAAa,MAChB,QAAO,aAAa;AAGrB,OAAI,aAAa,QAChB,QAAO,YAAY;AAGpB,UAAO,KAAK,iDAAiD;KAC3D;GAAC,aAAa;GAAO,aAAa;GAAS;GAAK,CAAC;EAKnD;EACA;EACA,QAAQ;EACR;EACA"}
@@ -27,6 +27,7 @@ function useConversationTimelineItems(conversationId, options = {}) {
27
27
  }), [options.cursor, options.limit]);
28
28
  const { refetch: queryRefetch, isLoading: queryLoading, error } = useClientQuery({
29
29
  client,
30
+ queryKey: conversationId ? `timeline:${conversationId}:${baseArgs.limit}:${baseArgs.cursor ?? ""}` : void 0,
30
31
  queryFn: (instance, args) => {
31
32
  if (!conversationId) return Promise.resolve({
32
33
  items: [],
@@ -41,7 +42,7 @@ function useConversationTimelineItems(conversationId, options = {}) {
41
42
  },
42
43
  enabled: Boolean(conversationId) && (options.enabled ?? true),
43
44
  refetchInterval: options.refetchInterval ?? false,
44
- refetchOnWindowFocus: options.refetchOnWindowFocus ?? true,
45
+ refetchOnWindowFocus: options.refetchOnWindowFocus ?? false,
45
46
  refetchOnMount: selection.items.length === 0,
46
47
  initialArgs: baseArgs,
47
48
  dependencies: [
@@ -1 +1 @@
1
- {"version":3,"file":"use-conversation-timeline-items.js","names":["EMPTY_STATE: ConversationTimelineItemsState"],"sources":["../../src/hooks/use-conversation-timeline-items.ts"],"sourcesContent":["import type { ConversationTimelineItemsState } from \"@cossistant/core\";\nimport type {\n\tGetConversationTimelineItemsRequest,\n\tGetConversationTimelineItemsResponse,\n} from \"@cossistant/types/api/timeline-item\";\nimport { useCallback, useMemo } from \"react\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\nconst EMPTY_STATE: ConversationTimelineItemsState = {\n\titems: [],\n\thasNextPage: false,\n\tnextCursor: undefined,\n};\n\nconst DEFAULT_LIMIT = 50;\n\nconst NO_CONVERSATION_ID = \"__no_conversation__\" as const;\n\nexport type UseConversationTimelineItemsOptions = {\n\tlimit?: number;\n\tcursor?: string | null;\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationTimelineItemsResult =\n\tConversationTimelineItemsState & {\n\t\tisLoading: boolean;\n\t\terror: Error | null;\n\t\trefetch: (\n\t\t\targs?: Pick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">\n\t\t) => Promise<GetConversationTimelineItemsResponse | undefined>;\n\t\tfetchNextPage: () => Promise<\n\t\t\tGetConversationTimelineItemsResponse | undefined\n\t\t>;\n\t};\n\n/**\n * Fetches timeline items for a conversation and keeps the local store in sync\n * with pagination helpers.\n */\nexport function useConversationTimelineItems(\n\tconversationId: string | null | undefined,\n\toptions: UseConversationTimelineItemsOptions = {}\n): UseConversationTimelineItemsResult {\n\tconst { client } = useSupport();\n\tconst store = client.timelineItemsStore;\n\n\tif (!store) {\n\t\tthrow new Error(\n\t\t\t\"Timeline items store is not available on the client instance\"\n\t\t);\n\t}\n\n\tconst stableConversationId = conversationId ?? NO_CONVERSATION_ID;\n\n\tconst selection = useStoreSelector(\n\t\tstore,\n\t\t(state) => state.conversations[stableConversationId] ?? EMPTY_STATE\n\t);\n\n\tconst baseArgs = useMemo(\n\t\t() =>\n\t\t\t({\n\t\t\t\tlimit: options.limit ?? DEFAULT_LIMIT,\n\t\t\t\tcursor: options.cursor ?? undefined,\n\t\t\t}) satisfies Pick<\n\t\t\t\tGetConversationTimelineItemsRequest,\n\t\t\t\t\"limit\" | \"cursor\"\n\t\t\t>,\n\t\t[options.cursor, options.limit]\n\t);\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<\n\t\tGetConversationTimelineItemsResponse,\n\t\tPick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">\n\t>({\n\t\tclient,\n\t\tqueryFn: (instance, args) => {\n\t\t\tif (!conversationId) {\n\t\t\t\treturn Promise.resolve({\n\t\t\t\t\titems: [],\n\t\t\t\t\thasNextPage: false,\n\t\t\t\t\tnextCursor: null,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn instance.getConversationTimelineItems({\n\t\t\t\tconversationId,\n\t\t\t\tlimit: args?.limit ?? baseArgs.limit,\n\t\t\t\tcursor: args?.cursor ?? baseArgs.cursor ?? undefined,\n\t\t\t});\n\t\t},\n\t\tenabled: Boolean(conversationId) && (options.enabled ?? true),\n\t\trefetchInterval: options.refetchInterval ?? false,\n\t\trefetchOnWindowFocus: options.refetchOnWindowFocus ?? true,\n\t\trefetchOnMount: selection.items.length === 0,\n\t\tinitialArgs: baseArgs,\n\t\tdependencies: [\n\t\t\tstableConversationId,\n\t\t\tbaseArgs.limit,\n\t\t\tbaseArgs.cursor ?? null,\n\t\t],\n\t});\n\n\tconst refetch = useCallback(\n\t\t(args?: Pick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">) => {\n\t\t\tif (!conversationId) {\n\t\t\t\treturn Promise.resolve(undefined);\n\t\t\t}\n\n\t\t\treturn queryRefetch({\n\t\t\t\tlimit: baseArgs.limit,\n\t\t\t\tcursor: baseArgs.cursor,\n\t\t\t\t...args,\n\t\t\t});\n\t\t},\n\t\t[queryRefetch, baseArgs, conversationId]\n\t);\n\n\tconst fetchNextPage = useCallback(() => {\n\t\tif (!(selection.hasNextPage && selection.nextCursor)) {\n\t\t\treturn Promise.resolve(undefined);\n\t\t}\n\n\t\treturn refetch({ cursor: selection.nextCursor, limit: baseArgs.limit });\n\t}, [selection.hasNextPage, selection.nextCursor, refetch, baseArgs.limit]);\n\n\tconst isInitialLoad = selection.items.length === 0;\n\tconst isLoading = isInitialLoad ? queryLoading : false;\n\n\treturn {\n\t\titems: selection.items,\n\t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,QAAQ,SAAS;GACjB,GAAG;GACH,CAAC;IAEH;EAAC;EAAc;EAAU;EAAe,CACxC;CAED,MAAM,gBAAgB,kBAAkB;AACvC,MAAI,EAAE,UAAU,eAAe,UAAU,YACxC,QAAO,QAAQ,QAAQ,OAAU;AAGlC,SAAO,QAAQ;GAAE,QAAQ,UAAU;GAAY,OAAO,SAAS;GAAO,CAAC;IACrE;EAAC,UAAU;EAAa,UAAU;EAAY;EAAS,SAAS;EAAM,CAAC;CAG1E,MAAM,YADgB,UAAU,MAAM,WAAW,IACf,eAAe;AAEjD,QAAO;EACN,OAAO,UAAU;EACjB,aAAa,UAAU;EACvB,YAAY,UAAU;EACtB;EACA;EACA;EACA;EACA"}
1
+ {"version":3,"file":"use-conversation-timeline-items.js","names":["EMPTY_STATE: ConversationTimelineItemsState"],"sources":["../../src/hooks/use-conversation-timeline-items.ts"],"sourcesContent":["import type { ConversationTimelineItemsState } from \"@cossistant/core\";\nimport type {\n\tGetConversationTimelineItemsRequest,\n\tGetConversationTimelineItemsResponse,\n} from \"@cossistant/types/api/timeline-item\";\nimport { useCallback, useMemo } from \"react\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\nconst EMPTY_STATE: ConversationTimelineItemsState = {\n\titems: [],\n\thasNextPage: false,\n\tnextCursor: undefined,\n};\n\nconst DEFAULT_LIMIT = 50;\n\nconst NO_CONVERSATION_ID = \"__no_conversation__\" as const;\n\nexport type UseConversationTimelineItemsOptions = {\n\tlimit?: number;\n\tcursor?: string | null;\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationTimelineItemsResult =\n\tConversationTimelineItemsState & {\n\t\tisLoading: boolean;\n\t\terror: Error | null;\n\t\trefetch: (\n\t\t\targs?: Pick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">\n\t\t) => Promise<GetConversationTimelineItemsResponse | undefined>;\n\t\tfetchNextPage: () => Promise<\n\t\t\tGetConversationTimelineItemsResponse | undefined\n\t\t>;\n\t};\n\n/**\n * Fetches timeline items for a conversation and keeps the local store in sync\n * with pagination helpers.\n */\nexport function useConversationTimelineItems(\n\tconversationId: string | null | undefined,\n\toptions: UseConversationTimelineItemsOptions = {}\n): UseConversationTimelineItemsResult {\n\tconst { client } = useSupport();\n\tconst store = client.timelineItemsStore;\n\n\tif (!store) {\n\t\tthrow new Error(\n\t\t\t\"Timeline items store is not available on the client instance\"\n\t\t);\n\t}\n\n\tconst stableConversationId = conversationId ?? NO_CONVERSATION_ID;\n\n\tconst selection = useStoreSelector(\n\t\tstore,\n\t\t(state) => state.conversations[stableConversationId] ?? EMPTY_STATE\n\t);\n\n\tconst baseArgs = useMemo(\n\t\t() =>\n\t\t\t({\n\t\t\t\tlimit: options.limit ?? DEFAULT_LIMIT,\n\t\t\t\tcursor: options.cursor ?? undefined,\n\t\t\t}) satisfies Pick<\n\t\t\t\tGetConversationTimelineItemsRequest,\n\t\t\t\t\"limit\" | \"cursor\"\n\t\t\t>,\n\t\t[options.cursor, options.limit]\n\t);\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<\n\t\tGetConversationTimelineItemsResponse,\n\t\tPick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">\n\t>({\n\t\tclient,\n\t\tqueryKey: conversationId\n\t\t\t? `timeline:${conversationId}:${baseArgs.limit}:${baseArgs.cursor ?? \"\"}`\n\t\t\t: undefined,\n\t\tqueryFn: (instance, args) => {\n\t\t\tif (!conversationId) {\n\t\t\t\treturn Promise.resolve({\n\t\t\t\t\titems: [],\n\t\t\t\t\thasNextPage: false,\n\t\t\t\t\tnextCursor: null,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn instance.getConversationTimelineItems({\n\t\t\t\tconversationId,\n\t\t\t\tlimit: args?.limit ?? baseArgs.limit,\n\t\t\t\tcursor: args?.cursor ?? baseArgs.cursor ?? undefined,\n\t\t\t});\n\t\t},\n\t\tenabled: Boolean(conversationId) && (options.enabled ?? true),\n\t\trefetchInterval: options.refetchInterval ?? false,\n\t\trefetchOnWindowFocus: options.refetchOnWindowFocus ?? false,\n\t\trefetchOnMount: selection.items.length === 0,\n\t\tinitialArgs: baseArgs,\n\t\tdependencies: [\n\t\t\tstableConversationId,\n\t\t\tbaseArgs.limit,\n\t\t\tbaseArgs.cursor ?? null,\n\t\t],\n\t});\n\n\tconst refetch = useCallback(\n\t\t(args?: Pick<GetConversationTimelineItemsRequest, \"cursor\" | \"limit\">) => {\n\t\t\tif (!conversationId) {\n\t\t\t\treturn Promise.resolve(undefined);\n\t\t\t}\n\n\t\t\treturn queryRefetch({\n\t\t\t\tlimit: baseArgs.limit,\n\t\t\t\tcursor: baseArgs.cursor,\n\t\t\t\t...args,\n\t\t\t});\n\t\t},\n\t\t[queryRefetch, baseArgs, conversationId]\n\t);\n\n\tconst fetchNextPage = useCallback(() => {\n\t\tif (!(selection.hasNextPage && selection.nextCursor)) {\n\t\t\treturn Promise.resolve(undefined);\n\t\t}\n\n\t\treturn refetch({ cursor: selection.nextCursor, limit: baseArgs.limit });\n\t}, [selection.hasNextPage, selection.nextCursor, refetch, baseArgs.limit]);\n\n\tconst isInitialLoad = selection.items.length === 0;\n\tconst isLoading = isInitialLoad ? queryLoading : false;\n\n\treturn {\n\t\titems: selection.items,\n\t\thasNextPage: selection.hasNextPage,\n\t\tnextCursor: selection.nextCursor,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t\tfetchNextPage,\n\t};\n}\n"],"mappings":";;;;;;AAUA,MAAMA,cAA8C;CACnD,OAAO,EAAE;CACT,aAAa;CACb,YAAY;CACZ;AAED,MAAM,gBAAgB;AAEtB,MAAM,qBAAqB;;;;;AA0B3B,SAAgB,6BACf,gBACA,UAA+C,EAAE,EACZ;CACrC,MAAM,EAAE,WAAW,YAAY;CAC/B,MAAM,QAAQ,OA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U;EAAY;EAAS,SAAS;EAAM,CAAC;CAG1E,MAAM,YADgB,UAAU,MAAM,WAAW,IACf,eAAe;AAEjD,QAAO;EACN,OAAO,UAAU;EACjB,aAAa,UAAU;EACvB,YAAY,UAAU;EACtB;EACA;EACA;EACA;EACA"}
@@ -24,13 +24,14 @@ function useConversation(conversationId, options = {}) {
24
24
  const request = conversationId ? { conversationId } : void 0;
25
25
  const { refetch: queryRefetch, isLoading: queryLoading, error } = useClientQuery({
26
26
  client,
27
+ queryKey: conversationId ? `conversation:${conversationId}` : void 0,
27
28
  queryFn: (instance) => {
28
29
  if (!request) throw new Error("Conversation ID is required");
29
30
  return instance.getConversation(request);
30
31
  },
31
32
  enabled: Boolean(conversationId && (options.enabled ?? true)),
32
33
  refetchInterval: options.refetchInterval ?? false,
33
- refetchOnWindowFocus: options.refetchOnWindowFocus ?? true,
34
+ refetchOnWindowFocus: options.refetchOnWindowFocus ?? false,
34
35
  refetchOnMount: !conversation,
35
36
  initialArgs: request,
36
37
  dependencies: [conversationId ?? "null"]
@@ -1 +1 @@
1
- {"version":3,"file":"use-conversation.js","names":["request: GetConversationRequest | undefined"],"sources":["../../src/hooks/use-conversation.ts"],"sourcesContent":["import type {\n\tGetConversationRequest,\n\tGetConversationResponse,\n} from \"@cossistant/types/api/conversation\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\nexport type UseConversationOptions = {\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationResult = {\n\tconversation: GetConversationResponse[\"conversation\"] | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: (\n\t\targs?: GetConversationRequest\n\t) => Promise<GetConversationResponse | undefined>;\n};\n\n/**\n * Loads and caches a single conversation identified by `conversationId`.\n *\n * The hook keeps the conversations store hydrated, exposes a derived loading\n * state that respects cached data and provides a `refetch` helper to manually\n * refresh the thread.\n *\n * @param conversationId The conversation to retrieve; when `null` the hook\n * skips requests and returns `null` data.\n * @param options Additional react-query style controls for the request.\n */\nexport function useConversation(\n\tconversationId: string | null,\n\toptions: UseConversationOptions = {}\n): UseConversationResult {\n\tconst { client } = useSupport();\n\tconst store = client.conversationsStore;\n\n\tconst conversation = useStoreSelector(store, (state) => {\n\t\tif (!conversationId) {\n\t\t\treturn null;\n\t\t}\n\t\treturn state.byId[conversationId] ?? null;\n\t});\n\n\tconst request: GetConversationRequest | undefined = conversationId\n\t\t? { conversationId }\n\t\t: undefined;\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<GetConversationResponse, GetConversationRequest>({\n\t\tclient,\n\t\tqueryFn: (instance) => {\n\t\t\tif (!request) {\n\t\t\t\tthrow new Error(\"Conversation ID is required\");\n\t\t\t}\n\t\t\treturn instance.getConversation(request);\n\t\t},\n\t\tenabled: Boolean(conversationId && (options.enabled ?? true)),\n\t\trefetchInterval: options.refetchInterval ?? false,\n\t\trefetchOnWindowFocus: options.refetchOnWindowFocus ?? true,\n\t\trefetchOnMount: !conversation,\n\t\tinitialArgs: request,\n\t\tdependencies: [conversationId ?? \"null\"],\n\t});\n\n\tconst refetch = (args?: GetConversationRequest) => {\n\t\tif (!conversationId) {\n\t\t\treturn Promise.resolve(undefined);\n\t\t}\n\n\t\treturn queryRefetch({\n\t\t\tconversationId,\n\t\t\t...args,\n\t\t});\n\t};\n\n\tconst isLoading = conversation ? false : queryLoading;\n\n\treturn {\n\t\tconversation,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,SAAgB,gBACf,gBACA,UAAkC,EAAE,EACZ;CACxB,MAAM,EAAE,WAAW,YAAY;CAC/B,MAAM,QAAQ,OAAO;CAErB,MAAM,eAAe,iBAAiB,QAAQ,UAAU;AACvD,MAAI,CAAC,eACJ,QAAO;AAER,SAAO,MAAM,KAAK,mBAAmB;GACpC;CAEF,MAAMA,UAA8C,iBACjD,EAAE,gBAAgB,GAClB;CAEH,MAAM,EACL,SAAS,cACT,WAAW,cACX,UACG,eAAgE;EACnE;EACA,UAAU,aAAa;AACtB,OAAI,CAAC,QACJ,OAAM,IAAI,MAAM,8BAA8B;AAE/C,UAAO,SAAS,gBAAgB,QAAQ;;EAEzC,SAAS,QAAQ,mBAAmB,QAAQ,WAAW,MAAM;EAC7D,iBAAiB,QAAQ,mBAAmB;EAC5C,sBAAsB,QAAQ,wBAAwB;EACtD,gBAAgB,CAAC;EACjB,aAAa;EACb,cAAc,CAAC,kBAAkB,OAAO;EACxC,CAAC;CAEF,MAAM,WAAW,SAAkC;AAClD,MAAI,CAAC,eACJ,QAAO,QAAQ,QAAQ,OAAU;AAGlC,SAAO,aAAa;GACnB;GACA,GAAG;GACH,CAAC;;AAKH,QAAO;EACN;EACA,WAJiB,eAAe,QAAQ;EAKxC;EACA;EACA"}
1
+ {"version":3,"file":"use-conversation.js","names":["request: GetConversationRequest | undefined"],"sources":["../../src/hooks/use-conversation.ts"],"sourcesContent":["import type {\n\tGetConversationRequest,\n\tGetConversationResponse,\n} from \"@cossistant/types/api/conversation\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\nexport type UseConversationOptions = {\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationResult = {\n\tconversation: GetConversationResponse[\"conversation\"] | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: (\n\t\targs?: GetConversationRequest\n\t) => Promise<GetConversationResponse | undefined>;\n};\n\n/**\n * Loads and caches a single conversation identified by `conversationId`.\n *\n * The hook keeps the conversations store hydrated, exposes a derived loading\n * state that respects cached data and provides a `refetch` helper to manually\n * refresh the thread.\n *\n * @param conversationId The conversation to retrieve; when `null` the hook\n * skips requests and returns `null` data.\n * @param options Additional react-query style controls for the request.\n */\nexport function useConversation(\n\tconversationId: string | null,\n\toptions: UseConversationOptions = {}\n): UseConversationResult {\n\tconst { client } = useSupport();\n\tconst store = client.conversationsStore;\n\n\tconst conversation = useStoreSelector(store, (state) => {\n\t\tif (!conversationId) {\n\t\t\treturn null;\n\t\t}\n\t\treturn state.byId[conversationId] ?? null;\n\t});\n\n\tconst request: GetConversationRequest | undefined = conversationId\n\t\t? { conversationId }\n\t\t: undefined;\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<GetConversationResponse, GetConversationRequest>({\n\t\tclient,\n\t\tqueryKey: conversationId ? `conversation:${conversationId}` : undefined,\n\t\tqueryFn: (instance) => {\n\t\t\tif (!request) {\n\t\t\t\tthrow new Error(\"Conversation ID is required\");\n\t\t\t}\n\t\t\treturn instance.getConversation(request);\n\t\t},\n\t\tenabled: Boolean(conversationId && (options.enabled ?? true)),\n\t\trefetchInterval: options.refetchInterval ?? false,\n\t\trefetchOnWindowFocus: options.refetchOnWindowFocus ?? false,\n\t\trefetchOnMount: !conversation,\n\t\tinitialArgs: request,\n\t\tdependencies: [conversationId ?? \"null\"],\n\t});\n\n\tconst refetch = (args?: GetConversationRequest) => {\n\t\tif (!conversationId) {\n\t\t\treturn Promise.resolve(undefined);\n\t\t}\n\n\t\treturn queryRefetch({\n\t\t\tconversationId,\n\t\t\t...args,\n\t\t});\n\t};\n\n\tconst isLoading = conversation ? false : queryLoading;\n\n\treturn {\n\t\tconversation,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,SAAgB,gBACf,gBACA,UAAkC,EAAE,EACZ;CACxB,MAAM,EAAE,WAAW,YAAY;CAC/B,MAAM,QAAQ,OAAO;CAErB,MAAM,eAAe,iBAAiB,QAAQ,UAAU;AACvD,MAAI,CAAC,eACJ,QAAO;AAER,SAAO,MAAM,KAAK,mBAAmB;GACpC;CAEF,MAAMA,UAA8C,iBACjD,EAAE,gBAAgB,GAClB;CAEH,MAAM,EACL,SAAS,cACT,WAAW,cACX,UACG,eAAgE;EACnE;EACA,UAAU,iBAAiB,gBAAgB,mBAAmB;EAC9D,UAAU,aAAa;AACtB,OAAI,CAAC,QACJ,OAAM,IAAI,MAAM,8BAA8B;AAE/C,UAAO,SAAS,gBAAgB,QAAQ;;EAEzC,SAAS,QAAQ,mBAAmB,QAAQ,WAAW,MAAM;EAC7D,iBAAiB,QAAQ,mBAAmB;EAC5C,sBAAsB,QAAQ,wBAAwB;EACtD,gBAAgB,CAAC;EACjB,aAAa;EACb,cAAc,CAAC,kBAAkB,OAAO;EACxC,CAAC;CAEF,MAAM,WAAW,SAAkC;AAClD,MAAI,CAAC,eACJ,QAAO,QAAQ,QAAQ,OAAU;AAGlC,SAAO,aAAa;GACnB;GACA,GAAG;GACH,CAAC;;AAKH,QAAO;EACN;EACA,WAJiB,eAAe,QAAQ;EAKxC;EACA;EACA"}
@@ -44,6 +44,7 @@ function useConversations(options = {}) {
44
44
  }), areSelectionsEqual);
45
45
  const { refetch: queryRefetch, isLoading: queryLoading, error } = useClientQuery({
46
46
  client,
47
+ queryKey: `conversations:${limit ?? ""}:${page ?? ""}:${status ?? ""}:${orderBy ?? ""}:${order ?? ""}`,
47
48
  queryFn: (instance, args) => instance.listConversations({
48
49
  ...requestDefaults,
49
50
  ...args
@@ -1 +1 @@
1
- {"version":3,"file":"use-conversations.js","names":[],"sources":["../../src/hooks/use-conversations.ts"],"sourcesContent":["import type { ConversationPagination } from \"@cossistant/core\";\nimport type {\n\tListConversationsRequest,\n\tListConversationsResponse,\n} from \"@cossistant/types/api/conversation\";\nimport { useCallback, useMemo } from \"react\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\ntype ConversationsSelection = {\n\tconversations: ListConversationsResponse[\"conversations\"];\n\tpagination: ConversationPagination | null;\n};\n\nfunction areSelectionsEqual(\n\ta: ConversationsSelection,\n\tb: ConversationsSelection\n): boolean {\n\tconst samePagination =\n\t\ta.pagination === b.pagination ||\n\t\t(Boolean(a.pagination) &&\n\t\t\tBoolean(b.pagination) &&\n\t\t\ta.pagination?.page === b.pagination?.page &&\n\t\t\ta.pagination?.limit === b.pagination?.limit &&\n\t\t\ta.pagination?.total === b.pagination?.total &&\n\t\t\ta.pagination?.totalPages === b.pagination?.totalPages &&\n\t\t\ta.pagination?.hasMore === b.pagination?.hasMore);\n\n\tif (!samePagination) {\n\t\treturn false;\n\t}\n\n\tif (a.conversations.length !== b.conversations.length) {\n\t\treturn false;\n\t}\n\n\treturn a.conversations.every(\n\t\t(conversation, index) => conversation === b.conversations[index]\n\t);\n}\n\nexport type UseConversationsOptions = Partial<\n\tOmit<ListConversationsRequest, \"visitorId\">\n> & {\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationsResult = {\n\tconversations: ListConversationsResponse[\"conversations\"];\n\tpagination: ConversationPagination | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: (\n\t\targs?: Partial<ListConversationsRequest>\n\t) => Promise<ListConversationsResponse | undefined>;\n};\n\n/**\n * Fetches and subscribes to the authenticated visitor's conversation list.\n *\n * The hook keeps the store in sync with the REST client and exposes\n * pagination metadata plus a refetch helper for manual refreshes. The\n * `options` mirror the public API filters so the UI can request slices of the\n * inbox without duplicating data-fetching logic.\n *\n * @param options Filtering and lifecycle controls for the query.\n * @returns Conversations, pagination data, loading state, and a refetch\n * helper.\n */\nexport function useConversations(\n\toptions: UseConversationsOptions = {}\n): UseConversationsResult {\n\tconst { client } = useSupport();\n\n\tconst {\n\t\tlimit,\n\t\tpage,\n\t\torder,\n\t\torderBy,\n\t\tstatus,\n\t\tenabled = true,\n\t\trefetchInterval = false,\n\t\trefetchOnWindowFocus = true,\n\t} = options;\n\n\tconst requestDefaults = useMemo(\n\t\t() => ({ limit, page, status, orderBy, order }),\n\t\t[limit, page, status, orderBy, order]\n\t);\n\n\tconst store = client.conversationsStore;\n\n\tconst selection = useStoreSelector(\n\t\tstore,\n\t\t(state): ConversationsSelection => ({\n\t\t\tconversations: state.ids\n\t\t\t\t.map((id) => state.byId[id])\n\t\t\t\t.filter(\n\t\t\t\t\t(\n\t\t\t\t\t\tconversation\n\t\t\t\t\t): conversation is ListConversationsResponse[\"conversations\"][number] =>\n\t\t\t\t\t\tBoolean(conversation)\n\t\t\t\t),\n\t\t\tpagination: state.pagination,\n\t\t}),\n\t\tareSelectionsEqual\n\t);\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<\n\t\tListConversationsResponse,\n\t\tPartial<ListConversationsRequest>\n\t>({\n\t\tclient,\n\t\tqueryFn: (instance, args) =>\n\t\t\tinstance.listConversations({\n\t\t\t\t...requestDefaults,\n\t\t\t\t...args,\n\t\t\t}),\n\t\tenabled,\n\t\trefetchInterval,\n\t\trefetchOnWindowFocus,\n\t\trefetchOnMount: selection.conversations.length === 0,\n\t\tinitialArgs: requestDefaults,\n\t\tdependencies: [limit, page, status, orderBy, order],\n\t});\n\n\tconst refetch = useCallback(\n\t\t(args?: Partial<ListConversationsRequest>) =>\n\t\t\tqueryRefetch({\n\t\t\t\t...requestDefaults,\n\t\t\t\t...args,\n\t\t\t}),\n\t\t[queryRefetch, requestDefaults]\n\t);\n\n\tconst isInitialLoad = selection.conversations.length === 0;\n\tconst isLoading = isInitialLoad ? queryLoading : false;\n\n\treturn {\n\t\tconversations: selection.conversations,\n\t\tpagination: selection.pagination,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n"],"mappings":";;;;;;AAeA,SAAS,mBACR,GACA,GACU;AAWV,KAAI,EATH,EAAE,eAAe,EAAE,cAClB,QAAQ,EAAE,WAAW,IACrB,QAAQ,EAAE,WAAW,IACrB,EAAE,YAAY,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"}
1
+ {"version":3,"file":"use-conversations.js","names":[],"sources":["../../src/hooks/use-conversations.ts"],"sourcesContent":["import type { ConversationPagination } from \"@cossistant/core\";\nimport type {\n\tListConversationsRequest,\n\tListConversationsResponse,\n} from \"@cossistant/types/api/conversation\";\nimport { useCallback, useMemo } from \"react\";\nimport { useSupport } from \"../provider\";\nimport { useStoreSelector } from \"./private/store/use-store-selector\";\nimport { useClientQuery } from \"./private/use-client-query\";\n\ntype ConversationsSelection = {\n\tconversations: ListConversationsResponse[\"conversations\"];\n\tpagination: ConversationPagination | null;\n};\n\nfunction areSelectionsEqual(\n\ta: ConversationsSelection,\n\tb: ConversationsSelection\n): boolean {\n\tconst samePagination =\n\t\ta.pagination === b.pagination ||\n\t\t(Boolean(a.pagination) &&\n\t\t\tBoolean(b.pagination) &&\n\t\t\ta.pagination?.page === b.pagination?.page &&\n\t\t\ta.pagination?.limit === b.pagination?.limit &&\n\t\t\ta.pagination?.total === b.pagination?.total &&\n\t\t\ta.pagination?.totalPages === b.pagination?.totalPages &&\n\t\t\ta.pagination?.hasMore === b.pagination?.hasMore);\n\n\tif (!samePagination) {\n\t\treturn false;\n\t}\n\n\tif (a.conversations.length !== b.conversations.length) {\n\t\treturn false;\n\t}\n\n\treturn a.conversations.every(\n\t\t(conversation, index) => conversation === b.conversations[index]\n\t);\n}\n\nexport type UseConversationsOptions = Partial<\n\tOmit<ListConversationsRequest, \"visitorId\">\n> & {\n\tenabled?: boolean;\n\trefetchInterval?: number | false;\n\trefetchOnWindowFocus?: boolean;\n};\n\nexport type UseConversationsResult = {\n\tconversations: ListConversationsResponse[\"conversations\"];\n\tpagination: ConversationPagination | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: (\n\t\targs?: Partial<ListConversationsRequest>\n\t) => Promise<ListConversationsResponse | undefined>;\n};\n\n/**\n * Fetches and subscribes to the authenticated visitor's conversation list.\n *\n * The hook keeps the store in sync with the REST client and exposes\n * pagination metadata plus a refetch helper for manual refreshes. The\n * `options` mirror the public API filters so the UI can request slices of the\n * inbox without duplicating data-fetching logic.\n *\n * @param options Filtering and lifecycle controls for the query.\n * @returns Conversations, pagination data, loading state, and a refetch\n * helper.\n */\nexport function useConversations(\n\toptions: UseConversationsOptions = {}\n): UseConversationsResult {\n\tconst { client } = useSupport();\n\n\tconst {\n\t\tlimit,\n\t\tpage,\n\t\torder,\n\t\torderBy,\n\t\tstatus,\n\t\tenabled = true,\n\t\trefetchInterval = false,\n\t\trefetchOnWindowFocus = true,\n\t} = options;\n\n\tconst requestDefaults = useMemo(\n\t\t() => ({ limit, page, status, orderBy, order }),\n\t\t[limit, page, status, orderBy, order]\n\t);\n\n\tconst store = client.conversationsStore;\n\n\tconst selection = useStoreSelector(\n\t\tstore,\n\t\t(state): ConversationsSelection => ({\n\t\t\tconversations: state.ids\n\t\t\t\t.map((id) => state.byId[id])\n\t\t\t\t.filter(\n\t\t\t\t\t(\n\t\t\t\t\t\tconversation\n\t\t\t\t\t): conversation is ListConversationsResponse[\"conversations\"][number] =>\n\t\t\t\t\t\tBoolean(conversation)\n\t\t\t\t),\n\t\t\tpagination: state.pagination,\n\t\t}),\n\t\tareSelectionsEqual\n\t);\n\n\tconst {\n\t\trefetch: queryRefetch,\n\t\tisLoading: queryLoading,\n\t\terror,\n\t} = useClientQuery<\n\t\tListConversationsResponse,\n\t\tPartial<ListConversationsRequest>\n\t>({\n\t\tclient,\n\t\tqueryKey: `conversations:${limit ?? \"\"}:${page ?? \"\"}:${status ?? \"\"}:${orderBy ?? \"\"}:${order ?? \"\"}`,\n\t\tqueryFn: (instance, args) =>\n\t\t\tinstance.listConversations({\n\t\t\t\t...requestDefaults,\n\t\t\t\t...args,\n\t\t\t}),\n\t\tenabled,\n\t\trefetchInterval,\n\t\trefetchOnWindowFocus,\n\t\trefetchOnMount: selection.conversations.length === 0,\n\t\tinitialArgs: requestDefaults,\n\t\tdependencies: [limit, page, status, orderBy, order],\n\t});\n\n\tconst refetch = useCallback(\n\t\t(args?: Partial<ListConversationsRequest>) =>\n\t\t\tqueryRefetch({\n\t\t\t\t...requestDefaults,\n\t\t\t\t...args,\n\t\t\t}),\n\t\t[queryRefetch, requestDefaults]\n\t);\n\n\tconst isInitialLoad = selection.conversations.length === 0;\n\tconst isLoading = isInitialLoad ? queryLoading : false;\n\n\treturn {\n\t\tconversations: selection.conversations,\n\t\tpagination: selection.pagination,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n"],"mappings":";;;;;;AAeA,SAAS,mBACR,GACA,GACU;AAWV,KAAI,EATH,EAAE,eAAe,EAAE,cAClB,QAAQ,EAAE,WAAW,IACrB,QAAQ,EAAE,WAAW,IACrB,EAAE,YAAY,SAAS,EAAE,YAAY,QACrC,EAAE,YAAY,UAAU,EAAE,YAAY,SACtC,EAAE,YAAY,UAAU,EAAE,YAAY,SACtC,EAAE,YAAY,eAAe,EAAE,YAAY,cAC3C,EAAE,YAAY,YAAY,EAAE,YAAY,SAGzC,QAAO;AAGR,KAAI,EAAE,cAAc,WAAW,EAAE,cAAc,OAC9C,QAAO;AAGR,QAAO,EAAE,cAAc,OACrB,cAAc,UAAU,iBAAiB,EAAE,cAAc,OAC1D;;;;;;;;;;;;;;AAiCF,SAAgB,iBACf,UAAmC,EAAE,EACZ;CACzB,MAAM,EAAE,WAAW,YAAY;CAE/B,MAAM,EACL,OACA,MACA,OACA,SACA,QACA,UAAU,MACV,kBAAkB,OAClB,uBAAuB,SACpB;CAEJ,MAAM,kBAAkB,eAChB;EAAE;EAAO;EAAM;EAAQ;EAAS;EAAO,GAC9C;EAAC;EAAO;EAAM;EAAQ;EAAS;EAAM,CACrC;CAED,MAAM,QAAQ,OAAO;CAErB,MAAM,YAAY,iBACjB,QACC,WAAmC;EACnC,eAAe,MAAM,IACnB,KAAK,OAAO,MAAM,KAAK,IAAI,CAC3B,QAEC,iBAEA,QAAQ,aAAa,CACtB;EACF,YAAY,MAAM;EAClB,GACD,mBACA;CAED,MAAM,EACL,SAAS,cACT,WAAW,cACX,UACG,eAGF;EACD;EACA,UAAU,iBAAiB,SAAS,GAAG,GAAG,QAAQ,GAAG,GAAG,UAAU,GAAG,GAAG,WAAW,GAAG,GAAG,SAAS;EAClG,UAAU,UAAU,SACnB,SAAS,kBAAkB;GAC1B,GAAG;GACH,GAAG;GACH,CAAC;EACH;EACA;EACA;EACA,gBAAgB,UAAU,cAAc,WAAW;EACnD,aAAa;EACb,cAAc;GAAC;GAAO;GAAM;GAAQ;GAAS;GAAM;EACnD,CAAC;CAEF,MAAM,UAAU,aACd,SACA,aAAa;EACZ,GAAG;EACH,GAAG;EACH,CAAC,EACH,CAAC,cAAc,gBAAgB,CAC/B;CAGD,MAAM,YADgB,UAAU,cAAc,WAAW,IACvB,eAAe;AAEjD,QAAO;EACN,eAAe,UAAU;EACzB,YAAY,UAAU;EACtB;EACA;EACA;EACA"}
@@ -0,0 +1,55 @@
1
+ import { TimelinePartFile, TimelinePartImage } from "../timeline-item.js";
2
+ import { CossistantClient } from "@cossistant/core";
3
+
4
+ //#region src/hooks/use-file-upload.d.ts
5
+ type FileUploadPart = TimelinePartImage | TimelinePartFile;
6
+ type UseFileUploadOptions = {
7
+ /**
8
+ * Optional Cossistant client instance.
9
+ * If not provided, uses the client from SupportProvider context.
10
+ */
11
+ client?: CossistantClient;
12
+ };
13
+ type UseFileUploadReturn = {
14
+ /**
15
+ * Upload files and return timeline parts ready to include in a message.
16
+ * Files are uploaded to S3 in parallel.
17
+ */
18
+ uploadFiles: (files: File[], conversationId: string) => Promise<FileUploadPart[]>;
19
+ /**
20
+ * Whether an upload is currently in progress.
21
+ */
22
+ isUploading: boolean;
23
+ /**
24
+ * Upload progress (0-100). Updates as files complete.
25
+ */
26
+ progress: number;
27
+ /**
28
+ * Error from the most recent upload attempt, if any.
29
+ */
30
+ error: Error | null;
31
+ /**
32
+ * Reset the upload state (clear errors and progress).
33
+ */
34
+ reset: () => void;
35
+ };
36
+ /**
37
+ * Hook for uploading files to S3 for inclusion in chat messages.
38
+ * Handles validation, upload progress tracking, and error management.
39
+ *
40
+ * @example
41
+ * ```tsx
42
+ * const { uploadFiles, isUploading, error } = useFileUpload();
43
+ *
44
+ * const handleSend = async () => {
45
+ * if (files.length > 0) {
46
+ * const parts = await uploadFiles(files, conversationId);
47
+ * // Include parts in message...
48
+ * }
49
+ * };
50
+ * ```
51
+ */
52
+ declare function useFileUpload(options?: UseFileUploadOptions): UseFileUploadReturn;
53
+ //#endregion
54
+ export { FileUploadPart, UseFileUploadOptions, UseFileUploadReturn, useFileUpload };
55
+ //# sourceMappingURL=use-file-upload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-file-upload.d.ts","names":[],"sources":["../../src/hooks/use-file-upload.ts"],"sourcesContent":[],"mappings":";;;;KAeY,cAAA,GAAiB,oBAAoB;KAErC,oBAAA;EAFA;AAEZ;AAQA;;EAQc,MAAA,CAAA,EAXJ,gBAWI;CAAR;AAeE,KAvBI,mBAAA,GAuBJ;EAAK;AAwBb;;;uBAzCS,mCAEH,QAAQ;;;;;;;;;;;;SAeN;;;;;;;;;;;;;;;;;;;;;;iBAwBQ,aAAA,WACN,uBACP"}
@@ -0,0 +1,100 @@
1
+ "use client";
2
+
3
+
4
+ import { useSupport } from "../provider.js";
5
+ import { useCallback, useState } from "react";
6
+ import { MAX_FILES_PER_MESSAGE, isImageMimeType, validateFiles } from "@cossistant/core";
7
+
8
+ //#region src/hooks/use-file-upload.ts
9
+ /**
10
+ * Hook for uploading files to S3 for inclusion in chat messages.
11
+ * Handles validation, upload progress tracking, and error management.
12
+ *
13
+ * @example
14
+ * ```tsx
15
+ * const { uploadFiles, isUploading, error } = useFileUpload();
16
+ *
17
+ * const handleSend = async () => {
18
+ * if (files.length > 0) {
19
+ * const parts = await uploadFiles(files, conversationId);
20
+ * // Include parts in message...
21
+ * }
22
+ * };
23
+ * ```
24
+ */
25
+ function useFileUpload(options = {}) {
26
+ const { client: contextClient } = useSupport();
27
+ const client = options.client ?? contextClient;
28
+ const [isUploading, setIsUploading] = useState(false);
29
+ const [progress, setProgress] = useState(0);
30
+ const [error, setError] = useState(null);
31
+ const reset = useCallback(() => {
32
+ setIsUploading(false);
33
+ setProgress(0);
34
+ setError(null);
35
+ }, []);
36
+ return {
37
+ uploadFiles: useCallback(async (files, conversationId) => {
38
+ if (files.length === 0) return [];
39
+ const validationError = validateFiles(files);
40
+ if (validationError) {
41
+ const err = new Error(validationError);
42
+ setError(err);
43
+ throw err;
44
+ }
45
+ if (files.length > MAX_FILES_PER_MESSAGE) {
46
+ const err = /* @__PURE__ */ new Error(`Cannot upload more than ${MAX_FILES_PER_MESSAGE} files at once`);
47
+ setError(err);
48
+ throw err;
49
+ }
50
+ setIsUploading(true);
51
+ setProgress(0);
52
+ setError(null);
53
+ try {
54
+ const totalFiles = files.length;
55
+ let completedFiles = 0;
56
+ const uploadPromises = files.map(async (file) => {
57
+ const uploadInfo = await client.generateUploadUrl({
58
+ conversationId,
59
+ contentType: file.type,
60
+ fileName: file.name
61
+ });
62
+ await client.uploadFile(file, uploadInfo.uploadUrl, file.type);
63
+ completedFiles += 1;
64
+ setProgress(Math.round(completedFiles / totalFiles * 100));
65
+ if (isImageMimeType(file.type)) return {
66
+ type: "image",
67
+ url: uploadInfo.publicUrl,
68
+ mediaType: file.type,
69
+ fileName: file.name,
70
+ size: file.size
71
+ };
72
+ return {
73
+ type: "file",
74
+ url: uploadInfo.publicUrl,
75
+ mediaType: file.type,
76
+ fileName: file.name,
77
+ size: file.size
78
+ };
79
+ });
80
+ const parts = await Promise.all(uploadPromises);
81
+ setIsUploading(false);
82
+ setProgress(100);
83
+ return parts;
84
+ } catch (err) {
85
+ const normalizedError = err instanceof Error ? err : /* @__PURE__ */ new Error("Upload failed");
86
+ setError(normalizedError);
87
+ setIsUploading(false);
88
+ throw normalizedError;
89
+ }
90
+ }, [client]),
91
+ isUploading,
92
+ progress,
93
+ error,
94
+ reset
95
+ };
96
+ }
97
+
98
+ //#endregion
99
+ export { useFileUpload };
100
+ //# sourceMappingURL=use-file-upload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-file-upload.js","names":[],"sources":["../../src/hooks/use-file-upload.ts"],"sourcesContent":["\"use client\";\n\nimport type { CossistantClient } from \"@cossistant/core\";\nimport {\n\tisImageMimeType,\n\tMAX_FILES_PER_MESSAGE,\n\tvalidateFiles,\n} from \"@cossistant/core\";\nimport type {\n\tTimelinePartFile,\n\tTimelinePartImage,\n} from \"@cossistant/types/api/timeline-item\";\nimport { useCallback, useState } from \"react\";\nimport { useSupport } from \"../provider\";\n\nexport type FileUploadPart = TimelinePartImage | TimelinePartFile;\n\nexport type UseFileUploadOptions = {\n\t/**\n\t * Optional Cossistant client instance.\n\t * If not provided, uses the client from SupportProvider context.\n\t */\n\tclient?: CossistantClient;\n};\n\nexport type UseFileUploadReturn = {\n\t/**\n\t * Upload files and return timeline parts ready to include in a message.\n\t * Files are uploaded to S3 in parallel.\n\t */\n\tuploadFiles: (\n\t\tfiles: File[],\n\t\tconversationId: string\n\t) => Promise<FileUploadPart[]>;\n\n\t/**\n\t * Whether an upload is currently in progress.\n\t */\n\tisUploading: boolean;\n\n\t/**\n\t * Upload progress (0-100). Updates as files complete.\n\t */\n\tprogress: number;\n\n\t/**\n\t * Error from the most recent upload attempt, if any.\n\t */\n\terror: Error | null;\n\n\t/**\n\t * Reset the upload state (clear errors and progress).\n\t */\n\treset: () => void;\n};\n\n/**\n * Hook for uploading files to S3 for inclusion in chat messages.\n * Handles validation, upload progress tracking, and error management.\n *\n * @example\n * ```tsx\n * const { uploadFiles, isUploading, error } = useFileUpload();\n *\n * const handleSend = async () => {\n * if (files.length > 0) {\n * const parts = await uploadFiles(files, conversationId);\n * // Include parts in message...\n * }\n * };\n * ```\n */\nexport function useFileUpload(\n\toptions: UseFileUploadOptions = {}\n): UseFileUploadReturn {\n\tconst { client: contextClient } = useSupport();\n\tconst client = options.client ?? contextClient;\n\n\tconst [isUploading, setIsUploading] = useState(false);\n\tconst [progress, setProgress] = useState(0);\n\tconst [error, setError] = useState<Error | null>(null);\n\n\tconst reset = useCallback(() => {\n\t\tsetIsUploading(false);\n\t\tsetProgress(0);\n\t\tsetError(null);\n\t}, []);\n\n\tconst uploadFiles = useCallback(\n\t\tasync (\n\t\t\tfiles: File[],\n\t\t\tconversationId: string\n\t\t): Promise<FileUploadPart[]> => {\n\t\t\tif (files.length === 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\t// Validate files before upload\n\t\t\tconst validationError = validateFiles(files);\n\t\t\tif (validationError) {\n\t\t\t\tconst err = new Error(validationError);\n\t\t\t\tsetError(err);\n\t\t\t\tthrow err;\n\t\t\t}\n\n\t\t\tif (files.length > MAX_FILES_PER_MESSAGE) {\n\t\t\t\tconst err = new Error(\n\t\t\t\t\t`Cannot upload more than ${MAX_FILES_PER_MESSAGE} files at once`\n\t\t\t\t);\n\t\t\t\tsetError(err);\n\t\t\t\tthrow err;\n\t\t\t}\n\n\t\t\tsetIsUploading(true);\n\t\t\tsetProgress(0);\n\t\t\tsetError(null);\n\n\t\t\ttry {\n\t\t\t\tconst totalFiles = files.length;\n\t\t\t\tlet completedFiles = 0;\n\n\t\t\t\t// Upload files in parallel\n\t\t\t\tconst uploadPromises = files.map(async (file) => {\n\t\t\t\t\t// Generate presigned URL\n\t\t\t\t\tconst uploadInfo = await client.generateUploadUrl({\n\t\t\t\t\t\tconversationId,\n\t\t\t\t\t\tcontentType: file.type,\n\t\t\t\t\t\tfileName: file.name,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Upload file to S3\n\t\t\t\t\tawait client.uploadFile(file, uploadInfo.uploadUrl, file.type);\n\n\t\t\t\t\t// Update progress\n\t\t\t\t\tcompletedFiles += 1;\n\t\t\t\t\tsetProgress(Math.round((completedFiles / totalFiles) * 100));\n\n\t\t\t\t\t// Return timeline part based on file type\n\t\t\t\t\tconst isImage = isImageMimeType(file.type);\n\n\t\t\t\t\tif (isImage) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\ttype: \"image\" as const,\n\t\t\t\t\t\t\turl: uploadInfo.publicUrl,\n\t\t\t\t\t\t\tmediaType: file.type,\n\t\t\t\t\t\t\tfileName: file.name,\n\t\t\t\t\t\t\tsize: file.size,\n\t\t\t\t\t\t} satisfies TimelinePartImage;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"file\" as const,\n\t\t\t\t\t\turl: uploadInfo.publicUrl,\n\t\t\t\t\t\tmediaType: file.type,\n\t\t\t\t\t\tfileName: file.name,\n\t\t\t\t\t\tsize: file.size,\n\t\t\t\t\t} satisfies TimelinePartFile;\n\t\t\t\t});\n\n\t\t\t\tconst parts = await Promise.all(uploadPromises);\n\n\t\t\t\tsetIsUploading(false);\n\t\t\t\tsetProgress(100);\n\n\t\t\t\treturn parts;\n\t\t\t} catch (err) {\n\t\t\t\tconst normalizedError =\n\t\t\t\t\terr instanceof Error ? err : new Error(\"Upload failed\");\n\t\t\t\tsetError(normalizedError);\n\t\t\t\tsetIsUploading(false);\n\t\t\t\tthrow normalizedError;\n\t\t\t}\n\t\t},\n\t\t[client]\n\t);\n\n\treturn {\n\t\tuploadFiles,\n\t\tisUploading,\n\t\tprogress,\n\t\terror,\n\t\treset,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,cACf,UAAgC,EAAE,EACZ;CACtB,MAAM,EAAE,QAAQ,kBAAkB,YAAY;CAC9C,MAAM,SAAS,QAAQ,UAAU;CAEjC,MAAM,CAAC,aAAa,kBAAkB,SAAS,MAAM;CACrD,MAAM,CAAC,UAAU,eAAe,SAAS,EAAE;CAC3C,MAAM,CAAC,OAAO,YAAY,SAAuB,KAAK;CAEtD,MAAM,QAAQ,kBAAkB;AAC/B,iBAAe,MAAM;AACrB,cAAY,EAAE;AACd,WAAS,KAAK;IACZ,EAAE,CAAC;AA0FN,QAAO;EACN,aAzFmB,YACnB,OACC,OACA,mBAC+B;AAC/B,OAAI,MAAM,WAAW,EACpB,QAAO,EAAE;GAIV,MAAM,kBAAkB,cAAc,MAAM;AAC5C,OAAI,iBAAiB;IACpB,MAAM,MAAM,IAAI,MAAM,gBAAgB;AACtC,aAAS,IAAI;AACb,UAAM;;AAGP,OAAI,MAAM,SAAS,uBAAuB;IACzC,MAAM,sBAAM,IAAI,MACf,2BAA2B,sBAAsB,gBACjD;AACD,aAAS,IAAI;AACb,UAAM;;AAGP,kBAAe,KAAK;AACpB,eAAY,EAAE;AACd,YAAS,KAAK;AAEd,OAAI;IACH,MAAM,aAAa,MAAM;IACzB,IAAI,iBAAiB;IAGrB,MAAM,iBAAiB,MAAM,IAAI,OAAO,SAAS;KAEhD,MAAM,aAAa,MAAM,OAAO,kBAAkB;MACjD;MACA,aAAa,KAAK;MAClB,UAAU,KAAK;MACf,CAAC;AAGF,WAAM,OAAO,WAAW,MAAM,WAAW,WAAW,KAAK,KAAK;AAG9D,uBAAkB;AAClB,iBAAY,KAAK,MAAO,iBAAiB,aAAc,IAAI,CAAC;AAK5D,SAFgB,gBAAgB,KAAK,KAAK,CAGzC,QAAO;MACN,MAAM;MACN,KAAK,WAAW;MAChB,WAAW,KAAK;MAChB,UAAU,KAAK;MACf,MAAM,KAAK;MACX;AAGF,YAAO;MACN,MAAM;MACN,KAAK,WAAW;MAChB,WAAW,KAAK;MAChB,UAAU,KAAK;MACf,MAAM,KAAK;MACX;MACA;IAEF,MAAM,QAAQ,MAAM,QAAQ,IAAI,eAAe;AAE/C,mBAAe,MAAM;AACrB,gBAAY,IAAI;AAEhB,WAAO;YACC,KAAK;IACb,MAAM,kBACL,eAAe,QAAQ,sBAAM,IAAI,MAAM,gBAAgB;AACxD,aAAS,gBAAgB;AACzB,mBAAe,MAAM;AACrB,UAAM;;KAGR,CAAC,OAAO,CACR;EAIA;EACA;EACA;EACA;EACA"}
@@ -1,5 +1,6 @@
1
1
  import { TimelineItem } from "../timeline-item.js";
2
2
  import { UseMultimodalInputOptions } from "./private/use-multimodal-input.js";
3
+ import { AnyRealtimeEvent } from "../realtime-events.js";
3
4
  import { CossistantClient } from "@cossistant/core";
4
5
 
5
6
  //#region src/hooks/use-message-composer.d.ts
@@ -35,12 +36,22 @@ type UseMessageComposerOptions = {
35
36
  * File upload options (max size, allowed types, etc.)
36
37
  */
37
38
  fileOptions?: Pick<UseMultimodalInputOptions, "maxFileSize" | "maxFiles" | "allowedFileTypes">;
39
+ /**
40
+ * Optional WebSocket send function for real-time typing events.
41
+ * When provided, typing indicators are sent via WebSocket for better performance.
42
+ */
43
+ realtimeSend?: ((event: AnyRealtimeEvent) => void) | null;
44
+ /**
45
+ * Whether the WebSocket connection is currently established.
46
+ */
47
+ isRealtimeConnected?: boolean;
38
48
  };
39
49
  type UseMessageComposerReturn = {
40
50
  message: string;
41
51
  files: File[];
42
52
  error: Error | null;
43
53
  isSubmitting: boolean;
54
+ isUploading: boolean;
44
55
  canSubmit: boolean;
45
56
  setMessage: (message: string) => void;
46
57
  addFiles: (files: File[]) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"use-message-composer.d.ts","names":[],"sources":["../../src/hooks/use-message-composer.ts"],"sourcesContent":[],"mappings":";;;;;KAUY,yBAAA;;AAAZ;;EAewB,MAAA,EAXf,gBAWe;EAiBL;;;;EAWP,cAAA,EAAA,MAAA,GAAA,IAAwB;EAG5B;;;EASe,oBAAA,CAAA,EAxCC,YAwCD,EAAA;EAwCP;;;;;;;;;;;;;oBA/DG;;;;gBAKJ,KACb;;KAKU,wBAAA;;SAGJ;SACA;;;;oBAQW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwCH,kBAAA,UACN,4BACP"}
1
+ {"version":3,"file":"use-message-composer.d.ts","names":[],"sources":["../../src/hooks/use-message-composer.ts"],"sourcesContent":[],"mappings":";;;;;;KAWY,yBAAA;;AAAZ;;EAewB,MAAA,EAXf,gBAWe;EAiBL;;;;EAcsB,cAAA,EAAA,MAAA,GAAA,IAAA;EAQ7B;;;EAaO,oBAAA,CAAA,EApDK,YAoDL,EAAA;EAAI;AAwCvB;;;;;;;;;;;;oBA3EmB;;;;gBAKJ,KACb;;;;;0BAQuB;;;;;;KAQb,wBAAA;;SAGJ;SACA;;;;;oBASW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwCH,kBAAA,UACN,4BACP"}
@@ -38,11 +38,13 @@ import { useCallback, useEffect } from "react";
38
38
  * ```
39
39
  */
40
40
  function useMessageComposer(options) {
41
- const { client, conversationId, defaultTimelineItems = [], visitorId, onMessageSent, onError, fileOptions } = options;
41
+ const { client, conversationId, defaultTimelineItems = [], visitorId, onMessageSent, onError, fileOptions, realtimeSend, isRealtimeConnected = false } = options;
42
42
  const sendMessage = useSendMessage({ client });
43
43
  const { handleInputChange: reportTyping, handleSubmit: stopTyping, stop: forceStopTyping } = useVisitorTypingReporter({
44
44
  client,
45
- conversationId
45
+ conversationId,
46
+ realtimeSend,
47
+ isRealtimeConnected
46
48
  });
47
49
  const multimodalInput = useMultimodalInput({
48
50
  onSubmit: async ({ message: messageText, files }) => {
@@ -72,13 +74,15 @@ function useMessageComposer(options) {
72
74
  reportTyping(value);
73
75
  }, [multimodalInput, reportTyping]);
74
76
  const isSubmitting = multimodalInput.isSubmitting || sendMessage.isPending;
77
+ const isUploading = sendMessage.isUploading;
75
78
  const error = multimodalInput.error || sendMessage.error;
76
- const canSubmit = multimodalInput.canSubmit && !sendMessage.isPending;
79
+ const canSubmit = multimodalInput.canSubmit && !sendMessage.isPending && !isUploading;
77
80
  return {
78
81
  message: multimodalInput.message,
79
82
  files: multimodalInput.files,
80
83
  error,
81
84
  isSubmitting,
85
+ isUploading,
82
86
  canSubmit,
83
87
  setMessage,
84
88
  addFiles: multimodalInput.addFiles,
@@ -1 +1 @@
1
- {"version":3,"file":"use-message-composer.js","names":[],"sources":["../../src/hooks/use-message-composer.ts"],"sourcesContent":["import type { CossistantClient } from \"@cossistant/core\";\nimport type { TimelineItem } from \"@cossistant/types/api/timeline-item\";\nimport { useCallback, useEffect } from \"react\";\nimport {\n\ttype UseMultimodalInputOptions,\n\tuseMultimodalInput,\n} from \"./private/use-multimodal-input\";\nimport { useVisitorTypingReporter } from \"./private/use-visitor-typing-reporter\";\nimport { useSendMessage } from \"./use-send-message\";\n\nexport type UseMessageComposerOptions = {\n\t/**\n\t * The Cossistant client instance.\n\t */\n\tclient: CossistantClient;\n\n\t/**\n\t * Current conversation ID. Can be null if no real conversation exists yet.\n\t * Pass null when showing default timeline items before user sends first message.\n\t */\n\tconversationId: string | null;\n\n\t/**\n\t * Default timeline items to include when creating a new conversation.\n\t */\n\tdefaultTimelineItems?: TimelineItem[];\n\n\t/**\n\t * Visitor ID to associate with messages.\n\t */\n\tvisitorId?: string;\n\n\t/**\n\t * Callback when a message is successfully sent.\n\t * @param conversationId - The conversation ID (may be newly created)\n\t * @param messageId - The sent message ID\n\t */\n\tonMessageSent?: (conversationId: string, messageId: string) => void;\n\n\t/**\n\t * Callback when message sending fails.\n\t */\n\tonError?: (error: Error) => void;\n\n\t/**\n\t * File upload options (max size, allowed types, etc.)\n\t */\n\tfileOptions?: Pick<\n\t\tUseMultimodalInputOptions,\n\t\t\"maxFileSize\" | \"maxFiles\" | \"allowedFileTypes\"\n\t>;\n};\n\nexport type UseMessageComposerReturn = {\n\t// Input state\n\tmessage: string;\n\tfiles: File[];\n\terror: Error | null;\n\n\t// Status\n\tisSubmitting: boolean;\n\tcanSubmit: boolean;\n\n\t// Actions\n\tsetMessage: (message: string) => void;\n\taddFiles: (files: File[]) => void;\n\tremoveFile: (index: number) => void;\n\tclearFiles: () => void;\n\tsubmit: () => void;\n\treset: () => void;\n};\n\n/**\n * Combines message input, typing indicators, and message sending into\n * a single, cohesive hook for building message composers.\n *\n * This hook:\n * - Manages text input and file attachments via useMultimodalInput\n * - Sends typing indicators while user is composing\n * - Handles message submission with proper error handling\n * - Automatically resets input after successful send\n * - Works with both pending and real conversations\n *\n * @example\n * ```tsx\n * const composer = useMessageComposer({\n * client,\n * conversationId: realConversationId, // null if pending\n * defaultMessages,\n * visitorId: visitor?.id,\n * onMessageSent: (convId) => {\n * // Update conversation ID if it was created\n * },\n * });\n *\n * return (\n * <MessageInput\n * value={composer.message}\n * onChange={composer.setMessage}\n * onSubmit={composer.submit}\n * disabled={composer.isSubmitting}\n * />\n * );\n * ```\n */\nexport function useMessageComposer(\n\toptions: UseMessageComposerOptions\n): UseMessageComposerReturn {\n\tconst {\n\t\tclient,\n\t\tconversationId,\n\t\tdefaultTimelineItems = [],\n\t\tvisitorId,\n\t\tonMessageSent,\n\t\tonError,\n\t\tfileOptions,\n\t} = options;\n\n\tconst sendMessage = useSendMessage({ client });\n\n\tconst {\n\t\thandleInputChange: reportTyping,\n\t\thandleSubmit: stopTyping,\n\t\tstop: forceStopTyping,\n\t} = useVisitorTypingReporter({\n\t\tclient,\n\t\tconversationId,\n\t});\n\n\tconst multimodalInput = useMultimodalInput({\n\t\tonSubmit: async ({ message: messageText, files }) => {\n\t\t\t// Stop typing indicator\n\t\t\tstopTyping();\n\n\t\t\t// Send the message\n\t\t\tsendMessage.mutate({\n\t\t\t\tconversationId,\n\t\t\t\tmessage: messageText,\n\t\t\t\tfiles,\n\t\t\t\tdefaultTimelineItems,\n\t\t\t\tvisitorId,\n\t\t\t\tonSuccess: (resultConversationId, messageId) => {\n\t\t\t\t\tonMessageSent?.(resultConversationId, messageId);\n\t\t\t\t},\n\t\t\t\tonError: (err) => {\n\t\t\t\t\tonError?.(err);\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\tonError,\n\t\t...fileOptions,\n\t});\n\n\t// Clean up typing indicator on unmount\n\tuseEffect(\n\t\t() => () => {\n\t\t\tforceStopTyping();\n\t\t},\n\t\t[forceStopTyping]\n\t);\n\n\t// Wrap setMessage to also report typing\n\tconst setMessage = useCallback(\n\t\t(value: string) => {\n\t\t\tmultimodalInput.setMessage(value);\n\t\t\treportTyping(value);\n\t\t},\n\t\t[multimodalInput, reportTyping]\n\t);\n\n\t// Combine submission states\n\tconst isSubmitting = multimodalInput.isSubmitting || sendMessage.isPending;\n\tconst error = multimodalInput.error || sendMessage.error;\n\tconst canSubmit = multimodalInput.canSubmit && !sendMessage.isPending;\n\n\treturn {\n\t\tmessage: multimodalInput.message,\n\t\tfiles: multimodalInput.files,\n\t\terror,\n\t\tisSubmitting,\n\t\tcanSubmit,\n\t\tsetMessage,\n\t\taddFiles: multimodalInput.addFiles,\n\t\tremoveFile: multimodalInput.removeFile,\n\t\tclearFiles: multimodalInput.clearFiles,\n\t\tsubmit: multimodalInput.submit,\n\t\treset: multimodalInput.reset,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,SAAgB,mBACf,SAC2B;CAC3B,MAAM,EACL,QACA,gBACA,uBAAuB,EAAE,EACzB,WACA,eACA,SACA,gBACG;CAEJ,MAAM,cAAc,eAAe,EAAE,QAAQ,CAAC;CAE9C,MAAM,EACL,mBAAmB,cACnB,cAAc,YACd,MAAM,oBACH,yBAAyB;EAC5B;EACA;EACA,CAAC;CAEF,MAAM,kBAAkB,mBAAmB;EAC1C,UAAU,OAAO,EAAE,SAAS,aAAa,YAAY;AAEpD,eAAY;AAGZ,eAAY,OAAO;IAClB;IACA,SAAS;IACT;IACA;IACA;IACA,YAAY,sBAAsB,cAAc;AAC/C,qBAAgB,sBAAsB,UAAU;;IAEjD,UAAU,QAAQ;AACjB,eAAU,IAAI;;IAEf,CAAC;;EAEH;EACA,GAAG;EACH,CAAC;AAGF,uBACa;AACX,mBAAiB;IAElB,CAAC,gBAAgB,CACjB;CAGD,MAAM,aAAa,aACjB,UAAkB;AAClB,kBAAgB,WAAW,MAAM;AACjC,eAAa,MAAM;IAEpB,CAAC,iBAAiB,aAAa,CAC/B;CAGD,MAAM,eAAe,gBAAgB,gBAAgB,YAAY;CACjE,MAAM,QAAQ,gBAAgB,SAAS,YAAY;CACnD,MAAM,YAAY,gBAAgB,aAAa,CAAC,YAAY;AAE5D,QAAO;EACN,SAAS,gBAAgB;EACzB,OAAO,gBAAgB;EACvB;EACA;EACA;EACA;EACA,UAAU,gBAAgB;EAC1B,YAAY,gBAAgB;EAC5B,YAAY,gBAAgB;EAC5B,QAAQ,gBAAgB;EACxB,OAAO,gBAAgB;EACvB"}
1
+ {"version":3,"file":"use-message-composer.js","names":[],"sources":["../../src/hooks/use-message-composer.ts"],"sourcesContent":["import type { CossistantClient } from \"@cossistant/core\";\nimport type { TimelineItem } from \"@cossistant/types/api/timeline-item\";\nimport type { AnyRealtimeEvent } from \"@cossistant/types/realtime-events\";\nimport { useCallback, useEffect } from \"react\";\nimport {\n\ttype UseMultimodalInputOptions,\n\tuseMultimodalInput,\n} from \"./private/use-multimodal-input\";\nimport { useVisitorTypingReporter } from \"./private/use-visitor-typing-reporter\";\nimport { useSendMessage } from \"./use-send-message\";\n\nexport type UseMessageComposerOptions = {\n\t/**\n\t * The Cossistant client instance.\n\t */\n\tclient: CossistantClient;\n\n\t/**\n\t * Current conversation ID. Can be null if no real conversation exists yet.\n\t * Pass null when showing default timeline items before user sends first message.\n\t */\n\tconversationId: string | null;\n\n\t/**\n\t * Default timeline items to include when creating a new conversation.\n\t */\n\tdefaultTimelineItems?: TimelineItem[];\n\n\t/**\n\t * Visitor ID to associate with messages.\n\t */\n\tvisitorId?: string;\n\n\t/**\n\t * Callback when a message is successfully sent.\n\t * @param conversationId - The conversation ID (may be newly created)\n\t * @param messageId - The sent message ID\n\t */\n\tonMessageSent?: (conversationId: string, messageId: string) => void;\n\n\t/**\n\t * Callback when message sending fails.\n\t */\n\tonError?: (error: Error) => void;\n\n\t/**\n\t * File upload options (max size, allowed types, etc.)\n\t */\n\tfileOptions?: Pick<\n\t\tUseMultimodalInputOptions,\n\t\t\"maxFileSize\" | \"maxFiles\" | \"allowedFileTypes\"\n\t>;\n\n\t/**\n\t * Optional WebSocket send function for real-time typing events.\n\t * When provided, typing indicators are sent via WebSocket for better performance.\n\t */\n\trealtimeSend?: ((event: AnyRealtimeEvent) => void) | null;\n\n\t/**\n\t * Whether the WebSocket connection is currently established.\n\t */\n\tisRealtimeConnected?: boolean;\n};\n\nexport type UseMessageComposerReturn = {\n\t// Input state\n\tmessage: string;\n\tfiles: File[];\n\terror: Error | null;\n\n\t// Status\n\tisSubmitting: boolean;\n\tisUploading: boolean;\n\tcanSubmit: boolean;\n\n\t// Actions\n\tsetMessage: (message: string) => void;\n\taddFiles: (files: File[]) => void;\n\tremoveFile: (index: number) => void;\n\tclearFiles: () => void;\n\tsubmit: () => void;\n\treset: () => void;\n};\n\n/**\n * Combines message input, typing indicators, and message sending into\n * a single, cohesive hook for building message composers.\n *\n * This hook:\n * - Manages text input and file attachments via useMultimodalInput\n * - Sends typing indicators while user is composing\n * - Handles message submission with proper error handling\n * - Automatically resets input after successful send\n * - Works with both pending and real conversations\n *\n * @example\n * ```tsx\n * const composer = useMessageComposer({\n * client,\n * conversationId: realConversationId, // null if pending\n * defaultMessages,\n * visitorId: visitor?.id,\n * onMessageSent: (convId) => {\n * // Update conversation ID if it was created\n * },\n * });\n *\n * return (\n * <MessageInput\n * value={composer.message}\n * onChange={composer.setMessage}\n * onSubmit={composer.submit}\n * disabled={composer.isSubmitting}\n * />\n * );\n * ```\n */\nexport function useMessageComposer(\n\toptions: UseMessageComposerOptions\n): UseMessageComposerReturn {\n\tconst {\n\t\tclient,\n\t\tconversationId,\n\t\tdefaultTimelineItems = [],\n\t\tvisitorId,\n\t\tonMessageSent,\n\t\tonError,\n\t\tfileOptions,\n\t\trealtimeSend,\n\t\tisRealtimeConnected = false,\n\t} = options;\n\n\tconst sendMessage = useSendMessage({ client });\n\n\tconst {\n\t\thandleInputChange: reportTyping,\n\t\thandleSubmit: stopTyping,\n\t\tstop: forceStopTyping,\n\t} = useVisitorTypingReporter({\n\t\tclient,\n\t\tconversationId,\n\t\trealtimeSend,\n\t\tisRealtimeConnected,\n\t});\n\n\tconst multimodalInput = useMultimodalInput({\n\t\tonSubmit: async ({ message: messageText, files }) => {\n\t\t\t// Stop typing indicator\n\t\t\tstopTyping();\n\n\t\t\t// Send the message\n\t\t\tsendMessage.mutate({\n\t\t\t\tconversationId,\n\t\t\t\tmessage: messageText,\n\t\t\t\tfiles,\n\t\t\t\tdefaultTimelineItems,\n\t\t\t\tvisitorId,\n\t\t\t\tonSuccess: (resultConversationId, messageId) => {\n\t\t\t\t\tonMessageSent?.(resultConversationId, messageId);\n\t\t\t\t},\n\t\t\t\tonError: (err) => {\n\t\t\t\t\tonError?.(err);\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\tonError,\n\t\t...fileOptions,\n\t});\n\n\t// Clean up typing indicator on unmount\n\tuseEffect(\n\t\t() => () => {\n\t\t\tforceStopTyping();\n\t\t},\n\t\t[forceStopTyping]\n\t);\n\n\t// Wrap setMessage to also report typing\n\tconst setMessage = useCallback(\n\t\t(value: string) => {\n\t\t\tmultimodalInput.setMessage(value);\n\t\t\treportTyping(value);\n\t\t},\n\t\t[multimodalInput, reportTyping]\n\t);\n\n\t// Combine submission states\n\tconst isSubmitting = multimodalInput.isSubmitting || sendMessage.isPending;\n\tconst isUploading = sendMessage.isUploading;\n\tconst error = multimodalInput.error || sendMessage.error;\n\tconst canSubmit =\n\t\tmultimodalInput.canSubmit && !sendMessage.isPending && !isUploading;\n\n\treturn {\n\t\tmessage: multimodalInput.message,\n\t\tfiles: multimodalInput.files,\n\t\terror,\n\t\tisSubmitting,\n\t\tisUploading,\n\t\tcanSubmit,\n\t\tsetMessage,\n\t\taddFiles: multimodalInput.addFiles,\n\t\tremoveFile: multimodalInput.removeFile,\n\t\tclearFiles: multimodalInput.clearFiles,\n\t\tsubmit: multimodalInput.submit,\n\t\treset: multimodalInput.reset,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsHA,SAAgB,mBACf,SAC2B;CAC3B,MAAM,EACL,QACA,gBACA,uBAAuB,EAAE,EACzB,WACA,eACA,SACA,aACA,cACA,sBAAsB,UACnB;CAEJ,MAAM,cAAc,eAAe,EAAE,QAAQ,CAAC;CAE9C,MAAM,EACL,mBAAmB,cACnB,cAAc,YACd,MAAM,oBACH,yBAAyB;EAC5B;EACA;EACA;EACA;EACA,CAAC;CAEF,MAAM,kBAAkB,mBAAmB;EAC1C,UAAU,OAAO,EAAE,SAAS,aAAa,YAAY;AAEpD,eAAY;AAGZ,eAAY,OAAO;IAClB;IACA,SAAS;IACT;IACA;IACA;IACA,YAAY,sBAAsB,cAAc;AAC/C,qBAAgB,sBAAsB,UAAU;;IAEjD,UAAU,QAAQ;AACjB,eAAU,IAAI;;IAEf,CAAC;;EAEH;EACA,GAAG;EACH,CAAC;AAGF,uBACa;AACX,mBAAiB;IAElB,CAAC,gBAAgB,CACjB;CAGD,MAAM,aAAa,aACjB,UAAkB;AAClB,kBAAgB,WAAW,MAAM;AACjC,eAAa,MAAM;IAEpB,CAAC,iBAAiB,aAAa,CAC/B;CAGD,MAAM,eAAe,gBAAgB,gBAAgB,YAAY;CACjE,MAAM,cAAc,YAAY;CAChC,MAAM,QAAQ,gBAAgB,SAAS,YAAY;CACnD,MAAM,YACL,gBAAgB,aAAa,CAAC,YAAY,aAAa,CAAC;AAEzD,QAAO;EACN,SAAS,gBAAgB;EACzB,OAAO,gBAAgB;EACvB;EACA;EACA;EACA;EACA;EACA,UAAU,gBAAgB;EAC1B,YAAY,gBAAgB;EAC5B,YAAY,gBAAgB;EAC5B,QAAQ,gBAAgB;EACxB,OAAO,gBAAgB;EACvB"}
@@ -1 +1 @@
1
- {"version":3,"file":"use-realtime-support.d.ts","names":[],"sources":["../../src/hooks/use-realtime-support.ts"],"sourcesContent":[],"mappings":";;;KAIY,yBAAA;oBACO;AADnB,CAAA;AAIY,KAAA,wBAAA,GAAwB;EAG5B,WAAA,EAAA,OAAA;EACO,YAAA,EAAA,OAAA;EACH,KAAA,EAFJ,KAEI,GAAA,IAAA;EAEE,IAAA,EAAA,CAAA,KAAA,EAHC,gBAGD,EAAA,GAAA,IAAA;EACgB,SAAA,EAHlB,gBAGkB,GAAA,IAAA;EAAgB;EAQ9B,WAAA,EATF,gBASoB,GACxB,IAAA;+BAToB;;;;;;;iBAQd,kBAAA,WACN,4BACP"}
1
+ {"version":3,"file":"use-realtime-support.d.ts","names":[],"sources":["../../src/hooks/use-realtime-support.ts"],"sourcesContent":[],"mappings":";;;KAIY,yBAAA;oBACO;AADnB,CAAA;AAIY,KAAA,wBAAA,GAAwB;EAG5B,WAAA,EAAA,OAAA;EACO,YAAA,EAAA,OAAA;EACH,KAAA,EAFJ,KAEI,GAAA,IAAA;EAEE,IAAA,EAAA,CAAA,KAAA,EAHC,gBAGD,EAAA,GAAA,IAAA;EACgB,SAAA,EAHlB,gBAGkB,GAAA,IAAA;EAAgB;EAQ9B,WAAA,EATF,gBASoB,GAAA,IACxB;+BAToB;;;;;;;iBAQd,kBAAA,WACN,4BACP"}
@@ -27,6 +27,7 @@ type UseSendMessageResult = {
27
27
  mutate: (options: SendMessageOptions) => void;
28
28
  mutateAsync: (options: SendMessageOptions) => Promise<SendMessageResult | null>;
29
29
  isPending: boolean;
30
+ isUploading: boolean;
30
31
  error: Error | null;
31
32
  reset: () => void;
32
33
  };
@@ -1 +1 @@
1
- {"version":3,"file":"use-send-message.d.ts","names":[],"sources":["../../src/hooks/use-send-message.ts"],"sourcesContent":[],"mappings":";;;;;KAQY,kBAAA;;EAAA,OAAA,EAAA,MAAA;EAGH,KAAA,CAAA,EAAA,IAAA,EAAA;EACe,oBAAA,CAAA,EAAA,YAAA,EAAA;EAQL,SAAA,CAAA,EAAA,MAAA;EAAK;AAGxB;AAOA;;EAGW,SAAA,CAAA,EAAA,MAAA;EACG,SAAA,CAAA,EAAA,CAAA,cAAA,EAAA,MAAA,EAAA,SAAA,EAAA,MAAA,EAAA,GAAA,IAAA;EAAR,OAAA,CAAA,EAAA,CAAA,KAAA,EAda,KAcb,EAAA,GAAA,IAAA;CAEE;AAAK,KAbD,iBAAA,GAaC;EAID,cAAA,EAAA,MAAA;EA6CI,SAAA,EAAA,MAAc;iBA3Dd;yBACQ;;KAGZ,oBAAA;oBACO;yBAER,uBACL,QAAQ;;SAEN;;;KAII,qBAAA;WACF;;;;;;iBA4CM,cAAA,WACN,wBACP"}
1
+ {"version":3,"file":"use-send-message.d.ts","names":[],"sources":["../../src/hooks/use-send-message.ts"],"sourcesContent":[],"mappings":";;;;;KAiBY,kBAAA;;EAAA,OAAA,EAAA,MAAA;EAGH,KAAA,CAAA,EAAA,IAAA,EAAA;EACe,oBAAA,CAAA,EAAA,YAAA,EAAA;EAQL,SAAA,CAAA,EAAA,MAAA;EAAK;AAGxB;AAOA;;EAGW,SAAA,CAAA,EAAA,MAAA;EACG,SAAA,CAAA,EAAA,CAAA,cAAA,EAAA,MAAA,EAAA,SAAA,EAAA,MAAA,EAAA,GAAA,IAAA;EAAR,OAAA,CAAA,EAAA,CAAA,KAAA,EAda,KAcb,EAAA,GAAA,IAAA;CAGE;AAAK,KAdD,iBAAA,GAcC;EAID,cAAA,EAAA,MAAA;EAoHI,SAAA,EAAA,MAAc;iBAnId;yBACQ;;KAGZ,oBAAA;oBACO;yBAER,uBACL,QAAQ;;;SAGN;;;KAII,qBAAA;WACF;;;;;;iBAmHM,cAAA,WACN,wBACP"}
@@ -1,6 +1,6 @@
1
1
  import { useSupport } from "../provider.js";
2
2
  import { useCallback, useState } from "react";
3
- import { generateMessageId } from "@cossistant/core";
3
+ import { generateMessageId, isImageMimeType, validateFiles } from "@cossistant/core";
4
4
 
5
5
  //#region src/hooks/use-send-message.ts
6
6
  function toError(error) {
@@ -8,18 +8,21 @@ function toError(error) {
8
8
  if (typeof error === "string") return new Error(error);
9
9
  return /* @__PURE__ */ new Error("Unknown error");
10
10
  }
11
- function buildTimelineItemPayload(body, conversationId, visitorId, messageId) {
11
+ function buildTimelineItemPayload({ body, conversationId, visitorId, messageId, fileParts }) {
12
12
  const nowIso = typeof window !== "undefined" ? (/* @__PURE__ */ new Date()).toISOString() : "";
13
+ const id = messageId ?? generateMessageId();
14
+ const parts = [{
15
+ type: "text",
16
+ text: body
17
+ }];
18
+ if (fileParts && fileParts.length > 0) parts.push(...fileParts);
13
19
  return {
14
- id: messageId ?? generateMessageId(),
20
+ id,
15
21
  conversationId,
16
22
  organizationId: "",
17
23
  type: "message",
18
24
  text: body,
19
- parts: [{
20
- type: "text",
21
- text: body
22
- }],
25
+ parts,
23
26
  visibility: "public",
24
27
  userId: null,
25
28
  aiAgentId: null,
@@ -29,6 +32,37 @@ function buildTimelineItemPayload(body, conversationId, visitorId, messageId) {
29
32
  };
30
33
  }
31
34
  /**
35
+ * Upload files and return timeline parts for inclusion in a message.
36
+ */
37
+ async function uploadFilesForMessage(client, files, conversationId) {
38
+ if (files.length === 0) return [];
39
+ const validationError = validateFiles(files);
40
+ if (validationError) throw new Error(validationError);
41
+ const uploadPromises = files.map(async (file) => {
42
+ const uploadInfo = await client.generateUploadUrl({
43
+ conversationId,
44
+ contentType: file.type,
45
+ fileName: file.name
46
+ });
47
+ await client.uploadFile(file, uploadInfo.uploadUrl, file.type);
48
+ if (isImageMimeType(file.type)) return {
49
+ type: "image",
50
+ url: uploadInfo.publicUrl,
51
+ mediaType: file.type,
52
+ fileName: file.name,
53
+ size: file.size
54
+ };
55
+ return {
56
+ type: "file",
57
+ url: uploadInfo.publicUrl,
58
+ mediaType: file.type,
59
+ fileName: file.name,
60
+ size: file.size
61
+ };
62
+ });
63
+ return Promise.all(uploadPromises);
64
+ }
65
+ /**
32
66
  * Sends visitor messages while handling optimistic pending conversations and
33
67
  * exposing react-query-like mutation state.
34
68
  */
@@ -36,11 +70,12 @@ function useSendMessage(options = {}) {
36
70
  const { client: contextClient } = useSupport();
37
71
  const client = options.client ?? contextClient;
38
72
  const [isPending, setIsPending] = useState(false);
73
+ const [isUploading, setIsUploading] = useState(false);
39
74
  const [error, setError] = useState(null);
40
75
  const mutateAsync = useCallback(async (payload) => {
41
- const { conversationId: providedConversationId, message, defaultTimelineItems = [], visitorId, messageId: providedMessageId, onSuccess, onError } = payload;
42
- if (!message.trim()) {
43
- const emptyMessageError = /* @__PURE__ */ new Error("Message cannot be empty");
76
+ const { conversationId: providedConversationId, message, files = [], defaultTimelineItems = [], visitorId, messageId: providedMessageId, onSuccess, onError } = payload;
77
+ if (!message.trim() && files.length === 0) {
78
+ const emptyMessageError = /* @__PURE__ */ new Error("Message cannot be empty (or attach files)");
44
79
  setError(emptyMessageError);
45
80
  onError?.(emptyMessageError);
46
81
  return null;
@@ -60,7 +95,22 @@ function useSendMessage(options = {}) {
60
95
  preparedDefaultTimelineItems = initiated.defaultTimelineItems;
61
96
  initialConversation = initiated.conversation;
62
97
  }
63
- const timelineItemPayload = buildTimelineItemPayload(message, conversationId, visitorId ?? null, providedMessageId);
98
+ let fileParts = [];
99
+ if (files.length > 0) {
100
+ setIsUploading(true);
101
+ try {
102
+ fileParts = await uploadFilesForMessage(client, files, conversationId);
103
+ } finally {
104
+ setIsUploading(false);
105
+ }
106
+ }
107
+ const timelineItemPayload = buildTimelineItemPayload({
108
+ body: message,
109
+ conversationId,
110
+ visitorId: visitorId ?? null,
111
+ messageId: providedMessageId,
112
+ fileParts
113
+ });
64
114
  const response = await client.sendMessage({
65
115
  conversationId,
66
116
  item: {
@@ -107,10 +157,12 @@ function useSendMessage(options = {}) {
107
157
  }, [mutateAsync]),
108
158
  mutateAsync,
109
159
  isPending,
160
+ isUploading,
110
161
  error,
111
162
  reset: useCallback(() => {
112
163
  setError(null);
113
164
  setIsPending(false);
165
+ setIsUploading(false);
114
166
  }, [])
115
167
  };
116
168
  }