Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 84 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dirs = "5"
dotenvy = "0.15"
glob = "0.3"
regex = "1"
rustyline = "14"
rustyline = { version = "17", features = ["with-file-history"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = { package = "serde_yml", version = "0.0.12" }
Expand Down
44 changes: 41 additions & 3 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ use serde_json::{json, Value};

const MAX_ITERATIONS: usize = 12;

/// Statistics collected during command execution
#[derive(Debug, Default, Clone)]
pub struct CommandStats {
pub input_tokens: u64,
pub output_tokens: u64,
pub tool_uses: u64,
}

impl CommandStats {
/// Total tokens used (input + output)
pub fn total_tokens(&self) -> u64 {
self.input_tokens + self.output_tokens
}

/// Merge stats from another source (e.g., subagent)
pub fn merge(&mut self, other: &CommandStats) {
self.input_tokens += other.input_tokens;
self.output_tokens += other.output_tokens;
self.tool_uses += other.tool_uses;
}
}

const SYSTEM_PROMPT: &str = r#"You are an agentic coding assistant running locally.
You can only access files via tools. All paths are relative to the project root.
Use Glob/Grep to find files before Read. Before Edit/Write, explain what you will change.
Expand All @@ -25,7 +47,12 @@ fn verbose(ctx: &Context, message: &str) {
}
}

pub fn run_turn(ctx: &Context, user_input: &str, messages: &mut Vec<Value>) -> Result<()> {
pub fn run_turn(
ctx: &Context,
user_input: &str,
messages: &mut Vec<Value>,
) -> Result<CommandStats> {
let mut stats = CommandStats::default();
let _ = ctx.transcript.borrow_mut().user_message(user_input);

messages.push(json!({
Expand Down Expand Up @@ -159,6 +186,12 @@ pub fn run_turn(ctx: &Context, user_input: &str, messages: &mut Vec<Value>) -> R
client.chat(&request)?
};

// Track token usage from this LLM call
if let Some(usage) = &response.usage {
stats.input_tokens += usage.prompt_tokens;
stats.output_tokens += usage.completion_tokens;
}

if response.choices.is_empty() {
println!("No response from model");
break;
Expand Down Expand Up @@ -204,6 +237,9 @@ pub fn run_turn(ctx: &Context, user_input: &str, messages: &mut Vec<Value>) -> R
let name = &tc.function.name;
let args: Value = serde_json::from_str(&tc.function.arguments).unwrap_or(json!({}));

// Count this tool use
stats.tool_uses += 1;

trace(
ctx,
"CALL",
Expand Down Expand Up @@ -287,7 +323,9 @@ pub fn run_turn(ctx: &Context, user_input: &str, messages: &mut Vec<Value>) -> R
}
} else if name == "Task" {
// Execute Task tool (subagent delegation)
tools::task::execute(args.clone(), ctx)?
let (result, sub_stats) = tools::task::execute(args.clone(), ctx)?;
stats.merge(&sub_stats);
result
} else if name.starts_with("mcp.") {
// Execute MCP tool
let start = std::time::Instant::now();
Expand Down Expand Up @@ -376,5 +414,5 @@ pub fn run_turn(ctx: &Context, user_input: &str, messages: &mut Vec<Value>) -> R
}
}

Ok(())
Ok(stats)
}
Loading