ruby_reactor 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +10 -2
  3. data/README.md +72 -3
  4. data/Rakefile +27 -2
  5. data/documentation/images/failed_order_processing.png +0 -0
  6. data/documentation/images/payment_workflow.png +0 -0
  7. data/documentation/interrupts.md +161 -0
  8. data/gui/.gitignore +24 -0
  9. data/gui/README.md +73 -0
  10. data/gui/eslint.config.js +23 -0
  11. data/gui/index.html +13 -0
  12. data/gui/package-lock.json +5925 -0
  13. data/gui/package.json +46 -0
  14. data/gui/postcss.config.js +6 -0
  15. data/gui/public/vite.svg +1 -0
  16. data/gui/src/App.css +42 -0
  17. data/gui/src/App.tsx +51 -0
  18. data/gui/src/assets/react.svg +1 -0
  19. data/gui/src/components/DagVisualizer.tsx +424 -0
  20. data/gui/src/components/Dashboard.tsx +163 -0
  21. data/gui/src/components/ErrorBoundary.tsx +47 -0
  22. data/gui/src/components/ReactorDetail.tsx +135 -0
  23. data/gui/src/components/StepInspector.tsx +492 -0
  24. data/gui/src/components/__tests__/DagVisualizer.test.tsx +140 -0
  25. data/gui/src/components/__tests__/ReactorDetail.test.tsx +111 -0
  26. data/gui/src/components/__tests__/StepInspector.test.tsx +408 -0
  27. data/gui/src/globals.d.ts +7 -0
  28. data/gui/src/index.css +14 -0
  29. data/gui/src/lib/utils.ts +13 -0
  30. data/gui/src/main.tsx +14 -0
  31. data/gui/src/test/setup.ts +11 -0
  32. data/gui/tailwind.config.js +11 -0
  33. data/gui/tsconfig.app.json +28 -0
  34. data/gui/tsconfig.json +7 -0
  35. data/gui/tsconfig.node.json +26 -0
  36. data/gui/vite.config.ts +8 -0
  37. data/gui/vitest.config.ts +13 -0
  38. data/lib/ruby_reactor/async_router.rb +6 -2
  39. data/lib/ruby_reactor/context.rb +35 -9
  40. data/lib/ruby_reactor/dependency_graph.rb +2 -0
  41. data/lib/ruby_reactor/dsl/compose_builder.rb +8 -0
  42. data/lib/ruby_reactor/dsl/interrupt_builder.rb +48 -0
  43. data/lib/ruby_reactor/dsl/interrupt_step_config.rb +21 -0
  44. data/lib/ruby_reactor/dsl/map_builder.rb +8 -0
  45. data/lib/ruby_reactor/dsl/reactor.rb +12 -0
  46. data/lib/ruby_reactor/dsl/step_builder.rb +4 -0
  47. data/lib/ruby_reactor/executor/compensation_manager.rb +60 -27
  48. data/lib/ruby_reactor/executor/graph_manager.rb +2 -0
  49. data/lib/ruby_reactor/executor/result_handler.rb +117 -39
  50. data/lib/ruby_reactor/executor/retry_manager.rb +1 -0
  51. data/lib/ruby_reactor/executor/step_executor.rb +38 -4
  52. data/lib/ruby_reactor/executor.rb +86 -13
  53. data/lib/ruby_reactor/interrupt_result.rb +20 -0
  54. data/lib/ruby_reactor/map/collector.rb +0 -2
  55. data/lib/ruby_reactor/map/element_executor.rb +3 -0
  56. data/lib/ruby_reactor/map/execution.rb +28 -1
  57. data/lib/ruby_reactor/map/helpers.rb +44 -6
  58. data/lib/ruby_reactor/reactor.rb +187 -1
  59. data/lib/ruby_reactor/registry.rb +25 -0
  60. data/lib/ruby_reactor/sidekiq_workers/worker.rb +1 -1
  61. data/lib/ruby_reactor/step/compose_step.rb +22 -6
  62. data/lib/ruby_reactor/step/map_step.rb +30 -3
  63. data/lib/ruby_reactor/storage/adapter.rb +32 -0
  64. data/lib/ruby_reactor/storage/redis_adapter.rb +154 -11
  65. data/lib/ruby_reactor/utils/code_extractor.rb +31 -0
  66. data/lib/ruby_reactor/version.rb +1 -1
  67. data/lib/ruby_reactor/web/api.rb +206 -0
  68. data/lib/ruby_reactor/web/application.rb +53 -0
  69. data/lib/ruby_reactor/web/config.ru +5 -0
  70. data/lib/ruby_reactor/web/public/assets/index-VdeLgH9k.js +19 -0
  71. data/lib/ruby_reactor/web/public/assets/index-_z-6BvuM.css +1 -0
  72. data/lib/ruby_reactor/web/public/index.html +14 -0
  73. data/lib/ruby_reactor/web/public/vite.svg +1 -0
  74. data/lib/ruby_reactor.rb +94 -28
  75. data/llms-full.txt +66 -0
  76. data/llms.txt +7 -0
  77. metadata +63 -2
