diff --git a/Max API/src/llm/openrouter.service.spec.ts b/Max API/src/llm/openrouter.service.spec.ts index c3da222..4e96a22 100644 --- a/Max API/src/llm/openrouter.service.spec.ts +++ b/Max API/src/llm/openrouter.service.spec.ts @@ -30,6 +30,15 @@ describe('OpenRouterService', () => { (global.fetch as jest.Mock).mockImplementationOnce(impl); } + function getLastRequestedModel(): string { + const mock = global.fetch as jest.Mock< + Promise | Response, + [string, { body: string }] + >; + const [, init] = mock.mock.calls[mock.mock.calls.length - 1]; + return (JSON.parse(init.body) as { model: string }).model; + } + describe('chat()', () => { it('should return assistant message when the API responds successfully', async () => { mockFetchOnce( @@ -108,5 +117,35 @@ describe('OpenRouterService', () => { service.chat(1, [{ role: 'user', content: 'Salut' }]), ).rejects.toThrow(/malformee/); }); + + it('should append the ":online" suffix to the model when options.online is true', async () => { + mockFetchOnce( + () => + new Response( + JSON.stringify({ choices: [{ message: { content: 'ok' } }] }), + { status: 200 }, + ), + ); + + await service.chat(1, [{ role: 'user', content: 'Salut' }], { + online: true, + }); + + expect(getLastRequestedModel()).toBe('deepseek/deepseek-chat:online'); + }); + + it('should NOT append the ":online" suffix when options.online is absent', async () => { + mockFetchOnce( + () => + new Response( + JSON.stringify({ choices: [{ message: { content: 'ok' } }] }), + { status: 200 }, + ), + ); + + await service.chat(1, [{ role: 'user', content: 'Salut' }]); + + expect(getLastRequestedModel()).toBe('deepseek/deepseek-chat'); + }); }); }); diff --git a/Max API/src/llm/openrouter.service.ts b/Max API/src/llm/openrouter.service.ts index 1fc081c..2f14602 100644 --- a/Max API/src/llm/openrouter.service.ts +++ b/Max API/src/llm/openrouter.service.ts @@ -42,7 +42,7 @@ export class OpenRouterService { async chat( userId: number, messages: ChatMessage[], - options: { jsonResponse?: boolean } = {}, + options: { jsonResponse?: boolean; online?: boolean } = {}, ): Promise { const apiKey = await this.apiKeysService.getDecryptedValue( userId, @@ -52,7 +52,12 @@ export class OpenRouterService { let lastError = 'raison inconnue'; for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { - const model = attempt % 2 === 0 ? PRIMARY_MODEL : FALLBACK_MODEL; + const baseModel = attempt % 2 === 0 ? PRIMARY_MODEL : FALLBACK_MODEL; + // Le suffixe ":online" (fonctionnalite native OpenRouter) fait effectuer une vraie + // recherche web au modele avant de repondre : utilise par les agents de recherche + // (cartographie, recherche approfondie, veille) pour respecter l'interdiction de generer + // des sources depuis la seule memoire du modele (voir docs/Max Refonte Coach Freelance.md). + const model = options.online ? `${baseModel}:online` : baseModel; let response: Response; try { response = await fetch(OPENROUTER_URL, {