Develop #2

Open
w-vandal wants to merge 48 commits from develop into main
2 changed files with 46 additions and 2 deletions
Showing only changes of commit b5cdc30207 - Show all commits
@@ -30,6 +30,15 @@ describe('OpenRouterService', () => {
(global.fetch as jest.Mock).mockImplementationOnce(impl); (global.fetch as jest.Mock).mockImplementationOnce(impl);
} }
function getLastRequestedModel(): string {
const mock = global.fetch as jest.Mock<
Promise<Response> | 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()', () => { describe('chat()', () => {
it('should return assistant message when the API responds successfully', async () => { it('should return assistant message when the API responds successfully', async () => {
mockFetchOnce( mockFetchOnce(
@@ -108,5 +117,35 @@ describe('OpenRouterService', () => {
service.chat(1, [{ role: 'user', content: 'Salut' }]), service.chat(1, [{ role: 'user', content: 'Salut' }]),
).rejects.toThrow(/malformee/); ).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');
});
}); });
}); });
+7 -2
View File
@@ -42,7 +42,7 @@ export class OpenRouterService {
async chat( async chat(
userId: number, userId: number,
messages: ChatMessage[], messages: ChatMessage[],
options: { jsonResponse?: boolean } = {}, options: { jsonResponse?: boolean; online?: boolean } = {},
): Promise<string> { ): Promise<string> {
const apiKey = await this.apiKeysService.getDecryptedValue( const apiKey = await this.apiKeysService.getDecryptedValue(
userId, userId,
@@ -52,7 +52,12 @@ export class OpenRouterService {
let lastError = 'raison inconnue'; let lastError = 'raison inconnue';
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { 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; let response: Response;
try { try {
response = await fetch(OPENROUTER_URL, { response = await fetch(OPENROUTER_URL, {