feat(core): add global context map to Agent interface#1207
Open
KFCFans wants to merge 3 commits intoagentscope-ai:mainfrom
Open
feat(core): add global context map to Agent interface#1207KFCFans wants to merge 3 commits intoagentscope-ai:mainfrom
KFCFans wants to merge 3 commits intoagentscope-ai:mainfrom
Conversation
Provide a mutable Map<String, Object> context on the Agent interface, allowing hooks and other components to share custom data throughout the agent execution lifecycle. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
朱八 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Add the missing getContext() implementation to the TestAgent stub in JsonlTraceExporterTest, required after the new method was added to the Agent interface. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Verify that the context map is non-null, initially empty, supports put/get, and is isolated per agent instance. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



AgentScope-Java Version
1.0.11
Description
Background
When building complex agent applications with hooks, there is currently no built-in mechanism for hooks and other components to share custom data throughout the agent execution lifecycle. For example, a
PreReasoningEventhook might need to pass contextual information (e.g., request metadata, tracing context, feature flags) to aPostActingEventhook, but there is no standard way to do so without resorting to external state management likeThreadLocalor static maps, which are fragile in reactive/async environments.Changes
This PR adds a
getContext()method to theAgentinterface, providing a mutableMap<String, Object>that is scoped to each agent instance. This allows hooks and other components to share custom data across the entire agent execution lifecycle in a thread-safe manner.Specific changes:
Agent.java: AddedMap<String, Object> getContext()method to the interface with Javadoc.AgentBase.java: Added aConcurrentHashMap-backedcontextfield, initialized in the constructor. ThegetContext()method is declaredfinalto ensure consistent behavior across all agent implementations.Usage Example
Design Decisions
ConcurrentHashMap: Chosen for thread safety since hooks may execute on different reactor threads.finalmethod: Prevents subclasses from overriding the context behavior, ensuring a single consistent implementation.Checklist
mvn spotless:applymvn test)