Documentation
¶
Overview ¶
Package gwmw is the gateway's middleware point (P3 of docs/subsystems/plugin-runtime.md): what changes a request on its way through the gateway and its reply on the way back. It has two places, each with its own failure policy:
- Agent: a request an agent sent, in the agent's own API, before the 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. A Run that is also Tried changes the body again for each provider the gateway tries the request on, and one that is also Accounts can have an account passed over for the request.
- 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 no masker is served, or the masker fails, nothing is sent.
Index ¶
- Variables
- func Blocks(hits []Hit) bool
- func ForgetRefreshForTest()
- func Mask(m Masker) (remove func())
- func Refresh(f func()) (stop func())
- func Safely[T any](f func() T) (out T, err error)
- func Use(a Agent) (remove func())
- type Account
- type Accounts
- type Agent
- type ContextTeller
- type Hit
- type Info
- type Masker
- type Reject
- type Run
- type Teller
- type To
- type Tried
Constants ¶
This section is empty.
Variables ¶
var ErrUnmasked = fmt.Errorf("masking is on in magpie's Settings › Privacy, but the Masking built-in isn't serving it, so nothing was sent; turn it on in Plugins › Built-in")
ErrUnmasked is the error a request gets when masking is wanted and nothing can mask it.
Functions ¶
func Blocks ¶ added in v0.1.1317
Blocks says hits have a value the user asked to hold a request back for.
func ForgetRefreshForTest ¶
func ForgetRefreshForTest()
ForgetRefreshForTest lets the next Served refresh at once, as it would a second after the last one.
func Mask ¶
func Mask(m Masker) (remove func())
Mask serves m until remove. Registering one while another is served stacks: the last serves until it is removed.
func Refresh ¶
func Refresh(f func()) (stop func())
Refresh sets what the gateway asks when masking is wanted and no masker is served: pluginrt's refresh, which turns the plugin on when the settings ask for masking. stop takes it away.
Types ¶
type Account ¶ added in v0.1.2439
type Account struct {
Agent, User, Plan string
ID, Name, Model string // the provider, and the model asked of it there
}
Account is the subscription account a request is about to be tried on, as the gateway knows it. Agent, User and Plan are who it is (provider.Account) and never reach a middleware: a middleware is told a ref made from them (internal/middleware's accountRef). A zero Agent is a try on a key, no account.
type Accounts ¶ added in v0.1.2439
Accounts is a Run asked, before each try, whether the account it goes to may be used for this request: skip says it may not, and the gateway goes on to the next as it does past an account that can't be used, resting nothing. by names the middleware that said so.
type Agent ¶
Agent is a middleware on agents' requests.
func Agents ¶
func Agents() []Agent
Agents are the agent middlewares registered now, in their order.
func AgentsWanted ¶
AgentsWanted is Agents; with none, when want says some are (plugins.json has a middleware on), one refresh is asked first, at most once a second: a middleware added a moment ago turns its plugin on for the request that comes before the gateway's next look at the files.
type ContextTeller ¶ added in v0.1.1566
type ContextTeller interface {
JSONHitsContext(ctx context.Context, body []byte) ([]byte, []Hit)
TextHitsContext(ctx context.Context, s string) (string, []Hit)
}
ContextTeller is a Teller whose masking can wait (for a local model's turn) and stops waiting when ctx ends: the client went away, or the gateway gave up on the request. It then fails, as a masker that can't mask does, and nothing is sent.
type Hit ¶ added in v0.1.1305
Hit is one value a masker found, told without the value: its kind, the finder that found it (rule, yours, word, model or known), its placeholder, what was done with it (Action: "" masked, "warn" sent as written, "block" masked and the request to be held back), and as much of it as may be kept where it may not (Head, its first character or a key's prefix, and Len, how many it has).
type Info ¶
type Info struct {
Protocol string `json:"protocol"` // the agent's API: anthropic, chat, responses, gemini
Model string `json:"model"`
Stream bool `json:"stream"`
Path string `json:"path"`
Agent string `json:"agent,omitempty"`
}
Info is what an agent middleware is told of a request.
type Masker ¶
type Masker interface {
// JSON is body masked, and how many values it swapped
JSON(body []byte) ([]byte, int)
// Text is s masked, and how many values it swapped
Text(s string) (string, int)
// Restore is s with the values masked before back
Restore(s string, escaped bool) string
// Writer wraps w so what is written has the values back; done writes
// what it still holds
Writer(w http.ResponseWriter) (http.ResponseWriter, func())
}
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.
type Reject ¶
Reject is a request a middleware turned away: the agent gets Status with Message in its API's error shape.
type Run ¶
type Run interface {
// Request is the body the gateway, or the next middleware, gets; a
// Reject ends the request there
Request(body []byte) ([]byte, *Reject)
// Wrap is the writer the reply goes through; done writes what it holds
Wrap(w http.ResponseWriter) (http.ResponseWriter, func())
// End is the request's end, after done
End()
}
Run is an agent middleware on one request.
type Teller ¶ added in v0.1.1305
type Teller interface {
// JSONHits is JSON, with each value it swapped, once for each place
JSONHits(body []byte) ([]byte, []Hit)
// TextHits is Text, with each value it swapped
TextHits(s string) (string, []Hit)
// Value is the value behind a placeholder, while the masker remembers
// it
Value(placeholder string) (string, bool)
}
Teller is a Masker that says what it masked too, for the record of what was masked; one that doesn't is counted only.
type To ¶ added in v0.1.1283
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
}
To is the provider a request is tried on, as a middleware is told it.