Memory: byte-budgeted index, memory_search (BM25), auto extraction on compaction/exit
This commit is contained in:
1 parent
59bf45f41d
commit
8705b128ea
8 files changed
+311
-39
No files matched your search
@@ -313,6 +313,31 @@ impl Agent {
|
||||
self.compact(KEEP_MESSAGES).await
|
||||
}
|
||||
|
||||
pub async fn extract_memory(&mut self, transcript: &str) -> Vec<String> {
|
||||
let home = crate::context::hx_home();
|
||||
let req = crate::memory::extract_request(&self.model, transcript, &home, &self.cx.cwd);
|
||||
let Ok(mut s) = self.provider.stream(req).await else {
|
||||
return vec![];
|
||||
};
|
||||
let mut reply = String::new();
|
||||
while let Some(Ok(ev)) = s.next().await {
|
||||
if let StreamEvent::Done { message, usage, .. } = ev {
|
||||
self.usage.add(&usage);
|
||||
reply = message.text();
|
||||
}
|
||||
}
|
||||
crate::memory::apply_extracted(&reply, &home, &self.cx.cwd)
|
||||
}
|
||||
|
||||
pub async fn extract_memory_now(&mut self) -> Vec<String> {
|
||||
let msgs = self.session.messages();
|
||||
if msgs.len() < 4 {
|
||||
return vec![];
|
||||
}
|
||||
self.extract_memory(&crate::compact::transcript(&msgs))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn compact(&mut self, keep: usize) -> Result<bool, Error> {
|
||||
let Some(plan) = compact::plan(&self.session.messages(), keep) else {
|
||||
return Ok(false);
|
||||
@@ -326,6 +351,11 @@ impl Agent {
|
||||
}
|
||||
}
|
||||
let summary = summary.ok_or(Error::Truncated)?;
|
||||
let saved = self.extract_memory(plan.transcript()).await;
|
||||
if !saved.is_empty() {
|
||||
self.emit(Event::Info(format!("memory saved: {}", saved.join(", "))))
|
||||
.await;
|
||||
}
|
||||
self.session.rebase(plan.apply(&summary))?;
|
||||
self.window.reset();
|
||||
self.publish();
|
||||
@@ -1263,6 +1293,7 @@ mod tests {
|
||||
asst(vec![tu("2", "b")]),
|
||||
text("fin"),
|
||||
text("SUMMARY"),
|
||||
text("[]"),
|
||||
text("next"),
|
||||
]),
|
||||
Mutex::new(vec![]),
|
||||
@@ -1358,7 +1389,13 @@ mod tests {
|
||||
async fn compaction() {
|
||||
let text = |t: &str| asst(vec![ContentBlock::Text { text: t.into() }]);
|
||||
let script = Arc::new(Script(
|
||||
Mutex::new(vec![text("r1"), text("r2"), text("SUMMARY"), text("r3")]),
|
||||
Mutex::new(vec![
|
||||
text("r1"),
|
||||
text("r2"),
|
||||
text("SUMMARY"),
|
||||
text("[]"),
|
||||
text("r3"),
|
||||
]),
|
||||
Mutex::new(vec![]),
|
||||
));
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -1393,7 +1430,7 @@ mod tests {
|
||||
async fn replacing_the_session_drops_stale_context_usage() {
|
||||
let text = |t: &str| asst(vec![ContentBlock::Text { text: t.into() }]);
|
||||
let script = Arc::new(Script(
|
||||
Mutex::new(vec![text("r1"), text("SUMMARY"), text("r2")]),
|
||||
Mutex::new(vec![text("r1"), text("SUMMARY"), text("[]"), text("r2")]),
|
||||
Mutex::new(vec![]),
|
||||
));
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user