Skip to content

Commit 0f47fd4

Browse files
committed
fix automation service
1 parent 4b5e56c commit 0f47fd4

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

api/src/main/resources/templates/sso/automation/suggestions_list.html

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@
120120
</head>
121121

122122
<body>
123+
<div class="modal fade" id="aiPromptModal" tabindex="-1">
124+
<div class="modal-dialog">
125+
<div class="modal-content">
126+
<div class="modal-header">
127+
<h5 class="modal-title">Generate with AI</h5>
128+
</div>
129+
<div class="modal-body">
130+
<textarea id="aiPromptInput"
131+
class="form-control"
132+
rows="5"
133+
placeholder="Describe what you want the AI to generate"></textarea>
134+
</div>
135+
<div class="modal-footer">
136+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
137+
<button type="button" class="btn btn-primary" onclick="submitAIPrompt()">Generate</button>
138+
</div>
139+
</div>
140+
</div>
141+
</div>
123142
<input type="hidden" id="csrf-token" th:value="${_csrf.token}" />
124143
<div class="container-fluid">
125144
<div class="row flex-nowrap">
@@ -705,15 +724,13 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
705724
var script = monacoEditor.getValue();
706725
var description = $('#script-description').val();
707726
var scriptType = $('#script-type-select').val();
708-
727+
const csrfToken = document.getElementById("csrf-token").value;
709728
if (isNewScript) {
710729
// Create new automation script
711730
if (!description || description.trim() === '') {
712731
alert('Please enter a description for the script');
713732
return;
714733
}
715-
716-
const csrfToken = document.getElementById("csrf-token").value;
717734
$.ajax({
718735
url: '/api/v1/automation/suggestions',
719736
type: 'POST',
@@ -742,6 +759,10 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
742759
url: '/api/v1/automation/suggestions/' + id + '/script',
743760
type: 'PUT',
744761
contentType: 'application/json',
762+
headers: {
763+
'Content-Type': 'application/json',
764+
"X-CSRF-TOKEN": csrfToken
765+
},
745766
data: JSON.stringify({ script: script }),
746767
success: function(response) {
747768
alert('Script saved successfully!');
@@ -921,11 +942,15 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
921942

922943
// Generate code with AI
923944
function generateWithAI() {
945+
console.log("ahahahha");
924946
var id = $('#editor-suggestion-id').val();
925-
var prompt = window.prompt('What would you like the AI to generate or improve?',
947+
var prompt = window.prompt('What would you like the AI to generate or improve?',
926948
'Generate a complete automation script based on the description');
927-
928-
if (!prompt) return;
949+
950+
if (!prompt) {
951+
console.log("leaving....");
952+
return;
953+
}
929954
const csrfToken = document.getElementById("csrf-token").value;
930955
$.ajax({
931956
url: '/api/v1/automation/suggestions/' + id + '/generate',

dataplane/src/main/java/io/sentrius/sso/core/services/automation/AutomationAgentService.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,50 @@ public Map<String, Object> analyzeAutomationCode(String code, String scriptType)
140140
141141
Return ONLY the JSON, no additional text.
142142
""", scriptType, scriptType, code);
143-
144-
Object response = callLLM(systemPrompt, prompt);
145-
146-
Map<String, Object> result = new HashMap<>();
143+
144+
Object rawResponse = callLLM(systemPrompt, prompt);
145+
log.info("Analysis response: {}", rawResponse);
146+
147147
try {
148-
log.info("Analysis response: {}", response);
149-
return (Map<String, Object>) response;
148+
149+
if (rawResponse instanceof Map<?, ?> map &&
150+
map.containsKey("isDestructive")) {
151+
return (Map<String, Object>) map;
152+
}
153+
154+
if (rawResponse instanceof Map<?, ?> root &&
155+
root.containsKey("output")) {
156+
157+
List<?> output = (List<?>) root.get("output");
158+
Map<?, ?> msg = (Map<?, ?>) output.get(0);
159+
List<?> content = (List<?>) msg.get("content");
160+
Map<?, ?> textItem = (Map<?, ?>) content.get(0);
161+
162+
String jsonText = (String) textItem.get("text");
163+
return JsonUtil.MAPPER.readValue(jsonText, Map.class);
164+
}
165+
166+
// ✅ CASE 3: Raw JSON string
167+
if (rawResponse instanceof String s) {
168+
return JsonUtil.MAPPER.readValue(s, Map.class);
169+
}
170+
171+
throw new IllegalStateException("Unsupported LLM response type: "
172+
+ rawResponse.getClass());
173+
150174
} catch (Exception e) {
151-
log.warn("Failed to parse analysis response as JSON, using fallback", e);
152-
result.put("isDestructive", false);
153-
result.put("destructiveOperations", new ArrayList<>());
154-
result.put("securityIssues", new ArrayList<>());
155-
result.put("qualityIssues", new ArrayList<>());
156-
result.put("suggestions", new ArrayList<>());
157-
result.put("overallRisk", "UNKNOWN");
158-
result.put("rawResponse", response);
175+
log.warn("Failed to parse analysis response, using fallback", e);
176+
177+
Map<String, Object> fallback = new HashMap<>();
178+
fallback.put("isDestructive", false);
179+
fallback.put("destructiveOperations", List.of());
180+
fallback.put("securityIssues", List.of());
181+
fallback.put("qualityIssues", List.of());
182+
fallback.put("suggestions", List.of());
183+
fallback.put("overallRisk", "UNKNOWN");
184+
fallback.put("rawResponse", rawResponse);
185+
return fallback;
159186
}
160-
161-
return result;
162187
}
163188

164189
private String buildSystemPrompt(AutomationSuggestion suggestion) {

0 commit comments

Comments
 (0)