"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceAiModule = void 0;
const backend_common_1 = require("@n8n/backend-common");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const YELLOW = '\x1b[33m';
const CLEAR = '\x1b[0m';
const WARNING_MESSAGE = "[Instance AI] 'instance-ai' module is experimental, undocumented and subject to change. " +
    'Before its official release any features may become inaccessible at any point, ' +
    'and using the module could compromise the stability of your system. Use at your own risk!';
let InstanceAiModule = class InstanceAiModule {
    async init() {
        const logger = di_1.Container.get(backend_common_1.Logger).scoped('instance-ai');
        logger.warn(`${YELLOW}${WARNING_MESSAGE}${CLEAR}`);
        const { InstanceAiSettingsService } = await Promise.resolve().then(() => __importStar(require('./instance-ai-settings.service')));
        await di_1.Container.get(InstanceAiSettingsService).loadFromDb();
        await Promise.resolve().then(() => __importStar(require('./instance-ai.controller')));
        const { InstanceAiMemoryService } = await Promise.resolve().then(() => __importStar(require('./instance-ai-memory.service')));
        const { InstanceAiService } = await Promise.resolve().then(() => __importStar(require('./instance-ai.service')));
        const aiService = di_1.Container.get(InstanceAiService);
        void di_1.Container.get(InstanceAiMemoryService)
            .cleanupExpiredThreads(async (threadId) => await aiService.clearThreadState(threadId))
            .catch(() => undefined);
        const { SnapshotPruningService } = await Promise.resolve().then(() => __importStar(require('./snapshot-pruning.service')));
        di_1.Container.get(SnapshotPruningService).init();
    }
    async settings() {
        const { InstanceAiService } = await Promise.resolve().then(() => __importStar(require('./instance-ai.service')));
        const { InstanceAiSettingsService } = await Promise.resolve().then(() => __importStar(require('./instance-ai-settings.service')));
        const service = di_1.Container.get(InstanceAiService);
        const settingsService = di_1.Container.get(InstanceAiSettingsService);
        const enabled = service.isEnabled();
        const localGatewayDisabled = settingsService.isLocalGatewayDisabled();
        const optinModalDismissed = settingsService.getAdminSettings().optinModalDismissed;
        return {
            enabled,
            localGatewayDisabled,
            proxyEnabled: service.isProxyEnabled(),
            optinModalDismissed,
        };
    }
    async entities() {
        const { InstanceAiThread } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-thread.entity')));
        const { InstanceAiMessage } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-message.entity')));
        const { InstanceAiResource } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-resource.entity')));
        const { InstanceAiObservationalMemory } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-observational-memory.entity')));
        const { InstanceAiWorkflowSnapshot } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-workflow-snapshot.entity')));
        const { InstanceAiRunSnapshot } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-run-snapshot.entity')));
        const { InstanceAiIterationLog } = await Promise.resolve().then(() => __importStar(require('./entities/instance-ai-iteration-log.entity')));
        return [
            InstanceAiThread,
            InstanceAiMessage,
            InstanceAiResource,
            InstanceAiObservationalMemory,
            InstanceAiWorkflowSnapshot,
            InstanceAiRunSnapshot,
            InstanceAiIterationLog,
        ];
    }
    async shutdown() {
        const { InstanceAiService } = await Promise.resolve().then(() => __importStar(require('./instance-ai.service')));
        await di_1.Container.get(InstanceAiService).shutdown();
    }
};
exports.InstanceAiModule = InstanceAiModule;
__decorate([
    (0, decorators_1.OnShutdown)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], InstanceAiModule.prototype, "shutdown", null);
exports.InstanceAiModule = InstanceAiModule = __decorate([
    (0, decorators_1.BackendModule)({ name: 'instance-ai', instanceTypes: ['main'] })
], InstanceAiModule);
//# sourceMappingURL=instance-ai.module.js.map