UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

275 lines (274 loc) 6.99 kB
import { KlevuBaseQuerySettings, KlevuConfig, KlevuTypeOfSearch } from "../../index.js"; export type MoiContext = { klevuApiKey: string; sessionId?: string; visitorId?: string; mode?: MoiChatModes; url?: string; productId?: string; pqaWidgetId?: string; additionalData?: string; itemId?: string; itemGroupId?: string; itemVariantId?: string; channelId?: string; locale?: string; }; export type MoiRequest = { context: MoiContext; message?: string; filter?: { value: string; }; product?: { context: { url: string; }; id: string; intent: string; }; feedback?: { messageId: string; thumbs?: "UP" | "DOWN"; reason?: string; }; klevuSettings?: Omit<KlevuBaseQuerySettings, "query">; }; export type MoiResponse = { data: [MoiResponseContext, ...MoiResponseObjects[]]; }; export type MoiResponseContext = { context: MoiContext; }; export type MoiResponseText = { message: { id: string; note: string | null; type: "text"; value: string; collectFeedback?: boolean; }; }; export type MoiResponseFilter = { filter: { note: string | null; options: Array<{ count: string; name: string; selected: boolean | null; value: string; }>; settings: { chatFormat: string; chatFormatEmpty: string; key: string | null; label: string | null; }; }; }; export type MoiResponseGenericOptions = { genericOptions?: { options: Array<{ chat: string; name: string; type: "message" | "clearChat"; }>; }; }; export type MoiMenuOptions = { menuOptions?: { options: Array<{ name: string; chat: string; options: Array<{ key: string; validations: string; value: string; }>; type: "message" | "customerSupport"; }>; }; }; export type MoiQuestionsResponse = { questions: { options: MoiQuestion[]; }; }; export type MoiProducts = { productData: { note: string | null; totalResultsFound: string; typeOfQuery: KlevuTypeOfSearch; products: MoiProduct[]; }; }; export type MoiProduct = { id: string; currency: string; image: string; itemGroupId: string; name: string; noOfVariants: number; options: Array<{ chat: string; intent: string; name: string; }>; originalContent: null | string; price: string; salePrice: string; shortDesc: string; url: string; }; export type MoiActionsMessage = { actions: { actions: Array<{ type: "purgeHistory" | "redirectToUrl" | "askFeedbackReason"; context: { messageId?: string; value?: string; link?: string; }; }>; }; }; export type MoiLocalMessage = { local: { message: string; }; }; export type MoiResponseObjects = MoiResponseText | MoiResponseFilter | MoiResponseGenericOptions | MoiMenuOptions | MoiProducts | MoiActionsMessage | MoiQuestionsResponse; export type MoiQuestion = string; export type MoiMessages = Array<MoiResponseText | MoiResponseFilter | MoiProducts | MoiLocalMessage>; export type MoiChatModes = "PQA"; export type MoiSavedFeedback = { id: string; thumbs: "up" | "down"; reason?: string; }; export type MoiStartOptions = { /** * Called when a message is received from Moi * * @returns */ onMessage?: () => void; /** * Custom redirect handler * @param url * @returns */ onRedirect?: (url: string) => void; /** * Action listener for actions * @param action * @returns if false will prevent the default action */ onAction?: (action: MoiActionsMessage["actions"]["actions"][number]) => boolean | void; /** * The mode to use. If undefined will use the default Moi mode */ mode?: MoiChatModes; /** * URL for the PQA application */ url?: string; /** * productId for the PQA application */ productId?: string; /** * PQA widgetId for the PQA application */ pqaWidgetId?: string; /** * To pass additional information to the api as string */ additionalData?: string; /** * Product Id to be used in analytics */ itemId?: string; /** * Product Group Id to be used in analytics, in case of multiple variants */ itemGroupId?: string; /** * Optional Product Variant Id to be used in analytics */ itemVariantId?: string; /** * Channel Id to be used in analytics */ channelId?: string; /** * Locale to be used in analytics */ locale?: string; /** * Product details to be sent for analytics */ productInfo?: ProductInfo; settings?: { /** * Override the config */ configOverride?: KlevuConfig; /** * Always sends the initial message to Moi even if there are no messages stored for that case */ alwaysStartConversation?: boolean; }; }; export type ProductInfo = { itemId: string; itemGroupId: string; itemVariantId?: string; locale: string; channelId?: string; title: string; url: string; description: string; vendor: string; priceMax: string; priceMin: string; tags?: string[]; options?: { name: string; values: string[]; }[]; images: string[]; variants?: { itemVariantId: string; title: string; sku: string; url: string; image: string; price: string; weight: string; }[]; }; export declare function startMoi(options?: MoiStartOptions): Promise<MoiSession>; export declare class MoiSession { constructor(state: { questions: MoiQuestion[]; messages: MoiMessages; menu: MoiMenuOptions["menuOptions"]; genericOptions: MoiResponseGenericOptions["genericOptions"]; feedbacks: MoiSavedFeedback[]; }, options: MoiStartOptions, context: MoiContext, config: KlevuConfig); messages: MoiMessages; questions: MoiQuestion[]; menu: MoiMenuOptions["menuOptions"]; genericOptions?: MoiResponseGenericOptions["genericOptions"]; feedbacks: MoiSavedFeedback[]; options: MoiStartOptions; context: MoiContext; config: KlevuConfig; query(request: Omit<MoiRequest, "context">, target?: MoiAPITarget): Promise<MoiResponse>; clear(): void; save(): void; addFeedback(messageId: string, thumbs: "up" | "down", reason?: string): Promise<MoiResponse>; } type MoiAPITarget = "send" | "feedback"; export {};