fix: 仅按 Token 阈值压缩上下文
- EasyFlow 调用方忽略新旧消息数压缩配置 - 保留最小 Token 阈值与最近消息保留数量 - 隐藏智能体配置页的消息数阈值
This commit is contained in:
@@ -54,6 +54,11 @@ public class AgentRuntimeCompiler {
|
|||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(AgentRuntimeCompiler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(AgentRuntimeCompiler.class);
|
||||||
private static final int LOG_TEXT_MAX_LENGTH = 500;
|
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_.-]+)}");
|
private static final Pattern MCP_INPUT_PATTERN = Pattern.compile("\\$\\{input:([A-Za-z0-9_.-]+)}");
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@@ -147,6 +152,14 @@ public class AgentRuntimeCompiler {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编译 Agent 记忆策略。
|
||||||
|
*
|
||||||
|
* <p>EasyFlow 侧统一关闭按消息数触发压缩,已保存的旧消息阈值配置不会进入运行时。</p>
|
||||||
|
*
|
||||||
|
* @param config 记忆配置
|
||||||
|
* @return 运行时记忆策略
|
||||||
|
*/
|
||||||
private AgentMemoryPolicy buildMemoryPolicy(Map<String, Object> config) {
|
private AgentMemoryPolicy buildMemoryPolicy(Map<String, Object> config) {
|
||||||
AgentMemoryPolicy policy = new AgentMemoryPolicy();
|
AgentMemoryPolicy policy = new AgentMemoryPolicy();
|
||||||
policy.setType(memoryTypeValue(config, "type"));
|
policy.setType(memoryTypeValue(config, "type"));
|
||||||
@@ -159,17 +172,7 @@ public class AgentRuntimeCompiler {
|
|||||||
if (enabled != null) {
|
if (enabled != null) {
|
||||||
parameter.setEnabled(enabled);
|
parameter.setEnabled(enabled);
|
||||||
}
|
}
|
||||||
Integer msgThreshold = intValue(compressionConfig, "msgThreshold");
|
parameter.setMsgThreshold(DISABLED_MESSAGE_COMPRESSION_THRESHOLD);
|
||||||
if (msgThreshold == null) {
|
|
||||||
msgThreshold = intValue(config, "maxAttachedMessageCount");
|
|
||||||
}
|
|
||||||
if (msgThreshold == null) {
|
|
||||||
msgThreshold = intValue(config, "historyLimit");
|
|
||||||
}
|
|
||||||
if (msgThreshold != null) {
|
|
||||||
parameter.setMsgThreshold(msgThreshold);
|
|
||||||
policy.setMaxAttachedMessageCount(msgThreshold);
|
|
||||||
}
|
|
||||||
Integer lastKeep = intValue(compressionConfig, "lastKeep");
|
Integer lastKeep = intValue(compressionConfig, "lastKeep");
|
||||||
if (lastKeep != null) {
|
if (lastKeep != null) {
|
||||||
parameter.setLastKeep(lastKeep);
|
parameter.setLastKeep(lastKeep);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package tech.easyflow.agent.runtime;
|
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.AgentGenerationOptions;
|
||||||
import com.easyagents.agent.runtime.model.AgentHttpVersionPolicy;
|
import com.easyagents.agent.runtime.model.AgentHttpVersionPolicy;
|
||||||
import com.easyagents.agent.runtime.model.AgentModelSpec;
|
import com.easyagents.agent.runtime.model.AgentModelSpec;
|
||||||
@@ -65,6 +66,36 @@ public class AgentRuntimeCompilerModelConfigTest {
|
|||||||
Assert.assertEquals(AgentHttpVersionPolicy.AUTO, spec.getHttpVersionPolicy());
|
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);
|
method.setAccessible(true);
|
||||||
return (AgentModelSpec) method.invoke(compiler, agent);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,29 +106,6 @@ const emit = defineEmits<{ change: [] }>();
|
|||||||
/>
|
/>
|
||||||
</ElFormItem>
|
</ElFormItem>
|
||||||
<div class="agent-form__grid">
|
<div class="agent-form__grid">
|
||||||
<ElFormItem>
|
|
||||||
<template #label>
|
|
||||||
<span class="agent-form__label">
|
|
||||||
消息数触发阈值
|
|
||||||
<ElTooltip
|
|
||||||
content="工作记忆里的消息条数达到该值时,会触发上下文压缩;用户消息和智能体消息都会计入。"
|
|
||||||
effect="light"
|
|
||||||
placement="top"
|
|
||||||
>
|
|
||||||
<ElIcon class="agent-form__info" aria-label="消息数触发阈值说明">
|
|
||||||
<InfoFilled />
|
|
||||||
</ElIcon>
|
|
||||||
</ElTooltip>
|
|
||||||
</span>
|
|
||||||
</template>
|
|
||||||
<ElInputNumber
|
|
||||||
v-model="agent.memoryConfigJson!.compressionParameter.msgThreshold"
|
|
||||||
:min="1"
|
|
||||||
:max="100"
|
|
||||||
controls-position="right"
|
|
||||||
@change="emit('change')"
|
|
||||||
/>
|
|
||||||
</ElFormItem>
|
|
||||||
<ElFormItem>
|
<ElFormItem>
|
||||||
<template #label>
|
<template #label>
|
||||||
<span class="agent-form__label">
|
<span class="agent-form__label">
|
||||||
|
|||||||
Reference in New Issue
Block a user