2026-03-05 15:41:35 -05:00
|
|
|
// Configuration for poc-memory
|
|
|
|
|
//
|
2026-03-05 16:08:15 -05:00
|
|
|
// Loaded from ~/.config/poc-memory/config.jsonl (or POC_MEMORY_CONFIG env).
|
2026-03-05 15:41:35 -05:00
|
|
|
// Falls back to sensible defaults if no config file exists.
|
2026-03-05 16:08:15 -05:00
|
|
|
//
|
|
|
|
|
// Format: JSONL — one JSON object per line.
|
|
|
|
|
// First line with "config" key: global settings.
|
|
|
|
|
// Lines with "group" key: context loading groups (order preserved).
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
// {"config": {"user_name": "Alice", "data_dir": "~/.claude/memory"}}
|
|
|
|
|
// {"group": "identity", "keys": ["identity.md"]}
|
|
|
|
|
// {"group": "orientation", "keys": ["where-am-i.md"], "source": "file"}
|
2026-03-05 15:41:35 -05:00
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
|
|
|
|
|
static CONFIG: OnceLock<Config> = OnceLock::new();
|
|
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum ContextSource {
|
|
|
|
|
Store,
|
|
|
|
|
File,
|
|
|
|
|
Journal,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:54:44 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ContextGroup {
|
|
|
|
|
pub label: String,
|
|
|
|
|
pub keys: Vec<String>,
|
2026-03-05 16:08:15 -05:00
|
|
|
pub source: ContextSource,
|
2026-03-05 15:54:44 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:41:35 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Config {
|
|
|
|
|
/// Display name for the human user in transcripts/prompts.
|
|
|
|
|
pub user_name: String,
|
|
|
|
|
/// Display name for the AI assistant.
|
|
|
|
|
pub assistant_name: String,
|
|
|
|
|
/// Base directory for memory data (store, logs, status).
|
|
|
|
|
pub data_dir: PathBuf,
|
|
|
|
|
/// Directory containing Claude session transcripts.
|
|
|
|
|
pub projects_dir: PathBuf,
|
|
|
|
|
/// Core node keys that should never be decayed/deleted.
|
|
|
|
|
pub core_nodes: Vec<String>,
|
2026-03-05 15:54:44 -05:00
|
|
|
/// How many days of journal to include in load-context.
|
|
|
|
|
pub journal_days: u32,
|
|
|
|
|
/// Max journal entries to include in load-context.
|
|
|
|
|
pub journal_max: usize,
|
|
|
|
|
/// Ordered context groups for session-start loading.
|
|
|
|
|
pub context_groups: Vec<ContextGroup>,
|
2026-03-05 22:28:39 -05:00
|
|
|
/// Separate API key for background agent work (daemon jobs).
|
|
|
|
|
/// If set, passed as ANTHROPIC_API_KEY to model calls.
|
|
|
|
|
pub agent_api_key: Option<String>,
|
2026-03-05 15:41:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.md".to_string()],
|
2026-03-05 15:54:44 -05:00
|
|
|
journal_days: 7,
|
|
|
|
|
journal_max: 20,
|
|
|
|
|
context_groups: vec![
|
2026-03-05 16:08:15 -05:00
|
|
|
ContextGroup {
|
|
|
|
|
label: "identity".into(),
|
|
|
|
|
keys: vec!["identity.md".into()],
|
|
|
|
|
source: ContextSource::Store,
|
|
|
|
|
},
|
2026-03-05 15:54:44 -05:00
|
|
|
],
|
2026-03-05 22:28:39 -05:00
|
|
|
agent_api_key: None,
|
2026-03-05 15:41:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
|
fn load_from_file() -> 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"))
|
2026-03-05 16:08:15 -05:00
|
|
|
.join(".config/poc-memory/config.jsonl")
|
2026-03-05 15:41:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mut config = Config::default();
|
|
|
|
|
|
|
|
|
|
let Ok(content) = std::fs::read_to_string(&path) else {
|
|
|
|
|
return config;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-05 15:54:44 -05:00
|
|
|
let mut context_groups: Vec<ContextGroup> = Vec::new();
|
|
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
// Parse as a stream of JSON values (handles multi-line objects)
|
|
|
|
|
let stream = serde_json::Deserializer::from_str(&content)
|
|
|
|
|
.into_iter::<serde_json::Value>();
|
2026-03-05 15:54:44 -05:00
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
for result in stream {
|
|
|
|
|
let Ok(obj) = result else { continue };
|
2026-03-05 15:54:44 -05:00
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
// Global config line
|
|
|
|
|
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();
|
2026-03-05 15:54:44 -05:00
|
|
|
}
|
2026-03-05 16:08:15 -05:00
|
|
|
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()))
|
2026-03-05 15:41:35 -05:00
|
|
|
.collect();
|
|
|
|
|
}
|
2026-03-05 16:08:15 -05:00
|
|
|
if let Some(d) = cfg.get("journal_days").and_then(|v| v.as_u64()) {
|
|
|
|
|
config.journal_days = d as u32;
|
2026-03-05 15:54:44 -05:00
|
|
|
}
|
2026-03-05 16:08:15 -05:00
|
|
|
if let Some(m) = cfg.get("journal_max").and_then(|v| v.as_u64()) {
|
|
|
|
|
config.journal_max = m as usize;
|
2026-03-05 15:54:44 -05:00
|
|
|
}
|
2026-03-05 22:28:39 -05:00
|
|
|
if let Some(s) = cfg.get("agent_api_key").and_then(|v| v.as_str()) {
|
|
|
|
|
config.agent_api_key = Some(s.to_string());
|
|
|
|
|
}
|
2026-03-05 16:08:15 -05:00
|
|
|
continue;
|
2026-03-05 15:41:35 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
// Context group line
|
|
|
|
|
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 });
|
|
|
|
|
}
|
2026-03-05 15:54:44 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-05 16:08:15 -05:00
|
|
|
if !context_groups.is_empty() {
|
2026-03-05 15:54:44 -05:00
|
|
|
config.context_groups = context_groups;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:41:35 -05:00
|
|
|
config
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 (loaded once on first access).
|
|
|
|
|
pub fn get() -> &'static Config {
|
|
|
|
|
CONFIG.get_or_init(Config::load_from_file)
|
|
|
|
|
}
|