@botfabrik/engine-webclient 4.101.1 → 4.101.3-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/auth-pages.js +3 -8
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.js +26 -34
- package/dist/auth/relay-state.js +6 -17
- package/dist/auth/ttl-cache.js +1 -5
- package/dist/constants.js +1 -4
- package/dist/createSessionInfo.d.ts +2 -2
- package/dist/createSessionInfo.js +3 -5
- package/dist/createSessionInfo.test.d.ts +1 -0
- package/dist/createSessionInfo.test.js +40 -45
- package/dist/embed/bundle.js +5 -5
- package/dist/extractLocale.js +3 -5
- package/dist/extractLocale.test.d.ts +1 -0
- package/dist/extractLocale.test.js +13 -18
- package/dist/getSupportedClientLocale.js +1 -3
- package/dist/getSupportedClientLocale.test.d.ts +1 -0
- package/dist/getSupportedClientLocale.test.js +15 -20
- package/dist/index.d.ts +2 -2
- package/dist/index.js +45 -64
- package/dist/loadPreviousConversation.js +5 -7
- package/dist/middleware/index.d.ts +1 -1
- package/dist/middleware/index.js +20 -25
- package/dist/requestSessionData.d.ts +1 -1
- package/dist/requestSessionData.js +5 -7
- package/dist/requestSessionData.test.d.ts +1 -0
- package/dist/requestSessionData.test.js +42 -52
- package/dist/setTranslations.js +4 -6
- package/dist/speechToText.d.ts +1 -1
- package/dist/speechToText.js +3 -5
- package/dist/types.js +2 -5
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/dist/views/index.js +1 -3
- package/package.json +20 -9
package/dist/extractLocale.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const accept_language_parser_1 = require("accept-language-parser");
|
|
1
|
+
import { parse } from 'accept-language-parser';
|
|
4
2
|
const extractLocale = (parsedUrlQuery, acceptLanguage = '') => {
|
|
5
3
|
let locale = undefined;
|
|
6
4
|
if (parsedUrlQuery['lang']) {
|
|
7
5
|
locale = parsedUrlQuery['lang'];
|
|
8
6
|
}
|
|
9
7
|
else {
|
|
10
|
-
const languages =
|
|
8
|
+
const languages = parse(acceptLanguage);
|
|
11
9
|
if (languages !== undefined && languages[0]) {
|
|
12
10
|
const code = languages[0].code;
|
|
13
11
|
const region = languages[0].region;
|
|
@@ -16,4 +14,4 @@ const extractLocale = (parsedUrlQuery, acceptLanguage = '') => {
|
|
|
16
14
|
}
|
|
17
15
|
return locale;
|
|
18
16
|
};
|
|
19
|
-
|
|
17
|
+
export default extractLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,25 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const globals_1 = require("@jest/globals");
|
|
7
|
-
const extractLocale_1 = __importDefault(require("./extractLocale"));
|
|
8
|
-
(0, globals_1.describe)('should extract locale from accept-language header', () => {
|
|
9
|
-
(0, globals_1.it)('undefined header value', () => {
|
|
10
|
-
(0, globals_1.expect)((0, extractLocale_1.default)({}, undefined)).toBe(undefined);
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import extractLocale from './extractLocale.js';
|
|
3
|
+
describe('should extract locale from accept-language header', () => {
|
|
4
|
+
it('undefined header value', () => {
|
|
5
|
+
expect(extractLocale({}, undefined)).toBe(undefined);
|
|
11
6
|
});
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
it('lang via query parameer', () => {
|
|
8
|
+
expect(extractLocale({ lang: 'de' }, '')).toBe('de');
|
|
14
9
|
});
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
it('empty header value', () => {
|
|
11
|
+
expect(extractLocale({}, '')).toBe(undefined);
|
|
17
12
|
});
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
it('header value with one value', () => {
|
|
14
|
+
expect(extractLocale({}, 'de-CH')).toBe('de_CH');
|
|
20
15
|
});
|
|
21
|
-
|
|
16
|
+
it('header value with several values', () => {
|
|
22
17
|
const header = 'de-CH,de;q=0.8,en-US;q=0.6,en;q=0.4';
|
|
23
|
-
|
|
18
|
+
expect(extractLocale({}, header)).toBe('de_CH');
|
|
24
19
|
});
|
|
25
20
|
});
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
const getSupportedClientLocale = (userLocale) => {
|
|
4
2
|
// Prüfe, ob der Webclient für diese Sprache eine Übersetzung hat
|
|
5
3
|
const supportedLocales = ['de', 'en', 'fr', 'it'];
|
|
@@ -15,4 +13,4 @@ const getSupportedClientLocale = (userLocale) => {
|
|
|
15
13
|
// default locale
|
|
16
14
|
return 'de';
|
|
17
15
|
};
|
|
18
|
-
|
|
16
|
+
export default getSupportedClientLocale;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
(0, globals_1.expect)((0, getSupportedClientLocale_1.default)('en')).toBe('en');
|
|
13
|
-
(0, globals_1.expect)((0, getSupportedClientLocale_1.default)('en-us')).toBe('en');
|
|
14
|
-
(0, globals_1.expect)((0, getSupportedClientLocale_1.default)('fr')).toBe('fr');
|
|
15
|
-
(0, globals_1.expect)((0, getSupportedClientLocale_1.default)('fr-ch')).toBe('fr');
|
|
16
|
-
(0, globals_1.expect)((0, getSupportedClientLocale_1.default)('it')).toBe('it');
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import getSupportedClientLocale from './getSupportedClientLocale.js';
|
|
3
|
+
describe('get client locale', () => {
|
|
4
|
+
it('should return supported client language', () => {
|
|
5
|
+
expect(getSupportedClientLocale('de')).toBe('de');
|
|
6
|
+
expect(getSupportedClientLocale('de-ch')).toBe('de');
|
|
7
|
+
expect(getSupportedClientLocale('en')).toBe('en');
|
|
8
|
+
expect(getSupportedClientLocale('en-us')).toBe('en');
|
|
9
|
+
expect(getSupportedClientLocale('fr')).toBe('fr');
|
|
10
|
+
expect(getSupportedClientLocale('fr-ch')).toBe('fr');
|
|
11
|
+
expect(getSupportedClientLocale('it')).toBe('it');
|
|
17
12
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
it('should return "de" as default locale', () => {
|
|
14
|
+
expect(getSupportedClientLocale('ru')).toBe('de');
|
|
15
|
+
expect(getSupportedClientLocale('ru-de')).toBe('de');
|
|
16
|
+
expect(getSupportedClientLocale('sp')).toBe('de');
|
|
22
17
|
});
|
|
23
18
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Client, type Environment } from '@botfabrik/engine-domain';
|
|
2
|
-
import { type WebclientMiddlewareState, type WebClientProps } from './types';
|
|
3
|
-
export * from './types';
|
|
2
|
+
import { type WebclientMiddlewareState, type WebClientProps } from './types.js';
|
|
3
|
+
export * from './types.js';
|
|
4
4
|
declare const _default: (clientName: string, environment: Environment, props: WebClientProps) => Client<WebclientMiddlewareState>;
|
|
5
5
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -1,47 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const engine_domain_1 = require("@botfabrik/engine-domain");
|
|
21
|
-
const engine_transcript_export_1 = require("@botfabrik/engine-transcript-export");
|
|
22
|
-
const express_1 = require("express");
|
|
23
|
-
const package_json_1 = require("../package.json");
|
|
24
|
-
const auth_1 = require("./auth");
|
|
25
|
-
const constants_1 = require("./constants");
|
|
26
|
-
const createSessionInfo_1 = __importDefault(require("./createSessionInfo"));
|
|
27
|
-
const extractLocale_1 = __importDefault(require("./extractLocale"));
|
|
28
|
-
const getSupportedClientLocale_1 = __importDefault(require("./getSupportedClientLocale"));
|
|
29
|
-
const loadPreviousConversation_1 = __importDefault(require("./loadPreviousConversation"));
|
|
30
|
-
const middleware_1 = __importDefault(require("./middleware"));
|
|
31
|
-
const requestSessionData_1 = __importDefault(require("./requestSessionData"));
|
|
32
|
-
const setTranslations_1 = __importDefault(require("./setTranslations"));
|
|
33
|
-
const speechToText_1 = __importDefault(require("./speechToText"));
|
|
34
|
-
const types_1 = require("./types");
|
|
35
|
-
const views_1 = __importDefault(require("./views"));
|
|
36
|
-
__exportStar(require("./types"), exports);
|
|
37
|
-
exports.default = (clientName, environment, props) => async (bot) => {
|
|
38
|
-
const logger = bot.logger.child({ clientType: constants_1.CLIENT_TYPE, clientName });
|
|
1
|
+
import { Actions, ActionTypes, BotUser, TextMessage, } from '@botfabrik/engine-domain';
|
|
2
|
+
import { getPdf } from '@botfabrik/engine-transcript-export';
|
|
3
|
+
import { static as serveStatic } from 'express';
|
|
4
|
+
import { setUpSamlAuth, storeLoginRequestToken, verifyLoginToken, } from './auth/index.js';
|
|
5
|
+
import { CLIENT_TYPE } from './constants.js';
|
|
6
|
+
import createSessionInfo from './createSessionInfo.js';
|
|
7
|
+
import extractLocale from './extractLocale.js';
|
|
8
|
+
import getSupportedClientLocale from './getSupportedClientLocale.js';
|
|
9
|
+
import loadPreviousConversation from './loadPreviousConversation.js';
|
|
10
|
+
import middleware from './middleware/index.js';
|
|
11
|
+
import requestSessionData from './requestSessionData.js';
|
|
12
|
+
import setTranslations from './setTranslations.js';
|
|
13
|
+
import speechToText from './speechToText.js';
|
|
14
|
+
import { Devices, } from './types.js';
|
|
15
|
+
import { version } from './version.js';
|
|
16
|
+
import index from './views/index.js';
|
|
17
|
+
export * from './types.js';
|
|
18
|
+
export default (clientName, environment, props) => async (bot) => {
|
|
19
|
+
const logger = bot.logger.child({ clientType: CLIENT_TYPE, clientName });
|
|
39
20
|
// serve transcript pdf
|
|
40
21
|
bot.webserver.express.use('/transcript-pdf/:sessionId', async (req, res) => {
|
|
41
22
|
const sessionId = req.params['sessionId'];
|
|
42
23
|
if (sessionId?.length) {
|
|
43
24
|
const session = await bot.createSession(sessionId);
|
|
44
|
-
const pdf = await
|
|
25
|
+
const pdf = await getPdf(session);
|
|
45
26
|
res.set('Content-Type', 'application/pdf');
|
|
46
27
|
res.set('Content-Disposition', 'attachment; filename="Transcript.pdf"');
|
|
47
28
|
res.end(pdf);
|
|
@@ -64,7 +45,7 @@ exports.default = (clientName, environment, props) => async (bot) => {
|
|
|
64
45
|
}
|
|
65
46
|
}
|
|
66
47
|
res.set('Content-Type', 'text/html; charset=utf-8');
|
|
67
|
-
res.write((
|
|
48
|
+
res.write(index(serverUrl.toString(), `${server}/embed/bundle.js`));
|
|
68
49
|
res.end();
|
|
69
50
|
});
|
|
70
51
|
if (!isFabVisible(props)) {
|
|
@@ -80,7 +61,7 @@ exports.default = (clientName, environment, props) => async (bot) => {
|
|
|
80
61
|
}
|
|
81
62
|
});
|
|
82
63
|
}
|
|
83
|
-
bot.webserver.express.use(`/${clientName}/embed`, (
|
|
64
|
+
bot.webserver.express.use(`/${clientName}/embed`, serveStatic(__dirname + '/embed', serveStaticOptions));
|
|
84
65
|
bot.webserver.express.get(`/${clientName}/logo.svg`, (_req, res) => {
|
|
85
66
|
res.redirect(`/cms/chatbot/design/logo.svg?client=${clientName}`);
|
|
86
67
|
});
|
|
@@ -91,7 +72,7 @@ exports.default = (clientName, environment, props) => async (bot) => {
|
|
|
91
72
|
res.redirect(`/cms/chatbot/design/fab.svg?client=${clientName}`);
|
|
92
73
|
});
|
|
93
74
|
// serve chat client resources
|
|
94
|
-
bot.webserver.express.use(`/${clientName}`, (
|
|
75
|
+
bot.webserver.express.use(`/${clientName}`, serveStatic(__dirname + '/client', serveStaticOptions));
|
|
95
76
|
logger.info(`Webclient will be available on route: /${clientName}`);
|
|
96
77
|
const nsp = bot.webserver.socket.of(`/${clientName}/chat`);
|
|
97
78
|
nsp.on('connection', async (socket) => {
|
|
@@ -101,23 +82,23 @@ exports.default = (clientName, environment, props) => async (bot) => {
|
|
|
101
82
|
socket.on('terminate-session', onTerminateSession(socket, bot));
|
|
102
83
|
if (props.auth) {
|
|
103
84
|
socket.on('login-requested', (data) => {
|
|
104
|
-
|
|
85
|
+
storeLoginRequestToken(data.loginRequestToken, socket.id);
|
|
105
86
|
});
|
|
106
87
|
}
|
|
107
88
|
}
|
|
108
89
|
catch (error) {
|
|
109
90
|
logger.error('Error while connecting webclient with backend.');
|
|
110
91
|
logger.error(error);
|
|
111
|
-
const errorMessage = new
|
|
112
|
-
socket.emit('action',
|
|
92
|
+
const errorMessage = new TextMessage('Sorry I can’t talk at the moment. Please try again later.', new BotUser());
|
|
93
|
+
socket.emit('action', Actions.sendMessageToGuest(errorMessage));
|
|
113
94
|
}
|
|
114
95
|
});
|
|
115
96
|
if (props.auth) {
|
|
116
|
-
|
|
97
|
+
setUpSamlAuth(bot, props.auth, clientName, nsp);
|
|
117
98
|
}
|
|
118
99
|
const client = {
|
|
119
100
|
name: `${clientName}Webclient`,
|
|
120
|
-
middleware: (
|
|
101
|
+
middleware: middleware(clientName, nsp),
|
|
121
102
|
};
|
|
122
103
|
return client;
|
|
123
104
|
};
|
|
@@ -133,7 +114,7 @@ const serveStaticOptions = {
|
|
|
133
114
|
res.setHeader('Cache-Control', 'max-age=31536000');
|
|
134
115
|
}
|
|
135
116
|
else {
|
|
136
|
-
res.setHeader('ETag',
|
|
117
|
+
res.setHeader('ETag', version);
|
|
137
118
|
}
|
|
138
119
|
},
|
|
139
120
|
};
|
|
@@ -142,21 +123,21 @@ const onTerminateSession = (socket, bot) => async ({ sessionId, // passed if the
|
|
|
142
123
|
}) => {
|
|
143
124
|
const session = await bot.createSession(sessionId);
|
|
144
125
|
socket.leave(sessionId);
|
|
145
|
-
await session.dispatch(
|
|
126
|
+
await session.dispatch(Actions.guestDisconnected(sessionId));
|
|
146
127
|
};
|
|
147
128
|
const onStartChat = (socket, props, bot, clientName, environment, logger) => async ({ sessionId: sessionIdFromClient, userId: defaultUserId, querystrings, loginToken, }) => {
|
|
148
129
|
try {
|
|
149
|
-
const authenticatedUser =
|
|
150
|
-
const locale = (
|
|
130
|
+
const authenticatedUser = verifyLoginToken(loginToken, props.auth, logger);
|
|
131
|
+
const locale = extractLocale(querystrings, socket.request.headers['accept-language']);
|
|
151
132
|
const sessionsCollection = bot.store.db.collection('sessions');
|
|
152
|
-
const { sessionId, sessionInfo: defaultSessionInfo, isNew, } = await (
|
|
133
|
+
const { sessionId, sessionInfo: defaultSessionInfo, isNew, } = await requestSessionData(sessionIdFromClient, querystrings, sessionsCollection, clientName, props);
|
|
153
134
|
// create a channel for each session
|
|
154
135
|
socket.join(sessionId);
|
|
155
|
-
const sessionInfo = await (
|
|
136
|
+
const sessionInfo = await createSessionInfo(socket, clientName, environment, defaultSessionInfo, defaultUserId, locale, querystrings, authenticatedUser, props)();
|
|
156
137
|
const session = await bot.createSession(sessionId, sessionInfo);
|
|
157
138
|
sendConfigurationToClient(socket, props, bot, clientName);
|
|
158
139
|
// sending persisted state to client
|
|
159
|
-
const previousConversations = await (
|
|
140
|
+
const previousConversations = await loadPreviousConversation(bot.store, sessionId);
|
|
160
141
|
socket.emit('restore-client-state', {
|
|
161
142
|
sessionId,
|
|
162
143
|
messages: previousConversations,
|
|
@@ -168,7 +149,7 @@ const onStartChat = (socket, props, bot, clientName, environment, logger) => asy
|
|
|
168
149
|
});
|
|
169
150
|
}
|
|
170
151
|
// Notify middlewares about a connected or reconnected guest
|
|
171
|
-
await session.dispatch(
|
|
152
|
+
await session.dispatch(Actions.guestConnected(sessionId));
|
|
172
153
|
if (isNew && props.getStartedAction) {
|
|
173
154
|
await session.dispatch(props.getStartedAction);
|
|
174
155
|
}
|
|
@@ -176,24 +157,24 @@ const onStartChat = (socket, props, bot, clientName, environment, logger) => asy
|
|
|
176
157
|
await session.dispatch(JSON.parse(action));
|
|
177
158
|
});
|
|
178
159
|
registerListener(socket, 'disconnect', async () => {
|
|
179
|
-
await session.dispatch(
|
|
160
|
+
await session.dispatch(Actions.guestDisconnected(sessionId));
|
|
180
161
|
});
|
|
181
162
|
registerListener(socket, 'conversation-rating', async (rating) => {
|
|
182
163
|
sessionsCollection.updateOne({ _id: sessionId }, {
|
|
183
164
|
$set: { 'sessionInfo.client.payload.conversationRating': rating },
|
|
184
165
|
});
|
|
185
166
|
session.dispatch({
|
|
186
|
-
type:
|
|
167
|
+
type: ActionTypes.CONVERSATION_RATING_FROM_GUEST,
|
|
187
168
|
payload: { rating },
|
|
188
169
|
});
|
|
189
170
|
});
|
|
190
171
|
registerListener(socket, 'audio-message', async (buffer) => {
|
|
191
172
|
if (props.speech) {
|
|
192
173
|
try {
|
|
193
|
-
const text = await (
|
|
174
|
+
const text = await speechToText(props.speech, session.translator.locale, buffer);
|
|
194
175
|
if (text && text.length) {
|
|
195
176
|
socket.emit('speech-transcription', text);
|
|
196
|
-
session.dispatch(
|
|
177
|
+
session.dispatch(Actions.receiveTextMessageFromGuest(text));
|
|
197
178
|
}
|
|
198
179
|
}
|
|
199
180
|
catch (error) {
|
|
@@ -215,12 +196,12 @@ const registerListener = (socket, event, listener) => {
|
|
|
215
196
|
};
|
|
216
197
|
const sendConfigurationToClient = (socket, props, bot, clientName) => {
|
|
217
198
|
// sending active language and translations to client
|
|
218
|
-
const locale = (
|
|
199
|
+
const locale = extractLocale(socket.handshake.query, socket.request.headers['accept-language']);
|
|
219
200
|
const supportedLocale = bot.translation.detectSupportedLocale(locale);
|
|
220
|
-
const clientLocale = (
|
|
221
|
-
(
|
|
201
|
+
const clientLocale = getSupportedClientLocale(supportedLocale);
|
|
202
|
+
setTranslations(socket, bot, clientLocale, clientName);
|
|
222
203
|
const settings = {
|
|
223
|
-
version
|
|
204
|
+
version,
|
|
224
205
|
requiresUserAuthentication: !!props.auth,
|
|
225
206
|
enableStartScreen: !!props.enableStartScreen || !!props.auth,
|
|
226
207
|
enableStandaloneView: !!props.enableStandaloneView,
|
|
@@ -233,7 +214,7 @@ const sendConfigurationToClient = (socket, props, bot, clientName) => {
|
|
|
233
214
|
};
|
|
234
215
|
socket.emit('set-settings', settings);
|
|
235
216
|
if (props.expandChatWindowAtStart &&
|
|
236
|
-
props.expandChatWindowAtStart !==
|
|
217
|
+
props.expandChatWindowAtStart !== Devices.None) {
|
|
237
218
|
socket.emit('expand-window', {
|
|
238
219
|
devices: props.expandChatWindowAtStart,
|
|
239
220
|
initial: true,
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const engine_domain_1 = require("@botfabrik/engine-domain");
|
|
1
|
+
import { ActionTypes, } from '@botfabrik/engine-domain';
|
|
4
2
|
const loadPreviousConversations = async (store, sessionId) => {
|
|
5
3
|
const actionLogs = await store.db
|
|
6
4
|
.collection('actionlog')
|
|
@@ -9,9 +7,9 @@ const loadPreviousConversations = async (store, sessionId) => {
|
|
|
9
7
|
{ _sessionId: sessionId },
|
|
10
8
|
{
|
|
11
9
|
$or: [
|
|
12
|
-
{ type:
|
|
13
|
-
{ type:
|
|
14
|
-
{ type:
|
|
10
|
+
{ type: ActionTypes.MESSAGE_FROM_GUEST },
|
|
11
|
+
{ type: ActionTypes.MESSAGE_WITHOUT_ANALYSIS_FROM_GUEST },
|
|
12
|
+
{ type: ActionTypes.MESSAGE_TO_GUEST },
|
|
15
13
|
],
|
|
16
14
|
},
|
|
17
15
|
],
|
|
@@ -34,4 +32,4 @@ const loadPreviousConversations = async (store, sessionId) => {
|
|
|
34
32
|
});
|
|
35
33
|
return messages;
|
|
36
34
|
};
|
|
37
|
-
|
|
35
|
+
export default loadPreviousConversations;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Middleware } from '@botfabrik/engine-domain';
|
|
2
2
|
import type { Namespace } from 'socket.io';
|
|
3
|
-
import { type WebclientMiddlewareState } from '../types';
|
|
3
|
+
import { type WebclientMiddlewareState } from '../types.js';
|
|
4
4
|
declare const _default: (clientName: string, nsp: Namespace) => Middleware<WebclientMiddlewareState>;
|
|
5
5
|
export default _default;
|
package/dist/middleware/index.js
CHANGED
|
@@ -1,40 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
const engine_domain_1 = require("@botfabrik/engine-domain");
|
|
7
|
-
const engine_utils_1 = require("@botfabrik/engine-utils");
|
|
8
|
-
const setTranslations_1 = __importDefault(require("../setTranslations"));
|
|
9
|
-
const types_1 = require("../types");
|
|
10
|
-
exports.default = (clientName, nsp) => async (bot) => {
|
|
1
|
+
import { ActionTypes, } from '@botfabrik/engine-domain';
|
|
2
|
+
import { waitFor, waitForTyping } from '@botfabrik/engine-utils';
|
|
3
|
+
import setTranslations from '../setTranslations.js';
|
|
4
|
+
import { Devices, } from '../types.js';
|
|
5
|
+
export default (clientName, nsp) => async (bot) => {
|
|
11
6
|
return async (next, _state, action, session) => {
|
|
12
7
|
const sessionInfo = session.getSessionInfo();
|
|
13
8
|
const clientInfo = sessionInfo.client;
|
|
14
9
|
if (clientInfo.type === 'webclient' && clientInfo.name === clientName) {
|
|
15
10
|
switch (action.type) {
|
|
16
|
-
case
|
|
11
|
+
case ActionTypes.MESSAGE_TO_GUEST:
|
|
17
12
|
await emitMessageToGuestAction(nsp, session, action);
|
|
18
13
|
break;
|
|
19
|
-
case
|
|
20
|
-
case
|
|
21
|
-
case
|
|
22
|
-
case
|
|
23
|
-
case
|
|
24
|
-
case
|
|
25
|
-
case
|
|
14
|
+
case ActionTypes.TYPING_ON:
|
|
15
|
+
case ActionTypes.TYPING_OFF:
|
|
16
|
+
case ActionTypes.EVENT_TO_GUEST:
|
|
17
|
+
case ActionTypes.MESSAGE_FROM_GUEST:
|
|
18
|
+
case ActionTypes.MESSAGE_WITHOUT_ANALYSIS_FROM_GUEST:
|
|
19
|
+
case ActionTypes.DRAWER_OPEN:
|
|
20
|
+
case ActionTypes.DRAWER_CLOSE:
|
|
26
21
|
await sendToChatClient(nsp, session, action);
|
|
27
22
|
break;
|
|
28
23
|
case 'webclient.window.expand':
|
|
29
24
|
expandChatWindow(nsp, session, action);
|
|
30
25
|
break;
|
|
31
|
-
case
|
|
26
|
+
case ActionTypes.RESTART_CHAT_REQUEST:
|
|
32
27
|
restartChat(nsp, session);
|
|
33
28
|
break;
|
|
34
|
-
case
|
|
29
|
+
case ActionTypes.GUEST_LOCALE_CHANGED:
|
|
35
30
|
{
|
|
36
31
|
const { locale } = session.translator;
|
|
37
|
-
(
|
|
32
|
+
setTranslations(nsp, bot, locale, clientName);
|
|
38
33
|
await sendToChatClient(nsp, session, action);
|
|
39
34
|
}
|
|
40
35
|
break;
|
|
@@ -46,18 +41,18 @@ exports.default = (clientName, nsp) => async (bot) => {
|
|
|
46
41
|
const emitMessageToGuestAction = async (nsp, session, action) => {
|
|
47
42
|
const useTyping = !action.noTypingDelay;
|
|
48
43
|
if (useTyping) {
|
|
49
|
-
nsp.to(session.id).emit('action', { type:
|
|
50
|
-
await
|
|
44
|
+
nsp.to(session.id).emit('action', { type: ActionTypes.TYPING_ON });
|
|
45
|
+
await waitForTyping(action['message']);
|
|
51
46
|
}
|
|
52
47
|
await sendToChatClient(nsp, session, action);
|
|
53
|
-
await
|
|
48
|
+
await waitFor(500);
|
|
54
49
|
};
|
|
55
50
|
const sendToChatClient = async (nsp, session, action) => {
|
|
56
51
|
nsp.to(session.id).emit('action', action);
|
|
57
52
|
};
|
|
58
53
|
const expandChatWindow = (nsp, session, action) => {
|
|
59
54
|
const devices = action.payload?.devices;
|
|
60
|
-
if (!!devices && devices !==
|
|
55
|
+
if (!!devices && devices !== Devices.None) {
|
|
61
56
|
nsp
|
|
62
57
|
.to(session.id)
|
|
63
58
|
.emit('expand-window', { devices: action.payload.devices });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionInfo } from '@botfabrik/engine-domain';
|
|
2
|
-
import type { SessionInfoClientPayload, SessionInfoUserPayload, WebClientProps } from './types';
|
|
2
|
+
import type { SessionInfoClientPayload, SessionInfoUserPayload, WebClientProps } from './types.js';
|
|
3
3
|
interface SessionData {
|
|
4
4
|
sessionId: string;
|
|
5
5
|
sessionInfo: SessionInfo<SessionInfoClientPayload, SessionInfoUserPayload>;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const crypto_1 = require("crypto");
|
|
4
|
-
const constants_1 = require("./constants");
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { CLIENT_TYPE } from './constants.js';
|
|
5
3
|
const requestSessionData = async (sessionId, querystrings, sessionsCollection, clientName, props) => {
|
|
6
4
|
const baseQuery = {
|
|
7
|
-
'sessionInfo.client.type':
|
|
5
|
+
'sessionInfo.client.type': CLIENT_TYPE,
|
|
8
6
|
'sessionInfo.client.name': clientName,
|
|
9
7
|
};
|
|
10
8
|
let findSessionRecordQuery;
|
|
@@ -31,7 +29,7 @@ const requestSessionData = async (sessionId, querystrings, sessionsCollection, c
|
|
|
31
29
|
isNew = false;
|
|
32
30
|
}
|
|
33
31
|
else {
|
|
34
|
-
sesId =
|
|
32
|
+
sesId = randomUUID();
|
|
35
33
|
sessionInfo = {
|
|
36
34
|
client: {},
|
|
37
35
|
user: {},
|
|
@@ -45,4 +43,4 @@ const requestSessionData = async (sessionId, querystrings, sessionsCollection, c
|
|
|
45
43
|
isNew,
|
|
46
44
|
};
|
|
47
45
|
};
|
|
48
|
-
|
|
46
|
+
export default requestSessionData;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|