"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubAgentEvalService = void 0;
const backend_common_1 = require("@n8n/backend-common");
const di_1 = require("@n8n/di");
const instance_ai_1 = require("@n8n/instance-ai");
const node_crypto_1 = require("node:crypto");
const instance_ai_adapter_service_1 = require("../instance-ai.adapter.service");
const instance_ai_service_1 = require("../instance-ai.service");
const sub_agent_roles_1 = require("./sub-agent-roles");
const DEFAULT_TIMEOUT_MS = 120_000;
let SubAgentEvalService = class SubAgentEvalService {
    constructor(adapterService, instanceAiService, logger) {
        this.adapterService = adapterService;
        this.instanceAiService = instanceAiService;
        this.logger = logger.scoped('sub-agent-eval');
    }
    async run(user, request) {
        const startMs = Date.now();
        const role = (0, sub_agent_roles_1.resolveSubAgentRole)(request.role);
        const maxSteps = request.maxSteps ?? role.defaultMaxSteps;
        const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
        const capturedWorkflowIds = [];
        const baseContext = this.adapterService.createContext(user);
        const context = {
            ...baseContext,
            workflowService: this.wrapWorkflowService(baseContext.workflowService, capturedWorkflowIds),
        };
        const tools = (0, instance_ai_1.createAllTools)(context);
        const modelId = request.modelId ?? (await this.instanceAiService.resolveAgentModelConfig(user));
        const agentId = `eval-${role.label}-${(0, node_crypto_1.randomUUID)()}`;
        const agent = (0, instance_ai_1.createSubAgent)({
            agentId,
            role: role.label,
            instructions: role.systemPrompt,
            tools,
            modelId,
        });
        const abortController = new AbortController();
        const timeoutError = new Error(`Sub-agent timed out after ${String(timeoutMs)}ms`);
        const timeoutId = setTimeout(() => abortController.abort(timeoutError), timeoutMs);
        try {
            const result = await agent.generate(request.prompt, {
                maxSteps,
                abortSignal: abortController.signal,
            });
            return {
                text: result.text ?? '',
                toolCalls: serializeToolCalls(result.toolCalls ?? []),
                toolResults: serializeToolResults(result.toolResults ?? []),
                capturedWorkflowIds,
                durationMs: Date.now() - startMs,
                ...(typeof result.finishReason === 'string' ? { stopReason: result.finishReason } : {}),
            };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            this.logger.warn('sub-agent eval run failed', { error: message, agentId });
            return {
                text: '',
                toolCalls: [],
                toolResults: [],
                capturedWorkflowIds,
                durationMs: Date.now() - startMs,
                error: message,
            };
        }
        finally {
            clearTimeout(timeoutId);
        }
    }
    wrapWorkflowService(original, capturedIds) {
        const capture = (id) => {
            if (!capturedIds.includes(id))
                capturedIds.push(id);
        };
        return {
            ...original,
            createFromWorkflowJSON: async (json) => {
                const detail = await original.createFromWorkflowJSON(json);
                capture(detail.id);
                return detail;
            },
            updateFromWorkflowJSON: async (workflowId, json) => {
                const detail = await original.updateFromWorkflowJSON(workflowId, json);
                capture(detail.id);
                return detail;
            },
        };
    }
};
exports.SubAgentEvalService = SubAgentEvalService;
exports.SubAgentEvalService = SubAgentEvalService = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [instance_ai_adapter_service_1.InstanceAiAdapterService,
        instance_ai_service_1.InstanceAiService,
        backend_common_1.Logger])
], SubAgentEvalService);
function serializeToolCalls(raw) {
    return raw.map((tc) => {
        const payload = tc.payload;
        return {
            toolName: payload?.toolName ?? 'unknown',
            args: payload?.args,
        };
    });
}
function serializeToolResults(raw) {
    return raw.map((tr) => {
        const payload = tr.payload;
        const result = payload?.result;
        const isError = typeof result === 'object' &&
            result !== null &&
            'success' in result &&
            result.success === false;
        return {
            toolName: payload?.toolName ?? 'unknown',
            result,
            isError,
        };
    });
}
//# sourceMappingURL=sub-agent-eval.service.js.map