Skip to content

feat(autocontext): support custom context compress prompt#386

Merged
AlbumenJ merged 4 commits intoagentscope-ai:mainfrom
shiyiyue1102:main-autocontext-support-custom-prompt
Dec 30, 2025
Merged

feat(autocontext): support custom context compress prompt#386
AlbumenJ merged 4 commits intoagentscope-ai:mainfrom
shiyiyue1102:main-autocontext-support-custom-prompt

Conversation

@shiyiyue1102
Copy link
Contributor

This pull request introduces support for customizable context compression prompts in AutoContextMemory, allowing users to fine-tune how memory compression operates for specific domains and scenarios. The documentation in both English and Chinese has been updated to reflect these changes, and the integration requirements for AutoContextHook have been relaxed from "required" to "recommended" for easier adoption and flexibility.

Custom Prompt Support & Documentation Updates

  • Added a new customPrompt field to AutoContextConfig and corresponding getter, enabling users to specify a PromptConfig for domain-specific context compression prompts. [1] [2]
  • Updated both English and Chinese documentation (README.md, README_zh.md) to describe the new customizable prompt feature, including usage examples and domain-specific prompt templates (e.g., e-commerce order processing). [1] [2] [3] [4] [5] [6]
  • Added customPrompt as a documented configuration parameter in the configuration tables. [1] [2]

Integration & Usage Guidance Improvements

  • Changed the guidance for AutoContextHook from "must use" to "recommended," clarifying that it is no longer strictly required but still best practice for automatic setup. This is reflected in both documentation and code comments. [1] [2] [3] [4] [5] [6]

Other Documentation Refinements

  • Removed references to tool call preservation as a feature in the summary lists, focusing instead on the new prompt customization capability. [1] [2]
  • Clarified compression strategy explanations and separated character count requirements for prompts in the Chinese documentation. [1] [2]
  • Minor corrections and clarifications in the "Best Practices" and "Notes" sections for both languages. [1] [2] [3]

These changes make AutoContextMemory more flexible and adaptable to real-world business needs, while improving clarity and usability for developers integrating it into their agents.## AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.4, check your pom.xml dependency version or run mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]

Description

[Please describe the background, purpose, changes made, and how to test this PR]

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Change-Id: I245703e09158de869fb1fa297d8f7cba1fbb2af6
Change-Id: I604015fed79efef852979b523e033928e7ad4f5e
@codecov
Copy link

codecov bot commented Dec 29, 2025

Codecov Report

❌ Patch coverage is 98.43750% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tscope/core/memory/autocontext/PromptProvider.java 95.23% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for customizable context compression prompts in AutoContextMemory, enabling users to tailor compression behavior for specific domains and scenarios. The implementation includes new configuration classes, comprehensive test coverage, and updated documentation in both English and Chinese.

