agentscope-ai / agentscope-ai/agentscope-java

[Feature]:Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents

Aberta
#1,679 0 comentários 0 reações 0 responsáveis Ver no GitHub
area/core/agent area/harness enhancement
Linguagem predominante
Java
Estrelas
5.6k
Forks
1.3k
Merge médio
4d 12h
PRs com merge (30d)
77

Descrição

# [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 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`.

```java
// 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 instance
```

This 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 type)` (and the keyed variant `get(String key, Class type)`) to support **polymorphic / assignable type lookup**:

1. First attempt an exact type match (preserving current behavior and performance).
2. 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)`).
3. The same logic should apply to the `contains()` methods for consistency.

```java
// 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 true
```

If 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**

1. **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.
2. **Type-erased Object retrieval**: Store and retrieve as `Object`, then cast manually in each tool. This defeats the type-safety that `ContextStore` provides.
3. **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**

- `ContextStore` is currently marked `@Deprecated` in favor of `io.agentscope.core.agent.RuntimeContext`. If `RuntimeContext` provides 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 `DefaultContextStore` uses a `Map, Map>` 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.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.