File size: 4,753 Bytes
c6aaf95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a43827
 
 
 
 
 
c6aaf95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { SupabaseClient } from '@supabase/supabase-js';
import { logger } from '../utils/logger.js';

export interface ToolExecution {
  tool_name: string;
  tool_display_name: string;
  execution_order: number;
  input_data: any;
  output_data: any;
  execution_time_ms: number;
  status: 'success' | 'error' | 'skipped';
  error_message?: string;
}

export class ToolExecutionTrackerService {
  private supabaseClient: SupabaseClient;
  private currentMessageId: string | null = null;
  private executionOrder: number = 0;

  constructor(supabaseClient: SupabaseClient) {
    this.supabaseClient = supabaseClient;
  }

  /**
   * Start tracking for a new message
   */
  startTracking(messageId: string): void {
    this.currentMessageId = messageId;
    this.executionOrder = 0;
  }

  /**
   * Track a tool execution
   */
  async trackToolExecution(
    sessionId: string,
    execution: ToolExecution
  ): Promise<void> {
    try {
      if (!this.currentMessageId) {
        logger.warn('No message ID set for tool execution tracking');
        return;
      }

      const executionData = {
        session_id: sessionId,
        message_id: this.currentMessageId,
        tool_name: execution.tool_name,
        tool_display_name: execution.tool_display_name,
        execution_order: execution.execution_order,
        input_data: execution.input_data,
        output_data: execution.output_data,
        execution_time_ms: execution.execution_time_ms,
        status: execution.status,
        error_message: execution.error_message || null,
        created_at: new Date().toISOString()
      };

      const { error } = await this.supabaseClient
        .from('tool_executions')
        .insert(executionData);

      if (error) {
        logger.error({ error }, 'Failed to track tool execution');
        // Don't throw - tracking failure shouldn't break the workflow
      } else {
        logger.info(`[REPORT] ✓ Tool execution saved to database: ${execution.tool_display_name} (${execution.tool_name}, order: ${execution.execution_order}, time: ${execution.execution_time_ms}ms)`);
        logger.debug(`[REPORT] Tool execution data: ${JSON.stringify({
          tool: execution.tool_name,
          input: Object.keys(execution.input_data || {}),
          output_keys: Object.keys(execution.output_data || {})
        })}`);
      }
    } catch (error) {
      logger.error({ error }, 'Error tracking tool execution');
      // Don't throw - tracking is non-critical
    }
  }

  /**
   * Get next execution order number
   */
  getNextExecutionOrder(): number {
    this.executionOrder++;
    return this.executionOrder;
  }

  /**
   * Get all tool executions for a message
   */
  async getToolExecutionsForMessage(messageId: string): Promise<ToolExecution[]> {
    try {
      const { data, error } = await this.supabaseClient
        .from('tool_executions')
        .select('*')
        .eq('message_id', messageId)
        .order('execution_order', { ascending: true });

      if (error) {
        logger.error({ error }, 'Failed to get tool executions');
        return [];
      }

      return (data || []).map(row => ({
        tool_name: row.tool_name,
        tool_display_name: row.tool_display_name,
        execution_order: row.execution_order,
        input_data: row.input_data,
        output_data: row.output_data,
        execution_time_ms: row.execution_time_ms,
        status: row.status,
        error_message: row.error_message
      }));
    } catch (error) {
      logger.error({ error }, 'Error getting tool executions');
      return [];
    }
  }

  /**
   * Get all tool executions for a session
   */
  async getToolExecutionsForSession(sessionId: string): Promise<ToolExecution[]> {
    try {
      const { data, error } = await this.supabaseClient
        .from('tool_executions')
        .select('*')
        .eq('session_id', sessionId)
        .order('created_at', { ascending: true })
        .order('execution_order', { ascending: true });

      if (error) {
        logger.error({ error }, 'Failed to get tool executions for session');
        return [];
      }

      return (data || []).map(row => ({
        tool_name: row.tool_name,
        tool_display_name: row.tool_display_name,
        execution_order: row.execution_order,
        input_data: row.input_data,
        output_data: row.output_data,
        execution_time_ms: row.execution_time_ms,
        status: row.status,
        error_message: row.error_message
      }));
    } catch (error) {
      logger.error({ error }, 'Error getting tool executions for session');
      return [];
    }
  }

  /**
   * Reset tracking state
   */
  reset(): void {
    this.currentMessageId = null;
    this.executionOrder = 0;
  }
}