Files
magpie/internal/gateway/reqlog.go
T
wei 5a27b924e2 providers, gateway: Google Vertex AI is a preset, Gemini in the user's own Google Cloud project, signed with their Google credentials rather than a key
Gemini on Vertex AI was reachable only through a proxy of the user's own
in front of Vertex AI's OpenAI-compatible endpoint, added as a custom
provider. The Google Vertex AI preset (google-vertex) asks Vertex AI
itself.

  * The provider holds a project, a location (global when none is given;
    us, eu or a region), an optional credentials file and an optional
    service account to go as (provider.Vertex). It has no key and no
    URL: Save refuses a key, normalize clears a key and the four URLs
    kept by hand, and its one address is
    https://<host>/v1/projects/<project>/locations/<location>, the host
    following the location (vertexHost). It is ready when it has a
    project, never for a key or an account, and an import link can't add
    it.
  * Each request is signed with a Google token (Sign), and the token goes
    to no other address. The credentials are found as Google's libraries
    find Application Default Credentials: the provider's file, else
    GOOGLE_APPLICATION_CREDENTIALS's, else gcloud's
    application_default_credentials.json. A GOOGLE_APPLICATION_CREDENTIALS
    that isn't a full path is an error, not unset, since gcloud's file
    read in its place may be another account's. authorized_user,
    service_account and gcloud's impersonated_service_account are read;
    external_account and another universe are refused by name. With a
    service account set, the token is traded at IAM Credentials'
    generateAccessToken for one of that account's, as gcloud's
    --impersonate-service-account does. Tokens are kept in memory per
    file and account, minted once for requests made together, again 5
    minutes before they expire or when the file changes (gcloud signed
    in again). A mint is given 30 seconds; one that fails fails the
    requests that waited through it too, and a request given up on
    stops waiting. A sign-in Google refuses says the command that makes
    that file again: gcloud's for its own files, impersonating as it
    did.
  * The gateway speaks Gemini to it alone. A Gemini client's request is
    relayed as it came, its thinkingConfig, safetySettings and schema
    with it: streamed to
    publishers/google/models/<model>:streamGenerateContent?alt=sse, or
    asked whole to :generateContent. Every other API's is translated
    (buildVertex), streamed, and read back with thoughts as reasoning,
    never as the answer. The path names a model by its upstream name
    when one is set, as the usage log's endpoint does. Gemini 3 is sent a thinkingLevel fitted to the
    model's levels, 2.5 a thinkingBudget in the model's range, off
    included (Pro's least, as Pro can't stop). A call's thought
    signature goes to the client in its id and comes back on the call;
    a 400 "Invalid thought signature." is asked again with every call
    waved through, for that request, and a 400 refusing minimal is asked
    again at the model's lowest level, kept until restart. Neither is
    tried for another provider.
  * Vertex AI has no list of the models a location serves, so each
    location has the ones it answered with on 2026-10-06: 12 at global,
    6 Flash at us and eu, the 2.5 trio at a region.
  * The preset offers Priority PayGo's and Flex PayGo's request headers
    (HeaderHints). GOOGLE_APPLICATION_CREDENTIALS and CLOUDSDK_CONFIG are
    in agentenv.Vars, so the desktop app takes them from the login shell
    and the tests clear them.

Not read: the metadata server of Compute Engine, Cloud Run and GKE;
workload identity federation; Vertex AI's express-mode API keys. No
quota project is sent.

Reference: docs/subsystems/providers-accounts.md (Google Vertex AI,
Vertex AI tokens, four constraints) and gateway-routing.md (Vertex AI).

Verified: go test ./... (nogui), -race -count=5 on the Vertex tests,
gofmt and vet. A fake Google (token endpoint, IAM Credentials, Vertex
AI) checks each credentials type, the mint shared, bounded and
re-minted, every refusal's advice and that no token reaches another
host. Each of 24 mutations made after review, of the mint, the key
rules, the advice, the upstream path and the retries, fails a test.
Live, against a real project through an impersonated service account,
with magpie in a sandboxed HOME: Chat, Anthropic Messages, Responses and
Gemini clients, streamed and whole, with thinking and with tool calls, a
whole Gemini call and its signature sent back streamed, at global, us
and us-central1, and with Flex PayGo's header. No token was found in its
files or logs afterwards.
2026-10-10 19:21:56 +08:00

67 lines
1.9 KiB
Go

package gateway
import (
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// What the usage log keeps of a call besides its tokens: the path it came
// in on, the id its vendor gave it, and, when it failed, what was said.
// errKeep is how much of a failure's message the log keeps.
const errKeep = 500
// keepMsg is a message cut to what the log keeps of it.
func keepMsg(s string) string {
s = strings.Join(strings.Fields(s), " ")
if r := []rune(s); len(r) > errKeep {
return string(r[:errKeep]) + "…"
}
return s
}
// requestID is the id a vendor's reply gives the request, by the header
// Anthropic (request-id) and everyone else (x-request-id) send it in.
func requestID(h http.Header) string {
for _, k := range []string{"Request-Id", "X-Request-Id"} {
if v := strings.TrimSpace(h.Get(k)); v != "" {
return v
}
}
return ""
}
// endpointOf is the path a call came in on, and the one it went out on to
// model at p when that is another protocol's, as the vendors' own docs
// write them (the vendor's base URL, which decides the rest, isn't part of
// it).
func endpointOf(r *http.Request, from, to provider.Protocol, p provider.Provider, model string) string {
ep := r.URL.Path
if to != "" && to != from {
switch to {
case provider.Chat:
ep += " → /v1/chat/completions"
case provider.Responses:
ep += " → /v1/responses"
case provider.Anthropic:
ep += " → /v1/messages"
default:
if p.IsVertex() { // its path names the model as Vertex AI was asked for it
model = provider.UpstreamNameIn(wiresOf(r.Context()), p.ID, model)
}
ep += " → " + upstreamPath(p, to, model)
}
}
return ep
}
// failedWith fills in what a call that failed said, on a usage record.
func failedWith(rec *usage.Record, status int, msg, errType string) {
if status >= 400 || msg != "" {
rec.Error, rec.ErrType = keepMsg(msg), errType
}
}