"use client" import * as React from "react" import { cn } from "@/lib/utils" import { LanguageSelect, GraphSelect } from "@/components/Chat/ChatCfgSelect" import PdfSelect from "@/components/Chat/PdfSelect" import { useAppDispatch, useAppSelector, isRagGraph, isLanguageSupported } from "@/common" import { setRtmConnected, addChatItem } from "@/store/reducers/global" import MessageList from "@/components/Chat/MessageList" import { Button } from "@/components/ui/button" import { Send } from "lucide-react" import { rtmManager } from "@/manager/rtm" import { type IRTMTextItem, EMessageDataType, EMessageType, ERTMTextType } from "@/types" let hasInit: boolean = false export default function ChatCard(props: { className?: string }) { const { className } = props const [inputValue, setInputValue] = React.useState("") const graphName = useAppSelector((state) => state.global.graphName) const rtmConnected = useAppSelector((state) => state.global.rtmConnected) const options = useAppSelector((state) => state.global.options) const agentConnected = useAppSelector((state) => state.global.agentConnected) const dispatch = useAppDispatch() const disableInputMemo = React.useMemo(() => { return ( !options.channel || !options.userId || !options.appId || !options.token || !rtmConnected || !agentConnected ) }, [ options.channel, options.userId, options.appId, options.token, rtmConnected, agentConnected, ]) React.useEffect(() => { if ( !options.channel || !options.userId || !options.appId || !options.token ) { return } if (hasInit) { return } init() return () => { // if (hasInit) { // destory() // } } }, [options.channel, options.userId, options.appId, options.token]) const init = async () => { console.log("[rtm] init") await rtmManager.init({ channel: options.channel, userId: options.userId, appId: options.appId, token: options.token, }) dispatch(setRtmConnected(true)) rtmManager.on("rtmMessage", onTextChanged) hasInit = true } const destory = async () => { console.log("[rtm] destory") rtmManager.off("rtmMessage", onTextChanged) await rtmManager.destroy() dispatch(setRtmConnected(false)) hasInit = false } const onTextChanged = (text: IRTMTextItem) => { console.log("[rtm] onTextChanged", text) if (text.type == ERTMTextType.TRANSCRIBE) { // const isAgent = Number(text.uid) != Number(options.userId) dispatch( addChatItem({ userId: options.userId, text: text.text, type: `${text.stream_id}` === "0" ? EMessageType.AGENT : EMessageType.USER, data_type: EMessageDataType.TEXT, isFinal: text.is_final, time: text.ts, }), ) } if (text.type == ERTMTextType.INPUT_TEXT) { dispatch( addChatItem({ userId: options.userId, text: text.text, type: EMessageType.USER, data_type: EMessageDataType.TEXT, isFinal: true, time: text.ts, }), ) } } const handleInputChange = (e: React.ChangeEvent) => { setInputValue(e.target.value) } const handleInputSubmit = (e: React.FormEvent) => { e.preventDefault() if (!inputValue || disableInputMemo) { return } rtmManager.sendText(inputValue) setInputValue("") } return ( <> {/* Chat Card */}
{/* Action Bar */}
{ isLanguageSupported(graphName) ? : null } {isRagGraph(graphName) && }
{/* Chat messages would go here */}
) }