data/gui/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "ui",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc -b && vite build",
9
+ "test": "vitest",
10
+ "lint": "eslint .",
11
+ "preview": "vite preview"
12
+ },
13
+ "dependencies": {
14
+ "@xyflow/react": "^12.10.0",
15
+ "clsx": "^2.1.1",
16
+ "lucide-react": "^0.561.0",
17
+ "react": "^19.2.0",
18
+ "react-dom": "^19.2.0",
19
+ "react-router-dom": "^7.10.1",
20
+ "swr": "^2.3.8",
21
+ "tailwind-merge": "^3.4.0"
22
+ },
23
+ "devDependencies": {
24
+ "@eslint/js": "^9.39.1",
25
+ "@tailwindcss/postcss": "^4.1.18",
26
+ "@testing-library/jest-dom": "^6.9.1",
27
+ "@testing-library/react": "^16.3.1",
28
+ "@types/jest": "^30.0.0",
29
+ "@types/node": "^24.10.1",
30
+ "@types/react": "^19.2.5",
31
+ "@types/react-dom": "^19.2.3",
32
+ "@vitejs/plugin-react": "^5.1.1",
33
+ "autoprefixer": "^10.4.23",
34
+ "eslint": "^9.39.1",
35
+ "eslint-plugin-react-hooks": "^7.0.1",
36
+ "eslint-plugin-react-refresh": "^0.4.24",
37
+ "globals": "^16.5.0",
38
+ "jsdom": "^27.3.0",
39
+ "postcss": "^8.5.6",
40
+ "tailwindcss": "^4.1.18",
41
+ "typescript": "~5.9.3",
42
+ "typescript-eslint": "^8.46.4",
43
+ "vite": "^7.2.4",
44
+ "vitest": "^4.0.16"
45
+ }
46
+ }
@@ -0,0 +1,6 @@
1
+ export default {
2
+ plugins: {
3
+ '@tailwindcss/postcss': {},
4
+ autoprefixer: {},
5
+ },
6
+ }
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
data/gui/src/App.css ADDED
@@ -0,0 +1,42 @@
1
+ #root {
2
+ max-width: 1280px;
3
+ margin: 0 auto;
4
+ padding: 2rem;
5
+ text-align: center;
6
+ }
7
+
8
+ .logo {
9
+ height: 6em;
10
+ padding: 1.5em;
11
+ will-change: filter;
12
+ transition: filter 300ms;
13
+ }
14
+ .logo:hover {
15
+ filter: drop-shadow(0 0 2em #646cffaa);
16
+ }
17
+ .logo.react:hover {
18
+ filter: drop-shadow(0 0 2em #61dafbaa);
19
+ }
20
+
21
+ @keyframes logo-spin {
22
+ from {
23
+ transform: rotate(0deg);
24
+ }
25
+ to {
26
+ transform: rotate(360deg);
27
+ }
28
+ }
29
+
30
+ @media (prefers-reduced-motion: no-preference) {
31
+ a:nth-of-type(2) .logo {
32
+ animation: logo-spin infinite 20s linear;
33
+ }
34
+ }
35
+
36
+ .card {
37
+ padding: 2em;
38
+ }
39
+
40
+ .read-the-docs {
41
+ color: #888;
42
+ }
data/gui/src/App.tsx ADDED
@@ -0,0 +1,51 @@
1
+ import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
2
+ import { LayoutDashboard, Zap } from 'lucide-react';
3
+ import Dashboard from './components/Dashboard';
4
+ import ReactorDetail from './components/ReactorDetail';
5
+
6
+ const basename = window.RUBY_REACTOR_BASE || '/';
7
+
8
+ function App() {
9
+ return (
10
+ <Router basename={basename}>
11
+ <div className="min-h-screen bg-slate-950 text-slate-50 font-sans selection:bg-indigo-500/30">
12
+ <header className="border-b border-slate-800/60 bg-slate-900/50 backdrop-blur-xl sticky top-0 z-50">
13
+ <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
14
+ <Link to="/" className="flex items-center gap-3 group">
15
+ <div className="p-2 bg-indigo-500/10 rounded-lg group-hover:bg-indigo-500/20 transition-colors">
16
+ <Zap className="w-5 h-5 text-indigo-400 group-hover:text-indigo-300 transition-colors" />
17
+ </div>
18
+ <span className="font-bold text-lg tracking-tight text-white">
19
+ Ruby<span className="text-indigo-400">Reactor</span>
20
+ </span>
21
+ </Link>
22
+ <nav className="flex items-center gap-1">
23
+ <NavLink to="/" icon={<LayoutDashboard className="w-4 h-4" />}>Dashboard</NavLink>
24
+ </nav>
25
+ </div>
26
+ </header>
27
+
28
+ <main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
29
+ <Routes>
30
+ <Route path="/" element={<Dashboard />} />
31
+ <Route path="/reactors/:id" element={<ReactorDetail />} />
32
+ </Routes>
33
+ </main>
34
+ </div>
35
+ </Router>
36
+ );
37
+ }
38
+
39
+ function NavLink({ to, icon, children }: { to: string; icon: React.ReactNode; children: React.ReactNode }) {
40
+ return (
41
+ <Link
42
+ to={to}
43
+ className="flex items-center gap-2 px-3 py-2 text-sm font-medium text-slate-400 hover:text-white hover:bg-white/5 rounded-md transition-all"
44
+ >
45
+ {icon}
46
+ {children}
47
+ </Link>
48
+ );
49
+ }
50
+
51
+ export default App;
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
@@ -0,0 +1,424 @@
1
+ import { useMemo, useCallback } from 'react';
2
+ import {
3
+ ReactFlow,
4
+ Background,
5
+ Controls,
6
+ useNodesState,
7
+ useEdgesState,
8
+ MarkerType,
9
+ Handle,
10
+ Position,
11
+ type Node,
12
+ type Edge
13
+ } from '@xyflow/react';
14
+ import '@xyflow/react/dist/style.css';
15
+ import { cn } from '../lib/utils';
16
+ import { Activity, CheckCircle2, AlertCircle, Clock, Ban } from 'lucide-react';
17
+
18
+ interface DagVisualizerProps {
19
+ structure: Record<string, any>;
20
+ steps: any[];
21
+ currentStep?: string;
22
+ onStepSelect: (stepName: string) => void;
23
+ selectedStep: string | null;
24
+ reactorStatus?: string;
25
+ error?: any;
26
+ results?: Record<string, any>;
27
+ composedContexts?: Record<string, any>;
28
+ }
29
+
30
+ const StepNode = ({ data }: { data: any }) => {
31
+ const isSelected = data.selected;
32
+ const status = data.status;
33
+
34
+ const statusColors = {
35
+ pending: "border-slate-700 bg-slate-900 text-slate-500",
36
+ running: "border-indigo-500 bg-indigo-500/10 text-indigo-400 ring-2 ring-indigo-500/20",
37
+ completed: "border-teal-500 bg-teal-500/10 text-teal-400",
38
+ failed: "border-rose-500 bg-rose-500/10 text-rose-400 shadow-[0_0_15px_rgba(244,63,94,0.2)]",
39
+ cancelled: "border-amber-500/50 bg-amber-500/10 text-amber-500"
40
+ };
41
+
42
+ const StatusIcon = {
43
+ pending: Clock,
44
+ running: Activity,
45
+ completed: CheckCircle2,
46
+ failed: AlertCircle,
47
+ cancelled: Ban
48
+ }[status as keyof typeof statusColors] || Clock;
49
+
50
+ return (
51
+ <div className={cn(
52
+ "px-4 py-3 rounded-lg border shadow-lg transition-all min-w-[150px]",
53
+ statusColors[status as keyof typeof statusColors] || statusColors.pending,
54
+ isSelected && "ring-2 ring-white/20 scale-105"
55
+ )}>
56
+ <Handle type="target" position={Position.Top} className="!bg-slate-500 !w-2 !h-2" />
57
+
58
+ <div className="flex items-center gap-3">
59
+ <StatusIcon className={cn(
60
+ "w-4 h-4",
61
+ status === 'running' && "animate-pulse"
62
+ )} />
63
+ <div>
64
+ <div className="font-medium text-sm">{data.label}</div>
65
+ <div className="text-[10px] opacity-70 uppercase tracking-wider">
66
+ {status === 'cancelled' ? 'CANCELLED' : data.type}
67
+ </div>
68
+ </div>
69
+ </div>
70
+
71
+ <Handle type="source" position={Position.Bottom} className="!bg-slate-500 !w-2 !h-2" />
72
+ </div>
73
+ );
74
+ };
75
+
76
+ const GroupNode = ({ data }: { data: any }) => {
77
+ const isSelected = data.selected;
78
+ const status = data.status;
79
+
80
+ const statusBorderColors = {
81
+ pending: "border-slate-700",
82
+ running: "border-indigo-500",
83
+ completed: "border-teal-500",
84
+ failed: "border-rose-500",
85
+ cancelled: "border-amber-500/50"
86
+ };
87
+
88
+ return (
89
+ <div className={cn(
90
+ "px-4 py-8 rounded-xl border-2 border-dashed transition-all relative",
91
+ "bg-slate-900/50 backdrop-blur-sm",
92
+ statusBorderColors[status as keyof typeof statusBorderColors] || statusBorderColors.pending,
93
+ isSelected && "ring-2 ring-white/20 text-white"
94
+ )} style={{ width: '100%', height: '100%' }}>
95
+ {/* Visual Group Label */}
96
+ <div className="absolute top-0 left-0 bg-slate-800 px-3 py-1 rounded-br-lg text-xs font-bold text-slate-300 border-b border-r border-slate-700">
97
+ {data.label} ({data.type})
98
+ </div>
99
+
100
+ {/* Handles for connecting to the group as a whole step */}
101
+ <Handle type="target" position={Position.Top} className="!bg-slate-500 !w-2 !h-2" style={{ top: -10 }} />
102
+ <Handle type="source" position={Position.Bottom} className="!bg-slate-500 !w-2 !h-2" style={{ bottom: -10 }} />
103
+ </div>
104
+ );
105
+ };
106
+
107
+ const nodeTypes = {
108
+ step: StepNode,
109
+ group: GroupNode
110
+ };
111
+
112
+ // --- Recursive Processing & Layout ---
113
+
114
+ const NODE_WIDTH = 200;
115
+ const NODE_HEIGHT = 80;
116
+ const GROUP_PADDING = 40;
117
+ const RANK_SEP = 100; // vertical
118
+ const NODE_SEP = 50; // horizontal
119
+
120
+ interface LayoutResult {
121
+ nodes: Node[];
122
+ edges: Edge[];
123
+ width: number;
124
+ height: number;
125
+ }
126
+
127
+ const performLayout = (
128
+ nodes: Record<string, any>,
129
+ parentId: string | null = null,
130
+ nodeStatus: Record<string, string>,
131
+ path: string = ""
132
+ ): LayoutResult => {
133
+ if (!nodes || Object.keys(nodes).length === 0) {
134
+ return { nodes: [], edges: [], width: 0, height: 0 };
135
+ }
136
+
137
+ const resultNodes: Node[] = [];
138
+ const resultEdges: Edge[] = [];
139
+
140
+ // 1. Identify nodes and recursion
141
+ const currentLevelNodes: any[] = [];
142
+
143
+ Object.entries(nodes).forEach(([key, config]: [string, any]) => {
144
+ const fullId = path ? `${path}.${key}` : key;
145
+ // Check for nested structure
146
+ let childLayout: LayoutResult | null = null;
147
+ let width = NODE_WIDTH;
148
+ let height = NODE_HEIGHT;
149
+
150
+ if (config.nested_structure) {
151
+ // Recurse
152
+ childLayout = performLayout(config.nested_structure, fullId, nodeStatus, fullId);
153
+ width = childLayout.width + GROUP_PADDING * 2;
154
+ height = childLayout.height + GROUP_PADDING * 2 + 30; // 30 for label header
155
+
156
+ // Add child edges (nodes added later to preserve z-index)
157
+ resultEdges.push(...childLayout.edges);
158
+ }
159
+
160
+ currentLevelNodes.push({
161
+ key,
162
+ fullId,
163
+ config,
164
+ width,
165
+ height,
166
+ childLayout
167
+ });
168
+ });
169
+
170
+ // 2. Compute Ranks (Vertical Layers) for current level
171
+ const ranks: Record<string, number> = {};
172
+
173
+ const getRank = (nodeKey: string, visited = new Set<string>()): number => {
174
+ if (visited.has(nodeKey)) return 0; // Cycle protection
175
+ if (ranks[nodeKey] !== undefined) return ranks[nodeKey];
176
+
177
+ visited.add(nodeKey);
178
+ const config = nodes[nodeKey];
179
+ if (!config || !config.depends_on || config.depends_on.length === 0) {
180
+ ranks[nodeKey] = 0;
181
+ return 0;
182
+ }
183
+
184
+ // Only consider deps present in current level
185
+ const relevantDeps = config.depends_on.filter((d: string) => nodes[d]);
186
+ if (relevantDeps.length === 0) {
187
+ ranks[nodeKey] = 0;
188
+ return 0;
189
+ }
190
+
191
+ const parentRanks = relevantDeps.map((d: string) => getRank(d, new Set(visited)));
192
+ const rank = Math.max(...parentRanks) + 1;
193
+ ranks[nodeKey] = rank;
194
+ return rank;
195
+ };
196
+
197
+ currentLevelNodes.forEach(n => getRank(n.key));
198
+
199
+ // 3. Group by Rank
200
+ const layers: Record<number, any[]> = {};
201
+ currentLevelNodes.forEach(N => {
202
+ const rank = ranks[N.key];
203
+ if (!layers[rank]) layers[rank] = [];
204
+ layers[rank].push(N);
205
+ });
206
+
207
+ // 4. Assign Positions
208
+ let currentY = 0;
209
+ let maxLayerWidth = 0;
210
+
211
+ const sortedRanks = Object.keys(layers).map(Number).sort((a, b) => a - b);
212
+
213
+ sortedRanks.forEach(rank => {
214
+ const layerNodes = layers[rank];
215
+ // Calculate total width of this layer
216
+ const layerWidth = layerNodes.reduce((sum, n) => sum + n.width + NODE_SEP, 0) - NODE_SEP;
217
+ maxLayerWidth = Math.max(maxLayerWidth, layerWidth);
218
+
219
+ let currentX = -layerWidth / 2; // Center alignment
220
+
221
+ // Find max height in this layer to determine row height
222
+ const layerHeight = Math.max(...layerNodes.map(n => n.height));
223
+
224
+ layerNodes.forEach(N => {
225
+ // Create the Node
226
+ const isGroup = !!N.childLayout;
227
+ const type = isGroup ? 'group' : 'step';
228
+
229
+ const node: Node = {
230
+ id: N.fullId,
231
+ type: type,
232
+ data: {
233
+ label: N.key,
234
+ type: N.config.type,
235
+ status: nodeStatus[N.fullId] || 'pending',
236
+ selected: false // handled by parent check
237
+ },
238
+ position: { x: currentX + N.width / 2 - (isGroup ? N.width / 2 : NODE_WIDTH / 2), y: currentY },
239
+ // For groups, we need to set style width/height
240
+ style: isGroup ? { width: N.width, height: N.height } : undefined,
241
+ parentId: parentId || undefined,
242
+ extent: parentId ? 'parent' : undefined,
243
+ };
244
+
245
+ // If group, we need to offset its children for relative positioning
246
+ if (N.childLayout) {
247
+ const minChildX = Math.min(...N.childLayout.nodes.map((n: Node) => n.position.x));
248
+ const minChildY = Math.min(...N.childLayout.nodes.map((n: Node) => n.position.y));
249
+
250
+ // Shift children to be inside the group
251
+ const xOffset = -minChildX + GROUP_PADDING;
252
+ const yOffset = -minChildY + GROUP_PADDING + 30;
253
+
254
+ N.childLayout.nodes.forEach((childNode: Node) => {
255
+ childNode.position.x += xOffset;
256
+ childNode.position.y += yOffset;
257
+ });
258
+ }
259
+
260
+ resultNodes.push(node);
261
+
262
+ if (N.childLayout) {
263
+ resultNodes.push(...N.childLayout.nodes);
264
+ }
265
+
266
+ currentX += N.width + NODE_SEP;
267
+ });
268
+
269
+ currentY += layerHeight + RANK_SEP;
270
+ });
271
+
272
+ // 5. Generate Edges for current level
273
+ Object.entries(nodes).forEach(([key, config]: [string, any]) => {
274
+ const fullId = path ? `${path}.${key}` : key;
275
+ if (config.depends_on) {
276
+ config.depends_on.forEach((dep: string) => {
277
+ // Create edge if dep exists in this level
278
+ if (nodes[dep]) {
279
+ const depFullId = path ? `${path}.${dep}` : dep;
280
+ resultEdges.push({
281
+ id: `${depFullId}-${fullId}`,
282
+ source: depFullId,
283
+ target: fullId,
284
+ type: 'smoothstep',
285
+ animated: true,
286
+ style: { stroke: '#475569' },
287
+ markerEnd: { type: MarkerType.ArrowClosed, color: '#475569' },
288
+ });
289
+ }
290
+ });
291
+ }
292
+ });
293
+
294
+ return {
295
+ nodes: resultNodes,
296
+ edges: resultEdges,
297
+ width: maxLayerWidth,
298
+ height: currentY
299
+ };
300
+ };
301
+
302
+
303
+ export default function DagVisualizer({ structure, steps, onStepSelect, selectedStep, reactorStatus, error, results, composedContexts }: DagVisualizerProps) {
304
+ const nodeStatus = useMemo(() => {
305
+ const statusMap: Record<string, string> = {};
306
+
307
+ const resolveStatus = (struct: any, currentResults: any, currentTrace: any[], currentPath: string, contextObj: any, currentStatus?: string) => {
308
+ Object.keys(struct || {}).forEach(key => {
309
+ const fullId = currentPath ? `${currentPath}.${key}` : key;
310
+ const stepStatus = contextObj?.status || currentStatus || reactorStatus;
311
+
312
+ statusMap[fullId] = 'pending';
313
+
314
+ // 1. Check if this step is completed in THIS context
315
+ if (currentResults && currentResults[key] !== undefined) {
316
+ statusMap[fullId] = 'completed';
317
+ } else if (currentTrace && currentTrace.some((t: any) => t.step === key)) {
318
+ // If in trace but no result yet, might be running
319
+ if (stepStatus === 'running') {
320
+ statusMap[fullId] = 'running';
321
+ }
322
+ }
323
+
324
+ // 2. Check for error in THIS context
325
+ if (contextObj?.failure_reason?.step_name === key) {
326
+ statusMap[fullId] = 'failed';
327
+ }
328
+
329
+ // 3. Handle global cancellation/failure states for unreached steps at this level
330
+ if (statusMap[fullId] === 'pending' && (stepStatus === 'failed' || stepStatus === 'cancelled')) {
331
+ statusMap[fullId] = 'cancelled';
332
+ }
333
+
334
+ // 4. Recursively handle nested structures
335
+ if (struct[key].nested_structure) {
336
+ const nestedData = contextObj?.composed_contexts?.[key];
337
+ const nestedContext = nestedData?.context?.value || nestedData?.context;
338
+
339
+ // For map steps, if we don't have a specific context but the step itself is completed/failed/cancelled,
340
+ // we should propagate that status to nested steps.
341
+ const inheritedStatus = (struct[key].type === 'map' && (!nestedContext || statusMap[fullId] === 'completed'))
342
+ ? statusMap[fullId]
343
+ : nestedContext?.status;
344
+
345
+ resolveStatus(
346
+ struct[key].nested_structure,
347
+ nestedContext?.intermediate_results || {},
348
+ nestedContext?.execution_trace || [],
349
+ fullId,
350
+ nestedContext,
351
+ inheritedStatus
352
+ );
353
+ }
354
+ });
355
+ };
356
+
357
+ // Root call
358
+ resolveStatus(structure, results, steps, "", {
359
+ intermediate_results: results,
360
+ execution_trace: steps,
361
+ composed_contexts: composedContexts,
362
+ failure_reason: error,
363
+ status: reactorStatus
364
+ });
365
+
366
+ // Handle global cancellation/failure states for unreached steps
367
+ if (reactorStatus === 'failed' || reactorStatus === 'cancelled') {
368
+ Object.keys(statusMap).forEach(key => {
369
+ if (statusMap[key] === 'pending') {
370
+ statusMap[key] = 'cancelled';
371
+ }
372
+ });
373
+ }
374
+
375
+ return statusMap;
376
+ }, [structure, steps, reactorStatus, error, results, composedContexts]);
377
+
378
+ const { nodes, edges } = useMemo(() => {
379
+ if (!structure) return { nodes: [], edges: [] };
380
+
381
+ const layout = performLayout(structure, null, nodeStatus);
382
+
383
+ // Post-process to set selection state which changes dynamically
384
+ const finalNodes = layout.nodes.map(n => ({
385
+ ...n,
386
+ data: {
387
+ ...n.data,
388
+ selected: selectedStep === n.id
389
+ }
390
+ }));
391
+
392
+ return { nodes: finalNodes, edges: layout.edges };
393
+ }, [structure, nodeStatus, selectedStep]);
394
+
395
+ const [nodesState, setNodes, onNodesChange] = useNodesState<Node>([]);
396
+ const [edgesState, setEdges, onEdgesChange] = useEdgesState<Edge>([]);
397
+
398
+ useMemo(() => {
399
+ setNodes(nodes);
400
+ setEdges(edges);
401
+ }, [nodes, edges, setNodes, setEdges]);
402
+
403
+ const onNodeClick = useCallback((_: any, node: Node) => {
404
+ onStepSelect(node.id);
405
+ }, [onStepSelect]);
406
+
407
+ return (
408
+ <div className="w-full h-full min-h-[500px] bg-slate-950 rounded-xl overflow-hidden">
409
+ <ReactFlow
410
+ nodes={nodesState}
411
+ edges={edgesState}
412
+ onNodesChange={onNodesChange}
413
+ onEdgesChange={onEdgesChange}
414
+ nodeTypes={nodeTypes}
415
+ onNodeClick={onNodeClick}
416
+ fitView
417
+ className="bg-slate-900/50"
418
+ >
419
+ <Background color="#1e293b" gap={16} />
420
+ <Controls className="bg-slate-800 border-slate-700 fill-slate-400 text-slate-400" />
421
+ </ReactFlow>
422
+ </div>
423
+ );
424
+ }