merge: SSW AI proxy fixes

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-22 16:56:35 +08:00
commit 13d45b29b4
4 files changed
+334 -28

No files matched your search

-1
View File
@@ -19,7 +19,6 @@
<module>jnpf-admin</module>
<module>ssw-common</module>
<module>ssw-dos</module>
<module>ssw-jxcj</module>
</modules>
<properties>
+13 -1
View File
@@ -41,10 +41,22 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- v0.21+ (2026-07-22 BS-eventhub): 单元测试 (URL resolver + 响应解析) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.12.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- v0.21+ (2026-07-22): surefire 3.x 支持 JUnit Platform (JUnit 5) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
</project>
@@ -100,16 +100,12 @@ public class SswLLMClient {
// → baseUrl/chat/completions
// - Anthropic 兼容 (https://api.minimaxi.com/anthropic)
// → baseUrl/v1/messages
String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
String url;
boolean isAnthropic = normalized.contains("/anthropic");
if (isAnthropic) {
url = normalized.endsWith("/v1/messages") ? normalized : normalized + "/v1/messages";
log.info("[SswLLMClient] POST {} (anthropic) model={} maxTokens={}", url, model, maxTokens);
} else {
url = normalized.endsWith("/chat/completions") ? normalized : normalized + "/chat/completions";
log.info("[SswLLMClient] POST {} (openai) model={} maxTokens={}", url, model, maxTokens);
}
// - SSW internal proxy (http://nginx/api/ssw/ai/openai-proxy)
// → 精确使用, 不追加任何后缀 (它本身就是 chat/completions 端点)
boolean isAnthropic = isAnthropicBase(baseUrl);
String url = resolveChatUrl(baseUrl);
log.info("[SswLLMClient] POST {} ({}) model={} maxTokens={}",
url, isAnthropic ? "anthropic" : "openai", model, maxTokens);
String requestBody = isAnthropic
? toAnthropicBody(model, systemPrompt, userPrompt, maxTokens)
: body.toJSONString();
@@ -158,22 +154,7 @@ public class SswLLMClient {
} else {
// v0.20+ P0 修复: OpenAI 响应 schema 完整 null-check, 防 fastjson NPE
// 背景: LLM 限流/超时/错误响应可能只有 {"error":...} 而无 choices, 链式调用炸
JSONArray choicesArr = respJson.getJSONArray("choices");
if (choicesArr == null || choicesArr.isEmpty()) {
throw new RuntimeException("OpenAI 响应无 choices: " + respJson.toJSONString());
}
JSONObject choice0 = choicesArr.getJSONObject(0);
if (choice0 == null) {
throw new RuntimeException("OpenAI choices[0] 为空: " + respJson.toJSONString());
}
JSONObject message = choice0.getJSONObject("message");
if (message == null) {
throw new RuntimeException("OpenAI choices[0].message 为空: " + respJson.toJSONString());
}
content = message.getString("content");
if (content == null) {
throw new RuntimeException("OpenAI choices[0].message.content 为空: " + respJson.toJSONString());
}
content = parseOpenAiContent(respJson);
JSONObject usage = respJson.getJSONObject("usage");
if (usage != null) {
log.info("[SswLLMClient] usage: prompt={} completion={}/{}",
@@ -194,6 +175,122 @@ public class SswLLMClient {
}
}
/**
* v0.21+ (2026-07-22 BS-eventhub): 解析 LLM chat/completions 目标 URL.
*
* 协议自适应 + SSW internal proxy 精确匹配:
* - SSW internal proxy (http://nginx/api/ssw/ai/openai-proxy 以及 query/fragment/trailing-slash 变体)
* → 精确使用, 不追加任何后缀. 它本身就是 chat/completions 端点,
* 追加 /chat/completions 会让 provider credential 命中 JNPF AuthFilter 被当用户 token
* 误判, 返回 {code:600, msg:'Login expired'}.
* - Anthropic 兼容 (含 /anthropic) → baseUrl/v1/messages
* - OpenAI 兼容 (其余) → baseUrl/chat/completions
*/
static String resolveChatUrl(String baseUrl) {
String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
// SSW internal proxy 精确匹配: 端点本身即 chat/completions, 不追加任何后缀.
// 追加 /chat/completions 会让 JNPF AuthFilter 把 provider credential 当用户 token 误判,
// 返回 {code:600, msg:'Login expired'}.
// 0.21.x 同时识别 query/fragment 变体 (?token=..., #frag); 用 URI path 而非 endsWith.
if (isSswInternalProxy(normalized)) {
return normalized;
}
if (isAnthropicBase(normalized)) {
return normalized.endsWith("/v1/messages") ? normalized : normalized + "/v1/messages";
}
return normalized.endsWith("/chat/completions") ? normalized : normalized + "/chat/completions";
}
/**
* v0.21+ baseUrl 是否为 SSW 内部代理端点 (/api/ssw/ai/openai-proxy).
* 该端点本身即 OpenAI chat/completions 契约, 不能再追加路径后缀.
*
* 0.21.x: 支持 query/fragment/trailing-slash 变体. 用 java.net.URI 解析 path,
* 等价于剥离 ?query 与 #fragment 后再 endsWith, 不依赖 regex.
* 同时剥离 path 尾部的 "/" (处理 .../openai-proxy/?query= 情形).
* 解析失败回退到原 endsWith, 保持向后兼容.
*/
static boolean isSswInternalProxy(String baseUrl) {
String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
try {
// 仅 URI 语法解析, 不触发网络 — 对 path-only 字符串无副作用.
java.net.URI uri = new java.net.URI(normalized);
String path = uri.getPath(); // 已剥离 query/fragment
if (path != null && path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path != null && path.endsWith("/api/ssw/ai/openai-proxy");
} catch (java.net.URISyntaxException e) {
// 解析失败 (极少见, 几乎只在 baseUrl 含未编码空格) → 退回 endsWith.
return normalized.endsWith("/api/ssw/ai/openai-proxy");
}
}
/**
* v0.21+ baseUrl 是否走 Anthropic 协议 (含 /anthropic 路径段).
* 同样在 path 上检查, 忽略 query/fragment.
*/
static boolean isAnthropicBase(String baseUrl) {
String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
try {
java.net.URI uri = new java.net.URI(normalized);
String path = uri.getPath();
if (path == null) return false;
return path.contains("/anthropic") || normalized.contains("/anthropic");
} catch (java.net.URISyntaxException e) {
return normalized.contains("/anthropic");
}
}
/**
* v0.21+ 解析 OpenAI chat/completions 响应 content, 完整 null-check 防 NPE.
*
* v0.20+ P0: LLM 限流/超时可能只有 {"error":...} 而无 choices, 链式调用炸.
* v0.21.x (BS-eventhub 强化): 在解析 choices **前**无条件识别业务错误信封
* {code:Number, msg:String}, 即使响应同时携带 choices (网关/混合代理场景).
* 上一版仅在 !choices 时识别, 会让 {code:600, choices:[]} / {code:600, choices:[empty]} 等
* 混合响应丢失真实业务错误, 退化为模糊的 'OpenAI choices 内容为空'.
*/
static String parseOpenAiContent(JSONObject respJson) {
// BS-eventhub 强化: 无条件识别 code != 200 (成功) + msg 业务错误.
// 容忍 code 字段为 Number / 数字字符串 / null, msg 为任意 String; 仅依赖二者同时存在.
// 后续 JSON parse 也只读 String "msg", 不抛类型异常.
if (respJson.containsKey("msg") && respJson.containsKey("code")) {
Object codeObj = respJson.get("code");
boolean isBusinessError = false;
String codeStr = String.valueOf(codeObj);
if (codeObj instanceof Number) {
isBusinessError = ((Number) codeObj).intValue() != 200;
} else if (codeObj != null) {
// 字符串数字 (如 "600") 也视作业务错误, 但 "200" 跳过.
try { isBusinessError = Integer.parseInt(codeStr) != 200; }
catch (NumberFormatException ignored) { /* 未知类型, 不影响 choices 路径 */ }
}
if (isBusinessError) {
String msg = respJson.getString("msg");
throw new RuntimeException("LLM 业务错误 (code=" + codeStr
+ ", msg=" + msg + "): " + respJson.toJSONString());
}
}
JSONArray choicesArr = respJson.getJSONArray("choices");
if (choicesArr == null || choicesArr.isEmpty()) {
throw new RuntimeException("OpenAI 响应无 choices: " + respJson.toJSONString());
}
JSONObject choice0 = choicesArr.getJSONObject(0);
if (choice0 == null) {
throw new RuntimeException("OpenAI choices[0] 为空: " + respJson.toJSONString());
}
JSONObject message = choice0.getJSONObject("message");
if (message == null) {
throw new RuntimeException("OpenAI choices[0].message 为空: " + respJson.toJSONString());
}
String content = message.getString("content");
if (content == null) {
throw new RuntimeException("OpenAI choices[0].message.content 为空: " + respJson.toJSONString());
}
return content;
}
/**
* v0.17+ 转换 OpenAI chat 格式 → Anthropic messages 格式
*
@@ -0,0 +1,198 @@
package ssw.common.util;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* SswLLMClient URL resolver + OpenAI 响应解析单元测试.
*
* 覆盖 BS-eventhub 修复 (2026-07-22):
* 1. SSW internal proxy URL 精确不追加 /chat/completions (核心回归)
* 2. OpenAI baseUrl 追加 /chat/completions
* 3. Anthropic baseUrl 追加 /v1/messages
* 4. 业务错误 JSON {code:600, msg:'Login expired'} 在 choices 解析前给出含 code/msg 的错误
*/
class SswLLMClientTest {
// --- 1. SSW internal proxy 精确匹配, 不追加后缀 ---
@Test
@DisplayName("internal proxy URL 精确使用, 不追加 /chat/completions")
void internalProxyUrlNotAppended() {
String base = "http://nginx/api/ssw/ai/openai-proxy";
assertEquals(base, SswLLMClient.resolveChatUrl(base),
"internal proxy 追加 /chat/completions 会被 JNPF AuthFilter 误判 credential 为用户 token");
}
@Test
@DisplayName("internal proxy URL 带 trailing slash 也精确使用")
void internalProxyUrlTrailingSlashNotAppended() {
String base = "http://nginx/api/ssw/ai/openai-proxy/";
assertEquals("http://nginx/api/ssw/ai/openai-proxy",
SswLLMClient.resolveChatUrl(base));
}
@Test
@DisplayName("internal proxy 不是 Anthropic 协议")
void internalProxyIsNotAnthropic() {
assertTrue(!SswLLMClient.isAnthropicBase("http://nginx/api/ssw/ai/openai-proxy"));
}
// --- 2. OpenAI baseUrl 追加 /chat/completions ---
@Test
@DisplayName("OpenAI baseUrl 追加 /chat/completions")
void openAiUrlAppendsChatCompletions() {
assertEquals("https://api.deepseek.com/v1/chat/completions",
SswLLMClient.resolveChatUrl("https://api.deepseek.com/v1"));
}
@Test
@DisplayName("OpenAI baseUrl trailing slash 也追加 /chat/completions")
void openAiUrlTrailingSlashAppendsChatCompletions() {
assertEquals("https://token-plan-cn.xiaomimimo.com/v1/chat/completions",
SswLLMClient.resolveChatUrl("https://token-plan-cn.xiaomimimo.com/v1/"));
}
@Test
@DisplayName("OpenAI baseUrl 已含 /chat/completions 不重复追加")
void openAiUrlIdempotent() {
String base = "https://api.deepseek.com/v1/chat/completions";
assertEquals(base, SswLLMClient.resolveChatUrl(base));
}
// --- 3. Anthropic baseUrl 追加 /v1/messages ---
@Test
@DisplayName("Anthropic baseUrl 追加 /v1/messages")
void anthropicUrlAppendsV1Messages() {
assertTrue(SswLLMClient.isAnthropicBase("https://api.minimaxi.com/anthropic"));
assertEquals("https://api.minimaxi.com/anthropic/v1/messages",
SswLLMClient.resolveChatUrl("https://api.minimaxi.com/anthropic"));
}
@Test
@DisplayName("Anthropic baseUrl 已含 /v1/messages 不重复追加")
void anthropicUrlIdempotent() {
String base = "https://api.minimaxi.com/anthropic/v1/messages";
assertEquals(base, SswLLMClient.resolveChatUrl(base));
}
// --- 4. 业务错误 JSON 在 choices 解析前给出含 code/msg 的错误 ---
@Test
@DisplayName("业务错误 JSON {code:600, msg:'Login expired'} 抛出含 code/msg 的错误, 而非 '无 choices'")
void businessErrorEnvelopeReportsCodeAndMsg() {
JSONObject resp = new JSONObject();
resp.put("code", 600);
resp.put("msg", "Login expired, please log in again");
RuntimeException ex = assertThrows(RuntimeException.class,
() -> SswLLMClient.parseOpenAiContent(resp));
String m = ex.getMessage();
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
assertTrue(m.contains("Login expired"), "错误信息应含业务 msg, 实际: " + m);
assertTrue(!m.contains("无 choices"),
"业务错误不应被误报为 'OpenAI 响应无 choices', 实际: " + m);
}
@Test
@DisplayName("正常 OpenAI 响应正确解析 content")
void parsesNormalOpenAiContent() {
JSONObject resp = JSONObject.parseObject(
"{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"hello\"}}]}");
assertEquals("hello", SswLLMClient.parseOpenAiContent(resp));
}
@Test
@DisplayName("无 choices 且非业务错误信封仍报 '无 choices'")
void missingChoicesStillReportsNoChoices() {
JSONObject resp = new JSONObject();
resp.put("object", "chat.completion");
RuntimeException ex = assertThrows(RuntimeException.class,
() -> SswLLMClient.parseOpenAiContent(resp));
assertTrue(ex.getMessage().contains("无 choices"), ex.getMessage());
}
@Test
@DisplayName("BS-eventhub 强化: 业务错误即使带空 choices 也被识别 (混合信封)")
void businessErrorWithEmptyChoicesStillReported() {
JSONObject resp = new JSONObject();
resp.put("code", 600);
resp.put("msg", "Login expired, please log in again");
resp.put("choices", new com.alibaba.fastjson.JSONArray());
RuntimeException ex = assertThrows(RuntimeException.class,
() -> SswLLMClient.parseOpenAiContent(resp));
String m = ex.getMessage();
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
assertTrue(m.contains("Login expired"), "错误信息应含业务 msg, 实际: " + m);
assertTrue(!m.contains("无 choices"),
"混合信封不应被误报为 'OpenAI 响应无 choices', 实际: " + m);
}
@Test
@DisplayName("BS-eventhub 强化: 业务错误即使 choices.content=null 也被识别 (混合信封)")
void businessErrorWithContentNullStillReported() {
JSONObject resp = new JSONObject();
resp.put("code", 600);
resp.put("msg", "Login expired");
com.alibaba.fastjson.JSONObject choice = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject message = new com.alibaba.fastjson.JSONObject();
message.put("content", (String) null);
choice.put("message", message);
com.alibaba.fastjson.JSONArray choices = new com.alibaba.fastjson.JSONArray();
choices.add(choice);
resp.put("choices", choices);
RuntimeException ex = assertThrows(RuntimeException.class,
() -> SswLLMClient.parseOpenAiContent(resp));
String m = ex.getMessage();
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
assertTrue(!m.contains("content 为空"),
"混合信封不应被误报为 'OpenAI choices[0].message.content 为空', 实际: " + m);
}
@Test
@DisplayName("code=200 即使带 msg 也不视为业务错误 (正常 OpenAI 也可能带 msg 字段)")
void code200WithMsgIsNotBusinessError() {
JSONObject resp = JSONObject.parseObject(
"{\"code\":200,\"msg\":\"ok\",\"choices\":[{\"message\":{\"content\":\"hi\"}}]}");
assertEquals("hi", SswLLMClient.parseOpenAiContent(resp));
}
// --- 5. query/fragment 变体的 SSW internal proxy 识别 (0.21.x) ---
@Test
@DisplayName("internal proxy URL 带 query string 仍精确使用, 不追加 /chat/completions")
void internalProxyUrlWithQueryStringNotAppended() {
String base = "http://nginx/api/ssw/ai/openai-proxy?app=testspace";
assertEquals(base, SswLLMClient.resolveChatUrl(base),
"internal proxy 带 query 应原样返回, 不追加 /chat/completions? 等");
}
@Test
@DisplayName("internal proxy URL 带 fragment 仍精确使用")
void internalProxyUrlWithFragmentNotAppended() {
String base = "http://nginx/api/ssw/ai/openai-proxy#default";
assertEquals(base, SswLLMClient.resolveChatUrl(base));
}
@Test
@DisplayName("internal proxy URL 带 query + trailing slash 仍精确使用 (不加 /chat/completions)")
void internalProxyUrlQueryAndTrailingSlashNotAppended() {
String base = "http://nginx/api/ssw/ai/openai-proxy/?app=testspace";
String resolved = SswLLMClient.resolveChatUrl(base);
// 行为契约: 不加 /chat/completions, 可能是 .../openai-proxy?app=testspace 或 .../openai-proxy/?app=testspace,
// 两种均正确. 这里接受任一等价写法, 绝不附加路径后缀.
assertTrue(!resolved.contains("/chat/completions"),
"internal proxy 不应追加 /chat/completions, 实际: " + resolved);
assertTrue(resolved.equals("http://nginx/api/ssw/ai/openai-proxy/?app=testspace")
|| resolved.equals("http://nginx/api/ssw/ai/openai-proxy?app=testspace"),
"URL 应保持 internal proxy 原结构, 实际: " + resolved);
}
}