"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 LlmOutputParserService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LlmOutputParserService = void 0;
const common_1 = require("@nestjs/common");
let LlmOutputParserService = LlmOutputParserService_1 = class LlmOutputParserService {
    constructor() {
        this.logger = new common_1.Logger(LlmOutputParserService_1.name);
    }
    async parse(rawResponse, toolCalls) {
        if (toolCalls && toolCalls.length > 0) {
            this.logger.log(`Parsing ${toolCalls.length} native tool calls.`);
            return this.parseNativeToolCalls(rawResponse, toolCalls);
        }
        const explanation = rawResponse && rawResponse.trim() !== '' ? rawResponse.trim() : null;
        return { explanation, actions: [] };
    }
    parseNativeToolCalls(explanation, toolCalls) {
        const actions = [];
        for (const toolCall of toolCalls) {
            const { id, function: fn } = toolCall;
            const { name, arguments: args } = fn;
            try {
                const parsedArgs = typeof args === 'string' ? JSON.parse(args) : args;
                actions.push({
                    tool_name: name,
                    arguments: parsedArgs,
                    toolCallId: id,
                });
            }
            catch (error) {
                this.logger.warn(`Skipping malformed tool call "${name}" (id: ${id}): ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        return {
            explanation: explanation || null,
            actions,
        };
    }
};
exports.LlmOutputParserService = LlmOutputParserService;
exports.LlmOutputParserService = LlmOutputParserService = LlmOutputParserService_1 = __decorate([
    (0, common_1.Injectable)()
], LlmOutputParserService);
//# sourceMappingURL=llm-output-parser.service.js.map