import ajax from '../../base/ajax'; import { kuky_Authorization } from '../../base/storage'; /** * 获取会话历史记录,来自于小数系统 * http://192.168.20.119:9301/api/agent/90d533729c0211efbf6b0242ac160006/sessions */ /** * 获取某个对话的记录 http://192.168.20.119:9301/api/agent/42e4fcdc9bea11efac300242ac160006/c0f286901bcb48b3a1fb04c5d6f1871b/session_log */ /** * 新建会话 * http://192.168.20.119:9301/api/agent/get-chat-id/42e4fcdc9bea11efac300242ac160006 */ /** * 上传文档 * @param {string} id - agentId * @param {files} files - files * @returns {Promise} */ const uploadFiles = (id, file) => { return ajax.post(`/api/files/upload/${id}`, file, { headers: { 'Content-Type': 'multipart/form-data' } }); }; const uploadExcels = (file) => { return ajax.post('/api/document/excel/upload', file, { headers: { 'Content-Type': 'multipart/form-data' } }); }; const uploadFile = (id, chatId, file) => { return ajax.post(`/api/files/upload/${id}?chat_id=${chatId}`, file, { headers: { 'Content-Type': 'multipart/form-data' } }); }; /** * 新建对话 * @returns {Promise} */ const createDialog = (agent_id) => { // 返回数据示例: {"code":200,"msg":"","data":{"chat_id":"44f835504943467f9a9cde261299a06f"}} return ajax.get(`/api/agent/get-chat-id/${agent_id}`); }; /** * 获取某个agent的所有对话记录 * @returns {Promise} */ const getDialogs = (agent_id) => { return ajax.get(`/api/agent/${agent_id}/sessions`); }; /** * 获取某个对话的对话记录 * @returns {Promise} */ export const getHistoryLogs = (agent_id, conversation_id) => { return ajax.get(`/api/agent/${agent_id}/${conversation_id}/session_log`); }; // 对话过程: // ws://localhost/api/chat/ws/42e4fcdc9bea11efac300242ac160006/44f835504943467f9a9cde261299a06f?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyIiwidXNlcl9pZCI6MjEsImV4cCI6MTczNDAxMTU4NH0.tLVyD5ds3BqJ8UCZ1pnaUVBhqZ0leqXNUQhG5U2A2u0 const create = (token, agentId, dialogId, type) => { // const wsParam = type === 'report' ? 'report' : 'chat'; // const wsUrl = // type === 'excel' // ? `ws://${location.host}/api/document/ws/excel` // : `ws://${location.host}/api/${wsParam}/ws/${agentId}/${dialogId}?token=${token}`; const wsUrl = getWSUrl(type, agentId, dialogId); const ws = new WebSocket(wsUrl); // 当 WebSocket 打开时... ws.onopen = function (event) { console.log('WebSocket 连接已打开'); }; // 当 WebSocket 关闭时... ws.onclose = function (event) { console.log(event.code); }; // 当有错误发生时... ws.onerror = function (error) { console.error('WebSocket 出错: ', error); }; return ws; }; /** * 获取websocket连接的地址 * @param {string} type - 对话类型,可选值:report/excel/chat * @param {string} agent_id - 对话使用的agent ID * @param {string} dialog_id - 对话记录的ID * @returns */ function getWSUrl(type, agent_id, dialog_id) { const host = location.host; const token = kuky_Authorization.get(); if (type === 'report') { return `ws://${host}/api/${type}/ws/${agent_id}/${dialog_id}?token=${token}`; } else if (type === 'chat') { return `ws://${host}/api/${type}/ws/${agent_id}/${dialog_id}?token=${token}`; } else if (type === 'excel') { return `ws://${host}/api/document/ws/excel`; } else { return `ws://${host}/api/chat/ws/${agent_id}/${dialog_id}?token=${token}`; } }