feat: 支持 Completions/Responses 双协议,并在同选型下续链加速

This commit is contained in:
2026-09-24 17:10:34 +08:00
parent 9c2c1ed061
commit d20c6a1302
26 changed files with 533 additions and 43 deletions
@@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS `agent_company_llm_provider` (
`api_key_encrypted` varchar(2000) NOT NULL COMMENT 'API Key 密文',
`api_key_suffix` varchar(16) NOT NULL DEFAULT '' COMMENT 'Key 末几位(展示掩码)',
`display_name` varchar(100) NULL COMMENT '展示名,如 DeepSeek',
`api_style` varchar(32) NOT NULL DEFAULT 'auto' COMMENT 'API协议:auto|completions|responses',
`extra_headers_json` json NULL COMMENT '可选额外请求头',
`create_by` bigint UNSIGNED NULL COMMENT '创建人用户ID',
`update_by` bigint UNSIGNED NULL COMMENT '更新人用户ID',
@@ -0,0 +1,5 @@
-- 提供商增加 API 协议风格:auto | completions | responses
ALTER TABLE `agent_company_llm_provider`
ADD COLUMN `api_style` varchar(32) NOT NULL DEFAULT 'auto'
COMMENT 'API协议:auto=按base_url推断;completions;responses'
AFTER `display_name`;
@@ -16,4 +16,9 @@ public class CreateAgentLlmProviderDTO {
private String apiKey;
private String displayName;
/**
* API 协议:auto | completions | responses;空则 auto
*/
private String apiStyle;
}
@@ -16,4 +16,9 @@ public class UpdateAgentLlmProviderDTO {
/** 传 null 表示不改;传空串可清空备注 */
private String displayName;
/**
* API 协议:auto | completions | responses;null 不改
*/
private String apiStyle;
}
@@ -33,6 +33,12 @@ public class AgentCompanyLlmProviderPO {
@TableField("display_name")
private String displayName;
/**
* API 协议:auto | completions | responses
*/
@TableField("api_style")
private String apiStyle;
@TableField("extra_headers_json")
private String extraHeadersJson;
@@ -16,5 +16,9 @@ public class AgentLlmProviderVO {
private String displayName;
private String apiKeyMasked;
private String updateTime;
/**
* API 协议:auto | completions | responses
*/
private String apiStyle;
private List<AgentLlmModelVO> models = new ArrayList<>();
}
@@ -15,5 +15,9 @@ public class AgentLlmRuntimeVO {
private String model;
/** 上下文窗口 token;可能为 null */
private Integer contextWindow;
/**
* API 协议:auto | completions | responses
*/
private String apiStyle;
private Map<String, String> extraHeaders;
}
@@ -98,6 +98,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
po.setApiKeyEncrypted(crypto.encrypt(plain));
po.setApiKeySuffix(plain.length() >= 4 ? plain.substring(plain.length() - 4) : plain);
po.setDisplayName(trimToNull(dto.getDisplayName()));
po.setApiStyle(normalizeApiStyle(dto.getApiStyle()));
po.setCreateBy(userId);
po.setUpdateBy(userId);
providerMapper.insert(po);
@@ -123,6 +124,9 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
if (dto.getDisplayName() != null) {
existing.setDisplayName(trimToNull(dto.getDisplayName()));
}
if (dto.getApiStyle() != null) {
existing.setApiStyle(normalizeApiStyle(dto.getApiStyle()));
}
existing.setUpdateBy(userId);
providerMapper.updateById(existing);
evictRuntimeCache(companyId);
@@ -432,6 +436,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
vo.setApiKey(crypto.decrypt(provider.getApiKeyEncrypted()));
vo.setModel(model.getModel());
vo.setContextWindow(model.getContextWindow());
vo.setApiStyle(normalizeApiStyle(provider.getApiStyle()));
vo.setExtraHeaders(parseHeaders(provider.getExtraHeadersJson()));
return vo;
}
@@ -450,6 +455,17 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
}
}
private static String normalizeApiStyle(String raw) {
if (!StringUtils.hasText(raw)) {
return "auto";
}
String s = raw.trim().toLowerCase();
if ("auto".equals(s) || "completions".equals(s) || "responses".equals(s)) {
return s;
}
throw new BusinessException("apiStyle 必须是 auto / completions / responses");
}
private static Integer normalizeContextWindow(Integer raw) {
if (raw == null || raw <= 0) {
return null;
@@ -546,6 +562,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
vo.setConfigured(true);
vo.setBaseUrl(provider.getBaseUrl());
vo.setDisplayName(provider.getDisplayName());
vo.setApiStyle(normalizeApiStyle(provider.getApiStyle()));
vo.setApiKeyMasked("****" + (provider.getApiKeySuffix() == null ? "" : provider.getApiKeySuffix()));
if (provider.getUpdateTime() != null) {
vo.setUpdateTime(provider.getUpdateTime().format(ISO));
@@ -570,6 +587,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
payload.put("api_key", runtime.getApiKey());
payload.put("model", runtime.getModel());
payload.put("context_window", runtime.getContextWindow());
payload.put("api_style", runtime.getApiStyle() == null ? "auto" : runtime.getApiStyle());
payload.put("extra_headers",
runtime.getExtraHeaders() == null ? Map.of() : runtime.getExtraHeaders());
stringRedisTemplate.opsForValue().set(
@@ -612,6 +630,8 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
} else {
vo.setContextWindow(null);
}
Object style = map.get("api_style");
vo.setApiStyle(style == null ? "auto" : normalizeApiStyle(String.valueOf(style)));
Object headers = map.get("extra_headers");
if (headers instanceof Map<?, ?> m) {
Map<String, String> h = new HashMap<>();