From 9a7b5a36727c53f80c192418178c4e86e134b8bc Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 29 May 2025 19:30:16 +0100 Subject: [PATCH 1/7] fix(api): resolve CLI removing settings --- api/src/app.instance.ts | 6 +++++- api/src/extension/extension.module.ts | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/api/src/app.instance.ts b/api/src/app.instance.ts index c83df405..0447bb09 100644 --- a/api/src/app.instance.ts +++ b/api/src/app.instance.ts @@ -9,7 +9,7 @@ import { INestApplication } from '@nestjs/common'; export class AppInstance { - private static app: INestApplication; + private static app: INestApplication | null = null; static setApp(app: INestApplication) { this.app = app; @@ -21,4 +21,8 @@ export class AppInstance { } return this.app; } + + static isReady(): boolean { + return this.app !== null; + } } diff --git a/api/src/extension/extension.module.ts b/api/src/extension/extension.module.ts index 8d43c8ab..52fcc0a5 100644 --- a/api/src/extension/extension.module.ts +++ b/api/src/extension/extension.module.ts @@ -8,6 +8,7 @@ import { Global, Module, OnApplicationBootstrap } from '@nestjs/common'; +import { AppInstance } from '@/app.instance'; import { LoggerService } from '@/logger/logger.service'; import { CleanupService } from './cleanup.service'; @@ -24,6 +25,9 @@ export class ExtensionModule implements OnApplicationBootstrap { ) {} async onApplicationBootstrap() { + if (!AppInstance.isReady()) { + return; + } try { await this.cleanupService.pruneExtensionSettings(); } catch (error) { From 15ed7efa2d4f2b0a12c12974297fe01576ac95b1 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 30 May 2025 07:01:13 +0100 Subject: [PATCH 2/7] fix(api): resolve loading attachment onApplicationBootstrap logic from the CLI --- api/src/attachment/attachment.module.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/src/attachment/attachment.module.ts b/api/src/attachment/attachment.module.ts index c3fadbb6..b53f775a 100644 --- a/api/src/attachment/attachment.module.ts +++ b/api/src/attachment/attachment.module.ts @@ -12,6 +12,7 @@ import { Module, OnApplicationBootstrap } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { PassportModule } from '@nestjs/passport'; +import { AppInstance } from '@/app.instance'; import { config } from '@/config'; import { UserModule } from '@/user/user.module'; @@ -34,6 +35,9 @@ import { AttachmentService } from './services/attachment.service'; }) export class AttachmentModule implements OnApplicationBootstrap { onApplicationBootstrap() { + if (!AppInstance.isReady()) { + return; + } // Ensure the directories exists if (!existsSync(config.parameters.uploadDir)) { mkdirSync(config.parameters.uploadDir, { recursive: true }); From 26166e05e4ad273d8a97ac08336b79e6b60d2fa2 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 30 May 2025 13:33:46 +0100 Subject: [PATCH 3/7] fix(api): add support to llm-nlu helper --- api/src/attachment/attachment.module.ts | 1 + api/src/extension/extension.module.ts | 1 + api/src/extensions/helpers/llm-nlu/index.helper.ts | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/api/src/attachment/attachment.module.ts b/api/src/attachment/attachment.module.ts index b53f775a..77966bf0 100644 --- a/api/src/attachment/attachment.module.ts +++ b/api/src/attachment/attachment.module.ts @@ -38,6 +38,7 @@ export class AttachmentModule implements OnApplicationBootstrap { if (!AppInstance.isReady()) { return; } + // Ensure the directories exists if (!existsSync(config.parameters.uploadDir)) { mkdirSync(config.parameters.uploadDir, { recursive: true }); diff --git a/api/src/extension/extension.module.ts b/api/src/extension/extension.module.ts index 52fcc0a5..94ae5782 100644 --- a/api/src/extension/extension.module.ts +++ b/api/src/extension/extension.module.ts @@ -28,6 +28,7 @@ export class ExtensionModule implements OnApplicationBootstrap { if (!AppInstance.isReady()) { return; } + try { await this.cleanupService.pruneExtensionSettings(); } catch (error) { diff --git a/api/src/extensions/helpers/llm-nlu/index.helper.ts b/api/src/extensions/helpers/llm-nlu/index.helper.ts index 82241399..a9eef495 100644 --- a/api/src/extensions/helpers/llm-nlu/index.helper.ts +++ b/api/src/extensions/helpers/llm-nlu/index.helper.ts @@ -10,6 +10,7 @@ import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; import Handlebars from 'handlebars'; +import { AppInstance } from '@/app.instance'; import { HelperService } from '@/helper/helper.service'; import BaseNlpHelper from '@/helper/lib/base-nlp-helper'; import { HelperType, LLM, NLU } from '@/helper/types'; @@ -89,6 +90,10 @@ export default class LlmNluHelper } async onApplicationBootstrap() { + if (!AppInstance.isReady()) { + return; + } + try { this.logger.log('Initializing LLM NLU helper, building prompts...'); // Build prompts for language and trait classifiers From d906f350d1c5d14152e6838579c4985290ff79ca Mon Sep 17 00:00:00 2001 From: Yassine <95612053+yassinedorbozgithub@users.noreply.github.com> Date: Fri, 30 May 2025 15:28:59 +0100 Subject: [PATCH 4/7] Update api/src/extensions/helpers/llm-nlu/index.helper.ts Co-authored-by: Med Marrouchi --- api/src/extensions/helpers/llm-nlu/index.helper.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/api/src/extensions/helpers/llm-nlu/index.helper.ts b/api/src/extensions/helpers/llm-nlu/index.helper.ts index a9eef495..521f0393 100644 --- a/api/src/extensions/helpers/llm-nlu/index.helper.ts +++ b/api/src/extensions/helpers/llm-nlu/index.helper.ts @@ -91,6 +91,7 @@ export default class LlmNluHelper async onApplicationBootstrap() { if (!AppInstance.isReady()) { + // bypass in Test / CLI env return; } From 2d89f3eb2d893f600a111e79d46757e4c95202d8 Mon Sep 17 00:00:00 2001 From: Yassine <95612053+yassinedorbozgithub@users.noreply.github.com> Date: Fri, 30 May 2025 15:29:20 +0100 Subject: [PATCH 5/7] Update api/src/extension/extension.module.ts Co-authored-by: Med Marrouchi --- api/src/extension/extension.module.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/api/src/extension/extension.module.ts b/api/src/extension/extension.module.ts index 94ae5782..6aeb28a3 100644 --- a/api/src/extension/extension.module.ts +++ b/api/src/extension/extension.module.ts @@ -26,6 +26,7 @@ export class ExtensionModule implements OnApplicationBootstrap { async onApplicationBootstrap() { if (!AppInstance.isReady()) { + // bypass in test or CLI env return; } From 8a50bce083ef759f25d6a7412b5b82912dbe4af1 Mon Sep 17 00:00:00 2001 From: Yassine <95612053+yassinedorbozgithub@users.noreply.github.com> Date: Fri, 30 May 2025 15:29:32 +0100 Subject: [PATCH 6/7] Update api/src/app.instance.ts Co-authored-by: Med Marrouchi --- api/src/app.instance.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/src/app.instance.ts b/api/src/app.instance.ts index 0447bb09..ec39b20e 100644 --- a/api/src/app.instance.ts +++ b/api/src/app.instance.ts @@ -22,6 +22,11 @@ export class AppInstance { return this.app; } + /** + * Checks whether the application context is initialized. + * This may return `false` in environments where the app instance is not set, + * such as when running in test env or CLI mode without a full application bootstrap. + */ static isReady(): boolean { return this.app !== null; } From 3e8532e28737d98f8925560c02e272037a28c241 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 30 May 2025 15:33:15 +0100 Subject: [PATCH 7/7] fix(api): resolve lint issues --- api/src/app.instance.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/src/app.instance.ts b/api/src/app.instance.ts index ec39b20e..5c3760c1 100644 --- a/api/src/app.instance.ts +++ b/api/src/app.instance.ts @@ -23,10 +23,10 @@ export class AppInstance { } /** - * Checks whether the application context is initialized. - * This may return `false` in environments where the app instance is not set, - * such as when running in test env or CLI mode without a full application bootstrap. - */ + * Checks whether the application context is initialized. + * This may return `false` in environments where the app instance is not set, + * such as when running in test env or CLI mode without a full application bootstrap. + */ static isReady(): boolean { return this.app !== null; }