Skip to content

fix: change rag&longtermmemory hook from prereasoning to pre call#156

Merged
AlbumenJ merged 1 commit intoagentscope-ai:mainfrom
shiyiyue1102:main-fix-rag-long-term-memory-hooks
Dec 8, 2025
Merged

fix: change rag&longtermmemory hook from prereasoning to pre call#156
AlbumenJ merged 1 commit intoagentscope-ai:mainfrom
shiyiyue1102:main-fix-rag-long-term-memory-hooks

Conversation

@shiyiyue1102
Copy link
Contributor

This pull request refactors the agent hook system to streamline how input messages are modified by hooks before agent processing, and improves the handling and formatting of retrieved long-term memory and RAG knowledge. The most impactful changes are the consolidation of pre-processing hooks to use PreCallEvent, enhanced message wrapping for memory and knowledge retrieval, and improved file writing robustness.

Agent Hook System Refactor

  • Unified pre-processing hooks to use PreCallEvent instead of PreReasoningEvent, allowing hooks to modify input messages sequentially and simplifying hook implementations (AgentBase.java, PreCallEvent.java, StaticLongTermMemoryHook.java, GenericRAGHook.java). [1] [2] [3] [4]
  • Updated tests to reflect the new PreCallEvent constructor and usage. [1] [2] [3]

Long-Term Memory and RAG Knowledge Formatting

  • Introduced a standardized wrap method in LongTermMemoryTools to clearly mark retrieved memory content in messages, and updated all relevant usages and tests to utilize this format. [1] [2] [3] [4] [5] [6] [7]
  • RAG knowledge context is now wrapped in <retrieved_knowledge> tags for clarity, and injected as a system message at the end of the input message list. [1] [2] [3]

File Tool Robustness

  • Enhanced file writing tool to automatically create parent directories if they do not exist before writing a new file.

Dependency Update

  • Updated maven-source-plugin.version from 3.3.0 to 3.3.1 in pom.xml.此提交为从长期记忆检索到的内容添加了一个包装方法,并相应地调整了相关的测试和依赖项。此外,还改进了PreCallEvent处理逻辑,使其更通用且适用于更多场景。

Change-Id: I7cfedf2df9c37e09899e94636d551ca48f53f6e2
Co-developed-by: Aone Copilot noreply@alibaba-inc.com

AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.1, 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

此提交为从长期记忆检索到的内容添加了一个包装方法,并相应地调整了相关的测试和依赖项。此外,还改进了`PreCallEvent`处理逻辑,使其更通用且适用于更多场景。

Change-Id: I7cfedf2df9c37e09899e94636d551ca48f53f6e2
Co-developed-by: Aone Copilot <noreply@alibaba-inc.com>
@codecov
Copy link

codecov bot commented Dec 8, 2025

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 refactors the agent hook system to enable sequential message modification through PreCallEvent instead of PreReasoningEvent, standardizes formatting for long-term memory and RAG knowledge retrieval, and improves file writing robustness.

  • Consolidated pre-processing hooks to use PreCallEvent, allowing hooks to modify input messages sequentially with each hook receiving the event modified by previous hooks
  • Added standardized wrap() method in LongTermMemoryTools to format retrieved memory content with XML tags and descriptive text
  • Enhanced RAG knowledge injection to wrap content in <retrieved_knowledge> tags and append as system message at the end of input messages

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
agentscope-core/src/main/java/io/agentscope/core/hook/PreCallEvent.java Added inputMessages field with getter/setter to enable message modification by hooks
agentscope-core/src/main/java/io/agentscope/core/agent/AgentBase.java Refactored notifyPreCall() to execute hooks sequentially, allowing each hook to modify messages
agentscope-core/src/main/java/io/agentscope/core/memory/LongTermMemoryTools.java Added wrap() method to standardize formatting of retrieved memory content
agentscope-core/src/main/java/io/agentscope/core/memory/StaticLongTermMemoryHook.java Updated to use PreCallEvent and append memory messages at end of input list
agentscope-core/src/main/java/io/agentscope/core/rag/GenericRAGHook.java Updated to use PreCallEvent, wrap knowledge in XML tags, and append at end of messages
agentscope-core/src/main/java/io/agentscope/core/tool/file/WriteFileTool.java Enhanced to create parent directories automatically when writing new files
agentscope-core/pom.xml Updated maven-source-plugin from 3.3.0 to 3.3.1
agentscope-core/src/test/java/io/agentscope/core/hook/HookEventTest.java Updated tests to use new PreCallEvent constructor with inputMessages parameter
agentscope-core/src/test/java/io/agentscope/core/memory/StaticLongTermMemoryHookTest.java Updated to use PreCallEvent and verify memory messages at end of list
agentscope-core/src/test/java/io/agentscope/core/memory/LongTermMemoryToolsTest.java Updated expectations to verify wrapped memory content format
agentscope-extensions/agentscope-extensions-rag-simple/src/test/java/io/agentscope/core/rag/hook/GenericRAGHookTest.java Updated to use PreCallEvent and verify knowledge messages at end of list
agentscope-extensions/agentscope-extensions-studio/src/test/java/io/agentscope/core/studio/StudioMessageHookTest.java Updated to use new PreCallEvent constructor

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

Comment on lines +82 to +83
/*
* Wraps the given text in a special format to indicate that it is retrieved from long-term memory.
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

This method is using block comment syntax (/* /) but should use Javadoc syntax (/* */) since it's a public static method that should be documented. Additionally, it should include @param and @return tags to properly document the method signature.

