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
+8 -1
View File
@@ -73,6 +73,7 @@ export async function analyseScenario(scenario, opts = {}) {
const provider = getProvider();
let rawResponse;
let providerApiPath;
let providerExecution;
try {
const providerResult = await provider.generateReconstruction(
promptObj.prompt,
@@ -86,6 +87,7 @@ export async function analyseScenario(scenario, opts = {}) {
) {
rawResponse = providerResult.response;
providerApiPath = providerResult.providerApiPath;
providerExecution = providerResult.providerExecution;
} else {
rawResponse = providerResult;
}
@@ -95,6 +97,7 @@ export async function analyseScenario(scenario, opts = {}) {
Date.now() - startTime,
"500",
e.providerApiPath,
e.providerExecution,
);
}
@@ -150,6 +153,7 @@ export async function analyseScenario(scenario, opts = {}) {
promptVersion,
compatibility,
providerApiPath,
providerExecution,
);
}
@@ -169,7 +173,7 @@ function tryValidateAgainstSchema(data, schema) {
// ── Result builders ──────────────────────────────────
function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath) {
function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath, providerExecution) {
return {
success: false,
error: message,
@@ -180,6 +184,7 @@ function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath)
promptVersion: null,
statusCode,
providerApiPath,
providerExecution,
};
}
@@ -233,6 +238,7 @@ function buildPartialResult(
version,
compatibility,
providerApiPath,
providerExecution,
) {
let errors = [];
const validationIssues = error?.issues ?? [];
@@ -260,6 +266,7 @@ function buildPartialResult(
errors,
validationIssues,
providerApiPath,
providerExecution,
...buildCompatibilityDiagnostics(compatibility),
};
}
+1
View File
@@ -373,6 +373,7 @@ export async function startCase(body, dependencies = {}) {
analysisErrors: analysis.errors ?? undefined,
validationIssues: analysis.validationIssues ?? undefined,
providerApiPath: analysis.providerApiPath ?? undefined,
providerExecution: analysis.providerExecution ?? undefined,
rawResponse: analysis.rawResponse ?? undefined,
statusCode: Number(analysis.statusCode) || 502,
};
+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;