Skip to content

Commit e200e5e

Browse files
committed
fix(gemini): Fix configuration loss issues and simplify the authentication architecture #3
1 parent a1008c1 commit e200e5e

File tree

4 files changed

+79
-259
lines changed

4 files changed

+79
-259
lines changed

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/src/cli/commands/provider_input.rs

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,11 @@ fn prompt_gemini_config(current: Option<&Value>) -> Result<Value, AppError> {
372372
let current_auth_type = detect_gemini_auth_type(current);
373373
let default_index = match current_auth_type.as_deref() {
374374
Some("oauth") => 0,
375-
Some("packycode") => 1,
376-
Some("generic") => 2,
377-
_ => 1, // 默认 PackyCode
375+
_ => 1, // 默认 Generic API Key(包括 packycode 和 generic)
378376
};
379377

380378
let auth_options = vec![
381379
texts::google_oauth_official(),
382-
texts::packycode_api_key(),
383380
texts::generic_api_key(),
384381
];
385382

@@ -391,62 +388,15 @@ fn prompt_gemini_config(current: Option<&Value>) -> Result<Value, AppError> {
391388

392389
// Match using the translated strings
393390
let google_oauth = texts::google_oauth_official();
394-
let packycode = texts::packycode_api_key();
395391

396392
if auth_type == google_oauth {
397393
println!("{}", texts::use_google_oauth_warning().yellow());
398394
Ok(json!({
399395
"env": {},
400396
"config": {}
401397
}))
402-
} else if auth_type == packycode {
403-
let api_key = if let Some(current_key) = current
404-
.and_then(|v| v.get("env"))
405-
.and_then(|e| e.get("GEMINI_API_KEY"))
406-
.and_then(|k| k.as_str())
407-
.filter(|s| !s.is_empty())
408-
{
409-
Text::new(texts::gemini_api_key_label())
410-
.with_initial_value(current_key)
411-
.with_help_message(texts::packycode_api_key_help())
412-
.prompt()
413-
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
414-
} else {
415-
Text::new(texts::gemini_api_key_label())
416-
.with_placeholder("pk-...")
417-
.with_help_message(texts::packycode_api_key_help())
418-
.prompt()
419-
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
420-
};
421-
422-
let base_url = if let Some(current_url) = current
423-
.and_then(|v| v.get("env"))
424-
.and_then(|e| e.get("GOOGLE_GEMINI_BASE_URL"))
425-
.and_then(|u| u.as_str())
426-
.filter(|s| !s.is_empty())
427-
{
428-
Text::new(texts::gemini_base_url_label())
429-
.with_initial_value(current_url)
430-
.with_help_message(texts::packycode_endpoint_help())
431-
.prompt()
432-
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
433-
} else {
434-
Text::new(texts::gemini_base_url_label())
435-
.with_placeholder("https://packycode.com/api")
436-
.with_help_message(texts::packycode_endpoint_help())
437-
.prompt()
438-
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
439-
};
440-
441-
Ok(json!({
442-
"env": {
443-
"GEMINI_API_KEY": api_key.trim(),
444-
"GOOGLE_GEMINI_BASE_URL": base_url.trim()
445-
},
446-
"config": {}
447-
}))
448398
} else {
449-
// Generic API Key
399+
// Generic API Key (统一处理所有 API Key 供应商,包括 PackyCode)
450400
let api_key = if let Some(current_key) = current
451401
.and_then(|v| v.get("env"))
452402
.and_then(|e| e.get("GEMINI_API_KEY"))
@@ -460,15 +410,15 @@ fn prompt_gemini_config(current: Option<&Value>) -> Result<Value, AppError> {
460410
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
461411
} else {
462412
Text::new(texts::gemini_api_key_label())
463-
.with_placeholder("AIza...")
413+
.with_placeholder("AIza... or pk-...")
464414
.with_help_message(texts::generic_api_key_help())
465415
.prompt()
466416
.map_err(|e| AppError::Message(texts::input_failed_error(&e.to_string())))?
467417
};
468418

469419
let base_url = if let Some(current_url) = current
470420
.and_then(|v| v.get("env"))
471-
.and_then(|e| e.get("BASE_URL"))
421+
.and_then(|e| e.get("GOOGLE_GEMINI_BASE_URL"))
472422
.and_then(|u| u.as_str())
473423
.filter(|s| !s.is_empty())
474424
{
@@ -488,7 +438,7 @@ fn prompt_gemini_config(current: Option<&Value>) -> Result<Value, AppError> {
488438
Ok(json!({
489439
"env": {
490440
"GEMINI_API_KEY": api_key.trim(),
491-
"BASE_URL": base_url.trim()
441+
"GOOGLE_GEMINI_BASE_URL": base_url.trim()
492442
},
493443
"config": {}
494444
}))

src-tauri/src/gemini_config.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,40 +336,56 @@ fn update_selected_type(selected_type: &str) -> Result<(), AppError> {
336336
Ok(())
337337
}
338338

339-
/// 为 Packycode Gemini 供应商写入 settings.json
339+
/// 为 Google 官方 Gemini 供应商写入 settings.json(OAuth 模式)
340340
///
341341
/// 设置 `~/.gemini/settings.json` 中的:
342342
/// ```json
343343
/// {
344344
/// "security": {
345345
/// "auth": {
346-
/// "selectedType": "gemini-api-key"
346+
/// "selectedType": "oauth-personal"
347347
/// }
348348
/// }
349349
/// }
350350
/// ```
351351
///
352352
/// 保留文件中的其他所有字段。
353-
pub fn write_packycode_settings() -> Result<(), AppError> {
354-
update_selected_type("gemini-api-key")
353+
pub fn write_google_oauth_settings() -> Result<(), AppError> {
354+
update_selected_type("oauth-personal")
355355
}
356356

357-
/// 为 Google 官方 Gemini 供应商写入 settings.json(OAuth 模式)
357+
/// 为通用 Gemini API Key 供应商写入 settings.json
358358
///
359359
/// 设置 `~/.gemini/settings.json` 中的:
360360
/// ```json
361361
/// {
362362
/// "security": {
363363
/// "auth": {
364-
/// "selectedType": "oauth-personal"
364+
/// "selectedType": "gemini-api-key"
365365
/// }
366366
/// }
367367
/// }
368368
/// ```
369369
///
370370
/// 保留文件中的其他所有字段。
371-
pub fn write_google_oauth_settings() -> Result<(), AppError> {
372-
update_selected_type("oauth-personal")
371+
///
372+
/// 此函数适用于所有使用 API Key 认证的 Gemini 供应商,包括:
373+
/// - PackyCode(合作伙伴)
374+
/// - 其他第三方 Gemini API 服务
375+
pub fn write_generic_settings() -> Result<(), AppError> {
376+
update_selected_type("gemini-api-key")
377+
}
378+
379+
/// 为 Packycode Gemini 供应商写入 settings.json(已废弃,使用 write_generic_settings)
380+
///
381+
/// **注意**:此函数已废弃,仅为保持向后兼容性而保留。
382+
/// PackyCode 现在被视为普通的 API Key 供应商,请使用 `write_generic_settings()` 代替。
383+
#[deprecated(
384+
since = "4.1.1",
385+
note = "PackyCode is now treated as a generic API key provider. Use write_generic_settings() instead."
386+
)]
387+
pub fn write_packycode_settings() -> Result<(), AppError> {
388+
write_generic_settings()
373389
}
374390

375391
#[cfg(test)]

0 commit comments

Comments
 (0)