Add ollama-plagin-swap remote swap control plugin
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
// ollama-plagin-swap: low-overhead remote Ollama swap monitor for OpenCode.
|
||||
const fs = require("node:fs")
|
||||
const os = require("node:os")
|
||||
const path = require("node:path")
|
||||
|
||||
const CLIENT_CONFIG = path.join(os.homedir(), ".config", "ollama-plagin-swap", "clients.json")
|
||||
const STARTED_KEY = Symbol.for("ollamaPlaginSwapStarted")
|
||||
|
||||
function readClientConfig() {
|
||||
try {
|
||||
const raw = fs.readFileSync(CLIENT_CONFIG, "utf8")
|
||||
const parsed = JSON.parse(raw)
|
||||
if (Array.isArray(parsed?.controls)) return parsed
|
||||
if (Array.isArray(parsed)) return { controls: parsed, interval_seconds: 60 }
|
||||
return { controls: [], interval_seconds: 60 }
|
||||
} catch {
|
||||
return { controls: [], interval_seconds: 60 }
|
||||
}
|
||||
}
|
||||
|
||||
function inferControls(ctx) {
|
||||
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 configPath = path.join(ctx.directory || process.cwd(), "opencode.json")
|
||||
const raw = fs.readFileSync(configPath, "utf8")
|
||||
const config = JSON.parse(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.
|
||||
}
|
||||
|
||||
return { controls, interval_seconds: interval > 0 ? interval : 60 }
|
||||
}
|
||||
|
||||
async function fetchStatus(control) {
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), 5000)
|
||||
try {
|
||||
const response = await fetch(control.agent_url.replace(/\/$/, "") + "/status", {
|
||||
headers: control.token ? { Authorization: "Bearer " + control.token } : {},
|
||||
signal: controller.signal,
|
||||
})
|
||||
if (!response.ok) throw new Error("HTTP " + response.status)
|
||||
return await response.json()
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
|
||||
export const OllamaSwapPlugin = async (ctx) => {
|
||||
if (globalThis[STARTED_KEY]) {
|
||||
return {}
|
||||
}
|
||||
globalThis[STARTED_KEY] = true
|
||||
|
||||
const { controls, interval_seconds } = inferControls(ctx)
|
||||
|
||||
async function pollOnce(client) {
|
||||
for (const control of controls) {
|
||||
try {
|
||||
const status = await fetchStatus(control)
|
||||
const summary =
|
||||
control.name +
|
||||
": swap " +
|
||||
(status?.swap?.swap_used_mb ?? "?") +
|
||||
"/" +
|
||||
(status?.swap?.swap_total_mb ?? "?") +
|
||||
" MB (" +
|
||||
(status?.swap?.swap_percent ?? "?") +
|
||||
"%), recommendation=" +
|
||||
(status?.recommendation ?? "unknown")
|
||||
await client.app.log({
|
||||
body: {
|
||||
service: "ollama-plagin-swap",
|
||||
level: status?.threshold_exceeded ? "warn" : "info",
|
||||
message: summary,
|
||||
extra: { control: control.name, status },
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
await client.app.log({
|
||||
body: {
|
||||
service: "ollama-plagin-swap",
|
||||
level: "warn",
|
||||
message: "agent unreachable: " + control.name + " (" + String(error) + ")",
|
||||
extra: { control: control.name },
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const timer = setInterval(() => {
|
||||
pollOnce(ctx.client).catch(() => {})
|
||||
}, interval_seconds * 1000)
|
||||
|
||||
return {
|
||||
event: async ({ event }) => {
|
||||
if (event.type === "server.connected") {
|
||||
await pollOnce(ctx.client)
|
||||
}
|
||||
if (event.type === "session.deleted") {
|
||||
clearInterval(timer)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user