UNPKG

@langgraph-js/sdk

Version:

The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces

118 lines (117 loc) 4.03 kB
import { LangGraphClient, LangGraphClientConfig } from "./LangGraphClient.js"; import type { Thread } from "@langchain/langgraph-sdk"; export interface SessionInfo { /** 会话唯一标识,同时也是 threadId */ sessionId: string; /** LangGraphClient 实例(懒加载) */ client?: LangGraphClient; /** Thread 信息(懒加载,第一条消息后才创建) */ thread?: Thread<any>; /** Agent 名称 */ agentName: string; } export interface CreateSessionOptions { /** 会话 ID / Thread ID,不提供则自动生成 */ sessionId?: string; /** Agent 名称 */ agentName?: string; /** 是否从已有 Thread 恢复会话 */ restore?: boolean; /** Graph ID */ graphId?: string; } /** * @zh History 类用于管理多个 LangGraphClient 实例,支持多会话场景 * @en History class manages multiple LangGraphClient instances for multi-session scenarios */ export declare class History { /** 存储所有会话的 Map */ private sessions; /** 当前活跃的会话 ID */ private activeSessionId; /** 客户端配置,用于创建新的 LangGraphClient 实例 */ private clientConfig; /** 虚拟 Client,用于查询操作(不绑定特定 Thread) */ private virtualClient; constructor(clientConfig: LangGraphClientConfig); /** * @zh 创建新会话(延迟创建 Thread,直到发送第一条消息) * @en Creates a new session (lazy Thread creation until first message) */ createSession(options?: CreateSessionOptions): Promise<SessionInfo>; /** * @zh 激活指定会话(懒加载创建 Client) * @en Activates the specified session (lazy load client) */ activateSession(sessionId: string, mustResetStream?: boolean): Promise<SessionInfo>; /** * @zh 获取当前活跃的会话 * @en Gets the current active session */ getActiveSession(): SessionInfo | null; /** * @zh 获取指定会话 * @en Gets the specified session */ getSession(sessionId: string): SessionInfo | null; /** * @zh 获取所有会话 * @en Gets all sessions */ getAllSessions(): SessionInfo[]; /** * @zh 删除指定会话 * @en Deletes the specified session */ deleteSession(sessionId: string): Promise<boolean>; /** * @zh 清空所有会话 * @en Clears all sessions */ clearAllSessions(): Promise<void>; /** * @zh 获取会话数量 * @en Gets the number of sessions */ getSessionCount(): number; /** * @zh 生成会话 ID (UUID v4 格式) * @en Generates a session ID (UUID v4 format) */ private generateSessionId; /** * @zh 从已有的 Thread 添加会话(仅添加元数据,不创建 Client) * @en Adds a session from an existing Thread (metadata only, no client created) */ addSessionFromThread(threadId: string, agentName?: string): Promise<SessionInfo>; /** * @zh 获取当前活跃的客户端 * @en Gets the current active client */ getActiveClient(): LangGraphClient | null; /** * @zh 从远程列出所有会话 * @en Lists all sessions from remote */ listRemoteSessions(options?: { sortOrder?: "asc" | "desc"; sortBy?: "created_at" | "updated_at"; offset?: number; limit?: number; }): Promise<Thread<unknown>[]>; /** * @zh 从远程同步会话到本地(仅同步元数据,不创建 Client) * @en Syncs sessions from remote to local (metadata only, no client created) */ syncFromRemote(options?: { limit?: number; agentName?: string; }): Promise<SessionInfo[]>; /** * @zh 初始化 History(必须先调用) * @en Initializes History (must be called first) */ init(agentName?: string, config?: { fallbackToAvailableAssistants?: boolean; }): Promise<import("@langchain/langgraph-sdk").Assistant | undefined>; }