fix: 仅按 Token 阈值压缩上下文

- EasyFlow 调用方忽略新旧消息数压缩配置

- 保留最小 Token 阈值与最近消息保留数量

- 隐藏智能体配置页的消息数阈值
This commit is contained in:
2026-07-23 19:49:25 +08:00
parent 8b4ba65e1c
commit 417e846cbd
3 changed files with 60 additions and 34 deletions

View File

@@ -54,6 +54,11 @@ public class AgentRuntimeCompiler {
private static final Logger LOG = LoggerFactory.getLogger(AgentRuntimeCompiler.class);
private static final int LOG_TEXT_MAX_LENGTH = 500;
/**
* EasyFlow 仅按 Token 阈值触发压缩,消息数阈值固定为不可达上限。
*/
private static final int DISABLED_MESSAGE_COMPRESSION_THRESHOLD = Integer.MAX_VALUE;
private static final Pattern MCP_INPUT_PATTERN = Pattern.compile("\\$\\{input:([A-Za-z0-9_.-]+)}");
@Resource
@@ -147,6 +152,14 @@ public class AgentRuntimeCompiler {
return options;
}
/**
* 编译 Agent 记忆策略。
*
* <p>EasyFlow 侧统一关闭按消息数触发压缩,已保存的旧消息阈值配置不会进入运行时。</p>
*
* @param config 记忆配置
* @return 运行时记忆策略
*/
private AgentMemoryPolicy buildMemoryPolicy(Map<String, Object> config) {
AgentMemoryPolicy policy = new AgentMemoryPolicy();
policy.setType(memoryTypeValue(config, "type"));
@@ -159,17 +172,7 @@ public class AgentRuntimeCompiler {
if (enabled != null) {
parameter.setEnabled(enabled);
}
Integer msgThreshold = intValue(compressionConfig, "msgThreshold");
if (msgThreshold == null) {
msgThreshold = intValue(config, "maxAttachedMessageCount");
}
if (msgThreshold == null) {
msgThreshold = intValue(config, "historyLimit");
}
if (msgThreshold != null) {
parameter.setMsgThreshold(msgThreshold);
policy.setMaxAttachedMessageCount(msgThreshold);
}
parameter.setMsgThreshold(DISABLED_MESSAGE_COMPRESSION_THRESHOLD);
Integer lastKeep = intValue(compressionConfig, "lastKeep");
if (lastKeep != null) {
parameter.setLastKeep(lastKeep);

View File

@@ -1,5 +1,6 @@
package tech.easyflow.agent.runtime;
import com.easyagents.agent.runtime.memory.AgentMemoryPolicy;
import com.easyagents.agent.runtime.model.AgentGenerationOptions;
import com.easyagents.agent.runtime.model.AgentHttpVersionPolicy;
import com.easyagents.agent.runtime.model.AgentModelSpec;
@@ -65,6 +66,36 @@ public class AgentRuntimeCompilerModelConfigTest {
Assert.assertEquals(AgentHttpVersionPolicy.AUTO, spec.getHttpVersionPolicy());
}
/**
* 验证 EasyFlow 忽略新旧消息数阈值,仅保留 Token 压缩配置。
*
* @throws Exception 反射调用失败时抛出
*/
@Test
public void memoryCompressionShouldIgnoreMessageThresholdConfiguration() throws Exception {
AgentRuntimeCompiler compiler = new AgentRuntimeCompiler();
Map<String, Object> compressionConfig = Map.of(
"enabled", true,
"msgThreshold", 12,
"lastKeep", 8,
"minCompressionTokenThreshold", 128000
);
Map<String, Object> memoryConfig = Map.of(
"compressionParameter", compressionConfig,
"maxAttachedMessageCount", 12,
"historyLimit", 12
);
AgentMemoryPolicy policy = invokeMemoryPolicy(compiler, memoryConfig);
Assert.assertEquals(Integer.valueOf(Integer.MAX_VALUE),
policy.getCompressionParameter().getMsgThreshold());
Assert.assertEquals(Integer.valueOf(128000),
policy.getCompressionParameter().getMinCompressionTokenThreshold());
Assert.assertEquals(8, policy.getCompressionParameter().getLastKeep());
Assert.assertEquals(50, policy.getMaxAttachedMessageCount());
}
/**
* 创建已注入模型服务的编译器。
*
@@ -127,4 +158,19 @@ public class AgentRuntimeCompilerModelConfigTest {
method.setAccessible(true);
return (AgentModelSpec) method.invoke(compiler, agent);
}
/**
* 调用私有记忆策略编译方法。
*
* @param compiler 编译器
* @param config 记忆配置
* @return 记忆策略
* @throws Exception 反射调用失败时抛出
*/
private AgentMemoryPolicy invokeMemoryPolicy(AgentRuntimeCompiler compiler,
Map<String, Object> config) throws Exception {
Method method = AgentRuntimeCompiler.class.getDeclaredMethod("buildMemoryPolicy", Map.class);
method.setAccessible(true);
return (AgentMemoryPolicy) method.invoke(compiler, config);
}
}