"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 InvalidToolFeedbackHook_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvalidToolFeedbackHook = void 0;
const common_1 = require("@nestjs/common");
const chat_service_1 = require("../../interactive-chat/chat.service");
const application_state_service_1 = require("../../application-state/application-state.service");
const session_inputs_service_1 = require("../../session-inputs/session-inputs.service");
let InvalidToolFeedbackHook = InvalidToolFeedbackHook_1 = class InvalidToolFeedbackHook {
    constructor(chatService, applicationStateService, sessionInputsService) {
        this.chatService = chatService;
        this.applicationStateService = applicationStateService;
        this.sessionInputsService = sessionInputsService;
        this.logger = new common_1.Logger(InvalidToolFeedbackHook_1.name);
    }
    async run(sessionInput, executionContext) {
        const invalidTools = executionContext.feedback.invalidToolErrors;
        if (!invalidTools || invalidTools.length === 0) {
            return;
        }
        if (executionContext.toolResults &&
            executionContext.toolResults.length > 0) {
            this.logger.log(`Found ${invalidTools.length} invalid tool(s), but native tool calling is active. ` +
                `Errors will flow through toolResults. FollowUpPostExecutionHook will handle the follow-up.`);
            return;
        }
        this.logger.log(`Found ${invalidTools.length} invalid tool(s). Sending feedback to AI.`);
        const toolNames = invalidTools.map((e) => `'${e.tool_name}'`).join(', ');
        const feedbackMessage = `SYSTEM_FEEDBACK: The previous response included tools that are not valid. The following tool names could not be found: ${toolNames}. Please review the available tools and provide a corrected plan using only valid tool names. Do not apologize, just provide the corrected plan.`;
        const feedbackDto = {
            user_prompt: feedbackMessage,
            execution_strategy: sessionInput.execution_strategy,
            ad_hoc_context_definition: undefined,
            context_template_id: undefined,
        };
        try {
            await this.sessionInputsService.create(sessionInput.session_id, feedbackDto);
            executionContext.flags.should_halt_hooks = true;
        }
        catch (error) {
            this.logger.error(`Failed to send invalid tool feedback to AI for session ${sessionInput.session_id}`, error.stack);
        }
    }
};
exports.InvalidToolFeedbackHook = InvalidToolFeedbackHook;
exports.InvalidToolFeedbackHook = InvalidToolFeedbackHook = InvalidToolFeedbackHook_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => chat_service_1.ChatService))),
    __metadata("design:paramtypes", [chat_service_1.ChatService,
        application_state_service_1.ApplicationStateService,
        session_inputs_service_1.SessionInputsService])
], InvalidToolFeedbackHook);
//# sourceMappingURL=invalid-tool-feedback.hook.js.map