// 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.name}

${ data.flow.description || "No description" }

Tasks:

${data.tasks .map( (task) => `
${task.action_type}
Input: ${task.input_data}
Order: ${task.order_index}
` ) .join("")}
`; } catch (err) { console.error("Failed to load flow details"); } } // Execute flow async function executeFlow() { const flowId = executeFlowSelect.value; const payload = executePayloadInput.value.trim(); if (!flowId) { alert("Please select a flow to execute"); return; } try { const response = await fetch(`/flow/${flowId}/execute`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ payload }), }); if (response.ok) { const result = await response.json(); showExecutionResults(result); } else { const error = await response.json(); alert(`Error: ${error.error}`); } } catch (err) { alert("Failed to execute flow"); } } // Show execution results function showExecutionResults(result) { executionResults.innerHTML = `

Execution Results:

${result.results .map( (r) => `
Task ${r.task_id}
${r.result}
Status: ${r.status}
` ) .join("")}
`; executionResults.classList.remove("hidden"); } // Get webhook URL function getWebhookUrl() { const flowId = executeFlowSelect.value; if (!flowId) { alert("Please select a flow"); return; } const webhookUrl = `${window.location.origin}/flow/${flowId}/trigger?payload=test@example.com`; alert( `Webhook URL:\n${webhookUrl}\n\nCopy this URL to trigger the flow via webhook.` ); } // Event listeners createFlowBtn.addEventListener("click", createFlow); addTaskBtn.addEventListener("click", addTask); executeBtn.addEventListener("click", executeFlow); webhookBtn.addEventListener("click", getWebhookUrl); flowSelect.addEventListener("change", (e) => { if (e.target.value) { loadFlowDetails(e.target.value); } }); // Initialize loadFlows();