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)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { type Plugin, tool } from "@opencode-ai/plugin"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import { homedir } from "node:os"
|
||||
import path from "node:path"
|
||||
|
||||
interface Control {
|
||||
name: string
|
||||
agent_url: string
|
||||
token?: string | null
|
||||
}
|
||||
|
||||
async function controls() {
|
||||
const file = path.join(homedir(), ".config", "ollama-plagin-swap", "clients.json")
|
||||
try {
|
||||
const parsed = JSON.parse(await readFile(file, "utf8"))
|
||||
return Array.isArray(parsed?.controls) ? parsed.controls : []
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
async function request(control: Control, method: string, pathname: string, body?: unknown) {
|
||||
const response = await fetch(control.agent_url.replace(/\/$/, "") + pathname, {
|
||||
method,
|
||||
headers: {
|
||||
...(control.token ? { Authorization: "Bearer " + control.token } : {}),
|
||||
...(body ? { "Content-Type": "application/json" } : {}),
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
signal: AbortSignal.timeout(8000),
|
||||
})
|
||||
const text = await response.text()
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(text)
|
||||
} catch {
|
||||
parsed = text
|
||||
}
|
||||
if (!response.ok) throw new Error(control.name + ": HTTP " + response.status + " " + text)
|
||||
return parsed
|
||||
}
|
||||
|
||||
export const OllamaSwapPlugin: Plugin = async (ctx) => {
|
||||
return {
|
||||
event: async ({ event }) => {
|
||||
if (event.type !== "server.connected") return
|
||||
for (const control of await controls()) {
|
||||
try {
|
||||
const status = await request(control, "GET", "/status")
|
||||
await ctx.client.app.log({
|
||||
body: {
|
||||
service: "ollama-plagin-swap",
|
||||
level: status?.threshold_exceeded ? "warn" : "info",
|
||||
message: `${control.name}: swap ${status?.swap?.swap_used_mb}/${status?.swap?.swap_total_mb} MB (${status?.swap?.swap_percent}%), recommendation=${status?.recommendation}`,
|
||||
extra: { control: control.name, status },
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
await ctx.client.app.log({
|
||||
body: {
|
||||
service: "ollama-plagin-swap",
|
||||
level: "warn",
|
||||
message: `${control.name}: agent unreachable (${String(error)})`,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
tool: {
|
||||
ollama_swap_status: tool({
|
||||
description: "Read Ollama swap status from configured remote agents",
|
||||
args: {},
|
||||
async execute() {
|
||||
const result = []
|
||||
for (const control of await controls()) {
|
||||
try {
|
||||
result.push({ name: control.name, status: await request(control, "GET", "/status") })
|
||||
} catch (error) {
|
||||
result.push({ name: control.name, error: String(error) })
|
||||
}
|
||||
}
|
||||
return JSON.stringify(result, null, 2)
|
||||
},
|
||||
}),
|
||||
ollama_swap_restart: tool({
|
||||
description: "Restart a remote Ollama service after its swap threshold is exceeded",
|
||||
args: {
|
||||
name: tool.schema.string().describe("Agent name from clients.json"),
|
||||
force: tool.schema.boolean().optional().describe("Force restart even if models are active"),
|
||||
},
|
||||
async execute(args) {
|
||||
const control = (await controls()).find((item) => item.name === args.name)
|
||||
if (!control) throw new Error("unknown agent: " + args.name)
|
||||
return JSON.stringify(await request(control, "POST", "/restart", { force: Boolean(args.force), reason: "opencode" }), null, 2)
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user