|
| 1 | +import { ExtendedTool, ToolHandlers } from '../../utils/types' |
| 2 | +import { client, v1 } from '@datadog/datadog-api-client' |
| 3 | +import { createToolSchema } from '../../utils/tool' |
| 4 | +import { |
| 5 | + ListDowntimesZodSchema, |
| 6 | + ScheduleDowntimeZodSchema, |
| 7 | + CancelDowntimeZodSchema, |
| 8 | +} from './schema' |
| 9 | + |
| 10 | +type DowntimesToolName = |
| 11 | + | 'list_downtimes' |
| 12 | + | 'schedule_downtime' |
| 13 | + | 'cancel_downtime' |
| 14 | +type DowntimesTool = ExtendedTool<DowntimesToolName> |
| 15 | + |
| 16 | +export const DOWNTIMES_TOOLS: DowntimesTool[] = [ |
| 17 | + createToolSchema( |
| 18 | + ListDowntimesZodSchema, |
| 19 | + 'list_downtimes', |
| 20 | + 'List scheduled downtimes from Datadog', |
| 21 | + ), |
| 22 | + createToolSchema( |
| 23 | + ScheduleDowntimeZodSchema, |
| 24 | + 'schedule_downtime', |
| 25 | + 'Schedule a downtime in Datadog', |
| 26 | + ), |
| 27 | + createToolSchema( |
| 28 | + CancelDowntimeZodSchema, |
| 29 | + 'cancel_downtime', |
| 30 | + 'Cancel a scheduled downtime in Datadog', |
| 31 | + ), |
| 32 | +] as const |
| 33 | + |
| 34 | +type DowntimesToolHandlers = ToolHandlers<DowntimesToolName> |
| 35 | + |
| 36 | +export const createDowntimesToolHandlers = ( |
| 37 | + config: client.Configuration, |
| 38 | +): DowntimesToolHandlers => { |
| 39 | + const apiInstance = new v1.DowntimesApi(config) |
| 40 | + |
| 41 | + return { |
| 42 | + list_downtimes: async (request) => { |
| 43 | + const { currentOnly, monitorId } = ListDowntimesZodSchema.parse( |
| 44 | + request.params.arguments, |
| 45 | + ) |
| 46 | + |
| 47 | + const res = await apiInstance.listDowntimes({ |
| 48 | + currentOnly, |
| 49 | + monitorId, |
| 50 | + }) |
| 51 | + |
| 52 | + return { |
| 53 | + content: [ |
| 54 | + { |
| 55 | + type: 'text', |
| 56 | + text: `Listed downtimes:\n${JSON.stringify(res, null, 2)}`, |
| 57 | + }, |
| 58 | + ], |
| 59 | + } |
| 60 | + }, |
| 61 | + |
| 62 | + schedule_downtime: async (request) => { |
| 63 | + const params = ScheduleDowntimeZodSchema.parse(request.params.arguments) |
| 64 | + |
| 65 | + // Convert to the format expected by Datadog client |
| 66 | + const downtimeData: v1.Downtime = { |
| 67 | + scope: params.scope, |
| 68 | + start: params.start, |
| 69 | + end: params.end, |
| 70 | + message: params.message, |
| 71 | + timezone: params.timezone, |
| 72 | + monitorId: params.monitorId, |
| 73 | + monitor_tags: params.monitorTags, |
| 74 | + } |
| 75 | + |
| 76 | + // Add recurrence configuration if provided |
| 77 | + if (params.recurrence) { |
| 78 | + downtimeData.recurrence = { |
| 79 | + type: params.recurrence.type, |
| 80 | + period: params.recurrence.period, |
| 81 | + week_days: params.recurrence.weekDays, |
| 82 | + until: params.recurrence.until, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const res = await apiInstance.createDowntime({ |
| 87 | + body: downtimeData, |
| 88 | + }) |
| 89 | + |
| 90 | + return { |
| 91 | + content: [ |
| 92 | + { |
| 93 | + type: 'text', |
| 94 | + text: `Scheduled downtime: ${JSON.stringify(res, null, 2)}`, |
| 95 | + }, |
| 96 | + ], |
| 97 | + } |
| 98 | + }, |
| 99 | + |
| 100 | + cancel_downtime: async (request) => { |
| 101 | + const { downtimeId } = CancelDowntimeZodSchema.parse( |
| 102 | + request.params.arguments, |
| 103 | + ) |
| 104 | + |
| 105 | + await apiInstance.cancelDowntime({ |
| 106 | + downtimeId, |
| 107 | + }) |
| 108 | + |
| 109 | + return { |
| 110 | + content: [ |
| 111 | + { |
| 112 | + type: 'text', |
| 113 | + text: `Cancelled downtime with ID: ${downtimeId}`, |
| 114 | + }, |
| 115 | + ], |
| 116 | + } |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
0 commit comments