index.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { useState } from "react";
  2. import TextInputBar from "./TextInputBar";
  3. import VoiceInputBar from "./VoiceInputBar";
  4. import { textChat } from "@/service/bot";
  5. import { TMessage,TRobotMessage, useTextChat } from "@/store/textChat";
  6. import { TAgentDetail } from "@/types/agent";
  7. import { delay, getLoginId } from "@/utils";
  8. import { EAI_MODEL } from "@/consts/enum";
  9. import { useUnload } from "@tarojs/taro";
  10. import { EChatRole, EContentType } from "@/types/bot";
  11. import { saveMessageToServer } from './message'
  12. interface Props {
  13. agent: TAgentDetail | null;
  14. aiModel: EAI_MODEL;
  15. histories: (TMessage|TRobotMessage)[];
  16. setShowWelcome: (b: boolean) => void;
  17. }
  18. let stopReceiveChunk: (() => void) | undefined;
  19. export default ({ agent, setShowWelcome, histories }: Props) => {
  20. const [isVoice, setIsVoice] = useState(false);
  21. const [disabled, setDisabled] = useState(false);
  22. const {
  23. pushRobotMessage,
  24. updateRobotMessage,
  25. getCurrentRobotMessage,
  26. updateRobotReasoningMessage,
  27. pushMessage,
  28. updateMessage,
  29. deleteMessage,
  30. } = useTextChat();
  31. let myMsgUk = '';
  32. let mySessionId = '';
  33. const handleTextInputBarSwitch = () => {
  34. console.log("voice input on");
  35. setIsVoice(true);
  36. };
  37. const handleVoiceInputBarSwitch = () => {
  38. console.log("voice input off");
  39. setIsVoice(false);
  40. };
  41. const chatWithGpt = async (message: string, sessionId: string, msgUk: string) => {
  42. setShowWelcome(false)
  43. let currentRobotMsgUk = "";
  44. await delay(300);
  45. setDisabled(true);
  46. if (!agent?.agentId) {
  47. return;
  48. }
  49. const loginId = getLoginId();
  50. if (!loginId) {
  51. return;
  52. }
  53. // const greeting = "欢迎光临我的智能体,你想问什么?";
  54. // {
  55. // content: greeting,
  56. // contentType: EContentType.TextPlain,
  57. // role: EChatRole.System,
  58. // },
  59. const newMsg = {
  60. content: message,
  61. contentType: EContentType.TextPlain,
  62. role: EChatRole.User,
  63. }
  64. saveMessageToServer({
  65. loginId,
  66. messages: [{
  67. ...newMsg,
  68. isStreaming: false,
  69. msgUk,
  70. }],
  71. agentId: agent.agentId,
  72. sessionId,
  73. })
  74. // 发起文本聊天
  75. stopReceiveChunk = textChat({
  76. params: {
  77. agentId: agent.agentId,
  78. isEnableOutputAudioStream: false,
  79. isEnableSearch: false,
  80. isEnableThinking: false,
  81. loginId,
  82. messages: [newMsg],
  83. sessionId,
  84. },
  85. onStart: () => {
  86. currentRobotMsgUk = pushRobotMessage({
  87. role: EChatRole.Assistant,
  88. saveStatus: 2,
  89. content: "",
  90. reasoningContent: "",
  91. robot: {
  92. avatar: agent?.avatarUrl ?? "",
  93. name: agent?.name ?? "",
  94. agentId: agent?.agentId ?? "",
  95. },
  96. });
  97. },
  98. onReceived: (m) => {
  99. console.log("received:", m);
  100. if (m.reasoningContent) {
  101. updateRobotReasoningMessage(
  102. m.reasoningContent,
  103. currentRobotMsgUk
  104. );
  105. } else {
  106. updateRobotMessage(m.content);
  107. }
  108. },
  109. onFinished: () => {
  110. console.log("回复完毕 ok");
  111. const currentRobotMessage = getCurrentRobotMessage();
  112. console.log(currentRobotMessage,333)
  113. if(!agent.agentId){
  114. return
  115. }
  116. setDisabled(false);
  117. // 如果没有任何回答,则显示
  118. if (!currentRobotMessage?.content?.length) {
  119. updateRobotMessage("服务器繁忙...");
  120. return
  121. }
  122. saveMessageToServer({
  123. loginId,
  124. messages: [{
  125. content: currentRobotMessage.content,
  126. contentType: EContentType.TextPlain,
  127. isStreaming: false,
  128. role: currentRobotMessage.role,
  129. msgUk: currentRobotMessage.msgUk,
  130. }],
  131. agentId: agent.agentId,
  132. sessionId,
  133. })
  134. },
  135. onError: () => {
  136. deleteMessage(currentRobotMsgUk);
  137. setDisabled(false);
  138. },
  139. });
  140. };
  141. const handleVoiceSend = (message: string) => {
  142. updateMessage(message, myMsgUk);
  143. chatWithGpt(message, mySessionId, myMsgUk);
  144. };
  145. const handleOnSend = async (message: string) => {
  146. if(!agent?.agentId){
  147. return
  148. }
  149. const {sessionId, msgUk} = pushMessage(message);
  150. chatWithGpt(message, sessionId, msgUk);
  151. };
  152. // 推一个自己的空气泡框
  153. const handleBeforeSend = () => {
  154. if(!agent?.agentId){
  155. return
  156. }
  157. const {sessionId, msgUk} = pushMessage("");
  158. myMsgUk = msgUk
  159. mySessionId = sessionId
  160. };
  161. // 发生主意识别错误时,删除当前自己发出的气泡框
  162. const handleVoiceError = () => {
  163. deleteMessage(myMsgUk);
  164. };
  165. useUnload(() => {
  166. if (stopReceiveChunk) {
  167. stopReceiveChunk();
  168. }
  169. });
  170. if (isVoice) {
  171. console.log(agent)
  172. // 语音输入
  173. return (
  174. <VoiceInputBar
  175. agentId={agent?.agentId}
  176. disabled={disabled}
  177. onSend={handleVoiceSend}
  178. beforeSend={handleBeforeSend}
  179. onIconClick={handleVoiceInputBarSwitch}
  180. onError={handleVoiceError}
  181. />
  182. );
  183. }
  184. // 文本输入
  185. return (
  186. <TextInputBar
  187. disabled={disabled}
  188. onSend={handleOnSend}
  189. onIconClick={handleTextInputBarSwitch}
  190. ></TextInputBar>
  191. );
  192. };