"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomCommandsModule = void 0;
const BaseChatModule_1 = require("./BaseChatModule");
const config_1 = require("../../config");
class CustomCommandsModule extends BaseChatModule_1.BaseChatModule {
    name = "custom-commands";
    description = "Dynamically registered commands from language config /command keys";
    getCommands() {
        return [];
    }
    getPlugins() {
        return this.chatService.getTools()?.getContext()?.Plugins;
    }
    async initialize(service) {
        this.chatService = service;
        try {
            const languageConfig = await (0, config_1.getLanguageConfig)();
            const commandKeys = Object.keys(languageConfig).filter((key) => key.trim().startsWith("/"));
            for (const commandKey of commandKeys) {
                const commandName = commandKey.trim().slice(1);
                console.log(`CUSTOM-COMMANDS: Registering command /${commandName} from language config`);
                const command = {
                    name: commandName,
                    description: `Custom command: ${commandKey}`,
                    modes: ["agent:attached", "agent"],
                    handler: async (args) => {
                        const config = languageConfig[commandKey];
                        const result = await this.getPlugins().call("language", commandKey);
                        if (config.handled === true) {
                            try {
                                console.log(result);
                                return {
                                    handled: true,
                                    contents: result,
                                };
                            }
                            catch (error) {
                                console.error(`Error executing command ${commandKey}:`, error);
                                return { handled: true };
                            }
                        }
                        return {
                            handled: false,
                            contents: commandKey + " => " + result,
                        };
                    },
                };
                this.chatService.registerCommand(command);
            }
        }
        catch (error) {
            console.error("CUSTOM-COMMANDS: Error loading language config:", error);
        }
    }
}
exports.CustomCommandsModule = CustomCommandsModule;
//# sourceMappingURL=CustomCommandsModule.js.map