agente-toolkit
Version:
A barebones TypeScript library for building AI agents with intelligent tool execution and self-correction capabilities
127 lines (123 loc) • 4.3 kB
JavaScript
;
var winston = require('winston');
class AgentLogger {
constructor(config = { level: 'info', verbose: false }) {
this.isVerbose = false;
this.isVerbose = config.verbose;
const transports = [
new winston.transports.Console({
format: winston.format.combine(winston.format.timestamp({ format: 'HH:mm:ss' }), winston.format.colorize(), winston.format.printf(({ timestamp, level, message, ...meta }) => {
let output = `${timestamp} [${level}]: ${message}`;
// Add metadata in verbose mode
if (this.isVerbose && Object.keys(meta).length > 0) {
output += '\n' + JSON.stringify(meta, null, 2);
}
return output;
})),
}),
];
if (config.enableFileLogging) {
transports.push(new winston.transports.File({
filename: config.logFile || 'agent.log',
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
}));
}
this.logger = winston.createLogger({
level: config.level,
transports,
});
}
setVerbose(verbose) {
this.isVerbose = verbose;
}
// Core logging methods
error(message, meta) {
this.logger.error(message, meta);
}
warn(message, meta) {
this.logger.warn(message, meta);
}
info(message, meta) {
this.logger.info(message, meta);
}
debug(message, meta) {
this.logger.debug(message, meta);
}
// Specialized logging methods for agent operations
logPrompt(prompt, context) {
this.debug('Sending prompt to model', {
prompt: this.isVerbose ? prompt : `${prompt.substring(0, 100)}...`,
context,
});
}
logModelResponse(response, meta) {
this.debug('Received model response', {
response: this.isVerbose ? response : `${response.substring(0, 100)}...`,
...meta,
});
}
logPlanCreation(message, tools, plan) {
this.info('Created execution plan', {
userMessage: message,
availableTools: tools.map(t => t.name),
planSteps: plan.steps.length,
plan: this.isVerbose ? plan : undefined,
});
}
logToolExecution(toolName, params, result, duration) {
this.info(`Executed tool: ${toolName}`, {
tool: toolName,
params: this.isVerbose ? params : Object.keys(params),
result: this.isVerbose ? result : `${String(result).substring(0, 50)}...`,
duration: duration ? `${duration}ms` : undefined,
});
}
logParameterResolution(stepId, originalParams, resolvedParams) {
if (this.isVerbose) {
this.debug(`Resolved parameters for ${stepId}`, {
stepId,
original: originalParams,
resolved: resolvedParams,
});
}
}
logMemoryOperation(operation, details) {
this.debug(`Memory operation: ${operation}`, details);
}
logValidationError(toolName, errors) {
this.warn(`Parameter validation failed for ${toolName}`, {
tool: toolName,
errors,
});
}
logAgentStart(agentType) {
this.info('Agent session started', { agentType });
}
logAgentEnd() {
this.info('Agent session ended');
}
// Orchestration-focused helpers
logRunStart(meta) {
this.info('Run started', meta);
}
logRunEnd(meta) {
this.info('Run ended', meta);
}
logStepStart(stepId, toolName, meta) {
this.debug(`Step started: ${stepId}`, { stepId, toolName, ...meta });
}
logStepEnd(stepId, toolName, durationMs, meta) {
this.info(`Step ended: ${stepId}`, { stepId, toolName, duration: durationMs, ...meta });
}
}
// Create a singleton instance
let loggerInstance = null;
function getLogger() {
if (!loggerInstance) {
loggerInstance = new AgentLogger({ level: 'info', verbose: false });
}
return loggerInstance;
}
exports.AgentLogger = AgentLogger;
exports.getLogger = getLogger;
//# sourceMappingURL=logger.js.map