index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useState } from "react";
  2. import TextInputBar from "./TextInputBar";
  3. import VoiceInputBar from "./VoiceInputBar";
  4. import { textChat } from "@/service/bot";
  5. import { useTextChat } from "@/store/textChat";
  6. import { TAgentDetail } from "@/types/agent";
  7. import { delay } from "@/utils";
  8. import { EAI_MODEL } from "@/consts/enum";
  9. import { useUnload } from "@tarojs/taro";
  10. interface Props {
  11. character: TAgentDetail;
  12. aiModel: EAI_MODEL;
  13. }
  14. let stopReceiveChunk: (()=> void)|undefined
  15. export default ({ character, aiModel }: Props) => {
  16. const [isVoice, setIsVoice] = useState(false);
  17. const [disabled, setDisabled] = useState(false);
  18. const {
  19. pushRobotMessage,
  20. updateRobotMessage,
  21. getCurrentRobotMessage,
  22. updateRobotReasoningMessage,
  23. pushMessage,
  24. updateMessage,
  25. deleteMessage,
  26. } = useTextChat();
  27. // const currentRobotMessageId = useTextChat((state)=> state.currentRobotMessageId);
  28. let myMessageId = '';
  29. const handleTextInputBarSwitch = () => {
  30. console.log("voice input on");
  31. setIsVoice(true);
  32. };
  33. const handleVoiceInputBarSwitch = () => {
  34. console.log("voice input off");
  35. setIsVoice(false);
  36. };
  37. const chatWithGpt = async (message: string) => {
  38. let currentRobotMessageId = ''
  39. await delay(300);
  40. setDisabled(true);
  41. // 发起文本聊天
  42. stopReceiveChunk = textChat({
  43. agentId: character.agentId,
  44. params: { message, model: aiModel },
  45. onStart: () => {
  46. currentRobotMessageId = pushRobotMessage({
  47. content: "",
  48. reasoningContent: "",
  49. robot: {
  50. avatar: character.avatarUrl ?? "",
  51. name: character.name ?? "",
  52. agentId: character.agentId ?? "",
  53. },
  54. });
  55. },
  56. onReceived: (m) => {
  57. // console.log(m.reasoningContent,3333)
  58. if (m.reasoningContent) {
  59. updateRobotReasoningMessage(m.reasoningContent, currentRobotMessageId);
  60. } else {
  61. updateRobotMessage(m.content);
  62. }
  63. },
  64. onFinished: () => {
  65. console.log("回复完毕 ok");
  66. const currentRobotMessage = getCurrentRobotMessage()
  67. // 如果没有任何回答,则显示
  68. if(!currentRobotMessage?.content?.length){
  69. updateRobotMessage('服务器繁忙...')
  70. }
  71. setDisabled(false);
  72. },
  73. onError: () => {
  74. deleteMessage(currentRobotMessageId);
  75. setDisabled(false);
  76. },
  77. });
  78. };
  79. const handleVoiceSend = (message: string) => {
  80. updateMessage(message, myMessageId);
  81. chatWithGpt(message);
  82. };
  83. const handleOnSend = (message: string) => {
  84. pushMessage(message);
  85. chatWithGpt(message);
  86. };
  87. // 推一个自己的空气泡框
  88. const handleBeforeSend = () => {
  89. myMessageId = pushMessage("");
  90. };
  91. // 发生主意识别错误时,删除当前自己发出的气泡框
  92. const handleVoiceError = () => {
  93. deleteMessage(myMessageId);
  94. };
  95. useUnload(()=> {
  96. if(stopReceiveChunk){
  97. stopReceiveChunk()
  98. }
  99. })
  100. if (isVoice) {
  101. // 语音输入
  102. return (
  103. <VoiceInputBar
  104. disabled={disabled}
  105. onSend={handleVoiceSend}
  106. beforeSend={handleBeforeSend}
  107. onIconClick={handleVoiceInputBarSwitch}
  108. onError={handleVoiceError}
  109. />
  110. );
  111. }
  112. // 文本输入
  113. return (
  114. <TextInputBar
  115. disabled={disabled}
  116. onSend={handleOnSend}
  117. onIconClick={handleTextInputBarSwitch}
  118. ></TextInputBar>
  119. );
  120. };