UNPKG

mini-claude-code

Version:

Advanced AI-powered coding assistant with streaming responses, context memory, intelligent auto-completion, error handling, test generation, and task planning

253 lines (212 loc) 9.19 kB
/** * Mini Claude Code v2 - 完整功能演示 * 展示所有 3 个优先任务的集成效果 */ const ToolManager = require('../lib/tool-manager'); const chalk = require('chalk'); async function demonstrateV2Features() { console.log(chalk.blue.bold('🚀 Mini Claude Code v2 - 完整功能演示\n')); console.log(chalk.gray('展示流式响应、上下文记忆和自动补全的集成效果\n')); const toolManager = new ToolManager(); await toolManager.initialize(); // 检查所有系统状态 const aiStatus = toolManager.getAIStatus(); console.log(chalk.blue('🔍 系统状态检查:')); console.log(` ✅ AI 服务: ${aiStatus.available ? '可用' : '不可用'}`); console.log(` ✅ 记忆系统: ${aiStatus.memoryStats ? '已初始化' : '未初始化'}`); if (!aiStatus.available) { console.log(chalk.yellow(' ⚠️ 配置默认 API Key...')); await toolManager.configureAPI('sk-386b598ba19f49eba2d681f8135f5ae3'); } console.log(chalk.blue('\n📋 功能演示 1: 流式 AI 响应')); console.log(chalk.cyan('模拟用户问题: "什么是React组件的生命周期?"')); try { let streamingDemo = ''; const streamResult = await toolManager.ai.chatStream( '请简单解释React组件的生命周期,用中文回答,控制在150字以内', { temperature: 0.7, maxTokens: 200, timeout: 15000 }, async (data) => { if (data.content) { process.stdout.write(data.content); streamingDemo += data.content; } if (data.isComplete) { process.stdout.write('\n'); } } ); if (streamResult.success) { console.log(chalk.green(`✅ 流式响应完成 (${streamResult.totalChunks} 个数据块)`)); // 保存到记忆系统 await toolManager.memory.addConversation( '什么是React组件的生命周期?', streamResult.response, { projectPath: process.cwd(), language: 'javascript', framework: 'react', commandType: 'demo' } ); console.log(chalk.gray(' 💾 对话已保存到记忆系统')); } } catch (error) { console.log(chalk.red(`❌ 流式响应演示失败: ${error.message}`)); } console.log(chalk.blue('\n🧠 功能演示 2: 上下文记忆系统')); try { // 添加用户偏好 await toolManager.memory.updateUserPreferences({ preferredLanguage: 'JavaScript', codeStyle: 'ES6+', responseStyle: '简洁实用', framework: 'React' }); console.log(chalk.green('✅ 用户偏好已设置')); // 添加代码片段 const sampleCode = ` function Welcome(props) { return <h1>Hello, {props.name}!</h1>; } export default Welcome;`; await toolManager.memory.addCodeSnippet( sampleCode, 'React 欢迎组件示例', 'javascript', { projectPath: process.cwd(), framework: 'react', fileType: 'component' } ); console.log(chalk.green('✅ 代码片段已保存')); // 生成上下文提示 const contextPrompt = toolManager.memory.generateContextPrompt( '如何创建一个新的React组件?', toolManager.context.projectInfo ); console.log(chalk.gray('✅ 上下文提示已生成 (包含历史对话、用户偏好和相关代码)')); console.log(chalk.blue(' 上下文内容预览:')); console.log(chalk.gray(` ${contextPrompt.substring(0, 200)}...`)); // 搜索相关历史 const relatedHistory = toolManager.memory.getRelevantHistory('React 生命周期', 2); console.log(chalk.green(`✅ 找到 ${relatedHistory.length} 条相关历史对话`)); // 搜索相关代码 const relatedCode = toolManager.memory.searchCodeSnippets('React 组件', 'javascript', 2); console.log(chalk.green(`✅ 找到 ${relatedCode.length} 个相关代码片段`)); } catch (error) { console.log(chalk.red(`❌ 记忆系统演示失败: ${error.message}`)); } console.log(chalk.blue('\n⚡ 功能演示 3: 命令自动补全')); try { const AutoCompletion = require('../lib/auto-completion'); const autoCompletion = new AutoCompletion(toolManager); // 演示各种补全场景 const scenarios = [ { input: 'he', description: '命令补全' }, { input: 'ai s', description: 'AI 子命令补全' }, { input: 'create comp', description: '文件类型补全' }, { input: 'create component But', description: '文件名建议' }, { input: 'read package.', description: '文件路径补全' } ]; scenarios.forEach(scenario => { const completions = autoCompletion.getCommandCompletions(scenario.input); console.log(`✅ ${scenario.description}: "${scenario.input}" → [${completions.map(c => c.text).join(', ')}]`); }); // 智能建议 const smartSuggestions = autoCompletion.getSmartSuggestions(); console.log(`✅ 智能建议: ${smartSuggestions.length} 个上下文相关的建议`); smartSuggestions.slice(0, 3).forEach((suggestion, index) => { console.log(` ${index + 1}. ${suggestion.command} - ${suggestion.description}`); }); // 统计信息 const stats = autoCompletion.getStats(); console.log(`✅ 补全统计: ${stats.baseCommands} 个命令, ${stats.supportedFileTypes} 种文件类型`); } catch (error) { console.log(chalk.red(`❌ 自动补全演示失败: ${error.message}`)); } console.log(chalk.blue('\n🔗 功能演示 4: 集成效果')); try { console.log(chalk.cyan('模拟用户后续问题: "刚才的React组件如何添加状态管理?"')); // 使用集成的记忆上下文进行流式响应 const contextPrompt = toolManager.memory.generateContextPrompt( '刚才的React组件如何添加状态管理?', toolManager.context.projectInfo ); let integratedDemo = ''; const integratedResult = await toolManager.ai.chatStream( '刚才的React组件如何添加状态管理?请基于之前的对话上下文回答,用中文,控制在100字以内', { systemPrompt: `你是一个专业的React开发助手。${contextPrompt}`, temperature: 0.7, maxTokens: 150, timeout: 15000 }, async (data) => { if (data.content) { process.stdout.write(data.content); integratedDemo += data.content; } if (data.isComplete) { process.stdout.write('\n'); } } ); if (integratedResult.success) { console.log(chalk.green('✅ 集成流式响应完成 (包含上下文记忆)')); // 保存新的对话 await toolManager.memory.addConversation( '刚才的React组件如何添加状态管理?', integratedResult.response, { projectPath: process.cwd(), language: 'javascript', framework: 'react', commandType: 'integrated_demo' } ); console.log(chalk.gray(' 💾 新对话已关联到历史记录')); } } catch (error) { console.log(chalk.red(`❌ 集成效果演示失败: ${error.message}`)); } // 最终统计报告 console.log(chalk.blue('\n📊 Mini Claude Code v2 功能总览:')); const finalMemoryStats = toolManager.getMemoryStats(); console.log(chalk.green('\n✅ 已完成的 3 个优先任务:')); console.log(''); console.log(chalk.yellow('🚀 任务 1: 流式 AI 响应')); console.log(' • 实时流式输出,解决长响应超时问题'); console.log(' • 支持实时回调和数据块处理'); console.log(' • 优化的用户体验和响应速度'); console.log(''); console.log(chalk.yellow('🧠 任务 2: 上下文记忆系统')); console.log(` • 对话历史: ${finalMemoryStats.conversationCount} 条记录`); console.log(` • 代码片段: ${finalMemoryStats.codeSnippetCount} 个片段`); console.log(` • 用户偏好: ${finalMemoryStats.userPreferences} 项设置`); console.log(` • 项目记忆: ${finalMemoryStats.projectsRemembered} 个项目`); console.log(' • 智能上下文感知和关联检索'); console.log(''); console.log(chalk.yellow('⚡ 任务 3: 命令自动补全')); console.log(' • Tab 键智能补全和文件路径提示'); console.log(' • 项目特定命令建议和历史浏览'); console.log(' • 多层级补全和缓存优化'); console.log(''); console.log(chalk.blue('🎯 集成优势:')); console.log(' • 三个系统无缝协作,提供统一体验'); console.log(' • 上下文感知的智能补全和响应'); console.log(' • 跨会话的记忆持久化和学习能力'); console.log(' • 高性能的流式处理和缓存机制'); console.log(''); console.log(chalk.green.bold('🎉 Mini Claude Code v2 升级完成!')); console.log(chalk.gray('所有优先功能已成功实现并集成测试 ✅')); } // 运行演示 if (require.main === module) { demonstrateV2Features().catch(console.error); } module.exports = { demonstrateV2Features };