#!/usr/bin/env node
import { fetchZenrusData, getCacheInfo } from "./api.js";
// Цвета для консоли
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
green: "\x1b[32m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
yellow: "\x1b[33m",
red: "\x1b[31m",
};
function logHeader(text: string) {
console.log(`\n${colors.bright}${colors.blue}${"=".repeat(60)}${colors.reset}`);
console.log(`${colors.bright}${colors.blue} ${text}${colors.reset}`);
console.log(`${colors.bright}${colors.blue}${"=".repeat(60)}${colors.reset}\n`);
}
function logTool(name: string, description: string, value: string) {
console.log(`${colors.cyan}Инструмент:${colors.reset} ${colors.bright}${name}${colors.reset}`);
console.log(`${colors.yellow}Описание:${colors.reset} ${description}`);
console.log(`${colors.green}Значение:${colors.reset} ${colors.bright}${value}${colors.reset}`);
console.log();
}
async function debugFetch() {
try {
logHeader("Отладка MCP-сервера Zenrus");
console.log(`${colors.cyan}Выполняется запрос к https://zenrus.ru/...${colors.reset}\n`);
const startTime = Date.now();
const data = await fetchZenrusData();
const endTime = Date.now();
const cacheInfo = getCacheInfo();
const cacheStatus = cacheInfo.isCached
? `из кеша (возраст: ${Math.floor(cacheInfo.age! / 1000)}s)`
: "из удаленного API";
console.log(`${colors.green}✓ Данные успешно получены за ${endTime - startTime}ms ${cacheStatus}${colors.reset}\n`);
logHeader("Полученные данные");
console.log(JSON.stringify(data, null, 2));
console.log();
logHeader("Результаты работы MCP-инструментов (структурированные данные)");
// Эмулируем вывод каждого инструмента с новым форматом
const usdResult = {
rate: data.usd !== "N/A" ? parseFloat(data.usd) : null,
currency: "USD/RUB",
description: "US Dollar to Russian Ruble exchange rate",
};
logTool(
"get_usd_rate",
"Получить текущий курс доллара США в рублях",
JSON.stringify(usdResult, null, 2)
);
const eurResult = {
rate: data.eur !== "N/A" ? parseFloat(data.eur) : null,
currency: "EUR/RUB",
description: "Euro to Russian Ruble exchange rate",
};
logTool(
"get_eur_rate",
"Получить текущий курс евро в рублях",
JSON.stringify(eurResult, null, 2)
);
const brentUsdResult = {
price: data.brent !== "N/A" ? parseFloat(data.brent) : null,
commodity: "Brent Crude Oil",
currency: "USD",
unit: "per barrel",
};
logTool(
"get_brent_usd_rate",
"Получить текущую цену барреля нефти Brent в долларах",
JSON.stringify(brentUsdResult, null, 2)
);
const brentRubResult = {
price: data.brentRub !== "N/A" ? parseFloat(data.brentRub) : null,
commodity: "Brent Crude Oil",
currency: "RUB",
unit: "per barrel",
};
logTool(
"get_brent_rub_rate",
"Получить текущую цену барреля нефти Brent в рублях",
JSON.stringify(brentRubResult, null, 2)
);
logHeader("Расчетные инструменты (примеры)");
// Примеры расчетов
const testAmount = 100000;
const calcRubResult = {
amount: testAmount,
currency: "RUB",
barrels: parseFloat((testAmount / parseFloat(data.brentRub)).toFixed(4)),
pricePerBarrel: parseFloat(data.brentRub),
commodity: "Brent Crude Oil",
};
logTool(
"calculate_barrels_for_rub",
`Сколько баррелей нефти можно купить за ${testAmount} рублей`,
JSON.stringify(calcRubResult, null, 2)
);
const calcUsdResult = {
amount: 1000,
currency: "USD",
barrels: parseFloat((1000 / parseFloat(data.brent)).toFixed(4)),
pricePerBarrel: parseFloat(data.brent),
commodity: "Brent Crude Oil",
};
logTool(
"calculate_barrels_for_usd",
"Сколько баррелей нефти можно купить за 1000 долларов",
JSON.stringify(calcUsdResult, null, 2)
);
const eurAmount = 1000;
const amountInRub = eurAmount * parseFloat(data.eur);
const amountInUsd = amountInRub / parseFloat(data.usd);
const barrels = amountInUsd / parseFloat(data.brent);
const calcEurResult = {
amount: eurAmount,
currency: "EUR",
barrels: parseFloat(barrels.toFixed(4)),
pricePerBarrel: parseFloat((amountInRub / barrels).toFixed(2)),
pricePerBarrelCurrency: "RUB",
commodity: "Brent Crude Oil",
};
logTool(
"calculate_barrels_for_eur",
"Сколько баррелей нефти можно купить за 1000 евро",
JSON.stringify(calcEurResult, null, 2)
);
logHeader("Статистика");
console.log(`${colors.cyan}Всего инструментов:${colors.reset} ${colors.bright}7${colors.reset}`);
console.log(`${colors.cyan}Базовых инструментов:${colors.reset} ${colors.bright}4${colors.reset}`);
console.log(`${colors.cyan}Расчетных инструментов:${colors.reset} ${colors.bright}3${colors.reset}`);
console.log(`${colors.cyan}Ошибок:${colors.reset} ${colors.bright}${colors.green}0${colors.reset}`);
console.log();
// Проверка на N/A значения
const hasErrors = Object.values(data).some((value) => value === "N/A");
if (hasErrors) {
console.log(`${colors.red}${colors.bright}⚠ ВНИМАНИЕ: Некоторые данные не были получены (N/A)${colors.reset}`);
console.log(`${colors.yellow}Это может означать изменение структуры HTML на zenrus.ru${colors.reset}\n`);
} else {
console.log(`${colors.green}${colors.bright}✓ Все данные успешно получены и распарсены!${colors.reset}\n`);
}
} catch (error) {
console.error(`${colors.red}${colors.bright}✗ Ошибка при получении данных:${colors.reset}`);
if (error instanceof Error) {
console.error(`${colors.red}${error.message}${colors.reset}`);
if (error.stack) {
console.error(`\n${colors.yellow}Стек ошибки:${colors.reset}`);
console.error(error.stack);
}
} else {
console.error(String(error));
}
process.exit(1);
}
}
// Запускаем отладку
debugFetch();