use std::path::Path;
use crate::config::Config;
use crate::context::cache::SessionContext;
use crate::economy::agent_tracker;
use crate::session::{self, CurrentSession};
pub fn run(tool: &str, bytes: &str) -> i32 {
run_with_dir(tool, bytes, &session::sessions_dir())
}
pub fn run_with_dir(tool: &str, bytes: &str, sessions_dir: &Path) -> i32 {
let tokens = bytes.parse::<u64>().unwrap_or(0) / 4;
let mut current = match CurrentSession::load(sessions_dir) {
Some(s) => s,
None => return 0, };
current.total_tokens += tokens;
current.total_calls += 1;
current.save(sessions_dir);
let event = format!(
"{{\"type\":\"tool\",\"tool\":\"{}\",\"tokens_est\":{},\"ts\":{}}}",
crate::json_util::escape_str(tool),
tokens,
session::unix_now(),
);
session::append_event(sessions_dir, ¤t.session_file, &event);
let cfg = Config::load();
let mut ctx = SessionContext::load(sessions_dir);
if agent_tracker::is_agent_tool(tool) {
ctx.note_agent_spawn(tool, cfg.agent_spawn_cost);
}
if tokens > 0 {
ctx.note_burn(tokens);
ctx.note_tool_tokens(tool, tokens);
}
ctx.save(sessions_dir);
0
}