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