feat(ssw-common): SswLLMClient 改读 SSW_LLM_* 环境变量 + minimax 适配 (BS-新61/62)

## 背景
- minimax 替换 DeepSeek 作为后端 AI 推理
- 配置信息在环境变量 (SSW_LLM_*), 同步到 base_ai 表

## 数据源优先级 (v0.16+ BS-新62)
1. 环境变量 SSW_LLM_BASE_URL / SSW_LLM_API_KEY / SSW_LLM_MODEL (推荐)
2. base_ai 表 (主库 dbLinkId=0, fallback)
3. 环境变量 ANTHROPIC_* (last fallback)

## minimax URL normalize (BS-新61)
- minimax baseUrl 末尾带 / (如 https://token-plan-cn.xiaomimimo.com/v1/)
- 去掉末尾 / 避免 /v1//chat/completions 双斜杠返 301
- 已修复

## 协议
- OpenAI 兼容 chat/completions (minimax/Anthropic/OpenAI/DeepSeek 都支持)
- Anthropic 兼容是独立协议, 不混用

## 编译
- mvn compile -pl ssw-common/ssw-common-biz -am → BUILD SUCCESS (4.212s)
- Java 8 only (无 Map.of, 用 HashMap)

## 相关
- base_ai 表已切: minimaxi enabled=1, DeepSeek enabled=0
- dacli 0 错误读 minimax (DB 走通)
This commit is contained in:
ssw committed 2026-07-06 21:22:27 +08:00
1 parent eae1e2fcba
commit 2d6a91d858
1 file changed
+55 -16
@@ -20,8 +20,13 @@ import java.util.Map;
* 背景: jnpf SendAiMessage 是 static 方法, 硬编码 maxTokens=1500, 不读 yml,
* 也不对 LLM 输出做 format 修复. SSW 拦截层需要直接调 LLM API.
*
* 数据源: 从 base_ai 表 (主库 dbLinkId=0) 读 default AI 配置 (f_baseUrl, f_credential, f_model)
* API 协议: OpenAI 兼容 (MiniMax / 通义千问 dashscope / OpenAI 都支持)
* 协议: OpenAI 兼容 chat/completions (base_ai 表里填 OpenAI 地址)
* 数据源优先级 (v0.16+ BS-新62, 2026-07-06):
* 1. 环境变量 SSW_LLM_BASE_URL / SSW_LLM_API_KEY / SSW_LLM_MODEL
* 2. base_ai 表 (主库 dbLinkId=0, f_enabled_mark=1, 最新创建)
* 3. 环境变量 ANTHROPIC_* (fallback 给 Claude Code 兼容场景)
*
* v0.16+ (BS-新61): minimaxi 切换, baseUrl 末尾带 / 时 normalize
*
* @author claude (2026-07-02)
*/
@@ -42,7 +47,7 @@ public class SswLLMClient {
}
/**
* 调 LLM API (chat/completions)
* 调 LLM API (OpenAI 兼容 /v1/chat/completions)
*
* @param systemPrompt 系统提示词 (如 jnpf 的 GEN_MODEL_QUETION)
* @param userPrompt 用户提示词 (如 "--ai" 描述)
@@ -55,7 +60,7 @@ public class SswLLMClient {
String model = (String) aiConfig.get("model");
if (baseUrl == null || credential == null || model == null) {
log.error("SswLLMClient: AI 配置缺失 baseUrl={}, credential={}, model={}",
log.error("[SswLLMClient] AI 配置缺失 baseUrl={}, credential={}, model={}",
baseUrl, credential, model);
return null;
}
@@ -78,10 +83,9 @@ public class SswLLMClient {
body.put("messages", messages);
try {
// v0.15+ (2026-07-03): base_ai 的 baseUrl 是 API 前缀 (如 https://api.deepseek.com/v1),
// 实际 OpenAI-compatible endpoint 是 /v1/chat/completions.
// 之前的 HttpRequest.post(baseUrl) 直接 POST 到 /v1, minimaxi 返 301, deepseek 返 404.
String url = baseUrl.endsWith("/chat/completions") ? baseUrl : baseUrl + "/chat/completions";
// v0.16+ (BS-新61): 末尾 / normalize, 避免 /v1//chat/completions 双斜杠
String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
String url = normalized.endsWith("/chat/completions") ? normalized : normalized + "/chat/completions";
log.info("[SswLLMClient] POST {} model={} maxTokens={}", url, model, maxTokens);
HttpResponse resp = HttpRequest.post(url)
.header("Authorization", "Bearer " + credential)
@@ -121,24 +125,59 @@ public class SswLLMClient {
}
/**
* 从 base_ai 表读 default AI 配置 (主库 dbLinkId=0)
* 加载 AI 配置: 优先级
* 1. 环境变量 SSW_LLM_* (推荐, 2026-07-06 BS-新62)
* 2. base_ai 表 (fallback, 主库 dbLinkId=0)
* 3. 环境变量 ANTHROPIC_* (最后 fallback)
*
* 注意: 全部用 OpenAI 兼容 chat/completions 协议 (minimax/Anthropic/OpenAI/DeepSeek 都支持)
*/
@SuppressWarnings("unchecked")
private Map<String, Object> loadDefaultAIConfig() {
// 1. SSW_LLM_* 环境变量
String envBaseUrl = System.getenv("SSW_LLM_BASE_URL");
String envCredential = System.getenv("SSW_LLM_API_KEY");
String envModel = System.getenv("SSW_LLM_MODEL");
if (envBaseUrl != null && envCredential != null && envModel != null) {
log.info("[SswLLMClient] AI 配置: 环境变量 SSW_LLM_* (model={})", envModel);
Map<String, Object> env = new HashMap<>();
env.put("baseUrl", envBaseUrl);
env.put("credential", envCredential);
env.put("model", envModel);
return env;
}
// 2. base_ai 表
try {
String sql = "SELECT f_baseurl AS baseUrl, f_credential AS credential, f_model AS model " +
"FROM base_ai WHERE f_enabled_mark = 1 " +
"ORDER BY f_creator_time DESC LIMIT 1";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
if (rows == null || rows.isEmpty()) {
log.error("SswLLMClient: base_ai 表没有启用记录");
return new HashMap<>();
if (rows != null && !rows.isEmpty()) {
Map<String, Object> row = rows.get(0);
log.info("[SswLLMClient] AI 配置: base_ai 表 (model={})", row.get("model"));
return row;
}
return rows.get(0);
log.warn("[SswLLMClient] base_ai 表没有启用记录");
} catch (Exception e) {
log.error("SswLLMClient: 读 base_ai 失败: {}", e.getMessage());
return new HashMap<>();
log.error("[SswLLMClient] 读 base_ai 失败: {}", e.getMessage());
}
// 3. ANTHROPIC_* 环境变量 (last fallback)
String aBaseUrl = System.getenv("ANTHROPIC_BASE_URL");
String aCred = System.getenv("ANTHROPIC_AUTH_TOKEN");
String aModel = System.getenv("ANTHROPIC_MODEL");
if (aBaseUrl != null && aCred != null && aModel != null) {
log.info("[SswLLMClient] AI 配置: 环境变量 ANTHROPIC_* (model={})", aModel);
Map<String, Object> env = new HashMap<>();
env.put("baseUrl", aBaseUrl);
env.put("credential", aCred);
env.put("model", aModel);
return env;
}
log.error("[SswLLMClient] AI 配置完全缺失 (SSW_LLM_*/base_ai/ANTHROPIC_* 都无)");
return new HashMap<>();
}
/** 兼容: 不传 systemPrompt 用默认 */
@@ -153,4 +192,4 @@ public class SswLLMClient {
"fieldComponent (input/textarea/inputNumber/radio/select/switch/datePicker 等). " +
"如果字段是 radio/select, 需要 fieldOptions 数组, 元素格式 {\"id\":\"1\",\"fullName\":\"选项名\"}. " +
"请只返回 JSON, 不要包含其他文字或 markdown 包裹.";
}
}