@@ -16,8 +16,12 @@ package ai
1616
1717import (
1818 "context"
19+ "net/http"
20+ "net/url"
21+ "strings"
1922
2023 "github.com/KusionStack/karpor/pkg/kubernetes/registry"
24+ "k8s.io/klog/v2"
2125)
2226
2327const (
@@ -52,22 +56,30 @@ type AIProvider interface {
5256
5357// AIConfig represents the configuration settings for an AI client.
5458type AIConfig struct {
55- Name string
56- AuthToken string
57- BaseURL string
58- Model string
59- Temperature float32
60- TopP float32
59+ Name string
60+ AuthToken string
61+ BaseURL string
62+ Model string
63+ Temperature float32
64+ TopP float32
65+ ProxyEnabled bool
66+ HTTPProxy string
67+ HTTPSProxy string
68+ NoProxy string
6169}
6270
6371func ConvertToAIConfig (c registry.ExtraConfig ) AIConfig {
6472 return AIConfig {
65- Name : c .AIBackend ,
66- AuthToken : c .AIAuthToken ,
67- BaseURL : c .AIBaseURL ,
68- Model : c .AIModel ,
69- Temperature : c .AITemperature ,
70- TopP : c .AITopP ,
73+ Name : c .AIBackend ,
74+ AuthToken : c .AIAuthToken ,
75+ BaseURL : c .AIBaseURL ,
76+ Model : c .AIModel ,
77+ Temperature : c .AITemperature ,
78+ TopP : c .AITopP ,
79+ ProxyEnabled : c .AIProxyEnabled ,
80+ HTTPProxy : c .AIHTTPProxy ,
81+ HTTPSProxy : c .AIHTTPSProxy ,
82+ NoProxy : c .AINoProxy ,
7183 }
7284}
7385
@@ -79,3 +91,46 @@ func NewClient(name string) AIProvider {
7991 // default client
8092 return & OpenAIClient {}
8193}
94+
95+ // GetProxyHTTPClient returns a new http.Transport with proxy configuration
96+ func GetProxyHTTPClient (cfg AIConfig ) * http.Transport {
97+ noProxyList := strings .Split (cfg .NoProxy , "," )
98+
99+ return & http.Transport {
100+ Proxy : func (req * http.Request ) (* url.URL , error ) {
101+ host := req .URL .Host
102+ // Check if host matches NoProxy list
103+ for _ , np := range noProxyList {
104+ if np = strings .TrimSpace (np ); np != "" {
105+ // exact match
106+ if host == np {
107+ klog .Infof ("Skip proxy for %s: exact match in no_proxy list" , host )
108+ return nil , nil
109+ }
110+ // Domain suffix match with dot to prevent false positives
111+ // e.g. pattern "le.com", it would incorrectly match host "example.com".
112+ if ! strings .HasPrefix (np , "." ) {
113+ np = "." + np
114+ }
115+ if strings .HasSuffix (host , np ) {
116+ klog .Infof ("Skip proxy for %s: suffix match with %s in no_proxy list" , host , np )
117+ return nil , nil
118+ }
119+ }
120+ }
121+
122+ var proxyURL string
123+ if req .URL .Scheme == "https" && cfg .HTTPSProxy != "" {
124+ proxyURL = cfg .HTTPSProxy
125+ } else if req .URL .Scheme == "http" && cfg .HTTPProxy != "" {
126+ proxyURL = cfg .HTTPProxy
127+ }
128+
129+ if proxyURL != "" {
130+ klog .Infof ("Using proxy %s for %s" , proxyURL , req .URL )
131+ return url .Parse (proxyURL )
132+ }
133+ return nil , nil
134+ },
135+ }
136+ }
0 commit comments