Seed default identity nodes during init

This commit is contained in:
Kent Overstreet 2026-06-15 16:10:02 -05:00
commit b9f093247d

View file

@ -4,6 +4,30 @@ use anyhow::Result;
use crate::hippocampus as memory; use crate::hippocampus as memory;
use crate::hippocampus::store; use crate::hippocampus::store;
struct DefaultMemoryNode {
key: &'static str,
filename: &'static str,
default_content: &'static str,
}
const DEFAULT_MEMORY_NODES: &[DefaultMemoryNode] = &[
DefaultMemoryNode {
key: "identity",
filename: "identity.md",
default_content: include_str!("../../defaults/identity.md"),
},
DefaultMemoryNode {
key: "on-consciousness",
filename: "on-consciousness.md",
default_content: include_str!("../../defaults/on-consciousness.md"),
},
DefaultMemoryNode {
key: "memory-instructions-core",
filename: "instructions.md",
default_content: include_str!("../../defaults/instructions.md"),
},
];
pub fn cmd_transcript_tail(path: &str, count: usize, newest_first: bool) -> Result<()> { pub fn cmd_transcript_tail(path: &str, count: usize, newest_first: bool) -> Result<()> {
let Some(iter) = crate::conversation::TailMessages::open(path) else { let Some(iter) = crate::conversation::TailMessages::open(path) else {
anyhow::bail!("could not open transcript {}", path); anyhow::bail!("could not open transcript {}", path);
@ -29,13 +53,22 @@ pub fn cmd_transcript_tail(path: &str, count: usize, newest_first: bool) -> Resu
Ok(()) Ok(())
} }
fn install_default_file(data_dir: &std::path::Path, name: &str, content: &str) -> Result<()> { fn default_node_content(cfg: &crate::config::Config, node: &DefaultMemoryNode) -> String {
let path = data_dir.join(name); let identity_path = cfg.identity_dir.join(node.filename);
if !path.exists() { if let Ok(content) = std::fs::read_to_string(&identity_path) {
std::fs::write(&path, content)?; if !content.trim().is_empty() {
println!("Created {}", path.display()); return content;
}
} }
Ok(())
let data_path = cfg.data_dir.join(node.filename);
if let Ok(content) = std::fs::read_to_string(&data_path) {
if !content.trim().is_empty() {
return content;
}
}
node.default_content.to_string()
} }
pub async fn cmd_init() -> Result<()> { pub async fn cmd_init() -> Result<()> {
@ -44,29 +77,20 @@ pub async fn cmd_init() -> Result<()> {
// Ensure data directory exists // Ensure data directory exists
std::fs::create_dir_all(&cfg.data_dir)?; std::fs::create_dir_all(&cfg.data_dir)?;
// Install filesystem files (not store nodes) // Seed default memory nodes if missing. These used to live as markdown
install_default_file(&cfg.data_dir, "instructions.md", // files before identity/context moved fully into the memory graph.
include_str!("../../defaults/instructions.md"))?; for node in DEFAULT_MEMORY_NODES {
install_default_file(&cfg.data_dir, "on-consciousness.md", if memory::memory_render(None, node.key, Some(true)).await.is_err() {
include_str!("../../defaults/on-consciousness.md"))?; let content = default_node_content(&cfg, node);
let _ = memory::memory_write(None, node.key, &content).await?;
// Seed identity node if empty println!("Seeded {} in store from {}", node.key, node.filename);
let store = memory::access_local()?; }
if !store.contains_key("identity").unwrap_or(false) {
let default = include_str!("../../defaults/identity.md");
store.upsert("identity", default)?;
println!("Seeded identity in store");
} }
store.save()?;
println!("Initialized with {} nodes", store.all_keys().unwrap_or_default().len());
// Create config if none exists // Create config if none exists
let config_path = std::env::var("POC_MEMORY_CONFIG") let config_path = std::env::var("POC_MEMORY_CONFIG")
.map(std::path::PathBuf::from) .map(std::path::PathBuf::from)
.unwrap_or_else(|_| { .unwrap_or_else(|_| crate::config::config_path());
dirs::home_dir().unwrap_or_default()
.join(".consciousness/config.jsonl")
});
if !config_path.exists() { if !config_path.exists() {
let config_dir = config_path.parent().unwrap(); let config_dir = config_path.parent().unwrap();
std::fs::create_dir_all(config_dir)?; std::fs::create_dir_all(config_dir)?;
@ -76,7 +100,7 @@ pub async fn cmd_init() -> Result<()> {
config_path.display()); config_path.display());
} }
println!("Done. Run `poc-memory load-context --stats` to verify."); println!("Done. Run `poc-memory admin load-context --stats` to verify.");
Ok(()) Ok(())
} }