mirror of
https://github.com/aljazceru/goose.git
synced 2025-12-18 14:44:21 +01:00
Co-authored-by: Michael Neale <michael.neale@gmail.com> Co-authored-by: Wendy Tang <wendytang@squareup.com> Co-authored-by: Jarrod Sibbison <72240382+jsibbison-square@users.noreply.github.com> Co-authored-by: Alex Hancock <alex.hancock@example.com> Co-authored-by: Alex Hancock <alexhancock@block.xyz> Co-authored-by: Lifei Zhou <lifei@squareup.com> Co-authored-by: Wes <141185334+wesrblock@users.noreply.github.com> Co-authored-by: Max Novich <maksymstepanenko1990@gmail.com> Co-authored-by: Zaki Ali <zaki@squareup.com> Co-authored-by: Salman Mohammed <smohammed@squareup.com> Co-authored-by: Kalvin C <kalvinnchau@users.noreply.github.com> Co-authored-by: Alec Thomas <alec@swapoff.org> Co-authored-by: lily-de <119957291+lily-de@users.noreply.github.com> Co-authored-by: kalvinnchau <kalvin@block.xyz> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Rizel Scarlett <rizel@squareup.com> Co-authored-by: bwrage <bwrage@squareup.com> Co-authored-by: Kalvin Chau <kalvin@squareup.com> Co-authored-by: Alice Hau <110418948+ahau-square@users.noreply.github.com> Co-authored-by: Alistair Gray <ajgray@stripe.com> Co-authored-by: Nahiyan Khan <nahiyan.khan@gmail.com> Co-authored-by: Alex Hancock <alexhancock@squareup.com> Co-authored-by: Nahiyan Khan <nahiyan@squareup.com> Co-authored-by: marcelle <1852848+laanak08@users.noreply.github.com> Co-authored-by: Yingjie He <yingjiehe@block.xyz> Co-authored-by: Yingjie He <yingjiehe@squareup.com> Co-authored-by: Lily Delalande <ldelalande@block.xyz> Co-authored-by: Adewale Abati <acekyd01@gmail.com> Co-authored-by: Ebony Louis <ebony774@gmail.com> Co-authored-by: Angie Jones <jones.angie@gmail.com> Co-authored-by: Ebony Louis <55366651+EbonyLouis@users.noreply.github.com>
119 lines
3.9 KiB
Rust
119 lines
3.9 KiB
Rust
use super::base::Config;
|
|
use crate::agents::ExtensionConfig;
|
|
use anyhow::Result;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashMap;
|
|
|
|
const DEFAULT_EXTENSION: &str = "developer";
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
pub struct ExtensionEntry {
|
|
pub enabled: bool,
|
|
#[serde(flatten)]
|
|
pub config: ExtensionConfig,
|
|
}
|
|
|
|
/// Extension configuration management
|
|
pub struct ExtensionManager;
|
|
|
|
impl ExtensionManager {
|
|
/// Get the extension configuration if enabled
|
|
pub fn get_config(name: &str) -> Result<Option<ExtensionConfig>> {
|
|
let config = Config::global();
|
|
|
|
// Try to get the extension entry
|
|
let extensions: HashMap<String, ExtensionEntry> = match config.get("extensions") {
|
|
Ok(exts) => exts,
|
|
Err(super::ConfigError::NotFound(_)) => {
|
|
// Initialize with default developer extension
|
|
let defaults = HashMap::from([(
|
|
DEFAULT_EXTENSION.to_string(),
|
|
ExtensionEntry {
|
|
enabled: true,
|
|
config: ExtensionConfig::Builtin {
|
|
name: DEFAULT_EXTENSION.to_string(),
|
|
},
|
|
},
|
|
)]);
|
|
config.set("extensions", serde_json::to_value(&defaults)?)?;
|
|
defaults
|
|
}
|
|
Err(e) => return Err(e.into()),
|
|
};
|
|
|
|
Ok(extensions.get(name).and_then(|entry| {
|
|
if entry.enabled {
|
|
Some(entry.config.clone())
|
|
} else {
|
|
None
|
|
}
|
|
}))
|
|
}
|
|
|
|
/// Set or update an extension configuration
|
|
pub fn set(entry: ExtensionEntry) -> Result<()> {
|
|
let config = Config::global();
|
|
|
|
let mut extensions: HashMap<String, ExtensionEntry> =
|
|
config.get("extensions").unwrap_or_else(|_| HashMap::new());
|
|
|
|
extensions.insert(entry.config.name().parse()?, entry);
|
|
config.set("extensions", serde_json::to_value(extensions)?)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Remove an extension configuration
|
|
pub fn remove(name: &str) -> Result<()> {
|
|
let config = Config::global();
|
|
|
|
let mut extensions: HashMap<String, ExtensionEntry> =
|
|
config.get("extensions").unwrap_or_else(|_| HashMap::new());
|
|
|
|
extensions.remove(name);
|
|
config.set("extensions", serde_json::to_value(extensions)?)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Enable or disable an extension
|
|
pub fn set_enabled(name: &str, enabled: bool) -> Result<()> {
|
|
let config = Config::global();
|
|
|
|
let mut extensions: HashMap<String, ExtensionEntry> =
|
|
config.get("extensions").unwrap_or_else(|_| HashMap::new());
|
|
|
|
if let Some(entry) = extensions.get_mut(name) {
|
|
entry.enabled = enabled;
|
|
config.set("extensions", serde_json::to_value(extensions)?)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Get all extensions and their configurations
|
|
pub fn get_all() -> Result<Vec<ExtensionEntry>> {
|
|
let config = Config::global();
|
|
let extensions: HashMap<String, ExtensionEntry> =
|
|
config.get("extensions").unwrap_or_default();
|
|
Ok(Vec::from_iter(extensions.values().cloned()))
|
|
}
|
|
|
|
/// Get all extension names
|
|
pub fn get_all_names() -> Result<Vec<String>> {
|
|
let config = Config::global();
|
|
Ok(config
|
|
.get("extensions")
|
|
.unwrap_or_else(|_| get_keys(Default::default())))
|
|
}
|
|
|
|
/// Check if an extension is enabled
|
|
pub fn is_enabled(name: &str) -> Result<bool> {
|
|
let config = Config::global();
|
|
let extensions: HashMap<String, ExtensionEntry> =
|
|
config.get("extensions").unwrap_or_else(|_| HashMap::new());
|
|
|
|
Ok(extensions.get(name).map(|e| e.enabled).unwrap_or(false))
|
|
}
|
|
}
|
|
fn get_keys(entries: HashMap<String, ExtensionEntry>) -> Vec<String> {
|
|
entries.into_keys().collect()
|
|
}
|