// DOM elements const createFlowBtn = document.getElementById("create-flow-btn"); const flowNameInput = document.getElementById("flow-name"); const flowDescriptionInput = document.getElementById("flow-description"); const flowSelect = document.getElementById("flow-select"); const actionTypeSelect = document.getElementById("action-type"); const taskInput = document.getElementById("task-input"); const orderIndexInput = document.getElementById("order-index"); const addTaskBtn = document.getElementById("add-task-btn"); const flowDetails = document.getElementById("flow-details"); const executeFlowSelect = document.getElementById("execute-flow-select"); const executePayloadInput = document.getElementById("execute-payload"); const executeBtn = document.getElementById("execute-btn"); const webhookBtn = document.getElementById("webhook-btn"); const executionResults = document.getElementById("execution-results"); let currentFlows = []; // Create new flow async function createFlow() { const name = flowNameInput.value.trim(); const description = flowDescriptionInput.value.trim(); if (!name) { alert("Flow name is required"); return; } try { const response = await fetch("/flow", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, description }), }); if (response.ok) { const flow = await response.json(); alert(`Flow "${flow.name}" created successfully!`); flowNameInput.value = ""; flowDescriptionInput.value = ""; loadFlows(); } else { const error = await response.json(); alert(`Error: ${error.error}`); } } catch (err) { alert("Failed to create flow"); } } // Load all flows async function loadFlows() { try { const response = await fetch("/flows"); const flows = await response.json(); currentFlows = flows; // Update flow selects flowSelect.innerHTML = ''; executeFlowSelect.innerHTML = ''; flows.forEach((flow) => { flowSelect.innerHTML += ``; executeFlowSelect.innerHTML += ``; }); } catch (err) { console.error("Failed to load flows"); } } // Add task to flow async function addTask() { const flowId = flowSelect.value; const actionType = actionTypeSelect.value; const inputData = taskInput.value.trim(); const orderIndex = parseInt(orderIndexInput.value); if (!flowId || !actionType || !inputData || isNaN(orderIndex)) { alert("All fields are required"); return; } try { const response = await fetch(`/flow/${flowId}/task`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action_type: actionType, input_data: inputData, order_index: orderIndex, }), }); if (response.ok) { const task = await response.json(); alert(`Task "${task.action_type}" added successfully!`); taskInput.value = ""; orderIndexInput.value = ""; loadFlowDetails(flowId); } else { const error = await response.json(); alert(`Error: ${error.error}`); } } catch (err) { alert("Failed to add task"); } } // Load flow details async function loadFlowDetails(flowId) { if (!flowId) return; try { const response = await fetch(`/flow/${flowId}`); const data = await response.json(); flowDetails.innerHTML = `
${ data.flow.description || "No description" }