diff --git a/ssw-common/ssw-common-biz/pom.xml b/ssw-common/ssw-common-biz/pom.xml index 926dbf4..00d7315 100644 --- a/ssw-common/ssw-common-biz/pom.xml +++ b/ssw-common/ssw-common-biz/pom.xml @@ -20,6 +20,17 @@ jnpf-common-all ${project.version} + + + com.jnpf + jnpf-system-entity + ${project.version} + + + com.jnpf + jnpf-system-biz + ${project.version} + org.projectlombok lombok diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java index 102e472..2fcc269 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java @@ -5,13 +5,13 @@ import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import jnpf.base.entity.AiEntity; +import jnpf.base.service.AiService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -21,12 +21,14 @@ import java.util.Map; * 也不对 LLM 输出做 format 修复. SSW 拦截层需要直接调 LLM API. * * 协议: 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.18+ (2026-07-07, 用户反馈) + * 单一源 = base_ai 表 (主库 dbLinkId=0, f_enabled_mark=1, 最新创建). + * 删除环境变量分支: SSW_LLM_(env) / ANTHROPIC_(env) 不再用. + * 理由: Claude Code session 会自动设 ANTHROPIC_(env) / SSW_LLM_(env), 导致 base_ai 表配置失效, + * 测试不可重现, 项目级 AI 配置被覆盖. 用户原则 "单一源 = 行为可预测". * * v0.16+ (BS-新61): minimaxi 切换, baseUrl 末尾带 / 时 normalize + * v0.17+ (BS-新63): 协议自适应 (OpenAI / Anthropic 兼容) * * @author claude (2026-07-02) */ @@ -37,8 +39,14 @@ public class SswLLMClient { private static final int CONNECT_TIMEOUT = 5; private static final int READ_TIMEOUT = 60; + /** + * v0.18+ (2026-07-07): 用 jnpf AiService 替代 JdbcTemplate 直 SQL (用户反馈) + * - jnpf AiEntity: base_ai 表实体 (jnpf.base.entity.AiEntity) + * - jnpf AiService.getDefault(): 从 base_ai 表查 f_enabled_mark=1 的默认 AI 配置 + * - 避免 ssw-common 直接写 SQL 查 jnpf 表 (违反合规扩展层原则) + */ @Autowired - private JdbcTemplate jdbcTemplate; + private AiService aiService; private int maxTokens = 3000; @@ -184,59 +192,34 @@ public class SswLLMClient { } /** - * 加载 AI 配置: 优先级 - * 1. 环境变量 SSW_LLM_* (推荐, 2026-07-06 BS-新62) - * 2. base_ai 表 (fallback, 主库 dbLinkId=0) - * 3. 环境变量 ANTHROPIC_* (最后 fallback) + * 加载 AI 配置: v0.18+ 单一源 = base_ai 表 (经 jnpf AiService) * - * 注意: 全部用 OpenAI 兼容 chat/completions 协议 (minimax/Anthropic/OpenAI/DeepSeek 都支持) + * 删除 SSW_LLM_(env) / ANTHROPIC_(env) 环境变量分支 (用户反馈 2026-07-07): + * Claude Code session 自动设置环境变量, 会覆盖项目级 base_ai 表配置, + * 导致测试不可重现 + 项目级 AI 配置失效. + * + * 改用 jnpf AiService (用户反馈 2026-07-07): + * ssw-common 应通过 jnpf 基础包访问 jnpf 平台表, 不直接 SQL 查 jnpf 表. + * AiService.getDefault() 走 jnpf 框架 BaseMapper, 自动注入多数据源/事务. */ - @SuppressWarnings("unchecked") private Map 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 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> rows = jdbcTemplate.queryForList(sql); - if (rows != null && !rows.isEmpty()) { - Map row = rows.get(0); - log.info("[SswLLMClient] AI 配置: base_ai 表 (model={})", row.get("model")); - return row; + AiEntity ai = aiService.getDefault(); + if (ai == null) { + log.error("[SswLLMClient] base_ai 表没有默认 AI 配置 (AiService.getDefault() 返回 null)"); + return new HashMap<>(); } - log.warn("[SswLLMClient] base_ai 表没有启用记录"); + Map config = new HashMap<>(); + config.put("baseUrl", ai.getBaseUrl()); + config.put("credential", ai.getCredential()); + config.put("model", ai.getModel()); + log.info("[SswLLMClient] AI 配置: jnpf AiService.getDefault() (id={}, model={})", + ai.getId(), ai.getModel()); + return config; } catch (Exception e) { - log.error("[SswLLMClient] 读 base_ai 失败: {}", e.getMessage()); + log.error("[SswLLMClient] jnpf AiService 调用失败: {}", e.getMessage()); + return new HashMap<>(); } - - // 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 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 用默认 */