|
| 1 | +package com.steve.ai.ai; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | +import com.steve.ai.SteveMod; |
| 8 | +import com.steve.ai.action.Task; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class ResponseParser { |
| 16 | + |
| 17 | + public static ParsedResponse parseAIResponse(String response) { |
| 18 | + if (response == null || response.isEmpty()) { |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + String jsonString = extractJSON(response); |
| 24 | + |
| 25 | + JsonObject json = JsonParser.parseString(jsonString).getAsJsonObject(); |
| 26 | + |
| 27 | + String reasoning = json.has("reasoning") ? json.get("reasoning").getAsString() : ""; |
| 28 | + String plan = json.has("plan") ? json.get("plan").getAsString() : ""; |
| 29 | + List<Task> tasks = new ArrayList<>(); |
| 30 | + |
| 31 | + if (json.has("tasks") && json.get("tasks").isJsonArray()) { |
| 32 | + JsonArray tasksArray = json.getAsJsonArray("tasks"); |
| 33 | + |
| 34 | + for (JsonElement taskElement : tasksArray) { |
| 35 | + if (taskElement.isJsonObject()) { |
| 36 | + JsonObject taskObj = taskElement.getAsJsonObject(); |
| 37 | + Task task = parseTask(taskObj); |
| 38 | + if (task != null) { |
| 39 | + tasks.add(task); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!reasoning.isEmpty()) { } |
| 46 | + |
| 47 | + return new ParsedResponse(reasoning, plan, tasks); |
| 48 | + |
| 49 | + } catch (Exception e) { |
| 50 | + SteveMod.LOGGER.error("Failed to parse AI response: {}", response, e); |
| 51 | + return null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static String extractJSON(String response) { |
| 56 | + String cleaned = response.trim(); |
| 57 | + |
| 58 | + if (cleaned.startsWith("```json")) { |
| 59 | + cleaned = cleaned.substring(7); |
| 60 | + } else if (cleaned.startsWith("```")) { |
| 61 | + cleaned = cleaned.substring(3); |
| 62 | + } |
| 63 | + |
| 64 | + if (cleaned.endsWith("```")) { |
| 65 | + cleaned = cleaned.substring(0, cleaned.length() - 3); |
| 66 | + } |
| 67 | + |
| 68 | + cleaned = cleaned.trim(); |
| 69 | + |
| 70 | + // Fix common JSON formatting issues |
| 71 | + cleaned = cleaned.replaceAll("\\n\\s*", " "); |
| 72 | + |
| 73 | + // Fix missing commas between array/object elements (common AI mistake) |
| 74 | + cleaned = cleaned.replaceAll("}\\s+\\{", "},{"); |
| 75 | + cleaned = cleaned.replaceAll("}\\s+\\[", "},["); |
| 76 | + cleaned = cleaned.replaceAll("]\\s+\\{", "],{"); |
| 77 | + cleaned = cleaned.replaceAll("]\\s+\\[", "],["); |
| 78 | + |
| 79 | + return cleaned; |
| 80 | + } |
| 81 | + |
| 82 | + private static Task parseTask(JsonObject taskObj) { |
| 83 | + if (!taskObj.has("action")) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + String action = taskObj.get("action").getAsString(); |
| 88 | + Map<String, Object> parameters = new HashMap<>(); |
| 89 | + |
| 90 | + if (taskObj.has("parameters") && taskObj.get("parameters").isJsonObject()) { |
| 91 | + JsonObject paramsObj = taskObj.getAsJsonObject("parameters"); |
| 92 | + |
| 93 | + for (String key : paramsObj.keySet()) { |
| 94 | + JsonElement value = paramsObj.get(key); |
| 95 | + |
| 96 | + if (value.isJsonPrimitive()) { |
| 97 | + if (value.getAsJsonPrimitive().isNumber()) { |
| 98 | + parameters.put(key, value.getAsNumber()); |
| 99 | + } else if (value.getAsJsonPrimitive().isBoolean()) { |
| 100 | + parameters.put(key, value.getAsBoolean()); |
| 101 | + } else { |
| 102 | + parameters.put(key, value.getAsString()); |
| 103 | + } |
| 104 | + } else if (value.isJsonArray()) { |
| 105 | + List<Object> list = new ArrayList<>(); |
| 106 | + for (JsonElement element : value.getAsJsonArray()) { |
| 107 | + if (element.isJsonPrimitive()) { |
| 108 | + if (element.getAsJsonPrimitive().isNumber()) { |
| 109 | + list.add(element.getAsNumber()); |
| 110 | + } else { |
| 111 | + list.add(element.getAsString()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + parameters.put(key, list); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return new Task(action, parameters); |
| 121 | + } |
| 122 | + |
| 123 | + public static class ParsedResponse { |
| 124 | + private final String reasoning; |
| 125 | + private final String plan; |
| 126 | + private final List<Task> tasks; |
| 127 | + |
| 128 | + public ParsedResponse(String reasoning, String plan, List<Task> tasks) { |
| 129 | + this.reasoning = reasoning; |
| 130 | + this.plan = plan; |
| 131 | + this.tasks = tasks; |
| 132 | + } |
| 133 | + |
| 134 | + public String getReasoning() { |
| 135 | + return reasoning; |
| 136 | + } |
| 137 | + |
| 138 | + public String getPlan() { |
| 139 | + return plan; |
| 140 | + } |
| 141 | + |
| 142 | + public List<Task> getTasks() { |
| 143 | + return tasks; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments