Skip to content

Add context-aware agent factory with request input support #1359#1360

Open
XiangShuUncle wants to merge 6 commits into
agentscope-ai:mainfrom
XiangShuUncle:main
Open

Add context-aware agent factory with request input support #1359#1360
XiangShuUncle wants to merge 6 commits into
agentscope-ai:mainfrom
XiangShuUncle:main

Conversation

@XiangShuUncle
Copy link
Copy Markdown

#1359

AgentScope-Java Version

[1.0.13-SNAPSHOT]

Description

AG-UI Context-Aware Agent Feature

This change introduces context-aware agent creation in the AG-UI extension, allowing agents to be dynamically configured based on request context.

Key Changes:

  1. New Interface InputContextualAgentFactory: A functional interface that accepts RunAgentInput to create agents with full request context (thread ID, forwarded properties, tools, etc.)

  2. Enhanced AguiAgentRegistry:

    • Added inputContextualFactories map to store context-aware factories
    • New method registerFactoryWithInput() to register factories that receive request context
    • Updated getAgent() to support contextual agent creation with input parameter
    • Modified hasAgent(), unregister(), and size() to handle contextual factories
  3. Extended AgentResolver Interface:

    • Added overloaded resolveAgent() method that accepts RunAgentInput
    • Maintains backward compatibility with default implementation
  4. Updated AguiRequestProcessor:

    • Now passes the full RunAgentInput to the agent resolver for context-aware resolution
  5. Example Implementation (agentscope-examples/agui):

    • Added UserContext class with Lombok to hold user and context information
    • Created createAgentWithInput() method demonstrating context-aware agent creation
    • Registered new endpoint /agui/run/context with forwarded props (context, user)
    • Enhanced frontend with tabbed interface showing both standard and context-aware chat
    • Added tool getCurrentContext() to demonstrate context access within tools
  6. Dependencies:

    • Added Lombok dependency to the agui example project

Use Case: This enables scenarios where agents need dynamic configuration based on user context, session data, or request-specific parameters without creating separate agent instances for each variation.


AG-UI 上下文感知 Agent 功能

此更改在 AG-UI 扩展中引入了上下文感知的 agent 创建功能,允许根据请求上下文动态配置 agent。

主要更改:

  1. 新增接口 InputContextualAgentFactory:一个函数式接口,接受 RunAgentInput 来创建具有完整请求上下文(线程 ID、转发属性、工具等)的 agent

  2. 增强的 AguiAgentRegistry

    • 添加 inputContextualFactories 映射来存储上下文感知工厂
    • 新增 registerFactoryWithInput() 方法注册接收请求上下文的工厂
    • 更新 getAgent() 方法支持带输入参数的上下文感知 agent 创建
    • 修改 hasAgent()unregister()size() 以处理上下文工厂
  3. 扩展的 AgentResolver 接口

    • 添加接受 RunAgentInput 的重载 resolveAgent() 方法
    • 通过默认实现保持向后兼容性
  4. 更新的 AguiRequestProcessor

    • 现在将完整的 RunAgentInput 传递给 agent 解析器以实现上下文感知解析
  5. 示例实现 (agentscope-examples/agui):

    • 添加使用 Lombok 的 UserContext 类来保存用户和上下文信息
    • 创建 createAgentWithInput() 方法演示上下文感知的 agent 创建
    • 注册新的端点 /agui/run/context 并转发属性(context、user)
    • 增强前端界面,添加标签页展示标准聊天和上下文感知聊天
    • 添加 getCurrentContext() 工具演示在工具中访问上下文
  6. 依赖项

    • 为 agui 示例项目添加 Lombok 依赖

应用场景:这使得 agent 能够基于用户上下文、会话数据或请求特定参数进行动态配置,而无需为每个变体创建单独的 agent 实例。

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

Gogs and others added 2 commits May 8, 2026 16:34
- Introduce InputContextualAgentFactory interface for dynamic agent creation
- Extend AguiAgentRegistry to support context-aware factory registration
- Add overloaded resolveAgent method in AgentResolver with RunAgentInput
- Update AguiRequestProcessor to pass full request context to resolver
- Add example implementation with UserContext and tabbed UI demo
- Register new endpoint /agui/run/context with forwarded properties
- Add Lombok dependency for agui example project

This enables agents to be dynamically configured based on request context
including thread ID, forwarded properties, tools, and runtime parameters.

feat(agui): 添加支持请求输入的上下文感知 agent 工厂

- 引入 InputContextualAgentFactory 接口用于动态 agent 创建
- 扩展 AguiAgentRegistry 以支持上下文感知工厂注册
- 在 AgentResolver 中添加带 RunAgentInput 的重载 resolveAgent 方法
- 更新 AguiRequestProcessor 将完整请求上下文传递给解析器
- 添加包含 UserContext 和标签页 UI 演示的示例实现
- 注册带有转发属性的新端点 /agui/run/context
- 为 agui 示例项目添加 Lombok 依赖

这使得 agent 能够基于请求上下文(包括线程 ID、转发属性、工具和运行时参数)进行动态配置。
@XiangShuUncle XiangShuUncle requested a review from a team May 8, 2026 09:05
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 8, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ XiangShuUncle
❌ Gogs


Gogs 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.

- 在 ExampleTools.java 中格式化 getCurrentContext 方法的返回语句
- 在 InputContextualAgentFactory.java 中添加 Apache 许可证版权声明
- 在 UserContext.java 中添加 Apache 许可证版权声明
- 统一代码格式和缩进规范

Signed-off-by: XiangShuUncle <37469567+XiangShuUncle@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

…registration

- Introduce ToolGroup in AgentConfiguration and implement tool schema registration to tool groups
- Add context-aware factory support to AguiAgentRegistry
- Implement registerFactoryWithInput method for creating agents with input parameters
- Add ContextAwareFactoryTests suite to verify context-aware factory behavior
- Support passing threadId, runId, and forwarded properties to factories via RunAgentInput
- Implement priority handling logic between context-aware factories and regular factories
- Add comprehensive test coverage for null inputs, edge cases, and resource cleanup

feat(agui): 添加上下文感知工厂支持并完善工具组注册

- 在 AgentConfiguration 中引入 ToolGroup 并实现工具模式注册到工具组
- 为 AguiAgentRegistry 添加 context-aware factory 功能
- 实现 registerFactoryWithInput 方法支持输入参数传递
- 添加完整的单元测试覆盖上下文感知工厂的各种场景
- 确保上下文感知工厂与普通工厂的优先级处理
- 实现上下文感知工厂的属性转发功能
- 完善工厂注销和计数逻辑以支持新的工厂类型
@XiangShuUncle
Copy link
Copy Markdown
Author

CLA助理检查 感谢你的投稿!我们非常感激。像许多开源项目一样,我们要求大家先签署贡献者许可协议,我们才能接受你的贡献。每名提交者中就有一位签署了CLA协议。✅ 香叔❌叔戈格斯

Gogs 似乎不是 GitHub 用户。你需要一个GitHub账号才能签署CLA。如果您已有GitHub账户,请将此次承诺使用的电子邮件地址添加到您的账户中。
你已经签署了CLA但状态还在待处理?我们再检查一下。

Gogs is a temporary git username I used, and it has no meaning. Please ignore it.

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