66//! @ai-context: 密钥解析优先级:环境变量 SILICONFLOW_API_KEY > 凭据库(DPAPI);
77//! 密钥**永不回传前端**(视图只报存在性与来源——明文红线)。
88//! @ai-context: 授权红线:内容上传类调用(M2 精修/M3 补充)消费
9- //! content_gate(enabled+authorized 双条件);余额查询/测试连接
9+ //! content_gate(enabled+authorized 双条件);余额查询
1010//! 为配置验证读操作不 gate(但审计记录——AI 调用轨迹可见化)。
1111//! @ai-context: 锁序:设置读写在短锁内完成即释放;网络调用(余额)不持锁。
1212
@@ -16,8 +16,6 @@ use crate::ai_balance::{low_balance_warning, AiBalance, AiBalanceAdapter};
1616use crate :: ai_guardrails:: AiAuditEntry ;
1717use crate :: ai_settings:: AiSettings ;
1818use crate :: commands:: AppState ;
19- /// 密钥最大长度(防超长字符串污染凭据文件;真实密钥远小于此)。
20- const API_KEY_MAX_CHARS : usize = 512 ;
2119
2220/// 设置视图(前端展示;密钥只暴露存在性与来源,绝不回传明文——明文红线)。
2321#[ derive( Debug , Clone , serde:: Serialize ) ]
@@ -92,25 +90,6 @@ pub fn ai_get_settings(state: State<'_, AppState>) -> Result<AiSettingsView, Str
9290 } )
9391}
9492
95- /// 保存密钥到凭据库(Windows DPAPI 加密文件;明文红线——不落 SQLite/明文)。
96- #[ tauri:: command]
97- pub fn ai_save_key ( state : State < ' _ , AppState > , api_key : String ) -> Result < ( ) , String > {
98- let key = api_key. trim ( ) . to_string ( ) ;
99- if key. is_empty ( ) {
100- return Err ( "密钥不能为空" . to_string ( ) ) ;
101- }
102- if key. chars ( ) . count ( ) > API_KEY_MAX_CHARS {
103- return Err ( format ! ( "密钥超长(上限 {} 字符)" , API_KEY_MAX_CHARS ) ) ;
104- }
105- state. ai_credentials . save_key ( "default" , & key)
106- }
107-
108- /// 清除凭据库密钥(文件不存在视为已清除——幂等)。
109- #[ tauri:: command]
110- pub fn ai_clear_key ( state : State < ' _ , AppState > ) -> Result < ( ) , String > {
111- state. ai_credentials . clear_key ( "default" )
112- }
113-
11493/// v0.18.2(REQ-254):目标 AI 设置(read-modify-write 最小面——
11594/// 不覆盖其他设置字段;档位白名单 light/standard/deep)。
11695#[ tauri:: command]
@@ -247,19 +226,6 @@ pub fn ai_set_refine_strategy(
247226 lock. save ( & state. ai_settings_path )
248227}
249228
250- /// 一键测试连接(REQ-138:调余额接口验证密钥有效性——错误密钥明确报错)。
251- ///
252- /// @ai-context: 配置验证类读操作,不 gate 授权;401/403 → 明确"密钥无效"
253- /// 提示(AiBalanceAdapter 归一);无密钥 → 引导配置。
254- #[ tauri:: command]
255- pub fn ai_test_connection ( state : State < ' _ , AppState > ) -> Result < AiBalance , String > {
256- let adapter = balance_adapter ( & state) ?;
257- let balance = adapter. fetch ( ) ?;
258- // 测试连接也留审计痕迹(AI 调用轨迹可见化)
259- push_audit ( & state, "test-connection" , "ok" ) ;
260- Ok ( balance)
261- }
262-
263229/// 查询余额(REQ-139:实时可查 + 低余额提醒 + 审计记录)。
264230#[ tauri:: command]
265231pub fn ai_get_balance ( state : State < ' _ , AppState > ) -> Result < BalanceView , String > {
0 commit comments