Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

middleware: onProvider changes what one provider a request is tried on is sent

yetonee44b9aef331d
diff --git a/docs/integrating.md b/docs/integrating.md
index 349f54a7b..646cac000 100644
--- a/docs/integrating.md
+++ b/docs/integrating.md
@@ -141,7 +141,7 @@ kinds ([plugin guide](https://usemagpie.ai/docs/plugins)):
source of models, such as a subscription magpie doesn't sign in to
itself. They sign in, list models and make requests.
- **Gateway middleware**: rewrites requests and replies as they pass
- (`onRequest`, `onEvent`, `onResponse`).
+ (`onRequest`, `onProvider`, `onEvent`, `onResponse`).
A plugin can't add a row to the Agents page or write another app's
config, so it is not how an app registers itself.
diff --git a/docs/reference.md b/docs/reference.md
index 5ecc4f8dd..bfd30b4f3 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -946,7 +946,10 @@ the hook as a variable (or cast it), or put the icon in `package.json`.
#### Gateway middleware
A plugin can also be gateway middleware: `onRequest(body, ctx)` sees each
-request an agent sends before it is routed, `onEvent(event, ctx)` each
+request an agent sends before it is routed, `onProvider(body, ctx)` the
+body each provider it is tried on is sent (`ctx.provider` has its `id`,
+`name`, `account` and `model`; each try starts from what `onRequest`
+left), `onEvent(event, ctx)` each
event of a streamed reply and `onResponse(body, ctx)` a whole one, all in
the agent's own API. A hook returns the changed object, or nothing to
leave it; `onEvent` returns `null` to drop an event, and
@@ -999,7 +1002,9 @@ Ready-made middleware, under Plugins › Discover › Gateway middleware
(`@magpie-community/middleware-<name>`), mostly New API's channel settings
with the same JSON: `param-override` (`param_override`), `model-map`
(`model_mapping`), `system-prompt`, `word-guard` (sensitive words) and
-`think-tags` (strip `<think>…</think>`, or `thinking_to_content`). A
+`think-tags` (strip `<think>…</think>`, or `thinking_to_content`), and
+`antigravity-cloak`, which keeps the words Antigravity turns Claude Code's
+system prompt away for out of what it sends Antigravity alone. A
middleware's entry in the community `registry.json` has
`"kind": "middleware"` and no `providers`.
diff --git a/docs/subsystems/gateway-middleware.md b/docs/subsystems/gateway-middleware.md
index 78a627256..fbd09415f 100644
--- a/docs/subsystems/gateway-middleware.md
+++ b/docs/subsystems/gateway-middleware.md
@@ -14,7 +14,7 @@ A plugin can be a JS gateway plugin: JavaScript that magpie runs inside its gate
| GUI and CLI | Show each middleware's hooks, calls, µs per call, failures and load error; its routings and how often they were asked; its usage rows and those dropped. The routings served are `jsRoutings` in the settings state, offered in every routing picker; `magpie group set <id> routing=<plugin>/<name>` takes one | `middleware.States()`; `pluginEntryJSON.Middleware` in [`internal/gui/plugins.go`](../../internal/gui/plugins.go), `mwLine` in `plugins.js`, `listPlugins` in [`plugins_cli.go`](../../internal/cli/plugins_cli.go) |
| Options | A middleware's `options` in plugins.json, as `ctx.options`; set by name, spec, short name or the name without its scope (`plugin.Called`: agent-aider, which was its short name before `agent-`, `tools-` and `page-` came off too); a package's `magpie.options` is only the editor's example | `SetOptions`, `ShortName` in [`store.go`](../../internal/plugin/store.go); `pluginOptions` in `plugins_cli.go`; `optionsEditor` in `plugins.js` |
| Kind | Whether a plugin is a provider, middleware or both, shown on every card and row, also for one that is off | `isMiddleware`, `middlewareOnly` in `pluginEntryJSON`; `kind: "middleware"` market entries (`market.json`, the community `registry.json`); `kindChip` in `plugins.js` |
-| Community middleware | Ready-made packages (`@magpie-community/middleware-<name>`), mostly New API's channel settings | `packages/{param-override,model-map,system-prompt,word-guard,think-tags}` in [magpie-community/plugins](https://github.com/magpie-community/plugins), tested with `scripts/middleware.mjs` |
+| Community middleware | Ready-made packages (`@magpie-community/middleware-<name>`), mostly New API's channel settings | `packages/{param-override,model-map,system-prompt,word-guard,think-tags,antigravity-cloak}` in [magpie-community/plugins](https://github.com/magpie-community/plugins), tested with `scripts/middleware.mjs` |
| User docs | The hooks, `ctx` and an example for plugin authors | [usemagpie.ai/docs/plugins#middleware](https://usemagpie.ai/docs/plugins#middleware) (`site/public/docs/*plugins.html`) |
plugins.json is the source of truth for which middleware is installed and on, in its order; a middleware that is off isn't loaded.
@@ -23,8 +23,9 @@ plugins.json is the source of truth for which middleware is installed and on, in
1. `serveAgent` establishes the [gateway session identity and optional recording](gateway-sessions.md), runs any [asynchronous request hooks](gateway-request-hooks.md) through Bun, then calls `Begin` on each registered `gwmw.Agent` (the JS chain's is `middleware.Begin`) with the request's protocol, model, stream flag, path and agent. With no middleware it calls `serve` directly.
2. `Run.Request` passes the agent's body, in its own API, to each `onRequest` in turn. An object returned is the body the next middleware and the gateway get; `undefined` keeps the bytes as they were. `ctx.reject(status, message)` stops the chain, and the agent gets that error in its API's shape (`writeError`).
-3. `Run.Wrap` wraps the response writer when some middleware has `onEvent` or `onResponse`. An SSE reply is split into events. Each event whose data is a JSON object goes to every `onEvent` that wants its name (`export const events`): an object replaces it, `null` drops it, and `undefined` writes the original bytes. `data: [DONE]` and other non-object data pass by. A whole JSON reply is buffered for `onResponse`, with `ctx.status` set. A reply with a `Content-Encoding` passes as-is.
-4. `Run.End` returns the runtimes to their pools.
+3. `Run.Tried` runs each `onProvider` on the body each time `serve` tries it on a provider, just before that try is sent (`triedOn` in the candidate loop of `gateway.go`; `serveAgent` puts the runs that are `gwmw.Tried` on the request's context). `ctx.provider` says which: `id`, `name`, `account` (the subscription's kind, as `accountAgent` gives it: `claude`, `antigravity`, …; absent for a key) and `model`, the model's id there. Every try starts again from the body `onRequest` left, so what one provider is sent changes no other try's: this is how a plugin acts on one vendor only, for an agent such as Claude Desktop whose model names say nothing of where they go. `null` and `ctx.reject` turn nothing away here; the request has been routed.
+4. `Run.Wrap` wraps the response writer when some middleware has `onEvent` or `onResponse`. An SSE reply is split into events. Each event whose data is a JSON object goes to every `onEvent` that wants its name (`export const events`): an object replaces it, `null` drops it, and `undefined` writes the original bytes. `data: [DONE]` and other non-object data pass by. A whole JSON reply is buffered for `onResponse`, with `ctx.status` set. A reply with a `Content-Encoding` passes as-is.
+5. `Run.End` returns the runtimes to their pools.
Each middleware gets one runtime for the whole request, so its hooks share `ctx.state`. Runtimes come from a per-middleware `sync.Pool` and modules are compiled once per load. Compiling a plugin's module (imports kept inside the plugin) is [`internal/jsmod`](../../internal/jsmod/jsmod.go), which agent plugins ([Agent wiring](agent-wiring.md)) share.
@@ -41,8 +42,8 @@ Only requests agents send go through `serveAgent`. magpie's own requests (thread
## Constraints and failure behavior
-- Limits: 250ms for `onRequest` and `onResponse`, 50ms for each `onEvent`, 1s to load. A hook past its limit is interrupted.
-- Fail open: a hook that throws, returns something that is neither an object, `null` nor `undefined`, or is interrupted leaves the request or event as it was. The failure is counted and kept as the last error, and logged at most 60 lines a minute.
+- Limits: 250ms for `onRequest`, each `onProvider` and `onResponse`, 50ms for each `onEvent`, 1s to load. A hook past its limit is interrupted.
+- Fail open: a hook that throws, returns something that is neither an object, `null` nor `undefined` (an `onProvider` returning `null` too), or is interrupted leaves the request or event as it was. The failure is counted and kept as the last error, and logged at most 60 lines a minute.
- A middleware imports only relative files; packages must be bundled into it.
- Reload: a change to plugins.json or to a middleware file is picked up by the next request at most a second later (`stamp`).
- A strategy has 20ms, waiting for its runtime included; one that throws, runs out of time or returns what isn't an order of the seats leaves them in their listed order (fail open), counted and logged as a failure. `onUsage` has 250ms and fails open the same way. A request never waits for `onUsage`.
@@ -58,11 +59,11 @@ go test -tags nogui -run 'TestParseRouting|TestPluginListSaysMiddleware' ./inter
go test -tags nogui -run TestPluginRoutingKept ./internal/provider
go test -tags nogui -run TestOnServesStrategies ./plugins/middleware
node --test internal/gui/tests/routing-js.test.cjs
-go test -tags nogui -run TestMiddlewareThroughGateway ./internal/gateway
+go test -tags nogui -run 'TestMiddlewareThroughGateway|TestMiddlewareOnProviderPerTry' ./internal/gateway
go test -tags nogui -run TestPluginListSaysMiddleware .
go test -tags nogui -run "TestSetOptionsShortName|TestBuiltinMarket" ./internal/plugin
go test -tags nogui -bench Event ./internal/middleware
node --test internal/gui/tests/plugin-middleware.test.cjs
# in magpie-community/plugins: each package runs its cases.json through the hooks
-bun test packages/think-tags packages/model-map packages/param-override packages/system-prompt packages/word-guard
+bun test packages/think-tags packages/model-map packages/param-override packages/system-prompt packages/word-guard packages/antigravity-cloak
```
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index c5ffb1a9a..3894c35f3 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -2403,6 +2403,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
attemptBody, _ = textOnlyBody(from, body) // omit images in prior turns and tool results
}
}
+ // a middleware's onProvider changes what this provider is sent
+ attemptBody = triedOn(r.Context(), c.p, c.model, attemptBody)
if !last && s.antigravityForeseen(c.p, from, attemptBody) {
// Antigravity would only turn it away, as it did lately, and
// someone after it may answer: it isn't asked, and nothing is
diff --git a/internal/gateway/middleware.go b/internal/gateway/middleware.go
index 59006e9e3..504cd40b5 100644
--- a/internal/gateway/middleware.go
+++ b/internal/gateway/middleware.go
@@ -1,6 +1,7 @@
package gateway
import (
+ "context"
"net/http"
"slices"
@@ -30,12 +31,16 @@ func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provide
return
}
info := gwmw.Info{Protocol: string(from), Model: modelOf(body), Stream: streamOf(body), Path: r.URL.Path, Agent: agentOf(r)}
+ var tried []gwmw.Tried
for _, a := range agents {
run := a.Begin(info)
if run == nil {
continue
}
defer run.End()
+ if t, ok := run.(gwmw.Tried); ok {
+ tried = append(tried, t)
+ }
var turned *gwmw.Reject
body, turned = run.Request(body)
if turned != nil {
@@ -46,9 +51,30 @@ func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provide
w, done = run.Wrap(w)
defer done()
}
+ if len(tried) > 0 {
+ r = r.WithContext(context.WithValue(r.Context(), triedKey{}, tried))
+ }
s.serve(w, r, from, body)
}
+type triedKey struct{}
+
+// triedOn is body as it goes to model on p, in the agent's API: what each
+// middleware of the request that is also gwmw.Tried (the JS chain's
+// onProvider) made of it for that provider. Each try starts from the body
+// it is given, so a change for one provider never reaches another's.
+func triedOn(ctx context.Context, p provider.Provider, model string, body []byte) []byte {
+ tried, _ := ctx.Value(triedKey{}).([]gwmw.Tried)
+ if len(tried) == 0 {
+ return body
+ }
+ to := gwmw.To{ID: p.ID, Name: p.Name, Account: accountAgent(p), Model: model}
+ for _, t := range tried {
+ body = t.Tried(body, to)
+ }
+ return body
+}
+
// middlewareWanted: plugins.json has a JS middleware on, so plugins/middleware
// is in use.
func middlewareWanted() bool { return middleware.On() > 0 }
diff --git a/internal/gateway/middleware_test.go b/internal/gateway/middleware_test.go
index 50ed710eb..8984d70d9 100644
--- a/internal/gateway/middleware_test.go
+++ b/internal/gateway/middleware_test.go
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "slices"
"strings"
"testing"
@@ -95,3 +96,62 @@ export function onEvent(ev, ctx) {
t.Fatalf("state %+v", st)
}
}
+
+// onProvider changes the body of one try only: a group's first member is
+// sent what the middleware made of it for that member, and the member
+// tried next gets the body onRequest left, not the first try's.
+func TestMiddlewareOnProviderPerTry(t *testing.T) {
+ sent := map[string]string{}
+ member := func(id string, code int) {
+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var b struct {
+ System string `json:"system"`
+ }
+ raw, _ := io.ReadAll(r.Body)
+ json.Unmarshal(raw, &b)
+ sent[id] = b.System
+ if code != 200 {
+ http.Error(w, `{"type":"error","error":{"type":"api_error","message":"down"}}`, code)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ io.WriteString(w, `{"id":"m","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`)
+ }))
+ t.Cleanup(up.Close)
+ if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Models: []string{"m1"}, Anthropic: up.URL}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ t.Setenv("XDG_CACHE_HOME", t.TempDir())
+ t.Cleanup(plugin.Settle)
+ member("a", 500)
+ member("b", 200)
+ if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m1", "b/m1"}, Routing: provider.Ordered}); err != nil {
+ t.Fatal(err)
+ }
+ mw := filepath.Join(t.TempDir(), "per-try.middleware.js")
+ os.WriteFile(mw, []byte(`
+export function onRequest(body) { body.system = "asked"; return body; }
+export function onProvider(body, ctx) {
+ const p = ctx.provider;
+ if (p.id === "a") body.system += " for " + [p.id, p.name, p.model, p.account || "key"].join("/");
+ return body;
+}`), 0o644)
+ b, _ := json.Marshal(plugin.List{Plugins: []plugin.Entry{{Spec: mw}}})
+ os.MkdirAll(settings.Dir(), 0o755)
+ os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644)
+ middleware.Reload()
+ defer middleware.Reload()
+
+ code, body := post(t, "/v1/messages", `{"model":"group/g","max_tokens":10,"system":"agent's","messages":[{"role":"user","content":"hi"}]}`)
+ if code != 200 {
+ t.Fatalf("%d %s", code, body)
+ }
+ if sent["a"] != "asked for a/A/m1/key" || sent["b"] != "asked" {
+ t.Fatalf("sent %q", sent)
+ }
+ if st := middleware.States()[mw]; st.Error != "" || st.Last != "" || !slices.Contains(st.Hooks, "onProvider") {
+ t.Fatalf("state %+v", st)
+ }
+}
diff --git a/internal/gwmw/gwmw.go b/internal/gwmw/gwmw.go
index 72fa0e645..9342455a0 100644
--- a/internal/gwmw/gwmw.go
+++ b/internal/gwmw/gwmw.go
@@ -7,7 +7,8 @@
// gateway routes it, and its reply as the agent gets it. The JS gateway
// middleware (internal/middleware) is the one there, registered by
// plugins/middleware. It fails open: a middleware that fails leaves the
-// request as it was.
+// request as it was. A Run that is also Tried changes the body again
+// for each provider the gateway tries the request on.
// - Masker: what magpie sends to a vendor, masked as the settings say,
// and what the vendor answers with the values back. The redact plugin
// registers it. It fails closed: when the settings ask for masking and
@@ -56,6 +57,21 @@ type Run interface {
End()
}
+// To is the provider a request is tried on, as a middleware is told it.
+type To struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Account string `json:"account,omitempty"` // the subscription's kind (claude, antigravity, …); empty for a key
+ Model string `json:"model"` // the model asked of it, by its id there
+}
+
+// Tried is a Run that also changes the body each provider the request is
+// tried on is sent: Tried is that try's body, in the agent's API, and
+// changes no other try's.
+type Tried interface {
+ Tried(body []byte, to To) []byte
+}
+
// Masker masks what magpie sends to a vendor and puts the values back in
// what comes back. It reads its settings itself; Wanted is the core's
// reading of whether any are asked for.
diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go
index ceb115870..cab7bc670 100644
--- a/internal/middleware/middleware.go
+++ b/internal/middleware/middleware.go
@@ -1,7 +1,7 @@
// Package middleware runs the gateway middleware plugins declare: a
-// module with onRequest, onEvent and onResponse hooks that the gateway
-// calls on every request an agent sends it, every event of the reply's
-// stream and every whole reply.
+// module with onRequest, onProvider, onEvent and onResponse hooks that the
+// gateway calls on every request an agent sends it, each provider it tries
+// the request on, every event of the reply's stream and every whole reply.
//
// It runs in the gateway's own process, in moejs, a JavaScript engine
// written in Go: a hook on one streamed event costs about a microsecond,
@@ -44,9 +44,11 @@ type mw struct {
spec, name string
mod *moejs.Module
onReq moejs.Hook
+ onProv moejs.Hook
onEv moejs.Hook
onResp moejs.Hook
hasReq bool
+ hasProv bool
hasEv bool
hasResp bool
// events are the stream events onEvent is called for, by name; nil
@@ -72,7 +74,8 @@ type set struct {
stamp string
list []*mw
errs map[string]string // by spec: why it didn't load
- // hooked: some module of list has onRequest, onEvent or onResponse
+ // hooked: some module of list has onRequest, onProvider, onEvent or
+ // onResponse
hooked bool
droppedSaid atomic.Int64
}
@@ -154,7 +157,7 @@ func load(st string) *set {
continue
}
s.list = append(s.list, m)
- s.hooked = s.hooked || m.hasReq || m.hasEv || m.hasResp
+ s.hooked = s.hooked || m.hasReq || m.hasProv || m.hasEv || m.hasResp
}
return s
}
@@ -183,13 +186,14 @@ func compile(e plugin.Entry, file string) (*mw, error) {
return nil, err
}
m.onReq, m.hasReq = in.hook("onRequest")
+ m.onProv, m.hasProv = in.hook("onProvider")
m.onEv, m.hasEv = in.hook("onEvent")
m.onResp, m.hasResp = in.hook("onResponse")
if err := m.discover(in); err != nil {
return nil, err
}
- if !m.hasReq && !m.hasEv && !m.hasResp && len(m.strats) == 0 && !m.hasUse {
- return nil, errors.New("exports none of onRequest, onEvent, onResponse, strategies and onUsage")
+ if !m.hasReq && !m.hasProv && !m.hasEv && !m.hasResp && len(m.strats) == 0 && !m.hasUse {
+ return nil, errors.New("exports none of onRequest, onProvider, onEvent, onResponse, strategies and onUsage")
}
if names, ok := in.strings("events"); ok {
m.events = map[string]bool{}
@@ -441,7 +445,7 @@ func States() map[string]State {
for _, h := range []struct {
ok bool
name string
- }{{m.hasReq, "onRequest"}, {m.hasEv, "onEvent"}, {m.hasResp, "onResponse"}} {
+ }{{m.hasReq, "onRequest"}, {m.hasProv, "onProvider"}, {m.hasEv, "onEvent"}, {m.hasResp, "onResponse"}} {
if h.ok {
st.Hooks = append(st.Hooks, h.name)
}
diff --git a/internal/middleware/middleware_test.go b/internal/middleware/middleware_test.go
index f42d372e7..23c9f395f 100644
--- a/internal/middleware/middleware_test.go
+++ b/internal/middleware/middleware_test.go
@@ -6,11 +6,13 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "slices"
"strings"
"sync"
"testing"
"time"
+ "github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
@@ -172,6 +174,57 @@ export function onRequest(body) {
}
}
+func TestOnProvider(t *testing.T) {
+ e := file(t, `
+export function onProvider(body, ctx) {
+ const p = ctx.provider;
+ if (p.account === "antigravity") body.system = body.system.replace("SDK", "S\u200bDK");
+ if (p.id === "throw") throw new Error("bad");
+ if (p.id === "null") return null;
+ if (p.id === "reject") ctx.reject(429, "no");
+ body.to = p.id + "/" + p.model;
+ return body;
+}`)
+ use(t, e)
+ r := Begin(info())
+ if r == nil {
+ t.Fatal("a middleware with only onProvider began no run")
+ }
+ defer r.End()
+ in := `{"system":"Claude Agent SDK"}`
+ // onRequest isn't exported, so the agent's body goes on as it is
+ if body, rej := r.Request([]byte(in)); string(body) != in || rej != nil {
+ t.Fatalf("request %s %v", body, rej)
+ }
+ for _, c := range []struct {
+ to gwmw.To
+ want string
+ }{
+ {gwmw.To{ID: "ag", Account: "antigravity", Model: "gemini-3-flash"}, `{"system":"Claude Agent S\u200bDK","to":"ag/gemini-3-flash"}`},
+ // the next try starts from the agent's body, not the last try's
+ {gwmw.To{ID: "k", Model: "m1"}, `{"system":"Claude Agent SDK","to":"k/m1"}`},
+ {gwmw.To{ID: "throw"}, in},
+ {gwmw.To{ID: "null"}, in},
+ // a reject has nothing to turn away once the request is routed
+ {gwmw.To{ID: "reject", Model: "m"}, `{"system":"Claude Agent SDK","to":"reject/m"}`},
+ } {
+ var got, want any
+ json.Unmarshal(r.Tried([]byte(in), c.to), &got)
+ json.Unmarshal([]byte(c.want), &want)
+ if g, _ := json.Marshal(got); string(g) != func() string { w, _ := json.Marshal(want); return string(w) }() {
+ t.Fatalf("%s: got %s, want %s", c.to.ID, g, c.want)
+ }
+ }
+ st := States()[e.Spec]
+ if st.Failures != 2 || !slices.Contains(st.Hooks, "onProvider") || !strings.Contains(st.Last, "null") {
+ t.Fatalf("state %+v", st)
+ }
+ var none *Run
+ if body := none.Tried([]byte(in), gwmw.To{}); string(body) != in {
+ t.Fatalf("a nil Run changed %s", body)
+ }
+}
+
func TestAsyncHook(t *testing.T) {
use(t, file(t, `
export async function onRequest(body) {
diff --git a/internal/middleware/points_test.go b/internal/middleware/points_test.go
index 60cae2742..6c968b4a9 100644
--- a/internal/middleware/points_test.go
+++ b/internal/middleware/points_test.go
@@ -292,7 +292,7 @@ func TestPointsLoadErrors(t *testing.T) {
def := file(t, `export default { strategies: { d: { order: (p, s) => s.map((_, i) => i) } } };`)
use(t, none, badName, notFn, notObj, def)
st := States()
- if !strings.Contains(st[none.Spec].Error, "onRequest, onEvent, onResponse, strategies and onUsage") {
+ if !strings.Contains(st[none.Spec].Error, "onRequest, onProvider, onEvent, onResponse, strategies and onUsage") {
t.Fatal(st[none.Spec].Error)
}
for _, e := range []plugin.Entry{badName, notFn, notObj} {
diff --git a/internal/middleware/run.go b/internal/middleware/run.go
index a8deae38c..2c0a0ffec 100644
--- a/internal/middleware/run.go
+++ b/internal/middleware/run.go
@@ -149,6 +149,46 @@ func (r *Run) Request(body []byte) ([]byte, *Reject) {
return body, nil
}
+// Tried runs each onProvider on body as it goes to one provider, in turn,
+// with ctx.provider saying which (gwmw.To): what one returns is the body
+// the next is given and that try is sent, and undefined leaves it as it
+// was. Another try, on another provider or the same one, starts again from
+// the body onRequest left. A hook that throws, takes too long or returns
+// null leaves it as it was; ctx.reject turns nothing away here.
+func (r *Run) Tried(body []byte, to gwmw.To) []byte {
+ if r == nil {
+ return body
+ }
+ b, _ := json.Marshal(to)
+ for i, m := range r.set.list {
+ if !m.hasProv {
+ continue
+ }
+ in, ctx, err := r.acquire(i)
+ if err == nil {
+ var v moejs.Value
+ if v, err = in.rt.ParseJSON(b); err == nil {
+ realm := in.rt.Realm()
+ err = realm.SetV(ctx, realm.KeyFromGoString("provider"), v)
+ }
+ }
+ if err != nil {
+ m.fail("onProvider", err)
+ continue
+ }
+ out, err := r.apply(i, m.onProv, requestLimit, body, nil)
+ switch {
+ case errors.Is(err, errDropped):
+ m.fail("onProvider", errors.New("returned null; a request can't be dropped"))
+ case err != nil:
+ m.fail("onProvider", err)
+ case out != nil:
+ body = out
+ }
+ }
+ return body
+}
+
var errDropped = errors.New("dropped")
// apply calls h on the JSON in data; the JSON it returned, nil for
diff --git a/site/public/docs/ja/plugins.html b/site/public/docs/ja/plugins.html
index 8a2db737a..f30c33f8f 100644
--- a/site/public/docs/ja/plugins.html
+++ b/site/public/docs/ja/plugins.html
@@ -651,6 +651,7 @@ m plugin logout acme</pre>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/system-prompt"><code>system-prompt</code></a></td><td>すべてのリクエスト(または特定のエージェントやモデルのもの)に自分のシステムプロンプトを、各 API の決まった場所に加えます。リクエストに独自のものがあれば、<code>override</code> を有効にしない限りそのままにします。</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/word-guard"><code>word-guard</code></a></td><td>New API の禁止語フィルタ:ユーザーが送る内容にある語やパターンを断るか伏せ字にします。応答も伏せ字にできます。</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/think-tags"><code>think-tags</code></a></td><td>応答本文の先頭にある <code>&lt;think&gt;…&lt;/think&gt;</code> を取り除くか、Chat の応答の <code>reasoning_content</code> を本文に入れます(New API の <code>thinking_to_content</code>)。</td></tr>
+ <tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/antigravity-cloak"><code>antigravity-cloak</code></a></td><td>Antigravity が Claude Code と Agent SDK のシステムプロンプトを断る理由になる語を、Antigravity に送る分だけで分割します。他のプロバイダには元のプロンプトのまま送ります。</td></tr>
</table></div>
<div class="code">
<span class="lang">shell</span>
@@ -673,13 +674,14 @@ magpie plugin options model-map '{"mapping": {"fast": "deepseek/deepseek-chat"}}
<div class="table-wrap"><table>
<tr><th>フック</th><th>渡されるもの</th><th>戻り値</th></tr>
<tr><td><code>onRequest(body, ctx)</code></td><td>ルーティング前のリクエスト本文。<code>model</code> を変えると別の宛先にルーティングされます。</td><td>本文、または何も返さない。<code>ctx.reject(status, message)</code> はリクエストを断り、そのエラーをエージェントの API の形式で返します(ステータスを省くと 403)。</td></tr>
+ <tr><td><code>onProvider(body, ctx)</code></td><td>あるプロバイダに送る直前のリクエスト本文。<code>ctx.provider</code> にはその <code>id</code>、<code>name</code>、<code>account</code>(サブスクリプションの種類。<code>claude</code> や <code>antigravity</code> など。API キーにはありません)と <code>model</code> があります。リクエストを試すプロバイダごとに呼ばれ、毎回 <code>onRequest</code> が残した本文を受け取るので、あるプロバイダ向けの変更は他のプロバイダに届きません。</td><td>本文、または何も返さない。この時点でルーティングは済んでいるので、拒否も行き先の変更もできません。</td></tr>
<tr><td><code>onEvent(event, ctx)</code></td><td>ストリーム応答の各イベントの <code>data:</code> JSON。</td><td>イベント、何も返さない、またはイベントを落とす <code>null</code>。</td></tr>
<tr><td><code>onResponse(body, ctx)</code></td><td>ストリームでない応答の全体。<code>ctx.status</code> がそのステータスです。</td><td>本文、または何も返さない。</td></tr>
</table></div>
<p><code>export const events = ["content_block_delta"]</code> と書くと、<code>onEvent</code> はその名前のイベントだけで呼ばれます(Anthropic Messages と Responses のイベントには名前があります)。Chat Completions と Gemini のイベントには名前がないので、それらでは <code>events</code> を書かないでください。1 つのイベントは応答の一部なので、1 つの単語が 2 つのイベントに分かれることがあります。<code>data: [DONE]</code> の行とコメントはフックを通らずにそのまま流れます。圧縮された応答もそのまま流れます。</p>
<p><code>ctx</code> には次のものがあります:<code>protocol</code>(<code>anthropic</code>・<code>chat</code>・<code>responses</code>・<code>gemini</code>)、エージェントが指定した <code>model</code> と <code>stream</code>、<code>path</code>、magpie が把握している場合の <code>agent</code>、<code>options</code>(そのプラグインの<a href="#middleware-options">オプション</a>)、そしてリクエストごとのオブジェクト <code>state</code>。</p>
<h3>フックが失敗したとき</h3>
- <p>フックの持ち時間は、リクエストまたは応答全体で 250 ms、1 イベントで 50 ms、読み込みで 1 秒です。例外を投げたフック、時間切れになったフック、想定外の値を返したフックは、なかったものとして扱われます。つまりリクエストやイベントは元のまま送られます。設定 → プラグインには、ミドルウェアごとのフック、呼び出し回数、1 回あたりの平均時間、失敗回数と直近のエラーが表示されます。フックが見るのは、エージェントが送ったものと受け取るものそのものです。送信時は magpie のマスキングより前、受信時はマスク解除の後です。magpie 自身のリクエスト(スレッドのタイトル、ルーターの分類器)はミドルウェアを通りません。</p>
+ <p>フックの持ち時間は、リクエスト、プロバイダへの 1 回の試行、または応答全体で 250 ms、1 イベントで 50 ms、読み込みで 1 秒です。例外を投げたフック、時間切れになったフック、想定外の値を返したフックは、なかったものとして扱われます。つまりリクエストやイベントは元のまま送られます。設定 → プラグインには、ミドルウェアごとのフック、呼び出し回数、1 回あたりの平均時間、失敗回数と直近のエラーが表示されます。フックが見るのは、エージェントが送ったものと受け取るものそのものです。送信時は magpie のマスキングより前、受信時はマスク解除の後です。magpie 自身のリクエスト(スレッドのタイトル、ルーターの分類器)はミドルウェアを通りません。</p>
<h3 id="gateway-points"><a class="anchor" href="#gateway-points">ルーティングと使用量の行</a></h3>
<p>同じモジュールは <code>strategies</code> と <code>onUsage</code> もエクスポートできます。そのファイルは <code>*.gateway.js</code> と名付けてもよく、パッケージなら <code>"magpie": { "gateway": "./gw.js" }</code> と書けます。</p>
diff --git a/site/public/docs/plugins.html b/site/public/docs/plugins.html
index 79ca75c93..9224a888b 100644
--- a/site/public/docs/plugins.html
+++ b/site/public/docs/plugins.html
@@ -651,6 +651,7 @@ m plugin logout acme</pre>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/system-prompt"><code>system-prompt</code></a></td><td>A system prompt of yours on every request, or only some agents’ or models’, in each API’s own place. A request with its own keeps it unless <code>override</code> is on.</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/word-guard"><code>word-guard</code></a></td><td>New API’s sensitive-word filter: turn away or mask words and patterns in what users send, and in replies if you like.</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/think-tags"><code>think-tags</code></a></td><td>Take a <code>&lt;think&gt;…&lt;/think&gt;</code> block out of the start of a reply’s text, or put a Chat reply’s <code>reasoning_content</code> into it (New API’s <code>thinking_to_content</code>).</td></tr>
+ <tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/antigravity-cloak"><code>antigravity-cloak</code></a></td><td>Breaks up the words Antigravity turns Claude Code’s and the Agent SDK’s system prompts away for, in what it sends Antigravity only. Other providers are sent the prompt as it was.</td></tr>
</table></div>
<div class="code">
<span class="lang">shell</span>
@@ -673,13 +674,14 @@ magpie plugin options model-map '{"mapping": {"fast": "deepseek/deepseek-chat"}}
<div class="table-wrap"><table>
<tr><th>Hook</th><th>Is given</th><th>Returns</th></tr>
<tr><td><code>onRequest(body, ctx)</code></td><td>The request body, before routing: a new <code>model</code> routes it elsewhere.</td><td>The body, or nothing. <code>ctx.reject(status, message)</code> turns the request away with that error, in the agent’s API (403 when no status is given).</td></tr>
+ <tr><td><code>onProvider(body, ctx)</code></td><td>The request body as it is about to go to one provider; <code>ctx.provider</code> has its <code>id</code>, <code>name</code>, <code>account</code> (the subscription’s kind, such as <code>claude</code> or <code>antigravity</code>; absent for an API key) and <code>model</code>. It is called again for each provider a request is tried on, each time with the body <code>onRequest</code> left, so a change for one provider reaches no other.</td><td>The body, or nothing. The request is routed by now: it can’t be turned away or sent elsewhere.</td></tr>
<tr><td><code>onEvent(event, ctx)</code></td><td>Each event of a streamed reply: its <code>data:</code> JSON.</td><td>The event, nothing, or <code>null</code> to leave the event out.</td></tr>
<tr><td><code>onResponse(body, ctx)</code></td><td>A reply that isn’t streamed, whole; <code>ctx.status</code> is its status.</td><td>The body, or nothing.</td></tr>
</table></div>
<p><code>export const events = ["content_block_delta"]</code> calls <code>onEvent</code> only for events of those names (Anthropic Messages and Responses name theirs). Chat Completions and Gemini events have no name, so leave <code>events</code> out for them. An event holds a piece of the reply, so a word can be split over two events. A <code>data: [DONE]</code> line and comments pass by unseen. A compressed reply passes as it is.</p>
<p><code>ctx</code> has <code>protocol</code> (<code>anthropic</code>, <code>chat</code>, <code>responses</code> or <code>gemini</code>), <code>model</code> and <code>stream</code> as the agent asked, <code>path</code>, <code>agent</code> when magpie knows it, <code>options</code> (the plugin’s <a href="#middleware-options">options</a>) and <code>state</code>, an object of the request’s own.</p>
<h3>When a hook fails</h3>
- <p>A hook has 250 ms for a request or a whole reply and 50 ms for an event; loading has a second. A hook that throws, runs out of time or returns something else goes on as if it weren’t there: the request or the event is sent as it was. Settings → Plugins shows each middleware’s hooks, how many calls it had, the average time each took and how many failed, with the last error. The hooks see what the agent sent and gets back, before magpie’s redaction on the way out and after it on the way back. magpie’s own requests (thread titles, the router’s classifier) don’t go through middleware.</p>
+ <p>A hook has 250 ms for a request, a provider’s try or a whole reply and 50 ms for an event; loading has a second. A hook that throws, runs out of time or returns something else goes on as if it weren’t there: the request or the event is sent as it was. Settings → Plugins shows each middleware’s hooks, how many calls it had, the average time each took and how many failed, with the last error. The hooks see what the agent sent and gets back, before magpie’s redaction on the way out and after it on the way back. magpie’s own requests (thread titles, the router’s classifier) don’t go through middleware.</p>
<h3 id="gateway-points"><a class="anchor" href="#gateway-points">Routings and usage rows</a></h3>
<p>The same module can also export <code>strategies</code> and <code>onUsage</code>. Such a file can be named <code>*.gateway.js</code>, and a package can say <code>"magpie": { "gateway": "./gw.js" }</code>.</p>
diff --git a/site/public/docs/zh/plugins.html b/site/public/docs/zh/plugins.html
index 2ae407bbb..75456bb09 100644
--- a/site/public/docs/zh/plugins.html
+++ b/site/public/docs/zh/plugins.html
@@ -651,6 +651,7 @@ m plugin logout acme</pre>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/system-prompt"><code>system-prompt</code></a></td><td>给每个请求(或只给某些 agent、某些模型)加上你的系统提示词,放在各个 API 自己的位置。请求自带系统提示词时保留原样,除非打开 <code>override</code>。</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/word-guard"><code>word-guard</code></a></td><td>New API 的敏感词过滤:用户发送的内容里出现所列词语或正则时拒绝或打码,也可以对回复打码。</td></tr>
<tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/think-tags"><code>think-tags</code></a></td><td>去掉回复正文开头的 <code>&lt;think&gt;…&lt;/think&gt;</code>,或把 Chat 回复的 <code>reasoning_content</code> 放进正文(New API 的 <code>thinking_to_content</code>)。</td></tr>
+ <tr><td><a href="https://github.com/magpie-community/plugins/tree/main/packages/antigravity-cloak"><code>antigravity-cloak</code></a></td><td>把 Antigravity 据以拒绝 Claude Code 和 Agent SDK 系统提示词的那几个词拆开,只改发给 Antigravity 的请求;发给别的供应商的提示词保持原样。</td></tr>
</table></div>
<div class="code">
<span class="lang">shell</span>
@@ -673,13 +674,14 @@ magpie plugin options model-map '{"mapping": {"fast": "deepseek/deepseek-chat"}}
<div class="table-wrap"><table>
<tr><th>钩子</th><th>拿到的内容</th><th>返回</th></tr>
<tr><td><code>onRequest(body, ctx)</code></td><td>路由之前的请求体:改了 <code>model</code>,请求就会路由到别处。</td><td>请求体,或者不返回。<code>ctx.reject(status, message)</code> 会拒绝这个请求,并用 agent 的 API 格式返回这个错误(不给状态码时是 403)。</td></tr>
+ <tr><td><code>onProvider(body, ctx)</code></td><td>请求即将发给某个供应商时的请求体;<code>ctx.provider</code> 有它的 <code>id</code>、<code>name</code>、<code>account</code>(订阅的种类,比如 <code>claude</code> 或 <code>antigravity</code>;API 密钥没有这一项)和 <code>model</code>。请求每换一个供应商尝试就再调用一次,每次拿到的都是 <code>onRequest</code> 处理后的请求体,所以为一个供应商做的改动不会带到别的供应商。</td><td>请求体,或者不返回。这时请求已经路由好了:不能再拒绝,也不能改发别处。</td></tr>
<tr><td><code>onEvent(event, ctx)</code></td><td>流式回复里的每个事件:它的 <code>data:</code> JSON。</td><td>事件、不返回,或者 <code>null</code> 表示丢掉这个事件。</td></tr>
<tr><td><code>onResponse(body, ctx)</code></td><td>非流式的完整回复;<code>ctx.status</code> 是它的状态码。</td><td>回复体,或者不返回。</td></tr>
</table></div>
<p><code>export const events = ["content_block_delta"]</code> 让 <code>onEvent</code> 只处理这些名字的事件(Anthropic Messages 和 Responses 的事件有名字)。Chat Completions 和 Gemini 的事件没有名字,处理它们时不要写 <code>events</code>。一个事件只是回复的一小段,一个词可能被拆在两个事件里。<code>data: [DONE]</code> 行和注释直接放行,不经过钩子。压缩过的回复原样放行。</p>
<p><code>ctx</code> 里有 <code>protocol</code>(<code>anthropic</code>、<code>chat</code>、<code>responses</code> 或 <code>gemini</code>)、agent 请求的 <code>model</code> 和 <code>stream</code>、<code>path</code>、magpie 知道时的 <code>agent</code>、<code>options</code>(这个插件的<a href="#middleware-options">选项</a>),以及 <code>state</code>:每个请求独有的对象。</p>
<h3>钩子出错时</h3>
- <p>处理请求或完整回复时,钩子有 250 毫秒;处理一个事件时有 50 毫秒;加载有 1 秒。钩子抛出异常、超时或返回了别的东西,都当作它不存在:请求或事件按原样发送。设置 → 插件会显示每个中间件的钩子、调用次数、平均每次耗时和失败次数,以及最近一次错误。钩子看到的是 agent 发出和收到的原始内容:发出时在 magpie 脱敏之前,返回时在还原之后。magpie 自己的请求(会话标题、路由分类器)不经过中间件。</p>
+ <p>处理请求、一次供应商尝试或完整回复时,钩子有 250 毫秒;处理一个事件时有 50 毫秒;加载有 1 秒。钩子抛出异常、超时或返回了别的东西,都当作它不存在:请求或事件按原样发送。设置 → 插件会显示每个中间件的钩子、调用次数、平均每次耗时和失败次数,以及最近一次错误。钩子看到的是 agent 发出和收到的原始内容:发出时在 magpie 脱敏之前,返回时在还原之后。magpie 自己的请求(会话标题、路由分类器)不经过中间件。</p>
<h3 id="gateway-points"><a class="anchor" href="#gateway-points">路由与用量记录</a></h3>
<p>同一个模块还可以导出 <code>strategies</code> 和 <code>onUsage</code>。这样的文件可以命名为 <code>*.gateway.js</code>,包里也可以写 <code>"magpie": { "gateway": "./gw.js" }</code>。</p>