import { invoke } from "@tauri-apps/api/core"; import type { ProxyConfig, ProxyStatus, ProxyServerInfo, ProxyTakeoverStatus, GlobalProxyConfig, AppProxyConfig, } from "@/types/proxy"; export const proxyApi = { // ========== 代理服务器控制 API ========== // 启动代理服务器 async startProxyServer(): Promise { return invoke("start_proxy_server"); }, // 停止代理服务器并恢复配置 async stopProxyWithRestore(): Promise { return invoke("stop_proxy_with_restore"); }, // 获取代理服务器状态 async getProxyStatus(): Promise { return invoke("get_proxy_status"); }, // 检查代理服务器是否正在运行 async isProxyRunning(): Promise { return invoke("is_proxy_running"); }, // 检查是否处于接管模式 async isLiveTakeoverActive(): Promise { return invoke("is_live_takeover_active"); }, // 代理模式下切换供应商 async switchProxyProvider( appType: string, providerId: string, ): Promise { return invoke("switch_proxy_provider", { appType, providerId }); }, // ========== 接管状态 API ========== // 获取各应用接管状态 async getProxyTakeoverStatus(): Promise { return invoke("get_proxy_takeover_status"); }, // 为指定应用开启/关闭接管 async setProxyTakeoverForApp( appType: string, enabled: boolean, ): Promise { return invoke("set_proxy_takeover_for_app", { appType, enabled }); }, // ========== Legacy 代理配置 API (兼容) ========== // 获取代理配置(旧版 v2 兼容接口) async getProxyConfig(): Promise { return invoke("get_proxy_config"); }, // 更新代理配置(旧版 v2 兼容接口) async updateProxyConfig(config: ProxyConfig): Promise { return invoke("update_proxy_config", { config }); }, // ========== v3+ 全局/应用级配置 API ========== // 获取全局代理配置 async getGlobalProxyConfig(): Promise { return invoke("get_global_proxy_config"); }, // 更新全局代理配置 async updateGlobalProxyConfig(config: GlobalProxyConfig): Promise { return invoke("update_global_proxy_config", { config }); }, // 获取指定应用的代理配置 async getProxyConfigForApp(appType: string): Promise { return invoke("get_proxy_config_for_app", { appType }); }, // 更新指定应用的代理配置 async updateProxyConfigForApp(config: AppProxyConfig): Promise { return invoke("update_proxy_config_for_app", { config }); }, // ========== 计费默认配置 API ========== // 获取默认成本倍率 async getDefaultCostMultiplier(appType: string): Promise { return invoke("get_default_cost_multiplier", { appType }); }, // 设置默认成本倍率 async setDefaultCostMultiplier( appType: string, value: string, ): Promise { return invoke("set_default_cost_multiplier", { appType, value }); }, // 获取计费模式来源 async getPricingModelSource(appType: string): Promise { return invoke("get_pricing_model_source", { appType }); }, // 设置计费模式来源 async setPricingModelSource(appType: string, value: string): Promise { return invoke("set_pricing_model_source", { appType, value }); }, // ========== URL 预览 API ========== /** * 构建 URL 预览 * 根据 app_type、base_url 和 api_format 计算直连和代理模式的请求地址 */ async buildUrlPreview( appType: string, baseUrl: string, apiFormat?: string, ): Promise { return invoke("build_url_preview", { appType, baseUrl, apiFormat }); }, /** * 检查是否需要代理 * 返回需要代理的原因,null 表示不需要代理 */ async checkProxyRequirement( appType: string, baseUrl: string, apiFormat?: string, ): Promise { return invoke("check_proxy_requirement", { appType, baseUrl, apiFormat }); }, }; /** URL 预览结果 */ export interface UrlPreview { /** 直连模式请求地址 */ direct_url: string; /** 代理模式请求地址 */ proxy_url: string; /** 是否为全链接(base_url 已包含 API 路径) */ is_full_url: boolean; }