@kilnai/widget 0.9.1 → 0.10.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/widget.js +97 -11
- package/package.json +1 -1
package/dist/widget.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
(()=>{var{defineProperty:
|
|
1
|
+
(()=>{var{defineProperty:f,getOwnPropertyNames:w,getOwnPropertyDescriptor:z}=Object,Y=Object.prototype.hasOwnProperty;function $(n){return this[n]}var W=(n)=>{var i=(v??=new WeakMap).get(n),o;if(i)return i;if(i=f({},"__esModule",{value:!0}),n&&typeof n==="object"||typeof n==="function"){for(var r of w(n))if(!Y.call(i,r))f(i,r,{get:$.bind(n,r),enumerable:!(o=z(n,r))||o.enumerable})}return v.set(n,i),i},v;var C=(n)=>n;function M(n,i){this[n]=C.bind(null,i)}var j=(n,i)=>{for(var o in i)f(n,o,{get:i[o],enumerable:!0,configurable:!0,set:M.bind(i,o)})};var K={};j(K,{KilnWidget:()=>p});class u{ws=null;reconnectTimer=null;reconnectDelay=1000;maxReconnectDelay=30000;intentionalClose=!1;messageHandler=null;statusHandler=null;url;userId;constructor(n,i,o){let r=n.startsWith("https")?"wss":"ws",a=n.replace(/^https?:\/\//,"").replace(/^wss?:\/\//,""),l=`kiln_uid_${o}`;this.userId=localStorage.getItem(l)??crypto.randomUUID(),localStorage.setItem(l,this.userId),this.url=`${r}://${a}/apps/${i}/ws?widgetId=${o}&userId=${encodeURIComponent(this.userId)}`}connect(){this.intentionalClose=!1,this.setStatus("connecting");let n=new WebSocket(this.url);this.ws=n,n.onopen=()=>{this.reconnectDelay=1000,this.setStatus("connected")},n.onmessage=(i)=>{try{let o=JSON.parse(i.data);if(o.type==="ping"){try{n.send(JSON.stringify({type:"pong"}))}catch{}return}this.messageHandler?.(o)}catch{}},n.onerror=()=>this.setStatus("error"),n.onclose=()=>{if(this.ws=null,!this.intentionalClose)this.setStatus("disconnected"),this.scheduleReconnect()}}send(n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)return;let i={type:"message",content:n};this.ws.send(JSON.stringify(i))}identify(n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)return;let i={type:"identify",visitor:n};this.ws.send(JSON.stringify(i))}onMessage(n){this.messageHandler=n}onStatusChange(n){this.statusHandler=n}disconnect(){if(this.intentionalClose=!0,this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;this.ws?.close(),this.ws=null,this.setStatus("disconnected")}get connected(){return this.ws?.readyState===WebSocket.OPEN}setStatus(n){this.statusHandler?.(n)}scheduleReconnect(){this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.connect()},this.reconnectDelay),this.reconnectDelay=Math.min(this.reconnectDelay*2,this.maxReconnectDelay)}}function y(n){let i=n==="dark";return`
|
|
2
2
|
:host {
|
|
3
|
-
--kiln-bg: ${
|
|
4
|
-
--kiln-bg-secondary: ${
|
|
5
|
-
--kiln-text: ${
|
|
6
|
-
--kiln-text-secondary: ${
|
|
7
|
-
--kiln-border: ${
|
|
3
|
+
--kiln-bg: ${i?"#1a1a2e":"#ffffff"};
|
|
4
|
+
--kiln-bg-secondary: ${i?"#2a2a3e":"#f0f0f0"};
|
|
5
|
+
--kiln-text: ${i?"#e8e8f0":"#1a1a1a"};
|
|
6
|
+
--kiln-text-secondary: ${i?"#a0a0b8":"#6b7280"};
|
|
7
|
+
--kiln-border: ${i?"#3a3a5c":"#e5e7eb"};
|
|
8
8
|
--kiln-accent: #1a1a2e;
|
|
9
9
|
--kiln-accent-text: #ffffff;
|
|
10
10
|
--kiln-user-bubble: #1a1a2e;
|
|
11
11
|
--kiln-user-text: #ffffff;
|
|
12
|
-
--kiln-assistant-bubble: ${
|
|
13
|
-
--kiln-assistant-text: ${
|
|
12
|
+
--kiln-assistant-bubble: ${i?"#2a2a3e":"#f0f0f0"};
|
|
13
|
+
--kiln-assistant-text: ${i?"#e8e8f0":"#1a1a1a"};
|
|
14
14
|
--kiln-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
|
|
15
15
|
--kiln-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
16
16
|
--kiln-radius: 16px;
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
--kiln-status-error: #ef4444;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
${
|
|
23
|
+
${n==="auto"?`
|
|
24
24
|
@media (prefers-color-scheme: dark) {
|
|
25
25
|
:host {
|
|
26
26
|
--kiln-bg: #1a1a2e;
|
|
@@ -446,5 +446,91 @@
|
|
|
446
446
|
width: 18px;
|
|
447
447
|
height: 18px;
|
|
448
448
|
}
|
|
449
|
-
|
|
450
|
-
|
|
449
|
+
|
|
450
|
+
/* Chat area wrapper */
|
|
451
|
+
#kiln-chat-area {
|
|
452
|
+
display: flex;
|
|
453
|
+
flex-direction: column;
|
|
454
|
+
flex: 1;
|
|
455
|
+
min-height: 0;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
#kiln-chat-area.hidden {
|
|
459
|
+
display: none;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/* Pre-chat form */
|
|
463
|
+
#kiln-prechat-form {
|
|
464
|
+
flex: 1;
|
|
465
|
+
display: flex;
|
|
466
|
+
flex-direction: column;
|
|
467
|
+
gap: 14px;
|
|
468
|
+
padding: 24px 20px;
|
|
469
|
+
overflow-y: auto;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
.kiln-form-title {
|
|
473
|
+
font-size: 14px;
|
|
474
|
+
color: var(--kiln-text-secondary);
|
|
475
|
+
line-height: 1.5;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
.kiln-form-group {
|
|
479
|
+
display: flex;
|
|
480
|
+
flex-direction: column;
|
|
481
|
+
gap: 4px;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
.kiln-form-label {
|
|
485
|
+
font-size: 13px;
|
|
486
|
+
font-weight: 500;
|
|
487
|
+
color: var(--kiln-text);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
.kiln-form-required {
|
|
491
|
+
color: var(--kiln-status-error);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
.kiln-form-input {
|
|
495
|
+
padding: 9px 12px;
|
|
496
|
+
border: 1px solid var(--kiln-border);
|
|
497
|
+
border-radius: 8px;
|
|
498
|
+
background: var(--kiln-bg);
|
|
499
|
+
color: var(--kiln-text);
|
|
500
|
+
font-family: var(--kiln-font);
|
|
501
|
+
font-size: 14px;
|
|
502
|
+
outline: none;
|
|
503
|
+
transition: border-color 0.15s ease;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
.kiln-form-input:focus {
|
|
507
|
+
border-color: var(--kiln-accent);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
.kiln-form-input.kiln-form-error {
|
|
511
|
+
border-color: var(--kiln-status-error);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
.kiln-form-submit {
|
|
515
|
+
margin-top: auto;
|
|
516
|
+
padding: 10px 16px;
|
|
517
|
+
background: var(--kiln-accent);
|
|
518
|
+
color: var(--kiln-accent-text);
|
|
519
|
+
border: none;
|
|
520
|
+
border-radius: 10px;
|
|
521
|
+
font-family: var(--kiln-font);
|
|
522
|
+
font-size: 14px;
|
|
523
|
+
font-weight: 500;
|
|
524
|
+
cursor: pointer;
|
|
525
|
+
transition: opacity 0.15s ease;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
.kiln-form-submit:hover {
|
|
529
|
+
opacity: 0.88;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
.kiln-form-submit:active {
|
|
533
|
+
transform: scale(0.98);
|
|
534
|
+
}
|
|
535
|
+
`}var L='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',F='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',H='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>',m="kiln_visitor_";function P(n){let i=document.createDocumentFragment(),o=/(\*\*[^*]+\*\*|`[^`]+`|\n)/g,r=0,a;while((a=o.exec(n))!==null){if(a.index>r)i.appendChild(document.createTextNode(n.slice(r,a.index)));let l=a[0];if(l===`
|
|
536
|
+
`)i.appendChild(document.createElement("br"));else if(l.startsWith("**")){let e=document.createElement("strong");e.textContent=l.slice(2,-2),i.appendChild(e)}else if(l.startsWith("`")){let e=document.createElement("code");e.textContent=l.slice(1,-1),i.appendChild(e)}r=a.index+l.length}if(r<n.length)i.appendChild(document.createTextNode(n.slice(r)));return i}function g(n){try{let i=localStorage.getItem(`${m}${n}`);return i?JSON.parse(i):null}catch{return null}}function J(n,i){try{localStorage.setItem(`${m}${n}`,JSON.stringify(i))}catch{}}class p{config;client;container;shadow;messages=[];isOpen=!1;isLoading=!1;idCounter=0;greetingShown=!1;identified=!1;panelEl;messagesEl;typingEl;inputEl;sendEl;statusDotEl;launcherEl;formEl=null;chatAreaEl;constructor(n){this.config=n,this.client=new u(n.gatewayUrl,n.appName,n.widgetId);let i=g(n.widgetId);if(i)this.identified=!0;this.container=document.createElement("div"),this.container.id="kiln-widget-root",this.shadow=this.container.attachShadow({mode:"closed"}),document.body.appendChild(this.container);let o=document.createElement("style");if(o.textContent=y(this.resolveTheme()),this.shadow.appendChild(o),this.render(),this.client.onMessage((r)=>this.handleFrame(r)),this.client.onStatusChange((r)=>this.updateStatus(r)),this.client.connect(),n.greeting)this.addMessage({id:String(++this.idCounter),role:"assistant",content:n.greeting,timestamp:Date.now()}),this.greetingShown=!0;if(i)this.client.onStatusChange((r)=>{if(r==="connected"&&this.identified){let a=g(n.widgetId);if(a)this.client.identify(a)}this.updateStatus(r)})}resolveTheme(){let n=this.config.theme??"auto";if(n==="auto")return window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light";return n}render(){let n=this.config.position??"bottom-right",i=document.createElement("button");i.id="kiln-launcher",i.className=`position-${n}`,i.setAttribute("aria-label","Open chat"),i.innerHTML=L,i.addEventListener("click",()=>this.toggle()),this.launcherEl=i;let o=document.createElement("div");o.id="kiln-panel",o.className=`position-${n} hidden`,o.setAttribute("role","dialog"),o.setAttribute("aria-label","Chat");let r=document.createElement("div");r.id="kiln-header";let a=document.createElement("span");a.id="kiln-status-dot",a.className="disconnected",this.statusDotEl=a;let l=document.createElement("span");l.id="kiln-title",l.textContent=this.config.appName;let e=document.createElement("button");e.id="kiln-close",e.setAttribute("aria-label","Close chat"),e.innerHTML=F,e.addEventListener("click",()=>this.close()),r.appendChild(a),r.appendChild(l),r.appendChild(e);let s=document.createElement("div");s.id="kiln-chat-area",this.chatAreaEl=s;let t=document.createElement("div");t.id="kiln-messages",this.messagesEl=t;let d=document.createElement("div");d.id="kiln-typing",d.className="hidden";for(let b=0;b<3;b++){let h=document.createElement("span");h.className="kiln-typing-dot",d.appendChild(h)}this.typingEl=d,t.appendChild(d);let x=document.createElement("div");x.id="kiln-input-area";let k=document.createElement("textarea");k.id="kiln-input",k.rows=1,k.placeholder=this.config.placeholder??"Type a message...",k.setAttribute("aria-label","Message input"),k.addEventListener("keydown",(b)=>{if(b.key==="Enter"&&!b.shiftKey)b.preventDefault(),this.sendMessage()}),k.addEventListener("input",()=>this.autoResizeTextarea()),this.inputEl=k;let c=document.createElement("button");c.id="kiln-send",c.setAttribute("aria-label","Send message"),c.innerHTML=H,c.addEventListener("click",()=>this.sendMessage()),this.sendEl=c,x.appendChild(k),x.appendChild(c),s.appendChild(t),s.appendChild(x),o.appendChild(r),o.appendChild(s),this.panelEl=o,this.shadow.appendChild(i),this.shadow.appendChild(o)}renderPreChatForm(n){if(this.formEl)return;let i=document.createElement("div");i.id="kiln-prechat-form";let o=document.createElement("p");o.className="kiln-form-title",o.textContent="Before we start, tell us a bit about yourself",i.appendChild(o);let r=[];for(let l of n.fields){let e=document.createElement("div");e.className="kiln-form-group";let s=document.createElement("label");if(s.className="kiln-form-label",s.textContent=l.label,l.required){let d=document.createElement("span");d.className="kiln-form-required",d.textContent=" *",s.appendChild(d)}let t=document.createElement("input");t.className="kiln-form-input",t.type=l.type==="phone"?"tel":l.type,t.name=l.key,t.required=l.required,t.setAttribute("aria-label",l.label),r.push({key:l.key,input:t,required:l.required}),e.appendChild(s),e.appendChild(t),i.appendChild(e)}let a=document.createElement("button");a.className="kiln-form-submit",a.textContent=n.submitLabel??"Start Chat",a.addEventListener("click",()=>this.submitPreChatForm(r)),i.appendChild(a),i.addEventListener("keydown",(l)=>{if(l.key==="Enter")l.preventDefault(),this.submitPreChatForm(r)}),this.formEl=i,this.chatAreaEl.classList.add("hidden"),this.panelEl.insertBefore(i,this.chatAreaEl)}submitPreChatForm(n){for(let{input:a,required:l}of n){if(l&&!a.value.trim()){a.classList.add("kiln-form-error"),a.focus();return}a.classList.remove("kiln-form-error")}let i={},o={};for(let{key:a,input:l}of n){let e=l.value.trim();if(!e)continue;if(a==="name"||a==="email"||a==="phone")i[a]=e;else o[a]=e}if(Object.keys(o).length>0)i.custom=o;let r=i;J(this.config.widgetId,r),this.identified=!0,this.client.identify(r),this.formEl?.remove(),this.formEl=null,this.chatAreaEl.classList.remove("hidden"),this.inputEl.focus()}autoResizeTextarea(){this.inputEl.style.height="auto",this.inputEl.style.height=`${Math.min(this.inputEl.scrollHeight,120)}px`}open(){if(this.isOpen=!0,this.panelEl.classList.remove("hidden"),this.launcherEl.setAttribute("aria-expanded","true"),!this.formEl)this.inputEl.focus();this.scrollToBottom()}close(){this.isOpen=!1,this.panelEl.classList.add("hidden"),this.launcherEl.setAttribute("aria-expanded","false")}toggle(){if(this.isOpen)this.close();else this.open()}sendMessage(n){let i=n??this.inputEl.value.trim();if(!i||this.isLoading)return;this.removeSuggestions(),this.addMessage({id:String(++this.idCounter),role:"user",content:i,timestamp:Date.now()}),this.inputEl.value="",this.inputEl.style.height="auto",this.setLoading(!0),this.client.send(i)}addMessage(n){this.messages.push(n),this.renderMessage(n),this.scrollToBottom()}renderMessage(n){let i=document.createElement("div");i.className=`kiln-msg ${n.role}`,i.dataset.msgId=n.id;let o=document.createElement("div");if(o.className="kiln-bubble",n.role==="assistant")o.appendChild(P(n.content));else o.textContent=n.content;i.appendChild(o),this.messagesEl.insertBefore(i,this.typingEl)}renderErrorMessage(n){let i=document.createElement("div");i.className="kiln-msg error";let o=document.createElement("div");o.className="kiln-bubble",o.textContent=n,i.appendChild(o),this.messagesEl.insertBefore(i,this.typingEl),this.scrollToBottom()}setLoading(n){if(this.isLoading=n,this.sendEl.disabled=n,this.inputEl.disabled=n,n)this.typingEl.classList.remove("hidden"),this.scrollToBottom();else this.typingEl.classList.add("hidden")}scrollToBottom(){requestAnimationFrame(()=>{this.messagesEl.scrollTop=this.messagesEl.scrollHeight})}handleFrame(n){if(n.type==="done")this.setLoading(!1),this.addMessage({id:String(++this.idCounter),role:"assistant",content:n.content,timestamp:Date.now()});else if(n.type==="error")if(this.setLoading(!1),n.code==="BUDGET_EXHAUSTED")this.renderInfoMessage(n.message);else this.renderErrorMessage(n.message);else if(n.type==="welcome"){if(n.greeting&&!this.greetingShown)this.addMessage({id:String(++this.idCounter),role:"assistant",content:n.greeting,timestamp:Date.now()}),this.greetingShown=!0;if(n.suggestions&&n.suggestions.length>0)this.renderSuggestions([...n.suggestions]);if(n.preChatForm?.enabled&&!this.identified)this.renderPreChatForm(n.preChatForm);if(this.identified){let i=g(this.config.widgetId);if(i)this.client.identify(i)}}else if(n.type==="suggestions")this.renderSuggestions([...n.items])}renderSuggestions(n){this.removeSuggestions();let i=document.createElement("div");i.className="kiln-suggestions";for(let o of n){let r=document.createElement("button");r.className="kiln-chip",r.textContent=o,r.addEventListener("click",()=>{this.sendMessage(o)}),i.appendChild(r)}this.messagesEl.insertBefore(i,this.typingEl),this.scrollToBottom()}removeSuggestions(){let n=this.messagesEl.querySelector(".kiln-suggestions");if(n)n.remove()}renderInfoMessage(n){let i=document.createElement("div");i.className="kiln-msg info";let o=document.createElement("div");o.className="kiln-bubble",o.textContent=n,i.appendChild(o),this.messagesEl.insertBefore(i,this.typingEl),this.scrollToBottom()}updateStatus(n){this.statusDotEl.className=n}destroy(){this.client.disconnect(),this.container.remove()}}(function(){let n=document.currentScript;if(!n)return;let i=n.dataset.gateway,o=n.dataset.app,r=n.dataset.widgetId;if(!i||!o||!r)return;let a={gatewayUrl:i,appName:o,widgetId:r,position:n.dataset.position??"bottom-right",theme:n.dataset.theme??"auto",greeting:n.dataset.greeting,placeholder:n.dataset.placeholder},l=()=>new p(a);if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",l);else l()})();})();
|