|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + tea "github.com/charmbracelet/bubbletea" |
| 8 | + |
| 9 | + hawkconfig "github.com/GrayCodeAI/hawk/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +var zaiRegions = []struct { |
| 13 | + id string |
| 14 | + label string |
| 15 | +}{ |
| 16 | + {id: "international", label: "International (api.z.ai)"}, |
| 17 | + {id: "cn", label: "China (open.bigmodel.cn)"}, |
| 18 | +} |
| 19 | + |
| 20 | +func zaiRegionIndex(region string) int { |
| 21 | + region = strings.ToLower(strings.TrimSpace(region)) |
| 22 | + if region == "" { |
| 23 | + return 0 |
| 24 | + } |
| 25 | + for i, r := range zaiRegions { |
| 26 | + if r.id == region { |
| 27 | + return i |
| 28 | + } |
| 29 | + } |
| 30 | + return 0 |
| 31 | +} |
| 32 | + |
| 33 | +func (m chatModel) startConfigZAIRegion(providerID string) chatModel { |
| 34 | + m.configEntry = configEntryZAIRegion |
| 35 | + m.configProvider = providerID |
| 36 | + if hawkconfig.NeedsZAIRegion(providerID) { |
| 37 | + m.configZAIRegionSel = 0 |
| 38 | + } else { |
| 39 | + m.configZAIRegionSel = zaiRegionIndex(hawkconfig.ZAIRegionLabel(providerID)) |
| 40 | + } |
| 41 | + name := hawkconfig.GatewayDisplayName(providerID) |
| 42 | + notice := "Select " + name + " region (↑↓ · enter · esc cancel)" |
| 43 | + if saved := hawkconfig.ZAIRegionLabel(providerID); saved != "" { |
| 44 | + notice = name + " region · current " + saved + " (↑↓ · enter · esc cancel)" |
| 45 | + } |
| 46 | + m.configNotice = notice |
| 47 | + return m |
| 48 | +} |
| 49 | + |
| 50 | +func (m chatModel) configZAIRegionView() string { |
| 51 | + mutedStyle := configMutedStyle() |
| 52 | + accentStyle := configAccentStyle() |
| 53 | + rowStyle := configRowStyle() |
| 54 | + var b strings.Builder |
| 55 | + prov := m.configProvider |
| 56 | + name := hawkconfig.GatewayDisplayName(prov) |
| 57 | + b.WriteString(renderConfigBreadcrumb(name+" region") + "\n\n") |
| 58 | + for i, r := range zaiRegions { |
| 59 | + prefix := " " |
| 60 | + if i == m.configZAIRegionSel { |
| 61 | + prefix = "> " |
| 62 | + } |
| 63 | + line := prefix + r.label |
| 64 | + if i == m.configZAIRegionSel { |
| 65 | + b.WriteString(accentStyle.Render(line) + "\n") |
| 66 | + } else { |
| 67 | + b.WriteString(rowStyle.Render(line) + "\n") |
| 68 | + } |
| 69 | + } |
| 70 | + b.WriteString("\n" + mutedStyle.Render(" Coding Plan uses dedicated /coding/paas/v4 on the chosen region")) |
| 71 | + return m.configTabShellView(b.String()) |
| 72 | +} |
| 73 | + |
| 74 | +func (m chatModel) handleConfigZAIRegionKey(msg tea.KeyMsg) (chatModel, tea.Cmd) { |
| 75 | + switch msg.Type { |
| 76 | + case tea.KeyEsc: |
| 77 | + prov := m.configProvider |
| 78 | + m.configEntry = configEntryNone |
| 79 | + m.configProvider = "" |
| 80 | + if idx := m.configGatewayRowIndex(prov); idx >= 0 { |
| 81 | + m.configSel = idx |
| 82 | + } |
| 83 | + m.configNotice = "" |
| 84 | + return m, nil |
| 85 | + case tea.KeyUp: |
| 86 | + if m.configZAIRegionSel > 0 { |
| 87 | + m.configZAIRegionSel-- |
| 88 | + } |
| 89 | + return m, nil |
| 90 | + case tea.KeyDown: |
| 91 | + if m.configZAIRegionSel < len(zaiRegions)-1 { |
| 92 | + m.configZAIRegionSel++ |
| 93 | + } |
| 94 | + return m, nil |
| 95 | + case tea.KeyEnter: |
| 96 | + if m.configZAIRegionSel < 0 || m.configZAIRegionSel >= len(zaiRegions) { |
| 97 | + return m, nil |
| 98 | + } |
| 99 | + region := zaiRegions[m.configZAIRegionSel].id |
| 100 | + prov := m.configProvider |
| 101 | + if err := hawkconfig.SetZAIRegion(prov, region); err != nil { |
| 102 | + m.configNotice = "Region: " + err.Error() |
| 103 | + return m, nil |
| 104 | + } |
| 105 | + InvalidateModelCacheProvider(prov) |
| 106 | + m.configEntry = configEntryNone |
| 107 | + ctx := context.Background() |
| 108 | + if post := strings.TrimSpace(m.configPostSaveKeysProvider); post == prov { |
| 109 | + m.configPostSaveKeysProvider = "" |
| 110 | + return m.startConfigKeyReplace(post) |
| 111 | + } |
| 112 | + if hawkconfig.HasStoredCredentialForProvider(ctx, prov) { |
| 113 | + m.configNotice = "Region saved (" + region + ") — press r to refresh models" |
| 114 | + if idx := m.configGatewayRowIndex(prov); idx >= 0 { |
| 115 | + m.configSel = idx |
| 116 | + } |
| 117 | + return m, nil |
| 118 | + } |
| 119 | + m.configNotice = "Region saved (" + region + ") — paste Z.AI API key" |
| 120 | + return m.startConfigKeyForProvider(prov) |
| 121 | + default: |
| 122 | + return m, nil |
| 123 | + } |
| 124 | +} |
0 commit comments