@almadar/ui 1.0.1 → 1.0.11

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.
@@ -1,2267 +1,6 @@
1
- import * as React4 from 'react';
2
- import { createContext, useCallback, useState, useEffect, useMemo, useContext, useRef, useSyncExternalStore } from 'react';
3
- import 'react/jsx-runtime';
4
-
5
- var __typeError = (msg) => {
6
- throw TypeError(msg);
7
- };
8
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
9
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
10
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
11
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
12
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
13
- function useOrbitalHistory(options) {
14
- const { appId, authToken, userId, onHistoryChange, onRevertSuccess } = options;
15
- const getHeaders2 = useCallback(() => {
16
- const headers = {
17
- "Content-Type": "application/json"
18
- };
19
- if (authToken) {
20
- headers["Authorization"] = `Bearer ${authToken}`;
21
- }
22
- if (userId) {
23
- headers["x-user-id"] = userId;
24
- }
25
- return headers;
26
- }, [authToken, userId]);
27
- const [timeline, setTimeline] = useState([]);
28
- const [currentVersion, setCurrentVersion] = useState(1);
29
- const [isLoading, setIsLoading] = useState(false);
30
- const [error, setError] = useState(null);
31
- const refresh = useCallback(async () => {
32
- if (!appId) return;
33
- setIsLoading(true);
34
- setError(null);
35
- try {
36
- const headers = getHeaders2();
37
- const [changesetsRes, snapshotsRes] = await Promise.all([
38
- fetch(`/api/graphs/${appId}/history/changesets`, { headers }),
39
- fetch(`/api/graphs/${appId}/history/snapshots`, { headers })
40
- ]);
41
- if (!changesetsRes.ok) {
42
- throw new Error(`Failed to fetch changesets: ${changesetsRes.status}`);
43
- }
44
- if (!snapshotsRes.ok) {
45
- throw new Error(`Failed to fetch snapshots: ${snapshotsRes.status}`);
46
- }
47
- const changesetsData = await changesetsRes.json();
48
- const snapshotsData = await snapshotsRes.json();
49
- const changesetItems = (changesetsData.changesets || []).map((cs) => ({
50
- id: cs.id,
51
- type: "changeset",
52
- version: cs.version,
53
- timestamp: cs.timestamp,
54
- description: `Version ${cs.version}`,
55
- source: cs.source,
56
- summary: cs.summary
57
- }));
58
- const snapshotItems = (snapshotsData.snapshots || []).map((snap) => ({
59
- id: snap.id,
60
- type: "snapshot",
61
- version: snap.version,
62
- timestamp: snap.timestamp,
63
- description: snap.reason || `Snapshot v${snap.version}`,
64
- reason: snap.reason
65
- }));
66
- const mergedTimeline = [...changesetItems, ...snapshotItems].sort(
67
- (a, b) => b.timestamp - a.timestamp
68
- );
69
- setTimeline(mergedTimeline);
70
- if (mergedTimeline.length > 0) {
71
- setCurrentVersion(mergedTimeline[0].version);
72
- }
73
- } catch (err) {
74
- console.error("[useOrbitalHistory] Failed to load history:", err);
75
- setError(err instanceof Error ? err.message : "Failed to load history");
76
- } finally {
77
- setIsLoading(false);
78
- }
79
- }, [appId, getHeaders2]);
80
- const revertToSnapshot = useCallback(async (snapshotId) => {
81
- if (!appId) {
82
- return { success: false, error: "No app ID provided" };
83
- }
84
- try {
85
- const response = await fetch(`/api/graphs/${appId}/history/revert/${snapshotId}`, {
86
- method: "POST",
87
- headers: getHeaders2()
88
- });
89
- if (!response.ok) {
90
- const errorData = await response.json().catch(() => ({}));
91
- throw new Error(errorData.error || `Failed to revert: ${response.status}`);
92
- }
93
- const data = await response.json();
94
- if (data.success && data.schema) {
95
- await refresh();
96
- onRevertSuccess?.(data.schema);
97
- return {
98
- success: true,
99
- restoredSchema: data.schema
100
- };
101
- }
102
- return {
103
- success: false,
104
- error: data.error || "Unknown error during revert"
105
- };
106
- } catch (err) {
107
- console.error("[useOrbitalHistory] Failed to revert:", err);
108
- return {
109
- success: false,
110
- error: err instanceof Error ? err.message : "Failed to revert"
111
- };
112
- }
113
- }, [appId, getHeaders2, refresh, onRevertSuccess]);
114
- useEffect(() => {
115
- if (appId && authToken && userId) {
116
- refresh();
117
- }
118
- }, [appId, authToken, userId]);
119
- useEffect(() => {
120
- onHistoryChange?.(timeline);
121
- }, [timeline]);
122
- return {
123
- timeline,
124
- currentVersion,
125
- isLoading,
126
- error,
127
- revertToSnapshot,
128
- refresh
129
- };
130
- }
131
- function useFileSystem() {
132
- const [status, setStatus] = useState("idle");
133
- const [error, setError] = useState(null);
134
- const [isLoading, setIsLoading] = useState(false);
135
- const [files, setFiles] = useState([]);
136
- const [selectedFile, setSelectedFile] = useState(null);
137
- const [selectedPath, setSelectedPath] = useState(null);
138
- const [previewUrl, setPreviewUrl] = useState(null);
139
- const [fileContents, setFileContents] = useState(/* @__PURE__ */ new Map());
140
- const boot = useCallback(async () => {
141
- setStatus("booting");
142
- setError(null);
143
- setIsLoading(true);
144
- try {
145
- console.log("[useFileSystem] Booting WebContainer...");
146
- await new Promise((resolve) => setTimeout(resolve, 100));
147
- setStatus("ready");
148
- } catch (err) {
149
- setError(err instanceof Error ? err.message : "Failed to boot");
150
- setStatus("error");
151
- } finally {
152
- setIsLoading(false);
153
- }
154
- }, []);
155
- const mountFiles = useCallback(async (filesToMount) => {
156
- setIsLoading(true);
157
- try {
158
- let filesArray;
159
- if (Array.isArray(filesToMount)) {
160
- filesArray = filesToMount;
161
- } else {
162
- filesArray = [];
163
- const flattenTree = (obj, basePath = "") => {
164
- for (const [key, value] of Object.entries(obj)) {
165
- const path = basePath ? `${basePath}/${key}` : key;
166
- if (value && typeof value === "object" && "file" in value) {
167
- const fileObj = value;
168
- filesArray.push({ path, content: fileObj.file.contents || "" });
169
- } else if (value && typeof value === "object" && "directory" in value) {
170
- const dirObj = value;
171
- flattenTree(dirObj.directory, path);
172
- }
173
- }
174
- };
175
- flattenTree(filesToMount);
176
- }
177
- const newContents = /* @__PURE__ */ new Map();
178
- for (const file of filesArray) {
179
- newContents.set(file.path, file.content);
180
- }
181
- setFileContents(newContents);
182
- const newTree = [];
183
- for (const file of filesArray) {
184
- const parts = file.path.split("/").filter(Boolean);
185
- let current = newTree;
186
- for (let i = 0; i < parts.length; i++) {
187
- const part = parts[i];
188
- const isFile = i === parts.length - 1;
189
- const currentPath = "/" + parts.slice(0, i + 1).join("/");
190
- let node = current.find((n) => n.name === part);
191
- if (!node) {
192
- node = {
193
- path: currentPath,
194
- name: part,
195
- type: isFile ? "file" : "directory",
196
- children: isFile ? void 0 : []
197
- };
198
- current.push(node);
199
- }
200
- if (!isFile && node && node.children) {
201
- current = node.children;
202
- }
203
- }
204
- }
205
- setFiles(newTree);
206
- setStatus("running");
207
- } catch (err) {
208
- console.error("[useFileSystem] Failed to mount files:", err);
209
- } finally {
210
- setIsLoading(false);
211
- }
212
- }, []);
213
- const readFile = useCallback(async (path) => {
214
- return fileContents.get(path) || "";
215
- }, [fileContents]);
216
- const writeFile = useCallback(async (path, content) => {
217
- setFileContents((prev) => {
218
- const next = new Map(prev);
219
- next.set(path, content);
220
- return next;
221
- });
222
- }, []);
223
- const selectFile = useCallback(async (path) => {
224
- const content = fileContents.get(path) || "";
225
- const ext = path.split(".").pop()?.toLowerCase() || "";
226
- const languageMap = {
227
- ts: "typescript",
228
- tsx: "typescript",
229
- js: "javascript",
230
- jsx: "javascript",
231
- json: "json",
232
- md: "markdown",
233
- css: "css",
234
- html: "html",
235
- orb: "json"
236
- };
237
- setSelectedPath(path);
238
- setSelectedFile({
239
- path,
240
- content,
241
- language: languageMap[ext] || "plaintext",
242
- isDirty: false
243
- });
244
- }, [fileContents]);
245
- const updateContent = useCallback((pathOrContent, contentArg) => {
246
- const path = contentArg !== void 0 ? pathOrContent : selectedPath;
247
- const content = contentArg !== void 0 ? contentArg : pathOrContent;
248
- if (!path) {
249
- console.warn("[useFileSystem] updateContent called without path and no file selected");
250
- return;
251
- }
252
- setFileContents((prev) => {
253
- const next = new Map(prev);
254
- next.set(path, content);
255
- return next;
256
- });
257
- if (selectedPath === path) {
258
- setSelectedFile((prev) => prev ? { ...prev, content, isDirty: true } : null);
259
- }
260
- }, [selectedPath]);
261
- const updateSelectedContent = useCallback((content) => {
262
- setSelectedFile((prev) => prev ? { ...prev, content, isDirty: true } : null);
263
- }, []);
264
- const refreshTree = useCallback(async () => {
265
- console.log("[useFileSystem] Refreshing tree");
266
- }, []);
267
- const runCommand = useCallback(async (command) => {
268
- console.log("[useFileSystem] Running command:", command);
269
- return { exitCode: 0, output: "" };
270
- }, []);
271
- const startDevServer = useCallback(async () => {
272
- console.log("[useFileSystem] Starting dev server");
273
- setPreviewUrl("http://localhost:5173");
274
- }, []);
275
- return {
276
- status,
277
- error,
278
- isLoading,
279
- files,
280
- selectedFile,
281
- selectedPath,
282
- previewUrl,
283
- boot,
284
- mountFiles,
285
- readFile,
286
- writeFile,
287
- selectFile,
288
- updateContent,
289
- updateSelectedContent,
290
- refreshTree,
291
- runCommand,
292
- startDevServer
293
- };
294
- }
295
- var defaultManifest = {
296
- languages: {
297
- typescript: { extensions: [".ts", ".tsx"], icon: "ts", color: "#3178c6" },
298
- javascript: { extensions: [".js", ".jsx"], icon: "js", color: "#f7df1e" },
299
- json: { extensions: [".json", ".orb"], icon: "json", color: "#000000" },
300
- css: { extensions: [".css"], icon: "css", color: "#264de4" },
301
- html: { extensions: [".html"], icon: "html", color: "#e34c26" },
302
- markdown: { extensions: [".md", ".mdx"], icon: "md", color: "#083fa1" }
303
- },
304
- extensions: []
305
- };
306
- function useExtensions(options) {
307
- const { appId, loadOnMount = true } = options;
308
- const [extensions, setExtensions] = useState([]);
309
- const [manifest] = useState(defaultManifest);
310
- const [isLoading, setIsLoading] = useState(false);
311
- const [error, setError] = useState(null);
312
- const loadExtension = useCallback(async (extensionId) => {
313
- console.log("[useExtensions] Loading extension:", extensionId);
314
- }, []);
315
- const loadExtensions = useCallback(async () => {
316
- setIsLoading(true);
317
- setError(null);
318
- try {
319
- const defaultExtensions = [
320
- { id: "typescript", name: "TypeScript", language: "typescript", loaded: true },
321
- { id: "javascript", name: "JavaScript", language: "javascript", loaded: true },
322
- { id: "json", name: "JSON", language: "json", loaded: true },
323
- { id: "css", name: "CSS", language: "css", loaded: true },
324
- { id: "html", name: "HTML", language: "html", loaded: true },
325
- { id: "markdown", name: "Markdown", language: "markdown", loaded: true }
326
- ];
327
- setExtensions(defaultExtensions);
328
- } catch (err) {
329
- setError(err instanceof Error ? err.message : "Failed to load extensions");
330
- } finally {
331
- setIsLoading(false);
332
- }
333
- }, []);
334
- const getExtensionForFile = useCallback((filename) => {
335
- const ext = filename.split(".").pop()?.toLowerCase();
336
- if (!ext) return null;
337
- const languageMap = {
338
- ts: "typescript",
339
- tsx: "typescript",
340
- js: "javascript",
341
- jsx: "javascript",
342
- json: "json",
343
- md: "markdown",
344
- css: "css",
345
- html: "html",
346
- orb: "json"
347
- };
348
- const language = languageMap[ext];
349
- if (!language) return null;
350
- return extensions.find((e) => e.language === language) || null;
351
- }, [extensions]);
352
- useEffect(() => {
353
- if (!appId || !loadOnMount) return;
354
- const loadExtensions2 = async () => {
355
- setIsLoading(true);
356
- setError(null);
357
- try {
358
- const defaultExtensions = [
359
- { id: "typescript", name: "TypeScript", language: "typescript", loaded: true },
360
- { id: "javascript", name: "JavaScript", language: "javascript", loaded: true },
361
- { id: "json", name: "JSON", language: "json", loaded: true },
362
- { id: "css", name: "CSS", language: "css", loaded: true },
363
- { id: "html", name: "HTML", language: "html", loaded: true },
364
- { id: "markdown", name: "Markdown", language: "markdown", loaded: true }
365
- ];
366
- setExtensions(defaultExtensions);
367
- } catch (err) {
368
- setError(err instanceof Error ? err.message : "Failed to load extensions");
369
- } finally {
370
- setIsLoading(false);
371
- }
372
- };
373
- loadExtensions2();
374
- }, [appId, loadOnMount]);
375
- return {
376
- extensions,
377
- manifest,
378
- isLoading,
379
- error,
380
- loadExtension,
381
- loadExtensions,
382
- getExtensionForFile
383
- };
384
- }
385
- function useFileEditor(options) {
386
- const { extensions, fileSystem, onSchemaUpdate } = options;
387
- const [openFiles, setOpenFiles] = useState([]);
388
- const [activeFilePath, setActiveFilePath] = useState(null);
389
- const [isSaving, setIsSaving] = useState(false);
390
- const activeFile = openFiles.find((f) => f.path === activeFilePath) || null;
391
- const openFile = useCallback(async (path) => {
392
- const existing = openFiles.find((f) => f.path === path);
393
- if (existing) {
394
- setActiveFilePath(path);
395
- return;
396
- }
397
- try {
398
- const content = await fileSystem.readFile(path);
399
- const ext = extensions.getExtensionForFile(path);
400
- const newFile = {
401
- path,
402
- content,
403
- isDirty: false,
404
- language: ext?.language
405
- };
406
- setOpenFiles((prev) => [...prev, newFile]);
407
- setActiveFilePath(path);
408
- } catch (err) {
409
- console.error("[useFileEditor] Failed to open file:", err);
410
- }
411
- }, [openFiles, fileSystem, extensions]);
412
- const closeFile = useCallback((path) => {
413
- setOpenFiles((prev) => prev.filter((f) => f.path !== path));
414
- if (activeFilePath === path) {
415
- const remaining = openFiles.filter((f) => f.path !== path);
416
- setActiveFilePath(remaining.length > 0 ? remaining[0].path : null);
417
- }
418
- }, [activeFilePath, openFiles]);
419
- const setActiveFile = useCallback((path) => {
420
- setActiveFilePath(path);
421
- }, []);
422
- const updateFileContent = useCallback((path, content) => {
423
- setOpenFiles(
424
- (prev) => prev.map(
425
- (f) => f.path === path ? { ...f, content, isDirty: true } : f
426
- )
427
- );
428
- }, []);
429
- const handleFileEdit = useCallback(async (path, content) => {
430
- try {
431
- await fileSystem.writeFile(path, content);
432
- let action = "saved";
433
- if (path.endsWith(".orb") || path.endsWith("schema.json")) {
434
- try {
435
- const schema = JSON.parse(content);
436
- await onSchemaUpdate?.(schema);
437
- action = "updated_schema";
438
- } catch {
439
- }
440
- } else if (path.includes("/extensions/")) {
441
- action = path.endsWith(".new") ? "converted_extension" : "saved_extension";
442
- }
443
- return { success: true, action };
444
- } catch (err) {
445
- return {
446
- success: false,
447
- error: err instanceof Error ? err.message : "Failed to save file"
448
- };
449
- }
450
- }, [fileSystem, onSchemaUpdate]);
451
- const saveFile = useCallback(async (path) => {
452
- const file = openFiles.find((f) => f.path === path);
453
- if (!file) return;
454
- setIsSaving(true);
455
- try {
456
- await fileSystem.writeFile(path, file.content);
457
- setOpenFiles(
458
- (prev) => prev.map(
459
- (f) => f.path === path ? { ...f, isDirty: false } : f
460
- )
461
- );
462
- if (path.endsWith(".orb") || path.endsWith("schema.json")) {
463
- try {
464
- const schema = JSON.parse(file.content);
465
- await onSchemaUpdate?.(schema);
466
- } catch {
467
- }
468
- }
469
- } catch (err) {
470
- console.error("[useFileEditor] Failed to save file:", err);
471
- } finally {
472
- setIsSaving(false);
473
- }
474
- }, [openFiles, fileSystem, onSchemaUpdate]);
475
- const saveAllFiles = useCallback(async () => {
476
- setIsSaving(true);
477
- try {
478
- const dirtyFiles = openFiles.filter((f) => f.isDirty);
479
- for (const file of dirtyFiles) {
480
- await saveFile(file.path);
481
- }
482
- } finally {
483
- setIsSaving(false);
484
- }
485
- }, [openFiles, saveFile]);
486
- return {
487
- openFiles,
488
- activeFile,
489
- isSaving,
490
- openFile,
491
- closeFile,
492
- setActiveFile,
493
- updateFileContent,
494
- handleFileEdit,
495
- saveFile,
496
- saveAllFiles
497
- };
498
- }
499
- function useCompile() {
500
- const [isCompiling, setIsCompiling] = useState(false);
501
- const [stage, setStage] = useState("idle");
502
- const [lastResult, setLastResult] = useState(null);
503
- const [error, setError] = useState(null);
504
- const compileSchema = useCallback(async (schema) => {
505
- setIsCompiling(true);
506
- setStage("compiling");
507
- setError(null);
508
- try {
509
- console.log("[useCompile] Compiling schema:", schema.name);
510
- const result = {
511
- success: true,
512
- files: []
513
- };
514
- setLastResult(result);
515
- setStage("done");
516
- return result;
517
- } catch (err) {
518
- const errorMessage = err instanceof Error ? err.message : "Compilation failed";
519
- setError(errorMessage);
520
- setStage("error");
521
- setLastResult({ success: false, errors: [errorMessage] });
522
- return null;
523
- } finally {
524
- setIsCompiling(false);
525
- }
526
- }, []);
527
- return {
528
- isCompiling,
529
- stage,
530
- lastResult,
531
- error,
532
- compileSchema
533
- };
534
- }
535
- function usePreview(options) {
536
- const [previewUrl, setPreviewUrl] = useState(null);
537
- const [isLoading, setIsLoading] = useState(!!options?.appId);
538
- const [error, setError] = useState(null);
539
- const [loadError, setLoadError] = useState(null);
540
- const [app, setApp] = useState(null);
541
- const [isFullscreen, setIsFullscreen] = useState(false);
542
- const [isExecutingEvent, setIsExecutingEvent] = useState(false);
543
- const [errorToast, setErrorToast] = useState(null);
544
- const [currentStateName, setCurrentStateName] = useState(null);
545
- const [notificationsList, setNotificationsList] = useState([]);
546
- const [isPanelOpen, setIsPanelOpen] = useState(false);
547
- const notifications = useMemo(() => ({
548
- notifications: notificationsList,
549
- isPanelOpen,
550
- closePanel: () => setIsPanelOpen(false),
551
- dismissNotification: (id) => {
552
- setNotificationsList((prev) => prev.filter((n) => n.id !== id));
553
- },
554
- markAsRead: (id) => {
555
- setNotificationsList(
556
- (prev) => prev.map((n) => n.id === id ? { ...n, read: true } : n)
557
- );
558
- },
559
- clearAll: () => setNotificationsList([])
560
- }), [notificationsList, isPanelOpen]);
561
- useEffect(() => {
562
- const appId = options?.appId;
563
- if (!appId) {
564
- setApp(null);
565
- setIsLoading(false);
566
- return;
567
- }
568
- console.log("[usePreview] Setting up preview for app:", appId);
569
- setPreviewUrl(`/api/orbitals/${appId}`);
570
- setIsLoading(false);
571
- }, [options?.appId]);
572
- const startPreview = useCallback(async () => {
573
- console.log("[usePreview] startPreview called");
574
- }, []);
575
- const stopPreview = useCallback(async () => {
576
- setIsLoading(true);
577
- try {
578
- console.log("[usePreview] Stopping preview server...");
579
- setPreviewUrl(null);
580
- setApp(null);
581
- } finally {
582
- setIsLoading(false);
583
- }
584
- }, []);
585
- const refresh = useCallback(async () => {
586
- if (!previewUrl) return;
587
- console.log("[usePreview] Refreshing preview...");
588
- setPreviewUrl(`${previewUrl.split("?")[0]}?t=${Date.now()}`);
589
- }, [previewUrl]);
590
- const handleRefresh = useCallback(async () => {
591
- console.log("[usePreview] Handle refresh...");
592
- await refresh();
593
- }, [refresh]);
594
- const handleReset = useCallback(async () => {
595
- console.log("[usePreview] Resetting preview...");
596
- setError(null);
597
- setLoadError(null);
598
- setErrorToast(null);
599
- setIsExecutingEvent(false);
600
- setCurrentStateName(null);
601
- }, []);
602
- const toggleFullscreen = useCallback(() => {
603
- setIsFullscreen((prev) => !prev);
604
- }, []);
605
- const dismissErrorToast = useCallback(() => {
606
- setErrorToast(null);
607
- }, []);
608
- return {
609
- previewUrl,
610
- isLoading,
611
- error,
612
- loadError,
613
- app,
614
- isFullscreen,
615
- isExecutingEvent,
616
- errorToast,
617
- currentStateName,
618
- notifications,
619
- startPreview,
620
- stopPreview,
621
- refresh,
622
- handleRefresh,
623
- handleReset,
624
- toggleFullscreen,
625
- setErrorToast,
626
- dismissErrorToast
627
- };
628
- }
629
- function useAgentChat(options) {
630
- const [messages, setMessages] = useState([]);
631
- const [status, setStatus] = useState("idle");
632
- const [activities, setActivities] = useState([]);
633
- const [todos, setTodos] = useState([]);
634
- const [schemaDiffs, setSchemaDiffs] = useState([]);
635
- const [isLoading, setIsLoading] = useState(false);
636
- const [error, setError] = useState(null);
637
- const [threadId] = useState(null);
638
- const [interrupt, setInterrupt] = useState(null);
639
- const sendMessage = useCallback(async (content) => {
640
- setIsLoading(true);
641
- setStatus("running");
642
- setError(null);
643
- try {
644
- const userMessage = {
645
- id: Date.now().toString(),
646
- role: "user",
647
- content,
648
- timestamp: Date.now()
649
- };
650
- setMessages((prev) => [...prev, userMessage]);
651
- console.log("[useAgentChat] Sending message:", content);
652
- const assistantMessage = {
653
- id: (Date.now() + 1).toString(),
654
- role: "assistant",
655
- content: "Agent chat is not yet implemented.",
656
- timestamp: Date.now()
657
- };
658
- setMessages((prev) => [...prev, assistantMessage]);
659
- setStatus("idle");
660
- options?.onComplete?.();
661
- } catch (err) {
662
- setError(err instanceof Error ? err.message : "Failed to send message");
663
- setStatus("error");
664
- } finally {
665
- setIsLoading(false);
666
- }
667
- }, [options]);
668
- const startGeneration = useCallback(async (skill, prompt, genOptions) => {
669
- setStatus("running");
670
- setIsLoading(true);
671
- setError(null);
672
- const skillName = Array.isArray(skill) ? skill[0] : skill;
673
- try {
674
- console.log("[useAgentChat] Starting generation:", skillName, prompt, genOptions);
675
- await new Promise((resolve) => setTimeout(resolve, 100));
676
- setStatus("complete");
677
- options?.onComplete?.();
678
- } catch (err) {
679
- setError(err instanceof Error ? err.message : "Generation failed");
680
- setStatus("error");
681
- } finally {
682
- setIsLoading(false);
683
- }
684
- }, [options]);
685
- const continueConversation = useCallback(async (message) => {
686
- console.log("[useAgentChat] Continue conversation", message);
687
- }, []);
688
- const resumeWithDecision = useCallback(async (decisions) => {
689
- console.log("[useAgentChat] Resume with decision:", decisions);
690
- setInterrupt(null);
691
- }, []);
692
- const cancel = useCallback(() => {
693
- setStatus("idle");
694
- setIsLoading(false);
695
- setInterrupt(null);
696
- }, []);
697
- const clearMessages = useCallback(() => {
698
- setMessages([]);
699
- }, []);
700
- const clearHistory = useCallback(() => {
701
- setMessages([]);
702
- setActivities([]);
703
- setTodos([]);
704
- setSchemaDiffs([]);
705
- setError(null);
706
- }, []);
707
- return {
708
- messages,
709
- status,
710
- activities,
711
- todos,
712
- schemaDiffs,
713
- isLoading,
714
- error,
715
- threadId,
716
- interrupt,
717
- sendMessage,
718
- startGeneration,
719
- continueConversation,
720
- resumeWithDecision,
721
- cancel,
722
- clearMessages,
723
- clearHistory
724
- };
725
- }
726
- function useValidation() {
727
- const [result, setResult] = useState(null);
728
- const [isValidating, setIsValidating] = useState(false);
729
- const [error, setError] = useState(null);
730
- const [stage, setStage] = useState("idle");
731
- const [isFixing, setIsFixing] = useState(false);
732
- const [progressMessage, setProgressMessage] = useState(null);
733
- const validate = useCallback(async (appId) => {
734
- setIsValidating(true);
735
- setError(null);
736
- setStage("validating");
737
- setProgressMessage("Validating schema...");
738
- try {
739
- console.log("[useValidation] Validating app:", appId);
740
- const validationResult = {
741
- valid: true,
742
- errors: [],
743
- warnings: []
744
- };
745
- setResult(validationResult);
746
- setStage("complete");
747
- setProgressMessage(null);
748
- return validationResult;
749
- } catch (err) {
750
- const errorMessage = err instanceof Error ? err.message : "Validation failed";
751
- setError(errorMessage);
752
- const failedResult = {
753
- valid: false,
754
- errors: [{ code: "VALIDATION_ERROR", message: errorMessage, severity: "error" }],
755
- warnings: []
756
- };
757
- setResult(failedResult);
758
- setStage("complete");
759
- setProgressMessage(null);
760
- return failedResult;
761
- } finally {
762
- setIsValidating(false);
763
- }
764
- }, []);
765
- const clearResult = useCallback(() => {
766
- setResult(null);
767
- setError(null);
768
- }, []);
769
- const reset = useCallback(() => {
770
- setResult(null);
771
- setError(null);
772
- setStage("idle");
773
- setIsFixing(false);
774
- setProgressMessage(null);
775
- setIsValidating(false);
776
- }, []);
777
- return {
778
- result,
779
- isValidating,
780
- error,
781
- stage,
782
- isFixing,
783
- progressMessage,
784
- errors: result?.errors ?? [],
785
- warnings: result?.warnings ?? [],
786
- isValid: result?.valid ?? false,
787
- validate,
788
- clearResult,
789
- reset
790
- };
791
- }
792
- function useDeepAgentGeneration() {
793
- const [requests, setRequests] = useState([]);
794
- const [currentRequest, setCurrentRequest] = useState(null);
795
- const [isGenerating, setIsGenerating] = useState(false);
796
- const [isLoading, setIsLoading] = useState(false);
797
- const [isComplete, setIsComplete] = useState(false);
798
- const [progress, setProgress] = useState({ stage: "idle", percent: 0, message: "" });
799
- const [error, setError] = useState(null);
800
- const [interrupt, setInterrupt] = useState(null);
801
- const generate = useCallback(async (prompt) => {
802
- setIsGenerating(true);
803
- setIsLoading(true);
804
- setIsComplete(false);
805
- setError(null);
806
- setProgress({ stage: "starting", percent: 0, message: "Starting generation..." });
807
- const request = {
808
- id: Date.now().toString(),
809
- prompt,
810
- status: "running"
811
- };
812
- setCurrentRequest(request);
813
- setRequests((prev) => [...prev, request]);
814
- try {
815
- console.log("[useDeepAgentGeneration] Generating from prompt:", prompt);
816
- await new Promise((resolve) => setTimeout(resolve, 100));
817
- request.status = "completed";
818
- setCurrentRequest(request);
819
- setIsComplete(true);
820
- setProgress({ stage: "complete", percent: 100, message: "Generation complete" });
821
- return null;
822
- } catch (err) {
823
- const errorMessage = err instanceof Error ? err.message : "Generation failed";
824
- setError(errorMessage);
825
- request.status = "failed";
826
- request.error = errorMessage;
827
- setCurrentRequest(request);
828
- return null;
829
- } finally {
830
- setIsGenerating(false);
831
- setIsLoading(false);
832
- }
833
- }, []);
834
- const startGeneration = useCallback(async (skill, prompt, _options) => {
835
- console.log("[useDeepAgentGeneration] Starting generation with skill:", skill);
836
- await generate(prompt);
837
- }, [generate]);
838
- const cancelGeneration = useCallback(() => {
839
- if (currentRequest) {
840
- currentRequest.status = "failed";
841
- currentRequest.error = "Cancelled by user";
842
- setCurrentRequest(null);
843
- }
844
- setIsGenerating(false);
845
- setIsLoading(false);
846
- setIsComplete(false);
847
- setProgress({ stage: "idle", percent: 0, message: "" });
848
- }, [currentRequest]);
849
- const clearRequests = useCallback(() => {
850
- setRequests([]);
851
- setCurrentRequest(null);
852
- setError(null);
853
- setProgress({ stage: "idle", percent: 0, message: "" });
854
- setIsComplete(false);
855
- }, []);
856
- const submitInterruptDecisions = useCallback((decisions) => {
857
- console.log("[useDeepAgentGeneration] Submitting interrupt decisions:", decisions);
858
- setInterrupt(null);
859
- }, []);
860
- return {
861
- requests,
862
- currentRequest,
863
- isGenerating,
864
- isLoading,
865
- isComplete,
866
- progress,
867
- error,
868
- interrupt,
869
- generate,
870
- startGeneration,
871
- cancelGeneration,
872
- clearRequests,
873
- submitInterruptDecisions
874
- };
875
- }
876
- var EventBusContext = createContext(null);
877
-
878
- // hooks/useEventBus.ts
879
- function getGlobalEventBus() {
880
- if (typeof window !== "undefined") {
881
- return window.__kflowEventBus ?? null;
882
- }
883
- return null;
884
- }
885
- var fallbackListeners = /* @__PURE__ */ new Map();
886
- var fallbackEventBus = {
887
- emit: (type, payload) => {
888
- const event = {
889
- type,
890
- payload,
891
- timestamp: Date.now()
892
- };
893
- const handlers = fallbackListeners.get(type);
894
- if (handlers) {
895
- handlers.forEach((handler) => {
896
- try {
897
- handler(event);
898
- } catch (error) {
899
- console.error(`[EventBus] Error in listener for '${type}':`, error);
900
- }
901
- });
902
- }
903
- },
904
- on: (type, listener) => {
905
- if (!fallbackListeners.has(type)) {
906
- fallbackListeners.set(type, /* @__PURE__ */ new Set());
907
- }
908
- fallbackListeners.get(type).add(listener);
909
- return () => {
910
- const handlers = fallbackListeners.get(type);
911
- if (handlers) {
912
- handlers.delete(listener);
913
- if (handlers.size === 0) {
914
- fallbackListeners.delete(type);
915
- }
916
- }
917
- };
918
- },
919
- once: (type, listener) => {
920
- const wrappedListener = (event) => {
921
- fallbackListeners.get(type)?.delete(wrappedListener);
922
- listener(event);
923
- };
924
- return fallbackEventBus.on(type, wrappedListener);
925
- },
926
- hasListeners: (type) => {
927
- const handlers = fallbackListeners.get(type);
928
- return handlers !== void 0 && handlers.size > 0;
929
- }
930
- };
931
- function useEventBus() {
932
- const context = useContext(EventBusContext);
933
- return context ?? getGlobalEventBus() ?? fallbackEventBus;
934
- }
935
- function useEventListener(event, handler) {
936
- const eventBus = useEventBus();
937
- const handlerRef = useRef(handler);
938
- handlerRef.current = handler;
939
- useEffect(() => {
940
- const wrappedHandler = (evt) => {
941
- handlerRef.current(evt);
942
- };
943
- return eventBus.on(event, wrappedHandler);
944
- }, [event, eventBus]);
945
- }
946
- function useEmitEvent() {
947
- const eventBus = useEventBus();
948
- return useCallback(
949
- (type, payload) => {
950
- eventBus.emit(type, payload);
951
- },
952
- [eventBus]
953
- );
954
- }
955
- var DEFAULT_SLOTS = {
956
- main: null,
957
- sidebar: null,
958
- modal: null,
959
- drawer: null,
960
- overlay: null,
961
- center: null,
962
- toast: null,
963
- "hud-top": null,
964
- "hud-bottom": null,
965
- floating: null
966
- };
967
- var idCounter = 0;
968
- function generateId() {
969
- return `slot-content-${++idCounter}-${Date.now()}`;
970
- }
971
- function useUISlotManager() {
972
- const [slots, setSlots] = useState(DEFAULT_SLOTS);
973
- const subscribersRef = useRef(/* @__PURE__ */ new Set());
974
- const timersRef = useRef(/* @__PURE__ */ new Map());
975
- useEffect(() => {
976
- return () => {
977
- timersRef.current.forEach((timer) => clearTimeout(timer));
978
- timersRef.current.clear();
979
- };
980
- }, []);
981
- const notifySubscribers = useCallback((slot, content) => {
982
- subscribersRef.current.forEach((callback) => {
983
- try {
984
- callback(slot, content);
985
- } catch (error) {
986
- console.error("[UISlots] Subscriber error:", error);
987
- }
988
- });
989
- }, []);
990
- const render = useCallback((config) => {
991
- const id = generateId();
992
- const content = {
993
- id,
994
- pattern: config.pattern,
995
- props: config.props ?? {},
996
- priority: config.priority ?? 0,
997
- animation: config.animation ?? "fade",
998
- onDismiss: config.onDismiss,
999
- sourceTrait: config.sourceTrait
1000
- };
1001
- if (config.autoDismissMs && config.autoDismissMs > 0) {
1002
- content.autoDismissAt = Date.now() + config.autoDismissMs;
1003
- const timer = setTimeout(() => {
1004
- setSlots((prev) => {
1005
- if (prev[config.target]?.id === id) {
1006
- content.onDismiss?.();
1007
- notifySubscribers(config.target, null);
1008
- return { ...prev, [config.target]: null };
1009
- }
1010
- return prev;
1011
- });
1012
- timersRef.current.delete(id);
1013
- }, config.autoDismissMs);
1014
- timersRef.current.set(id, timer);
1015
- }
1016
- setSlots((prev) => {
1017
- const existing = prev[config.target];
1018
- if (existing && existing.priority > content.priority) {
1019
- console.warn(
1020
- `[UISlots] Slot "${config.target}" already has higher priority content (${existing.priority} > ${content.priority})`
1021
- );
1022
- return prev;
1023
- }
1024
- notifySubscribers(config.target, content);
1025
- return { ...prev, [config.target]: content };
1026
- });
1027
- return id;
1028
- }, [notifySubscribers]);
1029
- const clear = useCallback((slot) => {
1030
- setSlots((prev) => {
1031
- const content = prev[slot];
1032
- if (content) {
1033
- const timer = timersRef.current.get(content.id);
1034
- if (timer) {
1035
- clearTimeout(timer);
1036
- timersRef.current.delete(content.id);
1037
- }
1038
- content.onDismiss?.();
1039
- notifySubscribers(slot, null);
1040
- }
1041
- return { ...prev, [slot]: null };
1042
- });
1043
- }, [notifySubscribers]);
1044
- const clearById = useCallback((id) => {
1045
- setSlots((prev) => {
1046
- const entry = Object.entries(prev).find(([, content]) => content?.id === id);
1047
- if (entry) {
1048
- const [slot, content] = entry;
1049
- const timer = timersRef.current.get(id);
1050
- if (timer) {
1051
- clearTimeout(timer);
1052
- timersRef.current.delete(id);
1053
- }
1054
- content.onDismiss?.();
1055
- notifySubscribers(slot, null);
1056
- return { ...prev, [slot]: null };
1057
- }
1058
- return prev;
1059
- });
1060
- }, [notifySubscribers]);
1061
- const clearAll = useCallback(() => {
1062
- timersRef.current.forEach((timer) => clearTimeout(timer));
1063
- timersRef.current.clear();
1064
- setSlots((prev) => {
1065
- Object.entries(prev).forEach(([slot, content]) => {
1066
- if (content) {
1067
- content.onDismiss?.();
1068
- notifySubscribers(slot, null);
1069
- }
1070
- });
1071
- return DEFAULT_SLOTS;
1072
- });
1073
- }, [notifySubscribers]);
1074
- const subscribe2 = useCallback((callback) => {
1075
- subscribersRef.current.add(callback);
1076
- return () => {
1077
- subscribersRef.current.delete(callback);
1078
- };
1079
- }, []);
1080
- const hasContent = useCallback((slot) => {
1081
- return slots[slot] !== null;
1082
- }, [slots]);
1083
- const getContent = useCallback((slot) => {
1084
- return slots[slot];
1085
- }, [slots]);
1086
- return {
1087
- slots,
1088
- render,
1089
- clear,
1090
- clearById,
1091
- clearAll,
1092
- subscribe: subscribe2,
1093
- hasContent,
1094
- getContent
1095
- };
1096
- }
1097
- var SelectionContext = createContext(null);
1098
-
1099
- // hooks/useUIEvents.ts
1100
- var UI_EVENT_MAP = {
1101
- // Form/CRUD events
1102
- "UI:SAVE": "SAVE",
1103
- "UI:CANCEL": "CANCEL",
1104
- "UI:CLOSE": "CLOSE",
1105
- "UI:VIEW": "VIEW",
1106
- "UI:EDIT": "EDIT",
1107
- "UI:DELETE": "DELETE",
1108
- "UI:CREATE": "CREATE",
1109
- "UI:SELECT": "SELECT",
1110
- "UI:DESELECT": "DESELECT",
1111
- "UI:SUBMIT": "SAVE",
1112
- "UI:UPDATE_STATUS": "UPDATE_STATUS",
1113
- "UI:SEARCH": "SEARCH",
1114
- "UI:CLEAR_SEARCH": "CLEAR_SEARCH",
1115
- "UI:ADD": "CREATE",
1116
- // Game events (for closed circuit with GameMenu, GamePauseOverlay, GameOverScreen)
1117
- "UI:PAUSE": "PAUSE",
1118
- "UI:RESUME": "RESUME",
1119
- "UI:RESTART": "RESTART",
1120
- "UI:GAME_OVER": "GAME_OVER",
1121
- "UI:START": "START",
1122
- "UI:QUIT": "QUIT",
1123
- "UI:INIT": "INIT"
1124
- };
1125
- function useUIEvents(dispatch, validEvents, eventBusInstance) {
1126
- const defaultEventBus = useEventBus();
1127
- const eventBus = eventBusInstance ?? defaultEventBus;
1128
- const validEventsKey = validEvents ? validEvents.slice().sort().join(",") : "";
1129
- const stableValidEvents = useMemo(
1130
- () => validEvents,
1131
- // eslint-disable-next-line react-hooks/exhaustive-deps
1132
- [validEventsKey]
1133
- );
1134
- useEffect(() => {
1135
- const unsubscribes = [];
1136
- Object.entries(UI_EVENT_MAP).forEach(([uiEvent, smEvent]) => {
1137
- const handler = (event) => {
1138
- if (!stableValidEvents || stableValidEvents.includes(smEvent)) {
1139
- dispatch(smEvent, event.payload);
1140
- }
1141
- };
1142
- const unsubscribe = eventBus.on(uiEvent, handler);
1143
- unsubscribes.push(unsubscribe);
1144
- });
1145
- const genericHandler = (event) => {
1146
- const eventName = event.payload?.event;
1147
- if (eventName) {
1148
- const smEvent = eventName;
1149
- if (!stableValidEvents || stableValidEvents.includes(smEvent)) {
1150
- dispatch(smEvent, event.payload);
1151
- }
1152
- }
1153
- };
1154
- const genericUnsubscribe = eventBus.on("UI:DISPATCH", genericHandler);
1155
- unsubscribes.push(genericUnsubscribe);
1156
- if (stableValidEvents) {
1157
- stableValidEvents.forEach((smEvent) => {
1158
- const uiPrefixedEvent = `UI:${smEvent}`;
1159
- const alreadyMapped = Object.keys(UI_EVENT_MAP).includes(uiPrefixedEvent);
1160
- if (!alreadyMapped) {
1161
- const directHandler = (event) => {
1162
- dispatch(smEvent, event.payload);
1163
- };
1164
- const unsubscribePrefixed = eventBus.on(
1165
- uiPrefixedEvent,
1166
- directHandler
1167
- );
1168
- unsubscribes.push(unsubscribePrefixed);
1169
- const unsubscribeDirect = eventBus.on(smEvent, directHandler);
1170
- unsubscribes.push(unsubscribeDirect);
1171
- }
1172
- });
1173
- }
1174
- return () => {
1175
- unsubscribes.forEach((unsub) => unsub());
1176
- };
1177
- }, [eventBus, dispatch, stableValidEvents]);
1178
- }
1179
- function useSelectedEntity(eventBusInstance) {
1180
- const defaultEventBus = useEventBus();
1181
- const eventBus = eventBusInstance ?? defaultEventBus;
1182
- const selectionContext = useSelectionContext();
1183
- const [localSelected, setLocalSelected] = useState(null);
1184
- const usingContext = selectionContext !== null;
1185
- useEffect(() => {
1186
- if (usingContext) return;
1187
- const handleSelect = (event) => {
1188
- const row = event.payload?.row;
1189
- if (row) {
1190
- setLocalSelected(row);
1191
- }
1192
- };
1193
- const handleDeselect = () => {
1194
- setLocalSelected(null);
1195
- };
1196
- const unsubSelect = eventBus.on("UI:SELECT", handleSelect);
1197
- const unsubView = eventBus.on("UI:VIEW", handleSelect);
1198
- const unsubDeselect = eventBus.on("UI:DESELECT", handleDeselect);
1199
- const unsubClose = eventBus.on("UI:CLOSE", handleDeselect);
1200
- const unsubCancel = eventBus.on("UI:CANCEL", handleDeselect);
1201
- return () => {
1202
- unsubSelect();
1203
- unsubView();
1204
- unsubDeselect();
1205
- unsubClose();
1206
- unsubCancel();
1207
- };
1208
- }, [eventBus, usingContext]);
1209
- if (selectionContext) {
1210
- return [selectionContext.selected, selectionContext.setSelected];
1211
- }
1212
- return [localSelected, setLocalSelected];
1213
- }
1214
- function useSelectionContext() {
1215
- const context = useContext(SelectionContext);
1216
- return context;
1217
- }
1218
- var entityDataKeys = {
1219
- all: ["entities"],
1220
- lists: () => [...entityDataKeys.all, "list"],
1221
- list: (entity, filters) => [...entityDataKeys.lists(), entity, filters],
1222
- details: () => [...entityDataKeys.all, "detail"],
1223
- detail: (entity, id) => [...entityDataKeys.details(), entity, id]
1224
- };
1225
- function useEntityList(entity, options = {}) {
1226
- const { skip = false } = options;
1227
- const [data, setData] = useState([]);
1228
- const [isLoading, setIsLoading] = useState(!skip && !!entity);
1229
- const [error, setError] = useState(null);
1230
- const refetch = () => {
1231
- if (!entity || skip) return;
1232
- setIsLoading(true);
1233
- setError(null);
1234
- setTimeout(() => {
1235
- setData([]);
1236
- setIsLoading(false);
1237
- }, 100);
1238
- };
1239
- useEffect(() => {
1240
- if (skip || !entity) {
1241
- setIsLoading(false);
1242
- return;
1243
- }
1244
- refetch();
1245
- }, [entity, skip]);
1246
- return { data, isLoading, error, refetch };
1247
- }
1248
- function useEntity(entity, id) {
1249
- const [data, setData] = useState(null);
1250
- const [isLoading, setIsLoading] = useState(!!entity && !!id);
1251
- const [error, setError] = useState(null);
1252
- useEffect(() => {
1253
- if (!entity || !id) {
1254
- setIsLoading(false);
1255
- return;
1256
- }
1257
- setIsLoading(true);
1258
- setTimeout(() => {
1259
- setData(null);
1260
- setIsLoading(false);
1261
- }, 100);
1262
- }, [entity, id]);
1263
- return { data, isLoading, error };
1264
- }
1265
- function useEntityDetail(entity, id) {
1266
- const [data, setData] = useState(null);
1267
- const [isLoading, setIsLoading] = useState(!!entity && !!id);
1268
- const [error, setError] = useState(null);
1269
- const refetch = () => {
1270
- if (!entity || !id) return;
1271
- setIsLoading(true);
1272
- setError(null);
1273
- setTimeout(() => {
1274
- setData(null);
1275
- setIsLoading(false);
1276
- }, 100);
1277
- };
1278
- useEffect(() => {
1279
- if (!entity || !id) {
1280
- setIsLoading(false);
1281
- return;
1282
- }
1283
- refetch();
1284
- }, [entity, id]);
1285
- return { data, isLoading, error, refetch };
1286
- }
1287
- var queryStores = /* @__PURE__ */ new Map();
1288
- function getOrCreateStore(query) {
1289
- if (!queryStores.has(query)) {
1290
- queryStores.set(query, {
1291
- search: "",
1292
- filters: {},
1293
- sortField: void 0,
1294
- sortDirection: void 0,
1295
- listeners: /* @__PURE__ */ new Set()
1296
- });
1297
- }
1298
- return queryStores.get(query);
1299
- }
1300
- function useQuerySingleton(query) {
1301
- const [, forceUpdate] = useState({});
1302
- if (!query) {
1303
- return null;
1304
- }
1305
- const store = useMemo(() => getOrCreateStore(query), [query]);
1306
- useMemo(() => {
1307
- const listener = () => forceUpdate({});
1308
- store.listeners.add(listener);
1309
- return () => {
1310
- store.listeners.delete(listener);
1311
- };
1312
- }, [store]);
1313
- const notifyListeners = useCallback(() => {
1314
- store.listeners.forEach((listener) => listener());
1315
- }, [store]);
1316
- const setSearch = useCallback((value) => {
1317
- store.search = value;
1318
- notifyListeners();
1319
- }, [store, notifyListeners]);
1320
- const setFilter = useCallback((key, value) => {
1321
- store.filters = { ...store.filters, [key]: value };
1322
- notifyListeners();
1323
- }, [store, notifyListeners]);
1324
- const clearFilters = useCallback(() => {
1325
- store.filters = {};
1326
- store.search = "";
1327
- notifyListeners();
1328
- }, [store, notifyListeners]);
1329
- const setSort = useCallback((field, direction) => {
1330
- store.sortField = field;
1331
- store.sortDirection = direction;
1332
- notifyListeners();
1333
- }, [store, notifyListeners]);
1334
- return {
1335
- search: store.search,
1336
- setSearch,
1337
- filters: store.filters,
1338
- setFilter,
1339
- clearFilters,
1340
- sortField: store.sortField,
1341
- sortDirection: store.sortDirection,
1342
- setSort
1343
- };
1344
- }
1345
- function parseQueryBinding(binding) {
1346
- const cleaned = binding.startsWith("@") ? binding.slice(1) : binding;
1347
- const parts = cleaned.split(".");
1348
- return {
1349
- query: parts[0],
1350
- field: parts.length > 1 ? parts.slice(1).join(".") : void 0
1351
- };
1352
- }
1353
-
1354
- // ../../node_modules/.pnpm/@tanstack+query-core@5.90.20/node_modules/@tanstack/query-core/build/modern/subscribable.js
1355
- var Subscribable = class {
1356
- constructor() {
1357
- this.listeners = /* @__PURE__ */ new Set();
1358
- this.subscribe = this.subscribe.bind(this);
1359
- }
1360
- subscribe(listener) {
1361
- this.listeners.add(listener);
1362
- this.onSubscribe();
1363
- return () => {
1364
- this.listeners.delete(listener);
1365
- this.onUnsubscribe();
1366
- };
1367
- }
1368
- hasListeners() {
1369
- return this.listeners.size > 0;
1370
- }
1371
- onSubscribe() {
1372
- }
1373
- onUnsubscribe() {
1374
- }
1375
- };
1376
-
1377
- // ../../node_modules/.pnpm/@tanstack+query-core@5.90.20/node_modules/@tanstack/query-core/build/modern/timeoutManager.js
1378
- var defaultTimeoutProvider = {
1379
- // We need the wrapper function syntax below instead of direct references to
1380
- // global setTimeout etc.
1381
- //
1382
- // BAD: `setTimeout: setTimeout`
1383
- // GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`
1384
- //
1385
- // If we use direct references here, then anything that wants to spy on or
1386
- // replace the global setTimeout (like tests) won't work since we'll already
1387
- // have a hard reference to the original implementation at the time when this
1388
- // file was imported.
1389
- setTimeout: (callback, delay) => setTimeout(callback, delay),
1390
- clearTimeout: (timeoutId) => clearTimeout(timeoutId),
1391
- setInterval: (callback, delay) => setInterval(callback, delay),
1392
- clearInterval: (intervalId) => clearInterval(intervalId)
1393
- };
1394
- var _provider, _providerCalled, _a;
1395
- var TimeoutManager = (_a = class {
1396
- constructor() {
1397
- // We cannot have TimeoutManager<T> as we must instantiate it with a concrete
1398
- // type at app boot; and if we leave that type, then any new timer provider
1399
- // would need to support ReturnType<typeof setTimeout>, which is infeasible.
1400
- //
1401
- // We settle for type safety for the TimeoutProvider type, and accept that
1402
- // this class is unsafe internally to allow for extension.
1403
- __privateAdd(this, _provider, defaultTimeoutProvider);
1404
- __privateAdd(this, _providerCalled, false);
1405
- }
1406
- setTimeoutProvider(provider) {
1407
- if (process.env.NODE_ENV !== "production") {
1408
- if (__privateGet(this, _providerCalled) && provider !== __privateGet(this, _provider)) {
1409
- console.error(
1410
- `[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,
1411
- { previous: __privateGet(this, _provider), provider }
1412
- );
1413
- }
1414
- }
1415
- __privateSet(this, _provider, provider);
1416
- if (process.env.NODE_ENV !== "production") {
1417
- __privateSet(this, _providerCalled, false);
1418
- }
1419
- }
1420
- setTimeout(callback, delay) {
1421
- if (process.env.NODE_ENV !== "production") {
1422
- __privateSet(this, _providerCalled, true);
1423
- }
1424
- return __privateGet(this, _provider).setTimeout(callback, delay);
1425
- }
1426
- clearTimeout(timeoutId) {
1427
- __privateGet(this, _provider).clearTimeout(timeoutId);
1428
- }
1429
- setInterval(callback, delay) {
1430
- if (process.env.NODE_ENV !== "production") {
1431
- __privateSet(this, _providerCalled, true);
1432
- }
1433
- return __privateGet(this, _provider).setInterval(callback, delay);
1434
- }
1435
- clearInterval(intervalId) {
1436
- __privateGet(this, _provider).clearInterval(intervalId);
1437
- }
1438
- }, _provider = new WeakMap(), _providerCalled = new WeakMap(), _a);
1439
- new TimeoutManager();
1440
- function systemSetTimeoutZero(callback) {
1441
- setTimeout(callback, 0);
1442
- }
1443
- function noop() {
1444
- }
1445
- function hashKey(queryKey) {
1446
- return JSON.stringify(
1447
- queryKey,
1448
- (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
1449
- result[key] = val[key];
1450
- return result;
1451
- }, {}) : val
1452
- );
1453
- }
1454
- function shallowEqualObjects(a, b) {
1455
- if (!b || Object.keys(a).length !== Object.keys(b).length) {
1456
- return false;
1457
- }
1458
- for (const key in a) {
1459
- if (a[key] !== b[key]) {
1460
- return false;
1461
- }
1462
- }
1463
- return true;
1464
- }
1465
- function isPlainObject(o) {
1466
- if (!hasObjectPrototype(o)) {
1467
- return false;
1468
- }
1469
- const ctor = o.constructor;
1470
- if (ctor === void 0) {
1471
- return true;
1472
- }
1473
- const prot = ctor.prototype;
1474
- if (!hasObjectPrototype(prot)) {
1475
- return false;
1476
- }
1477
- if (!prot.hasOwnProperty("isPrototypeOf")) {
1478
- return false;
1479
- }
1480
- if (Object.getPrototypeOf(o) !== Object.prototype) {
1481
- return false;
1482
- }
1483
- return true;
1484
- }
1485
- function hasObjectPrototype(o) {
1486
- return Object.prototype.toString.call(o) === "[object Object]";
1487
- }
1488
- function shouldThrowError(throwOnError, params) {
1489
- if (typeof throwOnError === "function") {
1490
- return throwOnError(...params);
1491
- }
1492
- return !!throwOnError;
1493
- }
1494
-
1495
- // ../../node_modules/.pnpm/@tanstack+query-core@5.90.20/node_modules/@tanstack/query-core/build/modern/notifyManager.js
1496
- var defaultScheduler = systemSetTimeoutZero;
1497
- function createNotifyManager() {
1498
- let queue = [];
1499
- let transactions = 0;
1500
- let notifyFn = (callback) => {
1501
- callback();
1502
- };
1503
- let batchNotifyFn = (callback) => {
1504
- callback();
1505
- };
1506
- let scheduleFn = defaultScheduler;
1507
- const schedule = (callback) => {
1508
- if (transactions) {
1509
- queue.push(callback);
1510
- } else {
1511
- scheduleFn(() => {
1512
- notifyFn(callback);
1513
- });
1514
- }
1515
- };
1516
- const flush = () => {
1517
- const originalQueue = queue;
1518
- queue = [];
1519
- if (originalQueue.length) {
1520
- scheduleFn(() => {
1521
- batchNotifyFn(() => {
1522
- originalQueue.forEach((callback) => {
1523
- notifyFn(callback);
1524
- });
1525
- });
1526
- });
1527
- }
1528
- };
1529
- return {
1530
- batch: (callback) => {
1531
- let result;
1532
- transactions++;
1533
- try {
1534
- result = callback();
1535
- } finally {
1536
- transactions--;
1537
- if (!transactions) {
1538
- flush();
1539
- }
1540
- }
1541
- return result;
1542
- },
1543
- /**
1544
- * All calls to the wrapped function will be batched.
1545
- */
1546
- batchCalls: (callback) => {
1547
- return (...args) => {
1548
- schedule(() => {
1549
- callback(...args);
1550
- });
1551
- };
1552
- },
1553
- schedule,
1554
- /**
1555
- * Use this method to set a custom notify function.
1556
- * This can be used to for example wrap notifications with `React.act` while running tests.
1557
- */
1558
- setNotifyFunction: (fn) => {
1559
- notifyFn = fn;
1560
- },
1561
- /**
1562
- * Use this method to set a custom function to batch notifications together into a single tick.
1563
- * By default React Query will use the batch function provided by ReactDOM or React Native.
1564
- */
1565
- setBatchNotifyFunction: (fn) => {
1566
- batchNotifyFn = fn;
1567
- },
1568
- setScheduler: (fn) => {
1569
- scheduleFn = fn;
1570
- }
1571
- };
1572
- }
1573
- var notifyManager = createNotifyManager();
1574
-
1575
- // ../../node_modules/.pnpm/@tanstack+query-core@5.90.20/node_modules/@tanstack/query-core/build/modern/mutation.js
1576
- function getDefaultState() {
1577
- return {
1578
- context: void 0,
1579
- data: void 0,
1580
- error: null,
1581
- failureCount: 0,
1582
- failureReason: null,
1583
- isPaused: false,
1584
- status: "idle",
1585
- variables: void 0,
1586
- submittedAt: 0
1587
- };
1588
- }
1589
-
1590
- // ../../node_modules/.pnpm/@tanstack+query-core@5.90.20/node_modules/@tanstack/query-core/build/modern/mutationObserver.js
1591
- var _client, _currentResult, _currentMutation, _mutateOptions, _MutationObserver_instances, updateResult_fn, notify_fn, _a2;
1592
- var MutationObserver = (_a2 = class extends Subscribable {
1593
- constructor(client, options) {
1594
- super();
1595
- __privateAdd(this, _MutationObserver_instances);
1596
- __privateAdd(this, _client);
1597
- __privateAdd(this, _currentResult);
1598
- __privateAdd(this, _currentMutation);
1599
- __privateAdd(this, _mutateOptions);
1600
- __privateSet(this, _client, client);
1601
- this.setOptions(options);
1602
- this.bindMethods();
1603
- __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this);
1604
- }
1605
- bindMethods() {
1606
- this.mutate = this.mutate.bind(this);
1607
- this.reset = this.reset.bind(this);
1608
- }
1609
- setOptions(options) {
1610
- const prevOptions = this.options;
1611
- this.options = __privateGet(this, _client).defaultMutationOptions(options);
1612
- if (!shallowEqualObjects(this.options, prevOptions)) {
1613
- __privateGet(this, _client).getMutationCache().notify({
1614
- type: "observerOptionsUpdated",
1615
- mutation: __privateGet(this, _currentMutation),
1616
- observer: this
1617
- });
1618
- }
1619
- if (prevOptions?.mutationKey && this.options.mutationKey && hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)) {
1620
- this.reset();
1621
- } else if (__privateGet(this, _currentMutation)?.state.status === "pending") {
1622
- __privateGet(this, _currentMutation).setOptions(this.options);
1623
- }
1624
- }
1625
- onUnsubscribe() {
1626
- if (!this.hasListeners()) {
1627
- __privateGet(this, _currentMutation)?.removeObserver(this);
1628
- }
1629
- }
1630
- onMutationUpdate(action) {
1631
- __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this);
1632
- __privateMethod(this, _MutationObserver_instances, notify_fn).call(this, action);
1633
- }
1634
- getCurrentResult() {
1635
- return __privateGet(this, _currentResult);
1636
- }
1637
- reset() {
1638
- __privateGet(this, _currentMutation)?.removeObserver(this);
1639
- __privateSet(this, _currentMutation, void 0);
1640
- __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this);
1641
- __privateMethod(this, _MutationObserver_instances, notify_fn).call(this);
1642
- }
1643
- mutate(variables, options) {
1644
- __privateSet(this, _mutateOptions, options);
1645
- __privateGet(this, _currentMutation)?.removeObserver(this);
1646
- __privateSet(this, _currentMutation, __privateGet(this, _client).getMutationCache().build(__privateGet(this, _client), this.options));
1647
- __privateGet(this, _currentMutation).addObserver(this);
1648
- return __privateGet(this, _currentMutation).execute(variables);
1649
- }
1650
- }, _client = new WeakMap(), _currentResult = new WeakMap(), _currentMutation = new WeakMap(), _mutateOptions = new WeakMap(), _MutationObserver_instances = new WeakSet(), updateResult_fn = function() {
1651
- const state = __privateGet(this, _currentMutation)?.state ?? getDefaultState();
1652
- __privateSet(this, _currentResult, {
1653
- ...state,
1654
- isPending: state.status === "pending",
1655
- isSuccess: state.status === "success",
1656
- isError: state.status === "error",
1657
- isIdle: state.status === "idle",
1658
- mutate: this.mutate,
1659
- reset: this.reset
1660
- });
1661
- }, notify_fn = function(action) {
1662
- notifyManager.batch(() => {
1663
- if (__privateGet(this, _mutateOptions) && this.hasListeners()) {
1664
- const variables = __privateGet(this, _currentResult).variables;
1665
- const onMutateResult = __privateGet(this, _currentResult).context;
1666
- const context = {
1667
- client: __privateGet(this, _client),
1668
- meta: this.options.meta,
1669
- mutationKey: this.options.mutationKey
1670
- };
1671
- if (action?.type === "success") {
1672
- try {
1673
- __privateGet(this, _mutateOptions).onSuccess?.(
1674
- action.data,
1675
- variables,
1676
- onMutateResult,
1677
- context
1678
- );
1679
- } catch (e) {
1680
- void Promise.reject(e);
1681
- }
1682
- try {
1683
- __privateGet(this, _mutateOptions).onSettled?.(
1684
- action.data,
1685
- null,
1686
- variables,
1687
- onMutateResult,
1688
- context
1689
- );
1690
- } catch (e) {
1691
- void Promise.reject(e);
1692
- }
1693
- } else if (action?.type === "error") {
1694
- try {
1695
- __privateGet(this, _mutateOptions).onError?.(
1696
- action.error,
1697
- variables,
1698
- onMutateResult,
1699
- context
1700
- );
1701
- } catch (e) {
1702
- void Promise.reject(e);
1703
- }
1704
- try {
1705
- __privateGet(this, _mutateOptions).onSettled?.(
1706
- void 0,
1707
- action.error,
1708
- variables,
1709
- onMutateResult,
1710
- context
1711
- );
1712
- } catch (e) {
1713
- void Promise.reject(e);
1714
- }
1715
- }
1716
- }
1717
- this.listeners.forEach((listener) => {
1718
- listener(__privateGet(this, _currentResult));
1719
- });
1720
- });
1721
- }, _a2);
1722
- var QueryClientContext = React4.createContext(
1723
- void 0
1724
- );
1725
- var useQueryClient = (queryClient) => {
1726
- const client = React4.useContext(QueryClientContext);
1727
- if (!client) {
1728
- throw new Error("No QueryClient set, use QueryClientProvider to set one");
1729
- }
1730
- return client;
1731
- };
1732
- function useMutation(options, queryClient) {
1733
- const client = useQueryClient();
1734
- const [observer] = React4.useState(
1735
- () => new MutationObserver(
1736
- client,
1737
- options
1738
- )
1739
- );
1740
- React4.useEffect(() => {
1741
- observer.setOptions(options);
1742
- }, [observer, options]);
1743
- const result = React4.useSyncExternalStore(
1744
- React4.useCallback(
1745
- (onStoreChange) => observer.subscribe(notifyManager.batchCalls(onStoreChange)),
1746
- [observer]
1747
- ),
1748
- () => observer.getCurrentResult(),
1749
- () => observer.getCurrentResult()
1750
- );
1751
- const mutate = React4.useCallback(
1752
- (variables, mutateOptions) => {
1753
- observer.mutate(variables, mutateOptions).catch(noop);
1754
- },
1755
- [observer]
1756
- );
1757
- if (result.error && shouldThrowError(observer.options.throwOnError, [result.error])) {
1758
- throw result.error;
1759
- }
1760
- return { ...result, mutate, mutateAsync: result.mutate };
1761
- }
1762
-
1763
- // lib/api-client.ts
1764
- var API_BASE_URL = typeof import.meta !== "undefined" && import.meta.env?.VITE_API_URL ? import.meta.env.VITE_API_URL : "/api";
1765
- var ApiError = class extends Error {
1766
- constructor(status, statusText, message) {
1767
- super(message || `API Error: ${status} ${statusText}`);
1768
- this.status = status;
1769
- this.statusText = statusText;
1770
- this.name = "ApiError";
1771
- }
1772
- };
1773
- async function handleResponse(response) {
1774
- if (!response.ok) {
1775
- let message;
1776
- try {
1777
- const errorData = await response.json();
1778
- message = errorData.message || errorData.error;
1779
- } catch {
1780
- }
1781
- throw new ApiError(response.status, response.statusText, message);
1782
- }
1783
- const text = await response.text();
1784
- if (!text) {
1785
- return void 0;
1786
- }
1787
- return JSON.parse(text);
1788
- }
1789
- function getHeaders() {
1790
- const headers = {
1791
- "Content-Type": "application/json"
1792
- };
1793
- const token = typeof localStorage !== "undefined" ? localStorage.getItem("authToken") : null;
1794
- if (token) {
1795
- headers["Authorization"] = `Bearer ${token}`;
1796
- }
1797
- return headers;
1798
- }
1799
- var apiClient = {
1800
- /**
1801
- * GET request
1802
- */
1803
- async get(endpoint) {
1804
- const response = await fetch(`${API_BASE_URL}${endpoint}`, {
1805
- method: "GET",
1806
- headers: getHeaders()
1807
- });
1808
- return handleResponse(response);
1809
- },
1810
- /**
1811
- * POST request
1812
- */
1813
- async post(endpoint, data) {
1814
- const response = await fetch(`${API_BASE_URL}${endpoint}`, {
1815
- method: "POST",
1816
- headers: getHeaders(),
1817
- body: data ? JSON.stringify(data) : void 0
1818
- });
1819
- return handleResponse(response);
1820
- },
1821
- /**
1822
- * PUT request
1823
- */
1824
- async put(endpoint, data) {
1825
- const response = await fetch(`${API_BASE_URL}${endpoint}`, {
1826
- method: "PUT",
1827
- headers: getHeaders(),
1828
- body: data ? JSON.stringify(data) : void 0
1829
- });
1830
- return handleResponse(response);
1831
- },
1832
- /**
1833
- * PATCH request
1834
- */
1835
- async patch(endpoint, data) {
1836
- const response = await fetch(`${API_BASE_URL}${endpoint}`, {
1837
- method: "PATCH",
1838
- headers: getHeaders(),
1839
- body: data ? JSON.stringify(data) : void 0
1840
- });
1841
- return handleResponse(response);
1842
- },
1843
- /**
1844
- * DELETE request
1845
- */
1846
- async delete(endpoint) {
1847
- const response = await fetch(`${API_BASE_URL}${endpoint}`, {
1848
- method: "DELETE",
1849
- headers: getHeaders()
1850
- });
1851
- return handleResponse(response);
1852
- }
1853
- };
1854
-
1855
- // hooks/useOrbitalMutations.ts
1856
- var ENTITY_EVENTS = {
1857
- CREATE: "ENTITY_CREATE",
1858
- UPDATE: "ENTITY_UPDATE",
1859
- DELETE: "ENTITY_DELETE"
1860
- };
1861
- async function sendOrbitalEvent(orbitalName, eventPayload) {
1862
- const response = await apiClient.post(
1863
- `/orbitals/${orbitalName}/events`,
1864
- eventPayload
1865
- );
1866
- return response;
1867
- }
1868
- function useOrbitalMutations(entityName, orbitalName, options) {
1869
- const queryClient = useQueryClient();
1870
- const events = {
1871
- create: options?.events?.create || ENTITY_EVENTS.CREATE,
1872
- update: options?.events?.update || ENTITY_EVENTS.UPDATE,
1873
- delete: options?.events?.delete || ENTITY_EVENTS.DELETE
1874
- };
1875
- const log = (message, data) => {
1876
- if (options?.debug) {
1877
- console.log(`[useOrbitalMutations:${orbitalName}] ${message}`, data ?? "");
1878
- }
1879
- };
1880
- const createMutation = useMutation({
1881
- mutationFn: async (data) => {
1882
- log("Creating entity", data);
1883
- return sendOrbitalEvent(orbitalName, {
1884
- event: events.create,
1885
- payload: { data, entityType: entityName }
1886
- });
1887
- },
1888
- onSuccess: (response) => {
1889
- log("Create succeeded", response);
1890
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1891
- },
1892
- onError: (error) => {
1893
- console.error(`[useOrbitalMutations] Create failed:`, error);
1894
- }
1895
- });
1896
- const updateMutation = useMutation({
1897
- mutationFn: async ({
1898
- id,
1899
- data
1900
- }) => {
1901
- log(`Updating entity ${id}`, data);
1902
- return sendOrbitalEvent(orbitalName, {
1903
- event: events.update,
1904
- entityId: id,
1905
- payload: { data, entityType: entityName }
1906
- });
1907
- },
1908
- onSuccess: (response, variables) => {
1909
- log("Update succeeded", response);
1910
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1911
- queryClient.invalidateQueries({
1912
- queryKey: entityDataKeys.detail(entityName, variables.id)
1913
- });
1914
- },
1915
- onError: (error) => {
1916
- console.error(`[useOrbitalMutations] Update failed:`, error);
1917
- }
1918
- });
1919
- const deleteMutation = useMutation({
1920
- mutationFn: async (id) => {
1921
- log(`Deleting entity ${id}`);
1922
- return sendOrbitalEvent(orbitalName, {
1923
- event: events.delete,
1924
- entityId: id,
1925
- payload: { entityType: entityName }
1926
- });
1927
- },
1928
- onSuccess: (response, id) => {
1929
- log("Delete succeeded", response);
1930
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1931
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
1932
- },
1933
- onError: (error) => {
1934
- console.error(`[useOrbitalMutations] Delete failed:`, error);
1935
- }
1936
- });
1937
- return {
1938
- // Async functions
1939
- createEntity: async (data) => {
1940
- return createMutation.mutateAsync(data);
1941
- },
1942
- updateEntity: async (id, data) => {
1943
- if (!id) {
1944
- console.warn("[useOrbitalMutations] Cannot update without ID");
1945
- return;
1946
- }
1947
- return updateMutation.mutateAsync({ id, data });
1948
- },
1949
- deleteEntity: async (id) => {
1950
- if (!id) {
1951
- console.warn("[useOrbitalMutations] Cannot delete without ID");
1952
- return;
1953
- }
1954
- return deleteMutation.mutateAsync(id);
1955
- },
1956
- // Mutation objects for fine-grained control
1957
- createMutation,
1958
- updateMutation,
1959
- deleteMutation,
1960
- // Aggregate states
1961
- isCreating: createMutation.isPending,
1962
- isUpdating: updateMutation.isPending,
1963
- isDeleting: deleteMutation.isPending,
1964
- isMutating: createMutation.isPending || updateMutation.isPending || deleteMutation.isPending,
1965
- // Errors
1966
- createError: createMutation.error,
1967
- updateError: updateMutation.error,
1968
- deleteError: deleteMutation.error
1969
- };
1970
- }
1971
- function useSendOrbitalEvent(orbitalName) {
1972
- const mutation = useMutation({
1973
- mutationFn: async (payload) => {
1974
- return sendOrbitalEvent(orbitalName, payload);
1975
- }
1976
- });
1977
- return {
1978
- sendEvent: async (event, payload, entityId) => {
1979
- return mutation.mutateAsync({ event, payload, entityId });
1980
- },
1981
- isPending: mutation.isPending,
1982
- error: mutation.error,
1983
- data: mutation.data
1984
- };
1985
- }
1986
-
1987
- // hooks/useEntityMutations.ts
1988
- function entityToCollection(entityName) {
1989
- return entityName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase() + "-list";
1990
- }
1991
- function useCreateEntity(entityName) {
1992
- const queryClient = useQueryClient();
1993
- const collection = entityToCollection(entityName);
1994
- return useMutation({
1995
- mutationFn: async (data) => {
1996
- console.log(`[useCreateEntity] Creating ${entityName}:`, data);
1997
- const response = await apiClient.post(
1998
- `/${collection}`,
1999
- data
2000
- );
2001
- return response.data;
2002
- },
2003
- onSuccess: () => {
2004
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2005
- },
2006
- onError: (error) => {
2007
- console.error(`[useCreateEntity] Failed to create ${entityName}:`, error);
2008
- }
2009
- });
2010
- }
2011
- function useUpdateEntity(entityName) {
2012
- const queryClient = useQueryClient();
2013
- const collection = entityToCollection(entityName);
2014
- return useMutation({
2015
- mutationFn: async ({ id, data }) => {
2016
- console.log(`[useUpdateEntity] Updating ${entityName} ${id}:`, data);
2017
- const response = await apiClient.patch(
2018
- `/${collection}/${id}`,
2019
- data
2020
- );
2021
- return response.data;
2022
- },
2023
- onSuccess: (_, variables) => {
2024
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2025
- queryClient.invalidateQueries({ queryKey: entityDataKeys.detail(entityName, variables.id) });
2026
- },
2027
- onError: (error) => {
2028
- console.error(`[useUpdateEntity] Failed to update ${entityName}:`, error);
2029
- }
2030
- });
2031
- }
2032
- function useDeleteEntity(entityName) {
2033
- const queryClient = useQueryClient();
2034
- const collection = entityToCollection(entityName);
2035
- return useMutation({
2036
- mutationFn: async (id) => {
2037
- console.log(`[useDeleteEntity] Deleting ${entityName} ${id}`);
2038
- await apiClient.delete(`/${collection}/${id}`);
2039
- return { id };
2040
- },
2041
- onSuccess: (_, id) => {
2042
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2043
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
2044
- },
2045
- onError: (error) => {
2046
- console.error(`[useDeleteEntity] Failed to delete ${entityName}:`, error);
2047
- }
2048
- });
2049
- }
2050
- async function sendOrbitalMutation(orbitalName, event, entityId, payload) {
2051
- const response = await apiClient.post(
2052
- `/orbitals/${orbitalName}/events`,
2053
- { event, entityId, payload }
2054
- );
2055
- return response;
2056
- }
2057
- function useEntityMutations(entityName, options) {
2058
- const queryClient = useQueryClient();
2059
- const useOrbitalRoute = !!options?.orbitalName;
2060
- const events = {
2061
- create: options?.events?.create || ENTITY_EVENTS.CREATE,
2062
- update: options?.events?.update || ENTITY_EVENTS.UPDATE,
2063
- delete: options?.events?.delete || ENTITY_EVENTS.DELETE
2064
- };
2065
- const createMutation = useCreateEntity(entityName);
2066
- const updateMutation = useUpdateEntity(entityName);
2067
- const deleteMutation = useDeleteEntity(entityName);
2068
- const orbitalCreateMutation = useMutation({
2069
- mutationFn: async (data) => {
2070
- return sendOrbitalMutation(options.orbitalName, events.create, void 0, {
2071
- data,
2072
- entityType: entityName
2073
- });
2074
- },
2075
- onSuccess: () => {
2076
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2077
- }
2078
- });
2079
- const orbitalUpdateMutation = useMutation({
2080
- mutationFn: async ({ id, data }) => {
2081
- return sendOrbitalMutation(options.orbitalName, events.update, id, {
2082
- data,
2083
- entityType: entityName
2084
- });
2085
- },
2086
- onSuccess: (_, variables) => {
2087
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2088
- queryClient.invalidateQueries({
2089
- queryKey: entityDataKeys.detail(entityName, variables.id)
2090
- });
2091
- }
2092
- });
2093
- const orbitalDeleteMutation = useMutation({
2094
- mutationFn: async (id) => {
2095
- return sendOrbitalMutation(options.orbitalName, events.delete, id, {
2096
- entityType: entityName
2097
- });
2098
- },
2099
- onSuccess: (_, id) => {
2100
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
2101
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
2102
- }
2103
- });
2104
- const activeMutations = {
2105
- create: useOrbitalRoute ? orbitalCreateMutation : createMutation,
2106
- update: useOrbitalRoute ? orbitalUpdateMutation : updateMutation,
2107
- delete: useOrbitalRoute ? orbitalDeleteMutation : deleteMutation
2108
- };
2109
- return {
2110
- // Async functions that can be called directly
2111
- // Accepts either (data) or (entityName, data) for compiler compatibility
2112
- createEntity: async (entityOrData, data) => {
2113
- const actualData = typeof entityOrData === "string" ? data : entityOrData;
2114
- if (!actualData) {
2115
- console.warn("[useEntityMutations] Cannot create entity without data");
2116
- return;
2117
- }
2118
- return activeMutations.create.mutateAsync(actualData);
2119
- },
2120
- updateEntity: async (id, data) => {
2121
- if (!id) {
2122
- console.warn("[useEntityMutations] Cannot update entity without ID");
2123
- return;
2124
- }
2125
- return activeMutations.update.mutateAsync({ id, data });
2126
- },
2127
- deleteEntity: async (id) => {
2128
- if (!id) {
2129
- console.warn("[useEntityMutations] Cannot delete entity without ID");
2130
- return;
2131
- }
2132
- return activeMutations.delete.mutateAsync(id);
2133
- },
2134
- // Mutation states for UI feedback
2135
- isCreating: activeMutations.create.isPending,
2136
- isUpdating: activeMutations.update.isPending,
2137
- isDeleting: activeMutations.delete.isPending,
2138
- createError: activeMutations.create.error,
2139
- updateError: activeMutations.update.error,
2140
- deleteError: activeMutations.delete.error
2141
- };
2142
- }
2143
-
2144
- // stores/entityStore.ts
2145
- var entities = /* @__PURE__ */ new Map();
2146
- var listeners = /* @__PURE__ */ new Set();
2147
- var idCounter2 = 0;
2148
- function subscribe(listener) {
2149
- listeners.add(listener);
2150
- return () => listeners.delete(listener);
2151
- }
2152
- function notify() {
2153
- listeners.forEach((listener) => listener());
2154
- }
2155
- function getEntity(id) {
2156
- return entities.get(id);
2157
- }
2158
- function getByType(type) {
2159
- const types = Array.isArray(type) ? type : [type];
2160
- return [...entities.values()].filter((e) => types.includes(e.type));
2161
- }
2162
- function getAllEntities() {
2163
- return [...entities.values()];
2164
- }
2165
- function getSingleton(type) {
2166
- return [...entities.values()].find((e) => e.type === type);
2167
- }
2168
- function spawnEntity(config) {
2169
- const id = config.id ?? `entity_${++idCounter2}`;
2170
- const entity = { ...config, id };
2171
- entities = new Map(entities);
2172
- entities.set(id, entity);
2173
- notify();
2174
- return id;
2175
- }
2176
- function updateEntity(id, updates) {
2177
- const entity = entities.get(id);
2178
- if (entity) {
2179
- entities = new Map(entities);
2180
- entities.set(id, { ...entity, ...updates });
2181
- notify();
2182
- }
2183
- }
2184
- function updateSingleton(type, updates) {
2185
- const entity = getSingleton(type);
2186
- if (entity) {
2187
- updateEntity(entity.id, updates);
2188
- }
2189
- }
2190
- function removeEntity(id) {
2191
- if (entities.has(id)) {
2192
- entities = new Map(entities);
2193
- entities.delete(id);
2194
- notify();
2195
- }
2196
- }
2197
- function clearEntities() {
2198
- entities = /* @__PURE__ */ new Map();
2199
- notify();
2200
- }
2201
- function getSnapshot() {
2202
- return entities;
2203
- }
2204
-
2205
- // hooks/useEntities.ts
2206
- function useEntities() {
2207
- const entities2 = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
2208
- return {
2209
- entities: entities2,
2210
- getEntity,
2211
- getByType,
2212
- getAllEntities,
2213
- getSingleton,
2214
- spawnEntity,
2215
- updateEntity,
2216
- updateSingleton,
2217
- removeEntity,
2218
- clearEntities
2219
- };
2220
- }
2221
- function useEntity2(id) {
2222
- const entities2 = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
2223
- return entities2.get(id);
2224
- }
2225
- function useEntitiesByType(type) {
2226
- const entities2 = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
2227
- return [...entities2.values()].filter((e) => e.type === type);
2228
- }
2229
- function useSingletonEntity(type) {
2230
- const entities2 = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
2231
- return [...entities2.values()].find((e) => e.type === type);
2232
- }
2233
- function usePlayer() {
2234
- const player = useSingletonEntity("Player");
2235
- const update = useCallback((updates) => {
2236
- if (player) updateEntity(player.id, updates);
2237
- }, [player?.id]);
2238
- return { player, updatePlayer: update };
2239
- }
2240
- function usePhysics() {
2241
- const physics = useSingletonEntity("Physics");
2242
- const update = useCallback((updates) => {
2243
- if (physics) updateEntity(physics.id, updates);
2244
- }, [physics?.id]);
2245
- return { physics, updatePhysics: update };
2246
- }
2247
- function useInput() {
2248
- const input = useSingletonEntity("Input");
2249
- const update = useCallback((updates) => {
2250
- if (input) updateEntity(input.id, updates);
2251
- }, [input?.id]);
2252
- return { input, updateInput: update };
2253
- }
2254
-
2255
- // hooks/useAuthContext.ts
2256
- function useAuthContext() {
2257
- return {
2258
- user: null,
2259
- loading: false,
2260
- signIn: void 0,
2261
- signOut: void 0
2262
- };
2263
- }
2264
-
2265
- export { DEFAULT_SLOTS, ENTITY_EVENTS, clearEntities, entityDataKeys, getAllEntities, getByType, getEntity, getSingleton, parseQueryBinding, removeEntity, spawnEntity, updateEntity, updateSingleton, useAgentChat, useAuthContext, useCompile, useCreateEntity, useDeepAgentGeneration, useDeleteEntity, useEmitEvent, useEntities, useEntitiesByType, useEntity, useEntity2 as useEntityById, useEntityDetail, useEntityList, useEntityMutations, useEventBus, useEventListener, useExtensions, useFileEditor, useFileSystem, useInput, useOrbitalHistory, useOrbitalMutations, usePhysics, usePlayer, usePreview, useQuerySingleton, useSelectedEntity, useSendOrbitalEvent, useSingletonEntity, useUIEvents, useUISlotManager, useUpdateEntity, useValidation };
2266
- //# sourceMappingURL=index.js.map
2267
- //# sourceMappingURL=index.js.map
1
+ export { ENTITY_EVENTS, entityDataKeys, parseQueryBinding, useAgentChat, useAuthContext, useCompile, useConnectGitHub, useCreateEntity, useDeepAgentGeneration, useDeleteEntity, useDisconnectGitHub, useEntities, useEntitiesByType, useEntity, useEntity2 as useEntityById, useEntityDetail, useEntityList, useEntityMutations, useExtensions, useFileEditor, useFileSystem, useGitHubBranches, useGitHubRepo, useGitHubRepos, useGitHubStatus, useInput, useOrbitalHistory, useOrbitalMutations, usePhysics, usePlayer, usePreview, useQuerySingleton, useSelectedEntity, useSendOrbitalEvent, useSingletonEntity, useUIEvents, useUpdateEntity, useValidation } from '../chunk-4AIGHVQK.js';
2
+ import '../chunk-XSEDIUM6.js';
3
+ export { useEmitEvent, useEventBus, useEventListener } from '../chunk-TTXKOHDO.js';
4
+ export { DEFAULT_SLOTS, useUISlotManager } from '../chunk-7NEWMNNU.js';
5
+ export { clearEntities, getAllEntities, getByType, getEntity, getSingleton, removeEntity, spawnEntity, updateEntity, updateSingleton } from '../chunk-N7MVUW4R.js';
6
+ import '../chunk-S7EYY36U.js';