feat(llm): add OpenRouter ":online" web-search option
Agents 2, 4 and the future veille service must not hallucinate sources from the model's memory (docs/Max Refonte Coach Freelance.md). OpenRouter's native ":online" model suffix performs real web search grounding with no custom tool-calling plumbing needed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
85da9fcad0
commit
b5cdc30207
@@ -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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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, {
|
||||||
|
|||||||
Reference in New Issue
Block a user