@gientech/modual 1.4.2 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/.editorconfig +38 -0
  2. package/.prettierignore +16 -0
  3. package/.prettierrc +17 -0
  4. package/README.md +129 -129
  5. package/USAGE.md +191 -0
  6. package/bash.exe.stackdump +40 -0
  7. package/components.json +21 -0
  8. package/dist/README.md +129 -0
  9. package/{assets → dist/assets}/database.svg +11 -11
  10. package/{assets → dist/assets}/database_add.svg +53 -53
  11. package/{assets → dist/assets}/database_connect.svg +66 -66
  12. package/{assets → dist/assets}/database_upload.svg +29 -29
  13. package/{assets → dist/assets}/defaultWeLogo.svg +14 -14
  14. package/{assets → dist/assets}/mysql.svg +14 -14
  15. package/dist/database.js +20 -0
  16. package/dist/package.json +63 -0
  17. package/{worker → dist/worker}/pdf.worker.min.js +21 -21
  18. package/doc_assets/2.png +0 -0
  19. package/doc_assets/demo.md +27 -0
  20. package/doc_assets/demos/dist-app/assets/index.Dh-ZAS9Z.css +2 -0
  21. package/doc_assets/demos/dist-app/assets/index.Dv8KVW18.js +23699 -0
  22. package/doc_assets/demos/dist-app/assets/index.Dv8KVW18.js.map +1 -0
  23. package/doc_assets/demos/dist-app/index.html +14 -0
  24. package/doc_assets/demos/dist-app/vite.svg +1 -0
  25. package/doc_assets/images/1.png +0 -0
  26. package/doc_assets/images/3.png +0 -0
  27. package/doc_assets/images/component-screenshot.png +1 -0
  28. package/doc_assets/install.md +5 -0
  29. package/eslint.config.js +92 -0
  30. package/index.html +13 -0
  31. package/package.json +99 -44
  32. package/package.json.demo-backup +109 -0
  33. package/postcss.config.cjs +19 -0
  34. package/public/vite.svg +1 -0
  35. package/public/worker/pdf.worker.min.js +22 -0
  36. package/scripts/README.md +133 -0
  37. package/scripts/build-demo.js +88 -0
  38. package/scripts/demo-selector.js +216 -0
  39. package/scripts/dev-demo.js +76 -0
  40. package/scripts/preview-demo.js +130 -0
  41. package/scripts/run-demo.bat +34 -0
  42. package/src/assets/img/downArrow.png +0 -0
  43. package/src/assets/img/excel.png +0 -0
  44. package/src/assets/img/img.png +0 -0
  45. package/src/assets/img/pdf.png +0 -0
  46. package/src/assets/img/ppt.png +0 -0
  47. package/src/assets/img/txt.png +0 -0
  48. package/src/assets/img/word.png +0 -0
  49. package/src/assets/login/homeBg.png +0 -0
  50. package/src/assets/login/left.jpg +0 -0
  51. package/src/assets/login/logoImg.png +0 -0
  52. package/src/examples/LoginPage/index.tsx +18 -0
  53. package/src/examples/chat/components/DrawerGraphPreview.tsx +78 -0
  54. package/src/examples/chat/index.tsx +190 -0
  55. package/src/examples/gientechStreamFilesReader/index.tsx +950 -0
  56. package/src/examples/ragDatabaseDataPage/index.tsx +36 -0
  57. package/src/examples/ragDatabaseIdPage/index.tsx +44 -0
  58. package/src/examples/ragDatabasePage/index.tsx +36 -0
  59. package/src/examples/ragModelManagePage/index.tsx +37 -0
  60. package/src/examples/ragSearchPage/index.tsx +0 -0
  61. package/src/examples/ragSensitiveWordsPage/index.tsx +32 -0
  62. package/src/examples/streamFiles/index.tsx +384 -0
  63. package/src/lib_enter.ts +40 -0
  64. package/src/main.tsx +5 -0
  65. package/src/main.tsx.backup +5 -0
  66. package/src/modules/chat/Conversations/Item.tsx +167 -0
  67. package/src/modules/chat/Conversations/List.tsx +206 -0
  68. package/src/modules/chat/Conversations/groupByTime.ts +39 -0
  69. package/src/modules/chat/Conversations/index.tsx +217 -0
  70. package/src/modules/chat/constants.tsx +33 -0
  71. package/src/modules/chat/data.txt +82 -0
  72. package/src/modules/chat/index.tsx +1941 -0
  73. package/src/modules/chat/types.ts +17 -0
  74. package/src/modules/database/CreateModal.tsx +398 -0
  75. package/src/modules/database/assets/Doris.png +0 -0
  76. package/src/modules/database/assets/PostgreSQL.png +0 -0
  77. package/src/modules/database/assets/SQLServer.png +0 -0
  78. package/src/modules/database/assets/database.svg +11 -0
  79. package/src/modules/database/assets/database_add.svg +53 -0
  80. package/src/modules/database/assets/database_connect.svg +66 -0
  81. package/src/modules/database/assets/database_upload.svg +29 -0
  82. package/src/modules/database/assets/empty.png +0 -0
  83. package/src/modules/database/assets/mysql.svg +14 -0
  84. package/src/modules/database/index.tsx +466 -0
  85. package/src/modules/database/server.ts +196 -0
  86. package/src/modules/databaseId/CustomCom.tsx +156 -0
  87. package/src/modules/databaseId/EditConfig.tsx +268 -0
  88. package/src/modules/databaseId/UploadDrawer.tsx +520 -0
  89. package/src/modules/databaseId/assets/aiOptimize.svg +10 -0
  90. package/src/modules/databaseId/assets/empty.png +0 -0
  91. package/src/modules/databaseId/assets/template.svg +6 -0
  92. package/src/modules/databaseId/assets/upload.svg +9 -0
  93. package/src/modules/databaseId/assets/useTemp.svg +6 -0
  94. package/src/modules/databaseId/index.tsx +734 -0
  95. package/src/modules/databaseId/server.ts +286 -0
  96. package/src/modules/databaseId/style.css +5 -0
  97. package/src/modules/databaseTable/EditRowDrawer.tsx +119 -0
  98. package/src/modules/databaseTable/index.tsx +357 -0
  99. package/src/modules/databaseTable/server.ts +180 -0
  100. package/src/modules/headlessChat/constants.tsx +32 -0
  101. package/src/modules/headlessChat/index.tsx +1063 -0
  102. package/src/modules/headlessChat/types.ts +23 -0
  103. package/src/modules/login/components/Login/LoginBox/index.tsx +102 -0
  104. package/src/modules/login/components/Login/RegisterBox/index.tsx +180 -0
  105. package/src/modules/login/components/Login/index.tsx +100 -0
  106. package/src/modules/login/index.tsx +106 -0
  107. package/src/modules/login/style.css +3 -0
  108. package/src/modules/login/useServices.ts +53 -0
  109. package/src/modules/login/utils.ts +42 -0
  110. package/src/modules/modelManage/ConfigDrawer.tsx +249 -0
  111. package/src/modules/modelManage/assets/empty.png +0 -0
  112. package/src/modules/modelManage/const.ts +50 -0
  113. package/src/modules/modelManage/index.tsx +439 -0
  114. package/src/modules/modelManage/server.ts +185 -0
  115. package/src/modules/nodegraph/index.tsx +1 -0
  116. package/src/modules/search/assets/Icon-history.svg +8 -0
  117. package/src/modules/search/assets/answerAwartar.png +0 -0
  118. package/src/modules/search/assets/doc.png +0 -0
  119. package/src/modules/search/assets/genera.gif +0 -0
  120. package/src/modules/search/assets/icon-robot.svg +9 -0
  121. package/src/modules/search/assets/icon-search-bar.svg +14 -0
  122. package/src/modules/search/assets/icon-sub-title.svg +3 -0
  123. package/src/modules/search/assets/icon-title.svg +9 -0
  124. package/src/modules/search/assets/icon-zoomOut.svg +9 -0
  125. package/src/modules/search/assets/iconAi.svg +9 -0
  126. package/src/modules/search/assets/pdf.png +0 -0
  127. package/src/modules/search/assets/ppt.png +0 -0
  128. package/src/modules/search/assets/search.svg +3 -0
  129. package/src/modules/search/assets/selected.svg +4 -0
  130. package/src/modules/search/assets/txt.png +0 -0
  131. package/src/modules/search/assets/xls.png +0 -0
  132. package/src/modules/search/components/AssisSelect.tsx +137 -0
  133. package/src/modules/search/components/Editor/ChatViewEditor.tsx +261 -0
  134. package/src/modules/search/components/Editor/aichat.css +1 -0
  135. package/src/modules/search/components/Editor/constant.ts +13 -0
  136. package/src/modules/search/components/Editor/index.tsx +113 -0
  137. package/src/modules/search/components/Editor/plugins/autofomatRules.ts +332 -0
  138. package/src/modules/search/components/Editor/plugins/convertImgPlugins.tsx +20 -0
  139. package/src/modules/search/components/Editor/plugins/createIndexes.tsx +38 -0
  140. package/src/modules/search/components/Editor/plugins/displayer.ts +298 -0
  141. package/src/modules/search/components/Editor/plugins/imageClick.tsx +32 -0
  142. package/src/modules/search/components/Editor/plugins/myplugin.tsx +98 -0
  143. package/src/modules/search/components/Editor/ui/avatar.tsx +19 -0
  144. package/src/modules/search/components/Editor/ui/blockquote-element.tsx +21 -0
  145. package/src/modules/search/components/Editor/ui/button.tsx +58 -0
  146. package/src/modules/search/components/Editor/ui/calendar.tsx +68 -0
  147. package/src/modules/search/components/Editor/ui/caption.tsx +46 -0
  148. package/src/modules/search/components/Editor/ui/checkbox.tsx +27 -0
  149. package/src/modules/search/components/Editor/ui/code-block-combobox.tsx +188 -0
  150. package/src/modules/search/components/Editor/ui/code-block-element.css +434 -0
  151. package/src/modules/search/components/Editor/ui/code-block-element.tsx +39 -0
  152. package/src/modules/search/components/Editor/ui/code-leaf.tsx +24 -0
  153. package/src/modules/search/components/Editor/ui/code-line-element.tsx +10 -0
  154. package/src/modules/search/components/Editor/ui/code-syntax-leaf.tsx +21 -0
  155. package/src/modules/search/components/Editor/ui/column-element.tsx +30 -0
  156. package/src/modules/search/components/Editor/ui/column-group-element.tsx +94 -0
  157. package/src/modules/search/components/Editor/ui/command.tsx +75 -0
  158. package/src/modules/search/components/Editor/ui/comment-avatar.tsx +22 -0
  159. package/src/modules/search/components/Editor/ui/comment-create-form.tsx +37 -0
  160. package/src/modules/search/components/Editor/ui/comment-item.tsx +74 -0
  161. package/src/modules/search/components/Editor/ui/comment-leaf.tsx +49 -0
  162. package/src/modules/search/components/Editor/ui/comment-more-dropdown.tsx +42 -0
  163. package/src/modules/search/components/Editor/ui/comment-reply-items.tsx +22 -0
  164. package/src/modules/search/components/Editor/ui/comment-resolve-button.tsx +32 -0
  165. package/src/modules/search/components/Editor/ui/comment-value.tsx +34 -0
  166. package/src/modules/search/components/Editor/ui/comments-popover.tsx +63 -0
  167. package/src/modules/search/components/Editor/ui/date-element.tsx +83 -0
  168. package/src/modules/search/components/Editor/ui/dialog.tsx +63 -0
  169. package/src/modules/search/components/Editor/ui/draggable.tsx +177 -0
  170. package/src/modules/search/components/Editor/ui/dropdown-menu.tsx +180 -0
  171. package/src/modules/search/components/Editor/ui/emoji-input-element.tsx +85 -0
  172. package/src/modules/search/components/Editor/ui/excalidraw-element.tsx +28 -0
  173. package/src/modules/search/components/Editor/ui/fixed-toolbar-buttons.tsx +76 -0
  174. package/src/modules/search/components/Editor/ui/fixed-toolbar.tsx +8 -0
  175. package/src/modules/search/components/Editor/ui/floating-toolbar-buttons.tsx +51 -0
  176. package/src/modules/search/components/Editor/ui/floating-toolbar.tsx +77 -0
  177. package/src/modules/search/components/Editor/ui/heading-element.tsx +48 -0
  178. package/src/modules/search/components/Editor/ui/highlight-leaf.tsx +17 -0
  179. package/src/modules/search/components/Editor/ui/hr-element.tsx +30 -0
  180. package/src/modules/search/components/Editor/ui/icons.tsx +267 -0
  181. package/src/modules/search/components/Editor/ui/image-element.tsx +74 -0
  182. package/src/modules/search/components/Editor/ui/inline-combobox.tsx +368 -0
  183. package/src/modules/search/components/Editor/ui/input.tsx +25 -0
  184. package/src/modules/search/components/Editor/ui/insert-dropdown-menu.tsx +218 -0
  185. package/src/modules/search/components/Editor/ui/kbd-leaf.tsx +20 -0
  186. package/src/modules/search/components/Editor/ui/link-element.tsx +29 -0
  187. package/src/modules/search/components/Editor/ui/link-floating-toolbar.tsx +161 -0
  188. package/src/modules/search/components/Editor/ui/list-element.tsx +30 -0
  189. package/src/modules/search/components/Editor/ui/mark-toolbar-button.tsx +24 -0
  190. package/src/modules/search/components/Editor/ui/media-embed-element.tsx +133 -0
  191. package/src/modules/search/components/Editor/ui/media-popover.tsx +97 -0
  192. package/src/modules/search/components/Editor/ui/mention-element.tsx +43 -0
  193. package/src/modules/search/components/Editor/ui/mention-input-element.tsx +141 -0
  194. package/src/modules/search/components/Editor/ui/mode-dropdown-menu.tsx +93 -0
  195. package/src/modules/search/components/Editor/ui/more-dropdown-menu.tsx +67 -0
  196. package/src/modules/search/components/Editor/ui/paragraph-element.tsx +4 -0
  197. package/src/modules/search/components/Editor/ui/placeholder.tsx +52 -0
  198. package/src/modules/search/components/Editor/ui/popover.tsx +32 -0
  199. package/src/modules/search/components/Editor/ui/resizable.tsx +66 -0
  200. package/src/modules/search/components/Editor/ui/separator.tsx +25 -0
  201. package/src/modules/search/components/Editor/ui/style.less +12 -0
  202. package/src/modules/search/components/Editor/ui/table-cell-element.tsx +143 -0
  203. package/src/modules/search/components/Editor/ui/table-element.tsx +243 -0
  204. package/src/modules/search/components/Editor/ui/table-row-element.tsx +22 -0
  205. package/src/modules/search/components/Editor/ui/tableValue.tsx +135 -0
  206. package/src/modules/search/components/Editor/ui/todo-list-element.tsx +43 -0
  207. package/src/modules/search/components/Editor/ui/toggle-element.tsx +31 -0
  208. package/src/modules/search/components/Editor/ui/toolbar.tsx +157 -0
  209. package/src/modules/search/components/Editor/ui/tooltip.tsx +65 -0
  210. package/src/modules/search/components/Editor/ui/turn-into-dropdown-menu.tsx +160 -0
  211. package/src/modules/search/components/Editor/ui/with-draggables.tsx +175 -0
  212. package/src/modules/search/components/FileList.tsx +287 -0
  213. package/src/modules/search/components/ImageGroupView/index.tsx +85 -0
  214. package/src/modules/search/components/ResultContent.tsx +232 -0
  215. package/src/modules/search/components/SearchInput.tsx +232 -0
  216. package/src/modules/search/components/SearchLanding.tsx +74 -0
  217. package/src/modules/search/components/SearchView.tsx +563 -0
  218. package/src/modules/search/components/SimpleEditor.tsx +158 -0
  219. package/src/modules/search/components/SimpleFileList.tsx +215 -0
  220. package/src/modules/search/index.tsx +10 -0
  221. package/src/modules/search/reademe.md +1 -0
  222. package/src/modules/search/servers/apis.tsx +19 -0
  223. package/src/modules/search/servers/index.ts +184 -0
  224. package/src/modules/search/style.less +503 -0
  225. package/src/modules/search/type.ts +22 -0
  226. package/src/modules/search/utils.ts +34 -0
  227. package/src/modules/sensitive/index.tsx +313 -0
  228. package/src/modules/sensitive/server.ts +122 -0
  229. package/src/modules/streamFilesReader/GientechStreamReader.tsx +1619 -0
  230. package/src/modules/streamFilesReader/components/Header/Toolbar.tsx +0 -0
  231. package/src/modules/streamFilesReader/components/Header/index.tsx +297 -0
  232. package/src/modules/streamFilesReader/index.tsx +3 -0
  233. package/src/style.css +6 -0
  234. package/src/type.d.ts +0 -0
  235. package/src/utils/commonFn.tsx +111 -0
  236. package/src/utils/gientechCommon/components/AppError.tsx +32 -0
  237. package/src/utils/gientechCommon/components/AppLoading.tsx +75 -0
  238. package/src/utils/gientechCommon/components/DeleteModal.tsx +75 -0
  239. package/src/utils/gientechCommon/components/DisplayError.tsx +33 -0
  240. package/src/utils/gientechCommon/components/DisplayLoading.tsx +38 -0
  241. package/src/utils/gientechCommon/components/FeedBackModal.tsx +310 -0
  242. package/src/utils/gientechCommon/components/FileCardCommon.tsx +82 -0
  243. package/src/utils/gientechCommon/components/FileManager/index.tsx +390 -0
  244. package/src/utils/gientechCommon/components/FileManager/style.css +5 -0
  245. package/src/utils/gientechCommon/components/Messages/GientechNewChatWelcome.tsx +296 -0
  246. package/src/utils/gientechCommon/components/Messages/ReferenceCard.tsx +339 -0
  247. package/src/utils/gientechCommon/components/Messages/RetriveItem.tsx +245 -0
  248. package/src/utils/gientechCommon/components/Messages/WebRetriveItem.tsx +209 -0
  249. package/src/utils/gientechCommon/components/Messages/defaultBot.png +0 -0
  250. package/src/utils/gientechCommon/components/Messages/defaultStyleSet.tsx +148 -0
  251. package/src/utils/gientechCommon/components/Messages/defaultWeLogo.svg +14 -0
  252. package/src/utils/gientechCommon/components/RenameModal.tsx +86 -0
  253. package/src/utils/gientechCommon/components/style.less +11 -0
  254. package/src/utils/gientechCommon/configs/commonConfig.ts +2 -0
  255. package/src/utils/gientechCommon/configs/senderConfig.ts +0 -0
  256. package/src/utils/gientechCommon/configs/stylesConfig.ts +142 -0
  257. package/src/utils/gientechCommon/hooks/AichatUseController.tsx +345 -0
  258. package/src/utils/gientechCommon/slate/converters/deserializers.ts +763 -0
  259. package/src/utils/gientechCommon/slate/converters/mockData.ts +232 -0
  260. package/src/utils/gientechCommon/slate/converters/slateConverters.ts +258 -0
  261. package/src/utils/gientechCommon/slate/richElements/index.tsx +499 -0
  262. package/src/utils/gientechCommon/utils/request.ts +37 -0
  263. package/src/utils/gientechCommon/utils/serverFn.ts +172 -0
  264. package/src/utils/index.tsx +126 -0
  265. package/src/utils/testconfigs/demologin/index.tsx +32 -0
  266. package/src/utils/testconfigs/index.ts +53 -0
  267. package/src/vite-env.d.ts +11 -0
  268. package/stats.html +4949 -0
  269. package/tailwind.config.js +170 -0
  270. package/tsconfig.app.json +30 -0
  271. package/tsconfig.app.tsbuildinfo +11 -0
  272. package/tsconfig.json +13 -0
  273. package/tsconfig.node.json +22 -0
  274. package/tsconfig.node.tsbuildinfo +1 -0
  275. package/vite.config.ts +216 -0
  276. package/workflows/release.yml +60 -0
  277. package/database.js +0 -20
  278. /package/{assets → dist/assets}/AppLoading-BCu9TPs5.js +0 -0
  279. /package/{assets → dist/assets}/Doris.png +0 -0
  280. /package/{assets → dist/assets}/GientechStreamReader-A8zHypyu.js +0 -0
  281. /package/{assets → dist/assets}/LeftOutlined-BdBZ6FSD.js +0 -0
  282. /package/{assets → dist/assets}/PostgreSQL.png +0 -0
  283. /package/{assets → dist/assets}/SQLServer.png +0 -0
  284. /package/{assets → dist/assets}/empty.png +0 -0
  285. /package/{assets → dist/assets}/homeBg.png +0 -0
  286. /package/{assets → dist/assets}/index-B7u9FawR.js +0 -0
  287. /package/{assets → dist/assets}/index-CFlRqq2H.js +0 -0
  288. /package/{assets → dist/assets}/index-CNJZ9j4J.js +0 -0
  289. /package/{assets → dist/assets}/index-CpW6Dhpp.js +0 -0
  290. /package/{assets → dist/assets}/index-DfSHCQcq.js +0 -0
  291. /package/{assets → dist/assets}/index-DiQ4Me9J.js +0 -0
  292. /package/{assets → dist/assets}/index-EcdpegTC.js +0 -0
  293. /package/{assets → dist/assets}/index-rydP_SDR.js +0 -0
  294. /package/{assets → dist/assets}/left.jpg +0 -0
  295. /package/{assets → dist/assets}/logoImg.png +0 -0
  296. /package/{assets → dist/assets}/plus-ufD39rmY.js +0 -0
  297. /package/{assets → dist/assets}/style.css +0 -0
  298. /package/{assets → dist/assets}/style2.css +0 -0
  299. /package/{assets → dist/assets}/style3.css +0 -0
  300. /package/{assets → dist/assets}/style4.css +0 -0
  301. /package/{assets → dist/assets}/x-_1-UpSBt.js +0 -0
  302. /package/{chat.d.ts → dist/chat.d.ts} +0 -0
  303. /package/{chat.js → dist/chat.js} +0 -0
  304. /package/{database.d.ts → dist/database.d.ts} +0 -0
  305. /package/{databaseId.d.ts → dist/databaseId.d.ts} +0 -0
  306. /package/{databaseId.js → dist/databaseId.js} +0 -0
  307. /package/{databaseTable.d.ts → dist/databaseTable.d.ts} +0 -0
  308. /package/{databaseTable.js → dist/databaseTable.js} +0 -0
  309. /package/{index.d.ts → dist/index.d.ts} +0 -0
  310. /package/{index.js → dist/index.js} +0 -0
  311. /package/{modelManage.d.ts → dist/modelManage.d.ts} +0 -0
  312. /package/{modelManage.js → dist/modelManage.js} +0 -0
  313. /package/{sensitive.d.ts → dist/sensitive.d.ts} +0 -0
  314. /package/{sensitive.js → dist/sensitive.js} +0 -0
  315. /package/{streamFilesReader.d.ts → dist/streamFilesReader.d.ts} +0 -0
  316. /package/{streamFilesReader.js → dist/streamFilesReader.js} +0 -0
  317. /package/{vite.svg → dist/vite.svg} +0 -0
@@ -0,0 +1,1063 @@
1
+ import React, { useEffect, useState, useMemo, useRef } from 'react';
2
+ import AiChat, { AppStatusManager } from '@mxmweb/aichat';
3
+ import { ChatMessageAdapter } from '@mxmweb/rtext';
4
+ import request from '../../utils/gientechCommon/utils/request';
5
+ import { isCancel } from 'axios';
6
+ import dayjs from 'dayjs';
7
+
8
+ import {
9
+ getFeedbackList,
10
+ handleCancelFeedBack,
11
+ handleFeedBack,
12
+ removeFilefromTempUpload,
13
+ } from '../../utils/gientechCommon/utils/serverFn';
14
+ import { getFileTypeByName, toCopy } from '../../utils/commonFn';
15
+ import { uid } from 'uid';
16
+ import GientechNewChatWelcome from '../../utils/gientechCommon/components/Messages/GientechNewChatWelcome';
17
+ import AichatUseController from '../../utils/gientechCommon/hooks/AichatUseController';
18
+ import FeedBackModal from '../../utils/gientechCommon/components/FeedBackModal';
19
+ import { message, Modal } from 'antd';
20
+ import FileManager from '../../utils/gientechCommon/components/FileManager';
21
+ import AppLoading from '../../utils/gientechCommon/components/AppLoading';
22
+ import DisplayLoading from '../../utils/gientechCommon/components/DisplayLoading';
23
+ import AppError from '../../utils/gientechCommon/components/AppError';
24
+ import DisplayError from '../../utils/gientechCommon/components/DisplayError';
25
+ import { DefaultSenderConfig } from './constants';
26
+ import { maxPollCount, maxPollInterval } from '../../utils/gientechCommon/configs/commonConfig';
27
+ import { getNameFromFile } from '../../utils';
28
+
29
+ interface UploadedFile {
30
+ name: string;
31
+ size: number;
32
+ type: string;
33
+ filePath: string;
34
+ convertedFilePath?: string;
35
+ uid: string;
36
+ }
37
+
38
+ interface EmptyMessageData {
39
+ content: string;
40
+ filePaths?: string;
41
+ queryId: string;
42
+ currentFiles?: UploadedFile[];
43
+ }
44
+
45
+ interface HeadlessChatAdopterProps {
46
+ token: string;
47
+ url?: string;
48
+ configId?: string;
49
+ useLogin?: boolean;
50
+ styles?: any;
51
+ welcomeContent?: string;
52
+ onError?: (ems: any) => void;
53
+ eventsEmit?: (eventName: string, data: any) => void;
54
+ [key: string]: any;
55
+ }
56
+
57
+ export default function withHeadLessChatAdopter(WrappedComponent: React.ComponentType<any> = AiChat as any) {
58
+ /**
59
+ * GientechChatAdopter 高阶组件
60
+ * 封装了与Gientech后端服务的接口对接、状态管理、事件处理等核心业务逻辑
61
+ * @param {React.ComponentType} WrappedComponent - 需要包裹的基础聊天组件,默认为 AiChat
62
+ */
63
+ return function GientechChatAdopter({
64
+ token,
65
+ url = 'http://localhost:8888',
66
+ welcomeContent = '欢迎来到小鲸智能助手,有什么能帮你的么?',
67
+ styles,
68
+ eventsEmit,
69
+ onError,
70
+ configId,
71
+ ...rest
72
+ }: HeadlessChatAdopterProps) {
73
+ // =================================================================
74
+ // State Management - 状态管理
75
+ // =================================================================
76
+ const [conversationList, setConversationList] = useState<any[]>([]); // 对话列表
77
+ const [chatData, setChatData] = useState<any[]>([]); // 所有对话的聊天记录
78
+ const [activeSessionId, setActiveSessionId] = useState<string | undefined>(undefined); // 当前激活的对话ID
79
+ const [recommandQuestions, setRecommandQuestions] = useState<any[]>([]); // 推荐问题列表
80
+ const [assistantList, setAssistantList] = useState<any[]>([]); // 智能体(助手)列表
81
+ const [newAssistantId, setNewAssistantId] = useState<string | undefined>(undefined); // 新建对话时选择的智能体ID
82
+ const [fileManagerOpen, setFileManagerOpen] = useState<boolean>(false); // 文件管理器抽屉的开关状态
83
+ const [appStatus, setAppStatus] = useState<AppStatusManager>({
84
+ display: 'ready', // 主聊天区域状态: ready, loading, processing, error, uploading
85
+ sender: 'ready', // 发送器状态: ready, processing, error, uploading
86
+ app: 'initializing', // 应用整体状态: initializing, ready, error
87
+ });
88
+ const [pendingFiles, setPendingFiles] = useState<any[]>([]); // 准备上传的文件列表
89
+ const [dynamicSenderConfig, setDynamicSenderConfig] = useState(DefaultSenderConfig); // 动态的发送器配置
90
+ const [fileStatuses, setFileStatuses] = useState<any[]>([]); // 用于跟踪每个上传文件的状态
91
+ const [fileManagerData, setFileManagerData] = useState<{ uploadedFiles: any[] }>({
92
+ uploadedFiles: [],
93
+ });
94
+ useEffect(() => {
95
+ console.log('chatDatachatDatachatData', chatData);
96
+ }, [chatData]);
97
+ // =================================================================
98
+ // Refs - 引用
99
+ // =================================================================
100
+ const prevActiveSessionId = useRef<string | undefined>(undefined); // 记录上一个激活的对话ID,用于对比变化
101
+ const setUploadedFilesRef = useRef<(files: any[]) => void>(undefined); // 引用核心组件的文件上传方法
102
+ const removeFileRef = useRef<(fileObj: any, idx: number, _type: string) => void>(undefined); // 引用核心组件的文件移除方法
103
+
104
+ const [feedParam, setFeedParam] = useState<{
105
+ queryId?: number | string | undefined;
106
+ restult?: number;
107
+ }>({});
108
+ const [openFeed, setOpenFeed] = useState(false);
109
+ const [feedBackList, setFeedBackList] = useState<any>([]);
110
+ const stoppedRef = useRef(false);
111
+
112
+ // =================================================================
113
+ // Custom Hooks - 自定义钩子
114
+ // =================================================================
115
+ // 引入SSE控制器,封装了流式请求、消息管理等复杂逻辑
116
+ const {
117
+ api_startChat_re,
118
+ addEmptyMessage,
119
+ setLastEmptyMessage,
120
+ stopStream,
121
+ updateUserFilesMessage,
122
+ } = AichatUseController({
123
+ baseUrl: url,
124
+ token,
125
+ setChatData,
126
+ activeSessionId,
127
+ setAppStatus,
128
+ setConversationList,
129
+ });
130
+
131
+ // =================================================================
132
+ // Memoized Values - 缓存计算值
133
+ // =================================================================
134
+
135
+ // welcomeContent 伪消息唯一 id/time/queryId
136
+ const welcomeMsgId = useRef(uid(32));
137
+ const welcomeMsgQueryId = useRef(uid(32));
138
+ const welcomeMsgTime = useRef(Date.now());
139
+
140
+ /**
141
+ * 将智能体列表转换为以ID为键的Map,便于快速查找
142
+ */
143
+ const assistantMap = useMemo(() => {
144
+ const map: Record<string, any> = {};
145
+ (assistantList || []).forEach(item => {
146
+ map[item.id] = item;
147
+ });
148
+ return map;
149
+ }, [assistantList]);
150
+
151
+ /**
152
+ * 根据 activeSessionId 从 chatData 中筛选出当前对话的数据, 并合并智能体信息
153
+ */
154
+ const currentChatData = useMemo(() => {
155
+ let data = chatData.find((c: any) => c.id === activeSessionId) || {
156
+ id: activeSessionId,
157
+ messages: [],
158
+ };
159
+ let messages = data.messages || [];
160
+ // 检查第一条是否已经是 welcome 消息,如果不是则插入
161
+ if (welcomeContent && (messages.length === 0 || !messages[0]?.isDefault)) {
162
+ messages = [
163
+ {
164
+ istype: 'ai',
165
+ content: welcomeContent,
166
+ time: welcomeMsgTime.current,
167
+ clientSideId: welcomeMsgId.current,
168
+ isWelcome: true,
169
+ isDefault: true,
170
+ queryId: welcomeMsgQueryId.current,
171
+ },
172
+ ...messages,
173
+ ];
174
+ }
175
+ // 如果第一条已经是 isDefault: true 的欢迎消息,确保它的 isWelcome 字段存在
176
+ if (messages[0]?.isDefault && !messages[0]?.isWelcome) {
177
+ messages[0].isWelcome = true;
178
+ }
179
+ const assistantInfo = data.configId ? assistantMap[data.configId] : null;
180
+ return {
181
+ ...data,
182
+ messages,
183
+ assistantInfo,
184
+ };
185
+ }, [chatData, activeSessionId, assistantMap, welcomeContent]);
186
+
187
+ /**
188
+ * 用 useEffect 监听 currentChatData 自动同步
189
+ */
190
+ useEffect(() => {
191
+ let uploaded: any[] = [];
192
+ const lastAiMsg = [...(currentChatData.messages || [])]
193
+ .reverse()
194
+ .find((msg: any) => msg.istype === 'ai');
195
+ if (lastAiMsg?.reference && lastAiMsg.resultType === 5) {
196
+ try {
197
+ const fileList = JSON.parse(lastAiMsg.reference);
198
+ uploaded = fileList.map((f: any) => ({
199
+ file: {
200
+ name: f.name,
201
+ size: Number(f.size?.replace('MB', '')) * 1024 * 1024 || 0,
202
+ type: f.type,
203
+ } as File,
204
+ type: getFileTypeByName(f.name),
205
+ status: 'uploaded' as const,
206
+ url: f.filePath,
207
+ convertedFilePath: f.convertedFilePath,
208
+ uid: f.uid,
209
+ preview: f.type === 'image' ? f.filePath : undefined,
210
+ }));
211
+ } catch (e) {}
212
+ }
213
+ // 只有 reference 有内容时才 set,避免无意义清空
214
+ if (uploaded.length > 0) {
215
+ setFileManagerData({ uploadedFiles: uploaded });
216
+ }
217
+ // 否则不 set,保持上一次的 uploadedFiles
218
+ }, [currentChatData]);
219
+
220
+ // =================================================================
221
+ // Side Effects (useEffect) - 副作用钩子
222
+ // =================================================================
223
+
224
+ /**
225
+ * 跟踪 activeSessionId 的变化:
226
+ * 1. 清理上一个无消息的临时会话
227
+ * 2. 更新推荐问题
228
+ */
229
+ useEffect(() => {
230
+ // 切换会话时,检查上一个会话是否为临时且无消息
231
+ if (prevActiveSessionId.current && prevActiveSessionId.current !== activeSessionId) {
232
+ const prevId = prevActiveSessionId.current;
233
+ const prevConv = conversationList.find(c => c.sessionId === prevId && c.isNew);
234
+ const prevChat = chatData.find(c => c.id === prevId && c.isNew);
235
+ const prevHasMsg = prevChat && prevChat.messages && prevChat.messages.length > 0;
236
+ // 如果是临时的且没有消息,则自动删除
237
+ if (prevConv && !prevHasMsg) {
238
+ setConversationList(list => list.filter(c => c.sessionId !== prevId));
239
+ setChatData(list => list.filter(c => c.id !== prevId));
240
+ }
241
+ }
242
+ prevActiveSessionId.current = activeSessionId;
243
+
244
+ // 更新推荐问题为当前对话的最后一个推荐问题
245
+ const historyRQ =
246
+ currentChatData.messages && currentChatData.messages.length > 0
247
+ ? currentChatData.messages[currentChatData.messages.length - 1].recommendQuestion
248
+ : [];
249
+ setRecommandQuestions(historyRQ);
250
+ }, [activeSessionId, conversationList, chatData, currentChatData.messages]);
251
+
252
+ /**
253
+ * 1. useEffect 只拉助手列表,自动新建会话
254
+ */
255
+ useEffect(() => {
256
+ setAppStatus(prev => ({ ...prev, app: 'initializing' }));
257
+
258
+ if ( token) {
259
+ handleConversationCreate({ configId });
260
+ }
261
+ setAppStatus(prev => ({ ...prev, app: 'ready' }));
262
+ }, [url, token, configId]);
263
+
264
+ /**
265
+ * 当 activeSessionId 变化时,获取对应的聊天记录
266
+ */
267
+ useEffect(() => {
268
+ if (!activeSessionId) return;
269
+ setAppStatus(prev => ({ ...prev, display: 'loading' }));
270
+
271
+ setAppStatus(prev => ({ ...prev, display: 'ready' }));
272
+ }, [activeSessionId, url, token]);
273
+
274
+ /**
275
+ * 监听文件数据变化,更新Sender上的角标
276
+ */
277
+ useEffect(() => {
278
+ const totalFiles = fileManagerData.uploadedFiles.length;
279
+ setDynamicSenderConfig(prev => {
280
+ const newConfig = {
281
+ ...prev,
282
+ actions: prev.actions.map(action =>
283
+ action.name === 'history' ? { ...action, badgeCount: totalFiles } : action
284
+ ),
285
+ };
286
+ return newConfig;
287
+ });
288
+ }, [fileManagerData.uploadedFiles]);
289
+ useEffect(() => {
290
+ getFeedbackList({ url, token }, data => {
291
+ setFeedBackList(data);
292
+ });
293
+ }, []);
294
+ // =================================================================
295
+ // Core Functions - 核心功能函数
296
+ // =================================================================
297
+
298
+ // 1. 在组件作用域定义控制器和轮询停止标志
299
+ let uploadController: AbortController | null = null;
300
+ let stopPoll: (() => void) | null = null;
301
+
302
+ /**
303
+ * 轮询文件解析状态
304
+ * @param {string[]} uids - 文件UID列表
305
+ * @param {(serverStatuses: any[]) => void} onStatusUpdate - 每次获取到新状态后的回调
306
+ * @param {() => void} onComplete - 所有文件解析完成后的回调
307
+ * @param {(errorMsg: string) => void} onFail - 轮询失败或超时的回调
308
+ */
309
+ const pollFileStatus = (
310
+ uids: string[],
311
+ onStatusUpdate: (serverStatuses: any[]) => void,
312
+ onComplete: () => void,
313
+ onFail: (errorMsg: string) => void
314
+ ) => {
315
+ const uidsString = uids.join(',');
316
+ let pollCount = 0;
317
+ //const maxPollCount = maxPollCount;
318
+ stopPoll = () => {
319
+ stoppedRef.current = true;
320
+ };
321
+
322
+ const checkStatus = async () => {
323
+ if (stoppedRef.current) return;
324
+ if (pollCount >= maxPollCount) {
325
+ if (!stoppedRef.current) onFail('文件解析超时,请稍后重试');
326
+ return;
327
+ }
328
+ pollCount++;
329
+ try {
330
+ const res = await request.get(
331
+ `${url}/index/knowledgeBase/file/getFileStatusByUids?uids=${uidsString}`,
332
+ {
333
+ headers: { Authorization: token },
334
+ }
335
+ );
336
+ if (stoppedRef.current) return;
337
+ if (res.data.success) {
338
+ const statuses = res.data.data;
339
+ if (!stoppedRef.current) onStatusUpdate(statuses);
340
+ const allDone = statuses.every((file: any) => file.status === 3);
341
+ if (allDone) {
342
+ if (!stoppedRef.current) onComplete();
343
+ } else {
344
+ if (!stoppedRef.current) setTimeout(checkStatus, maxPollInterval);
345
+ }
346
+ } else {
347
+ if (!stoppedRef.current) onFail(res.data.errorMsg || '查询文件状态失败');
348
+ }
349
+ } catch (error) {
350
+ onError?.(error);
351
+ if (stoppedRef.current) return;
352
+ onFail('查询文件状态时发生网络错误');
353
+ }
354
+ };
355
+ checkStatus();
356
+ };
357
+
358
+ /**
359
+ * 设置消息的反馈结果(赞/踩)
360
+ * @param {string} queryId - 消息ID
361
+ * @param {number} result - 反馈结果 (1: 赞, -1: 踩)
362
+ */
363
+ const setFeed = (queryId: any, restult: any) => {
364
+ setChatData(chatList => {
365
+ const newList = [...chatList];
366
+ if (!newList[0]) return newList;
367
+ newList[0] = {
368
+ ...newList[0],
369
+ messages: newList[0].messages.map((item: any) => {
370
+ if (item.queryId === queryId) {
371
+ return {
372
+ ...item,
373
+ feedbackResult: restult,
374
+ };
375
+ }
376
+ return item;
377
+ }),
378
+ };
379
+ return newList;
380
+ });
381
+ };
382
+
383
+ /**
384
+ * 获取指定queryId的推荐问题
385
+ * @param {string} queryId - 消息ID
386
+ */
387
+ const fetchRecommendQuestions = async (queryId: string) => {
388
+ if (!queryId) return;
389
+ try {
390
+ const res = await request.get(`${url}/qa/ai/recommendQuestion?queryId=${queryId}`, {
391
+ headers: { Authorization: token },
392
+ });
393
+ setRecommandQuestions(res.data?.data || []);
394
+ } catch (e) {
395
+ onError?.(e);
396
+ setRecommandQuestions([]);
397
+ }
398
+ };
399
+
400
+ /**
401
+ * 处理重新发送消息逻辑
402
+ */
403
+ function handleReSenderSend(data: any) {
404
+ const { content, audioUrl, filePaths, queryId } = data;
405
+ if (!content) return;
406
+ const _fileUids = (
407
+ typeof filePaths === 'string' && filePaths ? JSON.parse(filePaths) : filePaths || []
408
+ ).map((item: any) => item.uid);
409
+ const now = Date.now();
410
+ setLastEmptyMessage({ now });
411
+ if (!activeSessionId) return;
412
+ setAppStatus(pre => {
413
+ return {
414
+ ...pre,
415
+ display: 'processing',
416
+ sender: 'processing',
417
+ };
418
+ });
419
+
420
+ data.clearFn && data.clearFn();
421
+ setConversationList(list =>
422
+ list.map(c => (c.sessionId === activeSessionId ? { ...c, gmtModified: now } : c))
423
+ );
424
+ api_startChat_re(
425
+ {
426
+ configId: configId || currentChatData.configId,
427
+ content,
428
+ fileUids: _fileUids,
429
+ lastDate: now,
430
+ name: currentChatData.label || '',
431
+ queryId: queryId,
432
+ sessionId: activeSessionId,
433
+ type: audioUrl ? 'audioUrl' : 'text',
434
+ audioUrl,
435
+ sysType: 'TEMPORARY',
436
+ },
437
+ gmtModified => {
438
+ fetchRecommendQuestions(queryId);
439
+ }
440
+ );
441
+ }
442
+
443
+ /**
444
+ * 处理消息发送(核心逻辑)
445
+ * 1. 包含文件:先上传文件,成功后再发消息
446
+ * 2. 新建会话:先调用创建会话接口,成功后再发消息
447
+ * 3. 普通消息:直接发送
448
+ * @param {object} data - 消息数据
449
+ */
450
+ async function handleSenderSend(data: any, clearFn: () => void) {
451
+ const queryId = 'query-' + uid(32);
452
+ // 1. 立即插入空壳消息
453
+ let localCurrentFiles = undefined;
454
+ if (data.files && data.files.length > 0) {
455
+ localCurrentFiles = data.files.map((fileItem: any) => ({
456
+ name: fileItem.file.name,
457
+ size: fileItem.file.size,
458
+ type: fileItem.file.type,
459
+ filePath: fileItem.filePath || '',
460
+ convertedFilePath: fileItem.convertedFilePath || '',
461
+ uid: fileItem.uid || '',
462
+ }));
463
+ }
464
+ addEmptyMessage({
465
+ content: data.content || data.text || '',
466
+ queryId,
467
+ currentFiles: localCurrentFiles,
468
+ filePaths: localCurrentFiles ? JSON.stringify(localCurrentFiles) : undefined,
469
+ });
470
+ setConversationList(list => {
471
+ const newList = list
472
+ .map(c =>
473
+ c.sessionId === activeSessionId ? { ...c, isNew: false, gmtModified: Date.now() } : c
474
+ )
475
+ .sort((a, b) => b.gmtModified - a.gmtModified);
476
+ setTimeout(() => {
477
+ setActiveSessionId(activeSessionId);
478
+ }, 300);
479
+ return newList;
480
+ });
481
+ // 2. 有文件时,先上传并轮询,全部解析成功后再发起AI请求
482
+ if (data.files && data.files.length > 0) {
483
+ setAppStatus(pre => ({ ...pre, sender: 'uploading', display: 'uploading' }));
484
+ uploadController = new AbortController();
485
+ stoppedRef.current = false;
486
+ const clientFileUids = data.files.map(
487
+ () => `rc-upload-${Date.now()}-${Math.random().toString(36).slice(2)}`
488
+ );
489
+ const initialFileStatuses = data.files.map((fileItem: any, index: number) => ({
490
+ ...fileItem,
491
+ uid: clientFileUids[index],
492
+ status: 'uploading',
493
+ }));
494
+ setFileStatuses(initialFileStatuses);
495
+ const formData = new FormData();
496
+ data.files.forEach((fileItem: any) => {
497
+ formData.append('files', fileItem.file);
498
+ });
499
+ const sessionIdStr = activeSessionId ? String(activeSessionId) : '';
500
+ formData.append('sessionId', sessionIdStr);
501
+ formData.append('uids', clientFileUids.join(','));
502
+ try {
503
+ const response = await request.post(
504
+ `${url}/index/knowledgeBase/file/uploadFiles`,
505
+ formData,
506
+ {
507
+ headers: {
508
+ Authorization: token,
509
+ 'Content-Type': 'multipart/form-data',
510
+ },
511
+ signal: uploadController.signal,
512
+ }
513
+ );
514
+ if (stoppedRef.current) return;
515
+ if (
516
+ response.data.success &&
517
+ Array.isArray(response.data.data) &&
518
+ response.data.data.length > 0
519
+ ) {
520
+ updateUserFilesMessage({
521
+ currentFiles: response.data.data.map((item: any) => {
522
+ return {
523
+ filePath: item.filePath,
524
+ name: item.fileName,
525
+ type: getNameFromFile(item.fileName || item.fileName),
526
+ };
527
+ }),
528
+ });
529
+ setAppStatus(pre => ({ ...pre, sender: 'processing', display: 'analyzing' }));
530
+ setFileStatuses(prev => prev.map(f => ({ ...f, status: 'parsing' })));
531
+ const uploadedFilesData = response.data.data;
532
+ const serverUids = uploadedFilesData.map((f: any) => f.uid);
533
+ pollFileStatus(
534
+ serverUids,
535
+ serverStatuses => {
536
+ setFileStatuses(currentLocalStatuses => {
537
+ return currentLocalStatuses.map(localFile => {
538
+ const correspondingServerFile = serverStatuses.find(
539
+ (sf: any) => sf.uid === localFile.uid
540
+ );
541
+ if (correspondingServerFile) {
542
+ switch (correspondingServerFile.status) {
543
+ case 3:
544
+ return { ...localFile, status: 'done' };
545
+ case 4:
546
+ return { ...localFile, status: 'error' };
547
+ case 1:
548
+ case 2:
549
+ default:
550
+ return { ...localFile, status: 'parsing' };
551
+ }
552
+ }
553
+ return localFile;
554
+ });
555
+ });
556
+ },
557
+ // onComplete: 全部解析成功后才发起AI请求
558
+ () => {
559
+ clearFn?.();
560
+ setAppStatus(pre => ({ ...pre, sender: 'processing', display: 'processing' }));
561
+ setFileStatuses(prev => prev.map(f => ({ ...f, status: 'finished' })));
562
+ const now = Date.now();
563
+ api_startChat_re(
564
+ {
565
+ configId: configId || currentChatData.configId,
566
+ content: data.content || data.text || '',
567
+ fileUids: serverUids,
568
+ lastDate: now,
569
+ name: currentChatData.label || '',
570
+ queryId: queryId,
571
+ sessionId: String(activeSessionId || ''),
572
+ type: 'text',
573
+ audioUrl: '',
574
+ sysType: 'TEMPORARY',
575
+ },
576
+ gmtModified => {
577
+ fetchRecommendQuestions(queryId);
578
+ }
579
+ );
580
+ },
581
+ (errorMsg: string) => {
582
+ setAppStatus(pre => ({ ...pre, display: 'ready', sender: 'ready' }));
583
+ setFileStatuses(prev =>
584
+ prev.map(f => ({ ...f, status: 'error', message: errorMsg }))
585
+ );
586
+ }
587
+ );
588
+ } else {
589
+ const errorMsg = response.data.errorMsg || '文件上传失败';
590
+ setAppStatus(pre => ({ ...pre, display: 'error', sender: 'ready' }));
591
+ setFileStatuses(prev => prev.map(f => ({ ...f, status: 'error', message: errorMsg })));
592
+ }
593
+ } catch (error) {
594
+ setAppStatus(pre => ({ ...pre, display: 'error', sender: 'ready' }));
595
+ setFileStatuses(prev => prev.map(f => ({ ...f, status: 'error', message: '网络错误' })));
596
+ }
597
+ return;
598
+ }
599
+ // 3. 无文件,直接发起AI请求
600
+ if (!activeSessionId) return;
601
+ setAppStatus(pre => ({ ...pre, display: 'processing', sender: 'processing' }));
602
+ const now = Date.now();
603
+ setConversationList(list =>
604
+ list.map(c => (c.sessionId === activeSessionId ? { ...c, gmtModified: now } : c))
605
+ );
606
+ clearFn?.();
607
+ api_startChat_re(
608
+ {
609
+ configId: configId || currentChatData.configId,
610
+ content: data.content || data.text || '',
611
+ fileUids: [],
612
+ lastDate: now,
613
+ name: currentChatData.label || '',
614
+ queryId: queryId,
615
+ sessionId: activeSessionId,
616
+ type: 'text',
617
+ audioUrl: '',
618
+ sysType: 'TEMPORARY',
619
+ },
620
+ gmtModified => {
621
+ fetchRecommendQuestions(queryId);
622
+ }
623
+ );
624
+ }
625
+
626
+ // =================================================================
627
+ // Event Handlers - 事件处理器
628
+ // =================================================================
629
+
630
+ /**
631
+ * 处理点赞反馈
632
+ */
633
+ function handleThumbsup(data: any) {
634
+ if (data.restult == 1) {
635
+ handleCancelFeedBack({ url, token }, { queryId: data.queryId }, () => {
636
+ setFeed(data.queryId, null);
637
+ });
638
+ } else {
639
+ handleFeedBack({ url, token }, { restult: 1, queryId: data.queryId }, () => {
640
+ setFeed(data.queryId, 1);
641
+ });
642
+ }
643
+ }
644
+
645
+ /**
646
+ * 处理新建一个临时会话
647
+ */
648
+ function handleConversationCreate(data: any) {
649
+ const sessionId = uid(32);
650
+ // 优先用 props.configId
651
+ const effectiveConfigId = configId || data?.configId || '';
652
+ const newConversation = {
653
+ sessionId,
654
+ label: '未命名会话',
655
+ gmtModified: Date.now(),
656
+ searchConfigDTO: {},
657
+ filePath: '',
658
+ configId: effectiveConfigId,
659
+ isNew: true,
660
+ };
661
+ setConversationList(list => [{ ...newConversation, messages: [] }, ...list]);
662
+ setChatData(list => [{ id: sessionId, messages: [], ...newConversation }, ...list]);
663
+ setActiveSessionId(sessionId);
664
+ // 自动触发新建会话接口
665
+
666
+ if ( token && url) {
667
+ request
668
+ .post(
669
+ `${url}/qa/dialogue/create`,
670
+ {
671
+ configId: effectiveConfigId,
672
+ label: `来自内嵌应用${uid(8)}`,
673
+ sessionId,
674
+ sysType: 'TEMPORARY',
675
+ },
676
+ {
677
+ headers: { Authorization: token },
678
+ }
679
+ )
680
+ .then(res => {
681
+ const newSession = res.data.data;
682
+ if (newSession?.sessionId) {
683
+ setConversationList(list => [
684
+ { ...newSession, messages: [] },
685
+ ...list.filter(c => c.sessionId !== sessionId),
686
+ ]);
687
+ setChatData(list => [
688
+ { id: newSession.sessionId, messages: [], ...newSession },
689
+ ...list.filter(c => c.id !== sessionId),
690
+ ]);
691
+ setActiveSessionId(newSession.sessionId);
692
+ }
693
+ })
694
+ .catch(err => {
695
+ onError?.(err);
696
+ });
697
+ }
698
+ }
699
+
700
+ /**
701
+ * 文件管理器:处理文件选择后的上传
702
+ */
703
+ const handleFileManagerUpload = (fileList: FileList) => {
704
+ // 1. 收集所有已经存在的文件名
705
+ const existingFileNames = new Set<string>([
706
+ ...fileManagerData.uploadedFiles.map((f: any) => f.file.name as string),
707
+ ...pendingFiles.map((f: any) => f.file.name as string),
708
+ ]);
709
+
710
+ const newFiles: { file: File; type: string; status: string; preview?: string }[] = [];
711
+ const duplicateFileNames = new Set<string>();
712
+
713
+ // 2. 遍历新选择的文件,筛选出重复项和有效项
714
+ Array.from(fileList).forEach(file => {
715
+ if (existingFileNames.has(file.name)) {
716
+ duplicateFileNames.add(file.name);
717
+ } else {
718
+ newFiles.push({
719
+ file,
720
+ type: getFileTypeByName(file.name),
721
+ status: 'pending',
722
+ preview: file.type.startsWith('image') ? URL.createObjectURL(file) : undefined,
723
+ });
724
+ existingFileNames.add(file.name); // 添加到Set中,以防本次上传的文件之间有重复
725
+ }
726
+ });
727
+
728
+ // 3. 如果有重复,发出警告
729
+ if (duplicateFileNames.size > 0) {
730
+ message.warning(`不能上传同名文件: ${Array.from(duplicateFileNames).join(', ')}`);
731
+ }
732
+
733
+ // 4. 将有效的文件添加到待上传列表
734
+ if (newFiles.length > 0) {
735
+ setPendingFiles(prev => [...prev, ...newFiles]);
736
+ // 直接同步到核心发送器组件
737
+ setUploadedFilesRef.current?.(newFiles);
738
+ }
739
+ };
740
+
741
+ /**
742
+ * 文件管理器:处理文件移除
743
+ */
744
+ const handleFileManagerRemove = (fileObj: any, idx: number, _type: string) => {
745
+ removeFileRef.current?.(fileObj, idx, _type);
746
+ };
747
+
748
+ /**
749
+ * 处理会话列表项点击
750
+ */
751
+ function handleItemClick(data: any) {
752
+ if (data?.sessionId && data.sessionId !== activeSessionId) {
753
+ setActiveSessionId(data.sessionId);
754
+ setAppStatus(prev => ({ ...prev, sender: 'ready' }));
755
+ // 点击会话时,使用其最后一条AI消息的推荐问题
756
+ const conv = chatData.find(c => c.id === data.sessionId);
757
+ if (conv && conv.messages && conv.messages.length > 0) {
758
+ const lastAiMsg = [...conv.messages].reverse().find((m: any) => m.istype === 'ai');
759
+ if (lastAiMsg && lastAiMsg.recommendQuestion) {
760
+ setRecommandQuestions(lastAiMsg.recommendQuestion);
761
+ } else {
762
+ setRecommandQuestions([]);
763
+ }
764
+ } else {
765
+ setRecommandQuestions([]);
766
+ }
767
+ }
768
+ }
769
+
770
+ // =================================================================
771
+ // Local State Updaters - 本地状态更新 (供事件发射器使用)
772
+ // =================================================================
773
+
774
+ const handleFeedDownConfirm = (data: any) => {
775
+ handleFeedBack({ url, token, }, { ...data, ...feedParam, restult: 0 }, () => {
776
+ setFeed(feedParam.queryId, 0);
777
+ });
778
+ };
779
+
780
+ // =================================================================
781
+ // Event Emitter - 事件分发器
782
+ // =================================================================
783
+ /**
784
+ * 统一事件分发器,连接业务逻辑与所有子组件的事件
785
+ * @param {string} eventName - 事件名称
786
+ * @param {any} data - 事件数据
787
+ */
788
+ const mergedEventsEmit = (eventName: string, data: any) => {
789
+ console.log(`[Event] ${eventName}`, data);
790
+ switch (eventName) {
791
+ // --- 对话列表事件 ---
792
+ case 'conversation:item_click':
793
+ handleItemClick(data);
794
+ break;
795
+
796
+ case 'files:finished_animation_complete':
797
+ setFileStatuses([]);
798
+ break;
799
+
800
+ case 'conversation:create':
801
+ handleConversationCreate(data);
802
+ break;
803
+ case 'conversation:new_assistant_change':
804
+ setNewAssistantId(data.assistantId);
805
+ break;
806
+
807
+ // --- 发送器事件 ---
808
+ case 'sender:send':
809
+ handleSenderSend(data, data.clearFn);
810
+ break;
811
+ case 'sender:send_recommandQuestion':
812
+ // 点击推荐问题,视作一次普通发送
813
+ handleSenderSend(data, data.clearFn);
814
+ break;
815
+ case 'sender:action_history':
816
+ setFileManagerOpen(true);
817
+ break;
818
+ case 'sender:stop':
819
+ stoppedRef.current = true;
820
+ if (uploadController) {
821
+ uploadController.abort();
822
+ uploadController = null;
823
+ }
824
+ if (stopPoll) {
825
+ stopPoll();
826
+ stopPoll = null;
827
+ }
828
+ setAppStatus(pre => ({ ...pre, display: 'ready', sender: 'ready' }));
829
+ setFileStatuses([]);
830
+ stopStream?.();
831
+ break;
832
+ case 'sender:clear':
833
+ data.clearFn?.();
834
+ break;
835
+ case 'sender:configChange':
836
+ // 配置项变化,可在此处理
837
+ break;
838
+
839
+ // --- 消息卡片事件 ---
840
+ case 'action_copy:click':
841
+ if (data) {
842
+ toCopy(data.content, data.event);
843
+ }
844
+ break;
845
+ case 'action_retry':
846
+ handleReSenderSend(data);
847
+ break;
848
+ case 'action_thumbsup':
849
+ handleThumbsup(data);
850
+ break;
851
+ case 'action_thumbsdown':
852
+ if (data.restult === 0) {
853
+ handleCancelFeedBack({ url, token }, { queryId: data.queryId }, () => {
854
+ setFeed(data.queryId, null);
855
+ });
856
+ } else {
857
+ setFeedParam(data);
858
+ setOpenFeed(true);
859
+ }
860
+ break;
861
+ // --- 文件管理器上传文件删除事件,透传到业务层 ---
862
+ case 'uploaded_file:removeFromTemp': {
863
+ console.log('[DEBUG] mergedEventsEmit uploaded_file:removeFromTemp 事件', data);
864
+
865
+ const uid = data?.file?.uid;
866
+ if (!uid) {
867
+ console.warn('[DEBUG] 缺少uid,无法删除临时文件', data);
868
+ break;
869
+ }
870
+ // 获取当前对话最后一条 AI 消息的 queryId
871
+ const lastAiMsg = [...(currentChatData.messages || [])]
872
+ .reverse()
873
+ .find((msg: any) => msg.istype === 'user');
874
+ const queryId = lastAiMsg?.queryId;
875
+ console.log('[DEBUG] mergedEventsEmit 获取到 queryId', queryId, lastAiMsg);
876
+
877
+ removeFilefromTempUpload({ url, token }, { uid, queryId }, (success, res) => {
878
+ console.log('[DEBUG] removeFilefromTempUpload 回调触发', { success, res });
879
+ if (success) {
880
+ message.success('文件已从临时知识库移除');
881
+ // 用后端返回的新data数组(新文件列表)更新本地uploadedFiles
882
+ //console.log('[DEBUG] removeFilefromTempUpload 回调 true,res:', res, Array.isArray(res.data && res.data.data));
883
+ if (res && Array.isArray(res.data && res.data.data)) {
884
+ const uploaded = res.data.data.map((f: any) => {
885
+ const fileName = f.name || f.fileName || '';
886
+ return {
887
+ file: {
888
+ name: fileName,
889
+ size: Number(f.size?.replace('MB', '')) * 1024 * 1024 || 0,
890
+ type: f.type,
891
+ } as File,
892
+ type: getFileTypeByName(fileName),
893
+ status: 'uploaded' as const,
894
+ url: f.filePath,
895
+ convertedFilePath: f.convertedFilePath,
896
+ uid: f.uid,
897
+ preview: f.type === 'image' ? f.filePath : undefined,
898
+ };
899
+ });
900
+ // console.log('[DEBUG] uploaded 构造完成:', uploaded, '当前 fileManagerData:', fileManagerData);
901
+ setFileManagerData({ uploadedFiles: uploaded });
902
+ } else {
903
+ //console.log('[DEBUG] res.data.data 不是数组', res && res.data && res.data.data);
904
+ }
905
+ } else {
906
+ console.log('[DEBUG] removeFilefromTempUpload 回调 false,res:', res);
907
+ message.error('文件移除失败');
908
+ }
909
+ });
910
+ break;
911
+ }
912
+ // --- 文件管理器事件 ---
913
+ case 'fileManager:change': {
914
+ const files = Array.isArray(data?.uploadedFiles) ? data.uploadedFiles : [];
915
+ setPendingFiles(files);
916
+ // 保存核心组件传递的文件处理函数引用
917
+ if (typeof data?.setUploadedFiles === 'function') {
918
+ setUploadedFilesRef.current = data.setUploadedFiles;
919
+ removeFileRef.current = data.removeFile;
920
+ }
921
+ break;
922
+ }
923
+
924
+ default:
925
+ // 其他未处理事件可继续向上层抛出
926
+ eventsEmit?.(eventName, data);
927
+ }
928
+ };
929
+
930
+ // =================================================================
931
+ // Component & UI Configuration - 组件及UI配置
932
+ // =================================================================
933
+
934
+ /**
935
+ * Sidebar 配置
936
+ */
937
+ const sidebar = undefined;
938
+
939
+ /**
940
+ * 业务层自定义组件
941
+ */
942
+ const businessCustomComponents: { [key: string]: any } = {
943
+ AiChatBox: (msg: any, idx: number) => {
944
+ const isLastMessage = idx === (currentChatData?.messages?.length || 0) - 1;
945
+
946
+ // 根据消息的 status 和 appStatus 来确定 displayStatus
947
+ let displayStatus = 'ready';
948
+ if (isLastMessage) {
949
+ // 优先使用消息的 status 字段
950
+ if (msg.status !== undefined) {
951
+ switch (msg.status) {
952
+ case 0: // StatusType.Pending
953
+ displayStatus = 'thinking';
954
+ break;
955
+ case 1: // StatusType.Process
956
+ displayStatus = 'processing';
957
+ break;
958
+ case 2: // StatusType.Done
959
+ displayStatus = 'ready';
960
+ break;
961
+ case 3: // StatusType.Error
962
+ displayStatus = 'error';
963
+ break;
964
+ default:
965
+ displayStatus = appStatus.display;
966
+ }
967
+ } else {
968
+ // 如果没有 status 字段,使用 appStatus.display
969
+ displayStatus = appStatus.display;
970
+ }
971
+ }
972
+ // console.log('AiChatBox', msg);
973
+ return (
974
+ <AiChatBox
975
+ {...msg}
976
+ displayStatus={displayStatus}
977
+ eventsEmit={mergedEventsEmit}
978
+ defaultAnswer={
979
+ currentChatData?.searchConfigDTO?.defaultAnswer ||
980
+ '不知道什么原因我们没能查到你要的问题*-*'
981
+ }
982
+ styles={styles}
983
+ from="headless"
984
+ />
985
+ );
986
+ },
987
+ LogoBox: () => <div className="text-2xl w-full text-center font-bold">小鲸智能助手</div>,
988
+ DisplayLoading: () => <DisplayLoading />,
989
+ DisplayError: () => <DisplayError />,
990
+ AppError: () => <AppError />,
991
+ AppLoading: () => <AppLoading />,
992
+ UserChatBox: (msg: any, idx: number) => {
993
+ // console.log('UserChatBox files:', msg.files, 'currentFiles:', msg.currentFiles, 'filePaths:', msg.filePaths);
994
+ return (
995
+ <UserChatBox
996
+ {...msg}
997
+ eventsEmit={mergedEventsEmit}
998
+ isLast={idx === (currentChatData?.messages?.length || 0) - 2}
999
+ styles={styles}
1000
+ idx={idx}
1001
+ />
1002
+ );
1003
+ },
1004
+ WelcomeComponent: () => (
1005
+ <GientechNewChatWelcome
1006
+ eventsEmit={mergedEventsEmit}
1007
+ assistantList={assistantList}
1008
+ styles={styles}
1009
+ // defaultSelect={newAssistantId || assistantList[0]?.id}
1010
+ />
1011
+ ),
1012
+ };
1013
+
1014
+ /**
1015
+ * 合并外部传入和业务默认的自定义组件,外部优先
1016
+ */
1017
+ const mergedCustomComponents: { [key: string]: any } = { ...businessCustomComponents };
1018
+ if (rest.CustomComponents) {
1019
+ Object.keys(rest.CustomComponents).forEach(key => {
1020
+ const val = rest.CustomComponents?.[key];
1021
+ if (val !== undefined) {
1022
+ mergedCustomComponents[key] = val;
1023
+ }
1024
+ });
1025
+ }
1026
+
1027
+ // =================================================================
1028
+ // Render - 渲染
1029
+ // =================================================================
1030
+ return (
1031
+ <>
1032
+ <FileManager
1033
+ open={fileManagerOpen}
1034
+ onCancel={() => setFileManagerOpen(false)}
1035
+ files={fileManagerData.uploadedFiles}
1036
+ currentFiles={pendingFiles}
1037
+ onUpload={handleFileManagerUpload}
1038
+ onRemoveFile={handleFileManagerRemove}
1039
+ styles={styles}
1040
+ eventsEmit={mergedEventsEmit}
1041
+ />
1042
+
1043
+ <WrappedComponent
1044
+ activeSessionId={activeSessionId}
1045
+ status={appStatus}
1046
+ chatData={currentChatData}
1047
+ sidebar={sidebar}
1048
+ recommandQuestions={recommandQuestions}
1049
+ eventsEmit={mergedEventsEmit}
1050
+ styles={styles}
1051
+ senderConfig={dynamicSenderConfig}
1052
+ fileUploadStatus={fileStatuses}
1053
+ CustomComponents={mergedCustomComponents} />
1054
+ <FeedBackModal
1055
+ open={openFeed}
1056
+ feedBackList={feedBackList}
1057
+ setOpen={setOpenFeed}
1058
+ handleConfirm={handleFeedDownConfirm}
1059
+ />
1060
+ </>
1061
+ );
1062
+ };
1063
+ }