@almadar/ui 2.15.8 → 2.15.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.
Files changed (126) hide show
  1. package/dist/components/atoms/ContentSection.d.ts +16 -0
  2. package/dist/components/atoms/SectionHeader.d.ts +14 -0
  3. package/dist/components/atoms/StatCard.d.ts +13 -0
  4. package/dist/components/atoms/index.d.ts +3 -0
  5. package/dist/components/atoms/svg/SvgBranch.d.ts +12 -0
  6. package/dist/components/atoms/svg/SvgConnection.d.ts +13 -0
  7. package/dist/components/atoms/svg/SvgFlow.d.ts +10 -0
  8. package/dist/components/atoms/svg/SvgGrid.d.ts +14 -0
  9. package/dist/components/atoms/svg/SvgLobe.d.ts +13 -0
  10. package/dist/components/atoms/svg/SvgMesh.d.ts +12 -0
  11. package/dist/components/atoms/svg/SvgMorph.d.ts +11 -0
  12. package/dist/components/atoms/svg/SvgNode.d.ts +12 -0
  13. package/dist/components/atoms/svg/SvgPulse.d.ts +12 -0
  14. package/dist/components/atoms/svg/SvgRing.d.ts +13 -0
  15. package/dist/components/atoms/svg/SvgShield.d.ts +11 -0
  16. package/dist/components/atoms/svg/SvgStack.d.ts +13 -0
  17. package/dist/components/atoms/svg/index.d.ts +12 -0
  18. package/dist/{chunk-ACUO2BBW.js → components/index.cjs} +24831 -16747
  19. package/dist/components/index.js +37754 -889
  20. package/dist/components/molecules/AnimatedCounter.d.ts +18 -0
  21. package/dist/components/molecules/ArticleSection.d.ts +18 -0
  22. package/dist/components/molecules/CTABanner.d.ts +31 -0
  23. package/dist/components/molecules/CaseStudyCard.d.ts +24 -0
  24. package/dist/components/molecules/CodeExample.d.ts +23 -0
  25. package/dist/components/molecules/CommunityLinks.d.ts +25 -0
  26. package/dist/components/molecules/DocBreadcrumb.d.ts +20 -0
  27. package/dist/components/molecules/DocCodeBlock.d.ts +13 -0
  28. package/dist/components/molecules/DocPagination.d.ts +14 -0
  29. package/dist/components/molecules/DocSearch.d.ts +15 -0
  30. package/dist/components/molecules/DocSidebar.d.ts +24 -0
  31. package/dist/components/molecules/DocTOC.d.ts +24 -0
  32. package/dist/components/molecules/FeatureCard.d.ts +26 -0
  33. package/dist/components/molecules/FeatureGrid.d.ts +19 -0
  34. package/dist/components/molecules/GradientDivider.d.ts +14 -0
  35. package/dist/components/molecules/HeroSection.d.ts +36 -0
  36. package/dist/components/molecules/InstallBox.d.ts +16 -0
  37. package/dist/components/molecules/MarketingFooter.d.ts +27 -0
  38. package/dist/components/molecules/PricingCard.d.ts +21 -0
  39. package/dist/components/molecules/PricingGrid.d.ts +13 -0
  40. package/dist/components/molecules/PullQuote.d.ts +14 -0
  41. package/dist/components/molecules/ServiceCatalog.d.ts +19 -0
  42. package/dist/components/molecules/ShowcaseCard.d.ts +20 -0
  43. package/dist/components/molecules/SocialProof.d.ts +25 -0
  44. package/dist/components/molecules/SplitSection.d.ts +21 -0
  45. package/dist/components/molecules/StatsGrid.d.ts +17 -0
  46. package/dist/components/molecules/StepFlow.d.ts +20 -0
  47. package/dist/components/molecules/TagCloud.d.ts +18 -0
  48. package/dist/components/molecules/TeamCard.d.ts +18 -0
  49. package/dist/components/molecules/index.d.ts +19 -0
  50. package/dist/components/molecules/svg/AIGenerates.d.ts +7 -0
  51. package/dist/components/molecules/svg/ClosedCircuit.d.ts +7 -0
  52. package/dist/components/molecules/svg/CommunityOwnership.d.ts +7 -0
  53. package/dist/components/molecules/svg/CompileAnywhere.d.ts +7 -0
  54. package/dist/components/molecules/svg/ComposableModels.d.ts +7 -0
  55. package/dist/components/molecules/svg/DescribeProveDeploy.d.ts +7 -0
  56. package/dist/components/molecules/svg/DomainGrid.d.ts +7 -0
  57. package/dist/components/molecules/svg/EventBus.d.ts +7 -0
  58. package/dist/components/molecules/svg/OrbitalUnit.d.ts +7 -0
  59. package/dist/components/molecules/svg/PlanVerifyRemember.d.ts +7 -0
  60. package/dist/components/molecules/svg/ProveCorrect.d.ts +7 -0
  61. package/dist/components/molecules/svg/ServiceLayers.d.ts +7 -0
  62. package/dist/components/molecules/svg/SharedReality.d.ts +7 -0
  63. package/dist/components/molecules/svg/StandardLibrary.d.ts +7 -0
  64. package/dist/components/molecules/svg/StateMachine.d.ts +7 -0
  65. package/dist/components/molecules/svg/WorldModel.d.ts +7 -0
  66. package/dist/components/molecules/svg/index.d.ts +16 -0
  67. package/dist/components/organisms/CaseStudyOrganism.d.ts +19 -0
  68. package/dist/components/organisms/FeatureGridOrganism.d.ts +20 -0
  69. package/dist/components/organisms/HeroOrganism.d.ts +18 -0
  70. package/dist/components/organisms/PricingOrganism.d.ts +19 -0
  71. package/dist/components/organisms/ShowcaseOrganism.d.ts +20 -0
  72. package/dist/components/organisms/StatsOrganism.d.ts +17 -0
  73. package/dist/components/organisms/StepFlowOrganism.d.ts +20 -0
  74. package/dist/components/organisms/TeamOrganism.d.ts +18 -0
  75. package/dist/components/organisms/game/three/index.cjs +2525 -0
  76. package/dist/components/organisms/game/three/index.js +1795 -50
  77. package/dist/components/organisms/index.d.ts +9 -0
  78. package/dist/components/organisms/marketing-types.d.ts +87 -0
  79. package/dist/components/templates/AboutPageTemplate.d.ts +26 -0
  80. package/dist/components/templates/FeatureDetailPageTemplate.d.ts +27 -0
  81. package/dist/components/templates/LandingPageTemplate.d.ts +31 -0
  82. package/dist/components/templates/PricingPageTemplate.d.ts +26 -0
  83. package/dist/components/templates/index.d.ts +4 -0
  84. package/dist/context/index.cjs +550 -0
  85. package/dist/context/index.js +420 -6
  86. package/dist/docs/index.cjs +4015 -0
  87. package/dist/docs/index.d.cts +412 -0
  88. package/dist/docs/index.d.ts +29 -0
  89. package/dist/docs/index.js +3977 -0
  90. package/dist/hooks/index.cjs +2606 -0
  91. package/dist/hooks/index.js +2535 -8
  92. package/dist/illustrations/index.cjs +3004 -0
  93. package/dist/illustrations/index.d.cts +261 -0
  94. package/dist/illustrations/index.d.ts +35 -0
  95. package/dist/illustrations/index.js +2971 -0
  96. package/dist/{chunk-XL7WB2O5.js → lib/index.cjs} +454 -274
  97. package/dist/lib/index.js +1407 -3
  98. package/dist/locales/index.cjs +340 -0
  99. package/dist/locales/index.js +105 -2
  100. package/dist/marketing/index.cjs +4680 -0
  101. package/dist/marketing/index.d.cts +831 -0
  102. package/dist/marketing/index.d.ts +62 -0
  103. package/dist/marketing/index.js +4623 -0
  104. package/dist/providers/index.cjs +4811 -0
  105. package/dist/providers/index.js +4765 -11
  106. package/dist/{chunk-K2D5D3WK.js → renderer/index.cjs} +101 -42
  107. package/dist/renderer/index.js +1036 -2
  108. package/dist/runtime/index.cjs +4400 -0
  109. package/dist/runtime/index.js +3615 -19
  110. package/dist/{chunk-N7MVUW4R.js → stores/index.cjs} +24 -1
  111. package/dist/stores/index.js +194 -2
  112. package/dist/tsup.config.d.ts +2 -1
  113. package/package.json +27 -12
  114. package/tailwind-preset.cjs +9 -0
  115. package/themes/almadar-website.css +195 -0
  116. package/themes/index.css +23 -20
  117. package/dist/chunk-3HJHHULT.js +0 -93
  118. package/dist/chunk-3JGAROCW.js +0 -149
  119. package/dist/chunk-4N3BAPDB.js +0 -1667
  120. package/dist/chunk-CDIOHSKG.js +0 -661
  121. package/dist/chunk-DKQN5FVU.js +0 -279
  122. package/dist/chunk-JJHCOO34.js +0 -375
  123. package/dist/chunk-PKBMQBKP.js +0 -5
  124. package/dist/chunk-QIABKRCN.js +0 -107
  125. package/dist/chunk-SD3KVCY6.js +0 -1465
  126. package/dist/chunk-YXZM3WCF.js +0 -222
