UNPKG

ollama-code-qwen

Version:

Un assistant IA en ligne de commande utilisant Ollama et le modèle qwen2.5-coder pour aider au développement, avec des capacités MCP améliorées et détection d'intentions en français et anglais

206 lines (175 loc) 6.17 kB
/** * Mode léger pour l'interface en ligne de commande, optimisé pour les connexions distantes * Ce mode utilise une interface plus simple avec moins de rafraîchissements d'écran */ import readline from 'readline'; import chalk from 'chalk'; import { marked } from 'marked'; import TerminalRenderer from 'marked-terminal'; // Configuration du renderer pour Markdown dans le terminal marked.setOptions({ renderer: new TerminalRenderer({ code: chalk.cyan, blockquote: chalk.gray.italic, table: chalk.white, listitem: chalk.yellow, strong: chalk.bold.green, em: chalk.italic.cyan, heading: chalk.bold.blueBright, }) }); export class LiteMode { /** * Initialise le mode léger * @param {OllamaService} ollamaService - Service Ollama * @param {ContextManager} contextManager - Gestionnaire de contexte * @param {Object} options - Options supplémentaires */ constructor(ollamaService, contextManager, options = {}) { this.ollamaService = ollamaService; this.contextManager = contextManager; this.options = options; // Initialiser readline this.rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: chalk.blue('ollama> '), historySize: 100, removeHistoryDuplicates: true }); // Historique de conversation this.conversation = []; this.projectContext = ''; // Commandes disponibles this.commands = { '/help': this.showHelp.bind(this), '/exit': this.exit.bind(this), '/quit': this.exit.bind(this), '/clear': this.clearScreen.bind(this), '/context': this.showContext.bind(this), '/refresh': this.refreshContext.bind(this) }; // Configuration des gestionnaires d'événements this.rl.on('line', this.handleInput.bind(this)); this.rl.on('close', () => { console.log(chalk.green('\nAu revoir!')); process.exit(0); }); } /** * Démarre le mode léger */ async start() { this.clearScreen(); console.log(chalk.green.bold('=== Ollama Code - Mode Léger ===')); console.log(chalk.blue('Modèle:') + ' ' + this.ollamaService.modelName); console.log(chalk.blue('Serveur:') + ' ' + this.ollamaService.host); console.log(chalk.yellow('Ce mode est optimisé pour les connexions à distance.')); console.log(chalk.yellow('Tapez /help pour voir les commandes disponibles.')); console.log(chalk.cyan('\nChargement du contexte du projet...')); this.projectContext = await this.contextManager.getContext(); console.log(chalk.green('Contexte chargé!')); // Initialiser la conversation avec le contexte du projet this.conversation = [ { role: 'system', content: `You are an AI coding assistant. You help with programming tasks and questions.\n\nCurrent project context:\n${this.projectContext}` } ]; this.rl.prompt(); } /** * Gère l'entrée utilisateur * @param {string} input - Entrée de l'utilisateur */ async handleInput(input) { input = input.trim(); // Ignorer les entrées vides if (!input) { this.rl.prompt(); return; } // Traiter les commandes if (input.startsWith('/')) { const commandName = input.split(' ')[0].toLowerCase(); const command = this.commands[commandName]; if (command) { await command(input); } else { console.log(chalk.red(`Commande inconnue: ${commandName}`)); console.log(chalk.yellow('Tapez /help pour voir les commandes disponibles.')); } this.rl.prompt(); return; } // Traiter l'entrée normale await this.handleUserQuery(input); } /** * Traite une requête utilisateur * @param {string} query - Requête de l'utilisateur */ async handleUserQuery(query) { // Ajouter le message utilisateur à la conversation this.conversation.push({ role: 'user', content: query }); console.log(chalk.cyan('\nRéflexion en cours...')); try { const response = await this.ollamaService.chat(this.conversation); // Ajouter la réponse à la conversation this.conversation.push({ role: 'assistant', content: response }); // Afficher la réponse console.log(chalk.green('\n=== Réponse ===')); console.log(marked(response)); } catch (error) { console.log(chalk.red(`\nErreur: ${error.message}`)); } this.rl.prompt(); } /** * Affiche l'aide */ async showHelp() { console.log(chalk.green('\nCommandes disponibles:')); console.log(chalk.cyan('/help') + ' - Affiche cette aide'); console.log(chalk.cyan('/exit') + ' ou ' + chalk.cyan('/quit') + ' - Quitte l\'application'); console.log(chalk.cyan('/clear') + ' - Efface l\'écran'); console.log(chalk.cyan('/context') + ' - Affiche le contexte du projet'); console.log(chalk.cyan('/refresh') + ' - Rafraîchit le contexte du projet'); } /** * Quitte l'application */ async exit() { console.log(chalk.green('\nAu revoir!')); process.exit(0); } /** * Efface l'écran */ async clearScreen() { console.clear(); } /** * Affiche le contexte du projet */ async showContext() { console.log(chalk.green('\nContexte du projet:')); console.log(this.projectContext); } /** * Rafraîchit le contexte du projet */ async refreshContext() { console.log(chalk.cyan('\nRafraîchissement du contexte...')); try { this.projectContext = await this.contextManager.getContext(); // Mettre à jour le message système if (this.conversation.length > 0 && this.conversation[0].role === 'system') { this.conversation[0].content = `You are an AI coding assistant. You help with programming tasks and questions.\n\nCurrent project context:\n${this.projectContext}`; } console.log(chalk.green('Contexte rafraîchi!')); } catch (error) { console.log(chalk.red(`Erreur lors du rafraîchissement du contexte: ${error.message}`)); } } }