@yassirbenmoussa/aicommerce-sdk 1.2.0 → 1.4.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/dist/ai-commerce.min.js +217 -30
- package/dist/index.cjs +463 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +29 -2
- package/dist/index.d.ts +29 -2
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +463 -14
- package/dist/index.mjs.map +1 -1
- package/dist/widget.min.js +223 -36
- package/dist/widget.min.js.map +1 -1
- package/package.json +1 -1
package/dist/ai-commerce.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var AICommerceSDK=(function(exports){'use strict';/*! AI Commerce SDK v1.0.0 | MIT License | https://aicommerce.dev */
|
|
2
|
-
var
|
|
2
|
+
var O=Object.defineProperty;var ce=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var le=Object.prototype.hasOwnProperty;var B=(o,e)=>()=>(o&&(e=o(o=0)),e);var X=(o,e)=>{for(var r in e)O(o,r,{get:e[r],enumerable:true});},me=(o,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let u of de(e))!le.call(o,u)&&u!==r&&O(o,u,{get:()=>e[u],enumerable:!(a=ce(e,u))||a.enumerable});return o};var W=o=>me(O({},"__esModule",{value:true}),o);var z={};X(z,{AICommerce:()=>exports.AICommerce,AICommerceError:()=>exports.AICommerceError});exports.AICommerce=void 0;exports.AICommerceError=void 0;var A=B(()=>{exports.AICommerce=class o{constructor(e){this.sessionToken=null;this.products={create:async e=>this.request("/api/v1/products",{method:"POST",body:JSON.stringify(e)}),batchUpsert:async e=>this.request("/api/v1/products",{method:"POST",body:JSON.stringify({products:e})}),list:async e=>{let r=new URLSearchParams;e?.page&&r.set("page",String(e.page)),e?.perPage&&r.set("perPage",String(e.perPage)),e?.search&&r.set("search",e.search),e?.categoryId&&r.set("categoryId",e.categoryId),e?.isActive!==void 0&&r.set("isActive",String(e.isActive));let a=r.toString();return this.request(`/api/v1/products${a?`?${a}`:""}`)},get:async e=>this.request(`/api/v1/products/${e}`),update:async(e,r)=>this.request(`/api/v1/products/${e}`,{method:"PUT",body:JSON.stringify(r)}),delete:async e=>this.request(`/api/v1/products/${e}`,{method:"DELETE"})};if(!e.apiKey)throw new Error("AICommerce: apiKey is required");this.apiKey=e.apiKey,this.baseUrl=this.normalizeUrl(e.baseUrl||this.detectBaseUrl()),this.timeout=e.timeout||3e4;}detectBaseUrl(){if(typeof window<"u"){let e=document.querySelector("script[data-aicommerce-url]");if(e)return e.getAttribute("data-aicommerce-url")||"https://api.aicommerce.dev"}return "https://api.aicommerce.dev"}normalizeUrl(e){return e.replace(/\/$/,"")}async request(e,r={}){let a=`${this.baseUrl}${e}`,u=new AbortController,s=setTimeout(()=>u.abort(),this.timeout);try{let m=await fetch(a,{...r,signal:u.signal,headers:{"Content-Type":"application/json","X-API-Key":this.apiKey,...this.sessionToken&&{"X-Session-Token":this.sessionToken},...r.headers}});if(clearTimeout(s),!m.ok){let T=await m.json().catch(()=>({})),f={code:T.code||"UNKNOWN_ERROR",message:T.message||T.error||`HTTP ${m.status}`,status:m.status};throw new exports.AICommerceError(f.message,f.code,f.status)}return m.json()}catch(m){throw clearTimeout(s),m instanceof exports.AICommerceError?m:m instanceof Error&&m.name==="AbortError"?new exports.AICommerceError("Request timeout","TIMEOUT",408):new exports.AICommerceError(m instanceof Error?m.message:"Unknown error","NETWORK_ERROR",0)}}async chat(e,r){let a=typeof e=="string"?{message:e,context:r,sessionToken:this.sessionToken||void 0}:{...e,sessionToken:e.sessionToken||this.sessionToken||void 0},u=await this.request("/api/v1/chat",{method:"POST",body:JSON.stringify(a)});return u.sessionToken&&(this.sessionToken=u.sessionToken),u}async chatWithAudio(e,r){let a=await e.arrayBuffer(),s={audioBase64:btoa(new Uint8Array(a).reduce((T,f)=>T+String.fromCharCode(f),"")),audioMimeType:e.type||"audio/webm",context:r,sessionToken:this.sessionToken||void 0},m=await this.request("/api/v1/chat",{method:"POST",body:JSON.stringify(s)});return m.sessionToken&&(this.sessionToken=m.sessionToken),m}async createSession(){let e=await this.request("/api/v1/chat/session",{method:"POST"});return this.sessionToken=e.session.token,e.session}clearSession(){this.sessionToken=null;}getSessionToken(){return this.sessionToken}setSessionToken(e){this.sessionToken=e;}async upload(e,r){let a=new FormData;a.append("file",e),r?.folder&&a.append("folder",r.folder),r?.productId&&a.append("productId",r.productId),r?.isPrimary&&a.append("isPrimary","true");let u=`${this.baseUrl}/api/v1/upload`,s=await fetch(u,{method:"POST",headers:{"X-API-Key":this.apiKey},body:a});if(!s.ok){let m=await s.json().catch(()=>({}));throw new exports.AICommerceError(m.message||m.error||`HTTP ${s.status}`,m.code||"UPLOAD_ERROR",s.status)}return s.json()}static async quickChat(e){return new o({apiKey:e.apiKey,baseUrl:e.baseUrl}).chat(e.message,e.context)}},exports.AICommerceError=class o extends Error{constructor(e,r,a){super(e),this.name="AICommerceError",this.code=r,this.status=a,Object.setPrototypeOf(this,o.prototype);}};});function ue(o){let e=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(o);return e?{r:parseInt(e[1],16),g:parseInt(e[2],16),b:parseInt(e[3],16)}:{r:99,g:102,b:241}}function D(o){let e=o.primaryColor,r=ue(e),a=o.position==="bottom-left";return `
|
|
3
3
|
/* AI Commerce Widget Styles */
|
|
4
4
|
#aicommerce-widget {
|
|
5
5
|
--aic-primary: ${e};
|
|
@@ -13,14 +13,14 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
13
13
|
--aic-border: #e2e8f0;
|
|
14
14
|
--aic-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
|
|
15
15
|
--aic-radius: 16px;
|
|
16
|
-
--aic-z-index: ${
|
|
16
|
+
--aic-z-index: ${o.zIndex};
|
|
17
17
|
|
|
18
18
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
|
|
19
19
|
font-size: 14px;
|
|
20
20
|
line-height: 1.5;
|
|
21
21
|
position: fixed;
|
|
22
22
|
bottom: 20px;
|
|
23
|
-
${
|
|
23
|
+
${a?"left: 20px;":"right: 20px;"}
|
|
24
24
|
z-index: var(--aic-z-index);
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -74,7 +74,7 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
74
74
|
.aicommerce-chat {
|
|
75
75
|
position: absolute;
|
|
76
76
|
bottom: 0;
|
|
77
|
-
${
|
|
77
|
+
${a?"left: 0;":"right: 0;"}
|
|
78
78
|
width: 380px;
|
|
79
79
|
max-width: calc(100vw - 40px);
|
|
80
80
|
height: 600px;
|
|
@@ -86,7 +86,7 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
86
86
|
flex-direction: column;
|
|
87
87
|
overflow: hidden;
|
|
88
88
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
|
89
|
-
transform-origin: bottom ${
|
|
89
|
+
transform-origin: bottom ${a?"left":"right"};
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
.aicommerce-chat.aicommerce-closed {
|
|
@@ -245,15 +245,24 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
245
245
|
/* Product Cards */
|
|
246
246
|
.aicommerce-products {
|
|
247
247
|
display: flex;
|
|
248
|
-
gap:
|
|
248
|
+
gap: 16px;
|
|
249
249
|
margin-top: 12px;
|
|
250
250
|
overflow-x: auto;
|
|
251
|
-
padding-bottom:
|
|
251
|
+
padding-bottom: 16px;
|
|
252
|
+
width: 100%;
|
|
253
|
+
max-width: 100%;
|
|
254
|
+
cursor: grab;
|
|
255
|
+
user-select: none;
|
|
256
|
+
-webkit-user-select: none;
|
|
257
|
+
scrollbar-width: none; /* Firefox */
|
|
258
|
+
}
|
|
259
|
+
.aicommerce-products::-webkit-scrollbar {
|
|
260
|
+
display: none; /* Chrome/Safari */
|
|
252
261
|
}
|
|
253
262
|
|
|
254
263
|
.aicommerce-product-card {
|
|
255
264
|
flex-shrink: 0;
|
|
256
|
-
width:
|
|
265
|
+
width: 280px;
|
|
257
266
|
background: var(--aic-bg);
|
|
258
267
|
border-radius: 12px;
|
|
259
268
|
overflow: hidden;
|
|
@@ -269,13 +278,15 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
269
278
|
|
|
270
279
|
.aicommerce-product-image {
|
|
271
280
|
width: 100%;
|
|
272
|
-
|
|
281
|
+
aspect-ratio: 16/9;
|
|
282
|
+
height: auto;
|
|
273
283
|
object-fit: cover;
|
|
274
284
|
}
|
|
275
285
|
|
|
276
286
|
.aicommerce-product-placeholder {
|
|
277
287
|
width: 100%;
|
|
278
|
-
|
|
288
|
+
aspect-ratio: 16/9;
|
|
289
|
+
height: auto;
|
|
279
290
|
background: var(--aic-bg-secondary);
|
|
280
291
|
display: flex;
|
|
281
292
|
align-items: center;
|
|
@@ -305,6 +316,109 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
305
316
|
color: var(--aic-primary);
|
|
306
317
|
}
|
|
307
318
|
|
|
319
|
+
.aicommerce-product-desc {
|
|
320
|
+
font-size: 12px;
|
|
321
|
+
color: var(--aic-text-secondary);
|
|
322
|
+
line-height: 1.4;
|
|
323
|
+
display: -webkit-box;
|
|
324
|
+
-webkit-line-clamp: 2;
|
|
325
|
+
-webkit-box-orient: vertical;
|
|
326
|
+
overflow: hidden;
|
|
327
|
+
margin-top: 4px;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/* Audio Player */
|
|
331
|
+
.aicommerce-audio-player {
|
|
332
|
+
display: flex;
|
|
333
|
+
align-items: center;
|
|
334
|
+
gap: 12px;
|
|
335
|
+
min-width: 240px;
|
|
336
|
+
padding: 4px 0;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
.aicommerce-audio-btn {
|
|
340
|
+
width: 40px;
|
|
341
|
+
height: 40px;
|
|
342
|
+
border-radius: 50%;
|
|
343
|
+
display: flex;
|
|
344
|
+
align-items: center;
|
|
345
|
+
justify-content: center;
|
|
346
|
+
border: none;
|
|
347
|
+
cursor: pointer;
|
|
348
|
+
transition: all 0.2s;
|
|
349
|
+
background: rgba(255, 255, 255, 0.25);
|
|
350
|
+
color: white;
|
|
351
|
+
flex-shrink: 0;
|
|
352
|
+
padding: 0;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
.aicommerce-audio-btn:hover {
|
|
356
|
+
background: rgba(255, 255, 255, 0.35);
|
|
357
|
+
transform: scale(1.05);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
.aicommerce-audio-btn:active {
|
|
361
|
+
transform: scale(0.95);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/* Invert colors for assistant (since background is white/gray) */
|
|
365
|
+
.aicommerce-assistant .aicommerce-audio-btn {
|
|
366
|
+
background: var(--aic-primary);
|
|
367
|
+
color: white;
|
|
368
|
+
}
|
|
369
|
+
.aicommerce-assistant .aicommerce-audio-btn:hover {
|
|
370
|
+
background: var(--aic-primary-dark);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
.aicommerce-audio-waveform {
|
|
374
|
+
flex: 1;
|
|
375
|
+
display: flex;
|
|
376
|
+
flex-direction: column;
|
|
377
|
+
gap: 6px;
|
|
378
|
+
min-width: 0; /* Prevent overflow */
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
.aicommerce-waveform-bars {
|
|
382
|
+
display: flex;
|
|
383
|
+
align-items: center;
|
|
384
|
+
gap: 2px;
|
|
385
|
+
height: 24px;
|
|
386
|
+
cursor: pointer;
|
|
387
|
+
width: 100%;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
.aicommerce-waveform-bar {
|
|
391
|
+
width: 3px;
|
|
392
|
+
border-radius: 2px;
|
|
393
|
+
min-height: 3px;
|
|
394
|
+
transition: background-color 0.1s;
|
|
395
|
+
flex-shrink: 0;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
.aicommerce-audio-time {
|
|
399
|
+
display: flex;
|
|
400
|
+
justify-content: space-between;
|
|
401
|
+
font-size: 11px;
|
|
402
|
+
font-weight: 500;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
.aicommerce-user .aicommerce-audio-time {
|
|
406
|
+
color: rgba(255, 255, 255, 0.8);
|
|
407
|
+
}
|
|
408
|
+
.aicommerce-assistant .aicommerce-audio-time {
|
|
409
|
+
color: var(--aic-text-secondary);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/* RTL Support */
|
|
413
|
+
.aicommerce-rtl {
|
|
414
|
+
direction: rtl;
|
|
415
|
+
text-align: right;
|
|
416
|
+
}
|
|
417
|
+
.aicommerce-ltr {
|
|
418
|
+
direction: ltr;
|
|
419
|
+
text-align: left;
|
|
420
|
+
}
|
|
421
|
+
|
|
308
422
|
/* Input Area */
|
|
309
423
|
.aicommerce-input-container {
|
|
310
424
|
padding: 16px 20px;
|
|
@@ -359,11 +473,49 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
359
473
|
cursor: not-allowed;
|
|
360
474
|
}
|
|
361
475
|
|
|
476
|
+
/* Microphone Button */
|
|
477
|
+
.aicommerce-mic {
|
|
478
|
+
width: 44px;
|
|
479
|
+
height: 44px;
|
|
480
|
+
border-radius: 50%;
|
|
481
|
+
background: var(--aic-bg-secondary);
|
|
482
|
+
border: 1px solid var(--aic-border);
|
|
483
|
+
color: var(--aic-text-secondary);
|
|
484
|
+
cursor: pointer;
|
|
485
|
+
display: flex;
|
|
486
|
+
align-items: center;
|
|
487
|
+
justify-content: center;
|
|
488
|
+
transition: all 0.2s;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
.aicommerce-mic:hover:not(:disabled) {
|
|
492
|
+
background: var(--aic-primary-light);
|
|
493
|
+
border-color: var(--aic-primary);
|
|
494
|
+
color: var(--aic-primary);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
.aicommerce-mic.aicommerce-recording {
|
|
498
|
+
background: #ef4444;
|
|
499
|
+
border-color: #ef4444;
|
|
500
|
+
color: white;
|
|
501
|
+
animation: aic-recording-pulse 1s infinite;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
.aicommerce-mic:disabled {
|
|
505
|
+
opacity: 0.6;
|
|
506
|
+
cursor: not-allowed;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
@keyframes aic-recording-pulse {
|
|
510
|
+
0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
|
|
511
|
+
50% { transform: scale(1.05); box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
|
|
512
|
+
}
|
|
513
|
+
|
|
362
514
|
/* Mobile Responsive */
|
|
363
515
|
@media (max-width: 420px) {
|
|
364
516
|
#aicommerce-widget {
|
|
365
517
|
bottom: 16px;
|
|
366
|
-
${
|
|
518
|
+
${a?"left: 16px;":"right: 16px;"}
|
|
367
519
|
}
|
|
368
520
|
|
|
369
521
|
.aicommerce-chat {
|
|
@@ -395,10 +547,10 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
395
547
|
.aicommerce-messages::-webkit-scrollbar-thumb:hover {
|
|
396
548
|
background: var(--aic-text-secondary);
|
|
397
549
|
}
|
|
398
|
-
`}function
|
|
399
|
-
${
|
|
550
|
+
`}function Y(o){let e=document.createElement("style");e.id="aicommerce-widget-styles",e.textContent=o;let r=document.getElementById("aicommerce-widget-styles");return r&&r.remove(),document.head.appendChild(e),e}var J=B(()=>{});var V={};X(V,{AICommerceWidget:()=>exports.AICommerceWidget,createWidget:()=>j});function j(o){if(!o.apiKey)throw new Error("AICommerceWidget: apiKey is required");let e=new exports.AICommerce({apiKey:o.apiKey,baseUrl:o.baseUrl}),r={isOpen:false,isLoading:true,isRecording:false,messages:[],storeConfig:null},a=null,u=[],s=null,f=null,p;async function G(){try{let t=o.baseUrl||K(),i=await fetch(`${t}/api/v1/store`,{headers:{"x-api-key":o.apiKey}});return i.ok?(await i.json()).store:null}catch(t){return console.error("Failed to fetch store config:",t),null}}function K(){if(typeof window<"u"){let t=document.querySelector("script[data-aicommerce-url]");if(t)return t.getAttribute("data-aicommerce-url")||""}return "https://api.aicommerce.dev"}async function _(){r.storeConfig=await G(),p={apiKey:o.apiKey,baseUrl:o.baseUrl||K(),position:o.position||"bottom-right",theme:o.theme||"auto",primaryColor:o.primaryColor||r.storeConfig?.primaryColor||"#6366f1",welcomeMessage:o.welcomeMessage||r.storeConfig?.welcomeMessage||"Hi! How can I help you find the perfect product today?",botName:o.botName||r.storeConfig?.chatBotName||"Shopping Assistant",zIndex:o.zIndex||9999,buttonText:o.buttonText||"\u{1F4AC}",hideLauncher:o.hideLauncher||false,onOpen:o.onOpen,onClose:o.onClose,onProductClick:o.onProductClick,onMessage:o.onMessage};let t=D(p);f=Y(t),s=document.createElement("div"),s.id="aicommerce-widget",s.className=`aicommerce-widget aicommerce-${p.position} aicommerce-theme-${p.theme}`,document.body.appendChild(s),b(),r.messages.push({role:"assistant",content:p.welcomeMessage}),r.isLoading=false,b();}function b(){if(!s)return;let t=`
|
|
551
|
+
${p.hideLauncher?"":`
|
|
400
552
|
<button class="aicommerce-launcher ${r.isOpen?"aicommerce-hidden":""}" aria-label="Open chat">
|
|
401
|
-
<span class="aicommerce-launcher-icon">${
|
|
553
|
+
<span class="aicommerce-launcher-icon">${p.buttonText}</span>
|
|
402
554
|
</button>
|
|
403
555
|
`}
|
|
404
556
|
|
|
@@ -406,10 +558,10 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
406
558
|
<div class="aicommerce-header">
|
|
407
559
|
<div class="aicommerce-header-info">
|
|
408
560
|
<div class="aicommerce-avatar">
|
|
409
|
-
${r.storeConfig?.logo?`<img src="${r.storeConfig.logo}" alt="${
|
|
561
|
+
${r.storeConfig?.logo?`<img src="${r.storeConfig.logo}" alt="${p.botName}" />`:"<span>\u{1F916}</span>"}
|
|
410
562
|
</div>
|
|
411
563
|
<div class="aicommerce-header-text">
|
|
412
|
-
<span class="aicommerce-bot-name">${
|
|
564
|
+
<span class="aicommerce-bot-name">${p.botName}</span>
|
|
413
565
|
<span class="aicommerce-status">Online</span>
|
|
414
566
|
</div>
|
|
415
567
|
</div>
|
|
@@ -417,28 +569,31 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
417
569
|
</div>
|
|
418
570
|
|
|
419
571
|
<div class="aicommerce-messages">
|
|
420
|
-
${r.messages.map(
|
|
421
|
-
<div class="aicommerce-message aicommerce-${
|
|
422
|
-
<div class="aicommerce-message-content"
|
|
423
|
-
|
|
572
|
+
${r.messages.map((n,y)=>{let l=te(n.content),P=n.role==="user";return `
|
|
573
|
+
<div class="aicommerce-message aicommerce-${n.role}">
|
|
574
|
+
<div class="aicommerce-message-content ${l?"aicommerce-rtl":"aicommerce-ltr"}">
|
|
575
|
+
${n.audioUrl?Q(n,y,P):E(n.content)}
|
|
576
|
+
</div>
|
|
577
|
+
${n.products&&n.products.length>0?`
|
|
424
578
|
<div class="aicommerce-products">
|
|
425
|
-
${
|
|
426
|
-
<div class="aicommerce-product-card" data-product-id="${
|
|
427
|
-
${
|
|
428
|
-
<img src="${
|
|
579
|
+
${n.products.map(g=>`
|
|
580
|
+
<div class="aicommerce-product-card" data-product-id="${g.id}">
|
|
581
|
+
${g.image||g.imageUrl?`
|
|
582
|
+
<img src="${g.image||g.imageUrl}" alt="${E(g.name)}" class="aicommerce-product-image" />
|
|
429
583
|
`:`
|
|
430
584
|
<div class="aicommerce-product-placeholder">\u{1F4E6}</div>
|
|
431
585
|
`}
|
|
432
586
|
<div class="aicommerce-product-info">
|
|
433
|
-
<span class="aicommerce-product-name">${
|
|
434
|
-
|
|
587
|
+
<span class="aicommerce-product-name" title="${E(g.name)}">${E(g.name)}</span>
|
|
588
|
+
${g.description?`<p class="aicommerce-product-desc">${E(g.description)}</p>`:""}
|
|
589
|
+
<span class="aicommerce-product-price">${ne(g.price,g.currency)}</span>
|
|
435
590
|
</div>
|
|
436
591
|
</div>
|
|
437
592
|
`).join("")}
|
|
438
593
|
</div>
|
|
439
594
|
`:""}
|
|
440
595
|
</div>
|
|
441
|
-
`).join("")}
|
|
596
|
+
`}).join("")}
|
|
442
597
|
${r.isLoading?`
|
|
443
598
|
<div class="aicommerce-message aicommerce-assistant">
|
|
444
599
|
<div class="aicommerce-typing">
|
|
@@ -453,14 +608,46 @@ var k=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var F=Object.g
|
|
|
453
608
|
type="text"
|
|
454
609
|
class="aicommerce-input"
|
|
455
610
|
placeholder="Type your message..."
|
|
456
|
-
${r.isLoading?"disabled":""}
|
|
611
|
+
${r.isLoading||r.isRecording?"disabled":""}
|
|
457
612
|
/>
|
|
458
|
-
<button class="aicommerce-
|
|
613
|
+
<button class="aicommerce-mic ${r.isRecording?"aicommerce-recording":""}" ${r.isLoading?"disabled":""} aria-label="${r.isRecording?"Stop recording":"Voice input"}">
|
|
614
|
+
${r.isRecording?`
|
|
615
|
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
|
|
616
|
+
<rect x="6" y="6" width="12" height="12" rx="2"/>
|
|
617
|
+
</svg>
|
|
618
|
+
`:`
|
|
619
|
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
620
|
+
<path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"/>
|
|
621
|
+
<path d="M19 10v2a7 7 0 0 1-14 0v-2"/>
|
|
622
|
+
<line x1="12" y1="19" x2="12" y2="23"/>
|
|
623
|
+
<line x1="8" y1="23" x2="16" y2="23"/>
|
|
624
|
+
</svg>
|
|
625
|
+
`}
|
|
626
|
+
</button>
|
|
627
|
+
<button class="aicommerce-send" ${r.isLoading||r.isRecording?"disabled":""} aria-label="Send message">
|
|
459
628
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
460
629
|
<path d="M22 2L11 13M22 2L15 22L11 13M22 2L2 9L11 13"/>
|
|
461
630
|
</svg>
|
|
462
631
|
</button>
|
|
463
632
|
</div>
|
|
464
633
|
</div>
|
|
465
|
-
`;
|
|
634
|
+
`;s.innerHTML=t,Z();let i=s.querySelector(".aicommerce-messages");i&&(i.scrollTop=i.scrollHeight);}function Q(t,i,n){return `
|
|
635
|
+
<div class="aicommerce-audio-player" data-message-index="${i}">
|
|
636
|
+
<button class="aicommerce-audio-btn">
|
|
637
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
|
|
638
|
+
</button>
|
|
639
|
+
<div class="aicommerce-audio-waveform">
|
|
640
|
+
<div class="aicommerce-waveform-bars">
|
|
641
|
+
${(t.waveformBars||Array(40).fill(10)).map(y=>`
|
|
642
|
+
<div class="aicommerce-waveform-bar" style="height: ${y}%; background-color: ${n?"rgba(255,255,255,0.4)":"rgba(99,102,241,0.3)"}"></div>
|
|
643
|
+
`).join("")}
|
|
644
|
+
</div>
|
|
645
|
+
<div class="aicommerce-audio-time">
|
|
646
|
+
<span class="aicommerce-current-time">0:00</span>
|
|
647
|
+
<span>${N(t.audioDuration||0)}</span>
|
|
648
|
+
</div>
|
|
649
|
+
</div>
|
|
650
|
+
<audio src="${t.audioUrl}" preload="metadata"></audio>
|
|
651
|
+
</div>
|
|
652
|
+
`}function Z(){if(!s)return;let t=s.querySelector(".aicommerce-launcher");t&&t.addEventListener("click",()=>I());let i=s.querySelector(".aicommerce-close");i&&i.addEventListener("click",()=>M());let n=s.querySelector(".aicommerce-input"),y=s.querySelector(".aicommerce-send");n&&n.addEventListener("keypress",c=>{c.key==="Enter"&&n.value.trim()&&(L(n.value.trim()),n.value="");}),y&&n&&y.addEventListener("click",()=>{n.value.trim()&&(L(n.value.trim()),n.value="");});let l=s.querySelector(".aicommerce-mic");l&&l.addEventListener("click",()=>ee()),s.querySelectorAll(".aicommerce-product-card").forEach(c=>{c.addEventListener("click",()=>{let d=c.getAttribute("data-product-id"),h=r.messages.flatMap(x=>x.products||[]).find(x=>x.id===d);h&&p.onProductClick&&p.onProductClick(h);});}),s.querySelectorAll(".aicommerce-products").forEach(c=>{let d=false,h=0,x=0;c.addEventListener("mousedown",k=>{d=true,c.style.cursor="grabbing",h=k.pageX-c.offsetLeft,x=c.scrollLeft;}),c.addEventListener("mouseleave",()=>{d=false,c.style.cursor="grab";}),c.addEventListener("mouseup",()=>{d=false,c.style.cursor="grab";}),c.addEventListener("mousemove",k=>{if(!d)return;k.preventDefault();let v=(k.pageX-c.offsetLeft-h)*2;c.scrollLeft=x-v;});}),s.querySelectorAll(".aicommerce-audio-player").forEach(c=>{let d=c.querySelector("audio"),h=c.querySelector(".aicommerce-audio-btn"),x=c.querySelectorAll(".aicommerce-waveform-bar"),k=c.querySelector(".aicommerce-current-time");if(!d||!h)return;h.addEventListener("click",()=>{!d.paused?(d.pause(),h.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>'):(s?.querySelectorAll("audio").forEach(w=>{if(w!==d&&!w.paused){w.pause();let $=w.closest(".aicommerce-audio-player")?.querySelector(".aicommerce-audio-btn");$&&($.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>');}}),d.play(),h.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>');}),d.addEventListener("timeupdate",()=>{if(k&&(k.textContent=N(d.currentTime)),d.duration){let v=d.currentTime/d.duration*100;x.forEach((w,q)=>{q/x.length*100<=v?w.style.backgroundColor=c.closest(".aicommerce-user")?"rgba(255,255,255,1)":"var(--aic-primary)":w.style.backgroundColor=c.closest(".aicommerce-user")?"rgba(255,255,255,0.4)":"rgba(99,102,241,0.3)";});}}),d.addEventListener("ended",()=>{h.innerHTML='<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>';});let S=c.querySelector(".aicommerce-waveform-bars");S&&S.addEventListener("click",v=>{let w=S.getBoundingClientRect(),$=(v.clientX-w.left)/w.width;d.duration&&(d.currentTime=$*d.duration);});});}async function ee(){if(r.isRecording)a&&a.state!=="inactive"&&a.stop();else try{let t=await navigator.mediaDevices.getUserMedia({audio:!0});u=[],a=new MediaRecorder(t,{mimeType:MediaRecorder.isTypeSupported("audio/webm")?"audio/webm":"audio/mp4"}),a.ondataavailable=i=>{i.data.size>0&&u.push(i.data);},a.onstop=async()=>{if(t.getTracks().forEach(i=>i.stop()),u.length>0){let i=new Blob(u,{type:a?.mimeType||"audio/webm"});await re(i);}r.isRecording=!1,b();},a.start(),r.isRecording=!0,b();}catch(t){console.error("Failed to start recording:",t),r.messages.push({role:"assistant",content:"Unable to access microphone. Please check your permissions."}),b();}}async function re(t){let i=URL.createObjectURL(t),n=Array(40).fill(10),y=0;try{n=await oe(t);let l=new Audio(i);await new Promise(P=>{l.onloadedmetadata=()=>{y=l.duration,P();},l.onerror=()=>P();});}catch(l){console.error("Audio analysis failed",l);}r.messages.push({role:"user",content:"Voice message",audioUrl:i,audioDuration:y,waveformBars:n}),r.isLoading=true,b();try{let l=await e.chatWithAudio(t);return r.messages.push({role:"assistant",content:l.reply,products:l.products}),p.onMessage&&p.onMessage("Voice message",l),l}catch(l){throw r.messages.push({role:"assistant",content:"Sorry, I encountered an error processing your voice message. Please try again."}),l}finally{r.isLoading=false,b();}}function te(t){return /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/.test(t)}function N(t){let i=Math.floor(t/60),n=Math.floor(t%60);return `${i}:${n.toString().padStart(2,"0")}`}async function oe(t){try{let i=new(window.AudioContext||window.webkitAudioContext),n=await t.arrayBuffer(),l=(await i.decodeAudioData(n)).getChannelData(0),P=40,g=Math.floor(l.length/P),U=[];for(let c=0;c<P;c++){let d=c*g,h=d+g,x=0;for(let v=d;v<h;v++)l[v]&&(x+=l[v]*l[v]);let k=Math.sqrt(x/g),S=Math.min(100,Math.max(10,k*400));U.push(S);}return U}catch(i){return console.error("Analysis error",i),Array.from({length:40},()=>20+Math.random()*60)}}async function L(t){r.messages.push({role:"user",content:t}),r.isLoading=true,b();try{let i=await e.chat(t);return r.messages.push({role:"assistant",content:i.reply,products:i.products}),p.onMessage&&p.onMessage(t,i),i}catch(i){throw r.messages.push({role:"assistant",content:"Sorry, I encountered an error. Please try again."}),i}finally{r.isLoading=false,b();}}function I(){r.isOpen=true,b(),p.onOpen?.(),setTimeout(()=>{s?.querySelector(".aicommerce-input")?.focus();},100);}function M(){r.isOpen=false,b(),p.onClose?.();}function ie(){r.isOpen?M():I();}function ae(){s&&(s.remove(),s=null),f&&(f.remove(),f=null);}function se(t){if(Object.assign(p,t),t.primaryColor){let i=D(p);f&&(f.textContent=i);}b();}function E(t){let i=document.createElement("div");return i.textContent=t,i.innerHTML}function ne(t,i){let y={USD:"$",EUR:"\u20AC",GBP:"\xA3",MAD:"DH",SAR:"SAR",AED:"AED",JPY:"\xA5",CNY:"\xA5"}[i||"USD"]||i||"$";return `${t.toFixed(2)} ${y}`}return _(),{open:I,close:M,toggle:ie,destroy:ae,sendMessage:L,updateConfig:se}}exports.AICommerceWidget=void 0;var F=B(()=>{A();J();exports.AICommerceWidget={init:j,VERSION:"1.0.0"};typeof window<"u"&&(window.AICommerceWidget=exports.AICommerceWidget);});A();F();var be="1.0.0";typeof window<"u"&&(window.AICommerce=(A(),W(z)).AICommerce,window.AICommerceError=(A(),W(z)).AICommerceError,window.AICommerceWidget=(F(),W(V)).AICommerceWidget);exports.VERSION=be;exports.createWidget=j;return exports;})({});//# sourceMappingURL=index.min.js.map
|
|
466
653
|
//# sourceMappingURL=index.min.js.map
|