Skip to main content

thoughts_tool/utils/
logging.rs

1//! Shared tool-call logging for thoughts_tool.
2//!
3//! This module provides logging helpers used by both MCP and agentic-tools
4//! wrappers to ensure consistent logging behavior.
5
6use crate::documents::active_logs_dir;
7use agentic_logging::{CallTimer, LogWriter, ToolCallRecord};
8
9/// Log a tool call. Behavior identical to MCP.
10///
11/// If logging is unavailable (e.g., no active branch), this function
12/// returns silently without panicking or affecting the caller.
13pub fn log_tool_call(
14    timer: &CallTimer,
15    tool: &str,
16    request: serde_json::Value,
17    success: bool,
18    error: Option<String>,
19    summary: Option<serde_json::Value>,
20) {
21    let writer = match active_logs_dir() {
22        Ok(dir) => LogWriter::new(dir),
23        Err(_) => return, // Logging unavailable (e.g., branch lockout)
24    };
25
26    let (completed_at, duration_ms) = timer.finish();
27    let record = ToolCallRecord {
28        call_id: timer.call_id.clone(),
29        server: "thoughts_tool".into(),
30        tool: tool.into(),
31        started_at: timer.started_at,
32        completed_at,
33        duration_ms,
34        request,
35        response_file: None,
36        success,
37        error,
38        model: None,
39        token_usage: None,
40        summary,
41    };
42
43    if let Err(e) = writer.append_jsonl(&record) {
44        tracing::warn!("Failed to append JSONL log: {}", e);
45    }
46}