Repository navigation
Description
[Feature]: Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents
Is your feature request related to a problem? Please describe.
When building multiple agents that each carry their own context type (e.g., AgentAContext extends BaseContext, AgentBContext extends BaseContext), we want to reuse the same tool class across all these agents. The tool should be able to retrieve the context via a shared parent type (e.g., BaseContext.class), without knowing the concrete agent context type.
Currently, ContextStore.get(Class<T> type) performs exact class matching against the internal Class → (Key → Object) map. If an agent registers an AgentAContext instance, calling store.get(BaseContext.class) returns null, even though AgentAContext extends BaseContext.
// Agent A setup
store.register("ctx", new AgentAContext()); // stored under AgentAContext.class
// Inside a reusable tool
BaseContext ctx = store.get(BaseContext.class); // returns null — expected a child instanceThis forces us to either write separate tool implementations per agent context type, or resort to fragile workarounds, both of which defeat the purpose of having a generic, reusable tool layer.
Describe the solution you'd like
Enhance ContextStore.get(Class<T> type) (and the keyed variant get(String key, Class<T> type)) to support polymorphic / assignable type lookup:
- First attempt an exact type match (preserving current behavior and performance).
- If no exact match is found, iterate over stored entries and return the first object whose type is assignable to the requested type (i.e.,
requestedType.isAssignableFrom(storedType)). - The same logic should apply to the
contains()methods for consistency.
// After the enhancement
store.register("ctx", new AgentAContext()); // AgentAContext extends BaseContext
BaseContext ctx = store.get(BaseContext.class); // returns the AgentAContext instance
boolean exists = store.contains(BaseContext.class); // returns trueIf multiple stored types match the requested parent type, implementations could either return the first match or throw an ambiguity exception — either approach is acceptable as long as the behavior is documented.
Describe alternatives you've considered
- Register under a common key/type: All agents explicitly register their context under
BaseContext.class. This works but requires manual discipline and breaks if a developer forgets to convert. - Type-erased Object retrieval: Store and retrieve as
Object, then cast manually in each tool. This defeats the type-safety thatContextStoreprovides. - Separate tool per agent context type: Write
AgentATool,AgentBTool, etc., each referencing its own context type. This leads to massive code duplication and is not maintainable.
Additional context
ContextStoreis currently marked@Deprecatedin favor ofio.agentscope.core.agent.RuntimeContext. IfRuntimeContextprovides a similar type-based retrieval API, the same polymorphic lookup capability should be considered there as well, since this is a fundamental need for any context-aware tool reuse pattern.- The current
DefaultContextStoreuses aMap<Class<?>, Map<String, Object>>structure. Adding assignable-type fallback only requires iterating over the outer map keys when the exact lookup misses, which has minimal performance impact given the typically small number of stored context types.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn progress
Activity
[-][Feature]:[/-][+][Feature]:Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents[/+]