"use strict";
/**
 * YAML workflow generator — slim orchestrator for the compilation pipeline.
 *
 * Coordinates five stages:
 *   extract → analyze → compile → build → validate
 *
 * Each stage is a dedicated module in ./pipeline/ that receives and returns
 * a shared PipelineContext.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.capToolArguments = void 0;
exports.generateYamlFromExecution = generateYamlFromExecution;
const index_1 = require("../export/index");
const extract_1 = require("./pipeline/extract");
const analyze_1 = require("./pipeline/analyze");
const compile_1 = require("./pipeline/compile");
const build_1 = require("./pipeline/build");
const validate_1 = require("./pipeline/validate");
const utils_1 = require("../../modules/utils");
// Re-exports for backward compatibility
var build_2 = require("./pipeline/build");
Object.defineProperty(exports, "capToolArguments", { enumerable: true, get: function () { return build_2.capToolArguments; } });
/**
 * Generate a HotMesh YAML workflow from a completed execution's tool call sequence.
 *
 * Analyzes the execution events, extracts the ordered tool calls, compiles
 * them through an LLM-powered intent analysis, and produces a deterministic
 * YAML DAG that replaces the LLM with direct tool-to-tool piping.
 */
async function generateYamlFromExecution(options) {
    const appId = options.appId || 'longtail';
    const graphTopic = (0, utils_1.sanitizeToolName)(options.subscribes || options.name);
    // 1. Export the execution to get events
    const execution = await (0, index_1.exportWorkflowExecution)(options.workflowId, options.taskQueue, options.workflowName);
    // 2. Initialize pipeline context
    let ctx = {
        options: { ...options, appId, subscribes: graphTopic },
        execution,
        originalPrompt: '',
        rawSteps: [],
        collapsedSteps: [],
        patternAnnotations: [],
        naiveInputs: [],
        compilationPlan: null,
        coreSteps: [],
        refinedInputs: [],
        yaml: '',
        inputSchema: {},
        outputSchema: {},
        activityManifest: [],
        tags: [],
        category: 'general',
        validationIssues: [],
        priorDeployError: options.compilationFeedback || options.priorDeployError,
        priorFailedYaml: options.priorFailedYaml,
    };
    // 3. Run pipeline stages
    ctx = await (0, extract_1.extract)(ctx);
    ctx = await (0, analyze_1.analyze)(ctx);
    ctx = await (0, compile_1.compile)(ctx);
    ctx = await (0, build_1.build)(ctx);
    ctx = await (0, validate_1.validate)(ctx);
    // 4. Log validation warnings (informational — do not recompile)
    if (ctx.validationIssues.length > 0) {
        const { loggerRegistry } = await Promise.resolve().then(() => __importStar(require('../../lib/logger')));
        loggerRegistry.warn(`[yaml-workflow] Validation warnings (${ctx.validationIssues.length}): ${ctx.validationIssues.join('; ')}`);
    }
    // 5. Return result
    return {
        yaml: ctx.yaml,
        inputSchema: ctx.inputSchema,
        outputSchema: ctx.outputSchema,
        activityManifest: ctx.activityManifest,
        graphTopic,
        appId,
        tags: ctx.tags,
        inputFieldMeta: ctx.refinedInputs,
        originalPrompt: ctx.originalPrompt,
        category: ctx.category,
        compilationPlan: ctx.compilationPlan,
        validationIssues: ctx.validationIssues,
    };
}
