open-chat-studio-widget 0.8.0 → 0.9.1
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-fFSp-Z_h.js} +3 -3
- package/dist/cjs/{index-Cf6K60f1.js.map → index-fFSp-Z_h.js.map} +1 -1
- package/dist/cjs/loader.cjs.js +2 -2
- package/dist/cjs/open-chat-studio-widget.cjs.entry.js +179 -24
- 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 +95 -20
- package/dist/collection/services/chat-session-service.js.map +1 -1
- package/dist/collection/services/file-attachment-manager.js +3 -0
- package/dist/collection/services/file-attachment-manager.js.map +1 -1
- package/dist/components/open-chat-studio-widget.js +179 -23
- package/dist/components/open-chat-studio-widget.js.map +1 -1
- package/dist/esm/{index-DXf2dIht.js → index-ythTKHg-.js} +3 -3
- package/dist/esm/{index-DXf2dIht.js.map → index-ythTKHg-.js.map} +1 -1
- package/dist/esm/loader.js +3 -3
- package/dist/esm/open-chat-studio-widget.entry.js +179 -24
- 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-2d31a15c.entry.js +4 -0
- package/dist/open-chat-studio-widget/p-2d31a15c.entry.js.map +1 -0
- package/dist/open-chat-studio-widget/{p-DXf2dIht.js → p-ythTKHg-.js} +2 -2
- package/dist/open-chat-studio-widget/{p-DXf2dIht.js.map → p-ythTKHg-.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 +23 -0
- package/dist/types/services/file-attachment-manager.d.ts +3 -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,15 +5166,88 @@ class ChatSessionService {
|
|
|
5149
5166
|
this.messagePollingTimer = undefined;
|
|
5150
5167
|
}
|
|
5151
5168
|
}
|
|
5152
|
-
|
|
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
|
+
}
|
|
5237
|
+
/** Headers for multipart requests (no Content-Type — fetch sets the boundary). */
|
|
5238
|
+
getUploadHeaders() {
|
|
5153
5239
|
const headers = this.getCommonHeaders();
|
|
5154
|
-
headers['Content-Type'] = 'application/json';
|
|
5155
5240
|
const csrfToken = this.csrfTokenProvider(this.apiBaseUrl);
|
|
5156
5241
|
if (csrfToken) {
|
|
5157
5242
|
headers['X-CSRFToken'] = csrfToken;
|
|
5158
5243
|
}
|
|
5159
5244
|
return headers;
|
|
5160
5245
|
}
|
|
5246
|
+
getJsonHeaders() {
|
|
5247
|
+
const headers = this.getUploadHeaders();
|
|
5248
|
+
headers['Content-Type'] = 'application/json';
|
|
5249
|
+
return headers;
|
|
5250
|
+
}
|
|
5161
5251
|
getCommonHeaders() {
|
|
5162
5252
|
const headers = {
|
|
5163
5253
|
'x-ocs-widget-version': this.widgetVersion,
|
|
@@ -5165,6 +5255,9 @@ class ChatSessionService {
|
|
|
5165
5255
|
if (this.embedKey) {
|
|
5166
5256
|
headers['X-Embed-Key'] = this.embedKey;
|
|
5167
5257
|
}
|
|
5258
|
+
if (this.sessionToken) {
|
|
5259
|
+
headers['X-Session-Token'] = this.sessionToken;
|
|
5260
|
+
}
|
|
5168
5261
|
return headers;
|
|
5169
5262
|
}
|
|
5170
5263
|
}
|
|
@@ -5214,6 +5307,7 @@ class FileAttachmentManager {
|
|
|
5214
5307
|
});
|
|
5215
5308
|
}
|
|
5216
5309
|
async uploadPendingFiles(existingFiles, context) {
|
|
5310
|
+
var _a;
|
|
5217
5311
|
if (existingFiles.length === 0) {
|
|
5218
5312
|
return { selectedFiles: existingFiles, uploadedIds: [] };
|
|
5219
5313
|
}
|
|
@@ -5233,6 +5327,7 @@ class FileAttachmentManager {
|
|
|
5233
5327
|
try {
|
|
5234
5328
|
const response = await fetch(`${context.apiBaseUrl}/api/chat/${context.sessionId}/upload/`, {
|
|
5235
5329
|
method: 'POST',
|
|
5330
|
+
headers: (_a = context.headers) !== null && _a !== void 0 ? _a : {},
|
|
5236
5331
|
body: formData,
|
|
5237
5332
|
});
|
|
5238
5333
|
if (!response.ok) {
|
|
@@ -5242,6 +5337,7 @@ class FileAttachmentManager {
|
|
|
5242
5337
|
selectedFiles: this.markPendingFilesWithError(existingFiles, errorMessage),
|
|
5243
5338
|
uploadedIds,
|
|
5244
5339
|
errorMessage,
|
|
5340
|
+
tokenRejected: response.status === 403,
|
|
5245
5341
|
};
|
|
5246
5342
|
}
|
|
5247
5343
|
const data = await this.safeJson(response);
|
|
@@ -5529,13 +5625,15 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5529
5625
|
if (this.isSessionBound()) {
|
|
5530
5626
|
// Bound to an externally-managed session: the host page is the source of truth.
|
|
5531
5627
|
this.activeSessionId = this.sessionId;
|
|
5628
|
+
this.applySessionToken(this.sessionToken);
|
|
5532
5629
|
}
|
|
5533
5630
|
else if (this.persistentSession && this.isLocalStorageAvailable()) {
|
|
5534
5631
|
// Always try to load existing session if localStorage is available
|
|
5535
|
-
const { sessionId, messages } = this.loadSessionFromStorage();
|
|
5632
|
+
const { sessionId, messages, sessionToken } = this.loadSessionFromStorage();
|
|
5536
5633
|
if (sessionId && messages) {
|
|
5537
5634
|
this.activeSessionId = sessionId;
|
|
5538
5635
|
this.messages = messages;
|
|
5636
|
+
this.applySessionToken(sessionToken);
|
|
5539
5637
|
}
|
|
5540
5638
|
}
|
|
5541
5639
|
this.parseWelcomeMessages();
|
|
@@ -5584,6 +5682,11 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5584
5682
|
this.removeButtonEventListeners();
|
|
5585
5683
|
window.removeEventListener('resize', this.handleWindowResize);
|
|
5586
5684
|
}
|
|
5685
|
+
applySessionToken(token) {
|
|
5686
|
+
var _a;
|
|
5687
|
+
this.currentSessionToken = token;
|
|
5688
|
+
(_a = this.chatService) === null || _a === void 0 ? void 0 : _a.setSessionToken(token);
|
|
5689
|
+
}
|
|
5587
5690
|
getChatService() {
|
|
5588
5691
|
if (!this.chatService) {
|
|
5589
5692
|
this.chatService = new ChatSessionService({
|
|
@@ -5593,6 +5696,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5593
5696
|
taskPollingIntervalMs: OcsChat.TASK_POLLING_INTERVAL_MS,
|
|
5594
5697
|
taskPollingMaxAttempts: OcsChat.TASK_POLLING_MAX_ATTEMPTS,
|
|
5595
5698
|
messagePollingIntervalMs: OcsChat.MESSAGE_POLLING_INTERVAL_MS,
|
|
5699
|
+
sessionToken: this.currentSessionToken,
|
|
5596
5700
|
});
|
|
5597
5701
|
}
|
|
5598
5702
|
return this.chatService;
|
|
@@ -5608,6 +5712,27 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5608
5712
|
this.saveSessionToStorage();
|
|
5609
5713
|
this.scrollToBottom();
|
|
5610
5714
|
}
|
|
5715
|
+
/**
|
|
5716
|
+
* Recover from a rejected session token (403). Unbound widgets discard the
|
|
5717
|
+
* dead session/token, show a notice, and start fresh on the next send; bound
|
|
5718
|
+
* widgets cannot restart a host-owned session, so they surface an error.
|
|
5719
|
+
*/
|
|
5720
|
+
handleSessionAccessError() {
|
|
5721
|
+
this.cleanup();
|
|
5722
|
+
this.isLoading = false;
|
|
5723
|
+
this.isTyping = false;
|
|
5724
|
+
this.isUploadingFiles = false;
|
|
5725
|
+
this.typingProgressMessage = '';
|
|
5726
|
+
if (this.isSessionBound()) {
|
|
5727
|
+
this.addErrorMessage(this.translationManager.get('status.sessionError', 'This chat session is no longer available.'));
|
|
5728
|
+
return;
|
|
5729
|
+
}
|
|
5730
|
+
this.sessionEpoch += 1;
|
|
5731
|
+
this.activeSessionId = undefined;
|
|
5732
|
+
this.applySessionToken(undefined);
|
|
5733
|
+
this.clearSessionStorage();
|
|
5734
|
+
this.addErrorMessage(this.translationManager.get('status.sessionExpired', 'Your chat session expired. Starting a new chat — please resend your message.'));
|
|
5735
|
+
}
|
|
5611
5736
|
handleError(errorText) {
|
|
5612
5737
|
// show as system message
|
|
5613
5738
|
this.addErrorMessage(errorText);
|
|
@@ -5680,12 +5805,14 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5680
5805
|
this.currentPollTaskId = '';
|
|
5681
5806
|
}
|
|
5682
5807
|
async startSession() {
|
|
5808
|
+
var _a;
|
|
5683
5809
|
const epoch = this.sessionEpoch;
|
|
5684
5810
|
try {
|
|
5685
5811
|
this.isLoading = true;
|
|
5686
5812
|
const userId = this.getOrGenerateUserId();
|
|
5687
5813
|
const requestBody = {
|
|
5688
5814
|
chatbot_id: this.chatbotId,
|
|
5815
|
+
use_session_token: true,
|
|
5689
5816
|
session_data: {
|
|
5690
5817
|
source: 'widget',
|
|
5691
5818
|
page_url: window.location.href,
|
|
@@ -5702,6 +5829,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5702
5829
|
if (epoch !== this.sessionEpoch)
|
|
5703
5830
|
return;
|
|
5704
5831
|
this.activeSessionId = data.session_id;
|
|
5832
|
+
this.applySessionToken((_a = data.session_token) !== null && _a !== void 0 ? _a : undefined);
|
|
5705
5833
|
this.saveSessionToStorage();
|
|
5706
5834
|
this.startMessagePolling();
|
|
5707
5835
|
}
|
|
@@ -5734,6 +5862,10 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5734
5862
|
catch (error) {
|
|
5735
5863
|
if (epoch !== this.sessionEpoch)
|
|
5736
5864
|
return;
|
|
5865
|
+
if (error instanceof SessionAccessError) {
|
|
5866
|
+
this.handleSessionAccessError();
|
|
5867
|
+
return;
|
|
5868
|
+
}
|
|
5737
5869
|
console.warn('Failed to load chat history:', error);
|
|
5738
5870
|
}
|
|
5739
5871
|
this.startMessagePolling();
|
|
@@ -5749,8 +5881,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5749
5881
|
sessionId: this.activeSessionId,
|
|
5750
5882
|
participantId: this.getOrGenerateUserId(),
|
|
5751
5883
|
participantName: this.userName,
|
|
5884
|
+
headers: this.getChatService().getUploadHeaders(),
|
|
5752
5885
|
});
|
|
5753
5886
|
this.selectedFiles = uploadResult.selectedFiles;
|
|
5887
|
+
if (uploadResult.tokenRejected) {
|
|
5888
|
+
throw new SessionAccessError(403, 'session_token_required', uploadResult.errorMessage || 'Session token rejected');
|
|
5889
|
+
}
|
|
5754
5890
|
return uploadResult.uploadedIds;
|
|
5755
5891
|
}
|
|
5756
5892
|
finally {
|
|
@@ -5842,6 +5978,10 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
5842
5978
|
catch (error) {
|
|
5843
5979
|
if (epoch !== this.sessionEpoch)
|
|
5844
5980
|
return;
|
|
5981
|
+
if (error instanceof SessionAccessError) {
|
|
5982
|
+
this.handleSessionAccessError();
|
|
5983
|
+
return;
|
|
5984
|
+
}
|
|
5845
5985
|
const errorText = error instanceof Error ? error.message : 'Failed to send message';
|
|
5846
5986
|
this.handleError(errorText);
|
|
5847
5987
|
}
|
|
@@ -6016,8 +6156,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6016
6156
|
},
|
|
6017
6157
|
onError: error => {
|
|
6018
6158
|
this.typingProgressMessage = '';
|
|
6019
|
-
this.handleError(error.message);
|
|
6020
6159
|
this.taskPollingHandle = undefined;
|
|
6160
|
+
if (error instanceof SessionAccessError) {
|
|
6161
|
+
this.handleSessionAccessError();
|
|
6162
|
+
return;
|
|
6163
|
+
}
|
|
6164
|
+
this.handleError(error.message);
|
|
6021
6165
|
this.startMessagePolling();
|
|
6022
6166
|
},
|
|
6023
6167
|
});
|
|
@@ -6379,6 +6523,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6379
6523
|
messages: `ocs-chat-messages-${this.chatbotId}`,
|
|
6380
6524
|
lastActivity: `ocs-chat-activity-${this.chatbotId}`,
|
|
6381
6525
|
visible: `ocs-chat-visible-${this.chatbotId}`,
|
|
6526
|
+
sessionToken: `ocs-chat-token-${this.chatbotId}`,
|
|
6382
6527
|
};
|
|
6383
6528
|
}
|
|
6384
6529
|
saveSessionToStorage() {
|
|
@@ -6390,6 +6535,12 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6390
6535
|
if (this.activeSessionId) {
|
|
6391
6536
|
localStorage.setItem(keys.sessionId, this.activeSessionId);
|
|
6392
6537
|
localStorage.setItem(keys.lastActivity, new Date().toISOString());
|
|
6538
|
+
if (this.currentSessionToken) {
|
|
6539
|
+
localStorage.setItem(keys.sessionToken, this.currentSessionToken);
|
|
6540
|
+
}
|
|
6541
|
+
else {
|
|
6542
|
+
localStorage.removeItem(keys.sessionToken);
|
|
6543
|
+
}
|
|
6393
6544
|
}
|
|
6394
6545
|
localStorage.setItem(keys.messages, JSON.stringify(this.messages));
|
|
6395
6546
|
}
|
|
@@ -6398,6 +6549,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6398
6549
|
}
|
|
6399
6550
|
}
|
|
6400
6551
|
loadSessionFromStorage() {
|
|
6552
|
+
var _a;
|
|
6401
6553
|
const keys = this.getStorageKeys();
|
|
6402
6554
|
try {
|
|
6403
6555
|
if (this.persistentSessionExpire > 0) {
|
|
@@ -6425,7 +6577,8 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6425
6577
|
messages = [];
|
|
6426
6578
|
}
|
|
6427
6579
|
}
|
|
6428
|
-
|
|
6580
|
+
const sessionToken = (_a = localStorage.getItem(keys.sessionToken)) !== null && _a !== void 0 ? _a : undefined;
|
|
6581
|
+
return { sessionId, messages, sessionToken };
|
|
6429
6582
|
}
|
|
6430
6583
|
catch (error) {
|
|
6431
6584
|
// fall back to starting a new session
|
|
@@ -6497,6 +6650,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6497
6650
|
localStorage.removeItem(keys.messages);
|
|
6498
6651
|
localStorage.removeItem(keys.lastActivity);
|
|
6499
6652
|
localStorage.removeItem(keys.visible);
|
|
6653
|
+
localStorage.removeItem(keys.sessionToken);
|
|
6500
6654
|
}
|
|
6501
6655
|
catch (error) {
|
|
6502
6656
|
console.warn('Failed to clear chat session from localStorage:', error);
|
|
@@ -6538,6 +6692,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6538
6692
|
// A session provided by the host page (session-id prop) cannot be cleared;
|
|
6539
6693
|
// stay bound to it. Unbound widgets start a new session on the next message.
|
|
6540
6694
|
this.activeSessionId = this.sessionId;
|
|
6695
|
+
this.applySessionToken(this.isSessionBound() ? this.sessionToken : undefined);
|
|
6541
6696
|
this.messages = [];
|
|
6542
6697
|
this.isTyping = false;
|
|
6543
6698
|
this.currentPollTaskId = '';
|
|
@@ -6603,6 +6758,7 @@ const OcsChat = /*@__PURE__*/ proxyCustomElement(class OcsChat extends HTMLEleme
|
|
|
6603
6758
|
"pageContext": [1040, "page-context"],
|
|
6604
6759
|
"versionNumber": [2, "version-number"],
|
|
6605
6760
|
"sessionId": [1, "session-id"],
|
|
6761
|
+
"sessionToken": [1, "session-token"],
|
|
6606
6762
|
"error": [32],
|
|
6607
6763
|
"messages": [32],
|
|
6608
6764
|
"activeSessionId": [32],
|