The provider editor has a Redaction row, "Send requests unmasked", while
every address of the provider is on this computer or the local network
(localhost, *.local, a loopback or private IP): Ollama, LM Studio, a vLLM
box. Ticked (provider.Unredacted; CLI `magpie provider set <id>
unmasked=yes`), a request whose provider and fallbacks, or whose routing
group's models, all skip redaction (provider.SkipsRedaction) goes to them
as the agent wrote it; anything that may reach another provider stays
masked for all of them. An address edited to a vendor's masks it again; a
signed-in account, a plugin's provider and another magpie never skip. The
log and recent calls keep the masked body. count_tokens and embeddings
follow the same rule.
It is a per-provider choice, not automatic for local addresses, because
a local address isn't a local model: a relay run locally (new-api,
CLIProxyAPI) or Ollama's cloud models pass the request on to a vendor.
Verified: TestLocalUnredactedProviderGetsTheRequestWhole (set, unset,
fallback to a vendor, fallback to another local one, a group of it alone,
a group with a vendor; chat and count_tokens) and TestLocalUnredactedEmbeddings
fail without the gateway change ("sent whole false, want true: … my key is
{{API_KEY_…}}"; count_tokens alone: "counted whole false"); TestSkipsRedaction;
TestProviderSaveKeepsUnredacted fails without the preset save line.
provider-unmasked.test.cjs (Chromium+WebKit, en/zh, 900 and 440 wide) 32/32,
3 of 4 fail on the old app.js; gui-ja, gui-de, i18n-keys, provider-search
and the other provider editor suites pass. go test -race on gateway,
provider, redact, gui; -race -count=10 on the new tests; go vet,
GOOS=linux/windows builds. Real: a sandbox gateway in front of the local
Ollama (llama3 8B) through a logging proxy: unset, Ollama was sent
{{API_KEY_…}}; with unmasked=yes it was sent the key as written, and
answered both times.
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// redacted masks a request's secrets, as the settings say, before it goes
|
|
// to a vendor, and wraps w so what the vendor answers has them back; done
|
|
// writes what the wrapper still holds. Nothing masked, nothing wrapped.
|
|
func redacted(w http.ResponseWriter, body []byte) (http.ResponseWriter, []byte, func()) {
|
|
o := redactionOptions()
|
|
if !o.Secrets && !o.Personal && len(o.Words) == 0 {
|
|
return w, body, func() {}
|
|
}
|
|
masked, n := redact.MaskJSON(body, o)
|
|
if n == 0 {
|
|
return w, body, func() {}
|
|
}
|
|
rw := redact.NewWriter(w)
|
|
return rw, masked, rw.Finish
|
|
}
|
|
|
|
// redactedPrompt masks a parsed image or video prompt before any vendor
|
|
// body is built, including multipart forms, and restores the reply's text.
|
|
func redactedPrompt(w http.ResponseWriter, prompt string) (http.ResponseWriter, string, func()) {
|
|
masked, n := redact.Mask(prompt, redactionOptions())
|
|
if n == 0 {
|
|
return w, prompt, func() {}
|
|
}
|
|
rw := redact.NewWriter(w)
|
|
return rw, masked, rw.Finish
|
|
}
|
|
|
|
func redactionOptions() redact.Options {
|
|
st := settings.Load()
|
|
return redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Words: st.RedactWords, Rules: st.RedactRules}
|
|
}
|
|
|
|
// unredactedRoute says a request resolved to p, or to the group whose
|
|
// models are ms, goes only where the user set requests to go unmasked
|
|
// (provider.SkipsRedaction): p and each of its fallbacks, or every model of
|
|
// the group (a member's own fallbacks are not the group's, see planGroup).
|
|
// One that may go on to anyone else stays masked for all of them.
|
|
func unredactedRoute(p provider.Provider, isGroup bool, ms []provider.Member) bool {
|
|
if isGroup {
|
|
if len(ms) == 0 {
|
|
return false
|
|
}
|
|
for _, m := range ms {
|
|
if !m.Provider.SkipsRedaction() {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
if !p.SkipsRedaction() {
|
|
return false
|
|
}
|
|
for _, id := range p.Fallback {
|
|
// one that resolves to nothing isn't tried (plan)
|
|
if fp, _, ok := provider.Resolve(id); ok && !fp.SkipsRedaction() {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|