@blotoutio/providers-shop-gpt-sdk 1.8.0 → 1.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/index.cjs.js +279 -77
- package/index.js +279 -77
- package/index.mjs +279 -77
- package/package.json +1 -1
package/index.mjs
CHANGED
@@ -470,7 +470,6 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
|
|
470
470
|
return url;
|
471
471
|
};
|
472
472
|
const processQuery = async (query, threadId, productHandle) => {
|
473
|
-
var _a;
|
474
473
|
const response = await fetchImpl(getURL('/query'), {
|
475
474
|
method: 'POST',
|
476
475
|
headers: getHeaders(),
|
@@ -485,14 +484,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
|
|
485
484
|
if (!response.ok) {
|
486
485
|
throw new Error(`Failed to process the query - ${response.status}: ${await response.text()}`);
|
487
486
|
}
|
488
|
-
|
489
|
-
return {
|
490
|
-
messageId: data.messageId,
|
491
|
-
message: data.message,
|
492
|
-
products: (_a = data.products) === null || _a === void 0 ? void 0 : _a.filter((item) => !!item).map((item) => ({ ...item, quantity: 1 })),
|
493
|
-
chatTitle: data.chatTitle,
|
494
|
-
welcomePrompts: data.welcomePrompts,
|
495
|
-
};
|
487
|
+
return response;
|
496
488
|
};
|
497
489
|
const fetchChatHistory = async (threadId) => {
|
498
490
|
if (!threadId) {
|
@@ -626,7 +618,7 @@ const init = (params) => {
|
|
626
618
|
// exit if not in top window
|
627
619
|
return;
|
628
620
|
}
|
629
|
-
const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
|
621
|
+
const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, botIconUrl, css, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
|
630
622
|
const experiment = createExperiment({
|
631
623
|
name: getExperimentName(mode),
|
632
624
|
userId: params.userId,
|
@@ -660,6 +652,8 @@ const init = (params) => {
|
|
660
652
|
quickPrompts,
|
661
653
|
merchantImage,
|
662
654
|
latestThreadLoad: latestThreadLoad !== null && latestThreadLoad !== void 0 ? latestThreadLoad : DEFAULT_MAX_THREAD_AGE,
|
655
|
+
botIconUrl,
|
656
|
+
css,
|
663
657
|
});
|
664
658
|
}
|
665
659
|
};
|
@@ -786,6 +780,14 @@ const scrollBarStyles = i$4 `
|
|
786
780
|
const shopGPTStyles = i$4 `
|
787
781
|
${scrollBarStyles}
|
788
782
|
|
783
|
+
:host {
|
784
|
+
--shopgpt-primary: #001531;
|
785
|
+
--shopgpt-secondary: #172a41;
|
786
|
+
--shopgpt-white: #ffffff;
|
787
|
+
--shopgpt-border: #dbe2eb;
|
788
|
+
--shopgpt-warning: #ffcc81;
|
789
|
+
}
|
790
|
+
|
789
791
|
* {
|
790
792
|
box-sizing: border-box;
|
791
793
|
}
|
@@ -818,6 +820,7 @@ const shopGPTStyles = i$4 `
|
|
818
820
|
0px 20px 20px 0px rgba(0, 0, 0, 0.08);
|
819
821
|
position: fixed;
|
820
822
|
z-index: 2000;
|
823
|
+
background: var(--shopgpt-white);
|
821
824
|
|
822
825
|
@media screen and (max-width: 768px) {
|
823
826
|
min-height: unset;
|
@@ -836,21 +839,21 @@ const shopGPTStyles = i$4 `
|
|
836
839
|
z-index: 1000;
|
837
840
|
|
838
841
|
button {
|
839
|
-
color:
|
842
|
+
color: var(--shopgpt-white);
|
840
843
|
border: none;
|
841
844
|
cursor: pointer;
|
842
845
|
width: 56px;
|
843
846
|
height: 56px;
|
844
|
-
background-color:
|
847
|
+
background-color: var(--shopgpt-primary);
|
845
848
|
border-radius: 50%;
|
846
849
|
justify-content: center;
|
847
850
|
align-items: center;
|
848
|
-
box-shadow: 0 0 4px 1px
|
851
|
+
box-shadow: 0 0 4px 1px var(--shopgpt-white);
|
849
852
|
}
|
850
853
|
|
851
854
|
.chatbot-hover-text {
|
852
855
|
position: absolute;
|
853
|
-
color:
|
856
|
+
color: var(--shopgpt-secondary);
|
854
857
|
padding: 8px;
|
855
858
|
white-space: nowrap;
|
856
859
|
font-size: 16px;
|
@@ -863,7 +866,7 @@ const shopGPTStyles = i$4 `
|
|
863
866
|
right: calc(100% + 5px);
|
864
867
|
|
865
868
|
border-radius: 5px 5px 0px;
|
866
|
-
background:
|
869
|
+
background: var(--shopgpt-warning);
|
867
870
|
box-shadow: 0px 4px 6px -1px rgba(0, 0, 0, 0.1),
|
868
871
|
0px 2px 4px -1px rgba(0, 0, 0, 0.06);
|
869
872
|
|
@@ -885,7 +888,7 @@ const shopGPTStyles = i$4 `
|
|
885
888
|
display: flex;
|
886
889
|
overflow: hidden;
|
887
890
|
height: 100%;
|
888
|
-
background:
|
891
|
+
background: var(--shopgpt-white);
|
889
892
|
justify-content: center;
|
890
893
|
align-items: center;
|
891
894
|
padding: 25px;
|
@@ -896,7 +899,7 @@ const shopGPTStyles = i$4 `
|
|
896
899
|
.shopgpt-container {
|
897
900
|
display: flex;
|
898
901
|
gap: 1px;
|
899
|
-
background:
|
902
|
+
background: var(--shopgpt-border);
|
900
903
|
height: 100%;
|
901
904
|
width: 100%;
|
902
905
|
|
@@ -1223,6 +1226,16 @@ class ChatThreads extends r$2 {
|
|
1223
1226
|
constructor() {
|
1224
1227
|
super(...arguments);
|
1225
1228
|
this.deleteAllThreads = false;
|
1229
|
+
this.isStylesheetInjected = false;
|
1230
|
+
}
|
1231
|
+
connectedCallback() {
|
1232
|
+
super.connectedCallback();
|
1233
|
+
if (!this.isStylesheetInjected && this.css) {
|
1234
|
+
const sheet = new CSSStyleSheet();
|
1235
|
+
sheet.replaceSync(this.css);
|
1236
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
1237
|
+
this.isStylesheetInjected = true;
|
1238
|
+
}
|
1226
1239
|
}
|
1227
1240
|
getDomain() {
|
1228
1241
|
var _a;
|
@@ -1391,6 +1404,10 @@ __decorate([
|
|
1391
1404
|
r(),
|
1392
1405
|
__metadata("design:type", Object)
|
1393
1406
|
], ChatThreads.prototype, "deleteAllThreads", void 0);
|
1407
|
+
__decorate([
|
1408
|
+
n({ type: String }),
|
1409
|
+
__metadata("design:type", String)
|
1410
|
+
], ChatThreads.prototype, "css", void 0);
|
1394
1411
|
if (!customElements.get('chat-threads')) {
|
1395
1412
|
customElements.define('chat-threads', ChatThreads);
|
1396
1413
|
}
|
@@ -1829,8 +1846,18 @@ if (!customElements.get('products-list')) {
|
|
1829
1846
|
}
|
1830
1847
|
|
1831
1848
|
class ProductsSection extends r$2 {
|
1849
|
+
constructor() {
|
1850
|
+
super(...arguments);
|
1851
|
+
this.isStylesheetInjected = false;
|
1852
|
+
}
|
1832
1853
|
connectedCallback() {
|
1833
1854
|
super.connectedCallback();
|
1855
|
+
if (!this.isStylesheetInjected && this.css) {
|
1856
|
+
const sheet = new CSSStyleSheet();
|
1857
|
+
sheet.replaceSync(this.css);
|
1858
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
1859
|
+
this.isStylesheetInjected = true;
|
1860
|
+
}
|
1834
1861
|
}
|
1835
1862
|
renderMerchantImage() {
|
1836
1863
|
if (this.merchantImage) {
|
@@ -1907,6 +1934,10 @@ __decorate([
|
|
1907
1934
|
e$3('.products'),
|
1908
1935
|
__metadata("design:type", Object)
|
1909
1936
|
], ProductsSection.prototype, "productsEle", void 0);
|
1937
|
+
__decorate([
|
1938
|
+
n({ type: String }),
|
1939
|
+
__metadata("design:type", String)
|
1940
|
+
], ProductsSection.prototype, "css", void 0);
|
1910
1941
|
if (!customElements.get('products-section')) {
|
1911
1942
|
customElements.define('products-section', ProductsSection);
|
1912
1943
|
}
|
@@ -2023,8 +2054,8 @@ const chatSectionStyles = i$4 `
|
|
2023
2054
|
}
|
2024
2055
|
|
2025
2056
|
.chatbot-section.modal-view {
|
2026
|
-
height: calc(100% -
|
2027
|
-
padding: 0px 16px
|
2057
|
+
height: calc(100% - 93px);
|
2058
|
+
padding: 0px 16px 16px;
|
2028
2059
|
gap: 16px;
|
2029
2060
|
}
|
2030
2061
|
|
@@ -2173,6 +2204,9 @@ const chatSectionStyles = i$4 `
|
|
2173
2204
|
border-radius: 5px;
|
2174
2205
|
border: 1px solid #dbe2eb;
|
2175
2206
|
background: #fff;
|
2207
|
+
width: 36px;
|
2208
|
+
height: 36px;
|
2209
|
+
box-sizing: border-box;
|
2176
2210
|
}
|
2177
2211
|
|
2178
2212
|
.line {
|
@@ -2409,6 +2443,17 @@ const chatSectionStyles = i$4 `
|
|
2409
2443
|
}
|
2410
2444
|
}
|
2411
2445
|
|
2446
|
+
footer {
|
2447
|
+
text-align: center;
|
2448
|
+
font-size: 10px;
|
2449
|
+
color: #4e647f;
|
2450
|
+
font-weight: 500;
|
2451
|
+
|
2452
|
+
a {
|
2453
|
+
text-decoration: none;
|
2454
|
+
}
|
2455
|
+
}
|
2456
|
+
|
2412
2457
|
${scrollBarStyles}
|
2413
2458
|
`;
|
2414
2459
|
|
@@ -3078,12 +3123,12 @@ const markdown = (text) => {
|
|
3078
3123
|
};
|
3079
3124
|
|
3080
3125
|
class MarkdownRenderer extends r$2 {
|
3126
|
+
constructor() {
|
3127
|
+
super(...arguments);
|
3128
|
+
this.content = '';
|
3129
|
+
}
|
3081
3130
|
render() {
|
3082
|
-
|
3083
|
-
const slotContent = this.innerHTML
|
3084
|
-
.trim()
|
3085
|
-
.replace(/<!--\?lit\$[\d$]+-->/g, '');
|
3086
|
-
return o(markdown(slotContent));
|
3131
|
+
return o(markdown(this.content));
|
3087
3132
|
}
|
3088
3133
|
}
|
3089
3134
|
MarkdownRenderer.styles = i$4 `
|
@@ -3097,6 +3142,10 @@ MarkdownRenderer.styles = i$4 `
|
|
3097
3142
|
}
|
3098
3143
|
}
|
3099
3144
|
`;
|
3145
|
+
__decorate([
|
3146
|
+
n({ type: String }),
|
3147
|
+
__metadata("design:type", Object)
|
3148
|
+
], MarkdownRenderer.prototype, "content", void 0);
|
3100
3149
|
if (!customElements.get('markdown-renderer')) {
|
3101
3150
|
customElements.define('markdown-renderer', MarkdownRenderer);
|
3102
3151
|
}
|
@@ -3552,6 +3601,16 @@ class ChatSection extends r$2 {
|
|
3552
3601
|
this.showChatThreads = false;
|
3553
3602
|
this.deleteAllThreads = false;
|
3554
3603
|
this.userQuery = '';
|
3604
|
+
this.isStylesheetInjected = false;
|
3605
|
+
}
|
3606
|
+
connectedCallback() {
|
3607
|
+
super.connectedCallback();
|
3608
|
+
if (!this.isStylesheetInjected && this.css) {
|
3609
|
+
const sheet = new CSSStyleSheet();
|
3610
|
+
sheet.replaceSync(this.css);
|
3611
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
3612
|
+
this.isStylesheetInjected = true;
|
3613
|
+
}
|
3555
3614
|
}
|
3556
3615
|
scrollToBottom() {
|
3557
3616
|
var _a;
|
@@ -3622,12 +3681,12 @@ class ChatSection extends r$2 {
|
|
3622
3681
|
return x `
|
3623
3682
|
<div class="message-wrapper">
|
3624
3683
|
<div class="message bot">
|
3625
|
-
<div>
|
3626
|
-
<div class="bot-icon">${botIcon}</div>
|
3627
|
-
</div>
|
3684
|
+
<div>${this.renderBotIcon()}</div>
|
3628
3685
|
<div>
|
3629
3686
|
${message.message
|
3630
|
-
? x ` <markdown-renderer
|
3687
|
+
? x ` <markdown-renderer
|
3688
|
+
.content=${message.message}
|
3689
|
+
></markdown-renderer>`
|
3631
3690
|
: E}
|
3632
3691
|
${this.viewType !== 'modal' && ((_a = message.products) === null || _a === void 0 ? void 0 : _a[0])
|
3633
3692
|
? x `
|
@@ -3672,6 +3731,14 @@ class ChatSection extends r$2 {
|
|
3672
3731
|
</div>
|
3673
3732
|
`;
|
3674
3733
|
}
|
3734
|
+
renderBotIcon() {
|
3735
|
+
if (this.botIconUrl) {
|
3736
|
+
return x `<div class="bot-icon">
|
3737
|
+
<img src=${this.botIconUrl} width="30" height="30" />
|
3738
|
+
</div>`;
|
3739
|
+
}
|
3740
|
+
return x ` <div class="bot-icon">${botIcon}</div> `;
|
3741
|
+
}
|
3675
3742
|
chatWindow() {
|
3676
3743
|
if (this.isLoadingHistory || this.isLoadingThreads) {
|
3677
3744
|
return x `<div class="messages loading">
|
@@ -3682,17 +3749,13 @@ class ChatSection extends r$2 {
|
|
3682
3749
|
<div class="messages">
|
3683
3750
|
${this.isTyping
|
3684
3751
|
? x ` <div class="message bot">
|
3685
|
-
<div>
|
3686
|
-
<div class="bot-icon">${botIcon}</div>
|
3687
|
-
</div>
|
3752
|
+
<div>${this.renderBotIcon()}</div>
|
3688
3753
|
${this.typingIndicator()}
|
3689
3754
|
</div>`
|
3690
3755
|
: ''}
|
3691
3756
|
${this.isFailed
|
3692
3757
|
? x `<div class="message bot">
|
3693
|
-
<div>
|
3694
|
-
<div class="bot-icon">${botIcon}</div>
|
3695
|
-
</div>
|
3758
|
+
<div>${this.renderBotIcon()}</div>
|
3696
3759
|
<div>
|
3697
3760
|
<p>
|
3698
3761
|
Uh-oh! Looks like I tripped over some alpha-stage wires.
|
@@ -3710,9 +3773,7 @@ class ChatSection extends r$2 {
|
|
3710
3773
|
return x ` <div class="message user">${message.message}</div> `;
|
3711
3774
|
})}
|
3712
3775
|
<div class="message bot">
|
3713
|
-
<div>
|
3714
|
-
<div class="bot-icon">${botIcon}</div>
|
3715
|
-
</div>
|
3776
|
+
<div>${this.renderBotIcon()}</div>
|
3716
3777
|
<div>
|
3717
3778
|
<p>
|
3718
3779
|
Hi,
|
@@ -3837,7 +3898,11 @@ class ChatSection extends r$2 {
|
|
3837
3898
|
modalViewHeader() {
|
3838
3899
|
var _a;
|
3839
3900
|
return x `
|
3840
|
-
<div
|
3901
|
+
<div>
|
3902
|
+
${this.botIconUrl
|
3903
|
+
? x `<img src=${this.botIconUrl} width="30" height="30" />`
|
3904
|
+
: botIcon}
|
3905
|
+
</div>
|
3841
3906
|
<h2>${((_a = this.thread) === null || _a === void 0 ? void 0 : _a.title) || 'New Search'}</h2>
|
3842
3907
|
<div class="btns-wrapper">
|
3843
3908
|
<tooltip-component .position=${'bottom-right'} .text=${'New Chat'}>
|
@@ -4023,6 +4088,11 @@ class ChatSection extends r$2 {
|
|
4023
4088
|
${sendFilledIcon}
|
4024
4089
|
</button>
|
4025
4090
|
</form>
|
4091
|
+
${this.viewType === 'modal'
|
4092
|
+
? x ` <footer>
|
4093
|
+
Powered by <a href="https://blotout.io">Blotout</a>
|
4094
|
+
</footer>`
|
4095
|
+
: E}
|
4026
4096
|
</div>
|
4027
4097
|
<personalize-dialog
|
4028
4098
|
.createChatThread=${this.createChatThread.bind(this)}
|
@@ -4079,6 +4149,10 @@ __decorate([
|
|
4079
4149
|
n({ type: String }),
|
4080
4150
|
__metadata("design:type", Object)
|
4081
4151
|
], ChatSection.prototype, "merchantImage", void 0);
|
4152
|
+
__decorate([
|
4153
|
+
n({ type: String }),
|
4154
|
+
__metadata("design:type", Object)
|
4155
|
+
], ChatSection.prototype, "botIconUrl", void 0);
|
4082
4156
|
__decorate([
|
4083
4157
|
n({ type: String }),
|
4084
4158
|
__metadata("design:type", Object)
|
@@ -4167,15 +4241,78 @@ __decorate([
|
|
4167
4241
|
n({ type: String }),
|
4168
4242
|
__metadata("design:type", Object)
|
4169
4243
|
], ChatSection.prototype, "userQuery", void 0);
|
4244
|
+
__decorate([
|
4245
|
+
n({ type: String }),
|
4246
|
+
__metadata("design:type", String)
|
4247
|
+
], ChatSection.prototype, "css", void 0);
|
4170
4248
|
if (!customElements.get('chat-section')) {
|
4171
4249
|
customElements.define('chat-section', ChatSection);
|
4172
4250
|
}
|
4173
4251
|
|
4252
|
+
async function* streamToIterable(stream) {
|
4253
|
+
const reader = stream.getReader();
|
4254
|
+
while (true) {
|
4255
|
+
const { done, value } = await reader.read();
|
4256
|
+
if (done)
|
4257
|
+
return;
|
4258
|
+
yield value;
|
4259
|
+
}
|
4260
|
+
}
|
4261
|
+
const handleEventSource = async (response, onEvent) => {
|
4262
|
+
var _a, _b;
|
4263
|
+
if (!response.body) {
|
4264
|
+
throw new Error('No body found in response');
|
4265
|
+
}
|
4266
|
+
let buffer = '';
|
4267
|
+
for await (const chunk of streamToIterable(response.body.pipeThrough(new TextDecoderStream()))) {
|
4268
|
+
buffer += chunk;
|
4269
|
+
const sections = buffer.split('\n\n');
|
4270
|
+
buffer = sections.pop() || '';
|
4271
|
+
for (const section of sections) {
|
4272
|
+
const event = parseMessage(section, true);
|
4273
|
+
onEvent((_a = event.event) !== null && _a !== void 0 ? _a : '', event.data);
|
4274
|
+
}
|
4275
|
+
}
|
4276
|
+
if (buffer) {
|
4277
|
+
const event = parseMessage(buffer, true);
|
4278
|
+
onEvent((_b = event.event) !== null && _b !== void 0 ? _b : '', event.data);
|
4279
|
+
}
|
4280
|
+
};
|
4281
|
+
const parseMessage = (message, parseDataAsJSON) => {
|
4282
|
+
const fields = message.split('\n');
|
4283
|
+
const result = {};
|
4284
|
+
for (const field of fields) {
|
4285
|
+
if (field.startsWith(':')) {
|
4286
|
+
continue;
|
4287
|
+
}
|
4288
|
+
if (field.startsWith('event: ')) {
|
4289
|
+
result.event = field.slice(7);
|
4290
|
+
}
|
4291
|
+
else if (field.startsWith('data: ')) {
|
4292
|
+
if (result.data) {
|
4293
|
+
result.data += '\n';
|
4294
|
+
}
|
4295
|
+
else {
|
4296
|
+
result.data = '';
|
4297
|
+
}
|
4298
|
+
result.data += field.slice(6);
|
4299
|
+
}
|
4300
|
+
else if (field.startsWith('retry: ')) {
|
4301
|
+
result.retry = field.slice(7);
|
4302
|
+
}
|
4303
|
+
}
|
4304
|
+
if (result.data && parseDataAsJSON) {
|
4305
|
+
result.data = JSON.parse(result.data);
|
4306
|
+
}
|
4307
|
+
return result;
|
4308
|
+
};
|
4309
|
+
|
4174
4310
|
const DIALOG_DELAY = 1000;
|
4175
4311
|
const normalizePath = (path) => path.replace(/\/$/, '');
|
4176
4312
|
class ShopGPT extends r$2 {
|
4177
4313
|
constructor() {
|
4178
4314
|
super(...arguments);
|
4315
|
+
this.isStylesheetInjected = false;
|
4179
4316
|
this.latestThreadLoad = DEFAULT_MAX_THREAD_AGE;
|
4180
4317
|
this.modalState = 'close';
|
4181
4318
|
this.isLoadingHistory = false;
|
@@ -4209,6 +4346,12 @@ class ShopGPT extends r$2 {
|
|
4209
4346
|
}
|
4210
4347
|
connectedCallback() {
|
4211
4348
|
super.connectedCallback();
|
4349
|
+
if (!this.isStylesheetInjected && this.css) {
|
4350
|
+
const sheet = new CSSStyleSheet();
|
4351
|
+
sheet.replaceSync(this.css);
|
4352
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
4353
|
+
this.isStylesheetInjected = true;
|
4354
|
+
}
|
4212
4355
|
if (!this.view || this.view === 'overlay') {
|
4213
4356
|
if (!this.path) {
|
4214
4357
|
return;
|
@@ -4278,27 +4421,12 @@ class ShopGPT extends r$2 {
|
|
4278
4421
|
: undefined;
|
4279
4422
|
try {
|
4280
4423
|
this.isTyping = true;
|
4281
|
-
const
|
4282
|
-
|
4283
|
-
this.setChatTitle(this.selectedThreadId, reply.chatTitle);
|
4284
|
-
}
|
4285
|
-
this.messages = [
|
4286
|
-
{
|
4287
|
-
messageId: reply.messageId,
|
4288
|
-
sender: 'bot',
|
4289
|
-
message: reply.message,
|
4290
|
-
products: reply.products,
|
4291
|
-
welcomePrompts: reply.welcomePrompts,
|
4292
|
-
},
|
4293
|
-
...this.messages,
|
4294
|
-
];
|
4295
|
-
this.products = reply.products || [];
|
4424
|
+
const response = await this.shopGPTAPI.processQuery('', thread.threadId, productHandle);
|
4425
|
+
this.processMessageResponse(response);
|
4296
4426
|
}
|
4297
4427
|
catch (error) {
|
4298
4428
|
logger.error(error);
|
4299
4429
|
this.isFailed = true;
|
4300
|
-
}
|
4301
|
-
finally {
|
4302
4430
|
this.isTyping = false;
|
4303
4431
|
}
|
4304
4432
|
}
|
@@ -4436,6 +4564,92 @@ class ShopGPT extends r$2 {
|
|
4436
4564
|
.catch(logger.error)
|
4437
4565
|
.finally(() => (this.isLoadingThreads = false));
|
4438
4566
|
}
|
4567
|
+
handleMessageChunks(messageData) {
|
4568
|
+
if (!messageData.message) {
|
4569
|
+
return;
|
4570
|
+
}
|
4571
|
+
const latestMessage = this.messages[0];
|
4572
|
+
this.isTyping = false;
|
4573
|
+
if (latestMessage === null || latestMessage === void 0 ? void 0 : latestMessage.isChunk) {
|
4574
|
+
this.messages = [
|
4575
|
+
{
|
4576
|
+
...latestMessage,
|
4577
|
+
message: latestMessage.message + messageData.message,
|
4578
|
+
isChunk: messageData.isChunk,
|
4579
|
+
sender: 'bot',
|
4580
|
+
},
|
4581
|
+
...this.messages.slice(1),
|
4582
|
+
];
|
4583
|
+
}
|
4584
|
+
else {
|
4585
|
+
this.messages = [
|
4586
|
+
{
|
4587
|
+
sender: 'bot',
|
4588
|
+
message: messageData.message || '',
|
4589
|
+
isChunk: messageData.isChunk,
|
4590
|
+
},
|
4591
|
+
...this.messages,
|
4592
|
+
];
|
4593
|
+
}
|
4594
|
+
}
|
4595
|
+
handleCompleteMessage(messageData) {
|
4596
|
+
const latestMessage = this.messages[0];
|
4597
|
+
this.messages = [
|
4598
|
+
{
|
4599
|
+
...latestMessage,
|
4600
|
+
message: messageData.message || '',
|
4601
|
+
messageId: messageData.messageId,
|
4602
|
+
welcomePrompts: messageData.welcomePrompts,
|
4603
|
+
sender: 'bot',
|
4604
|
+
isChunk: false,
|
4605
|
+
},
|
4606
|
+
...this.messages.slice(1),
|
4607
|
+
];
|
4608
|
+
}
|
4609
|
+
handleProductsComplete(products, isMessageCompleted) {
|
4610
|
+
if (!products || !isMessageCompleted) {
|
4611
|
+
return;
|
4612
|
+
}
|
4613
|
+
const latestMessage = this.messages[0];
|
4614
|
+
this.messages = [
|
4615
|
+
{
|
4616
|
+
...latestMessage,
|
4617
|
+
products,
|
4618
|
+
},
|
4619
|
+
...this.messages.slice(1),
|
4620
|
+
];
|
4621
|
+
if (products.length) {
|
4622
|
+
this.products = products;
|
4623
|
+
}
|
4624
|
+
}
|
4625
|
+
processMessageResponse(response) {
|
4626
|
+
let isMessageCompleted = false;
|
4627
|
+
let products;
|
4628
|
+
handleEventSource(response, (eventName, data) => {
|
4629
|
+
if (eventName === 'ChatTitle') {
|
4630
|
+
this.setChatTitle(this.selectedThreadId, data.chatTitle || '');
|
4631
|
+
}
|
4632
|
+
else if (eventName === 'Message') {
|
4633
|
+
this.handleMessageChunks(data);
|
4634
|
+
}
|
4635
|
+
else if (eventName === 'MessageComplete') {
|
4636
|
+
isMessageCompleted = true;
|
4637
|
+
this.handleProductsComplete(products, isMessageCompleted);
|
4638
|
+
this.handleCompleteMessage(data);
|
4639
|
+
}
|
4640
|
+
else if (eventName === 'ProductsComplete') {
|
4641
|
+
this.handleProductsComplete(data.products, isMessageCompleted);
|
4642
|
+
products = data.products;
|
4643
|
+
}
|
4644
|
+
else if (eventName === 'error') {
|
4645
|
+
if (this.messages[0].isChunk) {
|
4646
|
+
this.messages = this.messages.slice(1);
|
4647
|
+
}
|
4648
|
+
this.isFailed = true;
|
4649
|
+
throw new Error(data);
|
4650
|
+
}
|
4651
|
+
});
|
4652
|
+
}
|
4439
4653
|
async sendMessageToServer(e, message) {
|
4440
4654
|
e.preventDefault();
|
4441
4655
|
e.stopPropagation();
|
@@ -4446,32 +4660,12 @@ class ShopGPT extends r$2 {
|
|
4446
4660
|
try {
|
4447
4661
|
this.messages = [{ sender: 'user', message }, ...this.messages];
|
4448
4662
|
this.isTyping = true;
|
4449
|
-
const
|
4450
|
-
|
4451
|
-
return;
|
4452
|
-
}
|
4453
|
-
if (reply.chatTitle) {
|
4454
|
-
this.setChatTitle(this.selectedThreadId, reply.chatTitle);
|
4455
|
-
}
|
4456
|
-
this.messages = [
|
4457
|
-
{
|
4458
|
-
messageId: reply.messageId,
|
4459
|
-
sender: 'bot',
|
4460
|
-
message: reply.message,
|
4461
|
-
products: reply.products,
|
4462
|
-
welcomePrompts: reply.welcomePrompts,
|
4463
|
-
},
|
4464
|
-
...this.messages,
|
4465
|
-
];
|
4466
|
-
if (reply.products && reply.products.length > 0) {
|
4467
|
-
this.products = reply.products;
|
4468
|
-
}
|
4663
|
+
const response = await this.submitQuery(message);
|
4664
|
+
this.processMessageResponse(response);
|
4469
4665
|
}
|
4470
4666
|
catch (err) {
|
4471
4667
|
logger.error(err);
|
4472
4668
|
this.isFailed = true;
|
4473
|
-
}
|
4474
|
-
finally {
|
4475
4669
|
this.isTyping = false;
|
4476
4670
|
}
|
4477
4671
|
}
|
@@ -4518,6 +4712,7 @@ class ShopGPT extends r$2 {
|
|
4518
4712
|
.isLoading=${this.isLoadingThreads}
|
4519
4713
|
.isTyping=${this.isTyping}
|
4520
4714
|
.merchantUrl=${this.merchantUrl}
|
4715
|
+
.css=${this.css}
|
4521
4716
|
></chat-threads>
|
4522
4717
|
<products-section
|
4523
4718
|
.merchantImage=${this.merchantImage}
|
@@ -4525,6 +4720,7 @@ class ShopGPT extends r$2 {
|
|
4525
4720
|
.isLoadingHistory=${this.isLoadingHistory}
|
4526
4721
|
.siteCurrency=${this.getSiteCurrency()}
|
4527
4722
|
.isLoadingThreads=${this.isLoadingThreads}
|
4723
|
+
.css=${this.css}
|
4528
4724
|
></products-section>
|
4529
4725
|
<chat-section
|
4530
4726
|
.prompts=${this.quickPrompts}
|
@@ -4543,6 +4739,8 @@ class ShopGPT extends r$2 {
|
|
4543
4739
|
.viewType=${'overlay'}
|
4544
4740
|
.isLoadingThreads=${this.isLoadingThreads}
|
4545
4741
|
.customPrompts=${this.customPrompts}
|
4742
|
+
.botIconUrl=${this.botIconUrl}
|
4743
|
+
.css=${this.css}
|
4546
4744
|
></chat-section>
|
4547
4745
|
</div>
|
4548
4746
|
</dialog>
|
@@ -4594,6 +4792,8 @@ class ShopGPT extends r$2 {
|
|
4594
4792
|
.isLoadingThreads=${this.isLoadingThreads}
|
4595
4793
|
.merchantImage=${this.merchantImage}
|
4596
4794
|
.customPrompts=${this.customPrompts}
|
4795
|
+
.botIconUrl=${this.botIconUrl}
|
4796
|
+
.css=${this.css}
|
4597
4797
|
></chat-section>
|
4598
4798
|
</div>
|
4599
4799
|
`;
|
@@ -4675,6 +4875,8 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
|
|
4675
4875
|
shopGPT.quickPrompts = params.quickPrompts;
|
4676
4876
|
shopGPT.merchantImage = params.merchantImage;
|
4677
4877
|
shopGPT.latestThreadLoad = params.latestThreadLoad;
|
4878
|
+
shopGPT.botIconUrl = params.botIconUrl;
|
4879
|
+
shopGPT.css = params.css;
|
4678
4880
|
document.body.append(shopGPT);
|
4679
4881
|
},
|
4680
4882
|
destroy() {
|