Fix: Support token extraction from Metadata for file-based Kiro auth

- Modified extractKiroTokenData to support both Attributes and Metadata sources
- Fixes issue where JSON file-based tokens were not being read correctly
- FileSynthesizer stores tokens in Metadata, ConfigSynthesizer uses Attributes
- Now checks Attributes first (config.yaml), falls back to Metadata (JSON files)
- Ensures dynamic model fetching works for all Kiro authentication methods
- Prevents fallback to static model list that incorrectly includes opus for free accounts
This commit is contained in:
ricky
2026-01-31 00:15:35 +08:00
parent b3b8d71dfc
commit 101498e737
3 changed files with 29 additions and 214 deletions

View File

@@ -1416,29 +1416,44 @@ func (s *Service) fetchKiroModels(a *coreauth.Auth) []*ModelInfo {
}
// extractKiroTokenData extracts KiroTokenData from auth attributes and metadata.
// It supports both config-based tokens (stored in Attributes) and file-based tokens (stored in Metadata).
func (s *Service) extractKiroTokenData(a *coreauth.Auth) *kiroauth.KiroTokenData {
if a == nil || a.Attributes == nil {
if a == nil {
return nil
}
accessToken := strings.TrimSpace(a.Attributes["access_token"])
var accessToken, profileArn, refreshToken string
// Priority 1: Try to get from Attributes (config.yaml source)
if a.Attributes != nil {
accessToken = strings.TrimSpace(a.Attributes["access_token"])
profileArn = strings.TrimSpace(a.Attributes["profile_arn"])
refreshToken = strings.TrimSpace(a.Attributes["refresh_token"])
}
// Priority 2: If not found in Attributes, try Metadata (JSON file source)
if accessToken == "" && a.Metadata != nil {
if at, ok := a.Metadata["access_token"].(string); ok {
accessToken = strings.TrimSpace(at)
}
if pa, ok := a.Metadata["profile_arn"].(string); ok {
profileArn = strings.TrimSpace(pa)
}
if rt, ok := a.Metadata["refresh_token"].(string); ok {
refreshToken = strings.TrimSpace(rt)
}
}
// access_token is required
if accessToken == "" {
return nil
}
tokenData := &kiroauth.KiroTokenData{
AccessToken: accessToken,
ProfileArn: strings.TrimSpace(a.Attributes["profile_arn"]),
return &kiroauth.KiroTokenData{
AccessToken: accessToken,
ProfileArn: profileArn,
RefreshToken: refreshToken,
}
// Also try to get refresh token from metadata
if a.Metadata != nil {
if rt, ok := a.Metadata["refresh_token"].(string); ok {
tokenData.RefreshToken = rt
}
}
return tokenData
}
// convertKiroAPIModels converts Kiro API models to ModelInfo slice.