|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + hawkconfig "github.com/GrayCodeAI/hawk/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +func gatewayRegionOptionIndex(providerID, region string) int { |
| 14 | + region = strings.ToLower(strings.TrimSpace(region)) |
| 15 | + if region == "" { |
| 16 | + return 0 |
| 17 | + } |
| 18 | + opts := hawkconfig.GatewayRegionOptions(providerID) |
| 19 | + for i, r := range opts { |
| 20 | + if strings.EqualFold(r.Value, region) { |
| 21 | + return i |
| 22 | + } |
| 23 | + } |
| 24 | + return 0 |
| 25 | +} |
| 26 | + |
| 27 | +func (m chatModel) closeConfigEntry() chatModel { |
| 28 | + m.configEntry = configEntryNone |
| 29 | + m.configProvider = "" |
| 30 | + return m |
| 31 | +} |
| 32 | + |
| 33 | +func (m chatModel) startConfigGatewayRegion(providerID string) chatModel { |
| 34 | + switch providerID { |
| 35 | + case hawkconfig.ProviderXiaomiTokenPlan: |
| 36 | + m.configEntry = configEntryXiaomiRegion |
| 37 | + case hawkconfig.ProviderZAICoding, hawkconfig.ProviderZAIPayg: |
| 38 | + m.configEntry = configEntryZAIRegion |
| 39 | + default: |
| 40 | + m.configEntry = configEntryGatewayRegion |
| 41 | + } |
| 42 | + m.configProvider = providerID |
| 43 | + idx := 0 |
| 44 | + if !hawkconfig.NeedsGatewayRegion(providerID) { |
| 45 | + idx = gatewayRegionOptionIndex(providerID, hawkconfig.GatewayRegionLabel(providerID)) |
| 46 | + } |
| 47 | + m.configGatewayRegionSel = idx |
| 48 | + m.configZAIRegionSel = idx |
| 49 | + name := hawkconfig.GatewayDisplayName(providerID) |
| 50 | + notice := fmt.Sprintf("Select %s region (↑↓ · enter · esc cancel)", name) |
| 51 | + if saved := hawkconfig.GatewayRegionLabel(providerID); saved != "" { |
| 52 | + notice = fmt.Sprintf("%s region · current %s (↑↓ · enter · esc cancel)", name, saved) |
| 53 | + } |
| 54 | + m.configNotice = notice |
| 55 | + return m |
| 56 | +} |
| 57 | + |
| 58 | +func (m chatModel) configGatewayRegionView() string { |
| 59 | + mutedStyle := configMutedStyle() |
| 60 | + accentStyle := configAccentStyle() |
| 61 | + rowStyle := configRowStyle() |
| 62 | + var b strings.Builder |
| 63 | + prov := m.configProvider |
| 64 | + name := hawkconfig.GatewayDisplayName(prov) |
| 65 | + b.WriteString(renderConfigBreadcrumb(name+" region") + "\n\n") |
| 66 | + opts := hawkconfig.GatewayRegionOptions(prov) |
| 67 | + for i, r := range opts { |
| 68 | + prefix := " " |
| 69 | + if i == m.configGatewayRegionSel { |
| 70 | + prefix = "> " |
| 71 | + } |
| 72 | + label := r.DisplayName |
| 73 | + if label == "" { |
| 74 | + label = r.Value |
| 75 | + } |
| 76 | + if i == m.configGatewayRegionSel { |
| 77 | + b.WriteString(accentStyle.Render(prefix+label) + "\n") |
| 78 | + } else { |
| 79 | + b.WriteString(rowStyle.Render(prefix+label) + "\n") |
| 80 | + } |
| 81 | + } |
| 82 | + b.WriteString("\n" + mutedStyle.Render(" Press enter to apply region.")) |
| 83 | + return m.configTabShellView(b.String()) |
| 84 | +} |
| 85 | + |
| 86 | +func (m chatModel) handleConfigGatewayRegionKey(msg tea.KeyMsg) (chatModel, tea.Cmd) { |
| 87 | + opts := hawkconfig.GatewayRegionOptions(m.configProvider) |
| 88 | + count := len(opts) |
| 89 | + if count == 0 { |
| 90 | + return m.closeConfigEntry(), nil |
| 91 | + } |
| 92 | + switch msg.String() { |
| 93 | + case "up", "k": |
| 94 | + if m.configGatewayRegionSel > 0 { |
| 95 | + m.configGatewayRegionSel-- |
| 96 | + m.configZAIRegionSel = m.configGatewayRegionSel |
| 97 | + } |
| 98 | + return m, nil |
| 99 | + case "down", "j": |
| 100 | + if m.configGatewayRegionSel < count-1 { |
| 101 | + m.configGatewayRegionSel++ |
| 102 | + m.configZAIRegionSel = m.configGatewayRegionSel |
| 103 | + } |
| 104 | + return m, nil |
| 105 | + case "enter": |
| 106 | + if m.configGatewayRegionSel >= 0 && m.configGatewayRegionSel < count { |
| 107 | + chosen := opts[m.configGatewayRegionSel] |
| 108 | + if err := hawkconfig.SetGatewayRegion(m.configProvider, chosen.Value); err != nil { |
| 109 | + m.configNotice = "Error saving region: " + err.Error() |
| 110 | + return m, nil |
| 111 | + } |
| 112 | + InvalidateModelCacheProvider(m.configProvider) |
| 113 | + m.configGatewayRowsDirty = true |
| 114 | + ctx := context.Background() |
| 115 | + if post := strings.TrimSpace(m.configPostSaveKeysProvider); post == m.configProvider { |
| 116 | + m.configPostSaveKeysProvider = "" |
| 117 | + return m.startConfigKeyReplace(m.configProvider) |
| 118 | + } |
| 119 | + if hawkconfig.HasStoredCredentialForProvider(ctx, m.configProvider) { |
| 120 | + m.configNotice = "Saved region for " + hawkconfig.GatewayDisplayName(m.configProvider) |
| 121 | + if idx := m.configGatewayRowIndex(m.configProvider); idx >= 0 { |
| 122 | + m.configSel = idx |
| 123 | + } |
| 124 | + return m.closeConfigEntry(), nil |
| 125 | + } |
| 126 | + return m.startConfigKeyForProvider(m.configProvider) |
| 127 | + } |
| 128 | + return m.closeConfigEntry(), nil |
| 129 | + case "esc": |
| 130 | + m.configPostSaveKeysProvider = "" |
| 131 | + return m.closeConfigEntry(), nil |
| 132 | + default: |
| 133 | + return m, nil |
| 134 | + } |
| 135 | +} |
0 commit comments