@blotoutio/providers-shop-gpt-sdk 1.8.0 → 1.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/index.cjs.js +270 -77
- package/index.js +270 -77
- package/index.mjs +270 -77
- package/package.json +1 -1
package/index.cjs.js
CHANGED
@@ -472,7 +472,6 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
|
|
472
472
|
return url;
|
473
473
|
};
|
474
474
|
const processQuery = async (query, threadId, productHandle) => {
|
475
|
-
var _a;
|
476
475
|
const response = await fetchImpl(getURL('/query'), {
|
477
476
|
method: 'POST',
|
478
477
|
headers: getHeaders(),
|
@@ -487,14 +486,7 @@ const createShopGPTAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId, st
|
|
487
486
|
if (!response.ok) {
|
488
487
|
throw new Error(`Failed to process the query - ${response.status}: ${await response.text()}`);
|
489
488
|
}
|
490
|
-
|
491
|
-
return {
|
492
|
-
messageId: data.messageId,
|
493
|
-
message: data.message,
|
494
|
-
products: (_a = data.products) === null || _a === void 0 ? void 0 : _a.filter((item) => !!item).map((item) => ({ ...item, quantity: 1 })),
|
495
|
-
chatTitle: data.chatTitle,
|
496
|
-
welcomePrompts: data.welcomePrompts,
|
497
|
-
};
|
489
|
+
return response;
|
498
490
|
};
|
499
491
|
const fetchChatHistory = async (threadId) => {
|
500
492
|
if (!threadId) {
|
@@ -628,7 +620,7 @@ const init = (params) => {
|
|
628
620
|
// exit if not in top window
|
629
621
|
return;
|
630
622
|
}
|
631
|
-
const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
|
623
|
+
const { enabled, mode, devMode, merchantUrl, profiles, productHandles, targetPath, view, brandName, quickPrompts, merchantImage, latestThreadLoad, botIconUrl, css, } = (_c = params.manifest.variables) !== null && _c !== void 0 ? _c : {};
|
632
624
|
const experiment = createExperiment({
|
633
625
|
name: getExperimentName(mode),
|
634
626
|
userId: params.userId,
|
@@ -662,6 +654,8 @@ const init = (params) => {
|
|
662
654
|
quickPrompts,
|
663
655
|
merchantImage,
|
664
656
|
latestThreadLoad: latestThreadLoad !== null && latestThreadLoad !== void 0 ? latestThreadLoad : DEFAULT_MAX_THREAD_AGE,
|
657
|
+
botIconUrl,
|
658
|
+
css,
|
665
659
|
});
|
666
660
|
}
|
667
661
|
};
|
@@ -788,6 +782,14 @@ const scrollBarStyles = i$4 `
|
|
788
782
|
const shopGPTStyles = i$4 `
|
789
783
|
${scrollBarStyles}
|
790
784
|
|
785
|
+
:host {
|
786
|
+
--shopgpt-primary: #001531;
|
787
|
+
--shopgpt-secondary: #172a41;
|
788
|
+
--shopgpt-white: #ffffff;
|
789
|
+
--shopgpt-border: #dbe2eb;
|
790
|
+
--shopgpt-warning: #ffcc81;
|
791
|
+
}
|
792
|
+
|
791
793
|
* {
|
792
794
|
box-sizing: border-box;
|
793
795
|
}
|
@@ -820,6 +822,7 @@ const shopGPTStyles = i$4 `
|
|
820
822
|
0px 20px 20px 0px rgba(0, 0, 0, 0.08);
|
821
823
|
position: fixed;
|
822
824
|
z-index: 2000;
|
825
|
+
background: var(--shopgpt-white);
|
823
826
|
|
824
827
|
@media screen and (max-width: 768px) {
|
825
828
|
min-height: unset;
|
@@ -838,21 +841,21 @@ const shopGPTStyles = i$4 `
|
|
838
841
|
z-index: 1000;
|
839
842
|
|
840
843
|
button {
|
841
|
-
color:
|
844
|
+
color: var(--shopgpt-white);
|
842
845
|
border: none;
|
843
846
|
cursor: pointer;
|
844
847
|
width: 56px;
|
845
848
|
height: 56px;
|
846
|
-
background-color:
|
849
|
+
background-color: var(--shopgpt-primary);
|
847
850
|
border-radius: 50%;
|
848
851
|
justify-content: center;
|
849
852
|
align-items: center;
|
850
|
-
box-shadow: 0 0 4px 1px
|
853
|
+
box-shadow: 0 0 4px 1px var(--shopgpt-white);
|
851
854
|
}
|
852
855
|
|
853
856
|
.chatbot-hover-text {
|
854
857
|
position: absolute;
|
855
|
-
color:
|
858
|
+
color: var(--shopgpt-secondary);
|
856
859
|
padding: 8px;
|
857
860
|
white-space: nowrap;
|
858
861
|
font-size: 16px;
|
@@ -865,7 +868,7 @@ const shopGPTStyles = i$4 `
|
|
865
868
|
right: calc(100% + 5px);
|
866
869
|
|
867
870
|
border-radius: 5px 5px 0px;
|
868
|
-
background:
|
871
|
+
background: var(--shopgpt-warning);
|
869
872
|
box-shadow: 0px 4px 6px -1px rgba(0, 0, 0, 0.1),
|
870
873
|
0px 2px 4px -1px rgba(0, 0, 0, 0.06);
|
871
874
|
|
@@ -887,7 +890,7 @@ const shopGPTStyles = i$4 `
|
|
887
890
|
display: flex;
|
888
891
|
overflow: hidden;
|
889
892
|
height: 100%;
|
890
|
-
background:
|
893
|
+
background: var(--shopgpt-white);
|
891
894
|
justify-content: center;
|
892
895
|
align-items: center;
|
893
896
|
padding: 25px;
|
@@ -898,7 +901,7 @@ const shopGPTStyles = i$4 `
|
|
898
901
|
.shopgpt-container {
|
899
902
|
display: flex;
|
900
903
|
gap: 1px;
|
901
|
-
background:
|
904
|
+
background: var(--shopgpt-border);
|
902
905
|
height: 100%;
|
903
906
|
width: 100%;
|
904
907
|
|
@@ -1225,6 +1228,16 @@ class ChatThreads extends r$2 {
|
|
1225
1228
|
constructor() {
|
1226
1229
|
super(...arguments);
|
1227
1230
|
this.deleteAllThreads = false;
|
1231
|
+
this.isStylesheetInjected = false;
|
1232
|
+
}
|
1233
|
+
connectedCallback() {
|
1234
|
+
super.connectedCallback();
|
1235
|
+
if (!this.isStylesheetInjected && this.css) {
|
1236
|
+
const sheet = new CSSStyleSheet();
|
1237
|
+
sheet.replaceSync(this.css);
|
1238
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
1239
|
+
this.isStylesheetInjected = true;
|
1240
|
+
}
|
1228
1241
|
}
|
1229
1242
|
getDomain() {
|
1230
1243
|
var _a;
|
@@ -1393,6 +1406,10 @@ __decorate([
|
|
1393
1406
|
r(),
|
1394
1407
|
__metadata("design:type", Object)
|
1395
1408
|
], ChatThreads.prototype, "deleteAllThreads", void 0);
|
1409
|
+
__decorate([
|
1410
|
+
n({ type: String }),
|
1411
|
+
__metadata("design:type", String)
|
1412
|
+
], ChatThreads.prototype, "css", void 0);
|
1396
1413
|
if (!customElements.get('chat-threads')) {
|
1397
1414
|
customElements.define('chat-threads', ChatThreads);
|
1398
1415
|
}
|
@@ -1831,8 +1848,18 @@ if (!customElements.get('products-list')) {
|
|
1831
1848
|
}
|
1832
1849
|
|
1833
1850
|
class ProductsSection extends r$2 {
|
1851
|
+
constructor() {
|
1852
|
+
super(...arguments);
|
1853
|
+
this.isStylesheetInjected = false;
|
1854
|
+
}
|
1834
1855
|
connectedCallback() {
|
1835
1856
|
super.connectedCallback();
|
1857
|
+
if (!this.isStylesheetInjected && this.css) {
|
1858
|
+
const sheet = new CSSStyleSheet();
|
1859
|
+
sheet.replaceSync(this.css);
|
1860
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
1861
|
+
this.isStylesheetInjected = true;
|
1862
|
+
}
|
1836
1863
|
}
|
1837
1864
|
renderMerchantImage() {
|
1838
1865
|
if (this.merchantImage) {
|
@@ -1909,6 +1936,10 @@ __decorate([
|
|
1909
1936
|
e$3('.products'),
|
1910
1937
|
__metadata("design:type", Object)
|
1911
1938
|
], ProductsSection.prototype, "productsEle", void 0);
|
1939
|
+
__decorate([
|
1940
|
+
n({ type: String }),
|
1941
|
+
__metadata("design:type", String)
|
1942
|
+
], ProductsSection.prototype, "css", void 0);
|
1912
1943
|
if (!customElements.get('products-section')) {
|
1913
1944
|
customElements.define('products-section', ProductsSection);
|
1914
1945
|
}
|
@@ -2025,8 +2056,8 @@ const chatSectionStyles = i$4 `
|
|
2025
2056
|
}
|
2026
2057
|
|
2027
2058
|
.chatbot-section.modal-view {
|
2028
|
-
height: calc(100% -
|
2029
|
-
padding: 0px 16px
|
2059
|
+
height: calc(100% - 93px);
|
2060
|
+
padding: 0px 16px 16px;
|
2030
2061
|
gap: 16px;
|
2031
2062
|
}
|
2032
2063
|
|
@@ -2175,6 +2206,9 @@ const chatSectionStyles = i$4 `
|
|
2175
2206
|
border-radius: 5px;
|
2176
2207
|
border: 1px solid #dbe2eb;
|
2177
2208
|
background: #fff;
|
2209
|
+
width: 36px;
|
2210
|
+
height: 36px;
|
2211
|
+
box-sizing: border-box;
|
2178
2212
|
}
|
2179
2213
|
|
2180
2214
|
.line {
|
@@ -2411,6 +2445,17 @@ const chatSectionStyles = i$4 `
|
|
2411
2445
|
}
|
2412
2446
|
}
|
2413
2447
|
|
2448
|
+
footer {
|
2449
|
+
text-align: center;
|
2450
|
+
font-size: 10px;
|
2451
|
+
color: #4e647f;
|
2452
|
+
font-weight: 500;
|
2453
|
+
|
2454
|
+
a {
|
2455
|
+
text-decoration: none;
|
2456
|
+
}
|
2457
|
+
}
|
2458
|
+
|
2414
2459
|
${scrollBarStyles}
|
2415
2460
|
`;
|
2416
2461
|
|
@@ -3080,12 +3125,12 @@ const markdown = (text) => {
|
|
3080
3125
|
};
|
3081
3126
|
|
3082
3127
|
class MarkdownRenderer extends r$2 {
|
3128
|
+
constructor() {
|
3129
|
+
super(...arguments);
|
3130
|
+
this.content = '';
|
3131
|
+
}
|
3083
3132
|
render() {
|
3084
|
-
|
3085
|
-
const slotContent = this.innerHTML
|
3086
|
-
.trim()
|
3087
|
-
.replace(/<!--\?lit\$[\d$]+-->/g, '');
|
3088
|
-
return o(markdown(slotContent));
|
3133
|
+
return o(markdown(this.content));
|
3089
3134
|
}
|
3090
3135
|
}
|
3091
3136
|
MarkdownRenderer.styles = i$4 `
|
@@ -3099,6 +3144,10 @@ MarkdownRenderer.styles = i$4 `
|
|
3099
3144
|
}
|
3100
3145
|
}
|
3101
3146
|
`;
|
3147
|
+
__decorate([
|
3148
|
+
n({ type: String }),
|
3149
|
+
__metadata("design:type", Object)
|
3150
|
+
], MarkdownRenderer.prototype, "content", void 0);
|
3102
3151
|
if (!customElements.get('markdown-renderer')) {
|
3103
3152
|
customElements.define('markdown-renderer', MarkdownRenderer);
|
3104
3153
|
}
|
@@ -3554,6 +3603,16 @@ class ChatSection extends r$2 {
|
|
3554
3603
|
this.showChatThreads = false;
|
3555
3604
|
this.deleteAllThreads = false;
|
3556
3605
|
this.userQuery = '';
|
3606
|
+
this.isStylesheetInjected = false;
|
3607
|
+
}
|
3608
|
+
connectedCallback() {
|
3609
|
+
super.connectedCallback();
|
3610
|
+
if (!this.isStylesheetInjected && this.css) {
|
3611
|
+
const sheet = new CSSStyleSheet();
|
3612
|
+
sheet.replaceSync(this.css);
|
3613
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
3614
|
+
this.isStylesheetInjected = true;
|
3615
|
+
}
|
3557
3616
|
}
|
3558
3617
|
scrollToBottom() {
|
3559
3618
|
var _a;
|
@@ -3624,12 +3683,12 @@ class ChatSection extends r$2 {
|
|
3624
3683
|
return x `
|
3625
3684
|
<div class="message-wrapper">
|
3626
3685
|
<div class="message bot">
|
3627
|
-
<div>
|
3628
|
-
<div class="bot-icon">${botIcon}</div>
|
3629
|
-
</div>
|
3686
|
+
<div>${this.renderBotIcon()}</div>
|
3630
3687
|
<div>
|
3631
3688
|
${message.message
|
3632
|
-
? x ` <markdown-renderer
|
3689
|
+
? x ` <markdown-renderer
|
3690
|
+
.content=${message.message}
|
3691
|
+
></markdown-renderer>`
|
3633
3692
|
: E}
|
3634
3693
|
${this.viewType !== 'modal' && ((_a = message.products) === null || _a === void 0 ? void 0 : _a[0])
|
3635
3694
|
? x `
|
@@ -3674,6 +3733,14 @@ class ChatSection extends r$2 {
|
|
3674
3733
|
</div>
|
3675
3734
|
`;
|
3676
3735
|
}
|
3736
|
+
renderBotIcon() {
|
3737
|
+
if (this.botIconUrl) {
|
3738
|
+
return x `<div class="bot-icon">
|
3739
|
+
<img src=${this.botIconUrl} width="30" height="30" />
|
3740
|
+
</div>`;
|
3741
|
+
}
|
3742
|
+
return x ` <div class="bot-icon">${botIcon}</div> `;
|
3743
|
+
}
|
3677
3744
|
chatWindow() {
|
3678
3745
|
if (this.isLoadingHistory || this.isLoadingThreads) {
|
3679
3746
|
return x `<div class="messages loading">
|
@@ -3684,17 +3751,13 @@ class ChatSection extends r$2 {
|
|
3684
3751
|
<div class="messages">
|
3685
3752
|
${this.isTyping
|
3686
3753
|
? x ` <div class="message bot">
|
3687
|
-
<div>
|
3688
|
-
<div class="bot-icon">${botIcon}</div>
|
3689
|
-
</div>
|
3754
|
+
<div>${this.renderBotIcon()}</div>
|
3690
3755
|
${this.typingIndicator()}
|
3691
3756
|
</div>`
|
3692
3757
|
: ''}
|
3693
3758
|
${this.isFailed
|
3694
3759
|
? x `<div class="message bot">
|
3695
|
-
<div>
|
3696
|
-
<div class="bot-icon">${botIcon}</div>
|
3697
|
-
</div>
|
3760
|
+
<div>${this.renderBotIcon()}</div>
|
3698
3761
|
<div>
|
3699
3762
|
<p>
|
3700
3763
|
Uh-oh! Looks like I tripped over some alpha-stage wires.
|
@@ -3712,9 +3775,7 @@ class ChatSection extends r$2 {
|
|
3712
3775
|
return x ` <div class="message user">${message.message}</div> `;
|
3713
3776
|
})}
|
3714
3777
|
<div class="message bot">
|
3715
|
-
<div>
|
3716
|
-
<div class="bot-icon">${botIcon}</div>
|
3717
|
-
</div>
|
3778
|
+
<div>${this.renderBotIcon()}</div>
|
3718
3779
|
<div>
|
3719
3780
|
<p>
|
3720
3781
|
Hi,
|
@@ -3839,7 +3900,11 @@ class ChatSection extends r$2 {
|
|
3839
3900
|
modalViewHeader() {
|
3840
3901
|
var _a;
|
3841
3902
|
return x `
|
3842
|
-
<div
|
3903
|
+
<div>
|
3904
|
+
${this.botIconUrl
|
3905
|
+
? x `<img src=${this.botIconUrl} width="30" height="30" />`
|
3906
|
+
: botIcon}
|
3907
|
+
</div>
|
3843
3908
|
<h2>${((_a = this.thread) === null || _a === void 0 ? void 0 : _a.title) || 'New Search'}</h2>
|
3844
3909
|
<div class="btns-wrapper">
|
3845
3910
|
<tooltip-component .position=${'bottom-right'} .text=${'New Chat'}>
|
@@ -4025,6 +4090,11 @@ class ChatSection extends r$2 {
|
|
4025
4090
|
${sendFilledIcon}
|
4026
4091
|
</button>
|
4027
4092
|
</form>
|
4093
|
+
${this.viewType === 'modal'
|
4094
|
+
? x ` <footer>
|
4095
|
+
Powered by <a href="https://blotout.io">Blotout</a>
|
4096
|
+
</footer>`
|
4097
|
+
: E}
|
4028
4098
|
</div>
|
4029
4099
|
<personalize-dialog
|
4030
4100
|
.createChatThread=${this.createChatThread.bind(this)}
|
@@ -4081,6 +4151,10 @@ __decorate([
|
|
4081
4151
|
n({ type: String }),
|
4082
4152
|
__metadata("design:type", Object)
|
4083
4153
|
], ChatSection.prototype, "merchantImage", void 0);
|
4154
|
+
__decorate([
|
4155
|
+
n({ type: String }),
|
4156
|
+
__metadata("design:type", Object)
|
4157
|
+
], ChatSection.prototype, "botIconUrl", void 0);
|
4084
4158
|
__decorate([
|
4085
4159
|
n({ type: String }),
|
4086
4160
|
__metadata("design:type", Object)
|
@@ -4169,15 +4243,69 @@ __decorate([
|
|
4169
4243
|
n({ type: String }),
|
4170
4244
|
__metadata("design:type", Object)
|
4171
4245
|
], ChatSection.prototype, "userQuery", void 0);
|
4246
|
+
__decorate([
|
4247
|
+
n({ type: String }),
|
4248
|
+
__metadata("design:type", String)
|
4249
|
+
], ChatSection.prototype, "css", void 0);
|
4172
4250
|
if (!customElements.get('chat-section')) {
|
4173
4251
|
customElements.define('chat-section', ChatSection);
|
4174
4252
|
}
|
4175
4253
|
|
4254
|
+
const handleEventSource = async (response, onEvent) => {
|
4255
|
+
var _a, _b;
|
4256
|
+
if (!response.body) {
|
4257
|
+
throw new Error('No body found in response');
|
4258
|
+
}
|
4259
|
+
let buffer = '';
|
4260
|
+
for await (const chunk of response.body.pipeThrough(new TextDecoderStream())) {
|
4261
|
+
buffer += chunk;
|
4262
|
+
const sections = buffer.split('\n\n');
|
4263
|
+
buffer = sections.pop() || '';
|
4264
|
+
for (const section of sections) {
|
4265
|
+
const event = parseMessage(section, true);
|
4266
|
+
onEvent((_a = event.event) !== null && _a !== void 0 ? _a : '', event.data);
|
4267
|
+
}
|
4268
|
+
}
|
4269
|
+
if (buffer) {
|
4270
|
+
const event = parseMessage(buffer, true);
|
4271
|
+
onEvent((_b = event.event) !== null && _b !== void 0 ? _b : '', event.data);
|
4272
|
+
}
|
4273
|
+
};
|
4274
|
+
const parseMessage = (message, parseDataAsJSON) => {
|
4275
|
+
const fields = message.split('\n');
|
4276
|
+
const result = {};
|
4277
|
+
for (const field of fields) {
|
4278
|
+
if (field.startsWith(':')) {
|
4279
|
+
continue;
|
4280
|
+
}
|
4281
|
+
if (field.startsWith('event: ')) {
|
4282
|
+
result.event = field.slice(7);
|
4283
|
+
}
|
4284
|
+
else if (field.startsWith('data: ')) {
|
4285
|
+
if (result.data) {
|
4286
|
+
result.data += '\n';
|
4287
|
+
}
|
4288
|
+
else {
|
4289
|
+
result.data = '';
|
4290
|
+
}
|
4291
|
+
result.data += field.slice(6);
|
4292
|
+
}
|
4293
|
+
else if (field.startsWith('retry: ')) {
|
4294
|
+
result.retry = field.slice(7);
|
4295
|
+
}
|
4296
|
+
}
|
4297
|
+
if (result.data && parseDataAsJSON) {
|
4298
|
+
result.data = JSON.parse(result.data);
|
4299
|
+
}
|
4300
|
+
return result;
|
4301
|
+
};
|
4302
|
+
|
4176
4303
|
const DIALOG_DELAY = 1000;
|
4177
4304
|
const normalizePath = (path) => path.replace(/\/$/, '');
|
4178
4305
|
class ShopGPT extends r$2 {
|
4179
4306
|
constructor() {
|
4180
4307
|
super(...arguments);
|
4308
|
+
this.isStylesheetInjected = false;
|
4181
4309
|
this.latestThreadLoad = DEFAULT_MAX_THREAD_AGE;
|
4182
4310
|
this.modalState = 'close';
|
4183
4311
|
this.isLoadingHistory = false;
|
@@ -4211,6 +4339,12 @@ class ShopGPT extends r$2 {
|
|
4211
4339
|
}
|
4212
4340
|
connectedCallback() {
|
4213
4341
|
super.connectedCallback();
|
4342
|
+
if (!this.isStylesheetInjected && this.css) {
|
4343
|
+
const sheet = new CSSStyleSheet();
|
4344
|
+
sheet.replaceSync(this.css);
|
4345
|
+
this.shadowRoot.adoptedStyleSheets.push(sheet);
|
4346
|
+
this.isStylesheetInjected = true;
|
4347
|
+
}
|
4214
4348
|
if (!this.view || this.view === 'overlay') {
|
4215
4349
|
if (!this.path) {
|
4216
4350
|
return;
|
@@ -4280,27 +4414,12 @@ class ShopGPT extends r$2 {
|
|
4280
4414
|
: undefined;
|
4281
4415
|
try {
|
4282
4416
|
this.isTyping = true;
|
4283
|
-
const
|
4284
|
-
|
4285
|
-
this.setChatTitle(this.selectedThreadId, reply.chatTitle);
|
4286
|
-
}
|
4287
|
-
this.messages = [
|
4288
|
-
{
|
4289
|
-
messageId: reply.messageId,
|
4290
|
-
sender: 'bot',
|
4291
|
-
message: reply.message,
|
4292
|
-
products: reply.products,
|
4293
|
-
welcomePrompts: reply.welcomePrompts,
|
4294
|
-
},
|
4295
|
-
...this.messages,
|
4296
|
-
];
|
4297
|
-
this.products = reply.products || [];
|
4417
|
+
const response = await this.shopGPTAPI.processQuery('', thread.threadId, productHandle);
|
4418
|
+
this.processMessageResponse(response);
|
4298
4419
|
}
|
4299
4420
|
catch (error) {
|
4300
4421
|
logger.error(error);
|
4301
4422
|
this.isFailed = true;
|
4302
|
-
}
|
4303
|
-
finally {
|
4304
4423
|
this.isTyping = false;
|
4305
4424
|
}
|
4306
4425
|
}
|
@@ -4438,6 +4557,92 @@ class ShopGPT extends r$2 {
|
|
4438
4557
|
.catch(logger.error)
|
4439
4558
|
.finally(() => (this.isLoadingThreads = false));
|
4440
4559
|
}
|
4560
|
+
handleMessageChunks(messageData) {
|
4561
|
+
if (!messageData.message) {
|
4562
|
+
return;
|
4563
|
+
}
|
4564
|
+
const latestMessage = this.messages[0];
|
4565
|
+
this.isTyping = false;
|
4566
|
+
if (latestMessage === null || latestMessage === void 0 ? void 0 : latestMessage.isChunk) {
|
4567
|
+
this.messages = [
|
4568
|
+
{
|
4569
|
+
...latestMessage,
|
4570
|
+
message: latestMessage.message + messageData.message,
|
4571
|
+
isChunk: messageData.isChunk,
|
4572
|
+
sender: 'bot',
|
4573
|
+
},
|
4574
|
+
...this.messages.slice(1),
|
4575
|
+
];
|
4576
|
+
}
|
4577
|
+
else {
|
4578
|
+
this.messages = [
|
4579
|
+
{
|
4580
|
+
sender: 'bot',
|
4581
|
+
message: messageData.message || '',
|
4582
|
+
isChunk: messageData.isChunk,
|
4583
|
+
},
|
4584
|
+
...this.messages,
|
4585
|
+
];
|
4586
|
+
}
|
4587
|
+
}
|
4588
|
+
handleCompleteMessage(messageData) {
|
4589
|
+
const latestMessage = this.messages[0];
|
4590
|
+
this.messages = [
|
4591
|
+
{
|
4592
|
+
...latestMessage,
|
4593
|
+
message: messageData.message || '',
|
4594
|
+
messageId: messageData.messageId,
|
4595
|
+
welcomePrompts: messageData.welcomePrompts,
|
4596
|
+
sender: 'bot',
|
4597
|
+
isChunk: false,
|
4598
|
+
},
|
4599
|
+
...this.messages.slice(1),
|
4600
|
+
];
|
4601
|
+
}
|
4602
|
+
handleProductsComplete(products, isMessageCompleted) {
|
4603
|
+
if (!products || !isMessageCompleted) {
|
4604
|
+
return;
|
4605
|
+
}
|
4606
|
+
const latestMessage = this.messages[0];
|
4607
|
+
this.messages = [
|
4608
|
+
{
|
4609
|
+
...latestMessage,
|
4610
|
+
products,
|
4611
|
+
},
|
4612
|
+
...this.messages.slice(1),
|
4613
|
+
];
|
4614
|
+
if (products.length) {
|
4615
|
+
this.products = products;
|
4616
|
+
}
|
4617
|
+
}
|
4618
|
+
processMessageResponse(response) {
|
4619
|
+
let isMessageCompleted = false;
|
4620
|
+
let products;
|
4621
|
+
handleEventSource(response, (eventName, data) => {
|
4622
|
+
if (eventName === 'ChatTitle') {
|
4623
|
+
this.setChatTitle(this.selectedThreadId, data.chatTitle || '');
|
4624
|
+
}
|
4625
|
+
else if (eventName === 'Message') {
|
4626
|
+
this.handleMessageChunks(data);
|
4627
|
+
}
|
4628
|
+
else if (eventName === 'MessageComplete') {
|
4629
|
+
isMessageCompleted = true;
|
4630
|
+
this.handleProductsComplete(products, isMessageCompleted);
|
4631
|
+
this.handleCompleteMessage(data);
|
4632
|
+
}
|
4633
|
+
else if (eventName === 'ProductsComplete') {
|
4634
|
+
this.handleProductsComplete(data.products, isMessageCompleted);
|
4635
|
+
products = data.products;
|
4636
|
+
}
|
4637
|
+
else if (eventName === 'error') {
|
4638
|
+
if (this.messages[0].isChunk) {
|
4639
|
+
this.messages = this.messages.slice(1);
|
4640
|
+
}
|
4641
|
+
this.isFailed = true;
|
4642
|
+
throw new Error(data);
|
4643
|
+
}
|
4644
|
+
});
|
4645
|
+
}
|
4441
4646
|
async sendMessageToServer(e, message) {
|
4442
4647
|
e.preventDefault();
|
4443
4648
|
e.stopPropagation();
|
@@ -4448,32 +4653,12 @@ class ShopGPT extends r$2 {
|
|
4448
4653
|
try {
|
4449
4654
|
this.messages = [{ sender: 'user', message }, ...this.messages];
|
4450
4655
|
this.isTyping = true;
|
4451
|
-
const
|
4452
|
-
|
4453
|
-
return;
|
4454
|
-
}
|
4455
|
-
if (reply.chatTitle) {
|
4456
|
-
this.setChatTitle(this.selectedThreadId, reply.chatTitle);
|
4457
|
-
}
|
4458
|
-
this.messages = [
|
4459
|
-
{
|
4460
|
-
messageId: reply.messageId,
|
4461
|
-
sender: 'bot',
|
4462
|
-
message: reply.message,
|
4463
|
-
products: reply.products,
|
4464
|
-
welcomePrompts: reply.welcomePrompts,
|
4465
|
-
},
|
4466
|
-
...this.messages,
|
4467
|
-
];
|
4468
|
-
if (reply.products && reply.products.length > 0) {
|
4469
|
-
this.products = reply.products;
|
4470
|
-
}
|
4656
|
+
const response = await this.submitQuery(message);
|
4657
|
+
this.processMessageResponse(response);
|
4471
4658
|
}
|
4472
4659
|
catch (err) {
|
4473
4660
|
logger.error(err);
|
4474
4661
|
this.isFailed = true;
|
4475
|
-
}
|
4476
|
-
finally {
|
4477
4662
|
this.isTyping = false;
|
4478
4663
|
}
|
4479
4664
|
}
|
@@ -4520,6 +4705,7 @@ class ShopGPT extends r$2 {
|
|
4520
4705
|
.isLoading=${this.isLoadingThreads}
|
4521
4706
|
.isTyping=${this.isTyping}
|
4522
4707
|
.merchantUrl=${this.merchantUrl}
|
4708
|
+
.css=${this.css}
|
4523
4709
|
></chat-threads>
|
4524
4710
|
<products-section
|
4525
4711
|
.merchantImage=${this.merchantImage}
|
@@ -4527,6 +4713,7 @@ class ShopGPT extends r$2 {
|
|
4527
4713
|
.isLoadingHistory=${this.isLoadingHistory}
|
4528
4714
|
.siteCurrency=${this.getSiteCurrency()}
|
4529
4715
|
.isLoadingThreads=${this.isLoadingThreads}
|
4716
|
+
.css=${this.css}
|
4530
4717
|
></products-section>
|
4531
4718
|
<chat-section
|
4532
4719
|
.prompts=${this.quickPrompts}
|
@@ -4545,6 +4732,8 @@ class ShopGPT extends r$2 {
|
|
4545
4732
|
.viewType=${'overlay'}
|
4546
4733
|
.isLoadingThreads=${this.isLoadingThreads}
|
4547
4734
|
.customPrompts=${this.customPrompts}
|
4735
|
+
.botIconUrl=${this.botIconUrl}
|
4736
|
+
.css=${this.css}
|
4548
4737
|
></chat-section>
|
4549
4738
|
</div>
|
4550
4739
|
</dialog>
|
@@ -4596,6 +4785,8 @@ class ShopGPT extends r$2 {
|
|
4596
4785
|
.isLoadingThreads=${this.isLoadingThreads}
|
4597
4786
|
.merchantImage=${this.merchantImage}
|
4598
4787
|
.customPrompts=${this.customPrompts}
|
4788
|
+
.botIconUrl=${this.botIconUrl}
|
4789
|
+
.css=${this.css}
|
4599
4790
|
></chat-section>
|
4600
4791
|
</div>
|
4601
4792
|
`;
|
@@ -4677,6 +4868,8 @@ if (typeof window != 'undefined' && typeof document != 'undefined') {
|
|
4677
4868
|
shopGPT.quickPrompts = params.quickPrompts;
|
4678
4869
|
shopGPT.merchantImage = params.merchantImage;
|
4679
4870
|
shopGPT.latestThreadLoad = params.latestThreadLoad;
|
4871
|
+
shopGPT.botIconUrl = params.botIconUrl;
|
4872
|
+
shopGPT.css = params.css;
|
4680
4873
|
document.body.append(shopGPT);
|
4681
4874
|
},
|
4682
4875
|
destroy() {
|