|
|
@@ -17,10 +17,16 @@ import io.agentscope.core.tool.ToolParam;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.util.ArrayDeque;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Deque;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -103,6 +109,12 @@ public class SqlAnalysisTool {
|
|
|
+ "请使用当前工具列表里已装备的关系图工具;没有合适工具时用 execute_python 调用 "
|
|
|
+ "emit_relation_graph(nodes, edges, title) 输出,前端会自动渲染");
|
|
|
}
|
|
|
+ // sankey 校验:链接引用不存在的节点或存在环(双向边/循环链)时,echarts 前端直接
|
|
|
+ // 渲染失败且无任何提示(只剩空白容器)。工具侧提前拦截,把具体原因回给模型自修
|
|
|
+ String sankeyProblem = validateSankey(option);
|
|
|
+ if (sankeyProblem != null) {
|
|
|
+ return error(sankeyProblem);
|
|
|
+ }
|
|
|
// 回吐规范化后的 option JSON:键序不变、去掉多余空白,模型可直接放进 ```echarts 围栏块
|
|
|
return ok(Json.toStr(option));
|
|
|
}
|
|
|
@@ -134,6 +146,138 @@ public class SqlAnalysisTool {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * sankey 图校验:links 必须引用 data 里存在的节点,且不允许自环/双向边/多跳循环链
|
|
|
+ * (echarts sankey 是 DAG 布局,数据带环时前端渲染直接失败)。
|
|
|
+ *
|
|
|
+ * @return null = 通过;非 null = 面向模型的错误说明(指明具体的坏边/环节点)
|
|
|
+ */
|
|
|
+ private static String validateSankey(JsonNode root) {
|
|
|
+ JsonNode target = root;
|
|
|
+ if (root.isObject() && root.has("option") && root.get("option").isObject()) {
|
|
|
+ target = root.get("option");
|
|
|
+ }
|
|
|
+ JsonNode series = target.get("series");
|
|
|
+ if (series == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<JsonNode> sankeys = new ArrayList<>();
|
|
|
+ if (series.isArray()) {
|
|
|
+ for (JsonNode s : series) {
|
|
|
+ if ("sankey".equalsIgnoreCase(s.path("type").asText(""))) {
|
|
|
+ sankeys.add(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (series.isObject() && "sankey".equalsIgnoreCase(series.path("type").asText(""))) {
|
|
|
+ sankeys.add(series);
|
|
|
+ }
|
|
|
+ for (JsonNode sankey : sankeys) {
|
|
|
+ String problem = validateSankeySeries(sankey);
|
|
|
+ if (problem != null) {
|
|
|
+ return problem;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String validateSankeySeries(JsonNode sankey) {
|
|
|
+ Set<String> nodeNames = new LinkedHashSet<>();
|
|
|
+ JsonNode data = sankey.get("data");
|
|
|
+ if (data != null && data.isArray()) {
|
|
|
+ for (JsonNode node : data) {
|
|
|
+ String name = node.path("name").asText("");
|
|
|
+ if (!name.isBlank()) {
|
|
|
+ nodeNames.add(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JsonNode links = sankey.get("links");
|
|
|
+ if (links == null || !links.isArray() || links.isEmpty()) {
|
|
|
+ return "sankey 图校验失败:links 为空,请提供至少一条 source→target 链接";
|
|
|
+ }
|
|
|
+ Map<String, Set<String>> edges = new LinkedHashMap<>();
|
|
|
+ for (JsonNode link : links) {
|
|
|
+ String source = link.path("source").asText("");
|
|
|
+ String target = link.path("target").asText("");
|
|
|
+ if (source.isBlank() || target.isBlank()) {
|
|
|
+ return "sankey 图校验失败:存在 source/target 为空的链接";
|
|
|
+ }
|
|
|
+ if (!nodeNames.contains(source)) {
|
|
|
+ return "sankey 图校验失败:链接 source「" + source + "」不在 series.data 节点里,"
|
|
|
+ + "请把它加入 data 或修正节点名";
|
|
|
+ }
|
|
|
+ if (!nodeNames.contains(target)) {
|
|
|
+ return "sankey 图校验失败:链接 target「" + target + "」不在 series.data 节点里,"
|
|
|
+ + "请把它加入 data 或修正节点名";
|
|
|
+ }
|
|
|
+ if (source.equals(target)) {
|
|
|
+ return "sankey 图校验失败:存在自环链接(" + source + "→" + target + "),sankey 不支持;请删除该链接";
|
|
|
+ }
|
|
|
+ // 双向边即二节点环:A→B 已存在时再来 B→A
|
|
|
+ if (edges.getOrDefault(target, Set.of()).contains(source)) {
|
|
|
+ return "sankey 图校验失败:检测到双向边(" + source + "⇄" + target + ")构成环;"
|
|
|
+ + "echarts sankey 不支持环——请去掉其中一条、合并为净流向(两个金额相减),"
|
|
|
+ + "或引入中间节点拆开(如 A→中转→B)";
|
|
|
+ }
|
|
|
+ edges.computeIfAbsent(source, k -> new LinkedHashSet<>()).add(target);
|
|
|
+ }
|
|
|
+ String cycle = findCycle(edges);
|
|
|
+ if (cycle != null) {
|
|
|
+ return "sankey 图校验失败:检测到循环链路(" + cycle + ");echarts sankey 不支持环,"
|
|
|
+ + "请调整链接方向或删除构成环的边";
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 多跳环检测(A→B→C→A 这类三节点以上循环);返回环路径描述,无环返回 null。
|
|
|
+ */
|
|
|
+ private static String findCycle(Map<String, Set<String>> edges) {
|
|
|
+ Map<String, Integer> state = new HashMap<>();
|
|
|
+ Map<String, String> parent = new HashMap<>();
|
|
|
+ for (String start : edges.keySet()) {
|
|
|
+ if (state.getOrDefault(start, 0) != 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Deque<String> stack = new ArrayDeque<>();
|
|
|
+ stack.push(start);
|
|
|
+ state.put(start, 1);
|
|
|
+ while (!stack.isEmpty()) {
|
|
|
+ String cur = stack.peek();
|
|
|
+ boolean advanced = false;
|
|
|
+ for (String next : edges.getOrDefault(cur, Set.of())) {
|
|
|
+ int st = state.getOrDefault(next, 0);
|
|
|
+ if (st == 1) {
|
|
|
+ // 回边 cur→next:沿 parent 链从 cur 走回 next,还原环路径
|
|
|
+ List<String> chain = new ArrayList<>();
|
|
|
+ chain.add(cur);
|
|
|
+ String node = parent.get(cur);
|
|
|
+ while (node != null && !node.equals(next)) {
|
|
|
+ chain.add(node);
|
|
|
+ node = parent.get(node);
|
|
|
+ }
|
|
|
+ chain.add(next);
|
|
|
+ Collections.reverse(chain);
|
|
|
+ chain.add(next);
|
|
|
+ return String.join("→", chain);
|
|
|
+ }
|
|
|
+ if (st == 0) {
|
|
|
+ parent.put(next, cur);
|
|
|
+ state.put(next, 1);
|
|
|
+ stack.push(next);
|
|
|
+ advanced = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!advanced) {
|
|
|
+ state.put(cur, 2);
|
|
|
+ stack.pop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
// ==================== 内部实现 ====================
|
|
|
|
|
|
// 列推导 / 单元格清洗 / 分页切片 / 返回 JSON 组装统一由 ToolResultTable 提供,
|