chat.html 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  6. <title th:text="${nickname}">Solon Code Web</title>
  7. <link rel="stylesheet" href="/js/a/layui/css/layui.css"/>
  8. <link rel="stylesheet" href="/js/a/css/theme.css"/>
  9. <style>
  10. :root {
  11. --sidebar-width: 260px;
  12. }
  13. *, *::before, *::after {
  14. box-sizing: border-box;
  15. margin: 0;
  16. padding: 0;
  17. }
  18. html, body {
  19. height: 100%;
  20. overflow: hidden;
  21. }
  22. body {
  23. font-family: var(--font-sans);
  24. background: var(--bg-body);
  25. color: var(--text-primary);
  26. display: flex;
  27. font-size: 14px;
  28. transition: background 0.3s, color 0.3s;
  29. }
  30. /* ===== Sidebar ===== */
  31. .sidebar {
  32. width: var(--sidebar-width);
  33. background: var(--bg-sidebar);
  34. border-right: 1px solid var(--border-color);
  35. display: flex;
  36. flex-direction: column;
  37. flex-shrink: 0;
  38. height: 100vh;
  39. transition: background 0.3s;
  40. }
  41. .sidebar-header {
  42. padding: 20px 16px 16px;
  43. }
  44. .new-chat-btn {
  45. width: 100%;
  46. display: flex;
  47. align-items: center;
  48. justify-content: center;
  49. gap: 8px;
  50. padding: 11px 0;
  51. border: 1px dashed var(--border-color);
  52. border-radius: 10px;
  53. background: transparent;
  54. color: var(--text-primary);
  55. cursor: pointer;
  56. font-size: 14px;
  57. transition: all 0.2s;
  58. }
  59. .new-chat-btn:hover {
  60. background: var(--accent-light);
  61. border-color: var(--accent);
  62. color: var(--accent);
  63. }
  64. .sidebar-title {
  65. padding: 16px 16px 8px;
  66. font-size: 11px;
  67. color: var(--text-secondary);
  68. font-weight: 600;
  69. letter-spacing: 1px;
  70. text-transform: uppercase;
  71. }
  72. .sidebar-list {
  73. flex: 1;
  74. overflow-y: auto;
  75. padding: 4px 10px;
  76. }
  77. .sidebar-item {
  78. padding: 10px 12px;
  79. border-radius: 10px;
  80. cursor: pointer;
  81. color: var(--text-sidebar);
  82. font-size: 13px;
  83. display: flex;
  84. align-items: center;
  85. transition: all 0.15s;
  86. margin-bottom: 2px;
  87. }
  88. .sidebar-item:hover {
  89. background: var(--bg-hover);
  90. }
  91. .sidebar-item.active {
  92. background: var(--accent-light);
  93. color: var(--accent);
  94. font-weight: 500;
  95. }
  96. .sidebar-item-label {
  97. flex: 1;
  98. min-width: 0;
  99. white-space: nowrap;
  100. overflow: hidden;
  101. text-overflow: ellipsis;
  102. }
  103. .sidebar-item-del {
  104. display: none;
  105. flex-shrink: 0;
  106. width: 22px;
  107. height: 22px;
  108. border: none;
  109. background: none;
  110. border-radius: 6px;
  111. cursor: pointer;
  112. color: var(--text-secondary);
  113. font-size: 14px;
  114. align-items: center;
  115. justify-content: center;
  116. margin-left: 4px;
  117. transition: all 0.15s;
  118. }
  119. .sidebar-item:hover .sidebar-item-del {
  120. display: flex;
  121. }
  122. .sidebar-item-del:hover {
  123. background: #ef444422;
  124. color: #ef4444;
  125. }
  126. .sidebar-footer {
  127. padding: 14px 16px;
  128. border-top: 1px solid var(--border-color);
  129. display: flex;
  130. flex-direction: column;
  131. align-items: stretch;
  132. gap: 12px;
  133. }
  134. .sidebar-footer-meta {
  135. display: flex;
  136. align-items: center;
  137. gap: 10px;
  138. }
  139. .sidebar-config-btn {
  140. width: 100%;
  141. display: flex;
  142. align-items: center;
  143. justify-content: center;
  144. gap: 8px;
  145. padding: 10px 12px;
  146. border: 1px solid var(--border-color);
  147. border-radius: 10px;
  148. background: var(--bg-main);
  149. color: var(--text-primary);
  150. cursor: pointer;
  151. font-size: 13px;
  152. transition: all 0.2s;
  153. }
  154. .sidebar-config-btn:hover {
  155. border-color: var(--accent);
  156. background: var(--accent-light);
  157. color: var(--accent);
  158. }
  159. .model-modal {
  160. position: fixed;
  161. inset: 0;
  162. display: none;
  163. align-items: center;
  164. justify-content: center;
  165. padding: 24px;
  166. background: rgba(15, 23, 42, 0.45);
  167. z-index: 999;
  168. }
  169. .model-modal.active {
  170. display: flex;
  171. }
  172. .model-modal-card {
  173. width: min(680px, 100%);
  174. max-height: 90vh;
  175. overflow: hidden;
  176. display: flex;
  177. flex-direction: column;
  178. border: 1px solid var(--border-color);
  179. border-radius: 18px;
  180. background: var(--bg-input-box);
  181. box-shadow: var(--shadow-lg);
  182. }
  183. .model-modal-header {
  184. display: flex;
  185. align-items: center;
  186. justify-content: space-between;
  187. padding: 18px 20px 14px;
  188. border-bottom: 1px solid var(--border-color);
  189. }
  190. .model-modal-title {
  191. font-size: 18px;
  192. font-weight: 700;
  193. color: var(--text-primary);
  194. }
  195. .model-modal-close {
  196. width: 34px;
  197. height: 34px;
  198. border: none;
  199. border-radius: 10px;
  200. background: transparent;
  201. color: var(--text-secondary);
  202. cursor: pointer;
  203. transition: all 0.2s;
  204. }
  205. .model-modal-close:hover {
  206. background: var(--bg-hover);
  207. color: var(--text-primary);
  208. }
  209. .model-modal-body {
  210. padding: 18px 20px;
  211. overflow-y: auto;
  212. }
  213. .model-form-grid {
  214. display: grid;
  215. grid-template-columns: repeat(2, minmax(0, 1fr));
  216. gap: 16px;
  217. }
  218. .model-field {
  219. display: flex;
  220. flex-direction: column;
  221. gap: 8px;
  222. }
  223. .model-field.full {
  224. grid-column: 1 / -1;
  225. }
  226. .model-field label {
  227. font-size: 13px;
  228. font-weight: 600;
  229. color: var(--text-primary);
  230. }
  231. .model-field input,
  232. .model-field select,
  233. .model-field textarea {
  234. width: 100%;
  235. border: 1px solid var(--border-input);
  236. border-radius: 12px;
  237. background: var(--bg-main);
  238. color: var(--text-primary);
  239. padding: 11px 12px;
  240. font: inherit;
  241. outline: none;
  242. transition: all 0.2s;
  243. }
  244. .model-field input:focus,
  245. .model-field select:focus,
  246. .model-field textarea:focus {
  247. border-color: var(--accent);
  248. box-shadow: 0 0 0 4px rgba(79, 110, 247, 0.08);
  249. }
  250. .model-field textarea {
  251. min-height: 84px;
  252. resize: vertical;
  253. }
  254. .model-save-btn,
  255. .model-cancel-btn {
  256. height: 44px;
  257. border-radius: 12px;
  258. font-size: 14px;
  259. cursor: pointer;
  260. transition: all 0.2s;
  261. }
  262. .model-modal-footer {
  263. padding: 16px 20px 20px;
  264. border-top: 1px solid var(--border-color);
  265. display: flex;
  266. justify-content: flex-end;
  267. gap: 12px;
  268. }
  269. .model-cancel-btn {
  270. min-width: 92px;
  271. border: 1px solid var(--border-color);
  272. background: transparent;
  273. color: var(--text-secondary);
  274. }
  275. .model-cancel-btn:hover {
  276. background: var(--bg-hover);
  277. color: var(--text-primary);
  278. }
  279. .model-save-btn {
  280. min-width: 120px;
  281. border: none;
  282. background: var(--accent);
  283. color: #fff;
  284. }
  285. .model-save-btn:hover {
  286. background: var(--accent-hover);
  287. }
  288. .model-switch {
  289. display: flex;
  290. align-items: center;
  291. gap: 10px;
  292. padding-top: 8px;
  293. color: var(--text-primary);
  294. }
  295. .model-switch input {
  296. width: 16px;
  297. height: 16px;
  298. }
  299. .model-hint {
  300. margin-top: 4px;
  301. font-size: 12px;
  302. color: var(--text-secondary);
  303. }
  304. /* Streaming indicator for sidebar items */
  305. .sidebar-item.streaming {
  306. border-left: 3px solid var(--accent);
  307. padding-left: 9px;
  308. }
  309. .sidebar-item.streaming:hover .sidebar-item-del {
  310. display: none;
  311. }
  312. .sidebar-item-spinner {
  313. flex-shrink: 0;
  314. width: 16px;
  315. height: 16px;
  316. border: 2px solid var(--border-color);
  317. border-top-color: var(--accent);
  318. border-radius: 50%;
  319. animation: blob-spin 0.8s linear infinite;
  320. margin-left: 6px;
  321. }
  322. /* ===== Main ===== */
  323. .main-area {
  324. flex: 1;
  325. display: flex;
  326. flex-direction: column;
  327. height: 100vh;
  328. min-width: 0;
  329. background: var(--bg-main);
  330. transition: background 0.3s;
  331. }
  332. /* Header */
  333. .main-header {
  334. padding: 14px 28px;
  335. display: flex;
  336. align-items: center;
  337. justify-content: flex-end;
  338. flex-shrink: 0;
  339. }
  340. .theme-toggle {
  341. background: none;
  342. border: 1px solid var(--border-color);
  343. border-radius: 10px;
  344. padding: 6px 10px;
  345. cursor: pointer;
  346. color: var(--text-secondary);
  347. font-size: 16px;
  348. transition: all 0.2s;
  349. display: flex;
  350. align-items: center;
  351. }
  352. .theme-toggle:hover {
  353. background: var(--bg-hover);
  354. color: var(--text-primary);
  355. }
  356. /* ===== Welcome ===== */
  357. .welcome-view {
  358. flex: 1;
  359. display: flex;
  360. flex-direction: column;
  361. align-items: center;
  362. justify-content: center;
  363. padding: 0 24px 80px;
  364. }
  365. .welcome-avatar {
  366. width: 120px;
  367. height: 120px;
  368. position: relative;
  369. margin-bottom: 28px;
  370. display: flex;
  371. align-items: center;
  372. justify-content: center;
  373. }
  374. .welcome-avatar .blob {
  375. position: absolute;
  376. }
  377. .welcome-avatar .blob-outer {
  378. width: 110px;
  379. height: 110px;
  380. background: conic-gradient(from 0deg, #4f6ef7, #818cf8 15%, #a78bfa 30%, #c084fc 50%, #818cf8 70%, #4f6ef7 85%, #4f6ef7);
  381. border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
  382. animation: blob-morph1 8s ease-in-out infinite, blob-spin 6s linear infinite;
  383. opacity: 0.82;
  384. filter: blur(1.5px) drop-shadow(0 0 14px rgba(79, 110, 247, 0.4));
  385. }
  386. .welcome-avatar .blob-core {
  387. width: 38px;
  388. height: 38px;
  389. z-index: 1;
  390. border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
  391. background: linear-gradient(135deg, var(--accent), #a78bfa, #c084fc, var(--accent));
  392. background-size: 300% 300%;
  393. animation: core-flow 4s ease infinite, core-morph 5s ease-in-out infinite, core-pulse 3s ease-in-out infinite;
  394. box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
  395. filter: blur(0.5px);
  396. }
  397. @keyframes blob-morph1 {
  398. 0%, 100% {
  399. border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
  400. }
  401. 25% {
  402. border-radius: 30% 60% 70% 40% / 50% 60% 30% 60%;
  403. }
  404. 50% {
  405. border-radius: 50% 60% 30% 60% / 30% 50% 70% 60%;
  406. }
  407. 75% {
  408. border-radius: 40% 60% 50% 40% / 60% 40% 60% 50%;
  409. }
  410. }
  411. @keyframes blob-spin {
  412. to {
  413. transform: rotate(360deg);
  414. }
  415. }
  416. @keyframes core-flow {
  417. 0% {
  418. background-position: 0% 50%;
  419. }
  420. 50% {
  421. background-position: 100% 50%;
  422. }
  423. 100% {
  424. background-position: 0% 50%;
  425. }
  426. }
  427. @keyframes core-morph {
  428. 0%, 100% {
  429. border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
  430. }
  431. 20% {
  432. border-radius: 35% 55% 40% 60% / 55% 35% 60% 40%;
  433. }
  434. 40% {
  435. border-radius: 60% 35% 50% 45% / 35% 60% 45% 55%;
  436. }
  437. 60% {
  438. border-radius: 40% 60% 35% 55% / 60% 40% 55% 35%;
  439. }
  440. 80% {
  441. border-radius: 55% 45% 55% 40% / 45% 50% 40% 60%;
  442. }
  443. }
  444. @keyframes core-pulse {
  445. 0%, 100% {
  446. transform: scale(1);
  447. box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
  448. }
  449. 50% {
  450. transform: scale(1.1);
  451. box-shadow: 0 0 28px rgba(79, 110, 247, 0.55), 0 0 56px rgba(167, 139, 250, 0.3);
  452. }
  453. }
  454. .welcome-title {
  455. font-size: 26px;
  456. font-weight: 700;
  457. margin-bottom: 10px;
  458. }
  459. .welcome-sub {
  460. color: var(--text-secondary);
  461. font-size: 15px;
  462. margin-bottom: 24px;
  463. }
  464. /* Quick Questions */
  465. .quick-questions {
  466. width: 100%;
  467. max-width: 680px;
  468. margin-bottom: 16px;
  469. display: flex;
  470. flex-wrap: wrap;
  471. gap: 8px;
  472. }
  473. .quick-q-chip {
  474. padding: 6px 14px;
  475. border: 1px solid var(--border-color);
  476. border-radius: 20px;
  477. background: var(--bg-input-box);
  478. color: var(--text-secondary);
  479. font-size: 13px;
  480. cursor: pointer;
  481. transition: all 0.2s;
  482. white-space: nowrap;
  483. max-width: 320px;
  484. overflow: hidden;
  485. text-overflow: ellipsis;
  486. }
  487. .quick-q-chip:hover {
  488. border-color: var(--accent);
  489. color: var(--accent);
  490. background: var(--accent-light);
  491. }
  492. .welcome-input-box {
  493. width: 100%;
  494. max-width: 680px;
  495. background: var(--bg-input-box);
  496. border: 1px solid var(--border-input);
  497. border-radius: 16px;
  498. box-shadow: var(--shadow-lg);
  499. display: flex;
  500. flex-direction: column;
  501. transition: all 0.25s;
  502. }
  503. .welcome-input-box:focus-within {
  504. border-color: var(--accent);
  505. box-shadow: var(--shadow-lg), 0 0 0 4px rgba(79, 110, 247, 0.08);
  506. }
  507. .welcome-input-box textarea {
  508. border: none;
  509. outline: none;
  510. resize: none;
  511. background: transparent;
  512. padding: 18px 20px 8px;
  513. font-family: var(--font-sans);
  514. font-size: 15px;
  515. color: var(--text-primary);
  516. min-height: 54px;
  517. max-height: 140px;
  518. line-height: 1.6;
  519. }
  520. .welcome-input-box textarea::placeholder {
  521. color: var(--text-secondary);
  522. }
  523. .welcome-toolbar {
  524. display: flex;
  525. align-items: center;
  526. justify-content: flex-end;
  527. padding: 8px 14px;
  528. }
  529. .welcome-send-btn {
  530. width: 40px;
  531. height: 40px;
  532. border-radius: 50%;
  533. border: none;
  534. background: var(--accent);
  535. color: #fff;
  536. cursor: pointer;
  537. display: flex;
  538. align-items: center;
  539. justify-content: center;
  540. font-size: 18px;
  541. transition: all 0.2s;
  542. }
  543. .welcome-send-btn:hover {
  544. background: var(--accent-hover);
  545. transform: scale(1.06);
  546. }
  547. /* ===== Chat View ===== */
  548. .chat-view {
  549. flex: 1;
  550. display: none;
  551. flex-direction: column;
  552. min-height: 0;
  553. }
  554. .chat-view.active {
  555. display: flex;
  556. }
  557. .messages-wrap {
  558. flex: 1;
  559. overflow-y: auto;
  560. }
  561. .messages-inner {
  562. max-width: 820px;
  563. margin: 0 auto;
  564. padding: 28px 24px 12px;
  565. }
  566. /* Message */
  567. .msg-row {
  568. display: flex;
  569. margin-bottom: 28px;
  570. gap: 14px;
  571. animation: msg-in 0.25s ease-out;
  572. }
  573. @keyframes msg-in {
  574. from {
  575. opacity: 0;
  576. transform: translateY(8px);
  577. }
  578. to {
  579. opacity: 1;
  580. transform: none;
  581. }
  582. }
  583. .msg-row.user {
  584. justify-content: flex-end;
  585. }
  586. .msg-row.assistant {
  587. justify-content: flex-start;
  588. }
  589. .msg-avatar {
  590. width: 38px;
  591. height: 38px;
  592. border-radius: 12px;
  593. flex-shrink: 0;
  594. display: flex;
  595. align-items: center;
  596. justify-content: center;
  597. font-size: 14px;
  598. font-weight: 600;
  599. }
  600. .msg-row.assistant .msg-avatar {
  601. background: linear-gradient(135deg, var(--accent), #a78bfa);
  602. color: #fff;
  603. }
  604. .msg-row.user .msg-avatar {
  605. background: var(--bg-hover);
  606. color: var(--text-secondary);
  607. }
  608. .msg-bubble {
  609. max-width: 75%;
  610. line-height: 1.75;
  611. word-break: break-word;
  612. }
  613. .msg-row.user .msg-bubble {
  614. background: var(--bg-user-msg);
  615. color: var(--text-user-msg);
  616. padding: 12px 18px;
  617. border-radius: 20px 20px 4px 20px;
  618. font-size: 14px;
  619. }
  620. .msg-row.assistant .msg-bubble {
  621. max-width: 88%;
  622. min-width: 88%;
  623. }
  624. /* Markdown */
  625. .md-content {
  626. padding: 2px 0;
  627. color: var(--text-ai-msg);
  628. font-size: 14px;
  629. }
  630. .md-content > :first-child {
  631. margin-top: 0;
  632. }
  633. .md-content > :last-child {
  634. margin-bottom: 0;
  635. }
  636. .md-content p {
  637. margin: 7px 0;
  638. }
  639. .md-content h1, .md-content h2, .md-content h3, .md-content h4 {
  640. margin: 16px 0 8px;
  641. font-weight: 600;
  642. color: var(--text-primary);
  643. }
  644. .md-content h1 {
  645. font-size: 1.35em;
  646. }
  647. .md-content h2 {
  648. font-size: 1.2em;
  649. }
  650. .md-content h3 {
  651. font-size: 1.08em;
  652. }
  653. .md-content strong {
  654. font-weight: 600;
  655. }
  656. .md-content code {
  657. background: var(--bg-code);
  658. padding: 2px 7px;
  659. border-radius: 5px;
  660. font-family: var(--font-mono);
  661. font-size: 0.87em;
  662. }
  663. .md-content pre {
  664. background: var(--bg-code-block);
  665. padding: 16px 18px;
  666. border-radius: 10px;
  667. overflow-x: auto;
  668. margin: 10px 0;
  669. }
  670. .md-content pre code {
  671. background: none;
  672. padding: 0;
  673. color: var(--text-code);
  674. font-size: 13px;
  675. }
  676. .md-content ul, .md-content ol {
  677. padding-left: 22px;
  678. margin: 7px 0;
  679. }
  680. .md-content li {
  681. margin: 4px 0;
  682. }
  683. .md-content blockquote {
  684. border-left: 3px solid var(--accent);
  685. padding-left: 14px;
  686. color: var(--text-secondary);
  687. margin: 10px 0;
  688. }
  689. .md-content a {
  690. color: var(--accent);
  691. text-decoration: none;
  692. }
  693. .md-content a:hover {
  694. text-decoration: underline;
  695. }
  696. .md-content table {
  697. border-collapse: collapse;
  698. margin: 10px 0;
  699. width: 100%;
  700. font-size: 13px;
  701. }
  702. .md-content th, .md-content td {
  703. border: 1px solid var(--border-color);
  704. padding: 7px 12px;
  705. text-align: left;
  706. }
  707. .md-content th {
  708. background: var(--bg-hover);
  709. font-weight: 600;
  710. }
  711. .md-content img {
  712. max-width: 100%;
  713. border-radius: 8px;
  714. }
  715. .md-content hr {
  716. border: none;
  717. border-top: 1px solid var(--border-color);
  718. margin: 14px 0;
  719. }
  720. /* Tool Card */
  721. .tool-card {
  722. margin: 10px 0;
  723. border: 1px solid var(--border-color);
  724. border-radius: 10px;
  725. overflow: hidden;
  726. transition: border-color 0.2s;
  727. }
  728. .tool-card-header {
  729. display: flex;
  730. align-items: center;
  731. gap: 10px;
  732. padding: 10px 14px;
  733. cursor: pointer;
  734. transition: background 0.15s;
  735. font-size: 13px;
  736. }
  737. .tool-card-header:hover {
  738. background: var(--bg-hover);
  739. }
  740. .tool-status-icon {
  741. width: 22px;
  742. height: 22px;
  743. border-radius: 50%;
  744. display: flex;
  745. align-items: center;
  746. justify-content: center;
  747. flex-shrink: 0;
  748. font-size: 12px;
  749. transition: all 0.3s;
  750. }
  751. .tool-status-icon.loading {
  752. border: 2px solid var(--border-color);
  753. border-top-color: var(--accent);
  754. animation: blob-spin 0.8s linear infinite;
  755. }
  756. .tool-status-icon.done {
  757. background: #22c55e;
  758. color: #fff;
  759. border: none;
  760. animation: none;
  761. }
  762. .tool-name {
  763. font-weight: 600;
  764. color: var(--text-primary);
  765. }
  766. .tool-label {
  767. color: var(--text-secondary);
  768. font-size: 12px;
  769. }
  770. .tool-toggle {
  771. margin-left: auto;
  772. color: var(--text-secondary);
  773. font-size: 14px;
  774. transition: transform 0.2s;
  775. }
  776. .tool-card.expanded .tool-toggle {
  777. transform: rotate(90deg);
  778. }
  779. .tool-card-body {
  780. display: none;
  781. padding: 0 14px 12px;
  782. font-family: var(--font-mono);
  783. font-size: 12px;
  784. color: var(--text-secondary);
  785. white-space: pre-wrap;
  786. word-break: break-all;
  787. max-height: 200px;
  788. overflow-y: auto;
  789. border-top: 1px solid var(--border-color);
  790. }
  791. .tool-card.expanded .tool-card-body {
  792. display: block;
  793. }
  794. .chunk-error {
  795. margin: 8px 0;
  796. padding: 10px 14px;
  797. background: var(--error-bg);
  798. border-left: 3px solid var(--error-border);
  799. border-radius: 6px;
  800. color: var(--error-text);
  801. font-size: 13px;
  802. }
  803. /* HITL Card */
  804. .hitl-card {
  805. margin: 10px 0;
  806. border: 1px solid #f59e0b;
  807. border-radius: 10px;
  808. overflow: hidden;
  809. background: var(--bg-input-box);
  810. animation: msg-in 0.25s ease-out;
  811. }
  812. .hitl-card-header {
  813. display: flex;
  814. align-items: center;
  815. gap: 10px;
  816. padding: 12px 14px;
  817. font-size: 14px;
  818. font-weight: 600;
  819. color: #f59e0b;
  820. border-bottom: 1px solid var(--border-color);
  821. }
  822. .hitl-card-header i {
  823. font-size: 18px;
  824. }
  825. .hitl-card-body {
  826. padding: 12px 14px;
  827. font-size: 13px;
  828. color: var(--text-primary);
  829. }
  830. .hitl-card-body .hitl-tool {
  831. margin-bottom: 6px;
  832. color: var(--text-secondary);
  833. }
  834. .hitl-card-body .hitl-tool strong {
  835. color: var(--text-primary);
  836. }
  837. .hitl-card-body .hitl-command {
  838. background: var(--bg-code-block);
  839. padding: 10px 14px;
  840. border-radius: 8px;
  841. font-family: var(--font-mono);
  842. font-size: 13px;
  843. color: var(--text-code);
  844. word-break: break-all;
  845. margin: 8px 0 12px;
  846. }
  847. .hitl-card-actions {
  848. display: flex;
  849. gap: 10px;
  850. padding: 0 14px 14px;
  851. }
  852. .hitl-btn {
  853. padding: 8px 20px;
  854. border-radius: 8px;
  855. border: none;
  856. cursor: pointer;
  857. font-size: 13px;
  858. font-weight: 500;
  859. transition: all 0.2s;
  860. display: flex;
  861. align-items: center;
  862. gap: 6px;
  863. }
  864. .hitl-btn-approve {
  865. background: #22c55e;
  866. color: #fff;
  867. }
  868. .hitl-btn-approve:hover {
  869. background: #16a34a;
  870. }
  871. .hitl-btn-reject {
  872. background: #ef4444;
  873. color: #fff;
  874. }
  875. .hitl-btn-reject:hover {
  876. background: #dc2626;
  877. }
  878. .hitl-btn:disabled {
  879. opacity: 0.5;
  880. cursor: not-allowed;
  881. }
  882. /* Msg Actions */
  883. .msg-actions {
  884. display: flex;
  885. gap: 6px;
  886. margin-top: 10px;
  887. opacity: 0;
  888. transition: opacity 0.2s;
  889. }
  890. .msg-row.assistant:hover .msg-actions {
  891. opacity: 1;
  892. }
  893. .msg-action-btn {
  894. background: none;
  895. border: 1px solid var(--border-color);
  896. border-radius: 8px;
  897. padding: 5px 10px;
  898. cursor: pointer;
  899. color: var(--text-secondary);
  900. font-size: 12px;
  901. transition: all 0.15s;
  902. display: flex;
  903. align-items: center;
  904. gap: 4px;
  905. }
  906. .msg-action-btn:hover {
  907. background: var(--bg-hover);
  908. color: var(--text-primary);
  909. border-color: var(--accent);
  910. }
  911. /* Thinking */
  912. .inline-thinking {
  913. display: flex;
  914. align-items: center;
  915. gap: 8px;
  916. padding: 8px 0;
  917. color: var(--text-secondary);
  918. font-size: 13px;
  919. }
  920. .thinking-row {
  921. display: flex;
  922. gap: 14px;
  923. margin-bottom: 28px;
  924. animation: msg-in 0.25s ease-out;
  925. }
  926. .thinking-bubble {
  927. display: flex;
  928. align-items: center;
  929. gap: 10px;
  930. color: var(--text-secondary);
  931. font-size: 14px;
  932. padding: 10px 0;
  933. }
  934. .thinking-dots {
  935. display: flex;
  936. gap: 5px;
  937. }
  938. .thinking-dots span {
  939. width: 7px;
  940. height: 7px;
  941. border-radius: 50%;
  942. background: var(--thinking-dot);
  943. animation: dot-bounce 1.4s ease-in-out infinite;
  944. }
  945. .thinking-dots span:nth-child(2) {
  946. animation-delay: 0.2s;
  947. }
  948. .thinking-dots span:nth-child(3) {
  949. animation-delay: 0.4s;
  950. }
  951. @keyframes dot-bounce {
  952. 0%, 80%, 100% {
  953. opacity: 0.25;
  954. transform: scale(0.85);
  955. }
  956. 40% {
  957. opacity: 1;
  958. transform: scale(1.15);
  959. }
  960. }
  961. /* Thinking Block (collapsible reasoning content) */
  962. .thinking-block {
  963. margin: 0 0 12px;
  964. border: 1px solid var(--thinking-block-border);
  965. border-radius: 10px;
  966. overflow: hidden;
  967. background: var(--thinking-block-bg);
  968. }
  969. .thinking-block-header {
  970. display: flex;
  971. align-items: center;
  972. gap: 8px;
  973. padding: 10px 14px;
  974. cursor: pointer;
  975. font-size: 13px;
  976. user-select: none;
  977. transition: background 0.15s;
  978. }
  979. .thinking-block-header:hover {
  980. background: var(--bg-hover);
  981. }
  982. .thinking-block-label {
  983. color: var(--text-secondary);
  984. font-weight: 500;
  985. }
  986. .thinking-block-toggle {
  987. margin-left: auto;
  988. color: var(--text-secondary);
  989. font-size: 12px;
  990. transition: transform 0.25s;
  991. }
  992. .thinking-block.expanded .thinking-block-toggle {
  993. transform: rotate(90deg);
  994. }
  995. .thinking-block-body {
  996. padding: 0 14px 12px;
  997. font-size: 13px;
  998. color: var(--text-secondary);
  999. line-height: 1.7;
  1000. border-top: 1px solid var(--thinking-block-border);
  1001. max-height: 300px;
  1002. overflow-y: auto;
  1003. }
  1004. .thinking-block-body .md-content {
  1005. color: var(--text-secondary);
  1006. font-size: 13px;
  1007. }
  1008. .thinking-block:not(.expanded) .thinking-block-body {
  1009. display: none;
  1010. }
  1011. .thinking-block.streaming {
  1012. border-color: var(--accent);
  1013. }
  1014. .thinking-block.streaming .thinking-block-label {
  1015. color: var(--accent);
  1016. }
  1017. .thinking-block-dots {
  1018. display: inline-flex;
  1019. gap: 4px;
  1020. margin-left: 4px;
  1021. vertical-align: middle;
  1022. }
  1023. .thinking-block-dots span {
  1024. width: 5px;
  1025. height: 5px;
  1026. border-radius: 50%;
  1027. background: var(--accent);
  1028. animation: dot-bounce 1.4s ease-in-out infinite;
  1029. }
  1030. .thinking-block-dots span:nth-child(2) {
  1031. animation-delay: 0.2s;
  1032. }
  1033. .thinking-block-dots span:nth-child(3) {
  1034. animation-delay: 0.4s;
  1035. }
  1036. /* Bottom Input */
  1037. .input-wrap {
  1038. padding: 12px 24px 22px;
  1039. flex-shrink: 0;
  1040. }
  1041. .input-box {
  1042. max-width: 820px;
  1043. margin: 0 auto;
  1044. background: var(--bg-input-box);
  1045. border: 1px solid var(--border-input);
  1046. border-radius: 14px;
  1047. box-shadow: var(--shadow);
  1048. display: flex;
  1049. flex-direction: column;
  1050. transition: all 0.25s;
  1051. }
  1052. .input-box:focus-within {
  1053. border-color: var(--accent);
  1054. box-shadow: var(--shadow), 0 0 0 3px rgba(79, 110, 247, 0.06);
  1055. }
  1056. .input-box textarea {
  1057. border: none;
  1058. outline: none;
  1059. resize: none;
  1060. background: transparent;
  1061. padding: 14px 16px 6px;
  1062. font-family: var(--font-sans);
  1063. font-size: 14px;
  1064. color: var(--text-primary);
  1065. min-height: 44px;
  1066. max-height: 140px;
  1067. line-height: 1.6;
  1068. }
  1069. .input-box textarea::placeholder {
  1070. color: var(--text-secondary);
  1071. }
  1072. .input-toolbar {
  1073. display: flex;
  1074. align-items: center;
  1075. justify-content: flex-end;
  1076. padding: 6px 10px;
  1077. }
  1078. .send-btn {
  1079. width: 36px;
  1080. height: 36px;
  1081. border-radius: 50%;
  1082. border: none;
  1083. background: var(--accent);
  1084. color: #fff;
  1085. cursor: pointer;
  1086. display: flex;
  1087. align-items: center;
  1088. justify-content: center;
  1089. font-size: 18px;
  1090. transition: all 0.2s;
  1091. }
  1092. .send-btn:hover {
  1093. background: var(--accent-hover);
  1094. }
  1095. .send-btn:disabled {
  1096. opacity: 0.4;
  1097. cursor: not-allowed;
  1098. }
  1099. .send-btn.stop-mode {
  1100. background: #ef4444;
  1101. }
  1102. .send-btn.stop-mode:hover {
  1103. background: #dc2626;
  1104. }
  1105. .send-btn.stop-mode .stop-icon {
  1106. width: 14px;
  1107. height: 14px;
  1108. background: #fff;
  1109. border-radius: 3px;
  1110. }
  1111. /* Image Preview */
  1112. .img-preview-wrap {
  1113. padding: 8px 14px 0;
  1114. display: none;
  1115. }
  1116. .img-preview-wrap.has-img {
  1117. display: flex;
  1118. align-items: flex-start;
  1119. gap: 8px;
  1120. }
  1121. .img-preview {
  1122. position: relative;
  1123. display: inline-block;
  1124. }
  1125. .img-preview img {
  1126. max-height: 80px;
  1127. max-width: 200px;
  1128. border-radius: 8px;
  1129. border: 1px solid var(--border-color);
  1130. object-fit: cover;
  1131. }
  1132. .img-preview-remove {
  1133. position: absolute;
  1134. top: -6px;
  1135. right: -6px;
  1136. width: 20px;
  1137. height: 20px;
  1138. border-radius: 50%;
  1139. background: #ef4444;
  1140. color: #fff;
  1141. border: 2px solid var(--bg-input-box);
  1142. cursor: pointer;
  1143. display: flex;
  1144. align-items: center;
  1145. justify-content: center;
  1146. font-size: 12px;
  1147. line-height: 1;
  1148. }
  1149. .img-preview-remove:hover {
  1150. background: #dc2626;
  1151. }
  1152. /* Scrollbar */
  1153. .messages-wrap::-webkit-scrollbar, .sidebar-list::-webkit-scrollbar, .thinking-block-body::-webkit-scrollbar {
  1154. width: 5px;
  1155. }
  1156. .messages-wrap::-webkit-scrollbar-thumb, .sidebar-list::-webkit-scrollbar-thumb, .thinking-block-body::-webkit-scrollbar-thumb {
  1157. background: var(--border-color);
  1158. border-radius: 3px;
  1159. }
  1160. .messages-wrap::-webkit-scrollbar-track, .sidebar-list::-webkit-scrollbar-track, .thinking-block-body::-webkit-scrollbar-track {
  1161. background: transparent;
  1162. }
  1163. @media (max-width: 768px) {
  1164. .sidebar {
  1165. display: none;
  1166. }
  1167. .model-form-grid {
  1168. grid-template-columns: 1fr;
  1169. }
  1170. }
  1171. </style>
  1172. </head>
  1173. <body data-theme="light">
  1174. <!-- Sidebar -->
  1175. <div class="sidebar">
  1176. <div class="sidebar-header">
  1177. <button class="new-chat-btn" id="newChatBtn">
  1178. <i class="layui-icon layui-icon-addition"></i> 新对话
  1179. </button>
  1180. </div>
  1181. <div class="sidebar-title">最近对话</div>
  1182. <div class="sidebar-list" id="historyList"></div>
  1183. <div class="sidebar-footer">
  1184. <!-- <div class="sidebar-footer-meta">-->
  1185. <!-- <i class="layui-icon layui-icon-util" style="font-size:16px;color:var(&#45;&#45;text-secondary)"></i>-->
  1186. <!-- <span style="font-size:12px;color:var(&#45;&#45;text-secondary)" th:text="${nickname}">AI</span>-->
  1187. <!-- </div>-->
  1188. <button class="sidebar-config-btn" id="modelConfigBtn" type="button">
  1189. <i class="layui-icon layui-icon-set-fill"></i>
  1190. <span>模型配置</span>
  1191. </button>
  1192. </div>
  1193. </div>
  1194. <!-- Main -->
  1195. <div class="main-area">
  1196. <div class="main-header">
  1197. <!-- <button class="theme-toggle" id="themeBtn" title="切换主题">
  1198. <i class="layui-icon" id="themeIcon">&#xe748;</i>
  1199. </button>-->
  1200. </div>
  1201. <!-- Welcome View -->
  1202. <div class="welcome-view" id="welcomeView">
  1203. <div class="welcome-avatar">
  1204. <div class="blob blob-outer"></div>
  1205. <div class="blob-core"></div>
  1206. </div>
  1207. <div class="welcome-title" th:text="'你好,我是 ' + ${nickname} + '!'">你好,我是 清鉴 !</div>
  1208. <div class="welcome-sub">有什么我可以帮你的吗?</div>
  1209. <div class="quick-questions" id="welcomeQuickQs"></div>
  1210. <div class="welcome-input-box">
  1211. <div class="img-preview-wrap" id="welcomeImgWrap">
  1212. <div class="img-preview"><img id="welcomeImgPreview" src="" alt=""/><span class="img-preview-remove"
  1213. id="welcomeImgRemove">&times;</span>
  1214. </div>
  1215. </div>
  1216. <textarea id="welcomeInput" placeholder="和我聊聊天吧..." rows="1" autofocus></textarea>
  1217. <div class="welcome-toolbar">
  1218. <button class="welcome-send-btn" id="welcomeSendBtn" title="发送">
  1219. <i class="layui-icon layui-icon-release"></i>
  1220. </button>
  1221. </div>
  1222. </div>
  1223. </div>
  1224. <!-- Chat View -->
  1225. <div class="chat-view" id="chatView">
  1226. <div class="messages-wrap" id="messagesWrap">
  1227. <!-- Per-session containers are added here dynamically -->
  1228. </div>
  1229. <div class="input-wrap">
  1230. <div class="quick-questions" id="chatQuickQs" style="max-width:820px;margin:0 auto 10px;"></div>
  1231. <div class="input-box">
  1232. <div class="img-preview-wrap" id="chatImgWrap">
  1233. <div class="img-preview"><img id="chatImgPreview" src="" alt=""/><span class="img-preview-remove"
  1234. id="chatImgRemove">&times;</span>
  1235. </div>
  1236. </div>
  1237. <textarea id="chatInput" placeholder="请输入消息..." rows="1"></textarea>
  1238. <div class="input-toolbar">
  1239. <button class="send-btn" id="chatSendBtn" title="发送">
  1240. <i class="layui-icon layui-icon-release"></i>
  1241. </button>
  1242. </div>
  1243. </div>
  1244. </div>
  1245. </div>
  1246. </div>
  1247. <div class="model-modal" id="modelConfigModal">
  1248. <div class="model-modal-card" role="dialog" aria-modal="true" aria-labelledby="modelConfigTitle">
  1249. <div class="model-modal-header">
  1250. <div>
  1251. <div class="model-modal-title" id="modelConfigTitle">模型配置</div>
  1252. <div class="model-hint">系统当前仅维护一套聊天模型配置,打开后会自动加载默认模型信息。 </div>
  1253. </div>
  1254. <button class="model-modal-close" id="modelConfigCloseBtn" type="button" aria-label="关闭">
  1255. <i class="layui-icon layui-icon-close"></i>
  1256. </button>
  1257. </div>
  1258. <div class="model-modal-body">
  1259. <form id="modelConfigForm">
  1260. <input type="hidden" id="modelId" name="id"/>
  1261. <input type="hidden" id="modelCategory" name="category" value="chat"/>
  1262. <div class="model-form-grid">
  1263. <div class="model-field">
  1264. <label for="providerCode">服务商</label>
  1265. <select id="providerCode" name="providerCode"></select>
  1266. </div>
  1267. <div class="model-field">
  1268. <label for="modelName">模型名称</label>
  1269. <input id="modelName" name="modelName" type="text" placeholder="例如:qwen3.5-plus" required/>
  1270. </div>
  1271. <div class="model-field full">
  1272. <label for="apiHost">API Host</label>
  1273. <input id="apiHost" name="apiHost" type="text"
  1274. placeholder="例如:https://dashscope.aliyuncs.com/compatible-mode/v1"/>
  1275. </div>
  1276. <div class="model-field full">
  1277. <label for="apiKey">API Key</label>
  1278. <input id="apiKey" name="apiKey" type="text" placeholder="请输入 API Key"/>
  1279. </div>
  1280. <div class="model-field">
  1281. <label for="hasUse">启用状态</label>
  1282. <div class="model-switch">
  1283. <input id="hasUse" type="checkbox"/>
  1284. <span>设为当前聊天使用模型</span>
  1285. </div>
  1286. </div>
  1287. </div>
  1288. </form>
  1289. </div>
  1290. <div class="model-modal-footer">
  1291. <button class="model-cancel-btn" id="modelCancelBtn" type="button">取消</button>
  1292. <button class="model-save-btn" id="modelSaveBtn" type="button">保存配置</button>
  1293. </div>
  1294. </div>
  1295. </div>
  1296. <script src="/js/a/layui/layui.js"></script>
  1297. <script src="/js/a/js/marked.min.js"></script>
  1298. <script th:inline="javascript">
  1299. var SSE_ENDPOINT = /*[[${sseEndpoint}]]*/ '/js/a/ai/chat';
  1300. </script>
  1301. <script>
  1302. /* ===== DOM ===== */
  1303. var welcomeView = document.getElementById('welcomeView');
  1304. var chatView = document.getElementById('chatView');
  1305. var messagesWrap = document.getElementById('messagesWrap');
  1306. var welcomeInput = document.getElementById('welcomeInput');
  1307. var welcomeSendBtn = document.getElementById('welcomeSendBtn');
  1308. var chatInput = document.getElementById('chatInput');
  1309. var chatSendBtn = document.getElementById('chatSendBtn');
  1310. // var themeBtn = document.getElementById('themeBtn');
  1311. var themeIcon = document.getElementById('themeIcon');
  1312. var newChatBtn = document.getElementById('newChatBtn');
  1313. var historyList = document.getElementById('historyList');
  1314. var modelConfigBtn = document.getElementById('modelConfigBtn');
  1315. var modelConfigModal = document.getElementById('modelConfigModal');
  1316. var modelConfigCloseBtn = document.getElementById('modelConfigCloseBtn');
  1317. var modelCancelBtn = document.getElementById('modelCancelBtn');
  1318. var modelSaveBtn = document.getElementById('modelSaveBtn');
  1319. var providerCodeSelect = document.getElementById('providerCode');
  1320. var modelConfigForm = document.getElementById('modelConfigForm');
  1321. var modelIdInput = document.getElementById('modelId');
  1322. var modelNameInput = document.getElementById('modelName');
  1323. var apiHostInput = document.getElementById('apiHost');
  1324. var apiKeyInput = document.getElementById('apiKey');
  1325. var hasUseInput = document.getElementById('hasUse');
  1326. /* ===== Constants ===== */
  1327. var DOTS_HTML = '<span class="thinking-dots"><span></span><span></span><span></span></span>';
  1328. var CHAT_MODEL_CATEGORY = 'chat';
  1329. var SINGLE_MODEL_ID = '1';
  1330. /* ===== Per-Session State ===== */
  1331. function SessionState(sessionId) {
  1332. this.sessionId = sessionId;
  1333. this.container = document.createElement('div');
  1334. this.container.className = 'messages-inner';
  1335. this.container.style.display = 'none';
  1336. messagesWrap.appendChild(this.container);
  1337. this.eventSource = null;
  1338. this.isStreaming = false;
  1339. this.currentBubbleEl = null;
  1340. this.reasonBuffer = '';
  1341. this.thinkingBlockEl = null;
  1342. this.thinkingBodyMdEl = null;
  1343. this.thinkingBodyWrapEl = null;
  1344. this.thinkingBuffer = '';
  1345. this.pendingToolCard = null;
  1346. this.thinkingEl = null;
  1347. this.inlineThinkingEl = null;
  1348. this.silenceTimer = null;
  1349. this.contentRafId = null;
  1350. this.reasonRafId = null;
  1351. }
  1352. var sessionMap = {};
  1353. var activeSessionId = null;
  1354. /* ===== Global State ===== */
  1355. var SESSION_ID = 'web-' + Date.now().toString(36);
  1356. var isStreaming = false;
  1357. var inChatMode = false;
  1358. var chatHistory = [];
  1359. var currentChatIndex = -1;
  1360. var pendingImageBase64 = null;
  1361. var pendingImageMime = null;
  1362. var userScrolledUp = false;
  1363. var providerOptions = [
  1364. {providerCode: 'ollama', providerName: 'Ollama'},
  1365. {providerCode: 'qwen', providerName: '千问'},
  1366. {providerCode: 'DeepSeek', providerName: 'DeepSeek'}
  1367. ];
  1368. var modelConfigLoading = false;
  1369. /* ===== Session Helpers ===== */
  1370. function getOrCreateSession(sessionId) {
  1371. if (!sessionMap[sessionId]) {
  1372. sessionMap[sessionId] = new SessionState(sessionId);
  1373. }
  1374. return sessionMap[sessionId];
  1375. }
  1376. function setActiveSession(sessionId) {
  1377. if (activeSessionId && sessionMap[activeSessionId]) {
  1378. sessionMap[activeSessionId].container.style.display = 'none';
  1379. }
  1380. var sess = getOrCreateSession(sessionId);
  1381. sess.container.style.display = '';
  1382. activeSessionId = sessionId;
  1383. SESSION_ID = sessionId;
  1384. isStreaming = sess.isStreaming;
  1385. userScrolledUp = false;
  1386. if (isStreaming) setBtnStopMode();
  1387. else setBtnSendMode();
  1388. }
  1389. function deactivateSession() {
  1390. if (activeSessionId && sessionMap[activeSessionId]) {
  1391. sessionMap[activeSessionId].container.style.display = 'none';
  1392. }
  1393. activeSessionId = null;
  1394. isStreaming = false;
  1395. setBtnSendMode();
  1396. }
  1397. function showMessage(message, type) {
  1398. if (window.layui && layui.layer) {
  1399. layui.layer.msg(message, {icon: type === 'error' ? 2 : 1, time: 1800});
  1400. return;
  1401. }
  1402. alert(message);
  1403. }
  1404. function toggleModelModal(show) {
  1405. modelConfigModal.classList.toggle('active', !!show);
  1406. if (show) {
  1407. document.body.style.overflow = 'hidden';
  1408. } else {
  1409. document.body.style.overflow = '';
  1410. }
  1411. }
  1412. function resetModelForm() {
  1413. modelConfigForm.reset();
  1414. modelIdInput.value = SINGLE_MODEL_ID;
  1415. document.getElementById('modelCategory').value = CHAT_MODEL_CATEGORY;
  1416. hasUseInput.checked = true;
  1417. if (providerCodeSelect.options.length > 0) {
  1418. providerCodeSelect.selectedIndex = 0;
  1419. }
  1420. }
  1421. function fillModelForm(model) {
  1422. modelIdInput.value = model && model.id ? model.id : '';
  1423. document.getElementById('modelCategory').value = model && model.category ? model.category : CHAT_MODEL_CATEGORY;
  1424. modelNameInput.value = model && model.modelName ? model.modelName : '';
  1425. apiHostInput.value = model && model.apiHost ? model.apiHost : '';
  1426. apiKeyInput.value = model && model.apiKey ? model.apiKey : '';
  1427. hasUseInput.checked = !model || model.hasUse == null ? true : Number(model.hasUse) === 1;
  1428. providerCodeSelect.value = model && model.providerCode ? model.providerCode : '';
  1429. }
  1430. function renderProviderSelect() {
  1431. var html = '<option value="">请选择服务商</option>';
  1432. for (var i = 0; i < providerOptions.length; i++) {
  1433. var item = providerOptions[i];
  1434. html += '<option value="' + escapeHtml(item.providerCode || '') + '">' + escapeHtml(item.providerName || item.providerCode || '') + '</option>';
  1435. }
  1436. providerCodeSelect.innerHTML = html;
  1437. }
  1438. function httpGetJson(url, callback, onError) {
  1439. var xhr = new XMLHttpRequest();
  1440. xhr.open('GET', url, true);
  1441. xhr.onreadystatechange = function () {
  1442. if (xhr.readyState !== 4) return;
  1443. if (xhr.status >= 200 && xhr.status < 300) {
  1444. try {
  1445. callback(JSON.parse(xhr.responseText));
  1446. } catch (e) {
  1447. console.log(e)
  1448. if (onError) onError(e);
  1449. }
  1450. return;
  1451. }
  1452. if (onError) onError(xhr);
  1453. };
  1454. xhr.send();
  1455. }
  1456. function unwrapResponse(payload) {
  1457. if (!payload || typeof payload !== 'object') {
  1458. return payload;
  1459. }
  1460. if (Object.prototype.hasOwnProperty.call(payload, 'data')) {
  1461. return payload.data;
  1462. }
  1463. return payload;
  1464. }
  1465. function loadModelConfigData(done) {
  1466. if (modelConfigLoading) return;
  1467. modelConfigLoading = true;
  1468. var pending = 1;
  1469. var failed = false;
  1470. function finish() {
  1471. pending--;
  1472. if (pending > 0) return;
  1473. modelConfigLoading = false;
  1474. if (!failed && done) done();
  1475. }
  1476. function handleError() {
  1477. failed = true;
  1478. modelConfigLoading = false;
  1479. showMessage('加载模型配置失败', 'error');
  1480. }
  1481. renderProviderSelect();
  1482. httpGetJson('/js/a/ai/model/getInfo?id=' + encodeURIComponent(SINGLE_MODEL_ID), function (data) {
  1483. resetModelForm();
  1484. fillModelForm(unwrapResponse(data) || {});
  1485. finish();
  1486. }, handleError);
  1487. }
  1488. function openModelConfigModal() {
  1489. loadModelConfigData(function () {
  1490. toggleModelModal(true);
  1491. });
  1492. }
  1493. function saveModelConfig() {
  1494. var providerCode = providerCodeSelect.value.trim();
  1495. var modelName = modelNameInput.value.trim();
  1496. var apiHost = apiHostInput.value.trim();
  1497. var apiKey = apiKeyInput.value.trim();
  1498. if (!providerCode || !modelName || !apiHost || !apiKey) {
  1499. showMessage('请完整填写服务商、模型名称、API Host 和 API Key', 'error');
  1500. return;
  1501. }
  1502. modelSaveBtn.disabled = true;
  1503. var params = new URLSearchParams();
  1504. params.append('id', modelIdInput.value || SINGLE_MODEL_ID);
  1505. params.append('category', CHAT_MODEL_CATEGORY);
  1506. params.append('providerCode', providerCode);
  1507. params.append('modelName', modelName);
  1508. params.append('apiHost', apiHost);
  1509. params.append('apiKey', apiKey);
  1510. params.append('hasUse', hasUseInput.checked ? '1' : '0');
  1511. fetch('/js/a/ai/create', {
  1512. method: 'POST',
  1513. headers: {'Content-Type': 'application/json; charset=UTF-8'},
  1514. body: JSON.stringify(Object.fromEntries(params))
  1515. }).then(function (resp) {
  1516. if (!resp.ok) {
  1517. throw new Error('save failed');
  1518. }
  1519. return resp.text();
  1520. }).then(function () {
  1521. showMessage('模型配置已保存');
  1522. toggleModelModal(false);
  1523. }).catch(function () {
  1524. showMessage('保存模型配置失败', 'error');
  1525. }).finally(function () {
  1526. modelSaveBtn.disabled = false;
  1527. });
  1528. }
  1529. /* ===== Image Paste ===== */
  1530. var welcomeImgWrap = document.getElementById('welcomeImgWrap');
  1531. var welcomeImgPreview = document.getElementById('welcomeImgPreview');
  1532. var welcomeImgRemove = document.getElementById('welcomeImgRemove');
  1533. var chatImgWrap = document.getElementById('chatImgWrap');
  1534. var chatImgPreview = document.getElementById('chatImgPreview');
  1535. var chatImgRemove = document.getElementById('chatImgRemove');
  1536. function handlePasteImage(e) {
  1537. var items = (e.clipboardData || e.originalEvent.clipboardData).items;
  1538. for (var i = 0; i < items.length; i++) {
  1539. if (items[i].type.indexOf('image') !== -1) {
  1540. e.preventDefault();
  1541. var file = items[i].getAsFile();
  1542. var reader = new FileReader();
  1543. reader.onload = function(evt) {
  1544. var dataUrl = evt.target.result;
  1545. var parts = dataUrl.split(',');
  1546. var meta = parts[0];
  1547. pendingImageBase64 = parts[1];
  1548. pendingImageMime = meta.match(/data:(.*?);/)[1];
  1549. showImagePreview(dataUrl);
  1550. };
  1551. reader.readAsDataURL(file);
  1552. return;
  1553. }
  1554. }
  1555. }
  1556. function showImagePreview(dataUrl) {
  1557. if (inChatMode) {
  1558. chatImgPreview.src = dataUrl;
  1559. chatImgWrap.classList.add('has-img');
  1560. } else {
  1561. welcomeImgPreview.src = dataUrl;
  1562. welcomeImgWrap.classList.add('has-img');
  1563. }
  1564. }
  1565. function clearImagePreview() {
  1566. pendingImageBase64 = null;
  1567. pendingImageMime = null;
  1568. welcomeImgPreview.src = '';
  1569. welcomeImgWrap.classList.remove('has-img');
  1570. chatImgPreview.src = '';
  1571. chatImgWrap.classList.remove('has-img');
  1572. }
  1573. modelConfigBtn.addEventListener('click', openModelConfigModal);
  1574. modelConfigCloseBtn.addEventListener('click', function () {
  1575. toggleModelModal(false);
  1576. });
  1577. modelCancelBtn.addEventListener('click', function () {
  1578. toggleModelModal(false);
  1579. });
  1580. modelSaveBtn.addEventListener('click', saveModelConfig);
  1581. // modelConfigModal.addEventListener('click', function (e) {
  1582. // if (e.target === modelConfigModal) {
  1583. // toggleModelModal(false);
  1584. // }
  1585. // });
  1586. document.addEventListener('keydown', function (e) {
  1587. if (e.key === 'Escape' && modelConfigModal.classList.contains('active')) {
  1588. toggleModelModal(false);
  1589. }
  1590. });
  1591. welcomeInput.addEventListener('paste', handlePasteImage);
  1592. chatInput.addEventListener('paste', handlePasteImage);
  1593. welcomeImgRemove.addEventListener('click', clearImagePreview);
  1594. chatImgRemove.addEventListener('click', clearImagePreview);
  1595. /* ===== Quick Questions ===== */
  1596. var welcomeQuickQs = document.getElementById('welcomeQuickQs');
  1597. var chatQuickQs = document.getElementById('chatQuickQs');
  1598. function loadQuickQuestions() {
  1599. httpGetJson('/js/a/ai/qs', function (data) {
  1600. var questions = unwrapResponse(data);
  1601. if (!Array.isArray(questions) || questions.length === 0) return;
  1602. renderQuickQChips(welcomeQuickQs, questions);
  1603. renderQuickQChips(chatQuickQs, questions);
  1604. });
  1605. }
  1606. function renderQuickQChips(container, questions) {
  1607. container.innerHTML = '';
  1608. for (var i = 0; i < questions.length; i++) {
  1609. var chip = document.createElement('span');
  1610. chip.className = 'quick-q-chip';
  1611. chip.textContent = questions[i];
  1612. chip.title = questions[i];
  1613. chip.addEventListener('click', (function(q) {
  1614. return function() {
  1615. if (inChatMode) {
  1616. chatInput.value = q;
  1617. chatInput.focus();
  1618. autoResize(chatInput);
  1619. } else {
  1620. welcomeInput.value = q;
  1621. welcomeInput.focus();
  1622. autoResize(welcomeInput);
  1623. }
  1624. };
  1625. })(questions[i]));
  1626. container.appendChild(chip);
  1627. }
  1628. }
  1629. loadQuickQuestions();
  1630. /* ===== Marked ===== */
  1631. if (typeof marked !== 'undefined') { marked.setOptions({ breaks: true, gfm: true }); }
  1632. function renderMd(text) {
  1633. if (typeof marked !== 'undefined') return marked.parse(text);
  1634. return text.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>');
  1635. }
  1636. /* ===== Theme ===== */
  1637. var currentTheme = localStorage.getItem('chat-theme') || 'light';
  1638. document.body.setAttribute('data-theme', currentTheme);
  1639. /* updateThemeIcon();
  1640. themeBtn.addEventListener('click', function() {
  1641. currentTheme = currentTheme === 'light' ? 'dark' : 'light';
  1642. document.body.setAttribute('data-theme', currentTheme);
  1643. localStorage.setItem('chat-theme', currentTheme);
  1644. updateThemeIcon();
  1645. });
  1646. function updateThemeIcon() {
  1647. themeIcon.innerHTML = currentTheme === 'light' ? '&#xe6c2;' : '&#xe748;';
  1648. themeBtn.title = currentTheme === 'light' ? '切换至暗色' : '切换至浅色';
  1649. }*/
  1650. /* ===== View Switch ===== */
  1651. function switchToChatMode() {
  1652. if (inChatMode) return;
  1653. inChatMode = true;
  1654. welcomeView.style.display = 'none';
  1655. chatView.classList.add('active');
  1656. chatInput.focus();
  1657. }
  1658. function switchToWelcomeMode() {
  1659. inChatMode = false;
  1660. deactivateSession();
  1661. welcomeView.style.display = '';
  1662. chatView.classList.remove('active');
  1663. welcomeInput.focus();
  1664. }
  1665. /* ===== Auto-resize ===== */
  1666. function autoResize(el) {
  1667. el.style.height = 'auto';
  1668. el.style.height = Math.min(el.scrollHeight, 140) + 'px';
  1669. }
  1670. welcomeInput.addEventListener('input', function() { autoResize(this); });
  1671. chatInput.addEventListener('input', function() { autoResize(this); });
  1672. /* ===== Send from both inputs ===== */
  1673. function getInputText() {
  1674. if (inChatMode) return chatInput.value.trim();
  1675. return welcomeInput.value.trim();
  1676. }
  1677. function clearInput() {
  1678. if (inChatMode) { chatInput.value = ''; chatInput.style.height = 'auto'; }
  1679. else { welcomeInput.value = ''; welcomeInput.style.height = 'auto'; }
  1680. }
  1681. welcomeInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
  1682. chatInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
  1683. welcomeSendBtn.addEventListener('click', function() { sendMessage(); });
  1684. chatSendBtn.addEventListener('click', function() {
  1685. if (isStreaming && activeSessionId && sessionMap[activeSessionId]) {
  1686. finishStream(sessionMap[activeSessionId]);
  1687. } else {
  1688. sendMessage();
  1689. }
  1690. });
  1691. /* ===== Click to focus ===== */
  1692. document.querySelector('.welcome-input-box').addEventListener('click', function(e) {
  1693. if (!e.target.closest('button')) welcomeInput.focus();
  1694. });
  1695. document.querySelector('.input-box').addEventListener('click', function(e) {
  1696. if (!e.target.closest('button')) chatInput.focus();
  1697. });
  1698. /* ===== New Chat ===== */
  1699. newChatBtn.addEventListener('click', function() {
  1700. SESSION_ID = 'web-' + Date.now().toString(36);
  1701. currentChatIndex = -1;
  1702. switchToWelcomeMode();
  1703. updateHistoryUI();
  1704. });
  1705. /* ===== Helpers ===== */
  1706. messagesWrap.addEventListener('scroll', function() {
  1707. var gap = messagesWrap.scrollHeight - messagesWrap.scrollTop - messagesWrap.clientHeight;
  1708. userScrolledUp = gap > 80;
  1709. });
  1710. var scrollRafPending = false;
  1711. function scrollToBottom(force) {
  1712. if (!force && userScrolledUp) return;
  1713. if (force) userScrolledUp = false;
  1714. if (scrollRafPending) return;
  1715. scrollRafPending = true;
  1716. requestAnimationFrame(function() {
  1717. scrollRafPending = false;
  1718. messagesWrap.scrollTop = messagesWrap.scrollHeight;
  1719. });
  1720. }
  1721. function resetStreamState(sess) {
  1722. sess.currentBubbleEl = null;
  1723. sess.reasonBuffer = '';
  1724. sess.thinkingBlockEl = null;
  1725. sess.thinkingBodyMdEl = null;
  1726. sess.thinkingBodyWrapEl = null;
  1727. sess.thinkingBuffer = '';
  1728. if (sess.contentRafId) { cancelAnimationFrame(sess.contentRafId); sess.contentRafId = null; }
  1729. if (sess.reasonRafId) { cancelAnimationFrame(sess.reasonRafId); sess.reasonRafId = null; }
  1730. }
  1731. function setBtnStopMode() {
  1732. chatSendBtn.disabled = false;
  1733. chatSendBtn.classList.add('stop-mode');
  1734. chatSendBtn.innerHTML = '<div class="stop-icon"></div>';
  1735. chatSendBtn.title = '停止生成';
  1736. }
  1737. function setBtnSendMode() {
  1738. chatSendBtn.classList.remove('stop-mode');
  1739. chatSendBtn.innerHTML = '<i class="layui-icon layui-icon-release"></i>';
  1740. chatSendBtn.title = '发送';
  1741. chatSendBtn.disabled = false;
  1742. }
  1743. /* ===== History ===== */
  1744. function loadSessionHistory() {
  1745. var xhr = new XMLHttpRequest();
  1746. xhr.open('GET', '/js/a/chat/sessions', true);
  1747. xhr.onreadystatechange = function() {
  1748. if (xhr.readyState === 4 && xhr.status === 200) {
  1749. try {
  1750. var result = JSON.parse(xhr.responseText);
  1751. var list = result.data;
  1752. chatHistory = [];
  1753. for (var i = 0; i < list.length; i++) {
  1754. chatHistory.push({ label: list[i].label, sessionId: list[i].sessionId });
  1755. }
  1756. updateHistoryUI();
  1757. } catch (e) {}
  1758. }
  1759. };
  1760. xhr.send();
  1761. }
  1762. function saveChatToHistory(firstMsg) {
  1763. var label = firstMsg.length > 30 ? firstMsg.substring(0, 30) + '...' : firstMsg;
  1764. for (var i = 0; i < chatHistory.length; i++) {
  1765. if (chatHistory[i].sessionId === SESSION_ID) {
  1766. currentChatIndex = i;
  1767. updateHistoryUI();
  1768. return;
  1769. }
  1770. }
  1771. chatHistory.unshift({ label: label, sessionId: SESSION_ID });
  1772. if (chatHistory.length > 50) chatHistory.pop();
  1773. currentChatIndex = 0;
  1774. updateHistoryUI();
  1775. }
  1776. /* Sidebar event delegation — single listener instead of per-item binding */
  1777. historyList.addEventListener('click', function(e) {
  1778. var delBtn = e.target.closest('.sidebar-item-del');
  1779. if (delBtn) {
  1780. e.stopPropagation();
  1781. var idx = parseInt(delBtn.closest('.sidebar-item').getAttribute('data-idx'));
  1782. if (!isNaN(idx)) deleteSession(idx);
  1783. return;
  1784. }
  1785. var item = e.target.closest('.sidebar-item');
  1786. if (item) {
  1787. var idx = parseInt(item.getAttribute('data-idx'));
  1788. if (!isNaN(idx)) selectSession(idx);
  1789. }
  1790. });
  1791. function updateHistoryUI() {
  1792. var frag = document.createDocumentFragment();
  1793. for (var i = 0; i < chatHistory.length; i++) {
  1794. var item = document.createElement('div');
  1795. var sess = sessionMap[chatHistory[i].sessionId];
  1796. var streaming = sess && sess.isStreaming;
  1797. item.className = 'sidebar-item' + (i === currentChatIndex ? ' active' : '') + (streaming ? ' streaming' : '');
  1798. item.setAttribute('data-idx', i);
  1799. var label = document.createElement('span');
  1800. label.className = 'sidebar-item-label';
  1801. label.textContent = chatHistory[i].label;
  1802. var delBtn = document.createElement('button');
  1803. delBtn.className = 'sidebar-item-del';
  1804. delBtn.title = '删除对话';
  1805. delBtn.innerHTML = '<i class="layui-icon layui-icon-close"></i>';
  1806. item.appendChild(label);
  1807. if (streaming) {
  1808. var spinner = document.createElement('span');
  1809. spinner.className = 'sidebar-item-spinner';
  1810. spinner.title = '对话进行中...';
  1811. item.appendChild(spinner);
  1812. }
  1813. item.appendChild(delBtn);
  1814. frag.appendChild(item);
  1815. }
  1816. historyList.innerHTML = '';
  1817. historyList.appendChild(frag);
  1818. }
  1819. function deleteSession(idx) {
  1820. var entry = chatHistory[idx];
  1821. if (!entry) return;
  1822. var xhr = new XMLHttpRequest();
  1823. xhr.open('POST', '/js/a/chat/sessions/delete?sessionId=' + encodeURIComponent(entry.sessionId), true);
  1824. xhr.onreadystatechange = function() {
  1825. if (xhr.readyState === 4) {
  1826. /* Clean up session state after server confirms */
  1827. var sess = sessionMap[entry.sessionId];
  1828. if (sess) {
  1829. if (sess.eventSource) sess.eventSource.close();
  1830. if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
  1831. if (sess.contentRafId) cancelAnimationFrame(sess.contentRafId);
  1832. if (sess.reasonRafId) cancelAnimationFrame(sess.reasonRafId);
  1833. if (sess.container.parentNode) sess.container.parentNode.removeChild(sess.container);
  1834. delete sessionMap[entry.sessionId];
  1835. }
  1836. chatHistory.splice(idx, 1);
  1837. if (idx === currentChatIndex) {
  1838. currentChatIndex = -1;
  1839. SESSION_ID = 'web-' + Date.now().toString(36);
  1840. switchToWelcomeMode();
  1841. } else if (idx < currentChatIndex) {
  1842. currentChatIndex--;
  1843. }
  1844. updateHistoryUI();
  1845. }
  1846. };
  1847. xhr.send();
  1848. }
  1849. function selectSession(idx) {
  1850. if (idx === currentChatIndex && inChatMode) return;
  1851. var entry = chatHistory[idx];
  1852. if (!entry) return;
  1853. currentChatIndex = idx;
  1854. SESSION_ID = entry.sessionId;
  1855. if (!inChatMode) switchToChatMode();
  1856. setActiveSession(entry.sessionId);
  1857. updateHistoryUI();
  1858. var sess = sessionMap[entry.sessionId];
  1859. /* Only load from server if not streaming and container has no content */
  1860. if (!sess.isStreaming && sess.container.children.length === 0) {
  1861. loadMessages(sess);
  1862. } else {
  1863. scrollToBottom(true);
  1864. }
  1865. }
  1866. function loadMessages(sess) {
  1867. var xhr = new XMLHttpRequest();
  1868. xhr.open('GET', '/js/a/chat/messages?sessionId=' + encodeURIComponent(sess.sessionId), true);
  1869. xhr.onreadystatechange = function() {
  1870. if (xhr.readyState === 4 && xhr.status === 200) {
  1871. try {
  1872. var result = JSON.parse(xhr.responseText);
  1873. var msgs = result.data;
  1874. sess.container.innerHTML = '';
  1875. resetStreamState(sess);
  1876. for (var i = 0; i < msgs.length; i++) {
  1877. var m = msgs[i];
  1878. if (m.role === 'USER') {
  1879. resetStreamState(sess);
  1880. appendUserMessage(sess, m.content);
  1881. } else if (m.role === 'ASSISTANT') {
  1882. var isConsecutive = (i > 0 && msgs[i - 1].role === 'ASSISTANT');
  1883. if (!isConsecutive) resetStreamState(sess);
  1884. var el = ensureAssistantBubble(sess);
  1885. sess.reasonBuffer = isConsecutive ? sess.reasonBuffer + '\n\n' + m.content : m.content;
  1886. el.innerHTML = renderMd(sess.reasonBuffer);
  1887. }
  1888. }
  1889. resetStreamState(sess);
  1890. if (sess.sessionId === activeSessionId) scrollToBottom(true);
  1891. } catch (e) {}
  1892. }
  1893. };
  1894. xhr.send();
  1895. }
  1896. /* Load on startup */
  1897. loadSessionHistory();
  1898. /* ===== Message Rendering (Session-Aware) ===== */
  1899. function appendUserMessage(sess, text, imageDataUrl) {
  1900. var row = document.createElement('div');
  1901. row.className = 'msg-row user';
  1902. row.innerHTML = '<div class="msg-bubble"></div><div class="msg-avatar">我</div>';
  1903. var bubble = row.querySelector('.msg-bubble');
  1904. if (imageDataUrl) {
  1905. var img = document.createElement('img');
  1906. img.src = imageDataUrl;
  1907. img.style.cssText = 'max-height:120px;max-width:240px;border-radius:8px;display:block;margin-bottom:8px;';
  1908. bubble.appendChild(img);
  1909. }
  1910. var span = document.createElement('span');
  1911. span.textContent = text;
  1912. bubble.appendChild(span);
  1913. sess.container.appendChild(row);
  1914. if (sess.sessionId === activeSessionId) scrollToBottom(true);
  1915. }
  1916. function ensureAssistantBubble(sess) {
  1917. if (!sess.currentBubbleEl) {
  1918. removeThinking(sess);
  1919. var row = document.createElement('div');
  1920. row.className = 'msg-row assistant';
  1921. row.innerHTML = '<div class="msg-avatar"><i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
  1922. + '<div class="msg-bubble"><div class="md-content"></div>'
  1923. + '<div class="msg-actions">'
  1924. + '<button class="msg-action-btn copy-btn" title="复制"><i class="layui-icon layui-icon-file"></i> 复制</button>'
  1925. + '</div></div>';
  1926. sess.container.appendChild(row);
  1927. sess.currentBubbleEl = row.querySelector('.md-content');
  1928. var copyBtn = row.querySelector('.copy-btn');
  1929. var mdRef = sess.currentBubbleEl;
  1930. copyBtn.addEventListener('click', function() {
  1931. var txt = mdRef.innerText || '';
  1932. if (navigator.clipboard) { navigator.clipboard.writeText(txt); }
  1933. });
  1934. }
  1935. return sess.currentBubbleEl;
  1936. }
  1937. function ensureThinkingBlock(sess) {
  1938. if (!sess.thinkingBlockEl) {
  1939. ensureAssistantBubble(sess);
  1940. var parent = sess.currentBubbleEl.parentNode;
  1941. var block = document.createElement('div');
  1942. block.className = 'thinking-block streaming expanded';
  1943. block.innerHTML = '<div class="thinking-block-header">'
  1944. + '<span class="thinking-block-label">思考中...</span>'
  1945. + '<span class="thinking-block-dots"><span></span><span></span><span></span></span>'
  1946. + '<i class="layui-icon layui-icon-right thinking-block-toggle"></i>'
  1947. + '</div>'
  1948. + '<div class="thinking-block-body"><div class="md-content"></div></div>';
  1949. parent.insertBefore(block, sess.currentBubbleEl);
  1950. block.querySelector('.thinking-block-header').addEventListener('click', function() {
  1951. block.classList.toggle('expanded');
  1952. });
  1953. sess.thinkingBlockEl = block;
  1954. sess.thinkingBodyMdEl = block.querySelector('.thinking-block-body .md-content');
  1955. sess.thinkingBodyWrapEl = block.querySelector('.thinking-block-body');
  1956. sess.thinkingBuffer = '';
  1957. }
  1958. return sess.thinkingBlockEl;
  1959. }
  1960. function insertBeforeActions(sess, el) {
  1961. var parent = sess.currentBubbleEl.parentNode;
  1962. parent.insertBefore(el, parent.querySelector('.msg-actions'));
  1963. }
  1964. function finishThinkingBlock(sess) {
  1965. if (sess.thinkingBlockEl) {
  1966. if (sess.reasonRafId) {
  1967. cancelAnimationFrame(sess.reasonRafId);
  1968. sess.reasonRafId = null;
  1969. if (sess.thinkingBodyMdEl) {
  1970. sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
  1971. }
  1972. }
  1973. sess.thinkingBlockEl.classList.remove('streaming', 'expanded');
  1974. var label = sess.thinkingBlockEl.querySelector('.thinking-block-label');
  1975. if (label) label.textContent = '思考结束';
  1976. var dots = sess.thinkingBlockEl.querySelector('.thinking-block-dots');
  1977. if (dots) dots.remove();
  1978. sess.thinkingBlockEl = null;
  1979. sess.thinkingBodyMdEl = null;
  1980. sess.thinkingBodyWrapEl = null;
  1981. sess.thinkingBuffer = '';
  1982. }
  1983. }
  1984. function clearThinkTags(text) {
  1985. return text.replace(/<\s*\/?think\s*>/gi, '');
  1986. }
  1987. function appendReasonChunk(sess, text) {
  1988. removeThinking(sess);
  1989. ensureThinkingBlock(sess);
  1990. sess.thinkingBuffer += clearThinkTags(text);
  1991. if (!sess.reasonRafId) {
  1992. sess.reasonRafId = requestAnimationFrame(function() {
  1993. sess.reasonRafId = null;
  1994. if (!sess.thinkingBlockEl) return;
  1995. if (sess.thinkingBodyMdEl) {
  1996. sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
  1997. }
  1998. if (sess.thinkingBodyWrapEl) {
  1999. sess.thinkingBodyWrapEl.scrollTop = sess.thinkingBodyWrapEl.scrollHeight;
  2000. }
  2001. if (sess.sessionId === activeSessionId) scrollToBottom();
  2002. });
  2003. }
  2004. }
  2005. function finishPendingTool(sess) {
  2006. if (sess.pendingToolCard) {
  2007. var icon = sess.pendingToolCard.querySelector('.tool-status-icon');
  2008. if (icon) { icon.className = 'tool-status-icon done'; icon.innerHTML = '<i class="layui-icon layui-icon-ok" style="font-size:12px"></i>'; }
  2009. var label = sess.pendingToolCard.querySelector('.tool-label');
  2010. if (label) label.textContent = '已完成';
  2011. sess.pendingToolCard = null;
  2012. }
  2013. }
  2014. function appendActionEndChunk(sess, toolName, text) {
  2015. finishPendingTool(sess);
  2016. ensureAssistantBubble(sess);
  2017. var card = document.createElement('div');
  2018. card.className = 'tool-card';
  2019. card.innerHTML = '<div class="tool-card-header">'
  2020. + '<span class="tool-status-icon loading"></span>'
  2021. + '<span class="tool-name">' + escapeHtml(toolName || 'tool') + '</span>'
  2022. + '<span class="tool-label">执行中...</span>'
  2023. + '<i class="layui-icon layui-icon-right tool-toggle"></i>'
  2024. + '</div>'
  2025. + '<div class="tool-card-body"></div>';
  2026. card.querySelector('.tool-card-body').textContent = text || '';
  2027. card.querySelector('.tool-card-header').addEventListener('click', function() {
  2028. card.classList.toggle('expanded');
  2029. });
  2030. insertBeforeActions(sess, card);
  2031. sess.pendingToolCard = card;
  2032. sess.reasonBuffer = '';
  2033. var newMd = document.createElement('div');
  2034. newMd.className = 'md-content';
  2035. insertBeforeActions(sess, newMd);
  2036. sess.currentBubbleEl = newMd;
  2037. if (sess.sessionId === activeSessionId) scrollToBottom();
  2038. }
  2039. function appendContentChunk(sess, text, append) {
  2040. var clean = clearThinkTags(text);
  2041. sess.reasonBuffer = append ? sess.reasonBuffer + clean : clean;
  2042. if (!sess.contentRafId) {
  2043. sess.contentRafId = requestAnimationFrame(function() {
  2044. sess.contentRafId = null;
  2045. var el = ensureAssistantBubble(sess);
  2046. el.innerHTML = renderMd(sess.reasonBuffer);
  2047. if (sess.sessionId === activeSessionId) scrollToBottom();
  2048. });
  2049. }
  2050. }
  2051. function appendErrorChunk(sess, text) {
  2052. ensureAssistantBubble(sess);
  2053. var errEl = document.createElement('div');
  2054. errEl.className = 'chunk-error';
  2055. errEl.textContent = text;
  2056. insertBeforeActions(sess, errEl);
  2057. if (sess.sessionId === activeSessionId) scrollToBottom();
  2058. }
  2059. /* ===== Thinking Indicators ===== */
  2060. function showThinking(sess) {
  2061. removeThinking(sess);
  2062. sess.thinkingEl = document.createElement('div');
  2063. sess.thinkingEl.className = 'thinking-row';
  2064. sess.thinkingEl.innerHTML = '<div class="msg-avatar" style="background:linear-gradient(135deg,var(--accent),#a78bfa);color:#fff">'
  2065. + '<i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
  2066. + '<div class="thinking-bubble">思考中' + DOTS_HTML + '</div>';
  2067. sess.container.appendChild(sess.thinkingEl);
  2068. if (sess.sessionId === activeSessionId) scrollToBottom(true);
  2069. }
  2070. function removeThinking(sess) {
  2071. if (sess.thinkingEl && sess.thinkingEl.parentNode) { sess.thinkingEl.parentNode.removeChild(sess.thinkingEl); sess.thinkingEl = null; }
  2072. }
  2073. function showInlineThinking(sess) {
  2074. if (sess.inlineThinkingEl || !sess.currentBubbleEl) return;
  2075. sess.inlineThinkingEl = document.createElement('div');
  2076. sess.inlineThinkingEl.className = 'inline-thinking';
  2077. sess.inlineThinkingEl.innerHTML = '思考中 ' + DOTS_HTML;
  2078. insertBeforeActions(sess, sess.inlineThinkingEl);
  2079. if (sess.sessionId === activeSessionId) scrollToBottom();
  2080. }
  2081. function removeInlineThinking(sess) {
  2082. if (sess.inlineThinkingEl && sess.inlineThinkingEl.parentNode) { sess.inlineThinkingEl.parentNode.removeChild(sess.inlineThinkingEl); }
  2083. sess.inlineThinkingEl = null;
  2084. }
  2085. /* ===== HITL ===== */
  2086. function appendHitlCard(sess, toolName, command) {
  2087. ensureAssistantBubble(sess);
  2088. var card = document.createElement('div');
  2089. card.className = 'hitl-card';
  2090. card.innerHTML = '<div class="hitl-card-header">'
  2091. + '<i class="layui-icon layui-icon-tips"></i> \u9700\u8981\u6388\u6743'
  2092. + '</div>'
  2093. + '<div class="hitl-card-body">'
  2094. + '<div class="hitl-tool">\u5de5\u5177: <strong>' + escapeHtml(toolName || 'unknown') + '</strong></div>'
  2095. + (command ? '<div class="hitl-command">' + escapeHtml(command) + '</div>' : '')
  2096. + '</div>'
  2097. + '<div class="hitl-card-actions">'
  2098. + '<button class="hitl-btn hitl-btn-approve">\u6279\u51c6</button>'
  2099. + '<button class="hitl-btn hitl-btn-reject">\u62d2\u7edd</button>'
  2100. + '</div>';
  2101. insertBeforeActions(sess, card);
  2102. var approveBtn = card.querySelector('.hitl-btn-approve');
  2103. var rejectBtn = card.querySelector('.hitl-btn-reject');
  2104. approveBtn.addEventListener('click', function() {
  2105. approveBtn.disabled = true;
  2106. rejectBtn.disabled = true;
  2107. approveBtn.textContent = '\u5df2\u6279\u51c6';
  2108. rejectBtn.style.display = 'none';
  2109. card.style.borderColor = '#22c55e';
  2110. handleHitlResponse(sess, 'approve');
  2111. });
  2112. rejectBtn.addEventListener('click', function() {
  2113. approveBtn.disabled = true;
  2114. rejectBtn.disabled = true;
  2115. rejectBtn.textContent = '\u5df2\u62d2\u7edd';
  2116. approveBtn.style.display = 'none';
  2117. card.style.borderColor = '#ef4444';
  2118. handleHitlResponse(sess, 'reject');
  2119. });
  2120. if (sess.sessionId === activeSessionId) scrollToBottom();
  2121. }
  2122. function escapeHtml(str) {
  2123. var div = document.createElement('div');
  2124. div.textContent = str;
  2125. return div.innerHTML;
  2126. }
  2127. function handleHitlResponse(sess, action) {
  2128. if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
  2129. resetStreamState(sess);
  2130. sess.isStreaming = true;
  2131. if (sess.sessionId === activeSessionId) {
  2132. isStreaming = true;
  2133. setBtnStopMode();
  2134. }
  2135. var url = SSE_ENDPOINT + '?hitlAction=' + encodeURIComponent(action)
  2136. + '&sessionId=' + encodeURIComponent(sess.sessionId);
  2137. sess.eventSource = new EventSource(url);
  2138. sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
  2139. sess.eventSource.onerror = function() { finishStream(sess); };
  2140. }
  2141. /* ===== Send ===== */
  2142. function sendMessage() {
  2143. var text = getInputText();
  2144. if (!text && !pendingImageBase64) return;
  2145. /* Block only if the active session is currently streaming */
  2146. if (activeSessionId && sessionMap[activeSessionId] && sessionMap[activeSessionId].isStreaming) return;
  2147. if (!text) text = '请描述这张图片';
  2148. var imageDataUrl = pendingImageBase64 ? ('data:' + pendingImageMime + ';base64,' + pendingImageBase64) : null;
  2149. var imageBase64 = pendingImageBase64;
  2150. var imageMime = pendingImageMime;
  2151. if (currentChatIndex === -1) {
  2152. SESSION_ID = 'web-' + Date.now().toString(36);
  2153. saveChatToHistory(text);
  2154. }
  2155. clearInput();
  2156. clearImagePreview();
  2157. if (!inChatMode) switchToChatMode();
  2158. setActiveSession(SESSION_ID);
  2159. var sess = sessionMap[SESSION_ID];
  2160. appendUserMessage(sess, text, imageDataUrl);
  2161. sess.isStreaming = true;
  2162. isStreaming = true;
  2163. setBtnStopMode();
  2164. resetStreamState(sess);
  2165. showThinking(sess);
  2166. if (imageBase64) {
  2167. sendWithFetch(sess, text, imageBase64, imageMime);
  2168. } else {
  2169. sendWithEventSource(sess, text);
  2170. }
  2171. }
  2172. function sendWithEventSource(sess, text) {
  2173. if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
  2174. var url = SSE_ENDPOINT + '?input=' + encodeURIComponent(text)
  2175. + '&sessionId=' + encodeURIComponent(sess.sessionId);
  2176. sess.eventSource = new EventSource(url);
  2177. sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
  2178. sess.eventSource.onerror = function() { finishStream(sess); };
  2179. }
  2180. function sendWithFetch(sess, text, imageBase64, imageMime) {
  2181. if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
  2182. var body = JSON.stringify({ input: text, sessionId: sess.sessionId, imageBase64: imageBase64, imageMime: imageMime });
  2183. var abortCtrl = new AbortController();
  2184. sess.eventSource = { close: function() { abortCtrl.abort(); } };
  2185. fetch(SSE_ENDPOINT, {
  2186. method: 'POST',
  2187. headers: { 'Content-Type': 'application/json' },
  2188. signal: abortCtrl.signal,
  2189. body: body
  2190. }).then(function(resp) {
  2191. var reader = resp.body.getReader();
  2192. var decoder = new TextDecoder();
  2193. var buffer = '';
  2194. function pump() {
  2195. reader.read().then(function(result) {
  2196. if (result.done) { finishStream(sess); return; }
  2197. buffer += decoder.decode(result.value, { stream: true });
  2198. var lines = buffer.split('\n');
  2199. buffer = lines.pop();
  2200. for (var i = 0; i < lines.length; i++) {
  2201. var line = lines[i].trim();
  2202. if (line.startsWith('data:')) {
  2203. var raw = line.substring(5).trim();
  2204. handleSSEData(sess, raw);
  2205. }
  2206. }
  2207. pump();
  2208. }).catch(function() { finishStream(sess); });
  2209. }
  2210. pump();
  2211. }).catch(function() { finishStream(sess); });
  2212. }
  2213. /* ===== SSE Data Handling (Session-Aware) ===== */
  2214. function handleSSEData(sess, raw) {
  2215. if (raw === '[DONE]') { finishStream(sess); return; }
  2216. try {
  2217. var chunk = JSON.parse(raw);
  2218. if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
  2219. removeInlineThinking(sess);
  2220. switch (chunk.type) {
  2221. case 'reason': finishPendingTool(sess); appendReasonChunk(sess, chunk.text); break;
  2222. case 'text': finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, true); break;
  2223. case 'action': finishThinkingBlock(sess); appendActionEndChunk(sess, chunk.toolName, chunk.text); break;
  2224. case 'agent': finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, false); break;
  2225. case 'error': finishThinkingBlock(sess); appendErrorChunk(sess, chunk.text); break;
  2226. case 'hitl': finishThinkingBlock(sess); finishPendingTool(sess); appendHitlCard(sess, chunk.toolName, chunk.command); break;
  2227. }
  2228. sess.silenceTimer = setTimeout(function() {
  2229. if (sess.isStreaming && !sess.thinkingBlockEl) showInlineThinking(sess);
  2230. }, 1000);
  2231. } catch (e) {
  2232. console.error('SSE数据解析失败:', raw, e);
  2233. }
  2234. }
  2235. function finishStream(sess) {
  2236. sess.isStreaming = false;
  2237. if (sess.silenceTimer) { clearTimeout(sess.silenceTimer); sess.silenceTimer = null; }
  2238. removeThinking(sess);
  2239. removeInlineThinking(sess);
  2240. finishThinkingBlock(sess);
  2241. finishPendingTool(sess);
  2242. if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
  2243. resetStreamState(sess);
  2244. if (sess.sessionId === activeSessionId) {
  2245. isStreaming = false;
  2246. setBtnSendMode();
  2247. chatInput.focus();
  2248. }
  2249. loadSessionHistory();
  2250. }
  2251. </script>
  2252. </body>
  2253. </html>