@ai-matrx/alchemy 0.6.2 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/dist/actions.cjs +374 -0
  3. package/dist/actions.cjs.map +1 -0
  4. package/dist/actions.d.cts +773 -0
  5. package/dist/actions.d.ts +773 -0
  6. package/dist/actions.js +351 -0
  7. package/dist/actions.js.map +1 -0
  8. package/dist/checks.cjs +31 -2
  9. package/dist/checks.cjs.map +1 -1
  10. package/dist/checks.d.cts +24 -1
  11. package/dist/checks.d.ts +24 -1
  12. package/dist/checks.js +31 -2
  13. package/dist/checks.js.map +1 -1
  14. package/dist/core.cjs +1757 -2
  15. package/dist/core.cjs.map +1 -1
  16. package/dist/core.d.cts +595 -1
  17. package/dist/core.d.ts +595 -1
  18. package/dist/core.js +1749 -2
  19. package/dist/core.js.map +1 -1
  20. package/dist/declare.cjs +12 -10
  21. package/dist/declare.cjs.map +1 -1
  22. package/dist/declare.d.cts +18 -2
  23. package/dist/declare.d.ts +18 -2
  24. package/dist/declare.js +12 -10
  25. package/dist/declare.js.map +1 -1
  26. package/dist/index.cjs +1757 -2
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +595 -1
  29. package/dist/index.d.ts +595 -1
  30. package/dist/index.js +1749 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/menu.cjs +436 -0
  33. package/dist/menu.cjs.map +1 -0
  34. package/dist/menu.d.cts +834 -0
  35. package/dist/menu.d.ts +834 -0
  36. package/dist/menu.js +413 -0
  37. package/dist/menu.js.map +1 -0
  38. package/dist/operate-document.cjs +163 -0
  39. package/dist/operate-document.cjs.map +1 -0
  40. package/dist/operate-document.d.cts +132 -0
  41. package/dist/operate-document.d.ts +132 -0
  42. package/dist/operate-document.js +130 -0
  43. package/dist/operate-document.js.map +1 -0
  44. package/dist/operate-rich-html.cjs +129 -0
  45. package/dist/operate-rich-html.cjs.map +1 -0
  46. package/dist/operate-rich-html.d.cts +136 -0
  47. package/dist/operate-rich-html.d.ts +136 -0
  48. package/dist/operate-rich-html.js +96 -0
  49. package/dist/operate-rich-html.js.map +1 -0
  50. package/dist/operate-server.cjs +157 -0
  51. package/dist/operate-server.cjs.map +1 -0
  52. package/dist/operate-server.d.cts +183 -0
  53. package/dist/operate-server.d.ts +183 -0
  54. package/dist/operate-server.js +134 -0
  55. package/dist/operate-server.js.map +1 -0
  56. package/dist/operate-xlsx.cjs +248 -0
  57. package/dist/operate-xlsx.cjs.map +1 -0
  58. package/dist/operate-xlsx.d.cts +129 -0
  59. package/dist/operate-xlsx.d.ts +129 -0
  60. package/dist/operate-xlsx.js +215 -0
  61. package/dist/operate-xlsx.js.map +1 -0
  62. package/dist/operate.cjs +1774 -0
  63. package/dist/operate.cjs.map +1 -0
  64. package/dist/operate.d.cts +595 -0
  65. package/dist/operate.d.ts +595 -0
  66. package/dist/operate.js +1750 -0
  67. package/dist/operate.js.map +1 -0
  68. package/dist/ports.cjs.map +1 -1
  69. package/dist/ports.d.cts +361 -3
  70. package/dist/ports.d.ts +361 -3
  71. package/dist/react-bar.cjs +686 -0
  72. package/dist/react-bar.cjs.map +1 -0
  73. package/dist/react-bar.d.cts +379 -0
  74. package/dist/react-bar.d.ts +379 -0
  75. package/dist/react-bar.js +665 -0
  76. package/dist/react-bar.js.map +1 -0
  77. package/dist/react-host.cjs +301 -0
  78. package/dist/react-host.cjs.map +1 -0
  79. package/dist/react-host.d.cts +820 -0
  80. package/dist/react-host.d.ts +820 -0
  81. package/dist/react-host.js +269 -0
  82. package/dist/react-host.js.map +1 -0
  83. package/dist/react-menu.cjs +735 -0
  84. package/dist/react-menu.cjs.map +1 -0
  85. package/dist/react-menu.d.cts +370 -0
  86. package/dist/react-menu.d.ts +370 -0
  87. package/dist/react-menu.js +722 -0
  88. package/dist/react-menu.js.map +1 -0
  89. package/dist/react-overflow.cjs +592 -0
  90. package/dist/react-overflow.cjs.map +1 -0
  91. package/dist/react-overflow.d.cts +817 -0
  92. package/dist/react-overflow.d.ts +817 -0
  93. package/dist/react-overflow.js +571 -0
  94. package/dist/react-overflow.js.map +1 -0
  95. package/dist/react-palette.cjs +489 -0
  96. package/dist/react-palette.cjs.map +1 -0
  97. package/dist/react-palette.d.cts +359 -0
  98. package/dist/react-palette.d.ts +359 -0
  99. package/dist/react-palette.js +457 -0
  100. package/dist/react-palette.js.map +1 -0
  101. package/dist/react-selection.cjs +617 -0
  102. package/dist/react-selection.cjs.map +1 -0
  103. package/dist/react-selection.d.cts +357 -0
  104. package/dist/react-selection.d.ts +357 -0
  105. package/dist/react-selection.js +596 -0
  106. package/dist/react-selection.js.map +1 -0
  107. package/dist/react-sheet.cjs +616 -0
  108. package/dist/react-sheet.cjs.map +1 -0
  109. package/dist/react-sheet.d.cts +361 -0
  110. package/dist/react-sheet.d.ts +361 -0
  111. package/dist/react-sheet.js +595 -0
  112. package/dist/react-sheet.js.map +1 -0
  113. package/package.json +148 -5
