From 97b1e48cfd540a852cb0fcccc6d76659655d59b8 Mon Sep 17 00:00:00 2001 From: radmehr Date: Fri, 23 Oct 2026 19:19:40 +0330 Subject: [PATCH] feat: add development openrouter and fix changes --- .../providers/open-router.service.ts | 9 ++++++++- src/config/environment.validation.ts | 6 +++--- .../chatbot/providers/chatbot.service.ts | 20 +++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/common/modules/open-router/providers/open-router.service.ts b/src/common/modules/open-router/providers/open-router.service.ts index 6a8a42f..0d15800 100644 --- a/src/common/modules/open-router/providers/open-router.service.ts +++ b/src/common/modules/open-router/providers/open-router.service.ts @@ -27,6 +27,13 @@ export class OpenRouterService implements OnModuleDestroy { const { ProxyAgent, fetch: undiciFetch } = await import('undici'); const { HTTPClient, OpenRouter } = await import('@openrouter/sdk'); + const env = this.configService.get('app.environment'); + const apiKey = this.configService.getOrThrow('openRouter.apiKey'); + + if (env !== 'production') { + return new OpenRouter({ apiKey }); + } + const proxyUrl = this.configService.getOrThrow( 'openRouter.proxyUrl', ); @@ -66,7 +73,7 @@ export class OpenRouterService implements OnModuleDestroy { }); return new OpenRouter({ - apiKey: this.configService.getOrThrow('openRouter.apiKey'), + apiKey, httpClient, }); } catch (error) { diff --git a/src/config/environment.validation.ts b/src/config/environment.validation.ts index f7d41f4..7bea519 100644 --- a/src/config/environment.validation.ts +++ b/src/config/environment.validation.ts @@ -39,9 +39,9 @@ export default Joi.object({ // OpenRouter OPENROUTER_API_KEY: Joi.string().required(), OPENROUTER_MODEL: Joi.string().required(), - OPENROUTER_PROXY_URL: Joi.string().required(), - OPENROUTER_PROXY_USERNAME: Joi.string().required(), - OPENROUTER_PROXY_PASSWORD: Joi.string().required(), + OPENROUTER_PROXY_URL: Joi.string().optional(), + OPENROUTER_PROXY_USERNAME: Joi.string().optional(), + OPENROUTER_PROXY_PASSWORD: Joi.string().optional(), // Admin Panel (AdminJS) ADMIN_PANEL_ENABLED: Joi.string().valid('true', 'false').default('true'), diff --git a/src/modules/chatbot/providers/chatbot.service.ts b/src/modules/chatbot/providers/chatbot.service.ts index 69963e2..b83b843 100644 --- a/src/modules/chatbot/providers/chatbot.service.ts +++ b/src/modules/chatbot/providers/chatbot.service.ts @@ -6,6 +6,7 @@ import { Conversation } from '@/modules/conversation/entities/conversation.entit import { ConversationService } from '@/modules/conversation/providers/conversation.service'; import { MessageRole } from '@/modules/conversation/enums/message-role.enum'; import { OpenRouterService } from '@/common/modules/open-router/providers/open-router.service'; +import { SendChatCompletionRequestResponse } from '@openrouter/sdk/models/operations'; @Injectable() export class ChatbotService { @@ -77,12 +78,19 @@ export class ChatbotService { const openRouter = await this.openRouterService.getOpenRouter(); - const response = await openRouter.chat.send({ - chatRequest: { - model: this.configService.getOrThrow('openRouter.model'), - messages, - }, - }); + let response: SendChatCompletionRequestResponse; + + try { + response = await openRouter.chat.send({ + chatRequest: { + model: this.configService.getOrThrow('openRouter.model'), + messages, + }, + }); + } catch (err) { + console.error('[ChatbotService] OpenRouter request failed:', err); + throw err; + } if (response instanceof ReadableStream) { throw new Error('Expected non-streaming response');