File size: 3,654 Bytes
bf9e111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e6b65c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package mapper provides mapping functions between domain entities and DTOs.
package mapper

import (
	"github.com/router-for-me/CLIProxyAPI/v6/internal/application/dto"
	"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
)

// ToConfigResponse maps a domain Config to a ConfigResponse DTO
func ToConfigResponse(cfg *config.Config) *dto.ConfigResponse {
	if cfg == nil {
		return &dto.ConfigResponse{
			Routing:          dto.RoutingConfig{Strategy: "round-robin"},
			RemoteManagement: dto.RemoteManagementConfig{},
			QuotaExceeded:    dto.QuotaExceededConfig{},
			AmpCode:          config.AmpCode{},
		}
	}

	return &dto.ConfigResponse{
		Debug:                  cfg.Debug,
		UsageStatisticsEnabled: cfg.UsageStatisticsEnabled,
		LoggingToFile:          cfg.LoggingToFile,
		LogsMaxTotalSizeMB:     cfg.LogsMaxTotalSizeMB,
		RequestLog:             cfg.RequestLog,
		WebsocketAuth:          cfg.WebsocketAuth,
		RequestRetry:           cfg.RequestRetry,
		MaxRetryInterval:       cfg.MaxRetryInterval,
		ForceModelPrefix:       cfg.ForceModelPrefix,
		ProxyURL:               cfg.ProxyURL,
		Routing: dto.RoutingConfig{
			Strategy: cfg.Routing.Strategy,
		},
		RemoteManagement: dto.RemoteManagementConfig{
			AllowRemote: cfg.RemoteManagement.AllowRemote,
			SecretKey:   cfg.RemoteManagement.SecretKey,
		},
		QuotaExceeded: dto.QuotaExceededConfig{
			SwitchProject:      cfg.QuotaExceeded.SwitchProject,
			SwitchPreviewModel: cfg.QuotaExceeded.SwitchPreviewModel,
		},
		APIKeys:             cfg.APIKeys,
		GeminiKey:           cfg.GeminiKey,
		ClaudeKey:           cfg.ClaudeKey,
		CodexKey:            cfg.CodexKey,
		OpenAICompatibility: cfg.OpenAICompatibility,
		VertexCompatAPIKey:  cfg.VertexCompatAPIKey,
		KiroKey:             cfg.KiroKey,
		OAuthExcludedModels: cfg.OAuthExcludedModels,
		OAuthModelAlias:     cfg.OAuthModelAlias,
		AmpCode:             cfg.AmpCode,
	}
}

// ToConfig maps an UpdateConfigRequest DTO to a domain Config
func ToConfig(req *dto.UpdateConfigRequest) *config.Config {
	if req == nil {
		return &config.Config{}
	}
	return &req.Config
}

// ToConfigFromResponse maps a ConfigResponse DTO back to a domain Config
func ToConfigFromResponse(resp *dto.ConfigResponse) *config.Config {
	if resp == nil {
		return &config.Config{}
	}

	cfg := &config.Config{
		Debug:                  resp.Debug,
		UsageStatisticsEnabled: resp.UsageStatisticsEnabled,
		LoggingToFile:          resp.LoggingToFile,
		LogsMaxTotalSizeMB:     resp.LogsMaxTotalSizeMB,
		WebsocketAuth:          resp.WebsocketAuth,
		RequestRetry:           resp.RequestRetry,
		MaxRetryInterval:       resp.MaxRetryInterval,
		Routing: config.RoutingConfig{
			Strategy: resp.Routing.Strategy,
		},
		RemoteManagement: config.RemoteManagement{
			AllowRemote: resp.RemoteManagement.AllowRemote,
			SecretKey:   resp.RemoteManagement.SecretKey,
		},
		QuotaExceeded: config.QuotaExceeded{
			SwitchProject:      resp.QuotaExceeded.SwitchProject,
			SwitchPreviewModel: resp.QuotaExceeded.SwitchPreviewModel,
		},
		GeminiKey:           resp.GeminiKey,
		ClaudeKey:           resp.ClaudeKey,
		CodexKey:            resp.CodexKey,
		OpenAICompatibility: resp.OpenAICompatibility,
		VertexCompatAPIKey:  resp.VertexCompatAPIKey,
		KiroKey:             resp.KiroKey,
		OAuthExcludedModels: resp.OAuthExcludedModels,
		OAuthModelAlias:     resp.OAuthModelAlias,
		AmpCode:             resp.AmpCode,
	}

	// Set SDKConfig fields using the embedded type
	cfg.SDKConfig.RequestLog = resp.RequestLog
	cfg.SDKConfig.ForceModelPrefix = resp.ForceModelPrefix
	cfg.SDKConfig.ProxyURL = resp.ProxyURL
	cfg.SDKConfig.APIKeys = resp.APIKeys

	return cfg
}