@fgbg/mdocs 1.3.19 → 1.3.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +1 -1
  2. package/agent-skills/changelog/SKILL.md +13 -0
  3. package/agent-skills/index.json +1 -1
  4. package/agent-skills/usage-my-documents/SKILL.md +2 -5
  5. package/agent-skills/usage-search/SKILL.md +34 -12
  6. package/agent-skills/usage-settings/SKILL.md +2 -1
  7. package/dist/server/agent/Agent/tools-documents.js +57 -5
  8. package/dist/server/agent/Agent/tools-documents.js.map +1 -1
  9. package/dist/server/agent/Agent/tools-registry.test.js +2 -0
  10. package/dist/server/agent/Agent/tools-registry.test.js.map +1 -1
  11. package/dist/server/agent/Agent/tools-template.js +3 -1
  12. package/dist/server/agent/Agent/tools-template.js.map +1 -1
  13. package/dist/server/app.js +3 -0
  14. package/dist/server/app.js.map +1 -1
  15. package/dist/server/db/schema.js +11 -0
  16. package/dist/server/db/schema.js.map +1 -1
  17. package/dist/server/documents/document.service.js +9 -1
  18. package/dist/server/documents/document.service.js.map +1 -1
  19. package/dist/server/routes/documents.routes.js +43 -5
  20. package/dist/server/routes/documents.routes.js.map +1 -1
  21. package/dist/server/search/document-index-manager.js +66 -22
  22. package/dist/server/search/document-index-manager.js.map +1 -1
  23. package/dist/server/search/document-index-manager.test.js +24 -17
  24. package/dist/server/search/document-index-manager.test.js.map +1 -1
  25. package/dist/server/search/embedding-admin.js +158 -0
  26. package/dist/server/search/embedding-admin.js.map +1 -0
  27. package/dist/server/search/embedding-model.js +209 -0
  28. package/dist/server/search/embedding-model.js.map +1 -0
  29. package/dist/server/search/embedding-store.js +146 -0
  30. package/dist/server/search/embedding-store.js.map +1 -0
  31. package/dist/server/search/embedding-store.test.js +293 -0
  32. package/dist/server/search/embedding-store.test.js.map +1 -0
  33. package/dist/server/search/search.service.js +126 -48
  34. package/dist/server/search/search.service.js.map +1 -1
  35. package/dist/web/assets/{AgentChatPanel-DTj8Nz0Z.js → AgentChatPanel-BZlAgyrV.js} +3 -3
  36. package/dist/web/assets/{AgentSkillFormCard-DTgxybJh.js → AgentSkillFormCard-CzJG8FK4.js} +3 -3
  37. package/dist/web/assets/AiWriteWorkbench-DiSDDAee.js +7 -0
  38. package/dist/web/assets/{DiagramEditor-NtNY3sZL-B2Aeu79R.js → DiagramEditor-NtNY3sZL-DfkpQX3P.js} +1 -1
  39. package/dist/web/assets/{DocChrome-4qmjr6wa.js → DocChrome-QTGER_Af.js} +1 -1
  40. package/dist/web/assets/{DocumentEditor-KEURMFDp.js → DocumentEditor-DYSRZnJa.js} +1 -1
  41. package/dist/web/assets/{HtmlEditor-jCKtKOCT.js → HtmlEditor-oDPrhq3o.js} +1 -1
  42. package/dist/web/assets/{MergeView-eRcs-x1s.js → MergeView-CbFSN3Bi.js} +4 -4
  43. package/dist/web/assets/{PlaygroundPage-M7fqYn4Z.js → PlaygroundPage-DuMH_V-7.js} +1 -1
  44. package/dist/web/assets/SettingsPage-C_RMO-5M.js +1 -0
  45. package/dist/web/assets/{Toolbar-F3tR5o7R.js → Toolbar-D9FcRwg3.js} +1 -1
  46. package/dist/web/assets/{VisitorPickerModal-CPjKg8ox.js → VisitorPickerModal-CM13Y6jQ.js} +1 -1
  47. package/dist/web/assets/{check-CQSGOkW1.js → check-Bf8YbzH7.js} +1 -1
  48. package/dist/web/assets/{copy-BMAJ--_Y.js → copy-SBghRY2T.js} +1 -1
  49. package/dist/web/assets/{diagram-Bk84WZto.js → diagram-CgEd0hrk.js} +3 -3
  50. package/dist/web/assets/index-CIgmAX2Q.css +1 -0
  51. package/dist/web/assets/index-Cwy87sZC.js +293 -0
  52. package/dist/web/assets/{meta2dEngine-BD2XbzaV-BAZUiXoN.js → meta2dEngine-BD2XbzaV-ulfRZEvk.js} +1 -1
  53. package/dist/web/assets/{motion-DFABYlwt.js → motion-Dyy8pB0Q.js} +1 -1
  54. package/dist/web/assets/{plus-C3uXIATM.js → plus-CEapsZwl.js} +1 -1
  55. package/dist/web/assets/{react-BXGUMaBS.js → react-BkX5Koku.js} +3 -3
  56. package/dist/web/assets/ui-ZPt_BNBg.js +3165 -0
  57. package/dist/web/index.html +5 -5
  58. package/package.json +4 -2
  59. package/dist/web/assets/AiWriteWorkbench-D2RzXzty.js +0 -7
  60. package/dist/web/assets/SettingsPage-mS3g_vBq.js +0 -1
  61. package/dist/web/assets/index-C8iEyMxp.css +0 -1
  62. package/dist/web/assets/index-DUu4Is5p.js +0 -293
  63. package/dist/web/assets/ui-CvA4pq3d.js +0 -3116
@@ -27,12 +27,12 @@
27
27
  opacity: 0.65;
28
28
  }
29
29
  </style>
30
- <script type="module" crossorigin src="/assets/index-DUu4Is5p.js"></script>
30
+ <script type="module" crossorigin src="/assets/index-Cwy87sZC.js"></script>
31
31
  <link rel="modulepreload" crossorigin href="/assets/react-vendor-BuABiQkA.js">
32
- <link rel="modulepreload" crossorigin href="/assets/motion-DFABYlwt.js">
33
- <link rel="modulepreload" crossorigin href="/assets/ui-CvA4pq3d.js">
34
- <link rel="modulepreload" crossorigin href="/assets/diagram-Bk84WZto.js">
35
- <link rel="stylesheet" crossorigin href="/assets/index-C8iEyMxp.css">
32
+ <link rel="modulepreload" crossorigin href="/assets/motion-Dyy8pB0Q.js">
33
+ <link rel="modulepreload" crossorigin href="/assets/ui-ZPt_BNBg.js">
34
+ <link rel="modulepreload" crossorigin href="/assets/diagram-CgEd0hrk.js">
35
+ <link rel="stylesheet" crossorigin href="/assets/index-CIgmAX2Q.css">
36
36
  </head>
37
37
  <body>
38
38
  <div id="root"></div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgbg/mdocs",
3
- "version": "1.3.19",
3
+ "version": "1.3.20",
4
4
  "description": "A Markdown knowledge base for small teams",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -60,6 +60,7 @@
60
60
  "motion": "^12.0.0",
61
61
  "multer": "^2.1.1",
62
62
  "node-diff3": "^3.2.0",
63
+ "node-llama-cpp": "3.22.1",
63
64
  "react-force-graph-2d": "^1.29.1",
64
65
  "react-markdown": "10.1.0",
65
66
  "react-router-dom": "^7.14.2",
@@ -73,7 +74,8 @@
73
74
  "onlyBuiltDependencies": [
74
75
  "@fgbg/lobe-editor",
75
76
  "better-sqlite3",
76
- "esbuild"
77
+ "esbuild",
78
+ "node-llama-cpp"
77
79
  ]
78
80
  },
