Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

fix: enhance error handling in workflow extraction and sorting

Muhammad Hamza Khand615a844591b
diff --git a/packages/app/src/builder-ui/debugger.ts b/packages/app/src/builder-ui/debugger.ts
index 04479b96..5cebfff3 100644
--- a/packages/app/src/builder-ui/debugger.ts
+++ b/packages/app/src/builder-ui/debugger.ts
@@ -2600,7 +2600,16 @@ export function createDebugInjectDialog(
actionType: 'step' | 'run',
) {
const agentData = (await workspace.export(false)) || workspace?.agent?.data;
- const workflows = await extractWorkflows(agentData);
+
+ // Extract workflows and handle potential errors (e.g., cycle detection)
+ let workflows;
+ try {
+ workflows = await extractWorkflows(agentData);
+ } catch (error) {
+ console.error('Error extracting workflows:', error);
+ errorToast(error instanceof Error ? error.message : 'Unknown error occurred', 'Debug Failed');
+ return;
+ }
const selectedWorkflow = workflows.filter((wf) =>
wf.components.some((wc) => wc?.id === component?.uid),
diff --git a/packages/app/src/builder-ui/workspace/ComponentSort.ts b/packages/app/src/builder-ui/workspace/ComponentSort.ts
index e487a84b..36967c14 100644
--- a/packages/app/src/builder-ui/workspace/ComponentSort.ts
+++ b/packages/app/src/builder-ui/workspace/ComponentSort.ts
@@ -1,3 +1,4 @@
+import { errorToast } from '@src/shared/components/toast';
import { delay } from '../utils';
declare var workspace: any;
@@ -295,21 +296,56 @@ export async function extractWorkflows(data) {
return graph;
}
+ // Check if the graph has a cycle starting from a given node
+ function hasCycle(startId, graph) {
+ const visited = new Set();
+ const inStack = new Set();
+
+ function dfs(nodeId) {
+ if (inStack.has(nodeId)) {
+ return true; // Cycle detected
+ }
+ if (visited.has(nodeId)) {
+ return false; // Already processed, no cycle from this path
+ }
+
+ visited.add(nodeId);
+ inStack.add(nodeId);
+
+ const neighbors = graph.get(nodeId) || [];
+ for (const neighbor of neighbors) {
+ if (dfs(neighbor)) {
+ return true;
+ }
+ }
+
+ inStack.delete(nodeId);
+ return false;
+ }
+
+ return dfs(startId);
+ }
+
// Perform Topological Sort and Calculate Levels
+ // Throws an error if a cycle is detected
function calculateLevels(startId, graph) {
+ // Check for cycles first - if found, throw error
+ const _hasCycle = hasCycle(startId, graph);
+
+ if (_hasCycle) {
+ throw new Error('Circular dependency detected in the workflow');
+ }
+
const levels = {};
- const visited = new Set();
- // Initialize levels with 0
+ // Initialize levels with 0 for APIEndpoint, 1 for others
components.forEach((component) => {
levels[component.id] = component.name === 'APIEndpoint' ? 0 : 1;
});
// Recursive DFS to calculate levels
+ // Since we already checked for cycles with hasCycle, this is safe
function dfs(nodeId, currentLevel) {
- //if (visited.has(nodeId)) return;
-
- visited.add(nodeId);
levels[nodeId] = Math.max(levels[nodeId], currentLevel);
const neighbors = graph.get(nodeId) || [];
@@ -561,6 +597,7 @@ async function sortAll() {
// Resolve collisions with components that weren't originally inside Notes
await resolveNoteCollisions(noteGroups);
} catch (error) {
+ errorToast(error instanceof Error ? error.message : 'Unknown error occurred', 'Prettify Failed');
console.error('Error during sort:', error);
} finally {
workspace.unlock();