@loomcycle/explorer 0.7.0 → 0.11.0
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.
- package/dist/DocumentViewer.d.ts +6 -0
- package/dist/PathExplorer.d.ts +2 -1
- package/dist/components/ChunkTarget.d.ts +9 -0
- package/dist/components/Classification.d.ts +14 -0
- package/dist/components/Connections.d.ts +6 -1
- package/dist/components/CrossReferences.d.ts +20 -1
- package/dist/components/DocTrail.d.ts +6 -0
- package/dist/components/DocUnavailable.d.ts +6 -0
- package/dist/components/DocumentViewerBody.d.ts +10 -1
- package/dist/components/IdCopy.d.ts +7 -0
- package/dist/components/Mermaid.d.ts +3 -1
- package/dist/components/PathTree.d.ts +9 -6
- package/dist/components/ProposalReview.d.ts +23 -0
- package/dist/components/Splitter.d.ts +2 -1
- package/dist/components/Statement.d.ts +9 -0
- package/dist/components/SubjectTypeModal.d.ts +9 -0
- package/dist/index.cjs +14 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -4
- package/dist/index.js +4412 -2114
- package/dist/index.js.map +1 -1
- package/dist/lib/bulkReview.d.ts +23 -0
- package/dist/lib/chunkAncestry.d.ts +2 -0
- package/dist/lib/classify.d.ts +31 -0
- package/dist/lib/clipboard.d.ts +1 -0
- package/dist/lib/createClient.d.ts +15 -0
- package/dist/lib/dataLayer.d.ts +66 -9
- package/dist/lib/docTrail.d.ts +26 -0
- package/dist/lib/edgePages.d.ts +7 -0
- package/dist/lib/lazyPathTree.d.ts +43 -0
- package/dist/lib/navigator.d.ts +4 -0
- package/dist/lib/ruling.d.ts +114 -0
- package/dist/lib/statement.d.ts +13 -0
- package/dist/lib/subjectType.d.ts +4 -0
- package/dist/styles.css +558 -7
- package/dist/types.d.ts +130 -2
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
"use strict";var nt=Object.create;var be=Object.defineProperty;var st=Object.getOwnPropertyDescriptor;var at=Object.getOwnPropertyNames;var rt=Object.getPrototypeOf,ct=Object.prototype.hasOwnProperty;var ot=(t,s,n,a)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of at(s))!ct.call(t,r)&&r!==n&&be(t,r,{get:()=>s[r],enumerable:!(a=st(s,r))||a.enumerable});return t};var it=(t,s,n)=>(n=t!=null?nt(rt(t)):{},ot(s||!t||!t.__esModule?be(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),c=require("react"),lt=require("@loomcycle/client"),dt=require("react-markdown"),ut=require("remark-gfm");function pe(t,s){return{pathLs:(n,a,r,i)=>t.path({op:"ls",path:n,scope:a,recursive:r},i),pathMkdir:(n,a,r)=>t.path({op:"mkdir",path:n,scope:a},r),pathMv:(n,a,r,i)=>t.path({op:"mv",path:n,to:a,scope:r},i),pathRm:(n,a,r,i)=>t.path({op:"rm",path:n,scope:a,recursive:r},i),documentCreate:(n,a,r,i)=>t.document({op:"create_document",title:n,path:a,scope:r},i),documentDelete:(n,a,r)=>t.document({op:"delete_document",id:n,scope:a},r),documentGet:(n,a,r)=>t.document({op:"get_document",id:n,scope:a},r),documentsSummary:(n,a,r)=>t.document({op:"documents_summary",scope:a,...n.documentIds?{document_ids:n.documentIds}:{},...n.underPath?{under_path:n.underPath}:{}},r),documentQueryChunks:(n,a,r)=>t.document({op:"query_chunks",document_id:n,scope:a,limit:1e3},r),documentGetChunk:(n,a,r)=>t.document({op:"get_chunk",id:n,scope:a},r),documentCreateChunk:(n,a,r,i,o,l)=>t.document({op:"create_chunk",document_id:n,parent_id:a,scope:i,...l?{after_id:l}:{},...r},o),documentReorderChunk:(n,a,r,i)=>t.document({op:"reorder_chunk",id:n,direction:a,scope:r},i),documentDeleteChunk:(n,a,r)=>t.document({op:"delete_chunk",id:n,scope:a},r),documentSetAsset:(n,a,r,i,o,l)=>t.document({op:"set_asset",id:n,media_type:a,data:r,...i?{filename:i}:{},scope:o},l),documentGetEdges:(n,a,r)=>t.document({op:"get_edges",document_id:n,scope:a},r),documentUpdateChunk:(n,a,r,i,o)=>t.document({op:"update_chunk",id:n,revision:a,scope:i,...r},o),documentExportMd:(n,a,r,i)=>t.document({op:"export_md",document_id:n,scope:a,include_metadata:r},i),documentBacklinks:(n,a,r)=>t.document({op:"backlinks",id:n,scope:a},r),documentRelated:(n,a,r,i)=>t.document({op:"related",id:n,scope:a,...i?{limit:i}:{}},r),documentUnlinkedMentions:(n,a,r,i)=>t.document({op:"unlinked_mentions",id:n,scope:a,...i?{limit:i}:{}},r),documentHistory:(n,a,r,i)=>t.document({op:"history",id:n,scope:a,...i?{limit:i}:{}},r),documentGetVersion:(n,a,r,i)=>t.document({op:"get_version",id:n,revision:a,scope:r},i),documentDiff:(n,a,r,i,o)=>t.document({op:"diff",id:n,from_revision:a,to_revision:r,scope:i},o),documentExportCanvas:(n,a,r)=>t.document({op:"export_canvas",document_id:n,scope:a},r),...s?{documentAssetObjectUrl:async(n,a,r)=>{const i=new URLSearchParams({scope:a});r?.scopeId&&i.set("scope_id",r.scopeId),r?.tenant&&i.set("tenant",r.tenant);const o=await s(`/v1/_document/asset/${encodeURIComponent(n)}?${i.toString()}`);if(!o.ok)throw new Error(`asset fetch failed: ${o.status}`);const l=await o.blob();return URL.createObjectURL(l)}}:{}}}const Me=c.createContext(null);function mt({value:t,children:s}){return e.jsx(Me.Provider,{value:t,children:s})}function X(){const t=c.useContext(Me);if(!t)throw new Error("useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)");return t}function De(t){const s=t.fetch;return new lt.LoomcycleClient({baseUrl:t.baseUrl,authToken:t.token||void 0,fetch:(n,a)=>s?s(n,a):fetch(n,a)})}function Re(t){const s=t.fetch,n=(a,r)=>s?s(a,r):fetch(a,r);return a=>{const r={};return t.token&&(r.Authorization=`Bearer ${t.token}`),n(t.baseUrl+a,{headers:r})}}function $e(t){const{dataLayer:s,client:n,connection:a}=t;return c.useMemo(()=>s||(n?pe(n):a?pe(De(a),Re(a)):null),[s,n,a?.baseUrl,a?.token,a?.fetch])}function Le({theme:t,dataLayer:s,children:n}){const a=t?{"data-theme":t}:{};return s?e.jsx("div",{className:"loomcycle-explorer",...a,children:e.jsx(mt,{value:s,children:n})}):e.jsx("div",{className:"loomcycle-explorer",...a,children:e.jsxs("div",{className:"error-banner",children:["@loomcycle/explorer: provide a ",e.jsx("code",{children:"connection"}),", ",e.jsx("code",{children:"client"}),", or"," ",e.jsx("code",{children:"dataLayer"})," prop."]})})}const ke=6;function ht({children:t,defaultLeftWidth:s=380,minLeftWidth:n=180,minRightWidth:a=240,storageKey:r,className:i}){const[o,l]=c.useState(()=>{if(r){const v=localStorage.getItem(r),R=v?parseInt(v,10):NaN;if(Number.isFinite(R)&&R>=n)return R}return s}),u=c.useRef(null),[p,m]=c.useState(!1);c.useEffect(()=>{!r||p||localStorage.setItem(r,String(Math.round(o)))},[r,o,p]);const y=c.useCallback(v=>{v.preventDefault(),m(!0),v.target.setPointerCapture(v.pointerId)},[]),h=c.useCallback(v=>{if(!p||!u.current)return;const R=u.current.getBoundingClientRect(),O=v.clientX-R.left,A=R.width-a-ke,T=Math.max(n,Math.min(O,A));l(T)},[p,n,a]),L=c.useCallback(v=>{m(!1);try{v.target.releasePointerCapture(v.pointerId)}catch{}},[]);return e.jsxs("div",{ref:u,className:`splitter ${i??""} ${p?"dragging":""}`,style:{gridTemplateColumns:`${o}px ${ke}px 1fr`},children:[e.jsx("div",{className:"splitter-pane splitter-left",children:t[0]}),e.jsx("div",{className:"splitter-handle",role:"separator","aria-orientation":"vertical","aria-valuenow":Math.round(o),onPointerDown:y,onPointerMove:h,onPointerUp:L,onPointerCancel:L}),e.jsx("div",{className:"splitter-pane splitter-right",children:t[1]})]})}const ft=["image/png","image/jpeg","image/gif","image/webp"];function Te(t){return ft.includes(t)}function Pe(t){return new Promise((s,n)=>{if(!Te(t.type)){n(new Error(`unsupported image type "${t.type||"unknown"}" (png, jpeg, gif, webp only)`));return}const a=new FileReader;a.onerror=()=>n(a.error??new Error("failed to read file")),a.onload=()=>{const r=String(a.result??""),i=r.indexOf(",");if(i<0){n(new Error("could not decode image data"));return}s({mediaType:t.type,data:r.slice(i+1)})},a.readAsDataURL(t)})}function pt(t){if(!t)return null;for(let s=0;s<t.length;s++){const n=t[s];if(n.kind==="file"&&Te(n.type)){const a=n.getAsFile();if(a)return a}}return null}const re={"doc.rfc.draft":"#c99a3a","doc.rfc.review":"#6a7bd6","doc.rfc.confirmed":"#3a9ad6","doc.rfc.done":"#3aa06a","doc.plan.draft":"#c99a3a","doc.plan.review":"#6a7bd6","doc.plan.confirmed":"#3a9ad6","doc.plan.done":"#3aa06a","doc.document.done":"#3aa06a","doc.research.done":"#8a6ad6","doc.publication.draft":"#c99a3a","chunk.draft":"#c99a3a","chunk.review":"#6a7bd6","chunk.confirmed":"#3a9ad6","chunk.done":"#3aa06a","chunk.backlog":"#8a8f98","chunk.implementation":"#d1863a","chunk.merged":"#3aa06a"},xt=.16;function ce(t){return t&&Object.keys(t).length>0?t:re}function ge(t,s,n){const a=t??"",r=s??"";if(a&&r&&n[`doc.${a}.${r}`])return n[`doc.${a}.${r}`];if(a&&n[`doc.${a}`])return n[`doc.${a}`]}function Ae(t,s){const n=t??"";return n?s[`chunk.${n}`]:void 0}function Ue(t){const s=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(t.trim());if(!s)return;let n=s[1];n.length===3&&(n=n[0]+n[0]+n[1]+n[1]+n[2]+n[2]);const a=parseInt(n.slice(0,2),16),r=parseInt(n.slice(2,4),16),i=parseInt(n.slice(4,6),16);return`rgba(${a}, ${r}, ${i}, ${xt})`}function oe(t){if(!t)return;const s=Ue(t);if(s)return{background:s,borderLeft:`3px solid ${t}`}}function gt(t){const s=new Map;t.forEach(r=>s.set(r.id,{row:r,children:[]}));const n=[];t.forEach(r=>{const i=s.get(r.id);r.parent_id&&s.has(r.parent_id)?s.get(r.parent_id).children.push(i):n.push(i)});const a=r=>{r.sort((i,o)=>i.row.position-o.row.position),r.forEach(i=>a(i.children))};return a(n),n}function ve(t){return t==="image"?"🖼":t==="mermaid"?"📊":""}function Oe(t,s){for(const n of t){if(n.row.id===s)return n;const a=Oe(n.children,s);if(a)return a}}function jt({tree:t,selectedId:s,onSelect:n,colorEnabled:a,scheme:r,linkedIds:i}){const[o,l]=c.useState(()=>new Map),u=c.useCallback(m=>{l(y=>{const h=new Map(y);return h.set(m,y.get(m)===!1),h})},[]);c.useEffect(()=>{if(!s)return;const m=yt(t,s);m.length!==0&&l(y=>{let h=!1;const L=new Map(y);for(const v of m)L.get(v)===!1&&(L.set(v,!0),h=!0);return h?L:y})},[t,s]);const p=a?ce(r):null;return t.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"No chunks."})}):e.jsx("ul",{className:"tree chunk-tree",children:t.map(m=>e.jsx(Fe,{node:m,expanded:o,toggle:u,selectedId:s,onSelect:n,tintScheme:p,linkedIds:i},m.row.id))})}function yt(t,s){const n=[],a=(r,i)=>{for(const o of r){if(o.row.id===s)return n.push(...i),!0;if(a(o.children,[...i,o.row.id]))return!0}return!1};return a(t,[]),n}function Fe({node:t,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:i,linkedIds:o}){const l=t.children.length>0,u=s.get(t.row.id)!==!1,p=a===t.row.id,m=i?oe(Ae(t.row.status,i)):void 0,y=o?.has(t.row.id);return e.jsxs("li",{className:`node chunk-node ${p?"selected":""}`,children:[e.jsxs("div",{className:"row chunk-row",style:m,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":u?"collapse":"expand",disabled:!l,onClick:h=>{h.stopPropagation(),l&&n(t.row.id)},children:l?u?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"chunk-link",onClick:()=>r(t),title:t.row.title,children:[ve(t.row.type)&&e.jsx("span",{className:"chunk-type-icon","aria-hidden":!0,children:ve(t.row.type)}),e.jsx("span",{className:"chunk-title",children:t.row.title||"(untitled)"}),t.row.type&&e.jsx("span",{className:"chunk-badge",children:t.row.type}),t.row.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:t.row.status}),y&&e.jsx("span",{className:"chunk-link-badge",title:"Has cross-reference links","aria-label":"linked",children:"🔗"})]})]}),l&&u&&e.jsx("ul",{className:"children chunk-children",children:t.children.map(h=>e.jsx(Fe,{node:h,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:i,linkedIds:o},h.row.id))})]})}let bt=0;function ie({code:t}){const s=c.useRef(null),[n,a]=c.useState({});return c.useEffect(()=>{let r=!1;return(async()=>{try{const i=(await import("mermaid")).default,o=s.current?.closest("[data-theme]")?.getAttribute("data-theme"),l=o?o==="dark":!0;i.initialize({startOnLoad:!1,theme:l?"dark":"default",securityLevel:"strict"});const{svg:u}=await i.render(`lc-mmd-${bt++}`,t);r||a({svg:u})}catch{r||a({err:!0})}})(),()=>{r=!0}},[t]),n.err?e.jsx("pre",{className:"md-pre md-mermaid-error",title:"diagram failed to render",children:e.jsx("code",{children:t})}):n.svg?e.jsx("div",{ref:s,className:"md-mermaid",dangerouslySetInnerHTML:{__html:n.svg}}):e.jsx("div",{ref:s,className:"md-mermaid md-mermaid-loading",children:"rendering diagram…"})}function kt({chunk:t,scope:s,browse:n,onClose:a,onSaved:r}){const i=X(),[o,l]=c.useState(t.title),[u,p]=c.useState(t.type??""),[m,y]=c.useState(t.status??""),[h,L]=c.useState(t.body??""),[v,R]=c.useState(t.fields?JSON.stringify(t.fields,null,2):""),[O,A]=c.useState((t.tags??[]).join(", ")),[T,M]=c.useState(!1),[U,D]=c.useState(null),[F,E]=c.useState(!1),x=u==="mermaid",_=u==="image"||!!t.asset,[W,H]=c.useState(h);c.useEffect(()=>{if(!x)return;const k=setTimeout(()=>H(h),400);return()=>clearTimeout(k)},[h,x]);const b=async k=>{D(null),M(!0);try{const{mediaType:w,data:$}=await Pe(k),Z=await i.documentSetAsset(t.id,w,$,k.name,s,n);r(Z),a()}catch(w){D(w instanceof Error?w.message:String(w)),M(!1)}},g=x?"Mermaid source":_?"Caption (optional)":"Body (Markdown)",f=async k=>{k.preventDefault(),D(null),E(!1);const w={body:h,title:o,type:u,status:m,tags:O.split(",").map($=>$.trim()).filter($=>$!=="")};if(v.trim()!=="")try{w.fields=JSON.parse(v)}catch{D("fields must be valid JSON");return}M(!0);try{const $=await i.documentUpdateChunk(t.id,t.revision,w,s,n);r($),a()}catch($){const Z=$ instanceof Error?$.message:String($);D(Z),E(/revision conflict/i.test(Z)),M(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:a,children:e.jsxs("form",{className:"modal chunk-editor",onClick:k=>k.stopPropagation(),onSubmit:f,children:[e.jsxs("div",{className:"modal-header sticky-top",children:[e.jsx("h3",{children:"Edit chunk"}),e.jsxs("div",{className:"modal-buttons modal-buttons-top",children:[e.jsx("button",{type:"button",onClick:a,disabled:T,children:"cancel"}),e.jsx("button",{type:"submit",className:"primary",disabled:T,children:T?"saving…":"save"})]})]}),U&&e.jsxs("div",{className:"modal-err",children:[U,F&&" — reopen the chunk to load the latest, then reapply your edit."]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Title"}),e.jsx("input",{className:"path-modal-input",value:o,onChange:k=>l(k.target.value)})]}),e.jsxs("div",{className:"chunk-editor-row",children:[e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Type"}),e.jsx("input",{className:"path-modal-input",value:u,placeholder:"(none)",onChange:k=>p(k.target.value)})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Status"}),e.jsx("input",{className:"path-modal-input",value:m,placeholder:"(none)",onChange:k=>y(k.target.value)})]})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Tags"}),e.jsx("input",{className:"path-modal-input",value:O,placeholder:"comma-separated, nest with a slash: area/sub, priority/high",onChange:k=>A(k.target.value)})]}),_&&e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Replace image"}),e.jsx("input",{type:"file",accept:"image/png,image/jpeg,image/gif,image/webp",disabled:T,onChange:k=>{const w=k.target.files?.[0];w&&b(w)}})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:g}),e.jsx("textarea",{className:"modal-textarea chunk-body-input",rows:x?8:12,value:h,onChange:k=>L(k.target.value)})]}),x&&e.jsxs("div",{className:"path-field",children:[e.jsx("span",{children:"Preview"}),e.jsx("div",{className:"chunk-mermaid-preview",children:W.trim()?e.jsx(ie,{code:W}):e.jsx("p",{className:"doc-empty-body",children:"(enter Mermaid source above)"})})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Fields (JSON, optional)"}),e.jsx("textarea",{className:"modal-textarea chunk-fields-input",rows:4,value:v,placeholder:"{}",onChange:k=>R(k.target.value)})]})]})})}function vt(t){const s=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec((t??"").trim());if(!s)return"#888888";let n=s[1];return n.length===3&&(n=n[0]+n[0]+n[1]+n[1]+n[2]+n[2]),"#"+n.toLowerCase()}function Nt({documentId:t,rootChunkId:s,docType:n,docStatus:a,chunkStatuses:r,scope:i,browse:o,onClose:l,onSaved:u}){const p=X(),[m,y]=c.useState(null),[h,L]=c.useState(!1),[v,R]=c.useState({}),[O,A]=c.useState(!1),[T,M]=c.useState(null),[U,D]=c.useState(null),F=c.useMemo(()=>{const g=new Set(Object.keys(re));n&&g.add(`doc.${n}.${a||"draft"}`);for(const f of r)f&&g.add(`chunk.${f}`);return[...g].sort()},[n,a,r]);c.useEffect(()=>{let g=!1;return p.documentGetChunk(s,i,o).then(f=>{if(g)return;y(f);const k=f.fields??{};L(k.color_enabled===!0);const w=k.color_scheme??{};R({...re,...w})}).catch(f=>!g&&D(f instanceof Error?f.message:String(f))),()=>{g=!0}},[p,s,i,o]);const E=(g,f)=>R(k=>({...k,[g]:f})),x=g=>({...g??{},color_enabled:h,color_scheme:v}),_=async()=>{if(m){A(!0),D(null),M(null);try{await p.documentUpdateChunk(s,m.revision,{fields:x(m.fields)},i,o),u(),l()}catch(g){D(g instanceof Error?g.message:String(g)),A(!1)}}},W=async()=>{if(!n){D("this document has no type to match");return}A(!0),D(null),M(null);try{const{documents:g}=await p.documentsSummary({underPath:"/"},i,o),f=(g??[]).filter(w=>w.type===n&&w.document_id!==t&&w.root_chunk_id);let k=0;for(const w of f)try{const $=await p.documentGetChunk(w.root_chunk_id,i,o);await p.documentUpdateChunk(w.root_chunk_id,$.revision,{fields:x($.fields)},i,o),k++}catch{}M(`Applied to ${k} other ${n} document(s).`),u()}catch(g){D(g instanceof Error?g.message:String(g))}finally{A(!1)}},H=F.filter(g=>g.startsWith("doc.")),b=F.filter(g=>g.startsWith("chunk."));return e.jsx("div",{className:"modal-overlay",onClick:l,children:e.jsxs("div",{className:"modal scheme-editor",onClick:g=>g.stopPropagation(),children:[e.jsxs("div",{className:"scheme-editor-head",children:[e.jsx("h3",{children:"Document colors"}),e.jsxs("div",{className:"scheme-editor-actions",children:[e.jsx("button",{type:"button",onClick:l,disabled:O,children:"cancel"}),e.jsx("button",{type:"button",className:"primary",onClick:()=>{_()},disabled:O||!m,children:O?"saving…":"save"})]})]}),U&&e.jsx("div",{className:"modal-err",children:U}),m?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"scheme-toggle",children:[e.jsx("input",{type:"checkbox",checked:h,onChange:g=>L(g.target.checked)}),e.jsx("span",{children:"Enable coloring for this document"})]}),e.jsx("p",{className:"modal-context",children:"Document rows are tinted by type + status; chunk tiles by status. Colors apply as a semi-transparent wash so text stays readable."}),e.jsxs("div",{className:"scheme-group",children:[e.jsx("h4",{children:"Document (type · status)"}),H.map(g=>e.jsx(Ne,{label:g.slice(4),value:v[g],onChange:f=>E(g,f)},g))]}),e.jsxs("div",{className:"scheme-group",children:[e.jsx("h4",{children:"Chunks (status)"}),b.map(g=>e.jsx(Ne,{label:g.slice(6),value:v[g],onChange:f=>E(g,f)},g))]}),e.jsxs("div",{className:"scheme-editor-foot",children:[e.jsxs("button",{type:"button",onClick:()=>{W()},disabled:O||!n,title:n?`Apply this palette to every other ${n} document`:"This document has no type",children:["copy to all ",n||"same-type"," documents"]}),T&&e.jsx("span",{className:"scheme-status",children:T})]})]}):e.jsx("p",{className:"modal-context",children:"Loading…"})]})})}function Ne({label:t,value:s,onChange:n}){const a=vt(s);return e.jsxs("label",{className:"scheme-row",children:[e.jsx("input",{type:"color",value:a,onChange:r=>n(r.target.value)}),e.jsx("span",{className:"scheme-key",children:t}),e.jsx("code",{className:"scheme-hex",children:a})]})}const wt=8,Ct=20;function St(t){return/embedder|embedding|vector|semantic|not configured/i.test(t)}function _t({documentId:t,selectedId:s,scope:n,browse:a,onSelectChunk:r}){const i=X(),[o,l]=c.useState(!1),[u,p]=c.useState(!1),[m,y]=c.useState(void 0),[h,L]=c.useState([]),[v,R]=c.useState([]),[O,A]=c.useState(null),[T,M]=c.useState([]),[U,D]=c.useState(!1);c.useEffect(()=>{if(!o||!s)return;const x=s;let _=!1;return p(!0),Promise.allSettled([i.documentBacklinks(x,n,a),i.documentRelated(x,n,a,wt),i.documentUnlinkedMentions(x,n,a,Ct)]).then(([W,H,b])=>{if(!_){if(L(W.status==="fulfilled"?W.value.backlinks??[]:[]),H.status==="fulfilled")R(H.value.related??[]),A(null);else{R([]);const g=H.reason instanceof Error?H.reason.message:String(H.reason);A(St(g)?"semantic search not configured":g)}b.status==="fulfilled"?(M(b.value.unlinked_mentions??[]),D(!!b.value.truncated)):(M([]),D(!1)),y(x),p(!1)}}),()=>{_=!0}},[i,o,s,n,a]);const F=x=>!!x&&x!==t,E=!!(o&&s&&!u&&m===s);return e.jsxs("div",{className:"doc-conn",children:[e.jsxs("button",{type:"button",className:"doc-conn-toggle",onClick:()=>l(x=>!x),children:[o?"▼":"▶"," Connections"]}),o&&e.jsx("div",{className:"doc-conn-body",children:s?E?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-conn-group",children:[e.jsx("h5",{children:"Backlinks"}),h.length===0?e.jsx("p",{className:"doc-conn-empty",children:"none"}):e.jsx("ul",{className:"doc-conn-list",children:h.map((x,_)=>e.jsxs("li",{className:"doc-conn-row",children:[e.jsx(he,{id:x.from_id,title:x.from_title,crossDoc:F(x.from_document_id),onSelect:r}),e.jsx("span",{className:"doc-ref-kind",children:x.kind}),x.auto&&e.jsx("span",{className:"doc-conn-auto",title:"Auto-linked from a [[wikilink]]",children:"auto"})]},`${x.from_id}-${x.kind}-${_}`))})]}),e.jsxs("div",{className:"doc-conn-group",children:[e.jsx("h5",{children:"Related"}),O?e.jsx("p",{className:"doc-conn-empty",children:O}):v.length===0?e.jsx("p",{className:"doc-conn-empty",children:"none"}):e.jsx("ul",{className:"doc-conn-list",children:v.map((x,_)=>e.jsxs("li",{className:"doc-conn-row",children:[e.jsx(he,{id:x.chunk_id,title:x.title,crossDoc:F(x.document_id),onSelect:r}),e.jsx("span",{className:"doc-conn-score",title:"similarity score",children:x.score.toFixed(2)})]},`${x.chunk_id}-${_}`))})]}),e.jsxs("div",{className:"doc-conn-group",children:[e.jsx("h5",{children:"Unlinked mentions"}),T.length===0?e.jsx("p",{className:"doc-conn-empty",children:"none"}):e.jsxs("ul",{className:"doc-conn-list",children:[T.map((x,_)=>e.jsx("li",{className:"doc-conn-row",children:e.jsx(he,{id:x.chunk_id,title:x.title,crossDoc:F(x.document_id),onSelect:r})},`${x.chunk_id}-${_}`)),U&&e.jsx("li",{className:"doc-conn-more","aria-hidden":!0,children:"(more…)"})]})]})]}):e.jsx("p",{className:"doc-conn-empty",children:"loading…"}):e.jsx("p",{className:"doc-conn-empty",children:"Select a chunk."})})]})}function he({id:t,title:s,crossDoc:n,onSelect:a}){const r=s||t.slice(0,8);return n?e.jsxs("span",{className:"doc-ref-target external",title:"In another document",children:[r," ↗"]}):e.jsx("button",{type:"button",className:"doc-ref-target",onClick:()=>a(t),children:r})}const fe=24;function Et({edges:t,documentId:s,selectedId:n,onSelectChunk:a}){const[r,i]=c.useState(!1),[o,l]=c.useState(!1);if(t.length===0)return null;const u=n?t.filter(y=>y.from_id===n||y.to_id===n):[],p=o?t:t.slice(0,fe),m=Dt(p);return e.jsxs("div",{className:"doc-refs",children:[n&&e.jsxs("div",{className:"doc-refs-list-wrap",children:[e.jsx("h4",{children:"References"}),u.length===0?e.jsx("p",{className:"doc-refs-empty",children:"This chunk has no links."}):e.jsx("ul",{className:"doc-refs-list",children:u.map((y,h)=>e.jsx(Mt,{edge:y,selectedId:n,documentId:s,onSelectChunk:a},`${y.from_id}-${y.to_id}-${y.kind}-${h}`))})]}),e.jsxs("div",{className:"doc-graph",children:[e.jsxs("button",{type:"button",className:"doc-graph-toggle",onClick:()=>i(y=>!y),children:[r?"▼":"▶"," relationship graph (",t.length,")"]}),r&&e.jsxs(e.Fragment,{children:[e.jsx(ie,{code:m}),t.length>fe&&e.jsx("button",{type:"button",className:"doc-graph-more",onClick:()=>l(y=>!y),children:o?`show top ${fe}`:`show all ${t.length}`})]})]})]})}function Mt({edge:t,selectedId:s,documentId:n,onSelectChunk:a}){const r=t.from_id===s,i=r?t.to_id:t.from_id,o=(r?t.to_title:t.from_title)||i.slice(0,8),l=r?t.to_document_id:t.from_document_id,u=!l||l===n;return e.jsxs("li",{className:"doc-ref",children:[e.jsx("span",{className:"doc-ref-kind",children:t.kind}),e.jsx("span",{className:"doc-ref-arrow","aria-hidden":!0,children:r?"→":"←"}),u?e.jsx("button",{type:"button",className:"doc-ref-target",onClick:()=>a(i),children:o}):e.jsxs("span",{className:"doc-ref-target external",title:"In another document",children:[o," ↗"]})]})}function Dt(t){const s=["graph LR"];for(const n of t){const a=`${we(n.from_id)}["${Ce(n.from_title,n.from_id)}"]`,r=`${we(n.to_id)}["${Ce(n.to_title,n.to_id)}"]`,i=(n.kind||"link").replace(/[|"]/g," ");s.push(` ${a} -->|${i}| ${r}`)}return s.join(`
|
|
2
|
-
`)}function
|
|
3
|
-
`);return
|
|
4
|
-
`]},
|
|
5
|
-
`)?
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("react"),l=require("react/jsx-runtime"),u=require("@loomcycle/client"),d=require("react-markdown");d=s(d,1);let f=require("remark-gfm");f=s(f,1);var p=`loomcycle.explorer.navigator`;function m(e){try{return e?.getItem(`loomcycle.explorer.navigator`)===`stars`?`stars`:`tree`}catch{return`tree`}}function h(e,t){try{e?.setItem(p,t)}catch{}}function g(e){return!!e&&!e.parent_id&&!!e.entity?.natural_key}function _(e){let t=e?.classes;if(!Array.isArray(t))return[];let n=new Set;for(let e of t){let{name:t,kind:r}=e??{};typeof t==`string`&&t!==``&&r!==`document`&&n.add(t)}return[...n].sort((e,t)=>e.localeCompare(t))}function v(e,t){let n=t??``;return n===``||e.some(e=>e.toLowerCase()===n.toLowerCase())?e:[n,...e]}var y=4,b=5e3;function x(e,t,n){return{...S(e,n),pathLs:(t,n,r,i,a)=>e.path({op:`ls`,path:t,scope:n,recursive:r,...a?.limit?{limit:a.limit}:{},...a?.cursor?{cursor:a.cursor}:{}},i),pathIsStored:(t,n,r)=>e.path({op:`stat`,path:t,scope:n},r).then(()=>!0,e=>{if(e?.name===`SubstrateToolRefusedError`)return!1;throw e}),documentPath:async(t,n,r)=>{let i;for(let a=0;a<y;a++){let a=await e.path({op:`ls`,path:`/`,scope:n,recursive:!0,kind_filter:`document`,limit:b,...i?{cursor:i}:{}},r),o=(a.entries??[]).find(e=>e.resource_ref?.document_id===t);if(o)return o.full_path;if(i=a.next_cursor||void 0,!i)return}},pathMkdir:(t,n,r)=>e.path({op:`mkdir`,path:t,scope:n},r),pathMv:(t,n,r,i)=>e.path({op:`mv`,path:t,to:n,scope:r},i),pathRm:(t,n,r,i)=>e.path({op:`rm`,path:t,scope:n,recursive:r},i),documentCreate:(t,n,r,i)=>e.document({op:`create_document`,title:t,path:n,scope:r},i),documentDelete:(t,n,r)=>e.document({op:`delete_document`,id:t,scope:n},r),documentGet:(t,n,r)=>e.document({op:`get_document`,id:t,scope:n},r),documentsSummary:(t,n,r)=>e.document({op:`documents_summary`,scope:n,...t.documentIds?{document_ids:t.documentIds}:{},...t.underPath?{under_path:t.underPath}:{}},r),documentQueryChunks:(t,n,r)=>e.document({op:`query_chunks`,document_id:t,scope:n,limit:1e3},r),documentGetChunk:(t,n,r)=>e.document({op:`get_chunk`,id:t,scope:n},r),documentCreateChunk:(t,n,r,i,a,o)=>e.document({op:`create_chunk`,document_id:t,parent_id:n,scope:i,...o?{after_id:o}:{},...r},a),documentReorderChunk:(t,n,r,i)=>e.document({op:`reorder_chunk`,id:t,direction:n,scope:r},i),documentDeleteChunk:(t,n,r)=>e.document({op:`delete_chunk`,id:t,scope:n},r),documentSetAsset:(t,n,r,i,a,o)=>e.document({op:`set_asset`,id:t,media_type:n,data:r,...i?{filename:i}:{},scope:a},o),documentGetEdges:(t,n,r,i)=>e.document({op:`get_edges`,document_id:t,scope:n,...i?.after?{after:i.after}:{}},r),documentGetEdge:(t,n,r)=>e.document({op:`get_edge`,id:t,scope:n},r),documentAcceptStatement:(t,n,r,i)=>e.document({op:`accept_statement`,id:t,scope:n,...i?{reason:i}:{}},r),documentRejectStatement:(t,n,r,i)=>e.document({op:`reject_statement`,id:t,reason:n,scope:r},i),documentListStatements:(t,n,r)=>e.document({op:`list_statements`,scope:t,...r?.status?{status:r.status}:{},...r?.kind?{kind:r.kind}:{},...r?.limit?{limit:r.limit}:{},...r?.after?{after:r.after}:{}},n),documentClassify:(t,n,r,i)=>e.document({op:`classify`,document_id:t,write:n,scope:r},i),documentEntityTypes:(t,n)=>e.document({op:`describe_ontology`,scope:t},n).then(_),documentAcceptClassification:(t,n,r)=>e.document({op:`accept_classification`,document_id:t,scope:n},r),documentUpdateChunk:(t,n,r,i,a)=>e.document({op:`update_chunk`,id:t,revision:n,scope:i,...r},a),documentExportMd:(t,n,r,i)=>e.document({op:`export_md`,document_id:t,scope:n,include_metadata:r},i),documentBacklinks:(t,n,r)=>e.document({op:`backlinks`,id:t,scope:n},r),documentRelated:(t,n,r,i)=>e.document({op:`related`,id:t,scope:n,...i?{limit:i}:{}},r),documentUnlinkedMentions:(t,n,r,i)=>e.document({op:`unlinked_mentions`,id:t,scope:n,...i?{limit:i}:{}},r),documentHistory:(t,n,r,i)=>e.document({op:`history`,id:t,scope:n,...i?{limit:i}:{}},r),documentGetVersion:(t,n,r,i)=>e.document({op:`get_version`,id:t,revision:n,scope:r},i),documentDiff:(t,n,r,i,a)=>e.document({op:`diff`,id:t,from_revision:n,to_revision:r,scope:i},a),documentExportCanvas:(t,n,r)=>e.document({op:`export_canvas`,document_id:t,scope:n},r),...t?{documentAssetObjectUrl:async(e,n,r)=>{let i=new URLSearchParams({scope:n});r?.scopeId&&i.set(`scope_id`,r.scopeId),r?.tenant&&i.set(`tenant`,r.tenant);let a=await t(`/v1/_document/asset/${encodeURIComponent(e)}?${i.toString()}`);if(!a.ok)throw Error(`asset fetch failed: ${a.status}`);let o=await a.blob();return URL.createObjectURL(o)}}:{}}}function S(e,t){let n=e,r=e=>e?.tenant?`?tenant=${encodeURIComponent(e.tenant)}`:``,i={};typeof n.ruleStatements==`function`?i.documentRuleStatements=(e,t)=>n.ruleStatements(e.action,e.ids?{ids:e.ids}:{filter:e.filter},{reason:e.reason,dryRun:e.dry_run,expectCount:e.expect_count,max:e.max,scope:e.scope,scopeId:e.scope_id,tenant:t?.tenant}):t&&(i.documentRuleStatements=(e,n)=>t(`/v1/_statements/rule${r(n)}`,e));let a=(e,t)=>e===`user`&&t?.scopeId?t.scopeId:void 0;return typeof n.revertRuling==`function`?i.documentRevertRuling=(e,t,r)=>n.revertRuling(e,{scope:t,scopeId:a(t,r),tenant:r?.tenant}):t&&(i.documentRevertRuling=(e,n,i)=>{let o=a(n,i);return t(`/v1/_statements/revert${r(i)}`,{ruling_id:e,scope:n,...o?{scope_id:o}:{}})}),i}var C=(0,c.createContext)(null);function w({value:e,children:t}){return(0,l.jsx)(C.Provider,{value:e,children:t})}function T(){let e=(0,c.useContext)(C);if(!e)throw Error(`useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)`);return e}var E=5e3;function D(e){return e===``?`/`:e}function O(e){let t=e.lastIndexOf(`/`);return t<=0?``:e.slice(0,t)}function k(e){let t=[``],n=e.split(`/`).filter(Boolean),r=``;for(let e=0;e<n.length-1;e++)r=`${r}/${n[e]}`,t.push(r);return t}function A(e){return e instanceof Error?e.message:String(e)}function j(e,t){return{entries:[...t,...e.entries??[]],nextCursor:e.next_cursor||void 0,loaded:!0,loading:!1}}var M=class{data;scope;browse;pageLimit;dirs=new Map;snapshot=new Map;listeners=new Set;inflight=new Map;epoch=new Map;constructor(e,t,n,r=500){this.data=e,this.scope=t,this.browse=n,this.pageLimit=r}getSnapshot=()=>this.snapshot;subscribe=e=>(this.listeners.add(e),()=>{this.listeners.delete(e)});publish(){this.snapshot=new Map(this.dirs),this.listeners.forEach(e=>e())}set(e,t){this.dirs.set(e,t),this.publish()}track(e,t){let n=t.finally(()=>{this.inflight.get(e)===n&&this.inflight.delete(e)});return this.inflight.set(e,n),n}loadDir(e){return this.inflight.get(e)||(this.dirs.get(e)?.loaded?Promise.resolve():this.fetchFirst(e,this.pageLimit))}reload(e){let t=this.dirs.get(e),n=t?.entries.length??0,r=t?.loaded&&!t.nextCursor?n+this.pageLimit:n;return this.fetchFirst(e,Math.min(E,Math.max(this.pageLimit,r)))}fetchFirst(e,t){let n=(this.epoch.get(e)??0)+1;this.epoch.set(e,n);let r=this.dirs.get(e);this.set(e,{entries:r?.entries??[],nextCursor:r?.nextCursor,loaded:r?.loaded??!1,loading:!0});let i=this.data.pathLs(D(e),this.scope,!1,this.browse,{limit:t}).then(t=>{this.epoch.get(e)===n&&this.set(e,j(t,[]))},t=>{throw this.epoch.get(e)===n&&this.set(e,{entries:r?.entries??[],nextCursor:r?.nextCursor,loaded:r?.loaded??!1,loading:!1,error:A(t)}),t});return this.track(e,i)}loadMore(e){let t=this.inflight.get(e);if(t)return t;let n=this.dirs.get(e);if(!n?.nextCursor)return Promise.resolve();let r=this.epoch.get(e)??0;this.set(e,{...n,loading:!0,error:void 0});let i=this.data.pathLs(D(e),this.scope,!1,this.browse,{limit:this.pageLimit,cursor:n.nextCursor}).then(t=>{this.epoch.get(e)===r&&this.set(e,j(t,n.entries))},t=>{throw this.epoch.get(e)===r&&this.set(e,{...n,loading:!1,error:A(t)}),t});return this.track(e,i)}async reveal(e){let t=e.split(`/`).filter(Boolean),n=``;for(let e of t){for(await this.loadDir(n);;){let t=this.dirs.get(n);if(!t)return!1;if(t.entries.some(t=>t.name===e))break;let r=t.entries[t.entries.length-1];if(!t.nextCursor||r&&r.name>e)return!1;let i=t.nextCursor;if(await this.loadMore(n),this.dirs.get(n)?.nextCursor===i)return!1}n=`${n}/${e}`}return!0}async invalidate(e,t){if(t?.dropSubtree){for(let t of[...this.dirs.keys()])(t===e||t.startsWith(`${e}/`))&&(this.dirs.delete(t),this.epoch.set(t,(this.epoch.get(t)??0)+1),this.inflight.delete(t));this.publish()}let n=k(e).filter(e=>this.dirs.has(e));await Promise.all(n.map(e=>this.reload(e)))}async refresh(e){let t=new Set(e);t.add(``);for(let e of[...this.dirs.keys()])t.has(e)||(this.dirs.delete(e),this.epoch.set(e,(this.epoch.get(e)??0)+1),this.inflight.delete(e));this.publish();let n=[...t].filter(e=>e===``||this.dirs.has(e));await Promise.all(n.map(e=>this.reload(e)))}};async function ee(e,t,n,r){let i=[],a;for(;;){let o=await e.pathLs(D(t),n,!0,r,{limit:E,cursor:a});i.push(...o.entries??[]);let s=o.next_cursor||void 0;if(!s||s===a)return i;a=s}}function te(e){let t=[];for(let n of e){if(n.kind!==`document`)continue;let e=n.resource_ref?.document_id;e&&t.push(e)}return t}function ne(e){let t=e.fetch;return new u.LoomcycleClient({baseUrl:e.baseUrl,authToken:e.token||void 0,fetch:(e,n)=>t?t(e,n):fetch(e,n)})}function N(e){let t=e.fetch,n=(e,n)=>t?t(e,n):fetch(e,n);return t=>{let r={};return e.token&&(r.Authorization=`Bearer ${e.token}`),n(e.baseUrl+t,{headers:r})}}var P=class extends Error{status;code;constructor(e,t,n){super(e),this.name=`RequestError`,this.status=t,this.code=n}};function F(e){let t=e.fetch,n=(e,n)=>t?t(e,n):fetch(e,n);return async(t,r)=>{let i={"Content-Type":`application/json`,Accept:`application/json`};e.token&&(i.Authorization=`Bearer ${e.token}`);let a=await n(e.baseUrl+t,{method:`POST`,headers:i,body:JSON.stringify(r)}),o=await a.text(),s;try{s=o?JSON.parse(o):void 0}catch{}if(!a.ok){let e=s&&typeof s==`object`?s:{};throw new P(typeof e.error==`string`&&e.error?e.error:`${a.status} ${a.statusText}`.trim(),a.status,typeof e.code==`string`?e.code:void 0)}return s}}function I(e){let{dataLayer:t,client:n,connection:r}=e;return(0,c.useMemo)(()=>t||(n?x(n):r?x(ne(r),N(r),F(r)):null),[t,n,r?.baseUrl,r?.token,r?.fetch])}function L({theme:e,dataLayer:t,children:n}){let r=e?{"data-theme":e}:{};return t?(0,l.jsx)(`div`,{className:`loomcycle-explorer`,...r,children:(0,l.jsx)(w,{value:t,children:n})}):(0,l.jsx)(`div`,{className:`loomcycle-explorer`,...r,children:(0,l.jsxs)(`div`,{className:`error-banner`,children:[`@loomcycle/explorer: provide a `,(0,l.jsx)(`code`,{children:`connection`}),`, `,(0,l.jsx)(`code`,{children:`client`}),`, or`,` `,(0,l.jsx)(`code`,{children:`dataLayer`}),` prop.`]})})}function R(e,t,n){let r=e[e.length-1];return[...r&&r.documentId===t.documentId?[...e.slice(0,-1),{...r,...z(t)}]:[...e,t],n]}function re(e,t){return t<0||t>=e.length-1?e:e.slice(0,t+1)}function ie(e,t,n){let r=e[e.length-1];if(!r||r.documentId!==t)return e;let i={...r,...z(n)};return i.title===r.title&&i.path===r.path?e:[...e.slice(0,-1),i]}function z(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!==void 0))}function B(e){return e.title?e.title:e.path?e.path.slice(e.path.lastIndexOf(`/`)+1)||e.path:e.documentId.slice(0,8)}function ae(e,t=4){let n=Array.from({length:e},(e,t)=>({kind:`crumb`,index:t}));if(e<=t||t<3)return n;let r=t-1,i=Array.from({length:e-1-r},(e,t)=>t+1);return[n[0],{kind:`gap`,hidden:i},...n.slice(e-r)]}function oe(e,t){for(let n of e.values()){let e=n.entries.find(e=>V(e)===t);if(e)return e.full_path}}function V(e){if(e.kind===`document`)return e.resource_ref?.document_id||void 0}function H(e,t){return t&&e.some(e=>e.id===t)?t:e.find(e=>!e.parent_id)?.id}function se({trail:e,onJump:t}){let[n,r]=(0,c.useState)(!1);if(e.length<2)return null;let i=e.length-1,a=e[i-1],o=ae(e.length,n?e.length:void 0);return(0,l.jsxs)(`nav`,{className:`doc-trail`,"aria-label":`Documents you came through`,children:[(0,l.jsx)(`button`,{type:`button`,className:`doc-trail-back`,onClick:()=>t(i-1),title:`Back to ${B(a)}`,children:`← Back`}),(0,l.jsx)(`ol`,{className:`doc-trail-list`,children:o.map(n=>n.kind===`gap`?(0,l.jsx)(`li`,{className:`doc-trail-item`,children:(0,l.jsx)(`button`,{type:`button`,className:`doc-trail-gap`,onClick:()=>r(!0),title:`Show ${n.hidden.length} more: ${n.hidden.map(t=>B(e[t])).join(` › `)}`,"aria-label":`Show ${n.hidden.length} hidden documents`,children:`…`})},`gap`):n.index===i?(0,l.jsx)(`li`,{className:`doc-trail-item`,children:(0,l.jsx)(`span`,{className:`doc-trail-current`,"aria-current":`page`,title:B(e[i]),children:B(e[i])})},n.index):(0,l.jsx)(`li`,{className:`doc-trail-item`,children:(0,l.jsx)(`button`,{type:`button`,className:`doc-trail-crumb`,onClick:()=>t(n.index),title:B(e[n.index]),children:B(e[n.index])})},n.index))})]})}var ce=6;function le({children:e,defaultLeftWidth:t=380,minLeftWidth:n=180,minRightWidth:r=240,storageKey:i,className:a,hideRight:o=!1}){let s=()=>{if(i){let e=localStorage.getItem(i),t=e?parseInt(e,10):NaN;if(Number.isFinite(t)&&t>=n)return t}return t},[u,d]=(0,c.useState)(s),[f,p]=(0,c.useState)(i);f!==i&&(p(i),d(s()));let m=(0,c.useRef)(null),[h,g]=(0,c.useState)(!1);(0,c.useEffect)(()=>{i&&!h&&f===i&&localStorage.setItem(i,String(Math.round(u)))},[i,f,u,h]);let _=(0,c.useCallback)(e=>{e.preventDefault(),g(!0),e.target.setPointerCapture(e.pointerId)},[]),v=(0,c.useCallback)(e=>{if(!h||!m.current)return;let t=m.current.getBoundingClientRect(),i=e.clientX-t.left,a=t.width-r-ce;d(Math.max(n,Math.min(i,a)))},[h,n,r]),y=(0,c.useCallback)(e=>{g(!1);try{e.target.releasePointerCapture(e.pointerId)}catch{}},[]);return(0,l.jsxs)(`div`,{ref:m,className:`splitter ${a??``} ${h?`dragging`:``}`,style:{gridTemplateColumns:o?`1fr 0 0`:`min(${u}px, calc(100% - ${r+ce}px)) ${ce}px 1fr`},children:[(0,l.jsx)(`div`,{className:`splitter-pane splitter-left`,children:e[0]}),(0,l.jsx)(`div`,{hidden:o,className:`splitter-handle`,role:`separator`,"aria-orientation":`vertical`,"aria-valuenow":Math.round(u),onPointerDown:_,onPointerMove:v,onPointerUp:y,onPointerCancel:y}),(0,l.jsx)(`div`,{className:`splitter-pane splitter-right`,hidden:o,children:e[1]})]})}var ue=[`image/png`,`image/jpeg`,`image/gif`,`image/webp`];function U(e){return ue.includes(e)}function de(e){return new Promise((t,n)=>{if(!U(e.type)){n(Error(`unsupported image type "${e.type||`unknown`}" (png, jpeg, gif, webp only)`));return}let r=new FileReader;r.onerror=()=>n(r.error??Error(`failed to read file`)),r.onload=()=>{let i=String(r.result??``),a=i.indexOf(`,`);a<0?n(Error(`could not decode image data`)):t({mediaType:e.type,data:i.slice(a+1)})},r.readAsDataURL(e)})}function fe(e){if(!e)return null;for(let t=0;t<e.length;t++){let n=e[t];if(n.kind===`file`&&U(n.type)){let e=n.getAsFile();if(e)return e}}return null}var pe=20;async function me(e,t,n,r,i=20){let a=[],o;for(let s=0;s<i;s++){let i=await e.documentGetEdges(t,n,r,o?{after:o}:void 0);if(o&&i.next_cursor===o)return{edges:a,incomplete:!0};if(a.push(...i.edges??[]),!i.truncated)return{edges:a,incomplete:!1};if(!i.next_cursor)return{edges:a,incomplete:!0};o=i.next_cursor}return{edges:a,incomplete:!0}}function he(e,t){if(!t)return[];let n=new Map(e.map(e=>[e.id,e])),r=new Set([t]),i=[],a=n.get(t)?.parent_id;for(;a&&!r.has(a);){let e=n.get(a);if(!e)break;r.add(a),i.push(e),a=e.parent_id}return i.reverse()}var ge=new Set([`manual`,`wikilink`]),W=280;function _e(e){return e.status&&e.status!==`asserted`?e.status:void 0}function ve(e){return e.method&&!ge.has(e.method)?e.method:void 0}function G(e){return e.status===`proposed`&&!!e.id}function K(e){return new Date(Math.floor(e/1e6)).toISOString().slice(0,10)}function ye(e){let t=e.valid_at==null?void 0:K(e.valid_at),n=e.invalid_at==null?void 0:K(e.invalid_at);if(t&&n)return`valid ${t} → ${n}`;if(t)return`valid from ${t}`;if(n)return`valid until ${n}`}function be(e){if(e.judged_at==null)return;let t=`judged by ${e.judged_by||`a judge`} on ${K(e.judged_at)}`;return e.judge_reason?`${t}: ${e.judge_reason}`:t}function xe(e,t=3){let n=(e.evidence??[]).slice(0,t),r=Math.max(e.evidence_count??0,e.evidence?.length??0);return{rows:n,more:Math.max(0,r-n.length)}}function Se(e,t=W){let n=e.replace(/\s+/g,` `).trim();return n.length>t?n.slice(0,t-1)+`…`:n}var Ce=1e3,we=3,q=1e3;function Te(e){return!!e&&typeof e==`object`&&e.code===`queue_changed`}async function J(e,t,n={}){let r={action:t.action,...t.ids?{ids:t.ids}:{filter:t.filter??{}},...t.reason?{reason:t.reason}:{},...t.scope&&t.scope!==`tenant`?{scope:t.scope}:{},...t.scope===`user`&&t.scopeId?{scope_id:t.scopeId}:{}},i={rulingIds:[],ruled:0,failed:0,skipped:0,total:0,cancelled:!1,failures:[]},a=async()=>{let t=await e.rule({...r,dry_run:!0});return i.skipped=t.skipped,t},o=async(e,t)=>!n.confirm||await n.confirm(e,t)?(i.total=i.ruled+e.matched,!0):(i.cancelled=!0,!1),s;try{s=await a()}catch(e){return i.error=e,i}if(s.matched===0||!await o(s,`first`))return i;let c=0;for(let t=0;t<q&&s.to_rule!==0;t++){let t;try{t=await e.rule({...r,dry_run:!1,expect_count:s.matched})}catch(e){if(!Te(e)||c>=3)return i.error=e,i;c++;try{s=await a()}catch(e){return i.error=e,i}if(s.matched===0||!await o(s,`queue_changed`))return i;continue}t.ruling_id&&i.rulingIds.push(t.ruling_id),i.ruled+=t.ruled,i.failed+=t.failed;for(let e of t.items??[])e.error&&i.failures.push(e);if(n.onProgress?.({ruled:i.ruled,failed:i.failed,total:i.total,chunks:i.rulingIds.length}),t.ruled===0||t.matched<=t.selected)break;try{s=await a()}catch(e){return i.error=e,i}if(s.matched===0)break;if(s.matched>i.total-i.ruled&&!await o(s,`grew`))return i}return i}async function Ee(e,t){let n={reverted:0,changed:0,errors:[]};for(let r of[...t].reverse())try{let t=await e(r);n.reverted+=t.reverted,n.changed+=t.changed}catch(e){n.errors.push(e instanceof Error?e.message:String(e))}return n}function Y(e){return Math.max(1,Math.ceil(e/Ce))}function X(e){return e===`tenant`||e===`user`?e:void 0}function De(e,t){return e.documentRuleStatements?X(t):void 0}function Oe(e,t){return e.filter(e=>G(e)&&!!e.id&&e.from_document_id===t)}function ke(e,t,n,r){return e?{proposals:t,scope:e,incomplete:n,onChanged:r}:void 0}function Z(e,t,n){return e.documentRuleStatements?J({rule:n=>e.documentRuleStatements(n,t.browse)},{action:t.action,...t.ids&&t.ids.length>0?{ids:t.ids}:{filter:{from_document_id:t.documentId}},...t.reason?{reason:t.reason}:{},scope:t.scope,...t.scope===`user`&&t.browse?.scopeId?{scopeId:t.browse.scopeId}:{}},n):Promise.reject(Error(`this data layer cannot rule in bulk`))}function Q(e,t){let n=e=>e.toLocaleString(`en-US`),r=[`${e===`accept`?`Accepted`:`Rejected`} ${n(t.ruled)} statement${t.ruled===1?``:`s`}`];return t.failed>0&&r.push(`${n(t.failed)} did not take the ruling`),t.skipped>0&&r.push(`${n(t.skipped)} skipped: not yours to rule`),r.join(`; `)+`.`}var Ae={"doc.rfc.draft":`#c99a3a`,"doc.rfc.review":`#6a7bd6`,"doc.rfc.confirmed":`#3a9ad6`,"doc.rfc.done":`#3aa06a`,"doc.plan.draft":`#c99a3a`,"doc.plan.review":`#6a7bd6`,"doc.plan.confirmed":`#3a9ad6`,"doc.plan.done":`#3aa06a`,"doc.document.done":`#3aa06a`,"doc.research.done":`#8a6ad6`,"doc.publication.draft":`#c99a3a`,"chunk.draft":`#c99a3a`,"chunk.review":`#6a7bd6`,"chunk.confirmed":`#3a9ad6`,"chunk.done":`#3aa06a`,"chunk.backlog":`#8a8f98`,"chunk.implementation":`#d1863a`,"chunk.merged":`#3aa06a`},je=.16;function Me(e){return e&&Object.keys(e).length>0?e:Ae}function Ne(e,t,n){let r=e??``,i=t??``;if(r&&i&&n[`doc.${r}.${i}`])return n[`doc.${r}.${i}`];if(r&&n[`doc.${r}`])return n[`doc.${r}`]}function Pe(e,t){let n=e??``;return n?t[`chunk.${n}`]:void 0}function $(e){let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(!t)return;let n=t[1];return n.length===3&&(n=n[0]+n[0]+n[1]+n[1]+n[2]+n[2]),`rgba(${parseInt(n.slice(0,2),16)}, ${parseInt(n.slice(2,4),16)}, ${parseInt(n.slice(4,6),16)}, ${je})`}function Fe(e){if(!e)return;let t=$(e);if(t)return{background:t,borderLeft:`3px solid ${e}`}}async function Ie(e){try{return typeof navigator>`u`||!navigator.clipboard?!1:(await navigator.clipboard.writeText(e),!0)}catch{return!1}}function Le({value:e,label:t,compact:n,title:r}){let[i,a]=(0,c.useState)(`idle`),o=async t=>{t.stopPropagation(),t.preventDefault();let n=await Ie(e);a(n?`copied`:`error`),window.setTimeout(()=>a(`idle`),n?1200:1500)},s=i===`copied`?`✓`:i===`error`?`✗`:`⎘`,u=r??`Copy ${t??`id`}: ${e}`,d=(0,l.jsx)(`button`,{type:`button`,className:`id-copy-btn id-copy-${i}`,onClick:o,title:u,"aria-label":u,children:s});return n?d:(0,l.jsxs)(`span`,{className:`id-copy`,children:[t&&(0,l.jsx)(`span`,{className:`id-copy-label`,children:t}),(0,l.jsx)(`code`,{className:`id-copy-value`,title:e,children:e}),d]})}function Re(e){let t=new Map;e.forEach(e=>t.set(e.id,{row:e,children:[]}));let n=[];e.forEach(e=>{let r=t.get(e.id);e.parent_id&&t.has(e.parent_id)?t.get(e.parent_id).children.push(r):n.push(r)});let r=e=>{e.sort((e,t)=>e.row.position-t.row.position),e.forEach(e=>r(e.children))};return r(n),n}function ze(e){return e===`image`?`🖼`:e===`mermaid`?`📊`:``}function Be(e,t){for(let n of e){if(n.row.id===t)return n;let e=Be(n.children,t);if(e)return e}}function Ve({tree:e,selectedId:t,onSelect:n,colorEnabled:r,scheme:i,linkedIds:a}){let[o,s]=(0,c.useState)(()=>new Map),u=(0,c.useCallback)(e=>{s(t=>{let n=new Map(t);return n.set(e,t.get(e)===!1),n})},[]);(0,c.useEffect)(()=>{if(!t)return;let n=He(e,t);n.length!==0&&s(e=>{let t=!1,r=new Map(e);for(let e of n)r.get(e)===!1&&(r.set(e,!0),t=!0);return t?r:e})},[e,t]);let d=r?Me(i):null;return e.length===0?(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`No chunks.`})}):(0,l.jsx)(`ul`,{className:`tree chunk-tree`,children:e.map(e=>(0,l.jsx)(Ue,{node:e,expanded:o,toggle:u,selectedId:t,onSelect:n,tintScheme:d,linkedIds:a},e.row.id))})}function He(e,t){let n=[],r=(e,i)=>{for(let a of e){if(a.row.id===t)return n.push(...i),!0;if(r(a.children,[...i,a.row.id]))return!0}return!1};return r(e,[]),n}function Ue({node:e,expanded:t,toggle:n,selectedId:r,onSelect:i,tintScheme:a,linkedIds:o}){let s=e.children.length>0,c=t.get(e.row.id)!==!1,u=r===e.row.id,d=a?Fe(Pe(e.row.status,a)):void 0,f=o?.has(e.row.id);return(0,l.jsxs)(`li`,{className:`node chunk-node ${u?`selected`:``}`,children:[(0,l.jsxs)(`div`,{className:`row chunk-row`,style:d,children:[(0,l.jsx)(`button`,{type:`button`,className:`tree-caret`,"aria-label":c?`collapse`:`expand`,disabled:!s,onClick:t=>{t.stopPropagation(),s&&n(e.row.id)},children:s?c?`▼`:`▶`:`·`}),(0,l.jsxs)(`button`,{type:`button`,className:`chunk-link`,onClick:()=>i(e),title:e.row.title,children:[ze(e.row.type)&&(0,l.jsx)(`span`,{className:`chunk-type-icon`,"aria-hidden":!0,children:ze(e.row.type)}),(0,l.jsx)(`span`,{className:`chunk-title`,children:e.row.title||`(untitled)`}),e.row.type&&(0,l.jsx)(`span`,{className:`chunk-badge`,children:e.row.type}),e.row.status&&(0,l.jsx)(`span`,{className:`chunk-badge chunk-status`,children:e.row.status}),f&&(0,l.jsx)(`span`,{className:`chunk-link-badge`,title:`Has cross-reference links`,"aria-label":`linked`,children:`🔗`})]}),(0,l.jsx)(Le,{value:e.row.id,compact:!0,title:`Copy chunk id: ${e.row.id}`})]}),s&&c&&(0,l.jsx)(`ul`,{className:`children chunk-children`,children:e.children.map(e=>(0,l.jsx)(Ue,{node:e,expanded:t,toggle:n,selectedId:r,onSelect:i,tintScheme:a,linkedIds:o},e.row.id))})]})}var We=0;function Ge(e){let t=e?.closest(`g.node`);return t?/flowchart-(.+)-\d+$/.exec(t.id)?.[1]:void 0}function Ke({code:e,onNodeClick:t}){let n=(0,c.useRef)(null),[r,i]=(0,c.useState)({});return(0,c.useEffect)(()=>{let t=!1;return(async()=>{try{let r=(await import(`mermaid`)).default,a=n.current?.closest(`[data-theme]`)?.getAttribute(`data-theme`),o=!a||a===`dark`;r.initialize({startOnLoad:!1,theme:o?`dark`:`default`,securityLevel:`strict`});let{svg:s}=await r.render(`lc-mmd-${We++}`,e);t||i({svg:s})}catch{t||i({err:!0})}})(),()=>{t=!0}},[e]),r.err?(0,l.jsx)(`pre`,{className:`md-pre md-mermaid-error`,title:`diagram failed to render`,children:(0,l.jsx)(`code`,{children:e})}):r.svg?(0,l.jsx)(`div`,{ref:n,className:t?`md-mermaid md-mermaid-nav`:`md-mermaid`,onClick:t&&(e=>{let n=Ge(e.target);n&&t(n)}),dangerouslySetInnerHTML:{__html:r.svg}}):(0,l.jsx)(`div`,{ref:n,className:`md-mermaid md-mermaid-loading`,children:`rendering diagram…`})}function qe({chunk:e,scope:t,browse:n,onClose:r,onSaved:i}){let a=T(),[o,s]=(0,c.useState)(e.title),[u,d]=(0,c.useState)(e.type??``),[f,p]=(0,c.useState)(e.status??``),[m,h]=(0,c.useState)(e.body??``),[g,_]=(0,c.useState)(e.fields?JSON.stringify(e.fields,null,2):``),[v,y]=(0,c.useState)((e.tags??[]).join(`, `)),[b,x]=(0,c.useState)(!1),[S,C]=(0,c.useState)(null),[w,E]=(0,c.useState)(!1),D=u===`mermaid`,O=u===`image`||!!e.asset,[k,A]=(0,c.useState)(m);(0,c.useEffect)(()=>{if(!D)return;let e=setTimeout(()=>A(m),400);return()=>clearTimeout(e)},[m,D]);let j=async o=>{C(null),x(!0);try{let{mediaType:s,data:c}=await de(o);i(await a.documentSetAsset(e.id,s,c,o.name,t,n)),r()}catch(e){C(e instanceof Error?e.message:String(e)),x(!1)}},M=D?`Mermaid source`:O?`Caption (optional)`:`Body (Markdown)`;return(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:r,children:(0,l.jsxs)(`form`,{className:`modal chunk-editor`,onClick:e=>e.stopPropagation(),onSubmit:async s=>{s.preventDefault(),C(null),E(!1);let c={body:m,title:o,type:u,status:f,tags:v.split(`,`).map(e=>e.trim()).filter(e=>e!==``)};if(g.trim()!==``)try{c.fields=JSON.parse(g)}catch{C(`fields must be valid JSON`);return}x(!0);try{i(await a.documentUpdateChunk(e.id,e.revision,c,t,n)),r()}catch(e){let t=e instanceof Error?e.message:String(e);C(t),E(/revision conflict/i.test(t)),x(!1)}},children:[(0,l.jsxs)(`div`,{className:`modal-header sticky-top`,children:[(0,l.jsx)(`h3`,{children:`Edit chunk`}),(0,l.jsxs)(`div`,{className:`modal-buttons modal-buttons-top`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:r,disabled:b,children:`cancel`}),(0,l.jsx)(`button`,{type:`submit`,className:`primary`,disabled:b,children:b?`saving…`:`save`})]})]}),S&&(0,l.jsxs)(`div`,{className:`modal-err`,children:[S,w&&` — reopen the chunk to load the latest, then reapply your edit.`]}),(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Title`}),(0,l.jsx)(`input`,{className:`path-modal-input`,value:o,onChange:e=>s(e.target.value)})]}),(0,l.jsxs)(`div`,{className:`chunk-editor-row`,children:[(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Type`}),(0,l.jsx)(`input`,{className:`path-modal-input`,value:u,placeholder:`(none)`,onChange:e=>d(e.target.value)})]}),(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Status`}),(0,l.jsx)(`input`,{className:`path-modal-input`,value:f,placeholder:`(none)`,onChange:e=>p(e.target.value)})]})]}),(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Tags`}),(0,l.jsx)(`input`,{className:`path-modal-input`,value:v,placeholder:`comma-separated, nest with a slash: area/sub, priority/high`,onChange:e=>y(e.target.value)})]}),O&&(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Replace image`}),(0,l.jsx)(`input`,{type:`file`,accept:`image/png,image/jpeg,image/gif,image/webp`,disabled:b,onChange:e=>{let t=e.target.files?.[0];t&&j(t)}})]}),(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:M}),(0,l.jsx)(`textarea`,{className:`modal-textarea chunk-body-input`,rows:D?8:12,value:m,onChange:e=>h(e.target.value)})]}),D&&(0,l.jsxs)(`div`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Preview`}),(0,l.jsx)(`div`,{className:`chunk-mermaid-preview`,children:k.trim()?(0,l.jsx)(Ke,{code:k}):(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`(enter Mermaid source above)`})})]}),(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Fields (JSON, optional)`}),(0,l.jsx)(`textarea`,{className:`modal-textarea chunk-fields-input`,rows:4,value:g,placeholder:`{}`,onChange:e=>_(e.target.value)})]})]})})}function Je({chunk:e,scope:t,browse:n,onClose:r,onSaved:i}){let a=T(),[o,s]=(0,c.useState)(null),[u,d]=(0,c.useState)(e.type??``),[f,p]=(0,c.useState)(!1),[m,h]=(0,c.useState)(null),[g,_]=(0,c.useState)(null);(0,c.useEffect)(()=>{let e=!1;return a.documentEntityTypes?.(t,n).then(t=>{e||s(t)}).catch(t=>{e||h(t instanceof Error?t.message:String(t))}),()=>{e=!0}},[a,t,n]);let y=o?v(o,e.type):[],b=u.toLowerCase()===(e.type??``).toLowerCase();return(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:r,children:(0,l.jsxs)(`form`,{className:`modal`,onClick:e=>e.stopPropagation(),onSubmit:async o=>{if(o.preventDefault(),!(b||f)){h(null),p(!0);try{let o=await a.documentUpdateChunk(e.id,e.revision,{type:u},t,n);if(i(o),o.not_announced){_(o.not_announced),p(!1);return}r()}catch(e){h(e instanceof Error?e.message:String(e)),p(!1)}}},children:[(0,l.jsx)(`h3`,{children:`Change type`}),(0,l.jsxs)(`p`,{className:`modal-context`,children:[`“`,e.title||`(untitled)`,`” is a subject (`,(0,l.jsx)(`code`,{children:e.entity?.natural_key}),`). Its key stays as it is; relations about it are re-checked against the new type.`]}),m&&(0,l.jsx)(`div`,{className:`modal-err`,children:m}),g?(0,l.jsxs)(`div`,{className:`modal-err`,children:[`The type was changed, but subscribers were not told: `,g]}):(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:`Type`}),o===null?(0,l.jsx)(`span`,{className:`doc-empty-body`,children:m?``:`loading types…`}):(0,l.jsx)(`select`,{className:`path-modal-input`,value:u,disabled:f,onChange:e=>d(e.target.value),children:y.map(e=>(0,l.jsx)(`option`,{value:e,children:e},e))})]}),(0,l.jsxs)(`div`,{className:`modal-buttons`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:r,disabled:f,children:g?`close`:`cancel`}),!g&&(0,l.jsx)(`button`,{type:`submit`,className:`primary`,disabled:f||b||o===null,children:f?`saving…`:`change type`})]})]})})}function Ye(e){return Array.isArray(e)&&e.every(e=>typeof e==`string`)}function Xe(e){if(!e||typeof e!=`object`)return;let t=e.classification;if(!t||typeof t!=`object`||Array.isArray(t))return;let n=t,r={};return typeof n.by==`string`&&(r.by=n.by),typeof n.at==`number`&&(r.at=n.at),typeof n.applied_type==`string`&&n.applied_type&&(r.applied_type=n.applied_type),typeof n.proposed_type==`string`&&n.proposed_type&&(r.proposed_type=n.proposed_type),n.proposed_fields&&typeof n.proposed_fields==`object`&&!Array.isArray(n.proposed_fields)&&(r.proposed_fields=n.proposed_fields),Ye(n.proposed_tags)&&(r.proposed_tags=n.proposed_tags),Ye(n.statements)&&(r.statements=n.statements),r}function Ze(e){let t=Xe(e);if(!t)return;let n=t.proposed_tags??[],r=t.statements?.length??0;if(t.proposed_type||n.length!==0||r!==0)return{proposedType:t.proposed_type,proposedTags:n,statementCount:r}}function Qe(e){let t=e.write===`report`;switch(e.result){case`classified`:return t?`would be classified`:`classified`;case`proposed`:return t?`would be proposed for review`:`proposed — waits for review`;case`kept`:return`kept — the type was set by hand`;case`unclassified`:return`unclassified — nothing decided`;default:return String(e.result)}}function $e(e){if(!e.class)return;if(e.result===`kept`)return`not applied`;let t=e.write===`report`;return e.live?t?`would be applied`:`applied`:t?`would be proposed`:`proposed`}function et(e){return e.write===`report`?e.result===`classified`||e.result===`proposed`||(e.concepts?.length??0)>0||(e.issued_by?.length??0)>0:!1}function tt(e){return e==null||!Number.isFinite(e)?``:`${Math.round(e*100)}%`}var nt={rules:`Rules`,vote:`Neighbourhood vote`,model:`Model`};function rt(e){return nt[e]??e}function it(e){let t=[];e.quote&&t.push(e.from?`“${e.quote}” (${e.from})`:`“${e.quote}”`),e.votes!=null&&t.push(`${e.votes} vote${e.votes===1?``:`s`}`);let n=tt(e.share);return n&&t.push(n),t.join(` · `)}function at(e){return{label:rt(e.step),...e.chosen?{chosen:e.chosen}:{},...e.method?{method:e.method}:{},...e.neighbours?{neighbours:e.neighbours}:{},...e.reason?{reason:e.reason}:{},candidates:(e.candidates??[]).map(e=>({class:e.class,detail:it(e)}))}}function ot(e){let t=[];e.type&&t.push(`type ${e.type}`);let n=e.accepted_statements?.length??0;n>0&&t.push(`${n} statement${n===1?``:`s`}`),e.tags&&e.tags.length>0&&t.push(`tags ${e.tags.join(`, `)}`);let r=[];e.type_pending&&r.push(`The proposed class stays pending: another document already holds its identity.`),e.skipped_type&&r.push(`Class ${e.skipped_type} was not applied: it is not a declared document class.`);let i=e.skipped_statements?.length??0;return i>0&&r.push(`${i} statement${i===1?` was`:`s were`} skipped (already ruled on, or not this document's proposal).`),e.skipped_tags&&e.skipped_tags.length>0&&r.push(`Tags not applied (not concepts of the ontology): ${e.skipped_tags.join(`, `)}.`),r.push(...e.warnings??[]),{applied:t.length>0?`Accepted ${t.join(`, `)}.`:`Nothing was accepted.`,notices:r}}function st(e){let t=e instanceof Error?e.message:String(e);return t.includes(`nothing_pending`)?`Nothing is pending for this document — it may already have been accepted.`:t}function ct({documentId:e,scope:t,browse:n,rootFields:r,requestKey:i,onClose:a,onChanged:o}){let s=T(),[u,d]=(0,c.useState)(null),[f,p]=(0,c.useState)(!1),[m,h]=(0,c.useState)(null),[g,_]=(0,c.useState)(!1),[v,y]=(0,c.useState)(null),[b,x]=(0,c.useState)(null),S=(0,c.useMemo)(()=>s.documentAcceptClassification?Ze(r):void 0,[s,r]);(0,c.useEffect)(()=>{if(i===0||!s.documentClassify)return;let r=!1;return p(!0),h(null),d(null),s.documentClassify(e,`report`,t,n).then(e=>!r&&d(e)).catch(e=>!r&&h(st(e))).finally(()=>!r&&p(!1)),()=>{r=!0}},[s,e,t,n,i]);let C=async()=>{if(s.documentClassify){p(!0),h(null);try{d(await s.documentClassify(e,`propose`,t,n)),o()}catch(e){h(st(e))}finally{p(!1)}}},w=async()=>{if(s.documentAcceptClassification){_(!0),y(null);try{x(await s.documentAcceptClassification(e,t,n)),o()}catch(e){y(st(e))}finally{_(!1)}}},E=i>0;if(!E&&!S&&!b&&!v)return null;let D=b?ot(b):void 0;return(0,l.jsxs)(`div`,{className:`doc-classify`,children:[S&&(0,l.jsxs)(`div`,{className:`doc-classify-pending`,children:[(0,l.jsx)(`span`,{className:`doc-classify-label`,children:`Pending classification`}),S.proposedType&&(0,l.jsxs)(`span`,{children:[`class `,(0,l.jsx)(`strong`,{children:S.proposedType})]}),S.proposedTags.length>0&&(0,l.jsxs)(`span`,{children:[`tags`,` `,S.proposedTags.map(e=>(0,l.jsx)(`span`,{className:`chunk-tag`,children:e},e))]}),S.statementCount>0&&(0,l.jsxs)(`span`,{children:[S.statementCount,` proposed statement`,S.statementCount===1?``:`s`]}),(0,l.jsx)(`button`,{type:`button`,disabled:g,onClick:()=>void w(),children:g?`Accepting…`:`Accept`})]}),v&&(0,l.jsxs)(`div`,{className:`doc-classify-err`,role:`alert`,children:[v,(0,l.jsx)(`button`,{type:`button`,className:`doc-classify-dismiss`,onClick:()=>y(null),"aria-label":`Dismiss`,children:`×`})]}),D&&(0,l.jsxs)(`div`,{className:`doc-classify-notice`,role:`status`,children:[(0,l.jsx)(`span`,{children:D.applied}),D.notices.map((e,t)=>(0,l.jsx)(`span`,{className:`doc-classify-warn`,children:e},t)),(0,l.jsx)(`button`,{type:`button`,className:`doc-classify-dismiss`,onClick:()=>x(null),"aria-label":`Dismiss`,children:`×`})]}),E&&(0,l.jsxs)(`div`,{className:`doc-classify-panel`,children:[(0,l.jsxs)(`div`,{className:`doc-classify-head`,children:[(0,l.jsx)(`h4`,{children:`Classify`}),u&&(0,l.jsx)(`span`,{className:`doc-classify-result ${u.result}`,children:Qe(u)}),u?.write===`report`&&(0,l.jsx)(`span`,{className:`doc-classify-hint`,children:`report only — nothing written`}),(0,l.jsx)(`button`,{type:`button`,className:`doc-classify-dismiss`,onClick:a,"aria-label":`Close`,children:`×`})]}),!u&&!m&&(0,l.jsx)(`p`,{className:`doc-classify-hint`,children:`Classifying…`}),m&&(0,l.jsx)(`p`,{className:`doc-classify-err`,role:`alert`,children:m}),u&&(0,l.jsx)(lt,{answer:u}),u&&et(u)&&(0,l.jsx)(`div`,{className:`doc-classify-actions`,children:(0,l.jsx)(`button`,{type:`button`,disabled:f,onClick:()=>void C(),title:`Apply a unique rule match to the document; file everything else for review`,children:f?`Applying…`:`Apply`})})]})]})}function lt({answer:e}){let t=$e(e),n=Object.entries(e.fields??{}),r=e.issued_by??[],i=e.concepts??[],a=(e.steps??[]).map(at),o=e.statements?.length??0;return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(`dl`,{className:`doc-classify-rows`,children:[e.class&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`class`}),(0,l.jsxs)(`dd`,{children:[(0,l.jsx)(`strong`,{children:e.class}),t&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` (`,t,`)`]})]})]}),e.work_key&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`work key`}),(0,l.jsx)(`dd`,{className:`doc-classify-mono`,children:e.work_key})]}),n.length>0&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`fields`}),(0,l.jsx)(`dd`,{className:`doc-classify-mono`,children:n.map(([e,t])=>`${e}=${typeof t==`string`?t:JSON.stringify(t)}`).join(` · `)})]}),r.length>0&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`issued by`}),(0,l.jsx)(`dd`,{children:r.map((e,t)=>(0,l.jsxs)(`span`,{className:`doc-classify-item`,title:e.quote,children:[e.instance,e.quote&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` “`,e.quote,`”`]})]},`${e.instance}-${t}`))})]}),i.length>0&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`concepts`}),(0,l.jsx)(`dd`,{children:i.map(e=>(0,l.jsxs)(`span`,{className:`doc-classify-item`,title:e.tag,children:[e.concept,(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` `,tt(e.share)]})]},e.tag))})]}),o>0&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`statements`}),(0,l.jsxs)(`dd`,{children:[o,` written`,e.statements.some(e=>e.status===`proposed`)&&(0,l.jsx)(`span`,{className:`doc-classify-hint`,children:` (proposed ones wait for review)`})]})]})]}),a.length>0&&(0,l.jsx)(`ol`,{className:`doc-classify-steps`,children:a.map((e,t)=>(0,l.jsxs)(`li`,{children:[(0,l.jsx)(`span`,{className:`doc-classify-step`,children:e.label}),e.method&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` [`,e.method,`]`]}),e.chosen?(0,l.jsxs)(l.Fragment,{children:[` → `,(0,l.jsx)(`strong`,{children:e.chosen})]}):(0,l.jsx)(`span`,{className:`doc-classify-hint`,children:` no choice`}),e.neighbours!=null&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` · `,e.neighbours,` neighbours`]}),e.reason&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` — `,e.reason]}),e.candidates.length>0&&(0,l.jsxs)(`details`,{className:`doc-classify-candidates`,children:[(0,l.jsxs)(`summary`,{children:[e.candidates.length,` candidate`,e.candidates.length===1?``:`s`]}),(0,l.jsx)(`ul`,{children:e.candidates.map((e,t)=>(0,l.jsxs)(`li`,{children:[(0,l.jsx)(`strong`,{children:e.class}),e.detail&&(0,l.jsxs)(`span`,{className:`doc-classify-hint`,children:[` `,e.detail]})]},`${e.class}-${t}`))})]})]},`${e.label}-${t}`))}),e.warnings&&e.warnings.length>0&&(0,l.jsx)(`ul`,{className:`doc-classify-warnings`,children:e.warnings.map((e,t)=>(0,l.jsx)(`li`,{className:`doc-classify-warn`,children:e},t))})]})}function ut(e){let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec((e??``).trim());if(!t)return`#888888`;let n=t[1];return n.length===3&&(n=n[0]+n[0]+n[1]+n[1]+n[2]+n[2]),`#`+n.toLowerCase()}function dt({documentId:e,rootChunkId:t,docType:n,docStatus:r,chunkStatuses:i,scope:a,browse:o,onClose:s,onSaved:u}){let d=T(),[f,p]=(0,c.useState)(null),[m,h]=(0,c.useState)(!1),[g,_]=(0,c.useState)({}),[v,y]=(0,c.useState)(!1),[b,x]=(0,c.useState)(null),[S,C]=(0,c.useState)(null),w=(0,c.useMemo)(()=>{let e=new Set(Object.keys(Ae));n&&e.add(`doc.${n}.${r||`draft`}`);for(let t of i)t&&e.add(`chunk.${t}`);return[...e].sort()},[n,r,i]);(0,c.useEffect)(()=>{let e=!1;return d.documentGetChunk(t,a,o).then(t=>{if(e)return;p(t);let n=t.fields??{};h(n.color_enabled===!0);let r=n.color_scheme??{};_({...Ae,...r})}).catch(t=>!e&&C(t instanceof Error?t.message:String(t))),()=>{e=!0}},[d,t,a,o]);let E=(e,t)=>_(n=>({...n,[e]:t})),D=e=>({...e??{},color_enabled:m,color_scheme:g}),O=async()=>{if(f){y(!0),C(null),x(null);try{await d.documentUpdateChunk(t,f.revision,{fields:D(f.fields)},a,o),u(),s()}catch(e){C(e instanceof Error?e.message:String(e)),y(!1)}}},k=async()=>{if(!n)C(`this document has no type to match`);else{y(!0),C(null),x(null);try{let{documents:t}=await d.documentsSummary({underPath:`/`},a,o),r=(t??[]).filter(t=>t.type===n&&t.document_id!==e&&t.root_chunk_id),i=0;for(let e of r)try{let t=await d.documentGetChunk(e.root_chunk_id,a,o);await d.documentUpdateChunk(e.root_chunk_id,t.revision,{fields:D(t.fields)},a,o),i++}catch{}x(`Applied to ${i} other ${n} document(s).`),u()}catch(e){C(e instanceof Error?e.message:String(e))}finally{y(!1)}}},A=w.filter(e=>e.startsWith(`doc.`)),j=w.filter(e=>e.startsWith(`chunk.`));return(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:s,children:(0,l.jsxs)(`div`,{className:`modal scheme-editor`,onClick:e=>e.stopPropagation(),children:[(0,l.jsxs)(`div`,{className:`scheme-editor-head`,children:[(0,l.jsx)(`h3`,{children:`Document colors`}),(0,l.jsxs)(`div`,{className:`scheme-editor-actions`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:s,disabled:v,children:`cancel`}),(0,l.jsx)(`button`,{type:`button`,className:`primary`,onClick:()=>void O(),disabled:v||!f,children:v?`saving…`:`save`})]})]}),S&&(0,l.jsx)(`div`,{className:`modal-err`,children:S}),f?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(`label`,{className:`scheme-toggle`,children:[(0,l.jsx)(`input`,{type:`checkbox`,checked:m,onChange:e=>h(e.target.checked)}),(0,l.jsx)(`span`,{children:`Enable coloring for this document`})]}),(0,l.jsx)(`p`,{className:`modal-context`,children:`Document rows are tinted by type + status; chunk tiles by status. Colors apply as a semi-transparent wash so text stays readable.`}),(0,l.jsxs)(`div`,{className:`scheme-group`,children:[(0,l.jsx)(`h4`,{children:`Document (type · status)`}),A.map(e=>(0,l.jsx)(ft,{label:e.slice(4),value:g[e],onChange:t=>E(e,t)},e))]}),(0,l.jsxs)(`div`,{className:`scheme-group`,children:[(0,l.jsx)(`h4`,{children:`Chunks (status)`}),j.map(e=>(0,l.jsx)(ft,{label:e.slice(6),value:g[e],onChange:t=>E(e,t)},e))]}),(0,l.jsxs)(`div`,{className:`scheme-editor-foot`,children:[(0,l.jsxs)(`button`,{type:`button`,onClick:()=>void k(),disabled:v||!n,title:n?`Apply this palette to every other ${n} document`:`This document has no type`,children:[`copy to all `,n||`same-type`,` documents`]}),b&&(0,l.jsx)(`span`,{className:`scheme-status`,children:b})]})]}):(0,l.jsx)(`p`,{className:`modal-context`,children:`Loading…`})]})})}function ft({label:e,value:t,onChange:n}){let r=ut(t);return(0,l.jsxs)(`label`,{className:`scheme-row`,children:[(0,l.jsx)(`input`,{type:`color`,value:r,onChange:e=>n(e.target.value)}),(0,l.jsx)(`span`,{className:`scheme-key`,children:e}),(0,l.jsx)(`code`,{className:`scheme-hex`,children:r})]})}function pt({id:e,title:t,targetDocumentId:n,documentId:r,onSelectChunk:i,onOpenDocument:a}){let o=t||e.slice(0,8);return!n||n===r?(0,l.jsx)(`button`,{type:`button`,className:`doc-ref-target`,onClick:()=>i(e),children:o}):a?(0,l.jsxs)(`button`,{type:`button`,className:`doc-ref-target external`,title:`In another document — opens it at this chunk`,onClick:()=>a(n,e,t),children:[o,` ↗`]}):(0,l.jsxs)(`span`,{className:`doc-ref-target external`,title:`In another document`,children:[o,` ↗`]})}function mt({statement:e}){let t=_e(e),n=ve(e),r=be(e),i=ye(e),{rows:a,more:o}=xe(e);return(0,l.jsxs)(l.Fragment,{children:[t&&(0,l.jsx)(`span`,{className:`doc-stmt-status ${t}`,children:t}),n&&(0,l.jsx)(`span`,{className:`doc-stmt-method`,title:`how this relation was made`,children:n}),r&&(0,l.jsx)(`span`,{className:`doc-stmt-verdict`,title:r,children:`judged`}),i&&(0,l.jsx)(`span`,{className:`doc-stmt-valid`,children:i}),a.length>0&&(0,l.jsxs)(`ul`,{className:`doc-stmt-evidence`,children:[a.map(e=>(0,l.jsxs)(`li`,{title:e.quote,children:[`“`,Se(e.quote),`”`,e.stale&&(0,l.jsx)(`span`,{className:`doc-stmt-stale`,children:` (source changed)`})]},e.id)),o>0&&(0,l.jsxs)(`li`,{className:`doc-stmt-more`,children:[`+`,o,` more`]})]})]})}function ht({id:e,onRule:t}){let[n,r]=(0,c.useState)(!1),[i,a]=(0,c.useState)(``),[o,s]=(0,c.useState)(!1),[u,d]=(0,c.useState)(null),f=async n=>{s(!0),d(null);try{await t(e,n,n?void 0:i.trim()),r(!1),a(``)}catch(e){d(e instanceof Error?e.message:String(e))}finally{s(!1)}};return(0,l.jsxs)(`span`,{className:`doc-stmt-review`,children:[n?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`input`,{type:`text`,"aria-label":`Reason for rejecting`,placeholder:`why reject?`,value:i,disabled:o,onChange:e=>a(e.target.value),onKeyDown:e=>{e.key===`Enter`&&i.trim()&&f(!1),e.key===`Escape`&&r(!1)},autoFocus:!0}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,disabled:o||!i.trim(),onClick:()=>void f(!1),children:`Reject`}),(0,l.jsx)(`button`,{type:`button`,disabled:o,onClick:()=>r(!1),children:`Cancel`})]}):(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`button`,{type:`button`,disabled:o,onClick:()=>void f(!0),children:`Accept`}),(0,l.jsx)(`button`,{type:`button`,disabled:o,onClick:()=>r(!0),children:`Reject…`})]}),u&&(0,l.jsx)(`span`,{className:`doc-stmt-err`,role:`alert`,children:u})]})}var gt=3;function _t({documentId:e,proposals:t,incomplete:n,scope:r,browse:i,onChanged:a,onSelectChunk:o,onOpenDocument:s}){let u=T(),[d,f]=(0,c.useState)(new Set),[p,m]=(0,c.useState)({kind:`idle`}),[h,g]=(0,c.useState)(``),[_,v]=(0,c.useState)(null),[y,b]=(0,c.useState)(null),x=(0,c.useRef)(null),S=new Set(t.map(e=>e.id)),C=[...d].filter(e=>S.has(e)),w=p.kind!==`idle`&&p.kind!==`reason`,E=(e,t)=>{v(null),b(null),e===`reject`?m({kind:`reason`,action:e,all:t}):D(e,t,``)},D=async(t,n,o)=>{m({kind:`counting`,action:t,all:n});let s;try{s=await Z(u,{documentId:e,action:t,ids:n?void 0:C,reason:o.trim()||void 0,scope:r,browse:i},{confirm:(e,r)=>new Promise(i=>{x.current=i,m({kind:`confirm`,action:t,all:n,dry:e,changed:r!==`first`})}),onProgress:e=>m({kind:`ruling`,action:t,ruled:e.ruled,total:e.total})})}catch(e){s={rulingIds:[],ruled:0,failed:0,skipped:0,total:0,cancelled:!1,failures:[],error:e}}x.current=null,m({kind:`idle`}),g(``),s.rulingIds.length>0?(f(new Set),b({text:Q(t,s),rulingIds:s.rulingIds}),a()):s.error===void 0&&!s.cancelled&&b({text:`Nothing to rule: no proposals match.`,rulingIds:[]}),s.error!==void 0&&v(s.error instanceof Error?s.error.message:String(s.error))},O=e=>{e&&p.kind===`confirm`&&m({kind:`ruling`,action:p.action}),x.current?.(e)},k=async()=>{if(!y||!u.documentRevertRuling)return;let e=y,t=await Ee(e=>u.documentRevertRuling(e,r,i),e.rulingIds),n=[`${t.reverted} came back to review`];t.changed>0&&n.push(`${t.changed} changed since and left as ruled`),t.errors.length>0&&n.push(`not undone: ${t.errors.join(`; `)}`),b({...e,undone:!0,text:n.join(`; `)+`.`}),a()},A=e=>f(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n}),j=t.length>0&&C.length===t.length,M=t.length===0&&!n;return(0,l.jsxs)(`div`,{className:`doc-conn-group doc-bulk`,children:[(0,l.jsxs)(`h5`,{children:[`Proposals from this document (`,t.length,`)`]}),(0,l.jsxs)(`div`,{className:`doc-bulk-bar`,children:[(0,l.jsxs)(`label`,{children:[(0,l.jsx)(`input`,{type:`checkbox`,"aria-label":`select every proposal shown`,checked:j,disabled:w||t.length===0,onChange:()=>f(j?new Set:new Set(S))}),` `,`all shown`]}),(0,l.jsxs)(`button`,{type:`button`,disabled:w||C.length===0,onClick:()=>E(`accept`,!1),children:[`Accept selected (`,C.length,`)`]}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,disabled:w||C.length===0,onClick:()=>E(`reject`,!1),children:`Reject selected…`}),(0,l.jsx)(`button`,{type:`button`,disabled:w||M,onClick:()=>E(`accept`,!0),children:`Accept all from this document`}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,disabled:w||M,onClick:()=>E(`reject`,!0),children:`Reject all…`})]}),p.kind===`reason`&&(0,l.jsxs)(`form`,{className:`doc-bulk-step`,onSubmit:e=>{e.preventDefault(),h.trim()&&D(p.action,p.all,h)},children:[(0,l.jsx)(`input`,{type:`text`,"aria-label":`Reason for rejecting`,placeholder:`why reject?`,value:h,onChange:e=>g(e.target.value),autoFocus:!0}),(0,l.jsx)(`button`,{type:`submit`,className:`danger`,disabled:!h.trim(),children:`Count`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>m({kind:`idle`}),children:`Cancel`})]}),p.kind===`counting`&&(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`counting…`}),p.kind===`confirm`&&(0,l.jsx)(vt,{step:p,onAnswer:O}),p.kind===`ruling`&&(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:p.total?`ruled ${p.ruled??0} of ${p.total}…`:`ruling…`}),_&&(0,l.jsx)(`p`,{className:`doc-stmt-err`,role:`alert`,children:_}),y&&(0,l.jsxs)(`p`,{className:`doc-bulk-result`,role:`status`,children:[y.text,y.rulingIds.length>0&&!y.undone&&u.documentRevertRuling&&(0,l.jsx)(`button`,{type:`button`,onClick:()=>void k(),children:`Undo`})]}),t.length===0?(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`none`}):(0,l.jsx)(`ul`,{className:`doc-conn-list`,children:t.map(t=>(0,l.jsxs)(`li`,{className:`doc-conn-row`,children:[(0,l.jsx)(`input`,{type:`checkbox`,"aria-label":`select ${t.from_title??t.from_id} ${t.kind} ${t.to_title??t.to_id}`,checked:d.has(t.id),disabled:w,onChange:()=>A(t.id)}),(0,l.jsx)(pt,{id:t.from_id,title:t.from_title,targetDocumentId:t.from_document_id,documentId:e,onSelectChunk:o,onOpenDocument:s}),(0,l.jsx)(`span`,{className:`doc-ref-kind`,children:t.kind}),(0,l.jsx)(pt,{id:t.to_id,title:t.to_title,targetDocumentId:t.to_document_id,documentId:e,onSelectChunk:o,onOpenDocument:s}),(0,l.jsx)(mt,{statement:t})]},t.id))})]})}function vt({step:e,onAnswer:t}){let{dry:n}=e,r=e.action===`accept`?`Accept`:`Reject`,i=Y(n.matched);return(0,l.jsxs)(`div`,{className:`doc-bulk-step doc-bulk-confirm`,children:[e.changed&&(0,l.jsx)(`p`,{className:`doc-bulk-warn`,children:`The proposals changed since the last count; counted again.`}),(0,l.jsxs)(`p`,{children:[r,` `,n.matched,` statement`,n.matched===1?``:`s`,e.all?` filed from this document`:``,i>1?`, in ${i} rulings of up to 1,000`:``,`?`,n.skipped>0&&` ${n.skipped} of them are not yours to rule and will be skipped.`]}),(n.sample??[]).length>0&&(0,l.jsx)(`ul`,{className:`doc-bulk-sample`,children:(n.sample??[]).slice(0,gt).map(e=>(0,l.jsxs)(`li`,{children:[e.from_title||e.from_id,` `,(0,l.jsx)(`span`,{className:`doc-ref-kind`,children:e.kind}),` `,e.to_title||e.to_id]},e.id))}),(0,l.jsxs)(`button`,{type:`button`,className:e.action===`reject`?`danger`:void 0,onClick:()=>t(!0),children:[r,` `,n.matched]}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>t(!1),children:`Cancel`})]})}var yt=8,bt=20;function xt(e){return/embedder|embedding|vector|semantic|not configured/i.test(e)}function St({documentId:e,selectedId:t,scope:n,browse:r,onSelectChunk:i,onOpenDocument:a,refreshKey:o,review:s}){let u=T(),[d,f]=(0,c.useState)(!1),[p,m]=(0,c.useState)(!1),[h,g]=(0,c.useState)(void 0),[_,v]=(0,c.useState)([]),[y,b]=(0,c.useState)([]),[x,S]=(0,c.useState)(null),[C,w]=(0,c.useState)([]),[E,D]=(0,c.useState)(!1);(0,c.useEffect)(()=>{if(!d||!t)return;let e=t,i=!1;return m(!0),Promise.allSettled([u.documentBacklinks(e,n,r),u.documentRelated(e,n,r,yt),u.documentUnlinkedMentions(e,n,r,bt)]).then(([t,n,r])=>{if(!i){if(v(t.status===`fulfilled`?t.value.backlinks??[]:[]),n.status===`fulfilled`)b(n.value.related??[]),S(null);else{b([]);let e=n.reason instanceof Error?n.reason.message:String(n.reason);S(xt(e)?`semantic search not configured`:e)}r.status===`fulfilled`?(w(r.value.unlinked_mentions??[]),D(!!r.value.truncated)):(w([]),D(!1)),g(e),m(!1)}}),()=>{i=!0}},[u,d,t,n,r,o]);let O=(t,n,r)=>(0,l.jsx)(pt,{id:t,title:n,targetDocumentId:r,documentId:e,onSelectChunk:i,onOpenDocument:a}),k=!!(d&&t&&!p&&h===t);return(0,l.jsxs)(`div`,{className:`doc-conn`,children:[(0,l.jsxs)(`button`,{type:`button`,className:`doc-conn-toggle`,onClick:()=>f(e=>!e),children:[d?`▼`:`▶`,` Connections`]}),d&&(0,l.jsxs)(`div`,{className:`doc-conn-body`,children:[s&&(0,l.jsx)(_t,{documentId:e,proposals:s.proposals,incomplete:s.incomplete,scope:s.scope,browse:r,onChanged:s.onChanged,onSelectChunk:i,onOpenDocument:a}),t?k?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(`div`,{className:`doc-conn-group`,children:[(0,l.jsx)(`h5`,{children:`Backlinks`}),_.length===0?(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`none`}):(0,l.jsx)(`ul`,{className:`doc-conn-list`,children:_.map((e,t)=>(0,l.jsxs)(`li`,{className:`doc-conn-row`,children:[O(e.from_id,e.from_title,e.from_document_id),(0,l.jsx)(`span`,{className:`doc-ref-kind`,children:e.kind}),e.auto&&(0,l.jsx)(`span`,{className:`doc-conn-auto`,title:`Auto-linked from a [[wikilink]]`,children:`auto`}),(0,l.jsx)(mt,{statement:e})]},`${e.from_id}-${e.kind}-${t}`))})]}),(0,l.jsxs)(`div`,{className:`doc-conn-group`,children:[(0,l.jsx)(`h5`,{children:`Related`}),x?(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:x}):y.length===0?(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`none`}):(0,l.jsx)(`ul`,{className:`doc-conn-list`,children:y.map((e,t)=>(0,l.jsxs)(`li`,{className:`doc-conn-row`,children:[O(e.chunk_id,e.title,e.document_id),(0,l.jsx)(`span`,{className:`doc-conn-score`,title:`similarity score`,children:e.score.toFixed(2)})]},`${e.chunk_id}-${t}`))})]}),(0,l.jsxs)(`div`,{className:`doc-conn-group`,children:[(0,l.jsx)(`h5`,{children:`Unlinked mentions`}),C.length===0?(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`none`}):(0,l.jsxs)(`ul`,{className:`doc-conn-list`,children:[C.map((e,t)=>(0,l.jsx)(`li`,{className:`doc-conn-row`,children:O(e.chunk_id,e.title,e.document_id)},`${e.chunk_id}-${t}`)),E&&(0,l.jsx)(`li`,{className:`doc-conn-more`,"aria-hidden":!0,children:`(more…)`})]})]})]}):(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`loading…`}):(0,l.jsx)(`p`,{className:`doc-conn-empty`,children:`Select a chunk.`})]})]})}var Ct=24;function wt({edges:e,documentId:t,selectedId:n,onSelectChunk:r,onOpenDocument:i,onRuleStatement:a,incomplete:o}){let[s,u]=(0,c.useState)(!1),[d,f]=(0,c.useState)(!1);if(e.length===0)return null;let p=n?e.filter(e=>e.from_id===n||e.to_id===n):[],m=d?e:e.slice(0,Ct),h=Et(m);return(0,l.jsxs)(`div`,{className:`doc-refs`,children:[o&&(0,l.jsxs)(`p`,{className:`doc-refs-incomplete`,children:[`Showing the first `,e.length,` links of this document; it has more, which are not shown here or in the graph.`]}),n&&(0,l.jsxs)(`div`,{className:`doc-refs-list-wrap`,children:[(0,l.jsx)(`h4`,{children:`References`}),p.length===0?(0,l.jsx)(`p`,{className:`doc-refs-empty`,children:`This chunk has no links.`}):(0,l.jsx)(`ul`,{className:`doc-refs-list`,children:p.map((e,o)=>(0,l.jsx)(Tt,{edge:e,selectedId:n,documentId:t,onSelectChunk:r,onOpenDocument:i,onRuleStatement:a},`${e.from_id}-${e.to_id}-${e.kind}-${o}`))})]}),(0,l.jsxs)(`div`,{className:`doc-graph`,children:[(0,l.jsxs)(`button`,{type:`button`,className:`doc-graph-toggle`,onClick:()=>u(e=>!e),children:[s?`▼`:`▶`,` relationship graph (`,e.length,`)`]}),s&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(Ke,{code:h,onNodeClick:e=>{let n=Dt(m,e);n&&(!n.documentId||n.documentId===t?r(n.id):i?.(n.documentId,n.id,n.title))}}),e.length>Ct&&(0,l.jsx)(`button`,{type:`button`,className:`doc-graph-more`,onClick:()=>f(e=>!e),children:d?`show top ${Ct}`:`show all ${e.length}`})]})]})]})}function Tt({edge:e,selectedId:t,documentId:n,onSelectChunk:r,onOpenDocument:i,onRuleStatement:a}){let o=e.from_id===t,s=o?e.to_id:e.from_id,c=o?e.to_title:e.from_title,u=o?e.to_document_id:e.from_document_id;return(0,l.jsxs)(`li`,{className:`doc-ref`,children:[(0,l.jsx)(`span`,{className:`doc-ref-kind`,children:e.kind}),(0,l.jsx)(`span`,{className:`doc-ref-arrow`,"aria-hidden":!0,children:o?`→`:`←`}),(0,l.jsx)(pt,{id:s,title:c,targetDocumentId:u,documentId:n,onSelectChunk:r,onOpenDocument:i}),(0,l.jsx)(mt,{statement:e}),a&&G(e)&&(0,l.jsx)(ht,{id:e.id,onRule:a})]})}function Et(e){let t=[`graph LR`];for(let n of e){let e=`${Ot(n.from_id)}["${kt(n.from_title,n.from_id)}"]`,r=`${Ot(n.to_id)}["${kt(n.to_title,n.to_id)}"]`,i=(n.kind||`link`).replace(/[|"]/g,` `),a=n.status===`proposed`?`-.->`:`-->`;t.push(` ${e} ${a}|${i}| ${r}`)}return t.join(`
|
|
2
|
+
`)}function Dt(e,t){for(let n of e){if(Ot(n.from_id)===t)return{id:n.from_id,documentId:n.from_document_id,title:n.from_title};if(Ot(n.to_id)===t)return{id:n.to_id,documentId:n.to_document_id,title:n.to_title}}}function Ot(e){return`n`+e.replace(/[^a-zA-Z0-9]/g,``)}function kt(e,t){let n=(e??``).replace(/["[\]{}|<>]/g,` `).replace(/\s+/g,` `).trim()||t.slice(0,6);return n.length>30?n.slice(0,29)+`…`:n}function At({documentId:e,scope:t,detail:n}){return(0,l.jsxs)(`div`,{className:`doc-unavailable`,role:`alert`,children:[(0,l.jsx)(`p`,{className:`doc-unavailable-lead`,children:`This document can’t be opened.`}),(0,l.jsxs)(`p`,{children:[`It may have been deleted, live in a scope other than `,(0,l.jsx)(`code`,{children:t}),`, or not be yours to read.`]}),(0,l.jsxs)(`p`,{className:`doc-unavailable-detail`,children:[(0,l.jsx)(`code`,{children:e}),n&&(0,l.jsxs)(l.Fragment,{children:[` — `,n]})]})]})}function jt(e){let t=typeof e==`number`?e/1e6:Date.parse(String(e));return!Number.isFinite(t)||t<=0?String(e??``):new Date(t).toLocaleString()}function Mt({chunkId:e,chunkTitle:t,scope:n,browse:r,onClose:i}){let a=T(),[o,s]=(0,c.useState)([]),[u,d]=(0,c.useState)(!0),[f,p]=(0,c.useState)(null),[m,h]=(0,c.useState)(`none`),[g,_]=(0,c.useState)(null),[v,y]=(0,c.useState)(``),[b,x]=(0,c.useState)(!1),[S,C]=(0,c.useState)(null),[w,E]=(0,c.useState)(null),[D,O]=(0,c.useState)(``);(0,c.useEffect)(()=>{let t=!1;return d(!0),p(null),a.documentHistory(e,n,r).then(e=>{if(t)return;let n=e.revisions??[];if(s(n),n.length>=2){let e=n.map(e=>e.revision);C(Math.min(...e)),E(Math.max(...e))}}).catch(e=>!t&&p(e instanceof Error?e.message:String(e))).finally(()=>!t&&d(!1)),()=>{t=!0}},[a,e,n,r]);let k=async t=>{p(null),x(!0),_(t),h(`version`);try{let i=await a.documentGetVersion(e,t,n,r);y(i.body??``)}catch(e){p(e instanceof Error?e.message:String(e))}finally{x(!1)}},A=async()=>{if(S!==null&&w!==null){p(null),x(!0),h(`diff`);try{let t=await a.documentDiff(e,S,w,n,r);O(t.diff??``)}catch(e){p(e instanceof Error?e.message:String(e))}finally{x(!1)}}};return(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:i,children:(0,l.jsxs)(`div`,{className:`modal history-modal`,onClick:e=>e.stopPropagation(),children:[(0,l.jsxs)(`div`,{className:`modal-header sticky-top`,children:[(0,l.jsxs)(`h3`,{children:[`History`,t?` — ${t}`:``]}),(0,l.jsx)(`div`,{className:`modal-buttons modal-buttons-top`,children:(0,l.jsx)(`button`,{type:`button`,onClick:i,children:`close`})})]}),f&&(0,l.jsx)(`div`,{className:`modal-err`,children:f}),u?(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`loading…`}):o.length===0?(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`No history recorded for this chunk.`}):(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`div`,{className:`doc-hist-revs`,children:o.map(e=>(0,l.jsxs)(`button`,{type:`button`,className:`doc-hist-rev`+(m===`version`&&g===e.revision?` active`:``),onClick:()=>void k(e.revision),children:[(0,l.jsxs)(`span`,{className:`doc-hist-rev-num`,children:[`rev `,e.revision]}),(0,l.jsx)(`span`,{className:`doc-hist-rev-time`,children:jt(e.created_at)}),(0,l.jsx)(`span`,{className:`doc-hist-rev-actor`,children:e.actor||`unknown`})]},e.revision))}),o.length>=2&&(0,l.jsxs)(`div`,{className:`doc-hist-diffbar`,children:[(0,l.jsx)(`span`,{children:`Compare`}),(0,l.jsx)(`select`,{value:S??``,onChange:e=>C(e.target.value===``?null:Number(e.target.value)),"aria-label":`diff from revision`,children:o.map(e=>(0,l.jsxs)(`option`,{value:e.revision,children:[`rev `,e.revision]},e.revision))}),(0,l.jsx)(`span`,{"aria-hidden":!0,children:`→`}),(0,l.jsx)(`select`,{value:w??``,onChange:e=>E(e.target.value===``?null:Number(e.target.value)),"aria-label":`diff to revision`,children:o.map(e=>(0,l.jsxs)(`option`,{value:e.revision,children:[`rev `,e.revision]},e.revision))}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void A(),disabled:b||S===null||w===null,children:`diff`})]}),(0,l.jsx)(`div`,{className:`doc-hist-output`,children:b?(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`loading…`}):m===`diff`?(0,l.jsx)(Nt,{text:D}):m===`version`?(0,l.jsx)(`pre`,{className:`md-pre doc-hist-version`,children:(0,l.jsx)(`code`,{children:v||`(empty)`})}):(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`Select a revision to view it, or compare two.`})})]})]})})}function Nt({text:e}){if(!e.trim())return(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`(no differences)`});let t=e.split(`
|
|
3
|
+
`);return(0,l.jsx)(`pre`,{className:`md-pre doc-hist-diff`,children:t.map((e,t)=>{let n=e.startsWith(`+`)&&!e.startsWith(`+++`)?`diff-add`:e.startsWith(`-`)&&!e.startsWith(`---`)?`diff-del`:e.startsWith(`@@`)?`diff-hunk`:``;return(0,l.jsxs)(`span`,{className:n,children:[e,`
|
|
4
|
+
`]},t)})})}function Pt({source:e}){return(0,l.jsx)(`div`,{className:`md`,children:(0,l.jsx)(d.default,{remarkPlugins:[f.default],components:Ft,children:e})})}var Ft={a({href:e,children:t}){let n=e??``,r=/^https?:\/\//i.test(n);return(0,l.jsx)(`a`,{href:n,target:r?`_blank`:void 0,rel:r?`noopener noreferrer`:void 0,children:t})},pre({children:e}){return(0,l.jsx)(l.Fragment,{children:e})},code({className:e,children:t}){let n=/language-(\w+)/.exec(e??``)?.[1],r=String(t??``);return n===`mermaid`?(0,l.jsx)(Ke,{code:r.replace(/\n$/,``)}):n||r.includes(`
|
|
5
|
+
`)?(0,l.jsx)(`pre`,{className:`md-pre`,children:(0,l.jsx)(`code`,{className:e,children:t})}):(0,l.jsx)(`code`,{children:t})}};function It(e){let t=e.fields??{};return typeof t.alt==`string`&&t.alt.trim()!==``?t.alt:e.title||`image`}function Lt({documentId:e,scope:t,titleHint:n,browse:r,principal:i,renderAssistant:a,initialChunkId:o,onOpenDocument:s,onTitle:u,onChunkSelect:d,focusChunk:f}){let p=T(),m=(0,c.useMemo)(()=>r&&(r.scopeId||r.tenant)?{scopeId:r.scopeId,tenant:r.tenant}:void 0,[r?.scopeId,r?.tenant]),[h,_]=(0,c.useState)([]),[v,y]=(0,c.useState)(void 0),[b,x]=(0,c.useState)(null),[S,C]=(0,c.useState)(`chunks`),[w,E]=(0,c.useState)(null),[D,O]=(0,c.useState)(null),[k,A]=(0,c.useState)(null),j=(0,c.useRef)(null),[M,ee]=(0,c.useState)(null),[te,ne]=(0,c.useState)(null),[N,P]=(0,c.useState)(!1),[F,I]=(0,c.useState)(!1),[L,R]=(0,c.useState)(null),[re,ie]=(0,c.useState)(!1),[z,B]=(0,c.useState)(void 0),[ae,oe]=(0,c.useState)(null),[V,se]=(0,c.useState)(0),[ce,ue]=(0,c.useState)(!1),[U,pe]=(0,c.useState)(null),[ge,W]=(0,c.useState)(!1),[_e,ve]=(0,c.useState)([]),[G,K]=(0,c.useState)(!1),[ye,be]=(0,c.useState)(0),[xe,Se]=(0,c.useState)(0),[Ce,we]=(0,c.useState)(void 0),q=(0,c.useCallback)(()=>se(e=>e+1),[]),Te=(0,c.useRef)(o);Te.current=o;let J=(0,c.useRef)(u);J.current=u;let Ee=(0,c.useMemo)(()=>Re(h),[h]),Y=(0,c.useMemo)(()=>h.find(e=>!e.parent_id)?.title||n||`document`,[h,n]),X=(0,c.useMemo)(()=>!!b&&!b.parent_id,[b]),Z=(0,c.useMemo)(()=>he(h,b?.id),[h,b]),Q=(0,c.useMemo)(()=>U?.root_chunk_id??h.find(e=>!e.parent_id)?.id,[U,h]),Ae=(0,c.useRef)(d);Ae.current=d,(0,c.useEffect)(()=>{v&&Ae.current?.(v===Q?void 0:v)},[v,Q]);let je=(0,c.useMemo)(()=>[...new Set(h.map(e=>e.status).filter(e=>!!e))],[h]),Pe=!!U?.color_enabled,$=(0,c.useMemo)(()=>{let e=new Set;for(let t of _e)e.add(t.from_id),e.add(t.to_id);return e},[_e]),Ie=(0,c.useMemo)(()=>Pe?Fe(Ne(U?.type,U?.status,Me(U?.color_scheme))):void 0,[Pe,U?.type,U?.status,U?.color_scheme]);(0,c.useEffect)(()=>{let n=!1;return ie(!0),R(null),oe(null),p.documentQueryChunks(e,t,m).then(r=>{if(n)return;let i=r.chunks??[];_(i),B(`${t}:${e}`),y(e=>e??H(i,Te.current))}).catch(e=>{n||(_([]),oe(e instanceof Error?e.message:String(e)))}).finally(()=>!n&&ie(!1)),()=>{n=!0}},[p,e,t,V,m]);let ze=!re&&(ae!==null||z===`${t}:${e}`&&h.length===0),He=(0,c.useMemo)(()=>h.find(t=>!t.parent_id&&t.document_id===e)?.title,[h,e]);(0,c.useEffect)(()=>{He&&J.current?.(e,He)},[e,He]),(0,c.useEffect)(()=>{let n=!1;return p.documentGet(e,t,m).then(e=>!n&&pe(e)).catch(()=>!n&&pe(null)),()=>{n=!0}},[p,e,t,V,m]),(0,c.useEffect)(()=>{let n=!1;return me(p,e,t,m).then(e=>{n||(ve(e.edges),K(e.incomplete))}).catch(()=>{n||(ve([]),K(!1))}),()=>{n=!0}},[p,e,t,V,m,ye]),(0,c.useEffect)(()=>{if(!Q||!p.documentAcceptClassification){we(void 0);return}let n=!1;return p.documentGetChunk(Q,t,m).then(t=>!n&&we(t.document_id===e?t.fields:void 0)).catch(()=>!n&&we(void 0)),()=>{n=!0}},[p,e,Q,t,V,m]);let Ue=(0,c.useMemo)(()=>{if(p.documentAcceptStatement&&p.documentRejectStatement)return async(e,n,r)=>{n?await p.documentAcceptStatement(e,t,m,r):await p.documentRejectStatement(e,r??``,t,m),be(e=>e+1)}},[p,t,m]),We=(0,c.useMemo)(()=>De(p,t),[p,t]),Ge=(0,c.useMemo)(()=>Oe(_e,e),[_e,e]),Ye=(0,c.useCallback)(()=>be(e=>e+1),[]),Xe=(0,c.useMemo)(()=>ke(We,Ge,G,Ye),[We,Ge,G,Ye]),Ze=f?.seq,Qe=(0,c.useRef)(f);Qe.current=f;let $e=(0,c.useRef)(Q);$e.current=Q,(0,c.useEffect)(()=>{if(Ze===void 0)return;let e=Qe.current?.chunkId??$e.current;e&&y(e)},[Ze]),(0,c.useEffect)(()=>{y(void 0),x(null),E(null),C(`chunks`),pe(null),ve([]),K(!1),Se(0),we(void 0)},[e,t]),(0,c.useEffect)(()=>{if(!v){x(null);return}let e=!1;return p.documentGetChunk(v,t,m).then(t=>!e&&x(t)).catch(t=>!e&&R(t instanceof Error?t.message:String(t))),()=>{e=!0}},[p,v,t,V,m]),(0,c.useEffect)(()=>{let e=b?.type===`image`||!!b?.asset;if(!b||!e||!p.documentAssetObjectUrl){O(null),A(null);return}let n=!1,r=null;return A(null),p.documentAssetObjectUrl(b.id,t,m).then(e=>{n?URL.revokeObjectURL(e):(r=e,O(e))}).catch(e=>!n&&A(e instanceof Error?e.message:String(e))),()=>{n=!0,r&&URL.revokeObjectURL(r)}},[p,b,t,m]),(0,c.useEffect)(()=>{if(S!==`markdown`||!v){E(null);return}let e=Be(Ee,v);if(!e)return;let n=!1,r=[],i=(e,t)=>{r.push({node:e,depth:t}),e.children.forEach(e=>i(e,t+1))};return i(e,0),Promise.all(r.map(e=>p.documentGetChunk(e.node.row.id,t,m))).then(e=>{if(n)return;let t=r.map((t,n)=>{let r=Math.min(t.depth+1,6),i=e[n],a=`#`.repeat(r)+` `+t.node.row.title;if(i.type===`mermaid`&&i.body)return a+`
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
\`\`\`mermaid
|
|
8
|
+
`+i.body+"\n```\n";if(i.type===`image`||i.asset){let e=i.body?` — `+i.body:``;return a+`
|
|
9
9
|
|
|
10
|
-
`+
|
|
10
|
+
_🖼 image chunk_`+e+`
|
|
11
|
+
`}return a+(i.body?`
|
|
12
|
+
|
|
13
|
+
`+i.body:``)+`
|
|
11
14
|
`}).join(`
|
|
12
|
-
`);
|
|
13
|
-
A[Start] --> B[End]`},s,l);G(),y(j.id),x(j)}catch(j){f(j instanceof Error?j.message:String(j))}}},[o,t,s,l,m,z,G]),Qe=c.useCallback(async()=>{if(!o.documentCreateChunk)return;const d=m&&!Q?m:void 0,j=m||z;if(j){f(null);try{const N=await o.documentCreateChunk(t,d?"":j,{title:"Text",body:""},s,l,d);G(),y(N.id),x(N)}catch(N){f(N instanceof Error?N.message:String(N))}}},[o,t,s,l,m,Q,z,G]),Xe=c.useCallback(async()=>{if(!(!o.documentCreateChunk||!m)){f(null);try{const d=await o.documentCreateChunk(t,m,{title:"Text",body:""},s,l);G(),y(d.id),x(d)}catch(d){f(d instanceof Error?d.message:String(d))}}},[o,t,s,l,m,G]),je=c.useMemo(()=>{if(!h||Q)return{canUp:!1,canDown:!1};const d=h.parent_id??"",j=u.filter(C=>(C.parent_id??"")===d).sort((C,P)=>C.position-P.position||C.id.localeCompare(P.id)),N=j.findIndex(C=>C.id===h.id);return{canUp:N>0,canDown:N>=0&&N<j.length-1}},[u,h,Q]),ye=c.useCallback(async d=>{if(!(!o.documentReorderChunk||!m)){f(null);try{await o.documentReorderChunk(m,d,s,l),G()}catch(j){f(j instanceof Error?j.message:String(j))}}},[o,m,s,l,G]),Ye=c.useCallback(async()=>{if(!o.documentDeleteChunk||!h)return;f(null);const d=h.parent_id;try{await o.documentDeleteChunk(h.id,s,l),b(!1),y(d||z),G()}catch(j){f(j instanceof Error?j.message:String(j)),b(!1)}},[o,h,s,l,z,G]);return c.useEffect(()=>{if(!de)return;const d=j=>{if(E||S)return;const N=document.activeElement,C=N?.tagName;if(C==="INPUT"||C==="TEXTAREA"||N?.isContentEditable)return;const P=pt(j.clipboardData?.items);P&&(j.preventDefault(),ue(P))};return window.addEventListener("paste",d),()=>window.removeEventListener("paste",d)},[de,E,S,ue]),e.jsxs("div",{className:"doc-viewer",children:[e.jsx("input",{ref:F,type:"file",accept:"image/png,image/jpeg,image/gif,image/webp",style:{display:"none"},onChange:d=>{const j=d.target.files?.[0];d.target.value="",j&&ue(j)}}),e.jsxs("div",{className:"doc-toolbar",style:Ve,children:[e.jsx("strong",{className:"doc-title",title:te,children:te}),e.jsxs("div",{className:"doc-toolbar-actions",children:[de&&e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",onClick:()=>{Qe()},title:"Add a text chunk AFTER the selected chunk, at the same level",children:"+ text"}),e.jsx("button",{type:"button",onClick:()=>{Xe()},disabled:!m,title:m?"Add a chunk NESTED UNDER the selected chunk (in the ontology, a child is a subclass)":"Select a chunk first — this nests a new chunk under it",children:"+ child"}),e.jsx("button",{type:"button",onClick:()=>F.current?.click(),title:"Add an image chunk (upload — or paste a screenshot into the view)",children:"+ image"}),e.jsx("button",{type:"button",onClick:()=>{Ze()},title:"Add a Mermaid diagram chunk",children:"+ diagram"})]}),e.jsx("button",{type:"button",className:ne?"active":"",onClick:()=>I(!0),disabled:!z,title:"Colors — tint chunk tiles + the Path tree by type/status",children:"colors"}),e.jsx("button",{type:"button",onClick:()=>{ze()},title:"Download the whole document as Markdown (.md)",children:"↓ .md"}),e.jsx("button",{type:"button",onClick:()=>{Je()},title:"Download the whole document as a node/edge canvas graph (.canvas)",children:"↓ .canvas"}),i&&e.jsx("button",{type:"button",className:K?"active":"",onClick:()=>ee(d=>!d),title:"Document Assistant — instruct an agent to restructure/import/link chunks",children:"assistant"}),e.jsx("button",{type:"button",onClick:G,title:"Reload",children:"↻"})]})]}),g&&e.jsx("div",{className:"paths-err",children:g}),e.jsxs("div",{className:"doc-split",children:[e.jsx("div",{className:"doc-chunktree",children:k&&u.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(jt,{tree:le,selectedId:m,onSelect:qe,colorEnabled:ne,scheme:V?.color_scheme,linkedIds:We})}),e.jsx("div",{className:"doc-content",children:h?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-content-head",children:[e.jsx("h3",{children:h.title||"(untitled)"}),e.jsxs("div",{className:"doc-content-meta",children:[h.type&&e.jsx("span",{className:"chunk-badge",children:h.type}),h.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:h.status}),e.jsxs("span",{className:"chunk-rev",children:["rev ",h.revision]}),h.tags?.map(d=>e.jsx("span",{className:"chunk-tag",children:d},d))]}),e.jsxs("div",{className:"doc-content-controls",children:[e.jsxs("div",{className:"doc-mode-toggle",children:[e.jsx("button",{type:"button",className:v==="chunks"?"active":"",onClick:()=>R("chunks"),title:"This chunk's own content",children:"chunk"}),e.jsx("button",{type:"button",className:v==="markdown"?"active":"",onClick:()=>R("markdown"),title:Q?"Whole document as Markdown":"This chunk + its sub-chunks as Markdown",children:"markdown"})]}),e.jsx("button",{type:"button",onClick:()=>x(h),children:"edit"}),e.jsx("button",{type:"button",onClick:()=>W(!0),title:"View this chunk's edit history + diffs",children:"history"}),!Q&&e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",onClick:()=>{ye("up")},disabled:!je.canUp,title:"Move this chunk up within its level",children:"↑"}),e.jsx("button",{type:"button",onClick:()=>{ye("down")},disabled:!je.canDown,title:"Move this chunk down within its level",children:"↓"})]}),!Q&&e.jsx("button",{type:"button",className:"danger",onClick:()=>b(!0),title:"Delete this chunk and its sub-chunks",children:"delete"})]})]}),e.jsx("div",{className:"doc-content-body",children:h.type==="mermaid"?h.body?e.jsxs(e.Fragment,{children:[e.jsx(ie,{code:h.body}),v==="markdown"&&e.jsx("pre",{className:"md-pre doc-mermaid-source",children:e.jsx("code",{children:h.body})})]}):e.jsx("p",{className:"doc-empty-body",children:"(empty diagram)"}):h.type==="image"||h.asset?U?e.jsxs("p",{className:"doc-empty-body",children:["Image failed to load: ",U]}):T?e.jsxs("figure",{className:"doc-image",children:[e.jsx("img",{src:T,alt:Pt(h)}),h.body&&e.jsx("figcaption",{children:h.body})]}):e.jsx("p",{className:"doc-empty-body",children:"Loading image…"}):v==="markdown"?O!==null?e.jsx(Se,{source:O}):e.jsx("p",{className:"doc-empty-body",children:"Assembling Markdown…"}):h.body?e.jsx(Se,{source:h.body}):e.jsx("p",{className:"doc-empty-body",children:"(empty chunk)"})}),e.jsx(Et,{edges:B,documentId:t,selectedId:m,onSelectChunk:y}),e.jsx(_t,{documentId:t,selectedId:m,scope:s,browse:l,onSelectChunk:y})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a chunk on the left."})})})]}),K&&i&&e.jsx("div",{className:"doc-assistant",children:i({documentId:t,scope:s,subject:r?.subject,onChanged:G})}),E&&e.jsx(kt,{chunk:E,scope:s,browse:l,onClose:()=>x(null),onSaved:G}),_&&h&&e.jsx($t,{chunkId:h.id,chunkTitle:h.title,scope:s,browse:l,onClose:()=>W(!1)}),S&&z&&e.jsx(Nt,{documentId:t,rootChunkId:z,docType:V?.type,docStatus:V?.status,chunkStatuses:He,scope:s,browse:l,onClose:()=>I(!1),onSaved:G}),H&&h&&e.jsx("div",{className:"modal-overlay",onClick:()=>b(!1),children:e.jsxs("div",{className:"modal chunk-delete-confirm",onClick:d=>d.stopPropagation(),children:[e.jsx("h3",{children:"Delete chunk"}),e.jsxs("p",{className:"modal-context",children:["Delete “",h.title||"(untitled)","” and all of its sub-chunks? This cannot be undone."]}),e.jsxs("div",{className:"modal-buttons",children:[e.jsx("button",{type:"button",onClick:()=>b(!1),children:"cancel"}),e.jsx("button",{type:"button",className:"danger",onClick:()=>{Ye()},children:"delete"})]})]})})]})}function At(t){const s={name:"",fullPath:"",kind:"directory",explicit:!0,children:[]},n=new Map([["",s]]),a=i=>{const o=n.get(i);if(o)return o;const l=i.lastIndexOf("/"),u=l<=0?"":i.slice(0,l),p=i.slice(l+1),m=a(u),y={name:p,fullPath:i,kind:"directory",explicit:!1,children:[]};return m.children.push(y),n.set(i,y),y};for(const i of t){const o=a(i.full_path);o.kind=i.kind,o.explicit=!0,o.resourceRef=i.resource_ref}const r=i=>{i.sort((o,l)=>{const u=o.kind==="directory",p=l.kind==="directory";return u!==p?u?-1:1:o.name.localeCompare(l.name)}),i.forEach(o=>r(o.children))};return r(s.children),s.children}function xe(t){const s=t.lastIndexOf("/");return s<=0?"":t.slice(0,s)}function Ut(t){const s=[],n=a=>{if(a.kind==="document"){const r=a.resourceRef;r?.document_id&&s.push(r.document_id)}a.children.forEach(n)};return n(t),s}const Ot={directory:"📁",document:"📄",volume_mount:"💾",memory_entry:"🧠"};function Ft({tree:t,selectedPath:s,onSelect:n,summaries:a}){const[r,i]=c.useState(()=>new Map),o=c.useCallback(l=>{i(u=>{const p=new Map(u);return p.set(l,u.get(l)===!1),p})},[]);return c.useEffect(()=>{s&&i(l=>{let u=!1;const p=new Map(l);let m=xe(s);for(;m!=="";)p.get(m)===!1&&(p.set(m,!0),u=!0),m=xe(m);return u?p:l})},[s]),t.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"This tree is empty. Create a folder or document to begin."})}):e.jsx("ul",{className:"tree path-tree",children:t.map(l=>e.jsx(Be,{node:l,expanded:r,toggle:o,selectedPath:s,onSelect:n,summaries:a},l.fullPath))})}function It(t,s){if(t.kind!=="document"||!s)return;const n=t.resourceRef;return n?.document_id?s.get(n.document_id):void 0}function Be({node:t,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:i}){const o=t.children.length>0,l=s.get(t.fullPath)!==!1,u=a===t.fullPath,p=It(t,i),m=p&&p.color_enabled?oe(ge(p.type,p.status,ce(p.color_scheme))):void 0;return e.jsxs("li",{className:`node path-node kind-${t.kind} ${u?"selected":""}`,children:[e.jsxs("div",{className:"row path-row",style:m,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":l?"collapse":"expand",disabled:!o,onClick:y=>{y.stopPropagation(),o&&n(t.fullPath)},children:o?l?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"path-link",onClick:()=>r(t),title:t.fullPath,children:[e.jsx("span",{className:"path-kind-icon","aria-hidden":!0,children:Ot[t.kind]??"•"}),e.jsx("span",{className:"path-name",children:t.name}),p?.type&&e.jsx("span",{className:"chunk-badge",children:p.type}),p?.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:p.status}),!t.explicit&&e.jsx("span",{className:"path-implicit",title:"Implicit directory (no stored entry)",children:"implicit"})]})]}),o&&l&&e.jsx("ul",{className:"children path-children",children:t.children.map(y=>e.jsx(Be,{node:y,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:i},y.fullPath))})]})}const _e=/^[A-Za-z0-9._-]+$/;function Ee(t,s){return`${t}/${s}`}function Ge(t,s){for(const n of t){if(n.fullPath===s)return n;const a=Ge(n.children,s);if(a)return a}}function Bt(t){const{theme:s,defaultScope:n,path:a,onPathChange:r,browse:i,principal:o,renderAssistant:l,onError:u}=t,p=$e(t);return e.jsx(Le,{theme:s,dataLayer:p,children:e.jsx(Gt,{defaultScope:n,path:a,onPathChange:r,browse:i,principal:o,renderAssistant:l,onError:u})})}function Gt({defaultScope:t,path:s,onPathChange:n,browse:a,principal:r,renderAssistant:i,onError:o}){const l=X(),[u,p]=c.useState(t??"user"),[m,y]=c.useState([]),[h,L]=c.useState(void 0),[v,R]=c.useState(null),[O,A]=c.useState(!1),[T,M]=c.useState(null),U=s!==void 0?s:h,D=c.useRef(n);c.useEffect(()=>{D.current=n},[n]);const F=c.useRef(o);c.useEffect(()=>{F.current=o},[o]);const E=c.useCallback(S=>{D.current?.(S),L(S)},[]),x=c.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),_=c.useCallback(async()=>{A(!0),R(null);try{const S=await l.pathLs("/",u,!0,x);y(S.entries??[])}catch(S){R(S instanceof Error?S.message:String(S)),y([]),F.current?.(S)}finally{A(!1)}},[l,u,x]),W=c.useRef(!0);c.useEffect(()=>{W.current?W.current=!1:E(void 0),_()},[_,E]);const H=c.useMemo(()=>At(m),[m]),[b,g]=c.useState(()=>new Map);c.useEffect(()=>{if(u==="tenant"){g(new Map);return}const S=m.filter(B=>B.kind==="document").map(B=>B.resource_ref?.document_id).filter(B=>!!B);if(S.length===0){g(new Map);return}let I=!1;return l.documentsSummary({documentIds:S},u,x).then(B=>{if(I)return;const q=new Map;for(const G of B.documents??[])q.set(G.document_id,G);g(q)}).catch(()=>{I||g(new Map)}),()=>{I=!0}},[l,m,u,x]);const f=c.useMemo(()=>U?Ge(H,U):void 0,[H,U]),k=c.useMemo(()=>f?f.kind==="directory"?f.fullPath:xe(f.fullPath):"",[f]),w=k||"/",$=u!=="tenant",Z=c.useCallback(()=>{M({title:`New folder in ${w}`,fields:[{key:"name",label:"Folder name",placeholder:"e.g. launches"}],submitLabel:"Create",onSubmit:async S=>{const I=S.name.trim();if(!_e.test(I))throw new Error("name may contain only [A-Za-z0-9._-]");const B=Ee(k,I);await l.pathMkdir(B,u,x),await _(),E(B)}})},[l,k,w,u,x,_,E]),K=c.useCallback(()=>{M({title:`New document in ${w}`,fields:[{key:"name",label:"Path name",placeholder:"e.g. launch-plan"},{key:"title",label:"Title",placeholder:"e.g. Launch Plan"}],submitLabel:"Create",onSubmit:async S=>{const I=S.name.trim();if(!_e.test(I))throw new Error("name may contain only [A-Za-z0-9._-]");const B=S.title.trim()||I,q=Ee(k,I);await l.documentCreate(B,q,u,x),await _(),E(q)}})},[l,k,w,u,x,_,E]),ee=c.useCallback(()=>{f&&M({title:`Rename / move ${f.fullPath}`,fields:[{key:"to",label:"New path",value:f.fullPath}],submitLabel:"Move",onSubmit:async S=>{const I=S.to.trim();if(!I.startsWith("/"))throw new Error("path must start with /");await l.pathMv(f.fullPath,I,u,x),await _(),E(I)}})},[l,f,u,x,_,E]),V=c.useCallback(()=>{if(!f)return;if(f.kind==="document"){const q=f.resourceRef?.document_id;M({title:`Delete document ${f.fullPath}`,message:"This deletes the document, all its chunks, and its path entry. This cannot be undone.",fields:[],submitLabel:"Delete",danger:!0,onSubmit:async()=>{if(!q)throw new Error("this document dirent has no document_id");await l.documentDelete(q,u,x),await _(),E(void 0)}});return}const S=u==="tenant"?[]:Ut(f),I=f.children.length;M({title:`Delete branch ${f.fullPath}`,message:I===0?"This removes the (empty) folder.":"This removes the folder and everything under it"+(S.length>0?` — including ${S.length} document(s), whose chunks are deleted.`:"."),fields:[],submitLabel:"Delete",danger:!0,onSubmit:async()=>{for(const B of S)await l.documentDelete(B,u,x);await l.pathRm(f.fullPath,u,!0,x),await _(),E(void 0)}})},[l,f,u,x,_,E]),Y=f&&(f.kind==="directory"||f.kind==="document");return e.jsxs(e.Fragment,{children:[e.jsxs(ht,{className:"paths-view",defaultLeftWidth:420,minLeftWidth:280,minRightWidth:300,storageKey:"loomcycle.explorer.split.paths",children:[e.jsxs("div",{className:"left",children:[e.jsxs("div",{className:"paths-toolbar",children:[e.jsxs("label",{className:"paths-scope",children:[e.jsx("span",{children:"scope"}),e.jsxs("select",{value:u,onChange:S=>p(S.target.value),children:[e.jsx("option",{value:"user",children:"user"}),e.jsx("option",{value:"agent",children:"agent"}),e.jsx("option",{value:"tenant",children:"tenant"})]})]}),e.jsxs("div",{className:"paths-toolbar-actions",children:[e.jsx("button",{type:"button",onClick:Z,title:`New folder in ${w}`,children:"+ folder"}),e.jsx("button",{type:"button",onClick:K,disabled:!$,title:$?`New document in ${w}`:"Documents are agent/user scope only",children:"+ document"}),e.jsx("button",{type:"button",onClick:()=>{_()},title:"Reload the tree",children:"↻"})]})]}),e.jsxs("p",{className:"paths-context",children:["new items → ",e.jsx("code",{children:w}),x?.scopeId&&e.jsxs(e.Fragment,{children:[" · ","subject ",e.jsx("code",{children:x.scopeId})]}),x?.tenant&&e.jsxs(e.Fragment,{children:[" · ","tenant ",e.jsx("code",{children:x.tenant})]})]}),v&&e.jsx("div",{className:"paths-err",children:v}),O&&m.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(Ft,{tree:H,selectedPath:U,onSelect:S=>E(S.fullPath),summaries:b})]}),e.jsx("div",{className:"right",children:f?f.kind==="document"?e.jsxs("div",{className:"paths-doc",children:[e.jsxs("div",{className:"paths-doc-head",children:[e.jsx("code",{className:"paths-doc-path",children:f.fullPath}),e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:ee,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:V,children:"delete"})]})]}),e.jsx(Ie,{documentId:f.resourceRef?.document_id??"",scope:u,titleHint:f.name,browse:x,principal:r,renderAssistant:i})]}):e.jsxs("div",{className:"paths-detail",children:[e.jsxs("h2",{children:[e.jsx("span",{className:"paths-detail-kind",children:f.kind}),e.jsx("code",{children:f.fullPath})]}),e.jsxs("dl",{className:"paths-detail-meta",children:[e.jsx("dt",{children:"scope"}),e.jsx("dd",{children:u}),e.jsx("dt",{children:"stored"}),e.jsx("dd",{children:f.explicit?"yes":"implicit (no entry)"})]}),Y?e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:ee,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:V,children:"delete"})]}):e.jsxs("p",{className:"paths-readonly",children:["Read-only — ",f.kind," entries are managed by their own tool."]})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a path on the left, or create a folder / document."})})})]}),T&&e.jsx(Ht,{state:T,onClose:()=>M(null)})]})}function Ht({state:t,onClose:s}){const[n,a]=c.useState(()=>Object.fromEntries(t.fields.map(p=>[p.key,p.value??""]))),[r,i]=c.useState(!1),[o,l]=c.useState(null),u=async p=>{p.preventDefault(),i(!0),l(null);try{await t.onSubmit(n),s()}catch(m){l(m instanceof Error?m.message:String(m)),i(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:s,children:e.jsxs("form",{className:"modal",onClick:p=>p.stopPropagation(),onSubmit:u,children:[e.jsxs("div",{className:"modal-header sticky-top",children:[e.jsx("h3",{children:t.title}),e.jsxs("div",{className:"modal-buttons modal-buttons-top",children:[e.jsx("button",{type:"button",onClick:s,disabled:r,children:"cancel"}),e.jsx("button",{type:"submit",className:t.danger?"danger":"primary",disabled:r,children:r?"working…":t.submitLabel})]})]}),t.message&&e.jsx("p",{className:"modal-context",children:t.message}),t.fields.map((p,m)=>e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:p.label}),e.jsx("input",{className:"path-modal-input",value:n[p.key],placeholder:p.placeholder,autoFocus:m===0,onChange:y=>a(h=>({...h,[p.key]:y.target.value}))})]},p.key)),o&&e.jsx("div",{className:"modal-err",children:o})]})})}function Wt(t){const{documentId:s,scope:n,titleHint:a,browse:r,theme:i,principal:o,renderAssistant:l}=t,u=$e(t);return e.jsx(Le,{theme:i,dataLayer:u,children:e.jsx(Ie,{documentId:s,scope:n,titleHint:a,browse:r,principal:o,renderAssistant:l})})}exports.DEFAULT_SCHEME=re;exports.DocumentViewer=Wt;exports.PathExplorer=Bt;exports.assetFetchFromConnection=Re;exports.chunkColor=Ae;exports.createLoomcycleClient=De;exports.dataLayerFromClient=pe;exports.docColor=ge;exports.effectiveScheme=ce;exports.hexToTint=Ue;exports.tintStyle=oe;
|
|
14
|
-
//# sourceMappingURL=index.cjs.map
|
|
15
|
+
`);E(t)}).catch(e=>!n&&R(e instanceof Error?e.message:String(e))),()=>{n=!0}},[p,S,v,Ee,t,V,m]);let et=(0,c.useCallback)(e=>y(e.row.id),[]),tt=(0,c.useCallback)((t,n,r)=>s?.({documentId:t,chunkId:n,title:r},{documentId:e,chunkId:v,title:Y}),[s,e,v,Y]),nt=(0,c.useCallback)(async()=>{try{let n=await p.documentExportMd(e,t,!0,m),r=new Blob([n.markdown],{type:`text/markdown`}),i=URL.createObjectURL(r),a=document.createElement(`a`);a.href=i,a.download=(n.title||`document`).replace(/[^\w.-]+/g,`_`)+`.md`,a.click(),URL.revokeObjectURL(i)}catch(e){R(e instanceof Error?e.message:String(e))}},[p,e,t,m]),rt=(0,c.useCallback)(async()=>{try{let n=await p.documentExportCanvas(e,t,m),r=new Blob([JSON.stringify(n.canvas,null,2)],{type:`application/json`}),i=URL.createObjectURL(r),a=document.createElement(`a`);a.href=i,a.download=(Y||`document`).replace(/[^\w.-]+/g,`_`)+`.canvas`,a.click(),URL.revokeObjectURL(i)}catch(e){R(e instanceof Error?e.message:String(e))}},[p,e,t,m,Y]),it=!!p.documentCreateChunk&&!!Q,at=(0,c.useCallback)(async n=>{if(!p.documentCreateChunk||!p.documentSetAsset)return;let r=v||Q;if(r){R(null);try{let{mediaType:i,data:a}=await de(n),o=await p.documentCreateChunk(e,r,{title:n.name||`image`,type:`image`},t,m);await p.documentSetAsset(o.id,i,a,n.name,t,m),q(),y(o.id)}catch(e){R(e instanceof Error?e.message:String(e))}}},[p,e,t,m,v,Q,q]),ot=(0,c.useCallback)(async()=>{if(!p.documentCreateChunk)return;let n=v||Q;if(n){R(null);try{let r=await p.documentCreateChunk(e,n,{title:`Diagram`,type:`mermaid`,body:`graph TD
|
|
16
|
+
A[Start] --> B[End]`},t,m);q(),y(r.id),ee(r)}catch(e){R(e instanceof Error?e.message:String(e))}}},[p,e,t,m,v,Q,q]),st=(0,c.useCallback)(async()=>{if(!p.documentCreateChunk)return;let n=v&&!X?v:void 0,r=v||Q;if(r){R(null);try{let i=await p.documentCreateChunk(e,n?``:r,{title:`Text`,body:``},t,m,n);q(),y(i.id),ee(i)}catch(e){R(e instanceof Error?e.message:String(e))}}},[p,e,t,m,v,X,Q,q]),lt=(0,c.useCallback)(async()=>{if(p.documentCreateChunk&&v){R(null);try{let n=await p.documentCreateChunk(e,v,{title:`Text`,body:``},t,m);q(),y(n.id),ee(n)}catch(e){R(e instanceof Error?e.message:String(e))}}},[p,e,t,m,v,q]),ut=(0,c.useMemo)(()=>{if(!b||X)return{canUp:!1,canDown:!1};let e=b.parent_id??``,t=h.filter(t=>(t.parent_id??``)===e).sort((e,t)=>e.position-t.position||e.id.localeCompare(t.id)),n=t.findIndex(e=>e.id===b.id);return{canUp:n>0,canDown:n>=0&&n<t.length-1}},[h,b,X]),ft=(0,c.useCallback)(async e=>{if(p.documentReorderChunk&&v){R(null);try{await p.documentReorderChunk(v,e,t,m),q()}catch(e){R(e instanceof Error?e.message:String(e))}}},[p,v,t,m,q]),pt=(0,c.useCallback)(async()=>{if(!p.documentDeleteChunk||!b)return;R(null);let e=b.parent_id;try{await p.documentDeleteChunk(b.id,t,m),I(!1),y(e||Q),q()}catch(e){R(e instanceof Error?e.message:String(e)),I(!1)}},[p,b,t,m,Q,q]);return(0,c.useEffect)(()=>{if(!it)return;let e=e=>{if(M||ge)return;let t=document.activeElement,n=t?.tagName;if(n===`INPUT`||n===`TEXTAREA`||t?.isContentEditable)return;let r=fe(e.clipboardData?.items);r&&(e.preventDefault(),at(r))};return window.addEventListener(`paste`,e),()=>window.removeEventListener(`paste`,e)},[it,M,ge,at]),(0,l.jsxs)(`div`,{className:`doc-viewer`,children:[(0,l.jsx)(`input`,{ref:j,type:`file`,accept:`image/png,image/jpeg,image/gif,image/webp`,style:{display:`none`},onChange:e=>{let t=e.target.files?.[0];e.target.value=``,t&&at(t)}}),(0,l.jsxs)(`div`,{className:`doc-toolbar`,style:Ie,children:[(0,l.jsx)(`strong`,{className:`doc-title`,title:Y,children:Y}),(0,l.jsx)(Le,{value:e,label:`doc`}),(0,l.jsxs)(`div`,{className:`doc-toolbar-actions`,children:[it&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`button`,{type:`button`,onClick:()=>void st(),title:`Add a text chunk AFTER the selected chunk, at the same level`,children:`+ text`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void lt(),disabled:!v,title:v?`Add a chunk NESTED UNDER the selected chunk (in the ontology, a child is a subclass)`:`Select a chunk first — this nests a new chunk under it`,children:`+ child`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>j.current?.click(),title:`Add an image chunk (upload — or paste a screenshot into the view)`,children:`+ image`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void ot(),title:`Add a Mermaid diagram chunk`,children:`+ diagram`})]}),(0,l.jsx)(`button`,{type:`button`,className:Pe?`active`:``,onClick:()=>W(!0),disabled:!Q,title:`Colors — tint chunk tiles + the Path tree by type/status`,children:`colors`}),p.documentClassify&&(0,l.jsx)(`button`,{type:`button`,className:xe>0?`active`:``,onClick:()=>Se(e=>e>0?0:1),title:`Classify — what this document is, by the confirmed ontology (a report first; nothing is written until Apply)`,children:`classify`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void nt(),title:`Download the whole document as Markdown (.md)`,children:`↓ .md`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void rt(),title:`Download the whole document as a node/edge canvas graph (.canvas)`,children:`↓ .canvas`}),a&&(0,l.jsx)(`button`,{type:`button`,className:ce?`active`:``,onClick:()=>ue(e=>!e),title:`Document Assistant — instruct an agent to restructure/import/link chunks`,children:`assistant`}),(0,l.jsx)(`button`,{type:`button`,onClick:q,title:`Reload`,children:`↻`})]})]}),L&&(0,l.jsx)(`div`,{className:`paths-err`,children:L}),!ze&&(0,l.jsx)(ct,{documentId:e,scope:t,browse:m,rootFields:Ce,requestKey:xe,onClose:()=>Se(0),onChanged:q},`${t}:${e}`),ze?(0,l.jsx)(At,{documentId:e,scope:t,detail:ae??void 0}):(0,l.jsxs)(le,{className:`doc-split`,defaultLeftWidth:320,minLeftWidth:160,minRightWidth:320,storageKey:`loomcycle.explorer.split.doc`,children:[(0,l.jsx)(`div`,{className:`doc-chunktree`,children:re&&h.length===0?(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`Loading…`})}):(0,l.jsx)(Ve,{tree:Ee,selectedId:v,onSelect:et,colorEnabled:Pe,scheme:U?.color_scheme,linkedIds:$})}),(0,l.jsx)(`div`,{className:`doc-content`,children:b?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(`div`,{className:`doc-content-head`,children:[Z.length>0&&(0,l.jsxs)(`nav`,{className:`chunk-ancestry`,"aria-label":`Chunks above this one`,children:[(0,l.jsx)(`button`,{type:`button`,className:`chunk-ancestry-up`,onClick:()=>y(Z[Z.length-1].id),title:`Up to ${Z[Z.length-1].title||`(untitled)`}`,children:`↰ Up`}),(0,l.jsxs)(`ol`,{className:`doc-trail-list`,children:[Z.map(e=>(0,l.jsx)(`li`,{className:`doc-trail-item`,children:(0,l.jsx)(`button`,{type:`button`,className:`doc-trail-crumb`,onClick:()=>y(e.id),title:e.title||`(untitled)`,children:e.title||`(untitled)`})},e.id)),(0,l.jsx)(`li`,{className:`doc-trail-item`,children:(0,l.jsx)(`span`,{className:`doc-trail-current`,"aria-current":`location`,title:b.title||`(untitled)`,children:b.title||`(untitled)`})})]})]}),(0,l.jsx)(`h3`,{children:b.title||`(untitled)`}),(0,l.jsxs)(`div`,{className:`doc-content-meta`,children:[b.type&&(0,l.jsx)(`span`,{className:`chunk-badge`,children:b.type}),b.status&&(0,l.jsx)(`span`,{className:`chunk-badge chunk-status`,children:b.status}),(0,l.jsxs)(`span`,{className:`chunk-rev`,children:[`rev `,b.revision]}),b.tags?.map(e=>(0,l.jsx)(`span`,{className:`chunk-tag`,children:e},e)),(0,l.jsx)(Le,{value:b.id,label:`chunk`})]}),(0,l.jsxs)(`div`,{className:`doc-content-controls`,children:[(0,l.jsxs)(`div`,{className:`doc-mode-toggle`,children:[(0,l.jsx)(`button`,{type:`button`,className:S===`chunks`?`active`:``,onClick:()=>C(`chunks`),title:`This chunk's own content`,children:`chunk`}),(0,l.jsx)(`button`,{type:`button`,className:S===`markdown`?`active`:``,onClick:()=>C(`markdown`),title:X?`Whole document as Markdown`:`This chunk + its sub-chunks as Markdown`,children:`markdown`})]}),(0,l.jsx)(`button`,{type:`button`,className:`chunk-edit`,onClick:()=>ee(b),children:`edit`}),p.documentEntityTypes&&g(b)&&(0,l.jsx)(`button`,{type:`button`,onClick:()=>ne(b),title:`Change this subject's type to another type in force`,children:`change type`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>P(!0),title:`View this chunk's edit history + diffs`,children:`history`}),!X&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`button`,{type:`button`,onClick:()=>void ft(`up`),disabled:!ut.canUp,title:`Move this chunk up within its level`,children:`↑`}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void ft(`down`),disabled:!ut.canDown,title:`Move this chunk down within its level`,children:`↓`})]}),!X&&(0,l.jsx)(`button`,{type:`button`,className:`danger`,onClick:()=>I(!0),title:`Delete this chunk and its sub-chunks`,children:`delete`})]})]}),(0,l.jsx)(`div`,{className:`doc-content-body`,children:b.type===`mermaid`?b.body?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(Ke,{code:b.body}),S===`markdown`&&(0,l.jsx)(`pre`,{className:`md-pre doc-mermaid-source`,children:(0,l.jsx)(`code`,{children:b.body})})]}):(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`(empty diagram)`}):b.type===`image`||b.asset?k?(0,l.jsxs)(`p`,{className:`doc-empty-body`,children:[`Image failed to load: `,k]}):D?(0,l.jsxs)(`figure`,{className:`doc-image`,children:[(0,l.jsx)(`img`,{src:D,alt:It(b)}),b.body&&(0,l.jsx)(`figcaption`,{children:b.body})]}):(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`Loading image…`}):S===`markdown`?w===null?(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`Assembling Markdown…`}):(0,l.jsx)(Pt,{source:w}):b.body?(0,l.jsx)(Pt,{source:b.body}):(0,l.jsx)(`p`,{className:`doc-empty-body`,children:`(empty chunk)`})}),(0,l.jsx)(wt,{edges:_e,incomplete:G,documentId:e,selectedId:v,onSelectChunk:y,onOpenDocument:s?tt:void 0,onRuleStatement:Ue}),(0,l.jsx)(St,{documentId:e,selectedId:v,scope:t,browse:m,onSelectChunk:y,onOpenDocument:s?tt:void 0,refreshKey:ye,review:Xe})]}):(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`Select a chunk on the left.`})})})]}),ce&&a&&(0,l.jsx)(`div`,{className:`doc-assistant`,children:a({documentId:e,scope:t,subject:i?.subject,onChanged:q})}),M&&(0,l.jsx)(qe,{chunk:M,scope:t,browse:m,onClose:()=>ee(null),onSaved:q}),te&&(0,l.jsx)(Je,{chunk:te,scope:t,browse:m,onClose:()=>ne(null),onSaved:q}),N&&b&&(0,l.jsx)(Mt,{chunkId:b.id,chunkTitle:b.title,scope:t,browse:m,onClose:()=>P(!1)}),ge&&Q&&(0,l.jsx)(dt,{documentId:e,rootChunkId:Q,docType:U?.type,docStatus:U?.status,chunkStatuses:je,scope:t,browse:m,onClose:()=>W(!1),onSaved:q}),F&&b&&(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:()=>I(!1),children:(0,l.jsxs)(`div`,{className:`modal chunk-delete-confirm`,onClick:e=>e.stopPropagation(),children:[(0,l.jsx)(`h3`,{children:`Delete chunk`}),(0,l.jsxs)(`p`,{className:`modal-context`,children:[`Delete “`,b.title||`(untitled)`,`” and all of its sub-chunks? This cannot be undone.`]}),(0,l.jsxs)(`div`,{className:`modal-buttons`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:()=>I(!1),children:`cancel`}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,onClick:()=>void pt(),children:`delete`})]})]})})]})}function Rt(e,t=``){let n=e.get(t);if(!n)return[];let r=n.entries.map(n=>{let r=n.kind===`directory`&&n.full_path.startsWith(`${t}/`);return{name:n.name,fullPath:n.full_path,kind:n.kind,resourceRef:n.resource_ref,listing:r?e.get(n.full_path):void 0,children:r?Rt(e,n.full_path):[]}});return r.sort((e,t)=>{let n=e.kind===`directory`;return n===(t.kind===`directory`)?e.name.localeCompare(t.name):n?-1:1}),r}var zt={directory:`📁`,document:`📄`,volume_mount:`💾`,memory_entry:`🧠`};function Bt(e){let{tree:t,root:n}=e;return!n||!n.loaded&&n.loading?(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`Loading…`})}):n.loaded&&t.length===0&&!n.nextCursor?(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`This tree is empty. Create a folder or document to begin.`})}):(0,l.jsxs)(`ul`,{className:`tree path-tree`,children:[t.map(t=>(0,l.jsx)(Ut,{node:t,...e},t.fullPath)),(0,l.jsx)(Vt,{dir:``,listing:n,...e})]})}function Vt({dir:e,listing:t,onLoadMore:n,onRetry:r}){let i=[];return!t||t.loading&&t.entries.length===0?i.push((0,l.jsx)(`li`,{className:`node path-status`,children:(0,l.jsx)(`span`,{className:`path-implicit`,children:`loading…`})},`status`)):t.error?i.push((0,l.jsxs)(`li`,{className:`node path-status`,children:[e!==``&&(0,l.jsxs)(`span`,{className:`path-err`,children:[`failed to load: `,t.error]}),` `,(0,l.jsx)(`button`,{type:`button`,className:`path-more`,onClick:()=>r(e),children:`retry`})]},`status`)):t.loaded&&t.entries.length===0&&e!==``&&i.push((0,l.jsx)(`li`,{className:`node path-status`,children:(0,l.jsx)(`span`,{className:`path-implicit`,children:`empty`})},`status`)),t?.nextCursor&&!t.error&&i.push((0,l.jsx)(`li`,{className:`node path-status`,children:(0,l.jsx)(`button`,{type:`button`,className:`path-more`,disabled:t.loading,onClick:()=>n(e),children:t.loading?`loading…`:`Load more (${t.entries.length} shown)`})},`more`)),(0,l.jsx)(l.Fragment,{children:i})}function Ht(e,t){if(e.kind!==`document`||!t)return;let n=e.resourceRef;return n?.document_id?t.get(n.document_id):void 0}function Ut(e){let{node:t,expanded:n,onToggle:r,selectedPath:i,onSelect:a,summaries:o}=e,s=t.kind===`directory`,c=s&&n.has(t.fullPath),u=i===t.fullPath,d=Ht(t,o),f=d&&d.color_enabled?Fe(Ne(d.type,d.status,Me(d.color_scheme))):void 0;return(0,l.jsxs)(`li`,{className:`node path-node kind-${t.kind} ${u?`selected`:``}`,children:[(0,l.jsxs)(`div`,{className:`row path-row`,style:f,children:[(0,l.jsx)(`button`,{type:`button`,className:`tree-caret`,"aria-label":c?`collapse`:`expand`,"aria-expanded":s?c:void 0,disabled:!s,onClick:e=>{e.stopPropagation(),s&&r(t.fullPath)},children:s?c?`▼`:`▶`:`·`}),(0,l.jsxs)(`button`,{type:`button`,className:`path-link`,onClick:()=>a(t),title:t.fullPath,children:[(0,l.jsx)(`span`,{className:`path-kind-icon`,"aria-hidden":!0,children:zt[t.kind]??`•`}),(0,l.jsx)(`span`,{className:`path-name`,children:t.name}),d?.type&&(0,l.jsx)(`span`,{className:`chunk-badge`,children:d.type}),d?.status&&(0,l.jsx)(`span`,{className:`chunk-badge chunk-status`,children:d.status})]})]}),c&&(0,l.jsxs)(`ul`,{className:`children path-children`,children:[t.children.map(t=>(0,l.jsx)(Ut,{...e,node:t},t.fullPath)),(0,l.jsx)(Vt,{...e,dir:t.fullPath,listing:t.listing})]})]})}var Wt=/^[A-Za-z0-9._-]+$/;function Gt(e,t){return`${e}/${t}`}function Kt(e,t){for(let n of e){if(n.fullPath===t)return n;let e=Kt(n.children,t);if(e)return e}}function qt(e){let{theme:t,defaultScope:n,path:r,onPathChange:i,browse:a,principal:o,renderAssistant:s,renderNavigator:c,onError:u}=e,d=I(e);return(0,l.jsx)(L,{theme:t,dataLayer:d,children:(0,l.jsx)(Jt,{defaultScope:n,path:r,onPathChange:i,browse:a,principal:o,renderAssistant:s,renderNavigator:c,onError:u})})}function Jt({defaultScope:e,path:t,onPathChange:n,browse:r,principal:i,renderAssistant:a,renderNavigator:o,onError:s}){let u=T(),[d,f]=(0,c.useState)(e??`user`),[p,g]=(0,c.useState)(void 0),[_,v]=(0,c.useState)(null),[y,b]=(0,c.useState)(null),[x,S]=(0,c.useState)(()=>new Set),C=t===void 0?p:t,w=(0,c.useRef)(n);(0,c.useEffect)(()=>{w.current=n},[n]);let E=(0,c.useRef)(s);(0,c.useEffect)(()=>{E.current=s},[s]);let[D,A]=(0,c.useState)([]),j=(0,c.useRef)(0),ne=(0,c.useRef)(void 0),N=(0,c.useCallback)(e=>{ne.current=e,w.current?.(e),g(e)},[]),P=(0,c.useCallback)(e=>{j.current++,A(e=>e.length>0?[]:e),N(e)},[N]);(0,c.useEffect)(()=>{t!==void 0&&t!==ne.current&&(j.current++,A(e=>e.length>0?[]:e))},[t]);let F=(0,c.useMemo)(()=>r&&(r.scopeId||r.tenant)?{scopeId:r.scopeId,tenant:r.tenant}:void 0,[r?.scopeId,r?.tenant]),I=(0,c.useMemo)(()=>new M(u,d,F),[u,d,F]),L=(0,c.useSyncExternalStore)(I.subscribe,I.getSnapshot),z=(0,c.useCallback)(e=>{E.current?.(e)},[]),[ae,V]=(0,c.useState)(()=>o?m(globalThis.localStorage):`tree`),H=!!o&&ae===`stars`,ce=(0,c.useCallback)(e=>{V(e),h(globalThis.localStorage,e),e===`tree`&&U(!1)},[]),[ue,U]=(0,c.useState)(!1),[de,fe]=(0,c.useState)(0),[pe,me]=(0,c.useState)(),[he,ge]=(0,c.useState)(),W=(0,c.useCallback)((e,t)=>(fe(e=>e+1),I.invalidate(e,t)),[I]),[_e,ve]=(0,c.useState)(()=>new Map),G=(0,c.useRef)(new Set),K=(0,c.useRef)(0),ye=(0,c.useCallback)(()=>{K.current++,G.current=new Set,ve(new Map)},[]),be=(0,c.useRef)(!0);(0,c.useEffect)(()=>{be.current?be.current=!1:(P(void 0),v(null),S(new Set),ye()),I.loadDir(``).catch(z)},[I,P,ye,z]),(0,c.useEffect)(()=>{for(let e of x)L.has(e)||I.loadDir(e).catch(z)},[I,x,L,z]);let xe=(0,c.useRef)(null);(0,c.useEffect)(()=>{let e=xe.current!==null&&xe.current!==I;if(xe.current=I,!C||e)return;let t=k(C).filter(e=>e!==``);S(e=>{if(t.every(t=>e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.add(e)),n}),I.reveal(C).catch(z)},[I,C,z]),(0,c.useEffect)(()=>{if(d===`tenant`)return;let e=[];for(let t of L.values())for(let n of te(t.entries))G.current.has(n)||(G.current.add(n),e.push(n));let t=K.current;for(let n=0;n<e.length;n+=500)u.documentsSummary({documentIds:e.slice(n,n+500)},d,F).then(e=>{K.current===t&&ve(t=>{let n=new Map(t);for(let t of e.documents??[])n.set(t.document_id,t);return n})}).catch(()=>{})},[u,L,d,F]);let Se=(0,c.useMemo)(()=>Rt(L),[L]),Ce=(0,c.useCallback)(async()=>{v(null),ye(),fe(e=>e+1),await I.refresh(x).catch(z),C&&await I.reveal(C).catch(z)},[I,x,C,ye,z]),we=(0,c.useCallback)(e=>{S(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[]),q=(0,c.useCallback)(e=>I.loadMore(e).catch(z),[I,z]),Te=(0,c.useCallback)(e=>{(I.getSnapshot().get(e)?.loaded?I.loadMore(e):I.loadDir(e)).catch(z)},[I,z]),J=(0,c.useMemo)(()=>C?Kt(Se,C):void 0,[Se,C]),Ee=L.get(``)?.error,Y=J?.kind===`directory`?J.fullPath:void 0,[X,De]=(0,c.useState)(null);(0,c.useEffect)(()=>{if(!Y||!u.pathIsStored)return;let e=!1;return u.pathIsStored(Y,d,F).then(t=>{e||De({path:Y,explicit:t})},()=>{}),()=>{e=!0}},[u,Y,d,F]);let Oe=J?J.kind===`directory`?X?.path===J.fullPath?X.explicit?`yes`:`implicit (no entry)`:void 0:`yes`:void 0,ke=(0,c.useMemo)(()=>J?J.kind===`directory`?J.fullPath:O(J.fullPath):``,[J]),Z=ke||`/`,Q=d!==`tenant`,Ae=(0,c.useCallback)(()=>{b({title:`New folder in ${Z}`,fields:[{key:`name`,label:`Folder name`,placeholder:`e.g. launches`}],submitLabel:`Create`,onSubmit:async e=>{let t=e.name.trim();if(!Wt.test(t))throw Error(`name may contain only [A-Za-z0-9._-]`);let n=Gt(ke,t);await u.pathMkdir(n,d,F),await W(n).catch(z),P(n)}})},[u,I,ke,Z,d,F,z,P]),je=(0,c.useCallback)(()=>{b({title:`New document in ${Z}`,fields:[{key:`name`,label:`Path name`,placeholder:`e.g. launch-plan`},{key:`title`,label:`Title`,placeholder:`e.g. Launch Plan`}],submitLabel:`Create`,onSubmit:async e=>{let t=e.name.trim();if(!Wt.test(t))throw Error(`name may contain only [A-Za-z0-9._-]`);let n=e.title.trim()||t,r=Gt(ke,t);await u.documentCreate(n,r,d,F),await W(r).catch(z),P(r)}})},[u,I,ke,Z,d,F,z,P]),Me=(0,c.useCallback)(()=>{J&&b({title:`Rename / move ${J.fullPath}`,fields:[{key:`to`,label:`New path`,value:J.fullPath}],submitLabel:`Move`,onSubmit:async e=>{let t=e.to.trim();if(!t.startsWith(`/`))throw Error(`path must start with /`);await u.pathMv(J.fullPath,t,d,F),await Promise.all([W(J.fullPath,{dropSubtree:!0}),W(t)]).catch(z),P(t)}})},[u,I,J,d,F,z,P]),Ne=(0,c.useCallback)(()=>{if(!J)return;if(J.kind===`document`){let e=J.resourceRef?.document_id;b({title:`Delete document ${J.fullPath}`,message:`This deletes the document, all its chunks, and its path entry. This cannot be undone.`,fields:[],submitLabel:`Delete`,danger:!0,onSubmit:async()=>{if(!e)throw Error(`this document dirent has no document_id`);await u.documentDelete(e,d,F),await W(J.fullPath).catch(z),P(void 0)}});return}let e=J;v(null),(async()=>{let t;try{t=await ee(u,e.fullPath,d,F)}catch(t){v(`could not list ${e.fullPath} for delete: ${t instanceof Error?t.message:String(t)}`);return}let n=d===`tenant`?[]:te(t);b({title:`Delete branch ${e.fullPath}`,message:t.length===0?`This removes the (empty) folder.`:`This removes the folder and everything under it`+(n.length>0?` — including ${n.length} document(s), whose chunks are deleted.`:`.`),fields:[],submitLabel:`Delete`,danger:!0,onSubmit:async()=>{for(let e of n)await u.documentDelete(e,d,F);await u.pathRm(e.fullPath,d,!0,F),await W(e.fullPath,{dropSubtree:!0}).catch(z),P(void 0)}})})()},[u,I,J,d,F,z,P]),Pe=J&&(J.kind===`directory`||J.kind===`document`),$=(0,c.useMemo)(()=>{if(D.length>0)return D[D.length-1];if(J?.kind===`document`)return{documentId:J.resourceRef?.document_id??``,path:J.fullPath,title:J.name}},[D,J]),Fe=(0,c.useCallback)((e,t)=>{let n=++j.current,r=oe(I.getSnapshot(),e.documentId);A(R(D,{...t,path:$?.path},{...e,path:r})),N(r),!r&&u.documentPath&&u.documentPath(e.documentId,d,F).then(t=>{t&&j.current===n&&(A(n=>ie(n,e.documentId,{path:t})),N(t))},()=>{})},[u,I,D,$?.path,d,F,N]),Ie=(0,c.useCallback)(e=>{let t=re(D,e);t!==D&&(j.current++,A(t),N(t[t.length-1].path))},[D,N]),Le=(0,c.useCallback)((e,t)=>A(n=>ie(n,e,{title:t})),[]),Re=(0,c.useCallback)(e=>{ge(t=>({documentId:e.documentId,chunkId:e.chunkId,seq:(t?.seq??0)+1})),$?.documentId!==e.documentId&&(e.path?P(e.path):(j.current++,A([{documentId:e.documentId,chunkId:e.chunkId}]),N(void 0)))},[$?.documentId,P,N]),ze=(0,c.useMemo)(()=>({scope:d,browse:F,selection:{documentId:$?.documentId,chunkId:pe&&pe.documentId===$?.documentId?pe.chunkId:void 0,path:$?.path},onSelect:Re,reloadToken:de,maximized:ue,onToggleMaximize:()=>U(e=>!e),subject:i?.subject}),[d,F,$?.documentId,$?.path,pe,Re,de,ue,i?.subject]),Be=he&&$&&he.documentId===$.documentId?he:void 0,Ve=!!$?.path&&J?.kind===`document`&&J.fullPath===$.path;return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(le,{className:H?`paths-view paths-view--stars`:`paths-view`,defaultLeftWidth:H?720:420,minLeftWidth:280,minRightWidth:300,storageKey:H?`loomcycle.explorer.split.stars`:`loomcycle.explorer.split.paths`,hideRight:H&&ue,children:[(0,l.jsxs)(`div`,{className:`left`,children:[(0,l.jsxs)(`div`,{className:`paths-toolbar`,children:[(0,l.jsxs)(`label`,{className:`paths-scope`,children:[(0,l.jsx)(`span`,{children:`scope`}),(0,l.jsxs)(`select`,{value:d,onChange:e=>f(e.target.value),children:[(0,l.jsx)(`option`,{value:`user`,children:`user`}),(0,l.jsx)(`option`,{value:`agent`,children:`agent`}),(0,l.jsx)(`option`,{value:`tenant`,children:`tenant`})]})]}),o&&(0,l.jsxs)(`div`,{className:`paths-nav-switch`,role:`group`,"aria-label":`Navigator`,children:[(0,l.jsx)(`button`,{type:`button`,"aria-pressed":!H,className:H?void 0:`active`,onClick:()=>ce(`tree`),title:`Browse the Path tree`,children:`tree`}),(0,l.jsx)(`button`,{type:`button`,"aria-pressed":H,className:H?`active`:void 0,onClick:()=>ce(`stars`),title:`Browse the scope as a 3D star map`,children:`stars`})]}),(0,l.jsxs)(`div`,{className:`paths-toolbar-actions`,children:[!H&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`button`,{type:`button`,onClick:Ae,title:`New folder in ${Z}`,children:`+ folder`}),(0,l.jsx)(`button`,{type:`button`,onClick:je,disabled:!Q,title:Q?`New document in ${Z}`:`Documents are agent/user scope only`,children:`+ document`})]}),(0,l.jsx)(`button`,{type:`button`,onClick:()=>void Ce(),title:`Reload the tree`,children:`↻`})]})]}),!H&&(0,l.jsxs)(`p`,{className:`paths-context`,children:[`new items → `,(0,l.jsx)(`code`,{children:Z}),F?.scopeId&&(0,l.jsxs)(l.Fragment,{children:[` · `,`subject `,(0,l.jsx)(`code`,{children:F.scopeId})]}),F?.tenant&&(0,l.jsxs)(l.Fragment,{children:[` · `,`tenant `,(0,l.jsx)(`code`,{children:F.tenant})]})]}),(_||Ee)&&(0,l.jsx)(`div`,{className:`paths-err`,children:_||Ee}),H&&o?(0,l.jsx)(`div`,{className:`paths-navigator`,children:o(ze)}):(0,l.jsx)(Bt,{tree:Se,root:L.get(``),expanded:x,onToggle:we,onLoadMore:q,onRetry:Te,selectedPath:C,onSelect:e=>P(e.fullPath),summaries:_e})]}),(0,l.jsx)(`div`,{className:`right`,children:$?(0,l.jsxs)(`div`,{className:`paths-doc`,children:[(0,l.jsx)(se,{trail:D,onJump:Ie}),(0,l.jsxs)(`div`,{className:`paths-doc-head`,children:[$.path?(0,l.jsx)(`code`,{className:`paths-doc-path`,children:$.path}):(0,l.jsxs)(`span`,{className:`paths-doc-path paths-doc-unfiled`,title:`Opened by id — no entry for this document was found in the Path tree`,children:[B($),` `,(0,l.jsx)(`em`,{children:`· opened by id, not in the tree`})]}),Ve&&(0,l.jsxs)(`div`,{className:`paths-detail-actions`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:Me,children:`rename / move`}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,onClick:Ne,children:`delete`})]})]}),(0,l.jsx)(Lt,{documentId:$.documentId,scope:d,titleHint:$.title,browse:F,principal:i,renderAssistant:a,initialChunkId:$.chunkId??Be?.chunkId,onOpenDocument:Fe,onTitle:Le,onChunkSelect:e=>me({documentId:$.documentId,chunkId:e}),focusChunk:Be},D.length)]}):J?(0,l.jsxs)(`div`,{className:`paths-detail`,children:[(0,l.jsxs)(`h2`,{children:[(0,l.jsx)(`span`,{className:`paths-detail-kind`,children:J.kind}),(0,l.jsx)(`code`,{children:J.fullPath})]}),(0,l.jsxs)(`dl`,{className:`paths-detail-meta`,children:[(0,l.jsx)(`dt`,{children:`scope`}),(0,l.jsx)(`dd`,{children:d}),Oe&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`dt`,{children:`stored`}),(0,l.jsx)(`dd`,{children:Oe})]})]}),Pe?(0,l.jsxs)(`div`,{className:`paths-detail-actions`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:Me,children:`rename / move`}),(0,l.jsx)(`button`,{type:`button`,className:`danger`,onClick:Ne,children:`delete`})]}):(0,l.jsxs)(`p`,{className:`paths-readonly`,children:[`Read-only — `,J.kind,` entries are managed by their own tool.`]})]}):(0,l.jsx)(`div`,{className:`empty`,children:(0,l.jsx)(`p`,{children:`Select a path on the left, or create a folder / document.`})})})]}),y&&(0,l.jsx)(Yt,{state:y,onClose:()=>b(null)})]})}function Yt({state:e,onClose:t}){let[n,r]=(0,c.useState)(()=>Object.fromEntries(e.fields.map(e=>[e.key,e.value??``]))),[i,a]=(0,c.useState)(!1),[o,s]=(0,c.useState)(null);return(0,l.jsx)(`div`,{className:`modal-overlay`,onClick:t,children:(0,l.jsxs)(`form`,{className:`modal`,onClick:e=>e.stopPropagation(),onSubmit:async r=>{r.preventDefault(),a(!0),s(null);try{await e.onSubmit(n),t()}catch(e){s(e instanceof Error?e.message:String(e)),a(!1)}},children:[(0,l.jsxs)(`div`,{className:`modal-header sticky-top`,children:[(0,l.jsx)(`h3`,{children:e.title}),(0,l.jsxs)(`div`,{className:`modal-buttons modal-buttons-top`,children:[(0,l.jsx)(`button`,{type:`button`,onClick:t,disabled:i,children:`cancel`}),(0,l.jsx)(`button`,{type:`submit`,className:e.danger?`danger`:`primary`,disabled:i,children:i?`working…`:e.submitLabel})]})]}),e.message&&(0,l.jsx)(`p`,{className:`modal-context`,children:e.message}),e.fields.map((e,t)=>(0,l.jsxs)(`label`,{className:`path-field`,children:[(0,l.jsx)(`span`,{children:e.label}),(0,l.jsx)(`input`,{className:`path-modal-input`,value:n[e.key],placeholder:e.placeholder,autoFocus:t===0,onChange:t=>r(n=>({...n,[e.key]:t.target.value}))})]},e.key)),o&&(0,l.jsx)(`div`,{className:`modal-err`,children:o})]})})}function Xt(e){let{documentId:t,scope:n,browse:r,theme:i}=e,a=I(e);return(0,l.jsx)(L,{theme:i,dataLayer:a,children:(0,l.jsx)(Zt,{...e},`${n}:${t}:${r?.scopeId??``}:${r?.tenant??``}`)})}function Zt({documentId:e,scope:t,titleHint:n,browse:r,principal:i,renderAssistant:a,onOpenDocument:o}){let[s,u]=(0,c.useState)([]),d=s[s.length-1],f=(0,c.useCallback)((e,t)=>{o?o(e,t):u(n=>R(n,t,e))},[o]),p=(0,c.useCallback)(e=>u(t=>re(t,e)),[]),m=(0,c.useCallback)((e,t)=>u(n=>ie(n,e,{title:t})),[]);return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(se,{trail:s,onJump:p}),(0,l.jsx)(Lt,{documentId:d?.documentId??e,scope:t,titleHint:d?d.title:n,browse:r,principal:i,renderAssistant:a,initialChunkId:d?.chunkId,onOpenDocument:f,onTitle:m},s.length)]})}exports.DEFAULT_SCHEME=Ae,exports.DocumentViewer=Xt,exports.EDGE_PAGE_CAP=pe,exports.PathExplorer=qt,exports.QUEUE_CHANGED_RETRIES=we,exports.RULING_CHUNK=Ce,exports.RequestError=P,exports.assetFetchFromConnection=N,exports.chunkColor=Pe,exports.chunkCount=Y,exports.createLoomcycleClient=ne,exports.dataLayerFromClient=x,exports.docColor=Ne,exports.effectiveScheme=Me,exports.hexToTint=$,exports.isQueueChanged=Te,exports.jsonPostFromConnection=F,exports.loadDocumentEdges=me,exports.runRuling=J,exports.tintStyle=Fe,exports.undoRulings=Ee;
|
|
17
|
+
//# sourceMappingURL=index.cjs.map
|