Handle message answer

This commit is contained in:
Yang Luo
2023-05-02 01:30:06 +08:00
parent 2cd6f9df8e
commit 84a7fdcd07
7 changed files with 93 additions and 17 deletions

View File

@ -13,7 +13,7 @@
// limitations under the License.
import React from "react";
import {Avatar, Input, List, Spin} from "antd";
import {Alert, Avatar, Input, List, Spin} from "antd";
import {CopyOutlined, DislikeOutlined, LikeOutlined, SendOutlined} from "@ant-design/icons";
import i18next from "i18next";
@ -107,7 +107,15 @@ class ChatBox extends React.Component {
<div style={{width: "800px", margin: "0 auto", position: "relative"}}>
<List.Item.Meta
avatar={<Avatar style={{width: "30px", height: "30px", borderRadius: "3px"}} src={item.author === `${this.props.account.owner}/${this.props.account.name}` ? this.props.account.avatar : "https://cdn.casbin.com/casdoor/resource/built-in/admin/gpt.png"} />}
title={<div style={{fontSize: "16px", fontWeight: "normal", lineHeight: "24px", marginTop: "-15px", marginLeft: "5px", marginRight: "80px"}}>{item.text}</div>}
title={
<div style={{fontSize: "16px", fontWeight: "normal", lineHeight: "24px", marginTop: "-15px", marginLeft: "5px", marginRight: "80px"}}>
{
!item.text.includes("#ERROR#") ? item.text : (
<Alert message={item.text.slice("#ERROR#: ".length)} type="error" showIcon />
)
}
</div>
}
/>
<div style={{position: "absolute", top: "0px", right: "0px"}}
>

View File

@ -56,6 +56,7 @@ class ChatPage extends BaseListPage {
createdTime: moment().format(),
organization: this.props.account.owner,
chat: this.state.chatName,
replyTo: "",
author: `${this.props.account.owner}/${this.props.account.name}`,
text: text,
};
@ -83,6 +84,31 @@ class ChatPage extends BaseListPage {
messages: messages,
});
if (messages.length > 0) {
const lastMessage = messages[messages.length - 1];
if (lastMessage.author === "AI" && lastMessage.replyTo !== "" && lastMessage.text === "") {
let text = "";
MessageBackend.getMessageAnswer(lastMessage.owner, lastMessage.name, (data) => {
const lastMessage2 = Setting.deepCopy(lastMessage);
text += data;
lastMessage2.text = text;
messages[messages.length - 1] = lastMessage2;
this.setState({
messages: messages,
});
}, (error) => {
Setting.showMessage("error", `${i18next.t("general:Failed to get answer")}: ${error}`);
const lastMessage2 = Setting.deepCopy(lastMessage);
lastMessage2.text = `#ERROR#: ${error}`;
messages[messages.length - 1] = lastMessage2;
this.setState({
messages: messages,
});
});
}
}
Setting.scrollToDiv(`chatbox-list-item-${messages.length}`);
});
}

View File

@ -31,6 +31,7 @@ class MessageListPage extends BaseListPage {
createdTime: moment().format(),
organization: this.props.account.owner,
chat: "",
replyTo: "",
author: `${this.props.account.owner}/${this.props.account.name}`,
text: "",
};

View File

@ -45,13 +45,20 @@ export function getMessage(owner, name) {
}
export function getMessageAnswer(owner, name, onMessage, onError) {
const source = new EventSource(`${Setting.ServerUrl}/api/get-message-answer?id=${owner}/${encodeURIComponent(name)}`);
source.onmessage = function(event) {
onMessage(event.data);
};
source.onerror = function(error) {
onError(error);
};
const eventSource = new EventSource(`${Setting.ServerUrl}/api/get-message-answer?id=${owner}/${encodeURIComponent(name)}`);
eventSource.addEventListener("message", (e) => {
onMessage(e.data);
});
eventSource.addEventListener("myerror", (e) => {
onError(e.data);
eventSource.close();
});
eventSource.addEventListener("end", (e) => {
eventSource.close();
});
}
export function updateMessage(owner, name, message) {