"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FinalHandler = void 0;
const common_1 = require("@nestjs/common");
const final_args_dto_1 = require("./dto/final.args.dto");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const utils_1 = require("../../utils");
let FinalHandler = class FinalHandler {
    constructor() {
        this.toolName = 'final';
    }
    getMetadata() {
        return {
            name: this.toolName,
            description: this.getDefinition(),
            arguments: [
                {
                    name: 'plain',
                    type: 'string',
                    description: 'A final concluding message or summary for user.',
                    required: false,
                },
                {
                    name: 'selections',
                    type: 'string',
                    description: 'JSON array of selection groups. Each group is {"question":"...","selections":["a","b","c"]}. Renders as clickable buttons in the UI.',
                    required: false,
                },
            ],
        };
    }
    generateExample(toolCall) {
        return (0, utils_1.generateToolCallJson)(toolCall);
    }
    getDefinition() {
        const example = this.generateExample({
            tool_name: this.toolName,
            plain: 'The task is complete. I have refactored the component and added the new feature.',
        });
        const selectionsExample = this.generateExample({
            tool_name: this.toolName,
            plain: 'I need some decisions before proceeding with the implementation.',
            selections: '[{"question":"Which API style should we use?","selections":["REST","GraphQL"]},{"question":"Which database?","selections":["Postgres","MySQL","SQLite"]}]',
        });
        const definition = `
<${this.toolName}>
  Marks the end of a plan. This should be the last action in a sequence when the task is complete.

  Parameters:
  - "plain": (string, optional) A final concluding message or summary for the user.
  - "selections": (string, optional) JSON array of selection groups. Each group is {"question":"...","selections":["a","b","c"]}. The user will see clickable buttons for each selection. Use this when you need the user to make a choice before continuing.

  <example>
:${example}
  </example>

  <example>
${selectionsExample}
  </example>
</${this.toolName}>
`;
        return definition.trim();
    }
    async execute(args, context) {
        if (args.selections && typeof args.selections !== 'string') {
            args = { ...args, selections: JSON.stringify(args.selections) };
        }
        const validatedArgs = (0, class_transformer_1.plainToClass)(final_args_dto_1.FinalArgsDto, args);
        const errors = await (0, class_validator_1.validate)(validatedArgs);
        if (errors.length > 0) {
            const errorMessages = errors
                .map((err) => Object.values(err.constraints || {}).join(', '))
                .join('; ');
            return {
                status: 'FAILURE',
                summary: `Invalid arguments for ${this.toolName}.`,
                error_message: errorMessages,
                persisted_args: args,
                execution_log: { output: '', error_message: errorMessages },
            };
        }
        context.flags.is_final = true;
        return {
            status: 'SUCCESS',
            summary: 'Plan marked as final.',
            persisted_args: validatedArgs,
            execution_log: { output: 'Task completed.', error_message: '' },
        };
    }
};
exports.FinalHandler = FinalHandler;
exports.FinalHandler = FinalHandler = __decorate([
    (0, common_1.Injectable)()
], FinalHandler);
//# sourceMappingURL=final.handler.js.map