返回 DeepSeek-Reasonix
pricing.go
根目录 / internal / config / pricing.go
1 package config
2
3 import (
4 "fmt"
5 "strings"
6
7 "reasonix/internal/fileutil"
8 "reasonix/internal/provider"
9 "reasonix/internal/provider/openai"
10 )
11
12 func deepSeekV4FlashPriceCNY() *provider.Pricing {
13 return &provider.Pricing{CacheHit: 0.04, Input: 2, Output: 8, Currency: "¥"}
14 }
15
16 func deepSeekV4ProPriceCNY() *provider.Pricing {
17 return &provider.Pricing{CacheHit: 0.30, Input: 9, Output: 27, Currency: "¥"}
18 }
19
20 // deepSeekV4FlashModelIDs are the ids the vendor serves at the Flash price: the
21 // V4.1 name, and the retired V4 ids it still routes there.
22 func deepSeekV4FlashModelIDs() []string {
23 return []string{"deepseek-flash", "deepseek-v4-flash", openai.OfficialDeepSeekVisionModel}
24 }
25
26 func deepSeekV4PricesCNY() map[string]*provider.Pricing {
27 prices := map[string]*provider.Pricing{"deepseek-v4-pro": deepSeekV4ProPriceCNY()}
28 for _, model := range deepSeekV4FlashModelIDs() {
29 prices[model] = deepSeekV4FlashPriceCNY()
30 }
31 return prices
32 }
33
34 func deepSeekV4FlashPriceUSD() *provider.Pricing {
35 return &provider.Pricing{CacheHit: 0.006, Input: 0.3, Output: 1.2, Currency: "$"}
36 }
37
38 func deepSeekV4ProPriceUSD() *provider.Pricing {
39 return &provider.Pricing{CacheHit: 0.044, Input: 1.32, Output: 3.96, Currency: "$"}
40 }
41
42 func deepSeekV4PricesUSD() map[string]*provider.Pricing {
43 prices := map[string]*provider.Pricing{"deepseek-v4-pro": deepSeekV4ProPriceUSD()}
44 for _, model := range deepSeekV4FlashModelIDs() {
45 prices[model] = deepSeekV4FlashPriceUSD()
46 }
47 return prices
48 }
49
50 // DeepSeekV4PricesForCurrency returns the official regional price table.
51 // Persisted custom prices still win; this is only used for built-in templates
52 // and known-default refreshes.
53 func DeepSeekV4PricesForCurrency(currency string) map[string]*provider.Pricing {
54 if normalizeDeepSeekPricingCurrency(currency) == "CNY" {
55 return deepSeekV4PricesCNY()
56 }
57 return deepSeekV4PricesUSD()
58 }
59
60 // DeepSeekV4PricesForLanguage is retained for compatibility with older call
61 // sites. New desktop code should pass an explicit pricing currency.
62 func DeepSeekV4PricesForLanguage(lang string) map[string]*provider.Pricing {
63 if normalizeDeepSeekPricingLanguage(lang) == "zh" {
64 return DeepSeekV4PricesForCurrency("CNY")
65 }
66 return DeepSeekV4PricesForCurrency("USD")
67 }
68
69 func deepSeekV4PricesForConfig(c *Config) map[string]*provider.Pricing {
70 return DeepSeekV4PricesForCurrency(c.DeepSeekOfficialPricingCurrency())
71 }
72
73 func deepSeekV4PriceForModel(currency, model string) *provider.Pricing {
74 return clonePricing(DeepSeekV4PricesForCurrency(currency)[strings.TrimSpace(model)])
75 }
76
77 // DeepSeekOfficialPricingLanguage is retained for older settings/template call
78 // sites that still express the pricing region as a language. List-price region
79 // is frozen per provider (billing_currency), not the global display currency.
80 func (c *Config) DeepSeekOfficialPricingLanguage() string {
81 if c.DeepSeekOfficialPricingCurrency() == "CNY" {
82 return "zh"
83 }
84 return "en"
85 }
86
87 func normalizeDeepSeekPricingCurrency(currency string) string {
88 switch strings.ToUpper(strings.TrimSpace(currency)) {
89 case "CNY", "RMB", "CNH", "¥", "¥":
90 return "CNY"
91 case "USD", "$", "US$":
92 return "USD"
93 default:
94 return ""
95 }
96 }
97
98 func normalizeDeepSeekPricingLanguage(lang string) string {
99 switch strings.ToLower(strings.TrimSpace(lang)) {
100 case "zh", "zh-cn", "zh-hans", "cn", "chinese", "中文", "zh-tw", "zh-hant", "zh-hk", "zh-mo":
101 return "zh"
102 case "en", "en-us", "en-gb", "english":
103 return "en"
104 default:
105 return ""
106 }
107 }
108
109 // ApplyDeepSeekOfficialDefaultPricing refreshes built-in/official DeepSeek
110 // prices that still match known official defaults for each provider's frozen
111 // billing_currency. Custom user prices and display-currency switches never
112 // rewrite list prices.
113 func (c *Config) ApplyDeepSeekOfficialDefaultPricing() {
114 applyDeepSeekOfficialDefaultPricing(c)
115 }
116
117 func applyDeepSeekOfficialDefaultPricing(c *Config) {
118 applyDeepSeekOfficialDefaultPricingWithOverride(c, false)
119 }
120
121 func applyDeepSeekOfficialDefaultPricingWithOverride(c *Config, overridePersisted bool) {
122 if c == nil {
123 return
124 }
125 for i := range c.Providers {
126 p := &c.Providers[i]
127 if officialProviderKind(p) != "deepseek" || !isOfficialDeepSeekBillingEndpoint(p) {
128 continue
129 }
130 currency := p.ProviderBillingCurrency()
131 if currency == "" {
132 currency = "USD"
133 }
134 // Only refresh when the row still matches a known official default in
135 // the provider's own billing currency. Display currency must not win.
136 if isKnownDeepSeekOfficialPricing(p.Model, p.Price) && (overridePersisted || p.persistedOfficialCurrency == "" || p.persistedOfficialCurrency == currency) {
137 if samePricing(p.Price, deepSeekV4PriceForModel(currency, p.Model)) || overridePersisted {
138 p.Price = deepSeekV4PriceForModel(currency, p.Model)
139 }
140 }
141 for model, price := range p.Prices {
142 if isKnownDeepSeekOfficialPricing(model, price) && (overridePersisted || p.persistedOfficialCurrency == "" || p.persistedOfficialCurrency == currency) {
143 if samePricing(price, deepSeekV4PriceForModel(currency, model)) || overridePersisted {
144 p.Prices[model] = deepSeekV4PriceForModel(currency, model)
145 }
146 }
147 }
148 if strings.TrimSpace(p.BillingCurrency) == "" {
149 p.BillingCurrency = currency
150 }
151 }
152 }
153
154 // markPersistedDeepSeekOfficialPricing records which recognized regional
155 // prices came from TOML. Auto locale refreshes preserve those values, while an
156 // explicit currency choice can still replace them with the selected table.
157 func markPersistedDeepSeekOfficialPricing(c *Config) {
158 if c == nil {
159 return
160 }
161 for i := range c.Providers {
162 p := &c.Providers[i]
163 if officialProviderKind(p) != "deepseek" || !isOfficialDeepSeekBillingEndpoint(p) {
164 continue
165 }
166 p.persistedOfficialCurrency = completeDeepSeekOfficialPricingCurrency(p)
167 if c.ConfigVersion >= Default().ConfigVersion && isStandardDeepSeekProviderTemplate(p) {
168 p.persistedOfficialCurrency = ""
169 }
170 }
171 }
172
173 func isStandardDeepSeekProviderTemplate(p *ProviderEntry) bool {
174 if p == nil || officialProviderKind(p) != "deepseek" {
175 return false
176 }
177 return strings.TrimSpace(p.APIKeyEnv) == "DEEPSEEK_API_KEY" &&
178 strings.TrimSpace(p.BalanceURL) == "https://api.deepseek.com/user/balance" &&
179 p.ContextWindow == 1_000_000
180 }
181
182 func completeDeepSeekOfficialPricingCurrency(p *ProviderEntry) string {
183 if p == nil {
184 return ""
185 }
186 models := p.ModelList()
187 if len(models) == 1 && isKnownDeepSeekOfficialPricing(models[0], p.Price) {
188 return normalizeDeepSeekPricingCurrency(p.Price.Currency)
189 }
190 if len(models) == 0 || p.Price != nil {
191 return ""
192 }
193 currency := ""
194 for _, model := range models {
195 price := p.Prices[strings.TrimSpace(model)]
196 if !isKnownDeepSeekOfficialPricing(model, price) {
197 return ""
198 }
199 nextCurrency := normalizeDeepSeekPricingCurrency(price.Currency)
200 if nextCurrency == "" {
201 return ""
202 }
203 if currency == "" {
204 currency = nextCurrency
205 } else if currency != nextCurrency {
206 return ""
207 }
208 }
209 return currency
210 }
211
212 func mimoV25ProPrice() *provider.Pricing {
213 return &provider.Pricing{CacheHit: 0.025, Input: 3, Output: 6, Currency: "¥"}
214 }
215
216 func mimoV25Price() *provider.Pricing {
217 return &provider.Pricing{CacheHit: 0.02, Input: 1, Output: 2, Currency: "¥"}
218 }
219
220 func mimoV2FlashPrice() *provider.Pricing {
221 return &provider.Pricing{CacheHit: 0.07, Input: 0.70, Output: 2.10, Currency: "¥"}
222 }
223
224 func mimoDomesticPrices(models []string) map[string]*provider.Pricing {
225 prices := map[string]*provider.Pricing{}
226 for _, model := range models {
227 switch strings.TrimSpace(model) {
228 case "mimo-v2.6-pro", "mimo-v2.5-pro", "mimo-v2-pro":
229 prices[model] = mimoV25ProPrice()
230 case "mimo-v2.6-flash", "mimo-v2.5", "mimo-v2-omni":
231 prices[model] = mimoV25Price()
232 case "mimo-v2-flash":
233 prices[model] = mimoV2FlashPrice()
234 }
235 }
236 return prices
237 }
238
239 func longCat20Price() *provider.Pricing {
240 return &provider.Pricing{CacheHit: 0.04, Input: 2, Output: 8, Currency: "¥"}
241 }
242
243 func longCat20Prices(models []string) map[string]*provider.Pricing {
244 prices := map[string]*provider.Pricing{}
245 for _, model := range models {
246 switch strings.TrimSpace(model) {
247 case "LongCat-2.0":
248 prices[model] = longCat20Price()
249 }
250 }
251 return prices
252 }
253
254 const (
255 deepSeekPricingResetConfigVersion = 3
256 windowsBashSandboxDefaultConfigVersion = 4
257 retiredAutoPlanConfigVersion = 5
258 billingSplitConfigVersion = 6
259 deepSeekScheduledPricingConfigVersion = 7
260 )
261
262 // ApplyUserConfigUpgradesOnStartup applies one-time startup migrations. It
263 // intentionally runs from the desktop and CLI startup paths, not every config
264 // Load(), so user edits made after the upgrade are preserved.
265 func ApplyUserConfigUpgradesOnStartup(path string) (bool, error) {
266 path = strings.TrimSpace(path)
267 if path == "" {
268 return false, nil
269 }
270 unlock, err := LockConfigFileEdits(path)
271 if err != nil {
272 return false, err
273 }
274 defer unlock()
275
276 changed, err := applyUserConfigUpgradesThroughV10Locked(path)
277 if err != nil {
278 return changed, err
279 }
280 upgraded, err := upgradeDeepSeekCatalogFileLocked(path, fileutil.AtomicWriteFile)
281 changed = changed || upgraded
282 if err != nil {
283 return changed, err
284 }
285 upgraded, err = upgradeMimoCatalogFileLocked(path, fileutil.AtomicWriteFile)
286 changed = changed || upgraded
287 if err != nil {
288 return changed, err
289 }
290 upgraded, err = upgradeOpenCodeGoInferenceFileLocked(path, fileutil.AtomicWriteFile)
291 return changed || upgraded, err
292 }
293
294 func applyUserConfigUpgradesThroughV10Locked(path string) (bool, error) {
295 _, exists, err := statConfigPath(path)
296 if err != nil {
297 return false, err
298 }
299 if !exists {
300 return false, nil
301 }
302 var header Config
303 if _, err := decodeTOMLFile(path, &header); err != nil {
304 return false, fmt.Errorf("config %s: %w", path, err)
305 }
306 defaultVersion := Default().ConfigVersion
307 if header.ConfigVersion > defaultVersion {
308 return false, nil
309 }
310 if header.ConfigVersion >= openCodeGoUpgradeVersion {
311 resolved, _, err := statConfigPath(path)
312 if err != nil {
313 return false, err
314 }
315 if err := finalizeOpenCodeGoJournal(resolved); err != nil {
316 return false, err
317 }
318 }
319 classicDesktopLayout := strings.EqualFold(strings.TrimSpace(header.Desktop.LayoutStyle), "classic")
320 if header.ConfigVersion == defaultVersion && !classicDesktopLayout {
321 return repairProviderEndpointContractsOnStartup(path, false)
322 }
323 // Versions 7-9 commit the protocol upgrades and final version together,
324 // preserving the original bytes in one backup before any replacement.
325 if header.ConfigVersion >= deepSeekScheduledPricingConfigVersion && header.ConfigVersion < openCodeGoUpgradeVersion {
326 return upgradeOpenCodeGoAndRepairProviderEndpoints(path)
327 }
328 cfg := LoadForEdit(path)
329 changed := false
330 if classicDesktopLayout {
331 cfg.Desktop.LayoutStyle = "workbench"
332 changed = true
333 }
334 if header.ConfigVersion < deepSeekPricingResetConfigVersion {
335 resetOfficialProviderPricingDefaults(cfg)
336 changed = true
337 }
338 if shouldMarkWindowsBashSandboxDefaultUpgrade(header.ConfigVersion) {
339 resetWindowsBashSandboxDefaultOnUpgrade(cfg)
340 // Mark the Windows v4 migration even when the user was already on off,
341 // so a later manual enforce choice is not treated as the old template default.
342 changed = true
343 }
344 if header.ConfigVersion < retiredAutoPlanConfigVersion {
345 normalizeRetiredAutoPlan(cfg)
346 // Mark every older config as migrated even when Auto Plan was already off;
347 // the v5 renderer removes both retired keys so older binaries also observe
348 // the manual-only default after a downgrade.
349 changed = true
350 }
351 if header.ConfigVersion < billingSplitConfigVersion {
352 migrateBillingDisplayCurrency(cfg)
353 freezeProviderBillingCurrencies(cfg)
354 changed = true
355 }
356 if header.ConfigVersion < deepSeekScheduledPricingConfigVersion {
357 migrateDeepSeekScheduledPricingDefaults(cfg)
358 // Mark every older config, including custom-price configs, so their values
359 // remain user-owned on later startups instead of being reconsidered.
360 changed = true
361 }
362 if header.ConfigVersion < deepSeekChatDefaultConfigVersion {
363 restoreDeepSeekChatDefaults(cfg)
364 changed = true
365 }
366 if header.ConfigVersion < deepSeekOfficialChatUpgradeConfigVersion {
367 migrateOfficialDeepSeekChat(cfg)
368 changed = true
369 }
370 if !changed {
371 return repairProviderEndpointContractsOnStartup(path, false)
372 }
373 if header.ConfigVersion < openCodeGoUpgradeVersion {
374 cfg.ConfigVersion = deepSeekOfficialChatUpgradeConfigVersion
375 }
376 if err := cfg.SaveTo(path); err != nil {
377 return false, err
378 }
379 if header.ConfigVersion < openCodeGoUpgradeVersion {
380 if _, err := upgradeOpenCodeGoFileLocked(path); err != nil {
381 return false, err
382 }
383 }
384 return repairProviderEndpointContractsOnStartup(path, true)
385 }
386
387 func upgradeOpenCodeGoAndRepairProviderEndpoints(path string) (bool, error) {
388 upgraded, err := upgradeOpenCodeGoFileLocked(path)
389 if err != nil {
390 return false, err
391 }
392 return repairProviderEndpointContractsOnStartup(path, upgraded)
393 }
394
395 func repairProviderEndpointContractsOnStartup(path string, changed bool) (bool, error) {
396 repairs, err := repairProviderEndpointContractsFileLocked(path)
397 if err != nil {
398 return false, err
399 }
400 recordProviderEndpointRepairs(path, repairs)
401 return changed || len(repairs) > 0, nil
402 }
403
404 // ResetOfficialProviderPricingOnUpgrade is retained for older call sites.
405 func ResetOfficialProviderPricingOnUpgrade(path string) (bool, error) {
406 return ApplyUserConfigUpgradesOnStartup(path)
407 }
408
409 func shouldMarkWindowsBashSandboxDefaultUpgrade(fromVersion int) bool {
410 // Windows resolves every [sandbox].bash value to off at load time (see
411 // BashModeForGOOS), so no persisted rewrite is needed; explicit values stay
412 // readable and doctor reports them as ignored.
413 return false
414 }
415
416 func resetWindowsBashSandboxDefaultOnUpgrade(c *Config) {
417 if c == nil {
418 return
419 }
420 if strings.TrimSpace(c.Sandbox.Bash) != "enforce" {
421 return
422 }
423 c.Sandbox.Bash = "off"
424 }
425
426 func resetOfficialProviderPricingDefaults(c *Config) {
427 if c == nil {
428 return
429 }
430 for i := range c.Providers {
431 p := &c.Providers[i]
432 switch {
433 case officialProviderKind(p) == "deepseek":
434 resetDeepSeekOfficialPricing(p, deepSeekV4PricesForConfig(c))
435 }
436 }
437 }
438
439 func resetDeepSeekOfficialPricing(p *ProviderEntry, defaults map[string]*provider.Pricing) {
440 if p == nil {
441 return
442 }
443 p.Price = nil
444 if strings.TrimSpace(p.Model) != "" && len(p.Models) == 0 {
445 if price := defaults[strings.TrimSpace(p.Model)]; price != nil {
446 p.Price = clonePricing(price)
447 p.Prices = nil
448 return
449 }
450 }
451 if p.Prices == nil {
452 p.Prices = map[string]*provider.Pricing{}
453 }
454 for model, price := range defaults {
455 if p.HasModel(model) {
456 p.Prices[model] = clonePricing(price)
457 }
458 }
459 }
460
461 func legacyDeepSeekV4PricesCNY() map[string]*provider.Pricing {
462 return map[string]*provider.Pricing{
463 "deepseek-v4-flash": {CacheHit: 0.02, Input: 1, Output: 2, Currency: "¥"},
464 "deepseek-v4-pro": {CacheHit: 0.025, Input: 3, Output: 6, Currency: "¥"},
465 }
466 }
467
468 func legacyDeepSeekV4PricesUSD() map[string]*provider.Pricing {
469 return map[string]*provider.Pricing{
470 "deepseek-v4-flash": {CacheHit: 0.0028, Input: 0.14, Output: 0.28, Currency: "$"},
471 "deepseek-v4-pro": {CacheHit: 0.003625, Input: 0.435, Output: 0.87, Currency: "$"},
472 }
473 }
474
475 // augustDeepSeekV4Prices* are the 2026-08-17 table. They are recognized so a
476 // config that never left that generation is still refreshed to the current
477 // prices, and so it is not mistaken for a hand-edited custom rate.
478 func augustDeepSeekV4PricesCNY() map[string]*provider.Pricing {
479 return map[string]*provider.Pricing{
480 "deepseek-v4-flash": {CacheHit: 0.10, Input: 3, Output: 9, Currency: "¥"},
481 openai.OfficialDeepSeekVisionModel: {CacheHit: 0.10, Input: 3, Output: 9, Currency: "¥"},
482 "deepseek-v4-pro": {CacheHit: 0.30, Input: 9, Output: 27, Currency: "¥"},
483 }
484 }
485
486 func augustDeepSeekV4PricesUSD() map[string]*provider.Pricing {
487 return map[string]*provider.Pricing{
488 "deepseek-v4-flash": {CacheHit: 0.014, Input: 0.44, Output: 1.32, Currency: "$"},
489 openai.OfficialDeepSeekVisionModel: {CacheHit: 0.014, Input: 0.44, Output: 1.32, Currency: "$"},
490 "deepseek-v4-pro": {CacheHit: 0.044, Input: 1.32, Output: 3.96, Currency: "$"},
491 }
492 }
493
494 // migrateDeepSeekScheduledPricingDefaults replaces only the exact pre-August
495 // official defaults. Custom endpoints and any edited numeric rate remain intact.
496 func migrateDeepSeekScheduledPricingDefaults(c *Config) {
497 if c == nil {
498 return
499 }
500 for i := range c.Providers {
501 p := &c.Providers[i]
502 if officialProviderKind(p) != "deepseek" || !isOfficialDeepSeekBillingEndpoint(p) {
503 continue
504 }
505 currency := p.ProviderBillingCurrency()
506 if currency == "" {
507 currency = "USD"
508 }
509 legacy := legacyDeepSeekV4PricesUSD()
510 if normalizeDeepSeekPricingCurrency(currency) == "CNY" {
511 legacy = legacyDeepSeekV4PricesCNY()
512 }
513 migrate := func(model string, price *provider.Pricing) *provider.Pricing {
514 old := legacy[strings.TrimSpace(model)]
515 if !samePricingNormalizedCurrency(price, old) {
516 return price
517 }
518 return deepSeekV4PriceForModel(currency, model)
519 }
520 if p.Price != nil {
521 p.Price = migrate(p.Model, p.Price)
522 }
523 // Treat a multi-model official table atomically. A mixture of an old
524 // default row and a user-edited row is custom as a whole; partially
525 // rewriting it would leave an incoherent price book.
526 canMigrateTable := false
527 for model, price := range p.Prices {
528 old, known := legacy[strings.TrimSpace(model)]
529 if !known {
530 continue
531 }
532 canMigrateTable = true
533 if !samePricingNormalizedCurrency(price, old) {
534 canMigrateTable = false
535 break
536 }
537 }
538 if canMigrateTable {
539 for model, price := range p.Prices {
540 p.Prices[model] = migrate(model, price)
541 }
542 }
543 }
544 }
545
546 func isKnownDeepSeekOfficialPricing(model string, price *provider.Pricing) bool {
547 model = strings.TrimSpace(model)
548 if model == "" || price == nil {
549 return false
550 }
551 for _, prices := range []map[string]*provider.Pricing{
552 deepSeekV4PricesCNY(), deepSeekV4PricesUSD(),
553 augustDeepSeekV4PricesCNY(), augustDeepSeekV4PricesUSD(),
554 legacyDeepSeekV4PricesCNY(), legacyDeepSeekV4PricesUSD(),
555 } {
556 if samePricingNormalizedCurrency(price, prices[model]) {
557 return true
558 }
559 }
560 return false
561 }
562
563 // IsOfficialDeepSeekProvider reports whether an entry targets DeepSeek's
564 // official API endpoint. Desktop telemetry uses this after a regional-currency
565 // change so custom endpoints and rates stay untouched.
566 func IsOfficialDeepSeekProvider(p *ProviderEntry) bool {
567 return officialProviderKind(p) == "deepseek"
568 }
569
570 // IsKnownDeepSeekOfficialPricing reports whether price is one of Reasonix's
571 // built-in DeepSeek regional defaults for model.
572 func IsKnownDeepSeekOfficialPricing(model string, price *provider.Pricing) bool {
573 return isKnownDeepSeekOfficialPricing(model, price)
574 }
575
576 func samePricing(a, b *provider.Pricing) bool {
577 if a == nil || b == nil {
578 return false
579 }
580 return a.CacheHit == b.CacheHit && a.Input == b.Input && a.Output == b.Output && a.Currency == b.Currency
581 }
582
583 func samePricingNormalizedCurrency(a, b *provider.Pricing) bool {
584 if a == nil || b == nil {
585 return false
586 }
587 return a.CacheHit == b.CacheHit && a.Input == b.Input && a.Output == b.Output &&
588 normalizeDeepSeekPricingCurrency(a.Currency) == normalizeDeepSeekPricingCurrency(b.Currency)
589 }
590
590 lines GO