"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.loggingMiddleware = void 0;
exports.createLoggingMiddleware = createLoggingMiddleware;
/**
 * Logging Middleware
 *
 * 自动记录所有钩子事件的中间件
 */
const hookTypes_1 = require("../hookTypes");
const DEFAULT_CONFIG = {
    level: 'info',
    logData: false,
    logToConsole: true,
    excludeEvents: [],
};
// =============================================================================
// Logging Middleware
// =============================================================================
/**
 * 创建日志中间件
 */
function createLoggingMiddleware(config = {}) {
    const finalConfig = { ...DEFAULT_CONFIG, ...config };
    const handler = async (event, context) => {
        // 检查是否排除此事件类型
        if (finalConfig.excludeEvents?.includes(event.type)) {
            return hookTypes_1.HOOK_CONTINUE;
        }
        const timestamp = new Date(event.timestamp).toISOString();
        const prefix = `[Hook:${event.type}:${event.phase}]`;
        const sessionInfo = `session=${context.sessionId.slice(0, 8)}`;
        let message = `${prefix} ${timestamp} ${sessionInfo}`;
        // 根据事件类型添加更多信息
        switch (event.type) {
            case 'tool:use':
                message += ` tool=${event.data.toolName}`;
                break;
            case 'subagent:start':
            case 'subagent:complete':
            case 'subagent:error':
                message += ` agent=${event.data.agentName} stage=${event.data.stageId}`;
                break;
            case 'session:start':
            case 'session:end':
                if (event.data.traceId) {
                    message += ` trace=${event.data.traceId.slice(0, 8)}`;
                }
                break;
        }
        // 记录日志
        const logFn = finalConfig.logger || console.log;
        if (finalConfig.logToConsole) {
            switch (finalConfig.level) {
                case 'debug':
                    console.debug(message);
                    break;
                case 'info':
                    console.log(message);
                    break;
                case 'warn':
                    console.warn(message);
                    break;
                case 'error':
                    console.error(message);
                    break;
            }
        }
        if (finalConfig.logger) {
            logFn(message, finalConfig.logData ? event.data : undefined);
        }
        // 记录到 context metadata 供后续使用
        const logKey = `log:${event.type}:${event.phase}:${event.timestamp}`;
        context.set(logKey, { message, data: event.data });
        return hookTypes_1.HOOK_CONTINUE;
    };
    return {
        name: 'logging',
        events: [
            'tool:use',
            'subagent:start',
            'subagent:complete',
            'subagent:error',
            'session:start',
            'session:end',
            'session:checkpoint',
            'session:error',
            'iteration:start',
            'iteration:end',
        ],
        phases: ['pre', 'post'],
        priority: 1000, // 低优先级，最后执行
        handler,
        enabled: true,
    };
}
/**
 * 默认的日志中间件实例
 */
exports.loggingMiddleware = createLoggingMiddleware();
//# sourceMappingURL=loggingMiddleware.js.map