@loomcycle/explorer 0.3.0 → 0.4.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/index.cjs CHANGED
@@ -1,12 +1,12 @@
1
- "use strict";var We=Object.create;var he=Object.defineProperty;var ze=Object.getOwnPropertyDescriptor;var Ve=Object.getOwnPropertyNames;var Je=Object.getPrototypeOf,Ze=Object.prototype.hasOwnProperty;var Qe=(t,s,n,a)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of Ve(s))!Ze.call(t,r)&&r!==n&&he(t,r,{get:()=>s[r],enumerable:!(a=ze(s,r))||a.enumerable});return t};var Xe=(t,s,n)=>(n=t!=null?We(Je(t)):{},Qe(s||!t||!t.__esModule?he(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),c=require("react"),Ye=require("@loomcycle/client"),Ke=require("react-markdown"),et=require("remark-gfm");function de(t,s){return{pathLs:(n,a,r,l)=>t.path({op:"ls",path:n,scope:a,recursive:r},l),pathMkdir:(n,a,r)=>t.path({op:"mkdir",path:n,scope:a},r),pathMv:(n,a,r,l)=>t.path({op:"mv",path:n,to:a,scope:r},l),pathRm:(n,a,r,l)=>t.path({op:"rm",path:n,scope:a,recursive:r},l),documentCreate:(n,a,r,l)=>t.document({op:"create_document",title:n,path:a,scope:r},l),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,l,o)=>t.document({op:"create_chunk",document_id:n,parent_id:a,scope:l,...r},o),documentSetAsset:(n,a,r,l,o,i)=>t.document({op:"set_asset",id:n,media_type:a,data:r,...l?{filename:l}:{},scope:o},i),documentGetEdges:(n,a,r)=>t.document({op:"get_edges",document_id:n,scope:a},r),documentUpdateChunk:(n,a,r,l,o)=>t.document({op:"update_chunk",id:n,revision:a,scope:l,...r},o),documentExportMd:(n,a,r,l)=>t.document({op:"export_md",document_id:n,scope:a,include_metadata:r},l),...s?{documentAssetObjectUrl:async(n,a,r)=>{const l=new URLSearchParams({scope:a});r?.scopeId&&l.set("scope_id",r.scopeId),r?.tenant&&l.set("tenant",r.tenant);const o=await s(`/v1/_document/asset/${encodeURIComponent(n)}?${l.toString()}`);if(!o.ok)throw new Error(`asset fetch failed: ${o.status}`);const i=await o.blob();return URL.createObjectURL(i)}}:{}}}const ve=c.createContext(null);function tt({value:t,children:s}){return e.jsx(ve.Provider,{value:t,children:s})}function ne(){const t=c.useContext(ve);if(!t)throw new Error("useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)");return t}function Ne(t){const s=t.fetch;return new Ye.LoomcycleClient({baseUrl:t.baseUrl,authToken:t.token||void 0,fetch:(n,a)=>s?s(n,a):fetch(n,a)})}function we(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 Ce(t){const{dataLayer:s,client:n,connection:a}=t;return c.useMemo(()=>s||(n?de(n):a?de(Ne(a),we(a)):null),[s,n,a?.baseUrl,a?.token,a?.fetch])}function Se({theme:t,dataLayer:s,children:n}){const a=t?{"data-theme":t}:{};return s?e.jsx("div",{className:"loomcycle-explorer",...a,children:e.jsx(tt,{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 fe=6;function nt({children:t,defaultLeftWidth:s=380,minLeftWidth:n=180,minRightWidth:a=240,storageKey:r,className:l}){const[o,i]=c.useState(()=>{if(r){const b=localStorage.getItem(r),$=b?parseInt(b,10):NaN;if(Number.isFinite($)&&$>=n)return $}return s}),d=c.useRef(null),[h,f]=c.useState(!1);c.useEffect(()=>{!r||h||localStorage.setItem(r,String(Math.round(o)))},[r,o,h]);const g=c.useCallback(b=>{b.preventDefault(),f(!0),b.target.setPointerCapture(b.pointerId)},[]),x=c.useCallback(b=>{if(!h||!d.current)return;const $=d.current.getBoundingClientRect(),A=b.clientX-$.left,R=$.width-a-fe,F=Math.max(n,Math.min(A,R));i(F)},[h,n,a]),P=c.useCallback(b=>{f(!1);try{b.target.releasePointerCapture(b.pointerId)}catch{}},[]);return e.jsxs("div",{ref:d,className:`splitter ${l??""} ${h?"dragging":""}`,style:{gridTemplateColumns:`${o}px ${fe}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:g,onPointerMove:x,onPointerUp:P,onPointerCancel:P}),e.jsx("div",{className:"splitter-pane splitter-right",children:t[1]})]})}const st=["image/png","image/jpeg","image/gif","image/webp"];function Ee(t){return st.includes(t)}function _e(t){return new Promise((s,n)=>{if(!Ee(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??""),l=r.indexOf(",");if(l<0){n(new Error("could not decode image data"));return}s({mediaType:t.type,data:r.slice(l+1)})},a.readAsDataURL(t)})}function at(t){if(!t)return null;for(let s=0;s<t.length;s++){const n=t[s];if(n.kind==="file"&&Ee(n.type)){const a=n.getAsFile();if(a)return a}}return null}const te={"doc.rfc.draft":"#c99a3a","doc.rfc.review":"#6a7bd6","doc.rfc.confirmed":"#3a9ad6","doc.rfc.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"},rt=.16;function se(t){return t&&Object.keys(t).length>0?t:te}function me(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 Me(t,s){const n=t??"";return n?s[`chunk.${n}`]:void 0}function $e(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),l=parseInt(n.slice(4,6),16);return`rgba(${a}, ${r}, ${l}, ${rt})`}function ae(t){if(!t)return;const s=$e(t);if(s)return{background:s,borderLeft:`3px solid ${t}`}}function ct(t){const s=new Map;t.forEach(r=>s.set(r.id,{row:r,children:[]}));const n=[];t.forEach(r=>{const l=s.get(r.id);r.parent_id&&s.has(r.parent_id)?s.get(r.parent_id).children.push(l):n.push(l)});const a=r=>{r.sort((l,o)=>l.row.position-o.row.position),r.forEach(l=>a(l.children))};return a(n),n}function pe(t){return t==="image"?"🖼":t==="mermaid"?"📊":""}function Re(t,s){for(const n of t){if(n.row.id===s)return n;const a=Re(n.children,s);if(a)return a}}function ot({tree:t,selectedId:s,onSelect:n,colorEnabled:a,scheme:r,linkedIds:l}){const[o,i]=c.useState(()=>new Map),d=c.useCallback(f=>{i(g=>{const x=new Map(g);return x.set(f,g.get(f)===!1),x})},[]);c.useEffect(()=>{if(!s)return;const f=lt(t,s);f.length!==0&&i(g=>{let x=!1;const P=new Map(g);for(const b of f)P.get(b)===!1&&(P.set(b,!0),x=!0);return x?P:g})},[t,s]);const h=a?se(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(f=>e.jsx(Le,{node:f,expanded:o,toggle:d,selectedId:s,onSelect:n,tintScheme:h,linkedIds:l},f.row.id))})}function lt(t,s){const n=[],a=(r,l)=>{for(const o of r){if(o.row.id===s)return n.push(...l),!0;if(a(o.children,[...l,o.row.id]))return!0}return!1};return a(t,[]),n}function Le({node:t,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:l,linkedIds:o}){const i=t.children.length>0,d=s.get(t.row.id)!==!1,h=a===t.row.id,f=l?ae(Me(t.row.status,l)):void 0,g=o?.has(t.row.id);return e.jsxs("li",{className:`node chunk-node ${h?"selected":""}`,children:[e.jsxs("div",{className:"row chunk-row",style:f,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":d?"collapse":"expand",disabled:!i,onClick:x=>{x.stopPropagation(),i&&n(t.row.id)},children:i?d?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"chunk-link",onClick:()=>r(t),title:t.row.title,children:[pe(t.row.type)&&e.jsx("span",{className:"chunk-type-icon","aria-hidden":!0,children:pe(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}),g&&e.jsx("span",{className:"chunk-link-badge",title:"Has cross-reference links","aria-label":"linked",children:"🔗"})]})]}),i&&d&&e.jsx("ul",{className:"children chunk-children",children:t.children.map(x=>e.jsx(Le,{node:x,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:l,linkedIds:o},x.row.id))})]})}let it=0;function re({code:t}){const s=c.useRef(null),[n,a]=c.useState({});return c.useEffect(()=>{let r=!1;return(async()=>{try{const l=(await import("mermaid")).default,o=s.current?.closest("[data-theme]")?.getAttribute("data-theme"),i=o?o==="dark":!0;l.initialize({startOnLoad:!1,theme:i?"dark":"default",securityLevel:"strict"});const{svg:d}=await l.render(`lc-mmd-${it++}`,t);r||a({svg:d})}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 dt({chunk:t,scope:s,browse:n,onClose:a,onSaved:r}){const l=ne(),[o,i]=c.useState(t.title),[d,h]=c.useState(t.type??""),[f,g]=c.useState(t.status??""),[x,P]=c.useState(t.body??""),[b,$]=c.useState(t.fields?JSON.stringify(t.fields,null,2):""),[A,R]=c.useState(!1),[F,_]=c.useState(null),[G,I]=c.useState(!1),U=d==="mermaid",N=d==="image"||!!t.asset,[k,M]=c.useState(x);c.useEffect(()=>{if(!U)return;const u=setTimeout(()=>M(x),400);return()=>clearTimeout(u)},[x,U]);const O=async u=>{_(null),R(!0);try{const{mediaType:m,data:v}=await _e(u),w=await l.documentSetAsset(t.id,m,v,u.name,s,n);r(w),a()}catch(m){_(m instanceof Error?m.message:String(m)),R(!1)}},q=U?"Mermaid source":N?"Caption (optional)":"Body (Markdown)",z=async u=>{u.preventDefault(),_(null),I(!1);const m={body:x,title:o,type:d,status:f};if(b.trim()!=="")try{m.fields=JSON.parse(b)}catch{_("fields must be valid JSON");return}R(!0);try{const v=await l.documentUpdateChunk(t.id,t.revision,m,s,n);r(v),a()}catch(v){const w=v instanceof Error?v.message:String(v);_(w),I(/revision conflict/i.test(w)),R(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:a,children:e.jsxs("form",{className:"modal chunk-editor",onClick:u=>u.stopPropagation(),onSubmit:z,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:A,children:"cancel"}),e.jsx("button",{type:"submit",className:"primary",disabled:A,children:A?"saving…":"save"})]})]}),F&&e.jsxs("div",{className:"modal-err",children:[F,G&&" — 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:u=>i(u.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:d,placeholder:"(none)",onChange:u=>h(u.target.value)})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Status"}),e.jsx("input",{className:"path-modal-input",value:f,placeholder:"(none)",onChange:u=>g(u.target.value)})]})]}),N&&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:A,onChange:u=>{const m=u.target.files?.[0];m&&O(m)}})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:q}),e.jsx("textarea",{className:"modal-textarea chunk-body-input",rows:U?8:12,value:x,onChange:u=>P(u.target.value)})]}),U&&e.jsxs("div",{className:"path-field",children:[e.jsx("span",{children:"Preview"}),e.jsx("div",{className:"chunk-mermaid-preview",children:k.trim()?e.jsx(re,{code:k}):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:b,placeholder:"{}",onChange:u=>$(u.target.value)})]})]})})}function ut(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 mt({documentId:t,rootChunkId:s,docType:n,docStatus:a,chunkStatuses:r,scope:l,browse:o,onClose:i,onSaved:d}){const h=ne(),[f,g]=c.useState(null),[x,P]=c.useState(!1),[b,$]=c.useState({}),[A,R]=c.useState(!1),[F,_]=c.useState(null),[G,I]=c.useState(null),U=c.useMemo(()=>{const u=new Set(Object.keys(te));n&&u.add(`doc.${n}.${a||"draft"}`);for(const m of r)m&&u.add(`chunk.${m}`);return[...u].sort()},[n,a,r]);c.useEffect(()=>{let u=!1;return h.documentGetChunk(s,l,o).then(m=>{if(u)return;g(m);const v=m.fields??{};P(v.color_enabled===!0);const w=v.color_scheme??{};$({...te,...w})}).catch(m=>!u&&I(m instanceof Error?m.message:String(m))),()=>{u=!0}},[h,s,l,o]);const N=(u,m)=>$(v=>({...v,[u]:m})),k=u=>({...u??{},color_enabled:x,color_scheme:b}),M=async()=>{if(f){R(!0),I(null),_(null);try{await h.documentUpdateChunk(s,f.revision,{fields:k(f.fields)},l,o),d(),i()}catch(u){I(u instanceof Error?u.message:String(u)),R(!1)}}},O=async()=>{if(!n){I("this document has no type to match");return}R(!0),I(null),_(null);try{const{documents:u}=await h.documentsSummary({underPath:"/"},l,o),m=(u??[]).filter(w=>w.type===n&&w.document_id!==t&&w.root_chunk_id);let v=0;for(const w of m)try{const L=await h.documentGetChunk(w.root_chunk_id,l,o);await h.documentUpdateChunk(w.root_chunk_id,L.revision,{fields:k(L.fields)},l,o),v++}catch{}_(`Applied to ${v} other ${n} document(s).`),d()}catch(u){I(u instanceof Error?u.message:String(u))}finally{R(!1)}},q=U.filter(u=>u.startsWith("doc.")),z=U.filter(u=>u.startsWith("chunk."));return e.jsx("div",{className:"modal-overlay",onClick:i,children:e.jsxs("div",{className:"modal scheme-editor",onClick:u=>u.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:i,disabled:A,children:"cancel"}),e.jsx("button",{type:"button",className:"primary",onClick:()=>{M()},disabled:A||!f,children:A?"saving…":"save"})]})]}),G&&e.jsx("div",{className:"modal-err",children:G}),f?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"scheme-toggle",children:[e.jsx("input",{type:"checkbox",checked:x,onChange:u=>P(u.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)"}),q.map(u=>e.jsx(xe,{label:u.slice(4),value:b[u],onChange:m=>N(u,m)},u))]}),e.jsxs("div",{className:"scheme-group",children:[e.jsx("h4",{children:"Chunks (status)"}),z.map(u=>e.jsx(xe,{label:u.slice(6),value:b[u],onChange:m=>N(u,m)},u))]}),e.jsxs("div",{className:"scheme-editor-foot",children:[e.jsxs("button",{type:"button",onClick:()=>{O()},disabled:A||!n,title:n?`Apply this palette to every other ${n} document`:"This document has no type",children:["copy to all ",n||"same-type"," documents"]}),F&&e.jsx("span",{className:"scheme-status",children:F})]})]}):e.jsx("p",{className:"modal-context",children:"Loading…"})]})})}function xe({label:t,value:s,onChange:n}){const a=ut(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 ie=24;function ht({edges:t,documentId:s,selectedId:n,onSelectChunk:a}){const[r,l]=c.useState(!1),[o,i]=c.useState(!1);if(t.length===0)return null;const d=n?t.filter(g=>g.from_id===n||g.to_id===n):[],h=o?t:t.slice(0,ie),f=pt(h);return e.jsxs("div",{className:"doc-refs",children:[n&&e.jsxs("div",{className:"doc-refs-list-wrap",children:[e.jsx("h4",{children:"References"}),d.length===0?e.jsx("p",{className:"doc-refs-empty",children:"This chunk has no links."}):e.jsx("ul",{className:"doc-refs-list",children:d.map((g,x)=>e.jsx(ft,{edge:g,selectedId:n,documentId:s,onSelectChunk:a},`${g.from_id}-${g.to_id}-${g.kind}-${x}`))})]}),e.jsxs("div",{className:"doc-graph",children:[e.jsxs("button",{type:"button",className:"doc-graph-toggle",onClick:()=>l(g=>!g),children:[r?"▼":"▶"," relationship graph (",t.length,")"]}),r&&e.jsxs(e.Fragment,{children:[e.jsx(re,{code:f}),t.length>ie&&e.jsx("button",{type:"button",className:"doc-graph-more",onClick:()=>i(g=>!g),children:o?`show top ${ie}`:`show all ${t.length}`})]})]})]})}function ft({edge:t,selectedId:s,documentId:n,onSelectChunk:a}){const r=t.from_id===s,l=r?t.to_id:t.from_id,o=(r?t.to_title:t.from_title)||l.slice(0,8),i=r?t.to_document_id:t.from_document_id,d=!i||i===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?"→":"←"}),d?e.jsx("button",{type:"button",className:"doc-ref-target",onClick:()=>a(l),children:o}):e.jsxs("span",{className:"doc-ref-target external",title:"In another document",children:[o," ↗"]})]})}function pt(t){const s=["graph LR"];for(const n of t){const a=`${ge(n.from_id)}["${je(n.from_title,n.from_id)}"]`,r=`${ge(n.to_id)}["${je(n.to_title,n.to_id)}"]`,l=(n.kind||"link").replace(/[|"]/g," ");s.push(` ${a} -->|${l}| ${r}`)}return s.join(`
2
- `)}function ge(t){return"n"+t.replace(/[^a-zA-Z0-9]/g,"")}function je(t,s){const a=(t??"").replace(/["[\]{}|<>]/g," ").replace(/\s+/g," ").trim()||s.slice(0,6);return a.length>30?a.slice(0,29)+"…":a}function ye({source:t}){return e.jsx("div",{className:"md",children:e.jsx(Ke,{remarkPlugins:[et],components:xt,children:t})})}const xt={a({href:t,children:s}){const n=t??"",a=/^https?:\/\//i.test(n);return e.jsx("a",{href:n,target:a?"_blank":void 0,rel:a?"noopener noreferrer":void 0,children:s})},pre({children:t}){return e.jsx(e.Fragment,{children:t})},code({className:t,children:s}){const n=/language-(\w+)/.exec(t??"")?.[1],a=String(s??"");return n==="mermaid"?e.jsx(re,{code:a.replace(/\n$/,"")}):n||a.includes(`
3
- `)?e.jsx("pre",{className:"md-pre",children:e.jsx("code",{className:t,children:s})}):e.jsx("code",{children:s})}};function gt(t){const s=t.fields??{};return typeof s.alt=="string"&&s.alt.trim()!==""?s.alt:t.title||"image"}function De({documentId:t,scope:s,titleHint:n,browse:a,principal:r,renderAssistant:l}){const o=ne(),i=c.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),[d,h]=c.useState([]),[f,g]=c.useState(void 0),[x,P]=c.useState(null),[b,$]=c.useState("chunks"),[A,R]=c.useState(null),[F,_]=c.useState(null),[G,I]=c.useState(null),U=c.useRef(null),[N,k]=c.useState(null),[M,O]=c.useState(null),[q,z]=c.useState(!1),[u,m]=c.useState(0),[v,w]=c.useState(!1),[L,J]=c.useState(null),[Z,Q]=c.useState(!1),[V,X]=c.useState([]),y=c.useCallback(()=>m(p=>p+1),[]),E=c.useMemo(()=>ct(d),[d]),D=c.useMemo(()=>d.find(j=>!j.parent_id)?.title||n||"document",[d,n]),H=c.useMemo(()=>!!x&&!x.parent_id,[x]),B=c.useMemo(()=>L?.root_chunk_id??d.find(p=>!p.parent_id)?.id,[L,d]),Ae=c.useMemo(()=>[...new Set(d.map(p=>p.status).filter(p=>!!p))],[d]),Y=!!L?.color_enabled,Ie=c.useMemo(()=>{const p=new Set;for(const j of V)p.add(j.from_id),p.add(j.to_id);return p},[V]),Oe=c.useMemo(()=>Y?ae(me(L?.type,L?.status,se(L?.color_scheme))):void 0,[Y,L?.type,L?.status,L?.color_scheme]);c.useEffect(()=>{let p=!1;return z(!0),O(null),o.documentQueryChunks(t,s,i).then(j=>{if(p)return;const C=j.chunks??[];h(C),g(S=>S??C.find(T=>!T.parent_id)?.id)}).catch(j=>!p&&O(j instanceof Error?j.message:String(j))).finally(()=>!p&&z(!1)),()=>{p=!0}},[o,t,s,u,i]),c.useEffect(()=>{let p=!1;return o.documentGet(t,s,i).then(j=>!p&&J(j)).catch(()=>!p&&J(null)),()=>{p=!0}},[o,t,s,u,i]),c.useEffect(()=>{let p=!1;return o.documentGetEdges(t,s,i).then(j=>!p&&X(j.edges??[])).catch(()=>!p&&X([])),()=>{p=!0}},[o,t,s,u,i]),c.useEffect(()=>{g(void 0),P(null),R(null),$("chunks"),J(null),X([])},[t,s]),c.useEffect(()=>{if(!f){P(null);return}let p=!1;return o.documentGetChunk(f,s,i).then(j=>!p&&P(j)).catch(j=>!p&&O(j instanceof Error?j.message:String(j))),()=>{p=!0}},[o,f,s,u,i]),c.useEffect(()=>{const p=x?.type==="image"||!!x?.asset;if(!x||!p||!o.documentAssetObjectUrl){_(null),I(null);return}let j=!1,C=null;return I(null),o.documentAssetObjectUrl(x.id,s,i).then(S=>{if(j){URL.revokeObjectURL(S);return}C=S,_(S)}).catch(S=>!j&&I(S instanceof Error?S.message:String(S))),()=>{j=!0,C&&URL.revokeObjectURL(C)}},[o,x,s,i]),c.useEffect(()=>{if(b!=="markdown"||!f){R(null);return}const p=Re(E,f);if(!p)return;let j=!1;const C=[],S=(T,K)=>{C.push({node:T,depth:K}),T.children.forEach(ee=>S(ee,K+1))};return S(p,0),Promise.all(C.map(T=>o.documentGetChunk(T.node.row.id,s,i))).then(T=>{if(j)return;const K=C.map((ee,Be)=>{const He=Math.min(ee.depth+1,6),W=T[Be],le="#".repeat(He)+" "+ee.node.row.title;if(W.type==="mermaid"&&W.body)return le+"\n\n```mermaid\n"+W.body+"\n```\n";if(W.type==="image"||W.asset){const qe=W.body?" — "+W.body:"";return le+`
1
+ "use strict";var Xe=Object.create;var ge=Object.defineProperty;var Ye=Object.getOwnPropertyDescriptor;var Ke=Object.getOwnPropertyNames;var et=Object.getPrototypeOf,tt=Object.prototype.hasOwnProperty;var nt=(t,s,n,a)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of Ke(s))!tt.call(t,r)&&r!==n&&ge(t,r,{get:()=>s[r],enumerable:!(a=Ye(s,r))||a.enumerable});return t};var st=(t,s,n)=>(n=t!=null?Xe(et(t)):{},nt(s||!t||!t.__esModule?ge(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),o=require("react"),at=require("@loomcycle/client"),rt=require("react-markdown"),ct=require("remark-gfm");function he(t,s){return{pathLs:(n,a,r,l)=>t.path({op:"ls",path:n,scope:a,recursive:r},l),pathMkdir:(n,a,r)=>t.path({op:"mkdir",path:n,scope:a},r),pathMv:(n,a,r,l)=>t.path({op:"mv",path:n,to:a,scope:r},l),pathRm:(n,a,r,l)=>t.path({op:"rm",path:n,scope:a,recursive:r},l),documentCreate:(n,a,r,l)=>t.document({op:"create_document",title:n,path:a,scope:r},l),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,l,c,i)=>t.document({op:"create_chunk",document_id:n,parent_id:a,scope:l,...i?{after_id:i}:{},...r},c),documentReorderChunk:(n,a,r,l)=>t.document({op:"reorder_chunk",id:n,direction:a,scope:r},l),documentDeleteChunk:(n,a,r)=>t.document({op:"delete_chunk",id:n,scope:a},r),documentSetAsset:(n,a,r,l,c,i)=>t.document({op:"set_asset",id:n,media_type:a,data:r,...l?{filename:l}:{},scope:c},i),documentGetEdges:(n,a,r)=>t.document({op:"get_edges",document_id:n,scope:a},r),documentUpdateChunk:(n,a,r,l,c)=>t.document({op:"update_chunk",id:n,revision:a,scope:l,...r},c),documentExportMd:(n,a,r,l)=>t.document({op:"export_md",document_id:n,scope:a,include_metadata:r},l),...s?{documentAssetObjectUrl:async(n,a,r)=>{const l=new URLSearchParams({scope:a});r?.scopeId&&l.set("scope_id",r.scopeId),r?.tenant&&l.set("tenant",r.tenant);const c=await s(`/v1/_document/asset/${encodeURIComponent(n)}?${l.toString()}`);if(!c.ok)throw new Error(`asset fetch failed: ${c.status}`);const i=await c.blob();return URL.createObjectURL(i)}}:{}}}const Se=o.createContext(null);function ot({value:t,children:s}){return e.jsx(Se.Provider,{value:t,children:s})}function se(){const t=o.useContext(Se);if(!t)throw new Error("useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)");return t}function _e(t){const s=t.fetch;return new at.LoomcycleClient({baseUrl:t.baseUrl,authToken:t.token||void 0,fetch:(n,a)=>s?s(n,a):fetch(n,a)})}function Ee(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 Me(t){const{dataLayer:s,client:n,connection:a}=t;return o.useMemo(()=>s||(n?he(n):a?he(_e(a),Ee(a)):null),[s,n,a?.baseUrl,a?.token,a?.fetch])}function De({theme:t,dataLayer:s,children:n}){const a=t?{"data-theme":t}:{};return s?e.jsx("div",{className:"loomcycle-explorer",...a,children:e.jsx(ot,{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 je=6;function it({children:t,defaultLeftWidth:s=380,minLeftWidth:n=180,minRightWidth:a=240,storageKey:r,className:l}){const[c,i]=o.useState(()=>{if(r){const y=localStorage.getItem(r),$=y?parseInt(y,10):NaN;if(Number.isFinite($)&&$>=n)return $}return s}),d=o.useRef(null),[x,m]=o.useState(!1);o.useEffect(()=>{!r||x||localStorage.setItem(r,String(Math.round(c)))},[r,c,x]);const j=o.useCallback(y=>{y.preventDefault(),m(!0),y.target.setPointerCapture(y.pointerId)},[]),f=o.useCallback(y=>{if(!x||!d.current)return;const $=d.current.getBoundingClientRect(),P=y.clientX-$.left,L=$.width-a-je,F=Math.max(n,Math.min(P,L));i(F)},[x,n,a]),A=o.useCallback(y=>{m(!1);try{y.target.releasePointerCapture(y.pointerId)}catch{}},[]);return e.jsxs("div",{ref:d,className:`splitter ${l??""} ${x?"dragging":""}`,style:{gridTemplateColumns:`${c}px ${je}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(c),onPointerDown:j,onPointerMove:f,onPointerUp:A,onPointerCancel:A}),e.jsx("div",{className:"splitter-pane splitter-right",children:t[1]})]})}const lt=["image/png","image/jpeg","image/gif","image/webp"];function Re(t){return lt.includes(t)}function $e(t){return new Promise((s,n)=>{if(!Re(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??""),l=r.indexOf(",");if(l<0){n(new Error("could not decode image data"));return}s({mediaType:t.type,data:r.slice(l+1)})},a.readAsDataURL(t)})}function dt(t){if(!t)return null;for(let s=0;s<t.length;s++){const n=t[s];if(n.kind==="file"&&Re(n.type)){const a=n.getAsFile();if(a)return a}}return null}const ne={"doc.rfc.draft":"#c99a3a","doc.rfc.review":"#6a7bd6","doc.rfc.confirmed":"#3a9ad6","doc.rfc.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"},ut=.16;function ae(t){return t&&Object.keys(t).length>0?t:ne}function fe(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 Le(t,s){const n=t??"";return n?s[`chunk.${n}`]:void 0}function Te(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),l=parseInt(n.slice(4,6),16);return`rgba(${a}, ${r}, ${l}, ${ut})`}function re(t){if(!t)return;const s=Te(t);if(s)return{background:s,borderLeft:`3px solid ${t}`}}function ht(t){const s=new Map;t.forEach(r=>s.set(r.id,{row:r,children:[]}));const n=[];t.forEach(r=>{const l=s.get(r.id);r.parent_id&&s.has(r.parent_id)?s.get(r.parent_id).children.push(l):n.push(l)});const a=r=>{r.sort((l,c)=>l.row.position-c.row.position),r.forEach(l=>a(l.children))};return a(n),n}function be(t){return t==="image"?"🖼":t==="mermaid"?"📊":""}function Ae(t,s){for(const n of t){if(n.row.id===s)return n;const a=Ae(n.children,s);if(a)return a}}function mt({tree:t,selectedId:s,onSelect:n,colorEnabled:a,scheme:r,linkedIds:l}){const[c,i]=o.useState(()=>new Map),d=o.useCallback(m=>{i(j=>{const f=new Map(j);return f.set(m,j.get(m)===!1),f})},[]);o.useEffect(()=>{if(!s)return;const m=ft(t,s);m.length!==0&&i(j=>{let f=!1;const A=new Map(j);for(const y of m)A.get(y)===!1&&(A.set(y,!0),f=!0);return f?A:j})},[t,s]);const x=a?ae(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(Pe,{node:m,expanded:c,toggle:d,selectedId:s,onSelect:n,tintScheme:x,linkedIds:l},m.row.id))})}function ft(t,s){const n=[],a=(r,l)=>{for(const c of r){if(c.row.id===s)return n.push(...l),!0;if(a(c.children,[...l,c.row.id]))return!0}return!1};return a(t,[]),n}function Pe({node:t,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:l,linkedIds:c}){const i=t.children.length>0,d=s.get(t.row.id)!==!1,x=a===t.row.id,m=l?re(Le(t.row.status,l)):void 0,j=c?.has(t.row.id);return e.jsxs("li",{className:`node chunk-node ${x?"selected":""}`,children:[e.jsxs("div",{className:"row chunk-row",style:m,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":d?"collapse":"expand",disabled:!i,onClick:f=>{f.stopPropagation(),i&&n(t.row.id)},children:i?d?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"chunk-link",onClick:()=>r(t),title:t.row.title,children:[be(t.row.type)&&e.jsx("span",{className:"chunk-type-icon","aria-hidden":!0,children:be(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}),j&&e.jsx("span",{className:"chunk-link-badge",title:"Has cross-reference links","aria-label":"linked",children:"🔗"})]})]}),i&&d&&e.jsx("ul",{className:"children chunk-children",children:t.children.map(f=>e.jsx(Pe,{node:f,expanded:s,toggle:n,selectedId:a,onSelect:r,tintScheme:l,linkedIds:c},f.row.id))})]})}let pt=0;function ce({code:t}){const s=o.useRef(null),[n,a]=o.useState({});return o.useEffect(()=>{let r=!1;return(async()=>{try{const l=(await import("mermaid")).default,c=s.current?.closest("[data-theme]")?.getAttribute("data-theme"),i=c?c==="dark":!0;l.initialize({startOnLoad:!1,theme:i?"dark":"default",securityLevel:"strict"});const{svg:d}=await l.render(`lc-mmd-${pt++}`,t);r||a({svg:d})}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 xt({chunk:t,scope:s,browse:n,onClose:a,onSaved:r}){const l=se(),[c,i]=o.useState(t.title),[d,x]=o.useState(t.type??""),[m,j]=o.useState(t.status??""),[f,A]=o.useState(t.body??""),[y,$]=o.useState(t.fields?JSON.stringify(t.fields,null,2):""),[P,L]=o.useState(!1),[F,R]=o.useState(null),[G,I]=o.useState(!1),U=d==="mermaid",S=d==="image"||!!t.asset,[v,T]=o.useState(f);o.useEffect(()=>{if(!U)return;const h=setTimeout(()=>T(f),400);return()=>clearTimeout(h)},[f,U]);const B=async h=>{R(null),L(!0);try{const{mediaType:p,data:C}=await $e(h),_=await l.documentSetAsset(t.id,p,C,h.name,s,n);r(_),a()}catch(p){R(p instanceof Error?p.message:String(p)),L(!1)}},W=U?"Mermaid source":S?"Caption (optional)":"Body (Markdown)",E=async h=>{h.preventDefault(),R(null),I(!1);const p={body:f,title:c,type:d,status:m};if(y.trim()!=="")try{p.fields=JSON.parse(y)}catch{R("fields must be valid JSON");return}L(!0);try{const C=await l.documentUpdateChunk(t.id,t.revision,p,s,n);r(C),a()}catch(C){const _=C instanceof Error?C.message:String(C);R(_),I(/revision conflict/i.test(_)),L(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:a,children:e.jsxs("form",{className:"modal chunk-editor",onClick:h=>h.stopPropagation(),onSubmit:E,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:P,children:"cancel"}),e.jsx("button",{type:"submit",className:"primary",disabled:P,children:P?"saving…":"save"})]})]}),F&&e.jsxs("div",{className:"modal-err",children:[F,G&&" — 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:c,onChange:h=>i(h.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:d,placeholder:"(none)",onChange:h=>x(h.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:h=>j(h.target.value)})]})]}),S&&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:P,onChange:h=>{const p=h.target.files?.[0];p&&B(p)}})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:W}),e.jsx("textarea",{className:"modal-textarea chunk-body-input",rows:U?8:12,value:f,onChange:h=>A(h.target.value)})]}),U&&e.jsxs("div",{className:"path-field",children:[e.jsx("span",{children:"Preview"}),e.jsx("div",{className:"chunk-mermaid-preview",children:v.trim()?e.jsx(ce,{code:v}):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:y,placeholder:"{}",onChange:h=>$(h.target.value)})]})]})})}function gt(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 jt({documentId:t,rootChunkId:s,docType:n,docStatus:a,chunkStatuses:r,scope:l,browse:c,onClose:i,onSaved:d}){const x=se(),[m,j]=o.useState(null),[f,A]=o.useState(!1),[y,$]=o.useState({}),[P,L]=o.useState(!1),[F,R]=o.useState(null),[G,I]=o.useState(null),U=o.useMemo(()=>{const h=new Set(Object.keys(ne));n&&h.add(`doc.${n}.${a||"draft"}`);for(const p of r)p&&h.add(`chunk.${p}`);return[...h].sort()},[n,a,r]);o.useEffect(()=>{let h=!1;return x.documentGetChunk(s,l,c).then(p=>{if(h)return;j(p);const C=p.fields??{};A(C.color_enabled===!0);const _=C.color_scheme??{};$({...ne,..._})}).catch(p=>!h&&I(p instanceof Error?p.message:String(p))),()=>{h=!0}},[x,s,l,c]);const S=(h,p)=>$(C=>({...C,[h]:p})),v=h=>({...h??{},color_enabled:f,color_scheme:y}),T=async()=>{if(m){L(!0),I(null),R(null);try{await x.documentUpdateChunk(s,m.revision,{fields:v(m.fields)},l,c),d(),i()}catch(h){I(h instanceof Error?h.message:String(h)),L(!1)}}},B=async()=>{if(!n){I("this document has no type to match");return}L(!0),I(null),R(null);try{const{documents:h}=await x.documentsSummary({underPath:"/"},l,c),p=(h??[]).filter(_=>_.type===n&&_.document_id!==t&&_.root_chunk_id);let C=0;for(const _ of p)try{const V=await x.documentGetChunk(_.root_chunk_id,l,c);await x.documentUpdateChunk(_.root_chunk_id,V.revision,{fields:v(V.fields)},l,c),C++}catch{}R(`Applied to ${C} other ${n} document(s).`),d()}catch(h){I(h instanceof Error?h.message:String(h))}finally{L(!1)}},W=U.filter(h=>h.startsWith("doc.")),E=U.filter(h=>h.startsWith("chunk."));return e.jsx("div",{className:"modal-overlay",onClick:i,children:e.jsxs("div",{className:"modal scheme-editor",onClick:h=>h.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:i,disabled:P,children:"cancel"}),e.jsx("button",{type:"button",className:"primary",onClick:()=>{T()},disabled:P||!m,children:P?"saving…":"save"})]})]}),G&&e.jsx("div",{className:"modal-err",children:G}),m?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"scheme-toggle",children:[e.jsx("input",{type:"checkbox",checked:f,onChange:h=>A(h.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)"}),W.map(h=>e.jsx(ye,{label:h.slice(4),value:y[h],onChange:p=>S(h,p)},h))]}),e.jsxs("div",{className:"scheme-group",children:[e.jsx("h4",{children:"Chunks (status)"}),E.map(h=>e.jsx(ye,{label:h.slice(6),value:y[h],onChange:p=>S(h,p)},h))]}),e.jsxs("div",{className:"scheme-editor-foot",children:[e.jsxs("button",{type:"button",onClick:()=>{B()},disabled:P||!n,title:n?`Apply this palette to every other ${n} document`:"This document has no type",children:["copy to all ",n||"same-type"," documents"]}),F&&e.jsx("span",{className:"scheme-status",children:F})]})]}):e.jsx("p",{className:"modal-context",children:"Loading…"})]})})}function ye({label:t,value:s,onChange:n}){const a=gt(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 ue=24;function bt({edges:t,documentId:s,selectedId:n,onSelectChunk:a}){const[r,l]=o.useState(!1),[c,i]=o.useState(!1);if(t.length===0)return null;const d=n?t.filter(j=>j.from_id===n||j.to_id===n):[],x=c?t:t.slice(0,ue),m=kt(x);return e.jsxs("div",{className:"doc-refs",children:[n&&e.jsxs("div",{className:"doc-refs-list-wrap",children:[e.jsx("h4",{children:"References"}),d.length===0?e.jsx("p",{className:"doc-refs-empty",children:"This chunk has no links."}):e.jsx("ul",{className:"doc-refs-list",children:d.map((j,f)=>e.jsx(yt,{edge:j,selectedId:n,documentId:s,onSelectChunk:a},`${j.from_id}-${j.to_id}-${j.kind}-${f}`))})]}),e.jsxs("div",{className:"doc-graph",children:[e.jsxs("button",{type:"button",className:"doc-graph-toggle",onClick:()=>l(j=>!j),children:[r?"▼":"▶"," relationship graph (",t.length,")"]}),r&&e.jsxs(e.Fragment,{children:[e.jsx(ce,{code:m}),t.length>ue&&e.jsx("button",{type:"button",className:"doc-graph-more",onClick:()=>i(j=>!j),children:c?`show top ${ue}`:`show all ${t.length}`})]})]})]})}function yt({edge:t,selectedId:s,documentId:n,onSelectChunk:a}){const r=t.from_id===s,l=r?t.to_id:t.from_id,c=(r?t.to_title:t.from_title)||l.slice(0,8),i=r?t.to_document_id:t.from_document_id,d=!i||i===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?"→":"←"}),d?e.jsx("button",{type:"button",className:"doc-ref-target",onClick:()=>a(l),children:c}):e.jsxs("span",{className:"doc-ref-target external",title:"In another document",children:[c," ↗"]})]})}function kt(t){const s=["graph LR"];for(const n of t){const a=`${ke(n.from_id)}["${ve(n.from_title,n.from_id)}"]`,r=`${ke(n.to_id)}["${ve(n.to_title,n.to_id)}"]`,l=(n.kind||"link").replace(/[|"]/g," ");s.push(` ${a} -->|${l}| ${r}`)}return s.join(`
2
+ `)}function ke(t){return"n"+t.replace(/[^a-zA-Z0-9]/g,"")}function ve(t,s){const a=(t??"").replace(/["[\]{}|<>]/g," ").replace(/\s+/g," ").trim()||s.slice(0,6);return a.length>30?a.slice(0,29)+"…":a}function Ne({source:t}){return e.jsx("div",{className:"md",children:e.jsx(rt,{remarkPlugins:[ct],components:vt,children:t})})}const vt={a({href:t,children:s}){const n=t??"",a=/^https?:\/\//i.test(n);return e.jsx("a",{href:n,target:a?"_blank":void 0,rel:a?"noopener noreferrer":void 0,children:s})},pre({children:t}){return e.jsx(e.Fragment,{children:t})},code({className:t,children:s}){const n=/language-(\w+)/.exec(t??"")?.[1],a=String(s??"");return n==="mermaid"?e.jsx(ce,{code:a.replace(/\n$/,"")}):n||a.includes(`
3
+ `)?e.jsx("pre",{className:"md-pre",children:e.jsx("code",{className:t,children:s})}):e.jsx("code",{children:s})}};function Nt(t){const s=t.fields??{};return typeof s.alt=="string"&&s.alt.trim()!==""?s.alt:t.title||"image"}function Ie({documentId:t,scope:s,titleHint:n,browse:a,principal:r,renderAssistant:l}){const c=se(),i=o.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),[d,x]=o.useState([]),[m,j]=o.useState(void 0),[f,A]=o.useState(null),[y,$]=o.useState("chunks"),[P,L]=o.useState(null),[F,R]=o.useState(null),[G,I]=o.useState(null),U=o.useRef(null),[S,v]=o.useState(null),[T,B]=o.useState(!1),[W,E]=o.useState(null),[h,p]=o.useState(!1),[C,_]=o.useState(0),[V,oe]=o.useState(!1),[O,Z]=o.useState(null),[Q,Y]=o.useState(!1),[k,D]=o.useState([]),N=o.useCallback(()=>_(u=>u+1),[]),H=o.useMemo(()=>ht(d),[d]),X=o.useMemo(()=>d.find(g=>!g.parent_id)?.title||n||"document",[d,n]),J=o.useMemo(()=>!!f&&!f.parent_id,[f]),q=o.useMemo(()=>O?.root_chunk_id??d.find(u=>!u.parent_id)?.id,[O,d]),Fe=o.useMemo(()=>[...new Set(d.map(u=>u.status).filter(u=>!!u))],[d]),K=!!O?.color_enabled,Ge=o.useMemo(()=>{const u=new Set;for(const g of k)u.add(g.from_id),u.add(g.to_id);return u},[k]),Be=o.useMemo(()=>K?re(fe(O?.type,O?.status,ae(O?.color_scheme))):void 0,[K,O?.type,O?.status,O?.color_scheme]);o.useEffect(()=>{let u=!1;return p(!0),E(null),c.documentQueryChunks(t,s,i).then(g=>{if(u)return;const b=g.chunks??[];x(b),j(w=>w??b.find(M=>!M.parent_id)?.id)}).catch(g=>!u&&E(g instanceof Error?g.message:String(g))).finally(()=>!u&&p(!1)),()=>{u=!0}},[c,t,s,C,i]),o.useEffect(()=>{let u=!1;return c.documentGet(t,s,i).then(g=>!u&&Z(g)).catch(()=>!u&&Z(null)),()=>{u=!0}},[c,t,s,C,i]),o.useEffect(()=>{let u=!1;return c.documentGetEdges(t,s,i).then(g=>!u&&D(g.edges??[])).catch(()=>!u&&D([])),()=>{u=!0}},[c,t,s,C,i]),o.useEffect(()=>{j(void 0),A(null),L(null),$("chunks"),Z(null),D([])},[t,s]),o.useEffect(()=>{if(!m){A(null);return}let u=!1;return c.documentGetChunk(m,s,i).then(g=>!u&&A(g)).catch(g=>!u&&E(g instanceof Error?g.message:String(g))),()=>{u=!0}},[c,m,s,C,i]),o.useEffect(()=>{const u=f?.type==="image"||!!f?.asset;if(!f||!u||!c.documentAssetObjectUrl){R(null),I(null);return}let g=!1,b=null;return I(null),c.documentAssetObjectUrl(f.id,s,i).then(w=>{if(g){URL.revokeObjectURL(w);return}b=w,R(w)}).catch(w=>!g&&I(w instanceof Error?w.message:String(w))),()=>{g=!0,b&&URL.revokeObjectURL(b)}},[c,f,s,i]),o.useEffect(()=>{if(y!=="markdown"||!m){L(null);return}const u=Ae(H,m);if(!u)return;let g=!1;const b=[],w=(M,ee)=>{b.push({node:M,depth:ee}),M.children.forEach(te=>w(te,ee+1))};return w(u,0),Promise.all(b.map(M=>c.documentGetChunk(M.node.row.id,s,i))).then(M=>{if(g)return;const ee=b.map((te,Je)=>{const Ze=Math.min(te.depth+1,6),z=M[Je],de="#".repeat(Ze)+" "+te.node.row.title;if(z.type==="mermaid"&&z.body)return de+"\n\n```mermaid\n"+z.body+"\n```\n";if(z.type==="image"||z.asset){const Qe=z.body?" — "+z.body:"";return de+`
4
4
 
5
- _🖼 image chunk_`+qe+`
6
- `}return le+(W.body?`
5
+ _🖼 image chunk_`+Qe+`
6
+ `}return de+(z.body?`
7
7
 
8
- `+W.body:"")+`
8
+ `+z.body:"")+`
9
9
  `}).join(`
10
- `);R(K)}).catch(T=>!j&&O(T instanceof Error?T.message:String(T))),()=>{j=!0}},[o,b,f,E,s,u,i]);const Ue=c.useCallback(p=>g(p.row.id),[]),Fe=c.useCallback(async()=>{try{const p=await o.documentExportMd(t,s,!0,i),j=new Blob([p.markdown],{type:"text/markdown"}),C=URL.createObjectURL(j),S=document.createElement("a");S.href=C,S.download=(p.title||"document").replace(/[^\w.-]+/g,"_")+".md",S.click(),URL.revokeObjectURL(C)}catch(p){O(p instanceof Error?p.message:String(p))}},[o,t,s,i]),ce=!!o.documentCreateChunk&&!!B,oe=c.useCallback(async p=>{if(!o.documentCreateChunk||!o.documentSetAsset)return;const j=f||B;if(j){O(null);try{const{mediaType:C,data:S}=await _e(p),T=await o.documentCreateChunk(t,j,{title:p.name||"image",type:"image"},s,i);await o.documentSetAsset(T.id,C,S,p.name,s,i),y(),g(T.id)}catch(C){O(C instanceof Error?C.message:String(C))}}},[o,t,s,i,f,B,y]),Ge=c.useCallback(async()=>{if(!o.documentCreateChunk)return;const p=f||B;if(p){O(null);try{const j=await o.documentCreateChunk(t,p,{title:"Diagram",type:"mermaid",body:`graph TD
11
- A[Start] --> B[End]`},s,i);y(),g(j.id),k(j)}catch(j){O(j instanceof Error?j.message:String(j))}}},[o,t,s,i,f,B,y]);return c.useEffect(()=>{if(!ce)return;const p=j=>{if(N||Z)return;const C=document.activeElement,S=C?.tagName;if(S==="INPUT"||S==="TEXTAREA"||C?.isContentEditable)return;const T=at(j.clipboardData?.items);T&&(j.preventDefault(),oe(T))};return window.addEventListener("paste",p),()=>window.removeEventListener("paste",p)},[ce,N,Z,oe]),e.jsxs("div",{className:"doc-viewer",children:[e.jsx("input",{ref:U,type:"file",accept:"image/png,image/jpeg,image/gif,image/webp",style:{display:"none"},onChange:p=>{const j=p.target.files?.[0];p.target.value="",j&&oe(j)}}),e.jsxs("div",{className:"doc-toolbar",style:Oe,children:[e.jsx("strong",{className:"doc-title",title:D,children:D}),e.jsxs("div",{className:"doc-toolbar-actions",children:[ce&&e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",onClick:()=>U.current?.click(),title:"Add an image chunk (upload — or paste a screenshot into the view)",children:"+ image"}),e.jsx("button",{type:"button",onClick:()=>{Ge()},title:"Add a Mermaid diagram chunk",children:"+ diagram"})]}),e.jsx("button",{type:"button",className:Y?"active":"",onClick:()=>Q(!0),disabled:!B,title:"Colors — tint chunk tiles + the Path tree by type/status",children:"colors"}),e.jsx("button",{type:"button",onClick:()=>{Fe()},title:"Download the whole document as Markdown (.md)",children:"↓ .md"}),l&&e.jsx("button",{type:"button",className:v?"active":"",onClick:()=>w(p=>!p),title:"Document Assistant — instruct an agent to restructure/import/link chunks",children:"assistant"}),e.jsx("button",{type:"button",onClick:y,title:"Reload",children:"↻"})]})]}),M&&e.jsx("div",{className:"paths-err",children:M}),e.jsxs("div",{className:"doc-split",children:[e.jsx("div",{className:"doc-chunktree",children:q&&d.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(ot,{tree:E,selectedId:f,onSelect:Ue,colorEnabled:Y,scheme:L?.color_scheme,linkedIds:Ie})}),e.jsx("div",{className:"doc-content",children:x?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-content-head",children:[e.jsx("h3",{children:x.title||"(untitled)"}),e.jsxs("div",{className:"doc-content-meta",children:[x.type&&e.jsx("span",{className:"chunk-badge",children:x.type}),x.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:x.status}),e.jsxs("span",{className:"chunk-rev",children:["rev ",x.revision]})]}),e.jsxs("div",{className:"doc-content-controls",children:[e.jsxs("div",{className:"doc-mode-toggle",children:[e.jsx("button",{type:"button",className:b==="chunks"?"active":"",onClick:()=>$("chunks"),title:"This chunk's own content",children:"chunk"}),e.jsx("button",{type:"button",className:b==="markdown"?"active":"",onClick:()=>$("markdown"),title:H?"Whole document as Markdown":"This chunk + its sub-chunks as Markdown",children:"markdown"})]}),e.jsx("button",{type:"button",onClick:()=>k(x),children:"edit"})]})]}),e.jsx("div",{className:"doc-content-body",children:x.type==="mermaid"?x.body?e.jsxs(e.Fragment,{children:[e.jsx(re,{code:x.body}),b==="markdown"&&e.jsx("pre",{className:"md-pre doc-mermaid-source",children:e.jsx("code",{children:x.body})})]}):e.jsx("p",{className:"doc-empty-body",children:"(empty diagram)"}):x.type==="image"||x.asset?G?e.jsxs("p",{className:"doc-empty-body",children:["Image failed to load: ",G]}):F?e.jsxs("figure",{className:"doc-image",children:[e.jsx("img",{src:F,alt:gt(x)}),x.body&&e.jsx("figcaption",{children:x.body})]}):e.jsx("p",{className:"doc-empty-body",children:"Loading image…"}):b==="markdown"?A!==null?e.jsx(ye,{source:A}):e.jsx("p",{className:"doc-empty-body",children:"Assembling Markdown…"}):x.body?e.jsx(ye,{source:x.body}):e.jsx("p",{className:"doc-empty-body",children:"(empty chunk)"})}),e.jsx(ht,{edges:V,documentId:t,selectedId:f,onSelectChunk:g})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a chunk on the left."})})})]}),v&&l&&e.jsx("div",{className:"doc-assistant",children:l({documentId:t,scope:s,subject:r?.subject,onChanged:y})}),N&&e.jsx(dt,{chunk:N,scope:s,browse:i,onClose:()=>k(null),onSaved:y}),Z&&B&&e.jsx(mt,{documentId:t,rootChunkId:B,docType:L?.type,docStatus:L?.status,chunkStatuses:Ae,scope:s,browse:i,onClose:()=>Q(!1),onSaved:y})]})}function jt(t){const s={name:"",fullPath:"",kind:"directory",explicit:!0,children:[]},n=new Map([["",s]]),a=l=>{const o=n.get(l);if(o)return o;const i=l.lastIndexOf("/"),d=i<=0?"":l.slice(0,i),h=l.slice(i+1),f=a(d),g={name:h,fullPath:l,kind:"directory",explicit:!1,children:[]};return f.children.push(g),n.set(l,g),g};for(const l of t){const o=a(l.full_path);o.kind=l.kind,o.explicit=!0,o.resourceRef=l.resource_ref}const r=l=>{l.sort((o,i)=>{const d=o.kind==="directory",h=i.kind==="directory";return d!==h?d?-1:1:o.name.localeCompare(i.name)}),l.forEach(o=>r(o.children))};return r(s.children),s.children}function ue(t){const s=t.lastIndexOf("/");return s<=0?"":t.slice(0,s)}function yt(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 bt={directory:"📁",document:"📄",volume_mount:"💾",memory_entry:"🧠"};function kt({tree:t,selectedPath:s,onSelect:n,summaries:a}){const[r,l]=c.useState(()=>new Map),o=c.useCallback(i=>{l(d=>{const h=new Map(d);return h.set(i,d.get(i)===!1),h})},[]);return c.useEffect(()=>{s&&l(i=>{let d=!1;const h=new Map(i);let f=ue(s);for(;f!=="";)h.get(f)===!1&&(h.set(f,!0),d=!0),f=ue(f);return d?h:i})},[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(i=>e.jsx(Te,{node:i,expanded:r,toggle:o,selectedPath:s,onSelect:n,summaries:a},i.fullPath))})}function vt(t,s){if(t.kind!=="document"||!s)return;const n=t.resourceRef;return n?.document_id?s.get(n.document_id):void 0}function Te({node:t,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:l}){const o=t.children.length>0,i=s.get(t.fullPath)!==!1,d=a===t.fullPath,h=vt(t,l),f=h&&h.color_enabled?ae(me(h.type,h.status,se(h.color_scheme))):void 0;return e.jsxs("li",{className:`node path-node kind-${t.kind} ${d?"selected":""}`,children:[e.jsxs("div",{className:"row path-row",style:f,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":i?"collapse":"expand",disabled:!o,onClick:g=>{g.stopPropagation(),o&&n(t.fullPath)},children:o?i?"▼":"▶":"·"}),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:bt[t.kind]??"•"}),e.jsx("span",{className:"path-name",children:t.name}),h?.type&&e.jsx("span",{className:"chunk-badge",children:h.type}),h?.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:h.status}),!t.explicit&&e.jsx("span",{className:"path-implicit",title:"Implicit directory (no stored entry)",children:"implicit"})]})]}),o&&i&&e.jsx("ul",{className:"children path-children",children:t.children.map(g=>e.jsx(Te,{node:g,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:l},g.fullPath))})]})}const be=/^[A-Za-z0-9._-]+$/;function ke(t,s){return`${t}/${s}`}function Pe(t,s){for(const n of t){if(n.fullPath===s)return n;const a=Pe(n.children,s);if(a)return a}}function Nt(t){const{theme:s,defaultScope:n,path:a,onPathChange:r,browse:l,principal:o,renderAssistant:i,onError:d}=t,h=Ce(t);return e.jsx(Se,{theme:s,dataLayer:h,children:e.jsx(wt,{defaultScope:n,path:a,onPathChange:r,browse:l,principal:o,renderAssistant:i,onError:d})})}function wt({defaultScope:t,path:s,onPathChange:n,browse:a,principal:r,renderAssistant:l,onError:o}){const i=ne(),[d,h]=c.useState(t??"user"),[f,g]=c.useState([]),[x,P]=c.useState(void 0),[b,$]=c.useState(null),[A,R]=c.useState(!1),[F,_]=c.useState(null),G=s!==void 0?s:x,I=c.useRef(n);c.useEffect(()=>{I.current=n},[n]);const U=c.useRef(o);c.useEffect(()=>{U.current=o},[o]);const N=c.useCallback(y=>{I.current?.(y),P(y)},[]),k=c.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),M=c.useCallback(async()=>{R(!0),$(null);try{const y=await i.pathLs("/",d,!0,k);g(y.entries??[])}catch(y){$(y instanceof Error?y.message:String(y)),g([]),U.current?.(y)}finally{R(!1)}},[i,d,k]),O=c.useRef(!0);c.useEffect(()=>{O.current?O.current=!1:N(void 0),M()},[M,N]);const q=c.useMemo(()=>jt(f),[f]),[z,u]=c.useState(()=>new Map);c.useEffect(()=>{if(d==="tenant"){u(new Map);return}const y=f.filter(D=>D.kind==="document").map(D=>D.resource_ref?.document_id).filter(D=>!!D);if(y.length===0){u(new Map);return}let E=!1;return i.documentsSummary({documentIds:y},d,k).then(D=>{if(E)return;const H=new Map;for(const B of D.documents??[])H.set(B.document_id,B);u(H)}).catch(()=>{E||u(new Map)}),()=>{E=!0}},[i,f,d,k]);const m=c.useMemo(()=>G?Pe(q,G):void 0,[q,G]),v=c.useMemo(()=>m?m.kind==="directory"?m.fullPath:ue(m.fullPath):"",[m]),w=v||"/",L=d!=="tenant",J=c.useCallback(()=>{_({title:`New folder in ${w}`,fields:[{key:"name",label:"Folder name",placeholder:"e.g. launches"}],submitLabel:"Create",onSubmit:async y=>{const E=y.name.trim();if(!be.test(E))throw new Error("name may contain only [A-Za-z0-9._-]");const D=ke(v,E);await i.pathMkdir(D,d,k),await M(),N(D)}})},[i,v,w,d,k,M,N]),Z=c.useCallback(()=>{_({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 y=>{const E=y.name.trim();if(!be.test(E))throw new Error("name may contain only [A-Za-z0-9._-]");const D=y.title.trim()||E,H=ke(v,E);await i.documentCreate(D,H,d,k),await M(),N(H)}})},[i,v,w,d,k,M,N]),Q=c.useCallback(()=>{m&&_({title:`Rename / move ${m.fullPath}`,fields:[{key:"to",label:"New path",value:m.fullPath}],submitLabel:"Move",onSubmit:async y=>{const E=y.to.trim();if(!E.startsWith("/"))throw new Error("path must start with /");await i.pathMv(m.fullPath,E,d,k),await M(),N(E)}})},[i,m,d,k,M,N]),V=c.useCallback(()=>{if(!m)return;if(m.kind==="document"){const H=m.resourceRef?.document_id;_({title:`Delete document ${m.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(!H)throw new Error("this document dirent has no document_id");await i.documentDelete(H,d,k),await M(),N(void 0)}});return}const y=d==="tenant"?[]:yt(m),E=m.children.length;_({title:`Delete branch ${m.fullPath}`,message:E===0?"This removes the (empty) folder.":"This removes the folder and everything under it"+(y.length>0?` — including ${y.length} document(s), whose chunks are deleted.`:"."),fields:[],submitLabel:"Delete",danger:!0,onSubmit:async()=>{for(const D of y)await i.documentDelete(D,d,k);await i.pathRm(m.fullPath,d,!0,k),await M(),N(void 0)}})},[i,m,d,k,M,N]),X=m&&(m.kind==="directory"||m.kind==="document");return e.jsxs(e.Fragment,{children:[e.jsxs(nt,{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:d,onChange:y=>h(y.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:J,title:`New folder in ${w}`,children:"+ folder"}),e.jsx("button",{type:"button",onClick:Z,disabled:!L,title:L?`New document in ${w}`:"Documents are agent/user scope only",children:"+ document"}),e.jsx("button",{type:"button",onClick:()=>{M()},title:"Reload the tree",children:"↻"})]})]}),e.jsxs("p",{className:"paths-context",children:["new items → ",e.jsx("code",{children:w}),k?.scopeId&&e.jsxs(e.Fragment,{children:[" · ","subject ",e.jsx("code",{children:k.scopeId})]}),k?.tenant&&e.jsxs(e.Fragment,{children:[" · ","tenant ",e.jsx("code",{children:k.tenant})]})]}),b&&e.jsx("div",{className:"paths-err",children:b}),A&&f.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(kt,{tree:q,selectedPath:G,onSelect:y=>N(y.fullPath),summaries:z})]}),e.jsx("div",{className:"right",children:m?m.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:m.fullPath}),e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:Q,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:V,children:"delete"})]})]}),e.jsx(De,{documentId:m.resourceRef?.document_id??"",scope:d==="agent"?"agent":"user",titleHint:m.name,browse:k,principal:r,renderAssistant:l})]}):e.jsxs("div",{className:"paths-detail",children:[e.jsxs("h2",{children:[e.jsx("span",{className:"paths-detail-kind",children:m.kind}),e.jsx("code",{children:m.fullPath})]}),e.jsxs("dl",{className:"paths-detail-meta",children:[e.jsx("dt",{children:"scope"}),e.jsx("dd",{children:d}),e.jsx("dt",{children:"stored"}),e.jsx("dd",{children:m.explicit?"yes":"implicit (no entry)"})]}),X?e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:Q,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:V,children:"delete"})]}):e.jsxs("p",{className:"paths-readonly",children:["Read-only — ",m.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."})})})]}),F&&e.jsx(Ct,{state:F,onClose:()=>_(null)})]})}function Ct({state:t,onClose:s}){const[n,a]=c.useState(()=>Object.fromEntries(t.fields.map(h=>[h.key,h.value??""]))),[r,l]=c.useState(!1),[o,i]=c.useState(null),d=async h=>{h.preventDefault(),l(!0),i(null);try{await t.onSubmit(n),s()}catch(f){i(f instanceof Error?f.message:String(f)),l(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:s,children:e.jsxs("form",{className:"modal",onClick:h=>h.stopPropagation(),onSubmit:d,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((h,f)=>e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:h.label}),e.jsx("input",{className:"path-modal-input",value:n[h.key],placeholder:h.placeholder,autoFocus:f===0,onChange:g=>a(x=>({...x,[h.key]:g.target.value}))})]},h.key)),o&&e.jsx("div",{className:"modal-err",children:o})]})})}function St(t){const{documentId:s,scope:n,titleHint:a,browse:r,theme:l,principal:o,renderAssistant:i}=t,d=Ce(t);return e.jsx(Se,{theme:l,dataLayer:d,children:e.jsx(De,{documentId:s,scope:n,titleHint:a,browse:r,principal:o,renderAssistant:i})})}exports.DEFAULT_SCHEME=te;exports.DocumentViewer=St;exports.PathExplorer=Nt;exports.assetFetchFromConnection=we;exports.chunkColor=Me;exports.createLoomcycleClient=Ne;exports.dataLayerFromClient=de;exports.docColor=me;exports.effectiveScheme=se;exports.hexToTint=$e;exports.tintStyle=ae;
10
+ `);L(ee)}).catch(M=>!g&&E(M instanceof Error?M.message:String(M))),()=>{g=!0}},[c,y,m,H,s,C,i]);const He=o.useCallback(u=>j(u.row.id),[]),qe=o.useCallback(async()=>{try{const u=await c.documentExportMd(t,s,!0,i),g=new Blob([u.markdown],{type:"text/markdown"}),b=URL.createObjectURL(g),w=document.createElement("a");w.href=b,w.download=(u.title||"document").replace(/[^\w.-]+/g,"_")+".md",w.click(),URL.revokeObjectURL(b)}catch(u){E(u instanceof Error?u.message:String(u))}},[c,t,s,i]),ie=!!c.documentCreateChunk&&!!q,le=o.useCallback(async u=>{if(!c.documentCreateChunk||!c.documentSetAsset)return;const g=m||q;if(g){E(null);try{const{mediaType:b,data:w}=await $e(u),M=await c.documentCreateChunk(t,g,{title:u.name||"image",type:"image"},s,i);await c.documentSetAsset(M.id,b,w,u.name,s,i),N(),j(M.id)}catch(b){E(b instanceof Error?b.message:String(b))}}},[c,t,s,i,m,q,N]),We=o.useCallback(async()=>{if(!c.documentCreateChunk)return;const u=m||q;if(u){E(null);try{const g=await c.documentCreateChunk(t,u,{title:"Diagram",type:"mermaid",body:`graph TD
11
+ A[Start] --> B[End]`},s,i);N(),j(g.id),v(g)}catch(g){E(g instanceof Error?g.message:String(g))}}},[c,t,s,i,m,q,N]),ze=o.useCallback(async()=>{if(!c.documentCreateChunk)return;const u=m&&!J?m:void 0,g=m||q;if(g){E(null);try{const b=await c.documentCreateChunk(t,u?"":g,{title:"Text",body:""},s,i,u);N(),j(b.id),v(b)}catch(b){E(b instanceof Error?b.message:String(b))}}},[c,t,s,i,m,J,q,N]),pe=o.useMemo(()=>{if(!f||J)return{canUp:!1,canDown:!1};const u=f.parent_id??"",g=d.filter(w=>(w.parent_id??"")===u).sort((w,M)=>w.position-M.position||w.id.localeCompare(M.id)),b=g.findIndex(w=>w.id===f.id);return{canUp:b>0,canDown:b>=0&&b<g.length-1}},[d,f,J]),xe=o.useCallback(async u=>{if(!(!c.documentReorderChunk||!m)){E(null);try{await c.documentReorderChunk(m,u,s,i),N()}catch(g){E(g instanceof Error?g.message:String(g))}}},[c,m,s,i,N]),Ve=o.useCallback(async()=>{if(!c.documentDeleteChunk||!f)return;E(null);const u=f.parent_id;try{await c.documentDeleteChunk(f.id,s,i),B(!1),j(u||q),N()}catch(g){E(g instanceof Error?g.message:String(g)),B(!1)}},[c,f,s,i,q,N]);return o.useEffect(()=>{if(!ie)return;const u=g=>{if(S||Q)return;const b=document.activeElement,w=b?.tagName;if(w==="INPUT"||w==="TEXTAREA"||b?.isContentEditable)return;const M=dt(g.clipboardData?.items);M&&(g.preventDefault(),le(M))};return window.addEventListener("paste",u),()=>window.removeEventListener("paste",u)},[ie,S,Q,le]),e.jsxs("div",{className:"doc-viewer",children:[e.jsx("input",{ref:U,type:"file",accept:"image/png,image/jpeg,image/gif,image/webp",style:{display:"none"},onChange:u=>{const g=u.target.files?.[0];u.target.value="",g&&le(g)}}),e.jsxs("div",{className:"doc-toolbar",style:Be,children:[e.jsx("strong",{className:"doc-title",title:X,children:X}),e.jsxs("div",{className:"doc-toolbar-actions",children:[ie&&e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",onClick:()=>{ze()},title:"Add a text chunk after the selected chunk",children:"+ text"}),e.jsx("button",{type:"button",onClick:()=>U.current?.click(),title:"Add an image chunk (upload — or paste a screenshot into the view)",children:"+ image"}),e.jsx("button",{type:"button",onClick:()=>{We()},title:"Add a Mermaid diagram chunk",children:"+ diagram"})]}),e.jsx("button",{type:"button",className:K?"active":"",onClick:()=>Y(!0),disabled:!q,title:"Colors — tint chunk tiles + the Path tree by type/status",children:"colors"}),e.jsx("button",{type:"button",onClick:()=>{qe()},title:"Download the whole document as Markdown (.md)",children:"↓ .md"}),l&&e.jsx("button",{type:"button",className:V?"active":"",onClick:()=>oe(u=>!u),title:"Document Assistant — instruct an agent to restructure/import/link chunks",children:"assistant"}),e.jsx("button",{type:"button",onClick:N,title:"Reload",children:"↻"})]})]}),W&&e.jsx("div",{className:"paths-err",children:W}),e.jsxs("div",{className:"doc-split",children:[e.jsx("div",{className:"doc-chunktree",children:h&&d.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(mt,{tree:H,selectedId:m,onSelect:He,colorEnabled:K,scheme:O?.color_scheme,linkedIds:Ge})}),e.jsx("div",{className:"doc-content",children:f?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-content-head",children:[e.jsx("h3",{children:f.title||"(untitled)"}),e.jsxs("div",{className:"doc-content-meta",children:[f.type&&e.jsx("span",{className:"chunk-badge",children:f.type}),f.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:f.status}),e.jsxs("span",{className:"chunk-rev",children:["rev ",f.revision]})]}),e.jsxs("div",{className:"doc-content-controls",children:[e.jsxs("div",{className:"doc-mode-toggle",children:[e.jsx("button",{type:"button",className:y==="chunks"?"active":"",onClick:()=>$("chunks"),title:"This chunk's own content",children:"chunk"}),e.jsx("button",{type:"button",className:y==="markdown"?"active":"",onClick:()=>$("markdown"),title:J?"Whole document as Markdown":"This chunk + its sub-chunks as Markdown",children:"markdown"})]}),e.jsx("button",{type:"button",onClick:()=>v(f),children:"edit"}),!J&&e.jsxs(e.Fragment,{children:[e.jsx("button",{type:"button",onClick:()=>{xe("up")},disabled:!pe.canUp,title:"Move this chunk up within its level",children:"↑"}),e.jsx("button",{type:"button",onClick:()=>{xe("down")},disabled:!pe.canDown,title:"Move this chunk down within its level",children:"↓"})]}),!J&&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:f.type==="mermaid"?f.body?e.jsxs(e.Fragment,{children:[e.jsx(ce,{code:f.body}),y==="markdown"&&e.jsx("pre",{className:"md-pre doc-mermaid-source",children:e.jsx("code",{children:f.body})})]}):e.jsx("p",{className:"doc-empty-body",children:"(empty diagram)"}):f.type==="image"||f.asset?G?e.jsxs("p",{className:"doc-empty-body",children:["Image failed to load: ",G]}):F?e.jsxs("figure",{className:"doc-image",children:[e.jsx("img",{src:F,alt:Nt(f)}),f.body&&e.jsx("figcaption",{children:f.body})]}):e.jsx("p",{className:"doc-empty-body",children:"Loading image…"}):y==="markdown"?P!==null?e.jsx(Ne,{source:P}):e.jsx("p",{className:"doc-empty-body",children:"Assembling Markdown…"}):f.body?e.jsx(Ne,{source:f.body}):e.jsx("p",{className:"doc-empty-body",children:"(empty chunk)"})}),e.jsx(bt,{edges:k,documentId:t,selectedId:m,onSelectChunk:j})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a chunk on the left."})})})]}),V&&l&&e.jsx("div",{className:"doc-assistant",children:l({documentId:t,scope:s,subject:r?.subject,onChanged:N})}),S&&e.jsx(xt,{chunk:S,scope:s,browse:i,onClose:()=>v(null),onSaved:N}),Q&&q&&e.jsx(jt,{documentId:t,rootChunkId:q,docType:O?.type,docStatus:O?.status,chunkStatuses:Fe,scope:s,browse:i,onClose:()=>Y(!1),onSaved:N}),T&&f&&e.jsx("div",{className:"modal-overlay",onClick:()=>B(!1),children:e.jsxs("div",{className:"modal chunk-delete-confirm",onClick:u=>u.stopPropagation(),children:[e.jsx("h3",{children:"Delete chunk"}),e.jsxs("p",{className:"modal-context",children:["Delete “",f.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:()=>{Ve()},children:"delete"})]})]})})]})}function wt(t){const s={name:"",fullPath:"",kind:"directory",explicit:!0,children:[]},n=new Map([["",s]]),a=l=>{const c=n.get(l);if(c)return c;const i=l.lastIndexOf("/"),d=i<=0?"":l.slice(0,i),x=l.slice(i+1),m=a(d),j={name:x,fullPath:l,kind:"directory",explicit:!1,children:[]};return m.children.push(j),n.set(l,j),j};for(const l of t){const c=a(l.full_path);c.kind=l.kind,c.explicit=!0,c.resourceRef=l.resource_ref}const r=l=>{l.sort((c,i)=>{const d=c.kind==="directory",x=i.kind==="directory";return d!==x?d?-1:1:c.name.localeCompare(i.name)}),l.forEach(c=>r(c.children))};return r(s.children),s.children}function me(t){const s=t.lastIndexOf("/");return s<=0?"":t.slice(0,s)}function Ct(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 St={directory:"📁",document:"📄",volume_mount:"💾",memory_entry:"🧠"};function _t({tree:t,selectedPath:s,onSelect:n,summaries:a}){const[r,l]=o.useState(()=>new Map),c=o.useCallback(i=>{l(d=>{const x=new Map(d);return x.set(i,d.get(i)===!1),x})},[]);return o.useEffect(()=>{s&&l(i=>{let d=!1;const x=new Map(i);let m=me(s);for(;m!=="";)x.get(m)===!1&&(x.set(m,!0),d=!0),m=me(m);return d?x:i})},[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(i=>e.jsx(Ue,{node:i,expanded:r,toggle:c,selectedPath:s,onSelect:n,summaries:a},i.fullPath))})}function Et(t,s){if(t.kind!=="document"||!s)return;const n=t.resourceRef;return n?.document_id?s.get(n.document_id):void 0}function Ue({node:t,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:l}){const c=t.children.length>0,i=s.get(t.fullPath)!==!1,d=a===t.fullPath,x=Et(t,l),m=x&&x.color_enabled?re(fe(x.type,x.status,ae(x.color_scheme))):void 0;return e.jsxs("li",{className:`node path-node kind-${t.kind} ${d?"selected":""}`,children:[e.jsxs("div",{className:"row path-row",style:m,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":i?"collapse":"expand",disabled:!c,onClick:j=>{j.stopPropagation(),c&&n(t.fullPath)},children:c?i?"▼":"▶":"·"}),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:St[t.kind]??"•"}),e.jsx("span",{className:"path-name",children:t.name}),x?.type&&e.jsx("span",{className:"chunk-badge",children:x.type}),x?.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:x.status}),!t.explicit&&e.jsx("span",{className:"path-implicit",title:"Implicit directory (no stored entry)",children:"implicit"})]})]}),c&&i&&e.jsx("ul",{className:"children path-children",children:t.children.map(j=>e.jsx(Ue,{node:j,expanded:s,toggle:n,selectedPath:a,onSelect:r,summaries:l},j.fullPath))})]})}const we=/^[A-Za-z0-9._-]+$/;function Ce(t,s){return`${t}/${s}`}function Oe(t,s){for(const n of t){if(n.fullPath===s)return n;const a=Oe(n.children,s);if(a)return a}}function Mt(t){const{theme:s,defaultScope:n,path:a,onPathChange:r,browse:l,principal:c,renderAssistant:i,onError:d}=t,x=Me(t);return e.jsx(De,{theme:s,dataLayer:x,children:e.jsx(Dt,{defaultScope:n,path:a,onPathChange:r,browse:l,principal:c,renderAssistant:i,onError:d})})}function Dt({defaultScope:t,path:s,onPathChange:n,browse:a,principal:r,renderAssistant:l,onError:c}){const i=se(),[d,x]=o.useState(t??"user"),[m,j]=o.useState([]),[f,A]=o.useState(void 0),[y,$]=o.useState(null),[P,L]=o.useState(!1),[F,R]=o.useState(null),G=s!==void 0?s:f,I=o.useRef(n);o.useEffect(()=>{I.current=n},[n]);const U=o.useRef(c);o.useEffect(()=>{U.current=c},[c]);const S=o.useCallback(k=>{I.current?.(k),A(k)},[]),v=o.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),T=o.useCallback(async()=>{L(!0),$(null);try{const k=await i.pathLs("/",d,!0,v);j(k.entries??[])}catch(k){$(k instanceof Error?k.message:String(k)),j([]),U.current?.(k)}finally{L(!1)}},[i,d,v]),B=o.useRef(!0);o.useEffect(()=>{B.current?B.current=!1:S(void 0),T()},[T,S]);const W=o.useMemo(()=>wt(m),[m]),[E,h]=o.useState(()=>new Map);o.useEffect(()=>{if(d==="tenant"){h(new Map);return}const k=m.filter(N=>N.kind==="document").map(N=>N.resource_ref?.document_id).filter(N=>!!N);if(k.length===0){h(new Map);return}let D=!1;return i.documentsSummary({documentIds:k},d,v).then(N=>{if(D)return;const H=new Map;for(const X of N.documents??[])H.set(X.document_id,X);h(H)}).catch(()=>{D||h(new Map)}),()=>{D=!0}},[i,m,d,v]);const p=o.useMemo(()=>G?Oe(W,G):void 0,[W,G]),C=o.useMemo(()=>p?p.kind==="directory"?p.fullPath:me(p.fullPath):"",[p]),_=C||"/",V=d!=="tenant",oe=o.useCallback(()=>{R({title:`New folder in ${_}`,fields:[{key:"name",label:"Folder name",placeholder:"e.g. launches"}],submitLabel:"Create",onSubmit:async k=>{const D=k.name.trim();if(!we.test(D))throw new Error("name may contain only [A-Za-z0-9._-]");const N=Ce(C,D);await i.pathMkdir(N,d,v),await T(),S(N)}})},[i,C,_,d,v,T,S]),O=o.useCallback(()=>{R({title:`New document in ${_}`,fields:[{key:"name",label:"Path name",placeholder:"e.g. launch-plan"},{key:"title",label:"Title",placeholder:"e.g. Launch Plan"}],submitLabel:"Create",onSubmit:async k=>{const D=k.name.trim();if(!we.test(D))throw new Error("name may contain only [A-Za-z0-9._-]");const N=k.title.trim()||D,H=Ce(C,D);await i.documentCreate(N,H,d,v),await T(),S(H)}})},[i,C,_,d,v,T,S]),Z=o.useCallback(()=>{p&&R({title:`Rename / move ${p.fullPath}`,fields:[{key:"to",label:"New path",value:p.fullPath}],submitLabel:"Move",onSubmit:async k=>{const D=k.to.trim();if(!D.startsWith("/"))throw new Error("path must start with /");await i.pathMv(p.fullPath,D,d,v),await T(),S(D)}})},[i,p,d,v,T,S]),Q=o.useCallback(()=>{if(!p)return;if(p.kind==="document"){const H=p.resourceRef?.document_id;R({title:`Delete document ${p.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(!H)throw new Error("this document dirent has no document_id");await i.documentDelete(H,d,v),await T(),S(void 0)}});return}const k=d==="tenant"?[]:Ct(p),D=p.children.length;R({title:`Delete branch ${p.fullPath}`,message:D===0?"This removes the (empty) folder.":"This removes the folder and everything under it"+(k.length>0?` — including ${k.length} document(s), whose chunks are deleted.`:"."),fields:[],submitLabel:"Delete",danger:!0,onSubmit:async()=>{for(const N of k)await i.documentDelete(N,d,v);await i.pathRm(p.fullPath,d,!0,v),await T(),S(void 0)}})},[i,p,d,v,T,S]),Y=p&&(p.kind==="directory"||p.kind==="document");return e.jsxs(e.Fragment,{children:[e.jsxs(it,{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:d,onChange:k=>x(k.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:oe,title:`New folder in ${_}`,children:"+ folder"}),e.jsx("button",{type:"button",onClick:O,disabled:!V,title:V?`New document in ${_}`:"Documents are agent/user scope only",children:"+ document"}),e.jsx("button",{type:"button",onClick:()=>{T()},title:"Reload the tree",children:"↻"})]})]}),e.jsxs("p",{className:"paths-context",children:["new items → ",e.jsx("code",{children:_}),v?.scopeId&&e.jsxs(e.Fragment,{children:[" · ","subject ",e.jsx("code",{children:v.scopeId})]}),v?.tenant&&e.jsxs(e.Fragment,{children:[" · ","tenant ",e.jsx("code",{children:v.tenant})]})]}),y&&e.jsx("div",{className:"paths-err",children:y}),P&&m.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(_t,{tree:W,selectedPath:G,onSelect:k=>S(k.fullPath),summaries:E})]}),e.jsx("div",{className:"right",children:p?p.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:p.fullPath}),e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:Z,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:Q,children:"delete"})]})]}),e.jsx(Ie,{documentId:p.resourceRef?.document_id??"",scope:d==="agent"?"agent":"user",titleHint:p.name,browse:v,principal:r,renderAssistant:l})]}):e.jsxs("div",{className:"paths-detail",children:[e.jsxs("h2",{children:[e.jsx("span",{className:"paths-detail-kind",children:p.kind}),e.jsx("code",{children:p.fullPath})]}),e.jsxs("dl",{className:"paths-detail-meta",children:[e.jsx("dt",{children:"scope"}),e.jsx("dd",{children:d}),e.jsx("dt",{children:"stored"}),e.jsx("dd",{children:p.explicit?"yes":"implicit (no entry)"})]}),Y?e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:Z,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:Q,children:"delete"})]}):e.jsxs("p",{className:"paths-readonly",children:["Read-only — ",p.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."})})})]}),F&&e.jsx(Rt,{state:F,onClose:()=>R(null)})]})}function Rt({state:t,onClose:s}){const[n,a]=o.useState(()=>Object.fromEntries(t.fields.map(x=>[x.key,x.value??""]))),[r,l]=o.useState(!1),[c,i]=o.useState(null),d=async x=>{x.preventDefault(),l(!0),i(null);try{await t.onSubmit(n),s()}catch(m){i(m instanceof Error?m.message:String(m)),l(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:s,children:e.jsxs("form",{className:"modal",onClick:x=>x.stopPropagation(),onSubmit:d,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((x,m)=>e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:x.label}),e.jsx("input",{className:"path-modal-input",value:n[x.key],placeholder:x.placeholder,autoFocus:m===0,onChange:j=>a(f=>({...f,[x.key]:j.target.value}))})]},x.key)),c&&e.jsx("div",{className:"modal-err",children:c})]})})}function $t(t){const{documentId:s,scope:n,titleHint:a,browse:r,theme:l,principal:c,renderAssistant:i}=t,d=Me(t);return e.jsx(De,{theme:l,dataLayer:d,children:e.jsx(Ie,{documentId:s,scope:n,titleHint:a,browse:r,principal:c,renderAssistant:i})})}exports.DEFAULT_SCHEME=ne;exports.DocumentViewer=$t;exports.PathExplorer=Mt;exports.assetFetchFromConnection=Ee;exports.chunkColor=Le;exports.createLoomcycleClient=_e;exports.dataLayerFromClient=he;exports.docColor=fe;exports.effectiveScheme=ae;exports.hexToTint=Te;exports.tintStyle=re;
12
12
  //# sourceMappingURL=index.cjs.map