123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import { useState } from "react";
- import TextInputBar from "./TextInputBar";
- import VoiceInputBar from "./VoiceInputBar";
- import { textChat } from "@/service/bot";
- import { useTextChat } from "@/store/textChat";
- import { TAgentDetail } from "@/types/agent";
- import { delay, getLoginId, isSuccess } from "@/utils";
- import { EAI_MODEL } from "@/consts/enum";
- import { useUnload } from "@tarojs/taro";
- import { EChatRole, EContentType } from "@/types/bot";
- import { saveMessageToServer } from './message'
- import { getRecommendPrompt } from "@/service/bot"
- interface Props {
- agent: TAgentDetail | null;
- setShowWelcome?: (b: boolean) => void;
- setIsVoice?: (b: boolean) => void;
- setDisabled?: (b: boolean) => void;
- }
- let stopReceiveChunk: (() => void) | undefined;
- export const useChatInput = ({ agent, setShowWelcome, setDisabled, }: Props) => {
- const {
- pushRobotMessage,
- updateRobotMessage,
- getCurrentRobotMessage,
- updateRobotReasoningMessage,
- pushMessage,
- updateMessage,
- deleteMessage,
- setQuestions,
- questions,
- } = useTextChat();
-
- let myMsgUk = '';
- let mySessionId = '';
- const chatWithGpt = async (message: string, sessionId: string, msgUk: string) => {
- setShowWelcome?.(false)
- setQuestions([])
- let currentRobotMsgUk = "";
- await delay(300);
- setDisabled?.(true);
- if (!agent?.agentId) {
- return;
- }
- const loginId = getLoginId();
- if (!loginId) {
- return;
- }
- // const greeting = "欢迎光临我的智能体,你想问什么?";
- // {
- // content: greeting,
- // contentType: EContentType.TextPlain,
- // role: EChatRole.System,
- // },
-
- const newMsg = {
- content: message,
- contentType: EContentType.TextPlain,
- role: EChatRole.User,
- }
-
- saveMessageToServer({
- loginId,
- messages: [{
- ...newMsg,
- isStreaming: false,
- msgUk,
- }],
- agentId: agent.agentId,
- sessionId,
- })
- // 发起文本聊天
- stopReceiveChunk = textChat({
- params: {
- agentId: agent.agentId,
- isEnableOutputAudioStream: false,
- isEnableSearch: false,
- isEnableThinking: false,
- loginId,
- messages: [newMsg],
- sessionId,
- },
- onStart: () => {
- currentRobotMsgUk = pushRobotMessage({
- role: EChatRole.Assistant,
- saveStatus: 2,
- content: "",
- reasoningContent: "",
- robot: {
- avatar: agent?.avatarUrl ?? "",
- name: agent?.name ?? "",
- agentId: agent?.agentId ?? "",
- },
- });
- },
- onReceived: (m) => {
- console.log("received:", m);
- if (m.reasoningContent) {
- updateRobotReasoningMessage(
- currentRobotMsgUk,
- m.reasoningContent,
- m.body,
- );
- } else {
- updateRobotMessage(m.content, m.body);
- }
- },
- onFinished: async () => {
- const currentRobotMessage = getCurrentRobotMessage();
- console.log("回复完毕 ok, 当前robotmessage: ", currentRobotMessage);
- if(!agent.agentId){
- return
- }
- setDisabled?.(false);
- // 如果没有任何回答,则显示
- if (!currentRobotMessage?.content?.length) {
- updateRobotMessage("服务器繁忙...");
- return
- }
-
- // 将智能体的回答保存至服务器
- // currentRobotMessage.content 保存的是当前完整的智能体回复信息文本
- await saveMessageToServer({
- loginId,
- messages: [{
- content: currentRobotMessage.content ?? currentRobotMessage?.body?.content,
- contentType: currentRobotMessage?.body?.contentType ?? EContentType.TextPlain,
- isStreaming: false,
- role: currentRobotMessage.role,
- msgUk: currentRobotMessage.msgUk,
- }],
- agentId: agent.agentId,
- sessionId,
- })
-
- const response = await getRecommendPrompt({
- agentId: agent.agentId,
- sessionId,
- })
- // todo: 如果用户快速输入需要将前面的问题答案丢弃,根据 currentRobotMessage.msgUk 来设置 questions
- if(isSuccess(response.status)){
- setQuestions(response.data.questions)
- }
- },
- onError: () => {
- deleteMessage(currentRobotMsgUk);
- setDisabled?.(false);
- },
- });
- };
- const handleVoiceSend = (message: string) => {
- updateMessage(message, myMsgUk);
- chatWithGpt(message, mySessionId, myMsgUk);
- };
- const handleOnSend = async (message: string) => {
- if(!agent?.agentId){
- return
- }
- const {sessionId, msgUk} = pushMessage(message);
- chatWithGpt(message, sessionId, msgUk);
- };
- // 推一个自己的空气泡框
- const handleBeforeSend = () => {
- if(!agent?.agentId){
- return
- }
- const {sessionId, msgUk} = pushMessage("");
- myMsgUk = msgUk
- mySessionId = sessionId
- };
- // 发生主意识别错误时,删除当前自己发出的气泡框
- const handleVoiceError = () => {
- deleteMessage(myMsgUk);
- };
- useUnload(() => {
- if (stopReceiveChunk) {
- stopReceiveChunk();
- }
- });
- return {
- setQuestions,
- handleVoiceSend,
- handleOnSend,
- questions,
- handleBeforeSend,
- handleVoiceError,
- }
- }
|