🚀. Socket Launch Week Day 3:Socket Firewall Now Blocks Malicious VS Code and Open VSX Extensions.Learn more
Sign In

@klevu/core

Package Overview
Dependencies
Maintainers
3
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@klevu/core - npm Package Compare versions

Comparing version
5.2.3
to
5.3.0
+67
-1
dist/cjs/connection/moi/moi.d.ts

@@ -5,2 +5,3 @@ import { KlevuBaseQuerySettings, KlevuConfig, KlevuTypeOfSearch } from "../../index.js";

sessionId?: string;
visitorId?: string;
mode?: MoiChatModes;

@@ -11,2 +12,7 @@ url?: string;

additionalData?: string;
itemId?: string;
itemGroupId?: string;
itemVariantId?: string;
channelId?: string;
locale?: string;
};

@@ -88,2 +94,7 @@ export type MoiRequest = {

};
export type MoiQuestionsResponse = {
questions: {
options: MoiQuestion[];
};
};
export type MoiProducts = {

@@ -132,3 +143,4 @@ productData: {

};
export type MoiResponseObjects = MoiResponseText | MoiResponseFilter | MoiResponseGenericOptions | MoiMenuOptions | MoiProducts | MoiActionsMessage;
export type MoiResponseObjects = MoiResponseText | MoiResponseFilter | MoiResponseGenericOptions | MoiMenuOptions | MoiProducts | MoiActionsMessage | MoiQuestionsResponse;
export type MoiQuestion = string;
export type MoiMessages = Array<MoiResponseText | MoiResponseFilter | MoiProducts | MoiLocalMessage>;

@@ -180,2 +192,26 @@ export type MoiChatModes = "PQA";

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?: {

@@ -192,5 +228,34 @@ /**

};
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;

@@ -202,2 +267,3 @@ menu: MoiMenuOptions["menuOptions"];

messages: MoiMessages;
questions: MoiQuestion[];
menu: MoiMenuOptions["menuOptions"];

@@ -204,0 +270,0 @@ genericOptions?: MoiResponseGenericOptions["genericOptions"];

+37
-19

@@ -17,16 +17,13 @@ "use strict";

const STORAGE_KEY = "klevu-moi-session";
const MAX_MESSAGES = 10;
const saveProductInfo = (config, productInfo) => __awaiter(void 0, void 0, void 0, function* () {
return yield (0, fetch_js_1.post)(`${config.moiApiUrl}chat/productInfo`, productInfo);
});
function startMoi(options = {}) {
var _a, _b, _c;
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
const config = ((_a = options.settings) === null || _a === void 0 ? void 0 : _a.configOverride) || index_js_1.KlevuConfig.getDefault();
let startingMessages = [];
let ctx = {
klevuApiKey: config.apiKey,
sessionId: "",
mode: options.mode,
url: options.url,
productId: options.productId,
pqaWidgetId: options.pqaWidgetId,
additionalData: options.additionalData,
};
let questions = [];
let ctx = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ klevuApiKey: config.apiKey, sessionId: "", visitorId: "", mode: options.mode, url: options.url, productId: options.productId, pqaWidgetId: options.pqaWidgetId, additionalData: options.additionalData }, (options.itemId && { itemId: options.itemId })), (options.itemGroupId && { itemGroupId: options.itemGroupId })), (options.itemVariantId && { itemVariantId: options.itemVariantId })), (options.channelId && { channelId: options.channelId })), (options.locale && { locale: options.locale }));
const storedSession = yield getStoredSession();

@@ -36,3 +33,3 @@ let menu;

let feedbacks = [];
let shouldSendMessage = false;
let shouldSendMessage = (_b = options.settings) === null || _b === void 0 ? void 0 : _b.alwaysStartConversation;
const PQAKey = options.productId || options.url;

@@ -43,2 +40,3 @@ if (storedSession && storedSession.context) {

ctx.sessionId = storedSession.context.sessionId;
ctx.visitorId = storedSession.context.visitorId;
if (storedSession.MOI) {

@@ -49,3 +47,3 @@ startingMessages = storedSession.MOI.messages;

feedbacks = storedSession.MOI.feedbacks;
if ((_b = options.settings) === null || _b === void 0 ? void 0 : _b.alwaysStartConversation) {
if ((_c = options.settings) === null || _c === void 0 ? void 0 : _c.alwaysStartConversation) {
shouldSendMessage = storedSession.MOI.messages.length === 0;

@@ -57,2 +55,3 @@ }

ctx.sessionId = storedSession.context.sessionId;
ctx.visitorId = storedSession.context.visitorId;
if (PQAKey && storedSession.PQA && storedSession.PQA[PQAKey]) {

@@ -63,3 +62,4 @@ startingMessages = storedSession.PQA[PQAKey].messages;

feedbacks = storedSession.PQA[PQAKey].feedbacks;
if ((_c = options.settings) === null || _c === void 0 ? void 0 : _c.alwaysStartConversation) {
questions = storedSession.PQA[PQAKey].questions || [];
if ((_d = options.settings) === null || _d === void 0 ? void 0 : _d.alwaysStartConversation) {
shouldSendMessage = storedSession.PQA[PQAKey].messages.length === 0;

@@ -70,6 +70,10 @@ }

}
else {
shouldSendMessage = true;
}
if (shouldSendMessage) {
try {
if (options.productInfo && options.mode === "PQA")
yield saveProductInfo(config, options.productInfo);
}
catch (err) {
console.warn("Failed to save product Info", err);
}
const result = yield queryMoi({

@@ -82,6 +86,7 @@ context: ctx,

const parsed = parseResponse(result);
ctx = parsed.context;
ctx = Object.assign(Object.assign({}, ctx), parsed.context);
startingMessages = parsed.messages;
menu = parsed.menu;
genericOptions = parsed.genericOptions;
questions = parsed.questions || [];
}

@@ -91,2 +96,3 @@ return new MoiSession({

messages: startingMessages,
questions,
menu,

@@ -102,2 +108,3 @@ genericOptions,

this.messages = state.messages;
this.questions = state.questions;
this.menu = state.menu;

@@ -152,8 +159,14 @@ this.genericOptions = state.genericOptions;

}
const { messages, genericOptions, menu, context } = parseResponse(res);
const { messages, genericOptions, menu, context, questions } = parseResponse(res);
this.messages = [...this.messages, ...messages];
if (this.messages.length > MAX_MESSAGES) {
this.messages.shift();
}
if (target === "send") {
this.questions = [...questions];
}
(_g = (_f = this.options).onMessage) === null || _g === void 0 ? void 0 : _g.call(_f);
this.genericOptions = genericOptions;
this.menu = menu;
this.context = context;
this.context = Object.assign(Object.assign({}, this.context), context);
this.save();

@@ -196,2 +209,3 @@ return res;

feedbacks: this.feedbacks,
questions: this.questions || [],
},

@@ -261,3 +275,5 @@ },

function parseResponse(response) {
var _a;
const messages = [];
const questions = [];
let genericOptions = undefined;

@@ -270,2 +286,3 @@ let menu = undefined;

"productData" in d && messages.push(d);
"questions" in d && questions.push(...(((_a = d.questions) === null || _a === void 0 ? void 0 : _a.options) || []));
if ("genericOptions" in d) {

@@ -283,3 +300,4 @@ genericOptions = d.genericOptions;

genericOptions,
questions,
};
}

@@ -5,2 +5,3 @@ import { KlevuBaseQuerySettings, KlevuConfig, KlevuTypeOfSearch } from "../../index.js";

sessionId?: string;
visitorId?: string;
mode?: MoiChatModes;

@@ -11,2 +12,7 @@ url?: string;

additionalData?: string;
itemId?: string;
itemGroupId?: string;
itemVariantId?: string;
channelId?: string;
locale?: string;
};

@@ -88,2 +94,7 @@ export type MoiRequest = {

};
export type MoiQuestionsResponse = {
questions: {
options: MoiQuestion[];
};
};
export type MoiProducts = {

@@ -132,3 +143,4 @@ productData: {

};
export type MoiResponseObjects = MoiResponseText | MoiResponseFilter | MoiResponseGenericOptions | MoiMenuOptions | MoiProducts | MoiActionsMessage;
export type MoiResponseObjects = MoiResponseText | MoiResponseFilter | MoiResponseGenericOptions | MoiMenuOptions | MoiProducts | MoiActionsMessage | MoiQuestionsResponse;
export type MoiQuestion = string;
export type MoiMessages = Array<MoiResponseText | MoiResponseFilter | MoiProducts | MoiLocalMessage>;

@@ -180,2 +192,26 @@ export type MoiChatModes = "PQA";

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?: {

@@ -192,5 +228,34 @@ /**

};
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;

@@ -202,2 +267,3 @@ menu: MoiMenuOptions["menuOptions"];

messages: MoiMessages;
questions: MoiQuestion[];
menu: MoiMenuOptions["menuOptions"];

@@ -204,0 +270,0 @@ genericOptions?: MoiResponseGenericOptions["genericOptions"];

@@ -14,16 +14,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

const STORAGE_KEY = "klevu-moi-session";
const MAX_MESSAGES = 10;
const saveProductInfo = (config, productInfo) => __awaiter(void 0, void 0, void 0, function* () {
return yield post(`${config.moiApiUrl}chat/productInfo`, productInfo);
});
export function startMoi(options = {}) {
var _a, _b, _c;
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
const config = ((_a = options.settings) === null || _a === void 0 ? void 0 : _a.configOverride) || KlevuConfig.getDefault();
let startingMessages = [];
let ctx = {
klevuApiKey: config.apiKey,
sessionId: "",
mode: options.mode,
url: options.url,
productId: options.productId,
pqaWidgetId: options.pqaWidgetId,
additionalData: options.additionalData,
};
let questions = [];
let ctx = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ klevuApiKey: config.apiKey, sessionId: "", visitorId: "", mode: options.mode, url: options.url, productId: options.productId, pqaWidgetId: options.pqaWidgetId, additionalData: options.additionalData }, (options.itemId && { itemId: options.itemId })), (options.itemGroupId && { itemGroupId: options.itemGroupId })), (options.itemVariantId && { itemVariantId: options.itemVariantId })), (options.channelId && { channelId: options.channelId })), (options.locale && { locale: options.locale }));
const storedSession = yield getStoredSession();

@@ -33,3 +30,3 @@ let menu;

let feedbacks = [];
let shouldSendMessage = false;
let shouldSendMessage = (_b = options.settings) === null || _b === void 0 ? void 0 : _b.alwaysStartConversation;
const PQAKey = options.productId || options.url;

@@ -40,2 +37,3 @@ if (storedSession && storedSession.context) {

ctx.sessionId = storedSession.context.sessionId;
ctx.visitorId = storedSession.context.visitorId;
if (storedSession.MOI) {

@@ -46,3 +44,3 @@ startingMessages = storedSession.MOI.messages;

feedbacks = storedSession.MOI.feedbacks;
if ((_b = options.settings) === null || _b === void 0 ? void 0 : _b.alwaysStartConversation) {
if ((_c = options.settings) === null || _c === void 0 ? void 0 : _c.alwaysStartConversation) {
shouldSendMessage = storedSession.MOI.messages.length === 0;

@@ -54,2 +52,3 @@ }

ctx.sessionId = storedSession.context.sessionId;
ctx.visitorId = storedSession.context.visitorId;
if (PQAKey && storedSession.PQA && storedSession.PQA[PQAKey]) {

@@ -60,3 +59,4 @@ startingMessages = storedSession.PQA[PQAKey].messages;

feedbacks = storedSession.PQA[PQAKey].feedbacks;
if ((_c = options.settings) === null || _c === void 0 ? void 0 : _c.alwaysStartConversation) {
questions = storedSession.PQA[PQAKey].questions || [];
if ((_d = options.settings) === null || _d === void 0 ? void 0 : _d.alwaysStartConversation) {
shouldSendMessage = storedSession.PQA[PQAKey].messages.length === 0;

@@ -67,6 +67,10 @@ }

}
else {
shouldSendMessage = true;
}
if (shouldSendMessage) {
try {
if (options.productInfo && options.mode === "PQA")
yield saveProductInfo(config, options.productInfo);
}
catch (err) {
console.warn("Failed to save product Info", err);
}
const result = yield queryMoi({

@@ -79,6 +83,7 @@ context: ctx,

const parsed = parseResponse(result);
ctx = parsed.context;
ctx = Object.assign(Object.assign({}, ctx), parsed.context);
startingMessages = parsed.messages;
menu = parsed.menu;
genericOptions = parsed.genericOptions;
questions = parsed.questions || [];
}

@@ -88,2 +93,3 @@ return new MoiSession({

messages: startingMessages,
questions,
menu,

@@ -98,2 +104,3 @@ genericOptions,

this.messages = state.messages;
this.questions = state.questions;
this.menu = state.menu;

@@ -148,8 +155,14 @@ this.genericOptions = state.genericOptions;

}
const { messages, genericOptions, menu, context } = parseResponse(res);
const { messages, genericOptions, menu, context, questions } = parseResponse(res);
this.messages = [...this.messages, ...messages];
if (this.messages.length > MAX_MESSAGES) {
this.messages.shift();
}
if (target === "send") {
this.questions = [...questions];
}
(_g = (_f = this.options).onMessage) === null || _g === void 0 ? void 0 : _g.call(_f);
this.genericOptions = genericOptions;
this.menu = menu;
this.context = context;
this.context = Object.assign(Object.assign({}, this.context), context);
this.save();

@@ -192,2 +205,3 @@ return res;

feedbacks: this.feedbacks,
questions: this.questions || [],
},

@@ -256,3 +270,5 @@ },

function parseResponse(response) {
var _a;
const messages = [];
const questions = [];
let genericOptions = undefined;

@@ -265,2 +281,3 @@ let menu = undefined;

"productData" in d && messages.push(d);
"questions" in d && questions.push(...(((_a = d.questions) === null || _a === void 0 ? void 0 : _a.options) || []));
if ("genericOptions" in d) {

@@ -278,3 +295,4 @@ genericOptions = d.genericOptions;

genericOptions,
questions,
};
}
{
"name": "@klevu/core",
"sideEffects": false,
"version": "5.2.3",
"version": "5.3.0",
"description": "Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.",

@@ -6,0 +6,0 @@ "keywords": [

@@ -152,3 +152,3 @@ ![Klevu](../../images/klevu_header.jpg?raw=true "Klevu")

| `applyFilters()` | Applies selected filters to query |
| `applyFiltersWithManager()` | Applies filters that's state is managed by `FilterManager` |
| `applyFilterWithManager()` | Applies filters that's state is managed by `FilterManager` |
| `fallback()` | When query returns less results than fallback treshold then additional fallback query is sent |

@@ -171,3 +171,3 @@ | `boostWithKeywords()` | Boost or deboost results with keywords |

Filter Manager is a helper class that takes care of the state of filters. What filters are currently selected and what should be sent. It can be passed to `listFilters()` and then the result is automatically applied to the state. Modifier `applyFiltersWithManager()` can base used to apply the current state of filters to query.
Filter Manager is a helper class that takes care of the state of filters. What filters are currently selected and what should be sent. It can be passed to `listFilters()` and then the result is automatically applied to the state. Modifier `applyFilterWithManager()` can base used to apply the current state of filters to query.

@@ -174,0 +174,0 @@ ## Internal DOM events