agentscope-ai / agentscope-ai/agentscope-java

[Question] Recommended way to pass server-side userId when using AG-UI starter with HarnessAgent?

Đang mở
#2,014 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
area/core/agent question
Ngôn ngữ chính
Java
Star
5.6k
Fork
1.3k
Merge trung bình
4 ngày 12 giờ
Pull request đã merge (30 ngày)
77

Mô tả

## Context

We are building a multi-user platform with:

- **Backend**: Spring Boot + Spring Security (session/cookie auth)
- **Agent**: `HarnessAgent` (singleton, stateless engine with `AgentStateStore`)
- **Frontend**: CopilotKit → AG-UI protocol
- **Integration**: `agentscope-agui-spring-boot-starter` (`AguiRestController` / `AguiAgentAdapter`)

Per the [Context & AgentState docs](https://java.agentscope.io/v2/en/docs/building-blocks/context.html), `(userId, sessionId)` should be passed via `RuntimeContext` on each call. The [Quickstart](https://java.agentscope.io/v2/en/docs/quickstart.html) also shows `userId` / `sessionId` being set in the HTTP handler.

However, the AG-UI integration path seems different from the `HarnessGateway` / builder example (`ChatController` → `MsgContext.userId` → `RuntimeContext`).

## What we observed

1. **RC4 (`AguiAgentAdapter`)**: calls `agent.stream(msgs, options)` without `RuntimeContext`, so `threadId` is not mapped to `sessionId`.
2. **main (after #1895)**: `buildRuntimeContext()` sets `sessionId = threadId` and puts `RunAgentInput` into context — but still **no `userId`**.
3. Without `userId`, `ReActAgent` falls back to `__anon__/{sessionId}` slot keys, which breaks multi-user isolation when multiple logged-in users share the same AG-UI `threadId` namespace.

We understand `userId` should come from the **integration layer** (server auth), not from the frontend `forwardedProps` (untrusted).

## Questions

1. Is there an **official recommended pattern** for passing **server-side authenticated `userId`** into `RuntimeContext` when using the **AG-UI Spring Boot starter** with **HarnessAgent**?
2. Should we wait for / contribute to the hook mechanism proposed in #901 (`AguiRequestHook` + `forwardedProps`), or is there a better extension point (e.g. custom `AguiRequestProcessor`, `RuntimeContextCustomizer`)?
3. For production multi-tenant chat, do you recommend staying on AG-UI + starter, or switching to the **builder-style `HarnessGateway`** path (custom SSE, not AG-UI)?

## Related issues / PRs

- #751 — AG-UI sessionId not exposed during execution (persistence)
- #901 — Custom hooks via `forwardedProps` (open)
- #1895 / #1780 — RuntimeContext propagation for AG-UI (`sessionId` ← `threadId`)

## Our current workaround

Wrapping the registered `Agent` to inject `SecurityContext` → `RuntimeContext.userId` before delegating to `HarnessAgent.stream(..., ctx)`. This works but feels like integration-layer logic placed at the wrong abstraction level.

Any guidance or planned API for this scenario would be appreciated. Happy to contribute a PR if there is a preferred design.

---

**Environment**: `agentscope-java` 2.0.0-RC4, Spring Boot 3.4, Java 17

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.