1// Probe a list of service health endpoints, retrying flaky responses, and
2// open one incident ticket listing everything that is down.
3
4type HealthCheck = {
5 name: string;
6 url: string;
7 isHealthy: (body: unknown) => boolean;
8};
9
10export const input = z.object({
11 open_ticket_when_down: z
12 .boolean()
13 .default(true)
14 .describe("Open an incident ticket when any endpoint is down"),
15});
16
17export const domains = {
18 "slack-status.com": "read",
19 "status.openai.com": "read",
20 "status.claude.com": "read",
21};
22
23const isObject = (value: unknown): value is Record<string, unknown> =>
24 typeof value === "object" && value !== null;
25
26const getNestedString = (value: unknown, path: string[]): string | null => {
27 let current: unknown = value;
28 for (const key of path) {
29 if (!isObject(current) || typeof current[key] === "undefined") {
30 return null;
31 }
32 current = current[key];
33 }
34 return typeof current === "string" ? current : null;
35};
36
37// One entry per service: a display name, its health URL, and the predicate
38// that determines whether the JSON response reports a healthy state.
39const ENDPOINTS: HealthCheck[] = [
40 {
41 name: "Slack",
42 url: "https://slack-status.com/api/v2.0.0/current",
43 // Slack's Status API reports "ok" when all services are normal;
44 // "active" means an incident is currently ongoing.
45 isHealthy: (body) => getNestedString(body, ["status"]) === "ok",
46 },
47 {
48 name: "OpenAI",
49 url: "https://status.openai.com/api/v2/summary.json",
50 isHealthy: (body) => getNestedString(body, ["status", "indicator"]) === "none",
51 },
52 {
53 name: "Anthropic",
54 url: "https://status.claude.com/api/v2/summary.json",
55 isHealthy: (body) => getNestedString(body, ["status", "indicator"]) === "none",
56 },
57];
58
59// Transient failures are retried before an endpoint counts as down.
60const MAX_ATTEMPTS = 3;
61
62export default async function run(params: z.infer<typeof input>) {
63 // **Probe each endpoint**
64 const down: string[] = [];
65 for (const endpoint of ENDPOINTS) {
66 let healthy = false;
67 for (let attempt = 1; attempt <= MAX_ATTEMPTS && !healthy; attempt++) {
68 const response = await http.get(endpoint.url);
69 healthy = response.status_code === 200 && endpoint.isHealthy(response.body);
70 }
71 if (!healthy) {
72 down.push(endpoint.name);
73 }
74 }
75
76 // **Summarize results**
77 if (down.length === 0) {
78 return `All ${ENDPOINTS.length} services are healthy.`;
79 }
80 const summary = `${down.length} of ${ENDPOINTS.length} services are down: ${down.join(", ")}.`;
81 if (!params.open_ticket_when_down) {
82 return summary;
83 }
84
85 // **Open an incident ticket**
86 const ticket = await risotto.create_ticket({
87 title: `Service health alert: ${down.join(", ")} down`,
88 description:
89 `${summary} Each endpoint was retried up to ${MAX_ATTEMPTS} times ` +
90 "before being reported as down.",
91 });
92 return `${summary} Opened incident ticket ${ticket.ticket_id}.`;
93}
63 // **Probe each endpoint**
64 const down: string[] = [];
65 for (const endpoint of ENDPOINTS) {
66 let healthy = false;
67 for (let attempt = 1; attempt <= MAX_ATTEMPTS && !healthy; attempt++) {
68 const response = await http.get(endpoint.url);
69 healthy = response.status_code === 200 && endpoint.isHealthy(response.body);
70 }
71 if (!healthy) {
72 down.push(endpoint.name);
73 }
74 }
76 // **Summarize results**
77 if (down.length === 0) {
78 return `All ${ENDPOINTS.length} services are healthy.`;
79 }
80 const summary = `${down.length} of ${ENDPOINTS.length} services are down: ${down.join(", ")}.`;
81 if (!params.open_ticket_when_down) {
82 return summary;
83 }
85 // **Open an incident ticket**
86 const ticket = await risotto.create_ticket({
87 title: `Service health alert: ${down.join(", ")} down`,
88 description:
89 `${summary} Each endpoint was retried up to ${MAX_ATTEMPTS} times ` +
90 "before being reported as down.",
91 });
92 return `${summary} Opened incident ticket ${ticket.ticket_id}.`;