优化:初始设置页面自动创建默认配置文件

当检测到 OpenClaw CLI 已安装但 openclaw.json 不存在时:
1. 自动创建包含合理默认值的配置文件(mode:local, tools:full 等)
2. 如果自动创建失败,显示「一键初始化配置」按钮供手动操作
3. 新增 init_openclaw_config API(dev-api.js + Tauri Rust 后端)
4. 用户不再需要去终端手动执行 openclaw configure
This commit is contained in:
晴天
2026-03-05 23:14:13 +08:00
parent b709b70839
commit bb2c6df8ec
5 changed files with 99 additions and 3 deletions

View File

@@ -627,6 +627,46 @@ pub async fn upgrade_openclaw(app: tauri::AppHandle, source: String) -> Result<S
Ok(msg)
}
/// 自动初始化配置文件CLI 已装但 openclaw.json 不存在时)
#[tauri::command]
pub fn init_openclaw_config() -> Result<Value, String> {
let dir = super::openclaw_dir();
let config_path = dir.join("openclaw.json");
let mut result = serde_json::Map::new();
if config_path.exists() {
result.insert("created".into(), Value::Bool(false));
result.insert("message".into(), Value::String("配置文件已存在".into()));
return Ok(Value::Object(result));
}
// 确保目录存在
if !dir.exists() {
std::fs::create_dir_all(&dir).map_err(|e| format!("创建目录失败: {e}"))?;
}
let default_config = serde_json::json!({
"$schema": "https://openclaw.ai/schema/config.json",
"meta": { "lastTouchedVersion": "2026.1.1" },
"mode": "local",
"models": { "providers": {} },
"gateway": {
"port": 18789,
"auth": { "mode": "none" },
"controlUi": { "allowedOrigins": ["*"], "allowInsecureAuth": true }
},
"tools": { "profile": "full", "sessions": { "visibility": "all" } }
});
let content = serde_json::to_string_pretty(&default_config)
.map_err(|e| format!("序列化失败: {e}"))?;
std::fs::write(&config_path, content).map_err(|e| format!("写入失败: {e}"))?;
result.insert("created".into(), Value::Bool(true));
result.insert("message".into(), Value::String("配置文件已创建".into()));
Ok(Value::Object(result))
}
#[tauri::command]
pub fn check_installation() -> Result<Value, String> {
let dir = super::openclaw_dir();

View File

@@ -20,6 +20,7 @@ pub fn run() {
config::write_mcp_config,
config::get_version_info,
config::check_installation,
config::init_openclaw_config,
config::check_node,
config::check_node_at_path,
config::scan_node_paths,