A provider's own headers were written into the request as typed (req.Header[k] = …) to keep their case, beside magpie's own written with Header.Set under the canonical name. A "user-agent" or "authorization" typed in lower case, as devtools show headers, therefore went out next to magpie's: two User-Agents, magpie's (or Go's Go-http-client/1.1, which Go's HTTP/1 writer adds when it finds no canonical "User-Agent") first. The reporter's relay checks the client and answered every probe 401 "unauthorized client detected", so the headers looked ignored. catalog.PutUserHeader now takes every header of the same name in any case off before writing the user's, and keeps User-Agent under its canonical name so Go adds none. It is used everywhere the provider's headers are applied as typed: Sign (forwarded calls, endpoint tests, detection), the model lists (catalog FetchURL/FetchAt, Gemini pages), Kilo's list and the Gemini image path. Azure's list merge drops an api-key of another case too. The balance read already used Header.Set. Run: curl against the reporter's relay (ps.air-outer.com, HTTP/1.1): User-Agent "claude-cli/2.1.0 (external, cli)" alone gets past its client check (to "无效的令牌" for a fake key); curl/8, or Go-http-client/1.1 ahead of the claude-cli one, gets "unauthorized client detected". A scratch Go client confirmed Go sends "User-Agent: Go-http-client/1.1" beside a lower-case "user-agent". TestUserHeadersInLowerCaseAreSentOnce (HTTP/1.1 httptest server that refuses two User-Agents or two Authorizations) fails without the change on list, endpoints and detect (User-Agent ["Go-http-client/1.1" "claude-cli/…"], Authorization ["Bearer sk-test" "Bearer relay-own"]) and passes with it. Not tried through a built magpie against the relay with the reporter's key.
176 lines
5.4 KiB
Go
176 lines
5.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// KiloVersion is the Kilo CLI release magpie says it is to the Kilo
|
|
// Gateway.
|
|
const KiloVersion = "7.8.3"
|
|
|
|
// KiloClient makes a request to the Kilo Gateway carry what the Kilo CLI
|
|
// sends it (Kilo-Org/kilocode packages/opencode/src/session/llm/request.ts
|
|
// and kilocode/const.ts, packages/kilo-gateway/src/headers.ts): Kilo
|
|
// Code's attribution and User-Agent, the editor it runs in, its mode, and
|
|
// the session as the task, which the gateway keys its prompt cache on.
|
|
// session names the conversation, made into one of the CLI's ids (ses_…)
|
|
// as for OpenCode; none leaves the session headers out. A provider with no
|
|
// key sends no Authorization at all: the gateway answers a free model to a
|
|
// caller with no credential as anonymous (by IP, 200 requests an hour), and
|
|
// refuses a bearer it can't verify with 401 (Kilo-Org/cloud
|
|
// web-shared/src/lib/tokens.ts, ai-gateway/handlers/llm-proxy.ts). Kilo's
|
|
// clients still send "Bearer anonymous", which the gateway reads as none
|
|
// only until no released client sends it, so it is not copied here.
|
|
func KiloClient(h http.Header, key, session string) {
|
|
h.Set("HTTP-Referer", "https://kilocode.ai")
|
|
h.Set("X-Title", "Kilo Code")
|
|
h.Set("User-Agent", "Kilo-Code/"+KiloVersion)
|
|
h.Set("X-KILOCODE-EDITORNAME", "Kilo CLI "+KiloVersion)
|
|
if session != "" {
|
|
id := openCodeID("ses", session)
|
|
h.Set("x-kilocode-mode", "code")
|
|
h.Set("X-KILOCODE-TASKID", id)
|
|
h.Set("x-session-affinity", id)
|
|
h.Set("X-Session-Id", id)
|
|
}
|
|
if key == "" {
|
|
h.Del("Authorization")
|
|
}
|
|
}
|
|
|
|
// IsKilo reports whether the provider is the Kilo Gateway: made from its
|
|
// preset, or at Kilo's API host.
|
|
func (p Provider) IsKilo() bool {
|
|
if p.Preset == "kilo" {
|
|
return true
|
|
}
|
|
h := p.Host()
|
|
return h == "api.kilo.ai" || h == "api.kilocode.ai"
|
|
}
|
|
|
|
// kiloModels is the gateway's list as Kilo's clients ask it (GET
|
|
// {base}/models, kilo-gateway src/api/models.ts): OpenRouter's shape, with
|
|
// isFree on the models it serves at no cost. Without a key only those are
|
|
// listed, the rest wanting a Kilo account; with one, all of them, the free
|
|
// ones marked. A model that says it takes no tools is left out, as Kilo
|
|
// leaves it out.
|
|
func (p Provider) kiloModels(ctx context.Context) ([]catalog.Model, string, error) {
|
|
base := strings.TrimRight(strings.TrimSpace(p.Chat), "/")
|
|
u := base + "/models"
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
|
if err != nil {
|
|
return nil, base, err
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
KiloClient(req.Header, p.Key, "")
|
|
if p.Key != "" {
|
|
req.Header.Set("Authorization", "Bearer "+p.Key)
|
|
}
|
|
for k, v := range p.Headers {
|
|
catalog.PutUserHeader(req.Header, k, v)
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, base, err
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, base, errorf("%s: HTTP %d", u, res.StatusCode)
|
|
}
|
|
var list struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
ContextLength any `json:"context_length"`
|
|
MaxOutput any `json:"max_completion_tokens"`
|
|
Params []string `json:"supported_parameters"`
|
|
IsFree bool `json:"isFree"`
|
|
TopProvider struct {
|
|
MaxOutput any `json:"max_completion_tokens"`
|
|
} `json:"top_provider"`
|
|
Architecture struct {
|
|
Input []string `json:"input_modalities"`
|
|
Output []string `json:"output_modalities"`
|
|
} `json:"architecture"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(b, &list); err != nil {
|
|
return nil, base, errorf("%s: not a model list", u)
|
|
}
|
|
num := func(v any) int {
|
|
if n, ok := v.(float64); ok && n > 0 {
|
|
return int(n)
|
|
}
|
|
return 0
|
|
}
|
|
var ms []catalog.Model
|
|
for _, r := range list.Data {
|
|
if r.ID == "" || len(r.Params) > 0 && !slices.Contains(r.Params, "tools") {
|
|
continue
|
|
}
|
|
if out := r.Architecture.Output; len(out) > 0 && !slices.Contains(out, "text") {
|
|
continue // one that only draws or makes sound
|
|
}
|
|
free := r.IsFree || isKiloFree(r.ID)
|
|
if p.Key == "" && !free {
|
|
continue
|
|
}
|
|
name := r.Name
|
|
if name == "" {
|
|
name = r.ID
|
|
}
|
|
m := catalog.Model{ID: r.ID, Name: name, Free: free, Context: num(r.ContextLength),
|
|
Reasoning: slices.Contains(r.Params, "reasoning")}
|
|
if m.Output = num(r.TopProvider.MaxOutput); m.Output == 0 {
|
|
m.Output = num(r.MaxOutput)
|
|
}
|
|
if in := r.Architecture.Input; in != nil {
|
|
yes := slices.Contains(in, "image")
|
|
m.ImageInput, m.Images = &yes, yes
|
|
}
|
|
if free {
|
|
m.Price = &catalog.Price{}
|
|
}
|
|
ms = append(ms, m)
|
|
}
|
|
if len(ms) == 0 {
|
|
return nil, base, errors.New(u + ": no models listed")
|
|
}
|
|
return ms, base, nil
|
|
}
|
|
|
|
// isKiloFree reports whether id is one of the gateway's free models by its
|
|
// id alone: a ":free" one, which its docs say anyone may ask, or Kilo's
|
|
// own free router.
|
|
func isKiloFree(id string) bool {
|
|
return strings.HasSuffix(id, ":free") || id == "kilo-auto/free"
|
|
}
|
|
|
|
// kiloFreeModel reports whether model is one of the Kilo Gateway's free
|
|
// models on p: one the fetched list marks free, else one free by its id.
|
|
func (p Provider) kiloFreeModel(model string) bool {
|
|
if !p.IsKilo() {
|
|
return false
|
|
}
|
|
if ms, _, ok := p.live(); ok {
|
|
for _, m := range ms {
|
|
if m.ID == model {
|
|
return m.Free
|
|
}
|
|
}
|
|
}
|
|
return isKiloFree(model)
|
|
}
|