From 70a3e0f8f775936a7181b9869a298a6d90db2ee4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 04:37:07 +0000 Subject: [PATCH 1/3] Initial plan From 31d899177ee0af86185c6a96530316e2951a1426 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 04:40:16 +0000 Subject: [PATCH 2/3] Fix connection test to use /models endpoint instead of chat/completions This change fixes the error where testing connection would fail if the model crashes when trying to process a minimal chat completion. The /models endpoint is a much lighter connectivity check that: 1. Only verifies the LLM server is reachable (no model execution needed) 2. Lists available models to verify the selected model exists 3. Provides a clearer success/warning message about model availability This resolves the HTTP 400 Bad Request errors that occurred when testing connection with models like deepseek-r1 that may crash with minimal token generation requests. Co-authored-by: raux <5723643+raux@users.noreply.github.com> --- src/extension.ts | 38 +++++++++++++++++++++----------------- src/server.ts | 38 +++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 143de94..3b2e899 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -564,30 +564,34 @@ export function activate(context: vscode.ExtensionContext) { throw new Error('Invalid URL format. Expected format: host:port/path (e.g., localhost:1234/v1)'); } - const res = await fetch(`http://${testUrl}/chat/completions`, { - method: 'POST', + // First, test basic connectivity using the /models endpoint (lightweight, no model execution) + const modelsRes = await fetch(`http://${testUrl}/models`, { + method: 'GET', headers: { - 'Content-Type': 'application/json', 'Authorization': 'Bearer lm-studio', }, - body: JSON.stringify({ - model: testModel, - messages: [{ role: 'user', content: 'Say "connected" in one word.' }], - temperature: 0, - max_tokens: 10, - stream: false, - }), }); - if (!res.ok) { - const text = await res.text().catch(() => ''); - throw new Error(`HTTP ${res.status} ${res.statusText}${text ? ` - ${text}` : ''}`); + if (!modelsRes.ok) { + const text = await modelsRes.text().catch(() => ''); + throw new Error(`Server not reachable: HTTP ${modelsRes.status} ${modelsRes.statusText}${text ? ` - ${text}` : ''}`); } - const json: any = await res.json(); - const output = json?.choices?.[0]?.message?.content?.trim?.() ?? ''; - bonsaiLog('Connection test successful. Response:', output); - panel.webview.postMessage({ command: 'connectionTestResult', success: true, message: `✓ Connected! Model responded: "${output}"` }); + const modelsJson: any = await modelsRes.json(); + const availableModels = modelsJson?.data?.map((m: any) => m.id) ?? []; + + // Check if the specified model is available + const modelAvailable = availableModels.length === 0 || availableModels.includes(testModel); + const modelStatus = modelAvailable + ? `Model "${testModel}" is available.` + : `Warning: Model "${testModel}" not found in available models: ${availableModels.join(', ')}`; + + bonsaiLog('Connection test successful. Server reachable.', modelStatus); + panel.webview.postMessage({ + command: 'connectionTestResult', + success: true, + message: `✓ Connected to LLM server! ${modelStatus}` + }); } catch (err: any) { bonsaiLog('Connection test failed:', err?.message || err); panel.webview.postMessage({ command: 'connectionTestResult', success: false, message: `✗ Connection failed: ${err?.message || err}` }); diff --git a/src/server.ts b/src/server.ts index 415bcbb..08373c9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -502,30 +502,34 @@ async function handleMessage(message: any): Promise { throw new Error('Invalid URL format. Expected format: host:port/path (e.g., localhost:1234/v1)'); } - const res = await fetch(`http://${testUrl}/chat/completions`, { - method: 'POST', + // First, test basic connectivity using the /models endpoint (lightweight, no model execution) + const modelsRes = await fetch(`http://${testUrl}/models`, { + method: 'GET', headers: { - 'Content-Type': 'application/json', 'Authorization': 'Bearer lm-studio', }, - body: JSON.stringify({ - model: testModel, - messages: [{ role: 'user', content: 'Say "connected" in one word.' }], - temperature: 0, - max_tokens: 10, - stream: false, - }), }); - if (!res.ok) { - const text = await res.text().catch(() => ''); - throw new Error(`HTTP ${res.status} ${res.statusText}${text ? ` - ${text}` : ''}`); + if (!modelsRes.ok) { + const text = await modelsRes.text().catch(() => ''); + throw new Error(`Server not reachable: HTTP ${modelsRes.status} ${modelsRes.statusText}${text ? ` - ${text}` : ''}`); } - const json: any = await res.json(); - const output = json?.choices?.[0]?.message?.content?.trim?.() ?? ''; - bonsaiLog('Connection test successful. Response:', output); - broadcast({ command: 'connectionTestResult', success: true, message: `✓ Connected! Model responded: "${output}"` }); + const modelsJson: any = await modelsRes.json(); + const availableModels = modelsJson?.data?.map((m: any) => m.id) ?? []; + + // Check if the specified model is available + const modelAvailable = availableModels.length === 0 || availableModels.includes(testModel); + const modelStatus = modelAvailable + ? `Model "${testModel}" is available.` + : `Warning: Model "${testModel}" not found in available models: ${availableModels.join(', ')}`; + + bonsaiLog('Connection test successful. Server reachable.', modelStatus); + broadcast({ + command: 'connectionTestResult', + success: true, + message: `✓ Connected to LLM server! ${modelStatus}` + }); } catch (err: any) { bonsaiLog('Connection test failed:', err?.message || err); broadcast({ command: 'connectionTestResult', success: false, message: `✗ Connection failed: ${err?.message || err}` }); From aac2ecd82c15ce03f6b69f16f16d5bac0a52007d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 04:41:53 +0000 Subject: [PATCH 3/3] Improve model availability check to warn when no models are reported Address code review feedback: if the server returns an empty models list, show a warning message instead of assuming the model is available. Co-authored-by: raux <5723643+raux@users.noreply.github.com> --- src/extension.ts | 12 ++++++++---- src/server.ts | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 3b2e899..5445043 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -581,10 +581,14 @@ export function activate(context: vscode.ExtensionContext) { const availableModels = modelsJson?.data?.map((m: any) => m.id) ?? []; // Check if the specified model is available - const modelAvailable = availableModels.length === 0 || availableModels.includes(testModel); - const modelStatus = modelAvailable - ? `Model "${testModel}" is available.` - : `Warning: Model "${testModel}" not found in available models: ${availableModels.join(', ')}`; + let modelStatus: string; + if (availableModels.length === 0) { + modelStatus = `Warning: No models reported by server. Make sure "${testModel}" is loaded.`; + } else if (availableModels.includes(testModel)) { + modelStatus = `Model "${testModel}" is available.`; + } else { + modelStatus = `Warning: Model "${testModel}" not found. Available: ${availableModels.join(', ')}`; + } bonsaiLog('Connection test successful. Server reachable.', modelStatus); panel.webview.postMessage({ diff --git a/src/server.ts b/src/server.ts index 08373c9..c25c79f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -519,10 +519,14 @@ async function handleMessage(message: any): Promise { const availableModels = modelsJson?.data?.map((m: any) => m.id) ?? []; // Check if the specified model is available - const modelAvailable = availableModels.length === 0 || availableModels.includes(testModel); - const modelStatus = modelAvailable - ? `Model "${testModel}" is available.` - : `Warning: Model "${testModel}" not found in available models: ${availableModels.join(', ')}`; + let modelStatus: string; + if (availableModels.length === 0) { + modelStatus = `Warning: No models reported by server. Make sure "${testModel}" is loaded.`; + } else if (availableModels.includes(testModel)) { + modelStatus = `Model "${testModel}" is available.`; + } else { + modelStatus = `Warning: Model "${testModel}" not found. Available: ${availableModels.join(', ')}`; + } bonsaiLog('Connection test successful. Server reachable.', modelStatus); broadcast({