import { CLOSEBOT_ID_SCHEMA, CLOSEBOT_LIBRARY_FILE_CREATED_SCHEMA, CLOSEBOT_LIBRARY_FILE_PREVIEW_SCHEMA, CLOSEBOT_LIBRARY_FILE_SCHEMA, CLOSEBOT_WEB_SCRAPE_CREATED_SCHEMA, CLOSEBOT_WEB_SCRAPE_PAGE_SCHEMA, } from "@automate.ax/integration-contracts/closebot" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CLOSEBOT_ACCOUNT, getCloseBotApi } from "./lib" /** Lists files in the connected CloseBot agency library. */ export const listCloseBotLibraryFiles = defineAction( "List CloseBot library files", ) .describe("Lists uploaded, web-scraped, and synchronized library files.") .account(CLOSEBOT_ACCOUNT) .output(CLOSEBOT_LIBRARY_FILE_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => getCloseBotApi(account.secret).request("library/files", { responseSchema: CLOSEBOT_LIBRARY_FILE_SCHEMA.array(), }), ) /** Retrieves metadata for one CloseBot library file. */ export const getCloseBotLibraryFile = defineAction("Get CloseBot library file") .describe("Retrieves metadata for one library file.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA })) .output(CLOSEBOT_LIBRARY_FILE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}`, { responseSchema: CLOSEBOT_LIBRARY_FILE_SCHEMA }, ), ) /** Retrieves the extracted text preview for one CloseBot library file. */ export const getCloseBotLibraryFilePreview = defineAction( "Get CloseBot library file preview", ) .describe("Retrieves extracted text and chunks for one library file.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA })) .output(CLOSEBOT_LIBRARY_FILE_PREVIEW_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}/preview`, { responseSchema: CLOSEBOT_LIBRARY_FILE_PREVIEW_SCHEMA }, ), ) /** Uploads a file to the connected CloseBot agency library. */ export const uploadCloseBotLibraryFile = defineAction( "Upload CloseBot library file", ) .describe("Uploads a file to the agency library for later source attachment.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ file: z.instanceof(File) })) .output(CLOSEBOT_LIBRARY_FILE_CREATED_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const body = new FormData() body.set("file", input.file, input.file.name) return getCloseBotApi(account.secret).request("library/files", { body, method: "POST", responseSchema: CLOSEBOT_LIBRARY_FILE_CREATED_SCHEMA, }) }) /** Replaces the content of one CloseBot library file. */ export const replaceCloseBotLibraryFile = defineAction( "Replace CloseBot library file", ) .describe("Replaces one library file while retaining its file ID.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ file: z.instanceof(File), fileId: CLOSEBOT_ID_SCHEMA })) .output(CLOSEBOT_LIBRARY_FILE_CREATED_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const body = new FormData() body.set("newFile", input.file, input.file.name) return getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}`, { body, method: "PUT", responseSchema: CLOSEBOT_LIBRARY_FILE_CREATED_SCHEMA, }, ) }) /** Deletes one CloseBot library file. */ export const deleteCloseBotLibraryFile = defineAction( "Delete CloseBot library file", ) .describe("Deletes one file from the agency library.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA })) .output(z.null()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}`, { method: "DELETE", responseSchema: z.null() }, ), ) /** Attaches a CloseBot library file to a source. */ export const attachCloseBotLibraryFileToSource = defineAction( "Attach CloseBot library file to source", ) .describe("Attaches one library file to a source's bot knowledge.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA, sourceId: CLOSEBOT_ID_SCHEMA })) .output(z.null()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}/source/${encodeURIComponent(input.sourceId)}`, { method: "POST", responseSchema: z.null() }, ), ) /** Detaches a CloseBot library file from a source. */ export const detachCloseBotLibraryFileFromSource = defineAction( "Detach CloseBot library file from source", ) .describe("Detaches one library file from a source's bot knowledge.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA, sourceId: CLOSEBOT_ID_SCHEMA })) .output(z.null()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}/source/${encodeURIComponent(input.sourceId)}`, { method: "DELETE", responseSchema: z.null() }, ), ) /** Lists pages discovered for a CloseBot web-scrape file. */ export const listCloseBotWebScrapePages = defineAction( "List CloseBot web-scrape pages", ) .describe("Lists discovered pages and their inclusion status.") .account(CLOSEBOT_ACCOUNT) .input(z.object({ fileId: CLOSEBOT_ID_SCHEMA })) .output(CLOSEBOT_WEB_SCRAPE_PAGE_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}/scrape-pages`, { responseSchema: CLOSEBOT_WEB_SCRAPE_PAGE_SCHEMA.array() }, ), ) /** Updates the included pages for a CloseBot web-scrape file. */ export const updateCloseBotWebScrapePages = defineAction( "Update CloseBot web-scrape pages", ) .describe("Updates which discovered pages feed bot knowledge.") .account(CLOSEBOT_ACCOUNT) .input( z.object({ fileId: CLOSEBOT_ID_SCHEMA, pages: z.object({ enabled: z.boolean(), url: z.url() }).array(), }), ) .output(z.null()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request( `library/files/${encodeURIComponent(input.fileId)}/scrape-pages`, { body: { pages: input.pages }, method: "PUT", responseSchema: z.null(), }, ), ) /** Creates a CloseBot web-scrape library file. */ export const createCloseBotWebScrape = defineAction( "Create CloseBot web scrape", ) .describe("Creates a library file by crawling a website.") .account(CLOSEBOT_ACCOUNT) .input( z.object({ maxBreadth: z.number().int().nonnegative(), maxDepth: z.number().int().nonnegative(), scrapeUrl: z.url(), }), ) .output(CLOSEBOT_WEB_SCRAPE_CREATED_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseBotApi(account.secret).request("library/webscrape", { body: input, method: "POST", responseSchema: CLOSEBOT_WEB_SCRAPE_CREATED_SCHEMA, }), )