@loomcycle/explorer 0.1.0 → 0.2.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.
@@ -0,0 +1,13 @@
1
+ import type { BrowseScope, DocScope } from "../types";
2
+ export interface ColorSchemeEditorProps {
3
+ documentId: string;
4
+ rootChunkId: string;
5
+ docType?: string;
6
+ docStatus?: string;
7
+ chunkStatuses: string[];
8
+ scope: DocScope;
9
+ browse?: BrowseScope;
10
+ onClose: () => void;
11
+ onSaved: () => void;
12
+ }
13
+ export default function ColorSchemeEditor({ documentId, rootChunkId, docType, docStatus, chunkStatuses, scope, browse, onClose, onSaved, }: ColorSchemeEditorProps): import("react").JSX.Element;
@@ -0,0 +1,8 @@
1
+ import type { DocEdge } from "../types";
2
+ export interface CrossReferencesProps {
3
+ edges: DocEdge[];
4
+ documentId: string;
5
+ selectedId?: string;
6
+ onSelectChunk: (id: string) => void;
7
+ }
8
+ export default function CrossReferences({ edges, documentId, selectedId, onSelectChunk, }: CrossReferencesProps): import("react").JSX.Element | null;
@@ -1,4 +1,5 @@
1
1
  import type { ChunkRow } from "../types";
