Skip to content
Prev Previous commit
Use 502 Bad Gateway for malformed responses and fix validation placement
Co-authored-by: bullpowerhubgit <150299642+bullpowerhubgit@users.noreply.github.com>
  • Loading branch information
Copilot and bullpowerhubgit committed Feb 3, 2026
commit 765db0fc7619ea452cbea5fbaadab6764a3b84d6
20 changes: 10 additions & 10 deletions src/ai-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class AIProvider {
const data = await response.json();

if (!data.choices || !data.choices[0] || !data.choices[0].message) {
throw new APIError('Ungültige Antwort vom Groq API: Keine Nachricht erhalten', 500);
throw new APIError('Ungültige Antwort vom Groq API: Keine Nachricht erhalten', 502);
}

return data.choices[0].message.content;
Expand Down Expand Up @@ -228,7 +228,7 @@ export class AIProvider {
const data = await response.json();

if (!data.choices || !data.choices[0] || !data.choices[0].message) {
throw new APIError('Ungültige Antwort vom OpenRouter API: Keine Nachricht erhalten', 500);
throw new APIError('Ungültige Antwort vom OpenRouter API: Keine Nachricht erhalten', 502);
}

return data.choices[0].message.content;
Expand Down Expand Up @@ -257,18 +257,18 @@ export class AIProvider {
);
}

const lastMessage = conversationHistory[conversationHistory.length - 1];
if (!lastMessage || !lastMessage.content) {
throw new APIError('Letzte Nachricht in der Konversationshistorie ist ungültig oder fehlt', 400);
}

const modelMap = {
'huggingface-llama': 'meta-llama/Llama-2-7b-chat-hf',
'huggingface-mistral': 'mistralai/Mistral-7B-Instruct-v0.2',
'huggingface-zephyr': 'HuggingFaceH4/zephyr-7b-beta'
};

try {
const lastMessage = conversationHistory[conversationHistory.length - 1];
if (!lastMessage || !lastMessage.content) {
throw new APIError('Letzte Nachricht in der Konversationshistorie ist ungültig oder fehlt', 400);
}

const response = await fetch(
`https://api-inference.huggingface.co/models/${modelMap[this.modelName] || modelMap['huggingface-mistral']}`,
{
Expand Down Expand Up @@ -297,7 +297,7 @@ export class AIProvider {

const generatedText = data[0]?.generated_text || data.generated_text;
if (!generatedText) {
throw new APIError('Ungültige Antwort vom Hugging Face API: Kein Text generiert', 500);
throw new APIError('Ungültige Antwort vom Hugging Face API: Kein Text generiert', 502);
}

return generatedText;
Expand Down Expand Up @@ -366,7 +366,7 @@ export class AIProvider {

const generatedText = data.candidates?.[0]?.content?.parts?.[0]?.text;
if (!generatedText) {
throw new APIError('Ungültige Antwort vom Google Gemini API: Kein Text generiert', 500);
throw new APIError('Ungültige Antwort vom Google Gemini API: Kein Text generiert', 502);
}

return generatedText;
Expand Down Expand Up @@ -417,7 +417,7 @@ export class AIProvider {

const generatedText = data.message?.content;
if (!generatedText) {
throw new APIError('Ungültige Antwort von Ollama: Keine Nachricht erhalten', 500);
throw new APIError('Ungültige Antwort von Ollama: Keine Nachricht erhalten', 502);
}

return generatedText;
Expand Down