* 交互式聊天
( agent: Agent, scenario: string, useStream: boolean = false, useContext: boolean = false )
| 329 | * 交互式聊天 |
| 330 | */ |
| 331 | async function startInteractiveChat( |
| 332 | agent: Agent, |
| 333 | scenario: string, |
| 334 | useStream: boolean = false, |
| 335 | useContext: boolean = false |
| 336 | ) { |
| 337 | console.log(chalk.cyan(`\n=== 🤖 ${getScenarioName(scenario)} ===`)); |
| 338 | if (useContext) { |
| 339 | console.log(chalk.gray('🧠 上下文记忆已启用 - 我会记住我们的对话')); |
| 340 | |
| 341 | // 显示当前会话信息 |
| 342 | const sessionId = agent.getCurrentSessionId(); |
| 343 | if (sessionId) { |
| 344 | console.log(chalk.gray(`📋 当前会话: ${sessionId}`)); |
| 345 | } |
| 346 | } |
| 347 | console.log(chalk.gray('输入 "quit" 或 "exit" 退出聊天')); |
| 348 | console.log(chalk.gray('输入 "stats" 查看上下文统计信息')); |
| 349 | console.log(chalk.gray('输入 "sessions" 搜索历史会话\n')); |
| 350 | |
| 351 | try { |
| 352 | while (true) { |
| 353 | const { message } = await inquirer.prompt([ |
| 354 | { |
| 355 | type: 'input', |
| 356 | name: 'message', |
| 357 | message: '你:', |
| 358 | }, |
| 359 | ]); |
| 360 | |
| 361 | if (!message.trim()) { |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | if (message.toLowerCase() === 'quit' || message.toLowerCase() === 'exit') { |
| 366 | console.log(chalk.blue('👋 再见!')); |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | // 特殊命令处理 |
| 371 | if (message.toLowerCase() === 'stats' && useContext) { |
| 372 | const stats = await agent.getContextStats(); |
| 373 | if (stats) { |
| 374 | console.log(chalk.cyan('\n📊 上下文统计信息:')); |
| 375 | console.log(chalk.gray(`- 当前会话: ${stats.currentSession}`)); |
| 376 | console.log(chalk.gray(`- 内存消息数: ${stats.memory.messageCount}`)); |
| 377 | console.log(chalk.gray(`- 缓存大小: ${stats.cache.size}`)); |
| 378 | console.log(chalk.gray(`- 存储会话数: ${stats.storage.totalSessions}\n`)); |
| 379 | } |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | if (message.toLowerCase() === 'sessions' && useContext) { |
| 384 | try { |
| 385 | const sessions = await agent.searchContextSessions('', 5); |
| 386 | if (sessions.length > 0) { |
| 387 | console.log(chalk.cyan('\n📂 最近的会话:')); |
| 388 | sessions.forEach((session, index) => { |
no test coverage detected