/** * AgentSessions:本地 Agent session 集合入口。 * * 关键点(中文) * - 统一管理 session 缓存、创建、恢复、默认配置注入与列表查询。 * - 该服务只负责 session 生命周期与查询,不负责 plugin / RPC 启停。 * - Session 对象创建细节集中在这里,避免 facade 和 lifecycle 重复依赖 Session 构造逻辑。 */ import type { RuntimeTool as Tool } from "@downcity/type"; import type { AgentModel } from "./AgentModel.js"; import type { Logger } from "../utils/logger/Logger.js"; import type { AgentCreateSessionInput, AgentArchiveSessionInput, AgentArchiveSessionsInput, AgentArchiveSessionResult, AgentArchiveSessionsResult, AgentCleanArchiveResult, AgentListSessionsInput, AgentSessionSummaryPage } from "../types/agent/SessionTypes.js"; import type { AgentSessionConstructor } from "../types/agent/AgentOptions.js"; import type { AgentSession, AgentSessions as AgentSessionsContract } from "../types/agent/SessionActor.js"; import type { AgentManagedSession } from "../types/session/SessionOptions.js"; import type { SessionPort } from "../types/session/SessionPort.js"; import type { AgentPluginExecutionRuntime } from "../types/plugin/PluginRuntime.js"; import type { SessionStore } from "../types/store/SessionStore.js"; import type { WorkspaceBase } from "@downcity/workspace"; import type { SessionOrigin } from "../types/session/SessionOrigin.js"; type AgentSessionsOptions = { /** * 当前 agent 稳定标识。 */ agent_id: string; /** * 按 Session 解析执行上下文。 * * AgentSessions 是 Agent 唯一的 Session 集合;Workspace 相关能力不能 * 固定在集合实例上,而应在创建或恢复单个 Session 时解析。 */ resolve_session_context: (workspace?: WorkspaceBase) => { workspace_path: string; workspace_id?: string; logger: Logger; tools: Record; get_workspace_env: () => Record; get_agent_plugins: () => AgentPluginExecutionRuntime; store: SessionStore; }; /** * 当前统一日志器。 */ logger: Logger; /** * 当前静态 instruction 文本集合。 */ get_instruction: () => string[]; /** * 等待当前 Agent 持有的长期运行时启动完成。 */ ensure_agent_ready: () => Promise; /** * 当前 agent 使用的本地 Session 类。 */ session_class?: AgentSessionConstructor; /** 读取 Agent 当前持有的运行时模型实例。 */ get_agent_model: () => AgentModel | undefined; /** Session 创建或恢复后的内部路由登记回调。 */ on_session_routed?: (session_id: string, sessions: AgentSessions) => void; }; /** * 本地 Agent session 管理服务。 */ export declare class AgentSessions implements AgentSessionsContract { private readonly agent_id; private readonly resolve_session_context; private readonly logger; private readonly get_instruction; private readonly ensure_agent_ready; private readonly session_class; private readonly get_agent_model; private readonly on_session_routed?; private readonly sessions_by_id; constructor(options: AgentSessionsOptions); /** * 返回当前缓存的 session 实例。 */ list_cached_sessions(): AgentManagedSession[]; /** 返回当前所有执行中的 Session 标识;可按 Workspace 限定。 */ list_executing_session_ids(workspace_id?: string): string[]; /** 返回当前执行中的 Session 数量。 */ get_executing_session_count(): number; /** 停止当前集合内正在执行的 Session;可按 Workspace 限定。 */ stop_executing_sessions(workspace_id?: string): Promise; /** 释放全部缓存 Session 的标题后台任务。 */ dispose_title_generation(workspace_id?: string): void; /** * 把 Agent env 修改广播到已有 Session 的统一输入队列。 */ broadcast_env(env: Record, command_id: string, workspace_id?: string): void; /** * 把 Plugin registry 修改广播到已有 Session 的统一输入队列。 */ broadcast_plugins(input: { command_id: string; title: string; plugins: AgentPluginExecutionRuntime; workspace_id?: string; }): void; /** * 获取已缓存 Session 的 runtime port。 * * runtime 是执行层的内部投影,不负责创建 Session。需要创建时必须 * 通过 `create()` 获取内部生成的 ID;需要恢复时必须先调用 `get()`。 */ runtime(session_id: string): SessionPort; /** * 新建一个 session。 */ create(input?: AgentCreateSessionInput & { workspace?: WorkspaceBase; origin?: SessionOrigin; }): Promise; /** * 获取一个已存在的 session。 */ get(session_id: string, input?: { workspace?: WorkspaceBase; origin?: SessionOrigin; }): Promise; /** * 永久删除一个 Session 及其全部 Agent 领域数据。 * * 关键点(中文) * - 正在执行的 Session 会先停止,避免删除后继续写入。 * - 该方法不处理任何 Plugin 自有数据。 */ remove(session_id: string): Promise; /** * 清空一个 Session 的消息目录。 */ clear_messages(session_id: string): Promise; /** * 列出当前 agent 的 session 摘要页。 */ list(input?: AgentListSessionsInput): Promise; /** * 归档单个 session。 */ archive(input: AgentArchiveSessionInput): Promise; /** * 列出当前 agent 的已归档 session 摘要页。 */ archived(input?: AgentArchiveSessionsInput): Promise; /** * 永久清空已归档 session。 */ clean_archive(): Promise; private get_or_create_session; private load_instruction_system_blocks; } export {}; //# sourceMappingURL=AgentSessions.d.ts.map