rapida-react
Version:
An easy to use react client for building generative ai application using Rapida platform.
1 lines • 4.41 kB
Source Map (JSON)
{"version":3,"sources":["../src/components/text/conversations/index.tsx"],"sourcesContent":["/*\n * Copyright (c) 2024. Rapida\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n *\n * Author: Prashant <prashant@rapida.ai>\n *\n */\nimport { agentMessageChangeEventObserver } from \"@/rapida/hooks/observables/voice-agent\";\nimport { Message } from \"@/rapida/agents/message\";\nimport { useMaybeVoiceAgent } from \"@/rapida/hooks/use-voice-agent\";\nimport { cn } from \"@/rapida/styles\";\nimport React, { FC, HTMLAttributes, useEffect, useRef, useState } from \"react\";\n\n/**\n * Props for the Conversation component.\n * @interface ConversationProps\n * @extends HTMLAttributes<HTMLDivElement>\n */\ninterface ConversationProps extends HTMLAttributes<HTMLDivElement> {\n /**\n * A React component used to wrap individual messages.\n * It should accept props of type HTMLAttributes<HTMLLIElement> and a 'message' prop of type Message.\n */\n MessageWrapper: React.ComponentType<\n HTMLAttributes<HTMLLIElement> & { message: Message }\n >;\n\n /**\n * An optional array of initial messages to populate the conversation.\n */\n intialConversations?: Message[];\n}\n\nexport const Conversation: FC<ConversationProps> = ({\n className,\n MessageWrapper,\n intialConversations = [],\n}) => {\n const [chats, setChats] = useState<Message[]>([]);\n const ctrRef = useRef<HTMLDivElement>(null);\n const [_, setChanged] = useState(false);\n const agentContext = useMaybeVoiceAgent();\n //\n React.useEffect(() => {\n const serverEventListner = agentMessageChangeEventObserver(\n agentContext!\n ).subscribe((agentEvents) => {\n setChats([...intialConversations, ...agentEvents.chats]);\n setChanged((prevChanged) => !prevChanged);\n });\n\n return () => {\n serverEventListner.unsubscribe();\n };\n }, [agentContext]);\n\n const scrollTo = (ref) => {\n setTimeout(\n () =>\n ref.current?.scrollIntoView({ inline: \"center\", behavior: \"smooth\" }),\n 777\n );\n };\n\n // on change of message to scroll\n useEffect(() => {\n scrollTo(ctrRef);\n }, [JSON.stringify(chats)]);\n\n return (\n <ul className={cn(className)}>\n {chats.map((chat, idx) => (\n <MessageWrapper key={`msg-idx-${idx}`} message={chat} />\n ))}\n <li>\n <div ref={ctrRef} />\n </li>\n </ul>\n );\n};\n"],"mappings":";;;;;;;;;AA4BA,OAAO,SAA6B,WAAW,QAAQ,gBAAgB;AA2DnE,SAEI,KAFJ;AArCG,IAAM,eAAsC,CAAC;AAAA,EAClD;AAAA,EACA;AAAA,EACA,sBAAsB,CAAC;AACzB,MAAM;AACJ,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAoB,CAAC,CAAC;AAChD,QAAM,SAAS,OAAuB,IAAI;AAC1C,QAAM,CAAC,GAAG,UAAU,IAAI,SAAS,KAAK;AACtC,QAAM,eAAe,mBAAmB;AAExC,QAAM,UAAU,MAAM;AACpB,UAAM,qBAAqB;AAAA,MACzB;AAAA,IACF,EAAE,UAAU,CAAC,gBAAgB;AAC3B,eAAS,CAAC,GAAG,qBAAqB,GAAG,YAAY,KAAK,CAAC;AACvD,iBAAW,CAAC,gBAAgB,CAAC,WAAW;AAAA,IAC1C,CAAC;AAED,WAAO,MAAM;AACX,yBAAmB,YAAY;AAAA,IACjC;AAAA,EACF,GAAG,CAAC,YAAY,CAAC;AAEjB,QAAM,WAAW,CAAC,QAAQ;AACxB;AAAA,MACE,MACE,IAAI,SAAS,eAAe,EAAE,QAAQ,UAAU,UAAU,SAAS,CAAC;AAAA,MACtE;AAAA,IACF;AAAA,EACF;AAGA,YAAU,MAAM;AACd,aAAS,MAAM;AAAA,EACjB,GAAG,CAAC,KAAK,UAAU,KAAK,CAAC,CAAC;AAE1B,SACE,qBAAC,QAAG,WAAW,GAAG,SAAS,GACxB;AAAA,UAAM,IAAI,CAAC,MAAM,QAChB,oBAAC,kBAAsC,SAAS,QAA3B,WAAW,GAAG,EAAmB,CACvD;AAAA,IACD,oBAAC,QACC,8BAAC,SAAI,KAAK,QAAQ,GACpB;AAAA,KACF;AAEJ;","names":[]}