Files
magpie/internal/provider/kilo_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

124 lines
5.2 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// kiloList is the Kilo Gateway's /models as it answers (October 2026):
// OpenRouter's shape with isFree, an auto router priced at -1, a free
// model by its flag alone, ":free" ones, one that takes no tools and one
// that only draws.
const kiloList = `{"data":[
{"id":"kilo-auto/efficient","name":"Auto Efficient","context_length":1000000,"pricing":{"prompt":"-1","completion":"-1"},"supported_parameters":["tools","reasoning"],"isFree":false},
{"id":"kilo-auto/free","name":"Auto Free","context_length":256000,"pricing":{"prompt":"0","completion":"0"},"supported_parameters":["tools"],"isFree":true},
{"id":"stealth/space-bunny-alpha","name":"Space Bunny Alpha","context_length":1000000,"top_provider":{"max_completion_tokens":65536},"architecture":{"input_modalities":["text","image"],"output_modalities":["text"]},"pricing":{"prompt":"0","completion":"0"},"supported_parameters":["tools","reasoning"],"isFree":true},
{"id":"qwen/qwen3.8-27b:free","name":"Qwen: Qwen3.8 27B (free)","context_length":262144,"supported_parameters":["tools"],"isFree":true},
{"id":"nvidia/nemotron-3.5-content-safety:free","name":"NVIDIA: Nemotron 3.5 Content Safety (free)","context_length":128000,"supported_parameters":["max_tokens"],"isFree":true},
{"id":"google/lyria-3-pro-preview","name":"Lyria","context_length":1048576,"architecture":{"output_modalities":["audio"]},"pricing":{"prompt":"0","completion":"0"},"isFree":false},
{"id":"anthropic/claude-sonnet-5","name":"Anthropic: Claude Sonnet 5","context_length":1000000,"pricing":{"prompt":"0.000003","completion":"0.000015"},"supported_parameters":["tools"],"isFree":false}
]}`
// The Kilo Gateway serves its free models to anyone (by IP), and Kilo's
// clients list them from its /models, asked as the Kilo CLI: with no key
// the provider lists the free ones alone, asked with no Authorization;
// with a key every model, the free ones marked Free and priced at nothing
// (lml on Discord: the community plugin's free models from KiloCode).
func TestKiloModels(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
var asked []*http.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/openrouter/models" {
http.NotFound(w, r)
return
}
asked = append(asked, r)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(kiloList))
}))
defer srv.Close()
p, err := FromPreset("kilo")
if err != nil {
t.Fatal(err)
}
if !p.Ready() {
t.Fatal("a Kilo provider with no key isn't ready")
}
p.ID, p.Chat = "kilo-test", srv.URL+"/api/openrouter"
ids := func(ms []catalog.Model) (all, free []string) {
for _, m := range ms {
all = append(all, m.ID)
if m.Free {
free = append(free, m.ID)
}
}
return
}
ms, err := p.Fetch(context.Background())
if err != nil {
t.Fatal(err)
}
h := asked[0].Header
for k, v := range map[string]string{"Authorization": "", "User-Agent": "Kilo-Code/" + KiloVersion,
"X-KILOCODE-EDITORNAME": "Kilo CLI " + KiloVersion, "HTTP-Referer": "https://kilocode.ai", "X-Title": "Kilo Code"} {
if h.Get(k) != v {
t.Errorf("list asked with %s %q, want %q", k, h.Get(k), v)
}
}
all, free := ids(ms)
want := []string{"kilo-auto/free", "stealth/space-bunny-alpha", "qwen/qwen3.8-27b:free"}
if !slices.Equal(all, want) || !slices.Equal(free, want) {
t.Fatalf("no key: %v (free %v)", all, free)
}
if m := ms[1]; m.Context != 1000000 || m.Output != 65536 || !m.Images || !m.Reasoning || m.Name != "Space Bunny Alpha" {
t.Fatalf("model: %+v", m)
}
// a free model by its flag alone costs nothing, not its maker's price
if pr, ok := p.ListPrice("stealth/space-bunny-alpha"); !ok || pr != (catalog.Price{}) {
t.Fatalf("free price: %+v %v", pr, ok)
}
p.Key = "kilo_jwt"
ms, err = p.Fetch(context.Background())
if err != nil {
t.Fatal(err)
}
if got := asked[1].Header.Get("Authorization"); got != "Bearer kilo_jwt" {
t.Fatalf("with a key, asked with %q", got)
}
all, free = ids(ms)
if !slices.Equal(all, []string{"kilo-auto/efficient", "kilo-auto/free", "stealth/space-bunny-alpha", "qwen/qwen3.8-27b:free", "anthropic/claude-sonnet-5"}) || !slices.Equal(free, want) {
t.Fatalf("with a key: %v (free %v)", all, free)
}
// another provider's ":free" model isn't Kilo's to price
o, _ := FromPreset("together")
if o.kiloFreeModel("qwen/qwen3.8-27b:free") {
t.Fatal("kiloFreeModel on another provider")
}
}
// The CLI's headers: the session as its task, and no Authorization at all
// when there is no key (a placeholder bearer is refused).
func TestKiloClient(t *testing.T) {
h := http.Header{"Authorization": {"Bearer "}}
KiloClient(h, "", "conversation-1")
id := h.Get("X-KILOCODE-TASKID")
if !openCodeIDRe.MatchString(id) || id[:4] != "ses_" || h.Get("x-session-affinity") != id || h.Get("X-Session-Id") != id ||
h.Get("x-kilocode-mode") != "code" || len(h.Values("Authorization")) != 0 {
t.Fatalf("headers: %v", h)
}
h2 := http.Header{}
KiloClient(h2, "k", "conversation-1")
if h2.Get("X-KILOCODE-TASKID") != id || h2.Get("Authorization") != "" {
t.Fatalf("with a key: %v", h2)
}
}