forked from kent/consciousness
Move save_agent_log to oneshot.rs for shared Mind/CLI use
Both Mind-run agents (unconscious/subconscious) and CLI-run agents (poc-memory agent run) now use the same logging path. AutoAgent::run() calls save_agent_log automatically at the end. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
271e09adcc
commit
d2dbdedc8f
3 changed files with 67 additions and 56 deletions
|
|
@ -617,7 +617,7 @@ impl Subconscious {
|
|||
|
||||
self.agents[idx].handle = Some(tokio::spawn(async move {
|
||||
let result = auto.run_forked_shared(&forked, &keys, &st, &recent).await;
|
||||
super::unconscious::save_agent_log(&auto.name, &forked).await;
|
||||
crate::agent::oneshot::save_agent_log(&auto.name, &forked).await;
|
||||
(auto, result)
|
||||
}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use std::time::Instant;
|
|||
use std::collections::HashMap;
|
||||
use futures::FutureExt;
|
||||
|
||||
use crate::agent::oneshot::{AutoAgent, AutoStep};
|
||||
use crate::agent::oneshot::{AutoAgent, AutoStep, RunStats};
|
||||
use crate::agent::tools;
|
||||
use crate::subconscious::defs;
|
||||
|
||||
|
|
@ -293,61 +293,11 @@ impl Unconscious {
|
|||
|
||||
self.agents[idx].handle = Some(tokio::spawn(async move {
|
||||
let result = auto.run_shared(&agent).await;
|
||||
let stats = save_agent_log(&auto.name, &agent).await;
|
||||
let stats = crate::agent::oneshot::save_agent_log(&auto.name, &agent).await;
|
||||
auto.steps = orig_steps;
|
||||
(auto, result, stats)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn save_agent_log(name: &str, agent: &std::sync::Arc<crate::agent::Agent>) -> RunStats {
|
||||
let dir = dirs::home_dir().unwrap_or_default()
|
||||
.join(format!(".consciousness/logs/{}", name));
|
||||
let ctx = agent.context.lock().await;
|
||||
let stats = compute_run_stats(ctx.conversation());
|
||||
if std::fs::create_dir_all(&dir).is_ok() {
|
||||
let ts = chrono::Utc::now().format("%Y%m%d-%H%M%S");
|
||||
let path = dir.join(format!("{}.json", ts));
|
||||
let mut context: Vec<&crate::agent::context::AstNode> = Vec::new();
|
||||
for section in ctx.sections() {
|
||||
context.extend(section);
|
||||
}
|
||||
if let Ok(json) = serde_json::to_string_pretty(&context) {
|
||||
let _ = std::fs::write(&path, json);
|
||||
}
|
||||
}
|
||||
dbglog!("[unconscious] {} — {} msgs, {} tool calls",
|
||||
name, stats.messages, stats.tool_calls);
|
||||
stats
|
||||
}
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
pub struct RunStats {
|
||||
pub messages: usize,
|
||||
pub tool_calls: usize,
|
||||
pub tool_calls_by_type: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
fn compute_run_stats(conversation: &[crate::agent::context::AstNode]) -> RunStats {
|
||||
use crate::agent::context::{AstNode, NodeBody};
|
||||
|
||||
let mut messages = 0usize;
|
||||
let mut tool_calls = 0usize;
|
||||
let mut by_type: HashMap<String, usize> = HashMap::new();
|
||||
|
||||
for node in conversation {
|
||||
if let AstNode::Branch { children, .. } = node {
|
||||
messages += 1;
|
||||
for child in children {
|
||||
if let AstNode::Leaf(leaf) = child {
|
||||
if let NodeBody::ToolCall { name, .. } = leaf.body() {
|
||||
tool_calls += 1;
|
||||
*by_type.entry(name.to_string()).or_default() += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RunStats { messages, tool_calls, tool_calls_by_type: by_type }
|
||||
}
|
||||
// save_agent_log and RunStats moved to crate::agent::oneshot
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue