/ hooks / useIdeLogging.ts
useIdeLogging.ts
 1  import { useEffect } from 'react'
 2  import { logEvent } from 'src/services/analytics/index.js'
 3  import { z } from 'zod/v4'
 4  import type { MCPServerConnection } from '../services/mcp/types.js'
 5  import { getConnectedIdeClient } from '../utils/ide.js'
 6  import { lazySchema } from '../utils/lazySchema.js'
 7  
 8  const LogEventSchema = lazySchema(() =>
 9    z.object({
10      method: z.literal('log_event'),
11      params: z.object({
12        eventName: z.string(),
13        eventData: z.object({}).passthrough(),
14      }),
15    }),
16  )
17  
18  export function useIdeLogging(mcpClients: MCPServerConnection[]): void {
19    useEffect(() => {
20      // Skip if there are no clients
21      if (!mcpClients.length) {
22        return
23      }
24  
25      // Find the IDE client from the MCP clients list
26      const ideClient = getConnectedIdeClient(mcpClients)
27      if (ideClient) {
28        // Register the log event handler
29        ideClient.client.setNotificationHandler(
30          LogEventSchema(),
31          notification => {
32            const { eventName, eventData } = notification.params
33            logEvent(
34              `tengu_ide_${eventName}`,
35              eventData as { [key: string]: boolean | number | undefined },
36            )
37          },
38        )
39      }
40    }, [mcpClients])
41  }