-
Notifications
You must be signed in to change notification settings - Fork 793
fix: port auto context memory to v2 #1682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guslegend0510
wants to merge
4
commits into
agentscope-ai:main
Choose a base branch
from
guslegend0510:fix/issue-1447-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c3c542
fix: port auto context memory to v2
guslegend 0323696
fix: sync autocontext module into distribution poms
guslegend 5197bb2
fix(autocontext): address review feedback on compression flow
guslegend f8e8394
test(autocontext): improve patch coverage for review fixes
guslegend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ope-extensions/agentscope-extensions-mem/agentscope-extensions-autocontext-memory/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Copyright 2024-2026 the original author or authors. | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
|
|
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.agentscope</groupId> | ||
| <artifactId>agentscope-extensions-mem</artifactId> | ||
| <version>${revision}</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>agentscope-extensions-autocontext-memory</artifactId> | ||
| <name>AgentScope Java - Extensions - AutoContext Memory</name> | ||
| <description>Auto context compression and offload for AgentScope Java</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.agentscope</groupId> | ||
| <artifactId>agentscope-core</artifactId> | ||
| <scope>provided</scope> | ||
| <optional>true</optional> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
181 changes: 181 additions & 0 deletions
181
...context-memory/src/main/java/io/agentscope/core/memory/autocontext/AutoContextConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.agentscope.core.memory.autocontext; | ||
|
|
||
| /** Configuration for auto context compression. */ | ||
| public class AutoContextConfig { | ||
|
|
||
| long largePayloadThreshold = 5 * 1024; | ||
| long maxToken = 128 * 1024; | ||
| double tokenRatio = 0.75; | ||
| int offloadSinglePreview = 200; | ||
| int msgThreshold = 100; | ||
| int lastKeep = 50; | ||
| int minConsecutiveToolMessages = 6; | ||
| double currentRoundCompressionRatio = 0.3; | ||
| int minCompressionTokenThreshold = 5000; | ||
| private PromptConfig customPrompt; | ||
|
|
||
| public long getLargePayloadThreshold() { | ||
| return largePayloadThreshold; | ||
| } | ||
|
|
||
| public long getMaxToken() { | ||
| return maxToken; | ||
| } | ||
|
|
||
| public double getTokenRatio() { | ||
| return tokenRatio; | ||
| } | ||
|
|
||
| public int getOffloadSinglePreview() { | ||
| return offloadSinglePreview; | ||
| } | ||
|
|
||
| public int getMsgThreshold() { | ||
| return msgThreshold; | ||
| } | ||
|
|
||
| public int getLastKeep() { | ||
| return lastKeep; | ||
| } | ||
|
|
||
| public int getMinConsecutiveToolMessages() { | ||
| return minConsecutiveToolMessages; | ||
| } | ||
|
|
||
| public double getCurrentRoundCompressionRatio() { | ||
| return currentRoundCompressionRatio; | ||
| } | ||
|
|
||
| public int getMinCompressionTokenThreshold() { | ||
| return minCompressionTokenThreshold; | ||
| } | ||
|
|
||
| public PromptConfig getCustomPrompt() { | ||
| return customPrompt; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final AutoContextConfig config = new AutoContextConfig(); | ||
|
|
||
| public Builder largePayloadThreshold(long largePayloadThreshold) { | ||
| config.largePayloadThreshold = largePayloadThreshold; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder maxToken(long maxToken) { | ||
| config.maxToken = maxToken; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder tokenRatio(double tokenRatio) { | ||
| config.tokenRatio = tokenRatio; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder offloadSinglePreview(int offloadSinglePreview) { | ||
| config.offloadSinglePreview = offloadSinglePreview; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder msgThreshold(int msgThreshold) { | ||
| config.msgThreshold = msgThreshold; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder lastKeep(int lastKeep) { | ||
| config.lastKeep = lastKeep; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder minConsecutiveToolMessages(int minConsecutiveToolMessages) { | ||
| config.minConsecutiveToolMessages = minConsecutiveToolMessages; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder currentRoundCompressionRatio(double currentRoundCompressionRatio) { | ||
| config.currentRoundCompressionRatio = currentRoundCompressionRatio; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder minCompressionTokenThreshold(int minCompressionTokenThreshold) { | ||
| config.minCompressionTokenThreshold = minCompressionTokenThreshold; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder customPrompt(PromptConfig customPrompt) { | ||
| config.customPrompt = customPrompt; | ||
| return this; | ||
| } | ||
|
|
||
| public AutoContextConfig build() { | ||
| validate(); | ||
| AutoContextConfig result = new AutoContextConfig(); | ||
| result.largePayloadThreshold = config.largePayloadThreshold; | ||
| result.maxToken = config.maxToken; | ||
| result.tokenRatio = config.tokenRatio; | ||
| result.offloadSinglePreview = config.offloadSinglePreview; | ||
| result.msgThreshold = config.msgThreshold; | ||
| result.lastKeep = config.lastKeep; | ||
| result.minConsecutiveToolMessages = config.minConsecutiveToolMessages; | ||
| result.currentRoundCompressionRatio = config.currentRoundCompressionRatio; | ||
| result.minCompressionTokenThreshold = config.minCompressionTokenThreshold; | ||
| result.customPrompt = config.customPrompt; | ||
| return result; | ||
| } | ||
|
|
||
| private void validate() { | ||
| requirePositive(config.largePayloadThreshold, "largePayloadThreshold"); | ||
| requirePositive(config.maxToken, "maxToken"); | ||
| requireRatio(config.tokenRatio, "tokenRatio"); | ||
| requireNonNegative(config.offloadSinglePreview, "offloadSinglePreview"); | ||
| requirePositive(config.msgThreshold, "msgThreshold"); | ||
| requireNonNegative(config.lastKeep, "lastKeep"); | ||
| requirePositive(config.minConsecutiveToolMessages, "minConsecutiveToolMessages"); | ||
| requireRatio(config.currentRoundCompressionRatio, "currentRoundCompressionRatio"); | ||
| requirePositive(config.minCompressionTokenThreshold, "minCompressionTokenThreshold"); | ||
| } | ||
|
|
||
| private void requirePositive(long value, String fieldName) { | ||
| if (value <= 0) { | ||
| throw new IllegalArgumentException(fieldName + " must be positive"); | ||
| } | ||
| } | ||
|
|
||
| private void requirePositive(int value, String fieldName) { | ||
| if (value <= 0) { | ||
| throw new IllegalArgumentException(fieldName + " must be positive"); | ||
| } | ||
| } | ||
|
|
||
| private void requireNonNegative(int value, String fieldName) { | ||
| if (value < 0) { | ||
| throw new IllegalArgumentException(fieldName + " must be non-negative"); | ||
| } | ||
| } | ||
|
|
||
| private void requireRatio(double value, String fieldName) { | ||
| if (value <= 0.0 || value > 1.0) { | ||
| throw new IllegalArgumentException(fieldName + " must be in (0.0, 1.0]"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.