import { httpRequest } from '@/utils/http' // 全局配置 let globalConfig = { botCode: '', userName: '', baseURL: '/rag' } export interface MessageRequest { botCode: string userName: string streamId: string question: string } export interface MessageResponse { status: { code: number detail: string serverResponseTime: number } body: { content: string streamId: string finish: boolean } } export interface SDKConfig { botCode: string userName: string baseURL?: string themeColor?: string | string[] position?: { top?: string right?: string bottom?: string left?: string } draggable?: boolean pollingInterval?: number } // 设置全局配置 export const setSDKConfig = (config: SDKConfig) => { globalConfig.botCode = config.botCode globalConfig.userName = config.userName if (config.baseURL) { globalConfig.baseURL = config.baseURL == '/' ? '' : config.baseURL } } // 获取全局配置 export const getSDKConfig = () => ({ ...globalConfig }) export const apiService = { /** * 发送消息到AI接口 * @param request 消息请求参数(可选,如果不传则使用全局配置) * @returns Promise */ async sendMessage(request?: Partial): Promise { try { const url = `${globalConfig.baseURL}/api/web-bot/qa/message` const messageRequest = { botCode: request?.botCode || globalConfig.botCode, userName: request?.userName || globalConfig.userName, streamId: request?.streamId || '', question: request?.question || '' } const response = await httpRequest(url, { method: 'POST', body: JSON.stringify({ ...messageRequest, userName: undefined, userId: messageRequest.userName }) }) return response } catch (error) { console.error('发送消息失败:', error) throw error } } }