import z from 'zod'; import { authedServiceRequest, serviceRequest, serviceResponse, } from '../interfaces/common'; import { metadata } from '../interfaces/Metadata'; import { file } from './FileService'; import { addSourceParams, source, SourceResolved } from './SourceService'; import { Invoice } from './InvoiceService'; import { sync } from './SyncService'; import { subscription } from './SubscriptionService'; import { zGPTModels } from '../enums/GPTModels'; export const zEmbedTheme = z.enum(['dark', 'light']); export const zEmbedType = z.enum(['attribute', 'launcher']); export const zEmbedPosition = z.enum(['bottom-right', 'bottom-left']); export const embedConfiguration = z.object({ enabled: z.boolean().optional(), theme: zEmbedTheme.optional(), type: zEmbedType.optional(), position: zEmbedPosition.optional(), background: z.string().optional(), color: z.string().optional(), }); export const trackingConfiguration = z.object({ utms: z.array(z.string()), }); export const promptConfiguration = z.object({ topic: z.string(), context: z.string(), }); export const bot = z.object({ _id: z.string(), organizationId: z.string(), sourceIds: z.array(z.string()), title: z.string(), description: z.string(), imageFileId: z.string(), brandColor: z.string(), website: z.string(), faqs: z.array(z.string()), model: zGPTModels, archived: z.boolean(), embed: embedConfiguration, tracking: trackingConfiguration, prompt: promptConfiguration, meta: metadata, }); export const botFiles = z.object({ imageFile: file.nullable(), }); export type Bot = z.infer; export type BotFiles = z.infer; export type EmbedConfiguration = z.infer; export type EmbedTheme = z.infer; export type EmbedType = z.infer; export type EmbedPosition = z.infer; export type TrackingConfiguration = z.infer; export type PromptConfiguration = z.infer; export type BotResolved = Bot & { sources: SourceResolved[]; files: BotFiles; invoice: Invoice; }; export interface BotService { create(request: CreateBotRequest): Promise; edit(request: EditBotRequest): Promise; archive(request: ArchiveBotRequest): Promise; get(request: GetBotRequest): Promise; list(request: ListBotsRequest): Promise; superList(request: SuperListBotsRequest): Promise; } /** ****************************************************************************** * Create Bot ******************************************************************************* */ export const createBotParams = z.object({ title: z.string().min(4).max(64), sourceIds: z.array(z.string()), description: z.string(), imageFileId: z.string(), brandColor: z.string(), website: z.string(), faqs: z.array(z.string()), tracking: trackingConfiguration, prompt: promptConfiguration, addSource: addSourceParams, model: zGPTModels, }); export const createBotRequest = authedServiceRequest.merge( z.object({ params: createBotParams, }), ); export const createBotResponse = serviceResponse.merge( z.object({ bot: bot.optional(), sources: z.array(source).nullable().optional(), syncs: z.array(sync).nullable().optional(), subscription: subscription.nullable().optional(), }), ); export type CreateBotParams = z.infer; export type CreateBotRequest = z.infer; export type CreateBotResponse = z.infer; /** ****************************************************************************** * Edit Bot ******************************************************************************* */ export const editBotParams = z.object({ _id: z.string(), title: z.string().min(4).max(64), sourceIds: z.array(z.string()), description: z.string(), imageFileId: z.string(), brandColor: z.string(), website: z.string(), faqs: z.array(z.string()), model: zGPTModels, embed: embedConfiguration.partial(), tracking: trackingConfiguration, prompt: promptConfiguration, }); export const editBotRequest = authedServiceRequest.merge( z.object({ params: editBotParams.partial(), }), ); export const editBotResponse = serviceResponse.merge( z.object({ bot: bot.optional(), }), ); export type EditBotParams = z.infer; export type EditBotRequest = z.infer; export type EditBotResponse = z.infer; /** ****************************************************************************** * Archive Bot ******************************************************************************* */ export const archiveBotParams = z.object({ _id: z.string(), archived: z.boolean(), }); export const archiveBotRequest = authedServiceRequest.merge( z.object({ params: archiveBotParams, }), ); export const archiveBotResponse = serviceResponse.merge( z.object({ bot: bot.optional(), }), ); export type ArchiveBotParams = z.infer; export type ArchiveBotRequest = z.infer; export type ArchiveBotResponse = z.infer; /** ****************************************************************************** * Get Bot ******************************************************************************* */ export const getBotParams = z.object({ botId: z.string(), }); export const getBotRequest = serviceRequest.merge( z.object({ params: getBotParams, }), ); export const getBotResponse = serviceResponse.merge( z.object({ bot: bot.nullable().optional(), }), ); export type GetBotParams = z.infer; export type GetBotRequest = z.infer; export type GetBotResponse = z.infer; /** ****************************************************************************** * List Bots ******************************************************************************* */ export const listBotsParams = z.object({ archived: z.boolean().optional(), }); export const listBotsRequest = authedServiceRequest.merge( z.object({ params: listBotsParams, }), ); export const listBotsResponse = serviceResponse.merge( z.object({ bots: z.array(bot).optional(), }), ); export type ListBotsParams = z.infer; export type ListBotsRequest = z.infer; export type ListBotsResponse = z.infer; /** ****************************************************************************** * Super List Bots ******************************************************************************* */ export const superListBotsParams = z.object({}); export const superListBotsRequest = authedServiceRequest.merge( z.object({ params: superListBotsParams, }), ); export const superListBotsResponse = serviceResponse.merge( z.object({ bots: z.array(bot).optional(), }), ); export type SuperListBotsParams = z.infer; export type SuperListBotsRequest = z.infer; export type SuperListBotsResponse = z.infer;