106 lines
3.6 KiB
TypeScript
106 lines
3.6 KiB
TypeScript
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) => {
|
|
const startedKey = Symbol.for("ollamaPlaginSwapStarted")
|
|
if ((globalThis as any)[startedKey]) {
|
|
return {}
|
|
}
|
|
(globalThis as any)[startedKey] = true
|
|
|
|
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)
|
|
},
|
|
}),
|
|
},
|
|
}
|
|
}
|