middleware: param-override, model-map, system-prompt, word-guard and think-tags — gateway middleware that magpie runs on every agent's requests and replies, mostly New API's channel settings with the same JSON (the owner) — magpie can now run a plugin as gateway middleware (onRequest, onEvent, onResponse, in-process on moejs), and these are the first ones: param-override is New API's param_override (both forms, operations with gjson paths and conditions, return_error); model-map is model_mapping (chains, /regex/ keys with $1) and puts the asked-for name back into replies; system-prompt adds a prompt in each API's own place, keeping an agent's own unless override (New API's semantics), by agent or model; word-guard is the sensitive-word filter (reject or mask the user's text, optionally replies); think-tags strips a leading <think>…</think> from replies across stream chunks, or puts Chat reasoning_content into the text (thinking_to_content). Each is one file with no imports, named @magpie-community/middleware-<name>, with magpie.options as the example the app's editor starts from; registry.json lists them with "kind": "middleware" and no providers; check.mjs skips middleware-only packages. scripts/middleware.mjs calls the hooks as the gateway does (events split, [DONE] passed by) and runs each package's cases.json. Tested with a real DeepSeek account and real Claude Code through magpie's gateway: a mapped model named back in message_start, max_tokens capped, a system prompt appended for claude only, a word turned away as a 400 in Anthropic's shape, return_error as a 403 in Chat's, <think> stripped from Chat and Responses streams and an Anthropic whole reply, reasoning_content moved into the text. bun test (61 for these five) and bun scripts/check.mjs pass; packages/devin's two image tests fail on this machine only, as on main. Their first npm version has to be published by hand (trusted publishing can't create a package); until then magpie shows them as Coming soon.
This commit is contained in:
@@ -14,6 +14,8 @@ for (const name of readdirSync(root)) {
|
||||
const pj = join(dir, "package.json")
|
||||
if (!existsSync(pj)) continue
|
||||
const pkg = JSON.parse(readFileSync(pj, "utf8"))
|
||||
// gateway middleware has no OpenCode plugin; its own tests check it
|
||||
if (pkg.magpie?.middleware) continue
|
||||
const problems = []
|
||||
try {
|
||||
const mod = await import(pathToFileURL(join(dir, pkg.main ?? "index.mjs")).href)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Runs a gateway middleware's cases the way magpie's gateway calls it: one
|
||||
// ctx for the request's hooks (so ctx.state carries over), each body a fresh
|
||||
// copy of its JSON, undefined keeping what came in, null dropping an event.
|
||||
// The same cases.json runs in magpie's own JavaScript engine (moejs), so a
|
||||
// middleware is checked in both.
|
||||
import { readFileSync } from "node:fs"
|
||||
import { join } from "node:path"
|
||||
import { expect, test } from "bun:test"
|
||||
|
||||
export function cases(dir) {
|
||||
return JSON.parse(readFileSync(join(dir, "cases.json"), "utf8"))
|
||||
}
|
||||
|
||||
export async function run(mod, c) {
|
||||
const m = mod.default ?? mod
|
||||
let rejected
|
||||
const ctx = {
|
||||
protocol: "anthropic", model: "", stream: false, path: "", ...c.ctx,
|
||||
options: c.options ?? {}, state: {},
|
||||
reject(status, message) { rejected ??= { status, message } },
|
||||
}
|
||||
const copy = (v) => JSON.parse(JSON.stringify(v))
|
||||
const out = {}
|
||||
if (c.request) {
|
||||
const r = m.onRequest ? await m.onRequest(copy(c.request), ctx) : undefined
|
||||
if (rejected) out.reject = rejected
|
||||
else out.request = r === undefined ? c.request : copy(r)
|
||||
}
|
||||
if (c.events) {
|
||||
out.events = []
|
||||
for (const e of c.events) {
|
||||
const wants = !m.events || (e.event && m.events.includes(e.event))
|
||||
if (typeof e.data !== "object" || !m.onEvent || !wants) {
|
||||
out.events.push(e.data)
|
||||
continue
|
||||
}
|
||||
const r = await m.onEvent(copy(e.data), ctx)
|
||||
if (r === null) continue
|
||||
out.events.push(r === undefined ? e.data : copy(r))
|
||||
}
|
||||
}
|
||||
if (c.response) {
|
||||
ctx.status = c.status ?? 200
|
||||
const r = m.onResponse ? await m.onResponse(copy(c.response), ctx) : undefined
|
||||
out.response = r === undefined ? c.response : copy(r)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// check runs each case and compares it with what it expects: a request,
|
||||
// events or response not expected are expected to pass unchanged.
|
||||
export function check(dir, mod) {
|
||||
for (const c of cases(dir)) {
|
||||
test(c.name, async () => {
|
||||
const got = await run(mod, c)
|
||||
if (c.reject) {
|
||||
expect(got.reject?.status).toBe(c.reject.status)
|
||||
if (c.reject.message) expect(got.reject?.message).toContain(c.reject.message)
|
||||
} else {
|
||||
expect(got.reject).toBeUndefined()
|
||||
if (c.request) expect(got.request).toEqual(c.expect_request ?? c.request)
|
||||
}
|
||||
if (c.events) expect(got.events).toEqual(c.expect_events ?? c.events.map((e) => e.data))
|
||||
if (c.response) expect(got.response).toEqual(c.expect_response ?? c.response)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user