feat(ai): понятные ошибки провайдера вместо 'AI service error: <статус>'

Хелпер lib/ai-errors.js маппит HTTP-статус ответа провайдера на
человеческое пояснение (402 — закончились лимиты, 401 — ключ, 429 —
лимит запросов и т.д.) и добавляет короткий текст ошибки провайдера
из тела ответа. Подключён во всех точках вызова модели: воркеры
записей/отчётов/отзывов/заметок, aiComplete, probeAiModel.
This commit is contained in:
dev
2026-11-10 11:05:54 +03:00
parent 9b2fa7811f
commit 5baaf3de7f
3 changed files with 52 additions and 6 deletions
+5 -4
View File
@@ -6,6 +6,7 @@ const MIN_TEXT_CHARS = 4;
const crypto = require('crypto');
const { textDiff, FIELD_LABELS } = require('./diff');
const { saveFeedbackVersion } = require('./lib/feedback-versions');
const { aiHttpErrorFromResponse } = require('./lib/ai-errors');
const PHOTO_MAX_ATTEMPTS = 3;
const PHOTO_AI_TIMEOUT_MS = Math.max(1000, parseInt(process.env.PHOTO_AI_TIMEOUT_MS || '300000', 10) || 300000);
const PHOTO_AI_FACE_TIMEOUT_MS = Math.max(PHOTO_AI_TIMEOUT_MS, parseInt(process.env.PHOTO_AI_FACE_TIMEOUT_MS || '600000', 10) || 600000);
@@ -712,7 +713,7 @@ function createEntryAutoChecker({ pool, getSetting, logAudit, aiUrl, defaultProm
max_tokens: maxTokens,
}),
});
if (!res.ok) throw new Error(`AI service error: ${res.status}`);
if (!res.ok) throw await aiHttpErrorFromResponse(res);
const data = await res.json();
const choice = data.choices && data.choices[0];
const content = (choice && choice.message && choice.message.content || '').trim();
@@ -995,7 +996,7 @@ async function resolveActiveProfile() {
max_tokens: maxTokens,
}),
});
if (!res.ok) throw new Error(`AI service error: ${res.status}`);
if (!res.ok) throw await aiHttpErrorFromResponse(res);
const data = await res.json();
const choice = data.choices && data.choices[0];
return (choice && choice.message && choice.message.content || '').trim();
@@ -1268,7 +1269,7 @@ function createFeedbackWriter(opts) {
body: JSON.stringify({ model, messages, temperature: 0.1, max_tokens: maxTokens }),
signal: controller.signal
});
if (!res.ok) throw new Error(`AI service error: ${res.status}`);
if (!res.ok) throw await aiHttpErrorFromResponse(res);
const data = await res.json();
const choice = data.choices && data.choices[0];
const content = (choice && choice.message && choice.message.content || '').trim();
@@ -1668,7 +1669,7 @@ function createNoteTitleWorker({ pool, getSetting, logAudit, aiUrl, model, defau
max_tokens: maxTokens,
}),
});
if (!res.ok) throw new Error(`AI service error: ${res.status}`);
if (!res.ok) throw await aiHttpErrorFromResponse(res);
const data = await res.json();
const choice = data.choices && data.choices[0];
return (choice && choice.message && choice.message.content || '').trim();