Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions backstage/intproxy/internal/credential/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ type brokerRequest struct {
}

type brokerResponse struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Account string `json:"account,omitempty"`
Upstream string `json:"upstream,omitempty"`
Meter string `json:"meter,omitempty"`
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Account string `json:"account,omitempty"`
AuthHeader string `json:"auth_header,omitempty"`
Upstream string `json:"upstream,omitempty"`
Meter string `json:"meter,omitempty"`
}

type brokerError struct {
Expand Down Expand Up @@ -102,7 +103,10 @@ func (b *Broker) Token(ctx context.Context, s Scope) (Token, error) {
Message: "intproxy: the control server returned an unusable token",
}
}
return Token{Value: br.Token, ExpiresAt: br.ExpiresAt, Upstream: br.Upstream, Meter: br.Meter, Account: br.Account}, nil
return Token{
Value: br.Token, ExpiresAt: br.ExpiresAt, Upstream: br.Upstream,
Meter: br.Meter, Account: br.Account, AuthHeader: br.AuthHeader,
}, nil
}

type relayRequest struct {
Expand Down
3 changes: 3 additions & 0 deletions backstage/intproxy/internal/credential/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Scope struct {

type Token struct {
Value string
// AuthHeader is integration-defined. The llm integration recognizes
// x-api-key; empty keeps the usual Authorization bearer token.
AuthHeader string
// ExpiresAt zero means a static credential that never rotates.
ExpiresAt time.Time
// Upstream is an https base URL the source picked for this token, for
Expand Down
8 changes: 6 additions & 2 deletions backstage/intproxy/internal/integration/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
maxBody = 32 << 20
)

var sourcePattern = regexp.MustCompile(`^[a-z0-9]+(@global)?$`)
var sourcePattern = regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*(@global)?$`)

type LLM struct{}

Expand Down Expand Up @@ -97,7 +97,11 @@ func withModel(r *http.Request, route integration.Route) (integration.Route, boo
}

func (l *LLM) Apply(pr *httputil.ProxyRequest, route integration.Route, tok credential.Token) {
pr.Out.Header.Set("Authorization", "Bearer "+tok.Value)
if tok.AuthHeader == "x-api-key" {
pr.Out.Header.Set("X-Api-Key", tok.Value)
} else {
pr.Out.Header.Set("Authorization", "Bearer "+tok.Value)
}
// A chatgpt subscription's backend wants the account and codex's own headers.
if tok.Account != "" {
pr.Out.Header.Set("Chatgpt-Account-Id", tok.Account)
Expand Down
2 changes: 2 additions & 0 deletions backstage/intproxy/internal/integration/llm/llm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ func TestClassify(t *testing.T) {
{"global key", http.MethodPost, "/v1/messages", `{"model":"zai@global/glm-4.6"}`, true, kindAnthropic, "zai@global/anthropic", ""},
{"no source", http.MethodPost, "/v1/chat/completions", `{"model":"glm-4.6"}`, false, "", "", ""},
{"bad source", http.MethodPost, "/v1/chat/completions", `{"model":"zai@team/glm-4.6"}`, false, "", "", ""},
{"bad hyphenated source", http.MethodPost, "/v1/chat/completions", `{"model":"open--code/glm-5.3"}`, false, "", "", ""},
{"empty model", http.MethodPost, "/v1/chat/completions", `{"model":"zai/"}`, false, "", "", ""},
{"not json", http.MethodPost, "/v1/chat/completions", `nope`, false, "", "", ""},
{"wrong method", http.MethodGet, "/v1/chat/completions", "", false, "", "", ""},
{"unknown path", http.MethodPost, "/v1/embeddings", `{"model":"zai/x"}`, false, "", "", ""},
{"responses", http.MethodPost, "/v1/responses", `{"model":"chatgpt/gpt-5-codex"}`, true, kindResponses, "chatgpt/responses", ""},
{"hyphenated source", http.MethodPost, "/v1/chat/completions", `{"model":"opencode-go/glm-5.3"}`, true, kindOpenAI, "opencode-go/openai", ""},
}
l := &LLM{}
for _, tc := range cases {
Expand Down
46 changes: 46 additions & 0 deletions backstage/intproxy/internal/intproxy/llm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,52 @@ func TestEndToEndLLMUsesTheBrokersUpstream(t *testing.T) {
}
}

func TestEndToEndLLMUsesAnthropicAPIKeyAndKeepsOpenCodeSession(t *testing.T) {
var seen struct{ auth, apiKey, session, client, userAgent, path, model string }
upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct{ Model string }
_ = json.NewDecoder(r.Body).Decode(&body)
seen.auth = r.Header.Get("Authorization")
seen.apiKey = r.Header.Get("X-Api-Key")
seen.session = r.Header.Get("X-OpenCode-Session")
seen.client = r.Header.Get("X-OpenCode-Client")
seen.userAgent = r.Header.Get("User-Agent")
seen.path, seen.model = r.URL.Path, body.Model
_, _ = w.Write([]byte(`{"id":"x"}`))
}))
defer upstream.Close()

src := fakeSource{fn: func(credential.Scope) (credential.Token, error) {
return credential.Token{Value: "go_key", AuthHeader: "x-api-key", Upstream: upstream.URL + "/zen/go"}, nil
}}
s := testServer(t, src, "")
s.rp.Transport = upstream.Client().Transport
host := withLLM(t, s)

r := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(`{"model":"opencode-go@global/minimax-m3","messages":[]}`))
r.Host = host
r.Header.Set("Authorization", "Bearer guest-key")
r.Header.Set("X-Api-Key", "guest-key")
r.Header.Set("X-OpenCode-Session", "conversation-1")
r.Header.Set("X-OpenCode-Client", "pi")
r.Header.Set("User-Agent", "pi/1.2.3")
w := httptest.NewRecorder()
s.ServeHTTP(w, r)

if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %q", w.Code, w.Body.String())
}
if seen.auth != "" || seen.apiKey != "go_key" {
t.Fatalf("upstream auth = %q, x-api-key = %q", seen.auth, seen.apiKey)
}
if seen.session != "conversation-1" || seen.client != "pi" || seen.userAgent != "pi/1.2.3" {
t.Fatalf("upstream attribution headers = %+v", seen)
}
if seen.path != "/zen/go/v1/messages" || seen.model != "minimax-m3" {
t.Fatalf("upstream path = %q, model = %q", seen.path, seen.model)
}
}

func TestEndToEndChatGPTSendsTheBrokersAccount(t *testing.T) {
var seen struct{ auth, account, beta, path string }
upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 3 additions & 0 deletions control/internal/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ type IntegrationTokenResponse struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Account string `json:"account,omitempty"`
// AuthHeader is empty for Authorization: Bearer. The llm integration also
// accepts x-api-key for providers using the Anthropic wire protocol.
AuthHeader string `json:"auth_header,omitempty"`
// Upstream is the https base the token belongs to, when the integration
// has more than one.
Upstream string `json:"upstream,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions control/llm_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
llmGlobalSuffix = "@global"
)

var llmResourceRe = regexp.MustCompile(`^([a-z0-9]+)(@global)?/(openai|anthropic|responses)$`)
var llmResourceRe = regexp.MustCompile(`^([a-z0-9]+(?:-[a-z0-9]+)*)(@global)?/(openai|anthropic|responses)$`)

var llmFormatPaths = map[string]string{
"openai": "/v1/chat/completions",
Expand Down Expand Up @@ -64,7 +64,7 @@ func (h *ClientHandler) llmToken(c *echo.Context, client db.Client, vmIP, resour
}
return echo.NewHTTPError(http.StatusForbidden, provider+" does not serve "+llmFormatPaths[format]+"; use "+strings.Join(served, " or "))
}
resp := proto.IntegrationTokenResponse{Upstream: upstream}
resp := proto.IntegrationTokenResponse{Upstream: upstream, AuthHeader: plan.authHeader(format)}
if llmUsesDevice(k.Provider) {
cred, err := h.chatgptAccess(c.Request().Context(), k)
if errors.Is(err, errLLMKeyRejected) {
Expand Down
23 changes: 21 additions & 2 deletions control/llm_broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import (
)

func TestLLMResourceShape(t *testing.T) {
for _, ok := range []string{"zai/openai", "zai@global/anthropic", "chatgpt/responses"} {
for _, ok := range []string{"zai/openai", "zai@global/anthropic", "chatgpt/responses", "opencode-go/openai", "opencode-go@global/responses"} {
if llmResourceRe.FindStringSubmatch(ok) == nil {
t.Errorf("%q was refused", ok)
}
}
for _, bad := range []string{"", "zai", "zai/", "zai@team/openai", "zai/gemini", "ZAI/openai", "zai/openai/x"} {
for _, bad := range []string{"", "zai", "zai/", "zai@team/openai", "zai/gemini", "ZAI/openai", "zai/openai/x", "-opencode/openai", "opencode-/openai", "open--code/openai"} {
if llmResourceRe.FindStringSubmatch(bad) != nil {
t.Errorf("%q was accepted", bad)
}
}
m := llmResourceRe.FindStringSubmatch("opencode-go@global/anthropic")
if m == nil || m[1] != "opencode-go" || m[2] != "@global" || m[3] != "anthropic" {
t.Fatalf("captures = %q", m)
}
}

func TestLLMPlansResolveToHTTPS(t *testing.T) {
Expand Down Expand Up @@ -45,4 +49,19 @@ func TestLLMPlansResolveToHTTPS(t *testing.T) {
if _, ok := llmPlanFor("zai", "enterprise"); ok {
t.Error("an unknown plan resolved")
}
p, ok := llmPlanFor("opencode-go", "go")
if !ok {
t.Fatal("opencode-go/go did not resolve")
}
if p.OpenAIBase != "https://opencode.ai/zen/go/v1" ||
p.AnthropicBase != "https://opencode.ai/zen/go" ||
p.ResponsesBase != "https://opencode.ai/zen/go/v1" {
t.Errorf("opencode-go/go bases = %+v", p)
}
if p.authHeader("anthropic") != "x-api-key" || p.authHeader("openai") != "" {
t.Errorf("opencode-go/go auth headers = %v", p.AuthHeaders)
}
if _, ok := llmPlanFor("opencode-go", "go-plus"); ok {
t.Error("the redundant go-plus plan resolved")
}
}
23 changes: 14 additions & 9 deletions control/llm_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ type LLMKeyHandler struct {
}

type llmKeyDTO struct {
Provider string `json:"provider"`
Label string `json:"label"`
Auth string `json:"auth"`
Plans []llmPlan `json:"plans"`
Plan string `json:"plan"`
KeySet bool `json:"key_set"`
UpdatedAt string `json:"updated_at,omitempty"`
Provider string `json:"provider"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
KeyURL string `json:"key_url,omitempty"`
Auth string `json:"auth"`
Plans []llmPlan `json:"plans"`
Plan string `json:"plan"`
KeySet bool `json:"key_set"`
UpdatedAt string `json:"updated_at,omitempty"`
}

type putLLMKeyReq struct {
Expand All @@ -44,7 +46,10 @@ func toLLMKeyDTOs(keys []db.LlmKey) []llmKeyDTO {
}
out := make([]llmKeyDTO, 0, len(llmProviders))
for _, p := range llmProviders {
d := llmKeyDTO{Provider: p.ID, Label: p.Label, Auth: p.Auth, Plans: p.Plans, Plan: p.Plans[0].ID}
d := llmKeyDTO{
Provider: p.ID, Label: p.Label, Description: p.Description, KeyURL: p.KeyURL,
Auth: p.Auth, Plans: p.Plans, Plan: p.Plans[0].ID,
}
if k, ok := byProvider[p.ID]; ok {
d.Plan, d.KeySet = k.Plan, true
d.UpdatedAt = k.UpdatedAt.Time.Format(time.RFC3339)
Expand Down Expand Up @@ -156,7 +161,7 @@ func (h *LLMKeyHandler) put(c *echo.Context, owner, by pgtype.UUID) error {
}
}

if _, err := fetchLLMModels(ctx, plan, key); err != nil {
if err := checkLLMKey(ctx, plan, key); err != nil {
if errors.Is(err, errLLMKeyRejected) {
return echo.NewHTTPError(http.StatusBadRequest, "the provider rejected this key for the "+plan.Label+" plan")
}
Expand Down
72 changes: 63 additions & 9 deletions control/llm_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
)

type llmPlan struct {
ID string `json:"id"`
Label string `json:"label"`
OpenAIBase string `json:"-"`
AnthropicBase string `json:"-"`
ResponsesBase string `json:"-"`
ID string `json:"id"`
Label string `json:"label"`
OpenAIBase string `json:"-"`
AnthropicBase string `json:"-"`
ResponsesBase string `json:"-"`
KeyCheckURL string `json:"-"`
AuthHeaders map[string]string `json:"-"`
}

// base is where this plan serves the api format names, or "" if it does not.
Expand All @@ -34,16 +36,24 @@ func (p llmPlan) base(format string) string {
return ""
}

// authHeader is empty for the usual Authorization bearer token. A plan can
// name x-api-key for an api format that follows Anthropic's wire protocol.
func (p llmPlan) authHeader(format string) string {
return p.AuthHeaders[format]
}

const (
llmAuthKey = "key"
llmAuthDevice = "device"
)

type llmProvider struct {
ID string `json:"id"`
Label string `json:"label"`
Auth string `json:"auth"`
Plans []llmPlan `json:"plans"`
ID string `json:"id"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
KeyURL string `json:"key_url,omitempty"`
Auth string `json:"auth"`
Plans []llmPlan `json:"plans"`
}

// llmProviders is every upstream a key can be stored for. The ids are also the
Expand All @@ -56,6 +66,20 @@ var llmProviders = []llmProvider{{
{ID: "api", Label: "API", OpenAIBase: "https://api.z.ai/api/paas/v4", AnthropicBase: "https://api.z.ai/api/anthropic"},
{ID: "coding", Label: "Coding plan", OpenAIBase: "https://api.z.ai/api/coding/paas/v4", AnthropicBase: "https://api.z.ai/api/anthropic"},
},
}, {
ID: "opencode-go",
Label: "OpenCode Go",
Description: "Works with both Go and Go Plus subscriptions; OpenCode applies the limits for your account.",
KeyURL: "https://opencode.ai/auth",
Auth: llmAuthKey,
Plans: []llmPlan{
{
ID: "go", Label: "Go / Go Plus",
OpenAIBase: "https://opencode.ai/zen/go/v1", AnthropicBase: "https://opencode.ai/zen/go",
ResponsesBase: "https://opencode.ai/zen/go/v1", KeyCheckURL: "https://opencode.ai/zen/go/v1/usage",
AuthHeaders: map[string]string{"anthropic": "x-api-key"},
},
},
}, {
ID: "chatgpt",
Label: "ChatGPT",
Expand Down Expand Up @@ -132,6 +156,36 @@ func fetchLLMModels(ctx context.Context, plan llmPlan, key string) ([]llmModel,
return list.Data, nil
}

// checkLLMKey verifies a credential before it is stored. Most providers
// authenticate their model catalog; OpenCode's catalog is public, so its plan
// points at the authenticated, non-generating usage endpoint instead.
func checkLLMKey(ctx context.Context, plan llmPlan, key string) error {
if plan.KeyCheckURL == "" {
_, err := fetchLLMModels(ctx, plan, key)
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, plan.KeyCheckURL, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+key)
resp, err := llmHTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<20))

switch {
case resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden:
return errLLMKeyRejected
case resp.StatusCode != http.StatusOK:
return fmt.Errorf("checking the key returned %d", resp.StatusCode)
default:
return nil
}
}

const llmModelsTTL = 10 * time.Minute

// llmModelsCache keeps each key's model list for llmModelsTTL, shared across
Expand Down
Loading
Loading