"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.HtmlFinalHandler = void 0;
const common_1 = require("@nestjs/common");
const html_final_args_dto_1 = require("./dto/html-final.args.dto");
const class_transformer_1 = require("class-transformer");
const class_validator_1 = require("class-validator");
const utils_1 = require("../../utils");
let HtmlFinalHandler = class HtmlFinalHandler {
    constructor() {
        this.toolName = 'html_final';
    }
    getMetadata() {
        return {
            name: this.toolName,
            description: this.getDefinition(),
            arguments: [
                {
                    name: 'html',
                    type: 'string',
                    description: 'Complete HTML document (including <!DOCTYPE html>, <html>, <head>, <body>) to render in a sandboxed iframe. May include <style> and <script> tags for rich interactive content.',
                    required: true,
                },
                {
                    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 below the HTML content.',
                    required: false,
                },
            ],
        };
    }
    generateExample(toolCall) {
        return (0, utils_1.generateToolCallJson)(toolCall);
    }
    getDefinition() {
        const example = this.generateExample({
            tool_name: this.toolName,
            html: '<!DOCTYPE html>\n<html>\n<head>\n<style>\n  table { border-collapse: collapse; width: 100%; }\n  th, td { border: 1px solid #ddd; padding: 8px; }\n  th { background-color: #f2f2f2; }\n</style>\n</head>\n<body>\n  <h1>Analysis Report</h1>\n  <table>\n    <tr><th>Metric</th><th>Value</th></tr>\n    <tr><td>Files</td><td>42</td></tr>\n  </table>\n</body>\n</html>',
        });
        const selectionsExample = this.generateExample({
            tool_name: this.toolName,
            html: '<!DOCTYPE html>\n<html>\n<head>\n<style>\n  body { font-family: sans-serif; padding: 1rem; }\n  .option { padding: 0.5rem; margin: 0.5rem 0; border: 1px solid #ccc; border-radius: 4px; }\n</style>\n</head>\n<body>\n  <h2>Choose your approach</h2>\n  <div class="option">REST API</div>\n  <div class="option">GraphQL</div>\n</body>\n</html>',
            selections: '[{"question":"Which approach?","selections":["REST API","GraphQL","gRPC"]}]',
        });
        const definition = `
<${this.toolName}>
  Marks the end of a plan and renders a complete HTML document to the user. This should be the last action in a sequence when the task is complete and the result is best presented as rich HTML (tables, charts, styled markup, interactive content).

  Parameters:
  - "html": (string, required) A complete HTML document (including <!DOCTYPE html>, <html>, <head>, <body>). Rendered in a sandboxed iframe. You may include <style> and <script> tags.
  - "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 below the HTML content. 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)(html_final_args_dto_1.HtmlFinalArgsDto, 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 with HTML content.',
            persisted_args: {
                content: validatedArgs.html,
                selections: validatedArgs.selections,
            },
            execution_log: {
                output: 'Task completed with HTML output.',
                error_message: '',
            },
        };
    }
};
exports.HtmlFinalHandler = HtmlFinalHandler;
exports.HtmlFinalHandler = HtmlFinalHandler = __decorate([
    (0, common_1.Injectable)()
], HtmlFinalHandler);
//# sourceMappingURL=html-final.handler.js.map