refactor tool call logging: send as .md file per tool, remove tool_log accumulation

This commit is contained in:
Fam Zheng
2026-04-11 12:17:01 +01:00
parent b55ed0127c
commit 688387dac3
2 changed files with 14 additions and 29 deletions

View File

@@ -43,7 +43,6 @@ pub struct TelegramOutput {
use_draft: bool,
last_edit: Instant,
http: reqwest::Client,
tool_log: Vec<String>,
}
impl TelegramOutput {
@@ -56,7 +55,6 @@ impl TelegramOutput {
use_draft: is_private,
last_edit: Instant::now(),
http: reqwest::Client::new(),
tool_log: Vec::new(),
}
}
}
@@ -120,28 +118,10 @@ impl Output for TelegramOutput {
text,
)
.await;
// send tool call log as .md file if any
if !self.tool_log.is_empty() {
let md = self.tool_log.join("\n");
// extract tool names for filename
let names: Vec<&str> = self.tool_log.iter()
.filter_map(|s| s.strip_prefix('[')?.split('(').next())
.collect();
let label = if names.is_empty() { "tools".to_string() } else { names.join("_") };
let tmp = format!("/tmp/{label}.md");
if std::fs::write(&tmp, &md).is_ok() {
let input_file = InputFile::file(std::path::Path::new(&tmp));
let _ = self.bot.send_document(self.chat_id, input_file).await;
let _ = std::fs::remove_file(&tmp);
}
self.tool_log.clear();
}
Ok(())
}
async fn status(&mut self, text: &str) -> Result<()> {
self.tool_log.push(text.to_string());
async fn status(&mut self, _text: &str) -> Result<()> {
Ok(())
}

View File

@@ -193,19 +193,24 @@ pub async fn run_openai_with_tools(
for tc in &tool_calls {
info!(tool = %tc.name, "executing tool call");
let args_preview = truncate_at_char_boundary(&tc.arguments, 200);
let _ = output
.status(&format!("### `{}`\n```json\n{args_preview}\n```", tc.name))
.await;
let result =
execute_tool(&tc.name, &tc.arguments, state, output, sid, config, chat_id)
.await;
let result_preview = truncate_at_char_boundary(&result, 500);
let _ = output
.status(&format!("**Result** ({} bytes)\n```\n{result_preview}\n```\n---", result.len()))
.await;
// send tool call details as a .md file named after the tool
let md = format!(
"## {}\n\n### Arguments\n```json\n{}\n```\n\n### Result ({} bytes)\n```\n{}\n```\n",
tc.name,
&tc.arguments,
result.len(),
truncate_at_char_boundary(&result, 4000),
);
let tmp = format!("/tmp/{}.md", tc.name);
if std::fs::write(&tmp, &md).is_ok() {
let _ = output.send_file(std::path::Path::new(&tmp), "").await;
let _ = std::fs::remove_file(&tmp);
}
messages.push(serde_json::json!({
"role": "tool",