"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.orchestrateAnalyze = void 0;
const uuid_1 = require("uuid");
const executeCachedBatch_1 = require("../../utils/executeCachedBatch");
const orchestrateAnalyzeReview_1 = require("./orchestrateAnalyzeReview");
const orchestrateAnalyzeScenario_1 = require("./orchestrateAnalyzeScenario");
const orchestrateAnalyzeWrite_1 = require("./orchestrateAnalyzeWrite");
const orchestrateAnalyze = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    // Initialize analysis state
    const step = ((_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : -1) + 1;
    const startTime = new Date();
    ctx.dispatch({
        type: "analyzeStart",
        id: (0, uuid_1.v7)(),
        reason: props.instruction,
        step,
        created_at: startTime.toISOString(),
    });
    // Generate analysis scenario
    const scenario = yield (0, orchestrateAnalyzeScenario_1.orchestrateAnalyzeScenario)(ctx, props.instruction);
    if (scenario.type === "assistantMessage")
        return ctx.assistantMessage(scenario);
    else
        ctx.dispatch(scenario);
    // write documents
    const writeProgress = {
        total: scenario.files.length,
        completed: 0,
    };
    const fileList = yield (0, executeCachedBatch_1.executeCachedBatch)(scenario.files.map((file) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () {
        const event = yield (0, orchestrateAnalyzeWrite_1.orchestrateAnalyzeWrite)(ctx, {
            scenario,
            file,
            progress: writeProgress,
            promptCacheKey,
            instruction: props.instruction,
        });
        return event.file;
    })));
    // review documents
    const reviewProgress = {
        total: fileList.length,
        completed: 0,
    };
    const newFiles = yield (0, executeCachedBatch_1.executeCachedBatch)(fileList.map((file) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () {
        try {
            const event = yield (0, orchestrateAnalyzeReview_1.orchestrateAnalyzeReview)(ctx, {
                scenario,
                allFiles: fileList, // all files
                myFile: file,
                progress: reviewProgress,
                promptCacheKey,
            });
            return Object.assign(Object.assign({}, event.file), { content: event.content });
        }
        catch (_a) {
            return file;
        }
    })));
    // Complete the analysis
    return ctx.dispatch({
        type: "analyzeComplete",
        id: (0, uuid_1.v7)(),
        roles: scenario.roles,
        prefix: scenario.prefix,
        files: newFiles,
        step,
        elapsed: new Date().getTime() - startTime.getTime(),
        created_at: new Date().toISOString(),
    });
});
exports.orchestrateAnalyze = orchestrateAnalyze;
//# sourceMappingURL=orchestrateAnalyze.js.map