"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var FollowUpPostExecutionHook_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FollowUpPostExecutionHook = void 0;
const common_1 = require("@nestjs/common");
const session_inputs_service_1 = require("../../session-inputs/session-inputs.service");
const sessions_service_1 = require("../../sessions/sessions.service");
let FollowUpPostExecutionHook = FollowUpPostExecutionHook_1 = class FollowUpPostExecutionHook {
    constructor(sessionInputsService, sessionsService) {
        this.sessionInputsService = sessionInputsService;
        this.sessionsService = sessionsService;
        this.logger = new common_1.Logger(FollowUpPostExecutionHook_1.name);
    }
    async run(sessionInput, context) {
        if (context.flags.should_halt_hooks) {
            this.logger.log('Follow-up hook skipped because hook chain was halted.');
            return;
        }
        if (context.flags.is_final) {
            this.logger.log('Plan marked as final. No follow-up will be initiated.');
            return;
        }
        if (context.toolResults.length > 0) {
            this.logger.log(`Triggering follow-up with ${context.toolResults.length} tool result(s)`);
            let contextTemplateId;
            const session = await this.sessionsService.findOne(sessionInput.session_id);
            if (session?.default_followup_context_template_id) {
                contextTemplateId = session.default_followup_context_template_id;
                this.logger.log(`Using follow-up context template ${contextTemplateId} for session ${sessionInput.session_id}`);
            }
            await this.sessionInputsService.create(sessionInput.session_id, {
                user_prompt: '',
                execution_strategy: sessionInput.execution_strategy,
                skip_persistence: true,
                ad_hoc_context_definition: undefined,
                context_template_id: contextTemplateId,
            });
            context.flags.follow_up_initiated = true;
            return;
        }
        this.logger.log('No tool results - no follow-up needed.');
    }
};
exports.FollowUpPostExecutionHook = FollowUpPostExecutionHook;
exports.FollowUpPostExecutionHook = FollowUpPostExecutionHook = FollowUpPostExecutionHook_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => session_inputs_service_1.SessionInputsService))),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => sessions_service_1.SessionsService))),
    __metadata("design:paramtypes", [session_inputs_service_1.SessionInputsService,
        sessions_service_1.SessionsService])
], FollowUpPostExecutionHook);
//# sourceMappingURL=follow-up-post-execution.hook.js.map