open-chat-studio-widget 0.8.0 → 0.9.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/README.md +1 -0
- package/dist/cjs/{index-Cf6K60f1.js → index-DDod9Zyw.js} +3 -3
- package/dist/cjs/{index-Cf6K60f1.js.map → index-DDod9Zyw.js.map} +1 -1
- package/dist/cjs/loader.cjs.js +2 -2
- package/dist/cjs/open-chat-studio-widget.cjs.entry.js +175 -22
- package/dist/cjs/open-chat-studio-widget.cjs.entry.js.map +1 -1
- package/dist/cjs/open-chat-studio-widget.cjs.js +2 -2
- package/dist/cjs/open-chat-studio-widget.entry.cjs.js.map +1 -1
- package/dist/collection/components/ocs-chat/ocs-chat.js +82 -4
- package/dist/collection/components/ocs-chat/ocs-chat.js.map +1 -1
- package/dist/collection/services/chat-session-service.js +88 -18
- package/dist/collection/services/chat-session-service.js.map +1 -1
- package/dist/collection/services/file-attachment-manager.js +6 -0
- package/dist/collection/services/file-attachment-manager.js.map +1 -1
- package/dist/components/open-chat-studio-widget.js +175 -21
- package/dist/components/open-chat-studio-widget.js.map +1 -1
- package/dist/esm/{index-DXf2dIht.js → index-iUBQH9om.js} +3 -3
- package/dist/esm/{index-DXf2dIht.js.map → index-iUBQH9om.js.map} +1 -1
- package/dist/esm/loader.js +3 -3
- package/dist/esm/open-chat-studio-widget.entry.js +175 -22
- package/dist/esm/open-chat-studio-widget.entry.js.map +1 -1
- package/dist/esm/open-chat-studio-widget.js +3 -3
- package/dist/open-chat-studio-widget/open-chat-studio-widget.entry.esm.js.map +1 -1
- package/dist/open-chat-studio-widget/open-chat-studio-widget.esm.js +1 -1
- package/dist/open-chat-studio-widget/p-9c925476.entry.js +4 -0
- package/dist/open-chat-studio-widget/p-9c925476.entry.js.map +1 -0
- package/dist/open-chat-studio-widget/{p-DXf2dIht.js → p-iUBQH9om.js} +2 -2
- package/dist/open-chat-studio-widget/{p-DXf2dIht.js.map → p-iUBQH9om.js.map} +1 -1
- package/dist/types/components/ocs-chat/ocs-chat.d.ts +14 -0
- package/dist/types/components.d.ts +8 -0
- package/dist/types/services/chat-session-service.d.ts +21 -0
- package/dist/types/services/file-attachment-manager.d.ts +2 -0
- package/package.json +1 -1
- package/dist/open-chat-studio-widget/p-ff47dabf.entry.js +0 -4
- package/dist/open-chat-studio-widget/p-ff47dabf.entry.js.map +0 -1
|
@@ -4468,6 +4468,8 @@ var ar = {
|
|
|
4468
4468
|
"status.starting": "جارٍ بدء المحادثة...",
|
|
4469
4469
|
"status.typing": "جارٍ تحضير الرد",
|
|
4470
4470
|
"status.uploading": "جارٍ التحميل",
|
|
4471
|
+
"status.sessionExpired": "انتهت صلاحية جلسة الدردشة. سيتم بدء محادثة جديدة — يُرجى إعادة إرسال رسالتك.",
|
|
4472
|
+
"status.sessionError": "لم تعد جلسة الدردشة هذه متاحة.",
|
|
4471
4473
|
"modal.newChatTitle": "بدء محادثة جديدة",
|
|
4472
4474
|
"modal.newChatBody": "بدء محادثة جديدة سيؤدي إلى مسح المحادثة الحالية. هل ترغب بالمتابعة؟",
|
|
4473
4475
|
"modal.cancel": "إلغاء",
|
|
@@ -4500,6 +4502,8 @@ var en = {
|
|
|
4500
4502
|
"status.starting": "Starting chat...",
|
|
4501
4503
|
"status.typing": "Preparing response",
|
|
4502
4504
|
"status.uploading": "Uploading",
|
|
4505
|
+
"status.sessionExpired": "Your chat session expired. Starting a new chat — please resend your message.",
|
|
4506
|
+
"status.sessionError": "This chat session is no longer available.",
|
|
4503
4507
|
"modal.newChatTitle": "Start New Chat",
|
|
4504
4508
|
"modal.newChatBody": "Starting a new chat will clear your current conversation. Continue?",
|
|
4505
4509
|
"modal.cancel": "Cancel",
|
|
@@ -4532,6 +4536,8 @@ var es = {
|
|
|
4532
4536
|
"status.starting": "Iniciando chat...",
|
|
4533
4537
|
"status.typing": "Preparando respuesta",
|
|
4534
4538
|
"status.uploading": "Subiendo",
|
|
4539
|
+
"status.sessionExpired": "Tu sesión de chat ha expirado. Se iniciará un nuevo chat: vuelve a enviar tu mensaje.",
|
|
4540
|
+
"status.sessionError": "Esta sesión de chat ya no está disponible.",
|
|
4535
4541
|
"modal.newChatTitle": "Iniciar Nuevo Chat",
|
|
4536
4542
|
"modal.newChatBody": "Iniciar un nuevo chat borrará tu conversación actual. ¿Continuar?",
|
|
4537
4543
|
"modal.cancel": "Cancelar",
|
|
@@ -4564,6 +4570,8 @@ var fr = {
|
|
|
4564
4570
|
"status.starting": "Démarrage du chat...",
|
|
4565
4571
|
"status.typing": "Préparation de la réponse",
|
|
4566
4572
|
"status.uploading": "Téléversement",
|
|
4573
|
+
"status.sessionExpired": "Votre session de chat a expiré. Un nouveau chat va démarrer — veuillez renvoyer votre message.",
|
|
4574
|
+
"status.sessionError": "Cette session de chat n'est plus disponible.",
|
|
4567
4575
|
"modal.newChatTitle": "Nouveau Chat",
|
|
4568
4576
|
"modal.newChatBody": "Démarrer un nouveau chat effacera votre conversation actuelle. Continuer?",
|
|
4569
4577
|
"modal.cancel": "Annuler",
|
|
@@ -4596,6 +4604,8 @@ var hi = {
|
|
|
4596
4604
|
"status.starting": "चैट शुरू हो रही है...",
|
|
4597
4605
|
"status.typing": "जवाब तैयार किया जा रहा है",
|
|
4598
4606
|
"status.uploading": "अपलोड हो रहा है",
|
|
4607
|
+
"status.sessionExpired": "आपका चैट सत्र समाप्त हो गया है। एक नई चैट शुरू हो रही है — कृपया अपना संदेश फिर से भेजें।",
|
|
4608
|
+
"status.sessionError": "यह चैट सत्र अब उपलब्ध नहीं है।",
|
|
4599
4609
|
"modal.newChatTitle": "नई चैट शुरू करें",
|
|
4600
4610
|
"modal.newChatBody": "नई चैट शुरू करने से आपकी मौजूदा बातचीत हट जाएगी। क्या आप जारी रखना चाहते हैं?",
|
|
4601
4611
|
"modal.cancel": "रद्द करें",
|
|
@@ -4628,6 +4638,8 @@ var it = {
|
|
|
4628
4638
|
"status.starting": "Avvio della chat...",
|
|
4629
4639
|
"status.typing": "Preparazione della risposta",
|
|
4630
4640
|
"status.uploading": "Caricamento",
|
|
4641
|
+
"status.sessionExpired": "La tua sessione di chat è scaduta. Verrà avviata una nuova chat: invia di nuovo il tuo messaggio.",
|
|
4642
|
+
"status.sessionError": "Questa sessione di chat non è più disponibile.",
|
|
4631
4643
|
"modal.newChatTitle": "Avvia nuova chat",
|
|
4632
4644
|
"modal.newChatBody": "Avviare una nuova chat cancellerà la conversazione attuale. Continuare?",
|
|
4633
4645
|
"modal.cancel": "Annulla",
|
|
@@ -4660,6 +4672,8 @@ var pt = {
|
|
|
4660
4672
|
"status.starting": "Iniciando chat...",
|
|
4661
4673
|
"status.typing": "Preparando resposta",
|
|
4662
4674
|
"status.uploading": "Carregando",
|
|
4675
|
+
"status.sessionExpired": "Sua sessão de chat expirou. Iniciando um novo chat — reenvie sua mensagem.",
|
|
4676
|
+
"status.sessionError": "Esta sessão de chat não está mais disponível.",
|
|
4663
4677
|
"modal.newChatTitle": "Iniciar novo chat",
|
|
4664
4678
|
"modal.newChatBody": "Iniciar um novo chat apagará sua conversa atual. Deseja continuar?",
|
|
4665
4679
|
"modal.cancel": "Cancelar",
|
|
@@ -4692,6 +4706,8 @@ var sw = {
|
|
|
4692
4706
|
"status.starting": "Inaanzisha gumzo...",
|
|
4693
4707
|
"status.typing": "Inatayarisha jibu",
|
|
4694
4708
|
"status.uploading": "Inapakia",
|
|
4709
|
+
"status.sessionExpired": "Kipindi chako cha gumzo kimeisha. Gumzo jipya linaanza — tafadhali tuma tena ujumbe wako.",
|
|
4710
|
+
"status.sessionError": "Kipindi hiki cha gumzo hakipatikani tena.",
|
|
4695
4711
|
"modal.newChatTitle": "Anza gumzo jipya",
|
|
4696
4712
|
"modal.newChatBody": "Kuanza gumzo jipya kutafuta gumzo lako la sasa. Je, ungependa kuendelea?",
|
|
4697
4713
|
"modal.cancel": "Ghairi",
|
|
@@ -4724,6 +4740,8 @@ var uk = {
|
|
|
4724
4740
|
"status.starting": "Запуск чату...",
|
|
4725
4741
|
"status.typing": "Готується відповідь",
|
|
4726
4742
|
"status.uploading": "Завантаження",
|
|
4743
|
+
"status.sessionExpired": "Ваш сеанс чату завершився. Розпочинається новий чат — будь ласка, надішліть повідомлення ще раз.",
|
|
4744
|
+
"status.sessionError": "Цей сеанс чату більше недоступний.",
|
|
4727
4745
|
"modal.newChatTitle": "Почати новий чат",
|
|
4728
4746
|
"modal.newChatBody": "Початок нового чату видалить вашу поточну розмову. Продовжити?",
|
|
4729
4747
|
"modal.cancel": "Скасувати",
|
|
@@ -4980,55 +4998,54 @@ function currentDomainMatchesApiBaseUrl(apiBaseUrl) {
|
|
|
4980
4998
|
return window.location.origin === apiBase.origin;
|
|
4981
4999
|
}
|
|
4982
5000
|
|
|
5001
|
+
class SessionAccessError extends Error {
|
|
5002
|
+
constructor(status, code, message) {
|
|
5003
|
+
super(message);
|
|
5004
|
+
this.name = 'SessionAccessError';
|
|
5005
|
+
this.status = status;
|
|
5006
|
+
this.code = code;
|
|
5007
|
+
}
|
|
5008
|
+
}
|
|
4983
5009
|
class ChatSessionService {
|
|
4984
5010
|
constructor(options) {
|
|
4985
5011
|
var _a, _b, _c, _d;
|
|
4986
5012
|
this.apiBaseUrl = options.apiBaseUrl;
|
|
4987
5013
|
this.embedKey = options.embedKey;
|
|
4988
5014
|
this.widgetVersion = options.widgetVersion;
|
|
5015
|
+
this.sessionToken = options.sessionToken;
|
|
4989
5016
|
this.csrfTokenProvider = (_a = options.csrfTokenProvider) !== null && _a !== void 0 ? _a : getCSRFToken;
|
|
4990
5017
|
this.taskPollingIntervalMs = (_b = options.taskPollingIntervalMs) !== null && _b !== void 0 ? _b : 1000;
|
|
4991
5018
|
this.taskPollingMaxAttempts = (_c = options.taskPollingMaxAttempts) !== null && _c !== void 0 ? _c : 120;
|
|
4992
5019
|
this.messagePollingIntervalMs = (_d = options.messagePollingIntervalMs) !== null && _d !== void 0 ? _d : 30000;
|
|
4993
5020
|
}
|
|
4994
5021
|
async startSession(requestBody) {
|
|
4995
|
-
const response = await
|
|
5022
|
+
const response = await this.request(`${this.apiBaseUrl}/api/chat/start/`, {
|
|
4996
5023
|
method: 'POST',
|
|
4997
5024
|
headers: this.getJsonHeaders(),
|
|
4998
5025
|
body: JSON.stringify(requestBody),
|
|
4999
5026
|
});
|
|
5000
5027
|
if (!response.ok) {
|
|
5001
|
-
|
|
5028
|
+
await this.raiseForStatus(response, 'Failed to start session');
|
|
5002
5029
|
}
|
|
5003
5030
|
return response.json();
|
|
5004
5031
|
}
|
|
5005
5032
|
async sendMessage(sessionId, payload) {
|
|
5006
|
-
const response = await
|
|
5033
|
+
const response = await this.request(`${this.apiBaseUrl}/api/chat/${sessionId}/message/`, {
|
|
5007
5034
|
method: 'POST',
|
|
5008
5035
|
headers: this.getJsonHeaders(),
|
|
5009
5036
|
body: JSON.stringify(payload),
|
|
5010
5037
|
});
|
|
5011
5038
|
if (!response.ok) {
|
|
5012
|
-
|
|
5039
|
+
await this.raiseForStatus(response, 'Failed to send message');
|
|
5013
5040
|
}
|
|
5014
5041
|
return response.json();
|
|
5015
5042
|
}
|
|
5016
5043
|
async pollTaskOnce(sessionId, taskId) {
|
|
5017
|
-
const response = await
|
|
5044
|
+
const response = await this.request(`${this.apiBaseUrl}/api/chat/${sessionId}/${taskId}/poll/`, {
|
|
5018
5045
|
headers: this.getCommonHeaders(),
|
|
5019
5046
|
});
|
|
5020
5047
|
if (!response.ok) {
|
|
5021
|
-
|
|
5022
|
-
try {
|
|
5023
|
-
const data = (await response.json());
|
|
5024
|
-
if (data === null || data === void 0 ? void 0 : data.error) {
|
|
5025
|
-
errorMessage = data.error;
|
|
5026
|
-
}
|
|
5027
|
-
}
|
|
5028
|
-
catch (_a) {
|
|
5029
|
-
// non-JSON body; keep statusText fallback
|
|
5030
|
-
}
|
|
5031
|
-
throw new Error(errorMessage);
|
|
5048
|
+
await this.raiseForStatus(response, 'Failed to poll task');
|
|
5032
5049
|
}
|
|
5033
5050
|
return response.json();
|
|
5034
5051
|
}
|
|
@@ -5088,11 +5105,11 @@ class ChatSessionService {
|
|
|
5088
5105
|
if (since) {
|
|
5089
5106
|
url.searchParams.set('since', since);
|
|
5090
5107
|
}
|
|
5091
|
-
const response = await
|
|
5108
|
+
const response = await this.request(url.toString(), {
|
|
5092
5109
|
headers: this.getCommonHeaders(),
|
|
5093
5110
|
});
|
|
5094
5111
|
if (!response.ok) {
|
|
5095
|
-
|
|
5112
|
+
await this.raiseForStatus(response, 'Failed to poll messages');
|
|
5096
5113
|
}
|
|
5097
5114
|
return response.json();
|
|
5098
5115
|
}
|
|
@@ -5149,6 +5166,74 @@ class ChatSessionService {
|
|
|
5149
5166
|
this.messagePollingTimer = undefined;
|
|
5150
5167
|
}
|
|
5151
5168
|
}
|
|
5169
|
+
setSessionToken(token) {
|
|
5170
|
+
this.sessionToken = token;
|
|
5171
|
+
}
|
|
5172
|
+
async request(input, init) {
|
|
5173
|
+
const response = await fetch(input, init);
|
|
5174
|
+
this.checkSunsetHeaders(response);
|
|
5175
|
+
return response;
|
|
5176
|
+
}
|
|
5177
|
+
/**
|
|
5178
|
+
* Log a deprecation warning (RFC 8594 `Deprecation`/`Sunset`/`Link` headers)
|
|
5179
|
+
* when the server reports that this widget version is deprecated. Warns
|
|
5180
|
+
* during the deprecation window and errors once the sunset date has passed.
|
|
5181
|
+
* Logs at most once per level so polling does not flood the console.
|
|
5182
|
+
*/
|
|
5183
|
+
checkSunsetHeaders(response) {
|
|
5184
|
+
const headers = response === null || response === void 0 ? void 0 : response.headers;
|
|
5185
|
+
if (!headers || typeof headers.get !== 'function') {
|
|
5186
|
+
return;
|
|
5187
|
+
}
|
|
5188
|
+
if (headers.get('Deprecation') !== 'true') {
|
|
5189
|
+
return;
|
|
5190
|
+
}
|
|
5191
|
+
const sunsetAt = this.parseSunsetDate(headers.get('Sunset'));
|
|
5192
|
+
const pastSunset = sunsetAt !== null && Date.now() >= sunsetAt.getTime();
|
|
5193
|
+
const level = pastSunset ? 'error' : 'warn';
|
|
5194
|
+
if (this.loggedSunsetLevel === level) {
|
|
5195
|
+
return;
|
|
5196
|
+
}
|
|
5197
|
+
this.loggedSunsetLevel = level;
|
|
5198
|
+
const upgradeUrl = this.parseSuccessorUrl(headers.get('Link'));
|
|
5199
|
+
const upgradeSuffix = upgradeUrl ? ` Upgrade: ${upgradeUrl}` : '';
|
|
5200
|
+
const sunsetText = sunsetAt ? sunsetAt.toUTCString() : 'an upcoming date';
|
|
5201
|
+
if (level === 'error') {
|
|
5202
|
+
console.error(`[open-chat-studio-widget] Widget version ${this.widgetVersion} is past its sunset date ` + `(${sunsetText}) and may stop working.${upgradeSuffix}`);
|
|
5203
|
+
}
|
|
5204
|
+
else {
|
|
5205
|
+
console.warn(`[open-chat-studio-widget] Widget version ${this.widgetVersion} is deprecated and will stop ` + `working after ${sunsetText}.${upgradeSuffix}`);
|
|
5206
|
+
}
|
|
5207
|
+
}
|
|
5208
|
+
parseSunsetDate(sunset) {
|
|
5209
|
+
if (!sunset) {
|
|
5210
|
+
return null;
|
|
5211
|
+
}
|
|
5212
|
+
const parsed = new Date(sunset);
|
|
5213
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
5214
|
+
}
|
|
5215
|
+
parseSuccessorUrl(link) {
|
|
5216
|
+
const match = link === null || link === void 0 ? void 0 : link.match(/<([^>]+)>\s*;\s*rel="?successor-version"?/);
|
|
5217
|
+
return match === null || match === void 0 ? void 0 : match[1];
|
|
5218
|
+
}
|
|
5219
|
+
async raiseForStatus(response, fallbackPrefix) {
|
|
5220
|
+
let message = `${fallbackPrefix}: ${response.statusText}`;
|
|
5221
|
+
let code;
|
|
5222
|
+
try {
|
|
5223
|
+
const data = (await response.json());
|
|
5224
|
+
if (data === null || data === void 0 ? void 0 : data.error) {
|
|
5225
|
+
message = data.error;
|
|
5226
|
+
}
|
|
5227
|
+
code = data === null || data === void 0 ? void 0 : data.code;
|
|
5228
|
+
}
|
|
5229
|
+
catch (_a) {
|
|
5230
|
+
// non-JSON body; keep statusText fallback
|
|
5231
|
+
}
|
|
5232
|
+
if (response.status === 403) {
|
|
5233
|
+
throw new SessionAccessError(response.status, code, message);
|
|
5234
|
+
}
|
|
5235
|
+
throw new Error(message);
|
|
5236
|
+
}
|
|
5152
5237
|
getJsonHeaders() {
|
|
5153
5238
|
const headers = this.getCommonHeaders();
|
|
5154
5239
|
headers['Content-Type'] = 'application/json';
|
|
@@ -5165,6 +5250,9 @@ class ChatSessionService {
|
|
|
5165
5250
|
if (this.embedKey) {
|
|
5166
5251
|
headers['X-Embed-Key'] = this.embedKey;
|
|
5167
5252
|
}
|
|
5253
|
+
if (this.sessionToken) {
|
|
5254
|
+
headers['X-Session-Token'] = this.sessionToken;
|
|
5255
|
+
}
|
|
5168
5256
|
return headers;
|
|
5169
5257
|
}
|
|
5170
5258
|
}
|
|
@@ -5231,8 +5319,13 @@ class FileAttachmentManager {
|
|
|
5231
5319
|
formData.append('participant_name', context.participantName);
|
|
5232
5320
|
}
|
|
5233
5321
|
try {
|
|
5322
|
+
const headers = {};
|
|
5323
|
+
if (context.sessionToken) {
|
|
5324
|
+
headers['X-Session-Token'] = context.sessionToken;
|
|
5325
|
+
}
|
|
5234
5326
|
const response = await fetch(`${context.apiBaseUrl}/api/chat/${context.sessionId}/upload/`, {
|
|
5235
5327
|
method: 'POST',
|
|
5328
|
+
headers,
|
|
5236
5329
|
body: formData,
|
|
5237
5330
|
});
|
|
5238
5331
|
if (!response.ok) {
|
|
@@ -5242,6 +5335,7 @@ class FileAttachmentManager {
|
|
|
5242
5335
|
selectedFiles: this.markPendingFilesWithError(existingFiles, errorMessage),
|
|
5243
5336
|
uploadedIds,
|
|
5244
5337
|
errorMessage,
|
|
5338
|
+
tokenRejected: response.status === 403,
|
|
5245
5339
|
};
|
|
5246
5340
|
}
|
|
5247
5341
|
const data = await this.safeJson(response);
|
|
@@ -5529,13 +5623,15 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5529
5623
|
if (this.isSessionBound()) {
|
|
5530
5624
|
// Bound to an externally-managed session: the host page is the source of truth.
|
|
5531
5625
|
this.activeSessionId = this.sessionId;
|
|
5626
|
+
this.applySessionToken(this.sessionToken);
|
|
5532
5627
|
}
|
|
5533
5628
|
else if (this.persistentSession && this.isLocalStorageAvailable()) {
|
|
5534
5629
|
// Always try to load existing session if localStorage is available
|
|
5535
|
-
const { sessionId, messages } = this.loadSessionFromStorage();
|
|
5630
|
+
const { sessionId, messages, sessionToken } = this.loadSessionFromStorage();
|
|
5536
5631
|
if (sessionId && messages) {
|
|
5537
5632
|
this.activeSessionId = sessionId;
|
|
5538
5633
|
this.messages = messages;
|
|
5634
|
+
this.applySessionToken(sessionToken);
|
|
5539
5635
|
}
|
|
5540
5636
|
}
|
|
5541
5637
|
this.parseWelcomeMessages();
|
|
@@ -5584,6 +5680,11 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5584
5680
|
this.removeButtonEventListeners();
|
|
5585
5681
|
window.removeEventListener('resize', this.handleWindowResize);
|
|
5586
5682
|
}
|
|
5683
|
+
applySessionToken(token) {
|
|
5684
|
+
var _a;
|
|
5685
|
+
this.currentSessionToken = token;
|
|
5686
|
+
(_a = this.chatService) === null || _a === void 0 ? void 0 : _a.setSessionToken(token);
|
|
5687
|
+
}
|
|
5587
5688
|
getChatService() {
|
|
5588
5689
|
if (!this.chatService) {
|
|
5589
5690
|
this.chatService = new ChatSessionService({
|
|
@@ -5593,6 +5694,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5593
5694
|
taskPollingIntervalMs: OcsChat.TASK_POLLING_INTERVAL_MS,
|
|
5594
5695
|
taskPollingMaxAttempts: OcsChat.TASK_POLLING_MAX_ATTEMPTS,
|
|
5595
5696
|
messagePollingIntervalMs: OcsChat.MESSAGE_POLLING_INTERVAL_MS,
|
|
5697
|
+
sessionToken: this.currentSessionToken,
|
|
5596
5698
|
});
|
|
5597
5699
|
}
|
|
5598
5700
|
return this.chatService;
|
|
@@ -5608,6 +5710,27 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5608
5710
|
this.saveSessionToStorage();
|
|
5609
5711
|
this.scrollToBottom();
|
|
5610
5712
|
}
|
|
5713
|
+
/**
|
|
5714
|
+
* Recover from a rejected session token (403). Unbound widgets discard the
|
|
5715
|
+
* dead session/token, show a notice, and start fresh on the next send; bound
|
|
5716
|
+
* widgets cannot restart a host-owned session, so they surface an error.
|
|
5717
|
+
*/
|
|
5718
|
+
handleSessionAccessError() {
|
|
5719
|
+
this.cleanup();
|
|
5720
|
+
this.isLoading = false;
|
|
5721
|
+
this.isTyping = false;
|
|
5722
|
+
this.isUploadingFiles = false;
|
|
5723
|
+
this.typingProgressMessage = '';
|
|
5724
|
+
if (this.isSessionBound()) {
|
|
5725
|
+
this.addErrorMessage(this.translationManager.get('status.sessionError', 'This chat session is no longer available.'));
|
|
5726
|
+
return;
|
|
5727
|
+
}
|
|
5728
|
+
this.sessionEpoch += 1;
|
|
5729
|
+
this.activeSessionId = undefined;
|
|
5730
|
+
this.applySessionToken(undefined);
|
|
5731
|
+
this.clearSessionStorage();
|
|
5732
|
+
this.addErrorMessage(this.translationManager.get('status.sessionExpired', 'Your chat session expired. Starting a new chat — please resend your message.'));
|
|
5733
|
+
}
|
|
5611
5734
|
handleError(errorText) {
|
|
5612
5735
|
// show as system message
|
|
5613
5736
|
this.addErrorMessage(errorText);
|
|
@@ -5680,12 +5803,14 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5680
5803
|
this.currentPollTaskId = '';
|
|
5681
5804
|
}
|
|
5682
5805
|
async startSession() {
|
|
5806
|
+
var _a;
|
|
5683
5807
|
const epoch = this.sessionEpoch;
|
|
5684
5808
|
try {
|
|
5685
5809
|
this.isLoading = true;
|
|
5686
5810
|
const userId = this.getOrGenerateUserId();
|
|
5687
5811
|
const requestBody = {
|
|
5688
5812
|
chatbot_id: this.chatbotId,
|
|
5813
|
+
use_session_token: true,
|
|
5689
5814
|
session_data: {
|
|
5690
5815
|
source: 'widget',
|
|
5691
5816
|
page_url: window.location.href,
|
|
@@ -5702,6 +5827,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5702
5827
|
if (epoch !== this.sessionEpoch)
|
|
5703
5828
|
return;
|
|
5704
5829
|
this.activeSessionId = data.session_id;
|
|
5830
|
+
this.applySessionToken((_a = data.session_token) !== null && _a !== void 0 ? _a : undefined);
|
|
5705
5831
|
this.saveSessionToStorage();
|
|
5706
5832
|
this.startMessagePolling();
|
|
5707
5833
|
}
|
|
@@ -5734,6 +5860,10 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5734
5860
|
catch (error) {
|
|
5735
5861
|
if (epoch !== this.sessionEpoch)
|
|
5736
5862
|
return;
|
|
5863
|
+
if (error instanceof SessionAccessError) {
|
|
5864
|
+
this.handleSessionAccessError();
|
|
5865
|
+
return;
|
|
5866
|
+
}
|
|
5737
5867
|
console.warn('Failed to load chat history:', error);
|
|
5738
5868
|
}
|
|
5739
5869
|
this.startMessagePolling();
|
|
@@ -5749,8 +5879,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5749
5879
|
sessionId: this.activeSessionId,
|
|
5750
5880
|
participantId: this.getOrGenerateUserId(),
|
|
5751
5881
|
participantName: this.userName,
|
|
5882
|
+
sessionToken: this.currentSessionToken,
|
|
5752
5883
|
});
|
|
5753
5884
|
this.selectedFiles = uploadResult.selectedFiles;
|
|
5885
|
+
if (uploadResult.tokenRejected) {
|
|
5886
|
+
throw new SessionAccessError(403, 'session_token_required', uploadResult.errorMessage || 'Session token rejected');
|
|
5887
|
+
}
|
|
5754
5888
|
return uploadResult.uploadedIds;
|
|
5755
5889
|
}
|
|
5756
5890
|
finally {
|
|
@@ -5842,6 +5976,10 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5842
5976
|
catch (error) {
|
|
5843
5977
|
if (epoch !== this.sessionEpoch)
|
|
5844
5978
|
return;
|
|
5979
|
+
if (error instanceof SessionAccessError) {
|
|
5980
|
+
this.handleSessionAccessError();
|
|
5981
|
+
return;
|
|
5982
|
+
}
|
|
5845
5983
|
const errorText = error instanceof Error ? error.message : 'Failed to send message';
|
|
5846
5984
|
this.handleError(errorText);
|
|
5847
5985
|
}
|
|
@@ -6016,8 +6154,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6016
6154
|
},
|
|
6017
6155
|
onError: error => {
|
|
6018
6156
|
this.typingProgressMessage = '';
|
|
6019
|
-
this.handleError(error.message);
|
|
6020
6157
|
this.taskPollingHandle = undefined;
|
|
6158
|
+
if (error instanceof SessionAccessError) {
|
|
6159
|
+
this.handleSessionAccessError();
|
|
6160
|
+
return;
|
|
6161
|
+
}
|
|
6162
|
+
this.handleError(error.message);
|
|
6021
6163
|
this.startMessagePolling();
|
|
6022
6164
|
},
|
|
6023
6165
|
});
|
|
@@ -6379,6 +6521,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6379
6521
|
messages: `ocs-chat-messages-${this.chatbotId}`,
|
|
6380
6522
|
lastActivity: `ocs-chat-activity-${this.chatbotId}`,
|
|
6381
6523
|
visible: `ocs-chat-visible-${this.chatbotId}`,
|
|
6524
|
+
sessionToken: `ocs-chat-token-${this.chatbotId}`,
|
|
6382
6525
|
};
|
|
6383
6526
|
}
|
|
6384
6527
|
saveSessionToStorage() {
|
|
@@ -6390,6 +6533,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6390
6533
|
if (this.activeSessionId) {
|
|
6391
6534
|
localStorage.setItem(keys.sessionId, this.activeSessionId);
|
|
6392
6535
|
localStorage.setItem(keys.lastActivity, new Date().toISOString());
|
|
6536
|
+
if (this.currentSessionToken) {
|
|
6537
|
+
localStorage.setItem(keys.sessionToken, this.currentSessionToken);
|
|
6538
|
+
}
|
|
6539
|
+
else {
|
|
6540
|
+
localStorage.removeItem(keys.sessionToken);
|
|
6541
|
+
}
|
|
6393
6542
|
}
|
|
6394
6543
|
localStorage.setItem(keys.messages, JSON.stringify(this.messages));
|
|
6395
6544
|
}
|
|
@@ -6398,6 +6547,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6398
6547
|
}
|
|
6399
6548
|
}
|
|
6400
6549
|
loadSessionFromStorage() {
|
|
6550
|
+
var _a;
|
|
6401
6551
|
const keys = this.getStorageKeys();
|
|
6402
6552
|
try {
|
|
6403
6553
|
if (this.persistentSessionExpire > 0) {
|
|
@@ -6425,7 +6575,8 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6425
6575
|
messages = [];
|
|
6426
6576
|
}
|
|
6427
6577
|
}
|
|
6428
|
-
|
|
6578
|
+
const sessionToken = (_a = localStorage.getItem(keys.sessionToken)) !== null && _a !== void 0 ? _a : undefined;
|
|
6579
|
+
return { sessionId, messages, sessionToken };
|
|
6429
6580
|
}
|
|
6430
6581
|
catch (error) {
|
|
6431
6582
|
// fall back to starting a new session
|
|
@@ -6497,6 +6648,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6497
6648
|
localStorage.removeItem(keys.messages);
|
|
6498
6649
|
localStorage.removeItem(keys.lastActivity);
|
|
6499
6650
|
localStorage.removeItem(keys.visible);
|
|
6651
|
+
localStorage.removeItem(keys.sessionToken);
|
|
6500
6652
|
}
|
|
6501
6653
|
catch (error) {
|
|
6502
6654
|
console.warn('Failed to clear chat session from localStorage:', error);
|
|
@@ -6538,6 +6690,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6538
6690
|
// A session provided by the host page (session-id prop) cannot be cleared;
|
|
6539
6691
|
// stay bound to it. Unbound widgets start a new session on the next message.
|
|
6540
6692
|
this.activeSessionId = this.sessionId;
|
|
6693
|
+
this.applySessionToken(this.isSessionBound() ? this.sessionToken : undefined);
|
|
6541
6694
|
this.messages = [];
|
|
6542
6695
|
this.isTyping = false;
|
|
6543
6696
|
this.currentPollTaskId = '';
|
|
@@ -6603,6 +6756,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6603
6756
|
"pageContext": [1040, "page-context"],
|
|
6604
6757
|
"versionNumber": [2, "version-number"],
|
|
6605
6758
|
"sessionId": [1, "session-id"],
|
|
6759
|
+
"sessionToken": [1, "session-token"],
|
|
6606
6760
|
"error": [32],
|
|
6607
6761
|
"messages": [32],
|
|
6608
6762
|
"activeSessionId": [32],
|