79
81
  "devDependencies": {
@@ -1,7 +0,0 @@
1
- import{r as c,j as e}from"./react-vendor-BuABiQkA.js";import{az as Se,aA as Ie,ax as ve,ao as de,ap as Me,q as Ae,aq as ue,a4 as Te,at as Le,aB as Re,au as $e,av as Ee,aw as qe}from"./index-DUu4Is5p.js";import{H as _e,S as Pe,A as De,a as Be}from"./AgentSkillFormCard-DTgxybJh.js";import{P as Fe}from"./plus-C3uXIATM.js";import{ae as He,af as Ke}from"./ui-CvA4pq3d.js";import"./diagram-Bk84WZto.js";import"./motion-DFABYlwt.js";function Oe(t){const{block:s,onResolved:i}=t,[r,f]=c.useState(""),[g,p]=c.useState(!1),[N,k]=c.useState(1),h=c.useRef(!1),j=c.useRef(i);j.current=i;const u=s.status==="open",E=c.useRef(0);c.useEffect(()=>{if(!u)return;h.current=!1;const m=new Date(s.expiresAt).getTime(),b=Date.now(),P=Math.max(m-b,1);E.current=P,k(1);const $=()=>{const K=m-Date.now(),A=Math.max(0,Math.min(1,K/P));k(A),K<=0&&!h.current&&(h.current=!0,Se(s.requestId).catch(()=>{}).finally(()=>{j.current(s.requestId,"","expired")}))};$();const H=window.setInterval($,100);return()=>window.clearInterval(H)},[u,s.expiresAt,s.requestId]);async function y(m){const b=m.trim();if(!(!b||g||!u)){p(!0);try{await Ie(s.requestId,b),i(s.requestId,b,"selected")}catch{i(s.requestId,b,"failed")}finally{p(!1)}}}async function R(){if(!(g||!u)){p(!0),h.current=!0;try{await ve(s.requestId),i(s.requestId,"","cancelled")}catch{i(s.requestId,"","failed")}finally{p(!1)}}}const F=Math.max(0,Math.ceil(N*E.current/1e3));return e.jsxs("div",{className:"mdocs-agent-choice-card"+(u?"":" mdocs-agent-choice-card-done"),children:[e.jsxs("div",{className:"mdocs-agent-choice-head",children:[e.jsx("p",{className:"mdocs-agent-choice-title",children:s.title}),u?e.jsxs("span",{className:"mdocs-agent-choice-timer","aria-live":"polite",children:[F,"s"]}):null]}),u?e.jsx("div",{className:"mdocs-agent-choice-progress",role:"progressbar","aria-valuemin":0,"aria-valuemax":100,"aria-valuenow":Math.round(N*100),children:e.jsx("div",{className:"mdocs-agent-choice-progress-bar",style:{transform:`scaleX(${N})`}})}):null,s.status==="selected"&&s.selected?e.jsxs("p",{className:"mdocs-agent-choice-result",children:["已选择:",s.selected]}):null,s.status==="cancelled"?e.jsx("p",{className:"mdocs-agent-choice-result muted",children:"已取消"}):null,s.status==="expired"?e.jsx("p",{className:"mdocs-agent-choice-result muted",children:"已超时,选择已失效"}):null,s.status==="failed"?e.jsx("p",{className:"mdocs-agent-choice-result muted",children:"提交失败或已失效"}):null,u?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mdocs-agent-choice-options",children:s.options.map(m=>e.jsx("button",{type:"button",className:"mdocs-agent-choice-option",disabled:g,onClick:()=>void y(m),children:m},m))}),e.jsxs("div",{className:"mdocs-agent-choice-custom",children:[e.jsx("input",{value:r,disabled:g,placeholder:"或输入自己的内容…",onChange:m=>f(m.target.value),onKeyDown:m=>{m.key==="Enter"&&(m.preventDefault(),y(r))}}),e.jsx("button",{type:"button",disabled:g||!r.trim(),onClick:()=>void y(r),children:"提交"})]}),e.jsx("div",{className:"mdocs-agent-choice-footer",children:e.jsx("button",{type:"button",className:"mdocs-agent-choice-cancel",disabled:g,onClick:()=>void R(),children:"取消"})})]}):null]})}function te(t){return t===""?[]:t.split(`
2
- `)}function We(t){return t.join(`
3
- `)}function ne(t,s){const i=te(t),r=te(s),f=i.length,g=r.length,p=Array.from({length:f+1},()=>Array.from({length:g+1},()=>0));for(let y=f-1;y>=0;y--)for(let R=g-1;R>=0;R--)i[y]===r[R]?p[y][R]=p[y+1][R+1]+1:p[y][R]=Math.max(p[y+1][R],p[y][R+1]);const N=[];let k=0,h=0;for(;k<f||h<g;)if(k<f&&h<g&&i[k]===r[h])N.push({kind:"eq",oi:k,ni:h}),k++,h++;else if(h<g&&(k===f||p[k][h+1]>=p[k+1][h]))N.push({kind:"ins",oi:k,ni:h}),h++;else if(k<f)N.push({kind:"del",oi:k,ni:h}),k++;else break;const j=[];let u=0,E=0;for(;u<N.length;){if(N[u].kind==="eq"){u++;continue}const y=N[u].oi,R=N[u].ni,F=[],m=[];let b=y,P=R;for(;u<N.length&&N[u].kind!=="eq";){const $=N[u];$.kind==="del"?(F.push(i[$.oi]),b=$.oi+1):(m.push(r[$.ni]),P=$.ni+1),u++}j.push({id:`h${E++}`,oldStart:y,oldEnd:b,newStart:R,newEnd:P,oldLines:F,newLines:m})}return j}function ze(t,s){const i=te(t);return We([...i.slice(0,s.oldStart),...s.newLines,...i.slice(s.oldEnd)])}function U(t){return t===""?[]:t.split(`
4
- `)}function z(t){return t.join(`
5
- `)}function Je(t,s){const i=U(t),r=[];let f=0;return s.forEach((g,p)=>{g.oldStart>f&&r.push({kind:"same",lines:i.slice(f,g.oldStart),key:`s-${f}-${g.oldStart}`}),r.push({kind:"hunk",hunk:g,hunkIndex:p,key:`h-${g.id}-${p}`}),f=g.oldEnd}),f<i.length?r.push({kind:"same",lines:i.slice(f),key:`s-${f}-end`}):s.length===0&&i.length===0&&r.push({kind:"same",lines:[],key:"s-empty"}),r}function Ge(t){const s=[],i=[];for(const r of t)r.kind==="same"?s.push(z(r.lines)):i.push(z(r.hunk.newLines));return{sameTexts:s,newTexts:i}}function me(t,s){const i=[];let r=0,f=0;for(const g of t)if(g.kind==="same"){const p=s.sameTexts[r++]??"";i.push(...U(p))}else{const p=s.newTexts[f++]??"";i.push(...U(p))}return z(i)}function Ue(t){return t.oldLines.length===0?"add":t.newLines.length===0?"del":"mod"}function he(t){const s=Math.max(1,U(t.value).length);return e.jsxs("div",{className:`mdocs-ai-write-line ${t.className}`,children:[e.jsx("span",{className:"mdocs-ai-write-rail","aria-hidden":!0}),e.jsx("textarea",{className:"mdocs-ai-write-inline-edit",value:t.value,rows:s,spellCheck:!1,"aria-label":t["aria-label"],onChange:i=>t.onChange(i.target.value),onBlur:()=>t.onBlurFlush()})]})}function Ve(t){const s=!!t.sending,[i,r]=c.useState(0),[f,g]=c.useState(null),[p,N]=c.useState({sameTexts:[],newTexts:[]}),[k,h]=c.useState([]),j=c.useRef(!1),u=c.useRef(!1),E=c.useRef([]),y=c.useRef(p);y.current=p,E.current=k;const R=c.useMemo(()=>t.proposedMd==null||t.proposedMd===t.currentMd?[]:ne(t.currentMd,t.proposedMd),[t.currentMd,t.proposedMd]),F=!s&&(k.length>0?k.some(l=>l.kind==="hunk"):R.length>0);c.useEffect(()=>{if(s){h([]),j.current=!1,u.current=!1;return}if(t.proposedMd==null||t.proposedMd===t.currentMd){h([]),j.current=!1,u.current=!1;return}if(u.current){u.current=!1;return}const l=ne(t.currentMd,t.proposedMd);if(l.length===0){h([]);return}const x=Je(t.currentMd,l);h(x),j.current=!1,N(Ge(x))},[s,t.proposedMd,t.currentMd]);const m=c.useMemo(()=>k.filter(l=>l.kind==="hunk").map(l=>l.hunk),[k]);c.useEffect(()=>{if(m.length===0){r(0);return}r(l=>Math.min(l,m.length-1))},[m.length]);const b=f??i;function P(){return me(E.current,y.current)}function $(){if(!F||E.current.length===0)return t.proposedMd;const l=P();return j.current=!1,l===t.currentMd?(u.current=!1,t.onProposedChange(null),null):(l!==t.proposedMd&&(u.current=!0,t.onProposedChange(l)),l)}function H(l){const x=E.current.find(D=>D.kind==="hunk"&&D.hunk.id===l.id);if(!x)return;const S=y.current.newTexts[x.hunkIndex],I={...l,newLines:S!=null?U(S):l.newLines},v=P();if(j.current=!1,u.current=!1,v===t.currentMd){t.onProposedChange(null);return}const T=ze(t.currentMd,I);t.onCurrentChange(T),v===T?t.onProposedChange(null):t.onProposedChange(v)}function K(l){const x=E.current.find(v=>v.kind==="hunk"&&v.hunk.id===l.id);if(!x)return;const S={sameTexts:y.current.sameTexts.slice(),newTexts:y.current.newTexts.slice()};S.newTexts[x.hunkIndex]=z(l.oldLines);const I=me(E.current,S);j.current=!1,u.current=!1,I===t.currentMd?t.onProposedChange(null):t.onProposedChange(I)}function A(){const l=P();j.current=!1,u.current=!1,t.onCurrentChange(l),t.onProposedChange(null)}function J(){j.current=!1,u.current=!1,t.onProposedChange(null)}function C(l){m.length!==0&&r(x=>{const S=(x+l+m.length)%m.length,I=document.getElementById(`mdocs-ai-hunk-${S}`);return I==null||I.scrollIntoView({block:"center",behavior:"smooth"}),S})}function W(l,x){j.current=!0,N(S=>{const I=S.sameTexts.slice();return I[l]=x,{...S,sameTexts:I}})}function V(l,x){j.current=!0,N(S=>{const I=S.newTexts.slice();return I[l]=x,{...S,newTexts:I}})}const q=m[b]??null;let G=0;return e.jsxs("div",{className:"mdocs-ai-write-md-pane",children:[e.jsxs("div",{className:"mdocs-ai-write-diff-toolbar",children:[e.jsx("span",{className:"mdocs-ai-write-diff-count",children:s?"接收中·编辑我的稿":m.length===0?t.proposedMd?"与提案一致(可编辑)":"Markdown 源码(可编辑)":`${m.length} 处变更(可改提案)`}),!s&&m.length>0?e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",className:"mdocs-ai-write-diff-accept-all",onClick:A,children:"全部接受"}),e.jsx("button",{type:"button",className:"mdocs-ai-write-diff-reject-all",onClick:J,children:"全部拒绝"})]}):null]}),F&&k.length>0?e.jsxs("div",{className:"mdocs-ai-write-inline",role:"document",children:[k.map(l=>{if(l.kind==="same"){const T=G++,D=p.sameTexts[T]??z(l.lines);return e.jsx("div",{className:"mdocs-ai-write-inline-same",children:e.jsx(he,{className:"ctx",value:D,"aria-label":"提案相同行",onChange:Z=>W(T,Z),onBlurFlush:()=>$()})},l.key)}const x=l.hunk,S=Ue(x),I=l.hunkIndex===b,v=p.newTexts[l.hunkIndex]??z(x.newLines);return e.jsxs("div",{id:`mdocs-ai-hunk-${l.hunkIndex}`,className:`mdocs-ai-write-inline-hunk mdocs-ai-write-inline-hunk-${S}`+(I?" is-active":""),onMouseEnter:()=>g(l.hunkIndex),onMouseLeave:()=>g(null),onClick:()=>r(l.hunkIndex),children:[e.jsxs("div",{className:"mdocs-ai-write-hunk-hover",hidden:f!==l.hunkIndex,children:[e.jsx("button",{type:"button",className:"mdocs-ai-write-hunk-reject",onClick:T=>{T.stopPropagation(),K(x)},children:"拒绝"}),e.jsx("button",{type:"button",className:"mdocs-ai-write-hunk-accept",onClick:T=>{T.stopPropagation(),H(x)},children:"接受"})]}),e.jsx("pre",{className:"mdocs-ai-write-hunk-pre",children:x.oldLines.map((T,D)=>e.jsxs("div",{className:"mdocs-ai-write-line del mdocs-ai-write-line-readonly",children:[e.jsx("span",{className:"mdocs-ai-write-rail","aria-hidden":!0}),e.jsx("span",{className:"mdocs-ai-write-code",children:T||" "})]},`o${D}`))}),x.newLines.length>0||v!==""||S!=="del"?e.jsx(he,{className:"add",value:v,"aria-label":"提案新增行",onChange:T=>V(l.hunkIndex,T),onBlurFlush:()=>$()}):null]},l.key)}),q?e.jsxs("div",{className:"mdocs-ai-write-float-nav",role:"toolbar","aria-label":"变更导航",children:[e.jsx("button",{type:"button","aria-label":"上一段",onClick:()=>C(-1),children:"↑"}),e.jsxs("span",{children:[b+1," / ",m.length]}),e.jsx("button",{type:"button","aria-label":"下一段",onClick:()=>C(1),children:"↓"}),e.jsx("button",{type:"button",className:"mdocs-ai-write-hunk-reject",onClick:()=>K(q),children:"拒绝"}),e.jsx("button",{type:"button",className:"mdocs-ai-write-hunk-accept",onClick:()=>H(q),children:"接受"})]}):null]}):e.jsx("textarea",{className:"mdocs-ai-write-editor",value:t.currentMd,onChange:l=>t.onCurrentChange(l.target.value),spellCheck:!1,placeholder:`# 标题
6
-
7
- 在此编辑 Markdown 源码…`})]})}let fe=0;function ge(t){return fe+=1,`${t}-${Date.now()}-${fe}`}function Ye(t,s){const i=t.slice(),r=i[i.length-1];return(r==null?void 0:r.type)==="text"?i[i.length-1]={type:"text",text:r.text+s}:i.push({type:"text",text:s}),i}function Xe(t,s){const i=t.slice(),r=i[i.length-1];return(r==null?void 0:r.type)==="thinking"?i[i.length-1]={type:"thinking",text:r.text+s}:i.push({type:"thinking",text:s}),i}function Q(t,s){const i=s.trim();return i?[...t,{type:"tool",text:i}]:t}function Qe(t){return t.some(s=>s.type==="text"||s.type==="thinking"?s.text.length>0:!0)}function Ze(t){const s=new Date(t);if(Number.isNaN(s.getTime()))return"";const i=new Date;return s.getFullYear()===i.getFullYear()&&s.getMonth()===i.getMonth()&&s.getDate()===i.getDate()?s.toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit"}):s.toLocaleDateString(void 0,{month:"2-digit",day:"2-digit"})}function xe(t,s){return s.map((i,r)=>{const f=`h-${t}-${r}-${i.role}`;if(i.role==="user"){const g=i.skillNames&&i.skillNames.length>0?i.skillNames:i.skillIds&&i.skillIds.length>0?i.skillIds:void 0;return{id:f,role:"user",content:i.content,skillNames:g}}return{id:f,role:"assistant",blocks:i.content?[{type:"text",text:i.content}]:[]}})}function rt(t){const[s,i]=c.useState(t.initialMarkdown),[r,f]=c.useState(null),[g]=c.useState(t.initialMarkdown),[p,N]=c.useState(t.displayName||"未命名"),[k,h]=c.useState([]),[j,u]=c.useState(null),[E,y]=c.useState([]),[R,F]=c.useState(!1),[m,b]=c.useState(!1),[P,$]=c.useState(""),[H,K]=c.useState([]),[A,J]=c.useState(!1),[C,W]=c.useState(null),[V,q]=c.useState(null),[G,l]=c.useState(!1),[x,S]=c.useState(null),I=c.useRef(null),v=c.useRef(null),T=c.useRef(null),D=c.useRef(!0),[Z,ee]=c.useState(!1),se=120;function ie(n){return n.scrollHeight-n.scrollTop-n.clientHeight}function Y(){const n=T.current;n&&(n.scrollTop=n.scrollHeight,ee(!1))}function ke(){D.current&&Y()}function pe(){const n=T.current;if(!n)return;const o=ie(n);D.current=o<=48,ee(o>se)}function ye(){D.current=!0,Y()}c.useLayoutEffect(()=>{if(!t.open)return;ke();const n=T.current;n&&ee(ie(n)>se)},[k,t.open]);async function X(){F(!0);try{const n=await Le("coding",t.documentId);y(n.sessions),j||u(n.lastOpenedSessionId)}catch{}finally{F(!1)}}async function ae(){try{const n=await de();W(n)}catch{W({enabled:!1,skillsReady:!1,model:null})}}if(c.useEffect(()=>{var o;if(!t.open){b(!1),(o=v.current)==null||o.abort();return}i(t.initialMarkdown),f(null),N(t.displayName||"未命名"),$(""),q(null),b(!1);let n=!1;return(async()=>{try{const w=await de();n||W(w)}catch{n||W({enabled:!1,skillsReady:!1,model:null})}try{const w=await Me("coding",t.documentId);if(n)return;u(w.sessionId),h(xe(w.sessionId,w.messages))}catch{n||(u(null),h([]))}X()})(),()=>{n=!0}},[t.open,t.initialMarkdown,t.displayName,t.documentId]),c.useEffect(()=>()=>{var n;return(n=v.current)==null?void 0:n.abort()},[]),c.useEffect(()=>{t.open&&m&&X()},[m,t.open,t.documentId]),!t.open)return null;const le=(C==null?void 0:C.reason)==="missing_api_key",we=r!=null&&r!==s?ne(s,r):[];async function je(){var n;if(!A){(n=v.current)==null||n.abort(),q(null),b(!1),J(!1);try{const o=await $e("coding",t.documentId);u(o.sessionId),h([]),$(""),X()}catch(o){q(o instanceof Error?o.message:String(o))}}}async function Ne(n){var o;if(!A){if(n===j){b(!1);return}(o=v.current)==null||o.abort(),q(null);try{const w=await Ee(n,"coding",t.documentId);u(w.sessionId),h(xe(w.sessionId,w.messages)),$(""),b(!1)}catch(w){q(w instanceof Error?w.message:String(w))}}}async function re(){var O;const n=P.trim();if(!n||A)return;if(!(C!=null&&C.enabled)){q("请先配置 API Key");return}(O=v.current)==null||O.abort();const o=new AbortController;v.current=o;const w=ge("u"),M=ge("a");D.current=!0,h(_=>[..._,{id:w,role:"user",content:n,skillNames:H.length>0?[...H]:void 0},{id:M,role:"assistant",blocks:[]}]),$(""),J(!0),q(null),requestAnimationFrame(()=>{Y(),requestAnimationFrame(Y)});const L=_=>{h(a=>a.map(d=>d.id===M&&d.role==="assistant"?{...d,blocks:_(d.blocks)}:d))};try{let _=!1;await qe(n,{mode:"coding",skillNames:H.length>0?H:void 0,documentId:t.documentId,workingMarkdown:s,baseMarkdown:g,signal:o.signal,onEvent:a=>{a.type==="text_delta"?L(d=>Ye(d,a.text)):a.type==="thinking_delta"?L(d=>Xe(d,a.text)):a.type==="markdown_set"?f(a.markdown):a.type==="tool_notice"?L(d=>Q(d,a.text)):a.type==="document_table"?L(d=>Q(d,a.title)):a.type==="document_card"?L(d=>Q(d,`读取文档「${a.title}」`)):a.type==="sources"?a.items.length>0&&L(d=>Q(d,`已阅读 ${a.items.length} 个手册页面`)):a.type==="skill_form_card"?L(d=>[...d,{type:"skill_form_card",requestId:a.requestId,mode:a.mode,title:a.title,currentName:a.currentName,initialName:a.initialName,initialDescription:a.initialDescription,initialBody:a.initialBody,expiresAt:a.expiresAt,status:"open"}]):a.type==="choice_card"?(D.current=!0,L(d=>[...d,{type:"choice_card",requestId:a.requestId,title:a.title,options:a.options,expiresAt:a.expiresAt,status:"open"}])):a.type==="choice_expired"?L(d=>d.map(B=>B.type==="choice_card"&&B.requestId===a.requestId&&B.status==="open"?{...B,status:"expired"}:B)):a.type==="skill_form_expired"?L(d=>d.map(B=>B.type==="skill_form_card"&&B.requestId===a.requestId&&B.status==="open"?{...B,status:"expired"}:B)):a.type==="error"&&(_=!0,q(a.message))}}),!o.signal.aborted&&!_&&K([]),X()}catch(_){o.signal.aborted||q(_ instanceof Error?_.message:String(_))}finally{v.current===o&&(v.current=null),J(!1)}}function oe(n){return new Promise(o=>{I.current=o,S({message:n.message,confirmLabel:n.confirmLabel??"确定",cancelLabel:n.cancelLabel??"返回",danger:n.danger??!1})})}function ce(n){const o=I.current;I.current=null,S(null),o==null||o(n)}async function be(){if(!await oe({message:"是否回写",confirmLabel:"确认",cancelLabel:"返回"}))return;let o=s;we.length>0&&(o=r??s,i(o),f(null)),l(!0);const w=!t.documentId;try{const M=await t.onComplete({markdown:o,documentId:t.documentId,displayName:p.trim()||"未命名"}),L=M&&typeof M=="object"&&typeof M.documentId=="string"?M.documentId.trim():"";if(w&&L&&j)try{await Re(j,L)}catch{}t.onClose()}catch(M){if(M!=null&&M.silent||M instanceof Error&&M.message==="cancelled")return;q(M instanceof Error?M.message:String(M))}finally{l(!1)}}async function Ce(){var n;(s!==g||r!=null)&&!await oe({message:"取消帮写?未写回的修改将丢失。",confirmLabel:"放弃修改",danger:!0})||((n=v.current)==null||n.abort(),t.onClose())}return e.jsxs("div",{className:"mdocs-ai-write-overlay",role:"dialog","aria-modal":"true","aria-label":"帮写",children:[e.jsxs("header",{className:"mdocs-ai-write-header",children:[e.jsxs("div",{className:"mdocs-ai-write-header-left",children:[e.jsx("img",{src:Ae,alt:"",className:"mdocs-ai-write-title-logo"}),e.jsx("strong",{children:"帮写"}),e.jsx("input",{className:"mdocs-ai-write-title",value:p,onChange:n=>N(n.target.value),"aria-label":"文档标题"})]}),e.jsxs("div",{className:"mdocs-ai-write-header-actions",children:[e.jsx("button",{type:"button",onClick:()=>void Ce(),disabled:G||x!=null,children:"取消"}),e.jsx("button",{type:"button",className:"mdocs-ai-write-primary",onClick:()=>void be(),disabled:G||x!=null,children:G?"写回中…":"完成并写回"})]})]}),e.jsxs("div",{className:"mdocs-ai-write-body",children:[e.jsxs("section",{className:"mdocs-ai-write-chat",children:[e.jsxs("div",{className:"mdocs-ai-write-chat-toolbar",children:[e.jsx("span",{className:"mdocs-ai-write-chat-toolbar-label",children:"对话"}),e.jsxs("div",{className:"mdocs-ai-write-chat-toolbar-actions",children:[e.jsx("button",{type:"button",className:"mdocs-agent-panel-icon-btn",title:"新会话","aria-label":"新会话",disabled:A,onClick:()=>void je(),children:e.jsx(Fe,{size:16,strokeWidth:1.8})}),e.jsx("button",{type:"button",className:"mdocs-agent-panel-icon-btn"+(m?" active":""),title:"历史会话","aria-label":"历史会话",onClick:()=>b(n=>!n),children:e.jsx(_e,{size:16,strokeWidth:1.8})})]})]}),m?e.jsxs("div",{className:"mdocs-ai-write-history mdocs-agent-panel-history",children:[e.jsxs("div",{className:"mdocs-agent-panel-history-head",children:[e.jsx("span",{children:"历史会话"}),e.jsx("button",{type:"button",className:"mdocs-agent-panel-history-back",onClick:()=>b(!1),children:"返回对话"})]}),R?e.jsx("p",{className:"mdocs-agent-panel-history-hint",children:"加载中…"}):E.length===0?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"mdocs-agent-panel-history-empty",children:"暂无历史会话"}),e.jsx("p",{className:"mdocs-agent-panel-history-hint",children:"发一条消息后会出现在这里"})]}):e.jsx("ul",{className:"mdocs-agent-panel-history-list",children:E.map(n=>e.jsx("li",{children:e.jsxs("button",{type:"button",className:"mdocs-agent-panel-history-item"+(n.id===j?" active":""),disabled:A,onClick:()=>void Ne(n.id),children:[e.jsx("span",{className:"mdocs-agent-panel-history-title",children:n.title}),e.jsx("span",{className:"mdocs-agent-panel-history-time",children:Ze(n.updatedAt)})]})},n.id))})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mdocs-ai-write-chat-scroll-wrap",children:[e.jsx("div",{className:"mdocs-ai-write-chat-messages",ref:T,onScroll:pe,children:k.length===0?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"mdocs-ai-write-hint",children:"描述你想写或改的内容。助手可读取右侧当前稿与进场快照,再更新提案;请按段接受或拒绝后完成写回。"}),le?e.jsx(ue,{onConfigured:ae}):null]}):k.map(n=>e.jsx("div",{className:"mdocs-ai-write-msg"+(n.role==="user"?" mdocs-ai-write-msg-user":""),children:n.role==="user"?e.jsxs(e.Fragment,{children:[n.skillNames&&n.skillNames.length>0?e.jsxs("div",{className:"mdocs-agent-msg-skills",children:["Skill ×",n.skillNames.length]}):null,n.content]}):Qe(n.blocks)?e.jsx("div",{className:"mdocs-ai-write-timeline",children:n.blocks.map((o,w)=>o.type==="thinking"?o.text?e.jsxs("details",{className:"mdocs-ai-write-thinking",open:A,children:[e.jsx("summary",{children:"思考中"}),e.jsx("pre",{className:"mdocs-ai-write-thinking-body",children:o.text})]},`${n.id}-think-${w}`):null:o.type==="tool"?e.jsx("p",{className:"mdocs-agent-panel-tool-notice",children:o.text},`${n.id}-tool-${w}`):o.type==="skill_form_card"?e.jsx(Pe,{block:o,onResolved:(M,L,O)=>{h(_=>_.map(a=>a.id!==n.id||a.role!=="assistant"?a:{...a,blocks:a.blocks.map(d=>d.type==="skill_form_card"&&d.requestId===M?{...d,status:L,submittedName:L==="submitted"?O:d.submittedName}:d)}))}},`${n.id}-sf-${w}`):o.type==="choice_card"?e.jsx(Oe,{block:o,onResolved:(M,L,O)=>{h(_=>_.map(a=>a.id!==n.id||a.role!=="assistant"?a:{...a,blocks:a.blocks.map(d=>d.type==="choice_card"&&d.requestId===M?{...d,status:O,selected:O==="selected"?L:d.selected}:d)}))}},`${n.id}-ch-${w}`):o.type==="text"&&o.text?e.jsx("div",{className:"mdocs-agent-panel-md",children:e.jsx(He,{remarkPlugins:[Ke],children:o.text})},`${n.id}-text-${w}`):null)}):A?e.jsxs("span",{className:"mdocs-agent-panel-thinking","aria-label":"思考中",children:[e.jsx("span",{}),e.jsx("span",{}),e.jsx("span",{})]}):null},n.id))}),Z?e.jsxs("button",{type:"button",className:"mdocs-chat-jump-bottom",onClick:ye,title:"回到底部","aria-label":"回到底部",children:[e.jsx(De,{size:14,strokeWidth:2,"aria-hidden":!0}),e.jsx("span",{children:"置底"})]}):null]}),V?e.jsx("p",{className:"mdocs-ai-write-error",children:V}):null,le&&k.length>0?e.jsx(ue,{onConfigured:ae}):null,e.jsxs("div",{className:"mdocs-ai-write-input-row",children:[e.jsxs("div",{style:{flex:1,display:"flex",flexDirection:"column",gap:6},children:[e.jsx(Be,{selectedNames:H,onChange:K,disabled:A||!(C!=null&&C.enabled)}),e.jsx("textarea",{rows:2,value:P,disabled:A||!(C!=null&&C.enabled),placeholder:C!=null&&C.enabled?"说说要写什么…":"请先配置 API Key",onChange:n=>$(n.target.value),onKeyDown:n=>{n.key==="Enter"&&!n.shiftKey&&(n.preventDefault(),re())}})]}),e.jsx("button",{type:"button",className:A?"mdocs-ai-write-stop":void 0,disabled:A?!1:!(C!=null&&C.enabled),onClick:()=>{var n;if(A){(n=v.current)==null||n.abort();return}re()},children:A?"停止":"发送"})]})]})]}),e.jsx(Ve,{currentMd:s,proposedMd:r,sending:A,onCurrentChange:i,onProposedChange:f})]}),x?e.jsx(Te,{message:x.message,confirmLabel:x.confirmLabel,cancelLabel:x.cancelLabel,danger:x.danger,onConfirm:()=>ce(!0),onCancel:()=>ce(!1)}):null]})}export{rt as AiWriteWorkbench};
@@ -1 +0,0 @@
1
- import{r as a,j as e}from"./react-vendor-BuABiQkA.js";import{c as ie,t as bs,v as Ns,p as vs,l as Ae,w as ks,x as ws,y as Q,z as Pe,A as Ss,B as Cs,G as Ds,a as Is,d as Oe,H as Ye,I as qe,J as Ts,K as As,L as Ps,O as Ms,Q as Es,T as Xe,U as Ie,V as Rs,W as Ls,X as Fs,Y as zs,Z as Bs,_ as $s,f as Qe,$ as Ks,a0 as Os,a1 as Vs,a2 as Us,a3 as Ws,a4 as es,a5 as _s,a6 as Gs,a7 as Hs,a8 as Js,a9 as Zs,aa as Ys,ab as qs,ac as Xs,ad as Qs,ae as et,af as st,ag as tt,ah as at,P as nt,ai as it,aj as ct,ak as lt,al as ot,r as rt,j as dt,am as mt,k as ut,m as ht}from"./index-DUu4Is5p.js";import{V as ss,a as pt,M as Ve}from"./VisitorPickerModal-CPjKg8ox.js";import{C as gt}from"./check-CQSGOkW1.js";import"./ui-CvA4pq3d.js";import"./motion-DFABYlwt.js";import"./diagram-Bk84WZto.js";function ft({draft:t,onClose:s,onSuccess:j}){const{t:d,lang:p}=ie(),[B,u]=a.useState([]),[w,N]=a.useState(t.domainId??""),[g,k]=a.useState([]),[T,S]=a.useState(""),[c,x]=a.useState(`${t.displayName||"untitled"}_recovered.md`),[o,h]=a.useState(!1),[I,A]=a.useState(null),y=a.useRef(null),M=a.useRef(!0);a.useEffect(()=>(M.current=!0,()=>{M.current=!1}),[]),a.useEffect(()=>{bs().then(v=>{M.current&&u(v)})},[]),a.useEffect(()=>{w&&(k([]),S(""),Ns(w).then(v=>{M.current&&k(v)}).catch(()=>{M.current&&k([])}))},[w]),a.useEffect(()=>{const v=window.setTimeout(()=>{var P;return(P=y.current)==null?void 0:P.focus()},0);return()=>window.clearTimeout(v)},[]),a.useEffect(()=>{const v=P=>{P.key==="Escape"&&s()};return window.addEventListener("keydown",v),()=>window.removeEventListener("keydown",v)},[s]);function z(v,P){for(const C of v){if(C.type==="folder"&&C.path===P)return C.documentId;if(C.type==="folder"){const E=z(C.children,P);if(E)return E}}return null}async function F(v){v.preventDefault(),h(!0),A(null);const P=c.replace(/\s+/g,"_").replace(/[^a-zA-Z0-9_\-.]/g,""),C=P.endsWith(".md")?P:P+".md",E=C.replace(/\.md$/i,"");try{const m=T?`${T}/${C}`:C,l=T?z(g,T)??void 0:void 0;await ws({fileName:C,displayName:E,content:t.content,domainId:w,parentId:l}),j()}catch(m){A(Q(d,m))}finally{h(!1)}}return e.jsx("div",{className:"mdocs-dialog-backdrop",role:"presentation",onMouseDown:v=>{v.target===v.currentTarget&&!o&&s()},children:e.jsxs("div",{className:"mdocs-dialog card mdocs-recovery-dialog",role:"dialog","aria-modal":"true",children:[e.jsx("h1",{children:d("recoverDraft")}),e.jsx("p",{className:"muted",children:d("recoverDraftDesc",{name:t.displayName||d("unknownTitle")})}),e.jsxs("form",{onSubmit:F,className:"mdocs-dialog-form",children:[e.jsxs("label",{className:"mdocs-dialog-label",children:[d("domainLabel"),B.length>0&&e.jsx(vs,{domains:B,value:w,onChange:N,onDomainsChange:u,ariaLabel:d("domainLabel"),localizeName:v=>Ae(v,p,d)})]}),e.jsxs("label",{className:"mdocs-dialog-label mdocs-recovery-tree-label",children:[d("recoverTargetFolder"),e.jsx("div",{className:"mdocs-recovery-tree",children:e.jsx(ks,{nodes:g,activeDocumentId:null,selectedParentPath:T,onOpen:()=>{},onOpenFolder:v=>S(v),onContextMenu:()=>{},onDeselect:()=>S("")})})]}),e.jsxs("label",{className:"mdocs-dialog-label",children:[d("fileNameLabel"),e.jsx("input",{ref:y,value:c,onChange:v=>x(v.target.value),placeholder:"recovered.md",maxLength:200,disabled:o})]}),I&&e.jsx("div",{className:"mdocs-dialog-error",children:I}),e.jsxs("div",{className:"mdocs-dialog-actions",children:[e.jsx("button",{type:"button",onClick:()=>!o&&s(),disabled:o,children:d("cancel")}),e.jsx("button",{type:"submit",className:"primary",disabled:o,children:d(o?"creating":"create")})]})]})]})})}function xt({onPublish:t,onClose:s,onCountChange:j,onRecover:d}){const{t:p}=ie(),[B,u]=a.useState([]),[w,N]=a.useState(new Set),[g,k]=a.useState(!1),[T,S]=a.useState(null),[c,x]=a.useState(null),[o,h]=a.useState(null),I=a.useRef(null),A=a.useRef(!0);a.useEffect(()=>(A.current=!0,Pe().then(l=>{if(!A.current)return;const f=l.filter(D=>!D.published);u(f),j==null||j(f.length)}),()=>{A.current=!1,I.current&&(clearTimeout(I.current),I.current=null)}),[]),a.useEffect(()=>{const l=f=>{f.key==="Escape"&&s()};return window.addEventListener("keydown",l),()=>window.removeEventListener("keydown",l)},[s]);const y=a.useCallback((l,f)=>{h({message:l,type:f}),I.current&&clearTimeout(I.current),I.current=setTimeout(()=>h(null),3e3)},[]);async function M(l){await Cs(l),N(f=>new Set(f).add(l));try{await t(l),u(f=>{const D=f.filter($=>$.documentId!==l);return j==null||j(D.length),D}),y(p("published"),"success")}catch(f){f instanceof Ds||y(p("publishFailed"),"error");const D=await Is(l);D&&u($=>$.map(U=>U.documentId===l?D:U))}finally{N(f=>{const D=new Set(f);return D.delete(l),D})}}async function z(l){S(null),await Oe(l),u(f=>{const D=f.filter($=>$.documentId!==l);return j==null||j(D.length),D}),y(p("draftDeleted"),"success")}function F(l){const f=B.find(D=>D.documentId===l);f&&x(f)}function v(l){x(null),Oe(l),u(f=>{const D=f.filter($=>$.documentId!==l);return j==null||j(D.length),D}),d==null||d(B.find(f=>f.documentId===l)),y(p("published"),"success")}async function P(){k(!0);let l=0,f=0;for(const U of B)try{await t(U.documentId),l++}catch{f++}const $=(await Pe()).filter(U=>!U.published);u($),j==null||j($.length),f===0?y(p("publishAll"),"success"):y(`${l} published, ${f} failed`,"error"),k(!1)}const C=l=>new Date(l).toLocaleString(void 0,{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit"}),E=l=>Ss(p,l,l.publishError??"UNKNOWN"),m=B;return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mdocs-drawer-overlay",onClick:s}),e.jsxs("div",{className:"mdocs-drawer",role:"dialog","aria-modal":"true",children:[e.jsxs("header",{className:"mdocs-drawer-header",children:[e.jsxs("div",{className:"mdocs-drawer-header-text",children:[e.jsxs("h2",{className:"mdocs-drawer-title",children:[p("unpublishedDrafts"),m.length>0&&e.jsx("span",{className:"mdocs-drawer-count-badge",children:m.length})]}),e.jsx("span",{className:"mdocs-drawer-subtitle",children:p("draftCount",{count:String(m.length)})})]}),e.jsx("button",{type:"button",className:"mdocs-drawer-close-btn",onClick:s,"aria-label":p("close"),children:"×"})]}),e.jsx("div",{className:"mdocs-drawer-body",children:m.length===0?e.jsxs("div",{className:"mdocs-drawer-empty",children:[e.jsx("div",{className:"mdocs-drawer-empty-icon","aria-hidden":"true",children:"✓"}),e.jsx("p",{className:"mdocs-drawer-empty-text",children:p("draftsEmptyState")})]}):e.jsx("div",{className:"mdocs-drawer-items",children:m.map(l=>{const f=!!l.publishError;return e.jsxs("div",{className:f?"mdocs-drawer-item mdocs-drawer-item-failed":"mdocs-drawer-item",children:[e.jsx("div",{className:"mdocs-drawer-item-icon","aria-hidden":"true",children:"📄"}),e.jsxs("div",{className:"mdocs-drawer-item-info",children:[e.jsx("span",{className:"mdocs-drawer-item-name",children:l.displayName||p("unknownTitle")}),f?e.jsxs("span",{className:"mdocs-drawer-item-error",children:[C(l.publishErrorAt)," · ",E(l)]}):e.jsxs("span",{className:"mdocs-drawer-item-meta",children:[C(l.updatedAt)," · ",p("localSnapshot")]})]}),e.jsxs("div",{className:"mdocs-drawer-item-actions",children:[f?e.jsx("button",{type:"button",className:"mdocs-btn-ghost mdocs-btn-ghost-primary",disabled:g,onClick:()=>F(l.documentId),children:p("recoverDraft")}):e.jsx("button",{type:"button",className:"mdocs-btn-ghost mdocs-btn-ghost-primary",disabled:w.has(l.documentId)||g,onClick:()=>void M(l.documentId),children:w.has(l.documentId)?p("publishing"):p("publish")}),e.jsx("button",{type:"button",className:"mdocs-btn-ghost mdocs-btn-ghost-danger",disabled:g,onClick:()=>S(l.documentId),children:p("delete")})]})]},l.documentId)})})}),e.jsxs("footer",{className:"mdocs-drawer-footer",children:[e.jsx("button",{type:"button",onClick:s,children:p("close")}),e.jsx("button",{type:"button",className:"primary",disabled:m.length===0||g,onClick:()=>void P(),children:g?p("publishing"):`${p("publishAll")} (${m.length})`})]}),o&&e.jsx("div",{className:"mdocs-drawer-toast mdocs-drawer-toast--"+o.type,role:"status",children:o.message})]}),T&&e.jsx("div",{className:"mdocs-dialog-backdrop",role:"presentation",onMouseDown:l=>{l.target===l.currentTarget&&S(null)},children:e.jsxs("div",{className:"mdocs-dialog card",role:"dialog","aria-modal":"true",children:[e.jsx("h1",{children:p("delete")}),e.jsx("p",{className:"muted",children:p("confirmDeleteDraft")}),e.jsxs("div",{className:"mdocs-dialog-actions",children:[e.jsx("button",{type:"button",onClick:()=>S(null),children:p("cancel")}),e.jsx("button",{type:"button",className:"danger",onClick:()=>void z(T),children:p("delete")})]})]})}),c&&e.jsx(ft,{draft:c,onClose:()=>x(null),onSuccess:()=>v(c.documentId)})]})}function jt(){const{t,lang:s}=ie(),j=Ye(),d=a.useRef(!1),[p,B]=a.useState([]),[u,w]=a.useState(!1),[N,g]=a.useState(""),[k,T]=a.useState("restricted"),[S,c]=a.useState(!1),[x,o]=a.useState(null),[h,I]=a.useState(null),[A,y]=a.useState(""),[M,z]=a.useState(""),[F,v]=a.useState("all"),[P,C]=a.useState(null);a.useEffect(()=>(d.current=!0,()=>{d.current=!1}),[]);const E=a.useCallback(async()=>{w(!0),o(null);try{const b=await qe();if(!d.current)return;B(b)}catch(b){if(!d.current)return;o(Q(t,b))}finally{d.current&&w(!1)}},[t]);a.useEffect(()=>{E()},[E]);async function m(b){if(b.preventDefault(),!N.trim()){o(t("domainNameRequired"));return}c(!0),o(null);try{if(await Es({domainName:N.trim(),permission:k}),!d.current)return;g(""),T("restricted"),await E()}catch(K){if(!d.current)return;o(Q(t,K))}finally{d.current&&c(!1)}}async function l(b){if(A.trim()){o(null);try{if(await Ms(b,A.trim()),!d.current)return;I(null),C(null),await E()}catch(K){if(!d.current)return;o(Q(t,K))}}}function f(b){I(b.domainId),y(b.domainName),C(null)}function D(){I(null),C(null)}async function $(b,K){o(null);try{if(await Ps(b,K),!d.current)return;C(null),await E()}catch(W){if(!d.current)return;o(Q(t,W))}}async function U(b){if(window.confirm(t("deleteDomainConfirmDetail",{name:b.domainName,count:String(b.docCount)}))){o(null);try{if(await As(b.domainId),!d.current)return;await E()}catch(K){if(!d.current)return;o(Q(t,K))}}}const Z=a.useCallback(b=>Ts(b,t),[t]),Y=a.useCallback(b=>Ae(b.domainName,s,t),[s,t]),q=a.useMemo(()=>{const b=M.trim().toLowerCase();return p.filter(K=>F!=="all"&&K.permission!==F?!1:b?Ae(K.domainName,s,t).toLowerCase().includes(b)||K.domainName.toLowerCase().includes(b):!0)},[p,M,F,s,t]),_=a.useCallback((b,K)=>{const W=Z(b.permission);return K?`${W} ${t("domainTypeBuiltinSuffix")}`:W},[Z,t]);return{domains:p,loadingDomains:u,domainError:x,newDomainName:N,setNewDomainName:g,newDomainPermission:k,setNewDomainPermission:T,creating:S,handleCreateDomain:m,renamingId:h,setRenamingId:I,renameDraft:A,setRenameDraft:y,startRename:f,cancelRename:D,handleRename:l,changeTypeForId:P,setChangeTypeForId:C,handlePermissionChange:$,handleDelete:U,domainSearch:M,setDomainSearch:z,domainFilter:F,setDomainFilter:v,filteredDomains:q,visitorId:j,plabel:Z,localizeDomain:Y,domainTypeLabel:_}}const Ue={private:0,restricted:1,public:2};function We(t){return Object.prototype.hasOwnProperty.call(Ue,t)?Ue[t]:null}function _e(t,s){const j=We(t),d=We(s);return j===null||d===null?"invalid":d>j?"upgrade":d<j?"downgrade":"same"}function yt(){const{t}=ie(),s=jt(),[j,d]=a.useState([]),[p,B]=a.useState(null),[u,w]=a.useState(""),N=a.useRef(null),[g,k]=a.useState(null),T=a.useCallback(async()=>{try{const c=await Xe();d(c)}catch{d([])}},[]);a.useEffect(()=>{T()},[T]),a.useEffect(()=>{if(!s.changeTypeForId)return;const c=x=>{var o;(o=N.current)!=null&&o.contains(x.target)||s.setChangeTypeForId(null)};return document.addEventListener("mousedown",c),()=>document.removeEventListener("mousedown",c)},[s.changeTypeForId,s.setChangeTypeForId]);async function S(c){B(null);try{const x=await $s(c.domainId);k({domainId:c.domainId,creatorVisitorId:c.creatorVisitorId,initialIds:x.map(o=>o.visitorId),memberRows:x})}catch(x){B({kind:"err",text:translateError(t,x)})}}return e.jsxs("div",{className:"mdocs-settings",children:[e.jsx("div",{className:"mdocs-settings-header",children:e.jsx("h2",{className:"mdocs-settings-title",children:t("domainManagement")})}),e.jsx("p",{className:"mdocs-domain-mgmt-restricted-hint",children:t("domainMgmtRestrictedMemberHint")}),e.jsxs("div",{className:"mdocs-settings-cards",children:[e.jsxs("div",{className:"mdocs-settings-card mdocs-domain-mgmt-create",children:[e.jsx("div",{className:"mdocs-settings-card-title",children:t("createDomain")}),e.jsxs("form",{onSubmit:s.handleCreateDomain,className:"mdocs-domain-mgmt-create-form",children:[e.jsxs("label",{className:"mdocs-domain-mgmt-field",children:[e.jsx("span",{className:"mdocs-domain-mgmt-label",children:t("domainName")}),e.jsx("input",{value:s.newDomainName,onChange:c=>s.setNewDomainName(c.target.value),placeholder:t("domainName"),maxLength:100,disabled:s.creating,className:"mdocs-domain-name-input"})]}),e.jsxs("fieldset",{className:"mdocs-domain-mgmt-fieldset",children:[e.jsx("legend",{className:"mdocs-domain-mgmt-label",children:t("domainPermission")}),e.jsx("div",{className:"mdocs-domain-mgmt-type-radios",children:Ie.map(c=>e.jsxs("label",{className:"mdocs-domain-mgmt-radio",children:[e.jsx("input",{type:"radio",name:"newDomainPermission",checked:s.newDomainPermission===c,onChange:()=>s.setNewDomainPermission(c),disabled:s.creating}),e.jsx("span",{children:s.plabel(c)})]},c))})]}),e.jsx("button",{type:"submit",className:"primary mdocs-domain-mgmt-submit",disabled:s.creating,children:s.creating?t("creating"):t("create")})]})]}),p&&e.jsx("div",{className:"mdocs-settings-card",style:{color:p.kind==="ok"?"var(--mdocs-success, #0a7)":"var(--mdocs-danger)"},children:e.jsx("span",{className:"mdocs-settings-item-desc",children:p.text})}),s.domainError&&e.jsx("div",{className:"mdocs-settings-card mdocs-domain-error",children:e.jsx("span",{className:"mdocs-settings-item-desc",style:{color:"var(--mdocs-danger)"},children:s.domainError})}),e.jsxs("div",{className:"mdocs-settings-card mdocs-domain-mgmt-toolbar",children:[e.jsx("input",{type:"search",value:s.domainSearch,onChange:c=>s.setDomainSearch(c.target.value),placeholder:t("domainSearchPlaceholder"),className:"mdocs-domain-mgmt-search","aria-label":t("domainSearchPlaceholder")}),e.jsx("div",{className:"mdocs-domain-mgmt-filters",role:"group","aria-label":t("domainPermission"),children:["all",...Ie].map(c=>e.jsx("button",{type:"button",className:"mdocs-domain-mgmt-filter"+(s.domainFilter===c?" active":""),"aria-pressed":s.domainFilter===c,onClick:()=>s.setDomainFilter(c),children:c==="all"?t("domainFilterAll"):s.plabel(c)},c))})]}),s.loadingDomains?e.jsx("div",{className:"mdocs-settings-card",children:e.jsx("span",{className:"mdocs-settings-item-desc",children:t("loading")})}):s.domains.length===0?e.jsx("div",{className:"mdocs-settings-card mdocs-settings-draft-empty",children:e.jsx("span",{className:"mdocs-settings-item-desc",children:t("noDomains")})}):s.filteredDomains.length===0?e.jsx("div",{className:"mdocs-settings-card mdocs-settings-draft-empty",children:e.jsx("span",{className:"mdocs-settings-item-desc",children:t("domainNoMatch")})}):e.jsx("div",{className:"mdocs-settings-card mdocs-domain-table-wrap",children:e.jsxs("table",{className:"mdocs-domain-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",children:t("domainColName")}),e.jsx("th",{scope:"col",children:t("domainColType")}),e.jsx("th",{scope:"col",className:"mdocs-domain-table-num",children:t("domainColDocCount")}),e.jsx("th",{scope:"col",children:t("domainColActions")})]})}),e.jsx("tbody",{children:s.filteredDomains.map(c=>{const x=Rs(c,s.visitorId),o=Ls(c.domainId),h=Fs(c),I=_e(c.permission,"public")==="upgrade",A=I?h?t("domainTooltipTypeLocked"):void 0:t("domainPermissionAlreadyTop");return e.jsxs("tr",{className:o?"mdocs-domain-table-row-builtin":void 0,children:[e.jsx("td",{children:s.renamingId===c.domainId?e.jsxs("div",{className:"mdocs-domain-table-rename",children:[e.jsx("input",{value:s.renameDraft,onChange:y=>s.setRenameDraft(y.target.value),maxLength:100,autoFocus:!0,className:"mdocs-domain-name-input"}),e.jsx("button",{type:"button",className:"primary",onClick:()=>void s.handleRename(c.domainId),children:t("save")}),e.jsx("button",{type:"button",onClick:s.cancelRename,children:t("cancel")})]}):e.jsxs("div",{className:"mdocs-domain-table-name-cell",children:[e.jsx("span",{className:"mdocs-domain-table-name",children:s.localizeDomain(c)}),o&&e.jsx("span",{className:"mdocs-domain-badge-builtin",children:t("domainBuiltIn")}),!x&&!o&&e.jsx("span",{className:"mdocs-domain-badge-shared",children:t("shared")})]})}),e.jsx("td",{children:e.jsx("span",{className:"mdocs-domain-table-type",title:A,children:e.jsx("span",{className:"mdocs-domain-permission-badge","data-permission":c.permission,children:s.domainTypeLabel(c,o)})})}),e.jsx("td",{className:"mdocs-domain-table-num",children:c.docCount}),e.jsxs("td",{className:"mdocs-domain-table-actions",children:[o&&e.jsx("span",{className:"mdocs-domain-not-modifiable",children:t("domainNotModifiable")}),!o&&!x&&e.jsx("span",{className:"mdocs-domain-not-creator-inline",children:t("domainNotCreator")}),!o&&x&&s.changeTypeForId===c.domainId&&e.jsxs("div",{className:"mdocs-domain-change-type-panel",ref:N,children:[e.jsx("div",{className:"mdocs-domain-type-segment",role:"radiogroup","aria-label":t("domainChangeType"),children:Ie.map(y=>{const z=_e(c.permission,y)==="downgrade";return e.jsx("span",{title:z?t("domainPermissionNoDowngrade"):void 0,children:e.jsx("button",{type:"button",role:"radio","aria-checked":u===y,className:u===y?"active":"",disabled:z,onClick:()=>w(y),children:s.plabel(y)})},y)})}),e.jsx("button",{type:"button",className:"mdocs-domain-type-icon confirm","aria-label":t("save"),disabled:u===""||u===c.permission,onClick:()=>{u&&u!==c.permission&&s.handlePermissionChange(c.domainId,u)},children:e.jsx(gt,{size:14,strokeWidth:2.5})}),e.jsx("button",{type:"button",className:"mdocs-domain-type-icon cancel","aria-label":t("cancel"),onClick:()=>s.setChangeTypeForId(null),children:e.jsx(zs,{size:14,strokeWidth:2.5})})]}),!o&&x&&s.renamingId!==c.domainId&&s.changeTypeForId!==c.domainId&&e.jsxs("div",{className:"mdocs-domain-action-buttons",children:[c.permission==="restricted"&&e.jsx("button",{type:"button",className:"mdocs-domain-action mdocs-domain-action--primary",onClick:()=>void S(c),children:t("domainMembersManageButton")}),e.jsx("button",{type:"button",className:"mdocs-domain-action",title:h?t("domainLocked"):void 0,onClick:()=>{s.setChangeTypeForId(null),s.startRename(c)},disabled:h,children:t("rename")}),e.jsx("button",{type:"button",className:"mdocs-domain-action",title:A,onClick:()=>{s.setRenamingId(null),w(c.permission),s.setChangeTypeForId(c.domainId)},disabled:!I,children:t("domainChangeType")}),e.jsx("button",{type:"button",className:"mdocs-domain-action mdocs-domain-action--danger",title:h?t("domainLocked"):void 0,onClick:()=>void s.handleDelete(c),disabled:h,children:t("deleteDomain")})]})]})]},c.domainId)})})]})})]}),e.jsx(ss,{open:g!==null,title:t("domainMembersPickerTitle"),initialSelectedIds:(g==null?void 0:g.initialIds)??[],lockedIds:g?[g.creatorVisitorId]:[],seedMembers:g==null?void 0:g.memberRows,templates:j,onClose:()=>k(null),onConfirm:async c=>{g&&(await Bs(g.domainId,c),B({kind:"ok",text:t("domainMembersSaved",{count:String(c.length)})}))}})]})}function Ge(t){return t.length<=6?t:`${t.slice(0,6)}…`}function bt(){const{t}=ie(),s=a.useRef(!0),[j,d]=a.useState([]),[p,B]=a.useState(!0),[u,w]=a.useState(null),[N,g]=a.useState(1),[k,T]=a.useState(null),[S,c]=a.useState(""),[x,o]=a.useState([]),[h,I]=a.useState(!1),[A,y]=a.useState(null),[M,z]=a.useState(!1),F=Ye(),[v,P]=a.useState([]),[C,E]=a.useState("idle"),[m,l]=a.useState("");a.useEffect(()=>(s.current=!0,()=>{s.current=!1}),[]),a.useEffect(()=>{E("loading"),Qe().then(r=>{s.current&&(P(r),E("ok"))}).catch(()=>{s.current&&E("err")})},[]);const f=a.useMemo(()=>{const r=new Map;for(const R of v)r.set(R.visitorId,R.visitorName);return r},[v]),D=a.useCallback(async()=>{B(!0),y(null);try{const r=await Xe();s.current&&d(r)}catch(r){s.current&&y(Q(t,r))}finally{s.current&&B(!1)}},[t]);a.useEffect(()=>{D()},[D]);const $=a.useMemo(()=>x.map(r=>{const R=v.find(O=>O.visitorId===r);return{visitorId:r,name:R?R.visitorName:t("visitorPickerUnknownVisitor"),muted:!R}}),[x,v,t]);function U(r){o(R=>R.includes(r)?R.filter(O=>O!==r):[...R,r])}const Z=N===1?S.trim().length>0:N===2?x.length>0:!0;function Y(){if(!(N>=3)){if(!Z){z(!0);return}z(!1),g(N+1),l("")}}function q(){N<=1||(z(!1),g(N-1))}function _(){w(null),T(null),c(""),o(F?[F]:[]),y(null),g(1),l(""),z(!1)}function b(r){w(null),T(r.id),c(r.displayName);const R=F&&!r.visitorIds.includes(F)?[...r.visitorIds,F]:[...r.visitorIds];o(R),y(null),g(1),l(""),z(!1)}async function K(){if(!(!S.trim()||x.length===0)){I(!0),y(null);try{if(k===null?await Ks({displayName:S.trim(),visitorIds:x}):await Os(k,{displayName:S.trim(),visitorIds:x}),!s.current)return;_(),await D()}catch(r){if(!s.current)return;y(Q(t,r))}finally{s.current&&I(!1)}}}async function W(r){I(!0),y(null);try{if(await Vs(r),!s.current)return;w(null),k===r&&_(),await D()}catch(R){if(!s.current)return;y(Q(t,R))}finally{s.current&&I(!1)}}const X=[{key:1,label:t("memberTemplateStepName")},{key:2,label:t("memberTemplateStepMembers")},{key:3,label:t("memberTemplateStepSave")}],ee=`${(N-1)/2*100}%`;return e.jsxs("div",{className:"mdocs-settings",children:[e.jsxs("div",{className:"mdocs-settings-header",children:[e.jsx("h2",{className:"mdocs-settings-title",children:t(k!==null?"memberTemplateUpdate":"memberTemplateFormNew")}),k!==null&&e.jsx("button",{type:"button",className:"secondary",onClick:_,style:{fontSize:12},children:t("memberTemplateCancelEdit")})]}),e.jsxs("div",{className:"mdocs-wizard-progress",children:[e.jsx("div",{className:"mdocs-wizard-progress-track",children:e.jsx("div",{className:"mdocs-wizard-progress-fill",style:{width:ee}})}),e.jsx("div",{className:"mdocs-wizard-progress-steps",children:X.map(r=>{const R=N===r.key,O=N>r.key;return e.jsxs("div",{className:"mdocs-wizard-step-indicator",children:[e.jsx("div",{className:`mdocs-wizard-step-circle${R?" current":""}${O?" done":""}`,children:O?"✓":r.key}),e.jsx("span",{className:`mdocs-wizard-step-label${R?" current":""}${O?" done":""}`,children:r.label})]},r.key)})})]}),A&&e.jsx("div",{className:"mdocs-settings-item-desc",style:{color:"var(--mdocs-danger)",marginBottom:12,marginTop:12},children:A}),e.jsxs("div",{className:"mdocs-settings-cards",children:[e.jsxs("div",{className:"mdocs-settings-card mdocs-wizard-card",children:[N===1&&e.jsxs("div",{className:"mdocs-wizard-step-content",children:[e.jsx("input",{className:"mdocs-wizard-name-input",value:S,onChange:r=>c(r.target.value),placeholder:t("memberTemplateNamePlaceholder"),maxLength:200,disabled:h,autoFocus:!0,"aria-invalid":M&&!S.trim()}),M&&!S.trim()&&e.jsx("p",{className:"mdocs-wizard-hint",style:{color:"var(--mdocs-danger)"},children:t("domainNameRequired")})]}),N===2&&e.jsxs("div",{className:"mdocs-wizard-step-content",children:[e.jsx(pt,{visitors:v,loadState:C,searchFilter:m,onSearchChange:l,selectedIds:new Set(x),onToggle:U,selectedRows:$,myVisitorId:F}),e.jsx("p",{className:`mdocs-wizard-hint${M&&x.length===0?" danger":""}`,children:"💡 "+t("memberTemplateStep2Hint")})]}),N===3&&e.jsx("div",{className:"mdocs-wizard-step-content",children:e.jsxs("div",{className:"mdocs-wizard-preview-card",children:[e.jsxs("div",{className:"mdocs-wizard-preview-row",children:[e.jsx("span",{className:"mdocs-wizard-preview-label",children:t("memberTemplateNameLabel")}),e.jsx("span",{className:"mdocs-wizard-preview-value",children:S})]}),e.jsxs("div",{className:"mdocs-wizard-preview-row",children:[e.jsx("span",{className:"mdocs-wizard-preview-label",children:t("memberTemplateIdsLabel")}),e.jsx("span",{className:"mdocs-wizard-preview-value",children:t("memberTemplateSelectedSummary",{count:String(x.length)})})]}),x.length>0&&e.jsxs("div",{className:"mdocs-wizard-preview-tags",children:[x.slice(0,15).map(r=>e.jsx("span",{className:"mdocs-wizard-preview-tag",children:Ge(r)},r)),x.length>15&&e.jsxs("span",{className:"mdocs-wizard-preview-tag mdocs-wizard-preview-tag-more",children:["+",x.length-15]})]})]})}),e.jsxs("div",{className:"mdocs-wizard-footer",children:[e.jsxs("button",{type:"button",className:"secondary",onClick:q,children:["← ",t("memberTemplateWizardBack")]}),N<3?e.jsxs("button",{type:"button",className:"primary",onClick:Y,children:[t("memberTemplateWizardNext")," →"]}):e.jsx("button",{type:"button",className:"primary",onClick:()=>void K(),children:t(h?"saving":k===null?"memberTemplateCreate":"memberTemplateUpdate")})]})]}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsx("div",{className:"mdocs-settings-card-title",children:t("memberTemplateSavedList")}),p?e.jsx("span",{className:"mdocs-settings-item-desc",children:t("loading")}):j.length===0?e.jsxs("div",{className:"mdocs-member-template-empty",children:[e.jsx("span",{className:"mdocs-settings-item-desc",children:t("memberTemplateEmpty")}),k===null&&e.jsx("button",{type:"button",className:"primary",onClick:_,children:t("memberTemplateEmptyAction")})]}):e.jsx("ul",{className:"mdocs-member-template-list",children:j.map(r=>e.jsx("li",{className:"mdocs-member-template-row",children:u===r.id?e.jsxs("div",{className:"mdocs-member-template-confirm-row",children:[e.jsx("span",{className:"mdocs-member-template-confirm-text",children:t("memberTemplateConfirmDeleteDesc",{name:r.displayName})}),e.jsxs("div",{className:"mdocs-member-template-confirm-actions",children:[e.jsx("button",{type:"button",className:"secondary",style:{color:"var(--mdocs-danger)"},onClick:()=>void W(r.id),children:t(h?"saving":"memberTemplateConfirmDeleteSubmit")}),e.jsx("button",{type:"button",className:"secondary",onClick:()=>w(null),children:t("memberTemplateConfirmDeleteCancel")})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mdocs-member-template-row-head",children:[e.jsx("strong",{children:r.displayName}),e.jsx("span",{className:"mdocs-settings-item-desc",children:t("memberTemplateCount",{count:String(r.visitorIds.length)})}),r.visitorIds.length>0&&e.jsx("div",{className:"mdocs-member-template-tags mdocs-member-template-tags-scroll",children:r.visitorIds.map(R=>e.jsx("span",{className:"mdocs-member-template-tag",children:f.get(R)??Ge(R)},R))})]}),e.jsxs("div",{className:"mdocs-member-template-row-actions",children:[e.jsx("button",{type:"button",className:"secondary",onClick:()=>b(r),children:t("edit")}),e.jsx("button",{type:"button",className:"secondary",onClick:()=>w(r.id),children:t("memberTemplateDelete")})]})]})},r.id))})]})]})]})}const Nt=[{value:"deepseek-flash",label:"deepseek-flash(V4.1 Flash)"},{value:"deepseek-v4-pro",label:"deepseek-v4-pro(将路由至 Flash)"}],vt=[{value:"openai-completions",label:"OpenAI 兼容"},{value:"anthropic-messages",label:"Anthropic 兼容"}];function kt(t,s,j){return t==="deepseek"?"Deepseek":s||j}function He(t){return`${kt(t.kind,t.providerId,t.name)} · ${t.modelId} · ${t.name}`}function wt(){const{t}=ie(),s=a.useRef(!0),[j,d]=a.useState(""),[p,B]=a.useState([]),[u,w]=a.useState(null),[N,g]=a.useState(null),[k,T]=a.useState("deepseek"),[S,c]=a.useState(""),[x,o]=a.useState(""),[h,I]=a.useState(""),[A,y]=a.useState("openai-completions"),[M,z]=a.useState("deepseek-flash"),[F,v]=a.useState(1e6),[P,C]=a.useState(""),[E,m]=a.useState(!1),[l,f]=a.useState(null),[D,$]=a.useState(!1),[U,Z]=a.useState(!0),[Y,q]=a.useState(!1),[_,b]=a.useState(null),[K,W]=a.useState(!1),[X,ee]=a.useState(!1),[r,R]=a.useState(!1),[O,ce]=a.useState(null),[me,oe]=a.useState(null),re=a.useRef(!1);a.useEffect(()=>(s.current=!0,()=>{s.current=!1}),[]);const se=a.useCallback(async()=>{const i=await Us();if(s.current)return B(i),i},[]);function te(){g(null),T("deepseek"),c(""),o(""),I(""),y("openai-completions"),z("deepseek-flash"),v(1e6),C(""),m(!1),f(null),$(!1),ee(!1),R(!1),b(null),W(!1)}function ue(i){g(i.id),T(i.kind),c(i.name),o(i.providerId??""),I(i.baseUrl),y(i.apiType);const V=i.modelId.trim(),ge=new Set(["deepseek-flash","deepseek-v4-flash","deepseek-v4-flash-vision-exp","deepseek-v4.1-flash","deepseek-v4.1-flash-expires-on-0910"]);z(i.kind==="deepseek"?V==="deepseek-v4-pro"?"deepseek-v4-pro":ge.has(V)?"deepseek-flash":V||"deepseek-flash":V),v(i.contextWindow||1e6),m(i.hasApiKey),f(i.apiKeyMasked),C(""),$(i.isDefault)}a.useEffect(()=>{(async()=>{try{const i=await Ws();if(!s.current)return;d(i.visitorName);const V=await se();if(!s.current||!V)return;V.length===0&&(te(),w("create"))}catch(i){if(!s.current)return;b(i instanceof Error?i.message:String(i)),w("create")}finally{s.current&&Z(!1)}})()},[se]);function je(){te(),w("create")}function he(i){ue(i),w("edit"),W(!1),b(null)}function ae(){te(),w(null)}async function ye(i){if(!(i.isDefault||me)){oe(i.id),b(null);try{await _s(i.id),await se()}catch(V){if(!s.current)return;b(V instanceof Error?V.message:String(V))}finally{s.current&&oe(null)}}}async function pe(){if(O){q(!0),b(null);try{await Hs(O.id);const i=await se();if(!s.current)return;u==="edit"&&N===O.id&&ae(),((i==null?void 0:i.length)??0)===0&&(te(),w("create"))}catch(i){if(!s.current)return;b(i instanceof Error?i.message:String(i))}finally{s.current&&(q(!1),ce(null))}}}async function be(){q(!0),b(null),W(!1);try{const i=k==="custom"?{...u==="edit"&&N?{id:N}:{},...u==="create"&&D?{isDefault:!0}:{},kind:k,baseUrl:h,modelId:M,providerId:x.trim()||void 0,apiType:A,name:S,contextWindow:F,...P.trim()?{apiKey:P.trim()}:{}}:{...u==="edit"&&N?{id:N}:{},...u==="create"&&D?{isDefault:!0}:{},kind:k,modelId:M,name:S,contextWindow:F,...P.trim()?{apiKey:P.trim()}:{}};if(await Gs(i),!s.current)return;await se(),W(!0),w(null),te()}catch(i){if(!s.current)return;b(i instanceof Error?i.message:String(i))}finally{s.current&&q(!1)}}const Ne=u!==null;return e.jsxs("div",{className:"mdocs-agent-config-panel",children:[e.jsxs("div",{className:"mdocs-settings-header",children:[e.jsx("h2",{className:"mdocs-settings-title",children:t("agentConfig")}),U?null:e.jsx("button",{type:"button",className:"secondary",onClick:je,children:"添加配置"})]}),e.jsxs("div",{className:"mdocs-settings-cards",children:[!U&&p.length>0?e.jsxs("div",{className:"mdocs-settings-card mdocs-agent-config-card",children:[e.jsx("p",{className:"mdocs-agent-config-lead",children:"选择默认配置后,智能助手与帮写将使用该模型。"}),e.jsx("ul",{className:"mdocs-agent-config-list",children:p.map(i=>e.jsxs("li",{className:"mdocs-agent-config-item",children:[e.jsxs("label",{className:"mdocs-agent-config-item-default",children:[e.jsx("input",{type:"radio",name:"agent-default-config",checked:i.isDefault,disabled:me!==null,onChange:()=>void ye(i)}),e.jsx("span",{children:i.isDefault?"默认":"设为默认"})]}),e.jsxs("div",{className:"mdocs-agent-config-item-main",children:[e.jsx("strong",{children:He(i)}),i.kind==="custom"?e.jsx("span",{className:"mdocs-agent-badge",children:"自定义"}):null,e.jsx("p",{className:"mdocs-agent-config-item-meta",children:i.hasApiKey?`Key ${i.apiKeyMasked??"已配置"}`:"未配置 Key"})]}),e.jsxs("div",{className:"mdocs-agent-config-item-actions",children:[e.jsx("button",{type:"button",className:"secondary small",onClick:()=>he(i),children:t("edit")}),e.jsx("button",{type:"button",className:"secondary small",onClick:()=>ce(i),children:"删除"})]})]},i.id))}),K?e.jsx("p",{className:"mdocs-agent-config-ok",children:t("saved")}):null,_?e.jsx("p",{className:"mdocs-agent-config-error",children:_}):null]}):null,Ne?e.jsxs("div",{className:"mdocs-settings-card mdocs-agent-config-card",children:[e.jsxs("p",{className:"mdocs-agent-config-lead",children:[u==="create"?"新建模型配置。":"编辑模型配置。","可以保存多套,勾选一条作为默认。智能助手和帮写使用默认那套。"]}),e.jsxs("div",{className:"mdocs-agent-form",children:[e.jsxs("div",{className:"mdocs-agent-kind-tabs",role:"tablist","aria-label":"配置方式",children:[e.jsx("button",{type:"button",role:"tab","aria-selected":k==="deepseek",className:k==="deepseek"?"active":"",onClick:()=>T("deepseek"),children:"Deepseek"}),e.jsx("button",{type:"button",role:"tab","aria-selected":k==="custom",className:k==="custom"?"active":"",onClick:()=>T("custom"),children:"自定义"})]}),k==="deepseek"?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:t("agentConfigModel")}),e.jsx(Ve,{options:[...Nt],value:M,onChange:z})]}),e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:t("agentConfigApiKey")}),e.jsxs("div",{className:"mdocs-agent-config-apikey",children:[e.jsx("input",{type:X?"text":"password",value:P,onChange:i=>C(i.target.value),placeholder:E?t("agentConfigApiKeyConfigured").replace("{{masked}}",l??"…"):t("agentConfigApiKeyPlaceholder"),autoComplete:"off"}),e.jsx("button",{type:"button",className:"mdocs-agent-config-eye","aria-label":t(X?"agentConfigHideKey":"agentConfigShowKey"),onClick:()=>ee(i=>!i),children:X?"隐藏":"显示"})]})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:"API 地址"}),e.jsx("input",{type:"url",value:h,onChange:i=>{const V=i.target.value;if(I(V),re.current)return;V.toLowerCase().includes("anthropic")?y("anthropic-messages"):y("openai-completions")},placeholder:"https://api.example.com/v1",className:"mdocs-agent-mono"})]}),e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:"Model ID"}),e.jsx("input",{type:"text",value:M,onChange:i=>z(i.target.value),placeholder:"模型名称,如 gpt-4o",className:"mdocs-agent-mono"})]}),e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:t("agentConfigApiKey")}),e.jsxs("div",{className:"mdocs-agent-config-apikey",children:[e.jsx("input",{type:X?"text":"password",value:P,onChange:i=>C(i.target.value),placeholder:E?t("agentConfigApiKeyConfigured").replace("{{masked}}",l??"…"):"粘贴 API Key",autoComplete:"off"}),e.jsx("button",{type:"button",className:"mdocs-agent-config-eye","aria-label":t(X?"agentConfigHideKey":"agentConfigShowKey"),onClick:()=>ee(i=>!i),children:X?"隐藏":"显示"})]})]})]}),e.jsxs("details",{className:"mdocs-agent-advanced",open:r,onToggle:i=>R(i.target.open),children:[e.jsx("summary",{children:"高级选项"}),k==="custom"?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:"Provider ID(选填)"}),e.jsx("input",{type:"text",value:x,onChange:i=>o(i.target.value),placeholder:"不填则自动从 API 地址提取域名"})]}),e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:"API 协议"}),e.jsx(Ve,{options:[...vt],value:A,onChange:i=>{re.current=!0,y(i)}})]})]}):null,e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:t("agentConfigName")}),e.jsx("input",{type:"text",value:S,onChange:i=>c(i.target.value),placeholder:t("agentConfigNameDefault").replace("{{name}}",j||"…")})]}),e.jsxs("label",{className:"mdocs-agent-field",children:[e.jsx("span",{children:t("agentConfigContextWindow")}),e.jsx("input",{type:"number",min:1e3,max:2e6,step:1e3,value:F,onChange:i=>v(Number(i.target.value)||1e6)}),e.jsx("p",{className:"mdocs-agent-field-hint",children:t("agentConfigContextWindowDesc")})]})]}),u==="create"&&p.length>0?e.jsxs("label",{className:"mdocs-agent-config-default-check",children:[e.jsx("input",{type:"checkbox",checked:D,onChange:i=>$(i.target.checked)}),e.jsx("span",{children:"保存后设为默认配置"})]}):null,_?e.jsx("p",{className:"mdocs-agent-config-error",children:_}):null,e.jsxs("div",{className:"mdocs-agent-config-actions",children:[e.jsx("button",{type:"button",onClick:()=>void be(),disabled:Y,children:t(Y?"saving":"save")}),p.length>0?e.jsx("button",{type:"button",className:"secondary",onClick:ae,disabled:Y,children:t("cancel")}):null]})]})]}):U?e.jsx("div",{className:"mdocs-settings-card mdocs-agent-config-card",children:e.jsx("p",{className:"mdocs-agent-config-muted",children:t("loading")})}):null]}),O?e.jsx(es,{title:"删除配置",message:`确定删除「${He(O)}」?${O.isDefault&&p.length>1?" 删除后将自动把另一条设为默认。":""}`,confirmLabel:"删除",cancelLabel:t("cancel"),onConfirm:()=>void pe(),onCancel:()=>ce(null)}):null]})}const St=/^[A-Za-z0-9_]+$/;function Te(t){return St.test(t.trim())}function Ct(){const t=a.useRef(!0),[s,j]=a.useState([]),[d,p]=a.useState(!0),[B,u]=a.useState(null),[w,N]=a.useState(null),[g,k]=a.useState(!1),[T,S]=a.useState(""),[c,x]=a.useState(""),[o,h]=a.useState(""),[I,A]=a.useState(!1);a.useEffect(()=>(t.current=!0,()=>{t.current=!1}),[]);async function y(){p(!0),u(null);try{const m=await Js();if(!t.current)return;j(m)}catch(m){if(!t.current)return;u(m instanceof Error?m.message:String(m))}finally{t.current&&p(!1)}}a.useEffect(()=>{y()},[]);function M(){k(!0),N(null),S(""),x(""),h(""),u(null)}function z(m){k(!1),N(m.id),S(m.name),x(m.description),h(m.body),u(null)}function F(){k(!1),N(null),S(""),x(""),h("")}async function v(){const m=T.trim();if(!Te(m)){u("名称仅允许英文、数字、下划线");return}if(!o.trim()){u("正文不能为空");return}A(!0),u(null);try{if(g?await Zs({name:m,description:c,body:o}):w&&await Ys(w,{name:m,description:c,body:o}),!t.current)return;F(),await y()}catch(l){if(!t.current)return;u(l instanceof Error?l.message:String(l))}finally{t.current&&A(!1)}}async function P(m){if(window.confirm("删除该 Skill?已引用的历史轮次将跳过缺失项。")){u(null);try{if(await qs(m),!t.current)return;w===m&&F(),await y()}catch(l){if(!t.current)return;u(l instanceof Error?l.message:String(l))}}}const C=g||w!==null,E=!T.trim()||Te(T);return e.jsxs("div",{className:"mdocs-agent-skills-panel",children:[e.jsxs("div",{className:"mdocs-settings-header",children:[e.jsxs("h2",{className:"mdocs-settings-title",children:["Agent Skills",!d&&s.length>0?e.jsx("span",{className:"mdocs-agent-skills-count",children:s.length}):null]}),C?null:e.jsx("button",{type:"button",className:"secondary",onClick:M,children:"新建"})]}),e.jsx("div",{className:"mdocs-settings-cards",children:e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsx("p",{className:"mdocs-settings-item-desc mdocs-agent-skills-lead",children:"私人提示词宏。名称对本账号唯一,仅英文、数字、下划线;智能助手 / 帮写里按名称引用。"}),B?e.jsx("p",{className:"mdocs-agent-skills-error",children:B}):null,C?e.jsxs("div",{className:"mdocs-agent-skills-form",children:[e.jsx("div",{className:"mdocs-agent-skills-form-head",children:e.jsx("strong",{children:g?"新建 Skill":"编辑 Skill"})}),e.jsxs("label",{className:"mdocs-agent-skills-field",children:[e.jsx("span",{children:"名称"}),e.jsx("input",{type:"text",value:T,onChange:m=>S(m.target.value),maxLength:64,spellCheck:!1,autoComplete:"off",placeholder:"e.g. weekly_report",className:E?void 0:"mdocs-agent-skills-input-invalid"}),E?e.jsx("span",{className:"mdocs-agent-skills-hint",children:"引用与索引键,保存后按此名称使用"}):e.jsx("span",{className:"mdocs-agent-skills-hint-bad",children:"仅允许 A–Z、a–z、0–9、_"})]}),e.jsxs("label",{className:"mdocs-agent-skills-field",children:[e.jsx("span",{children:"简介(可选)"}),e.jsx("input",{type:"text",value:c,onChange:m=>x(m.target.value),maxLength:200,placeholder:"一句话说明用途"})]}),e.jsxs("label",{className:"mdocs-agent-skills-field",children:[e.jsx("span",{children:"正文"}),e.jsx("textarea",{value:o,onChange:m=>h(m.target.value),rows:10,placeholder:"写入 Agent 应遵守的步骤与约束…"})]}),e.jsxs("div",{className:"mdocs-agent-config-actions",children:[e.jsx("button",{type:"button",onClick:()=>void v(),disabled:I||!E||!T.trim()||!o.trim(),children:I?"保存中…":"保存"}),e.jsx("button",{type:"button",className:"secondary",onClick:F,disabled:I,children:"取消"})]})]}):null,d?e.jsx("p",{className:"mdocs-settings-item-desc",children:"加载中…"}):null,!d&&s.length===0&&!C?e.jsxs("div",{className:"mdocs-agent-skills-empty",children:[e.jsx("p",{children:"还没有 Skill"}),e.jsx("button",{type:"button",className:"secondary",onClick:M,children:"新建第一个"})]}):null,!d&&s.length>0?e.jsx("ul",{className:"mdocs-agent-skills-list",children:s.map(m=>{const l=Te(m.name),f=w===m.id;return e.jsxs("li",{className:"mdocs-agent-skills-item"+(f?" mdocs-agent-skills-item-active":""),children:[e.jsxs("div",{className:"mdocs-agent-skills-item-main",children:[e.jsxs("div",{className:"mdocs-agent-skills-item-title-row",children:[e.jsx("code",{className:"mdocs-agent-skills-name",children:m.name}),l?null:e.jsx("span",{className:"mdocs-agent-skills-badge-warn",title:"名称含非法字符,请编辑改为英文数字下划线",children:"需改名"})]}),e.jsx("span",{className:"mdocs-agent-skills-desc",children:m.description.trim()||"无简介"})]}),e.jsxs("div",{className:"mdocs-agent-skills-item-actions",children:[e.jsx("button",{type:"button",className:"secondary small",onClick:()=>z(m),disabled:C&&!f,children:"编辑"}),e.jsx("button",{type:"button",className:"secondary small",onClick:()=>void P(m.id),disabled:C,children:"删除"})]})]},m.id)})}):null]})})]})}function Je(t,s){const j=localStorage.getItem(t);return j===null?s:j==="true"}function Ze(t){const s=new Date(t);if(Number.isNaN(s.getTime()))return"—";const j=String(s.getMonth()+1).padStart(2,"0"),d=String(s.getDate()).padStart(2,"0");return`${s.getFullYear()}-${j}-${d}`}function Rt(t){const{t:s,lang:j,setLang:d}=ie(),{onBack:p,onPublishDraft:B}=t,u=Xs(),[w,N]=a.useState(!1),g=a.useRef(!0);a.useEffect(()=>(g.current=!0,()=>{g.current=!1}),[]),a.useEffect(()=>{u||N(!1)},[u]);const[k,T]=a.useState(()=>Je("mdocs.autoPublish",!1)),[S,c]=a.useState(()=>Je("mdocs.autoEdit",!0)),[x,o]=a.useState(!1),[h,I]=a.useState("general");function A(n){I(n),u&&N(!1)}const y=s(h==="general"?"general":h==="bookmarks"?"bookmarkTitle":h==="myDocuments"?"myDocuments":h==="domainManagement"?"domainManagement":h==="memberTemplates"?"memberTemplates":h==="agentConfig"?"agentConfig":"saveAndPublish"),[M,z]=a.useState([]),[F,v]=a.useState(!0),[P,C]=a.useState(null),[E,m]=a.useState(!1),[l,f]=a.useState(!1),[D,$]=a.useState(!1),[U,Z]=a.useState(!1),[Y,q]=a.useState(null),[_,b]=a.useState(!1),[K,W]=a.useState(!1),[X,ee]=a.useState(!1),[r,R]=a.useState(""),[O,ce]=a.useState(!1),[me,oe]=a.useState(!1),[re,se]=a.useState(0),[te,ue]=a.useState([]),[je,he]=a.useState(!1),[ae,ye]=a.useState(""),[pe,be]=a.useState([]),[Ne,i]=a.useState(!1),[V,ge]=a.useState(""),[le,ts]=a.useState(0),[Me,Ee]=a.useState("1"),[Re,as]=a.useState(0),[ve,ns]=a.useState(""),[ke,is]=a.useState(""),[fe,cs]=a.useState(""),[ls,os]=a.useState(null),[rs,ds]=a.useState([]),we=20,[Le,ms]=a.useState([]),[us,Se]=a.useState(!1),[Ce,Fe]=a.useState(null),[De,ze]=a.useState(new Map);a.useEffect(()=>{localStorage.setItem("mdocs.autoPublish",String(k))},[k]),a.useEffect(()=>{localStorage.setItem("mdocs.autoEdit",String(S))},[S]),a.useEffect(()=>{h==="savePublish"&&Pe().then(n=>{g.current&&se(n.filter(L=>!L.published).length)})},[h]),a.useEffect(()=>{h==="bookmarks"&&hs(),h==="myDocuments"&&(de(),Qe().then(n=>{g.current&&ds(n)}).catch(()=>{}))},[h,ve,ke,fe]),a.useEffect(()=>{Be()},[]),a.useEffect(()=>{qe().then(n=>{g.current&&ms(n)}).catch(()=>{})},[]);const xe=new Map(Le.map(n=>[n.domainId,n.domainName]));async function Be(){try{v(!0);const n=await Qs();if(!g.current)return;z(n)}catch{}finally{g.current&&v(!1)}}async function hs(){try{he(!0);const n=await et();if(!g.current)return;ue(n)}catch{}finally{g.current&&he(!1)}}async function de(n=0){try{i(!0);const L=await st({offset:n*we,limit:we,domainId:ve||void 0,creatorVisitorId:ke||void 0,groupBy:fe||void 0});if(!g.current)return;be(L.items),os(L.groups??null),as(L.total),ts(n),Ee(String(n+1))}catch{}finally{g.current&&i(!1)}}async function ps(n){try{await rt(n),ue(L=>L.filter(H=>H.documentId!==n))}catch{}}function gs(){if(M.some(L=>!L.revoked)){f(!0);return}$e()}async function $e(){try{m(!0);const n=await mt();C({token:n.token,name:n.name}),await Be()}finally{m(!1)}}async function fs(){try{ee(!0);const n=await lt();q(n.recoveryCode)}finally{ee(!1)}}async function xs(n){n.preventDefault();const L=r.trim();if(!(L&&L.length<4))try{ce(!0),await ot(L),R(""),oe(!0),setTimeout(()=>oe(!1),3e3)}finally{ce(!1)}}async function js(n){Fe(n);try{const L=await dt(n),H=new Map;L.forEach(G=>{H.set(G.visitorId,G.permission)}),ze(H)}catch{ze(new Map)}Se(!0)}async function ys(n){if(Ce)try{const L=new Set(n.map(G=>G.visitorId)),H=new Set(De.keys());for(const G of H)L.has(G)||await ut(Ce,G);for(const G of n)await ht(Ce,G.visitorId,G.permission);await de()}finally{Se(!1),Fe(null)}}const Ke=te.filter(n=>{if(!ae.trim())return!0;const L=ae.toLowerCase();return(n.displayName||n.relativePath||"").toLowerCase().includes(L)||(n.ownerVisitorName||"").toLowerCase().includes(L)||(n.domainId||"").toLowerCase().includes(L)}),ne=Math.ceil(Re/we);return e.jsxs("div",{className:"mdocs-layout"+(u?" mdocs-layout--reader mdocs-layout--settings":""),children:[u&&w?e.jsx("button",{type:"button",className:"mdocs-mobile-nav-scrim","aria-label":s("collapseSidebar"),onClick:()=>N(!1)}):null,e.jsxs("aside",{className:"mdocs-sidebar"+(u?w?" mdocs-sidebar--drawer-open":" mdocs-sidebar--drawer":""),children:[e.jsxs("header",{className:"mdocs-sidebar-header",children:[e.jsxs("div",{className:"mdocs-brand",children:[e.jsx("img",{src:tt,alt:s("brand"),className:"mdocs-brand-logo"}),e.jsx("span",{children:s("brand")})]}),u?e.jsx("button",{type:"button",className:"mdocs-sidebar-toggle",onClick:()=>N(!1),"aria-label":s("collapseSidebar"),style:{background:"none",border:"none",cursor:"pointer",padding:"6px",color:"var(--mdocs-text-muted, #999)",borderRadius:4,display:"flex",alignItems:"center",justifyContent:"center"},children:e.jsx(at,{size:18})}):null]}),e.jsxs("nav",{className:"mdocs-config-tree",children:[e.jsx("div",{className:"mdocs-config-item"+(h==="general"?" active":""),onClick:()=>A("general"),children:s("general")}),e.jsx("div",{className:"mdocs-config-item"+(h==="bookmarks"?" active":""),onClick:()=>A("bookmarks"),children:s("bookmarkTitle")}),e.jsx("div",{className:"mdocs-config-item"+(h==="myDocuments"?" active":""),onClick:()=>A("myDocuments"),children:s("myDocuments")}),e.jsx("div",{className:"mdocs-config-item"+(h==="domainManagement"?" active":""),onClick:()=>A("domainManagement"),children:s("domainManagement")}),e.jsx("div",{className:"mdocs-config-item"+(h==="memberTemplates"?" active":""),onClick:()=>A("memberTemplates"),children:s("memberTemplates")}),e.jsx("div",{className:"mdocs-config-item"+(h==="agentConfig"?" active":""),onClick:()=>A("agentConfig"),children:s("agentConfig")}),e.jsx("div",{className:"mdocs-config-item"+(h==="savePublish"?" active":""),onClick:()=>A("savePublish"),children:s("saveAndPublish")})]}),e.jsx("footer",{className:"mdocs-settings-sidebar-footer",onClick:p,children:s("backToDocs")})]}),e.jsxs("main",{className:"mdocs-main",children:[u?e.jsxs("div",{className:"mdocs-settings-mobile-bar",children:[e.jsx("button",{type:"button",className:"mdocs-reader-nav-btn",onClick:()=>N(!0),"aria-label":s("expandSidebar"),children:e.jsx(nt,{size:18,strokeWidth:1.75})}),e.jsx("span",{className:"mdocs-settings-mobile-bar-title",children:y})]}):null,it(),h==="general"?e.jsxs("div",{className:"mdocs-settings",children:[e.jsx("div",{className:"mdocs-settings-header",children:e.jsx("h2",{className:"mdocs-settings-title",children:s("general")})}),e.jsxs("div",{className:"mdocs-settings-cards",children:[e.jsx("div",{className:"mdocs-settings-card",children:e.jsxs("label",{className:"mdocs-settings-item",children:[e.jsx("span",{className:"mdocs-settings-item-info",children:e.jsx("span",{className:"mdocs-settings-card-title",children:s("language")})}),e.jsxs("span",{className:"mdocs-settings-lang",children:[e.jsx("button",{type:"button",className:j==="en"?"active":"",onClick:()=>d("en"),children:"EN"}),e.jsx("span",{children:"/"}),e.jsx("button",{type:"button",className:j==="zh"?"active":"",onClick:()=>d("zh"),children:"中"})]})]})}),e.jsx("div",{className:"mdocs-settings-card",children:e.jsxs("label",{className:"mdocs-settings-item",children:[e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:s("autoEdit")}),e.jsx("span",{className:"mdocs-settings-item-desc",children:s("autoEditDesc")})]}),e.jsx("button",{type:"button",className:"mdocs-toggle"+(S?" active":""),role:"switch","aria-checked":S,onClick:()=>c(n=>!n),children:e.jsx("span",{className:"mdocs-toggle-knob"})})]})}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsxs("div",{className:"mdocs-settings-item",children:[e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:s("cliToken")}),e.jsx("span",{className:"mdocs-settings-item-desc",children:s("cliTokenDesc")}),!F&&M.length>0&&e.jsx("span",{className:"mdocs-settings-item-desc",style:{marginTop:8,fontSize:"0.85em",opacity:.8},children:M.map(n=>e.jsxs("span",{style:{display:"block"},children:[n.name," — ",new Date(n.createdAt).toLocaleString()," ",n.revoked?`(${s("cliTokenRevoked")})`:`(${s("cliTokenActive")})`]},n.tokenId))})]}),e.jsx("button",{type:"button",className:"secondary",onClick:gs,disabled:E||F,children:E?"…":M.some(n=>!n.revoked)?s("cliTokenReset"):s("cliTokenCreate")})]}),P&&e.jsxs("div",{className:"mdocs-settings-secret-result mdocs-settings-secret-result--ok",children:[e.jsxs("div",{className:"mdocs-settings-secret-result-head",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:s("cliTokenGenerated")}),e.jsx("span",{className:"mdocs-settings-secret-value",children:P.token})]}),e.jsxs("div",{className:"mdocs-settings-secret-result-actions",children:[e.jsx("button",{type:"button",className:"secondary",onClick:()=>{(async()=>{try{await navigator.clipboard.writeText(P.token),Z(!1),$(!0),window.setTimeout(()=>$(!1),2e3)}catch{$(!1),Z(!0),window.setTimeout(()=>Z(!1),3e3)}})()},children:s(D?"cliTokenCopied":"cliTokenCopy")}),e.jsx("button",{type:"button",onClick:()=>C(null),children:s("close")})]}),D&&e.jsxs("div",{className:"mdocs-settings-copy-feedback mdocs-settings-copy-feedback--ok",role:"status",children:["✓ ",s("cliTokenCopied")]}),U&&e.jsx("div",{className:"mdocs-settings-copy-feedback mdocs-settings-copy-feedback--err",role:"status",children:s("cliTokenCopyFailed")})]})]}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsxs("div",{className:"mdocs-settings-item",children:[e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:"🔑 恢复码"}),e.jsx("span",{className:"mdocs-settings-item-desc",children:"当 Token 丢失时,可用恢复码找回身份。生成后请立即保存,仅显示一次。"})]}),e.jsx("button",{type:"button",className:"secondary",onClick:fs,disabled:X,children:X?"…":"生成恢复码"})]}),Y&&e.jsxs("div",{className:"mdocs-settings-secret-result mdocs-settings-secret-result--warn",children:[e.jsxs("div",{className:"mdocs-settings-secret-result-head",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:"🔑 你的恢复码"}),e.jsx("span",{className:"mdocs-settings-item-desc",style:{marginTop:8},children:"请立即复制保存,关闭后不可再查看。"}),e.jsx("div",{className:"mdocs-settings-secret-code",children:Y})]}),e.jsxs("div",{className:"mdocs-settings-secret-result-actions",children:[e.jsx("button",{type:"button",className:"primary",onClick:()=>{(async()=>{try{await navigator.clipboard.writeText(Y),W(!1),b(!0),window.setTimeout(()=>b(!1),2e3)}catch{b(!1),W(!0),window.setTimeout(()=>W(!1),3e3)}})()},children:_?"已复制":"复制恢复码"}),e.jsx("button",{type:"button",onClick:()=>q(null),children:s("close")})]}),_&&e.jsx("div",{className:"mdocs-settings-copy-feedback mdocs-settings-copy-feedback--ok",role:"status",children:"✓ 已复制到剪贴板"}),K&&e.jsx("div",{className:"mdocs-settings-copy-feedback mdocs-settings-copy-feedback--err",role:"status",children:"复制失败,请手动选中上方内容复制"})]})]}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsx("div",{className:"mdocs-settings-item",children:e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:"🔐 登录密码"}),e.jsx("span",{className:"mdocs-settings-item-desc",children:"设置密码后,可在其他浏览器或设备用「用户名+密码」登录。"})]})}),e.jsxs("form",{onSubmit:xs,style:{marginTop:12},children:[e.jsx("input",{type:"password",placeholder:"输入新密码(至少 4 位,留空则清除密码)",value:r,onChange:n=>R(n.target.value),minLength:0,style:{marginBottom:8}}),me&&e.jsx("div",{style:{color:"#4caf50",marginBottom:8,fontSize:"0.875rem"},children:"✓ 密码设置成功"}),e.jsx("button",{type:"submit",className:"secondary",disabled:O,children:O?"设置中…":"保存密码"})]})]})]})]}):h==="bookmarks"?e.jsxs("div",{className:"mdocs-settings",children:[e.jsx("div",{className:"mdocs-settings-header",children:e.jsx("h2",{className:"mdocs-settings-title",children:s("bookmarkTitle")})}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsx("input",{type:"text",className:"mdocs-settings-search",placeholder:s("bookmarkSearch"),value:ae,onChange:n=>ye(n.target.value)}),je?e.jsx("div",{style:{textAlign:"center",padding:"40px 0"},children:s("loading")}):e.jsx(e.Fragment,{children:Ke.length===0?e.jsx("div",{style:{textAlign:"center",padding:"40px 0",opacity:.6},children:s(ae?"bookmarkNoMatch":"bookmarkEmpty")}):e.jsx("div",{className:"mdocs-settings-table-wrap",children:e.jsxs("table",{className:"mdocs-settings-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:s("bookmarkColTitle")}),e.jsx("th",{children:s("bookmarkColDomain")}),e.jsx("th",{children:s("bookmarkColAuthor")}),e.jsx("th",{children:s("bookmarkColTime")}),e.jsx("th",{style:{textAlign:"right"}})]})}),e.jsx("tbody",{children:Ke.map(n=>e.jsxs("tr",{children:[e.jsx("td",{style:{cursor:"pointer",fontWeight:500},onClick:()=>t.onOpenDocument(n.documentId),children:n.displayName||n.relativePath||"Untitled"}),e.jsx("td",{children:n.isDeleted?e.jsx("span",{style:{opacity:.5,fontStyle:"italic"},children:"已删除"}):xe.get(n.domainId||"")||n.domainId||"—"}),e.jsx("td",{children:n.ownerVisitorName||"—"}),e.jsx("td",{children:new Date(n.bookmarkedAt).toLocaleDateString()}),e.jsx("td",{style:{textAlign:"right"},children:e.jsx("button",{type:"button",className:"secondary small",onClick:()=>ps(n.documentId),children:s("bookmarkRemove")})})]},n.documentId))})]})})})]})]}):h==="myDocuments"?e.jsxs("div",{className:"mdocs-settings mdocs-my-docs",children:[e.jsx("div",{className:"mdocs-settings-header",children:e.jsx("h2",{className:"mdocs-settings-title",children:s("myDocuments")})}),e.jsxs("div",{className:"mdocs-settings-card",children:[e.jsxs("div",{className:"mdocs-my-docs-toolbar",children:[e.jsxs("div",{className:"mdocs-my-docs-search",children:[e.jsx(ct,{size:16,strokeWidth:2,"aria-hidden":"true"}),e.jsx("input",{type:"text",placeholder:"搜索文章…",value:V,onChange:n=>ge(n.target.value)})]}),e.jsxs("select",{className:"mdocs-my-docs-select",value:ve,"aria-label":s("myDocumentsFilterDomain"),onChange:n=>ns(n.target.value),children:[e.jsx("option",{value:"",children:s("myDocumentsFilterAllDomains")}),Le.map(n=>e.jsx("option",{value:n.domainId,children:xe.get(n.domainId)||n.domainName},n.domainId))]}),e.jsxs("select",{className:"mdocs-my-docs-select",value:ke,"aria-label":s("myDocumentsFilterCreator"),onChange:n=>is(n.target.value),children:[e.jsx("option",{value:"",children:s("myDocumentsFilterAllCreators")}),rs.map(n=>e.jsx("option",{value:n.visitorId,children:n.visitorName},n.visitorId))]}),e.jsxs("select",{className:"mdocs-my-docs-select",value:fe,"aria-label":s("myDocumentsGroupBy"),onChange:n=>cs(n.target.value),children:[e.jsx("option",{value:"",children:s("myDocumentsGroupNone")}),e.jsx("option",{value:"domain",children:s("myDocumentsGroupDomain")}),e.jsx("option",{value:"creator",children:s("myDocumentsGroupCreator")})]})]}),Ne?e.jsx("div",{className:"mdocs-my-docs-empty",children:s("loading")}):e.jsxs(e.Fragment,{children:[pe.length===0?e.jsx("div",{className:"mdocs-my-docs-empty",children:s(V?"myDocumentsNoMatch":"myDocumentsEmpty")}):e.jsx("div",{className:"mdocs-settings-table-wrap",children:e.jsxs("table",{className:"mdocs-settings-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:s("myDocumentsColTitle")}),e.jsx("th",{children:s("myDocumentsColDomain")}),e.jsx("th",{children:s("myDocumentsColCreator")}),e.jsx("th",{children:s("myDocumentsColUpdated")}),e.jsx("th",{children:s("myDocumentsColCreated")}),e.jsx("th",{})]})}),e.jsx("tbody",{children:(ls??[{key:"",items:pe}]).flatMap(n=>{var G;const L=n.key?e.jsx("tr",{className:"mdocs-my-docs-group",children:e.jsx("td",{colSpan:6,children:fe==="domain"?xe.get(n.key)||n.key:((G=n.items[0])==null?void 0:G.creatorName)||n.key})},`group-${n.key}`):null,H=n.items.map(J=>e.jsxs("tr",{children:[e.jsx("td",{className:"mdocs-my-docs-title",onClick:()=>t.onOpenDocument(J.documentId),children:J.displayName||J.relativePath||"Untitled"}),e.jsx("td",{children:xe.get(J.domainId||"")||J.domainId||"—"}),e.jsx("td",{children:J.creatorName||J.creatorVisitorId||"—"}),e.jsx("td",{className:"mdocs-my-docs-date",children:Ze(J.updatedAt)}),e.jsx("td",{className:"mdocs-my-docs-date",children:Ze(J.createdAt)}),e.jsxs("td",{className:"mdocs-my-docs-actions",children:[e.jsx("button",{type:"button",className:"ghost small",onClick:()=>js(J.documentId),children:s("docInfoInviteMember")}),e.jsx("button",{type:"button",className:"primary small",onClick:()=>t.onOpenDocument(J.documentId),children:s("bookmarkOpen")})]})]},J.documentId));return L?[L,...H]:H})})]})}),e.jsxs("div",{className:"mdocs-my-docs-footer",children:[e.jsx("span",{className:"mdocs-settings-page-info",children:s("myDocumentsPageInfo",{current:String(ne>0?le+1:0),total:String(ne),count:String(Re)})}),ne>1&&e.jsxs("form",{className:"mdocs-my-docs-jump",onSubmit:n=>{n.preventDefault();const L=Number.parseInt(Me,10);if(!Number.isFinite(L))return;const H=Math.min(Math.max(L,1),ne)-1;H!==le&&de(H)},children:[e.jsx("span",{children:s("myDocumentsJumpTo")}),e.jsx("input",{type:"number",inputMode:"numeric",min:1,max:ne,"aria-label":s("myDocumentsJumpTo"),value:Me,onChange:n=>Ee(n.target.value)}),e.jsx("button",{type:"submit",className:"secondary small",children:s("myDocumentsJump")})]}),ne>1&&e.jsxs("div",{className:"mdocs-settings-pagination",children:[e.jsx("button",{type:"button",className:"secondary small",disabled:le<=0,onClick:()=>de(le-1),children:s("myDocumentsPrev")}),e.jsx("button",{type:"button",className:"secondary small",disabled:le>=ne-1,onClick:()=>de(le+1),children:s("myDocumentsNext")})]})]})]})]})]}):h==="domainManagement"?e.jsx(yt,{}):h==="memberTemplates"?e.jsx(bt,{}):h==="agentConfig"?e.jsxs("div",{className:"mdocs-settings",children:[e.jsx(wt,{}),e.jsx(Ct,{})]}):e.jsxs("div",{className:"mdocs-settings",children:[e.jsxs("div",{className:"mdocs-settings-header",children:[e.jsx("h2",{className:"mdocs-settings-title",children:s("saveAndPublish")}),e.jsx("button",{type:"button",className:"secondary",onClick:p,children:s("backToDocs")})]}),e.jsxs("div",{className:"mdocs-settings-cards",children:[e.jsx("div",{className:"mdocs-settings-card",children:e.jsxs("label",{className:"mdocs-settings-item",children:[e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:s("autoSave")}),e.jsx("span",{className:"mdocs-settings-item-desc",children:s("autoSaveAlwaysOnDesc")})]}),e.jsx("span",{className:"mdocs-toggle active",role:"switch","aria-checked":"true","aria-disabled":"true",style:{opacity:.6,cursor:"not-allowed"},children:e.jsx("span",{className:"mdocs-toggle-knob"})})]})}),e.jsx("div",{className:"mdocs-settings-card",children:e.jsxs("label",{className:"mdocs-settings-item",children:[e.jsxs("span",{className:"mdocs-settings-item-info",children:[e.jsx("span",{className:"mdocs-settings-card-title",children:s("autoPublish")}),e.jsx("span",{className:"mdocs-settings-item-desc",children:s("autoPublishDesc")})]}),e.jsx("button",{type:"button",className:"mdocs-toggle"+(k?" active":""),role:"switch","aria-checked":k,onClick:()=>T(n=>!n),children:e.jsx("span",{className:"mdocs-toggle-knob"})})]})}),re>0?e.jsxs("button",{className:"mdocs-settings-card mdocs-settings-draft-link",onClick:()=>o(!0),children:[e.jsxs("span",{className:"mdocs-settings-draft-link-text",children:["📄 ",s("viewDrafts")," (",re,")"]}),e.jsx("span",{className:"mdocs-settings-draft-arrow","aria-hidden":"true",children:"›"})]}):e.jsx("div",{className:"mdocs-settings-card mdocs-settings-draft-empty",children:e.jsx("span",{className:"mdocs-settings-item-desc",children:s("noDrafts")})})]})]}),x&&e.jsx(xt,{onPublish:B,onClose:()=>o(!1),onCountChange:se,onRecover:t.onRecoverDraft})]}),l&&e.jsx(es,{title:s("cliToken"),message:s("cliTokenResetConfirm"),confirmLabel:s("cliTokenReset"),cancelLabel:s("cancel"),busy:E,onConfirm:()=>{f(!1),$e()},onCancel:()=>f(!1)}),e.jsx(ss,{open:us,title:s("docInfoInviteMember"),initialSelectedIds:[...De.keys()],initialPermissions:De,templates:[],showPermissionSelect:!0,permissionOptions:[{value:"read",label:s("invitePermissionRead")},{value:"edit",label:s("invitePermissionEdit")}],onClose:()=>Se(!1),onConfirm:n=>{ys(n)}})]})}export{Rt as SettingsPage};