* 创建新会话
(
userId?: string,
preferences: Record<string, any> = {},
configuration: Record<string, any> = {},
customSessionId?: string
)
| 141 | * 创建新会话 |
| 142 | */ |
| 143 | public async createSession( |
| 144 | userId?: string, |
| 145 | preferences: Record<string, any> = {}, |
| 146 | configuration: Record<string, any> = {}, |
| 147 | customSessionId?: string |
| 148 | ): Promise<string> { |
| 149 | if (!this.isReady || !this.contextManager) { |
| 150 | throw new Error('上下文组件未初始化或已被禁用'); |
| 151 | } |
| 152 | |
| 153 | // 如果提供了自定义会话ID,先尝试加载 |
| 154 | if (customSessionId) { |
| 155 | const loadSuccess = await this.loadSession(customSessionId); |
| 156 | if (loadSuccess) { |
| 157 | this.log(`会话已存在,加载成功: ${customSessionId}`); |
| 158 | return customSessionId; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // 创建新会话,如果提供了自定义ID则使用 |
| 163 | if (customSessionId) { |
| 164 | // 将自定义ID放入配置中 |
| 165 | configuration.sessionId = customSessionId; |
| 166 | } |
| 167 | |
| 168 | this.currentSessionId = await this.contextManager!.createSession( |
| 169 | userId, |
| 170 | preferences, |
| 171 | configuration |
| 172 | ); |
| 173 | |
| 174 | // 如果指定了自定义ID但创建的ID不匹配,尝试重新设置 |
| 175 | if (customSessionId && this.currentSessionId !== customSessionId) { |
| 176 | this.currentSessionId = customSessionId; |
| 177 | } |
| 178 | |
| 179 | this.log(`创建新会话: ${this.currentSessionId}`); |
| 180 | return this.currentSessionId; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * 加载现有会话 |
no test coverage detected