Files
magpie/internal/provider/kilo.go
T
yetone 8e6354714f provider: OpenCode Zen's free models answer again, with a session id OpenCode itself could have made (c9cu on Discord)
c9cu (v0.1.1222, Pi, a group routed to OpenCode Zen's
muse-spark-1.3-contributor-free on /v1/responses) got 403 FreeTierError
"OpenCode's free tier can only be used from within OpenCode" on a setup
that worked the day before. Zen now reads x-opencode-session as
OpenCode's identifier.ts lays an id out (12 hex of ~(Date.now()*0x1000 +
counter), then 14 base62) and refuses one whose counter isn't small.
magpie's built-in sent a sha256 of the conversation cut to that shape,
so its counter was anything. The zen-free plugin hit the same (#100 on
magpie-community/plugins, 0.1.5); the built-in had the same code.

Header diff against the real upstream, same request with curl:
- magpie's ses_e9283da055b15JwOAra76bwUx4 (hash): 403 on /v1/responses
  and /v1/chat/completions.
- ses_ecc5d8f28ffe9Cy14V8Em7lI2u from a real OpenCode 1.19.0 run, the
  old opencode/1.18.34 UA unchanged: 200 on /v1/responses. So it is the
  session, not the UA or the endpoint; muse-spark is still served on
  Responses (Chat answers 400 ModelProtocolUnsupported, as before).

Now (opencode_client.go):
- openCodeSession makes a real descending id from the current time and
  a per-millisecond counter, kept per conversation (the last 1000) so a
  conversation's turns share one session; one OpenCode could have made
  is passed on unchanged, a hash-shaped one is not.
- x-opencode-request is an ascending msg_ id the same way.
- the session also goes in x-opencode-session-id, x-session-affinity
  and X-Session-Id, as OpenCode's packages/core/src/session/
  model-request.ts sends it, and the User-Agent is OpenCode 1.19.0's
  (opencode/1.19.0 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14,
  the latest on npm, as the plugin captured it).
- Kilo's task/session id goes through the same openCodeSession (the
  Kilo CLI is OpenCode's fork and makes ids the same way).

Run:
- Through an in-process gateway under a sandbox HOME against the real
  opencode.ai/zen with the public key: muse-spark-1.3-contributor-free
  on /v1/responses with reasoning xhigh, streamed and not, big-pickle
  streamed on Chat, mimo-v2.6-flash-free on Chat: all 200 "pong". The
  same file on origin/main's code: all 403 FreeTierError, the
  reporter's message word for word.
- TestOpenCodeSessionIsOpenCodes, TestOpenCodeSessionsKeptBounded and
  TestOpenCodeClientHeaders (now with a real 1.19.0 session) fail with
  the hash put back by hand.
- gofmt -l, go vet (darwin, GOOS=linux, GOOS=windows -tags nogui) on
  internal/provider and internal/gateway.
- go test -tags nogui -count=1 ./... under a temp HOME on base
  eba718736 (load average ~170): every package ok but provider
  (TestPluginModelsSayExpired, TempDir cleanup race, added to #1543),
  gateway and agent (10m timeout in TestNotAnAPIReplyFailsOver and
  TestOmpLevelSuffix). Rerun with -timeout 40m: provider, gateway and
  agent ok.
- Rebased onto 488569227 (kilo.go's model paging, no overlap) and not
  rebuilt there: the disk was under 10G free.

- Kilo Gateway, anonymous, nvidia/nemotron-3.5-lightning:free through
  the same in-process gateway: 200 twice.
2026-10-13 14:03:58 +08:00

204 lines
6.7 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 := openCodeSession(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()
var ms []catalog.Model
err := catalog.Paged(u, func(at string) (bool, string, error) {
page, more, last, err := p.kiloModelsPage(ctx, at)
if err != nil {
return false, "", err
}
ms = append(ms, page...)
return more, last, nil
})
if err != nil {
return nil, base, err
}
if len(ms) == 0 {
return nil, base, errors.New(u + ": no models listed")
}
return ms, base, nil
}
// kiloModelsPage asks the gateway's list at at, one page of it, and says
// whether another page follows, and the id it starts after. The reply's
// shape is OpenRouter's, which pages with has_more and last_id, so the
// pages are followed to the end of the list (catalog.Paged) rather than
// refused: a page read as the whole list drops every model of the pages
// after from the user's picks (#904). As far as was measured on 2026-10-06
// the gateway answers with the whole list either way — 400 models and no
// has_more at api.kilo.ai/api/openrouter/models, 466 and links.next: null
// at OpenRouter's own /api/v1/models — so this is the shape's rule kept in
// one place, for the day either of them does answer with a page.
func (p Provider) kiloModelsPage(ctx context.Context, at string) ([]catalog.Model, bool, string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, at, nil)
if err != nil {
return nil, false, "", 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, false, "", err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
if res.StatusCode != http.StatusOK {
return nil, false, "", errorf("%s: HTTP %d", at, 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"`
HasMore bool `json:"has_more"`
LastID string `json:"last_id"`
}
if err := json.Unmarshal(b, &list); err != nil {
return nil, false, "", errorf("%s: not a model list", at)
}
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)
}
return ms, list.HasMore, list.LastID, 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)
}