@loomcycle/explorer 0.1.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.js ADDED
@@ -0,0 +1,1023 @@
1
+ import { jsx as t, jsxs as d, Fragment as G } from "react/jsx-runtime";
2
+ import { createContext as pe, useContext as fe, useMemo as U, useState as f, useRef as H, useEffect as j, useCallback as S } from "react";
3
+ import { LoomcycleClient as be } from "@loomcycle/client";
4
+ function K(a) {
5
+ return {
6
+ pathLs: (e, r, n, s) => a.path(
7
+ { op: "ls", path: e, scope: r, recursive: n },
8
+ s
9
+ ),
10
+ pathMkdir: (e, r, n) => a.path({ op: "mkdir", path: e, scope: r }, n),
11
+ pathMv: (e, r, n, s) => a.path({ op: "mv", path: e, to: r, scope: n }, s),
12
+ pathRm: (e, r, n, s) => a.path({ op: "rm", path: e, scope: r, recursive: n }, s),
13
+ documentCreate: (e, r, n, s) => a.document(
14
+ { op: "create_document", title: e, path: r, scope: n },
15
+ s
16
+ ),
17
+ documentDelete: (e, r, n) => a.document({ op: "delete_document", id: e, scope: r }, n),
18
+ documentQueryChunks: (e, r, n) => a.document(
19
+ { op: "query_chunks", document_id: e, scope: r, limit: 1e3 },
20
+ n
21
+ ),
22
+ documentGetChunk: (e, r, n) => a.document({ op: "get_chunk", id: e, scope: r }, n),
23
+ documentUpdateChunk: (e, r, n, s, c) => a.document(
24
+ // `patch.fields` is unknown but DocumentToolInput.fields is a typed
25
+ // Record; the cast reconciles the spread (the server accepts arbitrary
26
+ // JSON there). Op order + arg names mirror the wire contract exactly.
27
+ {
28
+ op: "update_chunk",
29
+ id: e,
30
+ revision: r,
31
+ scope: s,
32
+ ...n
33
+ },
34
+ c
35
+ ),
36
+ documentExportMd: (e, r, n, s) => a.document(
37
+ {
38
+ op: "export_md",
39
+ document_id: e,
40
+ scope: r,
41
+ include_metadata: n
42
+ },
43
+ s
44
+ )
45
+ };
46
+ }
47
+ const re = pe(null);
48
+ function ge({
49
+ value: a,
50
+ children: e
51
+ }) {
52
+ return /* @__PURE__ */ t(re.Provider, { value: a, children: e });
53
+ }
54
+ function X() {
55
+ const a = fe(re);
56
+ if (!a)
57
+ throw new Error(
58
+ "useExplorerData must be used within <PathExplorer> or <DocumentViewer> (no ExplorerDataLayer in context)"
59
+ );
60
+ return a;
61
+ }
62
+ function ke(a) {
63
+ const e = a.fetch;
64
+ return new be({
65
+ baseUrl: a.baseUrl,
66
+ authToken: a.token || void 0,
67
+ fetch: (r, n) => e ? e(r, n) : fetch(r, n)
68
+ });
69
+ }
70
+ function le(a) {
71
+ const { dataLayer: e, client: r, connection: n } = a;
72
+ return U(() => e || (r ? K(r) : n ? K(ke(n)) : null), [e, r, n?.baseUrl, n?.token, n?.fetch]);
73
+ }
74
+ function se({
75
+ theme: a,
76
+ dataLayer: e,
77
+ children: r
78
+ }) {
79
+ const n = a ? { "data-theme": a } : {};
80
+ return e ? /* @__PURE__ */ t("div", { className: "loomcycle-explorer", ...n, children: /* @__PURE__ */ t(ge, { value: e, children: r }) }) : /* @__PURE__ */ t("div", { className: "loomcycle-explorer", ...n, children: /* @__PURE__ */ d("div", { className: "error-banner", children: [
81
+ "@loomcycle/explorer: provide a ",
82
+ /* @__PURE__ */ t("code", { children: "connection" }),
83
+ ", ",
84
+ /* @__PURE__ */ t("code", { children: "client" }),
85
+ ", or",
86
+ " ",
87
+ /* @__PURE__ */ t("code", { children: "dataLayer" }),
88
+ " prop."
89
+ ] }) });
90
+ }
91
+ const ee = 6;
92
+ function ye({
93
+ children: a,
94
+ defaultLeftWidth: e = 380,
95
+ minLeftWidth: r = 180,
96
+ minRightWidth: n = 240,
97
+ storageKey: s,
98
+ className: c
99
+ }) {
100
+ const [l, i] = f(() => {
101
+ if (s) {
102
+ const y = localStorage.getItem(s), E = y ? parseInt(y, 10) : NaN;
103
+ if (Number.isFinite(E) && E >= r) return E;
104
+ }
105
+ return e;
106
+ }), o = H(null), [u, h] = f(!1);
107
+ j(() => {
108
+ !s || u || localStorage.setItem(s, String(Math.round(l)));
109
+ }, [s, l, u]);
110
+ const x = S((y) => {
111
+ y.preventDefault(), h(!0), y.target.setPointerCapture(y.pointerId);
112
+ }, []), k = S(
113
+ (y) => {
114
+ if (!u || !o.current) return;
115
+ const E = o.current.getBoundingClientRect(), T = y.clientX - E.left, L = E.width - n - ee, P = Math.max(r, Math.min(T, L));
116
+ i(P);
117
+ },
118
+ [u, r, n]
119
+ ), M = S((y) => {
120
+ h(!1);
121
+ try {
122
+ y.target.releasePointerCapture(y.pointerId);
123
+ } catch {
124
+ }
125
+ }, []);
126
+ return /* @__PURE__ */ d(
127
+ "div",
128
+ {
129
+ ref: o,
130
+ className: `splitter ${c ?? ""} ${u ? "dragging" : ""}`,
131
+ style: {
132
+ gridTemplateColumns: `${l}px ${ee}px 1fr`
133
+ },
134
+ children: [
135
+ /* @__PURE__ */ t("div", { className: "splitter-pane splitter-left", children: a[0] }),
136
+ /* @__PURE__ */ t(
137
+ "div",
138
+ {
139
+ className: "splitter-handle",
140
+ role: "separator",
141
+ "aria-orientation": "vertical",
142
+ "aria-valuenow": Math.round(l),
143
+ onPointerDown: x,
144
+ onPointerMove: k,
145
+ onPointerUp: M,
146
+ onPointerCancel: M
147
+ }
148
+ ),
149
+ /* @__PURE__ */ t("div", { className: "splitter-pane splitter-right", children: a[1] })
150
+ ]
151
+ }
152
+ );
153
+ }
154
+ function ve(a) {
155
+ const e = /* @__PURE__ */ new Map();
156
+ a.forEach((s) => e.set(s.id, { row: s, children: [] }));
157
+ const r = [];
158
+ a.forEach((s) => {
159
+ const c = e.get(s.id);
160
+ s.parent_id && e.has(s.parent_id) ? e.get(s.parent_id).children.push(c) : r.push(c);
161
+ });
162
+ const n = (s) => {
163
+ s.sort((c, l) => c.row.position - l.row.position), s.forEach((c) => n(c.children));
164
+ };
165
+ return n(r), r;
166
+ }
167
+ function ce(a, e) {
168
+ for (const r of a) {
169
+ if (r.row.id === e) return r;
170
+ const n = ce(r.children, e);
171
+ if (n) return n;
172
+ }
173
+ }
174
+ function Ne({ tree: a, selectedId: e, onSelect: r }) {
175
+ const [n, s] = f(() => /* @__PURE__ */ new Map()), c = S((l) => {
176
+ s((i) => {
177
+ const o = new Map(i);
178
+ return o.set(l, i.get(l) === !1), o;
179
+ });
180
+ }, []);
181
+ return j(() => {
182
+ if (!e) return;
183
+ const l = we(a, e);
184
+ l.length !== 0 && s((i) => {
185
+ let o = !1;
186
+ const u = new Map(i);
187
+ for (const h of l)
188
+ u.get(h) === !1 && (u.set(h, !0), o = !0);
189
+ return o ? u : i;
190
+ });
191
+ }, [a, e]), a.length === 0 ? /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "No chunks." }) }) : /* @__PURE__ */ t("ul", { className: "tree chunk-tree", children: a.map((l) => /* @__PURE__ */ t(
192
+ oe,
193
+ {
194
+ node: l,
195
+ expanded: n,
196
+ toggle: c,
197
+ selectedId: e,
198
+ onSelect: r
199
+ },
200
+ l.row.id
201
+ )) });
202
+ }
203
+ function we(a, e) {
204
+ const r = [], n = (s, c) => {
205
+ for (const l of s) {
206
+ if (l.row.id === e)
207
+ return r.push(...c), !0;
208
+ if (n(l.children, [...c, l.row.id])) return !0;
209
+ }
210
+ return !1;
211
+ };
212
+ return n(a, []), r;
213
+ }
214
+ function oe({ node: a, expanded: e, toggle: r, selectedId: n, onSelect: s }) {
215
+ const c = a.children.length > 0, l = e.get(a.row.id) !== !1, i = n === a.row.id;
216
+ return /* @__PURE__ */ d("li", { className: `node chunk-node ${i ? "selected" : ""}`, children: [
217
+ /* @__PURE__ */ d("div", { className: "row chunk-row", children: [
218
+ /* @__PURE__ */ t(
219
+ "button",
220
+ {
221
+ type: "button",
222
+ className: "tree-caret",
223
+ "aria-label": l ? "collapse" : "expand",
224
+ disabled: !c,
225
+ onClick: (o) => {
226
+ o.stopPropagation(), c && r(a.row.id);
227
+ },
228
+ children: c ? l ? "▼" : "▶" : "·"
229
+ }
230
+ ),
231
+ /* @__PURE__ */ d(
232
+ "button",
233
+ {
234
+ type: "button",
235
+ className: "chunk-link",
236
+ onClick: () => s(a),
237
+ title: a.row.title,
238
+ children: [
239
+ /* @__PURE__ */ t("span", { className: "chunk-title", children: a.row.title || "(untitled)" }),
240
+ a.row.type && /* @__PURE__ */ t("span", { className: "chunk-badge", children: a.row.type }),
241
+ a.row.status && /* @__PURE__ */ t("span", { className: "chunk-badge chunk-status", children: a.row.status })
242
+ ]
243
+ }
244
+ )
245
+ ] }),
246
+ c && l && /* @__PURE__ */ t("ul", { className: "children chunk-children", children: a.children.map((o) => /* @__PURE__ */ t(
247
+ oe,
248
+ {
249
+ node: o,
250
+ expanded: e,
251
+ toggle: r,
252
+ selectedId: n,
253
+ onSelect: s
254
+ },
255
+ o.row.id
256
+ )) })
257
+ ] });
258
+ }
259
+ function Ce({ chunk: a, scope: e, browse: r, onClose: n, onSaved: s }) {
260
+ const c = X(), [l, i] = f(a.title), [o, u] = f(a.type ?? ""), [h, x] = f(a.status ?? ""), [k, M] = f(a.body ?? ""), [y, E] = f(
261
+ a.fields ? JSON.stringify(a.fields, null, 2) : ""
262
+ ), [T, L] = f(!1), [P, D] = f(null), [W, R] = f(!1);
263
+ return /* @__PURE__ */ t("div", { className: "modal-overlay", onClick: n, children: /* @__PURE__ */ d("form", { className: "modal chunk-editor", onClick: (b) => b.stopPropagation(), onSubmit: async (b) => {
264
+ b.preventDefault(), D(null), R(!1);
265
+ const N = {
266
+ body: k,
267
+ title: l,
268
+ type: o,
269
+ status: h
270
+ };
271
+ if (y.trim() !== "")
272
+ try {
273
+ N.fields = JSON.parse(y);
274
+ } catch {
275
+ D("fields must be valid JSON");
276
+ return;
277
+ }
278
+ L(!0);
279
+ try {
280
+ const C = await c.documentUpdateChunk(a.id, a.revision, N, e, r);
281
+ s(C), n();
282
+ } catch (C) {
283
+ const B = C instanceof Error ? C.message : String(C);
284
+ D(B), R(/revision conflict/i.test(B)), L(!1);
285
+ }
286
+ }, children: [
287
+ /* @__PURE__ */ t("h3", { children: "Edit chunk" }),
288
+ /* @__PURE__ */ d("label", { className: "path-field", children: [
289
+ /* @__PURE__ */ t("span", { children: "Title" }),
290
+ /* @__PURE__ */ t("input", { className: "path-modal-input", value: l, onChange: (b) => i(b.target.value) })
291
+ ] }),
292
+ /* @__PURE__ */ d("div", { className: "chunk-editor-row", children: [
293
+ /* @__PURE__ */ d("label", { className: "path-field", children: [
294
+ /* @__PURE__ */ t("span", { children: "Type" }),
295
+ /* @__PURE__ */ t(
296
+ "input",
297
+ {
298
+ className: "path-modal-input",
299
+ value: o,
300
+ placeholder: "(none)",
301
+ onChange: (b) => u(b.target.value)
302
+ }
303
+ )
304
+ ] }),
305
+ /* @__PURE__ */ d("label", { className: "path-field", children: [
306
+ /* @__PURE__ */ t("span", { children: "Status" }),
307
+ /* @__PURE__ */ t(
308
+ "input",
309
+ {
310
+ className: "path-modal-input",
311
+ value: h,
312
+ placeholder: "(none)",
313
+ onChange: (b) => x(b.target.value)
314
+ }
315
+ )
316
+ ] })
317
+ ] }),
318
+ /* @__PURE__ */ d("label", { className: "path-field", children: [
319
+ /* @__PURE__ */ t("span", { children: "Body (Markdown)" }),
320
+ /* @__PURE__ */ t(
321
+ "textarea",
322
+ {
323
+ className: "modal-textarea chunk-body-input",
324
+ rows: 12,
325
+ value: k,
326
+ onChange: (b) => M(b.target.value)
327
+ }
328
+ )
329
+ ] }),
330
+ /* @__PURE__ */ d("label", { className: "path-field", children: [
331
+ /* @__PURE__ */ t("span", { children: "Fields (JSON, optional)" }),
332
+ /* @__PURE__ */ t(
333
+ "textarea",
334
+ {
335
+ className: "modal-textarea chunk-fields-input",
336
+ rows: 4,
337
+ value: y,
338
+ placeholder: "{}",
339
+ onChange: (b) => E(b.target.value)
340
+ }
341
+ )
342
+ ] }),
343
+ P && /* @__PURE__ */ d("div", { className: "modal-err", children: [
344
+ P,
345
+ W && " — reopen the chunk to load the latest, then reapply your edit."
346
+ ] }),
347
+ /* @__PURE__ */ d("div", { className: "modal-buttons", children: [
348
+ /* @__PURE__ */ t("button", { type: "button", onClick: n, disabled: T, children: "cancel" }),
349
+ /* @__PURE__ */ t("button", { type: "submit", className: "primary", disabled: T, children: T ? "saving…" : "save" })
350
+ ] })
351
+ ] }) });
352
+ }
353
+ function te({ source: a }) {
354
+ return /* @__PURE__ */ t("div", { className: "md", children: xe(a) });
355
+ }
356
+ function xe(a) {
357
+ const e = a.replace(/\r\n/g, `
358
+ `).split(`
359
+ `), r = [];
360
+ let n = 0, s = 0;
361
+ for (; n < e.length; ) {
362
+ const c = e[n];
363
+ if (c.trim() === "") {
364
+ n++;
365
+ continue;
366
+ }
367
+ if (c.startsWith("```")) {
368
+ const h = [];
369
+ for (n++; n < e.length && !e[n].startsWith("```"); )
370
+ h.push(e[n]), n++;
371
+ n++, r.push(
372
+ /* @__PURE__ */ t("pre", { className: "md-pre", children: /* @__PURE__ */ t("code", { children: h.join(`
373
+ `) }) }, s++)
374
+ );
375
+ continue;
376
+ }
377
+ const l = /^(#{1,6})\s+(.*)$/.exec(c);
378
+ if (l) {
379
+ r.push(Ee(l[1].length, z(l[2]), s++)), n++;
380
+ continue;
381
+ }
382
+ if (/^>\s?/.test(c)) {
383
+ const h = [];
384
+ for (; n < e.length && /^>\s?/.test(e[n]); )
385
+ h.push(e[n].replace(/^>\s?/, "")), n++;
386
+ r.push(
387
+ /* @__PURE__ */ t("blockquote", { children: z(h.join(" ")) }, s++)
388
+ );
389
+ continue;
390
+ }
391
+ const i = /^[-*]\s+(.*)$/, o = /^\d+\.\s+(.*)$/;
392
+ if (i.test(c) || o.test(c)) {
393
+ const h = o.test(c), x = h ? o : i, k = [];
394
+ let M = 0;
395
+ for (; n < e.length && x.test(e[n]); ) {
396
+ const y = x.exec(e[n]);
397
+ k.push(/* @__PURE__ */ t("li", { children: z(y[1]) }, M++)), n++;
398
+ }
399
+ r.push(h ? /* @__PURE__ */ t("ol", { children: k }, s++) : /* @__PURE__ */ t("ul", { children: k }, s++));
400
+ continue;
401
+ }
402
+ const u = [];
403
+ for (; n < e.length && e[n].trim() !== "" && !e[n].startsWith("```") && !/^(#{1,6})\s+/.test(e[n]) && !/^>\s?/.test(e[n]) && !i.test(e[n]) && !o.test(e[n]); )
404
+ u.push(e[n]), n++;
405
+ r.push(/* @__PURE__ */ t("p", { children: z(u.join(" ")) }, s++));
406
+ }
407
+ return r;
408
+ }
409
+ function Ee(a, e, r) {
410
+ switch (a) {
411
+ case 1:
412
+ return /* @__PURE__ */ t("h1", { children: e }, r);
413
+ case 2:
414
+ return /* @__PURE__ */ t("h2", { children: e }, r);
415
+ case 3:
416
+ return /* @__PURE__ */ t("h3", { children: e }, r);
417
+ case 4:
418
+ return /* @__PURE__ */ t("h4", { children: e }, r);
419
+ case 5:
420
+ return /* @__PURE__ */ t("h5", { children: e }, r);
421
+ default:
422
+ return /* @__PURE__ */ t("h6", { children: e }, r);
423
+ }
424
+ }
425
+ const Se = /(`[^`]+`)|(\*\*[^*]+?\*\*)|(\*[^*]+?\*)|(\[[^\]]+\]\([^)]+\))/;
426
+ function z(a) {
427
+ const e = [];
428
+ let r = a, n = 0;
429
+ for (; r.length > 0; ) {
430
+ const s = Se.exec(r);
431
+ if (!s) {
432
+ e.push(r);
433
+ break;
434
+ }
435
+ s.index > 0 && e.push(r.slice(0, s.index));
436
+ const c = s[0];
437
+ if (c.startsWith("`"))
438
+ e.push(/* @__PURE__ */ t("code", { children: c.slice(1, -1) }, n++));
439
+ else if (c.startsWith("**"))
440
+ e.push(/* @__PURE__ */ t("strong", { children: c.slice(2, -2) }, n++));
441
+ else if (c.startsWith("*"))
442
+ e.push(/* @__PURE__ */ t("em", { children: c.slice(1, -1) }, n++));
443
+ else {
444
+ const l = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(c), i = l[1], o = l[2], u = o.startsWith("/") && !o.startsWith("//");
445
+ /^https?:\/\//i.test(o) || u ? e.push(
446
+ /* @__PURE__ */ t("a", { href: o, target: "_blank", rel: "noopener noreferrer", children: i }, n++)
447
+ ) : e.push(i);
448
+ }
449
+ r = r.slice(s.index + c.length);
450
+ }
451
+ return e;
452
+ }
453
+ function ie({
454
+ documentId: a,
455
+ scope: e,
456
+ titleHint: r,
457
+ browse: n,
458
+ principal: s,
459
+ renderAssistant: c
460
+ }) {
461
+ const l = X(), i = U(
462
+ () => n && (n.scopeId || n.tenant) ? { scopeId: n.scopeId, tenant: n.tenant } : void 0,
463
+ [n?.scopeId, n?.tenant]
464
+ ), [o, u] = f([]), [h, x] = f(void 0), [k, M] = f(null), [y, E] = f("chunks"), [T, L] = f(null), [P, D] = f(null), [W, R] = f(null), [F, b] = f(!1), [N, C] = f(0), [B, J] = f(!1), m = S(() => C((g) => g + 1), []), A = U(() => ve(o), [o]), $ = U(() => o.find((w) => !w.parent_id)?.title || r || "document", [o, r]), V = U(
465
+ () => !!k && !k.parent_id,
466
+ [k]
467
+ );
468
+ j(() => {
469
+ let g = !1;
470
+ return b(!0), R(null), l.documentQueryChunks(a, e, i).then((w) => {
471
+ if (g) return;
472
+ const I = w.chunks ?? [];
473
+ u(I), x((p) => p ?? I.find((v) => !v.parent_id)?.id);
474
+ }).catch((w) => !g && R(w instanceof Error ? w.message : String(w))).finally(() => !g && b(!1)), () => {
475
+ g = !0;
476
+ };
477
+ }, [l, a, e, N, i]), j(() => {
478
+ x(void 0), M(null), L(null), E("chunks");
479
+ }, [a, e]), j(() => {
480
+ if (!h) {
481
+ M(null);
482
+ return;
483
+ }
484
+ let g = !1;
485
+ return l.documentGetChunk(h, e, i).then((w) => !g && M(w)).catch((w) => !g && R(w instanceof Error ? w.message : String(w))), () => {
486
+ g = !0;
487
+ };
488
+ }, [l, h, e, N, i]), j(() => {
489
+ if (y !== "markdown" || !h) {
490
+ L(null);
491
+ return;
492
+ }
493
+ const g = ce(A, h);
494
+ if (!g) return;
495
+ let w = !1;
496
+ const I = [], p = (v, _) => {
497
+ I.push({ node: v, depth: _ }), v.children.forEach((O) => p(O, _ + 1));
498
+ };
499
+ return p(g, 0), Promise.all(I.map((v) => l.documentGetChunk(v.node.row.id, e, i))).then((v) => {
500
+ if (w) return;
501
+ const _ = I.map((O, he) => {
502
+ const me = Math.min(O.depth + 1, 6), Y = v[he];
503
+ return "#".repeat(me) + " " + O.node.row.title + (Y.body ? `
504
+
505
+ ` + Y.body : "") + `
506
+ `;
507
+ }).join(`
508
+ `);
509
+ L(_);
510
+ }).catch((v) => !w && R(v instanceof Error ? v.message : String(v))), () => {
511
+ w = !0;
512
+ };
513
+ }, [l, y, h, A, e, N, i]);
514
+ const Z = S((g) => x(g.row.id), []), q = S(async () => {
515
+ try {
516
+ const g = await l.documentExportMd(a, e, !0, i), w = new Blob([g.markdown], { type: "text/markdown" }), I = URL.createObjectURL(w), p = document.createElement("a");
517
+ p.href = I, p.download = (g.title || "document").replace(/[^\w.-]+/g, "_") + ".md", p.click(), URL.revokeObjectURL(I);
518
+ } catch (g) {
519
+ R(g instanceof Error ? g.message : String(g));
520
+ }
521
+ }, [l, a, e, i]);
522
+ return /* @__PURE__ */ d("div", { className: "doc-viewer", children: [
523
+ /* @__PURE__ */ d("div", { className: "doc-toolbar", children: [
524
+ /* @__PURE__ */ t("strong", { className: "doc-title", title: $, children: $ }),
525
+ /* @__PURE__ */ d("div", { className: "doc-toolbar-actions", children: [
526
+ /* @__PURE__ */ t("button", { type: "button", onClick: () => {
527
+ q();
528
+ }, title: "Download the whole document as Markdown (.md)", children: "↓ .md" }),
529
+ c && /* @__PURE__ */ t(
530
+ "button",
531
+ {
532
+ type: "button",
533
+ className: B ? "active" : "",
534
+ onClick: () => J((g) => !g),
535
+ title: "Document Assistant — instruct an agent to restructure/import/link chunks",
536
+ children: "assistant"
537
+ }
538
+ ),
539
+ /* @__PURE__ */ t("button", { type: "button", onClick: m, title: "Reload", children: "↻" })
540
+ ] })
541
+ ] }),
542
+ W && /* @__PURE__ */ t("div", { className: "paths-err", children: W }),
543
+ /* @__PURE__ */ d("div", { className: "doc-split", children: [
544
+ /* @__PURE__ */ t("div", { className: "doc-chunktree", children: F && o.length === 0 ? /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "Loading…" }) }) : /* @__PURE__ */ t(Ne, { tree: A, selectedId: h, onSelect: Z }) }),
545
+ /* @__PURE__ */ t("div", { className: "doc-content", children: k ? /* @__PURE__ */ d(G, { children: [
546
+ /* @__PURE__ */ d("div", { className: "doc-content-head", children: [
547
+ /* @__PURE__ */ t("h3", { children: k.title || "(untitled)" }),
548
+ /* @__PURE__ */ d("div", { className: "doc-content-meta", children: [
549
+ k.type && /* @__PURE__ */ t("span", { className: "chunk-badge", children: k.type }),
550
+ k.status && /* @__PURE__ */ t("span", { className: "chunk-badge chunk-status", children: k.status }),
551
+ /* @__PURE__ */ d("span", { className: "chunk-rev", children: [
552
+ "rev ",
553
+ k.revision
554
+ ] })
555
+ ] }),
556
+ /* @__PURE__ */ d("div", { className: "doc-content-controls", children: [
557
+ /* @__PURE__ */ d("div", { className: "doc-mode-toggle", children: [
558
+ /* @__PURE__ */ t(
559
+ "button",
560
+ {
561
+ type: "button",
562
+ className: y === "chunks" ? "active" : "",
563
+ onClick: () => E("chunks"),
564
+ title: "This chunk's own content",
565
+ children: "chunk"
566
+ }
567
+ ),
568
+ /* @__PURE__ */ t(
569
+ "button",
570
+ {
571
+ type: "button",
572
+ className: y === "markdown" ? "active" : "",
573
+ onClick: () => E("markdown"),
574
+ title: V ? "Whole document as Markdown" : "This chunk + its sub-chunks as Markdown",
575
+ children: "markdown"
576
+ }
577
+ )
578
+ ] }),
579
+ /* @__PURE__ */ t("button", { type: "button", onClick: () => D(k), children: "edit" })
580
+ ] })
581
+ ] }),
582
+ /* @__PURE__ */ t("div", { className: "doc-content-body", children: y === "markdown" ? T !== null ? /* @__PURE__ */ t(te, { source: T }) : /* @__PURE__ */ t("p", { className: "doc-empty-body", children: "Assembling Markdown…" }) : k.body ? /* @__PURE__ */ t(te, { source: k.body }) : /* @__PURE__ */ t("p", { className: "doc-empty-body", children: "(empty chunk)" }) })
583
+ ] }) : /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "Select a chunk on the left." }) }) })
584
+ ] }),
585
+ B && c && /* @__PURE__ */ t("div", { className: "doc-assistant", children: c({
586
+ documentId: a,
587
+ scope: e,
588
+ subject: s?.subject,
589
+ // Pass the viewer's own reload so the injected assistant can refresh
590
+ // the tree + selected chunk after a turn mutates the document.
591
+ onChanged: m
592
+ }) }),
593
+ P && /* @__PURE__ */ t(
594
+ Ce,
595
+ {
596
+ chunk: P,
597
+ scope: e,
598
+ browse: i,
599
+ onClose: () => D(null),
600
+ onSaved: m
601
+ }
602
+ )
603
+ ] });
604
+ }
605
+ function De(a) {
606
+ const e = {
607
+ name: "",
608
+ fullPath: "",
609
+ kind: "directory",
610
+ explicit: !0,
611
+ children: []
612
+ }, r = /* @__PURE__ */ new Map([["", e]]), n = (c) => {
613
+ const l = r.get(c);
614
+ if (l) return l;
615
+ const i = c.lastIndexOf("/"), o = i <= 0 ? "" : c.slice(0, i), u = c.slice(i + 1), h = n(o), x = {
616
+ name: u,
617
+ fullPath: c,
618
+ kind: "directory",
619
+ explicit: !1,
620
+ children: []
621
+ };
622
+ return h.children.push(x), r.set(c, x), x;
623
+ };
624
+ for (const c of a) {
625
+ const l = n(c.full_path);
626
+ l.kind = c.kind, l.explicit = !0, l.resourceRef = c.resource_ref;
627
+ }
628
+ const s = (c) => {
629
+ c.sort((l, i) => {
630
+ const o = l.kind === "directory", u = i.kind === "directory";
631
+ return o !== u ? o ? -1 : 1 : l.name.localeCompare(i.name);
632
+ }), c.forEach((l) => s(l.children));
633
+ };
634
+ return s(e.children), e.children;
635
+ }
636
+ function Q(a) {
637
+ const e = a.lastIndexOf("/");
638
+ return e <= 0 ? "" : a.slice(0, e);
639
+ }
640
+ function Me(a) {
641
+ const e = [], r = (n) => {
642
+ if (n.kind === "document") {
643
+ const s = n.resourceRef;
644
+ s?.document_id && e.push(s.document_id);
645
+ }
646
+ n.children.forEach(r);
647
+ };
648
+ return r(a), e;
649
+ }
650
+ const _e = {
651
+ directory: "📁",
652
+ document: "📄",
653
+ volume_mount: "💾",
654
+ memory_entry: "🧠"
655
+ };
656
+ function Le({ tree: a, selectedPath: e, onSelect: r }) {
657
+ const [n, s] = f(() => /* @__PURE__ */ new Map()), c = S((l) => {
658
+ s((i) => {
659
+ const o = new Map(i);
660
+ return o.set(l, i.get(l) === !1), o;
661
+ });
662
+ }, []);
663
+ return j(() => {
664
+ e && s((l) => {
665
+ let i = !1;
666
+ const o = new Map(l);
667
+ let u = Q(e);
668
+ for (; u !== ""; )
669
+ o.get(u) === !1 && (o.set(u, !0), i = !0), u = Q(u);
670
+ return i ? o : l;
671
+ });
672
+ }, [e]), a.length === 0 ? /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "This tree is empty. Create a folder or document to begin." }) }) : /* @__PURE__ */ t("ul", { className: "tree path-tree", children: a.map((l) => /* @__PURE__ */ t(
673
+ de,
674
+ {
675
+ node: l,
676
+ expanded: n,
677
+ toggle: c,
678
+ selectedPath: e,
679
+ onSelect: r
680
+ },
681
+ l.fullPath
682
+ )) });
683
+ }
684
+ function de({ node: a, expanded: e, toggle: r, selectedPath: n, onSelect: s }) {
685
+ const c = a.children.length > 0, l = e.get(a.fullPath) !== !1, i = n === a.fullPath;
686
+ return /* @__PURE__ */ d("li", { className: `node path-node kind-${a.kind} ${i ? "selected" : ""}`, children: [
687
+ /* @__PURE__ */ d("div", { className: "row path-row", children: [
688
+ /* @__PURE__ */ t(
689
+ "button",
690
+ {
691
+ type: "button",
692
+ className: "tree-caret",
693
+ "aria-label": l ? "collapse" : "expand",
694
+ disabled: !c,
695
+ onClick: (o) => {
696
+ o.stopPropagation(), c && r(a.fullPath);
697
+ },
698
+ children: c ? l ? "▼" : "▶" : "·"
699
+ }
700
+ ),
701
+ /* @__PURE__ */ d(
702
+ "button",
703
+ {
704
+ type: "button",
705
+ className: "path-link",
706
+ onClick: () => s(a),
707
+ title: a.fullPath,
708
+ children: [
709
+ /* @__PURE__ */ t("span", { className: "path-kind-icon", "aria-hidden": !0, children: _e[a.kind] ?? "•" }),
710
+ /* @__PURE__ */ t("span", { className: "path-name", children: a.name }),
711
+ !a.explicit && /* @__PURE__ */ t("span", { className: "path-implicit", title: "Implicit directory (no stored entry)", children: "implicit" })
712
+ ]
713
+ }
714
+ )
715
+ ] }),
716
+ c && l && /* @__PURE__ */ t("ul", { className: "children path-children", children: a.children.map((o) => /* @__PURE__ */ t(
717
+ de,
718
+ {
719
+ node: o,
720
+ expanded: e,
721
+ toggle: r,
722
+ selectedPath: n,
723
+ onSelect: s
724
+ },
725
+ o.fullPath
726
+ )) })
727
+ ] });
728
+ }
729
+ const ne = /^[A-Za-z0-9._-]+$/;
730
+ function ae(a, e) {
731
+ return `${a}/${e}`;
732
+ }
733
+ function ue(a, e) {
734
+ for (const r of a) {
735
+ if (r.fullPath === e) return r;
736
+ const n = ue(r.children, e);
737
+ if (n) return n;
738
+ }
739
+ }
740
+ function Oe(a) {
741
+ const { theme: e, defaultScope: r, path: n, onPathChange: s, browse: c, principal: l, renderAssistant: i, onError: o } = a, u = le(a);
742
+ return /* @__PURE__ */ t(se, { theme: e, dataLayer: u, children: /* @__PURE__ */ t(
743
+ Re,
744
+ {
745
+ defaultScope: r,
746
+ path: n,
747
+ onPathChange: s,
748
+ browse: c,
749
+ principal: l,
750
+ renderAssistant: i,
751
+ onError: o
752
+ }
753
+ ) });
754
+ }
755
+ function Re({
756
+ defaultScope: a,
757
+ path: e,
758
+ onPathChange: r,
759
+ browse: n,
760
+ principal: s,
761
+ renderAssistant: c,
762
+ onError: l
763
+ }) {
764
+ const i = X(), [o, u] = f(a ?? "user"), [h, x] = f([]), [k, M] = f(void 0), [y, E] = f(null), [T, L] = f(!1), [P, D] = f(null), W = e !== void 0 ? e : k, R = H(r);
765
+ j(() => {
766
+ R.current = r;
767
+ }, [r]);
768
+ const F = H(l);
769
+ j(() => {
770
+ F.current = l;
771
+ }, [l]);
772
+ const b = S((p) => {
773
+ R.current?.(p), M(p);
774
+ }, []), N = U(
775
+ () => n && (n.scopeId || n.tenant) ? { scopeId: n.scopeId, tenant: n.tenant } : void 0,
776
+ [n?.scopeId, n?.tenant]
777
+ ), C = S(async () => {
778
+ L(!0), E(null);
779
+ try {
780
+ const p = await i.pathLs("/", o, !0, N);
781
+ x(p.entries ?? []);
782
+ } catch (p) {
783
+ E(p instanceof Error ? p.message : String(p)), x([]), F.current?.(p);
784
+ } finally {
785
+ L(!1);
786
+ }
787
+ }, [i, o, N]), B = H(!0);
788
+ j(() => {
789
+ B.current ? B.current = !1 : b(void 0), C();
790
+ }, [C, b]);
791
+ const J = U(() => De(h), [h]), m = U(
792
+ () => W ? ue(J, W) : void 0,
793
+ [J, W]
794
+ ), A = U(() => m ? m.kind === "directory" ? m.fullPath : Q(m.fullPath) : "", [m]), $ = A || "/", V = o !== "tenant", Z = S(() => {
795
+ D({
796
+ title: `New folder in ${$}`,
797
+ fields: [{ key: "name", label: "Folder name", placeholder: "e.g. launches" }],
798
+ submitLabel: "Create",
799
+ onSubmit: async (p) => {
800
+ const v = p.name.trim();
801
+ if (!ne.test(v)) throw new Error("name may contain only [A-Za-z0-9._-]");
802
+ const _ = ae(A, v);
803
+ await i.pathMkdir(_, o, N), await C(), b(_);
804
+ }
805
+ });
806
+ }, [i, A, $, o, N, C, b]), q = S(() => {
807
+ D({
808
+ title: `New document in ${$}`,
809
+ fields: [
810
+ { key: "name", label: "Path name", placeholder: "e.g. launch-plan" },
811
+ { key: "title", label: "Title", placeholder: "e.g. Launch Plan" }
812
+ ],
813
+ submitLabel: "Create",
814
+ onSubmit: async (p) => {
815
+ const v = p.name.trim();
816
+ if (!ne.test(v)) throw new Error("name may contain only [A-Za-z0-9._-]");
817
+ const _ = p.title.trim() || v, O = ae(A, v);
818
+ await i.documentCreate(_, O, o, N), await C(), b(O);
819
+ }
820
+ });
821
+ }, [i, A, $, o, N, C, b]), g = S(() => {
822
+ m && D({
823
+ title: `Rename / move ${m.fullPath}`,
824
+ fields: [{ key: "to", label: "New path", value: m.fullPath }],
825
+ submitLabel: "Move",
826
+ onSubmit: async (p) => {
827
+ const v = p.to.trim();
828
+ if (!v.startsWith("/")) throw new Error("path must start with /");
829
+ await i.pathMv(m.fullPath, v, o, N), await C(), b(v);
830
+ }
831
+ });
832
+ }, [i, m, o, N, C, b]), w = S(() => {
833
+ if (!m) return;
834
+ if (m.kind === "document") {
835
+ const O = m.resourceRef?.document_id;
836
+ D({
837
+ title: `Delete document ${m.fullPath}`,
838
+ message: "This deletes the document, all its chunks, and its path entry. This cannot be undone.",
839
+ fields: [],
840
+ submitLabel: "Delete",
841
+ danger: !0,
842
+ onSubmit: async () => {
843
+ if (!O) throw new Error("this document dirent has no document_id");
844
+ await i.documentDelete(O, o, N), await C(), b(void 0);
845
+ }
846
+ });
847
+ return;
848
+ }
849
+ const p = o === "tenant" ? [] : Me(m), v = m.children.length;
850
+ D({
851
+ title: `Delete branch ${m.fullPath}`,
852
+ message: v === 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.` : "."),
853
+ fields: [],
854
+ submitLabel: "Delete",
855
+ danger: !0,
856
+ onSubmit: async () => {
857
+ for (const _ of p)
858
+ await i.documentDelete(_, o, N);
859
+ await i.pathRm(m.fullPath, o, !0, N), await C(), b(void 0);
860
+ }
861
+ });
862
+ }, [i, m, o, N, C, b]), I = m && (m.kind === "directory" || m.kind === "document");
863
+ return /* @__PURE__ */ d(G, { children: [
864
+ /* @__PURE__ */ d(
865
+ ye,
866
+ {
867
+ className: "paths-view",
868
+ defaultLeftWidth: 420,
869
+ minLeftWidth: 280,
870
+ minRightWidth: 300,
871
+ storageKey: "loomcycle.explorer.split.paths",
872
+ children: [
873
+ /* @__PURE__ */ d("div", { className: "left", children: [
874
+ /* @__PURE__ */ d("div", { className: "paths-toolbar", children: [
875
+ /* @__PURE__ */ d("label", { className: "paths-scope", children: [
876
+ /* @__PURE__ */ t("span", { children: "scope" }),
877
+ /* @__PURE__ */ d("select", { value: o, onChange: (p) => u(p.target.value), children: [
878
+ /* @__PURE__ */ t("option", { value: "user", children: "user" }),
879
+ /* @__PURE__ */ t("option", { value: "agent", children: "agent" }),
880
+ /* @__PURE__ */ t("option", { value: "tenant", children: "tenant" })
881
+ ] })
882
+ ] }),
883
+ /* @__PURE__ */ d("div", { className: "paths-toolbar-actions", children: [
884
+ /* @__PURE__ */ t("button", { type: "button", onClick: Z, title: `New folder in ${$}`, children: "+ folder" }),
885
+ /* @__PURE__ */ t(
886
+ "button",
887
+ {
888
+ type: "button",
889
+ onClick: q,
890
+ disabled: !V,
891
+ title: V ? `New document in ${$}` : "Documents are agent/user scope only",
892
+ children: "+ document"
893
+ }
894
+ ),
895
+ /* @__PURE__ */ t("button", { type: "button", onClick: () => {
896
+ C();
897
+ }, title: "Reload the tree", children: "↻" })
898
+ ] })
899
+ ] }),
900
+ /* @__PURE__ */ d("p", { className: "paths-context", children: [
901
+ "new items → ",
902
+ /* @__PURE__ */ t("code", { children: $ }),
903
+ N?.scopeId && /* @__PURE__ */ d(G, { children: [
904
+ " · ",
905
+ "subject ",
906
+ /* @__PURE__ */ t("code", { children: N.scopeId })
907
+ ] }),
908
+ N?.tenant && /* @__PURE__ */ d(G, { children: [
909
+ " · ",
910
+ "tenant ",
911
+ /* @__PURE__ */ t("code", { children: N.tenant })
912
+ ] })
913
+ ] }),
914
+ y && /* @__PURE__ */ t("div", { className: "paths-err", children: y }),
915
+ T && h.length === 0 ? /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "Loading…" }) }) : /* @__PURE__ */ t(
916
+ Le,
917
+ {
918
+ tree: J,
919
+ selectedPath: W,
920
+ onSelect: (p) => b(p.fullPath)
921
+ }
922
+ )
923
+ ] }),
924
+ /* @__PURE__ */ t("div", { className: "right", children: m ? m.kind === "document" ? /* @__PURE__ */ d("div", { className: "paths-doc", children: [
925
+ /* @__PURE__ */ d("div", { className: "paths-doc-head", children: [
926
+ /* @__PURE__ */ t("code", { className: "paths-doc-path", children: m.fullPath }),
927
+ /* @__PURE__ */ d("div", { className: "paths-detail-actions", children: [
928
+ /* @__PURE__ */ t("button", { type: "button", onClick: g, children: "rename / move" }),
929
+ /* @__PURE__ */ t("button", { type: "button", className: "danger", onClick: w, children: "delete" })
930
+ ] })
931
+ ] }),
932
+ /* @__PURE__ */ t(
933
+ ie,
934
+ {
935
+ documentId: m.resourceRef?.document_id ?? "",
936
+ scope: o === "agent" ? "agent" : "user",
937
+ titleHint: m.name,
938
+ browse: N,
939
+ principal: s,
940
+ renderAssistant: c
941
+ }
942
+ )
943
+ ] }) : /* @__PURE__ */ d("div", { className: "paths-detail", children: [
944
+ /* @__PURE__ */ d("h2", { children: [
945
+ /* @__PURE__ */ t("span", { className: "paths-detail-kind", children: m.kind }),
946
+ /* @__PURE__ */ t("code", { children: m.fullPath })
947
+ ] }),
948
+ /* @__PURE__ */ d("dl", { className: "paths-detail-meta", children: [
949
+ /* @__PURE__ */ t("dt", { children: "scope" }),
950
+ /* @__PURE__ */ t("dd", { children: o }),
951
+ /* @__PURE__ */ t("dt", { children: "stored" }),
952
+ /* @__PURE__ */ t("dd", { children: m.explicit ? "yes" : "implicit (no entry)" })
953
+ ] }),
954
+ I ? /* @__PURE__ */ d("div", { className: "paths-detail-actions", children: [
955
+ /* @__PURE__ */ t("button", { type: "button", onClick: g, children: "rename / move" }),
956
+ /* @__PURE__ */ t("button", { type: "button", className: "danger", onClick: w, children: "delete" })
957
+ ] }) : /* @__PURE__ */ d("p", { className: "paths-readonly", children: [
958
+ "Read-only — ",
959
+ m.kind,
960
+ " entries are managed by their own tool."
961
+ ] })
962
+ ] }) : /* @__PURE__ */ t("div", { className: "empty", children: /* @__PURE__ */ t("p", { children: "Select a path on the left, or create a folder / document." }) }) })
963
+ ]
964
+ }
965
+ ),
966
+ P && /* @__PURE__ */ t(Ie, { state: P, onClose: () => D(null) })
967
+ ] });
968
+ }
969
+ function Ie({ state: a, onClose: e }) {
970
+ const [r, n] = f(
971
+ () => Object.fromEntries(a.fields.map((u) => [u.key, u.value ?? ""]))
972
+ ), [s, c] = f(!1), [l, i] = f(null);
973
+ return /* @__PURE__ */ t("div", { className: "modal-overlay", onClick: e, children: /* @__PURE__ */ d("form", { className: "modal", onClick: (u) => u.stopPropagation(), onSubmit: async (u) => {
974
+ u.preventDefault(), c(!0), i(null);
975
+ try {
976
+ await a.onSubmit(r), e();
977
+ } catch (h) {
978
+ i(h instanceof Error ? h.message : String(h)), c(!1);
979
+ }
980
+ }, children: [
981
+ /* @__PURE__ */ t("h3", { children: a.title }),
982
+ a.message && /* @__PURE__ */ t("p", { className: "modal-context", children: a.message }),
983
+ a.fields.map((u, h) => /* @__PURE__ */ d("label", { className: "path-field", children: [
984
+ /* @__PURE__ */ t("span", { children: u.label }),
985
+ /* @__PURE__ */ t(
986
+ "input",
987
+ {
988
+ className: "path-modal-input",
989
+ value: r[u.key],
990
+ placeholder: u.placeholder,
991
+ autoFocus: h === 0,
992
+ onChange: (x) => n((k) => ({ ...k, [u.key]: x.target.value }))
993
+ }
994
+ )
995
+ ] }, u.key)),
996
+ l && /* @__PURE__ */ t("div", { className: "modal-err", children: l }),
997
+ /* @__PURE__ */ d("div", { className: "modal-buttons", children: [
998
+ /* @__PURE__ */ t("button", { type: "button", onClick: e, disabled: s, children: "cancel" }),
999
+ /* @__PURE__ */ t("button", { type: "submit", className: a.danger ? "danger" : "primary", disabled: s, children: s ? "working…" : a.submitLabel })
1000
+ ] })
1001
+ ] }) });
1002
+ }
1003
+ function je(a) {
1004
+ const { documentId: e, scope: r, titleHint: n, browse: s, theme: c, principal: l, renderAssistant: i } = a, o = le(a);
1005
+ return /* @__PURE__ */ t(se, { theme: c, dataLayer: o, children: /* @__PURE__ */ t(
1006
+ ie,
1007
+ {
1008
+ documentId: e,
1009
+ scope: r,
1010
+ titleHint: n,
1011
+ browse: s,
1012
+ principal: l,
1013
+ renderAssistant: i
1014
+ }
1015
+ ) });
1016
+ }
1017
+ export {
1018
+ je as DocumentViewer,
1019
+ Oe as PathExplorer,
1020
+ ke as createLoomcycleClient,
1021
+ K as dataLayerFromClient
1022
+ };
1023
+ //# sourceMappingURL=index.js.map