From 688387dac3ee33a3365a371ef4d7810dcb5a543c Mon Sep 17 00:00:00 2001 From: Fam Zheng Date: Sat, 11 Apr 2026 12:17:01 +0100 Subject: [PATCH] refactor tool call logging: send as .md file per tool, remove tool_log accumulation --- src/output.rs | 22 +--------------------- src/stream.rs | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/output.rs b/src/output.rs index 4fe0a09..4ea5303 100644 --- a/src/output.rs +++ b/src/output.rs @@ -43,7 +43,6 @@ pub struct TelegramOutput { use_draft: bool, last_edit: Instant, http: reqwest::Client, - tool_log: Vec, } 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(()) } diff --git a/src/stream.rs b/src/stream.rs index ca54ead..fd431d9 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -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",