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
186 lines (163 loc) • 5.67 kB
JavaScript
/**
* Service pour manipuler les fichiers
*/
import fs from 'fs/promises';
import path from 'path';
import { promisify } from 'util';
import { exec as execCallback } from 'child_process';
const exec = promisify(execCallback);
export class FileService {
/**
* Initialise le service de fichiers
* @param {string} basePath - Chemin de base (optionnel)
*/
constructor(basePath = process.cwd()) {
this.basePath = basePath;
}
/**
* Définit le chemin de base
* @param {string} basePath - Nouveau chemin de base
*/
setBasePath(basePath) {
this.basePath = basePath;
}
/**
* Lit un fichier
* @param {string} filePath - Chemin du fichier (relatif ou absolu)
* @returns {Promise<string>} - Contenu du fichier
*/
async readFile(filePath) {
const fullPath = this._resolvePath(filePath);
try {
return await fs.readFile(fullPath, 'utf-8');
} catch (error) {
throw new Error(`Erreur lors de la lecture du fichier ${filePath}: ${error.message}`);
}
}
/**
* Écrit dans un fichier
* @param {string} filePath - Chemin du fichier (relatif ou absolu)
* @param {string} content - Contenu à écrire
* @param {boolean} createDirectories - Crée les répertoires parents si nécessaire
* @returns {Promise<string>} - Message de succès
*/
async writeFile(filePath, content, createDirectories = true) {
const fullPath = this._resolvePath(filePath);
try {
if (createDirectories) {
await this._ensureDirectoryExists(path.dirname(fullPath));
}
await fs.writeFile(fullPath, content, 'utf-8');
return `Fichier ${filePath} créé avec succès.`;
} catch (error) {
throw new Error(`Erreur lors de l'écriture du fichier ${filePath}: ${error.message}`);
}
}
/**
* Modifie un fichier existant
* @param {string} filePath - Chemin du fichier (relatif ou absolu)
* @param {string} oldText - Texte à remplacer
* @param {string} newText - Nouveau texte
* @returns {Promise<string>} - Message de succès
*/
async editFile(filePath, oldText, newText) {
const fullPath = this._resolvePath(filePath);
try {
// Lire le fichier
const content = await fs.readFile(fullPath, 'utf-8');
// Vérifier que le texte à remplacer existe
if (!content.includes(oldText)) {
throw new Error(`Le texte à remplacer n'a pas été trouvé dans le fichier ${filePath}`);
}
// Remplacer le texte
const newContent = content.replace(oldText, newText);
// Écrire le fichier
await fs.writeFile(fullPath, newContent, 'utf-8');
return `Fichier ${filePath} modifié avec succès.`;
} catch (error) {
throw new Error(`Erreur lors de la modification du fichier ${filePath}: ${error.message}`);
}
}
/**
* Liste les fichiers d'un répertoire
* @param {string} dirPath - Chemin du répertoire (relatif ou absolu)
* @returns {Promise<Array>} - Liste des fichiers et répertoires
*/
async listDirectory(dirPath = '.') {
const fullPath = this._resolvePath(dirPath);
try {
const files = await fs.readdir(fullPath, { withFileTypes: true });
return Promise.all(files.map(async (file) => {
const filePath = path.join(dirPath, file.name);
const fullFilePath = path.join(fullPath, file.name);
const stats = await fs.stat(fullFilePath);
return {
name: file.name,
path: filePath,
isDirectory: file.isDirectory(),
size: stats.size,
modified: stats.mtime
};
}));
} catch (error) {
throw new Error(`Erreur lors de la lecture du répertoire ${dirPath}: ${error.message}`);
}
}
/**
* Crée un répertoire
* @param {string} dirPath - Chemin du répertoire (relatif ou absolu)
* @param {boolean} recursive - Crée les répertoires parents si nécessaire
* @returns {Promise<string>} - Message de succès
*/
async createDirectory(dirPath, recursive = true) {
const fullPath = this._resolvePath(dirPath);
try {
await fs.mkdir(fullPath, { recursive });
return `Répertoire ${dirPath} créé avec succès.`;
} catch (error) {
throw new Error(`Erreur lors de la création du répertoire ${dirPath}: ${error.message}`);
}
}
/**
* Exécute une commande système
* @param {string} command - Commande à exécuter
* @param {string} cwd - Répertoire de travail (par défaut: chemin de base)
* @returns {Promise<Object>} - Sortie standard et sortie d'erreur
*/
async executeCommand(command, cwd = this.basePath) {
try {
const { stdout, stderr } = await exec(command, { cwd });
return { stdout, stderr };
} catch (error) {
throw new Error(`Erreur lors de l'exécution de la commande: ${error.message}`);
}
}
/**
* Résout un chemin relatif en chemin absolu
* @param {string} relativePath - Chemin relatif ou absolu
* @returns {string} - Chemin absolu
* @private
*/
_resolvePath(relativePath) {
if (path.isAbsolute(relativePath)) {
return relativePath;
}
return path.resolve(this.basePath, relativePath);
}
/**
* S'assure qu'un répertoire existe
* @param {string} dirPath - Chemin du répertoire
* @returns {Promise<void>}
* @private
*/
async _ensureDirectoryExists(dirPath) {
try {
await fs.mkdir(dirPath, { recursive: true });
} catch (error) {
// Ignorer l'erreur si le répertoire existe déjà
if (error.code !== 'EEXIST') {
throw error;
}
}
}
}