* 启动代码助手
(config: AgentConfig)
| 633 | * 启动代码助手 |
| 634 | */ |
| 635 | async function startCodeAssistant(config: AgentConfig) { |
| 636 | console.log(chalk.cyan('\n=== 💻 代码助手 Agent ===')); |
| 637 | |
| 638 | const agent = new Agent(config); |
| 639 | await agent.init(); |
| 640 | |
| 641 | const sampleCode = ` |
| 642 | function calculateTotal(items) { |
| 643 | var total = 0; |
| 644 | for (var i = 0; i < items.length; i++) { |
| 645 | total += items[i].price * items[i].quantity; |
| 646 | } |
| 647 | return total; |
| 648 | }`; |
| 649 | |
| 650 | console.log(chalk.yellow('\n待分析的代码:')); |
| 651 | console.log(sampleCode); |
| 652 | |
| 653 | try { |
| 654 | console.log(chalk.gray('\n🔍 正在进行代码审查...')); |
| 655 | const review = await agent.reviewCode(sampleCode, 'javascript'); |
| 656 | console.log(chalk.green('\n📋 代码审查结果:')); |
| 657 | console.log(review); |
| 658 | |
| 659 | console.log(chalk.gray('\n🧪 正在生成测试用例...')); |
| 660 | const prompt = `为以下代码生成测试用例:\n${sampleCode}`; |
| 661 | const tests = await agent.chat(prompt); |
| 662 | console.log(chalk.green('\n🔬 生成的测试用例:')); |
| 663 | console.log(tests); |
| 664 | } catch (error) { |
| 665 | console.error(chalk.red('❌ 代码分析失败:'), error); |
| 666 | } |
| 667 | |
| 668 | await agent.destroy(); |
| 669 | console.log(chalk.green('\n✅ 代码助手演示完成')); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * 启动基础助手 |
no test coverage detected