mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-05-21 04:40:18 +08:00
8e219b5eb1
Implement full OMO Slim profile management to align with ai-toolbox: - Backend: Slim service methods, DAO, Tauri commands, plugin conflict handling - Frontend: types, API, query hooks, form integration with isSlim parameterization - Slim variant: 6 agents (no categories), separate config file and plugin name - Mutual exclusion: standard OMO and Slim cannot coexist as plugins - i18n: zh/en/ja translations for all Slim agent descriptions
205 lines
6.0 KiB
Rust
205 lines
6.0 KiB
Rust
use crate::config::write_json_file;
|
|
use crate::error::AppError;
|
|
use crate::provider::OpenCodeProviderConfig;
|
|
use crate::settings::get_opencode_override_dir;
|
|
use indexmap::IndexMap;
|
|
use serde_json::{json, Map, Value};
|
|
use std::path::PathBuf;
|
|
|
|
pub fn get_opencode_dir() -> PathBuf {
|
|
if let Some(override_dir) = get_opencode_override_dir() {
|
|
return override_dir;
|
|
}
|
|
|
|
dirs::home_dir()
|
|
.map(|h| h.join(".config").join("opencode"))
|
|
.unwrap_or_else(|| PathBuf::from(".config").join("opencode"))
|
|
}
|
|
|
|
pub fn get_opencode_config_path() -> PathBuf {
|
|
get_opencode_dir().join("opencode.json")
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn get_opencode_env_path() -> PathBuf {
|
|
get_opencode_dir().join(".env")
|
|
}
|
|
|
|
pub fn read_opencode_config() -> Result<Value, AppError> {
|
|
let path = get_opencode_config_path();
|
|
|
|
if !path.exists() {
|
|
return Ok(json!({
|
|
"$schema": "https://opencode.ai/config.json"
|
|
}));
|
|
}
|
|
|
|
let content = std::fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
|
serde_json::from_str(&content).map_err(|e| AppError::json(&path, e))
|
|
}
|
|
|
|
pub fn write_opencode_config(config: &Value) -> Result<(), AppError> {
|
|
let path = get_opencode_config_path();
|
|
write_json_file(&path, config)?;
|
|
|
|
log::debug!("OpenCode config written to {path:?}");
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_providers() -> Result<Map<String, Value>, AppError> {
|
|
let config = read_opencode_config()?;
|
|
Ok(config
|
|
.get("provider")
|
|
.and_then(|v| v.as_object())
|
|
.cloned()
|
|
.unwrap_or_default())
|
|
}
|
|
|
|
pub fn set_provider(id: &str, config: Value) -> Result<(), AppError> {
|
|
let mut full_config = read_opencode_config()?;
|
|
|
|
if full_config.get("provider").is_none() {
|
|
full_config["provider"] = json!({});
|
|
}
|
|
|
|
if let Some(providers) = full_config
|
|
.get_mut("provider")
|
|
.and_then(|v| v.as_object_mut())
|
|
{
|
|
providers.insert(id.to_string(), config);
|
|
}
|
|
|
|
write_opencode_config(&full_config)
|
|
}
|
|
|
|
pub fn remove_provider(id: &str) -> Result<(), AppError> {
|
|
let mut config = read_opencode_config()?;
|
|
|
|
if let Some(providers) = config.get_mut("provider").and_then(|v| v.as_object_mut()) {
|
|
providers.remove(id);
|
|
}
|
|
|
|
write_opencode_config(&config)
|
|
}
|
|
|
|
pub fn get_typed_providers() -> Result<IndexMap<String, OpenCodeProviderConfig>, AppError> {
|
|
let providers = get_providers()?;
|
|
let mut result = IndexMap::new();
|
|
|
|
for (id, value) in providers {
|
|
match serde_json::from_value::<OpenCodeProviderConfig>(value.clone()) {
|
|
Ok(config) => {
|
|
result.insert(id, config);
|
|
}
|
|
Err(e) => {
|
|
log::warn!("Failed to parse provider '{id}': {e}");
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
pub fn set_typed_provider(id: &str, config: &OpenCodeProviderConfig) -> Result<(), AppError> {
|
|
let value = serde_json::to_value(config).map_err(|e| AppError::JsonSerialize { source: e })?;
|
|
set_provider(id, value)
|
|
}
|
|
|
|
pub fn get_mcp_servers() -> Result<Map<String, Value>, AppError> {
|
|
let config = read_opencode_config()?;
|
|
Ok(config
|
|
.get("mcp")
|
|
.and_then(|v| v.as_object())
|
|
.cloned()
|
|
.unwrap_or_default())
|
|
}
|
|
|
|
pub fn set_mcp_server(id: &str, config: Value) -> Result<(), AppError> {
|
|
let mut full_config = read_opencode_config()?;
|
|
|
|
if full_config.get("mcp").is_none() {
|
|
full_config["mcp"] = json!({});
|
|
}
|
|
|
|
if let Some(mcp) = full_config.get_mut("mcp").and_then(|v| v.as_object_mut()) {
|
|
mcp.insert(id.to_string(), config);
|
|
}
|
|
|
|
write_opencode_config(&full_config)
|
|
}
|
|
|
|
pub fn remove_mcp_server(id: &str) -> Result<(), AppError> {
|
|
let mut config = read_opencode_config()?;
|
|
|
|
if let Some(mcp) = config.get_mut("mcp").and_then(|v| v.as_object_mut()) {
|
|
mcp.remove(id);
|
|
}
|
|
|
|
write_opencode_config(&config)
|
|
}
|
|
|
|
pub fn add_plugin(plugin_name: &str) -> Result<(), AppError> {
|
|
let mut config = read_opencode_config()?;
|
|
|
|
let plugins = config.get_mut("plugin").and_then(|v| v.as_array_mut());
|
|
|
|
match plugins {
|
|
Some(arr) => {
|
|
// Mutual exclusion: standard OMO and OMO Slim cannot coexist as plugins
|
|
if plugin_name.starts_with("oh-my-opencode")
|
|
&& !plugin_name.starts_with("oh-my-opencode-slim")
|
|
{
|
|
// Adding standard OMO -> remove all Slim variants
|
|
arr.retain(|v| {
|
|
v.as_str()
|
|
.map(|s| !s.starts_with("oh-my-opencode-slim"))
|
|
.unwrap_or(true)
|
|
});
|
|
} else if plugin_name.starts_with("oh-my-opencode-slim") {
|
|
// Adding Slim -> remove all standard OMO variants (but keep slim)
|
|
arr.retain(|v| {
|
|
v.as_str()
|
|
.map(|s| {
|
|
!s.starts_with("oh-my-opencode") || s.starts_with("oh-my-opencode-slim")
|
|
})
|
|
.unwrap_or(true)
|
|
});
|
|
}
|
|
|
|
let already_exists = arr.iter().any(|v| v.as_str() == Some(plugin_name));
|
|
if !already_exists {
|
|
arr.push(Value::String(plugin_name.to_string()));
|
|
}
|
|
}
|
|
None => {
|
|
config["plugin"] = json!([plugin_name]);
|
|
}
|
|
}
|
|
|
|
write_opencode_config(&config)
|
|
}
|
|
|
|
pub fn remove_plugin_by_prefix(prefix: &str) -> Result<(), AppError> {
|
|
let mut config = read_opencode_config()?;
|
|
|
|
if let Some(arr) = config.get_mut("plugin").and_then(|v| v.as_array_mut()) {
|
|
arr.retain(|v| {
|
|
v.as_str()
|
|
.map(|s| {
|
|
if !s.starts_with(prefix) {
|
|
return true; // Keep: doesn't match prefix at all
|
|
}
|
|
let rest = &s[prefix.len()..];
|
|
rest.starts_with('-')
|
|
})
|
|
.unwrap_or(true)
|
|
});
|
|
|
|
if arr.is_empty() {
|
|
config.as_object_mut().map(|obj| obj.remove("plugin"));
|
|
}
|
|
}
|
|
|
|
write_opencode_config(&config)
|
|
}
|