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.
142 lines
6.2 KiB
Go
142 lines
6.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// Google's Decisions API's choice answer, as its reference gives it
|
|
// (aistudio.google.com/api-reference/decisions/create-decision), with
|
|
// the usage its yes-or-no example answers with.
|
|
const googleChoiceReply = `{"answers":{"ticket_route":{"type":"choice","choice":"technical_support","confidence":0.95,"probabilities":{"account_access":0.02,"billing":0.03,"technical_support":0.95}}},"usage":{"input_tokens_by_modality":[{"modality":"text","tokens":152}],"total_input_tokens":152,"total_output_tokens":0,"total_tokens":152}}`
|
|
|
|
// googleDecisionsUp stands in for generativelanguage.googleapis.com, in
|
|
// process: POST /v1beta/decisions answers reply to the key as
|
|
// x-goog-api-key and an input that is not an object (Google reads an
|
|
// object as a content block, and refuses one without its type); anything
|
|
// else is refused as Google refuses it. No other host is reached.
|
|
type googleDecisionsUp struct {
|
|
mu sync.Mutex
|
|
reply string
|
|
asked []string
|
|
bodies []map[string]any
|
|
}
|
|
|
|
func (u *googleDecisionsUp) 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)
|
|
var q map[string]any
|
|
json.Unmarshal(b, &q)
|
|
u.mu.Lock()
|
|
u.asked = append(u.asked, r.URL.Path+" key="+r.Header.Get("x-goog-api-key")+" auth="+r.Header.Get("Authorization"))
|
|
u.bodies = append(u.bodies, q)
|
|
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.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":"Expected OAuth 2 access token","status":"UNAUTHENTICATED"}}`)
|
|
}
|
|
if _, isObject := q["input"].(map[string]any); isObject || q["input"] == nil {
|
|
return reply(400, `{"error":{"code":400,"message":"type parameter is required at input[0]","status":"INVALID_ARGUMENT"}}`)
|
|
}
|
|
return reply(200, u.reply)
|
|
}
|
|
|
|
func (u *googleDecisionsUp) got() ([]string, []map[string]any) {
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
return slices.Clone(u.asked), slices.Clone(u.bodies)
|
|
}
|
|
|
|
// googleKey saves WangYeruo's provider, the google preset with an AI
|
|
// Studio key, and has the gateway's Google be up.
|
|
func googleKey(t *testing.T, up *googleDecisionsUp) *Server {
|
|
t.Helper()
|
|
p, err := provider.FromPreset("google")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.Key = "AIza-test"
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = up
|
|
t.Cleanup(func() { http.DefaultClient.Transport = old })
|
|
s := New()
|
|
s.client = &http.Client{Transport: up}
|
|
return s
|
|
}
|
|
|
|
// WangYeruo on X: magpie's System One API, asked for
|
|
// google/gemini-decision-1 with a Google AI Studio key's provider, went to
|
|
// OpenRouter. It reaches Google's Decisions API on that key, as Google
|
|
// takes it — the state an input of text, the key as x-goog-api-key — and
|
|
// Google's answer comes back as System One's, its usage as System One
|
|
// names it.
|
|
func TestGoogleSystemOne(t *testing.T) {
|
|
fresh(t)
|
|
up := &googleDecisionsUp{reply: googleChoiceReply}
|
|
s := googleKey(t, up)
|
|
code, out := sendTo(s, "/v1/systemone", `{"model":"google/`+provider.GoogleDecision+`","state":{"message":"Every time I click Export to PDF, the app freezes and crashes with error code 504."},`+
|
|
`"questions":{"ticket_route":{"type":"choice","instructions":"Route the support ticket to the most appropriate team.","criteria":{"billing":"Invoices, charges, refunds, or subscription plan changes.","technical_support":"Bugs, crashes, errors, or product functionality issues.","account_access":null}}}}`)
|
|
if code != 200 || !strings.Contains(out, `"choice":"technical_support"`) || !strings.Contains(out, `"input_tokens":152`) {
|
|
t.Fatalf("System One: %d %s", code, out)
|
|
}
|
|
asked, bodies := up.got()
|
|
if !slices.Equal(asked, []string{"/v1beta/decisions key=AIza-test auth="}) {
|
|
t.Fatalf("Google asked: %q", asked)
|
|
}
|
|
if in := bodies[0]["input"]; in != `{"message":"Every time I click Export to PDF, the app freezes and crashes with error code 504."}` {
|
|
t.Fatalf("input %v", in)
|
|
}
|
|
if _, ok := bodies[0]["state"]; ok || bodies[0]["model"] != provider.GoogleDecision {
|
|
t.Fatalf("sent %v", bodies[0])
|
|
}
|
|
c := bodies[0]["questions"].(map[string]any)["ticket_route"].(map[string]any)["criteria"].(map[string]any)
|
|
if c["account_access"] != "" || c["billing"] != "Invoices, charges, refunds, or subscription plan changes." {
|
|
t.Fatalf("criteria %v", c)
|
|
}
|
|
if recs := usage.Load(time.Time{}); len(recs) != 1 || recs[0].Provider != "google" || recs[0].Model != provider.GoogleDecision || recs[0].Input+recs[0].CacheRead+recs[0].CacheWrite != 152 {
|
|
t.Fatalf("recorded %+v", recs)
|
|
}
|
|
}
|
|
|
|
// OpenAI's Decisions API asked for Google's decision model is asked as
|
|
// System One there, its message's image an image block of the input,
|
|
// which gemini-decision-1 reads.
|
|
func TestGoogleDecisionsTakesImages(t *testing.T) {
|
|
fresh(t)
|
|
up := &googleDecisionsUp{reply: `{"answers":{"visible_damage":{"type":"noul","noul":0.92,"confidence":0.84},"condition":{"type":"choice","choice":"used","confidence":0.6,"probabilities":{"new":0.2,"used":0.8}}},"usage":{"total_input_tokens":1290,"total_output_tokens":0,"total_tokens":1290}}`}
|
|
s := googleKey(t, up)
|
|
code, out := sendTo(s, "/v1/decisions", decisionAsked("google/"+provider.GoogleDecision))
|
|
if code != 200 || !strings.Contains(out, `"choice":"used"`) {
|
|
t.Fatalf("Decisions: %d %s", code, out)
|
|
}
|
|
_, bodies := up.got()
|
|
if len(bodies) != 1 {
|
|
t.Fatalf("Google asked %d times", len(bodies))
|
|
}
|
|
in, _ := json.Marshal(bodies[0]["input"])
|
|
if want := `[{"text":"{\"messages\":[{\"content\":\"Inspect the product in this photo.\",\"role\":\"user\"}]}","type":"text"},{"data":"iVBORw0KGgo=","mime_type":"image/png","type":"image"}]`; string(in) != want {
|
|
t.Fatalf("input %s\nwant %s", in, want)
|
|
}
|
|
}
|