@hivetechs/hive-ai
Version:
Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API
181 lines • 5.1 kB
JavaScript
/**
* Logging utility for Hive.AI
*
* Provides a consistent logging interface with support for different verbosity levels
* and command execution tracking
*/
// Log levels
export var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["QUIET"] = 0] = "QUIET";
LogLevel[LogLevel["ERROR"] = 1] = "ERROR";
LogLevel[LogLevel["WARN"] = 2] = "WARN";
LogLevel[LogLevel["INFO"] = 3] = "INFO";
LogLevel[LogLevel["DEBUG"] = 4] = "DEBUG";
LogLevel[LogLevel["TRACE"] = 5] = "TRACE"; // Show everything including trace data
})(LogLevel || (LogLevel = {}));
// Default log level - QUIET by default
let currentLogLevel = LogLevel.QUIET;
let showProviderErrors = false;
// Command tracking
let commandStartTime = 0;
let currentCommandName = '';
let commandInProgress = false;
/**
* Start tracking a command execution
*/
export function startCommand(commandName) {
commandInProgress = true;
commandStartTime = Date.now();
currentCommandName = commandName;
// Only show in INFO or higher
if (currentLogLevel >= LogLevel.INFO) {
console.log(`▶️ Starting command: ${commandName}...`);
}
}
/**
* End tracking a command execution and show completion message
*/
export function endCommand(success = true) {
if (commandInProgress) {
const duration = ((Date.now() - commandStartTime) / 1000).toFixed(2);
// Always show completion message regardless of log level
if (success) {
console.log(`\n✅ Command '${currentCommandName}' completed successfully in ${duration}s`);
}
else {
console.error(`\n❌ Command '${currentCommandName}' failed after ${duration}s`);
}
commandInProgress = false;
}
}
/**
* Set the global log level
*/
export function setLogLevel(level) {
currentLogLevel = level;
if (level > LogLevel.QUIET) {
console.log(`Log level set to: ${LogLevel[level]}`);
}
}
/**
* Set whether to show detailed provider errors
*/
export function setShowProviderErrors(show) {
showProviderErrors = show;
if (currentLogLevel >= LogLevel.INFO) {
console.log(`Show provider errors set to: ${show}`);
}
}
/**
* Log a message if the current log level is sufficient
*/
export function log(level, message, error) {
if (level <= currentLogLevel) {
const prefix = getLogPrefix(level);
if (level <= LogLevel.ERROR) {
console.error(`${prefix} ${message}`);
if (error && showProviderErrors) {
console.error(formatError(error));
}
}
else if (level === LogLevel.WARN) {
console.warn(`${prefix} ${message}`);
}
else {
console.log(`${prefix} ${message}`);
}
}
}
/**
* Display a result message that will always be shown regardless of log level
*/
export function showResult(message) {
console.log(message);
}
/**
* Shorthand logging methods
*/
export const logger = {
error: (message, error) => log(LogLevel.ERROR, message, error),
warn: (message) => log(LogLevel.WARN, message),
info: (message) => log(LogLevel.INFO, message),
debug: (message) => log(LogLevel.DEBUG, message),
trace: (message) => log(LogLevel.TRACE, message),
result: (message) => showResult(message),
startCommand,
endCommand
};
/**
* Get the prefix for a log level
*/
function getLogPrefix(level) {
switch (level) {
case LogLevel.ERROR:
return "❌";
case LogLevel.WARN:
return "⚠️";
case LogLevel.INFO:
return "ℹ️";
case LogLevel.DEBUG:
return "🔍";
case LogLevel.TRACE:
return "📋";
default:
return "";
}
}
/**
* Format an error for logging
*/
function formatError(error) {
if (!error)
return "";
// If it's an API error with a structured response
if (error.error && typeof error.error === 'object') {
return `API Error: ${error.error.message || 'Unknown API error'}`;
}
// If it has a message property
if (error.message) {
return `Error: ${error.message}`;
}
// If it's a string
if (typeof error === 'string') {
return `Error: ${error}`;
}
// Otherwise, stringify the object
try {
return `Error: ${JSON.stringify(error)}`;
}
catch (e) {
return `Error: [Cannot stringify error object]`;
}
}
/**
* Format a list of topics for display
*/
export function formatTopicList(topics) {
if (!topics || topics.length === 0) {
return 'No topics found';
}
return topics.join(', ');
}
/**
* Format a category name for display
*/
function formatCategoryName(name) {
return name
.split('_')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
/**
* Format a topic name for display
*/
function formatTopicName(name) {
return name
.split('_')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
//# sourceMappingURL=logging.js.map