import{a as p,j as c}from"./react-vendor-BVoutfaX.js";import{u as g}from"./index-CEwbkr-L.js";import"./xyflow-CYMCcsWN.js";import"./graph-layout-7tFr_anw.js";import"./elk-CXeXGyKz.js";import"./graphology-BgTy_cc3.js";function b(t){return Array.isArray(t)?t.join(" "):typeof t=="string"?t:""}function j(t){const n=t.split(`
`);let o=0;const e=n.map(s=>{const i=s.match(/^\s*\d+\|(.+)$/);return i?(o+=1,i[1]):s});return o>=Math.max(3,Math.floor(n.length/4))?e.join(`
`):t}function w(t){var n;return j([t.name,t.summary,t.filePath,t.tags.join(" "),b((n=t.knowledgeMeta)==null?void 0:n.content)].join(`
`))}function x(t){return t.toLowerCase().replace(/[\s`*_>#|\-\[\]()（）【】《》，。、：:；;,.\/\\]+/g,"")}function N(t){const n=t.toLowerCase(),o=n.match(/[a-z0-9_\-\.]{2,}/g)??[],e=n.match(/[\u4e00-\u9fa5]{2,}/g)??[],s=n.match(/[\u4e00-\u9fa5]/g)??[],i=[],l=[];for(let r=0;r<s.length-1;r+=1)i.push(`${s[r]}${s[r+1]}`);for(let r=0;r<s.length-2;r+=1)l.push(`${s[r]}${s[r+1]}${s[r+2]}`);return Array.from(new Set([t.trim(),x(t),...o,...e,...l,...i,...s].filter(r=>r.trim().length>0)))}function v(t){var s,i;const n=[];let o,e=[];for(const l of t.split(`
`)){const r=(i=(s=l.match(/^\s{0,3}#{1,6}\s+(.+)$/))==null?void 0:s[1])==null?void 0:i.trim();r?(e.length>0&&n.push({heading:o,text:e.join(`
`)}),o=r,e=[l]):e.push(l)}return e.length>0&&n.push({heading:o,text:e.join(`
`)}),n.length>0?n:[{text:t}]}function y(t,n){const o=t.replace(/\s+/g," ").trim();if(!o)return"";const e=o.toLowerCase(),s=x(o),i=n.map(d=>{const m=e.indexOf(d.toLowerCase());if(m>=0)return m;const a=s.indexOf(x(d));return a>=0?Math.min(a,o.length-1):-1}).filter(d=>d>=0).sort((d,m)=>d-m)[0],l=Math.max(0,(i??0)-90),r=o.slice(l,l+260);return`${l>0?"…":""}${r}${l+260<o.length?"…":""}`}function u(t,n,o){const e=t.toLowerCase(),s=x(t),i=x(n);let l=0;n&&e.includes(n.toLowerCase())&&(l+=220),i&&s.includes(i)&&(l+=180);for(const r of o){const d=r.toLowerCase();if(!d)continue;const m=x(d),a=e.split(d).length-1,f=m?s.split(m).length-1:0,h=Math.max(a,f);h>0&&(l+=h*Math.min(Math.max(d.length,m.length),12))}return l}function $(t,n){const o=N(n);return o.length===0?[]:t.flatMap(e=>{const s=w(e),i=[e.name,e.summary,e.filePath,e.tags.join(" ")].join(`
`),l=u(i,n,o)*4;return v(s).map(r=>{const d=r.heading?u(r.heading,n,o)*8:0,m=u(r.text,n,o),a=l+d+m;return{node:e,score:a,heading:r.heading,snippet:y(r.text,o)}})}).filter(e=>e.score>0&&e.snippet).sort((e,s)=>s.score-e.score).reduce((e,s)=>{const i=`${s.node.id}:${s.heading??s.snippet.slice(0,48)}`;return e.some(l=>`${l.node.id}:${l.heading??l.snippet.slice(0,48)}`===i)||e.push(s),e},[]).slice(0,12)}function k(t,n){if(!t.trim())return"请输入要检索或提问的内容。";if(n.length===0)return"知识库未检索到明确依据。建议换一个关键词，或确认相关文档是否已经加入 05_需求文档 / 07_技术文档 / 08_测试相关。";const o=n.slice(0,4);return[`结论：已基于本地知识库检索到 ${n.length} 条相关依据。以下是可追溯摘要：`,"",...o.map((e,s)=>`${s+1}. ${e.node.name}${e.heading?` / ${e.heading}`:""}
   ${e.snippet}
   来源：${e.node.filePath??e.node.id}`),"","说明：当前为本地抽取式 RAG，不调用外部大模型；回答只基于知识库原文片段生成。"].join(`
`)}function _({onClose:t}){const n=g(a=>a.graph),o=g(a=>a.navigateToNodeInLayer),[e,s]=p.useState(""),[i,l]=p.useState(""),r=p.useMemo(()=>$((n==null?void 0:n.nodes)??[],i),[n,i]),d=p.useMemo(()=>k(i,r),[i,r]),m=()=>l(e.trim());return c.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/65 backdrop-blur-sm p-4 sm:p-6",onMouseDown:t,children:c.jsxs("div",{className:"w-[calc(100vw-32px)] max-w-[920px] h-[calc(100vh-64px)] max-h-[760px] rounded-lg border border-border-medium bg-surface shadow-2xl overflow-hidden flex flex-col",onMouseDown:a=>a.stopPropagation(),children:[c.jsxs("div",{className:"flex items-center justify-between px-5 py-4 border-b border-border-subtle bg-elevated/40",children:[c.jsxs("div",{children:[c.jsx("h2",{className:"font-heading text-lg text-text-primary",children:"知识库 RAG 问答"}),c.jsx("p",{className:"text-xs text-text-muted mt-1",children:"本地检索 knowledge-graph.json 中的文档内容，返回可追溯来源。"})]}),c.jsx("button",{type:"button",onClick:t,className:"text-text-muted hover:text-accent transition-colors text-xl leading-none",children:"×"})]}),c.jsxs("div",{className:"p-5 border-b border-border-subtle",children:[c.jsxs("div",{className:"flex gap-2",children:[c.jsx("textarea",{value:e,onChange:a=>s(a.target.value),onKeyDown:a=>{(a.ctrlKey||a.metaKey)&&a.key==="Enter"&&m()},placeholder:"例如：如愿系统的真实人额度规则是什么？客服工单怎么流转？",className:"flex-1 min-h-[76px] resize-none bg-elevated text-text-primary text-sm rounded-lg px-3 py-2 border border-border-subtle focus:outline-none focus:border-accent/50 placeholder-text-muted"}),c.jsx("button",{type:"button",onClick:m,className:"px-4 py-2 rounded-lg bg-accent/20 text-accent hover:text-accent-bright border border-accent/30 text-sm font-semibold transition-colors",children:"检索问答"})]}),c.jsx("div",{className:"mt-2 text-[11px] text-text-muted",children:"快捷键：Ctrl + Enter 提交"})]}),c.jsxs("div",{className:"flex-1 min-h-0 overflow-auto p-5 grid grid-cols-1 lg:grid-cols-[1fr_320px] gap-5",children:[c.jsxs("section",{className:"min-w-0",children:[c.jsx("h3",{className:"text-[11px] font-semibold text-accent uppercase tracking-wider mb-2",children:"回答"}),c.jsx("pre",{className:"whitespace-pre-wrap text-sm leading-relaxed text-text-secondary bg-elevated/50 border border-border-subtle rounded-lg p-4 font-sans",children:i?d:"输入问题后点击“检索问答”。"})]}),c.jsxs("section",{className:"min-w-0",children:[c.jsx("h3",{className:"text-[11px] font-semibold text-accent uppercase tracking-wider mb-2",children:"命中文档"}),c.jsxs("div",{className:"space-y-2",children:[r.length===0&&i&&c.jsx("div",{className:"text-sm text-text-muted border border-border-subtle rounded-lg p-3",children:"暂无命中。"}),r.map(a=>c.jsxs("button",{type:"button",onClick:()=>o(a.node.id),className:"w-full text-left border border-border-subtle bg-elevated/40 hover:border-accent/40 rounded-lg p-3 transition-colors",children:[c.jsx("div",{className:"text-sm text-text-primary truncate",children:a.node.name}),a.heading&&c.jsx("div",{className:"text-[11px] text-accent mt-1 truncate",children:a.heading}),c.jsx("div",{className:"text-[11px] text-text-muted mt-1 truncate",children:a.node.filePath??a.node.id}),c.jsx("div",{className:"text-xs text-text-secondary mt-2 line-clamp-3",children:a.snippet})]},a.node.id))]})]})]})]})})}export{_ as default};
