fix(ssw-common): SswLLMClient 失败显式错误 (v0.19+ BS-新71)

源: dos 24 个 P0 漏洞 + 4 步法固化清单

之前: loadDefaultAIConfig 失败时返空 Map, chat() 看到 baseUrl=null 仅 log "配置缺失" + return null.
  端点返 code=400 "组件属性推理失败, 返回空结果", 调用方不知道为啥.

修复:
- loadDefaultAIConfig 失败 (base_ai 无启用) → RuntimeException 含详细 msg:
  "AI 配置缺失 (baseUrl=null, credential=null, model=null). " +
  "请检查 base_ai 表 (主库 dbLinkId=0) 是否有 f_enabled_mark=1 记录"
- HTTP 失败 (resp.isOk()=false) → RuntimeException 含 status + body 前 200 字符
- LLM 响应含 error 字段 → RuntimeException 含 LLM 错误
- 异常 catch → 包装 RuntimeException 透传 (不再吞)
- chat() 方法签名加 @throws RuntimeException 注 (调用方要 try/catch)

影响范围:
- 端点返 ActionResult.fail(e.getMessage()) 透传详细错误
- 调用方 (dacli smart_props, --ai 等) 收到具体错误信息

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-07 15:21:06 +08:00
1 parent 039ae881af
commit 558a39a5bf
1 file changed
+22 -10
@@ -60,6 +60,7 @@ public class SswLLMClient {
* @param systemPrompt 系统提示词 (如 jnpf 的 GEN_MODEL_QUETION)
* @param userPrompt 用户提示词 (如 "--ai" 描述)
* @return LLM 原始输出 (content 字段)
* @throws RuntimeException v0.19+ (BS-新71): AI 配置缺失/异常时显式抛错, 透传到底
*/
public String chat(String systemPrompt, String userPrompt) {
Map<String, Object> aiConfig = loadDefaultAIConfig();
@@ -68,9 +69,12 @@ public class SswLLMClient {
String model = (String) aiConfig.get("model");
if (baseUrl == null || credential == null || model == null) {
log.error("[SswLLMClient] AI 配置缺失 baseUrl={}, credential={}, model={}",
String err = String.format(
"AI 配置缺失 (baseUrl=%s, credential=%s, model=%s). " +
"请检查 base_ai 表 (主库 dbLinkId=0) 是否有 f_enabled_mark=1 记录",
baseUrl, credential, model);
return null;
log.error("[SswLLMClient] {}", err);
throw new RuntimeException(err);
}
JSONObject body = new JSONObject();
@@ -121,15 +125,19 @@ public class SswLLMClient {
HttpResponse resp = req.body(requestBody).timeout(READ_TIMEOUT * 1000).execute();
if (!resp.isOk()) {
log.error("[SswLLMClient] HTTP {} body={}", resp.getStatus(), resp.body());
return null;
// v0.19+ BS-新71: HTTP 失败显式错误 (含 status + body)
String errMsg = String.format("LLM API HTTP %d: %s", resp.getStatus(),
resp.body() != null ? resp.body().substring(0, Math.min(200, resp.body().length())) : "");
log.error("[SswLLMClient] {}", errMsg);
throw new RuntimeException(errMsg);
}
JSONObject respJson = JSON.parseObject(resp.body());
if (respJson.containsKey("error") || respJson.containsKey("type") && "error".equals(respJson.getString("type"))) {
log.error("[SswLLMClient] LLM error: {}", respJson.getString("error") != null
String errMsg = "LLM 响应 error: " + (respJson.getString("error") != null
? respJson.getString("error") : respJson.getString("message"));
return null;
log.error("[SswLLMClient] {}", errMsg);
throw new RuntimeException(errMsg);
}
// 协议差异解析: OpenAI choices[0].message.content vs Anthropic content[0].text
@@ -139,8 +147,7 @@ public class SswLLMClient {
if (contentArr != null && !contentArr.isEmpty()) {
content = contentArr.getJSONObject(0).getString("text");
} else {
log.error("[SswLLMClient] Anthropic 响应无 content: {}", respJson.toJSONString());
return null;
throw new RuntimeException("Anthropic 响应无 content: " + respJson.toJSONString());
}
JSONObject usage = respJson.getJSONObject("usage");
if (usage != null) {
@@ -162,9 +169,14 @@ public class SswLLMClient {
}
return content;
} catch (RuntimeException re) {
// v0.19+ BS-新71: 透传, 不吞
throw re;
} catch (Exception e) {
log.error("[SswLLMClient] 调用异常: {}", e.getMessage(), e);
return null;
// v0.19+ BS-新71: 包装网络异常显式错误
String errMsg = "SswLLMClient 调用异常: " + e.getMessage();
log.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
}