Files
Local-Cursor/internal/bridge/proxy.go

201 lines
7.6 KiB
Go
Raw Normal View History

package bridge
import (
serverconfig "cursor/internal/backend/server/config"
"cursor/internal/certs"
"cursor/internal/client"
"cursor/internal/mitm"
"runtime"
)
// Public DTOs remain in package main for Wails service compatibility.
// ProxyState 定义了当前模块中的 ProxyState 类型。
type ProxyState = client.ProxyState
// UserConfig 定义了当前模块中的 UserConfig 类型。
type UserConfig = client.UserConfig
// ModelAdapterConfig 定义模型测速使用的模型配置结构。
type ModelAdapterConfig = serverconfig.ModelAdapterConfig
// ModelAdapterTestResult 定义一次模型测速结果。
type ModelAdapterTestResult = client.ModelAdapterTestResult
// CLIStatus 定义 CLI 版 cursor-agent 的代理状态。
type CLIStatus = client.CLIStatus
// ModelAdapterTestResultsPayload 定义测速结果事件载荷。
type ModelAdapterTestResultsPayload = client.ModelAdapterTestResultsPayload
// LicenseActionRequest 定义了当前模块中的 LicenseActionRequest 类型。
type LicenseActionRequest = client.LicenseActionRequest
// LicenseSwitchDeviceRequest 定义了当前模块中的 LicenseSwitchDeviceRequest 类型。
type LicenseSwitchDeviceRequest = client.LicenseSwitchDeviceRequest
// LicenseAPIResult 定义了当前模块中的 LicenseAPIResult 类型。
type LicenseAPIResult = client.LicenseAPIResult
// UsageRecordsRequest 定义了当前模块中的 UsageRecordsRequest 类型。
type UsageRecordsRequest = client.UsageRecordsRequest
// UsageRecord 定义了当前模块中的 UsageRecord 类型。
type UsageRecord = client.UsageRecord
// UsageRecordsData 定义了当前模块中的 UsageRecordsData 类型。
type UsageRecordsData = client.UsageRecordsData
// UsageRecordsResult 定义了当前模块中的 UsageRecordsResult 类型。
type UsageRecordsResult = client.UsageRecordsResult
// RemoteModel 表示从模型服务 /models 端点拉取到的一个模型。
type RemoteModel = client.RemoteModel
// RemoteModelsResult 表示一次模型列表拉取结果。
type RemoteModelsResult = client.RemoteModelsResult
// ProxyService 定义了当前模块中的 ProxyService 类型。
type ProxyService struct {
// core 表示当前声明中的 core。
core *client.ProxyService
}
// NewProxyService 用于处理与 NewProxyService 相关的逻辑。
func NewProxyService(proxy *mitm.ProxyServer, certManager *certs.Manager, caCertPEM []byte) *ProxyService {
return &ProxyService{core: client.NewProxyService(proxy, certManager, caCertPEM)}
}
// StartProxy 用于处理与 StartProxy 相关的逻辑。
func (s *ProxyService) StartProxy() (state ProxyState, err error) {
logServiceCall("proxy", "start")
defer func() { logServiceResult("proxy", "start", err) }()
return s.core.StartProxy()
}
// StopProxy 用于处理与 StopProxy 相关的逻辑。
func (s *ProxyService) StopProxy() (state ProxyState, err error) {
logServiceCall("proxy", "stop")
defer func() { logServiceResult("proxy", "stop", err) }()
return s.core.StopProxy()
}
// GetState 用于处理与 GetState 相关的逻辑。
func (s *ProxyService) GetState() ProxyState {
return s.core.GetState()
}
// ClearLastError 用于处理与 ClearLastError 相关的逻辑。
func (s *ProxyService) ClearLastError() ProxyState {
logServiceCall("proxy", "clear_last_error")
state := s.core.ClearLastError()
logServiceResult("proxy", "clear_last_error", nil)
return state
}
// SetBaseURL 用于处理与 SetBaseURL 相关的逻辑。
func (s *ProxyService) SetBaseURL(baseURL string) (state ProxyState, err error) {
logServiceCall("proxy", "set_base_url")
defer func() { logServiceResult("proxy", "set_base_url", err) }()
return s.core.SetBaseURL(baseURL)
}
// LoadUserConfig 用于处理与 LoadUserConfig 相关的逻辑。
func (s *ProxyService) LoadUserConfig() (UserConfig, error) {
return s.core.LoadUserConfig()
}
// SaveUserConfig 用于处理与 SaveUserConfig 相关的逻辑。
func (s *ProxyService) SaveUserConfig(cfg UserConfig) error {
logServiceCall("config", "save_user_config")
err := s.core.SaveUserConfig(cfg)
logServiceResult("config", "save_user_config", err)
return err
}
// TestModelAdapter 用于处理与 TestModelAdapter 相关的逻辑。
func (s *ProxyService) TestModelAdapter(adapter ModelAdapterConfig) (result ModelAdapterTestResult, err error) {
logServiceCall("models", "test_adapter")
defer func() { logServiceResult("models", "test_adapter", err) }()
return s.core.TestModelAdapter(adapter)
}
// ListRemoteModels 通过模型服务的 /v1/models 端点拉取可用模型列表。
func (s *ProxyService) ListRemoteModels(adapterType string, baseURL string, apiKey string) (result RemoteModelsResult, err error) {
logServiceCall("models", "list_remote", "adapter_type", adapterType)
defer func() { logServiceResult("models", "list_remote", err) }()
return s.core.ListRemoteModels(adapterType, baseURL, apiKey)
}
// GetModelAdapterTestResults 用于处理与 GetModelAdapterTestResults 相关的逻辑。
func (s *ProxyService) GetModelAdapterTestResults() []ModelAdapterTestResult {
return s.core.GetModelAdapterTestResults()
}
// GetDeviceID 用于处理与 GetDeviceID 相关的逻辑。
func (s *ProxyService) GetDeviceID() (deviceID string, err error) {
logServiceCall("license", "get_device_id")
defer func() { logServiceResult("license", "get_device_id", err) }()
return s.core.GetDeviceID()
}
// ActivateLicense 用于处理与 ActivateLicense 相关的逻辑。
func (s *ProxyService) ActivateLicense(req LicenseActionRequest) (result LicenseAPIResult, err error) {
logServiceCall("license", "activate")
defer func() { logServiceResult("license", "activate", err) }()
return s.core.ActivateLicense(req)
}
// BindLicenseDevice 用于处理与 BindLicenseDevice 相关的逻辑。
func (s *ProxyService) BindLicenseDevice(req LicenseActionRequest) (result LicenseAPIResult, err error) {
logServiceCall("license", "bind_device")
defer func() { logServiceResult("license", "bind_device", err) }()
return s.core.BindLicenseDevice(req)
}
// SwitchLicenseDevice 用于处理与 SwitchLicenseDevice 相关的逻辑。
func (s *ProxyService) SwitchLicenseDevice(req LicenseSwitchDeviceRequest) (result LicenseAPIResult, err error) {
logServiceCall("license", "switch_device")
defer func() { logServiceResult("license", "switch_device", err) }()
return s.core.SwitchLicenseDevice(req)
}
// QueryUsageRecords 用于处理与 QueryUsageRecords 相关的逻辑。
func (s *ProxyService) QueryUsageRecords(req UsageRecordsRequest) (result UsageRecordsResult, err error) {
logServiceCall("license", "query_usage_records")
defer func() { logServiceResult("license", "query_usage_records", err) }()
return s.core.QueryUsageRecords(req)
}
// ApplyCursorSettings 用于处理与 ApplyCursorSettings 相关的逻辑。
func (s *ProxyService) ApplyCursorSettings() error {
logServiceCall("cursor", "apply_settings")
err := s.core.ApplyCursorSettings()
logServiceResult("cursor", "apply_settings", err)
return err
}
// ClearCursorSettings 用于处理与 ClearCursorSettings 相关的逻辑。
func (s *ProxyService) ClearCursorSettings() error {
logServiceCall("cursor", "clear_settings")
err := s.core.ClearCursorSettings()
logServiceResult("cursor", "clear_settings", err)
return err
}
// GetCLIStatus 返回 CLI 版 cursor-agent 的代理状态。
func (s *ProxyService) GetCLIStatus() CLIStatus {
return s.core.GetCLIStatus()
}
// ShutdownForQuit 用于处理与 ShutdownForQuit 相关的逻辑。
func (s *ProxyService) ShutdownForQuit() {
logServiceCall("proxy", "shutdown_for_quit")
s.core.ShutdownForQuit()
logServiceResult("proxy", "shutdown_for_quit", nil)
}
// IsWindows 用于处理与 IsWindows 相关的逻辑。
func (s *ProxyService) IsWindows() bool {
return runtime.GOOS == "windows"
}