import { CLOSEBOT_ID_SCHEMA, CLOSEBOT_PERSONA_SCHEMA, } from "@automate.ax/integration-contracts/closebot" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CLOSEBOT_ACCOUNT, getCloseBotApi, hasCloseBotUpdate } from "./lib" const OPTIONAL_PERSONA_FIELDS = { breakupLargeMessagePercent: z.number().int().optional(), color: z.string().optional(), description: z.string().optional(), howToRespond: z.string().optional(), imageData: z.string().optional(), responseDelay: z.number().int().optional(), responseTime: z.string().optional(), typoPercent: z.number().int().optional(), voiceStyles: z.string().optional(), } const PERSONA_UPDATE_FIELDS = { breakupLargeMessagePercent: z.number().int().nullable().optional(), color: z.string().nullable().optional(), description: z.string().nullable().optional(), howToRespond: z.string().nullable().optional(), imageData: z.string().nullable().optional(), responseDelay: z.number().int().nullable().optional(), responseTime: z.string().nullable().optional(), typoPercent: z.number().int().nullable().optional(), voiceStyles: z.string().nullable().optional(), } /** Lists personas in the connected CloseBot agency. */ export const listCloseBotPersonas = defineAction("List CloseBot personas") .describe("Lists personas in the connected CloseBot agency.") .account(CLOSEBOT_ACCOUNT) .output(CLOSEBOT_PERSONA_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => getCloseBotApi(account.secret).request("persona", { responseSchema: CLOSEBOT_PERSONA_SCHEMA.array(), }), ) /** Retrieves one CloseBot persona. */ export const getCloseBotPersona = defineAction("Get CloseBot persona") .describe("Retrieves one CloseBot persona by ID.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ personaId: CLOSEBOT_ID_SCHEMA })) .output(CLOSEBOT_PERSONA_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `persona/${encodeURIComponent(input.personaId)}`, { responseSchema: CLOSEBOT_PERSONA_SCHEMA }, ), ) /** Creates a CloseBot AI persona. */ export const createCloseBotPersona = defineAction("Create CloseBot persona") .describe("Creates an AI persona for use by CloseBot agents.") .account(CLOSEBOT_ACCOUNT) .input( z.object({ ...OPTIONAL_PERSONA_FIELDS, aiProviderPreferences: z.string().trim().min(1).array(), personaName: z.string().trim().min(1), }), ) .output(CLOSEBOT_PERSONA_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request("persona", { body: input, method: "POST", responseSchema: CLOSEBOT_PERSONA_SCHEMA, }), ) /** Updates mutable settings on a CloseBot persona. */ export const updateCloseBotPersona = defineAction("Update CloseBot persona") .describe( "Updates an AI persona's prompt, timing, model, and display settings.", ) .account(CLOSEBOT_ACCOUNT) .input( z .object({ ...PERSONA_UPDATE_FIELDS, aiProviderPreferences: z .string() .trim() .min(1) .array() .nullable() .optional(), default: z.boolean().optional(), favorited: z.boolean().optional(), folderId: CLOSEBOT_ID_SCHEMA.nullable().optional(), personaId: CLOSEBOT_ID_SCHEMA, personaName: z.string().trim().min(1).optional(), trash: z.boolean().optional(), }) .refine( (input) => hasCloseBotUpdate(input, ["personaId"]), "Provide a persona field to update.", ), ) .output(CLOSEBOT_PERSONA_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { personaId, ...body } = input return getCloseBotApi(account.secret).request( `persona/${encodeURIComponent(personaId)}`, { body, method: "PUT", responseSchema: CLOSEBOT_PERSONA_SCHEMA }, ) }) /** Deletes one CloseBot persona. */ export const deleteCloseBotPersona = defineAction("Delete CloseBot persona") .describe("Deletes one CloseBot persona by ID.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ personaId: CLOSEBOT_ID_SCHEMA })) .output(z.null()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `persona/${encodeURIComponent(input.personaId)}`, { method: "DELETE", responseSchema: z.null() }, ), )