@@ -0,0 +1,595 @@
1
+ "use client";
2
+
3
+ // src/react/sheet.tsx
4
+ import * as React4 from "react";
5
+ import { BottomSheet, TooltipProvider } from "@ai-matrx/design-system";
6
+
7
+ // src/menu/layouts.ts
8
+ var INLINE_SECTION_MAX = 3;
9
+ function isLeaf(node) {
10
+ return node.kind === "item" || node.kind === "checkbox" || node.kind === "link";
11
+ }
12
+ function fold(section) {
13
+ return {
14
+ kind: "submenu",
15
+ id: `${section.id}:fold`,
16
+ label: section.label,
17
+ ...section.icon !== void 0 ? { icon: section.icon } : {},
18
+ children: section.nodes
19
+ };
20
+ }
21
+ function rowCount(nodes) {
22
+ return nodes.filter((n) => n.kind !== "separator" && n.kind !== "label").length;
23
+ }
24
+ function stripOf(model) {
25
+ const clipboard = model.sections.find((s) => s.group === "clipboard" && !s.primary);
26
+ return clipboard ? clipboard.nodes.filter(isLeaf) : [];
27
+ }
28
+ function clipboardRest(model) {
29
+ const clipboard = model.sections.find((s) => s.group === "clipboard" && !s.primary);
30
+ return clipboard ? clipboard.nodes.filter((n) => !isLeaf(n) && n.kind === "submenu") : [];
31
+ }
32
+ function sheet(model) {
33
+ const strip = stripOf(model);
34
+ const root = [];
35
+ const rest = clipboardRest(model);
36
+ for (const s of model.sections) {
37
+ const nodes = s.group === "clipboard" && !s.primary ? rest : s.nodes;
38
+ if (rowCount(nodes) === 0) continue;
39
+ if (root.length) root.push({ kind: "separator", id: `${s.id}:sep` });
40
+ if (s.primary || !s.label || rowCount(nodes) <= INLINE_SECTION_MAX) {
41
+ if (s.label) root.push({ kind: "label", id: `${s.id}:label`, label: s.label });
42
+ root.push(...nodes);
43
+ } else {
44
+ root.push(fold({ ...s, label: s.label, nodes }));
45
+ }
46
+ }
47
+ return { strip, root };
48
+ }
49
+
50
+ // src/menu/model.ts
51
+ var CATEGORY_GROUP = {
52
+ clipboard: "clipboard",
53
+ copy: "transfer",
54
+ export: "transfer",
55
+ save: "document",
56
+ share: "document",
57
+ edit: "editable",
58
+ history: "history",
59
+ ai: "ai",
60
+ ask: "ai",
61
+ listen: "tools",
62
+ study: "tools",
63
+ app: "tools",
64
+ feedback: "quick",
65
+ creator: "admin",
66
+ admin: "admin",
67
+ "surface-info": "surface-info"
68
+ };
69
+ var GROUP_ORDER = [
70
+ "clipboard",
71
+ "editable",
72
+ "transfer",
73
+ "document",
74
+ "history",
75
+ "ai",
76
+ "tools",
77
+ "quick",
78
+ "surface",
79
+ "admin",
80
+ "surface-info"
81
+ ];
82
+ var CATEGORY_ORDER = [
83
+ "clipboard",
84
+ "edit",
85
+ "copy",
86
+ "export",
87
+ "save",
88
+ "share",
89
+ "history",
90
+ "ask",
91
+ "ai",
92
+ "listen",
93
+ "study",
94
+ "app",
95
+ "feedback",
96
+ "creator",
97
+ "admin",
98
+ "surface-info"
99
+ ];
100
+ function translate(i18n, key, fallback) {
101
+ if (!i18n) return fallback;
102
+ try {
103
+ return i18n.t(key, fallback);
104
+ } catch {
105
+ return fallback;
106
+ }
107
+ }
108
+ function actionToNode(resolved, target, options = {}) {
109
+ const { action, eligibility } = resolved;
110
+ const rawLabel = typeof action.label === "function" ? action.label(target) : action.label;
111
+ const label = translate(options.i18n, action.labelKey ?? action.id, rawLabel);
112
+ const base = {
113
+ id: action.id,
114
+ actionId: action.id,
115
+ ...action.icon !== void 0 ? { icon: action.icon } : {},
116
+ ...action.placement !== void 0 ? { placement: action.placement } : {}
117
+ };
118
+ if (action.expand) {
119
+ const node = {
120
+ ...base,
121
+ kind: "submenu",
122
+ label,
123
+ lazy: true,
124
+ loading: false,
125
+ children: [],
126
+ ...action.emptyLabel !== void 0 ? { emptyLabel: action.emptyLabel } : {}
127
+ };
128
+ return node;
129
+ }
130
+ if (action.href !== void 0) {
131
+ const href = typeof action.href === "function" ? action.href(target) : action.href;
132
+ return { ...base, kind: "link", label, href };
133
+ }
134
+ if (action.toggle) {
135
+ return {
136
+ ...base,
137
+ kind: "checkbox",
138
+ label,
139
+ checked: action.pressed?.(target) ?? false,
140
+ ...action.description !== void 0 ? { description: action.description } : {}
141
+ };
142
+ }
143
+ const state = action.stateIcon?.(target) ?? null;
144
+ const pressed = action.pressed?.(target);
145
+ const hint = action.hint !== void 0 ? translate(options.i18n, action.hintKey ?? `${action.id}.hint`, action.hint) : void 0;
146
+ return {
147
+ ...base,
148
+ kind: "item",
149
+ label,
150
+ ...action.iconTone !== void 0 ? { iconTone: action.iconTone } : {},
151
+ ...state ? { stateIcon: state } : {},
152
+ ...action.description !== void 0 ? { description: action.description } : {},
153
+ ...hint !== void 0 ? { hint } : {},
154
+ ...pressed !== void 0 ? { pressed } : {},
155
+ ...action.destructive ? { destructive: true } : {},
156
+ ...action.preserveSelection ? { preserveSelection: true } : {},
157
+ ...eligibility.status === "unavailable-verb" ? { unavailable: { sentence: eligibility.sentence } } : {}
158
+ };
159
+ }
160
+ function buildMenuModel(target, resolved, options = {}) {
161
+ const ordered = resolved.map((r, index) => ({ r, index })).sort((a, b) => {
162
+ const ca = CATEGORY_ORDER.indexOf(a.r.action.category);
163
+ const cb = CATEGORY_ORDER.indexOf(b.r.action.category);
164
+ if (ca !== cb) return ca - cb;
165
+ const oa = a.r.action.order ?? 0;
166
+ const ob = b.r.action.order ?? 0;
167
+ if (oa !== ob) return oa - ob;
168
+ return a.index - b.index;
169
+ }).map((x) => x.r);
170
+ const sections = /* @__PURE__ */ new Map();
171
+ const roles = {};
172
+ const explicit = /* @__PURE__ */ new Map();
173
+ resolved.forEach((r, index) => {
174
+ const sec = r.action.section;
175
+ if (!sec) return;
176
+ const entry = explicit.get(sec.id) ?? { group: CATEGORY_GROUP[r.action.category], rows: [] };
177
+ entry.rows.push({ r, index });
178
+ explicit.set(sec.id, entry);
179
+ });
180
+ const placed = /* @__PURE__ */ new Set();
181
+ for (const r of ordered) {
182
+ const sec = r.action.section;
183
+ if (sec && placed.has(sec.id)) continue;
184
+ const group = sec ? explicit.get(sec.id)?.group ?? CATEGORY_GROUP[r.action.category] : CATEGORY_GROUP[r.action.category];
185
+ const sectionId = sec ? `section:${sec.id}` : group;
186
+ let section = sections.get(sectionId);
187
+ if (!section) {
188
+ section = {
189
+ id: sectionId,
190
+ group,
191
+ nodes: [],
192
+ ...sec ? { label: translate(options.i18n, sec.labelKey ?? `section.${sec.id}`, sec.label) } : {},
193
+ ...sec?.icon !== void 0 ? { icon: sec.icon } : {},
194
+ ...sec?.primary ? { primary: true } : {}
195
+ };
196
+ sections.set(sectionId, section);
197
+ }
198
+ const rows = sec ? (explicit.get(sec.id)?.rows ?? []).sort((x, y) => (x.r.action.order ?? 0) - (y.r.action.order ?? 0) || x.index - y.index).map((x) => x.r) : [r];
199
+ if (sec) placed.add(sec.id);
200
+ for (const row of rows) {
201
+ section.nodes.push(actionToNode(row, target, options));
202
+ if (row.action.verb) roles[row.action.verb] = row.action.id;
203
+ }
204
+ }
205
+ const list = [...sections.values()].sort((a, b) => {
206
+ if (Boolean(a.primary) !== Boolean(b.primary)) return a.primary ? -1 : 1;
207
+ return GROUP_ORDER.indexOf(a.group) - GROUP_ORDER.indexOf(b.group);
208
+ });
209
+ const selected = target.selection?.text?.trim();
210
+ const content = options.content?.trim();
211
+ const header = selected ? { label: "Selected", text: target.selection?.text ?? "" } : content ? { label: "Content", text: options.content ?? "" } : null;
212
+ return { header, sections: list, roles };
213
+ }
214
+ function withExpandedChildren(model, nodeId, children) {
215
+ const visit = (nodes) => nodes.map((n) => {
216
+ if (n.kind !== "submenu") return n;
217
+ if (n.id === nodeId) {
218
+ return children === "loading" ? { ...n, loading: true } : { ...n, lazy: false, loading: false, children };
219
+ }
220
+ return { ...n, children: visit(n.children) };
221
+ });
222
+ return { ...model, sections: model.sections.map((s) => ({ ...s, nodes: visit(s.nodes) })) };
223
+ }
224
+ function findNode(nodes, id) {
225
+ for (const n of nodes) {
226
+ if (n.id === id) return n;
227
+ if (n.kind === "submenu") {
228
+ const hit = findNode(n.children, id);
229
+ if (hit) return hit;
230
+ }
231
+ }
232
+ return null;
233
+ }
234
+
235
+ // src/react/engine.tsx
236
+ import * as React2 from "react";
237
+
238
+ // src/declare/sensitivity.ts
239
+ var DEFAULT_SENSITIVITY = Object.freeze({
240
+ exportable: true,
241
+ classification: "ordinary",
242
+ includedByDefault: true
243
+ });
244
+ function resolveSensitivity(value) {
245
+ return {
246
+ exportable: value.sensitivity?.exportable ?? DEFAULT_SENSITIVITY.exportable,
247
+ classification: value.sensitivity?.classification ?? DEFAULT_SENSITIVITY.classification,
248
+ includedByDefault: value.sensitivity?.includedByDefault ?? DEFAULT_SENSITIVITY.includedByDefault
249
+ };
250
+ }
251
+
252
+ // src/actions/projection.ts
253
+ function toJson(value) {
254
+ if (value === void 0 || typeof value === "function" || typeof value === "symbol") return void 0;
255
+ if (value === null || typeof value === "string" || typeof value === "boolean") return value;
256
+ if (typeof value === "number") return Number.isFinite(value) ? value : null;
257
+ if (typeof value === "bigint") return value.toString();
258
+ if (value instanceof Date) return value.toISOString();
259
+ if (Array.isArray(value)) return value.map((v) => toJson(v) ?? null);
260
+ if (typeof value === "object") {
261
+ const out = {};
262
+ for (const [k, v] of Object.entries(value)) {
263
+ const j = toJson(v);
264
+ if (j !== void 0) out[k] = j;
265
+ }
266
+ return out;
267
+ }
268
+ return void 0;
269
+ }
270
+ function projectValues(raw, declared, pathPrefix = "") {
271
+ const byName = new Map(declared.map((d) => [d.name, d]));
272
+ const values = {};
273
+ const omissions = [];
274
+ for (const [name, value] of Object.entries(raw)) {
275
+ const path = pathPrefix ? `${pathPrefix}.${name}` : name;
276
+ const decl = byName.get(name);
277
+ if (!decl) {
278
+ omissions.push({ path, reason: "filtered" });
279
+ continue;
280
+ }
281
+ const sensitivity = resolveSensitivity(decl);
282
+ if (sensitivity.classification !== "ordinary") {
283
+ omissions.push({ path, reason: "excluded", classification: sensitivity.classification });
284
+ continue;
285
+ }
286
+ if (!sensitivity.exportable) {
287
+ omissions.push({ path, reason: "excluded" });
288
+ continue;
289
+ }
290
+ const json = toJson(value);
291
+ if (json === void 0) continue;
292
+ values[name] = json;
293
+ }
294
+ return { values, omissions };
295
+ }
296
+ function createProjectedReads(source) {
297
+ return {
298
+ async values(signal) {
299
+ if (!source.surface || !source.declaration) return { values: {}, omissions: [] };
300
+ const raw = await source.surface.readValues(signal);
301
+ const screenValues = source.declaration.values;
302
+ return projectValues(raw, screenValues);
303
+ },
304
+ async item(signal) {
305
+ const item = source.item;
306
+ if (!item || !source.declaration) return null;
307
+ const type = source.declaration.itemTypes?.find((t) => t.name === item.itemType);
308
+ if (!type) return { values: {}, omissions: [{ path: item.itemType, reason: "filtered" }] };
309
+ const raw = await item.readValues(signal);
310
+ return projectValues(raw, type.values, item.itemType);
311
+ },
312
+ async live(signal) {
313
+ if (!source.surface?.readLive) return {};
314
+ return source.surface.readLive(signal);
315
+ }
316
+ };
317
+ }
318
+
319
+ // src/actions/run.ts
320
+ async function runAction(action, target, options) {
321
+ const { ports } = options;
322
+ if (action.requiresAuth && !target.auth.authenticated) {
323
+ if (!ports.auth) {
324
+ ports.diagnostics.capture(new Error(`requiresAuth action "${action.id}" ran signed out with no auth port`), {
325
+ area: "actions",
326
+ detail: { actionId: action.id }
327
+ });
328
+ return { status: "failed", error: new Error("sign-in unavailable") };
329
+ }
330
+ ports.auth.signInThenResume({ actionId: action.id, contentFingerprint: options.contentFingerprint ?? action.id });
331
+ return { status: "auth-pending" };
332
+ }
333
+ const reads = createProjectedReads(options.source ?? { surface: null, declaration: target.declaration, item: null });
334
+ const signal = options.signal ?? new AbortController().signal;
335
+ try {
336
+ const outcome = await action.run(target, { reads, ports, signal });
337
+ return { status: "ran", outcome };
338
+ } catch (error) {
339
+ ports.diagnostics.capture(error, { area: "actions", detail: { actionId: action.id, stage: "run" } });
340
+ const label = typeof action.label === "string" ? action.label : action.id;
341
+ ports.notify?.error(`${label} did not finish.`, "Try again; if it keeps failing, report it from the feedback button.");
342
+ return { status: "failed", error };
343
+ }
344
+ }
345
+
346
+ // src/react/context.ts
347
+ import * as React from "react";
348
+ var KEY = /* @__PURE__ */ Symbol.for("ai-matrx.alchemy.actions-context");
349
+ var AlchemyActionsContext = globalThis[KEY] ??= React.createContext(null);
350
+ function useAlchemyActions() {
351
+ const value = React.useContext(AlchemyActionsContext);
352
+ if (!value) throw new Error("Alchemy actions: render inside <AlchemyActionsProvider> (@ai-matrx/alchemy/react/host).");
353
+ return value;
354
+ }
355
+
356
+ // src/react/engine.tsx
357
+ import { jsx } from "react/jsx-runtime";
358
+ function useMenuEngine(target, options = {}) {
359
+ const { registry, ports } = useAlchemyActions();
360
+ const [resolved, setResolved] = React2.useState([]);
361
+ const [model, setModel] = React2.useState(null);
362
+ const [tick, setTick] = React2.useState(0);
363
+ const expanded = React2.useRef(/* @__PURE__ */ new Map());
364
+ const active = options.active ?? true;
365
+ const optionsRef = React2.useRef(options);
366
+ optionsRef.current = options;
367
+ React2.useEffect(() => registry.subscribe(() => setTick((t) => t + 1)), [registry]);
368
+ React2.useEffect(() => {
369
+ if (!target || !active) return;
370
+ let live = true;
371
+ void registry.resolve(target, optionsRef.current.restrict).then((list) => {
372
+ if (!live) return;
373
+ expanded.current.clear();
374
+ setResolved(list);
375
+ setModel(buildMenuModel(target, list, { ...ports.i18n ? { i18n: ports.i18n } : {}, content: optionsRef.current.content ?? null }));
376
+ });
377
+ return () => {
378
+ live = false;
379
+ };
380
+ }, [registry, ports, target, active, tick, options.revision]);
381
+ React2.useEffect(() => {
382
+ if (!target) return;
383
+ const offs = resolved.map((r) => r.action.subscribe?.(() => setTick((t) => t + 1), target)).filter((off) => typeof off === "function");
384
+ return () => offs.forEach((off) => off());
385
+ }, [resolved, target]);
386
+ const findAction = React2.useCallback(
387
+ (id) => resolved.find((r) => r.action.id === id)?.action ?? expanded.current.get(id),
388
+ [resolved]
389
+ );
390
+ const run = React2.useCallback(
391
+ async (nodeId) => {
392
+ if (!target || !model) return null;
393
+ const node = model.sections.map((s) => findNode(s.nodes, nodeId)).find(Boolean) ?? null;
394
+ const action = findAction(node && "actionId" in node && node.actionId ? node.actionId : nodeId);
395
+ if (!action) return null;
396
+ const o = optionsRef.current;
397
+ const result = await runAction(action, target, {
398
+ ports,
399
+ ...o.source ? { source: o.source } : {},
400
+ ...o.contentFingerprint ? { contentFingerprint: o.contentFingerprint } : {}
401
+ });
402
+ o.onRan?.(nodeId, result);
403
+ return result;
404
+ },
405
+ [target, model, findAction, ports]
406
+ );
407
+ const expand = React2.useCallback(
408
+ async (nodeId) => {
409
+ const action = findAction(nodeId);
410
+ if (!target || !action?.expand) return;
411
+ setModel((m) => m ? withExpandedChildren(m, nodeId, "loading") : m);
412
+ try {
413
+ const children = await action.expand(target, new AbortController().signal);
414
+ const nodes = children.map((child) => {
415
+ expanded.current.set(child.id, child);
416
+ return actionToNode({ action: child, eligibility: { status: "available" } }, target, ports.i18n ? { i18n: ports.i18n } : {});
417
+ });
418
+ setModel((m) => m ? withExpandedChildren(m, nodeId, nodes) : m);
419
+ } catch (error) {
420
+ ports.diagnostics.capture(error, { area: "menu", detail: { nodeId, stage: "expand" } });
421
+ setModel((m) => m ? withExpandedChildren(m, nodeId, []) : m);
422
+ }
423
+ },
424
+ [findAction, target, ports]
425
+ );
426
+ const icon = React2.useCallback(
427
+ (key, tone) => {
428
+ if (!key || !ports.icons) return null;
429
+ const Resolved = ports.icons.resolve(key);
430
+ return Resolved ? /* @__PURE__ */ jsx(Resolved, { className: `h-4 w-4 shrink-0 ${tone ?? ""}`, "aria-hidden": true }) : null;
431
+ },
432
+ [ports.icons]
433
+ );
434
+ return { model, resolved, run, expand, icon };
435
+ }
436
+
437
+ // src/react/dropdown-nodes.tsx
438
+ import * as React3 from "react";
439
+ import {
440
+ DropdownMenuCheckboxItem,
441
+ DropdownMenuItem,
442
+ DropdownMenuLabel,
443
+ DropdownMenuSeparator,
444
+ DropdownMenuSub,
445
+ DropdownMenuSubContent,
446
+ DropdownMenuSubTrigger,
447
+ Tooltip,
448
+ TooltipContent,
449
+ TooltipTrigger
450
+ } from "@ai-matrx/design-system";
451
+ import { Fragment as Fragment2, jsx as jsx2, jsxs } from "react/jsx-runtime";
452
+ function iconOf(node, engine) {
453
+ if (node.kind === "item" && node.stateIcon) {
454
+ const glyph = engine.icon(node.stateIcon.icon);
455
+ return node.stateIcon.spin ? /* @__PURE__ */ jsx2("span", { className: "inline-flex animate-spin", children: glyph }) : glyph;
456
+ }
457
+ if (!("icon" in node)) return null;
458
+ return engine.icon(node.icon, node.kind === "item" ? node.iconTone : void 0);
459
+ }
460
+ function LeafButton({
461
+ node,
462
+ engine,
463
+ showLabel
464
+ }) {
465
+ if (node.kind !== "item" && node.kind !== "checkbox" && node.kind !== "link") return null;
466
+ const unavailable = node.kind === "item" ? node.unavailable : void 0;
467
+ const pressed = node.kind === "item" ? node.pressed : node.kind === "checkbox" ? node.checked : void 0;
468
+ const glyph = iconOf(node, engine);
469
+ const button = /* @__PURE__ */ jsxs(
470
+ "button",
471
+ {
472
+ type: "button",
473
+ "data-alchemy-node": node.id,
474
+ "aria-label": node.label,
475
+ "aria-disabled": unavailable ? true : void 0,
476
+ "aria-pressed": pressed,
477
+ onMouseDown: (e) => {
478
+ if (node.kind === "item" && node.preserveSelection) e.preventDefault();
479
+ },
480
+ onClick: () => {
481
+ if (unavailable) return;
482
+ if (node.kind === "link") {
483
+ window.location.assign(node.href);
484
+ return;
485
+ }
486
+ void engine.run(node.id);
487
+ },
488
+ className: `inline-flex h-7 min-w-7 items-center justify-center gap-1.5 rounded-md px-1.5 text-sm text-muted-foreground hover:bg-accent hover:text-foreground ${unavailable ? "cursor-default opacity-50 hover:bg-transparent" : ""} ${pressed ? "text-foreground" : ""}`,
489
+ children: [
490
+ glyph,
491
+ showLabel || !glyph ? /* @__PURE__ */ jsx2("span", { children: node.label }) : null
492
+ ]
493
+ }
494
+ );
495
+ return /* @__PURE__ */ jsxs(Tooltip, { children: [
496
+ /* @__PURE__ */ jsx2(TooltipTrigger, { asChild: true, children: button }),
497
+ /* @__PURE__ */ jsx2(TooltipContent, { children: unavailable ? unavailable.sentence : node.label })
498
+ ] });
499
+ }
500
+
501
+ // src/react/sheet.tsx
502
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
503
+ var ROW = "flex min-h-11 w-full items-center gap-3 rounded-md px-3 text-left text-sm hover:bg-accent";
504
+ function Row({ node, engine, onDrill }) {
505
+ const glyph = "icon" in node ? engine.icon(node.icon) : null;
506
+ switch (node.kind) {
507
+ case "separator":
508
+ return /* @__PURE__ */ jsx3("div", { role: "separator", className: "my-1 h-px bg-border" });
509
+ case "label":
510
+ return /* @__PURE__ */ jsx3("div", { className: "px-3 pt-2 text-xs font-medium text-muted-foreground", children: node.label });
511
+ case "submenu":
512
+ return /* @__PURE__ */ jsxs2(
513
+ "button",
514
+ {
515
+ type: "button",
516
+ "data-alchemy-node": node.id,
517
+ className: ROW,
518
+ onClick: () => {
519
+ if (node.lazy && !node.loading) void engine.expand(node.id);
520
+ onDrill(node.id);
521
+ },
522
+ children: [
523
+ glyph,
524
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 truncate", children: node.label }),
525
+ /* @__PURE__ */ jsx3("span", { "aria-hidden": true, className: "text-muted-foreground", children: "\u203A" })
526
+ ]
527
+ }
528
+ );
529
+ case "link":
530
+ return /* @__PURE__ */ jsxs2("a", { "data-alchemy-node": node.id, href: node.href, target: node.target, className: ROW, children: [
531
+ glyph,
532
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 truncate", children: node.label })
533
+ ] });
534
+ case "checkbox":
535
+ return /* @__PURE__ */ jsxs2("button", { type: "button", role: "menuitemcheckbox", "aria-checked": node.checked, "data-alchemy-node": node.id, className: ROW, onClick: () => void engine.run(node.id), children: [
536
+ glyph,
537
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 truncate", children: node.label }),
538
+ node.checked ? /* @__PURE__ */ jsx3("span", { "aria-hidden": true, children: "\u2713" }) : null
539
+ ] });
540
+ case "item":
541
+ return /* @__PURE__ */ jsxs2(
542
+ "button",
543
+ {
544
+ type: "button",
545
+ "data-alchemy-node": node.id,
546
+ "aria-disabled": node.unavailable ? true : void 0,
547
+ title: node.unavailable?.sentence,
548
+ className: `${ROW} ${node.unavailable ? "opacity-50" : ""} ${node.destructive ? "text-destructive" : ""}`,
549
+ onClick: () => {
550
+ if (!node.unavailable) void engine.run(node.id);
551
+ },
552
+ children: [
553
+ glyph,
554
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 truncate", children: node.label }),
555
+ node.unavailable ? /* @__PURE__ */ jsx3("span", { className: "text-xs text-muted-foreground", children: node.unavailable.sentence }) : null
556
+ ]
557
+ }
558
+ );
559
+ }
560
+ }
561
+ function ActionSheet({ target, open, onOpenChange, title = "Actions", ...options }) {
562
+ const engine = useMenuEngine(target, {
563
+ ...options,
564
+ active: open,
565
+ onRan: (id, r) => {
566
+ options.onRan?.(id, r);
567
+ onOpenChange(false);
568
+ }
569
+ });
570
+ const [path, setPath] = React4.useState([]);
571
+ React4.useEffect(() => {
572
+ if (!open) setPath([]);
573
+ }, [open]);
574
+ const arranged = engine.model ? sheet(engine.model) : null;
575
+ let nodes = arranged?.root ?? [];
576
+ let heading = title;
577
+ for (const id of path) {
578
+ const hit = findNode(nodes, id);
579
+ if (hit?.kind !== "submenu") break;
580
+ nodes = hit.children;
581
+ heading = hit.label;
582
+ }
583
+ const current = path.length ? findNode(arranged?.root ?? [], path[path.length - 1] ?? "") : null;
584
+ return /* @__PURE__ */ jsx3(BottomSheet, { open, onOpenChange, title: heading, children: /* @__PURE__ */ jsx3(TooltipProvider, { children: /* @__PURE__ */ jsxs2("div", { "data-alchemy-layout": "sheet", className: "flex flex-col gap-0.5 overflow-y-auto px-2 pb-4", style: { maxHeight: "70dvh" }, children: [
585
+ path.length ? /* @__PURE__ */ jsx3("button", { type: "button", className: `${ROW} text-muted-foreground`, onClick: () => setPath((p) => p.slice(0, -1)), children: "\u2039 Back" }) : arranged?.strip.length ? /* @__PURE__ */ jsx3("div", { role: "toolbar", "aria-label": "Clipboard", className: "flex items-center gap-1 px-1 pb-1", children: arranged.strip.map((n) => /* @__PURE__ */ jsx3(LeafButton, { node: n, engine, showLabel: true }, n.id)) }) : null,
586
+ !arranged ? /* @__PURE__ */ jsx3("div", { className: "px-3 py-2 text-sm text-muted-foreground", children: "Loading\u2026" }) : null,
587
+ current?.kind === "submenu" && current.loading ? /* @__PURE__ */ jsx3("div", { className: "px-3 py-2 text-sm text-muted-foreground", children: "Loading\u2026" }) : null,
588
+ current?.kind === "submenu" && !current.loading && !current.lazy && current.children.length === 0 ? /* @__PURE__ */ jsx3("div", { className: "px-3 py-2 text-sm text-muted-foreground", children: current.emptyLabel ?? "Nothing here yet" }) : null,
589
+ nodes.map((node) => /* @__PURE__ */ jsx3(Row, { node, engine, onDrill: (id) => setPath((p) => [...p, id]) }, node.id))
590
+ ] }) }) });
591
+ }
592
+ export {
593
+ ActionSheet
594
+ };
595
+ //# sourceMappingURL=react-sheet.js.map