import { AXIOM_ANNOTATION_SCHEMA, AXIOM_EMPTY_SCHEMA, AXIOM_ID_SCHEMA, } from "@automate.ax/integration-contracts/axiom" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT, axiomPath, getAxiomApi, } from "../lib" const ANNOTATION_FIELDS = { datasets: AXIOM_ID_SCHEMA.array().min(1), description: z.string().max(512).optional(), endTime: z.string().optional(), time: z.string().optional(), title: z.string().max(256).optional(), type: z .string() .regex(/^[a-z0-9-]+$/) .max(256), url: z.string().max(512).optional(), } export const listAxiomAnnotations = defineAction("List Axiom annotations") .describe("Lists annotations, optionally filtered by datasets and time.") .account(AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT) .input( z.object({ datasets: AXIOM_ID_SCHEMA.array().optional(), endTime: z.string().optional(), startTime: z.string().optional(), }), ) .output(AXIOM_ANNOTATION_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getAxiomApi(account).request("annotations", { query: { datasets: input.datasets?.join(","), end: input.endTime, start: input.startTime, }, responseSchema: AXIOM_ANNOTATION_SCHEMA.array(), }), ) export const createAxiomAnnotation = defineAction("Create Axiom annotation") .describe("Creates a chart annotation for one or more datasets.") .account(AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT) .input(z.object(ANNOTATION_FIELDS)) .output(AXIOM_ANNOTATION_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getAxiomApi(account).request("annotations", { body: input, method: "POST", responseSchema: AXIOM_ANNOTATION_SCHEMA, }), ) export const getAxiomAnnotation = defineAction("Get Axiom annotation") .describe("Retrieves one Axiom annotation by ID.") .account(AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT) .input(z.object({ annotationId: AXIOM_ID_SCHEMA })) .output(AXIOM_ANNOTATION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getAxiomApi(account).request( `annotations/${axiomPath(input.annotationId)}`, { responseSchema: AXIOM_ANNOTATION_SCHEMA }, ), ) export const updateAxiomAnnotation = defineAction("Update Axiom annotation") .describe("Updates mutable fields on one Axiom annotation.") .account(AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT) .input( z .object({ annotationId: AXIOM_ID_SCHEMA, datasets: ANNOTATION_FIELDS.datasets.optional(), description: ANNOTATION_FIELDS.description, endTime: ANNOTATION_FIELDS.endTime, time: ANNOTATION_FIELDS.time, title: ANNOTATION_FIELDS.title, type: ANNOTATION_FIELDS.type.optional(), url: ANNOTATION_FIELDS.url, }) .refine( ({ annotationId: _annotationId, ...fields }) => Object.keys(fields).length > 0, { message: "Provide at least one annotation field to update." }, ), ) .output(AXIOM_ANNOTATION_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { annotationId, ...body } }) => getAxiomApi(account).request(`annotations/${axiomPath(annotationId)}`, { body, method: "PUT", responseSchema: AXIOM_ANNOTATION_SCHEMA, }), ) export const deleteAxiomAnnotation = defineAction("Delete Axiom annotation") .describe("Deletes one Axiom annotation.") .account(AXIOM_ACCOUNT, AXIOM_API_TOKEN_ACCOUNT) .input(z.object({ annotationId: AXIOM_ID_SCHEMA })) .output(AXIOM_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getAxiomApi(account).request( `annotations/${axiomPath(input.annotationId)}`, { method: "DELETE", responseSchema: AXIOM_EMPTY_SCHEMA }, ), )