"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 GenerateTitleHandler_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateTitleHandler = void 0;
const common_1 = require("@nestjs/common");
const generate_title_args_dto_1 = require("./dto/generate-title.args.dto");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const utils_1 = require("../../utils");
const sessions_service_1 = require("../../sessions/sessions.service");
let GenerateTitleHandler = GenerateTitleHandler_1 = class GenerateTitleHandler {
    constructor(sessionsService) {
        this.sessionsService = sessionsService;
        this.toolName = 'generate_title';
        this.logger = new common_1.Logger(GenerateTitleHandler_1.name);
    }
    getMetadata() {
        return {
            name: this.toolName,
            description: this.getDefinition(),
            arguments: [
                {
                    name: 'title',
                    type: 'string',
                    description: 'A concise session title (3-5 words) that summarizes the task.',
                    required: true,
                },
            ],
        };
    }
    generateExample(toolCall) {
        return (0, utils_1.generateToolCallJson)(toolCall);
    }
    getDefinition() {
        const example = this.generateExample({
            tool_name: this.toolName,
            title: 'Fix authentication bug',
        });
        const definition = `
<${this.toolName}>
  Updates the session title to a concise, descriptive name.
  Use this tool when the current session needs a title or when the task scope has changed significantly.
  The title must be **3-5 words** - brief but informative.

  Parameters:
  - "title": (string) A concise title for the session (3-5 words).

  <example>
    Explanation: Set a descriptive title for a debugging session.
:${example}
  </example>
</${this.toolName}>
`;
        return definition.trim();
    }
    async execute(args, context) {
        const validatedArgs = (0, class_transformer_1.plainToClass)(generate_title_args_dto_1.GenerateTitleArgsDto, 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 },
            };
        }
        try {
            await this.sessionsService.update(context.session_id, {
                session_title: validatedArgs.title,
            });
            this.logger.log(`Session title updated to: "${validatedArgs.title}" for session ${context.session_id}`);
            return {
                status: 'SUCCESS',
                summary: `Session title updated to: "${validatedArgs.title}"`,
                persisted_args: validatedArgs,
                execution_log: {
                    output: `Session title set to: ${validatedArgs.title}`,
                    error_message: '',
                },
            };
        }
        catch (error) {
            this.logger.error(`Failed to update session title: ${error.message}`);
            return {
                status: 'FAILURE',
                summary: 'Failed to update session title.',
                error_message: error.message,
                persisted_args: args,
                execution_log: { output: '', error_message: error.message },
            };
        }
    }
};
exports.GenerateTitleHandler = GenerateTitleHandler;
exports.GenerateTitleHandler = GenerateTitleHandler = GenerateTitleHandler_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [sessions_service_1.SessionsService])
], GenerateTitleHandler);
//# sourceMappingURL=generate-title.handler.js.map