api-dialog.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import ajax from '../../base/ajax';
  2. import { kuky_Authorization } from '../../base/storage';
  3. /**
  4. * 获取会话历史记录,来自于小数系统
  5. * http://192.168.20.119:9301/api/agent/90d533729c0211efbf6b0242ac160006/sessions
  6. */
  7. /**
  8. * 获取某个对话的记录 http://192.168.20.119:9301/api/agent/42e4fcdc9bea11efac300242ac160006/c0f286901bcb48b3a1fb04c5d6f1871b/session_log
  9. */
  10. /**
  11. * 新建会话
  12. * http://192.168.20.119:9301/api/agent/get-chat-id/42e4fcdc9bea11efac300242ac160006
  13. */
  14. /**
  15. * 上传文档
  16. * @param {string} id - agentId
  17. * @param {files} files - files
  18. * @returns {Promise<Object>}
  19. */
  20. const uploadFiles = (id, file) => {
  21. return ajax.post(`/api/files/upload/${id}`, file, {
  22. headers: {
  23. 'Content-Type': 'multipart/form-data'
  24. }
  25. });
  26. };
  27. const uploadExcels = (file) => {
  28. return ajax.post('/api/document/excel/upload', file, {
  29. headers: {
  30. 'Content-Type': 'multipart/form-data'
  31. }
  32. });
  33. };
  34. const uploadFile = (id, chatId, file) => {
  35. return ajax.post(`/api/files/upload/${id}?chat_id=${chatId}`, file, {
  36. headers: {
  37. 'Content-Type': 'multipart/form-data'
  38. }
  39. });
  40. };
  41. /**
  42. * 新建对话
  43. * @returns {Promise<Object>}
  44. */
  45. const createDialog = (agent_id) => {
  46. // 返回数据示例: {"code":200,"msg":"","data":{"chat_id":"44f835504943467f9a9cde261299a06f"}}
  47. return ajax.get(`/api/agent/get-chat-id/${agent_id}`);
  48. };
  49. /**
  50. * 获取某个agent的所有对话记录
  51. * @returns {Promise<Object>}
  52. */
  53. const getDialogs = (agent_id) => {
  54. return ajax.get(`/api/agent/${agent_id}/sessions`);
  55. };
  56. /**
  57. * 获取某个对话的对话记录
  58. * @returns {Promise<Object>}
  59. */
  60. export const getHistoryLogs = (agent_id, conversation_id) => {
  61. return ajax.get(`/api/agent/${agent_id}/${conversation_id}/session_log`);
  62. };
  63. // 对话过程:
  64. // ws://localhost/api/chat/ws/42e4fcdc9bea11efac300242ac160006/44f835504943467f9a9cde261299a06f?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyIiwidXNlcl9pZCI6MjEsImV4cCI6MTczNDAxMTU4NH0.tLVyD5ds3BqJ8UCZ1pnaUVBhqZ0leqXNUQhG5U2A2u0
  65. const create = (token, agentId, dialogId, type) => {
  66. // const wsParam = type === 'report' ? 'report' : 'chat';
  67. // const wsUrl =
  68. // type === 'excel'
  69. // ? `ws://${location.host}/api/document/ws/excel`
  70. // : `ws://${location.host}/api/${wsParam}/ws/${agentId}/${dialogId}?token=${token}`;
  71. const wsUrl = getWSUrl(type, agentId, dialogId);
  72. const ws = new WebSocket(wsUrl);
  73. // 当 WebSocket 打开时...
  74. ws.onopen = function (event) {
  75. console.log('WebSocket 连接已打开');
  76. };
  77. // 当 WebSocket 关闭时...
  78. ws.onclose = function (event) {
  79. console.log(event.code);
  80. };
  81. // 当有错误发生时...
  82. ws.onerror = function (error) {
  83. console.error('WebSocket 出错: ', error);
  84. };
  85. return ws;
  86. };
  87. /**
  88. * 获取websocket连接的地址
  89. * @param {string} type - 对话类型,可选值:report/excel/chat
  90. * @param {string} agent_id - 对话使用的agent ID
  91. * @param {string} dialog_id - 对话记录的ID
  92. * @returns
  93. */
  94. function getWSUrl(type, agent_id, dialog_id) {
  95. const host = location.host;
  96. const token = kuky_Authorization.get();
  97. if (type === 'report') {
  98. return `ws://${host}/api/${type}/ws/${agent_id}/${dialog_id}?token=${token}`;
  99. } else if (type === 'chat') {
  100. return `ws://${host}/api/${type}/ws/${agent_id}/${dialog_id}?token=${token}`;
  101. } else if (type === 'excel') {
  102. return `ws://${host}/api/document/ws/excel`;
  103. } else {
  104. return `ws://${host}/api/chat/ws/${agent_id}/${dialog_id}?token=${token}`;
  105. }
  106. }