-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (91 loc) · 3.46 KB
/
Copy pathapp.js
File metadata and controls
104 lines (91 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Project Steve frontend interactions (CSP-safe)
(() => {
const generateBtn = document.getElementById('generate-btn');
const resultPanel = document.getElementById('result-panel');
const workflowJsonEl = document.getElementById('workflow-json');
const copyJsonBtn = document.getElementById('copy-json-btn');
const statusText = document.getElementById('status-text');
const setStatus = (msg, isSuccess = false) => {
if (statusText) {
statusText.innerHTML = isSuccess ? `<strong>${msg}</strong>` : msg;
}
};
async function generateWorkflow() {
const requirementsInput = document.getElementById('requirements');
const requirements = requirementsInput ? requirementsInput.value : '';
if (!requirements.trim()) {
alert('Please describe what you want to automate.');
return;
}
const originalText = generateBtn.textContent;
generateBtn.textContent = 'Processing... Build → QA → Security';
generateBtn.disabled = true;
setStatus('Running triad...');
try {
const response = await fetch('/api/generate-workflow', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'default-api-key-change-in-production'
},
body: JSON.stringify({ requirements })
});
console.log('Response status:', response.status);
const text = await response.text();
console.log('Raw response:', text.substring(0, 200));
let data;
try {
data = JSON.parse(text);
} catch (e) {
console.error('Failed to parse JSON. Raw text:', text);
throw new Error(`Invalid JSON response: ${text.substring(0, 100)}`);
}
if (!response.ok) {
throw new Error(data.error || data.details || `HTTP ${response.status}`);
}
const workflowId = data.workflowId;
setStatus('Completed. Downloading...', true);
if (workflowJsonEl && resultPanel) {
workflowJsonEl.value = JSON.stringify(data.workflow, null, 2);
resultPanel.classList.remove('hidden');
}
// Download the workflow file
try {
const exportResponse = await fetch(`/api/workflow/${workflowId}/export`, {
headers: { 'X-API-Key': 'default-api-key-change-in-production' }
});
const blob = await exportResponse.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `workflow_${workflowId}.json`;
a.click();
window.URL.revokeObjectURL(url);
} catch (downloadErr) {
console.error('Download error:', downloadErr);
}
} catch (error) {
console.error('Error:', error);
setStatus('Failed. See alert.', false);
alert(`Error: ${error.message}`);
} finally {
generateBtn.textContent = originalText;
generateBtn.disabled = false;
if (statusText && statusText.textContent === 'Running triad...') {
setStatus('Ready.');
}
}
}
if (generateBtn) generateBtn.addEventListener('click', generateWorkflow);
if (copyJsonBtn && workflowJsonEl) {
copyJsonBtn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(workflowJsonEl.value || '');
copyJsonBtn.textContent = 'Copied!';
setTimeout(() => { copyJsonBtn.textContent = 'Copy'; }, 1200);
} catch (err) {
alert('Copy failed. Please copy manually.');
}
});
}
})();