feature/pi-refactor
 1/**
 2 * Tool logging wrapper
 3 * Wraps tools to add execution logging
 4 */
 5
 6import type { AgentTool } from "@mariozechner/pi-agent-core";
 7
 8/**
 9 * Wrap a tool with logging
10 */
11export function withLogging(tool: AgentTool, debug: boolean = false): AgentTool {
12  if (!debug) {
13    return tool; // No wrapping if debug disabled
14  }
15
16  return {
17    ...tool,
18    execute: async (toolCallId, params, signal, onUpdate) => {
19      const startTime = Date.now();
20      
21      console.log(`[Tool:${tool.name}] Starting execution`);
22      console.log(`[Tool:${tool.name}] Parameters:`, JSON.stringify(params, null, 2));
23      
24      try {
25        const result = await tool.execute(toolCallId, params, signal, onUpdate);
26        
27        const elapsed = Date.now() - startTime;
28        const resultText = result.content
29          .filter((c: any) => c.type === "text")
30          .map((c: any) => c.text)
31          .join(" ");
32        
33        console.log(`[Tool:${tool.name}] Completed in ${elapsed}ms`);
34        console.log(`[Tool:${tool.name}] Result preview: ${resultText.slice(0, 100)}${resultText.length > 100 ? "..." : ""}`);
35        
36        if (result.details) {
37          console.log(`[Tool:${tool.name}] Details:`, JSON.stringify(result.details, null, 2));
38        }
39        
40        return result;
41      } catch (error) {
42        const elapsed = Date.now() - startTime;
43        console.error(`[Tool:${tool.name}] Failed after ${elapsed}ms:`, error);
44        throw error;
45      }
46    },
47  };
48}