Skip to content

Commit

Permalink
Add the term into the chat history emitted event
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayana Ilieva committed Mar 7, 2024
1 parent 0b11ad0 commit ee60f53
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 900 deletions.
1,728 changes: 864 additions & 864 deletions dist/index.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.es.js.map

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions dist/index.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/config/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ enum ScreenOrientation {
vertical = 'vertical',
}

enum QueryParams {
chat = 'utm_chat'
}

enum AllEvents {
addToCart = 'AddToCart',
contact = 'Contact',
Expand Down Expand Up @@ -74,4 +78,4 @@ enum Theme {
dark = 'dark',
}

export { AllEvents, Definition, Events, Intentions, Roles, ScreenOrientation, Theme }
export { AllEvents, Definition, Events, Intentions, QueryParams,Roles, ScreenOrientation, Theme }
8 changes: 4 additions & 4 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AssistantRecord } from '../interfaces/index';
import { uuidV4 } from '../utils';
import { Definition, Roles } from './enums';
import { getQueryParam, uuidV4 } from '../utils';
import { Definition, QueryParams, Roles } from './enums';
import { colors as baseThemeColors } from './themes/base';
export { Events, Roles } from './enums';
export { config } from './socket';
Expand All @@ -23,8 +23,8 @@ export const chat = (id: string) => ({
billingFrequencyTmsg: '',
},
marketing: {
utmParams: { utm_chat: 'db-sales-nopresetquestions' },
lastUtmParams: { utm_chat: 'db-sales-nopresetquestions' },
utmParams: { utm_chat: getQueryParam(QueryParams.chat) },
lastUtmParams: { utm_chat: getQueryParam(QueryParams.chat) },
screen: {
width: 2084,
height: 1608,
Expand Down
21 changes: 8 additions & 13 deletions src/middleware/socket.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { Middleware } from '@reduxjs/toolkit';
import { io,Socket } from 'socket.io-client';
import { io, Socket } from 'socket.io-client';

import { config as socketConfig, Events } from '../config';
import { Roles } from '../config/enums';
import { QueryParams, Roles } from '../config/enums';
import { CHAT_FINISHED_TIMESTAMP } from '../config/env';
import {
AssistantHistoryInitialMessage,
AssistantRecord,
ClientMessage,
SocketHistoryRecord,
UserMessageContent } from '../interfaces'
import { AssistantHistoryInitialMessage,AssistantRecord, ClientMessage, SocketHistoryRecord, UserMessageContent } from '../interfaces'
import {
fillAssistantHistoryData, fillInitialMessage,
hideResendIcon, resendMessage, resetError, resetHistory, resetIsLoading, resetOutgoing,
Expand Down Expand Up @@ -70,7 +65,7 @@ const chatMiddleware: Middleware = (store) => (next) => (action) => {
{
role: Roles.user,
message: data.message,
term: getQueryParam(window.location.search, 'utm_chat'),
term: getQueryParam(QueryParams.chat),
user_id: meta.cid,
region: meta.region,
},
Expand All @@ -86,7 +81,7 @@ const chatMiddleware: Middleware = (store) => (next) => (action) => {
handleMessageSending({
role: Roles.user,
message: action.payload,
term: getQueryParam(window.location.search, 'utm_chat') ?? '',
term: getQueryParam(QueryParams.chat),
user_id: meta.cid,
region: meta.region,
messageId: [...chat.historyIds].pop(),
Expand Down Expand Up @@ -116,7 +111,7 @@ const chatMiddleware: Middleware = (store) => (next) => (action) => {
handleMessageSending({
role: Roles.user,
message: lastMessage,
term: getQueryParam(window.location.search, 'utm_chat') || '',
term: getQueryParam(QueryParams.chat),
user_id: meta.cid,
region: meta.region,
messageId
Expand All @@ -140,7 +135,7 @@ const chatMiddleware: Middleware = (store) => (next) => (action) => {
socket.on(Events.connect, () => {
const { meta } = store.getState();
socket.sendBuffer = [];
socket.emit(Events.chatHistory, { user_id: meta.cid, region: meta.region });
socket.emit(Events.chatHistory, { user_id: meta.cid, region: meta.region, term: getQueryParam(QueryParams.chat) });
store.dispatch(setConnected(true));
});

Expand Down Expand Up @@ -180,7 +175,7 @@ const chatMiddleware: Middleware = (store) => (next) => (action) => {
config.aiProfile.initialMessage.forEach((message: SocketHistoryRecord) =>
handleMessageSending({
role: Roles.assistant,
term: getQueryParam(window.location.search, 'utm_chat') || '',
term: getQueryParam(QueryParams.chat),
user_id: meta.cid,
message: JSON.stringify(message.content),
messageId: message.id,
Expand Down
4 changes: 2 additions & 2 deletions src/store/slices/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSlice, Draft,PayloadAction } from '@reduxjs/toolkit';
import produce from 'immer';

import { getUnifiedSequence } from '../../config';
import { Definition, Roles } from '../../config/enums';
import { Definition, QueryParams, Roles } from '../../config/enums';
import {
AssistantHistoryDataFiller,
AssistantHistoryInitialMessage,
Expand All @@ -21,7 +21,7 @@ const configSlice = createSlice({
reducers: {
setOutgoing(state, { payload }: PayloadAction<string>) {
state.outgoing = {
term: getQueryParam(window.location.search, 'utm_chat') || '',
term: getQueryParam(QueryParams.chat),
user_id: localStorage.getItem('__cid') || '',
role: Roles.user,
message: payload,
Expand Down
9 changes: 4 additions & 5 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
/**
* Retrieves certain query parameter from a URL.
*
* @example getQueryParam(window.location.search, 'utm_chat')
* @param {string} url
* @example getQueryParam('utm_chat')
* @param {string} param
*/
export const getQueryParam = (url, param) => {
const urlParams = new URLSearchParams(url);
export const getQueryParam = (param) => {
const urlParams = new URLSearchParams(window.location.search);

return urlParams.get(param);
return urlParams.get(param) || '';
};

const formatDateByLocaleOptions = {
Expand Down

0 comments on commit ee60f53

Please sign in to comment.