"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.AskUserHandler = void 0;
const common_1 = require("@nestjs/common");
const ask_user_args_dto_1 = require("./dto/ask-user.args.dto");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const utils_1 = require("../../utils");
let AskUserHandler = class AskUserHandler {
    constructor() {
        this.toolName = 'ask_user';
    }
    getMetadata() {
        return {
            name: this.toolName,
            description: this.getDefinition(),
            arguments: [
                {
                    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: true,
                },
            ],
        };
    }
    generateExample(toolCall) {
        return (0, utils_1.generateToolCallJson)(toolCall);
    }
    getDefinition() {
        const example = this.generateExample({
            tool_name: this.toolName,
            selections: '[{"question":"Which database should we use?","selections":["Postgres","MySQL","SQLite"]}]',
        });
        const simpleExample = this.generateExample({
            tool_name: this.toolName,
            selections: '[{"question":"Should I proceed with the refactoring?","selections":["yes","no","skip"]}]',
        });
        const definition = `
<${this.toolName}>
  Ask the user a question. Use this when you need user input before continuing. The agent loop will pause and wait for the user's response.

  Parameters:
  - "selections": (string, required) JSON array of selection groups. Each group is {"question":"...","selections":["a","b","c"]}. The user will see clickable buttons for each selection.

  <example>
${example}
  </example>

  <example>
${simpleExample}
  </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)(ask_user_args_dto_1.AskUserArgsDto, 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: 'Waiting for user response.',
            persisted_args: validatedArgs,
            execution_log: {
                output: 'Waiting for user response.',
                error_message: '',
            },
        };
    }
};
exports.AskUserHandler = AskUserHandler;
exports.AskUserHandler = AskUserHandler = __decorate([
    (0, common_1.Injectable)()
], AskUserHandler);
//# sourceMappingURL=ask-user.handler.js.map