Config now derives serde::Deserialize with #[serde(default)] for all fields. Path fields use custom deserialize_path/deserialize_path_opt for ~ expansion. ContextGroup and ContextSource also derive Deserialize. try_load_shared() is now 20 lines instead of 100: json5 → serde → Config directly, then resolve API settings from the model/backend cross-reference. Removes MemoryConfigRaw intermediate struct entirely. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
289 lines
10 KiB
Rust
289 lines
10 KiB
Rust
// Configuration for poc-memory
|
|
//
|
|
// Primary config: ~/.config/poc-agent/config.json5 (shared with poc-agent)
|
|
// Memory-specific settings live in the "memory" section.
|
|
// API backend resolved from the shared "models" + backend configs.
|
|
//
|
|
// Fallback: ~/.config/poc-memory/config.jsonl (legacy, still supported)
|
|
// Env override: POC_MEMORY_CONFIG
|
|
//
|
|
// The shared config eliminates API credential duplication between
|
|
// poc-memory and poc-agent.
|
|
|
|
use std::path::PathBuf;
|
|
use std::sync::{Arc, OnceLock, RwLock};
|
|
|
|
static CONFIG: OnceLock<RwLock<Arc<Config>>> = OnceLock::new();
|
|
|
|
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum ContextSource {
|
|
#[serde(alias = "")]
|
|
Store,
|
|
File,
|
|
Journal,
|
|
}
|
|
|
|
#[derive(Debug, Clone, serde::Deserialize)]
|
|
pub struct ContextGroup {
|
|
pub label: String,
|
|
#[serde(default)]
|
|
pub keys: Vec<String>,
|
|
#[serde(default)]
|
|
pub source: ContextSource,
|
|
}
|
|
|
|
impl Default for ContextSource {
|
|
fn default() -> Self { Self::Store }
|
|
}
|
|
|
|
#[derive(Debug, Clone, serde::Deserialize)]
|
|
#[serde(default)]
|
|
pub struct Config {
|
|
pub user_name: String,
|
|
pub assistant_name: String,
|
|
#[serde(deserialize_with = "deserialize_path")]
|
|
pub data_dir: PathBuf,
|
|
#[serde(deserialize_with = "deserialize_path")]
|
|
pub projects_dir: PathBuf,
|
|
pub core_nodes: Vec<String>,
|
|
pub journal_days: u32,
|
|
pub journal_max: usize,
|
|
pub context_groups: Vec<ContextGroup>,
|
|
pub llm_concurrency: usize,
|
|
pub agent_budget: usize,
|
|
#[serde(deserialize_with = "deserialize_path")]
|
|
pub prompts_dir: PathBuf,
|
|
#[serde(default, deserialize_with = "deserialize_path_opt")]
|
|
pub agent_config_dir: Option<PathBuf>,
|
|
/// Resolved from agent_model → models → backend (not in config directly)
|
|
#[serde(skip)]
|
|
pub api_base_url: Option<String>,
|
|
#[serde(skip)]
|
|
pub api_key: Option<String>,
|
|
#[serde(skip)]
|
|
pub api_model: Option<String>,
|
|
/// Used to resolve API settings, not stored on Config
|
|
#[serde(default)]
|
|
agent_model: Option<String>,
|
|
pub api_reasoning: String,
|
|
pub agent_types: Vec<String>,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
let home = PathBuf::from(std::env::var("HOME").expect("HOME not set"));
|
|
Self {
|
|
user_name: "User".to_string(),
|
|
assistant_name: "Assistant".to_string(),
|
|
data_dir: home.join(".claude/memory"),
|
|
projects_dir: home.join(".claude/projects"),
|
|
core_nodes: vec!["identity".to_string(), "core-practices".to_string()],
|
|
journal_days: 7,
|
|
journal_max: 20,
|
|
context_groups: vec![
|
|
ContextGroup {
|
|
label: "identity".into(),
|
|
keys: vec!["identity".into()],
|
|
source: ContextSource::Store,
|
|
},
|
|
ContextGroup {
|
|
label: "core-practices".into(),
|
|
keys: vec!["core-practices".into()],
|
|
source: ContextSource::Store,
|
|
},
|
|
],
|
|
llm_concurrency: 1,
|
|
agent_budget: 1000,
|
|
prompts_dir: home.join("poc/memory/prompts"),
|
|
agent_config_dir: None,
|
|
api_base_url: None,
|
|
api_key: None,
|
|
api_model: None,
|
|
agent_model: None,
|
|
api_reasoning: "high".to_string(),
|
|
agent_types: vec![
|
|
"linker".into(), "organize".into(), "distill".into(),
|
|
"separator".into(), "split".into(),
|
|
],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
fn load_from_file() -> Self {
|
|
// Try shared config first, then legacy JSONL
|
|
if let Some(config) = Self::try_load_shared() {
|
|
return config;
|
|
}
|
|
Self::load_legacy_jsonl()
|
|
}
|
|
|
|
/// Load from shared poc-agent config (~/.config/poc-agent/config.json5).
|
|
/// Memory settings live in the "memory" section; API settings are
|
|
/// resolved from the shared model/backend configuration.
|
|
fn try_load_shared() -> Option<Self> {
|
|
let path = PathBuf::from(std::env::var("HOME").ok()?)
|
|
.join(".config/poc-agent/config.json5");
|
|
let content = std::fs::read_to_string(&path).ok()?;
|
|
let root: serde_json::Value = json5::from_str(&content).ok()?;
|
|
let mem_value = root.get("memory")?;
|
|
|
|
let mut config: Config = serde_json::from_value(mem_value.clone()).ok()?;
|
|
config.llm_concurrency = config.llm_concurrency.max(1);
|
|
|
|
// Resolve API settings: agent_model → models → backend
|
|
if let Some(model_name) = &config.agent_model {
|
|
if let Some(model_cfg) = root.get("models").and_then(|m| m.get(model_name.as_str())) {
|
|
let backend_name = model_cfg.get("backend").and_then(|v| v.as_str()).unwrap_or("");
|
|
let model_id = model_cfg.get("model_id").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
if let Some(backend) = root.get(backend_name) {
|
|
config.api_base_url = backend.get("base_url")
|
|
.and_then(|v| v.as_str()).map(String::from);
|
|
config.api_key = backend.get("api_key")
|
|
.and_then(|v| v.as_str()).map(String::from);
|
|
}
|
|
config.api_model = Some(model_id.to_string());
|
|
}
|
|
}
|
|
|
|
Some(config)
|
|
}
|
|
|
|
/// Load from legacy JSONL config (~/.config/poc-memory/config.jsonl).
|
|
fn load_legacy_jsonl() -> Self {
|
|
let path = std::env::var("POC_MEMORY_CONFIG")
|
|
.map(PathBuf::from)
|
|
.unwrap_or_else(|_| {
|
|
PathBuf::from(std::env::var("HOME").expect("HOME not set"))
|
|
.join(".config/poc-memory/config.jsonl")
|
|
});
|
|
|
|
let mut config = Config::default();
|
|
|
|
let Ok(content) = std::fs::read_to_string(&path) else {
|
|
return config;
|
|
};
|
|
|
|
let mut context_groups: Vec<ContextGroup> = Vec::new();
|
|
|
|
let stream = serde_json::Deserializer::from_str(&content)
|
|
.into_iter::<serde_json::Value>();
|
|
|
|
for result in stream {
|
|
let Ok(obj) = result else { continue };
|
|
|
|
if let Some(cfg) = obj.get("config") {
|
|
if let Some(s) = cfg.get("user_name").and_then(|v| v.as_str()) {
|
|
config.user_name = s.to_string();
|
|
}
|
|
if let Some(s) = cfg.get("assistant_name").and_then(|v| v.as_str()) {
|
|
config.assistant_name = s.to_string();
|
|
}
|
|
if let Some(s) = cfg.get("data_dir").and_then(|v| v.as_str()) {
|
|
config.data_dir = expand_home(s);
|
|
}
|
|
if let Some(s) = cfg.get("projects_dir").and_then(|v| v.as_str()) {
|
|
config.projects_dir = expand_home(s);
|
|
}
|
|
if let Some(arr) = cfg.get("core_nodes").and_then(|v| v.as_array()) {
|
|
config.core_nodes = arr.iter()
|
|
.filter_map(|v| v.as_str().map(|s| s.to_string()))
|
|
.collect();
|
|
}
|
|
if let Some(d) = cfg.get("journal_days").and_then(|v| v.as_u64()) {
|
|
config.journal_days = d as u32;
|
|
}
|
|
if let Some(m) = cfg.get("journal_max").and_then(|v| v.as_u64()) {
|
|
config.journal_max = m as usize;
|
|
}
|
|
if let Some(n) = cfg.get("llm_concurrency").and_then(|v| v.as_u64()) {
|
|
config.llm_concurrency = n.max(1) as usize;
|
|
}
|
|
if let Some(n) = cfg.get("agent_budget").and_then(|v| v.as_u64()) {
|
|
config.agent_budget = n as usize;
|
|
}
|
|
if let Some(s) = cfg.get("prompts_dir").and_then(|v| v.as_str()) {
|
|
config.prompts_dir = expand_home(s);
|
|
}
|
|
if let Some(s) = cfg.get("agent_config_dir").and_then(|v| v.as_str()) {
|
|
config.agent_config_dir = Some(expand_home(s));
|
|
}
|
|
if let Some(s) = cfg.get("api_base_url").and_then(|v| v.as_str()) {
|
|
config.api_base_url = Some(s.to_string());
|
|
}
|
|
if let Some(s) = cfg.get("api_key").and_then(|v| v.as_str()) {
|
|
config.api_key = Some(s.to_string());
|
|
}
|
|
if let Some(s) = cfg.get("api_model").and_then(|v| v.as_str()) {
|
|
config.api_model = Some(s.to_string());
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if let Some(label) = obj.get("group").and_then(|v| v.as_str()) {
|
|
let keys = obj.get("keys")
|
|
.and_then(|v| v.as_array())
|
|
.map(|arr| arr.iter()
|
|
.filter_map(|v| v.as_str().map(|s| s.to_string()))
|
|
.collect())
|
|
.unwrap_or_default();
|
|
|
|
let source = match obj.get("source").and_then(|v| v.as_str()) {
|
|
Some("file") => ContextSource::File,
|
|
Some("journal") => ContextSource::Journal,
|
|
_ => ContextSource::Store,
|
|
};
|
|
|
|
context_groups.push(ContextGroup { label: label.to_string(), keys, source });
|
|
}
|
|
}
|
|
|
|
if !context_groups.is_empty() {
|
|
config.context_groups = context_groups;
|
|
}
|
|
|
|
config
|
|
}
|
|
}
|
|
|
|
|
|
fn deserialize_path<'de, D: serde::Deserializer<'de>>(d: D) -> Result<PathBuf, D::Error> {
|
|
let s: String = serde::Deserialize::deserialize(d)?;
|
|
Ok(expand_home(&s))
|
|
}
|
|
|
|
fn deserialize_path_opt<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<PathBuf>, D::Error> {
|
|
let s: Option<String> = serde::Deserialize::deserialize(d)?;
|
|
Ok(s.map(|s| expand_home(&s)))
|
|
}
|
|
|
|
fn expand_home(path: &str) -> PathBuf {
|
|
if let Some(rest) = path.strip_prefix("~/") {
|
|
PathBuf::from(std::env::var("HOME").expect("HOME not set")).join(rest)
|
|
} else {
|
|
PathBuf::from(path)
|
|
}
|
|
}
|
|
|
|
/// Get the global config (cheap Arc clone).
|
|
pub fn get() -> Arc<Config> {
|
|
CONFIG
|
|
.get_or_init(|| RwLock::new(Arc::new(Config::load_from_file())))
|
|
.read()
|
|
.unwrap()
|
|
.clone()
|
|
}
|
|
|
|
/// Reload the config from disk. Returns true if changed.
|
|
pub fn reload() -> bool {
|
|
let lock = CONFIG.get_or_init(|| RwLock::new(Arc::new(Config::load_from_file())));
|
|
let new = Config::load_from_file();
|
|
let mut current = lock.write().unwrap();
|
|
let changed = format!("{:?}", **current) != format!("{:?}", new);
|
|
if changed {
|
|
*current = Arc::new(new);
|
|
}
|
|
changed
|
|
}
|