2
+ import { type ColorScheme } from "../lib/colorScheme";
2
3
  export interface ChunkNode {
3
4
  row: ChunkRow;
4
5
  children: ChunkNode[];
@@ -9,5 +10,8 @@ export interface DocumentChunkTreeProps {
9
10
  tree: ChunkNode[];
10
11
  selectedId?: string;
11
12
  onSelect: (node: ChunkNode) => void;
13
+ colorEnabled?: boolean;
14
+ scheme?: ColorScheme;
15
+ linkedIds?: Set<string>;
12
16
  }
13
- export default function DocumentChunkTree({ tree, selectedId, onSelect }: DocumentChunkTreeProps): import("react").JSX.Element;
17
+ export default function DocumentChunkTree({ tree, selectedId, onSelect, colorEnabled, scheme, linkedIds, }: DocumentChunkTreeProps): import("react").JSX.Element;
@@ -0,0 +1,4 @@
1
+ import { type ReactNode } from "react";
2
+ export default function MermaidDiagram({ code }: {
3
+ code: string;
4
+ }): ReactNode;
@@ -1,4 +1,5 @@
1
1
  import type { PathEntry } from "../types";
2
+ import { type DocSummary } from "../lib/colorScheme";
2
3
  export interface PathNode {
3
4
  name: string;
4
5
  fullPath: string;
@@ -14,5 +15,6 @@ export interface PathTreeProps {
14
15
  tree: PathNode[];
15
16
  selectedPath?: string;
16
17
  onSelect: (node: PathNode) => void;
18
+ summaries?: Map<string, DocSummary>;
17
19
  }
18
- export default function PathTree({ tree, selectedPath, onSelect }: PathTreeProps): import("react").JSX.Element;
20
+ export default function PathTree({ tree, selectedPath, onSelect, summaries }: PathTreeProps): import("react").JSX.Element;
package/dist/index.cjs CHANGED
@@ -1,9 +1,8 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),i=require("react"),ie=require("@loomcycle/client");function V(s){return{pathLs:(t,a,n,r)=>s.path({op:"ls",path:t,scope:a,recursive:n},r),pathMkdir:(t,a,n)=>s.path({op:"mkdir",path:t,scope:a},n),pathMv:(t,a,n,r)=>s.path({op:"mv",path:t,to:a,scope:n},r),pathRm:(t,a,n,r)=>s.path({op:"rm",path:t,scope:a,recursive:n},r),documentCreate:(t,a,n,r)=>s.document({op:"create_document",title:t,path:a,scope:n},r),documentDelete:(t,a,n)=>s.document({op:"delete_document",id:t,scope:a},n),documentQueryChunks:(t,a,n)=>s.document({op:"query_chunks",document_id:t,scope:a,limit:1e3},n),documentGetChunk:(t,a,n)=>s.document({op:"get_chunk",id:t,scope:a},n),documentUpdateChunk:(t,a,n,r,o)=>s.document({op:"update_chunk",id:t,revision:a,scope:r,...n},o),documentExportMd:(t,a,n,r)=>s.document({op:"export_md",document_id:t,scope:a,include_metadata:n},r)}}const Y=i.createContext(null);function de({value:s,children:t}){return e.jsx(Y.Provider,{value:s,children:t})}function z(){const s=i.useContext(Y);if(!s)throw new Error("useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)");return s}function K(s){const t=s.fetch;return new ie.LoomcycleClient({baseUrl:s.baseUrl,authToken:s.token||void 0,fetch:(a,n)=>t?t(a,n):fetch(a,n)})}function ee(s){const{dataLayer:t,client:a,connection:n}=s;return i.useMemo(()=>t||(a?V(a):n?V(K(n)):null),[t,a,n?.baseUrl,n?.token,n?.fetch])}function te({theme:s,dataLayer:t,children:a}){const n=s?{"data-theme":s}:{};return t?e.jsx("div",{className:"loomcycle-explorer",...n,children:e.jsx(de,{value:t,children:a})}):e.jsx("div",{className:"loomcycle-explorer",...n,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 H=6;function ue({children:s,defaultLeftWidth:t=380,minLeftWidth:a=180,minRightWidth:n=240,storageKey:r,className:o}){const[l,d]=i.useState(()=>{if(r){const b=localStorage.getItem(r),w=b?parseInt(b,10):NaN;if(Number.isFinite(w)&&w>=a)return w}return t}),c=i.useRef(null),[u,h]=i.useState(!1);i.useEffect(()=>{!r||u||localStorage.setItem(r,String(Math.round(l)))},[r,l,u]);const N=i.useCallback(b=>{b.preventDefault(),h(!0),b.target.setPointerCapture(b.pointerId)},[]),j=i.useCallback(b=>{if(!u||!c.current)return;const w=c.current.getBoundingClientRect(),L=b.clientX-w.left,M=w.width-n-H,R=Math.max(a,Math.min(L,M));d(R)},[u,a,n]),S=i.useCallback(b=>{h(!1);try{b.target.releasePointerCapture(b.pointerId)}catch{}},[]);return e.jsxs("div",{ref:c,className:`splitter ${o??""} ${u?"dragging":""}`,style:{gridTemplateColumns:`${l}px ${H}px 1fr`},children:[e.jsx("div",{className:"splitter-pane splitter-left",children:s[0]}),e.jsx("div",{className:"splitter-handle",role:"separator","aria-orientation":"vertical","aria-valuenow":Math.round(l),onPointerDown:N,onPointerMove:j,onPointerUp:S,onPointerCancel:S}),e.jsx("div",{className:"splitter-pane splitter-right",children:s[1]})]})}function he(s){const t=new Map;s.forEach(r=>t.set(r.id,{row:r,children:[]}));const a=[];s.forEach(r=>{const o=t.get(r.id);r.parent_id&&t.has(r.parent_id)?t.get(r.parent_id).children.push(o):a.push(o)});const n=r=>{r.sort((o,l)=>o.row.position-l.row.position),r.forEach(o=>n(o.children))};return n(a),a}function ne(s,t){for(const a of s){if(a.row.id===t)return a;const n=ne(a.children,t);if(n)return n}}function me({tree:s,selectedId:t,onSelect:a}){const[n,r]=i.useState(()=>new Map),o=i.useCallback(l=>{r(d=>{const c=new Map(d);return c.set(l,d.get(l)===!1),c})},[]);return i.useEffect(()=>{if(!t)return;const l=pe(s,t);l.length!==0&&r(d=>{let c=!1;const u=new Map(d);for(const h of l)u.get(h)===!1&&(u.set(h,!0),c=!0);return c?u:d})},[s,t]),s.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"No chunks."})}):e.jsx("ul",{className:"tree chunk-tree",children:s.map(l=>e.jsx(se,{node:l,expanded:n,toggle:o,selectedId:t,onSelect:a},l.row.id))})}function pe(s,t){const a=[],n=(r,o)=>{for(const l of r){if(l.row.id===t)return a.push(...o),!0;if(n(l.children,[...o,l.row.id]))return!0}return!1};return n(s,[]),a}function se({node:s,expanded:t,toggle:a,selectedId:n,onSelect:r}){const o=s.children.length>0,l=t.get(s.row.id)!==!1,d=n===s.row.id;return e.jsxs("li",{className:`node chunk-node ${d?"selected":""}`,children:[e.jsxs("div",{className:"row chunk-row",children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":l?"collapse":"expand",disabled:!o,onClick:c=>{c.stopPropagation(),o&&a(s.row.id)},children:o?l?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"chunk-link",onClick:()=>r(s),title:s.row.title,children:[e.jsx("span",{className:"chunk-title",children:s.row.title||"(untitled)"}),s.row.type&&e.jsx("span",{className:"chunk-badge",children:s.row.type}),s.row.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:s.row.status})]})]}),o&&l&&e.jsx("ul",{className:"children chunk-children",children:s.children.map(c=>e.jsx(se,{node:c,expanded:t,toggle:a,selectedId:n,onSelect:r},c.row.id))})]})}function fe({chunk:s,scope:t,browse:a,onClose:n,onSaved:r}){const o=z(),[l,d]=i.useState(s.title),[c,u]=i.useState(s.type??""),[h,N]=i.useState(s.status??""),[j,S]=i.useState(s.body??""),[b,w]=i.useState(s.fields?JSON.stringify(s.fields,null,2):""),[L,M]=i.useState(!1),[R,C]=i.useState(null),[P,D]=i.useState(!1),W=async f=>{f.preventDefault(),C(null),D(!1);const g={body:j,title:l,type:c,status:h};if(b.trim()!=="")try{g.fields=JSON.parse(b)}catch{C("fields must be valid JSON");return}M(!0);try{const v=await o.documentUpdateChunk(s.id,s.revision,g,t,a);r(v),n()}catch(v){const O=v instanceof Error?v.message:String(v);C(O),D(/revision conflict/i.test(O)),M(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:n,children:e.jsxs("form",{className:"modal chunk-editor",onClick:f=>f.stopPropagation(),onSubmit:W,children:[e.jsx("h3",{children:"Edit chunk"}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Title"}),e.jsx("input",{className:"path-modal-input",value:l,onChange:f=>d(f.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:c,placeholder:"(none)",onChange:f=>u(f.target.value)})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Status"}),e.jsx("input",{className:"path-modal-input",value:h,placeholder:"(none)",onChange:f=>N(f.target.value)})]})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Body (Markdown)"}),e.jsx("textarea",{className:"modal-textarea chunk-body-input",rows:12,value:j,onChange:f=>S(f.target.value)})]}),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:f=>w(f.target.value)})]}),R&&e.jsxs("div",{className:"modal-err",children:[R,P&&" — reopen the chunk to load the latest, then reapply your edit."]}),e.jsxs("div",{className:"modal-buttons",children:[e.jsx("button",{type:"button",onClick:n,disabled:L,children:"cancel"}),e.jsx("button",{type:"submit",className:"primary",disabled:L,children:L?"saving…":"save"})]})]})})}function Z({source:s}){return e.jsx("div",{className:"md",children:xe(s)})}function xe(s){const t=s.replace(/\r\n/g,`
2
- `).split(`
3
- `),a=[];let n=0,r=0;for(;n<t.length;){const o=t[n];if(o.trim()===""){n++;continue}if(o.startsWith("```")){const h=[];for(n++;n<t.length&&!t[n].startsWith("```");)h.push(t[n]),n++;n++,a.push(e.jsx("pre",{className:"md-pre",children:e.jsx("code",{children:h.join(`
4
- `)})},r++));continue}const l=/^(#{1,6})\s+(.*)$/.exec(o);if(l){a.push(je(l[1].length,U(l[2]),r++)),n++;continue}if(/^>\s?/.test(o)){const h=[];for(;n<t.length&&/^>\s?/.test(t[n]);)h.push(t[n].replace(/^>\s?/,"")),n++;a.push(e.jsx("blockquote",{children:U(h.join(" "))},r++));continue}const d=/^[-*]\s+(.*)$/,c=/^\d+\.\s+(.*)$/;if(d.test(o)||c.test(o)){const h=c.test(o),N=h?c:d,j=[];let S=0;for(;n<t.length&&N.test(t[n]);){const b=N.exec(t[n]);j.push(e.jsx("li",{children:U(b[1])},S++)),n++}a.push(h?e.jsx("ol",{children:j},r++):e.jsx("ul",{children:j},r++));continue}const u=[];for(;n<t.length&&t[n].trim()!==""&&!t[n].startsWith("```")&&!/^(#{1,6})\s+/.test(t[n])&&!/^>\s?/.test(t[n])&&!d.test(t[n])&&!c.test(t[n]);)u.push(t[n]),n++;a.push(e.jsx("p",{children:U(u.join(" "))},r++))}return a}function je(s,t,a){switch(s){case 1:return e.jsx("h1",{children:t},a);case 2:return e.jsx("h2",{children:t},a);case 3:return e.jsx("h3",{children:t},a);case 4:return e.jsx("h4",{children:t},a);case 5:return e.jsx("h5",{children:t},a);default:return e.jsx("h6",{children:t},a)}}const be=/(`[^`]+`)|(\*\*[^*]+?\*\*)|(\*[^*]+?\*)|(\[[^\]]+\]\([^)]+\))/;function U(s){const t=[];let a=s,n=0;for(;a.length>0;){const r=be.exec(a);if(!r){t.push(a);break}r.index>0&&t.push(a.slice(0,r.index));const o=r[0];if(o.startsWith("`"))t.push(e.jsx("code",{children:o.slice(1,-1)},n++));else if(o.startsWith("**"))t.push(e.jsx("strong",{children:o.slice(2,-2)},n++));else if(o.startsWith("*"))t.push(e.jsx("em",{children:o.slice(1,-1)},n++));else{const l=/^\[([^\]]+)\]\(([^)]+)\)$/.exec(o),d=l[1],c=l[2],u=c.startsWith("/")&&!c.startsWith("//");/^https?:\/\//i.test(c)||u?t.push(e.jsx("a",{href:c,target:"_blank",rel:"noopener noreferrer",children:d},n++)):t.push(d)}a=a.slice(r.index+o.length)}return t}function ae({documentId:s,scope:t,titleHint:a,browse:n,principal:r,renderAssistant:o}){const l=z(),d=i.useMemo(()=>n&&(n.scopeId||n.tenant)?{scopeId:n.scopeId,tenant:n.tenant}:void 0,[n?.scopeId,n?.tenant]),[c,u]=i.useState([]),[h,N]=i.useState(void 0),[j,S]=i.useState(null),[b,w]=i.useState("chunks"),[L,M]=i.useState(null),[R,C]=i.useState(null),[P,D]=i.useState(null),[W,f]=i.useState(!1),[g,v]=i.useState(0),[O,F]=i.useState(!1),m=i.useCallback(()=>v(x=>x+1),[]),$=i.useMemo(()=>he(c),[c]),T=i.useMemo(()=>c.find(y=>!y.parent_id)?.title||a||"document",[c,a]),A=i.useMemo(()=>!!j&&!j.parent_id,[j]);i.useEffect(()=>{let x=!1;return f(!0),D(null),l.documentQueryChunks(s,t,d).then(y=>{if(x)return;const _=y.chunks??[];u(_),N(p=>p??_.find(k=>!k.parent_id)?.id)}).catch(y=>!x&&D(y instanceof Error?y.message:String(y))).finally(()=>!x&&f(!1)),()=>{x=!0}},[l,s,t,g,d]),i.useEffect(()=>{N(void 0),S(null),M(null),w("chunks")},[s,t]),i.useEffect(()=>{if(!h){S(null);return}let x=!1;return l.documentGetChunk(h,t,d).then(y=>!x&&S(y)).catch(y=>!x&&D(y instanceof Error?y.message:String(y))),()=>{x=!0}},[l,h,t,g,d]),i.useEffect(()=>{if(b!=="markdown"||!h){M(null);return}const x=ne($,h);if(!x)return;let y=!1;const _=[],p=(k,E)=>{_.push({node:k,depth:E}),k.children.forEach(I=>p(I,E+1))};return p(x,0),Promise.all(_.map(k=>l.documentGetChunk(k.node.row.id,t,d))).then(k=>{if(y)return;const E=_.map((I,ce)=>{const oe=Math.min(I.depth+1,6),G=k[ce];return"#".repeat(oe)+" "+I.node.row.title+(G.body?`
1
+ "use strict";var De=Object.create;var le=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Re=Object.getOwnPropertyNames;var Pe=Object.getPrototypeOf,Te=Object.prototype.hasOwnProperty;var Oe=(t,s,n,a)=>{if(s&&typeof s=="object"||typeof s=="function")for(let c of Re(s))!Te.call(t,c)&&c!==n&&le(t,c,{get:()=>s[c],enumerable:!(a=Le(s,c))||a.enumerable});return t};var Ae=(t,s,n)=>(n=t!=null?De(Pe(t)):{},Oe(s||!t||!t.__esModule?le(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("react"),Ie=require("@loomcycle/client"),Fe=require("react-markdown"),Ge=require("remark-gfm");function ae(t){return{pathLs:(s,n,a,c)=>t.path({op:"ls",path:s,scope:n,recursive:a},c),pathMkdir:(s,n,a)=>t.path({op:"mkdir",path:s,scope:n},a),pathMv:(s,n,a,c)=>t.path({op:"mv",path:s,to:n,scope:a},c),pathRm:(s,n,a,c)=>t.path({op:"rm",path:s,scope:n,recursive:a},c),documentCreate:(s,n,a,c)=>t.document({op:"create_document",title:s,path:n,scope:a},c),documentDelete:(s,n,a)=>t.document({op:"delete_document",id:s,scope:n},a),documentGet:(s,n,a)=>t.document({op:"get_document",id:s,scope:n},a),documentsSummary:(s,n,a)=>t.document({op:"documents_summary",scope:n,...s.documentIds?{document_ids:s.documentIds}:{},...s.underPath?{under_path:s.underPath}:{}},a),documentQueryChunks:(s,n,a)=>t.document({op:"query_chunks",document_id:s,scope:n,limit:1e3},a),documentGetChunk:(s,n,a)=>t.document({op:"get_chunk",id:s,scope:n},a),documentGetEdges:(s,n,a)=>t.document({op:"get_edges",document_id:s,scope:n},a),documentUpdateChunk:(s,n,a,c,i)=>t.document({op:"update_chunk",id:s,revision:n,scope:c,...a},i),documentExportMd:(s,n,a,c)=>t.document({op:"export_md",document_id:s,scope:n,include_metadata:a},c)}}const xe=r.createContext(null);function Ue({value:t,children:s}){return e.jsx(xe.Provider,{value:t,children:s})}function ee(){const t=r.useContext(xe);if(!t)throw new Error("useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)");return t}function je(t){const s=t.fetch;return new Ie.LoomcycleClient({baseUrl:t.baseUrl,authToken:t.token||void 0,fetch:(n,a)=>s?s(n,a):fetch(n,a)})}function be(t){const{dataLayer:s,client:n,connection:a}=t;return r.useMemo(()=>s||(n?ae(n):a?ae(je(a)):null),[s,n,a?.baseUrl,a?.token,a?.fetch])}function ke({theme:t,dataLayer:s,children:n}){const a=t?{"data-theme":t}:{};return s?e.jsx("div",{className:"loomcycle-explorer",...a,children:e.jsx(Ue,{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 ie=6;function He({children:t,defaultLeftWidth:s=380,minLeftWidth:n=180,minRightWidth:a=240,storageKey:c,className:i}){const[l,o]=r.useState(()=>{if(c){const k=localStorage.getItem(c),C=k?parseInt(k,10):NaN;if(Number.isFinite(C)&&C>=n)return C}return s}),d=r.useRef(null),[u,h]=r.useState(!1);r.useEffect(()=>{!c||u||localStorage.setItem(c,String(Math.round(l)))},[c,l,u]);const p=r.useCallback(k=>{k.preventDefault(),h(!0),k.target.setPointerCapture(k.pointerId)},[]),j=r.useCallback(k=>{if(!u||!d.current)return;const C=d.current.getBoundingClientRect(),P=k.clientX-C.left,D=C.width-a-ie,T=Math.max(n,Math.min(P,D));o(T)},[u,n,a]),E=r.useCallback(k=>{h(!1);try{k.target.releasePointerCapture(k.pointerId)}catch{}},[]);return e.jsxs("div",{ref:d,className:`splitter ${i??""} ${u?"dragging":""}`,style:{gridTemplateColumns:`${l}px ${ie}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(l),onPointerDown:p,onPointerMove:j,onPointerUp:E,onPointerCancel:E}),e.jsx("div",{className:"splitter-pane splitter-right",children:t[1]})]})}const Y={"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"},Be=.16;function te(t){return t&&Object.keys(t).length>0?t:Y}function ce(t,s,n){const a=t??"",c=s??"";if(a&&c&&n[`doc.${a}.${c}`])return n[`doc.${a}.${c}`];if(a&&n[`doc.${a}`])return n[`doc.${a}`]}function ge(t,s){const n=t??"";return n?s[`chunk.${n}`]:void 0}function ye(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),c=parseInt(n.slice(2,4),16),i=parseInt(n.slice(4,6),16);return`rgba(${a}, ${c}, ${i}, ${Be})`}function ne(t){if(!t)return;const s=ye(t);if(s)return{background:s,borderLeft:`3px solid ${t}`}}function We(t){const s=new Map;t.forEach(c=>s.set(c.id,{row:c,children:[]}));const n=[];t.forEach(c=>{const i=s.get(c.id);c.parent_id&&s.has(c.parent_id)?s.get(c.parent_id).children.push(i):n.push(i)});const a=c=>{c.sort((i,l)=>i.row.position-l.row.position),c.forEach(i=>a(i.children))};return a(n),n}function ve(t,s){for(const n of t){if(n.row.id===s)return n;const a=ve(n.children,s);if(a)return a}}function qe({tree:t,selectedId:s,onSelect:n,colorEnabled:a,scheme:c,linkedIds:i}){const[l,o]=r.useState(()=>new Map),d=r.useCallback(h=>{o(p=>{const j=new Map(p);return j.set(h,p.get(h)===!1),j})},[]);r.useEffect(()=>{if(!s)return;const h=ze(t,s);h.length!==0&&o(p=>{let j=!1;const E=new Map(p);for(const k of h)E.get(k)===!1&&(E.set(k,!0),j=!0);return j?E:p})},[t,s]);const u=a?te(c):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(h=>e.jsx(Ne,{node:h,expanded:l,toggle:d,selectedId:s,onSelect:n,tintScheme:u,linkedIds:i},h.row.id))})}function ze(t,s){const n=[],a=(c,i)=>{for(const l of c){if(l.row.id===s)return n.push(...i),!0;if(a(l.children,[...i,l.row.id]))return!0}return!1};return a(t,[]),n}function Ne({node:t,expanded:s,toggle:n,selectedId:a,onSelect:c,tintScheme:i,linkedIds:l}){const o=t.children.length>0,d=s.get(t.row.id)!==!1,u=a===t.row.id,h=i?ne(ge(t.row.status,i)):void 0,p=l?.has(t.row.id);return e.jsxs("li",{className:`node chunk-node ${u?"selected":""}`,children:[e.jsxs("div",{className:"row chunk-row",style:h,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":d?"collapse":"expand",disabled:!o,onClick:j=>{j.stopPropagation(),o&&n(t.row.id)},children:o?d?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"chunk-link",onClick:()=>c(t),title:t.row.title,children:[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}),p&&e.jsx("span",{className:"chunk-link-badge",title:"Has cross-reference links","aria-label":"linked",children:"🔗"})]})]}),o&&d&&e.jsx("ul",{className:"children chunk-children",children:t.children.map(j=>e.jsx(Ne,{node:j,expanded:s,toggle:n,selectedId:a,onSelect:c,tintScheme:i,linkedIds:l},j.row.id))})]})}function Ve({chunk:t,scope:s,browse:n,onClose:a,onSaved:c}){const i=ee(),[l,o]=r.useState(t.title),[d,u]=r.useState(t.type??""),[h,p]=r.useState(t.status??""),[j,E]=r.useState(t.body??""),[k,C]=r.useState(t.fields?JSON.stringify(t.fields,null,2):""),[P,D]=r.useState(!1),[T,M]=r.useState(null),[O,_]=r.useState(!1),G=async b=>{b.preventDefault(),M(null),_(!1);const g={body:j,title:l,type:d,status:h};if(k.trim()!=="")try{g.fields=JSON.parse(k)}catch{M("fields must be valid JSON");return}D(!0);try{const N=await i.documentUpdateChunk(t.id,t.revision,g,s,n);c(N),a()}catch(N){const I=N instanceof Error?N.message:String(N);M(I),_(/revision conflict/i.test(I)),D(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:a,children:e.jsxs("form",{className:"modal chunk-editor",onClick:b=>b.stopPropagation(),onSubmit:G,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"})]})]}),T&&e.jsxs("div",{className:"modal-err",children:[T,O&&" — 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:l,onChange:b=>o(b.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:b=>u(b.target.value)})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Status"}),e.jsx("input",{className:"path-modal-input",value:h,placeholder:"(none)",onChange:b=>p(b.target.value)})]})]}),e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:"Body (Markdown)"}),e.jsx("textarea",{className:"modal-textarea chunk-body-input",rows:12,value:j,onChange:b=>E(b.target.value)})]}),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:k,placeholder:"{}",onChange:b=>C(b.target.value)})]})]})})}function Je(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 Ze({documentId:t,rootChunkId:s,docType:n,docStatus:a,chunkStatuses:c,scope:i,browse:l,onClose:o,onSaved:d}){const u=ee(),[h,p]=r.useState(null),[j,E]=r.useState(!1),[k,C]=r.useState({}),[P,D]=r.useState(!1),[T,M]=r.useState(null),[O,_]=r.useState(null),G=r.useMemo(()=>{const f=new Set(Object.keys(Y));n&&f.add(`doc.${n}.${a||"draft"}`);for(const m of c)m&&f.add(`chunk.${m}`);return[...f].sort()},[n,a,c]);r.useEffect(()=>{let f=!1;return u.documentGetChunk(s,i,l).then(m=>{if(f)return;p(m);const R=m.fields??{};E(R.color_enabled===!0);const S=R.color_scheme??{};C({...Y,...S})}).catch(m=>!f&&_(m instanceof Error?m.message:String(m))),()=>{f=!0}},[u,s,i,l]);const b=(f,m)=>C(R=>({...R,[f]:m})),g=f=>({...f??{},color_enabled:j,color_scheme:k}),N=async()=>{if(h){D(!0),_(null),M(null);try{await u.documentUpdateChunk(s,h.revision,{fields:g(h.fields)},i,l),d(),o()}catch(f){_(f instanceof Error?f.message:String(f)),D(!1)}}},I=async()=>{if(!n){_("this document has no type to match");return}D(!0),_(null),M(null);try{const{documents:f}=await u.documentsSummary({underPath:"/"},i,l),m=(f??[]).filter(S=>S.type===n&&S.document_id!==t&&S.root_chunk_id);let R=0;for(const S of m)try{const U=await u.documentGetChunk(S.root_chunk_id,i,l);await u.documentUpdateChunk(S.root_chunk_id,U.revision,{fields:g(U.fields)},i,l),R++}catch{}M(`Applied to ${R} other ${n} document(s).`),d()}catch(f){_(f instanceof Error?f.message:String(f))}finally{D(!1)}},W=G.filter(f=>f.startsWith("doc.")),L=G.filter(f=>f.startsWith("chunk."));return e.jsx("div",{className:"modal-overlay",onClick:o,children:e.jsxs("div",{className:"modal scheme-editor",onClick:f=>f.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:o,disabled:P,children:"cancel"}),e.jsx("button",{type:"button",className:"primary",onClick:()=>{N()},disabled:P||!h,children:P?"saving…":"save"})]})]}),O&&e.jsx("div",{className:"modal-err",children:O}),h?e.jsxs(e.Fragment,{children:[e.jsxs("label",{className:"scheme-toggle",children:[e.jsx("input",{type:"checkbox",checked:j,onChange:f=>E(f.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(f=>e.jsx(de,{label:f.slice(4),value:k[f],onChange:m=>b(f,m)},f))]}),e.jsxs("div",{className:"scheme-group",children:[e.jsx("h4",{children:"Chunks (status)"}),L.map(f=>e.jsx(de,{label:f.slice(6),value:k[f],onChange:m=>b(f,m)},f))]}),e.jsxs("div",{className:"scheme-editor-foot",children:[e.jsxs("button",{type:"button",onClick:()=>{I()},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"]}),T&&e.jsx("span",{className:"scheme-status",children:T})]})]}):e.jsx("p",{className:"modal-context",children:"Loading…"})]})})}function de({label:t,value:s,onChange:n}){const a=Je(s);return e.jsxs("label",{className:"scheme-row",children:[e.jsx("input",{type:"color",value:a,onChange:c=>n(c.target.value)}),e.jsx("span",{className:"scheme-key",children:t}),e.jsx("code",{className:"scheme-hex",children:a})]})}let Qe=0;function we({code:t}){const s=r.useRef(null),[n,a]=r.useState({});return r.useEffect(()=>{let c=!1;return(async()=>{try{const i=(await import("mermaid")).default,l=s.current?.closest("[data-theme]")?.getAttribute("data-theme"),o=l?l==="dark":!0;i.initialize({startOnLoad:!1,theme:o?"dark":"default",securityLevel:"strict"});const{svg:d}=await i.render(`lc-mmd-${Qe++}`,t);c||a({svg:d})}catch{c||a({err:!0})}})(),()=>{c=!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…"})}const se=24;function Xe({edges:t,documentId:s,selectedId:n,onSelectChunk:a}){const[c,i]=r.useState(!1),[l,o]=r.useState(!1);if(t.length===0)return null;const d=n?t.filter(p=>p.from_id===n||p.to_id===n):[],u=l?t:t.slice(0,se),h=Ye(u);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((p,j)=>e.jsx(Ke,{edge:p,selectedId:n,documentId:s,onSelectChunk:a},`${p.from_id}-${p.to_id}-${p.kind}-${j}`))})]}),e.jsxs("div",{className:"doc-graph",children:[e.jsxs("button",{type:"button",className:"doc-graph-toggle",onClick:()=>i(p=>!p),children:[c?"▼":"▶"," relationship graph (",t.length,")"]}),c&&e.jsxs(e.Fragment,{children:[e.jsx(we,{code:h}),t.length>se&&e.jsx("button",{type:"button",className:"doc-graph-more",onClick:()=>o(p=>!p),children:l?`show top ${se}`:`show all ${t.length}`})]})]})]})}function Ke({edge:t,selectedId:s,documentId:n,onSelectChunk:a}){const c=t.from_id===s,i=c?t.to_id:t.from_id,l=(c?t.to_title:t.from_title)||i.slice(0,8),o=c?t.to_document_id:t.from_document_id,d=!o||o===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:c?"→":"←"}),d?e.jsx("button",{type:"button",className:"doc-ref-target",onClick:()=>a(i),children:l}):e.jsxs("span",{className:"doc-ref-target external",title:"In another document",children:[l," ↗"]})]})}function Ye(t){const s=["graph LR"];for(const n of t){const a=`${ue(n.from_id)}["${he(n.from_title,n.from_id)}"]`,c=`${ue(n.to_id)}["${he(n.to_title,n.to_id)}"]`,i=(n.kind||"link").replace(/[|"]/g," ");s.push(` ${a} -->|${i}| ${c}`)}return s.join(`
2
+ `)}function ue(t){return"n"+t.replace(/[^a-zA-Z0-9]/g,"")}function he(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 me({source:t}){return e.jsx("div",{className:"md",children:e.jsx(Fe,{remarkPlugins:[Ge],components:et,children:t})})}const et={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(we,{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 Se({documentId:t,scope:s,titleHint:n,browse:a,principal:c,renderAssistant:i}){const l=ee(),o=r.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),[d,u]=r.useState([]),[h,p]=r.useState(void 0),[j,E]=r.useState(null),[k,C]=r.useState("chunks"),[P,D]=r.useState(null),[T,M]=r.useState(null),[O,_]=r.useState(null),[G,b]=r.useState(!1),[g,N]=r.useState(0),[I,W]=r.useState(!1),[L,f]=r.useState(null),[m,R]=r.useState(!1),[S,U]=r.useState([]),q=r.useCallback(()=>N(x=>x+1),[]),z=r.useMemo(()=>We(d),[d]),V=r.useMemo(()=>d.find(v=>!v.parent_id)?.title||n||"document",[d,n]),Z=r.useMemo(()=>!!j&&!j.parent_id,[j]),J=r.useMemo(()=>L?.root_chunk_id??d.find(x=>!x.parent_id)?.id,[L,d]),y=r.useMemo(()=>[...new Set(d.map(x=>x.status).filter(x=>!!x))],[d]),w=!!L?.color_enabled,$=r.useMemo(()=>{const x=new Set;for(const v of S)x.add(v.from_id),x.add(v.to_id);return x},[S]),F=r.useMemo(()=>w?ne(ce(L?.type,L?.status,te(L?.color_scheme))):void 0,[w,L?.type,L?.status,L?.color_scheme]);r.useEffect(()=>{let x=!1;return b(!0),_(null),l.documentQueryChunks(t,s,o).then(v=>{if(x)return;const H=v.chunks??[];u(H),p(B=>B??H.find(A=>!A.parent_id)?.id)}).catch(v=>!x&&_(v instanceof Error?v.message:String(v))).finally(()=>!x&&b(!1)),()=>{x=!0}},[l,t,s,g,o]),r.useEffect(()=>{let x=!1;return l.documentGet(t,s,o).then(v=>!x&&f(v)).catch(()=>!x&&f(null)),()=>{x=!0}},[l,t,s,g,o]),r.useEffect(()=>{let x=!1;return l.documentGetEdges(t,s,o).then(v=>!x&&U(v.edges??[])).catch(()=>!x&&U([])),()=>{x=!0}},[l,t,s,g,o]),r.useEffect(()=>{p(void 0),E(null),D(null),C("chunks"),f(null),U([])},[t,s]),r.useEffect(()=>{if(!h){E(null);return}let x=!1;return l.documentGetChunk(h,s,o).then(v=>!x&&E(v)).catch(v=>!x&&_(v instanceof Error?v.message:String(v))),()=>{x=!0}},[l,h,s,g,o]),r.useEffect(()=>{if(k!=="markdown"||!h){D(null);return}const x=ve(z,h);if(!x)return;let v=!1;const H=[],B=(A,X)=>{H.push({node:A,depth:X}),A.children.forEach(K=>B(K,X+1))};return B(x,0),Promise.all(H.map(A=>l.documentGetChunk(A.node.row.id,s,o))).then(A=>{if(v)return;const X=H.map((K,Me)=>{const $e=Math.min(K.depth+1,6),oe=A[Me];return"#".repeat($e)+" "+K.node.row.title+(oe.body?`
5
4
 
6
- `+G.body:"")+`
5
+ `+oe.body:"")+`
7
6
  `}).join(`
8
- `);M(E)}).catch(k=>!y&&D(k instanceof Error?k.message:String(k))),()=>{y=!0}},[l,b,h,$,t,g,d]);const B=i.useCallback(x=>N(x.row.id),[]),q=i.useCallback(async()=>{try{const x=await l.documentExportMd(s,t,!0,d),y=new Blob([x.markdown],{type:"text/markdown"}),_=URL.createObjectURL(y),p=document.createElement("a");p.href=_,p.download=(x.title||"document").replace(/[^\w.-]+/g,"_")+".md",p.click(),URL.revokeObjectURL(_)}catch(x){D(x instanceof Error?x.message:String(x))}},[l,s,t,d]);return e.jsxs("div",{className:"doc-viewer",children:[e.jsxs("div",{className:"doc-toolbar",children:[e.jsx("strong",{className:"doc-title",title:T,children:T}),e.jsxs("div",{className:"doc-toolbar-actions",children:[e.jsx("button",{type:"button",onClick:()=>{q()},title:"Download the whole document as Markdown (.md)",children:"↓ .md"}),o&&e.jsx("button",{type:"button",className:O?"active":"",onClick:()=>F(x=>!x),title:"Document Assistant — instruct an agent to restructure/import/link chunks",children:"assistant"}),e.jsx("button",{type:"button",onClick:m,title:"Reload",children:"↻"})]})]}),P&&e.jsx("div",{className:"paths-err",children:P}),e.jsxs("div",{className:"doc-split",children:[e.jsx("div",{className:"doc-chunktree",children:W&&c.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(me,{tree:$,selectedId:h,onSelect:B})}),e.jsx("div",{className:"doc-content",children:j?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-content-head",children:[e.jsx("h3",{children:j.title||"(untitled)"}),e.jsxs("div",{className:"doc-content-meta",children:[j.type&&e.jsx("span",{className:"chunk-badge",children:j.type}),j.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:j.status}),e.jsxs("span",{className:"chunk-rev",children:["rev ",j.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:()=>w("chunks"),title:"This chunk's own content",children:"chunk"}),e.jsx("button",{type:"button",className:b==="markdown"?"active":"",onClick:()=>w("markdown"),title:A?"Whole document as Markdown":"This chunk + its sub-chunks as Markdown",children:"markdown"})]}),e.jsx("button",{type:"button",onClick:()=>C(j),children:"edit"})]})]}),e.jsx("div",{className:"doc-content-body",children:b==="markdown"?L!==null?e.jsx(Z,{source:L}):e.jsx("p",{className:"doc-empty-body",children:"Assembling Markdown…"}):j.body?e.jsx(Z,{source:j.body}):e.jsx("p",{className:"doc-empty-body",children:"(empty chunk)"})})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a chunk on the left."})})})]}),O&&o&&e.jsx("div",{className:"doc-assistant",children:o({documentId:s,scope:t,subject:r?.subject,onChanged:m})}),R&&e.jsx(fe,{chunk:R,scope:t,browse:d,onClose:()=>C(null),onSaved:m})]})}function ke(s){const t={name:"",fullPath:"",kind:"directory",explicit:!0,children:[]},a=new Map([["",t]]),n=o=>{const l=a.get(o);if(l)return l;const d=o.lastIndexOf("/"),c=d<=0?"":o.slice(0,d),u=o.slice(d+1),h=n(c),N={name:u,fullPath:o,kind:"directory",explicit:!1,children:[]};return h.children.push(N),a.set(o,N),N};for(const o of s){const l=n(o.full_path);l.kind=o.kind,l.explicit=!0,l.resourceRef=o.resource_ref}const r=o=>{o.sort((l,d)=>{const c=l.kind==="directory",u=d.kind==="directory";return c!==u?c?-1:1:l.name.localeCompare(d.name)}),o.forEach(l=>r(l.children))};return r(t.children),t.children}function J(s){const t=s.lastIndexOf("/");return t<=0?"":s.slice(0,t)}function ge(s){const t=[],a=n=>{if(n.kind==="document"){const r=n.resourceRef;r?.document_id&&t.push(r.document_id)}n.children.forEach(a)};return a(s),t}const ye={directory:"📁",document:"📄",volume_mount:"💾",memory_entry:"🧠"};function ve({tree:s,selectedPath:t,onSelect:a}){const[n,r]=i.useState(()=>new Map),o=i.useCallback(l=>{r(d=>{const c=new Map(d);return c.set(l,d.get(l)===!1),c})},[]);return i.useEffect(()=>{t&&r(l=>{let d=!1;const c=new Map(l);let u=J(t);for(;u!=="";)c.get(u)===!1&&(c.set(u,!0),d=!0),u=J(u);return d?c:l})},[t]),s.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:s.map(l=>e.jsx(le,{node:l,expanded:n,toggle:o,selectedPath:t,onSelect:a},l.fullPath))})}function le({node:s,expanded:t,toggle:a,selectedPath:n,onSelect:r}){const o=s.children.length>0,l=t.get(s.fullPath)!==!1,d=n===s.fullPath;return e.jsxs("li",{className:`node path-node kind-${s.kind} ${d?"selected":""}`,children:[e.jsxs("div",{className:"row path-row",children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":l?"collapse":"expand",disabled:!o,onClick:c=>{c.stopPropagation(),o&&a(s.fullPath)},children:o?l?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"path-link",onClick:()=>r(s),title:s.fullPath,children:[e.jsx("span",{className:"path-kind-icon","aria-hidden":!0,children:ye[s.kind]??"•"}),e.jsx("span",{className:"path-name",children:s.name}),!s.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:s.children.map(c=>e.jsx(le,{node:c,expanded:t,toggle:a,selectedPath:n,onSelect:r},c.fullPath))})]})}const Q=/^[A-Za-z0-9._-]+$/;function X(s,t){return`${s}/${t}`}function re(s,t){for(const a of s){if(a.fullPath===t)return a;const n=re(a.children,t);if(n)return n}}function Ne(s){const{theme:t,defaultScope:a,path:n,onPathChange:r,browse:o,principal:l,renderAssistant:d,onError:c}=s,u=ee(s);return e.jsx(te,{theme:t,dataLayer:u,children:e.jsx(we,{defaultScope:a,path:n,onPathChange:r,browse:o,principal:l,renderAssistant:d,onError:c})})}function we({defaultScope:s,path:t,onPathChange:a,browse:n,principal:r,renderAssistant:o,onError:l}){const d=z(),[c,u]=i.useState(s??"user"),[h,N]=i.useState([]),[j,S]=i.useState(void 0),[b,w]=i.useState(null),[L,M]=i.useState(!1),[R,C]=i.useState(null),P=t!==void 0?t:j,D=i.useRef(a);i.useEffect(()=>{D.current=a},[a]);const W=i.useRef(l);i.useEffect(()=>{W.current=l},[l]);const f=i.useCallback(p=>{D.current?.(p),S(p)},[]),g=i.useMemo(()=>n&&(n.scopeId||n.tenant)?{scopeId:n.scopeId,tenant:n.tenant}:void 0,[n?.scopeId,n?.tenant]),v=i.useCallback(async()=>{M(!0),w(null);try{const p=await d.pathLs("/",c,!0,g);N(p.entries??[])}catch(p){w(p instanceof Error?p.message:String(p)),N([]),W.current?.(p)}finally{M(!1)}},[d,c,g]),O=i.useRef(!0);i.useEffect(()=>{O.current?O.current=!1:f(void 0),v()},[v,f]);const F=i.useMemo(()=>ke(h),[h]),m=i.useMemo(()=>P?re(F,P):void 0,[F,P]),$=i.useMemo(()=>m?m.kind==="directory"?m.fullPath:J(m.fullPath):"",[m]),T=$||"/",A=c!=="tenant",B=i.useCallback(()=>{C({title:`New folder in ${T}`,fields:[{key:"name",label:"Folder name",placeholder:"e.g. launches"}],submitLabel:"Create",onSubmit:async p=>{const k=p.name.trim();if(!Q.test(k))throw new Error("name may contain only [A-Za-z0-9._-]");const E=X($,k);await d.pathMkdir(E,c,g),await v(),f(E)}})},[d,$,T,c,g,v,f]),q=i.useCallback(()=>{C({title:`New document in ${T}`,fields:[{key:"name",label:"Path name",placeholder:"e.g. launch-plan"},{key:"title",label:"Title",placeholder:"e.g. Launch Plan"}],submitLabel:"Create",onSubmit:async p=>{const k=p.name.trim();if(!Q.test(k))throw new Error("name may contain only [A-Za-z0-9._-]");const E=p.title.trim()||k,I=X($,k);await d.documentCreate(E,I,c,g),await v(),f(I)}})},[d,$,T,c,g,v,f]),x=i.useCallback(()=>{m&&C({title:`Rename / move ${m.fullPath}`,fields:[{key:"to",label:"New path",value:m.fullPath}],submitLabel:"Move",onSubmit:async p=>{const k=p.to.trim();if(!k.startsWith("/"))throw new Error("path must start with /");await d.pathMv(m.fullPath,k,c,g),await v(),f(k)}})},[d,m,c,g,v,f]),y=i.useCallback(()=>{if(!m)return;if(m.kind==="document"){const I=m.resourceRef?.document_id;C({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(!I)throw new Error("this document dirent has no document_id");await d.documentDelete(I,c,g),await v(),f(void 0)}});return}const p=c==="tenant"?[]:ge(m),k=m.children.length;C({title:`Delete branch ${m.fullPath}`,message:k===0?"This removes the (empty) folder.":"This removes the folder and everything under it"+(p.length>0?` — including ${p.length} document(s), whose chunks are deleted.`:"."),fields:[],submitLabel:"Delete",danger:!0,onSubmit:async()=>{for(const E of p)await d.documentDelete(E,c,g);await d.pathRm(m.fullPath,c,!0,g),await v(),f(void 0)}})},[d,m,c,g,v,f]),_=m&&(m.kind==="directory"||m.kind==="document");return e.jsxs(e.Fragment,{children:[e.jsxs(ue,{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:c,onChange:p=>u(p.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:B,title:`New folder in ${T}`,children:"+ folder"}),e.jsx("button",{type:"button",onClick:q,disabled:!A,title:A?`New document in ${T}`:"Documents are agent/user scope only",children:"+ document"}),e.jsx("button",{type:"button",onClick:()=>{v()},title:"Reload the tree",children:"↻"})]})]}),e.jsxs("p",{className:"paths-context",children:["new items → ",e.jsx("code",{children:T}),g?.scopeId&&e.jsxs(e.Fragment,{children:[" · ","subject ",e.jsx("code",{children:g.scopeId})]}),g?.tenant&&e.jsxs(e.Fragment,{children:[" · ","tenant ",e.jsx("code",{children:g.tenant})]})]}),b&&e.jsx("div",{className:"paths-err",children:b}),L&&h.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(ve,{tree:F,selectedPath:P,onSelect:p=>f(p.fullPath)})]}),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:x,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:y,children:"delete"})]})]}),e.jsx(ae,{documentId:m.resourceRef?.document_id??"",scope:c==="agent"?"agent":"user",titleHint:m.name,browse:g,principal:r,renderAssistant:o})]}):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:c}),e.jsx("dt",{children:"stored"}),e.jsx("dd",{children:m.explicit?"yes":"implicit (no entry)"})]}),_?e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:x,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:y,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."})})})]}),R&&e.jsx(Ce,{state:R,onClose:()=>C(null)})]})}function Ce({state:s,onClose:t}){const[a,n]=i.useState(()=>Object.fromEntries(s.fields.map(u=>[u.key,u.value??""]))),[r,o]=i.useState(!1),[l,d]=i.useState(null),c=async u=>{u.preventDefault(),o(!0),d(null);try{await s.onSubmit(a),t()}catch(h){d(h instanceof Error?h.message:String(h)),o(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:t,children:e.jsxs("form",{className:"modal",onClick:u=>u.stopPropagation(),onSubmit:c,children:[e.jsx("h3",{children:s.title}),s.message&&e.jsx("p",{className:"modal-context",children:s.message}),s.fields.map((u,h)=>e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:u.label}),e.jsx("input",{className:"path-modal-input",value:a[u.key],placeholder:u.placeholder,autoFocus:h===0,onChange:N=>n(j=>({...j,[u.key]:N.target.value}))})]},u.key)),l&&e.jsx("div",{className:"modal-err",children:l}),e.jsxs("div",{className:"modal-buttons",children:[e.jsx("button",{type:"button",onClick:t,disabled:r,children:"cancel"}),e.jsx("button",{type:"submit",className:s.danger?"danger":"primary",disabled:r,children:r?"working…":s.submitLabel})]})]})})}function Se(s){const{documentId:t,scope:a,titleHint:n,browse:r,theme:o,principal:l,renderAssistant:d}=s,c=ee(s);return e.jsx(te,{theme:o,dataLayer:c,children:e.jsx(ae,{documentId:t,scope:a,titleHint:n,browse:r,principal:l,renderAssistant:d})})}exports.DocumentViewer=Se;exports.PathExplorer=Ne;exports.createLoomcycleClient=K;exports.dataLayerFromClient=V;
7
+ `);D(X)}).catch(A=>!v&&_(A instanceof Error?A.message:String(A))),()=>{v=!0}},[l,k,h,z,s,g,o]);const Q=r.useCallback(x=>p(x.row.id),[]),Ee=r.useCallback(async()=>{try{const x=await l.documentExportMd(t,s,!0,o),v=new Blob([x.markdown],{type:"text/markdown"}),H=URL.createObjectURL(v),B=document.createElement("a");B.href=H,B.download=(x.title||"document").replace(/[^\w.-]+/g,"_")+".md",B.click(),URL.revokeObjectURL(H)}catch(x){_(x instanceof Error?x.message:String(x))}},[l,t,s,o]);return e.jsxs("div",{className:"doc-viewer",children:[e.jsxs("div",{className:"doc-toolbar",style:F,children:[e.jsx("strong",{className:"doc-title",title:V,children:V}),e.jsxs("div",{className:"doc-toolbar-actions",children:[e.jsx("button",{type:"button",className:w?"active":"",onClick:()=>R(!0),disabled:!J,title:"Colors — tint chunk tiles + the Path tree by type/status",children:"colors"}),e.jsx("button",{type:"button",onClick:()=>{Ee()},title:"Download the whole document as Markdown (.md)",children:"↓ .md"}),i&&e.jsx("button",{type:"button",className:I?"active":"",onClick:()=>W(x=>!x),title:"Document Assistant — instruct an agent to restructure/import/link chunks",children:"assistant"}),e.jsx("button",{type:"button",onClick:q,title:"Reload",children:"↻"})]})]}),O&&e.jsx("div",{className:"paths-err",children:O}),e.jsxs("div",{className:"doc-split",children:[e.jsx("div",{className:"doc-chunktree",children:G&&d.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(qe,{tree:z,selectedId:h,onSelect:Q,colorEnabled:w,scheme:L?.color_scheme,linkedIds:$})}),e.jsx("div",{className:"doc-content",children:j?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"doc-content-head",children:[e.jsx("h3",{children:j.title||"(untitled)"}),e.jsxs("div",{className:"doc-content-meta",children:[j.type&&e.jsx("span",{className:"chunk-badge",children:j.type}),j.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:j.status}),e.jsxs("span",{className:"chunk-rev",children:["rev ",j.revision]})]}),e.jsxs("div",{className:"doc-content-controls",children:[e.jsxs("div",{className:"doc-mode-toggle",children:[e.jsx("button",{type:"button",className:k==="chunks"?"active":"",onClick:()=>C("chunks"),title:"This chunk's own content",children:"chunk"}),e.jsx("button",{type:"button",className:k==="markdown"?"active":"",onClick:()=>C("markdown"),title:Z?"Whole document as Markdown":"This chunk + its sub-chunks as Markdown",children:"markdown"})]}),e.jsx("button",{type:"button",onClick:()=>M(j),children:"edit"})]})]}),e.jsx("div",{className:"doc-content-body",children:k==="markdown"?P!==null?e.jsx(me,{source:P}):e.jsx("p",{className:"doc-empty-body",children:"Assembling Markdown…"}):j.body?e.jsx(me,{source:j.body}):e.jsx("p",{className:"doc-empty-body",children:"(empty chunk)"})}),e.jsx(Xe,{edges:S,documentId:t,selectedId:h,onSelectChunk:p})]}):e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Select a chunk on the left."})})})]}),I&&i&&e.jsx("div",{className:"doc-assistant",children:i({documentId:t,scope:s,subject:c?.subject,onChanged:q})}),T&&e.jsx(Ve,{chunk:T,scope:s,browse:o,onClose:()=>M(null),onSaved:q}),m&&J&&e.jsx(Ze,{documentId:t,rootChunkId:J,docType:L?.type,docStatus:L?.status,chunkStatuses:y,scope:s,browse:o,onClose:()=>R(!1),onSaved:q})]})}function tt(t){const s={name:"",fullPath:"",kind:"directory",explicit:!0,children:[]},n=new Map([["",s]]),a=i=>{const l=n.get(i);if(l)return l;const o=i.lastIndexOf("/"),d=o<=0?"":i.slice(0,o),u=i.slice(o+1),h=a(d),p={name:u,fullPath:i,kind:"directory",explicit:!1,children:[]};return h.children.push(p),n.set(i,p),p};for(const i of t){const l=a(i.full_path);l.kind=i.kind,l.explicit=!0,l.resourceRef=i.resource_ref}const c=i=>{i.sort((l,o)=>{const d=l.kind==="directory",u=o.kind==="directory";return d!==u?d?-1:1:l.name.localeCompare(o.name)}),i.forEach(l=>c(l.children))};return c(s.children),s.children}function re(t){const s=t.lastIndexOf("/");return s<=0?"":t.slice(0,s)}function nt(t){const s=[],n=a=>{if(a.kind==="document"){const c=a.resourceRef;c?.document_id&&s.push(c.document_id)}a.children.forEach(n)};return n(t),s}const st={directory:"📁",document:"📄",volume_mount:"💾",memory_entry:"🧠"};function at({tree:t,selectedPath:s,onSelect:n,summaries:a}){const[c,i]=r.useState(()=>new Map),l=r.useCallback(o=>{i(d=>{const u=new Map(d);return u.set(o,d.get(o)===!1),u})},[]);return r.useEffect(()=>{s&&i(o=>{let d=!1;const u=new Map(o);let h=re(s);for(;h!=="";)u.get(h)===!1&&(u.set(h,!0),d=!0),h=re(h);return d?u:o})},[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(o=>e.jsx(Ce,{node:o,expanded:c,toggle:l,selectedPath:s,onSelect:n,summaries:a},o.fullPath))})}function rt(t,s){if(t.kind!=="document"||!s)return;const n=t.resourceRef;return n?.document_id?s.get(n.document_id):void 0}function Ce({node:t,expanded:s,toggle:n,selectedPath:a,onSelect:c,summaries:i}){const l=t.children.length>0,o=s.get(t.fullPath)!==!1,d=a===t.fullPath,u=rt(t,i),h=u&&u.color_enabled?ne(ce(u.type,u.status,te(u.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:h,children:[e.jsx("button",{type:"button",className:"tree-caret","aria-label":o?"collapse":"expand",disabled:!l,onClick:p=>{p.stopPropagation(),l&&n(t.fullPath)},children:l?o?"▼":"▶":"·"}),e.jsxs("button",{type:"button",className:"path-link",onClick:()=>c(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}),u?.type&&e.jsx("span",{className:"chunk-badge",children:u.type}),u?.status&&e.jsx("span",{className:"chunk-badge chunk-status",children:u.status}),!t.explicit&&e.jsx("span",{className:"path-implicit",title:"Implicit directory (no stored entry)",children:"implicit"})]})]}),l&&o&&e.jsx("ul",{className:"children path-children",children:t.children.map(p=>e.jsx(Ce,{node:p,expanded:s,toggle:n,selectedPath:a,onSelect:c,summaries:i},p.fullPath))})]})}const fe=/^[A-Za-z0-9._-]+$/;function pe(t,s){return`${t}/${s}`}function _e(t,s){for(const n of t){if(n.fullPath===s)return n;const a=_e(n.children,s);if(a)return a}}function ct(t){const{theme:s,defaultScope:n,path:a,onPathChange:c,browse:i,principal:l,renderAssistant:o,onError:d}=t,u=be(t);return e.jsx(ke,{theme:s,dataLayer:u,children:e.jsx(ot,{defaultScope:n,path:a,onPathChange:c,browse:i,principal:l,renderAssistant:o,onError:d})})}function ot({defaultScope:t,path:s,onPathChange:n,browse:a,principal:c,renderAssistant:i,onError:l}){const o=ee(),[d,u]=r.useState(t??"user"),[h,p]=r.useState([]),[j,E]=r.useState(void 0),[k,C]=r.useState(null),[P,D]=r.useState(!1),[T,M]=r.useState(null),O=s!==void 0?s:j,_=r.useRef(n);r.useEffect(()=>{_.current=n},[n]);const G=r.useRef(l);r.useEffect(()=>{G.current=l},[l]);const b=r.useCallback(y=>{_.current?.(y),E(y)},[]),g=r.useMemo(()=>a&&(a.scopeId||a.tenant)?{scopeId:a.scopeId,tenant:a.tenant}:void 0,[a?.scopeId,a?.tenant]),N=r.useCallback(async()=>{D(!0),C(null);try{const y=await o.pathLs("/",d,!0,g);p(y.entries??[])}catch(y){C(y instanceof Error?y.message:String(y)),p([]),G.current?.(y)}finally{D(!1)}},[o,d,g]),I=r.useRef(!0);r.useEffect(()=>{I.current?I.current=!1:b(void 0),N()},[N,b]);const W=r.useMemo(()=>tt(h),[h]),[L,f]=r.useState(()=>new Map);r.useEffect(()=>{if(d==="tenant"){f(new Map);return}const y=h.filter($=>$.kind==="document").map($=>$.resource_ref?.document_id).filter($=>!!$);if(y.length===0){f(new Map);return}let w=!1;return o.documentsSummary({documentIds:y},d,g).then($=>{if(w)return;const F=new Map;for(const Q of $.documents??[])F.set(Q.document_id,Q);f(F)}).catch(()=>{w||f(new Map)}),()=>{w=!0}},[o,h,d,g]);const m=r.useMemo(()=>O?_e(W,O):void 0,[W,O]),R=r.useMemo(()=>m?m.kind==="directory"?m.fullPath:re(m.fullPath):"",[m]),S=R||"/",U=d!=="tenant",q=r.useCallback(()=>{M({title:`New folder in ${S}`,fields:[{key:"name",label:"Folder name",placeholder:"e.g. launches"}],submitLabel:"Create",onSubmit:async y=>{const w=y.name.trim();if(!fe.test(w))throw new Error("name may contain only [A-Za-z0-9._-]");const $=pe(R,w);await o.pathMkdir($,d,g),await N(),b($)}})},[o,R,S,d,g,N,b]),z=r.useCallback(()=>{M({title:`New document in ${S}`,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 w=y.name.trim();if(!fe.test(w))throw new Error("name may contain only [A-Za-z0-9._-]");const $=y.title.trim()||w,F=pe(R,w);await o.documentCreate($,F,d,g),await N(),b(F)}})},[o,R,S,d,g,N,b]),V=r.useCallback(()=>{m&&M({title:`Rename / move ${m.fullPath}`,fields:[{key:"to",label:"New path",value:m.fullPath}],submitLabel:"Move",onSubmit:async y=>{const w=y.to.trim();if(!w.startsWith("/"))throw new Error("path must start with /");await o.pathMv(m.fullPath,w,d,g),await N(),b(w)}})},[o,m,d,g,N,b]),Z=r.useCallback(()=>{if(!m)return;if(m.kind==="document"){const F=m.resourceRef?.document_id;M({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(!F)throw new Error("this document dirent has no document_id");await o.documentDelete(F,d,g),await N(),b(void 0)}});return}const y=d==="tenant"?[]:nt(m),w=m.children.length;M({title:`Delete branch ${m.fullPath}`,message:w===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 $ of y)await o.documentDelete($,d,g);await o.pathRm(m.fullPath,d,!0,g),await N(),b(void 0)}})},[o,m,d,g,N,b]),J=m&&(m.kind==="directory"||m.kind==="document");return e.jsxs(e.Fragment,{children:[e.jsxs(He,{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=>u(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:q,title:`New folder in ${S}`,children:"+ folder"}),e.jsx("button",{type:"button",onClick:z,disabled:!U,title:U?`New document in ${S}`:"Documents are agent/user scope only",children:"+ document"}),e.jsx("button",{type:"button",onClick:()=>{N()},title:"Reload the tree",children:"↻"})]})]}),e.jsxs("p",{className:"paths-context",children:["new items → ",e.jsx("code",{children:S}),g?.scopeId&&e.jsxs(e.Fragment,{children:[" · ","subject ",e.jsx("code",{children:g.scopeId})]}),g?.tenant&&e.jsxs(e.Fragment,{children:[" · ","tenant ",e.jsx("code",{children:g.tenant})]})]}),k&&e.jsx("div",{className:"paths-err",children:k}),P&&h.length===0?e.jsx("div",{className:"empty",children:e.jsx("p",{children:"Loading…"})}):e.jsx(at,{tree:W,selectedPath:O,onSelect:y=>b(y.fullPath),summaries:L})]}),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:V,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:Z,children:"delete"})]})]}),e.jsx(Se,{documentId:m.resourceRef?.document_id??"",scope:d==="agent"?"agent":"user",titleHint:m.name,browse:g,principal:c,renderAssistant:i})]}):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)"})]}),J?e.jsxs("div",{className:"paths-detail-actions",children:[e.jsx("button",{type:"button",onClick:V,children:"rename / move"}),e.jsx("button",{type:"button",className:"danger",onClick:Z,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."})})})]}),T&&e.jsx(lt,{state:T,onClose:()=>M(null)})]})}function lt({state:t,onClose:s}){const[n,a]=r.useState(()=>Object.fromEntries(t.fields.map(u=>[u.key,u.value??""]))),[c,i]=r.useState(!1),[l,o]=r.useState(null),d=async u=>{u.preventDefault(),i(!0),o(null);try{await t.onSubmit(n),s()}catch(h){o(h instanceof Error?h.message:String(h)),i(!1)}};return e.jsx("div",{className:"modal-overlay",onClick:s,children:e.jsxs("form",{className:"modal",onClick:u=>u.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:c,children:"cancel"}),e.jsx("button",{type:"submit",className:t.danger?"danger":"primary",disabled:c,children:c?"working…":t.submitLabel})]})]}),t.message&&e.jsx("p",{className:"modal-context",children:t.message}),t.fields.map((u,h)=>e.jsxs("label",{className:"path-field",children:[e.jsx("span",{children:u.label}),e.jsx("input",{className:"path-modal-input",value:n[u.key],placeholder:u.placeholder,autoFocus:h===0,onChange:p=>a(j=>({...j,[u.key]:p.target.value}))})]},u.key)),l&&e.jsx("div",{className:"modal-err",children:l})]})})}function it(t){const{documentId:s,scope:n,titleHint:a,browse:c,theme:i,principal:l,renderAssistant:o}=t,d=be(t);return e.jsx(ke,{theme:i,dataLayer:d,children:e.jsx(Se,{documentId:s,scope:n,titleHint:a,browse:c,principal:l,renderAssistant:o})})}exports.DEFAULT_SCHEME=Y;exports.DocumentViewer=it;exports.PathExplorer=ct;exports.chunkColor=ge;exports.createLoomcycleClient=je;exports.dataLayerFromClient=ae;exports.docColor=ce;exports.effectiveScheme=te;exports.hexToTint=ye;exports.tintStyle=ne;
9
8
  //# sourceMappingURL=index.cjs.map