Suggested change
/*
* Wraps the given text in a special format to indicate that it is retrieved from long-term memory.
/**
* Wraps the given text in a special format to indicate that it is retrieved from long-term memory.
*
* @param text The text content retrieved from long-term memory to be wrapped.
* @return The input text wrapped in a long-term memory annotation block.

Copilot uses AI. Check for mistakes.
*/
private List<Msg> createEnhancedMessages(
List<Msg> originalMessages, List<Document> retrievedDocs) {
private Msg createEnhancedMessages(List<Document> retrievedDocs) {
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The method name createEnhancedMessages is misleading since it now returns a single Msg object instead of a List<Msg>. Consider renaming to createKnowledgeMessage or buildKnowledgeMessage to better reflect its current purpose.

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +58

public List<Msg> getInputMessages() {
return inputMessages;
}

Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

Missing javadoc for the getInputMessages() and setInputMessages() methods. These public methods should have proper documentation explaining their purpose, parameters, and return values.

Suggested change
public List<Msg> getInputMessages() {
return inputMessages;
}
/**
* Returns the list of input messages that will be processed by the agent.
*
* @return the list of input {@link Msg} objects
*/
public List<Msg> getInputMessages() {
return inputMessages;
}
/**
* Sets the list of input messages to be processed by the agent.
*
* @param inputMessages the new list of input {@link Msg} objects
*/

Copilot uses AI. Check for mistakes.
/**
* Constructor for PreCallEvent.
*
* @param agent The agent instance (must not be null)
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The constructor javadoc should document the new inputMessages parameter, including its purpose and any constraints (e.g., can it be null?).

Suggested change
* @param agent The agent instance (must not be null)
* @param agent The agent instance (must not be null)
* @param inputMessages The list of input messages for the agent. May be {@code null} if there are no input messages.

Copilot uses AI. Check for mistakes.
@@ -156,8 +156,8 @@ private Mono<PreReasoningEvent> handlePreReasoning(PreReasoningEvent event) {

// Inject memory message at the beginning
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The comment on line 157 says "Inject memory message at the beginning" but the code on line 160 uses enhancedMessages.add(memoryMsg) after adding all input messages, which places it at the end, not the beginning. The comment should be updated to say "Inject memory message at the end" or similar.

Suggested change
// Inject memory message at the beginning
// Inject memory message at the end

Copilot uses AI. Check for mistakes.
*/
public static String wrap(String text) {
return "Below is content retrieved from the long-term memory associated with the current"
+ " user, Please extract useful information from it in the context of the"
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

Punctuation issue: The comma before "Please" should be a period since "Please extract useful information..." is a separate sentence from "Below is content retrieved...".

Suggested change
+ " user, Please extract useful information from it in the context of the"
+ " user. Please extract useful information from it in the context of the"

Copilot uses AI. Check for mistakes.
@@ -198,26 +199,17 @@ private String extractQueryFromMessages(List<Msg> messages) {
*
* <p>The knowledge is injected as a system message at the beginning of the message list.
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The javadoc comment is outdated. It says "The knowledge is injected as a system message at the beginning of the message list" but the new implementation adds it at the end (line 166 uses enhancedMessages.add(enhancedMessage) after adding all input messages). The documentation should be updated to reflect that the knowledge message is appended at the end.

Suggested change
* <p>The knowledge is injected as a system message at the beginning of the message list.
* <p>The knowledge is injected as a system message at the end of the message list.

Copilot uses AI. Check for mistakes.
Comment on lines 202 to 203
* @param retrievedDocs the retrieved documents
* @return the enhanced message list with knowledge context
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The javadoc return description says "the enhanced message list with knowledge context" but the method now returns a single Msg object, not a list. The documentation should be updated to accurately describe the return value as a single message containing the knowledge context.

Copilot uses AI. Check for mistakes.
Comment on lines 44 to +50
/**
* Constructor for PreCallEvent.
*
* @param agent The agent instance (must not be null)
* @throws NullPointerException if agent is null
*/
public PreCallEvent(Agent agent) {
public PreCallEvent(Agent agent, List<Msg> inputMessages) {
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

The javadoc at line 25 states "Modifiable: No (notification-only)" but the class now has setInputMessages() method (line 59-61) that allows modification of input messages. The documentation should be updated to indicate that this event is modifiable via setInputMessages(), similar to PreReasoningEvent's documentation. The Context section should also be updated to include getInputMessages() and note that messages can be modified.

Copilot uses AI. Check for mistakes.
Comment on lines 306 to +308
@DisplayName("Should ignore non-PreReasoningEvent events")
void testIgnoreOtherEvents() {
PreCallEvent preCallEvent = new PreCallEvent(mockAgent);
PreCallEvent preCallEvent = new PreCallEvent(mockAgent, List.of());
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

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

This test is checking that the hook ignores non-PreCallEvent events, but it's actually testing with a PreCallEvent, which the hook should handle (not ignore). The test should use a different event type (e.g., PostCallEvent or PreReasoningEvent) to properly validate that other event types are ignored. Additionally, the display name should be updated to say "non-PreCallEvent" instead of "non-PreReasoningEvent".

Copilot uses AI. Check for mistakes.
@AlbumenJ AlbumenJ changed the title change rag&longtermmemory hook from prereasoning to pre call fix: change rag&longtermmemory hook from prereasoning to pre call Dec 8, 2025
@AlbumenJ AlbumenJ merged commit 7e5f528 into agentscope-ai:main Dec 8, 2025
9 of 10 checks passed
JGoP-L pushed a commit to JGoP-L/agentscope-java that referenced this pull request Dec 29, 2025
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