* 添加消息到当前会话
(
role: ContextMessage['role'],
content: string,
metadata?: Record<string, any>
)
| 191 | * 添加消息到当前会话 |
| 192 | */ |
| 193 | async addMessage( |
| 194 | role: ContextMessage['role'], |
| 195 | content: string, |
| 196 | metadata?: Record<string, any> |
| 197 | ): Promise<void> { |
| 198 | if (!this.currentSessionId) { |
| 199 | throw new Error('没有活动会话'); |
| 200 | } |
| 201 | |
| 202 | const message: ContextMessage = { |
| 203 | id: this.generateMessageId(), |
| 204 | role, |
| 205 | content, |
| 206 | timestamp: Date.now(), |
| 207 | metadata, |
| 208 | }; |
| 209 | |
| 210 | this.memory.addMessage(message); |
| 211 | |
| 212 | // 如果需要压缩,执行压缩 |
| 213 | const contextData = this.memory.getContext(); |
| 214 | if (contextData && this.shouldCompress(contextData)) { |
| 215 | await this.compressCurrentContext(); |
| 216 | } |
| 217 | |
| 218 | // 异步保存到持久化存储 |
| 219 | this.saveCurrentSessionAsync(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * 添加工具调用记录 |
no test coverage detected