|
@@ -120,6 +120,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
|
|
|
* @returns
|
|
|
*/
|
|
|
const changeConversation = async (key: string) => {
|
|
|
+ cancel();
|
|
|
setActiveConversation(key);
|
|
|
if (key === "1") {
|
|
|
setMessages([]);
|
|
@@ -157,22 +158,27 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ const baseUrl = process.env.NODE_ENV === "production" ? "" : "/api";
|
|
|
+
|
|
|
/**
|
|
|
* 封装智能体
|
|
|
*/
|
|
|
const [agent] = useXAgent<ResponseMessageItem>({
|
|
|
request: async (message, { onError, onSuccess, onUpdate }) => {
|
|
|
+ const enterpriseCode = sessionStorage.getItem("enterpriseCode");
|
|
|
+ const token = localStorage.getItem("token_" + enterpriseCode) || '';
|
|
|
+
|
|
|
abortController.current = new AbortController();
|
|
|
const signal = abortController.current.signal;
|
|
|
try {
|
|
|
setLoading(true);
|
|
|
const response = await fetch(
|
|
|
- "https://design.shalu.com/api/ai/chat-message",
|
|
|
+ baseUrl + "/api/ai/chat-message",
|
|
|
{
|
|
|
method: "POST",
|
|
|
body: JSON.stringify(message),
|
|
|
headers: {
|
|
|
- Authorization: localStorage.getItem("token_a") || "",
|
|
|
+ Authorization: token,
|
|
|
"Content-Type": "application/json",
|
|
|
},
|
|
|
signal,
|
|
@@ -226,7 +232,11 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
|
|
|
* 发起请求
|
|
|
* @param chat_query 对话内容
|
|
|
*/
|
|
|
- const onRequest = (chat_query: string) => {
|
|
|
+ const onRequest = (chat_query: string, callbacks?: {
|
|
|
+ onUpdate: (message: ResponseMessageItem) => void;
|
|
|
+ onSuccess: (message: ResponseMessageItem) => void;
|
|
|
+ onError: (error: Error) => void;
|
|
|
+ }) => {
|
|
|
setConversationList((list) => {
|
|
|
return list?.map((item) => {
|
|
|
return {
|
|
@@ -243,7 +253,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
|
|
|
conversation_id:
|
|
|
activeConversation === "1" ? undefined : activeConversation,
|
|
|
},
|
|
|
- {
|
|
|
+ callbacks ?? {
|
|
|
onSuccess: (data) => {
|
|
|
onSuccess?.(data);
|
|
|
},
|
|
@@ -282,6 +292,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
|
|
|
* 新增会话
|
|
|
*/
|
|
|
const addConversation = () => {
|
|
|
+ cancel();
|
|
|
setMessages([]);
|
|
|
setActiveConversation("1");
|
|
|
// 还没产生对话时 直接清除当前对话
|