Use configured inference model for MCP assists
This commit is contained in:
@@ -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