1
0

fix memory

This commit is contained in:
2026-03-01 18:18:16 +09:00
parent 059ccdc5c6
commit eca2bdce77
5 changed files with 134 additions and 62 deletions

View File

@@ -4,7 +4,7 @@ use serde_json::json;
use std::fs;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::core::{config, reader};
use crate::core::config;
fn generate_tid() -> String {
// ATProto TID: 64-bit integer as 13 base32-sortable chars
@@ -23,18 +23,11 @@ fn generate_tid() -> String {
tid
}
fn next_version() -> u64 {
match reader::read_memory() {
Ok(Some(record)) => record["value"]["version"].as_u64().unwrap_or(0) + 1,
_ => 1,
}
}
/// Save a single memory element as a new TID file
pub fn save_memory(content: &str) -> Result<()> {
let cfg = config::load();
let did = cfg.did.clone().unwrap_or_else(|| "self".to_string());
let tid = generate_tid();
let version = next_version();
let now = Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
let record = json!({
@@ -46,7 +39,6 @@ pub fn save_memory(content: &str) -> Result<()> {
"$type": "ai.syui.gpt.memory#markdown",
"text": content
},
"version": version,
"createdAt": now
}
});
@@ -59,3 +51,62 @@ pub fn save_memory(content: &str) -> Result<()> {
fs::write(&path, json_str)
.with_context(|| format!("Failed to write {}", path.display()))
}
/// Delete all memory files, then write new ones from the given items
pub fn compress_memory(items: &[String]) -> Result<()> {
let cfg = config::load();
let did = cfg.did.clone().unwrap_or_else(|| "self".to_string());
let dir = config::collection_dir(&cfg, "ai.syui.gpt.memory");
// delete all existing memory files
if dir.exists() {
for entry in fs::read_dir(&dir)? {
let entry = entry?;
if entry.path().extension().is_some_and(|ext| ext == "json") {
fs::remove_file(entry.path())?;
}
}
}
fs::create_dir_all(&dir)
.with_context(|| format!("Failed to create {}", dir.display()))?;
// write each item as a new TID file
for item in items {
let micros = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros() as u64;
// small delay to ensure unique TIDs
std::thread::sleep(std::time::Duration::from_micros(1));
let v = (micros << 10) & 0x7FFFFFFFFFFFFFFF;
const CHARSET: &[u8] = b"234567abcdefghijklmnopqrstuvwxyz";
let mut tid = String::with_capacity(13);
for i in (0..13).rev() {
let idx = ((v >> (i * 5)) & 0x1f) as usize;
tid.push(CHARSET[idx] as char);
}
let now = Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
let record = json!({
"uri": format!("at://{}/ai.syui.gpt.memory/{}", did, tid),
"value": {
"$type": "ai.syui.gpt.memory",
"did": did,
"content": {
"$type": "ai.syui.gpt.memory#markdown",
"text": item
},
"createdAt": now
}
});
let path = dir.join(format!("{}.json", tid));
let json_str = serde_json::to_string_pretty(&record)?;
fs::write(&path, json_str)
.with_context(|| format!("Failed to write {}", path.display()))?;
}
Ok(())
}