Key changes:

  • Introduced PromptConfig and PromptProvider classes to support custom compression prompts with fallback to defaults
  • Added customPrompt field to AutoContextConfig for optional prompt customization
  • Updated documentation with detailed usage examples and domain-specific prompt templates
  • Relaxed AutoContextHook requirement from "must use" to "recommended" for better flexibility

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
PromptConfig.java New configuration class using builder pattern for custom compression prompts
PromptProvider.java New utility class providing prompt selection with automatic fallback to defaults
AutoContextConfig.java Added customPrompt field and getter with builder support
AutoContextMemory.java Integrated custom prompts via PromptProvider in compression strategies
Prompts.java Separated character count requirement from main compression prompt for Strategy 6
PromptConfigTest.java Comprehensive unit tests for PromptConfig builder pattern and getters
PromptProviderTest.java Unit tests for PromptProvider fallback logic with various scenarios
AutoContextConfigTest.java Tests for customPrompt integration in AutoContextConfig
AutoContextMemoryTest.java Integration tests verifying custom prompt usage in memory compression
README.md Added custom prompt documentation with e-commerce domain example
README_zh.md Chinese version of custom prompt documentation
AutoMemoryExample.java Removed StudioManager initialization (unrelated cleanup)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 71 to 73
public PromptConfig build() {
return config;
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Javadoc for public method build(). All public methods must have Javadoc according to coding guidelines.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines 81 to 95
public String getPreviousRoundToolCompressPrompt() {
return previousRoundToolCompressPrompt;
}

public String getPreviousRoundSummaryPrompt() {
return previousRoundSummaryPrompt;
}

public String getCurrentRoundLargeMessagePrompt() {
return currentRoundLargeMessagePrompt;
}

public String getCurrentRoundCompressPrompt() {
return currentRoundCompressPrompt;
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Javadoc for public getter methods. All public methods must have Javadoc according to coding guidelines.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +76 to +78
public static Builder builder() {
return new Builder();
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Javadoc for public method builder(). All public methods must have Javadoc according to coding guidelines.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +179 to +190
@Test
@DisplayName("Should handle empty string prompts")
void testEmptyStringPrompts() {
PromptConfig config =
PromptConfig.builder()
.previousRoundToolCompressPrompt("")
.previousRoundSummaryPrompt("")
.build();

assertEquals("", config.getPreviousRoundToolCompressPrompt());
assertEquals("", config.getPreviousRoundSummaryPrompt());
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test for empty string prompts may expose unexpected behavior. When a user sets an empty string as a custom prompt, it will be used instead of falling back to the default. This might not be the intended behavior - typically empty or blank strings should be treated the same as null and fall back to defaults. Consider validating input or treating empty/blank strings as null in PromptProvider methods.

Copilot uses AI. Check for mistakes.
Change-Id: I01b44216b908d5ebf506e6cd41ea7dca425cf3c6
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 79 to 94
// Getters (return null to use default value)
public String getPreviousRoundToolCompressPrompt() {
return previousRoundToolCompressPrompt;
}

public String getPreviousRoundSummaryPrompt() {
return previousRoundSummaryPrompt;
}

public String getCurrentRoundLargeMessagePrompt() {
return currentRoundLargeMessagePrompt;
}

public String getCurrentRoundCompressPrompt() {
return currentRoundCompressPrompt;
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public getter methods (getPreviousRoundToolCompressPrompt, getPreviousRoundSummaryPrompt, getCurrentRoundLargeMessagePrompt, getCurrentRoundCompressPrompt) are missing Javadoc documentation. According to the coding guidelines, all public methods must have Javadoc comments. The inline comment "return null to use default value" should be incorporated into proper Javadoc @return tags.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines 44 to 91
private PromptConfig config = new PromptConfig();

/** Strategy 1: Sets prompt for compressing previous round tool invocations */
public Builder previousRoundToolCompressPrompt(String prompt) {
config.previousRoundToolCompressPrompt = prompt;
return this;
}

/** Strategy 4: Sets prompt for summarizing previous round conversations */
public Builder previousRoundSummaryPrompt(String prompt) {
config.previousRoundSummaryPrompt = prompt;
return this;
}

/** Strategy 5: Sets prompt for summarizing current round large messages */
public Builder currentRoundLargeMessagePrompt(String prompt) {
config.currentRoundLargeMessagePrompt = prompt;
return this;
}

/** Strategy 6: Sets prompt for compressing current round messages (supports format: %d, %d, %.0f, %.0f) */
public Builder currentRoundCompressPrompt(String prompt) {
config.currentRoundCompressPrompt = prompt;
return this;
}

public PromptConfig build() {
return config;
}
}

public static Builder builder() {
return new Builder();
}

// Getters (return null to use default value)
public String getPreviousRoundToolCompressPrompt() {
return previousRoundToolCompressPrompt;
}

public String getPreviousRoundSummaryPrompt() {
return previousRoundSummaryPrompt;
}

public String getCurrentRoundLargeMessagePrompt() {
return currentRoundLargeMessagePrompt;
}

Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Builder pattern implementation has a critical thread-safety and reusability issue. The builder stores a single mutable PromptConfig instance and modifies it across multiple method calls. This creates two problems:

  1. If a Builder instance is reused to build multiple PromptConfig objects, all built instances will reference the same underlying object, causing unintended shared state.

  2. The builder is not thread-safe, as multiple threads could access and modify the same config instance.

The builder should create a new PromptConfig instance in the build() method or use a defensive copy approach. For example, the build() method should transfer values from builder fields to a new PromptConfig instance.

Suggested change
private PromptConfig config = new PromptConfig();
/** Strategy 1: Sets prompt for compressing previous round tool invocations */
public Builder previousRoundToolCompressPrompt(String prompt) {
config.previousRoundToolCompressPrompt = prompt;
return this;
}
/** Strategy 4: Sets prompt for summarizing previous round conversations */
public Builder previousRoundSummaryPrompt(String prompt) {
config.previousRoundSummaryPrompt = prompt;
return this;
}
/** Strategy 5: Sets prompt for summarizing current round large messages */
public Builder currentRoundLargeMessagePrompt(String prompt) {
config.currentRoundLargeMessagePrompt = prompt;
return this;
}
/** Strategy 6: Sets prompt for compressing current round messages (supports format: %d, %d, %.0f, %.0f) */
public Builder currentRoundCompressPrompt(String prompt) {
config.currentRoundCompressPrompt = prompt;
return this;
}
public PromptConfig build() {
return config;
}
}
public static Builder builder() {
return new Builder();
}
// Getters (return null to use default value)
public String getPreviousRoundToolCompressPrompt() {
return previousRoundToolCompressPrompt;
}
public String getPreviousRoundSummaryPrompt() {
return previousRoundSummaryPrompt;
}
public String getCurrentRoundLargeMessagePrompt() {
return currentRoundLargeMessagePrompt;
}
private String previousRoundToolCompressPrompt;
private String previousRoundSummaryPrompt;
private String currentRoundLargeMessagePrompt;
private String currentRoundCompressPrompt;
/** Strategy 1: Sets prompt for compressing previous round tool invocations */
public Builder previousRoundToolCompressPrompt(String prompt) {
this.previousRoundToolCompressPrompt = prompt;
return this;
}
/** Strategy 4: Sets prompt for summarizing previous round conversations */
public Builder previousRoundSummaryPrompt(String prompt) {
this.previousRoundSummaryPrompt = prompt;
return this;
}
/** Strategy 5: Sets prompt for summarizing current round large messages */
public Builder currentRoundLargeMessagePrompt(String prompt) {
this.currentRoundLargeMessagePrompt = prompt;
return this;
}
/**
* Strategy 6: Sets prompt for compressing current round messages
* (supports format: %d, %d, %.0f, %.0f).
*
* @param prompt the prompt template for compressing current round messages
* @return this builder instance
*/
public Builder currentRoundCompressPrompt(String prompt) {
this.currentRoundCompressPrompt = prompt;
return this;
}
/**
* Builds a new {@link PromptConfig} instance using the values configured on this builder.
*
* @return a new {@link PromptConfig} instance
*/
public PromptConfig build() {
PromptConfig config = new PromptConfig();
config.previousRoundToolCompressPrompt = this.previousRoundToolCompressPrompt;
config.previousRoundSummaryPrompt = this.previousRoundSummaryPrompt;
config.currentRoundLargeMessagePrompt = this.currentRoundLargeMessagePrompt;
config.currentRoundCompressPrompt = this.currentRoundCompressPrompt;
return config;
}
}
/**
* Creates a new {@link Builder} for configuring {@link PromptConfig} instances.
*
* @return a new {@link Builder} instance
*/
public static Builder builder() {
return new Builder();
}
// Getters (return null to use default value)
/**
* Gets the prompt for compressing previous round tool invocations.
*
* @return the configured prompt, or {@code null} to use the default value
*/
public String getPreviousRoundToolCompressPrompt() {
return previousRoundToolCompressPrompt;
}
/**
* Gets the prompt for summarizing previous round conversations.
*
* @return the configured prompt, or {@code null} to use the default value
*/
public String getPreviousRoundSummaryPrompt() {
return previousRoundSummaryPrompt;
}
/**
* Gets the prompt for summarizing current round large messages.
*
* @return the configured prompt, or {@code null} to use the default value
*/
public String getCurrentRoundLargeMessagePrompt() {
return currentRoundLargeMessagePrompt;
}
/**
* Gets the prompt for compressing current round messages.
*
* @return the configured prompt, or {@code null} to use the default value
*/

Copilot uses AI. Check for mistakes.
* default prompts from {@link Prompts} when custom prompts are not provided.
*/
public class PromptProvider {

Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PromptProvider is a utility class with only static methods but lacks a private constructor. According to Java best practices, utility classes should have a private constructor to prevent instantiation. This makes the intent clearer and prevents accidental instantiation.

Suggested change
private PromptProvider() {
}

Copilot uses AI. Check for mistakes.
Comment on lines 37 to 38
/** Strategy 6: Prompt for compressing current round messages (supports format: %d, %d, %.0f, %.0f) */
private String currentRoundCompressPrompt;
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions format support "supports format: %d, %d, %.0f, %.0f" which appears to be incorrect. Based on the Prompts.java changes, the character count requirement has been separated into CURRENT_ROUND_MESSAGE_COMPRESS_CHAR_REQUIREMENT, and the currentRoundCompressPrompt itself no longer contains format placeholders. The comment should be updated to reflect that this prompt does not include character count requirements, which are handled separately.

Copilot uses AI. Check for mistakes.
Comment on lines 64 to 67
/** Strategy 6: Sets prompt for compressing current round messages (supports format: %d, %d, %.0f, %.0f) */
public Builder currentRoundCompressPrompt(String prompt) {
config.currentRoundCompressPrompt = prompt;
return this;
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions format support "supports format: %d, %d, %.0f, %.0f" which appears to be incorrect. Based on the Prompts.java changes, the character count requirement has been separated into CURRENT_ROUND_MESSAGE_COMPRESS_CHAR_REQUIREMENT, and the currentRoundCompressPrompt itself no longer contains format placeholders. The comment should be updated to reflect that this prompt does not include character count requirements, which are handled separately.

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +77
public static Builder builder() {
return new Builder();
}
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public static builder() method is missing Javadoc documentation. According to the coding guidelines, all public methods must have Javadoc comments that explain their purpose, parameters, and return values.

Copilot generated this review using guidance from repository custom instructions.
Change-Id: Ibb987e2bdaf6347466526cc66e284e0954aff20c
@AlbumenJ AlbumenJ merged commit 6ded745 into agentscope-ai:main Dec 30, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants