feat: 对话式首页接口对接,逻辑调整
This commit is contained in:
@ -3,8 +3,10 @@ import type { EventSourceMessage } from '@microsoft/fetch-event-source';
|
||||
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
||||
import { glsWithCatch } from '@/utils/stroage';
|
||||
|
||||
const customHost = 'http://localhost:3000';
|
||||
const DEFAULT_SSE_URL = `${customHost}/agent/input`;
|
||||
const customHost = 'http://192.168.40.41:8001';
|
||||
//
|
||||
// const customHost = 'http://localhost:3000';
|
||||
const DEFAULT_SSE_URL = `${customHost}/api/agent/runs`;
|
||||
|
||||
const SSE_HEADERS = {
|
||||
'Content-Type': 'application/json',
|
||||
@ -20,26 +22,30 @@ interface SSEConfig {
|
||||
handleMessage?: (data: any) => void;
|
||||
handleError?: (err: any) => number | null | undefined | void;
|
||||
handleClose?: () => void;
|
||||
handleOpen?: (response: Response) => Promise<void>;
|
||||
handleOpen?: (response: Response) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建服务器发送事件(SSE)连接
|
||||
* @param config SSE 配置
|
||||
* @param url 可选的自定义 URL
|
||||
* @returns 包含abort方法的对象,用于中断SSE连接
|
||||
*/
|
||||
export default async (config: SSEConfig, url: string = DEFAULT_SSE_URL): Promise<void> => {
|
||||
export default async (config: SSEConfig, url: string = DEFAULT_SSE_URL): Promise<{ abort: () => void }> => {
|
||||
const {
|
||||
body = undefined,
|
||||
headers = {},
|
||||
method = 'get',
|
||||
method = 'post',
|
||||
handleMessage,
|
||||
handleError,
|
||||
handleOpen,
|
||||
handleClose,
|
||||
} = config;
|
||||
const store = useEnterpriseStore();
|
||||
|
||||
|
||||
// 创建AbortController实例用于中断请求
|
||||
const abortController = new AbortController();
|
||||
|
||||
fetchEventSource(url, {
|
||||
method,
|
||||
// credentials: 'include',
|
||||
@ -50,6 +56,7 @@ export default async (config: SSEConfig, url: string = DEFAULT_SSE_URL): Promise
|
||||
...headers,
|
||||
},
|
||||
body,
|
||||
signal: abortController.signal, // 传递signal给fetchEventSource
|
||||
openWhenHidden: true, // 用户切换到另一个页面后仍能保持SSE连接
|
||||
onmessage(event: EventSourceMessage) {
|
||||
if (event.data) {
|
||||
@ -75,4 +82,9 @@ export default async (config: SSEConfig, url: string = DEFAULT_SSE_URL): Promise
|
||||
handleOpen?.(response);
|
||||
},
|
||||
});
|
||||
|
||||
// 返回abort方法供外部调用
|
||||
return {
|
||||
abort: () => abortController.abort()
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user