app.js
6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 共享工具 + 上传/文档/抽取
const OT = {
TYPE_COLORS: {
"品牌":"#6366f1","集团":"#8b5cf6","门店":"#0ea5e9","兜底协议":"#f59e0b","口径":"#14b8a6",
"周期":"#ef4444","产品":"#22c55e","指标":"#ec4899","数据源":"#3b82f6","数仓层":"#64748b",
"分析模型":"#a855f7","策略":"#f97316","盯防":"#eab308","批次":"#06b6d4","角色":"#84cc16",
"功能项":"#94a3b8","业务规则":"#d946ef","任务":"#10b981","未分类":"#9ca3af"
},
typeColor(t){
if(this.TYPE_COLORS[t]) return this.TYPE_COLORS[t];
let h=0; for(const c of (t||"")) h=(h*31+c.charCodeAt(0))%360;
return `hsl(${h},60%,55%)`;
},
async api(path, opts){
const r = await fetch(path, opts);
if(!r.ok){ let m=""; try{m=(await r.json()).detail}catch(e){} throw new Error(m||("HTTP "+r.status)); }
const ct = r.headers.get("content-type")||"";
return ct.includes("json") ? r.json() : r.text();
},
esc(s){ return (s||"").replace(/[&<>]/g,c=>({"&":"&","<":"<",">":">"}[c])); },
pill(text, color, soft){
const style = soft ? "" : `style="background:${color}"`;
return `<span class="pill ${soft?'soft':''}" ${style}>${OT.esc(text)}</span>`;
}
};
// ---------- Tabs ----------
document.querySelectorAll(".tab").forEach(t=>{
t.addEventListener("click",()=>{
document.querySelectorAll(".tab").forEach(x=>x.classList.remove("active"));
document.querySelectorAll(".panel").forEach(x=>x.classList.remove("active"));
t.classList.add("active");
document.getElementById("tab-"+t.dataset.tab).classList.add("active");
if(t.dataset.tab==="graph" && window.GraphView) window.GraphView.ensure();
if(t.dataset.tab==="query" && window.QueryView) window.QueryView.ensure();
if(t.dataset.tab==="settings" && window.SettingsView) window.SettingsView.load();
});
});
// ---------- 上传 ----------
let pickedFile = null;
const dz = document.getElementById("dropzone");
const fileInput = document.getElementById("file-input");
dz.addEventListener("click",()=>fileInput.click());
fileInput.addEventListener("change",()=>{ pickedFile=fileInput.files[0]; showPicked(); });
["dragover","dragenter"].forEach(ev=>dz.addEventListener(ev,e=>{e.preventDefault();dz.classList.add("drag");}));
["dragleave","drop"].forEach(ev=>dz.addEventListener(ev,e=>{e.preventDefault();dz.classList.remove("drag");}));
dz.addEventListener("drop",e=>{ pickedFile=e.dataTransfer.files[0]; showPicked(); });
function showPicked(){ document.getElementById("dz-file").textContent = pickedFile? ("已选择:"+pickedFile.name) : "未选择文件"; }
document.getElementById("btn-upload").addEventListener("click", async ()=>{
if(!pickedFile){ alert("请先选择文件"); return; }
const hint=document.getElementById("upload-hint"); hint.textContent="上传中…";
try{
const fd=new FormData(); fd.append("file", pickedFile);
const d=await OT.api("/api/documents",{method:"POST",body:fd});
hint.textContent=`已上传:${d.filename}(ID ${d.id}),可在下方点击“抽取”。`;
pickedFile=null; fileInput.value=""; showPicked();
loadDocs();
}catch(e){ hint.textContent="上传失败:"+e.message; }
});
// ---------- 文档列表 ----------
document.getElementById("btn-refresh-docs").addEventListener("click",loadDocs);
async function loadDocs(){
const docs=await OT.api("/api/documents");
const tb=document.querySelector("#doc-table tbody"); tb.innerHTML="";
if(!docs.length){ tb.innerHTML=`<tr><td colspan="6" class="muted">暂无文档,请先上传。</td></tr>`; return; }
for(const d of docs){
const tr=document.createElement("tr");
const busy = d.status==="extracting"||d.status==="parsing";
tr.innerHTML=`<td>${d.id}</td><td>${OT.esc(d.filename)}</td>
<td><span class="badge ${d.status}">${d.status}</span></td>
<td>${d.entity_count}</td><td>${d.relation_count}</td>
<td><button class="btn ghost btn-extract" data-id="${d.id}" data-name="${OT.esc(d.filename)}" ${busy?'disabled':''}>抽取</button></td>`;
tb.appendChild(tr);
}
tb.querySelectorAll(".btn-extract").forEach(b=>b.addEventListener("click",()=>startExtract(b.dataset.id,b.dataset.name)));
}
// ---------- 抽取 SSE ----------
function startExtract(id, name){
const card=document.getElementById("extract-card"); card.hidden=false;
document.getElementById("extract-doc").textContent=`文档:${name}(ID ${id})`;
const bar=document.getElementById("extract-bar"); bar.style.width="0%";
const log=document.getElementById("extract-log"); log.innerHTML="";
const counts=document.getElementById("extract-counts"); counts.innerHTML="";
const addLog=(msg,cls="")=>{ const d=document.createElement("div"); d.className="l "+cls; d.textContent=msg; log.appendChild(d); log.scrollTop=log.scrollHeight; };
const es=new EventSource(`/api/documents/${id}/extract`);
es.onmessage=(e)=>{
const ev=JSON.parse(e.data);
if(ev.stage==="parse"||ev.stage==="chunk"||ev.stage==="index"){ addLog("• "+(ev.message||ev.stage)); }
else if(ev.stage==="extract"){
if(ev.error){ addLog(`× 片段 ${ev.i}/${ev.total} 出错:${ev.error}`,"err"); }
else{
bar.style.width=Math.round(ev.i/ev.total*100)+"%";
addLog(`✓ [${ev.i}/${ev.total}] ${ev.heading||"正文"} · 新增实体 ${ev.new_entities} / 关系 ${ev.new_relations}`,"ok");
counts.innerHTML=`${OT.pill("累计实体 "+ev.cum_entities,"#4f46e5")} ${OT.pill("累计关系 "+ev.cum_relations,"#0ea5e9")}`;
}
}
else if(ev.stage==="done"){
bar.style.width="100%";
addLog("✔ 抽取完成:实体 "+ev.stats.entity_count+" · 关系 "+ev.stats.relation_count,"ok");
counts.innerHTML=`${OT.pill("实体 "+ev.stats.entity_count,"#4f46e5")} ${OT.pill("关系 "+ev.stats.relation_count,"#0ea5e9")}`;
es.close(); loadDocs(); loadInfo();
if(window.GraphView) window.GraphView.markDirty();
}
else if(ev.stage==="error"||ev.type==="error"){ addLog("× 失败:"+ev.error,"err"); es.close(); loadDocs(); }
};
es.onerror=()=>{ addLog("× 连接中断","err"); es.close(); loadDocs(); };
}
// ---------- 顶部信息 ----------
async function loadInfo(){
try{
const i=await OT.api("/api/info");
document.getElementById("model-info").textContent =
`模型 ${i.model} · 向量 ${i.embed_model} · 实体 ${i.stats.entity_count} · 关系 ${i.stats.relation_count} · 文档 ${i.doc_count}`;
}catch(e){ document.getElementById("model-info").textContent="服务未就绪"; }
}
loadDocs(); loadInfo();