Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: hardening the local llm api

Ilian Sapundshiev59f7847909ab
diff --git a/src/renderer/components/chat/OpenAIClient.ts b/src/renderer/components/chat/OpenAIClient.ts
index f3868d41c..a3823cb37 100644
--- a/src/renderer/components/chat/OpenAIClient.ts
+++ b/src/renderer/components/chat/OpenAIClient.ts
@@ -167,6 +167,12 @@ export async function newOpenAIMessage(
return undefined;
}
const body = toOpenAIRequest(msg);
+ // An empty messages array is the Ollama-only model load/unload idiom; OpenAI
+ // servers reject it with HTTP 400 ("messages array cannot be empty"). Treat
+ // it as a no-op so it never reaches the wire as a spurious error.
+ if (!Array.isArray(body.messages) || body.messages.length === 0) {
+ return undefined;
+ }
try {
const response = await fetch(`${apiBase(url)}/chat/completions`, {
method: 'POST',
diff --git a/src/renderer/hooks/ChatProvider.tsx b/src/renderer/hooks/ChatProvider.tsx
index 1a0eb7204..a750304a5 100644
--- a/src/renderer/hooks/ChatProvider.tsx
+++ b/src/renderer/hooks/ChatProvider.tsx
@@ -894,8 +894,14 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {
const msgContent = getMessage(msg, mode);
const imagesArray =
imgArray.length > 0 ? imgArray : images.current.map((i) => i.base64);
+ // Build a message when there's prompt text OR an image to send. An image
+ // alone is a valid request (vision models), so it must not be dropped just
+ // because the prompt template resolved empty — otherwise `messages` ends up
+ // [] and OpenAI-compatible servers reject it ("messages array cannot be
+ // empty"). The empty-on-both case is preserved: it's the Ollama model
+ // load/unload idiom.
const messages =
- msgContent && !unload
+ (msgContent || imagesArray.length > 0) && !unload
? [
...getOllamaMessages(
includeHistory ? chatHistoryItems.current : [],
@@ -903,7 +909,7 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {
),
{
role: role,
- content: msgContent,
+ content: msgContent || '',
...(imagesArray.length > 0 && { images: imagesArray }),
},
]
@@ -955,7 +961,7 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {
};
// console.log('AI question: ' + JSON.stringify(messages));
if (!aiClient.current) {
- showNotification(t('core:ollamaServiceNotAlive'));
+ showNotification(t('core:aiServiceNotReachable'));
isTyping.current = false;
forceUpdate();
return Promise.resolve(undefined);
@@ -967,7 +973,10 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {
isTyping.current = false;
forceUpdate();
if (apiResponse === undefined && stream === false) {
- showNotification(t('core:ollamaServiceNotAlive'));
+ // The client returned nothing — the service is reachable but the
+ // request failed (bad model, rejected schema/image, etc.). Not a
+ // liveness problem, so don't claim the service is down.
+ showNotification(t('core:aiGenerationFailed'));
return undefined;
}
if (msg && includeHistory) {
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 44d6d7e72..eebd15479 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -1101,12 +1101,13 @@
"tagsGeneratedByAI": "Tags generated by an AI.",
"confirmRemoveModel": "Do you want to remove {{name}} model?",
"modelDeletedSuccess": "Deleting {{name}} succeeded",
- "ollamaServiceNotAlive": "Error checking if Ollama service is alive",
"modelNotFoundTryPull": "Model not found, try pulling it first",
"ollamaConnectionError": "There is a problem with the Ollama service connection",
"tagsGenerationNotSupportedFor": "Tags generation not supported for: {{name}}",
"generationNotSupportedFor": "AI generation not supported for: {{name}}",
"noModelConfiguredFor": "No AI model is configured for: {{name}}. Check the default image/text models in the AI settings.",
+ "aiServiceNotReachable": "The AI service is not reachable. Check that it is running and the provider URL in the AI settings is correct.",
+ "aiGenerationFailed": "AI generation failed. Check the AI service and the selected model, then try again.",
"aiNoProviderConfigured": "No AI provider is configured. Open the AI settings to add one.",
"aiNoSupportedFiles": "None of the selected items can be processed by the AI.",
"fileCreated": "File '{{name}}' created.",