Use configured inference model for MCP assists
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { resolveOpenAiCompatibleBaseUrl, resolvePrimaryInferenceModel } from "@/app/lib/inferenceModel"
|
||||
|
||||
const REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_INSTALL_ASSIST_TIMEOUT_MS || "45000", 10)
|
||||
const MODEL = process.env.MCP_PREFLIGHT_LLM_MODEL || process.env.OPENAI_MODEL || "gpt-4o-mini"
|
||||
const BASE_URL = (process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "http://localhost:8000/v1").replace(/\/+$/, "")
|
||||
|
||||
function extractJsonObject(value: string) {
|
||||
const trimmed = value.trim()
|
||||
@@ -34,8 +33,10 @@ export async function POST(request: Request) {
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const prompt = typeof body?.prompt === "string" ? body.prompt.trim() : ""
|
||||
if (!prompt) throw new Error("install prompt is required")
|
||||
const model = await resolvePrimaryInferenceModel()
|
||||
if (!model) throw new Error("No primary inference model is configured")
|
||||
|
||||
const response = await fetch(`${BASE_URL}/chat/completions`, {
|
||||
const response = await fetch(`${resolveOpenAiCompatibleBaseUrl()}/chat/completions`, {
|
||||
method: "POST",
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
@@ -45,7 +46,7 @@ export async function POST(request: Request) {
|
||||
: {}),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: MODEL,
|
||||
model,
|
||||
temperature: 0.1,
|
||||
response_format: { type: "json_object" },
|
||||
messages: [
|
||||
@@ -88,7 +89,7 @@ export async function POST(request: Request) {
|
||||
uploadEntrypoint: typeof suggestion?.uploadEntrypoint === "string" ? suggestion.uploadEntrypoint : "",
|
||||
notes: typeof suggestion?.notes === "string" ? suggestion.notes : "",
|
||||
},
|
||||
model: MODEL,
|
||||
model,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to assist MCP install"
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { resolveOpenAiCompatibleBaseUrl, resolvePrimaryInferenceModel } from "@/app/lib/inferenceModel"
|
||||
|
||||
const REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_REGISTRY_ASSIST_TIMEOUT_MS || "45000", 10)
|
||||
const MODEL = process.env.MCP_PREFLIGHT_LLM_MODEL || process.env.OPENAI_MODEL || "gpt-4o-mini"
|
||||
const BASE_URL = (process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "http://localhost:8000/v1").replace(/\/+$/, "")
|
||||
|
||||
function extractJsonObject(value: string) {
|
||||
const trimmed = value.trim()
|
||||
@@ -19,8 +18,10 @@ export async function POST(request: Request) {
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const prompt = typeof body?.prompt === "string" ? body.prompt.trim() : ""
|
||||
if (!prompt) throw new Error("registry prompt is required")
|
||||
const model = await resolvePrimaryInferenceModel()
|
||||
if (!model) throw new Error("No primary inference model is configured")
|
||||
|
||||
const response = await fetch(`${BASE_URL}/chat/completions`, {
|
||||
const response = await fetch(`${resolveOpenAiCompatibleBaseUrl()}/chat/completions`, {
|
||||
method: "POST",
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
@@ -30,7 +31,7 @@ export async function POST(request: Request) {
|
||||
: {}),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: MODEL,
|
||||
model,
|
||||
temperature: 0.1,
|
||||
response_format: { type: "json_object" },
|
||||
messages: [
|
||||
@@ -56,7 +57,7 @@ export async function POST(request: Request) {
|
||||
baseUrl: typeof suggestion?.baseUrl === "string" ? suggestion.baseUrl : "",
|
||||
description: typeof suggestion?.description === "string" ? suggestion.description : "",
|
||||
},
|
||||
model: MODEL,
|
||||
model,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to assist registry setup"
|
||||
|
||||
@@ -373,7 +373,7 @@ export async function POST(request: Request) {
|
||||
attempted: true,
|
||||
ok: false,
|
||||
provider: "openai-compatible",
|
||||
model: process.env.MCP_PREFLIGHT_LLM_MODEL || process.env.OPENAI_MODEL || "",
|
||||
model: "",
|
||||
baseUrl: process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "",
|
||||
summary: "LLM-assisted repair could not complete.",
|
||||
changes: [],
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { execFile } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
function stripAnsi(value: string) {
|
||||
return value.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "")
|
||||
}
|
||||
|
||||
function readField(output: string, label: string) {
|
||||
const match = output.match(new RegExp(`^\\s*${label}:\\s*(.+?)\\s*$`, "im"))
|
||||
return match ? match[1].trim() : ""
|
||||
}
|
||||
|
||||
function parseRouteModel(output: string, label: "Gateway inference" | "System inference") {
|
||||
const otherLabel = label === "Gateway inference" ? "System inference" : "Gateway inference"
|
||||
const pattern = new RegExp(`${label}:\\s*([\\s\\S]*?)(?:\\n\\s*${otherLabel}:|$)`, "i")
|
||||
const section = output.match(pattern)?.[1] ?? ""
|
||||
if (!section || /Not configured/i.test(section)) return ""
|
||||
return readField(section, "Model")
|
||||
}
|
||||
|
||||
async function readOpenShellInferenceModel() {
|
||||
const { stdout } = await execFileAsync(OPENSHELL_BIN, ["inference", "get"], {
|
||||
env: hostCommandEnv({
|
||||
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||
}),
|
||||
timeout: 30000,
|
||||
maxBuffer: 5 * 1024 * 1024,
|
||||
})
|
||||
const output = stripAnsi(String(stdout).trim())
|
||||
return parseRouteModel(output, "Gateway inference") || parseRouteModel(output, "System inference")
|
||||
}
|
||||
|
||||
export async function resolvePrimaryInferenceModel(explicitModel?: string | null) {
|
||||
if (explicitModel?.trim()) return explicitModel.trim()
|
||||
if (process.env.MCP_PREFLIGHT_LLM_MODEL?.trim()) return process.env.MCP_PREFLIGHT_LLM_MODEL.trim()
|
||||
try {
|
||||
const configured = await readOpenShellInferenceModel()
|
||||
if (configured) return configured
|
||||
} catch {
|
||||
// Callers surface endpoint failures; model resolution falls through to explicit env compatibility.
|
||||
}
|
||||
return process.env.OPENAI_MODEL?.trim() || process.env.VLLM_MODEL?.trim() || ""
|
||||
}
|
||||
|
||||
export function resolveOpenAiCompatibleBaseUrl() {
|
||||
return (process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "http://localhost:8000/v1").replace(/\/+$/, "")
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { readdir, readFile, stat, writeFile } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { resolveOpenAiCompatibleBaseUrl, resolvePrimaryInferenceModel } from "./inferenceModel"
|
||||
import { getSandboxInferenceConfig } from "./sandboxInferenceStore"
|
||||
import type { McpPreflightResult } from "./mcpPreflight"
|
||||
import type { McpServerInstall } from "./mcpServerStore"
|
||||
@@ -46,12 +47,6 @@ const MAX_CONTEXT_FILE_BYTES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_
|
||||
const MAX_CONTEXT_FILES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_MAX_FILES || "18", 10)
|
||||
const MAX_REPAIR_FILE_BYTES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_MAX_OUTPUT_FILE_BYTES || String(512 * 1024), 10)
|
||||
const REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_TIMEOUT_MS || "60000", 10)
|
||||
const DEFAULT_MODEL = process.env.MCP_PREFLIGHT_LLM_MODEL || process.env.OPENAI_MODEL || "gpt-4o-mini"
|
||||
const DEFAULT_BASE_URL = process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "http://localhost:8000/v1"
|
||||
|
||||
function cleanBaseUrl(value: string) {
|
||||
return value.replace(/\/+$/, "")
|
||||
}
|
||||
|
||||
function relativeInside(root: string, candidate: string) {
|
||||
const fullPath = path.resolve(root, candidate)
|
||||
@@ -110,20 +105,16 @@ async function readContextFiles(root: string) {
|
||||
}
|
||||
|
||||
async function resolveRepairRoute(sandboxId?: string | null) {
|
||||
let explicitModel = ""
|
||||
if (sandboxId) {
|
||||
const config = await getSandboxInferenceConfig(sandboxId)
|
||||
const enabled = config.routes.filter((route) => route.enabled)
|
||||
const primary = enabled.find((route) => route.id === config.primaryRouteId) || enabled[0]
|
||||
if (primary?.model) {
|
||||
return {
|
||||
model: process.env.MCP_PREFLIGHT_LLM_MODEL || primary.model,
|
||||
baseUrl: cleanBaseUrl(DEFAULT_BASE_URL),
|
||||
}
|
||||
}
|
||||
explicitModel = primary?.model || ""
|
||||
}
|
||||
return {
|
||||
model: DEFAULT_MODEL,
|
||||
baseUrl: cleanBaseUrl(DEFAULT_BASE_URL),
|
||||
model: await resolvePrimaryInferenceModel(explicitModel),
|
||||
baseUrl: resolveOpenAiCompatibleBaseUrl(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +128,7 @@ function extractJsonObject(value: string) {
|
||||
|
||||
async function requestRepairPlan(context: RepairContext, files: Awaited<ReturnType<typeof readContextFiles>>) {
|
||||
const route = await resolveRepairRoute(context.sandboxId)
|
||||
if (!route.model) throw new Error("No primary inference model is configured")
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user