| 1 | package tui |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | |
| 6 | "sourcecraft.dev/bigbes/claudio/cache" |
| 7 | "sourcecraft.dev/bigbes/claudio/provider/copilot" |
| 8 | ) |
| 9 | |
| 10 | type copilotFetcher struct { |
| 11 | oauthToken string |
| 12 | } |
| 13 | |
| 14 | func (f copilotFetcher) ProviderKey() string { return "copilot" } |
| 15 | |
| 16 | func (f copilotFetcher) FetchAndCategorize(cdb *cache.DB) (CategorizedResult, error) { |
| 17 | var models []copilot.CopilotModel |
| 18 | |
| 19 | // Try cache. |
| 20 | if cdb != nil { |
| 21 | if data, ok := cdb.GetModels(f.ProviderKey()); ok { |
| 22 | if json.Unmarshal(data, &models) != nil { |
| 23 | models = nil |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Fetch from API on cache miss. |
| 29 | if models == nil { |
| 30 | var err error |
| 31 | models, err = copilot.ListModels(f.oauthToken) |
| 32 | if err != nil { |
| 33 | return CategorizedResult{}, err |
| 34 | } |
| 35 | saveCacheModels(cdb, f.ProviderKey(), models) |
| 36 | } |
| 37 | |
| 38 | families, grouped := copilot.VendorFamilies(models) |
| 39 | items := make(map[string][]PickerItem) |
| 40 | for fam, cms := range grouped { |
| 41 | for _, cm := range cms { |
| 42 | items[fam] = append(items[fam], PickerItem{ID: cm.ID, Name: cm.DisplayName(), ContextLength: cm.ContextWindow}) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return CategorizedResult{Families: families, Items: items}, nil |
| 47 | } |
| 48 | |