ThreadSessionManager
public class ThreadSessionManager
io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:59–427会话线程会话管理器,承载智能体的部署、调度与服务化运行。位于 io.agentscope.spring.boot.agui.common 包。核心成员包括 getOrCreateAgent、ensureSession、hasMemory、getSession、getSessions、removeSession。
Manages agent sessions by (userId, threadId) for server-side memory.
When server-side memory is enabled, the same agent instance is reused for requests with the same user and thread, preserving conversation history across requests. Sessions for different users never share a slot, even when they reuse a thread id.
Usage:
`ThreadSessionManager manager = new ThreadSessionManager(1000, 30);
Agent agent =
manager.getOrCreateAgent(
"user-1", "thread-123", "default", () -> createAgent());
boolean hasMemory =
manager.hasMemory(
RuntimeContext.builder()
.userId("user-1")
.sessionId("thread-123")
.build());
manager.cleanupExpiredSessions();
`
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
ThreadSession | 类class | 会话线程会话,承载智能体的部署、调度与服务化运行。 Represents a thread session with its agent and metadata. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public ThreadSessionManager(int maxSessions, int sessionTimeoutMinutes) | 创建新的ThreadSessionManager。 Creates a new ThreadSessionManager. |
方法摘要Method Summary
构造器详情Constructor Detail
ThreadSessionManager
public ThreadSessionManager(int maxSessions, int sessionTimeoutMinutes)
创建新的ThreadSessionManager。
Creates a new ThreadSessionManager.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
maxSessions | int | 最大会话(类型 `int`)。Maximum number of sessions to maintain |
sessionTimeoutMinutes | int | 会话超时分钟(类型 `int`)。AgentStateStore timeout in minutes (0 = no timeout) |
io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:81–84方法详情Method Detail
getOrCreateAgent
public Agent getOrCreateAgent(String threadId, String agentId, Supplier<Agent> agentFactory)
获取或创建智能体。
Get or create an agent for the given threadId under the anonymous user slot.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
agentId | String | 智能体标识(类型 `String`)。The agent type identifier |
agentFactory | Supplier<Agent> | 智能体工厂(类型 `Supplier`)。Factory to create new agents if needed |
Agentio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:94–96getOrCreateAgent
public Agent getOrCreateAgent(String userId, String threadId, String agentId, Supplier<Agent> agentFactory)
获取或创建智能体。
Get or create an agent for the given user and thread.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user identifier, may be `null` for anonymous |
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
agentId | String | 智能体标识(类型 `String`)。The agent type identifier |
agentFactory | Supplier<Agent> | 智能体工厂(类型 `Supplier`)。Factory to create new agents if needed |
Agentio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:110–119ensureSession
public ThreadSession ensureSession(String threadId, String agentId, String name, Supplier<Agent> agentFactory)
执行确保会话相关操作。
Ensure a session exists for the given threadId under the anonymous user slot.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
agentId | String | 智能体标识(类型 `String`)。The agent type identifier |
name | String | 名称(类型 `String`)。Display name for the thread (may be null) |
agentFactory | Supplier<Agent> | 智能体工厂(类型 `Supplier`)。Factory to create new agents if needed |
ThreadSessionio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:130–133ensureSession
public ThreadSession ensureSession(String userId, String threadId, String agentId, String name, Supplier<Agent> agentFactory)
执行确保会话相关操作。
Ensure a session exists for the given user and thread, creating one if needed.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user identifier, may be `null` for anonymous |
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
agentId | String | 智能体标识(类型 `String`)。The agent type identifier |
name | String | 名称(类型 `String`)。Display name for the thread (may be null) |
agentFactory | Supplier<Agent> | 智能体工厂(类型 `Supplier`)。Factory to create new agents if needed |
ThreadSessionio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:145–156hasMemory
public boolean hasMemory(RuntimeContext runtimeContext)
判断是否具有记忆。
Check if a session exists and has memory for the given runtime context.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
runtimeContext | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The runtime context identifying the user and thread |
booleanio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:168–184getSession
public Optional<ThreadSession> getSession(String threadId)
获取会话。
Get the session for a threadId under the anonymous user slot if it exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
Optional<ThreadSession>io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:192–194getSession
public Optional<ThreadSession> getSession(String userId, String threadId)
获取会话。
Get the session for a user and thread if it exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user identifier, may be `null` for anonymous |
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
Optional<ThreadSession>io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:203–205getSessions
public Map<String, ThreadSession> getSessions()
获取会话。
Returns an unmodifiable snapshot of all sessions keyed by threadId.
Map<String, ThreadSession>io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:216–222removeSession
public boolean removeSession(String threadId)
移除会话。
Remove a session by threadId under the anonymous user slot.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
booleanio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:230–232removeSession
public boolean removeSession(String userId, String threadId)
移除会话。
Remove a session by user and thread.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user identifier, may be `null` for anonymous |
threadId | String | 会话线程标识(类型 `String`)。The thread identifier |
booleanio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:241–243cleanupExpiredSessions
public void cleanupExpiredSessions()
执行 cleanupExpiredSessions 方法。
Clean up sessions that have been inactive for longer than the timeout.
io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:246–266getSessionCount
public int getSessionCount()
获取会话数量。
Get the current number of active sessions.
intio/agentscope/spring/boot/agui/common/ThreadSessionManager.java:350–352clear
public void clear()
清空或重置内部状态。
Clear all sessions.
io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:355–357嵌套类详情Nested Class Detail
ThreadSession
public static class ThreadSession会话线程会话,承载智能体的部署、调度与服务化运行。位于 io.agentscope.spring.boot.agui.common 包。核心成员包括 getUserId、getThreadId、getAgentId、getAgent、getCreatedAt、getLastAccess。
Represents a thread session with its agent and metadata.