mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Casibase's upstream '// Copyright <yr> The OpenAgent Authors. All Rights Reserved.' (and legacy 'The Casibase Authors' on older files) was stripped from every derived source file and replaced with a Hanzo-only line. This re-adds it as a '// Portions Copyright ...' line beneath the Hanzo line on the 573 casibase-derived files, using each file's real upstream year/holder. Net-new Hanzo files are untouched. Nothing deleted. LICENSE + NOTICE were already correct.
895 lines
32 KiB
Go
895 lines
32 KiB
Go
// Copyright 2023 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
import React from "react";
|
|
import {AlertCircle, Loader2, Menu, PanelLeftClose, PanelLeftOpen, X} from "lucide-react";
|
|
import moment from "moment";
|
|
import * as StoreBackend from "./backend/StoreBackend";
|
|
import ChatMenu from "./ChatMenu";
|
|
import ChatBox from "./ChatBox";
|
|
import StoreInfoTitle from "./StoreInfoTitle";
|
|
import MultiPaneManager from "./MultiPaneManager";
|
|
import {renderReason, renderText} from "./ChatMessageRender";
|
|
import * as Setting from "./Setting";
|
|
import * as ChatBackend from "./backend/ChatBackend";
|
|
import * as MessageBackend from "./backend/MessageBackend";
|
|
import i18next from "i18next";
|
|
import BaseListPage from "./BaseListPage";
|
|
import * as Conf from "./Conf";
|
|
import {MessageCarrier} from "./chat/MessageCarrier";
|
|
|
|
class ChatPage extends BaseListPage {
|
|
constructor(props) {
|
|
super(props);
|
|
|
|
this.menu = React.createRef();
|
|
this.chatBox = React.createRef();
|
|
}
|
|
|
|
UNSAFE_componentWillMount() {
|
|
const savedCollapsedState = localStorage.getItem("chatMenuCollapsed");
|
|
const chatMenuCollapsed = savedCollapsedState ? JSON.parse(savedCollapsedState) : false;
|
|
|
|
const currentStore = this.getStore();
|
|
if (currentStore) {
|
|
Setting.setStore(currentStore);
|
|
}
|
|
|
|
this.setState({
|
|
loading: true,
|
|
disableInput: false,
|
|
isModalOpen: false,
|
|
messageLoading: false,
|
|
messageError: false,
|
|
autoRead: false,
|
|
chatMenuVisible: false,
|
|
chatMenuCollapsed: chatMenuCollapsed,
|
|
defaultStore: null,
|
|
filteredStores: [],
|
|
paneCount: 1,
|
|
storeName: currentStore,
|
|
});
|
|
|
|
this.fetch();
|
|
}
|
|
|
|
componentDidMount() {
|
|
super.componentDidMount();
|
|
window.addEventListener("message", event => {
|
|
if ((event.data.source !== undefined && event.data.source.includes("react-devtools")) || event.data.wappalyzer !== undefined) {
|
|
return;
|
|
}
|
|
|
|
if (event.data === "close") {
|
|
this.setState({
|
|
isModalOpen: false,
|
|
});
|
|
}
|
|
});
|
|
|
|
if (this.props.onCreateChatPage) {
|
|
this.props.onCreateChatPage(this);
|
|
}
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const newMessage = urlParams.get("newMessage");
|
|
|
|
if (newMessage && newMessage.trim() !== "") {
|
|
this.sendMessage(newMessage);
|
|
}
|
|
}
|
|
|
|
toggleChatMenu = () => {
|
|
this.setState({
|
|
chatMenuVisible: !this.state.chatMenuVisible,
|
|
});
|
|
};
|
|
|
|
closeChatMenu = () => {
|
|
this.setState({
|
|
chatMenuVisible: false,
|
|
});
|
|
};
|
|
|
|
toggleChatMenuCollapse = () => {
|
|
const newCollapsedState = !this.state.chatMenuCollapsed;
|
|
this.setState({
|
|
chatMenuCollapsed: newCollapsedState,
|
|
});
|
|
localStorage.setItem("chatMenuCollapsed", JSON.stringify(newCollapsedState));
|
|
};
|
|
|
|
generateChatUrl(chatName, storeName, owner = "admin") {
|
|
const currentStoreName = this.getStore();
|
|
if (!currentStoreName) {
|
|
if (chatName) {
|
|
return `/chat/${chatName}`;
|
|
}
|
|
return "/chat";
|
|
}
|
|
const targetStoreName = storeName || currentStoreName;
|
|
|
|
if (chatName) {
|
|
return `/${owner}/${targetStoreName}/chat/${chatName}`;
|
|
}
|
|
return `/${owner}/${targetStoreName}/chat`;
|
|
}
|
|
|
|
updateStoreAndUrl = (newStore) => {
|
|
if (!this.state.chat) {
|
|
return null;
|
|
}
|
|
const updatedChat = {...this.state.chat, store: newStore.name};
|
|
this.goToLinkSoft(this.generateChatUrl(updatedChat.name, updatedChat.store));
|
|
this.setState({chat: updatedChat});
|
|
return updatedChat;
|
|
};
|
|
|
|
getGlobalStores() {
|
|
StoreBackend.getGlobalStores().then((res) => {
|
|
if (res.status === "ok") {
|
|
const stores = res?.data;
|
|
const defaultStore = stores?.find(store => store.isDefault);
|
|
|
|
let filteredStores = [];
|
|
if (stores && defaultStore && defaultStore.childStores && defaultStore.childStores.length > 0) {
|
|
const childStoreNames = new Set(defaultStore.childStores);
|
|
filteredStores = stores.filter(store => childStoreNames.has(store.name));
|
|
}
|
|
|
|
this.setState({
|
|
stores: stores,
|
|
defaultStore: defaultStore,
|
|
filteredStores: filteredStores,
|
|
});
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to get")}: ${res.msg}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
getChat() {
|
|
if (this.props.match) {
|
|
return this.props.match.params.chatName;
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
getStore() {
|
|
if (this.props.match) {
|
|
return this.props.match.params.storeName;
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
goToLinkSoft(path) {
|
|
if (this.props.history) {
|
|
this.props.history.push(path);
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
getNextChatIndex(name) {
|
|
if (!name) {
|
|
return 1;
|
|
}
|
|
const prefix = `${i18next.t("chat:New Chat")} - `;
|
|
if (name.startsWith(prefix)) {
|
|
const numberPart = name.slice(prefix.length);
|
|
if (!isNaN(numberPart)) {
|
|
return parseInt(numberPart) + 1;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
newChat(chat, selectStore = {}) {
|
|
const randomName = Setting.getRandomName();
|
|
let store = selectStore.name;
|
|
if (!store) {
|
|
store = "";
|
|
}
|
|
return {
|
|
owner: "admin",
|
|
name: `chat_${randomName}`,
|
|
store: store,
|
|
createdTime: moment().format(),
|
|
updatedTime: moment().format(),
|
|
organization: this.props.account.owner,
|
|
displayName: `${i18next.t("chat:New Chat")} - ${this.getNextChatIndex(chat?.displayName) ?? randomName}`,
|
|
type: "AI",
|
|
user: this.props.account.name,
|
|
category: chat !== undefined ? chat.category : i18next.t("chat:Default Category"),
|
|
user1: "",
|
|
user2: "",
|
|
users: [],
|
|
clientIp: this.props.account.createdIp,
|
|
userAgent: this.props.account.education,
|
|
messageCount: 0,
|
|
needTitle: true,
|
|
};
|
|
}
|
|
|
|
newMessage(text, fileName, isHidden, isRegenerated, webSearchEnabled = false) {
|
|
const randomName = Setting.getRandomName();
|
|
const message = {
|
|
owner: "admin",
|
|
name: `message_${randomName}`,
|
|
createdTime: moment().format(),
|
|
organization: this.props.account.owner,
|
|
store: this.state.chat?.store,
|
|
user: this.props.account.name,
|
|
chat: this.state.chat?.name,
|
|
replyTo: "",
|
|
author: this.props.account.name,
|
|
text: text,
|
|
isHidden: isHidden,
|
|
isDeleted: false,
|
|
isAlerted: false,
|
|
isRegenerated: isRegenerated,
|
|
fileName: fileName,
|
|
webSearchEnabled: webSearchEnabled,
|
|
modelProvider: this.state.chat?.modelProvider,
|
|
};
|
|
|
|
if (!this.state.chat) {
|
|
const urlStoreName = this.state.storeName || this.getStore();
|
|
if (urlStoreName) {
|
|
message.store = urlStoreName;
|
|
}
|
|
}
|
|
|
|
if (!message.modelProvider) {
|
|
if (message.store) {
|
|
const store = this.state.stores?.find(store => store.name === message.store);
|
|
message.modelProvider = store?.modelProvider;
|
|
} else {
|
|
message.modelProvider = this.state.defaultStore?.modelProvider;
|
|
}
|
|
}
|
|
|
|
return message;
|
|
}
|
|
|
|
cancelMessage = () => {
|
|
if (this.state.messages && this.state.messages.length > 0) {
|
|
const lastMessage = this.state.messages[this.state.messages.length - 1];
|
|
if (lastMessage.author === "AI" && this.state.messageLoading) {
|
|
MessageBackend.closeMessageEventSource(lastMessage.owner, lastMessage.name);
|
|
|
|
MessageBackend.updateMessage(lastMessage.owner, lastMessage.name, lastMessage)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
this.setState({
|
|
messageLoading: false,
|
|
});
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
sendMessage(text, fileName, isHidden, isRegenerated, webSearchEnabled = false) {
|
|
const newMessage = this.newMessage(text, fileName, isHidden, isRegenerated, webSearchEnabled);
|
|
MessageBackend.addMessage(newMessage)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
const chat = res.data;
|
|
this.setState({
|
|
chat: chat,
|
|
});
|
|
|
|
const field = "user";
|
|
const value = this.props.account.name;
|
|
const sortField = "", sortOrder = "";
|
|
const storeName = this.state.storeName;
|
|
ChatBackend.getChats(value, storeName, -1, -1, field, value, sortField, sortOrder)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
const chats = res.data;
|
|
this.setState({
|
|
data: chats,
|
|
});
|
|
if (this.menu && this.menu.current) {
|
|
this.menu.current.setSelectedKeyToNewChat(chats);
|
|
}
|
|
}
|
|
});
|
|
|
|
this.getMessages(chat);
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to add")}: ${res.msg}`);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
|
});
|
|
}
|
|
|
|
getMessages(chat) {
|
|
this.setState({
|
|
messageError: false,
|
|
});
|
|
|
|
MessageBackend.getChatMessages("admin", chat.name)
|
|
.then((res) => {
|
|
res.data.map((message) => {
|
|
message.html = renderText(message.text);
|
|
});
|
|
this.setState({
|
|
messages: res.data,
|
|
});
|
|
|
|
if (Conf.IframeUrl !== "" && Setting.isAnonymousUser(this.props.account) && res.data.filter(message => message.author === "AI").length >= 3) {
|
|
this.setState({
|
|
isModalOpen: true,
|
|
});
|
|
}
|
|
|
|
if (res.data.length > 0) {
|
|
const lastMessage = res.data[res.data.length - 1];
|
|
if (lastMessage.author === "AI" && lastMessage.replyTo !== "" && lastMessage.text === "") {
|
|
let text = "";
|
|
let reasonText = "";
|
|
this.setState({
|
|
messageLoading: true,
|
|
});
|
|
|
|
if (lastMessage.errorText !== "") {
|
|
this.setState({
|
|
messageLoading: false,
|
|
messageError: true,
|
|
});
|
|
return;
|
|
}
|
|
const mssageCarrier = new MessageCarrier(chat.needTitle);
|
|
MessageBackend.getMessageAnswer(lastMessage.owner, lastMessage.name, (data) => {
|
|
const jsonData = JSON.parse(data);
|
|
|
|
if (jsonData.text === "") {
|
|
jsonData.text = "\n";
|
|
}
|
|
const currentMessage = res.data[res.data.length - 1];
|
|
const lastMessage2 = Setting.deepCopy(currentMessage);
|
|
text += jsonData.text;
|
|
const parsedResult = mssageCarrier.parseAnswerWithCarriers(text);
|
|
this.updateChatDisplayName(parsedResult.title, chat);
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
lastMessage2.text = parsedResult.finalAnswer;
|
|
|
|
lastMessage2.isReasoningPhase = false;
|
|
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
res.data.map((message, index) => {
|
|
message.html = renderText(message.text);
|
|
});
|
|
this.setState({
|
|
messages: res.data,
|
|
messageError: false,
|
|
});
|
|
}, (data) => {
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
const jsonData = JSON.parse(data);
|
|
|
|
if (jsonData.text === "") {
|
|
jsonData.text = "\n";
|
|
}
|
|
|
|
reasonText += jsonData.text;
|
|
|
|
const currentMessage = res.data[res.data.length - 1];
|
|
const lastMessage2 = Setting.deepCopy(currentMessage);
|
|
lastMessage2.reasonText = reasonText;
|
|
if (!lastMessage2.toolCalls || lastMessage2.toolCalls.length === 0) {
|
|
lastMessage2.isReasoningPhase = true;
|
|
}
|
|
|
|
if (text) {
|
|
lastMessage2.text = text;
|
|
}
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
|
|
this.setState({
|
|
messages: res.data,
|
|
});
|
|
}, (data) => {
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
const jsonData = JSON.parse(data);
|
|
|
|
const currentMessage = res.data[res.data.length - 1];
|
|
const toolCalls = currentMessage.toolCalls || [];
|
|
toolCalls.push({
|
|
name: jsonData.name,
|
|
arguments: jsonData.arguments,
|
|
content: jsonData.content,
|
|
});
|
|
|
|
const lastMessage2 = Setting.deepCopy(currentMessage);
|
|
lastMessage2.toolCalls = toolCalls;
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
|
|
this.setState({
|
|
messages: res.data,
|
|
});
|
|
}, (data) => {
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
const searchResults = JSON.parse(data);
|
|
|
|
const currentMessage = res.data[res.data.length - 1];
|
|
const lastMessage2 = Setting.deepCopy(currentMessage);
|
|
lastMessage2.searchResults = searchResults;
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
|
|
this.setState({
|
|
messages: res.data,
|
|
});
|
|
}, (data) => {
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
const vectorScores = JSON.parse(data);
|
|
|
|
const currentMessage = res.data[res.data.length - 1];
|
|
const lastMessage2 = Setting.deepCopy(currentMessage);
|
|
lastMessage2.vectorScores = vectorScores;
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
|
|
this.setState({
|
|
messages: res.data,
|
|
});
|
|
}, (error) => {
|
|
Setting.showMessage("error", Setting.getRefinedErrorText(error));
|
|
|
|
const lastMessage2 = Setting.deepCopy(lastMessage);
|
|
lastMessage2.errorText = error;
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
|
|
res.data.map((message) => {
|
|
message.html = renderText(message.text);
|
|
});
|
|
this.setState({
|
|
messages: res.data,
|
|
messageLoading: false,
|
|
messageError: true,
|
|
});
|
|
}, (data) => {
|
|
if (!chat || (this.state.chat.name !== chat.name)) {
|
|
return;
|
|
}
|
|
const lastMessage2 = Setting.deepCopy(lastMessage);
|
|
lastMessage2.text = text;
|
|
|
|
if (res.data[res.data.length - 1].reasonText) {
|
|
lastMessage2.reasonText = res.data[res.data.length - 1].reasonText;
|
|
lastMessage2.reasonHtml = res.data[res.data.length - 1].reasonHtml;
|
|
}
|
|
|
|
if (res.data[res.data.length - 1].toolCalls) {
|
|
lastMessage2.toolCalls = res.data[res.data.length - 1].toolCalls;
|
|
}
|
|
|
|
if (res.data[res.data.length - 1].searchResults) {
|
|
lastMessage2.searchResults = res.data[res.data.length - 1].searchResults;
|
|
}
|
|
|
|
if (res.data[res.data.length - 1].vectorScores) {
|
|
lastMessage2.vectorScores = res.data[res.data.length - 1].vectorScores;
|
|
}
|
|
|
|
lastMessage2.isReasoningPhase = false;
|
|
const parsedResult = mssageCarrier.parseAnswerWithCarriers(text);
|
|
text = parsedResult.finalAnswer;
|
|
if (parsedResult.title !== "") {
|
|
chat.displayName = parsedResult.title;
|
|
chat.needTitle = false;
|
|
}
|
|
lastMessage2.text = parsedResult.finalAnswer;
|
|
lastMessage2.suggestions = parsedResult.suggestionArray;
|
|
|
|
res.data[res.data.length - 1] = lastMessage2;
|
|
res.data.map((message, index) => {
|
|
message.html = renderText(message.text);
|
|
|
|
if (message.reasonText) {
|
|
message.reasonHtml = renderReason(message.reasonText);
|
|
}
|
|
});
|
|
|
|
this.setState({
|
|
messages: res.data,
|
|
messageLoading: false,
|
|
messageError: false,
|
|
});
|
|
|
|
if (this.state.autoRead) {
|
|
if (this.chatBox?.current?.toggleMessageReadState) {
|
|
this.chatBox.current.toggleMessageReadState(lastMessage2);
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
this.setState({
|
|
messageLoading: false,
|
|
});
|
|
}
|
|
}
|
|
|
|
Setting.scrollToDiv(`chatbox-list-item-${res.data.length}`);
|
|
});
|
|
}
|
|
|
|
updateChatDisplayName(title, chat) {
|
|
if (title !== "") {
|
|
const updatedChats = [...this.state.data];
|
|
const index = updatedChats.findIndex(c => c.name === chat.name);
|
|
if (index !== -1) {
|
|
updatedChats[index].displayName = title;
|
|
this.setState({data: updatedChats});
|
|
}
|
|
}
|
|
}
|
|
|
|
addChat(chat, selectStore) {
|
|
const newChat = this.newChat(chat, selectStore);
|
|
this.goToLinkSoft(this.generateChatUrl(newChat.name, newChat.store));
|
|
ChatBackend.addChat(newChat)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
Setting.showMessage("success", i18next.t("general:Successfully added"));
|
|
this.setState({
|
|
chat: newChat,
|
|
messages: null,
|
|
messageError: false,
|
|
});
|
|
this.getMessages(newChat);
|
|
|
|
this.fetch({}, false);
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to add")}: ${res.msg}`);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
|
});
|
|
return newChat;
|
|
}
|
|
|
|
deleteChat(chats, i, chat) {
|
|
ChatBackend.deleteChat(chat)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
Setting.showMessage("success", i18next.t("general:Successfully deleted"));
|
|
const data = Setting.deleteRow(this.state.data, i);
|
|
const j = Math.min(i, data.length - 1);
|
|
if (j < 0) {
|
|
this.setState({
|
|
chat: undefined,
|
|
messages: [],
|
|
data: data,
|
|
});
|
|
this.goToLinkSoft("/chat");
|
|
} else {
|
|
const focusedChat = data[j];
|
|
this.setState({
|
|
chat: focusedChat,
|
|
data: data,
|
|
});
|
|
this.getMessages(focusedChat);
|
|
this.goToLinkSoft(this.generateChatUrl(focusedChat.name, focusedChat.store));
|
|
}
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
|
});
|
|
}
|
|
|
|
updateChatName(chats, i, chat, newName) {
|
|
const name = chat.name;
|
|
chat.displayName = newName;
|
|
ChatBackend.updateChat("admin", name, chat)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
Setting.showMessage("success", i18next.t("general:Successfully saved"));
|
|
} else {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
|
});
|
|
}
|
|
|
|
handleMessageEdit = (chatName) => {
|
|
const chat = this.state.data.find(c => c.name === chatName);
|
|
if (chat) {
|
|
this.getMessages(chat);
|
|
}
|
|
};
|
|
|
|
getCurrentChat() {
|
|
return this.state.data.filter(chat => chat.name === this.state.chat?.name)[0];
|
|
}
|
|
|
|
handleChatUpdate = (updatedChat) => {
|
|
this.setState(prevState => ({
|
|
data: prevState.data.map(c => (c.name === updatedChat.name ? updatedChat : c)),
|
|
chat: updatedChat,
|
|
}));
|
|
};
|
|
|
|
renderModal() {
|
|
if (Conf.IframeUrl === "" || this.state.messages === null) {
|
|
return null;
|
|
}
|
|
|
|
const aiMessages = this.state.messages.filter(message => message.author === "AI");
|
|
const lastMessage = aiMessages[aiMessages.length - 1];
|
|
const json = (lastMessage === undefined) ? "" : Setting.parseJsonFromText(lastMessage.text);
|
|
|
|
if (!this.state.isModalOpen) {return null;}
|
|
|
|
return (
|
|
<>
|
|
<div className="fixed inset-0 bg-black/50 z-40" onClick={() => this.setState({isModalOpen: false})} />
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
|
|
<div className="bg-background border border-border rounded-lg shadow-xl w-[850px] h-[550px] relative">
|
|
<button
|
|
onClick={() => this.setState({isModalOpen: false})}
|
|
className="absolute top-2 right-2 p-1 rounded text-muted-foreground hover:text-foreground"
|
|
>
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
<iframe key={"provider"} title={"provider"} src={`${Conf.IframeUrl}&json=${encodeURIComponent(json)}`} className="w-full h-full border-0" seamless="seamless" scrolling="no" />
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
renderUnsafePasswordModal() {
|
|
if (this.props.account.password !== "#NeedToModify#") {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
|
<div className="bg-background border border-border rounded-lg shadow-xl p-6 max-w-md">
|
|
<div className="flex items-center gap-2 mb-4">
|
|
<AlertCircle className="w-5 h-5 text-destructive" />
|
|
<h3 className="font-semibold text-foreground">{i18next.t("account:Please Modify Your Password")}</h3>
|
|
</div>
|
|
<div className="space-y-2 text-sm text-muted-foreground">
|
|
<p>{i18next.t("account:The system has detected that you are using the default password, which is not secure. You need to modify your password immediately. Here are the instructions")}:</p>
|
|
<p>{i18next.t("account:1. Go to your setting page by clicking on the below \"My Account\" button.")}</p>
|
|
<p>{i18next.t("account:2. Click \"Modify password...\" button to change your password. Then close the setting page.")}</p>
|
|
<p>{i18next.t("account:3. Go back to this page and refresh it by pressing F5 key. This alert message should be gone.")}</p>
|
|
<p>{i18next.t("account:4. If you encounter any issues, please contact your administrator.")}</p>
|
|
</div>
|
|
<div className="flex justify-center mt-6">
|
|
<button
|
|
onClick={() => Setting.openLink(Setting.getMyProfileUrl(this.props.account))}
|
|
className="rounded-md bg-primary px-4 py-2 text-sm text-primary-foreground hover:bg-primary/90 transition-colors"
|
|
>
|
|
{i18next.t("account:My Account")}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
renderTable(chats) {
|
|
const onSelectChat = (i) => {
|
|
const chat = chats[i];
|
|
this.setState({
|
|
chat: chat,
|
|
chatMenuVisible: false,
|
|
messageError: false,
|
|
});
|
|
this.getMessages(chat);
|
|
this.goToLinkSoft(this.generateChatUrl(chat.name, chat.store));
|
|
};
|
|
|
|
const onAddChat = (selectStore = {}) => {
|
|
const chat = this.getCurrentChat();
|
|
this.addChat(chat, selectStore);
|
|
};
|
|
|
|
const onDeleteChat = (i) => {
|
|
const chat = chats[i];
|
|
this.deleteChat(chats, i, chat);
|
|
};
|
|
|
|
const onUpdateChatName = (i, newName) => {
|
|
const chat = chats[i];
|
|
this.updateChatName(chats, i, chat, newName);
|
|
};
|
|
|
|
const currentStoreName = this.state.storeName;
|
|
|
|
if (this.state.loading) {
|
|
return (
|
|
<div className="flex items-center justify-center h-full">
|
|
<Loader2 className="w-8 h-8 animate-spin text-muted-foreground" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const viewHeight = (Setting.getUrlParam("isRaw") !== null)
|
|
? "100vh"
|
|
: window.location.pathname.startsWith("/chat")
|
|
? "calc(100vh - 135px)"
|
|
: Setting.isMobile()
|
|
? "calc(100vh - 136px)"
|
|
: "calc(100vh - 135px)";
|
|
|
|
return (
|
|
<div className="flex" style={{height: viewHeight}}>
|
|
{this.renderModal()}
|
|
{this.renderUnsafePasswordModal()}
|
|
|
|
{/* Desktop sidebar */}
|
|
{!(Setting.isMobile() || Setting.getUrlParam("isRaw") !== null) && !this.state.chatMenuCollapsed && (
|
|
<div className="w-[250px] h-full mr-0.5 border-r border-border">
|
|
<ChatMenu ref={this.menu} chats={chats} chatName={this.getChat()} onSelectChat={onSelectChat} onAddChat={onAddChat} onDeleteChat={onDeleteChat} onUpdateChatName={onUpdateChatName} stores={this.state.stores} currentStoreName={currentStoreName} />
|
|
</div>
|
|
)}
|
|
|
|
{/* Mobile sidebar drawer */}
|
|
{Setting.isMobile() && this.state.chatMenuVisible && (
|
|
<>
|
|
<div className="fixed inset-0 bg-black/50 z-40" onClick={this.closeChatMenu} />
|
|
<div className="fixed inset-y-0 left-0 z-50 w-[250px] bg-background border-r border-border shadow-xl">
|
|
<div className="flex items-center justify-between px-3 py-2 border-b border-border">
|
|
<span className="text-sm font-semibold text-foreground">{i18next.t("general:Chats")}</span>
|
|
<button onClick={this.closeChatMenu} className="p-1 rounded text-muted-foreground hover:text-foreground">
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
<ChatMenu ref={this.menu} chats={chats} chatName={this.getChat()} onSelectChat={onSelectChat} onAddChat={onAddChat} onDeleteChat={onDeleteChat} onUpdateChatName={onUpdateChatName} stores={this.state.stores} />
|
|
</div>
|
|
</>
|
|
)}
|
|
|
|
{/* Main content */}
|
|
<div className="flex-1 h-full relative flex flex-col">
|
|
{this.state.chat && this.state.paneCount === 1 && (
|
|
<div className="flex items-center ml-4">
|
|
{Setting.isMobile() && (
|
|
<button onClick={this.toggleChatMenu} className="mr-2 p-1 rounded text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors">
|
|
<Menu className="w-5 h-5" />
|
|
</button>
|
|
)}
|
|
{!(Setting.isMobile() || Setting.getUrlParam("isRaw") !== null) && (
|
|
<button onClick={this.toggleChatMenuCollapse} className="mr-2 p-1 rounded text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors">
|
|
{this.state.chatMenuCollapsed ? <PanelLeftOpen className="w-5 h-5" /> : <PanelLeftClose className="w-5 h-5" />}
|
|
</button>
|
|
)}
|
|
<div className="flex-1">
|
|
<StoreInfoTitle chat={this.state.chat} stores={this.state.stores} onChatUpdated={this.handleChatUpdate} onStoreChange={this.updateStoreAndUrl} autoRead={this.state.autoRead} onUpdateAutoRead={(checked) => this.setState({autoRead: checked})} account={this.props.account} paneCount={this.state.paneCount} onPaneCountChange={(count) => this.setState({paneCount: count})} showPaneControls={true} />
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{this.state.paneCount > 1 ? (
|
|
<MultiPaneManager stores={this.state.stores} filteredStores={this.state.filteredStores} defaultStore={this.state.defaultStore} account={this.props.account} messageLoading={this.state.messageLoading} messageError={this.state.messageError} onCancelMessage={this.cancelMessage} initialChat={this.state.chat} onChatUpdate={(chat) => this.setState({chat})} onSetMessageLoading={(loading) => this.setState({messageLoading: loading})} paneCount={this.state.paneCount} onPaneCountChange={(count) => this.setState({paneCount: count})} />
|
|
) : (
|
|
<div className="flex-1 relative overflow-auto">
|
|
{(this.state.messages === undefined || this.state.messages === null) ? null : (
|
|
<div
|
|
className="absolute -top-[50px] left-0 right-0 bottom-0 pointer-events-none opacity-50"
|
|
style={{
|
|
backgroundImage: `url(${Conf.StaticBaseUrl}/img/hanzo-cloud-logo_1200x256.png)`,
|
|
backgroundPosition: "center",
|
|
backgroundRepeat: "no-repeat",
|
|
backgroundSize: "200px auto",
|
|
backgroundBlendMode: "luminosity",
|
|
filter: "grayscale(80%) brightness(140%) contrast(90%)",
|
|
}}
|
|
/>
|
|
)}
|
|
<ChatBox
|
|
ref={this.chatBox}
|
|
disableInput={this.state.disableInput}
|
|
loading={this.state.messageLoading}
|
|
messages={this.state.messages}
|
|
messageError={this.state.messageError}
|
|
sendMessage={(text, fileName, isHidden = false, regenerate = false, webSearchEnabled = false) => {
|
|
this.sendMessage(text, fileName, isHidden, regenerate, webSearchEnabled);
|
|
}}
|
|
onMessageEdit={this.handleMessageEdit}
|
|
onCancelMessage={this.cancelMessage}
|
|
account={this.props.account}
|
|
name={this.state.chat?.name}
|
|
displayName={this.state.chat?.displayName}
|
|
chat={this.state.chat}
|
|
store={this.state.chat ?
|
|
this.state.stores?.find(store => store.name === this.state.chat.store) :
|
|
this.state.stores?.find(store => store.name === this.state.storeName) ||
|
|
this.state.stores?.find(store => store.isDefault === true)}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
fetch = (params = {}, setLoading = true) => {
|
|
const field = "user";
|
|
const value = this.props.account.name;
|
|
const sortField = params.sortField, sortOrder = params.sortOrder;
|
|
const chatName = this.getChat();
|
|
const storeName = this.state.storeName;
|
|
|
|
if (setLoading) {
|
|
this.setState({loading: true});
|
|
}
|
|
ChatBackend.getChats(value, storeName, -1, -1, field, value, sortField, sortOrder)
|
|
.then((res) => {
|
|
if (res.status === "ok") {
|
|
const chats = res.data;
|
|
this.setState({
|
|
loading: false,
|
|
data: chats,
|
|
messages: [],
|
|
messageError: false,
|
|
searchText: params.searchText,
|
|
searchedColumn: params.searchedColumn,
|
|
});
|
|
|
|
if (chatName !== undefined && chats.length > 0) {
|
|
let chat = chats.find(chat => chat.name === chatName);
|
|
if (!chat) {
|
|
chat = chats[0];
|
|
this.goToLinkSoft(this.generateChatUrl(chat.name, chat.store));
|
|
}
|
|
this.getMessages(chat);
|
|
this.setState({
|
|
chat: chat,
|
|
});
|
|
}
|
|
this.getGlobalStores();
|
|
|
|
if (!setLoading) {
|
|
if (this.menu && this.menu.current) {
|
|
this.menu.current.setSelectedKeyToNewChat(chats);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
export default ChatPage;
|