Files
magpie/internal/provider/decide_google_test.go
T
yetone 32a1818681 provider: an AI Studio key decides with Google's gemini-decision-1 (WangYeruo on X)
WangYeruo (v0.1.4401) has a Google AI Studio key's provider, the google
preset. magpie's System One asked for google/gemini-decision-1 fell to
OpenRouter's decision list, because the google provider had no decision
API, and the user's own attempt got Google's 400 "type parameter is
required at input[0]" for an object input.

Google's Decisions API (launched 11-11, aistudio.google.com/docs/
decisions-api, api-reference/decisions/create-decision) serves
gemini-decision-1 at POST generativelanguage.googleapis.com/v1beta/
decisions on any AI Studio key, free tier included, with the key as
x-goog-api-key. It takes System One's questions (noul, choice, score),
an input of text or content blocks, and answers by name with usage
total_input_tokens / total_output_tokens.

- normalize gives a provider asked at the Gemini Developer API (the
  google preset's OpenAI-compatible base or a native Gemini base) its
  System One at AIStudioGemini (googleDecisions). Not Vertex, not a
  signed-in account, not another magpie, and a URL the user gave is kept.
- ViaGoogle: /decisions, signed as Gemini (x-goog-api-key, never Bearer;
  DecideProtocol), in AskSystemOne, the list and key checks, and the
  gateway's postDecideSent.
- googleAsk: the state goes as input (a string as it is, an object as
  its JSON text, never an object, which Google reads as a block), images
  as image blocks after it, a choice's null description as "".
- DecideResult: the usage comes back as System One's input_tokens /
  output_tokens; prompt_feedback.block_reason with no answers is a 400
  that says why. Every other way of asking is unchanged (tested).
- gemini-decision-1: 262,144 context, takes images, so the gateway lets
  images through to it as it does for the Clef.
- docs/subsystems/providers-accounts.md says so.

Tests, from Google's documented request and answer bytes, no network:
TestGoogleDecideWire, TestGoogleKeyDecides (with OpenRouter's live list
holding google/gemini-decision-1 seeded: it now routes to google),
TestGoogleSystemOne, TestGoogleDecisionsTakesImages.

Broken by hand, each red: the normalize fill dropped (KeyDecides,
SystemOne, TakesImages red); googleAsk skipped (Wire, KeyDecides,
SystemOne, TakesImages red); signed as Chat/Bearer (KeyDecides,
SystemOne, TakesImages red).

Run at this head:
- gofmt -l internal/: clean. go vet (provider, gateway), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./... on mote: ok.
- Full suite on mote at 6317fd789 (this change on 7707ba9ad+f4b202030):
  go test -count=1 -timeout 45m ./...: 103 packages ok, exit 0, no FAIL.
- Rebased onto bf1b2893d (GUI and docs only since: app.js, two GUI
  tests, skill-router.md; nothing this change reads), then at this head
  on mote: go test -run 'Google|Decide|Decision|SystemOne|Ark'
  ./internal/provider ./internal/gateway: ok. The whole internal/provider
  package was also ok at 6317fd789 (155s).
- No GUI file changed, so no GUI suite.

Not tried: a real call. No AI Studio key is in magpie's accounts here.
2026-11-18 12:08:46 +08:00

247 lines
10 KiB
Go

package provider
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"reflect"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// Google's Decisions API, as its reference gives it
// (aistudio.google.com/api-reference/decisions/create-decision, 11-11):
// the yes-or-no example's request and its answer.
const (
googleNoulAsk = `{"model": "gemini-decision-1","input": "Customer message: Hi, I was charged twice for my monthly subscription this morning. Can you refund the duplicate charge?","questions": {"needs_billing_refund": {"type": "noul","instructions": "Determine whether the customer is requesting a billing refund.","criteria": {"true_description": "The customer explicitly asks for a refund or reversal of a charge.","false_description": "The customer is asking a general question or reporting a non-billing issue."}}}}`
googleNoulAnswer = `{
"answers": {
"needs_billing_refund": {
"type": "noul",
"confidence": 0.97,
"noul": 0.97
}
},
"usage": {
"input_tokens_by_modality": [
{
"modality": "text",
"tokens": 152
}
],
"total_input_tokens": 152,
"total_output_tokens": 0,
"total_tokens": 152
}
}`
// what an object as the input was answered with (WangYeruo on X)
googleNoType = `{"error":{"code":400,"message":"type parameter is required at input[0]","status":"INVALID_ARGUMENT"}}`
)
func sameJSON(t *testing.T, what string, got []byte, want string) {
t.Helper()
var g, w any
if err := json.Unmarshal(got, &g); err != nil {
t.Fatalf("%s: %v in %s", what, err, got)
}
if err := json.Unmarshal([]byte(want), &w); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(g, w) {
t.Fatalf("%s:\n got %s\nwant %s", what, got, want)
}
}
// A System One request goes to Google as its Decisions API takes it: the
// state as the input, text as it is and an object as its JSON text, a
// choice's undescribed option with an empty description, images as image
// blocks after the text; and its answer comes back as System One's, with
// its usage named as System One names it.
func TestGoogleDecideWire(t *testing.T) {
ask := strings.Replace(googleNoulAsk, `"input"`, `"state"`, 1)
sameJSON(t, "text state", DecideAsk(ViaGoogle, GoogleDecision, []byte(ask)), googleNoulAsk)
obj := `{"model":"gemini-decision-1","state":{"message":"fix the build","previous_message_kind":"coding"},"questions":{"intent":{"type":"choice","instructions":"Which?","criteria":{"coding":null,"chat":"small talk"}}},"images":["data:image/png;base64,iVBORw0KGgo=","https://example.com/a.jpg"]}`
sameJSON(t, "object state", DecideAsk(ViaGoogle, GoogleDecision, []byte(obj)),
`{"model":"gemini-decision-1","input":[{"type":"text","text":"{\"message\":\"fix the build\",\"previous_message_kind\":\"coding\"}"},{"type":"image","data":"iVBORw0KGgo=","mime_type":"image/png"},{"type":"image","uri":"https://example.com/a.jpg"}],"questions":{"intent":{"type":"choice","instructions":"Which?","criteria":{"coding":"","chat":"small talk"}}}}`)
status, b := DecideResult(ViaGoogle, 200, []byte(googleNoulAnswer))
if status != 200 {
t.Fatalf("status %d", status)
}
sameJSON(t, "answer", b, `{"answers":{"needs_billing_refund":{"type":"noul","confidence":0.97,"noul":0.97}},"usage":{"input_tokens":152,"output_tokens":0}}`)
// an input its filters blocked answers nothing: a 400 that says why
status, b = DecideResult(ViaGoogle, 200, []byte(`{"answers":{},"prompt_feedback":{"block_reason":"safety","block_reason_message":"Harmful content."}}`))
if status != 400 || APIError(b, "") != "Google blocked the input (safety): Harmful content." {
t.Fatalf("blocked: %d %s", status, b)
}
// a refusal is passed on as Google gave it
if status, b = DecideResult(ViaGoogle, 400, []byte(googleNoType)); status != 400 || string(b) != googleNoType {
t.Fatalf("refusal: %d %s", status, b)
}
// and nothing changes for any other way of asking
for _, via := range []string{ViaSystemOne, ViaVercel} {
if got := DecideAsk(via, "jev-latest", []byte(obj)); string(got) != obj {
t.Fatalf("%s: %s", via, got)
}
if status, got := DecideResult(via, 200, []byte(googleNoulAnswer)); status != 200 || string(got) != googleNoulAnswer {
t.Fatalf("%s answer: %d %s", via, status, got)
}
}
}
// googleUp stands in for generativelanguage.googleapis.com, in process:
// POST /v1beta/decisions with the key as x-goog-api-key, the input not
// an object (an object is a content block, refused without its type), is
// answered with Google's documented answer; a key as a Bearer token is
// refused as Google refuses it. Any other host is never reached.
type googleUp struct {
mu sync.Mutex
asked []string
body []byte
}
func (u *googleUp) RoundTrip(r *http.Request) (*http.Response, error) {
if r.URL.Hostname() != "generativelanguage.googleapis.com" {
return nil, errors.New("test reached " + r.URL.Host)
}
b, _ := io.ReadAll(r.Body)
u.mu.Lock()
u.asked = append(u.asked, r.Method+" "+r.URL.Path+" key="+r.Header.Get("x-goog-api-key")+" auth="+r.Header.Get("Authorization"))
u.body = b
u.mu.Unlock()
reply := func(code int, s string) (*http.Response, error) {
return &http.Response{StatusCode: code, Header: http.Header{"Content-Type": {"application/json; charset=UTF-8"}},
Body: io.NopCloser(strings.NewReader(s)), Request: r}, nil
}
if r.Method != http.MethodPost || r.URL.Path != "/v1beta/decisions" {
return reply(404, `{"error":{"code":404,"message":"Not found","status":"NOT_FOUND"}}`)
}
if r.Header.Get("x-goog-api-key") != "AIza-test" {
return reply(401, `{"error":{"code":401,"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.","status":"UNAUTHENTICATED"}}`)
}
var q struct {
Input json.RawMessage `json:"input"`
}
if json.Unmarshal(b, &q) != nil || len(q.Input) == 0 || q.Input[0] == '{' {
return reply(400, googleNoType)
}
return reply(200, googleNoulAnswer)
}
func (u *googleUp) got() []string {
u.mu.Lock()
defer u.mu.Unlock()
return slices.Clone(u.asked)
}
func fakeGoogleDecisions(t *testing.T) *googleUp {
up := &googleUp{}
old := http.DefaultClient.Transport
http.DefaultClient.Transport = up
t.Cleanup(func() { http.DefaultClient.Transport = old })
return up
}
// WangYeruo on X: with a Google AI Studio key's provider (the google
// preset), google/gemini-decision-1 went to OpenRouter, the google
// provider having no decision API. Google answers it at the Gemini API's
// /v1beta/decisions on the same key: a provider asked at the Gemini
// Developer API, the preset's or a custom one, offers it with no URL
// typed, keeps it out of its chat models, and is the one asked for it;
// Vertex and other hosts are as they were.
func TestGoogleKeyDecides(t *testing.T) {
arkHome(t)
up := fakeGoogleDecisions(t)
g, err := FromPreset("google")
if err != nil {
t.Fatal(err)
}
g.Key, g.Models = "AIza-test", []string{"gemini-3.5-flash"}
or, err := FromPreset("openrouter")
if err != nil {
t.Fatal(err)
}
or.Key = "sk-or-1"
for _, p := range []Provider{g,
{ID: "gem-native", Name: "Gemini native", Gemini: AIStudioGemini, Key: "AIza-test"},
{ID: "other", Name: "Other", Chat: "https://api.example.com/v1beta/openai", Key: "k"},
{ID: "own", Name: "Own", Chat: AIStudioChat, Decide: "https://api.typesafe.ai/v1", Key: "AIza-test"},
or,
} {
if err := Save(p); err != nil {
t.Fatal(err)
}
}
// OpenRouter lists Google's decision model as its own, as it does
if err := catalog.SaveLive(decisionsID("openrouter"), "https://openrouter.ai/api/v1", []catalog.Model{{ID: "google/gemini-decision-1"}, {ID: OpenRouterJev}}); err != nil {
t.Fatal(err)
}
for id, want := range map[string]string{"google": AIStudioGemini, "gem-native": AIStudioGemini, "other": "", "own": "https://api.typesafe.ai/v1"} {
p, err := Find(id)
if err != nil {
t.Fatal(err)
}
if p.Decide != want {
t.Errorf("%s: decisions at %q, want %q", id, p.Decide, want)
}
}
p, err := Find("google")
if err != nil {
t.Fatal(err)
}
if p.DecideVia() != ViaGoogle || p.Jev() != GoogleDecision || !p.DecidesModel(GoogleDecision) || p.DecidesModel("gemini-3.5-flash") {
t.Fatalf("google: via %s, asked with %s", p.DecideVia(), p.Jev())
}
if ms := p.decideModels(); len(ms) != 1 || ms[0].Context != 262_144 || ms[0].ImageInput == nil || !*ms[0].ImageInput {
t.Fatalf("decision models: %+v", ms)
}
if u, _ := p.DecideURL(context.Background()); u != "https://generativelanguage.googleapis.com/v1beta/decisions" {
t.Fatalf("asked at %s", u)
}
// the user's model reaches Google, on the provider it names; Google's
// name alone, served by both here, asks which, and is never OpenRouter's
for _, id := range []string{"google", "gem-native"} {
got, m, err := RouteDecider(id + "/" + GoogleDecision)
if err != nil || got.ID != id || m != GoogleDecision {
t.Fatalf("%s routed to %s %s %v", id, got.ID, m, err)
}
}
if _, _, err := RouteDecider(GoogleDecision); err == nil || !strings.Contains(err.Error(), "name it gem-native/gemini-decision-1 or google/gemini-decision-1") {
t.Fatalf("Google's name alone: %v", err)
}
// OpenRouter's own is still OpenRouter's
if got, m, err := RouteDecider("openrouter/google/gemini-decision-1"); err != nil || got.ID != "openrouter" || m != "google/gemini-decision-1" {
t.Fatalf("openrouter's routed to %s %s %v", got.ID, m, err)
}
for _, e := range providerEntries() {
if e.ID == "google/"+GoogleDecision {
t.Fatalf("an agent is offered %s", e.ID)
}
}
// the provider's test asks it the smallest question, with the key as
// Google takes it
if err := p.AskSystemOne(context.Background(), GoogleDecision); err != nil {
t.Fatal(err)
}
if got := up.got(); !slices.Equal(got, []string{"POST /v1beta/decisions key=AIza-test auth="}) {
t.Fatalf("Google asked: %q", got)
}
sameJSON(t, "smallest question", up.body, `{"model":"gemini-decision-1","input":"{\"text\":\"ping\"}","questions":{"ok":{"type":"noul","instructions":"Is this a test?"}}}`)
// a signed-in account and Vertex say their own
if d := (Provider{Chat: AIStudioChat, Account: &Account{}}).googleDecisions(); d != "" {
t.Fatalf("an account: %q", d)
}
if d := (Provider{Preset: VertexPreset, Gemini: AIStudioGemini}).googleDecisions(); d != "" {
t.Fatalf("vertex: %q", d)
}
}