@@ -1,1465 +0,0 @@
1
- import { subscribe, getSnapshot, clearEntities, removeEntity, updateSingleton, updateEntity, spawnEntity, getSingleton, getAllEntities, getByType, getEntity } from './chunk-N7MVUW4R.js';
2
- import { apiClient } from './chunk-3HJHHULT.js';
3
- import { SelectionContext, entityDataKeys, useEntityList } from './chunk-CDIOHSKG.js';
4
- import { useEventBus } from './chunk-YXZM3WCF.js';
5
- import { useCallback, useState, useEffect, useMemo, useContext, useSyncExternalStore, useRef } from 'react';
6
- import { useQueryClient, useMutation, useQuery } from '@tanstack/react-query';
7
-
8
- function useOrbitalHistory(options) {
9
- const { appId, authToken, userId, onHistoryChange, onRevertSuccess } = options;
10
- const getHeaders = useCallback(() => {
11
- const headers = {
12
- "Content-Type": "application/json"
13
- };
14
- if (authToken) {
15
- headers["Authorization"] = `Bearer ${authToken}`;
16
- }
17
- if (userId) {
18
- headers["x-user-id"] = userId;
19
- }
20
- return headers;
21
- }, [authToken, userId]);
22
- const [timeline, setTimeline] = useState([]);
23
- const [currentVersion, setCurrentVersion] = useState(1);
24
- const [isLoading, setIsLoading] = useState(false);
25
- const [error, setError] = useState(null);
26
- const refresh = useCallback(async () => {
27
- if (!appId) return;
28
- setIsLoading(true);
29
- setError(null);
30
- try {
31
- const headers = getHeaders();
32
- const [changesetsRes, snapshotsRes] = await Promise.all([
33
- fetch(`/api/graphs/${appId}/history/changesets`, { headers }),
34
- fetch(`/api/graphs/${appId}/history/snapshots`, { headers })
35
- ]);
36
- if (!changesetsRes.ok) {
37
- throw new Error(`Failed to fetch changesets: ${changesetsRes.status}`);
38
- }
39
- if (!snapshotsRes.ok) {
40
- throw new Error(`Failed to fetch snapshots: ${snapshotsRes.status}`);
41
- }
42
- const changesetsData = await changesetsRes.json();
43
- const snapshotsData = await snapshotsRes.json();
44
- const changesetItems = (changesetsData.changesets || []).map((cs) => ({
45
- id: cs.id,
46
- type: "changeset",
47
- version: cs.version,
48
- timestamp: cs.timestamp,
49
- description: `Version ${cs.version}`,
50
- source: cs.source,
51
- summary: cs.summary
52
- }));
53
- const snapshotItems = (snapshotsData.snapshots || []).map((snap) => ({
54
- id: snap.id,
55
- type: "snapshot",
56
- version: snap.version,
57
- timestamp: snap.timestamp,
58
- description: snap.reason || `Snapshot v${snap.version}`,
59
- reason: snap.reason
60
- }));
61
- const mergedTimeline = [...changesetItems, ...snapshotItems].sort(
62
- (a, b) => b.timestamp - a.timestamp
63
- );
64
- setTimeline(mergedTimeline);
65
- if (mergedTimeline.length > 0) {
66
- setCurrentVersion(mergedTimeline[0].version);
67
- }
68
- } catch (err) {
69
- console.error("[useOrbitalHistory] Failed to load history:", err);
70
- setError(err instanceof Error ? err.message : "Failed to load history");
71
- } finally {
72
- setIsLoading(false);
73
- }
74
- }, [appId, getHeaders]);
75
- const revertToSnapshot = useCallback(async (snapshotId) => {
76
- if (!appId) {
77
- return { success: false, error: "No app ID provided" };
78
- }
79
- try {
80
- const response = await fetch(`/api/graphs/${appId}/history/revert/${snapshotId}`, {
81
- method: "POST",
82
- headers: getHeaders()
83
- });
84
- if (!response.ok) {
85
- const errorData = await response.json().catch(() => ({}));
86
- throw new Error(errorData.error || `Failed to revert: ${response.status}`);
87
- }
88
- const data = await response.json();
89
- if (data.success && data.schema) {
90
- await refresh();
91
- onRevertSuccess?.(data.schema);
92
- return {
93
- success: true,
94
- restoredSchema: data.schema
95
- };
96
- }
97
- return {
98
- success: false,
99
- error: data.error || "Unknown error during revert"
100
- };
101
- } catch (err) {
102
- console.error("[useOrbitalHistory] Failed to revert:", err);
103
- return {
104
- success: false,
105
- error: err instanceof Error ? err.message : "Failed to revert"
106
- };
107
- }
108
- }, [appId, getHeaders, refresh, onRevertSuccess]);
109
- useEffect(() => {
110
- if (appId && authToken && userId) {
111
- refresh();
112
- }
113
- }, [appId, authToken, userId]);
114
- useEffect(() => {
115
- onHistoryChange?.(timeline);
116
- }, [timeline]);
117
- return {
118
- timeline,
119
- currentVersion,
120
- isLoading,
121
- error,
122
- revertToSnapshot,
123
- refresh
124
- };
125
- }
126
- function useFileSystem() {
127
- const [status, setStatus] = useState("idle");
128
- const [error, setError] = useState(null);
129
- const [isLoading, setIsLoading] = useState(false);
130
- const [files, setFiles] = useState([]);
131
- const [selectedFile, setSelectedFile] = useState(null);
132
- const [selectedPath, setSelectedPath] = useState(null);
133
- const [previewUrl, setPreviewUrl] = useState(null);
134
- const [fileContents, setFileContents] = useState(/* @__PURE__ */ new Map());
135
- const boot = useCallback(async () => {
136
- setStatus("booting");
137
- setError(null);
138
- setIsLoading(true);
139
- try {
140
- console.log("[useFileSystem] Booting WebContainer...");
141
- await new Promise((resolve) => setTimeout(resolve, 100));
142
- setStatus("ready");
143
- } catch (err) {
144
- setError(err instanceof Error ? err.message : "Failed to boot");
145
- setStatus("error");
146
- } finally {
147
- setIsLoading(false);
148
- }
149
- }, []);
150
- const mountFiles = useCallback(async (filesToMount) => {
151
- setIsLoading(true);
152
- try {
153
- let filesArray;
154
- if (Array.isArray(filesToMount)) {
155
- filesArray = filesToMount;
156
- } else {
157
- filesArray = [];
158
- const flattenTree = (obj, basePath = "") => {
159
- for (const [key, value] of Object.entries(obj)) {
160
- const path = basePath ? `${basePath}/${key}` : key;
161
- if (value && typeof value === "object" && "file" in value) {
162
- const fileObj = value;
163
- filesArray.push({ path, content: fileObj.file.contents || "" });
164
- } else if (value && typeof value === "object" && "directory" in value) {
165
- const dirObj = value;
166
- flattenTree(dirObj.directory, path);
167
- }
168
- }
169
- };
170
- flattenTree(filesToMount);
171
- }
172
- const newContents = /* @__PURE__ */ new Map();
173
- for (const file of filesArray) {
174
- newContents.set(file.path, file.content);
175
- }
176
- setFileContents(newContents);
177
- const newTree = [];
178
- for (const file of filesArray) {
179
- const parts = file.path.split("/").filter(Boolean);
180
- let current = newTree;
181
- for (let i = 0; i < parts.length; i++) {
182
- const part = parts[i];
183
- const isFile = i === parts.length - 1;
184
- const currentPath = "/" + parts.slice(0, i + 1).join("/");
185
- let node = current.find((n) => n.name === part);
186
- if (!node) {
187
- node = {
188
- path: currentPath,
189
- name: part,
190
- type: isFile ? "file" : "directory",
191
- children: isFile ? void 0 : []
192
- };
193
- current.push(node);
194
- }
195
- if (!isFile && node && node.children) {
196
- current = node.children;
197
- }
198
- }
199
- }
200
- setFiles(newTree);
201
- setStatus("running");
202
- } catch (err) {
203
- console.error("[useFileSystem] Failed to mount files:", err);
204
- } finally {
205
- setIsLoading(false);
206
- }
207
- }, []);
208
- const readFile = useCallback(async (path) => {
209
- return fileContents.get(path) || "";
210
- }, [fileContents]);
211
- const writeFile = useCallback(async (path, content) => {
212
- setFileContents((prev) => {
213
- const next = new Map(prev);
214
- next.set(path, content);
215
- return next;
216
- });
217
- }, []);
218
- const selectFile = useCallback(async (path) => {
219
- const content = fileContents.get(path) || "";
220
- const ext = path.split(".").pop()?.toLowerCase() || "";
221
- const languageMap = {
222
- ts: "typescript",
223
- tsx: "typescript",
224
- js: "javascript",
225
- jsx: "javascript",
226
- json: "json",
227
- md: "markdown",
228
- css: "css",
229
- html: "html",
230
- orb: "json"
231
- };
232
- setSelectedPath(path);
233
- setSelectedFile({
234
- path,
235
- content,
236
- language: languageMap[ext] || "plaintext",
237
- isDirty: false
238
- });
239
- }, [fileContents]);
240
- const updateContent = useCallback((pathOrContent, contentArg) => {
241
- const path = contentArg !== void 0 ? pathOrContent : selectedPath;
242
- const content = contentArg !== void 0 ? contentArg : pathOrContent;
243
- if (!path) {
244
- console.warn("[useFileSystem] updateContent called without path and no file selected");
245
- return;
246
- }
247
- setFileContents((prev) => {
248
- const next = new Map(prev);
249
- next.set(path, content);
250
- return next;
251
- });
252
- if (selectedPath === path) {
253
- setSelectedFile((prev) => prev ? { ...prev, content, isDirty: true } : null);
254
- }
255
- }, [selectedPath]);
256
- const updateSelectedContent = useCallback((content) => {
257
- setSelectedFile((prev) => prev ? { ...prev, content, isDirty: true } : null);
258
- }, []);
259
- const refreshTree = useCallback(async () => {
260
- console.log("[useFileSystem] Refreshing tree");
261
- }, []);
262
- const runCommand = useCallback(async (command) => {
263
- console.log("[useFileSystem] Running command:", command);
264
- return { exitCode: 0, output: "" };
265
- }, []);
266
- const startDevServer = useCallback(async () => {
267
- console.log("[useFileSystem] Starting dev server");
268
- setPreviewUrl("http://localhost:5173");
269
- }, []);
270
- return {
271
- status,
272
- error,
273
- isLoading,
274
- files,
275
- selectedFile,
276
- selectedPath,
277
- previewUrl,
278
- boot,
279
- mountFiles,
280
- readFile,
281
- writeFile,
282
- selectFile,
283
- updateContent,
284
- updateSelectedContent,
285
- refreshTree,
286
- runCommand,
287
- startDevServer
288
- };
289
- }
290
- var defaultManifest = {
291
- languages: {
292
- typescript: { extensions: [".ts", ".tsx"], icon: "ts", color: "#3178c6" },
293
- javascript: { extensions: [".js", ".jsx"], icon: "js", color: "#f7df1e" },
294
- json: { extensions: [".json", ".orb"], icon: "json", color: "#000000" },
295
- css: { extensions: [".css"], icon: "css", color: "#264de4" },
296
- html: { extensions: [".html"], icon: "html", color: "#e34c26" },
297
- markdown: { extensions: [".md", ".mdx"], icon: "md", color: "#083fa1" }
298
- },
299
- extensions: []
300
- };
301
- function useExtensions(options) {
302
- const { appId, loadOnMount = true } = options;
303
- const [extensions, setExtensions] = useState([]);
304
- const [manifest] = useState(defaultManifest);
305
- const [isLoading, setIsLoading] = useState(false);
306
- const [error, setError] = useState(null);
307
- const loadExtension = useCallback(async (extensionId) => {
308
- console.log("[useExtensions] Loading extension:", extensionId);
309
- }, []);
310
- const loadExtensions = useCallback(async () => {
311
- setIsLoading(true);
312
- setError(null);
313
- try {
314
- const defaultExtensions = [
315
- { id: "typescript", name: "TypeScript", language: "typescript", loaded: true },
316
- { id: "javascript", name: "JavaScript", language: "javascript", loaded: true },
317
- { id: "json", name: "JSON", language: "json", loaded: true },
318
- { id: "css", name: "CSS", language: "css", loaded: true },
319
- { id: "html", name: "HTML", language: "html", loaded: true },
320
- { id: "markdown", name: "Markdown", language: "markdown", loaded: true }
321
- ];
322
- setExtensions(defaultExtensions);
323
- } catch (err) {
324
- setError(err instanceof Error ? err.message : "Failed to load extensions");
325
- } finally {
326
- setIsLoading(false);
327
- }
328
- }, []);
329
- const getExtensionForFile = useCallback((filename) => {
330
- const ext = filename.split(".").pop()?.toLowerCase();
331
- if (!ext) return null;
332
- const languageMap = {
333
- ts: "typescript",
334
- tsx: "typescript",
335
- js: "javascript",
336
- jsx: "javascript",
337
- json: "json",
338
- md: "markdown",
339
- css: "css",
340
- html: "html",
341
- orb: "json"
342
- };
343
- const language = languageMap[ext];
344
- if (!language) return null;
345
- return extensions.find((e) => e.language === language) || null;
346
- }, [extensions]);
347
- useEffect(() => {
348
- if (!appId || !loadOnMount) return;
349
- const loadExtensions2 = async () => {
350
- setIsLoading(true);
351
- setError(null);
352
- try {
353
- const defaultExtensions = [
354
- { id: "typescript", name: "TypeScript", language: "typescript", loaded: true },
355
- { id: "javascript", name: "JavaScript", language: "javascript", loaded: true },
356
- { id: "json", name: "JSON", language: "json", loaded: true },
357
- { id: "css", name: "CSS", language: "css", loaded: true },
358
- { id: "html", name: "HTML", language: "html", loaded: true },
359
- { id: "markdown", name: "Markdown", language: "markdown", loaded: true }
360
- ];
361
- setExtensions(defaultExtensions);
362
- } catch (err) {
363
- setError(err instanceof Error ? err.message : "Failed to load extensions");
364
- } finally {
365
- setIsLoading(false);
366
- }
367
- };
368
- loadExtensions2();
369
- }, [appId, loadOnMount]);
370
- return {
371
- extensions,
372
- manifest,
373
- isLoading,
374
- error,
375
- loadExtension,
376
- loadExtensions,
377
- getExtensionForFile
378
- };
379
- }
380
- function useFileEditor(options) {
381
- const { extensions, fileSystem, onSchemaUpdate } = options;
382
- const [openFiles, setOpenFiles] = useState([]);
383
- const [activeFilePath, setActiveFilePath] = useState(null);
384
- const [isSaving, setIsSaving] = useState(false);
385
- const activeFile = openFiles.find((f) => f.path === activeFilePath) || null;
386
- const openFile = useCallback(async (path) => {
387
- const existing = openFiles.find((f) => f.path === path);
388
- if (existing) {
389
- setActiveFilePath(path);
390
- return;
391
- }
392
- try {
393
- const content = await fileSystem.readFile(path);
394
- const ext = extensions.getExtensionForFile(path);
395
- const newFile = {
396
- path,
397
- content,
398
- isDirty: false,
399
- language: ext?.language
400
- };
401
- setOpenFiles((prev) => [...prev, newFile]);
402
- setActiveFilePath(path);
403
- } catch (err) {
404
- console.error("[useFileEditor] Failed to open file:", err);
405
- }
406
- }, [openFiles, fileSystem, extensions]);
407
- const closeFile = useCallback((path) => {
408
- setOpenFiles((prev) => prev.filter((f) => f.path !== path));
409
- if (activeFilePath === path) {
410
- const remaining = openFiles.filter((f) => f.path !== path);
411
- setActiveFilePath(remaining.length > 0 ? remaining[0].path : null);
412
- }
413
- }, [activeFilePath, openFiles]);
414
- const setActiveFile = useCallback((path) => {
415
- setActiveFilePath(path);
416
- }, []);
417
- const updateFileContent = useCallback((path, content) => {
418
- setOpenFiles(
419
- (prev) => prev.map(
420
- (f) => f.path === path ? { ...f, content, isDirty: true } : f
421
- )
422
- );
423
- }, []);
424
- const handleFileEdit = useCallback(async (path, content) => {
425
- try {
426
- await fileSystem.writeFile(path, content);
427
- let action = "saved";
428
- if (path.endsWith(".orb") || path.endsWith("schema.json")) {
429
- try {
430
- const schema = JSON.parse(content);
431
- await onSchemaUpdate?.(schema);
432
- action = "updated_schema";
433
- } catch {
434
- }
435
- } else if (path.includes("/extensions/")) {
436
- action = path.endsWith(".new") ? "converted_extension" : "saved_extension";
437
- }
438
- return { success: true, action };
439
- } catch (err) {
440
- return {
441
- success: false,
442
- error: err instanceof Error ? err.message : "Failed to save file"
443
- };
444
- }
445
- }, [fileSystem, onSchemaUpdate]);
446
- const saveFile = useCallback(async (path) => {
447
- const file = openFiles.find((f) => f.path === path);
448
- if (!file) return;
449
- setIsSaving(true);
450
- try {
451
- await fileSystem.writeFile(path, file.content);
452
- setOpenFiles(
453
- (prev) => prev.map(
454
- (f) => f.path === path ? { ...f, isDirty: false } : f
455
- )
456
- );
457
- if (path.endsWith(".orb") || path.endsWith("schema.json")) {
458
- try {
459
- const schema = JSON.parse(file.content);
460
- await onSchemaUpdate?.(schema);
461
- } catch {
462
- }
463
- }
464
- } catch (err) {
465
- console.error("[useFileEditor] Failed to save file:", err);
466
- } finally {
467
- setIsSaving(false);
468
- }
469
- }, [openFiles, fileSystem, onSchemaUpdate]);
470
- const saveAllFiles = useCallback(async () => {
471
- setIsSaving(true);
472
- try {
473
- const dirtyFiles = openFiles.filter((f) => f.isDirty);
474
- for (const file of dirtyFiles) {
475
- await saveFile(file.path);
476
- }
477
- } finally {
478
- setIsSaving(false);
479
- }
480
- }, [openFiles, saveFile]);
481
- return {
482
- openFiles,
483
- activeFile,
484
- isSaving,
485
- openFile,
486
- closeFile,
487
- setActiveFile,
488
- updateFileContent,
489
- handleFileEdit,
490
- saveFile,
491
- saveAllFiles
492
- };
493
- }
494
- function useCompile() {
495
- const [isCompiling, setIsCompiling] = useState(false);
496
- const [stage, setStage] = useState("idle");
497
- const [lastResult, setLastResult] = useState(null);
498
- const [error, setError] = useState(null);
499
- const compileSchema = useCallback(async (schema) => {
500
- setIsCompiling(true);
501
- setStage("compiling");
502
- setError(null);
503
- try {
504
- console.log("[useCompile] Compiling schema:", schema.name);
505
- const result = {
506
- success: true,
507
- files: []
508
- };
509
- setLastResult(result);
510
- setStage("done");
511
- return result;
512
- } catch (err) {
513
- const errorMessage = err instanceof Error ? err.message : "Compilation failed";
514
- setError(errorMessage);
515
- setStage("error");
516
- setLastResult({ success: false, errors: [errorMessage] });
517
- return null;
518
- } finally {
519
- setIsCompiling(false);
520
- }
521
- }, []);
522
- return {
523
- isCompiling,
524
- stage,
525
- lastResult,
526
- error,
527
- compileSchema
528
- };
529
- }
530
- function usePreview(options) {
531
- const [previewUrl, setPreviewUrl] = useState(null);
532
- const [isLoading, setIsLoading] = useState(!!options?.appId);
533
- const [error, setError] = useState(null);
534
- const [loadError, setLoadError] = useState(null);
535
- const [app, setApp] = useState(null);
536
- const [isFullscreen, setIsFullscreen] = useState(false);
537
- const [isExecutingEvent, setIsExecutingEvent] = useState(false);
538
- const [errorToast, setErrorToast] = useState(null);
539
- const [currentStateName, setCurrentStateName] = useState(null);
540
- const [notificationsList, setNotificationsList] = useState([]);
541
- const [isPanelOpen, setIsPanelOpen] = useState(false);
542
- const notifications = useMemo(() => ({
543
- notifications: notificationsList,
544
- isPanelOpen,
545
- closePanel: () => setIsPanelOpen(false),
546
- dismissNotification: (id) => {
547
- setNotificationsList((prev) => prev.filter((n) => n.id !== id));
548
- },
549
- markAsRead: (id) => {
550
- setNotificationsList(
551
- (prev) => prev.map((n) => n.id === id ? { ...n, read: true } : n)
552
- );
553
- },
554
- clearAll: () => setNotificationsList([])
555
- }), [notificationsList, isPanelOpen]);
556
- useEffect(() => {
557
- const appId = options?.appId;
558
- if (!appId) {
559
- setApp(null);
560
- setIsLoading(false);
561
- return;
562
- }
563
- console.log("[usePreview] Setting up preview for app:", appId);
564
- setPreviewUrl(`/api/orbitals/${appId}`);
565
- setIsLoading(false);
566
- }, [options?.appId]);
567
- const startPreview = useCallback(async () => {
568
- console.log("[usePreview] startPreview called");
569
- }, []);
570
- const stopPreview = useCallback(async () => {
571
- setIsLoading(true);
572
- try {
573
- console.log("[usePreview] Stopping preview server...");
574
- setPreviewUrl(null);
575
- setApp(null);
576
- } finally {
577
- setIsLoading(false);
578
- }
579
- }, []);
580
- const refresh = useCallback(async () => {
581
- if (!previewUrl) return;
582
- console.log("[usePreview] Refreshing preview...");
583
- setPreviewUrl(`${previewUrl.split("?")[0]}?t=${Date.now()}`);
584
- }, [previewUrl]);
585
- const handleRefresh = useCallback(async () => {
586
- console.log("[usePreview] Handle refresh...");
587
- await refresh();
588
- }, [refresh]);
589
- const handleReset = useCallback(async () => {
590
- console.log("[usePreview] Resetting preview...");
591
- setError(null);
592
- setLoadError(null);
593
- setErrorToast(null);
594
- setIsExecutingEvent(false);
595
- setCurrentStateName(null);
596
- }, []);
597
- const toggleFullscreen = useCallback(() => {
598
- setIsFullscreen((prev) => !prev);
599
- }, []);
600
- const dismissErrorToast = useCallback(() => {
601
- setErrorToast(null);
602
- }, []);
603
- return {
604
- previewUrl,
605
- isLoading,
606
- error,
607
- loadError,
608
- app,
609
- isFullscreen,
610
- isExecutingEvent,
611
- errorToast,
612
- currentStateName,
613
- notifications,
614
- startPreview,
615
- stopPreview,
616
- refresh,
617
- handleRefresh,
618
- handleReset,
619
- toggleFullscreen,
620
- setErrorToast,
621
- dismissErrorToast
622
- };
623
- }
624
- function useAgentChat(options) {
625
- const [messages, setMessages] = useState([]);
626
- const [status, setStatus] = useState("idle");
627
- const [activities, setActivities] = useState([]);
628
- const [todos, setTodos] = useState([]);
629
- const [schemaDiffs, setSchemaDiffs] = useState([]);
630
- const [isLoading, setIsLoading] = useState(false);
631
- const [error, setError] = useState(null);
632
- const [threadId] = useState(null);
633
- const [interrupt, setInterrupt] = useState(null);
634
- const sendMessage = useCallback(async (content) => {
635
- setIsLoading(true);
636
- setStatus("running");
637
- setError(null);
638
- try {
639
- const userMessage = {
640
- id: Date.now().toString(),
641
- role: "user",
642
- content,
643
- timestamp: Date.now()
644
- };
645
- setMessages((prev) => [...prev, userMessage]);
646
- console.log("[useAgentChat] Sending message:", content);
647
- const assistantMessage = {
648
- id: (Date.now() + 1).toString(),
649
- role: "assistant",
650
- content: "Agent chat is not yet implemented.",
651
- timestamp: Date.now()
652
- };
653
- setMessages((prev) => [...prev, assistantMessage]);
654
- setStatus("idle");
655
- options?.onComplete?.();
656
- } catch (err) {
657
- setError(err instanceof Error ? err.message : "Failed to send message");
658
- setStatus("error");
659
- } finally {
660
- setIsLoading(false);
661
- }
662
- }, [options]);
663
- const startGeneration = useCallback(async (skill, prompt, genOptions) => {
664
- setStatus("running");
665
- setIsLoading(true);
666
- setError(null);
667
- const skillName = Array.isArray(skill) ? skill[0] : skill;
668
- try {
669
- console.log("[useAgentChat] Starting generation:", skillName, prompt, genOptions);
670
- await new Promise((resolve) => setTimeout(resolve, 100));
671
- setStatus("complete");
672
- options?.onComplete?.();
673
- } catch (err) {
674
- setError(err instanceof Error ? err.message : "Generation failed");
675
- setStatus("error");
676
- } finally {
677
- setIsLoading(false);
678
- }
679
- }, [options]);
680
- const continueConversation = useCallback(async (message) => {
681
- console.log("[useAgentChat] Continue conversation", message);
682
- }, []);
683
- const resumeWithDecision = useCallback(async (decisions) => {
684
- console.log("[useAgentChat] Resume with decision:", decisions);
685
- setInterrupt(null);
686
- }, []);
687
- const cancel = useCallback(() => {
688
- setStatus("idle");
689
- setIsLoading(false);
690
- setInterrupt(null);
691
- }, []);
692
- const clearMessages = useCallback(() => {
693
- setMessages([]);
694
- }, []);
695
- const clearHistory = useCallback(() => {
696
- setMessages([]);
697
- setActivities([]);
698
- setTodos([]);
699
- setSchemaDiffs([]);
700
- setError(null);
701
- }, []);
702
- return {
703
- messages,
704
- status,
705
- activities,
706
- todos,
707
- schemaDiffs,
708
- isLoading,
709
- error,
710
- threadId,
711
- interrupt,
712
- sendMessage,
713
- startGeneration,
714
- continueConversation,
715
- resumeWithDecision,
716
- cancel,
717
- clearMessages,
718
- clearHistory
719
- };
720
- }
721
- function useValidation() {
722
- const [result, setResult] = useState(null);
723
- const [isValidating, setIsValidating] = useState(false);
724
- const [error, setError] = useState(null);
725
- const [stage, setStage] = useState("idle");
726
- const [isFixing, setIsFixing] = useState(false);
727
- const [progressMessage, setProgressMessage] = useState(null);
728
- const validate = useCallback(async (appId) => {
729
- setIsValidating(true);
730
- setError(null);
731
- setStage("validating");
732
- setProgressMessage("Validating schema...");
733
- try {
734
- console.log("[useValidation] Validating app:", appId);
735
- const validationResult = {
736
- valid: true,
737
- errors: [],
738
- warnings: []
739
- };
740
- setResult(validationResult);
741
- setStage("complete");
742
- setProgressMessage(null);
743
- return validationResult;
744
- } catch (err) {
745
- const errorMessage = err instanceof Error ? err.message : "Validation failed";
746
- setError(errorMessage);
747
- const failedResult = {
748
- valid: false,
749
- errors: [{ code: "VALIDATION_ERROR", message: errorMessage, severity: "error" }],
750
- warnings: []
751
- };
752
- setResult(failedResult);
753
- setStage("complete");
754
- setProgressMessage(null);
755
- return failedResult;
756
- } finally {
757
- setIsValidating(false);
758
- }
759
- }, []);
760
- const clearResult = useCallback(() => {
761
- setResult(null);
762
- setError(null);
763
- }, []);
764
- const reset = useCallback(() => {
765
- setResult(null);
766
- setError(null);
767
- setStage("idle");
768
- setIsFixing(false);
769
- setProgressMessage(null);
770
- setIsValidating(false);
771
- }, []);
772
- return {
773
- result,
774
- isValidating,
775
- error,
776
- stage,
777
- isFixing,
778
- progressMessage,
779
- errors: result?.errors ?? [],
780
- warnings: result?.warnings ?? [],
781
- isValid: result?.valid ?? false,
782
- validate,
783
- clearResult,
784
- reset
785
- };
786
- }
787
- function useDeepAgentGeneration() {
788
- const [requests, setRequests] = useState([]);
789
- const [currentRequest, setCurrentRequest] = useState(null);
790
- const [isGenerating, setIsGenerating] = useState(false);
791
- const [isLoading, setIsLoading] = useState(false);
792
- const [isComplete, setIsComplete] = useState(false);
793
- const [progress, setProgress] = useState({ stage: "idle", percent: 0, message: "" });
794
- const [error, setError] = useState(null);
795
- const [interrupt, setInterrupt] = useState(null);
796
- const generate = useCallback(async (prompt) => {
797
- setIsGenerating(true);
798
- setIsLoading(true);
799
- setIsComplete(false);
800
- setError(null);
801
- setProgress({ stage: "starting", percent: 0, message: "Starting generation..." });
802
- const request = {
803
- id: Date.now().toString(),
804
- prompt,
805
- status: "running"
806
- };
807
- setCurrentRequest(request);
808
- setRequests((prev) => [...prev, request]);
809
- try {
810
- console.log("[useDeepAgentGeneration] Generating from prompt:", prompt);
811
- await new Promise((resolve) => setTimeout(resolve, 100));
812
- request.status = "completed";
813
- setCurrentRequest(request);
814
- setIsComplete(true);
815
- setProgress({ stage: "complete", percent: 100, message: "Generation complete" });
816
- return null;
817
- } catch (err) {
818
- const errorMessage = err instanceof Error ? err.message : "Generation failed";
819
- setError(errorMessage);
820
- request.status = "failed";
821
- request.error = errorMessage;
822
- setCurrentRequest(request);
823
- return null;
824
- } finally {
825
- setIsGenerating(false);
826
- setIsLoading(false);
827
- }
828
- }, []);
829
- const startGeneration = useCallback(async (skill, prompt, _options) => {
830
- console.log("[useDeepAgentGeneration] Starting generation with skill:", skill);
831
- await generate(prompt);
832
- }, [generate]);
833
- const cancelGeneration = useCallback(() => {
834
- if (currentRequest) {
835
- currentRequest.status = "failed";
836
- currentRequest.error = "Cancelled by user";
837
- setCurrentRequest(null);
838
- }
839
- setIsGenerating(false);
840
- setIsLoading(false);
841
- setIsComplete(false);
842
- setProgress({ stage: "idle", percent: 0, message: "" });
843
- }, [currentRequest]);
844
- const clearRequests = useCallback(() => {
845
- setRequests([]);
846
- setCurrentRequest(null);
847
- setError(null);
848
- setProgress({ stage: "idle", percent: 0, message: "" });
849
- setIsComplete(false);
850
- }, []);
851
- const submitInterruptDecisions = useCallback((decisions) => {
852
- console.log("[useDeepAgentGeneration] Submitting interrupt decisions:", decisions);
853
- setInterrupt(null);
854
- }, []);
855
- return {
856
- requests,
857
- currentRequest,
858
- isGenerating,
859
- isLoading,
860
- isComplete,
861
- progress,
862
- error,
863
- interrupt,
864
- generate,
865
- startGeneration,
866
- cancelGeneration,
867
- clearRequests,
868
- submitInterruptDecisions
869
- };
870
- }
871
- var UI_EVENT_MAP = {
872
- // Form/CRUD events
873
- "UI:SAVE": "SAVE",
874
- "UI:CANCEL": "CANCEL",
875
- "UI:CLOSE": "CLOSE",
876
- "UI:VIEW": "VIEW",
877
- "UI:EDIT": "EDIT",
878
- "UI:DELETE": "DELETE",
879
- "UI:CREATE": "CREATE",
880
- "UI:SELECT": "SELECT",
881
- "UI:DESELECT": "DESELECT",
882
- "UI:SUBMIT": "SAVE",
883
- "UI:UPDATE_STATUS": "UPDATE_STATUS",
884
- "UI:SEARCH": "SEARCH",
885
- "UI:CLEAR_SEARCH": "CLEAR_SEARCH",
886
- "UI:ADD": "CREATE",
887
- // Game events (for closed circuit with GameMenu, GamePauseOverlay, GameOverScreen)
888
- "UI:PAUSE": "PAUSE",
889
- "UI:RESUME": "RESUME",
890
- "UI:RESTART": "RESTART",
891
- "UI:GAME_OVER": "GAME_OVER",
892
- "UI:START": "START",
893
- "UI:QUIT": "QUIT",
894
- "UI:INIT": "INIT"
895
- };
896
- function useUIEvents(dispatch, validEvents, eventBusInstance) {
897
- const defaultEventBus = useEventBus();
898
- const eventBus = eventBusInstance ?? defaultEventBus;
899
- const validEventsKey = validEvents ? validEvents.slice().sort().join(",") : "";
900
- const stableValidEvents = useMemo(
901
- () => validEvents,
902
- [validEventsKey]
903
- // intentional — validEventsKey is the stable dep, not validEvents array ref
904
- );
905
- useEffect(() => {
906
- const unsubscribes = [];
907
- Object.entries(UI_EVENT_MAP).forEach(([uiEvent, smEvent]) => {
908
- const handler = (event) => {
909
- if (!stableValidEvents || stableValidEvents.includes(smEvent)) {
910
- dispatch(smEvent, event.payload);
911
- }
912
- };
913
- const unsubscribe = eventBus.on(uiEvent, handler);
914
- unsubscribes.push(unsubscribe);
915
- });
916
- const genericHandler = (event) => {
917
- const eventName = event.payload?.event;
918
- if (eventName) {
919
- const smEvent = eventName;
920
- if (!stableValidEvents || stableValidEvents.includes(smEvent)) {
921
- dispatch(smEvent, event.payload);
922
- }
923
- }
924
- };
925
- const genericUnsubscribe = eventBus.on("UI:DISPATCH", genericHandler);
926
- unsubscribes.push(genericUnsubscribe);
927
- if (stableValidEvents) {
928
- stableValidEvents.forEach((smEvent) => {
929
- const uiPrefixedEvent = `UI:${smEvent}`;
930
- const alreadyMapped = Object.keys(UI_EVENT_MAP).includes(uiPrefixedEvent);
931
- if (!alreadyMapped) {
932
- const directHandler = (event) => {
933
- dispatch(smEvent, event.payload);
934
- };
935
- const unsubscribePrefixed = eventBus.on(
936
- uiPrefixedEvent,
937
- directHandler
938
- );
939
- unsubscribes.push(unsubscribePrefixed);
940
- const unsubscribeDirect = eventBus.on(smEvent, directHandler);
941
- unsubscribes.push(unsubscribeDirect);
942
- }
943
- });
944
- }
945
- return () => {
946
- unsubscribes.forEach((unsub) => {
947
- if (typeof unsub === "function") unsub();
948
- });
949
- };
950
- }, [eventBus, dispatch, stableValidEvents]);
951
- }
952
- function useSelectedEntity(eventBusInstance) {
953
- const defaultEventBus = useEventBus();
954
- const eventBus = eventBusInstance ?? defaultEventBus;
955
- const selectionContext = useSelectionContext();
956
- const [localSelected, setLocalSelected] = useState(null);
957
- const usingContext = selectionContext !== null;
958
- useEffect(() => {
959
- if (usingContext) return;
960
- const handleSelect = (event) => {
961
- const row = event.payload?.row;
962
- if (row) {
963
- setLocalSelected(row);
964
- }
965
- };
966
- const handleDeselect = () => {
967
- setLocalSelected(null);
968
- };
969
- const unsubSelect = eventBus.on("UI:SELECT", handleSelect);
970
- const unsubView = eventBus.on("UI:VIEW", handleSelect);
971
- const unsubDeselect = eventBus.on("UI:DESELECT", handleDeselect);
972
- const unsubClose = eventBus.on("UI:CLOSE", handleDeselect);
973
- const unsubCancel = eventBus.on("UI:CANCEL", handleDeselect);
974
- return () => {
975
- [unsubSelect, unsubView, unsubDeselect, unsubClose, unsubCancel].forEach(
976
- (unsub) => {
977
- if (typeof unsub === "function") unsub();
978
- }
979
- );
980
- };
981
- }, [eventBus, usingContext]);
982
- if (selectionContext) {
983
- return [selectionContext.selected, selectionContext.setSelected];
984
- }
985
- return [localSelected, setLocalSelected];
986
- }
987
- function useSelectionContext() {
988
- const context = useContext(SelectionContext);
989
- return context;
990
- }
991
- var ENTITY_EVENTS = {
992
- CREATE: "ENTITY_CREATE",
993
- UPDATE: "ENTITY_UPDATE",
994
- DELETE: "ENTITY_DELETE"
995
- };
996
- async function sendOrbitalEvent(orbitalName, eventPayload) {
997
- const response = await apiClient.post(
998
- `/orbitals/${orbitalName}/events`,
999
- eventPayload
1000
- );
1001
- return response;
1002
- }
1003
- function useOrbitalMutations(entityName, orbitalName, options) {
1004
- const queryClient = useQueryClient();
1005
- const events = {
1006
- create: options?.events?.create || ENTITY_EVENTS.CREATE,
1007
- update: options?.events?.update || ENTITY_EVENTS.UPDATE,
1008
- delete: options?.events?.delete || ENTITY_EVENTS.DELETE
1009
- };
1010
- const log = (message, data) => {
1011
- if (options?.debug) {
1012
- console.log(`[useOrbitalMutations:${orbitalName}] ${message}`, data ?? "");
1013
- }
1014
- };
1015
- const createMutation = useMutation({
1016
- mutationFn: async (data) => {
1017
- log("Creating entity", data);
1018
- return sendOrbitalEvent(orbitalName, {
1019
- event: events.create,
1020
- payload: { data, entityType: entityName }
1021
- });
1022
- },
1023
- onSuccess: (response) => {
1024
- log("Create succeeded", response);
1025
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1026
- },
1027
- onError: (error) => {
1028
- console.error(`[useOrbitalMutations] Create failed:`, error);
1029
- }
1030
- });
1031
- const updateMutation = useMutation({
1032
- mutationFn: async ({
1033
- id,
1034
- data
1035
- }) => {
1036
- log(`Updating entity ${id}`, data);
1037
- return sendOrbitalEvent(orbitalName, {
1038
- event: events.update,
1039
- entityId: id,
1040
- payload: { data, entityType: entityName }
1041
- });
1042
- },
1043
- onSuccess: (response, variables) => {
1044
- log("Update succeeded", response);
1045
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1046
- queryClient.invalidateQueries({
1047
- queryKey: entityDataKeys.detail(entityName, variables.id)
1048
- });
1049
- },
1050
- onError: (error) => {
1051
- console.error(`[useOrbitalMutations] Update failed:`, error);
1052
- }
1053
- });
1054
- const deleteMutation = useMutation({
1055
- mutationFn: async (id) => {
1056
- log(`Deleting entity ${id}`);
1057
- return sendOrbitalEvent(orbitalName, {
1058
- event: events.delete,
1059
- entityId: id,
1060
- payload: { entityType: entityName }
1061
- });
1062
- },
1063
- onSuccess: (response, id) => {
1064
- log("Delete succeeded", response);
1065
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1066
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
1067
- },
1068
- onError: (error) => {
1069
- console.error(`[useOrbitalMutations] Delete failed:`, error);
1070
- }
1071
- });
1072
- return {
1073
- // Async functions
1074
- createEntity: async (data) => {
1075
- return createMutation.mutateAsync(data);
1076
- },
1077
- updateEntity: async (id, data) => {
1078
- if (!id) {
1079
- console.warn("[useOrbitalMutations] Cannot update without ID");
1080
- return;
1081
- }
1082
- return updateMutation.mutateAsync({ id, data });
1083
- },
1084
- deleteEntity: async (id) => {
1085
- if (!id) {
1086
- console.warn("[useOrbitalMutations] Cannot delete without ID");
1087
- return;
1088
- }
1089
- return deleteMutation.mutateAsync(id);
1090
- },
1091
- // Mutation objects for fine-grained control
1092
- createMutation,
1093
- updateMutation,
1094
- deleteMutation,
1095
- // Aggregate states
1096
- isCreating: createMutation.isPending,
1097
- isUpdating: updateMutation.isPending,
1098
- isDeleting: deleteMutation.isPending,
1099
- isMutating: createMutation.isPending || updateMutation.isPending || deleteMutation.isPending,
1100
- // Errors
1101
- createError: createMutation.error,
1102
- updateError: updateMutation.error,
1103
- deleteError: deleteMutation.error
1104
- };
1105
- }
1106
- function useSendOrbitalEvent(orbitalName) {
1107
- const mutation = useMutation({
1108
- mutationFn: async (payload) => {
1109
- return sendOrbitalEvent(orbitalName, payload);
1110
- }
1111
- });
1112
- return {
1113
- sendEvent: async (event, payload, entityId) => {
1114
- return mutation.mutateAsync({ event, payload, entityId });
1115
- },
1116
- isPending: mutation.isPending,
1117
- error: mutation.error,
1118
- data: mutation.data
1119
- };
1120
- }
1121
-
1122
- // hooks/useEntityMutations.ts
1123
- function entityToCollection(entityName) {
1124
- return entityName.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase() + "-list";
1125
- }
1126
- function useCreateEntity(entityName) {
1127
- const queryClient = useQueryClient();
1128
- const collection = entityToCollection(entityName);
1129
- return useMutation({
1130
- mutationFn: async (data) => {
1131
- console.log(`[useCreateEntity] Creating ${entityName}:`, data);
1132
- const response = await apiClient.post(
1133
- `/${collection}`,
1134
- data
1135
- );
1136
- return response.data;
1137
- },
1138
- onSuccess: () => {
1139
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1140
- },
1141
- onError: (error) => {
1142
- console.error(`[useCreateEntity] Failed to create ${entityName}:`, error);
1143
- }
1144
- });
1145
- }
1146
- function useUpdateEntity(entityName) {
1147
- const queryClient = useQueryClient();
1148
- const collection = entityToCollection(entityName);
1149
- return useMutation({
1150
- mutationFn: async ({ id, data }) => {
1151
- console.log(`[useUpdateEntity] Updating ${entityName} ${id}:`, data);
1152
- const response = await apiClient.patch(
1153
- `/${collection}/${id}`,
1154
- data
1155
- );
1156
- return response.data;
1157
- },
1158
- onSuccess: (_, variables) => {
1159
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1160
- queryClient.invalidateQueries({ queryKey: entityDataKeys.detail(entityName, variables.id) });
1161
- },
1162
- onError: (error) => {
1163
- console.error(`[useUpdateEntity] Failed to update ${entityName}:`, error);
1164
- }
1165
- });
1166
- }
1167
- function useDeleteEntity(entityName) {
1168
- const queryClient = useQueryClient();
1169
- const collection = entityToCollection(entityName);
1170
- return useMutation({
1171
- mutationFn: async (id) => {
1172
- console.log(`[useDeleteEntity] Deleting ${entityName} ${id}`);
1173
- await apiClient.delete(`/${collection}/${id}`);
1174
- return { id };
1175
- },
1176
- onSuccess: (_, id) => {
1177
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1178
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
1179
- },
1180
- onError: (error) => {
1181
- console.error(`[useDeleteEntity] Failed to delete ${entityName}:`, error);
1182
- }
1183
- });
1184
- }
1185
- async function sendOrbitalMutation(orbitalName, event, entityId, payload) {
1186
- const response = await apiClient.post(
1187
- `/orbitals/${orbitalName}/events`,
1188
- { event, entityId, payload }
1189
- );
1190
- return response;
1191
- }
1192
- function useEntityMutations(entityName, options) {
1193
- const queryClient = useQueryClient();
1194
- const useOrbitalRoute = !!options?.orbitalName;
1195
- const events = {
1196
- create: options?.events?.create || ENTITY_EVENTS.CREATE,
1197
- update: options?.events?.update || ENTITY_EVENTS.UPDATE,
1198
- delete: options?.events?.delete || ENTITY_EVENTS.DELETE
1199
- };
1200
- const createMutation = useCreateEntity(entityName);
1201
- const updateMutation = useUpdateEntity(entityName);
1202
- const deleteMutation = useDeleteEntity(entityName);
1203
- const orbitalCreateMutation = useMutation({
1204
- mutationFn: async (data) => {
1205
- return sendOrbitalMutation(options.orbitalName, events.create, void 0, {
1206
- data,
1207
- entityType: entityName
1208
- });
1209
- },
1210
- onSuccess: () => {
1211
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1212
- }
1213
- });
1214
- const orbitalUpdateMutation = useMutation({
1215
- mutationFn: async ({ id, data }) => {
1216
- return sendOrbitalMutation(options.orbitalName, events.update, id, {
1217
- data,
1218
- entityType: entityName
1219
- });
1220
- },
1221
- onSuccess: (_, variables) => {
1222
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1223
- queryClient.invalidateQueries({
1224
- queryKey: entityDataKeys.detail(entityName, variables.id)
1225
- });
1226
- }
1227
- });
1228
- const orbitalDeleteMutation = useMutation({
1229
- mutationFn: async (id) => {
1230
- return sendOrbitalMutation(options.orbitalName, events.delete, id, {
1231
- entityType: entityName
1232
- });
1233
- },
1234
- onSuccess: (_, id) => {
1235
- queryClient.invalidateQueries({ queryKey: entityDataKeys.list(entityName) });
1236
- queryClient.removeQueries({ queryKey: entityDataKeys.detail(entityName, id) });
1237
- }
1238
- });
1239
- const activeMutations = {
1240
- create: useOrbitalRoute ? orbitalCreateMutation : createMutation,
1241
- update: useOrbitalRoute ? orbitalUpdateMutation : updateMutation,
1242
- delete: useOrbitalRoute ? orbitalDeleteMutation : deleteMutation
1243
- };
1244
- return {
1245
- // Async functions that can be called directly
1246
- // Accepts either (data) or (entityName, data) for compiler compatibility
1247
- createEntity: async (entityOrData, data) => {
1248
- const actualData = typeof entityOrData === "string" ? data : entityOrData;
1249
- if (!actualData) {
1250
- console.warn("[useEntityMutations] Cannot create entity without data");
1251
- return;
1252
- }
1253
- return activeMutations.create.mutateAsync(actualData);
1254
- },
1255
- updateEntity: async (id, data) => {
1256
- if (!id) {
1257
- console.warn("[useEntityMutations] Cannot update entity without ID");
1258
- return;
1259
- }
1260
- return activeMutations.update.mutateAsync({ id, data });
1261
- },
1262
- deleteEntity: async (id) => {
1263
- if (!id) {
1264
- console.warn("[useEntityMutations] Cannot delete entity without ID");
1265
- return;
1266
- }
1267
- return activeMutations.delete.mutateAsync(id);
1268
- },
1269
- // Mutation states for UI feedback
1270
- isCreating: activeMutations.create.isPending,
1271
- isUpdating: activeMutations.update.isPending,
1272
- isDeleting: activeMutations.delete.isPending,
1273
- createError: activeMutations.create.error,
1274
- updateError: activeMutations.update.error,
1275
- deleteError: activeMutations.delete.error
1276
- };
1277
- }
1278
- function useEntities() {
1279
- const entities = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
1280
- return {
1281
- entities,
1282
- getEntity,
1283
- getByType,
1284
- getAllEntities,
1285
- getSingleton,
1286
- spawnEntity,
1287
- updateEntity,
1288
- updateSingleton,
1289
- removeEntity,
1290
- clearEntities
1291
- };
1292
- }
1293
- function useEntity(id) {
1294
- const entities = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
1295
- return entities.get(id);
1296
- }
1297
- function useEntitiesByType(type) {
1298
- const entities = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
1299
- return [...entities.values()].filter((e) => e.type === type);
1300
- }
1301
- function useSingletonEntity(type) {
1302
- const entities = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
1303
- return [...entities.values()].find((e) => e.type === type);
1304
- }
1305
- function usePlayer() {
1306
- const player = useSingletonEntity("Player");
1307
- const update = useCallback((updates) => {
1308
- if (player) updateEntity(player.id, updates);
1309
- }, [player?.id]);
1310
- return { player, updatePlayer: update };
1311
- }
1312
- function usePhysics() {
1313
- const physics = useSingletonEntity("Physics");
1314
- const update = useCallback((updates) => {
1315
- if (physics) updateEntity(physics.id, updates);
1316
- }, [physics?.id]);
1317
- return { physics, updatePhysics: update };
1318
- }
1319
- function useInput() {
1320
- const input = useSingletonEntity("Input");
1321
- const update = useCallback((updates) => {
1322
- if (input) updateEntity(input.id, updates);
1323
- }, [input?.id]);
1324
- return { input, updateInput: update };
1325
- }
1326
- function useResolvedEntity(entity, data) {
1327
- const shouldFetch = !data && !!entity;
1328
- const fetched = useEntityList(entity, { skip: !shouldFetch });
1329
- return useMemo(() => {
1330
- if (data) {
1331
- return {
1332
- data,
1333
- isLocal: true,
1334
- isLoading: false,
1335
- error: null
1336
- };
1337
- }
1338
- return {
1339
- data: fetched.data,
1340
- isLocal: false,
1341
- isLoading: fetched.isLoading,
1342
- error: fetched.error
1343
- };
1344
- }, [data, fetched.data, fetched.isLoading, fetched.error]);
1345
- }
1346
- function getDistance(touches) {
1347
- const dx = touches[0].clientX - touches[1].clientX;
1348
- const dy = touches[0].clientY - touches[1].clientY;
1349
- return Math.sqrt(dx * dx + dy * dy);
1350
- }
1351
- function usePinchZoom(options = {}) {
1352
- const { minScale = 0.5, maxScale = 4 } = options;
1353
- const [scale, setScale] = useState(1);
1354
- const [isPinching, setIsPinching] = useState(false);
1355
- const initialDistance = useRef(0);
1356
- const initialScale = useRef(1);
1357
- const onTouchStart = useCallback((e) => {
1358
- if (e.touches.length === 2) {
1359
- initialDistance.current = getDistance(e.touches);
1360
- initialScale.current = scale;
1361
- setIsPinching(true);
1362
- }
1363
- }, [scale]);
1364
- const onTouchMove = useCallback((e) => {
1365
- if (e.touches.length !== 2 || !isPinching) return;
1366
- e.preventDefault();
1367
- const currentDistance = getDistance(e.touches);
1368
- const ratio = currentDistance / initialDistance.current;
1369
- const newScale = Math.min(maxScale, Math.max(minScale, initialScale.current * ratio));
1370
- setScale(newScale);
1371
- }, [isPinching, minScale, maxScale]);
1372
- const onTouchEnd = useCallback(() => {
1373
- setIsPinching(false);
1374
- }, []);
1375
- const resetZoom = useCallback(() => {
1376
- setScale(1);
1377
- }, []);
1378
- return {
1379
- scale,
1380
- isPinching,
1381
- gestureProps: {
1382
- onTouchStart,
1383
- onTouchMove,
1384
- onTouchEnd
1385
- },
1386
- resetZoom
1387
- };
1388
- }
1389
- var API_BASE = typeof process !== "undefined" && process.env?.VITE_API_URL ? process.env.VITE_API_URL : "http://localhost:3000";
1390
- function getUserId() {
1391
- return localStorage.getItem("userId") || "anonymous";
1392
- }
1393
- async function fetchWithAuth(endpoint, options) {
1394
- const userId = getUserId();
1395
- const response = await fetch(`${API_BASE}${endpoint}`, {
1396
- ...options,
1397
- headers: {
1398
- "Content-Type": "application/json",
1399
- "x-user-id": userId,
1400
- ...options?.headers
1401
- }
1402
- });
1403
- if (!response.ok) {
1404
- const error = await response.json().catch(() => ({ error: response.statusText }));
1405
- throw new Error(error.error || error.message || "Request failed");
1406
- }
1407
- return response.json();
1408
- }
1409
- function useGitHubStatus() {
1410
- return useQuery({
1411
- queryKey: ["github", "status"],
1412
- queryFn: () => fetchWithAuth("/api/github/status"),
1413
- staleTime: 6e4,
1414
- // 1 minute
1415
- retry: false
1416
- });
1417
- }
1418
- function useConnectGitHub() {
1419
- const connectGitHub = useCallback(() => {
1420
- const userId = getUserId();
1421
- const state = btoa(JSON.stringify({ userId, returnUrl: window.location.href }));
1422
- window.location.href = `${API_BASE}/api/github/oauth/authorize?state=${state}`;
1423
- }, []);
1424
- return { connectGitHub };
1425
- }
1426
- function useDisconnectGitHub() {
1427
- const queryClient = useQueryClient();
1428
- return useMutation({
1429
- mutationFn: () => fetchWithAuth("/api/github/disconnect", { method: "POST" }),
1430
- onSuccess: () => {
1431
- queryClient.invalidateQueries({ queryKey: ["github", "status"] });
1432
- queryClient.removeQueries({ queryKey: ["github", "repos"] });
1433
- }
1434
- });
1435
- }
1436
- function useGitHubRepos(page = 1, perPage = 30) {
1437
- return useQuery({
1438
- queryKey: ["github", "repos", page, perPage],
1439
- queryFn: () => fetchWithAuth(`/api/github/repos?page=${page}&per_page=${perPage}`),
1440
- enabled: true,
1441
- // Only fetch if user is connected
1442
- staleTime: 3e5
1443
- // 5 minutes
1444
- });
1445
- }
1446
- function useGitHubRepo(owner, repo, enabled = true) {
1447
- return useQuery({
1448
- queryKey: ["github", "repo", owner, repo],
1449
- queryFn: () => fetchWithAuth(`/api/github/repos/${owner}/${repo}`),
1450
- enabled: enabled && !!owner && !!repo,
1451
- staleTime: 3e5
1452
- // 5 minutes
1453
- });
1454
- }
1455
- function useGitHubBranches(owner, repo, enabled = true) {
1456
- return useQuery({
1457
- queryKey: ["github", "branches", owner, repo],
1458
- queryFn: () => fetchWithAuth(`/api/github/repos/${owner}/${repo}/branches`),
1459
- enabled: enabled && !!owner && !!repo,
1460
- staleTime: 6e4
1461
- // 1 minute
1462
- });
1463
- }
1464
-
1465
- export { ENTITY_EVENTS, useAgentChat, useCompile, useConnectGitHub, useCreateEntity, useDeepAgentGeneration, useDeleteEntity, useDisconnectGitHub, useEntities, useEntitiesByType, useEntity, useEntityMutations, useExtensions, useFileEditor, useFileSystem, useGitHubBranches, useGitHubRepo, useGitHubRepos, useGitHubStatus, useInput, useOrbitalHistory, useOrbitalMutations, usePhysics, usePinchZoom, usePlayer, usePreview, useResolvedEntity, useSelectedEntity, useSendOrbitalEvent, useSingletonEntity, useUIEvents, useUpdateEntity, useValidation };