@modochats/widget 0.1.4 → 1.1.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/.github/workflows/build-and-publish.yml +116 -174
- package/.releaserc +59 -0
- package/CHANGELOG.md +16 -0
- package/README.md +191 -191
- package/package.json +79 -76
- package/src/app.ts +117 -117
- package/src/constants/index.ts +21 -21
- package/src/constants/regex.ts +2 -2
- package/src/constants/version.ts +2 -0
- package/src/index.ts +16 -16
- package/src/services/chat/conversation.ts +135 -135
- package/src/services/chat/message-utils.ts +221 -221
- package/src/services/chat/model.ts +139 -139
- package/src/services/chatbot/chatbot.ts +66 -66
- package/src/services/checker.ts +10 -10
- package/src/services/listeners/fn.ts +77 -77
- package/src/services/socket/utils.ts +9 -9
- package/src/services/user/customer-data.ts +78 -78
- package/src/services/voice-chat/model.ts +79 -79
- package/src/services/voice-chat/utils.ts +137 -137
- package/src/tools/fetch.ts +7 -7
- package/src/types/app.ts +17 -17
- package/src/types/conversation.ts +14 -14
- package/src/types/socket.ts +7 -7
- package/src/types/window.ts +12 -12
- package/src/utils/audio.ts +67 -67
- package/src/utils/browser.ts +4 -4
- package/src/utils/fetch.ts +98 -98
- package/src/utils/uuid.ts +13 -13
- package/dist/src/app.d.ts +0 -29
- package/dist/src/app.js +0 -1
- package/dist/src/app.js.map +0 -1
- package/dist/src/constants/index.d.ts +0 -9
- package/dist/src/constants/index.js +0 -1
- package/dist/src/constants/index.js.map +0 -1
- package/dist/src/constants/regex.d.ts +0 -2
- package/dist/src/constants/regex.js +0 -1
- package/dist/src/constants/regex.js.map +0 -1
- package/dist/src/index.d.ts +0 -9
- package/dist/src/index.js +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/models/chatbot.d.ts +0 -23
- package/dist/src/models/chatbot.js +0 -1
- package/dist/src/models/chatbot.js.map +0 -1
- package/dist/src/models/conversation.d.ts +0 -22
- package/dist/src/models/conversation.js +0 -1
- package/dist/src/models/conversation.js.map +0 -1
- package/dist/src/models/customer-data.d.ts +0 -31
- package/dist/src/models/customer-data.js +0 -1
- package/dist/src/models/customer-data.js.map +0 -1
- package/dist/src/models/message-utils.d.ts +0 -12
- package/dist/src/models/message-utils.js +0 -1
- package/dist/src/models/message-utils.js.map +0 -1
- package/dist/src/services/chat/conversation.d.ts +0 -22
- package/dist/src/services/chat/conversation.js +0 -1
- package/dist/src/services/chat/conversation.js.map +0 -1
- package/dist/src/services/chat/message-utils.d.ts +0 -12
- package/dist/src/services/chat/message-utils.js +0 -1
- package/dist/src/services/chat/message-utils.js.map +0 -1
- package/dist/src/services/chat/model.d.ts +0 -27
- package/dist/src/services/chat/model.js +0 -1
- package/dist/src/services/chat/model.js.map +0 -1
- package/dist/src/services/chatbot/chatbot.d.ts +0 -23
- package/dist/src/services/chatbot/chatbot.js +0 -1
- package/dist/src/services/chatbot/chatbot.js.map +0 -1
- package/dist/src/services/checker.d.ts +0 -3
- package/dist/src/services/checker.js +0 -1
- package/dist/src/services/checker.js.map +0 -1
- package/dist/src/services/listeners/adders.d.ts +0 -3
- package/dist/src/services/listeners/adders.js +0 -1
- package/dist/src/services/listeners/adders.js.map +0 -1
- package/dist/src/services/listeners/fn.d.ts +0 -3
- package/dist/src/services/listeners/fn.js +0 -1
- package/dist/src/services/listeners/fn.js.map +0 -1
- package/dist/src/services/socket/utils.d.ts +0 -2
- package/dist/src/services/socket/utils.js +0 -1
- package/dist/src/services/socket/utils.js.map +0 -1
- package/dist/src/services/ui/fn.d.ts +0 -13
- package/dist/src/services/ui/fn.js +0 -1
- package/dist/src/services/ui/fn.js.map +0 -1
- package/dist/src/services/ui/html.d.ts +0 -3
- package/dist/src/services/ui/html.js +0 -1
- package/dist/src/services/ui/html.js.map +0 -1
- package/dist/src/services/user/customer-data.d.ts +0 -31
- package/dist/src/services/user/customer-data.js +0 -1
- package/dist/src/services/user/customer-data.js.map +0 -1
- package/dist/src/services/voice-chat/model.d.ts +0 -12
- package/dist/src/services/voice-chat/model.js +0 -1
- package/dist/src/services/voice-chat/model.js.map +0 -1
- package/dist/src/services/voice-chat/utils.d.ts +0 -9
- package/dist/src/services/voice-chat/utils.js +0 -1
- package/dist/src/services/voice-chat/utils.js.map +0 -1
- package/dist/src/tools/fetch.d.ts +0 -2
- package/dist/src/tools/fetch.js +0 -1
- package/dist/src/tools/fetch.js.map +0 -1
- package/dist/src/types/app.d.ts +0 -17
- package/dist/src/types/app.js +0 -1
- package/dist/src/types/app.js.map +0 -1
- package/dist/src/types/conversation.d.ts +0 -14
- package/dist/src/types/conversation.js +0 -1
- package/dist/src/types/conversation.js.map +0 -1
- package/dist/src/types/socket.d.ts +0 -6
- package/dist/src/types/socket.js +0 -1
- package/dist/src/types/socket.js.map +0 -1
- package/dist/src/types/window.d.ts +0 -9
- package/dist/src/types/window.js +0 -1
- package/dist/src/types/window.js.map +0 -1
- package/dist/src/utils/audio.d.ts +0 -3
- package/dist/src/utils/audio.js +0 -1
- package/dist/src/utils/audio.js.map +0 -1
- package/dist/src/utils/browser.d.ts +0 -2
- package/dist/src/utils/browser.js +0 -1
- package/dist/src/utils/browser.js.map +0 -1
- package/dist/src/utils/fetch.d.ts +0 -18
- package/dist/src/utils/fetch.js +0 -1
- package/dist/src/utils/fetch.js.map +0 -1
- package/dist/src/utils/uuid.d.ts +0 -6
- package/dist/src/utils/uuid.js +0 -1
- package/dist/src/utils/uuid.js.map +0 -1
- package/dist/types/src/app.d.ts +0 -30
- package/dist/types/src/app.d.ts.map +0 -1
- package/dist/types/src/constants/index.d.ts +0 -10
- package/dist/types/src/constants/index.d.ts.map +0 -1
- package/dist/types/src/constants/regex.d.ts +0 -3
- package/dist/types/src/constants/regex.d.ts.map +0 -1
- package/dist/types/src/index.d.ts +0 -10
- package/dist/types/src/index.d.ts.map +0 -1
- package/dist/types/src/models/chatbot.d.ts +0 -24
- package/dist/types/src/models/chatbot.d.ts.map +0 -1
- package/dist/types/src/models/conversation.d.ts +0 -23
- package/dist/types/src/models/conversation.d.ts.map +0 -1
- package/dist/types/src/models/customer-data.d.ts +0 -32
- package/dist/types/src/models/customer-data.d.ts.map +0 -1
- package/dist/types/src/models/message-utils.d.ts +0 -13
- package/dist/types/src/models/message-utils.d.ts.map +0 -1
- package/dist/types/src/services/chat/conversation.d.ts +0 -23
- package/dist/types/src/services/chat/conversation.d.ts.map +0 -1
- package/dist/types/src/services/chat/message-utils.d.ts +0 -13
- package/dist/types/src/services/chat/message-utils.d.ts.map +0 -1
- package/dist/types/src/services/chat/model.d.ts +0 -28
- package/dist/types/src/services/chat/model.d.ts.map +0 -1
- package/dist/types/src/services/chatbot/chatbot.d.ts +0 -24
- package/dist/types/src/services/chatbot/chatbot.d.ts.map +0 -1
- package/dist/types/src/services/checker.d.ts +0 -4
- package/dist/types/src/services/checker.d.ts.map +0 -1
- package/dist/types/src/services/listeners/adders.d.ts +0 -4
- package/dist/types/src/services/listeners/adders.d.ts.map +0 -1
- package/dist/types/src/services/listeners/fn.d.ts +0 -4
- package/dist/types/src/services/listeners/fn.d.ts.map +0 -1
- package/dist/types/src/services/socket/utils.d.ts +0 -3
- package/dist/types/src/services/socket/utils.d.ts.map +0 -1
- package/dist/types/src/services/ui/fn.d.ts +0 -14
- package/dist/types/src/services/ui/fn.d.ts.map +0 -1
- package/dist/types/src/services/ui/html.d.ts +0 -4
- package/dist/types/src/services/ui/html.d.ts.map +0 -1
- package/dist/types/src/services/user/customer-data.d.ts +0 -32
- package/dist/types/src/services/user/customer-data.d.ts.map +0 -1
- package/dist/types/src/services/voice-chat/model.d.ts +0 -13
- package/dist/types/src/services/voice-chat/model.d.ts.map +0 -1
- package/dist/types/src/services/voice-chat/utils.d.ts +0 -10
- package/dist/types/src/services/voice-chat/utils.d.ts.map +0 -1
- package/dist/types/src/tools/fetch.d.ts +0 -3
- package/dist/types/src/tools/fetch.d.ts.map +0 -1
- package/dist/types/src/types/app.d.ts +0 -18
- package/dist/types/src/types/app.d.ts.map +0 -1
- package/dist/types/src/types/conversation.d.ts +0 -15
- package/dist/types/src/types/conversation.d.ts.map +0 -1
- package/dist/types/src/types/socket.d.ts +0 -7
- package/dist/types/src/types/socket.d.ts.map +0 -1
- package/dist/types/src/types/window.d.ts +0 -10
- package/dist/types/src/types/window.d.ts.map +0 -1
- package/dist/types/src/utils/audio.d.ts +0 -4
- package/dist/types/src/utils/audio.d.ts.map +0 -1
- package/dist/types/src/utils/browser.d.ts +0 -3
- package/dist/types/src/utils/browser.d.ts.map +0 -1
- package/dist/types/src/utils/fetch.d.ts +0 -19
- package/dist/types/src/utils/fetch.d.ts.map +0 -1
- package/dist/types/src/utils/uuid.d.ts +0 -7
- package/dist/types/src/utils/uuid.d.ts.map +0 -1
package/src/app.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import {WidgetOptions} from "./types/app.js";
|
|
2
|
-
import {Chatbot} from "./services/chatbot/chatbot.js";
|
|
3
|
-
import {fetchChatbot} from "./utils/fetch.js";
|
|
4
|
-
import {checkIfHostIsAllowed} from "./services/checker.js";
|
|
5
|
-
import {createChatContainer} from "./services/ui/html.js";
|
|
6
|
-
import {CustomerData} from "./services/user/customer-data.js";
|
|
7
|
-
import {loadStarters, updateChatToggleImage, updateChatTitle, applyModoOptions, loadCss} from "./services/ui/fn.js";
|
|
8
|
-
import {VERSION} from "./constants/index.js";
|
|
9
|
-
import {VoiceChat} from "./services/voice-chat/model.js";
|
|
10
|
-
import {Chat} from "./services/chat/model.js";
|
|
11
|
-
|
|
12
|
-
class Widget {
|
|
13
|
-
container?: HTMLDivElement;
|
|
14
|
-
publicKey: string;
|
|
15
|
-
chatbot?: Chatbot;
|
|
16
|
-
customerData: CustomerData;
|
|
17
|
-
chat: Chat;
|
|
18
|
-
|
|
19
|
-
options: Partial<WidgetOptions> = {};
|
|
20
|
-
openedCount: number = 0;
|
|
21
|
-
version: string;
|
|
22
|
-
isInitialized: boolean = false;
|
|
23
|
-
isOpen: boolean = false;
|
|
24
|
-
voiceChat?: VoiceChat;
|
|
25
|
-
|
|
26
|
-
constructor(publicKey: string, options?: Partial<WidgetOptions>) {
|
|
27
|
-
this.publicKey = publicKey;
|
|
28
|
-
this.customerData = new CustomerData(this, options?.userData);
|
|
29
|
-
this.chat = new Chat();
|
|
30
|
-
this.version = VERSION;
|
|
31
|
-
this.options = {
|
|
32
|
-
position: options?.position || "right",
|
|
33
|
-
theme: options?.theme,
|
|
34
|
-
primaryColor: options?.primaryColor,
|
|
35
|
-
title: options?.title || "",
|
|
36
|
-
userData: options?.userData,
|
|
37
|
-
foregroundColor: options?.foregroundColor,
|
|
38
|
-
fullScreen: typeof options?.fullScreen === "boolean" ? options?.fullScreen : false
|
|
39
|
-
};
|
|
40
|
-
if (options?.autoInit) this.init();
|
|
41
|
-
}
|
|
42
|
-
async init() {
|
|
43
|
-
if (this.isInitialized) throw new Error("Widget already initialized");
|
|
44
|
-
const chatbotRes = await fetchChatbot(this.publicKey);
|
|
45
|
-
this.chatbot = new Chatbot(chatbotRes);
|
|
46
|
-
this.options = {
|
|
47
|
-
...this.options,
|
|
48
|
-
theme: this.options?.theme || this.chatbot?.uiConfig?.theme || "dark",
|
|
49
|
-
primaryColor: this.options?.primaryColor || this.chatbot?.uiConfig?.primaryColor || "#667eea",
|
|
50
|
-
foregroundColor: this.options?.foregroundColor || this.chatbot?.uiConfig?.foregroundColor || "#fff"
|
|
51
|
-
};
|
|
52
|
-
if (checkIfHostIsAllowed(this)) {
|
|
53
|
-
await loadCss();
|
|
54
|
-
window.getMWidget = () => this;
|
|
55
|
-
createChatContainer(this);
|
|
56
|
-
applyModoOptions();
|
|
57
|
-
loadStarters();
|
|
58
|
-
updateChatToggleImage();
|
|
59
|
-
updateChatTitle();
|
|
60
|
-
|
|
61
|
-
this.isInitialized = true;
|
|
62
|
-
this.chatbot.showTooltip();
|
|
63
|
-
|
|
64
|
-
// In fullscreen mode, automatically open the chat
|
|
65
|
-
if (this.options.fullScreen) {
|
|
66
|
-
// Ensure chat body is visible in fullscreen mode
|
|
67
|
-
const chatBody = this.container?.querySelector(".mw-chat-body");
|
|
68
|
-
if (chatBody) {
|
|
69
|
-
chatBody.classList.remove("mw-hidden");
|
|
70
|
-
chatBody.classList.add("mw-active");
|
|
71
|
-
}
|
|
72
|
-
try {
|
|
73
|
-
this.chat.initInstance();
|
|
74
|
-
} finally {
|
|
75
|
-
this.onOpen();
|
|
76
|
-
}
|
|
77
|
-
} else this.chat.initInstance();
|
|
78
|
-
} else throw new Error("host not allowed");
|
|
79
|
-
}
|
|
80
|
-
async onOpen() {
|
|
81
|
-
this.isOpen = true;
|
|
82
|
-
this.openedCount++;
|
|
83
|
-
|
|
84
|
-
// Hide tooltip when chat is opened
|
|
85
|
-
this.conversation?.hideTooltip();
|
|
86
|
-
this.chatbot?.hideTooltip();
|
|
87
|
-
this.conversation?.markAsRead();
|
|
88
|
-
this.conversation?.scrollToBottom();
|
|
89
|
-
if (this.openedCount === 1) {
|
|
90
|
-
if (this.chat.conversationD) {
|
|
91
|
-
await this.conversation?.loadMessages();
|
|
92
|
-
await this.chat?.socket?.connect();
|
|
93
|
-
}
|
|
94
|
-
if (this.chatbot?.voiceChat) this.voiceChat = new VoiceChat();
|
|
95
|
-
await this.customerData.fetchUpdate();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
onClose() {
|
|
99
|
-
this.isOpen = false;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Update user data with new values
|
|
104
|
-
* @param newUserData - Object containing new user data to merge
|
|
105
|
-
*/
|
|
106
|
-
async updateUserData(newUserData: Record<string, any>) {
|
|
107
|
-
await this.customerData.updateUserData(newUserData);
|
|
108
|
-
}
|
|
109
|
-
get conversation() {
|
|
110
|
-
return this.chat.conversation;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
window.ModoChat = Widget;
|
|
115
|
-
window.ModoWidget = Widget;
|
|
116
|
-
|
|
117
|
-
export type {Widget};
|
|
1
|
+
import {WidgetOptions} from "./types/app.js";
|
|
2
|
+
import {Chatbot} from "./services/chatbot/chatbot.js";
|
|
3
|
+
import {fetchChatbot} from "./utils/fetch.js";
|
|
4
|
+
import {checkIfHostIsAllowed} from "./services/checker.js";
|
|
5
|
+
import {createChatContainer} from "./services/ui/html.js";
|
|
6
|
+
import {CustomerData} from "./services/user/customer-data.js";
|
|
7
|
+
import {loadStarters, updateChatToggleImage, updateChatTitle, applyModoOptions, loadCss} from "./services/ui/fn.js";
|
|
8
|
+
import {VERSION} from "./constants/index.js";
|
|
9
|
+
import {VoiceChat} from "./services/voice-chat/model.js";
|
|
10
|
+
import {Chat} from "./services/chat/model.js";
|
|
11
|
+
|
|
12
|
+
class Widget {
|
|
13
|
+
container?: HTMLDivElement;
|
|
14
|
+
publicKey: string;
|
|
15
|
+
chatbot?: Chatbot;
|
|
16
|
+
customerData: CustomerData;
|
|
17
|
+
chat: Chat;
|
|
18
|
+
|
|
19
|
+
options: Partial<WidgetOptions> = {};
|
|
20
|
+
openedCount: number = 0;
|
|
21
|
+
version: string;
|
|
22
|
+
isInitialized: boolean = false;
|
|
23
|
+
isOpen: boolean = false;
|
|
24
|
+
voiceChat?: VoiceChat;
|
|
25
|
+
|
|
26
|
+
constructor(publicKey: string, options?: Partial<WidgetOptions>) {
|
|
27
|
+
this.publicKey = publicKey;
|
|
28
|
+
this.customerData = new CustomerData(this, options?.userData);
|
|
29
|
+
this.chat = new Chat();
|
|
30
|
+
this.version = VERSION;
|
|
31
|
+
this.options = {
|
|
32
|
+
position: options?.position || "right",
|
|
33
|
+
theme: options?.theme,
|
|
34
|
+
primaryColor: options?.primaryColor,
|
|
35
|
+
title: options?.title || "",
|
|
36
|
+
userData: options?.userData,
|
|
37
|
+
foregroundColor: options?.foregroundColor,
|
|
38
|
+
fullScreen: typeof options?.fullScreen === "boolean" ? options?.fullScreen : false
|
|
39
|
+
};
|
|
40
|
+
if (options?.autoInit) this.init();
|
|
41
|
+
}
|
|
42
|
+
async init() {
|
|
43
|
+
if (this.isInitialized) throw new Error("Widget already initialized");
|
|
44
|
+
const chatbotRes = await fetchChatbot(this.publicKey);
|
|
45
|
+
this.chatbot = new Chatbot(chatbotRes);
|
|
46
|
+
this.options = {
|
|
47
|
+
...this.options,
|
|
48
|
+
theme: this.options?.theme || this.chatbot?.uiConfig?.theme || "dark",
|
|
49
|
+
primaryColor: this.options?.primaryColor || this.chatbot?.uiConfig?.primaryColor || "#667eea",
|
|
50
|
+
foregroundColor: this.options?.foregroundColor || this.chatbot?.uiConfig?.foregroundColor || "#fff"
|
|
51
|
+
};
|
|
52
|
+
if (checkIfHostIsAllowed(this)) {
|
|
53
|
+
await loadCss();
|
|
54
|
+
window.getMWidget = () => this;
|
|
55
|
+
createChatContainer(this);
|
|
56
|
+
applyModoOptions();
|
|
57
|
+
loadStarters();
|
|
58
|
+
updateChatToggleImage();
|
|
59
|
+
updateChatTitle();
|
|
60
|
+
|
|
61
|
+
this.isInitialized = true;
|
|
62
|
+
this.chatbot.showTooltip();
|
|
63
|
+
|
|
64
|
+
// In fullscreen mode, automatically open the chat
|
|
65
|
+
if (this.options.fullScreen) {
|
|
66
|
+
// Ensure chat body is visible in fullscreen mode
|
|
67
|
+
const chatBody = this.container?.querySelector(".mw-chat-body");
|
|
68
|
+
if (chatBody) {
|
|
69
|
+
chatBody.classList.remove("mw-hidden");
|
|
70
|
+
chatBody.classList.add("mw-active");
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
this.chat.initInstance();
|
|
74
|
+
} finally {
|
|
75
|
+
this.onOpen();
|
|
76
|
+
}
|
|
77
|
+
} else this.chat.initInstance();
|
|
78
|
+
} else throw new Error("host not allowed");
|
|
79
|
+
}
|
|
80
|
+
async onOpen() {
|
|
81
|
+
this.isOpen = true;
|
|
82
|
+
this.openedCount++;
|
|
83
|
+
|
|
84
|
+
// Hide tooltip when chat is opened
|
|
85
|
+
this.conversation?.hideTooltip();
|
|
86
|
+
this.chatbot?.hideTooltip();
|
|
87
|
+
this.conversation?.markAsRead();
|
|
88
|
+
this.conversation?.scrollToBottom();
|
|
89
|
+
if (this.openedCount === 1) {
|
|
90
|
+
if (this.chat.conversationD) {
|
|
91
|
+
await this.conversation?.loadMessages();
|
|
92
|
+
await this.chat?.socket?.connect();
|
|
93
|
+
}
|
|
94
|
+
if (this.chatbot?.voiceChat) this.voiceChat = new VoiceChat();
|
|
95
|
+
await this.customerData.fetchUpdate();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
onClose() {
|
|
99
|
+
this.isOpen = false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Update user data with new values
|
|
104
|
+
* @param newUserData - Object containing new user data to merge
|
|
105
|
+
*/
|
|
106
|
+
async updateUserData(newUserData: Record<string, any>) {
|
|
107
|
+
await this.customerData.updateUserData(newUserData);
|
|
108
|
+
}
|
|
109
|
+
get conversation() {
|
|
110
|
+
return this.chat.conversation;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
window.ModoChat = Widget;
|
|
115
|
+
window.ModoWidget = Widget;
|
|
116
|
+
|
|
117
|
+
export type {Widget};
|
package/src/constants/index.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const BASE_STORAGE_URL = "https://modochats.s3.ir-thr-at1.arvanstorage.ir";
|
|
20
|
-
const NEW_MESSAGE_AUDIO_URL = `${BASE_STORAGE_URL}/new-message.mp3`;
|
|
21
|
-
export {BASE_API_URL, BASE_WEBSOCKET_URL, VERSION, isDev, isProd, getEnvironment, NEW_MESSAGE_AUDIO_URL, BASE_STORAGE_URL};
|
|
1
|
+
import {VERSION} from "./version.js";
|
|
2
|
+
const getEnvironment = () => {
|
|
3
|
+
// Check for browser global variable
|
|
4
|
+
if (typeof window !== "undefined" && (window as any).ENVIRONMENT) {
|
|
5
|
+
return (window as any).ENVIRONMENT;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Check for NODE_ENV in build process
|
|
9
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV) {
|
|
10
|
+
return process.env.NODE_ENV.toUpperCase();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return "PROD"; // Default to production
|
|
14
|
+
};
|
|
15
|
+
const isDev = getEnvironment() === "DEV";
|
|
16
|
+
const isProd = getEnvironment() === "PROD";
|
|
17
|
+
const BASE_API_URL = isDev ? "https://dev-api.modochats.com" : "https://api.modochats.com";
|
|
18
|
+
const BASE_WEBSOCKET_URL = isDev ? "wss://dev-api.modochats.com/ws" : "wss://api.modochats.com/ws";
|
|
19
|
+
const BASE_STORAGE_URL = "https://modochats.s3.ir-thr-at1.arvanstorage.ir";
|
|
20
|
+
const NEW_MESSAGE_AUDIO_URL = `${BASE_STORAGE_URL}/new-message.mp3`;
|
|
21
|
+
export {BASE_API_URL, BASE_WEBSOCKET_URL, VERSION, isDev, isProd, getEnvironment, NEW_MESSAGE_AUDIO_URL, BASE_STORAGE_URL};
|
package/src/constants/regex.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const PhoneNumberRegex = /^(\+98|0)?9\d{9}$/;
|
|
2
|
-
export {PhoneNumberRegex};
|
|
1
|
+
const PhoneNumberRegex = /^(\+98|0)?9\d{9}$/;
|
|
2
|
+
export {PhoneNumberRegex};
|
package/src/index.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
// Main exports
|
|
2
|
-
export {Widget} from "./app.js";
|
|
3
|
-
export type {WidgetOptions, FetchPaginationRes} from "./types/app.js";
|
|
4
|
-
|
|
5
|
-
// Models
|
|
6
|
-
export {Chatbot} from "./services/chatbot/chatbot.js";
|
|
7
|
-
export {Conversation} from "./services/chat/conversation.js";
|
|
8
|
-
export {CustomerData} from "./services/user/customer-data.js";
|
|
9
|
-
|
|
10
|
-
// Services
|
|
11
|
-
export {VoiceChat} from "./services/voice-chat/model.js";
|
|
12
|
-
export {Chat} from "./services/chat/model.js";
|
|
13
|
-
|
|
14
|
-
// Types
|
|
15
|
-
export type {ConversationStatus, MessageType} from "./types/conversation.js";
|
|
16
|
-
export type {SocketMessage} from "./types/socket.js";
|
|
1
|
+
// Main exports
|
|
2
|
+
export {Widget} from "./app.js";
|
|
3
|
+
export type {WidgetOptions, FetchPaginationRes} from "./types/app.js";
|
|
4
|
+
|
|
5
|
+
// Models
|
|
6
|
+
export {Chatbot} from "./services/chatbot/chatbot.js";
|
|
7
|
+
export {Conversation} from "./services/chat/conversation.js";
|
|
8
|
+
export {CustomerData} from "./services/user/customer-data.js";
|
|
9
|
+
|
|
10
|
+
// Services
|
|
11
|
+
export {VoiceChat} from "./services/voice-chat/model.js";
|
|
12
|
+
export {Chat} from "./services/chat/model.js";
|
|
13
|
+
|
|
14
|
+
// Types
|
|
15
|
+
export type {ConversationStatus, MessageType} from "./types/conversation.js";
|
|
16
|
+
export type {SocketMessage} from "./types/socket.js";
|
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import {NEW_MESSAGE_AUDIO_URL} from "#src/constants/index.js";
|
|
2
|
-
import {setConversationType, switchToConversationLayout, switchToStarterLayout} from "#src/services/ui/fn.js";
|
|
3
|
-
import {playAudio, preloadAudio} from "#src/utils/audio.js";
|
|
4
|
-
import {initMessageElement, showMessageTooltip} from "./message-utils.js";
|
|
5
|
-
import {ConversationMessage} from "@modochats/chat-client";
|
|
6
|
-
|
|
7
|
-
class Conversation {
|
|
8
|
-
constructor() {}
|
|
9
|
-
// d = data
|
|
10
|
-
get d() {
|
|
11
|
-
return window.getMWidget?.().chat?.conversationD;
|
|
12
|
-
}
|
|
13
|
-
addMessage(message: ConversationMessage, options?: {incoming: boolean}) {
|
|
14
|
-
const widget = window.getMWidget?.();
|
|
15
|
-
initMessageElement(message);
|
|
16
|
-
if (options?.incoming) {
|
|
17
|
-
if (widget?.isOpen) this.markAsRead();
|
|
18
|
-
else {
|
|
19
|
-
this.addBadge();
|
|
20
|
-
showMessageTooltip(message);
|
|
21
|
-
playAudio(NEW_MESSAGE_AUDIO_URL).catch(console.warn);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
this.scrollToBottom();
|
|
25
|
-
}
|
|
26
|
-
clearContainerEl() {
|
|
27
|
-
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
28
|
-
if (chatMessagesContainer) {
|
|
29
|
-
chatMessagesContainer.innerHTML = "";
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
addSystemMessage(message: string) {
|
|
33
|
-
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
34
|
-
if (chatMessagesContainer) {
|
|
35
|
-
const systemMessageElement = document.createElement("div");
|
|
36
|
-
systemMessageElement.className = "mw-system-message";
|
|
37
|
-
systemMessageElement.innerHTML = `
|
|
38
|
-
<div class="mw-system-message-content">
|
|
39
|
-
${message}
|
|
40
|
-
</div>
|
|
41
|
-
`;
|
|
42
|
-
chatMessagesContainer.appendChild(systemMessageElement);
|
|
43
|
-
this.scrollToBottom();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
scrollToBottom() {
|
|
48
|
-
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
49
|
-
if (chatMessagesContainer) {
|
|
50
|
-
chatMessagesContainer.scrollTop = chatMessagesContainer.scrollHeight;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
clear() {
|
|
55
|
-
this.d?.clear();
|
|
56
|
-
this.d!.messages = [];
|
|
57
|
-
const widget = window.getMWidget?.();
|
|
58
|
-
localStorage.removeItem(`modo-chat:${widget?.publicKey}-conversation-uuid`);
|
|
59
|
-
this.clearContainerEl();
|
|
60
|
-
switchToStarterLayout();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
onInit() {
|
|
64
|
-
switchToConversationLayout();
|
|
65
|
-
preloadAudio("./audio/new-message.mp3").catch(console.warn);
|
|
66
|
-
if (this?.d?.status) setConversationType(this.d?.status!);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
setStatus() {
|
|
70
|
-
setConversationType(this.d?.status!);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async loadMessages() {
|
|
74
|
-
const widget = window.getMWidget?.();
|
|
75
|
-
const messages = await this.d?.loadMessages();
|
|
76
|
-
const chatMessagesContainer = widget?.container?.querySelector(".mw-chat-messages-con");
|
|
77
|
-
if (chatMessagesContainer) chatMessagesContainer.innerHTML = "";
|
|
78
|
-
for (const message of messages || []) this.addMessage(message);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
addBadge() {
|
|
82
|
-
const widget = window.getMWidget?.();
|
|
83
|
-
if (!widget?.isOpen && this.unreadCount > 0 && widget) {
|
|
84
|
-
const badge = widget.container?.querySelector(".mw-badge");
|
|
85
|
-
const badgeText = widget.container?.querySelector(".mw-badge-text");
|
|
86
|
-
|
|
87
|
-
if (badge && badgeText) {
|
|
88
|
-
// Show the badge
|
|
89
|
-
badge.classList.remove("mw-hidden");
|
|
90
|
-
|
|
91
|
-
// Update badge text
|
|
92
|
-
const displayCount = this.unreadCount > 99 ? "99+" : this.unreadCount.toString();
|
|
93
|
-
badgeText.textContent = displayCount;
|
|
94
|
-
|
|
95
|
-
// Add plus class for 99+ counts
|
|
96
|
-
if (this.unreadCount > 99) {
|
|
97
|
-
badge.classList.add("mw-badge-plus");
|
|
98
|
-
} else {
|
|
99
|
-
badge.classList.remove("mw-badge-plus");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
get unreadCount() {
|
|
106
|
-
return this.d?.unreadCount || 0;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
hideBadge() {
|
|
110
|
-
const widget = window.getMWidget?.();
|
|
111
|
-
const badge = widget?.container?.querySelector(".mw-badge");
|
|
112
|
-
if (badge) {
|
|
113
|
-
badge.classList.add("mw-hidden");
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
hideTooltip() {
|
|
118
|
-
const widget = window.getMWidget?.();
|
|
119
|
-
const tooltip = widget?.container?.querySelector(".mw-toggle-tooltip");
|
|
120
|
-
if (tooltip) {
|
|
121
|
-
tooltip.classList.add("mw-hidden");
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async markAsRead() {
|
|
126
|
-
const widget = window.getMWidget?.();
|
|
127
|
-
await this.d?.markAsRead();
|
|
128
|
-
this.hideBadge();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
get containerElement(): HTMLDivElement | null {
|
|
132
|
-
return document.querySelector(".mw-chat-messages-con");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
export {Conversation};
|
|
1
|
+
import {NEW_MESSAGE_AUDIO_URL} from "#src/constants/index.js";
|
|
2
|
+
import {setConversationType, switchToConversationLayout, switchToStarterLayout} from "#src/services/ui/fn.js";
|
|
3
|
+
import {playAudio, preloadAudio} from "#src/utils/audio.js";
|
|
4
|
+
import {initMessageElement, showMessageTooltip} from "./message-utils.js";
|
|
5
|
+
import {ConversationMessage} from "@modochats/chat-client";
|
|
6
|
+
|
|
7
|
+
class Conversation {
|
|
8
|
+
constructor() {}
|
|
9
|
+
// d = data
|
|
10
|
+
get d() {
|
|
11
|
+
return window.getMWidget?.().chat?.conversationD;
|
|
12
|
+
}
|
|
13
|
+
addMessage(message: ConversationMessage, options?: {incoming: boolean}) {
|
|
14
|
+
const widget = window.getMWidget?.();
|
|
15
|
+
initMessageElement(message);
|
|
16
|
+
if (options?.incoming) {
|
|
17
|
+
if (widget?.isOpen) this.markAsRead();
|
|
18
|
+
else {
|
|
19
|
+
this.addBadge();
|
|
20
|
+
showMessageTooltip(message);
|
|
21
|
+
playAudio(NEW_MESSAGE_AUDIO_URL).catch(console.warn);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
this.scrollToBottom();
|
|
25
|
+
}
|
|
26
|
+
clearContainerEl() {
|
|
27
|
+
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
28
|
+
if (chatMessagesContainer) {
|
|
29
|
+
chatMessagesContainer.innerHTML = "";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
addSystemMessage(message: string) {
|
|
33
|
+
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
34
|
+
if (chatMessagesContainer) {
|
|
35
|
+
const systemMessageElement = document.createElement("div");
|
|
36
|
+
systemMessageElement.className = "mw-system-message";
|
|
37
|
+
systemMessageElement.innerHTML = `
|
|
38
|
+
<div class="mw-system-message-content">
|
|
39
|
+
${message}
|
|
40
|
+
</div>
|
|
41
|
+
`;
|
|
42
|
+
chatMessagesContainer.appendChild(systemMessageElement);
|
|
43
|
+
this.scrollToBottom();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
scrollToBottom() {
|
|
48
|
+
const chatMessagesContainer = document.querySelector(".mw-chat-messages-con");
|
|
49
|
+
if (chatMessagesContainer) {
|
|
50
|
+
chatMessagesContainer.scrollTop = chatMessagesContainer.scrollHeight;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
clear() {
|
|
55
|
+
this.d?.clear();
|
|
56
|
+
this.d!.messages = [];
|
|
57
|
+
const widget = window.getMWidget?.();
|
|
58
|
+
localStorage.removeItem(`modo-chat:${widget?.publicKey}-conversation-uuid`);
|
|
59
|
+
this.clearContainerEl();
|
|
60
|
+
switchToStarterLayout();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
onInit() {
|
|
64
|
+
switchToConversationLayout();
|
|
65
|
+
preloadAudio("./audio/new-message.mp3").catch(console.warn);
|
|
66
|
+
if (this?.d?.status) setConversationType(this.d?.status!);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setStatus() {
|
|
70
|
+
setConversationType(this.d?.status!);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async loadMessages() {
|
|
74
|
+
const widget = window.getMWidget?.();
|
|
75
|
+
const messages = await this.d?.loadMessages();
|
|
76
|
+
const chatMessagesContainer = widget?.container?.querySelector(".mw-chat-messages-con");
|
|
77
|
+
if (chatMessagesContainer) chatMessagesContainer.innerHTML = "";
|
|
78
|
+
for (const message of messages || []) this.addMessage(message);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addBadge() {
|
|
82
|
+
const widget = window.getMWidget?.();
|
|
83
|
+
if (!widget?.isOpen && this.unreadCount > 0 && widget) {
|
|
84
|
+
const badge = widget.container?.querySelector(".mw-badge");
|
|
85
|
+
const badgeText = widget.container?.querySelector(".mw-badge-text");
|
|
86
|
+
|
|
87
|
+
if (badge && badgeText) {
|
|
88
|
+
// Show the badge
|
|
89
|
+
badge.classList.remove("mw-hidden");
|
|
90
|
+
|
|
91
|
+
// Update badge text
|
|
92
|
+
const displayCount = this.unreadCount > 99 ? "99+" : this.unreadCount.toString();
|
|
93
|
+
badgeText.textContent = displayCount;
|
|
94
|
+
|
|
95
|
+
// Add plus class for 99+ counts
|
|
96
|
+
if (this.unreadCount > 99) {
|
|
97
|
+
badge.classList.add("mw-badge-plus");
|
|
98
|
+
} else {
|
|
99
|
+
badge.classList.remove("mw-badge-plus");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get unreadCount() {
|
|
106
|
+
return this.d?.unreadCount || 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
hideBadge() {
|
|
110
|
+
const widget = window.getMWidget?.();
|
|
111
|
+
const badge = widget?.container?.querySelector(".mw-badge");
|
|
112
|
+
if (badge) {
|
|
113
|
+
badge.classList.add("mw-hidden");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
hideTooltip() {
|
|
118
|
+
const widget = window.getMWidget?.();
|
|
119
|
+
const tooltip = widget?.container?.querySelector(".mw-toggle-tooltip");
|
|
120
|
+
if (tooltip) {
|
|
121
|
+
tooltip.classList.add("mw-hidden");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async markAsRead() {
|
|
126
|
+
const widget = window.getMWidget?.();
|
|
127
|
+
await this.d?.markAsRead();
|
|
128
|
+
this.hideBadge();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get containerElement(): HTMLDivElement | null {
|
|
132
|
+
return document.querySelector(".mw-chat-messages-con");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export {Conversation};
|