feat: 贯通 context_window 配置并按 token 预算裁剪对话历史

This commit is contained in:
2026-09-20 14:22:53 +08:00
parent 8251a57010
commit 4114b0b4fb
21 changed files with 307 additions and 42 deletions
@@ -17,4 +17,10 @@ public class UpsertAgentLlmSettingsDTO {
/** 首次必填;后续留空表示不改 Key */
private String apiKey;
/**
* 模型上下文窗口(token)。空/不传表示不改(更新时)或由 Agent 估计(新建)。
* 传正整数则写入;部分前端可用 0 表示清空为「自动估计」。
*/
private Integer contextWindow;
}
@@ -33,6 +33,10 @@ public class AgentCompanyLlmSettingsPO {
@TableField("model")
private String model;
/** 模型上下文窗口(token);null 表示由 Agent 按模型名估计 */
@TableField("context_window")
private Integer contextWindow;
@TableField("extra_headers_json")
private String extraHeadersJson;
@@ -12,5 +12,7 @@ public class AgentLlmRuntimeVO {
private String baseUrl;
private String apiKey;
private String model;
/** 上下文窗口 token;可能为 null */
private Integer contextWindow;
private Map<String, String> extraHeaders;
}
@@ -10,6 +10,8 @@ public class AgentLlmSettingsVO {
private boolean configured;
private String baseUrl;
private String model;
/** 上下文窗口 token;null 表示未配置、由 Agent 估计 */
private Integer contextWindow;
private String apiKeyMasked;
private String updateTime;
}
@@ -86,6 +86,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
po.setApiKeyEncrypted(encrypted);
po.setApiKeySuffix(suffix);
po.setModel(model);
po.setContextWindow(normalizeContextWindow(dto.getContextWindow()));
po.setCreateBy(userId);
po.setUpdateBy(userId);
mapper.insert(po);
@@ -95,6 +96,9 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
existing.setApiKeyEncrypted(encrypted);
existing.setApiKeySuffix(suffix);
existing.setModel(model);
if (dto.getContextWindow() != null) {
existing.setContextWindow(normalizeContextWindow(dto.getContextWindow()));
}
existing.setUpdateBy(userId);
mapper.updateById(existing);
}
@@ -132,10 +136,19 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
vo.setBaseUrl(po.getBaseUrl());
vo.setApiKey(crypto.decrypt(po.getApiKeyEncrypted()));
vo.setModel(po.getModel());
vo.setContextWindow(po.getContextWindow());
vo.setExtraHeaders(parseHeaders(po.getExtraHeadersJson()));
return vo;
}
/** 0 或负数视为清空(自动估计);正整数写入。 */
private static Integer normalizeContextWindow(Integer raw) {
if (raw == null || raw <= 0) {
return null;
}
return raw;
}
private Map<String, String> parseHeaders(String json) {
if (!StringUtils.hasText(json)) {
return Map.of();
@@ -163,6 +176,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
vo.setConfigured(true);
vo.setBaseUrl(po.getBaseUrl());
vo.setModel(po.getModel());
vo.setContextWindow(po.getContextWindow());
vo.setApiKeyMasked("****" + (po.getApiKeySuffix() == null ? "" : po.getApiKeySuffix()));
if (po.getUpdateTime() != null) {
vo.setUpdateTime(po.getUpdateTime().format(ISO));
@@ -180,6 +194,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
payload.put("base_url", runtime.getBaseUrl());
payload.put("api_key", runtime.getApiKey());
payload.put("model", runtime.getModel());
payload.put("context_window", runtime.getContextWindow());
payload.put("extra_headers", runtime.getExtraHeaders() == null ? Map.of() : runtime.getExtraHeaders());
stringRedisTemplate.opsForValue().set(
redisKey(companyId),
@@ -202,6 +217,19 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
vo.setBaseUrl(String.valueOf(map.get("base_url")));
vo.setApiKey(String.valueOf(map.get("api_key")));
vo.setModel(String.valueOf(map.get("model")));
Object cw = map.get("context_window");
if (cw instanceof Number n) {
vo.setContextWindow(n.intValue());
} else if (cw != null && StringUtils.hasText(String.valueOf(cw))
&& !"null".equalsIgnoreCase(String.valueOf(cw))) {
try {
vo.setContextWindow(Integer.parseInt(String.valueOf(cw)));
} catch (NumberFormatException ignored) {
vo.setContextWindow(null);
}
} else {
vo.setContextWindow(null);
}
Object headers = map.get("extra_headers");
if (headers instanceof Map<?, ?> m) {
Map<String, String> h = new HashMap<>();