auto(14b5ea1b9bd9): session end @ 2026-07-22T16:46:31 (+3f)
This commit is contained in:
1 parent
3fd7adcdb2
commit
f5ce7d925b
3 files changed
+136
-28
No files matched your search
@@ -19,7 +19,6 @@
|
||||
<module>jnpf-admin</module>
|
||||
<module>ssw-common</module>
|
||||
<module>ssw-dos</module>
|
||||
<module>ssw-jxcj</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -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 格式
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user