Files
magpie/internal/gateway/kilo_client_test.go
T
yetone bb136e1b70 providers: a Kilo Gateway preset serves Kilo Code's free models with no key (lml on Discord: the community plugin's free models come from OpenCode Zen, KiloCode Gateway and Cline) — Kilo's gateway is OpenRouter's API at https://api.kilo.ai/api/openrouter (KILO_OPENROUTER_BASE in Kilo-Org/kilocode packages/kilo-gateway; /api/gateway is the same), its list GET {base}/models in OpenRouter's shape with an isFree flag, readable with no key, and its docs (kilo-docs gateway/authentication.md) and backend (Kilo-Org/cloud web-shared ai-gateway/handlers/llm-proxy.ts, is-free-model.ts: kilo-auto/free, ":free", openrouter/free, stealth/*-alpha, Kilo's own free models) serve a free model to a caller with no credential, by IP, 200 requests an hour. The new "kilo" preset (Kilo Gateway, NoKey) is Ready with no key: its list is the gateway's own, asked as the Kilo CLI asks it, the free models alone without a key and every model with one, the free ones (isFree, or free by id) marked Free and priced at nothing in ListPrice; a model that says it takes no tools, or that makes no text (Lyria), is left out. Requests to Kilo's host (the gateway's and the Test button's) carry the Kilo CLI 7.8.3's headers (packages/opencode session/llm/request.ts, kilocode/const.ts, kilo-gateway headers.ts): HTTP-Referer https://kilocode.ai, X-Title Kilo Code, User-Agent Kilo-Code/7.8.3, X-KILOCODE-EDITORNAME "Kilo CLI 7.8.3", and on a chat x-kilocode-mode code with the conversation as its task (X-KILOCODE-TASKID, x-session-affinity, X-Session-Id, a ses_ id as for OpenCode), which the gateway keys its prompt cache on; no other provider gets them. Without a key no Authorization is sent: Kilo's clients send "Bearer anonymous", which the gateway reads as no credential only until no released client sends it (tokens.ts ANONYMOUS_BEARER_SENTINEL, "Delete this constant once no released client sends it"), and any other bearer it can't verify is refused with 401 rather than served as anonymous; pi-freeflow measured that a placeholder bearer gets 401 INVALID_TOKEN and no Authorization gets 200 on every free model. A key, when given, goes as a Bearer and reaches the paid models. The key field can now say more than "optional for local servers" (PresetDef.KeyHint: "optional: free models need no key", 可不填:免费模型无需密钥); Kilo's icon is its own (kilocode packages/ui provider/kilo.svg, MIT). Only Chat is set; Kilo's Responses and Anthropic routes are not used. One GET of the public /models was made while looking (399 models, 18 free); no chat request was made to Kilo. TestKiloModels (a fake gateway: asked as the CLI with no Authorization, the free models alone without a key and all with one, a model free by its flag priced at nothing), TestKiloClient and TestKiloClientHeaders (the CLI's headers and no Authorization on a keyless request through the gateway, the key on a keyed one, none of them on another provider) fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/... pass (TestClaudeRunEndsWhenTheClientLeaves, TestUserPath and TestClaudeSignedOut failed once under the full run's load, pass on the rerun; none touches Kilo).
2026-10-02 14:03:31 +08:00

85 lines
3.2 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The Kilo Gateway's free models are served with no key: a Kilo provider
// saved without one is asked as the Kilo CLI asks (its attribution and
// editor, the conversation as its task) with no Authorization, one with
// a key sends that key, and no other provider gets Kilo's headers.
func TestKiloClientHeaders(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
free, err := provider.FromPreset("kilo")
if err != nil {
t.Fatal(err)
}
free.ID, free.Models = "kilo", []string{"qwen/qwen3.8-27b:free"}
keyed := free
keyed.ID, keyed.Key, keyed.Chat = "kilo-keyed", "kilo_jwt", "https://api.kilocode.ai/api/openrouter"
for _, p := range []provider.Provider{free, keyed,
{ID: "other", Name: "Other", Key: "k", Chat: "https://other.test/v1", Models: []string{"qwen/qwen3.8-27b:free"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
got := map[string]*http.Request{}
s := New()
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
got[r.URL.Host] = r.Clone(r.Context())
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"id":"c","object":"chat.completion","model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"pong"},"finish_reason":"stop"}]}`))}, nil
})}
for _, id := range []string{"kilo", "kilo-keyed", "other"} {
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+id+`/qwen/qwen3.8-27b:free","messages":[{"role":"user","content":"Say pong"}]}`))
req.Header.Set("User-Agent", "some-agent/1.0")
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", id, rec.Code, rec.Body)
}
}
k, kk, o := got["api.kilo.ai"], got["api.kilocode.ai"], got["other.test"]
if k == nil || kk == nil || o == nil {
t.Fatalf("requests: %v", got)
}
if k.URL.Path != "/api/openrouter/chat/completions" {
t.Errorf("path %s", k.URL.Path)
}
want := map[string]string{
"HTTP-Referer": "https://kilocode.ai", "X-Title": "Kilo Code",
"User-Agent": "Kilo-Code/" + provider.KiloVersion, "X-KILOCODE-EDITORNAME": "Kilo CLI " + provider.KiloVersion,
"x-kilocode-mode": "code",
}
if a := k.Header.Values("Authorization"); len(a) != 0 {
t.Errorf("keyless kilo sent Authorization %q", a)
}
for k2, v := range want {
if k.Header.Get(k2) != v {
t.Errorf("kilo: %s %q, want %q", k2, k.Header.Get(k2), v)
}
}
if id := k.Header.Get("X-KILOCODE-TASKID"); !strings.HasPrefix(id, "ses_") || k.Header.Get("X-Session-Id") != id {
t.Errorf("kilo task %q", id)
}
if got := kk.Header.Get("Authorization"); got != "Bearer kilo_jwt" {
t.Errorf("keyed kilo: Authorization %q", got)
}
for _, h := range []string{"X-KILOCODE-EDITORNAME", "X-KILOCODE-TASKID", "X-Title"} {
if o.Header.Get(h) != "" {
t.Errorf("other provider got %s", h)
}
}
if o.Header.Get("Authorization") != "Bearer k" {
t.Errorf("other: %q", o.Header.Get("Authorization"))
}
}