mirror of
https://github.com/qingchencloud/clawpanel.git
synced 2026-06-05 15:49:43 +08:00
style: cargo fmt
This commit is contained in:
@@ -205,7 +205,11 @@ fn sync_providers_to_agent_models(config: &Value) {
|
||||
|
||||
let mut changed = false;
|
||||
|
||||
if models_json.get("providers").and_then(|p| p.as_object()).is_none() {
|
||||
if models_json
|
||||
.get("providers")
|
||||
.and_then(|p| p.as_object())
|
||||
.is_none()
|
||||
{
|
||||
if let Some(root) = models_json.as_object_mut() {
|
||||
root.insert("providers".into(), json!({}));
|
||||
changed = true;
|
||||
@@ -1362,7 +1366,10 @@ pub async fn test_model(
|
||||
req.send()
|
||||
}
|
||||
"google-gemini" => {
|
||||
let url = format!("{}/models/{}:generateContent?key={}", base, model_id, api_key);
|
||||
let url = format!(
|
||||
"{}/models/{}:generateContent?key={}",
|
||||
base, model_id, api_key
|
||||
);
|
||||
let body = json!({
|
||||
"contents": [{"role": "user", "parts": [{"text": "Hi"}]}]
|
||||
});
|
||||
@@ -1532,7 +1539,8 @@ pub async fn list_remote_models(
|
||||
let ids = serde_json::from_str::<serde_json::Value>(&text)
|
||||
.ok()
|
||||
.and_then(|v| {
|
||||
let mut ids: Vec<String> = if let Some(data) = v.get("data").and_then(|d| d.as_array()) {
|
||||
let mut ids: Vec<String> = if let Some(data) = v.get("data").and_then(|d| d.as_array())
|
||||
{
|
||||
data.iter()
|
||||
.filter_map(|m| m.get("id").and_then(|id| id.as_str()).map(String::from))
|
||||
.collect()
|
||||
@@ -1730,9 +1738,9 @@ pub async fn check_panel_update() -> Result<Value, String> {
|
||||
result.insert("latest".into(), Value::String(tag));
|
||||
result.insert(
|
||||
"url".into(),
|
||||
json.get("html_url").cloned().unwrap_or(Value::String(
|
||||
releases_url.to_string(),
|
||||
)),
|
||||
json.get("html_url")
|
||||
.cloned()
|
||||
.unwrap_or(Value::String(releases_url.to_string())),
|
||||
);
|
||||
result.insert("source".into(), Value::String(source.to_string()));
|
||||
result.insert(
|
||||
@@ -1823,7 +1831,16 @@ pub async fn auto_install_git(app: tauri::AppHandle) -> Result<String, String> {
|
||||
// 尝试 winget
|
||||
let _ = app.emit("upgrade-log", "尝试使用 winget 安装 Git...");
|
||||
let mut child = Command::new("winget")
|
||||
.args(["install", "--id", "Git.Git", "-e", "--source", "winget", "--accept-package-agreements", "--accept-source-agreements"])
|
||||
.args([
|
||||
"install",
|
||||
"--id",
|
||||
"Git.Git",
|
||||
"-e",
|
||||
"--source",
|
||||
"winget",
|
||||
"--accept-package-agreements",
|
||||
"--accept-source-agreements",
|
||||
])
|
||||
.creation_flags(0x08000000)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
@@ -1846,12 +1863,16 @@ pub async fn auto_install_git(app: tauri::AppHandle) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
let _ = handle.join();
|
||||
let status = child.wait().map_err(|e| format!("等待 winget 完成失败: {e}"))?;
|
||||
let status = child
|
||||
.wait()
|
||||
.map_err(|e| format!("等待 winget 完成失败: {e}"))?;
|
||||
if status.success() {
|
||||
let _ = app.emit("upgrade-log", "Git 安装成功!");
|
||||
return Ok("Git 已通过 winget 安装".to_string());
|
||||
}
|
||||
return Err("winget 安装 Git 失败,请手动下载安装: https://git-scm.com/downloads".to_string());
|
||||
return Err(
|
||||
"winget 安装 Git 失败,请手动下载安装: https://git-scm.com/downloads".to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -1868,22 +1889,48 @@ pub async fn auto_install_git(app: tauri::AppHandle) -> Result<String, String> {
|
||||
let _ = app.emit("upgrade-log", "Git 安装已触发,请在弹出的窗口中确认安装。");
|
||||
return Ok("已触发 xcode-select 安装,请在弹窗中确认".to_string());
|
||||
}
|
||||
return Err("xcode-select 安装失败,请手动安装 Xcode Command Line Tools 或 brew install git".to_string());
|
||||
return Err(
|
||||
"xcode-select 安装失败,请手动安装 Xcode Command Line Tools 或 brew install git"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// 检测包管理器
|
||||
let pkg_mgr = if Command::new("apt-get").arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
|
||||
let pkg_mgr = if Command::new("apt-get")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
"apt"
|
||||
} else if Command::new("yum").arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
|
||||
} else if Command::new("yum")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
"yum"
|
||||
} else if Command::new("dnf").arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
|
||||
} else if Command::new("dnf")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
"dnf"
|
||||
} else if Command::new("pacman").arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
|
||||
} else if Command::new("pacman")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|o| o.status.success())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
"pacman"
|
||||
} else {
|
||||
return Err("未找到包管理器,请手动安装 Git: sudo apt install git 或 sudo yum install git".to_string());
|
||||
return Err(
|
||||
"未找到包管理器,请手动安装 Git: sudo apt install git 或 sudo yum install git"
|
||||
.to_string(),
|
||||
);
|
||||
};
|
||||
|
||||
let (cmd_name, args): (&str, Vec<&str>) = match pkg_mgr {
|
||||
@@ -1894,7 +1941,10 @@ pub async fn auto_install_git(app: tauri::AppHandle) -> Result<String, String> {
|
||||
_ => return Err("不支持的包管理器".to_string()),
|
||||
};
|
||||
|
||||
let _ = app.emit("upgrade-log", format!("执行: {} {}", cmd_name, args.join(" ")));
|
||||
let _ = app.emit(
|
||||
"upgrade-log",
|
||||
format!("执行: {} {}", cmd_name, args.join(" ")),
|
||||
);
|
||||
let mut child = Command::new(cmd_name)
|
||||
.args(&args)
|
||||
.stdout(Stdio::piped())
|
||||
|
||||
@@ -391,7 +391,10 @@ pub async fn save_messaging_platform(
|
||||
.unwrap_or("")
|
||||
.trim();
|
||||
if !gateway_password.is_empty() {
|
||||
entry.insert("gatewayPassword".into(), Value::String(gateway_password.into()));
|
||||
entry.insert(
|
||||
"gatewayPassword".into(),
|
||||
Value::String(gateway_password.into()),
|
||||
);
|
||||
}
|
||||
|
||||
channels_map.insert(storage_key, Value::Object(entry));
|
||||
@@ -712,7 +715,9 @@ fn ensure_plugin_allowed(cfg: &mut Value, plugin_id: &str) -> Result<(), String>
|
||||
}
|
||||
|
||||
fn plugin_backup_root() -> PathBuf {
|
||||
super::openclaw_dir().join("backups").join("plugin-installs")
|
||||
super::openclaw_dir()
|
||||
.join("backups")
|
||||
.join("plugin-installs")
|
||||
}
|
||||
|
||||
fn qqbot_plugin_dir() -> PathBuf {
|
||||
@@ -801,7 +806,8 @@ fn repair_qqbot_package_manifest(plugin_dir: &Path) -> Result<String, String> {
|
||||
return Err("QQBot 插件缺少 package.json".into());
|
||||
}
|
||||
|
||||
let raw = fs::read_to_string(&package_path).map_err(|e| format!("读取 package.json 失败: {e}"))?;
|
||||
let raw =
|
||||
fs::read_to_string(&package_path).map_err(|e| format!("读取 package.json 失败: {e}"))?;
|
||||
let mut pkg: Value =
|
||||
serde_json::from_str(&raw).map_err(|e| format!("解析 package.json 失败: {e}"))?;
|
||||
|
||||
@@ -952,7 +958,10 @@ pub async fn install_channel_plugin(
|
||||
}
|
||||
if had_existing_plugin {
|
||||
fs::rename(&plugin_dir, &plugin_backup).map_err(|e| format!("备份旧插件失败: {e}"))?;
|
||||
let _ = app.emit("plugin-log", format!("检测到旧插件目录,已备份 {}", plugin_dir.display()));
|
||||
let _ = app.emit(
|
||||
"plugin-log",
|
||||
format!("检测到旧插件目录,已备份 {}", plugin_dir.display()),
|
||||
);
|
||||
}
|
||||
|
||||
if config_backup.exists() {
|
||||
@@ -970,7 +979,8 @@ pub async fn install_channel_plugin(
|
||||
let mut child = match spawn_result {
|
||||
Ok(child) => child,
|
||||
Err(e) => {
|
||||
let _ = cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config);
|
||||
let _ =
|
||||
cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config);
|
||||
return Err(format!("启动 openclaw 失败: {}", e));
|
||||
}
|
||||
};
|
||||
@@ -1004,14 +1014,21 @@ pub async fn install_channel_plugin(
|
||||
.wait()
|
||||
.map_err(|e| format!("等待安装进程失败: {}", e))?;
|
||||
if !status.success() {
|
||||
let rollback_err = cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let _ = app.emit("plugin-log", format!("插件 {} 安装失败,已回退", package_name));
|
||||
let rollback_err =
|
||||
cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let _ = app.emit(
|
||||
"plugin-log",
|
||||
format!("插件 {} 安装失败,已回退", package_name),
|
||||
);
|
||||
return if rollback_err.is_empty() {
|
||||
Err(format!("插件安装失败:{}", package_name))
|
||||
} else {
|
||||
Err(format!("插件安装失败:{};回退失败:{}", package_name, rollback_err))
|
||||
Err(format!(
|
||||
"插件安装失败:{};回退失败:{}",
|
||||
package_name, rollback_err
|
||||
))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1023,10 +1040,14 @@ pub async fn install_channel_plugin(
|
||||
})();
|
||||
|
||||
if let Err(err) = finalize {
|
||||
let rollback_err = cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let _ = app.emit("plugin-log", format!("插件 {} 安装后收尾失败,已回退: {}", package_name, err));
|
||||
let rollback_err =
|
||||
cleanup_failed_plugin_install(plugin_id, had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let _ = app.emit(
|
||||
"plugin-log",
|
||||
format!("插件 {} 安装后收尾失败,已回退: {}", package_name, err),
|
||||
);
|
||||
return if rollback_err.is_empty() {
|
||||
Err(format!("插件安装失败:{err}"))
|
||||
} else {
|
||||
@@ -1070,7 +1091,8 @@ pub async fn install_qqbot_plugin(app: tauri::AppHandle) -> Result<String, Strin
|
||||
let _ = fs::remove_dir_all(&plugin_backup);
|
||||
}
|
||||
if had_existing_plugin {
|
||||
fs::rename(&plugin_dir, &plugin_backup).map_err(|e| format!("备份旧 QQBot 插件失败: {e}"))?;
|
||||
fs::rename(&plugin_dir, &plugin_backup)
|
||||
.map_err(|e| format!("备份旧 QQBot 插件失败: {e}"))?;
|
||||
}
|
||||
|
||||
if config_backup.exists() {
|
||||
@@ -1125,7 +1147,10 @@ pub async fn install_qqbot_plugin(app: tauri::AppHandle) -> Result<String, Strin
|
||||
|
||||
let finalize = (|| -> Result<(), String> {
|
||||
if !status.success() {
|
||||
let _ = app.emit("plugin-log", "安装器返回失败,正在尝试自动修复 QQBot 插件...");
|
||||
let _ = app.emit(
|
||||
"plugin-log",
|
||||
"安装器返回失败,正在尝试自动修复 QQBot 插件...",
|
||||
);
|
||||
}
|
||||
|
||||
let entry = repair_qqbot_package_manifest(&plugin_dir)?;
|
||||
@@ -1134,7 +1159,10 @@ pub async fn install_qqbot_plugin(app: tauri::AppHandle) -> Result<String, Strin
|
||||
let mut cfg = super::config::load_openclaw_json()?;
|
||||
ensure_plugin_allowed(&mut cfg, "qqbot")?;
|
||||
super::config::save_openclaw_json(&cfg)?;
|
||||
let _ = app.emit("plugin-log", "已补齐 plugins.allow 与 entries.qqbot.enabled");
|
||||
let _ = app.emit(
|
||||
"plugin-log",
|
||||
"已补齐 plugins.allow 与 entries.qqbot.enabled",
|
||||
);
|
||||
Ok(())
|
||||
})();
|
||||
|
||||
@@ -1152,9 +1180,10 @@ pub async fn install_qqbot_plugin(app: tauri::AppHandle) -> Result<String, Strin
|
||||
}
|
||||
Err(err) => {
|
||||
let _ = app.emit("plugin-log", format!("自动修复失败,正在回退: {err}"));
|
||||
let rollback_err = cleanup_failed_qqbot_install(had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let rollback_err =
|
||||
cleanup_failed_qqbot_install(had_existing_plugin, had_existing_config)
|
||||
.err()
|
||||
.unwrap_or_default();
|
||||
let _ = app.emit("plugin-progress", 100);
|
||||
let _ = app.emit("plugin-log", "QQBot 插件安装失败,已自动回退到安装前状态");
|
||||
if rollback_err.is_empty() {
|
||||
|
||||
Reference in New Issue
Block a user