From 825813e2dd90cf8bdc48acbb6eee85159bc33b4d Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 03 三月 2026 13:04:28 +0800
Subject: [PATCH] #AI LLM路由

---
 src/main/java/com/zy/ai/service/LlmRoutingService.java |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/zy/ai/service/LlmRoutingService.java b/src/main/java/com/zy/ai/service/LlmRoutingService.java
index 4323d6a..96c4805 100644
--- a/src/main/java/com/zy/ai/service/LlmRoutingService.java
+++ b/src/main/java/com/zy/ai/service/LlmRoutingService.java
@@ -12,6 +12,7 @@
 
 import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -29,6 +30,14 @@
 
     private volatile List<LlmRouteConfig> allRouteCache = Collections.emptyList();
     private volatile long cacheExpireAt = 0L;
+    private static final Comparator<LlmRouteConfig> ROUTE_ORDER = (a, b) -> {
+        int pa = a == null || a.getPriority() == null ? Integer.MAX_VALUE : a.getPriority();
+        int pb = b == null || b.getPriority() == null ? Integer.MAX_VALUE : b.getPriority();
+        if (pa != pb) return Integer.compare(pa, pb);
+        long ia = a == null || a.getId() == null ? Long.MAX_VALUE : a.getId();
+        long ib = b == null || b.getId() == null ? Long.MAX_VALUE : b.getId();
+        return Long.compare(ia, ib);
+    };
 
     public void evictCache() {
         cacheExpireAt = 0L;
@@ -63,9 +72,11 @@
         }
         if (result.isEmpty() && !coolingRoutes.isEmpty()) {
             // 閬垮厤鎵�鏈夎矾鐢遍兘澶勪簬鍐峰嵈鏃剁郴缁熷畬鍏ㄤ笉鍙敤锛岄檷绾у厑璁镐娇鐢ㄥ喎鍗磋矾鐢�
+            coolingRoutes.sort(ROUTE_ORDER);
             log.warn("LLM 璺敱鍧囧浜庡喎鍗达紝闄嶇骇鍚敤鍐峰嵈璺敱銆俢ooling={}, total={}", coolingRoutes.size(), total);
             return coolingRoutes;
         }
+        result.sort(ROUTE_ORDER);
         if (result.isEmpty()) {
             log.warn("鏈壘鍒板彲鐢� LLM 璺敱銆倀otal={}, disabled={}, invalid={}", total, disabled, invalid);
         }
@@ -147,7 +158,12 @@
             EntityWrapper<LlmRouteConfig> wrapper = new EntityWrapper<>();
             wrapper.orderBy("priority", true).orderBy("id", true);
             List<LlmRouteConfig> list = llmRouteConfigService.selectList(wrapper);
-            allRouteCache = list == null ? Collections.emptyList() : list;
+            if (list == null) {
+                allRouteCache = Collections.emptyList();
+            } else {
+                list.sort(ROUTE_ORDER);
+                allRouteCache = list;
+            }
             cacheExpireAt = System.currentTimeMillis() + CACHE_TTL_MS;
             return allRouteCache;
         }

--
Gitblit v1.9.1