ergou0954_54426 on Discord routes by intent across Kiro's Claude and GPT models and DeepSeek, and sometimes a turn failed with Kiro saying the context was exceeded. Kiro says a conversation is past the model's window in three ways, and the plugin up to 0.1.23 turned only the HTTP 400 into the "input is too long for the model's context" that the gateway reads as an overflow (tooLong). The same ValidationException as an exception in Kiro's event stream came out as a 502, so the gateway took it for a passing vendor fault: it rested the account, asked Kiro again (15 asks for a lone provider in a probe), and never told the agent context_length_exceeded, so the agent never compacted. A reply Kiro stopped with MODEL_CONTEXT_WINDOW_EXCEEDED came out as finished. plugins e8050e8 (kiro 0.1.24, on npm) answers both as the 400 that says too long. Routing was already right: roomy skips a member whose known window the request is past, and withRoom moves an overflow to a member with room. The min goes to 0.1.24 so keepMovedCurrent updates every moved user. TestKiroMoverHasTheTooLongFix holds it there. TestKiroPluginOverflowGoesToAMemberWithRoom pins what the gateway does with 0.1.24's two wordings: in a group the other member answers, the try is failOverflow with no rest, and alone the agent gets context_length_exceeded after a single ask. Run on mote: go vet ./internal/provider ./internal/gateway; go test -timeout 45m -count=1 ./internal/provider (ok 150s) ./internal/gateway (ok 712s). gofmt -l clean. Broken by hand: at origin/main's min 0.1.23, TestKiroMoverHasTheTooLongFix fails. A probe with 0.1.23's 502 body showed Fail other, a rest, and a vendor 502 for the lone provider. Not run: the full ./... suite (another session's full suite held mote), and a real Kiro account pushed past its window.
181 lines
8.5 KiB
Go
181 lines
8.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Kiro's accounts on @magpie-community/opencode-kiro-auth:
|
|
// - kiro-cli's or the IDE's own sign-in goes as a marker ({source:
|
|
// "kiro"}): the plugin reads it where they keep it, as the built-in did;
|
|
// - an account magpie signed in (a home holding kiro-auth-token.json)
|
|
// goes whole, its tokens the plugin's to refresh from then on; the
|
|
// home stays, and moving back writes the plugin's tokens into it;
|
|
// - a key saved on the provider goes as an API key account, first (with
|
|
// a key, the built-in used nothing else), and comes off the provider
|
|
// while the plugin has it.
|
|
func init() {
|
|
movers["kiro"] = &mover{
|
|
pkg: "@magpie-community/opencode-kiro-auth",
|
|
min: "0.1.24", // 0.1.24: a conversation past the model's window is a 400 "input is too long for the model's context" however Kiro says it (an exception in its event stream was a 502, a reply stopped with MODEL_CONTEXT_WINDOW_EXCEEDED was finished), so magpie asks a member with room or tells the agent to compact (ergou0954_54426 on Discord); 0.1.23: thinking is asked for only when the request's thinking is enabled or adaptive; an effort with none, or with thinking disabled (Claude Code's title requests, a turn with thinking off), is sent no budget and no adaptive fields at any level (plugins#538, zengxiaosong); 0.1.22: a model whose Kiro catalog lists effort levels with adaptive thinking is sent the level as Kiro IDE sends it (additionalModelRequestFields), so xhigh and max stay apart, and a system message ending the conversation (Claude Code's environment context) goes as a turn before the question, not into it (plugins#538, zengxiaosong); 0.1.21: the browser sign-in asks first for AWS Builder ID, IAM Identity Center (Start URL and region) or Kiro's page; the AWS ways open AWS directly, so a magpie on a server or in Docker takes one pasted callback address instead of two, and a sign-in left behind frees its port when the next starts (plugins#507, SemonCat); 0.1.20: a tool whose input_schema has no root type reaches Kiro as an object, and a client's web_search server tool runs as Kiro's own search, at most 10 a request (plugins#492, SemonCat); 0.1.19: a usage read whose sign-in Kiro refused marks the account signed out, so its rows offer Sign in again, and a clean read takes the mark off (blue12345655 on Discord); 0.1.18: an image over Kiro's 5 MiB Base64 limit (a 4.2 MB view_image PNG is 5.6 MB as Base64) is shrunk on the request, or replaced by a note, so the conversation isn't stuck on IMAGE_SIZE_EXCEEDED (plugins#428); 0.1.17: the Free trial and Credits windows carry their credits used and the limit, so the card's Left shows the credits left (plugins#408); a reply with no token counts says its cache is unknown (magpie_cache_unknown), not 0: Kiro caches but counts no tokens (zeewell on X); 0.1.15: an API key is used as kiro-cli uses it: GetProfile in us-east-1 then eu-central-1, and with no profile found the key goes on in us-east-1; its models are the control plane's ListAvailableModels and no profile is named in its calls (Hollowhere: Auto alone, every request "Kiro didn't take the API key: 400"); 0.1.14: each account is named by its email and the way it was signed in to, "email (Google)", "email (GitHub)", not "email (slot)" (plugins#309); 0.1.13: two sign-ins of one email through different login providers (Google, GitHub, Builder ID) stay two accounts (plugins#309); 0.1.12: a request for JSON output (output_config.format) answers in JSON, its schema in the system prompt as the built-in; 0.1.11: a thinking model offers xhigh as its own level (50k), so Claude Code's xhigh isn't routed as max (plugins#120); 0.1.10: the caller's prompt-cache marks reach Kiro as cachePoints (plugins#110)
|
|
agents: []string{"kiro"},
|
|
out: func() ([]Moving, error) {
|
|
var out []Moving
|
|
key := kiroKey()
|
|
if key != "" {
|
|
// named as the built-in named it, which per-account proxies
|
|
// and headers are keyed on
|
|
out = append(out, Moving{User: "Kiro API key", First: true, On: true, Auth: map[string]any{"type": "api", "key": key, "accountId": "Kiro API key"}})
|
|
}
|
|
for _, l := range kiroLogins() {
|
|
// with a key, the built-in used nothing else: the accounts
|
|
// go along off, the key alone in use
|
|
m := Moving{User: l.User, First: l.Active && key == "", On: l.On && key == "", Lapsed: l.Lapsed != "", Plan: l.Plan}
|
|
if l.Home == "" {
|
|
m.Own = true
|
|
m.Auth = map[string]any{"type": "oauth", "source": "kiro", "access": "", "refresh": "", "expires": 0, "accountId": l.User}
|
|
} else {
|
|
a, err := kiroOut(l.Home, l.User, l.Plan)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m.Auth = a
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
return out, nil
|
|
},
|
|
back: func(ls []savedLogin, user string, auth map[string]any) ([]savedLogin, string, error) {
|
|
switch {
|
|
case str(auth["type"]) == "api":
|
|
// the key goes back onto the provider (give, or settle for
|
|
// one saved in the plugin since)
|
|
return ls, "", nil
|
|
case str(auth["source"]) != "":
|
|
// kiro-cli's or the IDE's: nothing of it was the plugin's
|
|
return ls, firstNonEmpty(user, str(auth["accountId"])), nil
|
|
}
|
|
if str(auth["access"]) == "" {
|
|
return ls, "", errors.New("Kiro: an unreadable plugin sign-in")
|
|
}
|
|
if user == "" {
|
|
user = str(auth["accountId"])
|
|
}
|
|
i := backInto(&ls, "kiro", user)
|
|
if ls[i].Home == "" {
|
|
home, err := newKiroHome()
|
|
if err != nil {
|
|
return ls, "", err
|
|
}
|
|
ls[i].Home = home
|
|
}
|
|
s := kiroSaved{AccessToken: str(auth["access"]), RefreshToken: str(auth["refresh"]), AuthMethod: "social",
|
|
Provider: str(auth["loginProvider"]), Region: str(auth["region"]), ProfileArn: str(auth["profileArn"]),
|
|
ClientID: str(auth["clientId"]), ClientSecret: str(auth["clientSecret"])}
|
|
if str(auth["method"]) == "idc" {
|
|
s.AuthMethod = "IdC"
|
|
}
|
|
if ms, _ := auth["expires"].(float64); ms > 0 {
|
|
s.ExpiresAt = time.UnixMilli(int64(ms)).UTC().Format(time.RFC3339Nano)
|
|
}
|
|
b, err := json.MarshalIndent(s, "", " ")
|
|
if err != nil {
|
|
return ls, "", err
|
|
}
|
|
if err := writePrivate(filepath.Join(ls[i].Home, kiroTokenFile), b); err != nil {
|
|
return ls, "", err
|
|
}
|
|
if p := str(auth["plan"]); p != "" {
|
|
ls[i].Plan = p
|
|
}
|
|
ls[i].Lapsed, ls[i].Seen = "", time.Now().UTC().Truncate(time.Second)
|
|
kiroForgetHome(ls[i].Home)
|
|
return ls, ls[i].User, nil
|
|
},
|
|
take: func() (json.RawMessage, error) {
|
|
k := kiroKey()
|
|
if k == "" {
|
|
return nil, nil
|
|
}
|
|
return json.RawMessage(jsonText(k)), setKiroKey("")
|
|
},
|
|
settle: func(auths map[string]map[string]any) error {
|
|
for _, a := range auths {
|
|
if k := str(a["key"]); str(a["type"]) == "api" && k != "" && kiroKey() == "" {
|
|
return setKiroKey(k)
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
give: func(kept json.RawMessage) error {
|
|
var k string
|
|
if json.Unmarshal(kept, &k) != nil || k == "" || kiroKey() != "" {
|
|
return nil
|
|
}
|
|
return setKiroKey(k)
|
|
},
|
|
}
|
|
}
|
|
|
|
// kiroOut is the account magpie signed in in home, as the plugin keeps one.
|
|
func kiroOut(home, user, plan string) (map[string]any, error) {
|
|
b, err := os.ReadFile(filepath.Join(home, kiroTokenFile))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var t kiroSaved
|
|
if json.Unmarshal(b, &t) != nil || t.AccessToken == "" {
|
|
return nil, errors.New("Kiro: " + user + "'s sign-in can't be read")
|
|
}
|
|
a := map[string]any{"type": "oauth", "access": t.AccessToken, "refresh": t.RefreshToken, "expires": 0,
|
|
"method": "social", "loginProvider": t.Provider, "region": firstNonEmpty(t.Region, "us-east-1"),
|
|
"profileArn": t.ProfileArn, "accountId": user}
|
|
if e, err := time.Parse(time.RFC3339Nano, t.ExpiresAt); err == nil {
|
|
a["expires"] = e.UnixMilli()
|
|
}
|
|
// as readKiroFile tells them apart
|
|
if strings.EqualFold(t.AuthMethod, "IdC") && t.ClientID != "" {
|
|
a["method"] = "idc"
|
|
}
|
|
if t.ClientID != "" {
|
|
a["clientId"], a["clientSecret"] = t.ClientID, t.ClientSecret
|
|
}
|
|
if plan != "" {
|
|
a["plan"] = plan
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// setKiroKey sets (or, "", clears) the key saved on the Kiro provider.
|
|
func setKiroKey(k string) error {
|
|
f, err := read()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := range f.Providers {
|
|
if f.Providers[i].ID == "kiro" {
|
|
if f.Providers[i].Key == k {
|
|
return nil
|
|
}
|
|
f.Providers[i].Key = k
|
|
return store(f)
|
|
}
|
|
}
|
|
if k == "" {
|
|
return nil
|
|
}
|
|
f.Providers = append(f.Providers, Provider{ID: "kiro", Key: k})
|
|
return store(f)
|
|
}
|
|
|
|
// kiroForgetHome drops what was cached of home's sign-in, rewritten.
|
|
func kiroForgetHome(home string) {
|
|
kiroAuthCache.Lock()
|
|
delete(kiroAuthCache.m, "\x00"+home)
|
|
kiroAuthCache.Unlock()
|
|
}
|