chore(confidence-engine): expose reconstruction fallback path

This commit is contained in:
2026-09-05 19:13:39 +01:00
parent c7a0a79d0f
commit cd1c6f4fc5
9 changed files with 119 additions and 5 deletions
+19
View File
@@ -64,6 +64,11 @@ function recoverJson(raw) {
let _chatSupported = null;
const reconstructionJsonSchema = z.toJSONSchema(reconstructionV2Schema);
/** @internal Test-only seam for isolated provider capability scenarios. */
export function __resetChatSupportForTests() {
_chatSupported = null;
}
async function detectChatSupport(baseUrl, modelName) {
if (_chatSupported !== null) return _chatSupported;
@@ -108,6 +113,12 @@ class OllamaLlmProvider {
let chatSupported = false;
let rawResponse = null;
let fullResponseData = null;
const providerExecution = {
chatCapabilityDetected: false,
chatRequestAttempted: false,
chatRequestSucceeded: false,
generateRequestAttempted: false,
};
// ================================================================
// Step 1: Detect whether /api/chat exists (cache result)
@@ -115,6 +126,7 @@ class OllamaLlmProvider {
try {
chatSupported = await detectChatSupport(baseUrl, modelName);
} catch { /* failed silently — defaults to false */ }
providerExecution.chatCapabilityDetected = chatSupported;
// ================================================================
// Step 2: Try /api/chat if supported with the reconstruction schema
@@ -125,6 +137,7 @@ class OllamaLlmProvider {
const timeout = setTimeout(() => controller.abort(), 60000);
apiUsed = "/api/chat";
providerExecution.chatRequestAttempted = true;
const res = await fetch(`${baseUrl}/api/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -140,6 +153,7 @@ class OllamaLlmProvider {
clearTimeout(timeout);
if (res.ok) {
providerExecution.chatRequestSucceeded = true;
fullResponseData = await res.json();
rawResponse = typeof fullResponseData.message?.content === "string"
? fullResponseData.message.content
@@ -163,6 +177,7 @@ class OllamaLlmProvider {
const timeout = setTimeout(() => controller.abort(), 300000); // 5 min for cold start
apiUsed = "/api/generate"; // set BEFORE the request so we know which API failed
providerExecution.generateRequestAttempted = true;
const res = await fetch(`${baseUrl}/api/generate`, {
method: "POST",
@@ -204,6 +219,7 @@ class OllamaLlmProvider {
`- Check Ollama logs: \`ollama serve\` or look at your system logs`
);
error.providerApiPath = apiUsed;
error.providerExecution = providerExecution;
throw error;
}
throw e;
@@ -244,6 +260,7 @@ class OllamaLlmProvider {
"- If your model is very small (e.g., tinyllama, phi), try a larger one like llama3.1 or mistral"
);
error.providerApiPath = apiUsed;
error.providerExecution = providerExecution;
throw error;
}
@@ -254,6 +271,7 @@ class OllamaLlmProvider {
return {
response: recoverJson(rawResponse),
providerApiPath: apiUsed,
providerExecution,
};
} catch (e) {
if (e instanceof SyntaxError) {
@@ -269,6 +287,7 @@ class OllamaLlmProvider {
"- Consider upgrading Ollama: https://ollama.com/download"
);
error.providerApiPath = apiUsed;
error.providerExecution = providerExecution;
throw error;
}
throw e;