Support OpenCode 2.x providers block and quiet provider inference
This commit is contained in:
@@ -77,41 +77,104 @@ function stripJsonComments(raw) {
|
||||
return out.replace(/,(\s*[}\]])/g, "$1")
|
||||
}
|
||||
|
||||
const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "0.0.0.0"])
|
||||
|
||||
function firstString(...values) {
|
||||
for (const value of values) {
|
||||
if (typeof value === "string" && value.trim()) return value.trim()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// OpenCode 2.x keeps provider endpoints in `providers.<id>.settings.baseURL`.
|
||||
// The deprecated 1.x shape used `provider.<id>.options.baseURL`; read both so a
|
||||
// config migration cannot silently disable inference.
|
||||
function providerBaseURL(provider) {
|
||||
return firstString(provider?.settings?.baseURL, provider?.options?.baseURL, provider?.baseURL)
|
||||
}
|
||||
|
||||
// Explicit opt-in: `settings.agentURL` (or the legacy `options.agentURL`) points
|
||||
// straight at the swap agent, so such providers bypass the heuristics below.
|
||||
function providerAgentURL(provider) {
|
||||
return firstString(
|
||||
provider?.settings?.agentURL,
|
||||
provider?.settings?.swap_agent_url,
|
||||
provider?.options?.agentURL,
|
||||
provider?.options?.swap_agent_url,
|
||||
)
|
||||
}
|
||||
|
||||
function hostOf(url) {
|
||||
const match = String(url || "").match(/^https?:\/\/([^:/]+)(?::\d+)?/)
|
||||
return match ? match[1] : null
|
||||
}
|
||||
|
||||
function isLoopbackHost(host) {
|
||||
return LOOPBACK_HOSTS.has(host) || host.endsWith(".localhost")
|
||||
}
|
||||
|
||||
// A swap agent listens on plain HTTP on a LAN address. Public HTTPS gateways and
|
||||
// loopback providers (a local Ollama with no agent) would only log a connection
|
||||
// error on every poll, so they are skipped unless inference was opted in.
|
||||
function looksLikeSwapHost(baseURL, options) {
|
||||
let url
|
||||
try {
|
||||
url = new URL(baseURL)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
if (url.protocol !== "http:") return false
|
||||
if (isLoopbackHost(url.hostname)) return options.infer_loopback
|
||||
return true
|
||||
}
|
||||
|
||||
function inferControls(baseDirectory) {
|
||||
const configured = readClientConfig()
|
||||
const controls = [...(configured.controls || [])]
|
||||
const token = process.env.OLLAMA_SWAP_TOKEN || null
|
||||
const interval = Number(configured.interval_seconds || process.env.OLLAMA_SWAP_INTERVAL || 60)
|
||||
|
||||
// Infer agent endpoints from OpenCode providers that point at an Ollama server.
|
||||
try {
|
||||
const directory = baseDirectory
|
||||
const configPath = ["opencode.json", "opencode.jsonc", path.join(".opencode", "opencode.json"), path.join(".opencode", "opencode.jsonc")]
|
||||
.map((name) => path.resolve(directory || process.cwd(), name))
|
||||
.find((candidate) => fs.existsSync(candidate))
|
||||
if (!configPath) throw new Error("no opencode config")
|
||||
const raw = fs.readFileSync(configPath, "utf8")
|
||||
const config = JSON.parse(stripJsonComments(raw))
|
||||
for (const provider of Object.values(config?.provider || {})) {
|
||||
const baseURL = provider?.options?.baseURL
|
||||
if (typeof baseURL !== "string") continue
|
||||
const match = baseURL.match(/^https?:\/\/([^:/]+)(?::\d+)?/)
|
||||
if (!match) continue
|
||||
const host = match[1]
|
||||
if (controls.some((item) => item.agent_url?.includes(host))) continue
|
||||
controls.push({
|
||||
name: provider?.name || host,
|
||||
host,
|
||||
agent_url: "http://" + host + ":8731",
|
||||
token,
|
||||
ollama_base_url: baseURL.replace(/\/v1\/?$/, ""),
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// OpenCode config is optional; explicit clients.json is enough.
|
||||
const options = {
|
||||
infer_providers: configured.infer_providers !== false,
|
||||
infer_loopback: configured.infer_loopback === true,
|
||||
}
|
||||
|
||||
return { controls, interval_seconds: interval > 0 ? interval : 60 }
|
||||
// Infer agent endpoints from OpenCode providers that point at an Ollama server.
|
||||
const inferred = []
|
||||
if (options.infer_providers) {
|
||||
try {
|
||||
const directory = baseDirectory
|
||||
const configPath = ["opencode.json", "opencode.jsonc", path.join(".opencode", "opencode.json"), path.join(".opencode", "opencode.jsonc")]
|
||||
.map((name) => path.resolve(directory || process.cwd(), name))
|
||||
.find((candidate) => fs.existsSync(candidate))
|
||||
if (!configPath) throw new Error("no opencode config")
|
||||
const raw = fs.readFileSync(configPath, "utf8")
|
||||
const config = JSON.parse(stripJsonComments(raw))
|
||||
// V2 `providers` wins over the deprecated `provider` block; both may exist.
|
||||
const providers = Object.entries({ ...(config?.provider || {}), ...(config?.providers || {}) })
|
||||
for (const [id, provider] of providers) {
|
||||
const baseURL = providerBaseURL(provider)
|
||||
const explicitAgent = providerAgentURL(provider)
|
||||
if (!baseURL && !explicitAgent) continue
|
||||
const host = hostOf(explicitAgent) || hostOf(baseURL)
|
||||
if (!host) continue
|
||||
if (!explicitAgent && !looksLikeSwapHost(baseURL, options)) continue
|
||||
if (controls.some((item) => hostOf(item.agent_url) === host || item.host === host)) continue
|
||||
if (inferred.some((item) => item.host === host)) continue
|
||||
inferred.push({
|
||||
name: provider?.name || id || host,
|
||||
host,
|
||||
agent_url: explicitAgent || "http://" + host + ":8731",
|
||||
token,
|
||||
ollama_base_url: baseURL ? baseURL.replace(/\/v1\/?$/, "") : null,
|
||||
inferred: true,
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// OpenCode config is optional; explicit clients.json is enough.
|
||||
}
|
||||
}
|
||||
|
||||
return { controls: [...controls, ...inferred], interval_seconds: interval > 0 ? interval : 60 }
|
||||
}
|
||||
|
||||
async function fetchStatus(control) {
|
||||
@@ -167,8 +230,10 @@ async function pollOnce(controls) {
|
||||
ollama_busy: status?.ollama_busy ?? null,
|
||||
})
|
||||
} catch (error) {
|
||||
logLine("warn", "agent unreachable: " + control.name + " (" + String(error) + ")", {
|
||||
const hint = control.inferred ? " — inferred from opencode config; set infer_providers: false in clients.json to silence" : ""
|
||||
logLine("warn", "agent unreachable: " + control.name + " (" + String(error) + ")" + hint, {
|
||||
control: control.name,
|
||||
inferred: Boolean(control.inferred),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user