perf: 中断SSE、更换测试环境接口

This commit is contained in:
rd
2025-08-28 14:13:59 +08:00
parent 10fc946eca
commit 6074f716ef
7 changed files with 51 additions and 53 deletions

View File

@ -2,6 +2,7 @@ import Http from '@/api';
import axios from 'axios';
import { glsWithCatch } from '@/utils/stroage';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
export const BASE_PYTHON_URL = 'https://agent.lvfunai.com';
// 历史记录-列表
export const getAgentHistory = (id: string) => {
@ -28,7 +29,6 @@ export const getConversationList = (params: {}) => {
return Http.get(`/v1/conversation/message/list`, params);
};
export const baseUrl = 'https://agent.lvfunai.com';
export const getHeaders = () => {
const store = useEnterpriseStore();
return {
@ -43,7 +43,7 @@ export const getHeaders = () => {
* 获取智能体信息
*/
export const getAgentData = async () => {
const { data } = await axios.get(`${baseUrl}/api/agent/info`, {
const { data } = await axios.get(`${BASE_PYTHON_URL}/api/agent/info`, {
headers: getHeaders(),
});
return data;
@ -53,7 +53,7 @@ export const getAgentData = async () => {
* 生成会话id
*/
export const createSession = async () => {
const { data } = await axios.get(`${baseUrl}/api/agent/create_session`, {
const { data } = await axios.get(`${BASE_PYTHON_URL}/api/agent/create_session`, {
headers: getHeaders(),
});
return data;

View File

@ -24,7 +24,7 @@ export const ANSWER_STYLE = {
export interface UseChatHandlerReturn {
roles?: BubbleListProps['roles'];
currentTaskId?: Ref<string | null>;
generateTeamRunTaskId?: Ref<string | null>;
handleMessage?: (parsedData: { event: string; data: MESSAGE.Answer }) => void;
handleOpen?: (data: Response) => void;
generateLoading?: Ref<boolean>;

View File

@ -45,11 +45,16 @@ export default {
initSse({ message });
};
const clearSseController = () => {
if (sseController.value) {
sseController.value.abort?.();
sseController.value = null;
}
}
const handleCancel = () => {
// 中止当前正在输出的回答
console.log('handleCancel', currentTaskId.value);
if (generateLoading.value) {
bubbleListRef.value?.abortTypingByKey(currentTaskId.value);
bubbleListRef.value?.abortTypingByKey(generateTeamRunTaskId.value);
sseController.value?.abort?.();
}
if (showRightView.value) {
@ -60,18 +65,15 @@ export default {
antdMessage.info('取消生成');
};
const initSse = (inputInfo: CHAT.TInputInfo): void => {
if (sseController.value) {
sseController.value.abort?.();
sseController.value = null;
}
const initSse = async (inputInfo: CHAT.TInputInfo): Promise<void> => {
clearSseController();
try {
const { message } = inputInfo;
generateLoading.value = true;
sseController.value = querySSE({
sseController.value = await querySSE({
method: 'POST',
handleMessage,
body: JSON.stringify({
@ -93,13 +95,11 @@ export default {
agent_id: chatStore.agentInfo.agent_id,
});
if (code === 200) {
conversationList.value = [
...conversationList.value,
...(data.list?.flat(1) ?? []).map((v) => ({
...v,
teamRunTaskId: v.step_run_id,
})),
];
const remoteData = (data.list?.flat(1) ?? []).map((v: any) => ({
...v,
teamRunTaskId: v.step_run_id,
}));
conversationList.value = [...conversationList.value, ...remoteData];
}
};
@ -107,7 +107,7 @@ export default {
roles,
showRightView,
rightViewData,
currentTaskId,
generateTeamRunTaskId,
handleMessage,
conversationList,
generateLoading,
@ -140,6 +140,10 @@ export default {
{ immediate: true },
);
onUnmounted(() => {
clearSseController();
})
return () => (
<div class="chat-view-wrap w-full h-full flex">
<section class="flex-1 flex flex-col pt-20px justify-center relative px-16px">

View File

@ -44,7 +44,7 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
const senderRef = ref(null);
const conversationList = ref<any[]>([]);
const generateLoading = ref<boolean>(false);
const currentTaskId = ref<string | null>(null);
const generateTeamRunTaskId = ref<string | null>(null);
const showRightView = ref(false);
const rightViewData = ref<any>({});
const lastTeamRunTaskId = ref<string | null>(null); // 最近一个对话的id
@ -74,9 +74,9 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
onTypingComplete: () => {
currentTaskId.value = null;
},
// onTypingComplete: () => {
// generateTeamRunTaskId.value = null;
// },
style: ROLE_STYLE,
},
[QUESTION_ROLE]: {
@ -128,6 +128,7 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
// 重置生成状态
const resetGenerateStatus = () => {
generateLoading.value = false;
generateTeamRunTaskId.value = null;
};
const handleRemoteRefresh = (item: MESSAGE.Answer) => {
@ -192,7 +193,7 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
// 过程节点开始
const handleRunTaskStart = (data: MESSAGE.Answer) => {
const { run_id } = data;
currentTaskId.value = run_id;
// generateTeamRunTaskId.value = run_id;
conversationList.value.push({
run_id,
key: run_id,
@ -210,20 +211,15 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
return (
<>
{isFirstRunTask(run_id) && (
<div class="flex items-center mb-8px">
<div
class="flex items-center mb-8px cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, !isCollapse)}
>
<span class="font-family-medium color-#211F24 text-14px font-400 lh-22px mr-4px"></span>
{isCollapse ? (
<IconCaretUp
size={16}
class="color-#211F24 cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, false)}
/>
<IconCaretUp size={16} class="color-#211F24 " />
) : (
<IconCaretDown
size={16}
class="color-#211F24 cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, true)}
/>
<IconCaretDown size={16} class="color-#211F24" />
)}
</div>
)}
@ -269,7 +265,7 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
const handleTeamRunTaskStart = (data: MESSAGE.Answer) => {
const { run_id } = data;
lastTeamRunTaskId.value = run_id;
currentTaskId.value = run_id;
generateTeamRunTaskId.value = run_id;
conversationList.value.push({
run_id,
isTeamRunTask: true,
@ -390,7 +386,7 @@ export default function useChatHandler({ initSse }): UseChatHandlerReturn {
return {
roles,
senderRef,
currentTaskId,
generateTeamRunTaskId,
handleMessage,
generateLoading,
conversationList,

View File

@ -94,7 +94,7 @@
display: flex;
justify-content: center;
align-items: center;
border-radius: 50px;
border-radius: 16px;
background-color: var(--BG-200, #f2f3f5);
}

View File

@ -2,11 +2,9 @@ import { fetchEventSource } from '@microsoft/fetch-event-source';
import type { EventSourceMessage } from '@microsoft/fetch-event-source';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
import { glsWithCatch } from '@/utils/stroage';
import { BASE_PYTHON_URL } from '@/api/all/chat';
const customHost = 'https://agent.lvfunai.com';
//
// const customHost = 'http://localhost:3000';
const DEFAULT_SSE_URL = `${customHost}/api/agent/runs`;
const DEFAULT_SSE_URL = `${BASE_PYTHON_URL}/api/agent/runs`;
const SSE_HEADERS = {
'Content-Type': 'application/json',
@ -70,21 +68,21 @@ export default async (config: SSEConfig, url: string = DEFAULT_SSE_URL): Promise
}
},
onerror(error: Error) {
// console.error('SSE error:', error);
// console.error('SSE error:', error);
handleError?.(error);
},
onclose() {
// console.log('SSE connection closed');
// console.log('SSE connection closed');
handleClose?.();
},
async onopen(response: Response) {
// console.log('onopen', response);
// console.log('onopen', response);
handleOpen?.(response);
},
});
// 返回abort方法供外部调用
return {
abort: () => abortController.abort()
abort: () => abortController.abort(),
};
};