diff --git a/src/Purrse.Api/Services/ChatService.cs b/src/Purrse.Api/Services/ChatService.cs index 87f41d6..cc1afda 100644 --- a/src/Purrse.Api/Services/ChatService.cs +++ b/src/Purrse.Api/Services/ChatService.cs @@ -171,6 +171,13 @@ public class ChatService : IChatService if (!string.IsNullOrEmpty(request.PageContext)) { userContent = $"{request.Message}\n\n[Current page data]\n{request.PageContext}"; + _logger.LogInformation("Page context attached — {Length} chars, preview: {Preview}", + request.PageContext.Length, + request.PageContext.Length > 200 ? request.PageContext[..200] + "..." : request.PageContext); + } + else + { + _logger.LogInformation("No page context attached to message"); } ollamaMessages.Add(new OllamaToolMessage { Role = "user", Content = userContent }); @@ -248,9 +255,31 @@ public class ChatService : IChatService } else { - var rawContent = string.IsNullOrWhiteSpace(response?.Message?.Content) - ? "I'm sorry, I couldn't process that request." - : response!.Message!.Content; + var rawContent = response?.Message?.Content; + + // If the model returned empty content and page context is present, + // the tool definitions may be overwhelming the model. Retry without + // tools so it can focus on answering from the page data. + if (string.IsNullOrWhiteSpace(rawContent) && !string.IsNullOrEmpty(request.PageContext) && i == 0) + { + _logger.LogWarning("Model returned empty content with page context present — retrying without tools"); + var retryRequest = new OllamaToolChatRequest + { + Model = modelName, + Messages = ollamaMessages, + Stream = false, + Tools = null // drop tools so the model focuses on the page data + }; + var retryResponse = await CallOllamaAsync(ollamaUrl, retryRequest); + rawContent = retryResponse?.Message?.Content; + } + + if (string.IsNullOrWhiteSpace(rawContent)) + { + _logger.LogWarning("Model returned empty content (no tool calls). ToolCalls: {ToolCalls}, Done: {Done}", + response?.Message?.ToolCalls?.Count ?? -1, response?.Done); + rawContent = "I'm sorry, I couldn't process that request."; + } // If the model responded without calling any tools on the first attempt // AND there are no prior tool results AND no page context data, it