| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "reflect" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/BurntSushi/toml" |
| 11 | fileencoding "reasonix/internal/fileutil/encoding" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | const openCodeGoInferenceUpgradeVersion = 13 |
| 16 | |
| 17 | // The startup edit lock covers the endpoint/model additions and their marker. |
| 18 | // Regular loads never reapply this upgrade or resurrect a deleted model. |
| 19 | func upgradeOpenCodeGoInferenceFileLocked(path string, write func(string, []byte, os.FileMode) error) (bool, error) { |
| 20 | resolved, exists, err := statConfigPath(path) |
| 21 | if err != nil || !exists { |
| 22 | return false, err |
| 23 | } |
| 24 | raw, err := os.ReadFile(resolved) |
| 25 | if err != nil { |
| 26 | return false, err |
| 27 | } |
| 28 | info, err := os.Stat(resolved) |
| 29 | if err != nil { |
| 30 | return false, err |
| 31 | } |
| 32 | encoding, data := fileencoding.Detect(raw) |
| 33 | body := string(fileencoding.Decode(data, encoding)) |
| 34 | crlf := strings.Contains(body, "\r\n") |
| 35 | if crlf { |
| 36 | body = strings.ReplaceAll(body, "\r\n", "\n") |
| 37 | } |
| 38 | next, changed, err := rewriteOpenCodeGoInferenceUpgrade(body) |
| 39 | if err != nil || !changed { |
| 40 | return false, err |
| 41 | } |
| 42 | if crlf { |
| 43 | next = strings.ReplaceAll(next, "\n", "\r\n") |
| 44 | } |
| 45 | encoded, err := fileencoding.Encode(next, encoding) |
| 46 | if err != nil { |
| 47 | return false, err |
| 48 | } |
| 49 | if err := write(resolved, encoded, info.Mode().Perm()); err != nil { |
| 50 | return false, fmt.Errorf("commit OpenCode Go inference upgrade: %w", err) |
| 51 | } |
| 52 | return true, nil |
| 53 | } |
| 54 | |
| 55 | func rewriteOpenCodeGoInferenceUpgrade(body string) (string, bool, error) { |
| 56 | var before, after Config |
| 57 | if _, err := toml.Decode(body, &before); err != nil { |
| 58 | return body, false, err |
| 59 | } |
| 60 | if before.ConfigVersion >= openCodeGoInferenceUpgradeVersion { |
| 61 | return body, false, nil |
| 62 | } |
| 63 | if _, err := toml.Decode(body, &after); err != nil { |
| 64 | return body, false, err |
| 65 | } |
| 66 | var expected map[string]any |
| 67 | if _, err := toml.Decode(body, &expected); err != nil { |
| 68 | return body, false, err |
| 69 | } |
| 70 | expected["config_version"] = int64(openCodeGoInferenceUpgradeVersion) |
| 71 | tables, _ := deepSeekCatalogDocumentValue(expected["providers"]).([]any) |
| 72 | for i := range after.Providers { |
| 73 | p := &after.Providers[i] |
| 74 | route, _ := openCodeGoMigrationRoute(*p) |
| 75 | if route == "" { |
| 76 | continue |
| 77 | } |
| 78 | base := provider.OpenCodeGoChatBaseURL |
| 79 | if route == provider.OpenCodeGoRouteAnthropic { |
| 80 | base = provider.OpenCodeGoAnthropicBaseURL |
| 81 | } |
| 82 | p.BaseURL = base |
| 83 | if p.RequestURL != "" { |
| 84 | p.RequestURL = ProviderRequestURL(p.Kind, base) |
| 85 | } |
| 86 | if p.ChatURL != "" { |
| 87 | p.ChatURL = provider.OpenCodeGoChatBaseURL + "/chat/completions" |
| 88 | } |
| 89 | if p.ModelsURL != "" { |
| 90 | if canonical, ok := provider.CanonicalOpenCodeGoModelsURL(p.ModelsURL); ok { |
| 91 | p.ModelsURL = canonical |
| 92 | } |
| 93 | } |
| 94 | // The new SKU is recommended on Chat Completions. Keep existing model |
| 95 | // selections and search-capable alternate routes intact. |
| 96 | if route == provider.OpenCodeGoRouteChat && !p.HasModel("deepseek-v4.1-flash") && |
| 97 | slices.ContainsFunc(p.ModelList(), provider.OpenCodeGoDeepSeekModel) { |
| 98 | p.Models = append(append([]string(nil), p.ModelList()...), "deepseek-v4.1-flash") |
| 99 | if p.Price != nil && p.Prices["deepseek-v4.1-flash"] == nil { |
| 100 | if p.Prices == nil { |
| 101 | p.Prices = map[string]*provider.Pricing{} |
| 102 | } |
| 103 | // Go's documented USD peak list rate; never inherit a |
| 104 | // singular custom/Pro price for the newly added Flash SKU. |
| 105 | p.Prices["deepseek-v4.1-flash"] = deepSeekV4FlashPriceUSD() |
| 106 | } |
| 107 | } |
| 108 | fields := tables[i].(map[string]any) |
| 109 | old := before.Providers[i] |
| 110 | for _, pair := range []struct { |
| 111 | key string |
| 112 | a, b any |
| 113 | }{ |
| 114 | {"base_url", old.BaseURL, p.BaseURL}, {"request_url", old.RequestURL, p.RequestURL}, |
| 115 | {"chat_url", old.ChatURL, p.ChatURL}, {"models_url", old.ModelsURL, p.ModelsURL}, {"models", old.Models, p.Models}, |
| 116 | } { |
| 117 | if !reflect.DeepEqual(pair.a, pair.b) { |
| 118 | encoded, err := rawTOMLValue(pair.b) |
| 119 | if err != nil { |
| 120 | return body, false, err |
| 121 | } |
| 122 | var value map[string]any |
| 123 | if _, err := toml.Decode("v = "+encoded, &value); err != nil { |
| 124 | return body, false, err |
| 125 | } |
| 126 | fields[pair.key] = value["v"] |
| 127 | } |
| 128 | } |
| 129 | if !reflect.DeepEqual(old.Prices, p.Prices) { |
| 130 | prices, _ := fields["prices"].(map[string]any) |
| 131 | if prices == nil { |
| 132 | prices = map[string]any{} |
| 133 | fields["prices"] = prices |
| 134 | } |
| 135 | price := p.Prices["deepseek-v4.1-flash"] |
| 136 | prices["deepseek-v4.1-flash"] = map[string]any{"cache_hit": price.CacheHit, "input": price.Input, "output": price.Output, "currency": price.Currency} |
| 137 | } |
| 138 | } |
| 139 | next, err := rewriteOpenCodeGoConfig(body, &before, &after, nil) |
| 140 | if err != nil { |
| 141 | return body, false, err |
| 142 | } |
| 143 | next, err = rawTOMLSet(next, []string{"config_version"}, openCodeGoInferenceUpgradeVersion) |
| 144 | if err != nil { |
| 145 | return body, false, err |
| 146 | } |
| 147 | var actual map[string]any |
| 148 | if _, err := toml.Decode(next, &actual); err != nil { |
| 149 | return body, false, err |
| 150 | } |
| 151 | if !reflect.DeepEqual(deepSeekCatalogDocumentValue(expected), deepSeekCatalogDocumentValue(actual)) { |
| 152 | return body, false, fmt.Errorf("OpenCode Go inference upgrade changed unplanned fields; original configuration retained") |
| 153 | } |
| 154 | return next, true, nil |
| 155 | } |
| 156 |