Improved response logging

This commit is contained in:
Kent Overstreet 2026-04-09 17:05:24 -04:00
parent 8a2f488d22
commit 7dd9daa2b9

View file

@ -607,19 +607,18 @@ impl ResponseParser {
super::api::StreamToken::Done { usage } => { super::api::StreamToken::Done { usage } => {
if let Some(ref mut f) = log_file { if let Some(ref mut f) = log_file {
use std::io::Write; use std::io::Write;
let tc_count = full_text.matches("<tool_call>").count(); let ctx = agent.context.lock().await;
let ctx_tokens = agent.context.lock().await.tokens(); let children = ctx.conversation().get(parser.branch_idx)
let _ = writeln!(f, "done: {} chars, {} <tool_call> tags, ctx: {} tokens", .map(|n| n.children()).unwrap_or(&[]);
full_text.len(), tc_count, ctx_tokens); let n_think = children.iter().filter(|c| matches!(c.leaf().map(|l| l.body()), Some(NodeBody::Thinking(_)))).count();
if tc_count == 0 && full_text.len() > 0 { let n_content = children.iter().filter(|c| matches!(c.leaf().map(|l| l.body()), Some(NodeBody::Content(_)))).count();
let n_tool = children.iter().filter(|c| matches!(c.leaf().map(|l| l.body()), Some(NodeBody::ToolCall { .. }))).count();
let _ = writeln!(f, "done: {} chars, {} content + {} think + {} tool_call, ctx: {} tokens",
full_text.len(), n_content, n_think, n_tool, ctx.tokens());
drop(ctx);
if full_text.len() > 0 && n_content == 0 && n_tool == 0 {
let end = full_text.floor_char_boundary(full_text.len().min(2000)); let end = full_text.floor_char_boundary(full_text.len().min(2000));
let _ = writeln!(f, "full text:\n{}", &full_text[..end]); let _ = writeln!(f, " unparsed text: {}", &full_text[..end]);
}
for (i, part) in full_text.split("<tool_call>").enumerate() {
if i > 0 {
let end = part.floor_char_boundary(part.len().min(200));
let _ = writeln!(f, "tool_call body: {}...", &part[..end]);
}
} }
} }
if let Some(u) = usage { if let Some(u) = usage {