Commit 5c76bcef by PLN (Algolia)

fix: retry transient local-server failures, surface server errors, name…

fix: retry transient local-server failures, surface server errors, name reasoning-ignored models, skip embeddings in auto pick
Co-Authored-By: 's avatarClaude Sonnet 5.5 <noreply@anthropic.com>
parent f6059afe
...@@ -82,7 +82,34 @@ export function buildPrompt(text, result) { ...@@ -82,7 +82,34 @@ export function buildPrompt(text, result) {
].join('\n'); ].join('\n');
} }
async function chat(url, model, prompt, timeoutMs) { const RETRY_DELAYS_MS = [2000, 4000];
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
/** Le message d'erreur du serveur (OpenAI-compatible : {error:{message}}), sinon vide. */
async function serverMessage(res) {
const data = await res.json().catch(() => null);
const m = data?.error?.message;
return typeof m === 'string' ? m : '';
}
/**
* LM Studio décroche par moments (chargement annulé, connexion coupée) :
* 3 essais, attentes de 2 s puis 4 s. Un délai dépassé ne se rejoue pas — le
* modèle charge peut-être, relancer ne ferait que l'allonger.
*/
async function chat(url, model, prompt, timeoutMs, delays) {
delays ??= RETRY_DELAYS_MS;
for (let attempt = 0; ; attempt++) {
try {
return await chatOnce(url, model, prompt, timeoutMs);
} catch (e) {
if (!(e instanceof FixError) || !e.transient || attempt >= delays.length) throw e;
await sleep(delays[attempt]);
}
}
}
async function chatOnce(url, model, prompt, timeoutMs) {
let res; let res;
try { try {
res = await fetch(`${url.replace(/\/+$/, '')}/chat/completions`, { res = await fetch(`${url.replace(/\/+$/, '')}/chat/completions`, {
...@@ -107,16 +134,32 @@ async function chat(url, model, prompt, timeoutMs) { ...@@ -107,16 +134,32 @@ async function chat(url, model, prompt, timeoutMs) {
} catch (e) { } catch (e) {
if (e?.name === 'TimeoutError' || e?.cause?.code === 'UND_ERR_CONNECT_TIMEOUT') if (e?.name === 'TimeoutError' || e?.cause?.code === 'UND_ERR_CONNECT_TIMEOUT')
throw new FixError(`délai dépassé (${Math.round(timeoutMs / 1000)} s) — le modèle local charge peut-être`); throw new FixError(`délai dépassé (${Math.round(timeoutMs / 1000)} s) — le modèle local charge peut-être`);
throw new FixError( const err = new FixError(
`le serveur local ne répond pas sur ${url} — démarre LM Studio (Developer → Start Server) ou règle BUVARD_FIX_URL`, `le serveur local ne répond pas sur ${url} — démarre LM Studio (Developer → Start Server) ou règle BUVARD_FIX_URL`,
{ cause: e }, { cause: e },
); );
err.transient = true;
throw err;
}
if (!res.ok) {
const detail = await serverMessage(res);
const err = new FixError(
`le serveur local a répondu ${res.status} ${res.statusText}${detail ? ` — ${detail}` : ''}`,
);
// 5xx, et le 400 « Failed to load model » de LM Studio, sont passagers.
err.transient = res.status >= 500 || /failed to load|operation canceled/i.test(detail);
throw err;
} }
if (!res.ok) throw new FixError(`le serveur local a répondu ${res.status} ${res.statusText}`);
const data = await res.json().catch(() => null); const data = await res.json().catch(() => null);
const content = data?.choices?.[0]?.message?.content; const message = data?.choices?.[0]?.message;
if (typeof content !== 'string' || !content.trim()) const content = message?.content;
if (typeof content !== 'string' || !content.trim()) {
if (typeof message?.reasoning_content === 'string' && message.reasoning_content.trim())
throw new FixError(
`le modèle ${model} raisonne malgré reasoning_effort=low / enable_thinking=false et n'a rendu aucun contenu — choisis un modèle sans raisonnement (BUVARD_FIX_MODEL)`,
);
throw new FixError('le modèle local n\'a renvoyé aucun contenu'); throw new FixError('le modèle local n\'a renvoyé aucun contenu');
}
return content; return content;
} }
...@@ -145,7 +188,8 @@ async function pickModel(url, timeoutMs) { ...@@ -145,7 +188,8 @@ async function pickModel(url, timeoutMs) {
.map((m) => m?.id) .map((m) => m?.id)
.filter((id) => typeof id === 'string' && id.length > 0); .filter((id) => typeof id === 'string' && id.length > 0);
if (ids.length === 1) return ids[0]; if (ids.length === 1) return ids[0];
const plain = ids.find((id) => !looksReasoning(id)); const usable = ids.filter((id) => !/embed/i.test(id) && !looksReasoning(id));
const plain = usable.find((id) => /instruct|(^|[-_/])it($|[-_])/i.test(id)) ?? usable[0];
if (!plain) if (!plain)
throw new FixError( throw new FixError(
`aucun modèle hors raisonnement sur ${url} (${ids.join(', ') || 'liste vide'}) — fixe BUVARD_FIX_MODEL pour choisir`, `aucun modèle hors raisonnement sur ${url} (${ids.join(', ') || 'liste vide'}) — fixe BUVARD_FIX_MODEL pour choisir`,
...@@ -271,7 +315,7 @@ export async function fixText(text, opts = {}) { ...@@ -271,7 +315,7 @@ export async function fixText(text, opts = {}) {
if (improved && model === null) model = await pickModel(url, timeoutMs); if (improved && model === null) model = await pickModel(url, timeoutMs);
for (let round = 1; improved && round <= maxRounds; round++) { for (let round = 1; improved && round <= maxRounds; round++) {
const here = lint(bodyOf(current)); const here = lint(bodyOf(current));
const content = await chat(url, model, buildPrompt(current, here), timeoutMs); const content = await chat(url, model, buildPrompt(current, here), timeoutMs, opts.retryDelaysMs);
const suggestions = parseSuggestions(content); const suggestions = parseSuggestions(content);
const { applied, dropped, text: next } = verifyAndApply(current, suggestions); const { applied, dropped, text: next } = verifyAndApply(current, suggestions);
const after = lint(bodyOf(next)); const after = lint(bodyOf(next));
......
...@@ -239,3 +239,83 @@ test('the payload always carries the reasoning-off fields, explicit model includ ...@@ -239,3 +239,83 @@ test('the payload always carries the reasoning-off fields, explicit model includ
assert.equal(payload.reasoning_effort, 'low'); assert.equal(payload.reasoning_effort, 'low');
assert.deepEqual(payload.chat_template_kwargs, { enable_thinking: false }); assert.deepEqual(payload.chat_template_kwargs, { enable_thinking: false });
}); });
const KEEP_ONE = jsonOf([{ excerpt: 'Soyons honnêtes : ', suggestion: '', rule: 'ouverture-vide', why: 'x' }]);
test('a passing "Failed to load model" 400 is retried, then succeeds', async () => {
let calls = 0;
const { server, url } = await mockServer((body, res) => {
calls++;
if (calls < 3) {
res.statusCode = 400;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ error: { message: 'Failed to load model "x". Error: Operation canceled.' } }));
return;
}
reply(res, KEEP_ONE);
});
const r = await fixText(SLOPPY, { url, maxRounds: 1, retryDelaysMs: [5, 5] });
server.close();
assert.equal(calls, 3);
assert.ok(r.rounds.length >= 1);
});
test('a persistent failure gives up after three tries and shows the server message', async () => {
let calls = 0;
const { server, url } = await mockServer((body, res) => {
calls++;
res.statusCode = 503;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ error: { message: 'model busy' } }));
});
await assert.rejects(
() => fixText(SLOPPY, { url, maxRounds: 1, retryDelaysMs: [5, 5] }),
(e) => {
assert.ok(e instanceof FixError);
assert.match(e.message, /503.*model busy/);
return true;
},
);
server.close();
assert.equal(calls, 3);
});
test('a plain 400 is not retried', async () => {
let calls = 0;
const { server, url } = await mockServer((body, res) => {
calls++;
res.statusCode = 400;
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ error: { message: 'bad field' } }));
});
await assert.rejects(() => fixText(SLOPPY, { url, maxRounds: 1, retryDelaysMs: [5, 5] }), FixError);
server.close();
assert.equal(calls, 1);
});
test('a model that reasons into reasoning_content with no content is named plainly', async () => {
const { server, url } = await mockServer((body, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ choices: [{ message: { content: '', reasoning_content: 'hmm' } }] }));
});
await assert.rejects(
() => fixText(SLOPPY, { url, maxRounds: 1, model: 'thinker' }),
(e) => {
assert.match(e.message, /raisonne malgré/);
assert.match(e.message, /BUVARD_FIX_MODEL/);
return true;
},
);
server.close();
});
test('automatic pick skips embedding models and prefers an instruct id', async () => {
const { server, url } = await mockServer((body, res) => reply(res, KEEP_ONE), [
{ id: 'text-embedding-nomic' },
{ id: 'big-chat-27b' },
{ id: 'falcon3-1b-instruct' },
]);
const r = await fixText(SLOPPY, { url, maxRounds: 1 });
server.close();
assert.equal(r.model, 'falcon3-1b-instruct');
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment