|
@@ -1,341 +0,0 @@
|
|
|
-<!DOCTYPE html>
|
|
|
|
|
-<html lang="zh-CN">
|
|
|
|
|
-<head>
|
|
|
|
|
-<meta charset="UTF-8">
|
|
|
|
|
-<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
|
|
|
-<title>AI 药典助手</title>
|
|
|
|
|
-<style>
|
|
|
|
|
-:root{--bk:#002FA7;--bk-l:#E8EDF8;--rd:#C41E3A;--rd-l:#FDEAEE;--bg:#F5F6FA;--card:#fff;--text:#1a1a2e;--sub:#888;--radius:12px}
|
|
|
|
|
-*{margin:0;padding:0;box-sizing:border-box}
|
|
|
|
|
-html,body{height:100%;overflow:hidden}
|
|
|
|
|
-body{font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei",sans-serif;background:var(--bg);display:flex;flex-direction:column}
|
|
|
|
|
-.header{background:linear-gradient(135deg,var(--bk),#1a3fbf);color:#fff;padding:12px 14px;text-align:center;flex-shrink:0;position:relative}
|
|
|
|
|
-.header h1{font-size:17px;font-weight:700}
|
|
|
|
|
-.header .sub{font-size:11px;opacity:.85;margin-top:2px}
|
|
|
|
|
-.menu-btn{position:absolute;left:10px;top:50%;transform:translateY(-50%);background:rgba(255,255,255,.15);border:none;color:#fff;font-size:22px;cursor:pointer;z-index:1;padding:8px 14px;border-radius:8px;min-width:44px;min-height:44px;display:flex;align-items:center;justify-content:center}
|
|
|
|
|
-.nav{display:flex;background:#fff;border-bottom:1px solid #e0e0e0;flex-shrink:0}
|
|
|
|
|
-.nav-btn{flex:1;padding:12px;text-align:center;font-size:13px;color:var(--sub);cursor:pointer;border:none;background:none;border-bottom:2px solid transparent;min-height:44px}
|
|
|
|
|
-.nav-btn.active{color:var(--bk);border-bottom-color:var(--bk);font-weight:600}
|
|
|
|
|
-.tab-content{flex:1;display:flex;flex-direction:column;overflow:hidden;min-height:0}
|
|
|
|
|
-.chat{flex:1;overflow-y:auto;padding:12px 14px;display:flex;flex-direction:column;gap:12px}
|
|
|
|
|
-.msg{max-width:85%;padding:10px 14px;border-radius:var(--radius);font-size:14px;line-height:1.7;word-break:break-word;animation:fadeIn .3s;position:relative}
|
|
|
|
|
-.msg.user{align-self:flex-end;background:var(--bk);color:#fff;border-radius:var(--radius) var(--radius) 4px var(--radius)}
|
|
|
|
|
-.msg.ai{align-self:flex-start;background:var(--card);color:var(--text);border-radius:var(--radius) var(--radius) var(--radius) 4px;box-shadow:0 1px 4px rgba(0,0,0,.06);border-left:3px solid var(--bk)}
|
|
|
|
|
-.msg.ai .tag{display:inline-block;font-size:11px;padding:2px 8px;border-radius:10px;margin-bottom:6px;font-weight:600}
|
|
|
|
|
-.tag-drug{background:var(--bk-l);color:var(--bk)}.tag-usage{background:var(--rd-l);color:var(--rd)}.tag-symptom{background:#fff3e0;color:#e65100}
|
|
|
|
|
-.tag-regulation{background:#f3e5f5;color:#6a1b9a}.tag-exam{background:#e0f2f1;color:#00695c}.tag-fallback{background:#f5f5f5;color:#999}
|
|
|
|
|
-.content p{margin:0 0 12px 0;line-height:1.8;word-break:break-word}.content p:last-child{margin-bottom:0}
|
|
|
|
|
-.content strong{color:#111}.content ul,.content ol{margin:8px 0;padding-left:18px;line-height:1.8}.content li{margin:4px 0}
|
|
|
|
|
-.content h3{font-size:15px;margin:16px 0 8px;color:var(--bk)}.content .warn{color:var(--rd);font-weight:600}
|
|
|
|
|
-.think-dot{display:inline-block;width:8px;height:8px;border-radius:50%;background:var(--bk);animation:thinkPulse 1s infinite;vertical-align:middle;margin-right:6px}
|
|
|
|
|
-@keyframes thinkPulse{0%,100%{opacity:1;transform:scale(1)}50%{opacity:.4;transform:scale(.7)}}
|
|
|
|
|
-.thinking{color:var(--sub);font-size:13px;padding:4px 0}.cursor{animation:blink .8s infinite;color:var(--bk);font-weight:700}
|
|
|
|
|
-@keyframes fadeIn{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:translateY(0)}}
|
|
|
|
|
-@keyframes blink{0%,100%{opacity:1}50%{opacity:0}}
|
|
|
|
|
-.source{margin-top:10px;padding-top:8px;border-top:1px solid #eee;font-size:11px;color:#aaa;line-height:1.6}.source span{color:#888;font-weight:500}
|
|
|
|
|
-.feedback{margin-top:6px;display:flex;gap:6px}
|
|
|
|
|
-.fb-btn{padding:3px 10px;border:1px solid #ddd;border-radius:12px;font-size:11px;cursor:pointer;background:#fff;color:var(--sub)}
|
|
|
|
|
-.fb-btn.active-like{background:#e8f5e9;color:#2e7d32;border-color:#2e7d32}
|
|
|
|
|
-.fb-btn.active-dislike{background:#fde7e7;color:var(--rd);border-color:var(--rd)}
|
|
|
|
|
-.input-area{display:flex;padding:10px 14px;background:#fff;border-top:1px solid #e0e0e0;gap:8px;flex-shrink:0}
|
|
|
|
|
-.input-area input{flex:1;padding:10px 14px;border:1px solid #ddd;border-radius:24px;font-size:14px;outline:none;min-height:44px}
|
|
|
|
|
-.input-area input:focus{border-color:var(--bk);box-shadow:0 0 0 2px rgba(0,47,167,.12)}
|
|
|
|
|
-.btn{padding:8px 18px;border:none;border-radius:24px;font-size:14px;font-weight:700;cursor:pointer;min-height:44px;white-space:nowrap}
|
|
|
|
|
-.btn-danger{background:var(--rd);color:#fff}.btn-secondary{background:#999;color:#fff}.btn:disabled{opacity:.4;cursor:default}
|
|
|
|
|
-.tags{display:flex;gap:6px;padding:6px 14px 10px;flex-wrap:wrap;flex-shrink:0}
|
|
|
|
|
-.tag-btn{font-size:12px;padding:5px 12px;border-radius:16px;border:1px solid rgba(0,47,167,.2);background:var(--bk-l);color:var(--bk);cursor:pointer;white-space:nowrap;min-height:36px;display:flex;align-items:center}
|
|
|
|
|
-.tag-btn:active{background:rgba(0,47,167,.15)}
|
|
|
|
|
-.welcome{text-align:center;color:var(--sub);padding:40px 20px}.welcome .icon{font-size:48px;margin-bottom:12px}
|
|
|
|
|
-/* queue */
|
|
|
|
|
-.queue{background:#fff;border-top:1px solid #eee;padding:6px 14px;flex-shrink:0;max-height:130px;overflow-y:auto}
|
|
|
|
|
-.queue-title{font-size:12px;color:var(--sub);margin-bottom:4px}
|
|
|
|
|
-.queue-item{display:flex;align-items:center;justify-content:space-between;padding:4px 8px;font-size:12px;color:var(--text);background:var(--bg);border-radius:6px;margin-bottom:3px}
|
|
|
|
|
-.queue-item .cancel-btn{color:var(--rd);cursor:pointer;font-weight:700;padding:2px 8px}
|
|
|
|
|
-.queue-item .running{color:var(--bk);font-weight:600}
|
|
|
|
|
-.queue-item .waiting{color:var(--sub)}
|
|
|
|
|
-/* sidebar */
|
|
|
|
|
-.sidebar-overlay{position:fixed;inset:0;background:rgba(0,0,0,.35);z-index:100;display:none}
|
|
|
|
|
-.sidebar-overlay.show{display:block}
|
|
|
|
|
-.sidebar{position:fixed;top:0;left:0;bottom:0;width:300px;max-width:85vw;background:#fff;z-index:101;display:flex;flex-direction:column;transform:translateX(-100%);transition:transform .12s ease-out;box-shadow:2px 0 12px rgba(0,0,0,.1)}
|
|
|
|
|
-.sidebar.show{transform:translateX(0)}
|
|
|
|
|
-.sidebar-header{padding:16px;background:var(--bk);color:#fff;display:flex;justify-content:space-between;align-items:center}
|
|
|
|
|
-.sidebar-header h2{font-size:16px}
|
|
|
|
|
-.sidebar-close{background:rgba(255,255,255,.2);border:none;color:#fff;font-size:20px;cursor:pointer;width:40px;height:40px;border-radius:8px;display:flex;align-items:center;justify-content:center}
|
|
|
|
|
-.sidebar-new{margin:12px;padding:14px;background:var(--bk);color:#fff;border:none;border-radius:8px;font-size:15px;cursor:pointer;width:calc(100% - 24px);min-height:44px}
|
|
|
|
|
-.sidebar-list{flex:1;overflow-y:auto;padding:0 10px}
|
|
|
|
|
-.sidebar-item{padding:14px;border-radius:8px;cursor:pointer;margin-bottom:2px;min-height:44px;display:flex;flex-direction:column;justify-content:center}
|
|
|
|
|
-.sidebar-item:active{background:var(--bk-l)}.sidebar-item.active{background:var(--bk-l);border-left:3px solid var(--bk)}
|
|
|
|
|
-.sidebar-item .s-title{font-size:13px;font-weight:500;color:var(--text);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
|
|
|
-.sidebar-item .s-meta{font-size:11px;color:var(--sub);margin-top:3px}
|
|
|
|
|
-/* drug page */
|
|
|
|
|
-.drug-list{flex:1;overflow-y:auto;padding:12px 14px}
|
|
|
|
|
-.drug-card{background:var(--card);border-radius:var(--radius);padding:14px;margin-bottom:10px;box-shadow:0 1px 4px rgba(0,0,0,.06);cursor:pointer}
|
|
|
|
|
-.drug-card h3{font-size:15px;color:var(--bk);margin-bottom:4px}.drug-card .meta{font-size:12px;color:var(--sub)}
|
|
|
|
|
-.drug-detail{padding:16px 14px;flex:1;overflow-y:auto}
|
|
|
|
|
-.drug-detail h2{font-size:20px;color:var(--bk);margin-bottom:8px}
|
|
|
|
|
-.drug-detail h3{font-size:15px;color:var(--bk);margin:16px 0 8px}
|
|
|
|
|
-.drug-detail .info-row{font-size:13px;color:var(--sub);margin:4px 0}
|
|
|
|
|
-.drug-detail .section{background:var(--card);border-radius:8px;padding:12px;margin:8px 0;font-size:14px;line-height:1.8}
|
|
|
|
|
-.drug-search-bar{display:flex;gap:8px;padding:10px 14px;background:#fff;border-bottom:1px solid #e0e0e0;flex-shrink:0}
|
|
|
|
|
-.drug-search-bar input{flex:1;padding:10px 14px;border:1px solid #ddd;border-radius:24px;font-size:14px;outline:none;min-height:44px}
|
|
|
|
|
-.drug-search-bar input:focus{border-color:var(--bk)}
|
|
|
|
|
-.status-bar{font-size:11px;padding:4px 14px;text-align:center;flex-shrink:0}
|
|
|
|
|
-.status-ok{color:#2e7d32}.status-warn{color:#e65100}.status-err{color:var(--rd)}
|
|
|
|
|
-.hidden{display:none!important}
|
|
|
|
|
-.pagination{display:flex;justify-content:center;gap:8px;padding:12px}
|
|
|
|
|
-.pagination button{padding:6px 16px;border:1px solid #ddd;border-radius:20px;background:#fff;cursor:pointer;font-size:13px;min-height:36px}
|
|
|
|
|
-.pagination button:disabled{opacity:.3}
|
|
|
|
|
-</style>
|
|
|
|
|
-</head>
|
|
|
|
|
-<body>
|
|
|
|
|
-<div class="header">
|
|
|
|
|
- <button class="menu-btn" onclick="toggleSidebar()">☰</button>
|
|
|
|
|
- <h1>AI 药典助手</h1>
|
|
|
|
|
- <div class="sub">基于《中国药典》2025版 + 通义千问 RAG</div>
|
|
|
|
|
-</div>
|
|
|
|
|
-<div class="nav">
|
|
|
|
|
- <button class="nav-btn active" data-tab="chat" onclick="switchTab('chat')">AI 对话</button>
|
|
|
|
|
- <button class="nav-btn" data-tab="drugs" onclick="switchTab('drugs')">药品库</button>
|
|
|
|
|
-</div>
|
|
|
|
|
-<div class="status-bar" id="statusBar">● 连接中...</div>
|
|
|
|
|
-
|
|
|
|
|
-<div id="tabChat" class="tab-content">
|
|
|
|
|
- <div class="chat" id="chat"><div class="welcome"><div class="icon"></div><p>输入药品名称或用药问题<br><small style="color:#aaa" id="statsInfo">加载中...</small></p></div></div>
|
|
|
|
|
- <div class="tags"><span class="tag-btn" onclick="quickAsk('阿莫西林禁忌')">阿莫西林禁忌</span><span class="tag-btn" onclick="quickAsk('布洛芬用法用量')">布洛芬用法用量</span><span class="tag-btn" onclick="quickAsk('二甲双胍不良反应')">二甲双胍不良反应</span><span class="tag-btn" onclick="quickAsk('青霉素过敏处理')">青霉素过敏处理</span><span class="tag-btn" onclick="quickAsk('吗啡药理作用')">吗啡药理作用</span><span class="tag-btn" onclick="quickAsk('疫苗保存条件')">疫苗保存条件</span></div>
|
|
|
|
|
- <div class="queue" id="queue" style="display:none"><div id="queueItems"></div></div>
|
|
|
|
|
- <div class="input-area"><input id="userInput" placeholder="输入药品问题..." onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();send()}"><button class="btn btn-danger" id="sendBtn" onclick="send()">发送</button><button class="btn btn-secondary" id="stopBtn" onclick="stopAll()" style="display:none">⏹ 停止</button></div>
|
|
|
|
|
-</div>
|
|
|
|
|
-
|
|
|
|
|
-<div id="tabDrugs" class="tab-content hidden">
|
|
|
|
|
- <div class="drug-search-bar"><input id="drugSearch" placeholder="搜索药品名称..." onkeydown="if(event.key==='Enter')searchDrugs(1)"><button class="btn btn-danger" onclick="searchDrugs(1)">搜索</button></div>
|
|
|
|
|
- <div class="drug-list" id="drugList"><div class="welcome"><div class="icon"></div><p>搜索药典收录的 <span id="drugCount">--</span> 种药品</p></div></div>
|
|
|
|
|
- <div class="pagination hidden" id="drugPager"></div>
|
|
|
|
|
-</div>
|
|
|
|
|
-
|
|
|
|
|
-<!-- Sidebar -->
|
|
|
|
|
-<div class="sidebar-overlay" id="overlay" onclick="toggleSidebar()"></div>
|
|
|
|
|
-<div class="sidebar" id="sidebar">
|
|
|
|
|
- <div class="sidebar-header"><h2>对话历史</h2><button class="sidebar-close" onclick="toggleSidebar()">✕</button></div>
|
|
|
|
|
- <button class="sidebar-new" onclick="newConversation()">+ 新建对话</button>
|
|
|
|
|
- <div class="sidebar-list" id="historyList"><div style="padding:20px;text-align:center;color:#999;font-size:13px">加载中...</div></div>
|
|
|
|
|
-</div>
|
|
|
|
|
-
|
|
|
|
|
-<script>
|
|
|
|
|
-(function(){
|
|
|
|
|
-var API=(function(){var o=window.location.origin;if(o&&o!=='null'&&!o.startsWith('file'))return o;return'http://'+window.location.hostname+':42158'})();
|
|
|
|
|
-var TOKEN='',ABORT=null,STREAMING=false,CID='',DRUG_PAGE=1,MQ=[];
|
|
|
|
|
-
|
|
|
|
|
-function setStatus(m,c){document.getElementById('statusBar').textContent=m;document.getElementById('statusBar').className='status-bar status-'+c}
|
|
|
|
|
-function intentLabel(i){var m={drug_query:'药品查询',usage_guide:'用法用量',symptom_advice:'用药建议',regulation:'法规条款',exam_tutor:'考试辅导'};return m[i]||'综合查询'}
|
|
|
|
|
-function tagClass(i){var m={drug_query:'tag-drug',usage_guide:'tag-usage',symptom_advice:'tag-symptom',regulation:'tag-regulation',exam_tutor:'tag-exam'};return m[i]||'tag-fallback'}
|
|
|
|
|
-function esc(s){if(!s)return'';return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')}
|
|
|
|
|
-function escA(s){if(!s)return'';return s.replace(/"/g,'"')}
|
|
|
|
|
-function fmtDate(iso){if(!iso)return'';return new Date(iso).toLocaleString('zh-CN',{month:'short',day:'numeric',hour:'2-digit',minute:'2-digit'})}
|
|
|
|
|
-function md2html(h){if(!h)return'';h=esc(h);h=h.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>');h=h.replace(/⚠️\s*([^\n<]+)/g,'<span class="warn">$1</span>');h=h.replace(/\n\n+/g,'</p><p>');h=h.replace(/\n/g,'<br>');h=h.replace(/【(.+?)】/g,'<h3>【$1】</h3>');return'<p>'+h+'</p>'}
|
|
|
|
|
-
|
|
|
|
|
-function addMsg(role,text,streaming,intent,msgId){
|
|
|
|
|
- var chat=document.getElementById('chat'),div=document.createElement('div');
|
|
|
|
|
- div.className='msg '+role+(streaming?' streaming':'');
|
|
|
|
|
- if(role==='user'){div.textContent=text}else{
|
|
|
|
|
- var tag=intent||'';
|
|
|
|
|
- var tagHtml=tag?'<div class="tag '+tagClass(tag)+'">'+intentLabel(tag)+'</div>':'<div class="tag tag-fallback">AI 药典助手</div>';
|
|
|
|
|
- div.innerHTML=tagHtml+'<div class="content">'+(text?md2html(text):'')+(streaming?'<span class="cursor">|</span>':'')+'</div>';
|
|
|
|
|
- if(msgId){div.dataset.msgId=msgId;
|
|
|
|
|
- div.innerHTML+='<div class="feedback"><button class="fb-btn" onclick="feedback('+msgId+',\'like\',this)">👍</button><button class="fb-btn" onclick="feedback('+msgId+',\'dislike\',this)">👎</button></div>'}
|
|
|
|
|
- }
|
|
|
|
|
- chat.appendChild(div);chat.scrollTop=chat.scrollHeight;return div;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-function renderQueue(curText){
|
|
|
|
|
- var q=document.getElementById('queue'),qi=document.getElementById('queueItems');
|
|
|
|
|
- var hasCur=!!curText,hasQ=MQ.length>0;
|
|
|
|
|
- if(!hasCur&&!hasQ){q.style.display='none';return}
|
|
|
|
|
- q.style.display='block';
|
|
|
|
|
- var h='';
|
|
|
|
|
- if(hasCur)h+='<div class="queue-title">⏳ 正在回答:</div><div class="queue-item"><span class="running">💬 '+esc(curText.substring(0,35))+(curText.length>35?'...':'')+'</span></div>';
|
|
|
|
|
- if(hasQ){
|
|
|
|
|
- if(hasCur)h+='<div class="queue-title" style="margin-top:4px">排队中:</div>';
|
|
|
|
|
- else h+='<div class="queue-title">排队中:</div>';
|
|
|
|
|
- for(var i=0;i<MQ.length;i++)h+='<div class="queue-item"><span class="waiting">'+(i+1)+'. '+esc(MQ[i].substring(0,35))+(MQ[i].length>35?'...':'')+'</span><span class="cancel-btn" onclick="cancelQ('+i+')">✕</span></div>';
|
|
|
|
|
- }
|
|
|
|
|
- qi.innerHTML=h;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-window.cancelQ=function(i){MQ.splice(i,1);if(STREAMING)renderQueue('...');else document.getElementById('queue').style.display='none';};
|
|
|
|
|
-
|
|
|
|
|
-// ==== Init ====
|
|
|
|
|
-async function init(){
|
|
|
|
|
- setStatus('● 连接后端...','warn');
|
|
|
|
|
- try{
|
|
|
|
|
- var r=await fetch(API+'/health');var d=await r.json();
|
|
|
|
|
- setStatus('● '+d.app+' '+d.env,'ok');
|
|
|
|
|
- // 动态获取统计数据
|
|
|
|
|
- try{
|
|
|
|
|
- var sr=await fetch(API+'/api/v1/admin/stats');
|
|
|
|
|
- var sd=await sr.json();
|
|
|
|
|
- if(sd.total_drugs){
|
|
|
|
|
- document.getElementById('statsInfo').textContent=sd.total_drugs+' 种药品 · '+sd.total_chunks+' 条知识片段';
|
|
|
|
|
- document.getElementById('drugCount').textContent=sd.total_drugs;
|
|
|
|
|
- setStatus('● '+d.app+' '+d.env+' | '+sd.total_drugs+'种药品','ok');
|
|
|
|
|
- }
|
|
|
|
|
- }catch(ex){}
|
|
|
|
|
- try{
|
|
|
|
|
- var lr=await fetch(API+'/api/v1/auth/guest',{method:'POST',headers:{'Content-Type':'application/json'}});
|
|
|
|
|
- var ld=await lr.json();if(ld.access_token)TOKEN=ld.access_token;
|
|
|
|
|
- }catch(e){}
|
|
|
|
|
- }catch(e){setStatus('○ 后端未启动','err')}
|
|
|
|
|
- loadHistory();
|
|
|
|
|
-}init();
|
|
|
|
|
-
|
|
|
|
|
-// ==== Tab ====
|
|
|
|
|
-var currentTab='chat';window.switchTab=function(tab){currentTab=tab;document.querySelectorAll('.nav-btn').forEach(b=>b.classList.toggle('active',b.dataset.tab===tab));document.getElementById('tabChat').classList.toggle('hidden',tab!=='chat');document.getElementById('tabDrugs').classList.toggle('hidden',tab!=='drugs');if(tab==='drugs')searchDrugs(1)}
|
|
|
|
|
-
|
|
|
|
|
-// ==== Chat with queue ====
|
|
|
|
|
-window.quickAsk=function(t){document.getElementById('userInput').value=t;send()};
|
|
|
|
|
-
|
|
|
|
|
-window.send=function(){
|
|
|
|
|
- var input=document.getElementById('userInput'),text=input.value.trim();
|
|
|
|
|
- if(!text)return;input.value='';
|
|
|
|
|
- MQ.push(text);
|
|
|
|
|
- if(!STREAMING)processQ();else renderQueue();
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-window.stopAll=function(){
|
|
|
|
|
- if(ABORT){ABORT.abort();ABORT=null}
|
|
|
|
|
- MQ=[];document.getElementById('queue').style.display='none';
|
|
|
|
|
- var msgs=document.getElementById('chat').querySelectorAll('.msg.ai.streaming');
|
|
|
|
|
- msgs.forEach(function(m){var c=m.querySelector('.content');if(c){var cur=c.innerHTML.replace(/<span class="cursor">\|<\/span>/g,'');c.innerHTML=cur+' <span style="color:var(--rd)">⏹ 已停止</span>'}m.classList.remove('streaming')});
|
|
|
|
|
- STREAMING=false;document.getElementById('stopBtn').style.display='none';
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-async function processQ(){
|
|
|
|
|
- if(STREAMING||MQ.length===0)return;
|
|
|
|
|
- STREAMING=true;var text=MQ.shift();
|
|
|
|
|
- renderQueue(text);
|
|
|
|
|
- document.getElementById('stopBtn').style.display='inline-block';
|
|
|
|
|
- var w=document.getElementById('chat').querySelector('.welcome');if(w)w.remove();
|
|
|
|
|
- addMsg('user',text);
|
|
|
|
|
-
|
|
|
|
|
- var aiDiv=addMsg('ai','',true,''),contentDiv=aiDiv.querySelector('.content');
|
|
|
|
|
- contentDiv.innerHTML='<div class="thinking"><span class="think-dot"></span> 检索中...</div>';
|
|
|
|
|
- ABORT=new AbortController();
|
|
|
|
|
- var fullAnswer='', curIntent='', curSources=[], curCid=CID||'';
|
|
|
|
|
- try{
|
|
|
|
|
- var r=await fetch(API+'/api/v1/chat/stream',{method:'POST',signal:ABORT.signal,headers:{'Content-Type':'application/json','Accept':'text/event-stream','Authorization':'Bearer '+TOKEN},body:JSON.stringify({message:text,conversationId:CID||undefined})});
|
|
|
|
|
- if(!r.ok)throw new Error('HTTP '+r.status);
|
|
|
|
|
- if(!r.body){throw new Error('浏览器不支持流式响应')}
|
|
|
|
|
-
|
|
|
|
|
- var reader=r.body.getReader();
|
|
|
|
|
- var decoder=new TextDecoder('utf-8');
|
|
|
|
|
- var buffer='';
|
|
|
|
|
- var inThinking=true;
|
|
|
|
|
-
|
|
|
|
|
- function appendToken(token){
|
|
|
|
|
- if(inThinking){inThinking=false;contentDiv.innerHTML='';}
|
|
|
|
|
- fullAnswer+=token;
|
|
|
|
|
- contentDiv.innerHTML=md2html(fullAnswer)+'<span class="cursor">|</span>';
|
|
|
|
|
- document.getElementById('chat').scrollTop=document.getElementById('chat').scrollHeight;
|
|
|
|
|
- }
|
|
|
|
|
- function applyIntent(intent){
|
|
|
|
|
- curIntent=intent;
|
|
|
|
|
- var tagEl=aiDiv.querySelector('.tag');if(tagEl){tagEl.textContent=intentLabel(intent);tagEl.className='tag '+tagClass(intent)}
|
|
|
|
|
- }
|
|
|
|
|
- function setStatus(status){
|
|
|
|
|
- if(inThinking&&status){var t=contentDiv.querySelector('.thinking');if(t)t.innerHTML='<span class="think-dot"></span> '+status;}
|
|
|
|
|
- }
|
|
|
|
|
- function applyMeta(meta){
|
|
|
|
|
- if(meta.conversation_id){curCid=meta.conversation_id;CID=curCid;loadHistory()}
|
|
|
|
|
- if(meta.sources&&meta.sources.length){curSources=meta.sources}
|
|
|
|
|
- }
|
|
|
|
|
- function finalize(){
|
|
|
|
|
- contentDiv.innerHTML=md2html(fullAnswer);contentDiv.querySelector('.cursor')?.remove();
|
|
|
|
|
- aiDiv.classList.remove('streaming');
|
|
|
|
|
- if(curSources.length){
|
|
|
|
|
- var srcDiv=document.createElement('div');srcDiv.className='source';var parts=[];
|
|
|
|
|
- for(var j=0;j<Math.min(5,curSources.length);j++){var s=curSources[j];parts.push('<span>'+(s.section||s.name||'')+' ('+Number(s.score||0).toFixed(2)+')</span>')}
|
|
|
|
|
- srcDiv.innerHTML='📚 '+parts.join(' · ');aiDiv.appendChild(srcDiv)}
|
|
|
|
|
- STREAMING=false;document.getElementById('stopBtn').style.display='none';
|
|
|
|
|
- document.getElementById('queue').style.display='none';
|
|
|
|
|
- document.getElementById('chat').scrollTop=document.getElementById('chat').scrollHeight;
|
|
|
|
|
- if(MQ.length>0)setTimeout(processQ,300);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // SSE 解析循环
|
|
|
|
|
- while(true){
|
|
|
|
|
- var done, value;
|
|
|
|
|
- var res=await reader.read();done=res.done;value=res.value;
|
|
|
|
|
- if(done)break;
|
|
|
|
|
- buffer+=decoder.decode(value,{stream:true});
|
|
|
|
|
- var lines=buffer.split('\n');
|
|
|
|
|
- buffer=lines.pop();
|
|
|
|
|
- var curEvent='content';
|
|
|
|
|
- for(var i=0;i<lines.length;i++){
|
|
|
|
|
- var line=lines[i];
|
|
|
|
|
- if(line.indexOf('event:')===0){curEvent=line.substring(6).trim();continue}
|
|
|
|
|
- if(line.indexOf('data:')===0){
|
|
|
|
|
- var data=line.substring(5);
|
|
|
|
|
- if(data.startsWith(' '))data=data.substring(1);
|
|
|
|
|
- if(data==='[DONE]'){finalize();return}
|
|
|
|
|
- if(curEvent==='intent'){applyIntent(data)}
|
|
|
|
|
- else if(curEvent==='status'){setStatus(data)}
|
|
|
|
|
- else if(curEvent==='meta'){try{applyMeta(JSON.parse(data))}catch(e){}}
|
|
|
|
|
- else{appendToken(data)}
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if(fullAnswer||curSources.length){finalize()}
|
|
|
|
|
- else{contentDiv.innerHTML='<span style="color:var(--rd)">无结果</span>';aiDiv.classList.remove('streaming');STREAMING=false;document.getElementById('stopBtn').style.display='none'}
|
|
|
|
|
- }catch(e){
|
|
|
|
|
- if(e.name!=='AbortError'){contentDiv.innerHTML='<span style="color:var(--rd)">❌ '+e.message+'</span>';aiDiv.classList.remove('streaming')}
|
|
|
|
|
- STREAMING=false;document.getElementById('stopBtn').style.display='none';
|
|
|
|
|
- }
|
|
|
|
|
- ABORT=null;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-window.feedback=async function(msgId,type,btn){
|
|
|
|
|
- try{await fetch(API+'/api/v1/chat/feedback',{method:'POST',headers:{'Content-Type':'application/json','Authorization':'Bearer '+TOKEN},body:JSON.stringify({message_id:msgId,feedback:type})});
|
|
|
|
|
- var fb=btn.parentElement;fb.querySelectorAll('.fb-btn').forEach(b=>b.classList.remove('active-like','active-dislike'));
|
|
|
|
|
- btn.classList.add(type==='like'?'active-like':'active-dislike')}catch(e){}
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// ==== Sidebar ====
|
|
|
|
|
-window.toggleSidebar=function(){var sb=document.getElementById('sidebar'),ov=document.getElementById('overlay'),show=!sb.classList.contains('show');sb.classList.toggle('show',show);ov.classList.toggle('show',show);if(show)loadHistory()}
|
|
|
|
|
-window.newConversation=function(){CID='';document.getElementById('chat').innerHTML='<div class="welcome"><div class="icon"></div><p>输入药品名称或用药问题</p></div>';toggleSidebar()}
|
|
|
|
|
-
|
|
|
|
|
-async function loadHistory(){
|
|
|
|
|
- var list=document.getElementById('historyList');
|
|
|
|
|
- try{var r=await fetch(API+'/api/v1/chat/history',{headers:{'Authorization':'Bearer '+TOKEN}});var d=await r.json();
|
|
|
|
|
- if(!d.items||!d.items.length){list.innerHTML='<div style="padding:20px;text-align:center;color:#999;font-size:13px">暂无对话</div>';return}
|
|
|
|
|
- var h='';for(var i=0;i<d.items.length;i++){var item=d.items[i],act=item.conversation_id===CID?' active':'';h+='<div class="sidebar-item'+act+'" data-cid="'+escA(item.conversation_id)+'" onclick="loadConv(\''+escA(item.conversation_id)+'\')"><div class="s-title">'+esc(item.title||'新对话')+'</div><div class="s-meta">'+item.message_count+' 条消息 · '+fmtDate(item.created_at)+'</div></div>'}list.innerHTML=h;
|
|
|
|
|
- }catch(e){list.innerHTML='<div style="padding:20px;text-align:center;color:#999;font-size:13px">加载失败</div>'}
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-window.loadConv=async function(cid){
|
|
|
|
|
- var chat=document.getElementById('chat');chat.innerHTML='<div style="text-align:center;padding:30px;color:#888"><span class="think-dot"></span> 加载中...</div>';
|
|
|
|
|
- try{var r=await fetch(API+'/api/v1/chat/history/'+cid,{headers:{'Authorization':'Bearer '+TOKEN}});var d=await r.json();chat.innerHTML='';
|
|
|
|
|
- for(var i=0;i<d.messages.length;i++){var m=d.messages[i];addMsg(m.role,m.content,false,m.intent||'',m.id)}CID=cid;
|
|
|
|
|
- document.getElementById('sidebar').classList.remove('show');document.getElementById('overlay').classList.remove('show');chat.scrollTop=chat.scrollHeight}catch(e){chat.innerHTML='<div style="text-align:center;padding:30px;color:var(--rd)">加载失败</div>'}
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// ==== Drug Search ====
|
|
|
|
|
-window.searchDrugs=async function(page){
|
|
|
|
|
- DRUG_PAGE=page||1;var kw=document.getElementById('drugSearch').value.trim();
|
|
|
|
|
- var list=document.getElementById('drugList'),pager=document.getElementById('drugPager');
|
|
|
|
|
- list.innerHTML='<div style="text-align:center;padding:30px;color:var(--sub)"><span class="think-dot"></span> 搜索中...</div>';
|
|
|
|
|
- try{var url=API+'/api/v1/drug/search?page='+DRUG_PAGE+'&pageSize=15'+(kw?'&keyword='+encodeURIComponent(kw):'');
|
|
|
|
|
- var r=await fetch(url,{headers:{'Authorization':'Bearer '+TOKEN}});var d=await r.json();
|
|
|
|
|
- if(!d.items||!d.items.length){list.innerHTML='<div class="welcome"><div class="icon"></div><p>未找到</p></div>';pager.classList.add('hidden');return}
|
|
|
|
|
- var h='';for(var i=0;i<d.items.length;i++){var item=d.items[i];h+='<div class="drug-card" onclick="loadDrug(\''+escA(item.drug_id)+'\')"><h3>'+esc(item.name)+'</h3><div class="meta">'+esc(item.category||'')+' · '+esc(item.subcategory||'')+' · '+esc(item.source_version||'')+'</div></div>'}list.innerHTML=h;
|
|
|
|
|
- var total=d.total_pages||1,ph='';if(DRUG_PAGE>1)ph+='<button onclick="searchDrugs('+(DRUG_PAGE-1)+')">上一页</button>';ph+='<button disabled>'+DRUG_PAGE+'/'+total+'</button>';if(DRUG_PAGE<total)ph+='<button onclick="searchDrugs('+(DRUG_PAGE+1)+')">下一页</button>';pager.innerHTML=ph;pager.classList.toggle('hidden',total<=1)}catch(e){list.innerHTML='<div style="text-align:center;padding:30px;color:var(--rd)">搜索失败</div>'}
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-window.loadDrug=async function(drugId){
|
|
|
|
|
- var list=document.getElementById('drugList'),pager=document.getElementById('drugPager');pager.classList.add('hidden');list.innerHTML='<div style="text-align:center;padding:30px;color:var(--sub)"><span class="think-dot"></span> 加载中...</div>';
|
|
|
|
|
- try{var r=await fetch(API+'/api/v1/drug/'+drugId,{headers:{'Authorization':'Bearer '+TOKEN}});var d=await r.json();if(!d.data){list.innerHTML='<div class="welcome"><div class="icon"></div><p>药品不存在</p></div>';return}
|
|
|
|
|
- var item=d.data,sections='';if(item.sections)for(var k in item.sections){sections+='<h3>【'+esc(k)+'】</h3><div class="section">'+item.sections[k]+'</div>'}
|
|
|
|
|
- list.innerHTML='<div class="drug-detail"><h2>'+esc(item.name)+'</h2>'+(item.name_en?'<div class="info-row"><b>英文:</b>'+esc(item.name_en)+'</div>':'')+(item.pinyin?'<div class="info-row"><b>拼音:</b>'+esc(item.pinyin)+'</div>':'')+(item.category?'<div class="info-row"><b>分类:</b>'+esc(item.category)+' / '+esc(item.subcategory||'')+'</div>':'')+(item.source_version?'<div class="info-row"><b>来源:</b>'+esc(item.source_version)+' '+esc(item.source_volume||'')+'</div>':'')+sections+'<button class="btn btn-danger" style="margin-top:16px;width:100%" onclick="searchDrugs('+DRUG_PAGE+')">← 返回列表</button></div>'}catch(e){list.innerHTML='<div style="text-align:center;padding:30px;color:var(--rd)">加载失败</div>'}
|
|
|
|
|
-};
|
|
|
|
|
-})();
|
|
|
|
|
-</script>
|
|
|
|
|
-</body>
|
|
|
|
|
-</html>
|
|
|