| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552 |
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
- <head>
- <meta charset="UTF-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <title th:text="${nickname}">Solon Code Web</title>
- <link rel="stylesheet" href="/js/a/layui/css/layui.css"/>
- <link rel="stylesheet" href="/js/a/css/theme.css"/>
- <style>
- :root {
- --sidebar-width: 260px;
- }
- *, *::before, *::after {
- box-sizing: border-box;
- margin: 0;
- padding: 0;
- }
- html, body {
- height: 100%;
- overflow: hidden;
- }
- body {
- font-family: var(--font-sans);
- background: var(--bg-body);
- color: var(--text-primary);
- display: flex;
- font-size: 14px;
- transition: background 0.3s, color 0.3s;
- }
- /* ===== Sidebar ===== */
- .sidebar {
- width: var(--sidebar-width);
- background: var(--bg-sidebar);
- border-right: 1px solid var(--border-color);
- display: flex;
- flex-direction: column;
- flex-shrink: 0;
- height: 100vh;
- transition: background 0.3s;
- }
- .sidebar-header {
- padding: 20px 16px 16px;
- }
- .new-chat-btn {
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: center;
- gap: 8px;
- padding: 11px 0;
- border: 1px dashed var(--border-color);
- border-radius: 10px;
- background: transparent;
- color: var(--text-primary);
- cursor: pointer;
- font-size: 14px;
- transition: all 0.2s;
- }
- .new-chat-btn:hover {
- background: var(--accent-light);
- border-color: var(--accent);
- color: var(--accent);
- }
- .sidebar-title {
- padding: 16px 16px 8px;
- font-size: 11px;
- color: var(--text-secondary);
- font-weight: 600;
- letter-spacing: 1px;
- text-transform: uppercase;
- }
- .sidebar-list {
- flex: 1;
- overflow-y: auto;
- padding: 4px 10px;
- }
- .sidebar-item {
- padding: 10px 12px;
- border-radius: 10px;
- cursor: pointer;
- color: var(--text-sidebar);
- font-size: 13px;
- display: flex;
- align-items: center;
- transition: all 0.15s;
- margin-bottom: 2px;
- }
- .sidebar-item:hover {
- background: var(--bg-hover);
- }
- .sidebar-item.active {
- background: var(--accent-light);
- color: var(--accent);
- font-weight: 500;
- }
- .sidebar-item-label {
- flex: 1;
- min-width: 0;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .sidebar-item-del {
- display: none;
- flex-shrink: 0;
- width: 22px;
- height: 22px;
- border: none;
- background: none;
- border-radius: 6px;
- cursor: pointer;
- color: var(--text-secondary);
- font-size: 14px;
- align-items: center;
- justify-content: center;
- margin-left: 4px;
- transition: all 0.15s;
- }
- .sidebar-item:hover .sidebar-item-del {
- display: flex;
- }
- .sidebar-item-del:hover {
- background: #ef444422;
- color: #ef4444;
- }
- .sidebar-footer {
- padding: 14px 16px;
- border-top: 1px solid var(--border-color);
- display: flex;
- flex-direction: column;
- align-items: stretch;
- gap: 12px;
- }
- .sidebar-footer-meta {
- display: flex;
- align-items: center;
- gap: 10px;
- }
- .sidebar-config-btn {
- width: 100%;
- display: flex;
- align-items: center;
- justify-content: center;
- gap: 8px;
- padding: 10px 12px;
- border: 1px solid var(--border-color);
- border-radius: 10px;
- background: var(--bg-main);
- color: var(--text-primary);
- cursor: pointer;
- font-size: 13px;
- transition: all 0.2s;
- }
- .sidebar-config-btn:hover {
- border-color: var(--accent);
- background: var(--accent-light);
- color: var(--accent);
- }
- .model-modal {
- position: fixed;
- inset: 0;
- display: none;
- align-items: center;
- justify-content: center;
- padding: 24px;
- background: rgba(15, 23, 42, 0.45);
- z-index: 999;
- }
- .model-modal.active {
- display: flex;
- }
- .model-modal-card {
- width: min(680px, 100%);
- max-height: 90vh;
- overflow: hidden;
- display: flex;
- flex-direction: column;
- border: 1px solid var(--border-color);
- border-radius: 18px;
- background: var(--bg-input-box);
- box-shadow: var(--shadow-lg);
- }
- .model-modal-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 18px 20px 14px;
- border-bottom: 1px solid var(--border-color);
- }
- .model-modal-title {
- font-size: 18px;
- font-weight: 700;
- color: var(--text-primary);
- }
- .model-modal-close {
- width: 34px;
- height: 34px;
- border: none;
- border-radius: 10px;
- background: transparent;
- color: var(--text-secondary);
- cursor: pointer;
- transition: all 0.2s;
- }
- .model-modal-close:hover {
- background: var(--bg-hover);
- color: var(--text-primary);
- }
- .model-modal-body {
- padding: 18px 20px;
- overflow-y: auto;
- }
- .model-form-grid {
- display: grid;
- grid-template-columns: repeat(2, minmax(0, 1fr));
- gap: 16px;
- }
- .model-field {
- display: flex;
- flex-direction: column;
- gap: 8px;
- }
- .model-field.full {
- grid-column: 1 / -1;
- }
- .model-field label {
- font-size: 13px;
- font-weight: 600;
- color: var(--text-primary);
- }
- .model-field input,
- .model-field select,
- .model-field textarea {
- width: 100%;
- border: 1px solid var(--border-input);
- border-radius: 12px;
- background: var(--bg-main);
- color: var(--text-primary);
- padding: 11px 12px;
- font: inherit;
- outline: none;
- transition: all 0.2s;
- }
- .model-field input:focus,
- .model-field select:focus,
- .model-field textarea:focus {
- border-color: var(--accent);
- box-shadow: 0 0 0 4px rgba(79, 110, 247, 0.08);
- }
- .model-field textarea {
- min-height: 84px;
- resize: vertical;
- }
- .model-save-btn,
- .model-cancel-btn {
- height: 44px;
- border-radius: 12px;
- font-size: 14px;
- cursor: pointer;
- transition: all 0.2s;
- }
- .model-modal-footer {
- padding: 16px 20px 20px;
- border-top: 1px solid var(--border-color);
- display: flex;
- justify-content: flex-end;
- gap: 12px;
- }
- .model-cancel-btn {
- min-width: 92px;
- border: 1px solid var(--border-color);
- background: transparent;
- color: var(--text-secondary);
- }
- .model-cancel-btn:hover {
- background: var(--bg-hover);
- color: var(--text-primary);
- }
- .model-save-btn {
- min-width: 120px;
- border: none;
- background: var(--accent);
- color: #fff;
- }
- .model-save-btn:hover {
- background: var(--accent-hover);
- }
- .model-switch {
- display: flex;
- align-items: center;
- gap: 10px;
- padding-top: 8px;
- color: var(--text-primary);
- }
- .model-switch input {
- width: 16px;
- height: 16px;
- }
- .model-hint {
- margin-top: 4px;
- font-size: 12px;
- color: var(--text-secondary);
- }
- /* Streaming indicator for sidebar items */
- .sidebar-item.streaming {
- border-left: 3px solid var(--accent);
- padding-left: 9px;
- }
- .sidebar-item.streaming:hover .sidebar-item-del {
- display: none;
- }
- .sidebar-item-spinner {
- flex-shrink: 0;
- width: 16px;
- height: 16px;
- border: 2px solid var(--border-color);
- border-top-color: var(--accent);
- border-radius: 50%;
- animation: blob-spin 0.8s linear infinite;
- margin-left: 6px;
- }
- /* ===== Main ===== */
- .main-area {
- flex: 1;
- display: flex;
- flex-direction: column;
- height: 100vh;
- min-width: 0;
- background: var(--bg-main);
- transition: background 0.3s;
- }
- /* Header */
- .main-header {
- padding: 14px 28px;
- display: flex;
- align-items: center;
- justify-content: flex-end;
- flex-shrink: 0;
- }
- .theme-toggle {
- background: none;
- border: 1px solid var(--border-color);
- border-radius: 10px;
- padding: 6px 10px;
- cursor: pointer;
- color: var(--text-secondary);
- font-size: 16px;
- transition: all 0.2s;
- display: flex;
- align-items: center;
- }
- .theme-toggle:hover {
- background: var(--bg-hover);
- color: var(--text-primary);
- }
- /* ===== Welcome ===== */
- .welcome-view {
- flex: 1;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 0 24px 80px;
- }
- .welcome-avatar {
- width: 120px;
- height: 120px;
- position: relative;
- margin-bottom: 28px;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .welcome-avatar .blob {
- position: absolute;
- }
- .welcome-avatar .blob-outer {
- width: 110px;
- height: 110px;
- background: conic-gradient(from 0deg, #4f6ef7, #818cf8 15%, #a78bfa 30%, #c084fc 50%, #818cf8 70%, #4f6ef7 85%, #4f6ef7);
- border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
- animation: blob-morph1 8s ease-in-out infinite, blob-spin 6s linear infinite;
- opacity: 0.82;
- filter: blur(1.5px) drop-shadow(0 0 14px rgba(79, 110, 247, 0.4));
- }
- .welcome-avatar .blob-core {
- width: 38px;
- height: 38px;
- z-index: 1;
- border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
- background: linear-gradient(135deg, var(--accent), #a78bfa, #c084fc, var(--accent));
- background-size: 300% 300%;
- animation: core-flow 4s ease infinite, core-morph 5s ease-in-out infinite, core-pulse 3s ease-in-out infinite;
- box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
- filter: blur(0.5px);
- }
- @keyframes blob-morph1 {
- 0%, 100% {
- border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%;
- }
- 25% {
- border-radius: 30% 60% 70% 40% / 50% 60% 30% 60%;
- }
- 50% {
- border-radius: 50% 60% 30% 60% / 30% 50% 70% 60%;
- }
- 75% {
- border-radius: 40% 60% 50% 40% / 60% 40% 60% 50%;
- }
- }
- @keyframes blob-spin {
- to {
- transform: rotate(360deg);
- }
- }
- @keyframes core-flow {
- 0% {
- background-position: 0% 50%;
- }
- 50% {
- background-position: 100% 50%;
- }
- 100% {
- background-position: 0% 50%;
- }
- }
- @keyframes core-morph {
- 0%, 100% {
- border-radius: 50% 40% 60% 35% / 40% 55% 35% 60%;
- }
- 20% {
- border-radius: 35% 55% 40% 60% / 55% 35% 60% 40%;
- }
- 40% {
- border-radius: 60% 35% 50% 45% / 35% 60% 45% 55%;
- }
- 60% {
- border-radius: 40% 60% 35% 55% / 60% 40% 55% 35%;
- }
- 80% {
- border-radius: 55% 45% 55% 40% / 45% 50% 40% 60%;
- }
- }
- @keyframes core-pulse {
- 0%, 100% {
- transform: scale(1);
- box-shadow: 0 0 20px rgba(79, 110, 247, 0.45), 0 0 44px rgba(167, 139, 250, 0.2);
- }
- 50% {
- transform: scale(1.1);
- box-shadow: 0 0 28px rgba(79, 110, 247, 0.55), 0 0 56px rgba(167, 139, 250, 0.3);
- }
- }
- .welcome-title {
- font-size: 26px;
- font-weight: 700;
- margin-bottom: 10px;
- }
- .welcome-sub {
- color: var(--text-secondary);
- font-size: 15px;
- margin-bottom: 24px;
- }
- /* Quick Questions */
- .quick-questions {
- width: 100%;
- max-width: 680px;
- margin-bottom: 16px;
- display: flex;
- flex-wrap: wrap;
- gap: 8px;
- }
- .quick-q-chip {
- padding: 6px 14px;
- border: 1px solid var(--border-color);
- border-radius: 20px;
- background: var(--bg-input-box);
- color: var(--text-secondary);
- font-size: 13px;
- cursor: pointer;
- transition: all 0.2s;
- white-space: nowrap;
- max-width: 320px;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .quick-q-chip:hover {
- border-color: var(--accent);
- color: var(--accent);
- background: var(--accent-light);
- }
- .welcome-input-box {
- width: 100%;
- max-width: 680px;
- background: var(--bg-input-box);
- border: 1px solid var(--border-input);
- border-radius: 16px;
- box-shadow: var(--shadow-lg);
- display: flex;
- flex-direction: column;
- transition: all 0.25s;
- }
- .welcome-input-box:focus-within {
- border-color: var(--accent);
- box-shadow: var(--shadow-lg), 0 0 0 4px rgba(79, 110, 247, 0.08);
- }
- .welcome-input-box textarea {
- border: none;
- outline: none;
- resize: none;
- background: transparent;
- padding: 18px 20px 8px;
- font-family: var(--font-sans);
- font-size: 15px;
- color: var(--text-primary);
- min-height: 54px;
- max-height: 140px;
- line-height: 1.6;
- }
- .welcome-input-box textarea::placeholder {
- color: var(--text-secondary);
- }
- .welcome-toolbar {
- display: flex;
- align-items: center;
- justify-content: flex-end;
- padding: 8px 14px;
- }
- .welcome-send-btn {
- width: 40px;
- height: 40px;
- border-radius: 50%;
- border: none;
- background: var(--accent);
- color: #fff;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 18px;
- transition: all 0.2s;
- }
- .welcome-send-btn:hover {
- background: var(--accent-hover);
- transform: scale(1.06);
- }
- /* ===== Chat View ===== */
- .chat-view {
- flex: 1;
- display: none;
- flex-direction: column;
- min-height: 0;
- }
- .chat-view.active {
- display: flex;
- }
- .messages-wrap {
- flex: 1;
- overflow-y: auto;
- }
- .messages-inner {
- max-width: 820px;
- margin: 0 auto;
- padding: 28px 24px 12px;
- }
- /* Message */
- .msg-row {
- display: flex;
- margin-bottom: 28px;
- gap: 14px;
- animation: msg-in 0.25s ease-out;
- }
- @keyframes msg-in {
- from {
- opacity: 0;
- transform: translateY(8px);
- }
- to {
- opacity: 1;
- transform: none;
- }
- }
- .msg-row.user {
- justify-content: flex-end;
- }
- .msg-row.assistant {
- justify-content: flex-start;
- }
- .msg-avatar {
- width: 38px;
- height: 38px;
- border-radius: 12px;
- flex-shrink: 0;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 14px;
- font-weight: 600;
- }
- .msg-row.assistant .msg-avatar {
- background: linear-gradient(135deg, var(--accent), #a78bfa);
- color: #fff;
- }
- .msg-row.user .msg-avatar {
- background: var(--bg-hover);
- color: var(--text-secondary);
- }
- .msg-bubble {
- max-width: 75%;
- line-height: 1.75;
- word-break: break-word;
- }
- .msg-row.user .msg-bubble {
- background: var(--bg-user-msg);
- color: var(--text-user-msg);
- padding: 12px 18px;
- border-radius: 20px 20px 4px 20px;
- font-size: 14px;
- }
- .msg-row.assistant .msg-bubble {
- max-width: 88%;
- min-width: 88%;
- }
- /* Markdown */
- .md-content {
- padding: 2px 0;
- color: var(--text-ai-msg);
- font-size: 14px;
- }
- .md-content > :first-child {
- margin-top: 0;
- }
- .md-content > :last-child {
- margin-bottom: 0;
- }
- .md-content p {
- margin: 7px 0;
- }
- .md-content h1, .md-content h2, .md-content h3, .md-content h4 {
- margin: 16px 0 8px;
- font-weight: 600;
- color: var(--text-primary);
- }
- .md-content h1 {
- font-size: 1.35em;
- }
- .md-content h2 {
- font-size: 1.2em;
- }
- .md-content h3 {
- font-size: 1.08em;
- }
- .md-content strong {
- font-weight: 600;
- }
- .md-content code {
- background: var(--bg-code);
- padding: 2px 7px;
- border-radius: 5px;
- font-family: var(--font-mono);
- font-size: 0.87em;
- }
- .md-content pre {
- background: var(--bg-code-block);
- padding: 16px 18px;
- border-radius: 10px;
- overflow-x: auto;
- margin: 10px 0;
- }
- .md-content pre code {
- background: none;
- padding: 0;
- color: var(--text-code);
- font-size: 13px;
- }
- .md-content ul, .md-content ol {
- padding-left: 22px;
- margin: 7px 0;
- }
- .md-content li {
- margin: 4px 0;
- }
- .md-content blockquote {
- border-left: 3px solid var(--accent);
- padding-left: 14px;
- color: var(--text-secondary);
- margin: 10px 0;
- }
- .md-content a {
- color: var(--accent);
- text-decoration: none;
- }
- .md-content a:hover {
- text-decoration: underline;
- }
- .md-content table {
- border-collapse: collapse;
- margin: 10px 0;
- width: 100%;
- font-size: 13px;
- }
- .md-content th, .md-content td {
- border: 1px solid var(--border-color);
- padding: 7px 12px;
- text-align: left;
- }
- .md-content th {
- background: var(--bg-hover);
- font-weight: 600;
- }
- .md-content img {
- max-width: 100%;
- border-radius: 8px;
- }
- .md-content hr {
- border: none;
- border-top: 1px solid var(--border-color);
- margin: 14px 0;
- }
- /* Tool Card */
- .tool-card {
- margin: 10px 0;
- border: 1px solid var(--border-color);
- border-radius: 10px;
- overflow: hidden;
- transition: border-color 0.2s;
- }
- .tool-card-header {
- display: flex;
- align-items: center;
- gap: 10px;
- padding: 10px 14px;
- cursor: pointer;
- transition: background 0.15s;
- font-size: 13px;
- }
- .tool-card-header:hover {
- background: var(--bg-hover);
- }
- .tool-status-icon {
- width: 22px;
- height: 22px;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-shrink: 0;
- font-size: 12px;
- transition: all 0.3s;
- }
- .tool-status-icon.loading {
- border: 2px solid var(--border-color);
- border-top-color: var(--accent);
- animation: blob-spin 0.8s linear infinite;
- }
- .tool-status-icon.done {
- background: #22c55e;
- color: #fff;
- border: none;
- animation: none;
- }
- .tool-name {
- font-weight: 600;
- color: var(--text-primary);
- }
- .tool-label {
- color: var(--text-secondary);
- font-size: 12px;
- }
- .tool-toggle {
- margin-left: auto;
- color: var(--text-secondary);
- font-size: 14px;
- transition: transform 0.2s;
- }
- .tool-card.expanded .tool-toggle {
- transform: rotate(90deg);
- }
- .tool-card-body {
- display: none;
- padding: 0 14px 12px;
- font-family: var(--font-mono);
- font-size: 12px;
- color: var(--text-secondary);
- white-space: pre-wrap;
- word-break: break-all;
- max-height: 200px;
- overflow-y: auto;
- border-top: 1px solid var(--border-color);
- }
- .tool-card.expanded .tool-card-body {
- display: block;
- }
- .chunk-error {
- margin: 8px 0;
- padding: 10px 14px;
- background: var(--error-bg);
- border-left: 3px solid var(--error-border);
- border-radius: 6px;
- color: var(--error-text);
- font-size: 13px;
- }
- /* HITL Card */
- .hitl-card {
- margin: 10px 0;
- border: 1px solid #f59e0b;
- border-radius: 10px;
- overflow: hidden;
- background: var(--bg-input-box);
- animation: msg-in 0.25s ease-out;
- }
- .hitl-card-header {
- display: flex;
- align-items: center;
- gap: 10px;
- padding: 12px 14px;
- font-size: 14px;
- font-weight: 600;
- color: #f59e0b;
- border-bottom: 1px solid var(--border-color);
- }
- .hitl-card-header i {
- font-size: 18px;
- }
- .hitl-card-body {
- padding: 12px 14px;
- font-size: 13px;
- color: var(--text-primary);
- }
- .hitl-card-body .hitl-tool {
- margin-bottom: 6px;
- color: var(--text-secondary);
- }
- .hitl-card-body .hitl-tool strong {
- color: var(--text-primary);
- }
- .hitl-card-body .hitl-command {
- background: var(--bg-code-block);
- padding: 10px 14px;
- border-radius: 8px;
- font-family: var(--font-mono);
- font-size: 13px;
- color: var(--text-code);
- word-break: break-all;
- margin: 8px 0 12px;
- }
- .hitl-card-actions {
- display: flex;
- gap: 10px;
- padding: 0 14px 14px;
- }
- .hitl-btn {
- padding: 8px 20px;
- border-radius: 8px;
- border: none;
- cursor: pointer;
- font-size: 13px;
- font-weight: 500;
- transition: all 0.2s;
- display: flex;
- align-items: center;
- gap: 6px;
- }
- .hitl-btn-approve {
- background: #22c55e;
- color: #fff;
- }
- .hitl-btn-approve:hover {
- background: #16a34a;
- }
- .hitl-btn-reject {
- background: #ef4444;
- color: #fff;
- }
- .hitl-btn-reject:hover {
- background: #dc2626;
- }
- .hitl-btn:disabled {
- opacity: 0.5;
- cursor: not-allowed;
- }
- /* Msg Actions */
- .msg-actions {
- display: flex;
- gap: 6px;
- margin-top: 10px;
- opacity: 0;
- transition: opacity 0.2s;
- }
- .msg-row.assistant:hover .msg-actions {
- opacity: 1;
- }
- .msg-action-btn {
- background: none;
- border: 1px solid var(--border-color);
- border-radius: 8px;
- padding: 5px 10px;
- cursor: pointer;
- color: var(--text-secondary);
- font-size: 12px;
- transition: all 0.15s;
- display: flex;
- align-items: center;
- gap: 4px;
- }
- .msg-action-btn:hover {
- background: var(--bg-hover);
- color: var(--text-primary);
- border-color: var(--accent);
- }
- /* Thinking */
- .inline-thinking {
- display: flex;
- align-items: center;
- gap: 8px;
- padding: 8px 0;
- color: var(--text-secondary);
- font-size: 13px;
- }
- .thinking-row {
- display: flex;
- gap: 14px;
- margin-bottom: 28px;
- animation: msg-in 0.25s ease-out;
- }
- .thinking-bubble {
- display: flex;
- align-items: center;
- gap: 10px;
- color: var(--text-secondary);
- font-size: 14px;
- padding: 10px 0;
- }
- .thinking-dots {
- display: flex;
- gap: 5px;
- }
- .thinking-dots span {
- width: 7px;
- height: 7px;
- border-radius: 50%;
- background: var(--thinking-dot);
- animation: dot-bounce 1.4s ease-in-out infinite;
- }
- .thinking-dots span:nth-child(2) {
- animation-delay: 0.2s;
- }
- .thinking-dots span:nth-child(3) {
- animation-delay: 0.4s;
- }
- @keyframes dot-bounce {
- 0%, 80%, 100% {
- opacity: 0.25;
- transform: scale(0.85);
- }
- 40% {
- opacity: 1;
- transform: scale(1.15);
- }
- }
- /* Thinking Block (collapsible reasoning content) */
- .thinking-block {
- margin: 0 0 12px;
- border: 1px solid var(--thinking-block-border);
- border-radius: 10px;
- overflow: hidden;
- background: var(--thinking-block-bg);
- }
- .thinking-block-header {
- display: flex;
- align-items: center;
- gap: 8px;
- padding: 10px 14px;
- cursor: pointer;
- font-size: 13px;
- user-select: none;
- transition: background 0.15s;
- }
- .thinking-block-header:hover {
- background: var(--bg-hover);
- }
- .thinking-block-label {
- color: var(--text-secondary);
- font-weight: 500;
- }
- .thinking-block-toggle {
- margin-left: auto;
- color: var(--text-secondary);
- font-size: 12px;
- transition: transform 0.25s;
- }
- .thinking-block.expanded .thinking-block-toggle {
- transform: rotate(90deg);
- }
- .thinking-block-body {
- padding: 0 14px 12px;
- font-size: 13px;
- color: var(--text-secondary);
- line-height: 1.7;
- border-top: 1px solid var(--thinking-block-border);
- max-height: 300px;
- overflow-y: auto;
- }
- .thinking-block-body .md-content {
- color: var(--text-secondary);
- font-size: 13px;
- }
- .thinking-block:not(.expanded) .thinking-block-body {
- display: none;
- }
- .thinking-block.streaming {
- border-color: var(--accent);
- }
- .thinking-block.streaming .thinking-block-label {
- color: var(--accent);
- }
- .thinking-block-dots {
- display: inline-flex;
- gap: 4px;
- margin-left: 4px;
- vertical-align: middle;
- }
- .thinking-block-dots span {
- width: 5px;
- height: 5px;
- border-radius: 50%;
- background: var(--accent);
- animation: dot-bounce 1.4s ease-in-out infinite;
- }
- .thinking-block-dots span:nth-child(2) {
- animation-delay: 0.2s;
- }
- .thinking-block-dots span:nth-child(3) {
- animation-delay: 0.4s;
- }
- /* Bottom Input */
- .input-wrap {
- padding: 12px 24px 22px;
- flex-shrink: 0;
- }
- .input-box {
- max-width: 820px;
- margin: 0 auto;
- background: var(--bg-input-box);
- border: 1px solid var(--border-input);
- border-radius: 14px;
- box-shadow: var(--shadow);
- display: flex;
- flex-direction: column;
- transition: all 0.25s;
- }
- .input-box:focus-within {
- border-color: var(--accent);
- box-shadow: var(--shadow), 0 0 0 3px rgba(79, 110, 247, 0.06);
- }
- .input-box textarea {
- border: none;
- outline: none;
- resize: none;
- background: transparent;
- padding: 14px 16px 6px;
- font-family: var(--font-sans);
- font-size: 14px;
- color: var(--text-primary);
- min-height: 44px;
- max-height: 140px;
- line-height: 1.6;
- }
- .input-box textarea::placeholder {
- color: var(--text-secondary);
- }
- .input-toolbar {
- display: flex;
- align-items: center;
- justify-content: flex-end;
- padding: 6px 10px;
- }
- .send-btn {
- width: 36px;
- height: 36px;
- border-radius: 50%;
- border: none;
- background: var(--accent);
- color: #fff;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 18px;
- transition: all 0.2s;
- }
- .send-btn:hover {
- background: var(--accent-hover);
- }
- .send-btn:disabled {
- opacity: 0.4;
- cursor: not-allowed;
- }
- .send-btn.stop-mode {
- background: #ef4444;
- }
- .send-btn.stop-mode:hover {
- background: #dc2626;
- }
- .send-btn.stop-mode .stop-icon {
- width: 14px;
- height: 14px;
- background: #fff;
- border-radius: 3px;
- }
- /* Image Preview */
- .img-preview-wrap {
- padding: 8px 14px 0;
- display: none;
- }
- .img-preview-wrap.has-img {
- display: flex;
- align-items: flex-start;
- gap: 8px;
- }
- .img-preview {
- position: relative;
- display: inline-block;
- }
- .img-preview img {
- max-height: 80px;
- max-width: 200px;
- border-radius: 8px;
- border: 1px solid var(--border-color);
- object-fit: cover;
- }
- .img-preview-remove {
- position: absolute;
- top: -6px;
- right: -6px;
- width: 20px;
- height: 20px;
- border-radius: 50%;
- background: #ef4444;
- color: #fff;
- border: 2px solid var(--bg-input-box);
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 12px;
- line-height: 1;
- }
- .img-preview-remove:hover {
- background: #dc2626;
- }
- /* Scrollbar */
- .messages-wrap::-webkit-scrollbar, .sidebar-list::-webkit-scrollbar, .thinking-block-body::-webkit-scrollbar {
- width: 5px;
- }
- .messages-wrap::-webkit-scrollbar-thumb, .sidebar-list::-webkit-scrollbar-thumb, .thinking-block-body::-webkit-scrollbar-thumb {
- background: var(--border-color);
- border-radius: 3px;
- }
- .messages-wrap::-webkit-scrollbar-track, .sidebar-list::-webkit-scrollbar-track, .thinking-block-body::-webkit-scrollbar-track {
- background: transparent;
- }
- @media (max-width: 768px) {
- .sidebar {
- display: none;
- }
- .model-form-grid {
- grid-template-columns: 1fr;
- }
- }
- </style>
- </head>
- <body data-theme="light">
- <!-- Sidebar -->
- <div class="sidebar">
- <div class="sidebar-header">
- <button class="new-chat-btn" id="newChatBtn">
- <i class="layui-icon layui-icon-addition"></i> 新对话
- </button>
- </div>
- <div class="sidebar-title">最近对话</div>
- <div class="sidebar-list" id="historyList"></div>
- <div class="sidebar-footer">
- <!-- <div class="sidebar-footer-meta">-->
- <!-- <i class="layui-icon layui-icon-util" style="font-size:16px;color:var(--text-secondary)"></i>-->
- <!-- <span style="font-size:12px;color:var(--text-secondary)" th:text="${nickname}">AI</span>-->
- <!-- </div>-->
- <button class="sidebar-config-btn" id="modelConfigBtn" type="button">
- <i class="layui-icon layui-icon-set-fill"></i>
- <span>模型配置</span>
- </button>
- </div>
- </div>
- <!-- Main -->
- <div class="main-area">
- <div class="main-header">
- <!-- <button class="theme-toggle" id="themeBtn" title="切换主题">
- <i class="layui-icon" id="themeIcon"></i>
- </button>-->
- </div>
- <!-- Welcome View -->
- <div class="welcome-view" id="welcomeView">
- <div class="welcome-avatar">
- <div class="blob blob-outer"></div>
- <div class="blob-core"></div>
- </div>
- <div class="welcome-title" th:text="'你好,我是 ' + ${nickname} + '!'">你好,我是 清鉴 !</div>
- <div class="welcome-sub">有什么我可以帮你的吗?</div>
- <div class="quick-questions" id="welcomeQuickQs"></div>
- <div class="welcome-input-box">
- <div class="img-preview-wrap" id="welcomeImgWrap">
- <div class="img-preview"><img id="welcomeImgPreview" src="" alt=""/><span class="img-preview-remove"
- id="welcomeImgRemove">×</span>
- </div>
- </div>
- <textarea id="welcomeInput" placeholder="和我聊聊天吧..." rows="1" autofocus></textarea>
- <div class="welcome-toolbar">
- <button class="welcome-send-btn" id="welcomeSendBtn" title="发送">
- <i class="layui-icon layui-icon-release"></i>
- </button>
- </div>
- </div>
- </div>
- <!-- Chat View -->
- <div class="chat-view" id="chatView">
- <div class="messages-wrap" id="messagesWrap">
- <!-- Per-session containers are added here dynamically -->
- </div>
- <div class="input-wrap">
- <div class="quick-questions" id="chatQuickQs" style="max-width:820px;margin:0 auto 10px;"></div>
- <div class="input-box">
- <div class="img-preview-wrap" id="chatImgWrap">
- <div class="img-preview"><img id="chatImgPreview" src="" alt=""/><span class="img-preview-remove"
- id="chatImgRemove">×</span>
- </div>
- </div>
- <textarea id="chatInput" placeholder="请输入消息..." rows="1"></textarea>
- <div class="input-toolbar">
- <button class="send-btn" id="chatSendBtn" title="发送">
- <i class="layui-icon layui-icon-release"></i>
- </button>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="model-modal" id="modelConfigModal">
- <div class="model-modal-card" role="dialog" aria-modal="true" aria-labelledby="modelConfigTitle">
- <div class="model-modal-header">
- <div>
- <div class="model-modal-title" id="modelConfigTitle">模型配置</div>
- <div class="model-hint">系统当前仅维护一套聊天模型配置,打开后会自动加载默认模型信息。 </div>
- </div>
- <button class="model-modal-close" id="modelConfigCloseBtn" type="button" aria-label="关闭">
- <i class="layui-icon layui-icon-close"></i>
- </button>
- </div>
- <div class="model-modal-body">
- <form id="modelConfigForm">
- <input type="hidden" id="modelId" name="id"/>
- <input type="hidden" id="modelCategory" name="category" value="chat"/>
- <div class="model-form-grid">
- <div class="model-field">
- <label for="providerCode">服务商</label>
- <select id="providerCode" name="providerCode"></select>
- </div>
- <div class="model-field">
- <label for="modelName">模型名称</label>
- <input id="modelName" name="modelName" type="text" placeholder="例如:qwen3.5-plus" required/>
- </div>
- <div class="model-field full">
- <label for="apiHost">API Host</label>
- <input id="apiHost" name="apiHost" type="text"
- placeholder="例如:https://dashscope.aliyuncs.com/compatible-mode/v1"/>
- </div>
- <div class="model-field full">
- <label for="apiKey">API Key</label>
- <input id="apiKey" name="apiKey" type="text" placeholder="请输入 API Key"/>
- </div>
- <div class="model-field">
- <label for="hasUse">启用状态</label>
- <div class="model-switch">
- <input id="hasUse" type="checkbox"/>
- <span>设为当前聊天使用模型</span>
- </div>
- </div>
- </div>
- </form>
- </div>
- <div class="model-modal-footer">
- <button class="model-cancel-btn" id="modelCancelBtn" type="button">取消</button>
- <button class="model-save-btn" id="modelSaveBtn" type="button">保存配置</button>
- </div>
- </div>
- </div>
- <script src="/js/a/layui/layui.js"></script>
- <script src="/js/a/js/marked.min.js"></script>
- <script th:inline="javascript">
- var SSE_ENDPOINT = /*[[${sseEndpoint}]]*/ '/js/a/ai/chat';
- </script>
- <script>
- /* ===== DOM ===== */
- var welcomeView = document.getElementById('welcomeView');
- var chatView = document.getElementById('chatView');
- var messagesWrap = document.getElementById('messagesWrap');
- var welcomeInput = document.getElementById('welcomeInput');
- var welcomeSendBtn = document.getElementById('welcomeSendBtn');
- var chatInput = document.getElementById('chatInput');
- var chatSendBtn = document.getElementById('chatSendBtn');
- // var themeBtn = document.getElementById('themeBtn');
- var themeIcon = document.getElementById('themeIcon');
- var newChatBtn = document.getElementById('newChatBtn');
- var historyList = document.getElementById('historyList');
- var modelConfigBtn = document.getElementById('modelConfigBtn');
- var modelConfigModal = document.getElementById('modelConfigModal');
- var modelConfigCloseBtn = document.getElementById('modelConfigCloseBtn');
- var modelCancelBtn = document.getElementById('modelCancelBtn');
- var modelSaveBtn = document.getElementById('modelSaveBtn');
- var providerCodeSelect = document.getElementById('providerCode');
- var modelConfigForm = document.getElementById('modelConfigForm');
- var modelIdInput = document.getElementById('modelId');
- var modelNameInput = document.getElementById('modelName');
- var apiHostInput = document.getElementById('apiHost');
- var apiKeyInput = document.getElementById('apiKey');
- var hasUseInput = document.getElementById('hasUse');
- /* ===== Constants ===== */
- var DOTS_HTML = '<span class="thinking-dots"><span></span><span></span><span></span></span>';
- var CHAT_MODEL_CATEGORY = 'chat';
- var SINGLE_MODEL_ID = '1';
- /* ===== Per-Session State ===== */
- function SessionState(sessionId) {
- this.sessionId = sessionId;
- this.container = document.createElement('div');
- this.container.className = 'messages-inner';
- this.container.style.display = 'none';
- messagesWrap.appendChild(this.container);
- this.eventSource = null;
- this.isStreaming = false;
- this.currentBubbleEl = null;
- this.reasonBuffer = '';
- this.thinkingBlockEl = null;
- this.thinkingBodyMdEl = null;
- this.thinkingBodyWrapEl = null;
- this.thinkingBuffer = '';
- this.pendingToolCard = null;
- this.thinkingEl = null;
- this.inlineThinkingEl = null;
- this.silenceTimer = null;
- this.contentRafId = null;
- this.reasonRafId = null;
- }
- var sessionMap = {};
- var activeSessionId = null;
- /* ===== Global State ===== */
- var SESSION_ID = 'web-' + Date.now().toString(36);
- var isStreaming = false;
- var inChatMode = false;
- var chatHistory = [];
- var currentChatIndex = -1;
- var pendingImageBase64 = null;
- var pendingImageMime = null;
- var userScrolledUp = false;
- var providerOptions = [
- {providerCode: 'ollama', providerName: 'Ollama'},
- {providerCode: 'qwen', providerName: '千问'},
- {providerCode: 'DeepSeek', providerName: 'DeepSeek'}
- ];
- var modelConfigLoading = false;
- /* ===== Session Helpers ===== */
- function getOrCreateSession(sessionId) {
- if (!sessionMap[sessionId]) {
- sessionMap[sessionId] = new SessionState(sessionId);
- }
- return sessionMap[sessionId];
- }
- function setActiveSession(sessionId) {
- if (activeSessionId && sessionMap[activeSessionId]) {
- sessionMap[activeSessionId].container.style.display = 'none';
- }
- var sess = getOrCreateSession(sessionId);
- sess.container.style.display = '';
- activeSessionId = sessionId;
- SESSION_ID = sessionId;
- isStreaming = sess.isStreaming;
- userScrolledUp = false;
- if (isStreaming) setBtnStopMode();
- else setBtnSendMode();
- }
- function deactivateSession() {
- if (activeSessionId && sessionMap[activeSessionId]) {
- sessionMap[activeSessionId].container.style.display = 'none';
- }
- activeSessionId = null;
- isStreaming = false;
- setBtnSendMode();
- }
- function showMessage(message, type) {
- if (window.layui && layui.layer) {
- layui.layer.msg(message, {icon: type === 'error' ? 2 : 1, time: 1800});
- return;
- }
- alert(message);
- }
- function toggleModelModal(show) {
- modelConfigModal.classList.toggle('active', !!show);
- if (show) {
- document.body.style.overflow = 'hidden';
- } else {
- document.body.style.overflow = '';
- }
- }
- function resetModelForm() {
- modelConfigForm.reset();
- modelIdInput.value = SINGLE_MODEL_ID;
- document.getElementById('modelCategory').value = CHAT_MODEL_CATEGORY;
- hasUseInput.checked = true;
- if (providerCodeSelect.options.length > 0) {
- providerCodeSelect.selectedIndex = 0;
- }
- }
- function fillModelForm(model) {
- modelIdInput.value = model && model.id ? model.id : '';
- document.getElementById('modelCategory').value = model && model.category ? model.category : CHAT_MODEL_CATEGORY;
- modelNameInput.value = model && model.modelName ? model.modelName : '';
- apiHostInput.value = model && model.apiHost ? model.apiHost : '';
- apiKeyInput.value = model && model.apiKey ? model.apiKey : '';
- hasUseInput.checked = !model || model.hasUse == null ? true : Number(model.hasUse) === 1;
- providerCodeSelect.value = model && model.providerCode ? model.providerCode : '';
- }
- function renderProviderSelect() {
- var html = '<option value="">请选择服务商</option>';
- for (var i = 0; i < providerOptions.length; i++) {
- var item = providerOptions[i];
- html += '<option value="' + escapeHtml(item.providerCode || '') + '">' + escapeHtml(item.providerName || item.providerCode || '') + '</option>';
- }
- providerCodeSelect.innerHTML = html;
- }
- function httpGetJson(url, callback, onError) {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.onreadystatechange = function () {
- if (xhr.readyState !== 4) return;
- if (xhr.status >= 200 && xhr.status < 300) {
- try {
- callback(JSON.parse(xhr.responseText));
- } catch (e) {
- console.log(e)
- if (onError) onError(e);
- }
- return;
- }
- if (onError) onError(xhr);
- };
- xhr.send();
- }
- function unwrapResponse(payload) {
- if (!payload || typeof payload !== 'object') {
- return payload;
- }
- if (Object.prototype.hasOwnProperty.call(payload, 'data')) {
- return payload.data;
- }
- return payload;
- }
- function loadModelConfigData(done) {
- if (modelConfigLoading) return;
- modelConfigLoading = true;
- var pending = 1;
- var failed = false;
- function finish() {
- pending--;
- if (pending > 0) return;
- modelConfigLoading = false;
- if (!failed && done) done();
- }
- function handleError() {
- failed = true;
- modelConfigLoading = false;
- showMessage('加载模型配置失败', 'error');
- }
- renderProviderSelect();
- httpGetJson('/js/a/ai/model/getInfo?id=' + encodeURIComponent(SINGLE_MODEL_ID), function (data) {
- resetModelForm();
- fillModelForm(unwrapResponse(data) || {});
- finish();
- }, handleError);
- }
- function openModelConfigModal() {
- loadModelConfigData(function () {
- toggleModelModal(true);
- });
- }
- function saveModelConfig() {
- var providerCode = providerCodeSelect.value.trim();
- var modelName = modelNameInput.value.trim();
- var apiHost = apiHostInput.value.trim();
- var apiKey = apiKeyInput.value.trim();
- if (!providerCode || !modelName || !apiHost || !apiKey) {
- showMessage('请完整填写服务商、模型名称、API Host 和 API Key', 'error');
- return;
- }
- modelSaveBtn.disabled = true;
- var params = new URLSearchParams();
- params.append('id', modelIdInput.value || SINGLE_MODEL_ID);
- params.append('category', CHAT_MODEL_CATEGORY);
- params.append('providerCode', providerCode);
- params.append('modelName', modelName);
- params.append('apiHost', apiHost);
- params.append('apiKey', apiKey);
- params.append('hasUse', hasUseInput.checked ? '1' : '0');
- fetch('/js/a/ai/create', {
- method: 'POST',
- headers: {'Content-Type': 'application/json; charset=UTF-8'},
- body: JSON.stringify(Object.fromEntries(params))
- }).then(function (resp) {
- if (!resp.ok) {
- throw new Error('save failed');
- }
- return resp.text();
- }).then(function () {
- showMessage('模型配置已保存');
- toggleModelModal(false);
- }).catch(function () {
- showMessage('保存模型配置失败', 'error');
- }).finally(function () {
- modelSaveBtn.disabled = false;
- });
- }
- /* ===== Image Paste ===== */
- var welcomeImgWrap = document.getElementById('welcomeImgWrap');
- var welcomeImgPreview = document.getElementById('welcomeImgPreview');
- var welcomeImgRemove = document.getElementById('welcomeImgRemove');
- var chatImgWrap = document.getElementById('chatImgWrap');
- var chatImgPreview = document.getElementById('chatImgPreview');
- var chatImgRemove = document.getElementById('chatImgRemove');
- function handlePasteImage(e) {
- var items = (e.clipboardData || e.originalEvent.clipboardData).items;
- for (var i = 0; i < items.length; i++) {
- if (items[i].type.indexOf('image') !== -1) {
- e.preventDefault();
- var file = items[i].getAsFile();
- var reader = new FileReader();
- reader.onload = function(evt) {
- var dataUrl = evt.target.result;
- var parts = dataUrl.split(',');
- var meta = parts[0];
- pendingImageBase64 = parts[1];
- pendingImageMime = meta.match(/data:(.*?);/)[1];
- showImagePreview(dataUrl);
- };
- reader.readAsDataURL(file);
- return;
- }
- }
- }
- function showImagePreview(dataUrl) {
- if (inChatMode) {
- chatImgPreview.src = dataUrl;
- chatImgWrap.classList.add('has-img');
- } else {
- welcomeImgPreview.src = dataUrl;
- welcomeImgWrap.classList.add('has-img');
- }
- }
- function clearImagePreview() {
- pendingImageBase64 = null;
- pendingImageMime = null;
- welcomeImgPreview.src = '';
- welcomeImgWrap.classList.remove('has-img');
- chatImgPreview.src = '';
- chatImgWrap.classList.remove('has-img');
- }
- modelConfigBtn.addEventListener('click', openModelConfigModal);
- modelConfigCloseBtn.addEventListener('click', function () {
- toggleModelModal(false);
- });
- modelCancelBtn.addEventListener('click', function () {
- toggleModelModal(false);
- });
- modelSaveBtn.addEventListener('click', saveModelConfig);
- // modelConfigModal.addEventListener('click', function (e) {
- // if (e.target === modelConfigModal) {
- // toggleModelModal(false);
- // }
- // });
- document.addEventListener('keydown', function (e) {
- if (e.key === 'Escape' && modelConfigModal.classList.contains('active')) {
- toggleModelModal(false);
- }
- });
- welcomeInput.addEventListener('paste', handlePasteImage);
- chatInput.addEventListener('paste', handlePasteImage);
- welcomeImgRemove.addEventListener('click', clearImagePreview);
- chatImgRemove.addEventListener('click', clearImagePreview);
- /* ===== Quick Questions ===== */
- var welcomeQuickQs = document.getElementById('welcomeQuickQs');
- var chatQuickQs = document.getElementById('chatQuickQs');
- function loadQuickQuestions() {
- httpGetJson('/js/a/ai/qs', function (data) {
- var questions = unwrapResponse(data);
- if (!Array.isArray(questions) || questions.length === 0) return;
- renderQuickQChips(welcomeQuickQs, questions);
- renderQuickQChips(chatQuickQs, questions);
- });
- }
- function renderQuickQChips(container, questions) {
- container.innerHTML = '';
- for (var i = 0; i < questions.length; i++) {
- var chip = document.createElement('span');
- chip.className = 'quick-q-chip';
- chip.textContent = questions[i];
- chip.title = questions[i];
- chip.addEventListener('click', (function(q) {
- return function() {
- if (inChatMode) {
- chatInput.value = q;
- chatInput.focus();
- autoResize(chatInput);
- } else {
- welcomeInput.value = q;
- welcomeInput.focus();
- autoResize(welcomeInput);
- }
- };
- })(questions[i]));
- container.appendChild(chip);
- }
- }
- loadQuickQuestions();
- /* ===== Marked ===== */
- if (typeof marked !== 'undefined') { marked.setOptions({ breaks: true, gfm: true }); }
- function renderMd(text) {
- if (typeof marked !== 'undefined') return marked.parse(text);
- return text.replace(/</g,'<').replace(/>/g,'>').replace(/\n/g,'<br>');
- }
- /* ===== Theme ===== */
- var currentTheme = localStorage.getItem('chat-theme') || 'light';
- document.body.setAttribute('data-theme', currentTheme);
- /* updateThemeIcon();
- themeBtn.addEventListener('click', function() {
- currentTheme = currentTheme === 'light' ? 'dark' : 'light';
- document.body.setAttribute('data-theme', currentTheme);
- localStorage.setItem('chat-theme', currentTheme);
- updateThemeIcon();
- });
- function updateThemeIcon() {
- themeIcon.innerHTML = currentTheme === 'light' ? '' : '';
- themeBtn.title = currentTheme === 'light' ? '切换至暗色' : '切换至浅色';
- }*/
- /* ===== View Switch ===== */
- function switchToChatMode() {
- if (inChatMode) return;
- inChatMode = true;
- welcomeView.style.display = 'none';
- chatView.classList.add('active');
- chatInput.focus();
- }
- function switchToWelcomeMode() {
- inChatMode = false;
- deactivateSession();
- welcomeView.style.display = '';
- chatView.classList.remove('active');
- welcomeInput.focus();
- }
- /* ===== Auto-resize ===== */
- function autoResize(el) {
- el.style.height = 'auto';
- el.style.height = Math.min(el.scrollHeight, 140) + 'px';
- }
- welcomeInput.addEventListener('input', function() { autoResize(this); });
- chatInput.addEventListener('input', function() { autoResize(this); });
- /* ===== Send from both inputs ===== */
- function getInputText() {
- if (inChatMode) return chatInput.value.trim();
- return welcomeInput.value.trim();
- }
- function clearInput() {
- if (inChatMode) { chatInput.value = ''; chatInput.style.height = 'auto'; }
- else { welcomeInput.value = ''; welcomeInput.style.height = 'auto'; }
- }
- welcomeInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
- chatInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } });
- welcomeSendBtn.addEventListener('click', function() { sendMessage(); });
- chatSendBtn.addEventListener('click', function() {
- if (isStreaming && activeSessionId && sessionMap[activeSessionId]) {
- finishStream(sessionMap[activeSessionId]);
- } else {
- sendMessage();
- }
- });
- /* ===== Click to focus ===== */
- document.querySelector('.welcome-input-box').addEventListener('click', function(e) {
- if (!e.target.closest('button')) welcomeInput.focus();
- });
- document.querySelector('.input-box').addEventListener('click', function(e) {
- if (!e.target.closest('button')) chatInput.focus();
- });
- /* ===== New Chat ===== */
- newChatBtn.addEventListener('click', function() {
- SESSION_ID = 'web-' + Date.now().toString(36);
- currentChatIndex = -1;
- switchToWelcomeMode();
- updateHistoryUI();
- });
- /* ===== Helpers ===== */
- messagesWrap.addEventListener('scroll', function() {
- var gap = messagesWrap.scrollHeight - messagesWrap.scrollTop - messagesWrap.clientHeight;
- userScrolledUp = gap > 80;
- });
- var scrollRafPending = false;
- function scrollToBottom(force) {
- if (!force && userScrolledUp) return;
- if (force) userScrolledUp = false;
- if (scrollRafPending) return;
- scrollRafPending = true;
- requestAnimationFrame(function() {
- scrollRafPending = false;
- messagesWrap.scrollTop = messagesWrap.scrollHeight;
- });
- }
- function resetStreamState(sess) {
- sess.currentBubbleEl = null;
- sess.reasonBuffer = '';
- sess.thinkingBlockEl = null;
- sess.thinkingBodyMdEl = null;
- sess.thinkingBodyWrapEl = null;
- sess.thinkingBuffer = '';
- if (sess.contentRafId) { cancelAnimationFrame(sess.contentRafId); sess.contentRafId = null; }
- if (sess.reasonRafId) { cancelAnimationFrame(sess.reasonRafId); sess.reasonRafId = null; }
- }
- function setBtnStopMode() {
- chatSendBtn.disabled = false;
- chatSendBtn.classList.add('stop-mode');
- chatSendBtn.innerHTML = '<div class="stop-icon"></div>';
- chatSendBtn.title = '停止生成';
- }
- function setBtnSendMode() {
- chatSendBtn.classList.remove('stop-mode');
- chatSendBtn.innerHTML = '<i class="layui-icon layui-icon-release"></i>';
- chatSendBtn.title = '发送';
- chatSendBtn.disabled = false;
- }
- /* ===== History ===== */
- function loadSessionHistory() {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', '/js/a/chat/sessions', true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4 && xhr.status === 200) {
- try {
- var result = JSON.parse(xhr.responseText);
- var list = result.data;
- chatHistory = [];
- for (var i = 0; i < list.length; i++) {
- chatHistory.push({ label: list[i].label, sessionId: list[i].sessionId });
- }
- updateHistoryUI();
- } catch (e) {}
- }
- };
- xhr.send();
- }
- function saveChatToHistory(firstMsg) {
- var label = firstMsg.length > 30 ? firstMsg.substring(0, 30) + '...' : firstMsg;
- for (var i = 0; i < chatHistory.length; i++) {
- if (chatHistory[i].sessionId === SESSION_ID) {
- currentChatIndex = i;
- updateHistoryUI();
- return;
- }
- }
- chatHistory.unshift({ label: label, sessionId: SESSION_ID });
- if (chatHistory.length > 50) chatHistory.pop();
- currentChatIndex = 0;
- updateHistoryUI();
- }
- /* Sidebar event delegation — single listener instead of per-item binding */
- historyList.addEventListener('click', function(e) {
- var delBtn = e.target.closest('.sidebar-item-del');
- if (delBtn) {
- e.stopPropagation();
- var idx = parseInt(delBtn.closest('.sidebar-item').getAttribute('data-idx'));
- if (!isNaN(idx)) deleteSession(idx);
- return;
- }
- var item = e.target.closest('.sidebar-item');
- if (item) {
- var idx = parseInt(item.getAttribute('data-idx'));
- if (!isNaN(idx)) selectSession(idx);
- }
- });
- function updateHistoryUI() {
- var frag = document.createDocumentFragment();
- for (var i = 0; i < chatHistory.length; i++) {
- var item = document.createElement('div');
- var sess = sessionMap[chatHistory[i].sessionId];
- var streaming = sess && sess.isStreaming;
- item.className = 'sidebar-item' + (i === currentChatIndex ? ' active' : '') + (streaming ? ' streaming' : '');
- item.setAttribute('data-idx', i);
- var label = document.createElement('span');
- label.className = 'sidebar-item-label';
- label.textContent = chatHistory[i].label;
- var delBtn = document.createElement('button');
- delBtn.className = 'sidebar-item-del';
- delBtn.title = '删除对话';
- delBtn.innerHTML = '<i class="layui-icon layui-icon-close"></i>';
- item.appendChild(label);
- if (streaming) {
- var spinner = document.createElement('span');
- spinner.className = 'sidebar-item-spinner';
- spinner.title = '对话进行中...';
- item.appendChild(spinner);
- }
- item.appendChild(delBtn);
- frag.appendChild(item);
- }
- historyList.innerHTML = '';
- historyList.appendChild(frag);
- }
- function deleteSession(idx) {
- var entry = chatHistory[idx];
- if (!entry) return;
- var xhr = new XMLHttpRequest();
- xhr.open('POST', '/js/a/chat/sessions/delete?sessionId=' + encodeURIComponent(entry.sessionId), true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4) {
- /* Clean up session state after server confirms */
- var sess = sessionMap[entry.sessionId];
- if (sess) {
- if (sess.eventSource) sess.eventSource.close();
- if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
- if (sess.contentRafId) cancelAnimationFrame(sess.contentRafId);
- if (sess.reasonRafId) cancelAnimationFrame(sess.reasonRafId);
- if (sess.container.parentNode) sess.container.parentNode.removeChild(sess.container);
- delete sessionMap[entry.sessionId];
- }
- chatHistory.splice(idx, 1);
- if (idx === currentChatIndex) {
- currentChatIndex = -1;
- SESSION_ID = 'web-' + Date.now().toString(36);
- switchToWelcomeMode();
- } else if (idx < currentChatIndex) {
- currentChatIndex--;
- }
- updateHistoryUI();
- }
- };
- xhr.send();
- }
- function selectSession(idx) {
- if (idx === currentChatIndex && inChatMode) return;
- var entry = chatHistory[idx];
- if (!entry) return;
- currentChatIndex = idx;
- SESSION_ID = entry.sessionId;
- if (!inChatMode) switchToChatMode();
- setActiveSession(entry.sessionId);
- updateHistoryUI();
- var sess = sessionMap[entry.sessionId];
- /* Only load from server if not streaming and container has no content */
- if (!sess.isStreaming && sess.container.children.length === 0) {
- loadMessages(sess);
- } else {
- scrollToBottom(true);
- }
- }
- function loadMessages(sess) {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', '/js/a/chat/messages?sessionId=' + encodeURIComponent(sess.sessionId), true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4 && xhr.status === 200) {
- try {
- var result = JSON.parse(xhr.responseText);
- var msgs = result.data;
- sess.container.innerHTML = '';
- resetStreamState(sess);
- for (var i = 0; i < msgs.length; i++) {
- var m = msgs[i];
- if (m.role === 'USER') {
- resetStreamState(sess);
- appendUserMessage(sess, m.content);
- } else if (m.role === 'ASSISTANT') {
- var isConsecutive = (i > 0 && msgs[i - 1].role === 'ASSISTANT');
- if (!isConsecutive) resetStreamState(sess);
- var el = ensureAssistantBubble(sess);
- sess.reasonBuffer = isConsecutive ? sess.reasonBuffer + '\n\n' + m.content : m.content;
- el.innerHTML = renderMd(sess.reasonBuffer);
- }
- }
- resetStreamState(sess);
- if (sess.sessionId === activeSessionId) scrollToBottom(true);
- } catch (e) {}
- }
- };
- xhr.send();
- }
- /* Load on startup */
- loadSessionHistory();
- /* ===== Message Rendering (Session-Aware) ===== */
- function appendUserMessage(sess, text, imageDataUrl) {
- var row = document.createElement('div');
- row.className = 'msg-row user';
- row.innerHTML = '<div class="msg-bubble"></div><div class="msg-avatar">我</div>';
- var bubble = row.querySelector('.msg-bubble');
- if (imageDataUrl) {
- var img = document.createElement('img');
- img.src = imageDataUrl;
- img.style.cssText = 'max-height:120px;max-width:240px;border-radius:8px;display:block;margin-bottom:8px;';
- bubble.appendChild(img);
- }
- var span = document.createElement('span');
- span.textContent = text;
- bubble.appendChild(span);
- sess.container.appendChild(row);
- if (sess.sessionId === activeSessionId) scrollToBottom(true);
- }
- function ensureAssistantBubble(sess) {
- if (!sess.currentBubbleEl) {
- removeThinking(sess);
- var row = document.createElement('div');
- row.className = 'msg-row assistant';
- row.innerHTML = '<div class="msg-avatar"><i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
- + '<div class="msg-bubble"><div class="md-content"></div>'
- + '<div class="msg-actions">'
- + '<button class="msg-action-btn copy-btn" title="复制"><i class="layui-icon layui-icon-file"></i> 复制</button>'
- + '</div></div>';
- sess.container.appendChild(row);
- sess.currentBubbleEl = row.querySelector('.md-content');
- var copyBtn = row.querySelector('.copy-btn');
- var mdRef = sess.currentBubbleEl;
- copyBtn.addEventListener('click', function() {
- var txt = mdRef.innerText || '';
- if (navigator.clipboard) { navigator.clipboard.writeText(txt); }
- });
- }
- return sess.currentBubbleEl;
- }
- function ensureThinkingBlock(sess) {
- if (!sess.thinkingBlockEl) {
- ensureAssistantBubble(sess);
- var parent = sess.currentBubbleEl.parentNode;
- var block = document.createElement('div');
- block.className = 'thinking-block streaming expanded';
- block.innerHTML = '<div class="thinking-block-header">'
- + '<span class="thinking-block-label">思考中...</span>'
- + '<span class="thinking-block-dots"><span></span><span></span><span></span></span>'
- + '<i class="layui-icon layui-icon-right thinking-block-toggle"></i>'
- + '</div>'
- + '<div class="thinking-block-body"><div class="md-content"></div></div>';
- parent.insertBefore(block, sess.currentBubbleEl);
- block.querySelector('.thinking-block-header').addEventListener('click', function() {
- block.classList.toggle('expanded');
- });
- sess.thinkingBlockEl = block;
- sess.thinkingBodyMdEl = block.querySelector('.thinking-block-body .md-content');
- sess.thinkingBodyWrapEl = block.querySelector('.thinking-block-body');
- sess.thinkingBuffer = '';
- }
- return sess.thinkingBlockEl;
- }
- function insertBeforeActions(sess, el) {
- var parent = sess.currentBubbleEl.parentNode;
- parent.insertBefore(el, parent.querySelector('.msg-actions'));
- }
- function finishThinkingBlock(sess) {
- if (sess.thinkingBlockEl) {
- if (sess.reasonRafId) {
- cancelAnimationFrame(sess.reasonRafId);
- sess.reasonRafId = null;
- if (sess.thinkingBodyMdEl) {
- sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
- }
- }
- sess.thinkingBlockEl.classList.remove('streaming', 'expanded');
- var label = sess.thinkingBlockEl.querySelector('.thinking-block-label');
- if (label) label.textContent = '思考结束';
- var dots = sess.thinkingBlockEl.querySelector('.thinking-block-dots');
- if (dots) dots.remove();
- sess.thinkingBlockEl = null;
- sess.thinkingBodyMdEl = null;
- sess.thinkingBodyWrapEl = null;
- sess.thinkingBuffer = '';
- }
- }
- function clearThinkTags(text) {
- return text.replace(/<\s*\/?think\s*>/gi, '');
- }
- function appendReasonChunk(sess, text) {
- removeThinking(sess);
- ensureThinkingBlock(sess);
- sess.thinkingBuffer += clearThinkTags(text);
- if (!sess.reasonRafId) {
- sess.reasonRafId = requestAnimationFrame(function() {
- sess.reasonRafId = null;
- if (!sess.thinkingBlockEl) return;
- if (sess.thinkingBodyMdEl) {
- sess.thinkingBodyMdEl.innerHTML = renderMd(sess.thinkingBuffer);
- }
- if (sess.thinkingBodyWrapEl) {
- sess.thinkingBodyWrapEl.scrollTop = sess.thinkingBodyWrapEl.scrollHeight;
- }
- if (sess.sessionId === activeSessionId) scrollToBottom();
- });
- }
- }
- function finishPendingTool(sess) {
- if (sess.pendingToolCard) {
- var icon = sess.pendingToolCard.querySelector('.tool-status-icon');
- if (icon) { icon.className = 'tool-status-icon done'; icon.innerHTML = '<i class="layui-icon layui-icon-ok" style="font-size:12px"></i>'; }
- var label = sess.pendingToolCard.querySelector('.tool-label');
- if (label) label.textContent = '已完成';
- sess.pendingToolCard = null;
- }
- }
- function appendActionEndChunk(sess, toolName, text) {
- finishPendingTool(sess);
- ensureAssistantBubble(sess);
- var card = document.createElement('div');
- card.className = 'tool-card';
- card.innerHTML = '<div class="tool-card-header">'
- + '<span class="tool-status-icon loading"></span>'
- + '<span class="tool-name">' + escapeHtml(toolName || 'tool') + '</span>'
- + '<span class="tool-label">执行中...</span>'
- + '<i class="layui-icon layui-icon-right tool-toggle"></i>'
- + '</div>'
- + '<div class="tool-card-body"></div>';
- card.querySelector('.tool-card-body').textContent = text || '';
- card.querySelector('.tool-card-header').addEventListener('click', function() {
- card.classList.toggle('expanded');
- });
- insertBeforeActions(sess, card);
- sess.pendingToolCard = card;
- sess.reasonBuffer = '';
- var newMd = document.createElement('div');
- newMd.className = 'md-content';
- insertBeforeActions(sess, newMd);
- sess.currentBubbleEl = newMd;
- if (sess.sessionId === activeSessionId) scrollToBottom();
- }
- function appendContentChunk(sess, text, append) {
- var clean = clearThinkTags(text);
- sess.reasonBuffer = append ? sess.reasonBuffer + clean : clean;
- if (!sess.contentRafId) {
- sess.contentRafId = requestAnimationFrame(function() {
- sess.contentRafId = null;
- var el = ensureAssistantBubble(sess);
- el.innerHTML = renderMd(sess.reasonBuffer);
- if (sess.sessionId === activeSessionId) scrollToBottom();
- });
- }
- }
- function appendErrorChunk(sess, text) {
- ensureAssistantBubble(sess);
- var errEl = document.createElement('div');
- errEl.className = 'chunk-error';
- errEl.textContent = text;
- insertBeforeActions(sess, errEl);
- if (sess.sessionId === activeSessionId) scrollToBottom();
- }
- /* ===== Thinking Indicators ===== */
- function showThinking(sess) {
- removeThinking(sess);
- sess.thinkingEl = document.createElement('div');
- sess.thinkingEl.className = 'thinking-row';
- sess.thinkingEl.innerHTML = '<div class="msg-avatar" style="background:linear-gradient(135deg,var(--accent),#a78bfa);color:#fff">'
- + '<i class="layui-icon layui-icon-bot" style="font-size:18px"></i></div>'
- + '<div class="thinking-bubble">思考中' + DOTS_HTML + '</div>';
- sess.container.appendChild(sess.thinkingEl);
- if (sess.sessionId === activeSessionId) scrollToBottom(true);
- }
- function removeThinking(sess) {
- if (sess.thinkingEl && sess.thinkingEl.parentNode) { sess.thinkingEl.parentNode.removeChild(sess.thinkingEl); sess.thinkingEl = null; }
- }
- function showInlineThinking(sess) {
- if (sess.inlineThinkingEl || !sess.currentBubbleEl) return;
- sess.inlineThinkingEl = document.createElement('div');
- sess.inlineThinkingEl.className = 'inline-thinking';
- sess.inlineThinkingEl.innerHTML = '思考中 ' + DOTS_HTML;
- insertBeforeActions(sess, sess.inlineThinkingEl);
- if (sess.sessionId === activeSessionId) scrollToBottom();
- }
- function removeInlineThinking(sess) {
- if (sess.inlineThinkingEl && sess.inlineThinkingEl.parentNode) { sess.inlineThinkingEl.parentNode.removeChild(sess.inlineThinkingEl); }
- sess.inlineThinkingEl = null;
- }
- /* ===== HITL ===== */
- function appendHitlCard(sess, toolName, command) {
- ensureAssistantBubble(sess);
- var card = document.createElement('div');
- card.className = 'hitl-card';
- card.innerHTML = '<div class="hitl-card-header">'
- + '<i class="layui-icon layui-icon-tips"></i> \u9700\u8981\u6388\u6743'
- + '</div>'
- + '<div class="hitl-card-body">'
- + '<div class="hitl-tool">\u5de5\u5177: <strong>' + escapeHtml(toolName || 'unknown') + '</strong></div>'
- + (command ? '<div class="hitl-command">' + escapeHtml(command) + '</div>' : '')
- + '</div>'
- + '<div class="hitl-card-actions">'
- + '<button class="hitl-btn hitl-btn-approve">\u6279\u51c6</button>'
- + '<button class="hitl-btn hitl-btn-reject">\u62d2\u7edd</button>'
- + '</div>';
- insertBeforeActions(sess, card);
- var approveBtn = card.querySelector('.hitl-btn-approve');
- var rejectBtn = card.querySelector('.hitl-btn-reject');
- approveBtn.addEventListener('click', function() {
- approveBtn.disabled = true;
- rejectBtn.disabled = true;
- approveBtn.textContent = '\u5df2\u6279\u51c6';
- rejectBtn.style.display = 'none';
- card.style.borderColor = '#22c55e';
- handleHitlResponse(sess, 'approve');
- });
- rejectBtn.addEventListener('click', function() {
- approveBtn.disabled = true;
- rejectBtn.disabled = true;
- rejectBtn.textContent = '\u5df2\u62d2\u7edd';
- approveBtn.style.display = 'none';
- card.style.borderColor = '#ef4444';
- handleHitlResponse(sess, 'reject');
- });
- if (sess.sessionId === activeSessionId) scrollToBottom();
- }
- function escapeHtml(str) {
- var div = document.createElement('div');
- div.textContent = str;
- return div.innerHTML;
- }
- function handleHitlResponse(sess, action) {
- if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
- resetStreamState(sess);
- sess.isStreaming = true;
- if (sess.sessionId === activeSessionId) {
- isStreaming = true;
- setBtnStopMode();
- }
- var url = SSE_ENDPOINT + '?hitlAction=' + encodeURIComponent(action)
- + '&sessionId=' + encodeURIComponent(sess.sessionId);
- sess.eventSource = new EventSource(url);
- sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
- sess.eventSource.onerror = function() { finishStream(sess); };
- }
- /* ===== Send ===== */
- function sendMessage() {
- var text = getInputText();
- if (!text && !pendingImageBase64) return;
- /* Block only if the active session is currently streaming */
- if (activeSessionId && sessionMap[activeSessionId] && sessionMap[activeSessionId].isStreaming) return;
- if (!text) text = '请描述这张图片';
- var imageDataUrl = pendingImageBase64 ? ('data:' + pendingImageMime + ';base64,' + pendingImageBase64) : null;
- var imageBase64 = pendingImageBase64;
- var imageMime = pendingImageMime;
- if (currentChatIndex === -1) {
- SESSION_ID = 'web-' + Date.now().toString(36);
- saveChatToHistory(text);
- }
- clearInput();
- clearImagePreview();
- if (!inChatMode) switchToChatMode();
- setActiveSession(SESSION_ID);
- var sess = sessionMap[SESSION_ID];
- appendUserMessage(sess, text, imageDataUrl);
- sess.isStreaming = true;
- isStreaming = true;
- setBtnStopMode();
- resetStreamState(sess);
- showThinking(sess);
- if (imageBase64) {
- sendWithFetch(sess, text, imageBase64, imageMime);
- } else {
- sendWithEventSource(sess, text);
- }
- }
- function sendWithEventSource(sess, text) {
- if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
- var url = SSE_ENDPOINT + '?input=' + encodeURIComponent(text)
- + '&sessionId=' + encodeURIComponent(sess.sessionId);
- sess.eventSource = new EventSource(url);
- sess.eventSource.onmessage = function(event) { handleSSEData(sess, event.data); };
- sess.eventSource.onerror = function() { finishStream(sess); };
- }
- function sendWithFetch(sess, text, imageBase64, imageMime) {
- if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
- var body = JSON.stringify({ input: text, sessionId: sess.sessionId, imageBase64: imageBase64, imageMime: imageMime });
- var abortCtrl = new AbortController();
- sess.eventSource = { close: function() { abortCtrl.abort(); } };
- fetch(SSE_ENDPOINT, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- signal: abortCtrl.signal,
- body: body
- }).then(function(resp) {
- var reader = resp.body.getReader();
- var decoder = new TextDecoder();
- var buffer = '';
- function pump() {
- reader.read().then(function(result) {
- if (result.done) { finishStream(sess); return; }
- buffer += decoder.decode(result.value, { stream: true });
- var lines = buffer.split('\n');
- buffer = lines.pop();
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i].trim();
- if (line.startsWith('data:')) {
- var raw = line.substring(5).trim();
- handleSSEData(sess, raw);
- }
- }
- pump();
- }).catch(function() { finishStream(sess); });
- }
- pump();
- }).catch(function() { finishStream(sess); });
- }
- /* ===== SSE Data Handling (Session-Aware) ===== */
- function handleSSEData(sess, raw) {
- if (raw === '[DONE]') { finishStream(sess); return; }
- try {
- var chunk = JSON.parse(raw);
- if (sess.silenceTimer) clearTimeout(sess.silenceTimer);
- removeInlineThinking(sess);
- switch (chunk.type) {
- case 'reason': finishPendingTool(sess); appendReasonChunk(sess, chunk.text); break;
- case 'text': finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, true); break;
- case 'action': finishThinkingBlock(sess); appendActionEndChunk(sess, chunk.toolName, chunk.text); break;
- case 'agent': finishThinkingBlock(sess); finishPendingTool(sess); appendContentChunk(sess, chunk.text, false); break;
- case 'error': finishThinkingBlock(sess); appendErrorChunk(sess, chunk.text); break;
- case 'hitl': finishThinkingBlock(sess); finishPendingTool(sess); appendHitlCard(sess, chunk.toolName, chunk.command); break;
- }
- sess.silenceTimer = setTimeout(function() {
- if (sess.isStreaming && !sess.thinkingBlockEl) showInlineThinking(sess);
- }, 1000);
- } catch (e) {
- console.error('SSE数据解析失败:', raw, e);
- }
- }
- function finishStream(sess) {
- sess.isStreaming = false;
- if (sess.silenceTimer) { clearTimeout(sess.silenceTimer); sess.silenceTimer = null; }
- removeThinking(sess);
- removeInlineThinking(sess);
- finishThinkingBlock(sess);
- finishPendingTool(sess);
- if (sess.eventSource) { sess.eventSource.close(); sess.eventSource = null; }
- resetStreamState(sess);
- if (sess.sessionId === activeSessionId) {
- isStreaming = false;
- setBtnSendMode();
- chatInput.focus();
- }
- loadSessionHistory();
- }
- </script>
- </body>
- </html>
|