fix logging and MCP response messages

This commit is contained in:
Joe Danziger
2025-05-11 13:32:24 -04:00
parent 0543ba3057
commit d28170ee03
5 changed files with 203 additions and 91 deletions

View File

@@ -7,7 +7,15 @@ import {
enableSilentMode,
disableSilentMode
} from '../../../../scripts/modules/utils.js';
import { removeBrandRules, convertAllRulesToBrandRules, BRAND_NAMES, isValidBrand, getBrandProfile } from '../../../../scripts/modules/rule-transformer.js';
import {
removeBrandRules,
convertAllRulesToBrandRules,
BRAND_NAMES,
isValidBrand,
getBrandProfile
} from '../../../../scripts/modules/rule-transformer.js';
import path from 'path';
import fs from 'fs';
/**
* Direct function wrapper for adding or removing brand rules.
@@ -45,16 +53,26 @@ export async function rulesDirect(args, log, context = {}) {
if (action === 'remove') {
for (const brand of rules) {
if (!isValidBrand(brand)) {
removalResults.push({ brandName: brand, success: false, error: `Profile not found: static import missing for '${brand}'. Valid brands: ${BRAND_NAMES.join(', ')}` });
removalResults.push({
brandName: brand,
success: false,
error: `Profile not found: static import missing for '${brand}'. Valid brands: ${BRAND_NAMES.join(', ')}`
});
continue;
}
const profile = getBrandProfile(brand);
const result = removeBrandRules(projectRoot, profile);
removalResults.push(result);
}
const successes = removalResults.filter(r => r.success).map(r => r.brandName);
const skipped = removalResults.filter(r => r.skipped).map(r => r.brandName);
const errors = removalResults.filter(r => r.error && !r.success && !r.skipped);
const successes = removalResults
.filter((r) => r.success)
.map((r) => r.brandName);
const skipped = removalResults
.filter((r) => r.skipped)
.map((r) => r.brandName);
const errors = removalResults.filter(
(r) => r.error && !r.success && !r.skipped
);
let summary = '';
if (successes.length > 0) {
@@ -64,22 +82,75 @@ export async function rulesDirect(args, log, context = {}) {
summary += `Skipped (default or protected): ${skipped.join(', ')}.`;
}
if (errors.length > 0) {
summary += errors.map(r => `Error removing ${r.brandName}: ${r.error}`).join(' ');
summary += errors
.map((r) => `Error removing ${r.brandName}: ${r.error}`)
.join(' ');
}
disableSilentMode();
return { success: errors.length === 0, data: { summary, results: removalResults } };
return {
success: errors.length === 0,
data: { summary, results: removalResults }
};
} else if (action === 'add') {
for (const brand of rules) {
if (!isValidBrand(brand)) {
addResults.push({ brandName: brand, success: false, error: `Profile not found: static import missing for '${brand}'. Valid brands: ${BRAND_NAMES.join(', ')}` });
addResults.push({
brandName: brand,
success: false,
error: `Profile not found: static import missing for '${brand}'. Valid brands: ${BRAND_NAMES.join(', ')}`
});
continue;
}
const profile = getBrandProfile(brand);
const result = convertAllRulesToBrandRules(projectRoot, profile);
addResults.push({ brandName: brand, ...result });
const { success, failed } = convertAllRulesToBrandRules(
projectRoot,
profile
);
// Determine paths
const rulesDir = profile.rulesDir;
const brandRulesDir = path.join(projectRoot, rulesDir);
const brandDir = path.dirname(brandRulesDir);
const mcpPath = path.join(brandDir, 'mcp.json');
// Check what was created
const mcpConfigCreated = fs.existsSync(mcpPath);
const rulesDirCreated = fs.existsSync(brandRulesDir);
const brandFolderCreated = fs.existsSync(brandDir);
const error =
failed > 0 ? `${failed} rule files failed to convert.` : null;
const resultObj = {
brandName: brand,
mcpConfigCreated,
rulesDirCreated,
brandFolderCreated,
skipped: false,
error,
success: mcpConfigCreated && rulesDirCreated && success > 0 && !error
};
addResults.push(resultObj);
}
const successes = addResults
.filter((r) => r.success)
.map((r) => r.brandName);
const errors = addResults.filter((r) => r.error && !r.success);
let summary = '';
if (successes.length > 0) {
summary += `Successfully added rules: ${successes.join(', ')}.`;
}
if (errors.length > 0) {
summary += errors
.map((r) => ` Error adding ${r.brandName}: ${r.error}`)
.join(' ');
}
disableSilentMode();
return { success: true, data: { results: addResults } };
return {
success: errors.length === 0,
data: { summary, results: addResults }
};
} else {
disableSilentMode();
return {

View File

@@ -73,7 +73,13 @@ import {
getApiKeyStatusReport
} from './task-manager/models.js';
import { findProjectRoot } from './utils.js';
import { convertAllRulesToBrandRules, removeBrandRules, BRAND_NAMES, isValidBrand, getBrandProfile } from './rule-transformer.js';
import {
convertAllRulesToBrandRules,
removeBrandRules,
BRAND_NAMES,
isValidBrand,
getBrandProfile
} from './rule-transformer.js';
/**
* Runs the interactive setup process for model configuration.
@@ -517,22 +523,30 @@ function registerCommands(programInstance) {
for (const brand of expandedBrands) {
if (!isValidBrand(brand)) {
console.warn(`Rules profile for brand "${brand}" not found. Valid brands: ${BRAND_NAMES.join(', ')}. Skipping.`);
console.warn(
`Rules profile for brand "${brand}" not found. Valid brands: ${BRAND_NAMES.join(', ')}. Skipping.`
);
continue;
}
const profile = getBrandProfile(brand);
if (action === 'add') {
convertAllRulesToBrandRules(projectDir, profile);
console.log(chalk.blue(`Adding rules for brand: ${brand}...`));
const addResult = convertAllRulesToBrandRules(projectDir, profile);
if (typeof profile.onAddBrandRules === 'function') {
profile.onAddBrandRules(projectDir);
}
console.log(chalk.blue(`Completed adding rules for brand: ${brand}`));
console.log(
chalk.green(
`Summary for ${brand}: ${addResult.success} rules added, ${addResult.failed} failed.`
)
);
} else if (action === 'remove') {
console.log(chalk.blue(`Removing rules for brand: ${brand}...`));
const result = removeBrandRules(projectDir, profile);
removalResults.push(result);
if (typeof profile.onRemoveBrandRules === 'function') {
profile.onRemoveBrandRules(projectDir);
}
console.log(chalk.blue(`Completed removal for brand: ${brand}`));
} else {
console.error('Unknown action. Use "add" or "remove".');
process.exit(1);
@@ -541,18 +555,30 @@ function registerCommands(programInstance) {
// Print summary for removals
if (action === 'remove') {
const successes = removalResults.filter(r => r.success).map(r => r.brandName);
const skipped = removalResults.filter(r => r.skipped).map(r => r.brandName);
const errors = removalResults.filter(r => r.error && !r.success && !r.skipped);
const successes = removalResults
.filter((r) => r.success)
.map((r) => r.brandName);
const skipped = removalResults
.filter((r) => r.skipped)
.map((r) => r.brandName);
const errors = removalResults.filter(
(r) => r.error && !r.success && !r.skipped
);
if (successes.length > 0) {
console.log(chalk.green(`Successfully removed rules: ${successes.join(', ')}`));
console.log(
chalk.green(`Successfully removed rules: ${successes.join(', ')}`)
);
}
if (skipped.length > 0) {
console.log(chalk.yellow(`Skipped (default or protected): ${skipped.join(', ')}`));
console.log(
chalk.yellow(
`Skipped (default or protected): ${skipped.join(', ')}`
)
);
}
if (errors.length > 0) {
errors.forEach(r => {
errors.forEach((r) => {
console.log(chalk.red(`Error removing ${r.brandName}: ${r.error}`));
});
}

View File

@@ -96,5 +96,4 @@ export function setupMCPConfiguration(configDir) {
fs.writeFileSync(mcpPath, JSON.stringify(newMCPConfig, null, 4));
log('success', 'Created MCP configuration file');
}
log('info', 'MCP server will use the installed task-master-ai package');
}

View File

@@ -150,7 +150,7 @@ function convertRuleToBrandRule(sourcePath, targetPath, profile) {
const { conversionConfig, brandName, globalReplacements } = profile;
try {
log(
'info',
'debug',
`Converting Cursor rule ${path.basename(sourcePath)} to ${brandName} rule ${path.basename(targetPath)}`
);
@@ -173,7 +173,7 @@ function convertRuleToBrandRule(sourcePath, targetPath, profile) {
// Write transformed content
fs.writeFileSync(targetPath, transformedContent);
log(
'success',
'debug',
`Successfully converted ${path.basename(sourcePath)} to ${path.basename(targetPath)}`
);
@@ -204,7 +204,7 @@ function convertAllRulesToBrandRules(projectDir, profile) {
// Ensure brand rules directory exists
if (!fs.existsSync(brandRulesDir)) {
fs.mkdirSync(brandRulesDir, { recursive: true });
log('info', `Created ${brandName} rules directory: ${brandRulesDir}`);
log('debug', `Created ${brandName} rules directory: ${brandRulesDir}`);
// Also create MCP configuration in the brand directory
const brandDir = path.dirname(brandRulesDir);
setupMCPConfiguration(brandDir);
@@ -233,7 +233,7 @@ function convertAllRulesToBrandRules(projectDir, profile) {
});
log(
'info',
'debug',
`Rule conversion complete: ${success} successful, ${failed} failed`
);
@@ -274,43 +274,57 @@ function removeBrandRules(projectDir, profile) {
} catch (e) {
const errorMessage = `Failed to remove MCP configuration at ${mcpPath}: ${e.message}`;
log('warn', errorMessage);
result.error = result.error ? `${result.error}; ${errorMessage}` : errorMessage;
result.error = result.error
? `${result.error}; ${errorMessage}`
: errorMessage;
}
}
if (brandName.toLowerCase() === 'cursor') {
const skipMessage = 'Cannot remove default Cursor rules directory. Skipping.';
log('warn', skipMessage);
result.skipped = true;
result.error = skipMessage;
return result; // Early exit for cursor brand
}
// Remove rules directory
if (fs.existsSync(brandRulesDir)) {
try {
fs.rmSync(brandRulesDir, { recursive: true, force: true });
result.rulesDirRemoved = true;
} catch (e) {
const errorMessage = `Failed to remove rules directory at ${brandRulesDir}: ${e.message}`;
log('warn', errorMessage);
result.error = result.error
? `${result.error}; ${errorMessage}`
: errorMessage;
}
}
if (
fs.existsSync(brandDir) &&
path.basename(brandDir) !== '.cursor' &&
fs.readdirSync(brandDir).length === 0
) {
// Remove brand folder if empty
if (fs.existsSync(brandDir) && fs.readdirSync(brandDir).length === 0) {
try {
fs.rmdirSync(brandDir);
result.brandFolderRemoved = true;
}
result.success = true; // Mark overall success if rules dir was removed
} catch (e) {
const errorMessage = `Failed to remove rules directory ${brandRulesDir} or brand folder ${brandDir}: ${e.message}`;
log('error', errorMessage); // Log as error since this is a primary operation failing
result.error = result.error ? `${result.error}; ${errorMessage}` : errorMessage;
const errorMessage = `Failed to remove empty brand folder at ${brandDir}: ${e.message}`;
log('warn', errorMessage);
result.error = result.error
? `${result.error}; ${errorMessage}`
: errorMessage;
}
} else {
const warnMessage = `Rules directory not found: ${brandRulesDir}`;
log('warn', warnMessage);
result.error = result.error ? `${result.error}; ${warnMessage}` : warnMessage;
// success remains false as the primary target (rulesDir) was not found
}
// Call onRemoveBrandRules hook if present
if (typeof profile.onRemoveBrandRules === 'function') {
try {
profile.onRemoveBrandRules(projectDir);
} catch (e) {
const errorMessage = `Error in onRemoveBrandRules for ${brandName}: ${e.message}`;
log('warn', errorMessage);
result.error = result.error
? `${result.error}; ${errorMessage}`
: errorMessage;
}
}
result.success =
result.mcpConfigRemoved ||
result.rulesDirRemoved ||
result.brandFolderRemoved;
return result;
}

View File

@@ -2,7 +2,7 @@
import { fileURLToPath } from 'url';
import path from 'path';
import fs from 'fs';
import { isSilentMode } from '../modules/utils.js';
import { isSilentMode, log } from '../modules/utils.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -127,7 +127,6 @@ export function onAddBrandRules(targetDir) {
const sourceDir = path.resolve(__dirname, '../../assets/roocode');
copyRecursiveSync(sourceDir, targetDir);
// Enhanced error checking and logging for .roomodes and rules-*mode* files
const rooModesDir = path.join(sourceDir, '.roo');
const rooModes = ['architect', 'ask', 'boomerang', 'code', 'debug', 'test'];
@@ -137,30 +136,28 @@ export function onAddBrandRules(targetDir) {
if (fs.existsSync(roomodesSrc)) {
try {
fs.copyFileSync(roomodesSrc, roomodesDest);
if (!isSilentMode()) console.log(`[Roo] Copied .roomodes to ${roomodesDest}`);
log('debug', `[Roo] Copied .roomodes to ${roomodesDest}`);
} catch (err) {
if (!isSilentMode()) console.warn(`[Roo] Failed to copy .roomodes: ${err.message}`);
log('debug', `[Roo] Failed to copy .roomodes: ${err.message}`);
}
} else {
if (!isSilentMode()) console.warn(`[Roo] .roomodes not found at ${roomodesSrc}`);
log('debug', `[Roo] .roomodes not found at ${roomodesSrc}`);
}
// Copy each <mode>-rules file into the corresponding .roo/rules-<mode>/ folder
for (const mode of rooModes) {
const src = path.join(rooModesDir, `rules-${mode}`, `${mode}-rules`);
const dest = path.join(targetDir, '.roo', `rules-${mode}`, `${mode}-rules`);
if (fs.existsSync(src)) {
try {
// Ensure destination directory exists
const destDir = path.dirname(dest);
if (!fs.existsSync(destDir)) fs.mkdirSync(destDir, { recursive: true });
fs.copyFileSync(src, dest);
if (!isSilentMode()) console.log(`[Roo] Copied ${src} to ${dest}`);
log('debug', `[Roo] Copied ${src} to ${dest}`);
} catch (err) {
if (!isSilentMode()) console.warn(`[Roo] Failed to copy ${src} to ${dest}: ${err.message}`);
log('debug', `[Roo] Failed to copy ${src} to ${dest}: ${err.message}`);
}
} else {
if (!isSilentMode()) console.warn(`[Roo] Roo rule file not found for mode '${mode}': ${src}`);
log('debug', `[Roo] Roo rule file not found for mode '${mode}': ${src}`);
}
}
}
@@ -183,36 +180,41 @@ function copyRecursiveSync(src, dest) {
}
export function onRemoveBrandRules(targetDir) {
const sourceDir = path.resolve(__dirname, '../../assets/roocode');
// Remove all files/folders that exist in assets/roocode from the project root
removeRecursiveSync(sourceDir, targetDir);
log('debug', `[Roo] onRemoveBrandRules called for ${targetDir}`);
const roomodesPath = path.join(targetDir, '.roomodes');
if (fs.existsSync(roomodesPath)) {
try {
fs.rmSync(roomodesPath, { force: true });
log('debug', `[Roo] Removed .roomodes from ${targetDir}`);
} catch (err) {
log('debug', `[Roo] Failed to remove .roomodes: ${err.message}`);
}
}
// After removing, check if .roo exists and is empty; if so, remove it
const rooDir = path.join(targetDir, '.roo');
if (fs.existsSync(rooDir) && isDirectoryEmpty(rooDir)) {
fs.rmSync(rooDir, { recursive: true, force: true });
if (fs.existsSync(rooDir)) {
fs.readdirSync(rooDir).forEach((entry) => {
if (entry.startsWith('rules-')) {
const modeDir = path.join(rooDir, entry);
try {
fs.rmSync(modeDir, { recursive: true, force: true });
log('debug', `[Roo] Removed ${modeDir}`);
} catch (err) {
log('debug', `[Roo] Failed to remove ${modeDir}: ${err.message}`);
}
}
function removeRecursiveSync(src, destRoot) {
if (!fs.existsSync(src)) return;
const stats = fs.statSync(src);
if (stats.isDirectory()) {
const destDir = path.join(destRoot, path.basename(src));
if (fs.existsSync(destDir)) {
fs.rmSync(destDir, { recursive: true, force: true });
}
// Also walk subfolders in src to remove nested structure
fs.readdirSync(src).forEach((child) => {
removeRecursiveSync(path.join(src, child), destRoot);
});
} else {
const destFile = path.join(destRoot, path.basename(src));
if (fs.existsSync(destFile)) {
fs.rmSync(destFile, { force: true });
if (fs.readdirSync(rooDir).length === 0) {
try {
fs.rmSync(rooDir, { recursive: true, force: true });
log('debug', `[Roo] Removed empty .roo directory`);
} catch (err) {
log('debug', `[Roo] Failed to remove .roo directory: ${err.message}`);
}
}
}
log('debug', `[Roo] onRemoveBrandRules completed for ${targetDir}`);
}
function isDirectoryEmpty(dirPath) {
return fs.readdirSync(dirPath).length === 0;