AS AgentScopeAPI 手册 / API Reference

ThreadSessionManager

public class ThreadSessionManager
源码Source 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

方法Method说明Description
public Agent getOrCreateAgent(String threadId, String agentId, Supplier<Agent> agentFactory)
获取或创建智能体。
Get or create an agent for the given threadId under the anonymous user slot.
public Agent getOrCreateAgent(String userId, String threadId, String agentId, Supplier<Agent> agentFactory)
获取或创建智能体。
Get or create an agent for the given user and thread.
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.
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.
public boolean hasMemory(RuntimeContext runtimeContext)
判断是否具有记忆。
Check if a session exists and has memory for the given runtime context.
public Optional<ThreadSession> getSession(String threadId)
获取会话。
Get the session for a threadId under the anonymous user slot if it exists.
public Optional<ThreadSession> getSession(String userId, String threadId)
获取会话。
Get the session for a user and thread if it exists.
public Map<String, ThreadSession> getSessions()
获取会话。
Returns an unmodifiable snapshot of all sessions keyed by threadId.
public boolean removeSession(String threadId)
移除会话。
Remove a session by threadId under the anonymous user slot.
public boolean removeSession(String userId, String threadId)
移除会话。
Remove a session by user and thread.
public void cleanupExpiredSessions()
执行 `cleanupExpiredSessions` 方法。
Clean up sessions that have been inactive for longer than the timeout.
public int getSessionCount()
获取会话数量。
Get the current number of active sessions.
public void clear()
清空或重置内部状态。
Clear all sessions.

构造器详情Constructor Detail

ThreadSessionManager

public ThreadSessionManager(int maxSessions, int sessionTimeoutMinutes)

创建新的ThreadSessionManager。

Creates a new ThreadSessionManager.

参数Parameters
名称Name类型Type说明Description
maxSessionsint最大会话(类型 `int`)。Maximum number of sessions to maintain
sessionTimeoutMinutesint会话超时分钟(类型 `int`)。AgentStateStore timeout in minutes (0 = no timeout)
源码Source 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.

参数Parameters
名称Name类型Type说明Description
threadIdString会话线程标识(类型 `String`)。The thread identifier
agentIdString智能体标识(类型 `String`)。The agent type identifier
agentFactorySupplier<Agent>智能体工厂(类型 `Supplier`)。Factory to create new agents if needed
返回Returns Agent
智能体用于此会话线程。The agent for this thread
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:94–96

getOrCreateAgent

public Agent getOrCreateAgent(String userId, String threadId, String agentId, Supplier<Agent> agentFactory)

获取或创建智能体。

Get or create an agent for the given user and thread.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user identifier, may be `null` for anonymous
threadIdString会话线程标识(类型 `String`)。The thread identifier
agentIdString智能体标识(类型 `String`)。The agent type identifier
agentFactorySupplier<Agent>智能体工厂(类型 `Supplier`)。Factory to create new agents if needed
返回Returns Agent
智能体用于此用户和会话线程。The agent for this user and thread
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:110–119

ensureSession

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.

参数Parameters
名称Name类型Type说明Description
threadIdString会话线程标识(类型 `String`)。The thread identifier
agentIdString智能体标识(类型 `String`)。The agent type identifier
nameString名称(类型 `String`)。Display name for the thread (may be null)
agentFactorySupplier<Agent>智能体工厂(类型 `Supplier`)。Factory to create new agents if needed
返回Returns ThreadSession
会话用于此会话线程。The session for this thread
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:130–133

ensureSession

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.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user identifier, may be `null` for anonymous
threadIdString会话线程标识(类型 `String`)。The thread identifier
agentIdString智能体标识(类型 `String`)。The agent type identifier
nameString名称(类型 `String`)。Display name for the thread (may be null)
agentFactorySupplier<Agent>智能体工厂(类型 `Supplier`)。Factory to create new agents if needed
返回Returns ThreadSession
会话用于此用户和会话线程。The session for this user and thread
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:145–156

hasMemory

public boolean hasMemory(RuntimeContext runtimeContext)

判断是否具有记忆。

Check if a session exists and has memory for the given runtime context.

参数Parameters
名称Name类型Type说明Description
runtimeContextRuntimeContext运行时上下文(类型 `RuntimeContext`)。The runtime context identifying the user and thread
返回Returns boolean
返回布尔值,表示判断结果。true if the session exists and the agent has non-empty memory
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:168–184

getSession

public Optional<ThreadSession> getSession(String threadId)

获取会话。

Get the session for a threadId under the anonymous user slot if it exists.

参数Parameters
名称Name类型Type说明Description
threadIdString会话线程标识(类型 `String`)。The thread identifier
返回Returns Optional<ThreadSession>
返回可选。Optional containing the session, or empty if not found
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:192–194

getSession

public Optional<ThreadSession> getSession(String userId, String threadId)

获取会话。

Get the session for a user and thread if it exists.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user identifier, may be `null` for anonymous
threadIdString会话线程标识(类型 `String`)。The thread identifier
返回Returns Optional<ThreadSession>
返回可选。Optional containing the session, or empty if not found
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:203–205

getSessions

public Map<String, ThreadSession> getSessions()

获取会话。

Returns an unmodifiable snapshot of all sessions keyed by threadId.

返回Returns Map<String, ThreadSession>
返回映射。session snapshot
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:216–222

removeSession

public boolean removeSession(String threadId)

移除会话。

Remove a session by threadId under the anonymous user slot.

参数Parameters
名称Name类型Type说明Description
threadIdString会话线程标识(类型 `String`)。The thread identifier
返回Returns boolean
返回布尔值,表示判断结果。true if a session was removed
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:230–232

removeSession

public boolean removeSession(String userId, String threadId)

移除会话。

Remove a session by user and thread.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user identifier, may be `null` for anonymous
threadIdString会话线程标识(类型 `String`)。The thread identifier
返回Returns boolean
返回布尔值,表示判断结果。true if a session was removed
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:241–243

cleanupExpiredSessions

public void cleanupExpiredSessions()

执行 cleanupExpiredSessions 方法。

Clean up sessions that have been inactive for longer than the timeout.

源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:246–266

getSessionCount

public int getSessionCount()

获取会话数量。

Get the current number of active sessions.

返回Returns int
会话的数量。Number of sessions
源码Source io/agentscope/spring/boot/agui/common/ThreadSessionManager.java:350–352

clear

public void clear()

清空或重置内部状态。

Clear all sessions.

源码Source 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.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.