incident-tools.ts63 lines · main
| 1 | import { tool } from 'ai' |
| 2 | import { IS_PLATFORM } from 'common' |
| 3 | import { z } from 'zod' |
| 4 | |
| 5 | import type { IncidentInfo } from '@/lib/api/incident-status' |
| 6 | |
| 7 | /** |
| 8 | * Creates incident-related tools for the AI assistant. |
| 9 | * |
| 10 | * @param baseUrl - The base URL for API requests (e.g., https://supabase.com/dashboard) |
| 11 | * This should be the public URL to leverage CDN caching. |
| 12 | */ |
| 13 | export const getIncidentTools = ({ baseUrl }: { baseUrl: string }) => ({ |
| 14 | get_active_incidents: tool({ |
| 15 | description: |
| 16 | 'Check for active incidents. Use this tool when the user reports issues with any Briven service, including the database, authentication, realtime, storage, and functions. Possible problems include, but are not limited to, connection issues, timeouts, service unavailability, authentication failures, or unexpected errors.', |
| 17 | inputSchema: z.object({}), |
| 18 | execute: async () => { |
| 19 | if (!IS_PLATFORM) { |
| 20 | return { |
| 21 | incidents: [], |
| 22 | message: 'Incident checking is only available on Briven platform.', |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | try { |
| 27 | const response = await fetch(`${baseUrl}/api/incident-status`, { |
| 28 | signal: AbortSignal.timeout(5_000), |
| 29 | }) |
| 30 | |
| 31 | if (!response.ok) { |
| 32 | console.warn('Failed to fetch incident status:', response.status) |
| 33 | return { incidents: [], error: 'Unable to check incident status at this time.' } |
| 34 | } |
| 35 | |
| 36 | const incidents: IncidentInfo[] = await response.json() |
| 37 | |
| 38 | if (incidents.length === 0) { |
| 39 | return { |
| 40 | incidents: [], |
| 41 | message: |
| 42 | 'No active incidents. The issue the user is experiencing is likely not related to a Briven infrastructure problem.', |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const incidentSummaries = incidents.map((incident) => ({ |
| 47 | name: incident.name, |
| 48 | status: incident.status, |
| 49 | impact: incident.impact, |
| 50 | active_since: incident.active_since, |
| 51 | })) |
| 52 | |
| 53 | return { |
| 54 | incidents: incidentSummaries, |
| 55 | message: `There ${incidents.length === 1 ? 'is' : 'are'} ${incidents.length} active incident${incidents.length === 1 ? '' : 's'} on Briven infrastructure. If the user's issue appears related, inform them about the ongoing incident(s) and direct them to https://status.supabase.com for real-time updates.`, |
| 56 | } |
| 57 | } catch (error) { |
| 58 | console.warn('Failed to fetch incident status:', error) |
| 59 | return { incidents: [], error: 'Unable to check incident status at this time.' } |
| 60 | } |
| 61 | }, |
| 62 | }), |
| 63 | }) |