UNPKG

@whyuds/coding-converse

Version:

An MCP server that enables interactive conversations between AI code editors and users for collaborative problem-solving

127 lines 4.9 kB
import * as readline from 'readline'; import chalk from 'chalk'; import inquirer from 'inquirer'; export class TerminalInterface { constructor() { this.rl = readline.createInterface({ input: process.stdin, output: process.stdout }); } async askUser(userQuestion) { // 显示问题头部 this.displayQuestionHeader(); // 显示上下文(如果有) if (userQuestion.context) { console.log(chalk.gray('Context:')); console.log(chalk.gray(userQuestion.context)); console.log(''); } // 显示问题 console.log(chalk.cyan('AI Editor asks:')); console.log(chalk.white(userQuestion.question)); console.log(''); // 如果有选项,显示选项 if (userQuestion.options && userQuestion.options.length > 0) { const answer = await inquirer.prompt([ { type: 'list', name: 'response', message: 'Please select an option:', choices: userQuestion.options.concat(['Other (type custom response)']) } ]); if (answer.response === 'Other (type custom response)') { return await this.getCustomResponse(); } return answer.response; } else { // 自由文本输入 return await this.getCustomResponse(); } } async getCustomResponse() { const answer = await inquirer.prompt([ { type: 'editor', name: 'response', message: 'Your response (this will open your default editor):', default: '' } ]); return answer.response.trim(); } async displayConversationStart(topic) { console.log(''); console.log(chalk.green('═'.repeat(60))); console.log(chalk.green.bold('🤖 CodingConverse - Interactive AI Conversation')); console.log(chalk.green('═'.repeat(60))); console.log(chalk.yellow(`Topic: ${topic}`)); console.log(chalk.gray(`Started at: ${new Date().toLocaleString()}`)); console.log(''); console.log(chalk.blue('The AI editor will ask you questions when it needs guidance.')); console.log(chalk.blue('Your responses will help improve the code generation process.')); console.log(''); } async displayConversationEnd(summary) { console.log(''); console.log(chalk.green('─'.repeat(60))); console.log(chalk.green.bold('✅ Conversation Completed')); console.log(chalk.green('─'.repeat(60))); if (summary) { console.log(chalk.yellow('Summary:')); console.log(chalk.white(summary)); } console.log(chalk.gray(`Ended at: ${new Date().toLocaleString()}`)); console.log(''); } displayQuestionHeader() { console.log(''); console.log(chalk.blue('┌' + '─'.repeat(58) + '┐')); console.log(chalk.blue('│') + chalk.bold.white(' 🤔 AI Editor needs your input ') + ' '.repeat(25) + chalk.blue('│')); console.log(chalk.blue('└' + '─'.repeat(58) + '┘')); console.log(''); } displayError(error) { console.log(''); console.log(chalk.red('❌ Error:')); console.log(chalk.red(error)); console.log(''); } displayInfo(message) { console.log(''); console.log(chalk.blue('ℹ️ Info:')); console.log(chalk.white(message)); console.log(''); } displaySuccess(message) { console.log(''); console.log(chalk.green('✅ Success:')); console.log(chalk.white(message)); console.log(''); } close() { this.rl.close(); } // 用于在Windows和Mac上显示不同的提示 displayPlatformSpecificInstructions() { const platform = process.platform; console.log(''); console.log(chalk.yellow('Platform-specific notes:')); if (platform === 'win32') { console.log(chalk.gray('• Windows: Use Ctrl+C to interrupt if needed')); console.log(chalk.gray('• Default editor: Notepad (can be changed via EDITOR env var)')); } else if (platform === 'darwin') { console.log(chalk.gray('• macOS: Use Cmd+C to interrupt if needed')); console.log(chalk.gray('• Default editor: nano/vim (can be changed via EDITOR env var)')); } else { console.log(chalk.gray('• Linux/Unix: Use Ctrl+C to interrupt if needed')); console.log(chalk.gray('• Default editor: nano/vim (can be changed via EDITOR env var)')); } console.log(''); } } //# sourceMappingURL=terminal-interface.js.map