@trillet-ai/web-sdk 1.9.1 → 1.9.2
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/index.d.ts +11 -0
- package/dist/widget/index.js +136 -1
- package/dist/widget/index.js.map +1 -1
- package/dist/widget.umd.js +53 -1
- package/dist/widget.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/widget.umd.js
CHANGED
|
@@ -3473,6 +3473,58 @@ a=extmap-allow-mixed`)!==-1){let Q=B.sdp.split(`
|
|
|
3473
3473
|
background-position: center;
|
|
3474
3474
|
animation: none;
|
|
3475
3475
|
}
|
|
3476
|
+
.tw-launcher-copy {
|
|
3477
|
+
display: flex;
|
|
3478
|
+
flex-direction: column;
|
|
3479
|
+
align-items: flex-start;
|
|
3480
|
+
line-height: 1.15;
|
|
3481
|
+
text-align: left;
|
|
3482
|
+
}
|
|
3483
|
+
.tw-launcher-state {
|
|
3484
|
+
font-size: 11px;
|
|
3485
|
+
font-weight: 500;
|
|
3486
|
+
opacity: 0.92;
|
|
3487
|
+
}
|
|
3488
|
+
/* Voice calls stay on this control: connecting, then hang up. */
|
|
3489
|
+
.tw-launcher.tw-hangup { background: #e5484d; }
|
|
3490
|
+
.tw-launcher.tw-listening {
|
|
3491
|
+
box-shadow: var(--trillet-shadow), 0 0 0 3px rgba(255, 255, 255, 0.45);
|
|
3492
|
+
}
|
|
3493
|
+
.tw-launcher.tw-speaking {
|
|
3494
|
+
box-shadow: var(--trillet-shadow), 0 0 0 4px rgba(255, 255, 255, 0.8);
|
|
3495
|
+
}
|
|
3496
|
+
.tw-launcher.tw-speaking .tw-launcher-orb {
|
|
3497
|
+
animation: tw-orb-rotate 4.5s linear infinite, tw-bob 0.7s ease-in-out infinite alternate;
|
|
3498
|
+
}
|
|
3499
|
+
.tw-launcher.tw-connecting { cursor: default; }
|
|
3500
|
+
.tw-launcher.tw-connecting .tw-launcher-orb {
|
|
3501
|
+
animation: tw-orb-rotate 1.2s linear infinite;
|
|
3502
|
+
}
|
|
3503
|
+
.tw-launcher:disabled { opacity: 0.72; cursor: default; }
|
|
3504
|
+
.tw-launcher:disabled:hover { transform: none; box-shadow: var(--trillet-shadow); }
|
|
3505
|
+
|
|
3506
|
+
/* Optional reCAPTCHA sits with the floating control. Not a call panel. */
|
|
3507
|
+
.tw-inline {
|
|
3508
|
+
display: flex;
|
|
3509
|
+
flex-direction: column;
|
|
3510
|
+
align-items: flex-end;
|
|
3511
|
+
gap: 10px;
|
|
3512
|
+
}
|
|
3513
|
+
:host([placement="bottom-left"]) .tw-inline,
|
|
3514
|
+
:host([placement="top-left"]) .tw-inline { align-items: flex-start; }
|
|
3515
|
+
:host([placement="bottom"]) .tw-inline,
|
|
3516
|
+
:host([placement="top"]) .tw-inline { align-items: center; }
|
|
3517
|
+
.tw-inline-hint {
|
|
3518
|
+
max-width: 304px;
|
|
3519
|
+
padding: 8px 12px;
|
|
3520
|
+
border-radius: 10px;
|
|
3521
|
+
background: var(--trillet-surface);
|
|
3522
|
+
color: var(--trillet-on-surface);
|
|
3523
|
+
border: 1px solid var(--trillet-border);
|
|
3524
|
+
box-shadow: var(--trillet-shadow);
|
|
3525
|
+
font-size: 12px;
|
|
3526
|
+
font-weight: 600;
|
|
3527
|
+
}
|
|
3476
3528
|
|
|
3477
3529
|
/* ---- Panel ---- */
|
|
3478
3530
|
.tw-panel {
|
|
@@ -3827,5 +3879,5 @@ a=extmap-allow-mixed`)!==-1){let Q=B.sdp.split(`
|
|
|
3827
3879
|
}
|
|
3828
3880
|
|
|
3829
3881
|
.tw-hidden { display: none !important; }
|
|
3830
|
-
`;var vN="https://www.google.com/recaptcha/api.js?render=explicit";function dK(){return window.grecaptcha}var hB=null;function JK(){return hB||(hB=new Promise((C,A)=>{if(document.querySelector(`script[src="${vN}"]`)){C();return}let I=document.createElement("script");I.src=vN,I.async=!0,I.defer=!0,I.onload=()=>C(),I.onerror=()=>{hB=null,A(new Error("Failed to load Google reCAPTCHA"))},document.head.appendChild(I)}),hB)}async function LK(){await JK();let C=dK();if(!C)throw new Error("reCAPTCHA loaded but grecaptcha is undefined");return await new Promise(A=>C.ready(A)),C}async function XN(C){return(await LK()).render(C.container,{sitekey:C.siteKey,callback:()=>C.onSolved(),"expired-callback":()=>C.onExpired(),"error-callback":()=>C.onExpired()})}var qI=class extends HTMLElement{constructor(){super();this.agent=null;this.state="idle";this.open=!1;this.micEnabled=!0;this.speaking=!1;this.transcripts=[];this.display=null;this.activeMode=null;this.switching=!1;this.durationTimer=null;this.remainingSeconds=null;this.elapsedSeconds=0;this.recaptchaSolved=!1;this.recaptchaSlot=null;this.recaptchaWidgetId=null;this.root=this.attachShadow({mode:"open"}),this.styleEl=document.createElement("style"),this.styleEl.textContent=VN,this.container=document.createElement("div"),this.container.className="tw-root",this.root.appendChild(this.styleEl),this.root.appendChild(this.container)}static get observedAttributes(){return WN}connectedCallback(){this.config=bD(this),this.syncActiveMode(),this.applyTheme(),this.render()}disconnectedCallback(){this.clearDurationTimer(),this.teardownAgent(),this.removeRecaptchaSlot()}attributeChangedCallback(){this.isConnected&&(this.config=bD(this),this.syncActiveMode(),this.applyTheme(),this.render())}async startConversation(g){if(!(this.state==="connecting"||this.state==="live")){if(this.open=!0,this.needsRecaptcha()){this.render();return}await this.beginCall(g)}}endConversation(){var g;(g=this.agent)==null||g.endCall()}startCall(){return this.startConversation()}endCall(){this.endConversation()}resolvedMode(){var g;return this.config.mode==="both"?(g=this.activeMode)!=null?g:"voice":this.config.mode}syncActiveMode(){(this.config.mode==="voice"||this.config.mode==="text")&&(this.activeMode=this.config.mode)}teardownAgent(){if(this.agent){try{this.agent.removeAllListeners()}catch(g){}try{this.agent.endCall()}catch(g){}this.agent=null}}async switchModality(){if(this.config.mode!=="both"||this.state!=="live")return;let g=this.resolvedMode()==="voice"?"text":"voice";this.switching=!0,this.clearDurationTimer(),this.speaking=!1,this.teardownAgent(),await this.beginCall(g)}async beginCall(g){g&&(this.activeMode=g);let I=this.resolvedMode();this.switching||(this.display=null),this.setState("connecting");try{let B=new gE({agentId:this.config.agentId,workspaceId:this.config.workspaceId,mode:I,variables:this.config.variables});if(this.agent=B,this.wireAgentEvents(B),this.config.signedUrlEndpoint||this.getRoomDetails){let Q;if(this.getRoomDetails)Q=await this.getRoomDetails();else{let i=await fetch(this.config.signedUrlEndpoint,{method:"GET",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`signed-url-endpoint returned ${i.status}`);Q=await i.json()}await B.joinRoom(Q)}else if(this.roomDetails)await B.joinRoom(this.roomDetails);else if(this.config.workspaceId)await B.startPublicCall();else throw new Error("No credential path configured: set signed-url-endpoint, roomDetails/getRoomDetails, or workspace-id")}catch(B){this.handleError(B)}}wireAgentEvents(g){g.on("connected",I=>{var Q;this.display=(Q=I==null?void 0:I.display)!=null?Q:null;let B=this.switching;this.setState("live"),this.startDurationTimer(!B),this.switching=!1}),g.on("disconnected",()=>{this.endLocalCall()}),g.on("error",I=>{this.handleError(I)}),g.on("assistantStartedSpeaking",()=>{this.speaking=!0,this.render()}),g.on("assistantStoppedSpeaking",()=>{this.speaking=!1,this.render()}),g.on("transcriptUpdate",I=>{this.transcripts=I,this.dispatch("transcript",I),this.render()})}endLocalCall(){this.state==="ended"||this.state==="idle"||(this.clearDurationTimer(),this.dispatch("call-end",this.elapsedSeconds),this.dispatch("conversationEnded",this.elapsedSeconds),this.setState("ended"),this.agent=null,this.config.mode==="both"&&(this.activeMode=null))}handleError(g){let I=g instanceof Error?g:new Error(String(g));this.clearDurationTimer(),this.switching=!1,this.dispatch("error",I),this.agent=null,this.config.mode==="both"&&(this.activeMode=null),this.setState("idle")}startDurationTimer(g=!0){this.clearDurationTimer(),g&&(this.elapsedSeconds=0,this.remainingSeconds=typeof this.config.maxCallDuration=="number"?this.config.maxCallDuration:null),this.durationTimer=setInterval(()=>{var I;if(this.elapsedSeconds+=1,this.remainingSeconds!==null&&(this.remainingSeconds-=1,this.remainingSeconds<=0)){(I=this.agent)==null||I.endCall();return}this.renderStatus()},1e3)}clearDurationTimer(){this.durationTimer&&(clearInterval(this.durationTimer),this.durationTimer=null)}setState(g){let I=this.state;this.state=g,g==="live"&&I!=="live"&&!this.switching&&(this.dispatch("call-start",void 0),this.dispatch("conversationStarted",void 0)),this.render()}dispatch(g,I){this.dispatchEvent(new CustomEvent(g,{detail:I,bubbles:!0,composed:!0}))}applyTheme(){let g=this.config.orbColor1||xN,I=this.config.orbColor2||ZN;this.style.setProperty("--trillet-orb-1",g),this.style.setProperty("--trillet-orb-2",I);let B=this.config.brand.primaryColor||this.config.orbColor1;B?this.style.setProperty("--trillet-accent",B):this.style.removeProperty("--trillet-accent")}formatTime(g){let I=Math.floor(g/60),B=g%60;return`${I}:${B.toString().padStart(2,"0")}`}statusText(){switch(this.state){case"connecting":return"Connecting\u2026";case"live":return this.speaking?this.config.speakingText:this.config.listeningText;case"ended":return"Call ended";default:return this.config.actionText||""}}renderStatus(){let g=this.root.querySelector(".tw-status");g&&(g.textContent=this.statusText());let I=this.root.querySelector(".tw-timer");I&&(I.textContent=this.timerText())}timerText(){return this.state!=="live"?"":this.remainingSeconds!==null?this.formatTime(Math.max(0,this.remainingSeconds)):this.formatTime(this.elapsedSeconds)}render(){if(this.container.replaceChildren(),!this.open){this.container.appendChild(this.renderLauncher());return}this.container.appendChild(this.renderPanel())}renderLauncher(){let g=document.createElement("button");g.className="tw-launcher",g.type="button";let I=document.createElement("span");I.className="tw-launcher-orb",this.config.avatar&&(I.classList.add("tw-launcher-orb-avatar"),I.style.backgroundImage=`url("${this.config.avatar}")`),g.appendChild(I);let B=document.createElement("span");return B.textContent=this.config.actionText||this.config.startCallText,g.appendChild(B),g.addEventListener("click",()=>{this.open=!0,this.render()}),g}renderPanel(){let g=document.createElement("div");return g.className=`tw-panel${this.config.variant==="expanded"?" tw-expanded":""}`,g.appendChild(this.renderHeader()),g.appendChild(this.renderBody()),g}headerTitle(){var Q,i,E,o,D,e;let g=(i=this.config.agentName)!=null?i:(Q=this.display)==null?void 0:Q.agent_name,I=(o=this.config.callFlowName)!=null?o:(E=this.display)==null?void 0:E.call_flow_name,B=(e=this.config.workspaceName)!=null?e:(D=this.display)==null?void 0:D.workspace_name;return g||I||B||""}renderHeader(){let g=document.createElement("div");if(g.className="tw-header",this.config.brand.logo||this.config.brand.name){let Q=document.createElement("div");if(Q.className="tw-brand",this.config.brand.logo){let i=document.createElement("img");i.className="tw-brand-logo",i.src=this.config.brand.logo,i.alt=this.config.brand.name||"",Q.appendChild(i)}if(this.config.brand.name){let i=document.createElement("span");i.className="tw-brand-name",i.textContent=this.config.brand.name,i.title=this.config.brand.name,Q.appendChild(i)}g.appendChild(Q)}let I=document.createElement("div");I.className="tw-header-title";let B=this.headerTitle();if(I.textContent=B,B&&(I.title=B),g.appendChild(I),this.config.variant==="compact"){let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title=this.config.expandText,Q.textContent="\u2922",Q.addEventListener("click",()=>{this.config={...this.config,variant:"expanded"},this.render()}),g.appendChild(Q)}else{let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title=this.config.collapseText,Q.textContent="\u2921",Q.addEventListener("click",()=>{this.config={...this.config,variant:"compact"},this.render()}),g.appendChild(Q)}if(this.config.dismissible){let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title="Close",Q.textContent="\u2715",Q.addEventListener("click",()=>{(this.state==="live"||this.state==="connecting")&&this.endConversation(),this.open=!1,this.render()}),g.appendChild(Q)}return g}isBothPreCall(){return this.config.mode==="both"&&this.activeMode===null&&(this.state==="idle"||this.state==="ended")}needsRecaptcha(){return!!this.config.recaptchaSiteKey&&!this.recaptchaSolved}renderRecaptchaGate(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-status",I.textContent=this.config.recaptchaText,g.appendChild(I);let B=document.createElement("slot");B.setAttribute("name","recaptcha"),B.className="tw-recaptcha-slot",g.appendChild(B);let Q=document.createElement("div");Q.className="tw-controls";let i=document.createElement("button");i.className="tw-primary-btn",i.type="button",i.textContent=this.config.startCallText,i.disabled=!0,Q.appendChild(i),g.appendChild(Q),this.ensureRecaptchaSlot()}ensureRecaptchaSlot(){let g=this.config.recaptchaSiteKey;if(!g||this.recaptchaSolved||this.recaptchaSlot)return;let I=document.createElement("div");I.setAttribute("slot","recaptcha"),I.className="tw-recaptcha-mount",this.appendChild(I),this.recaptchaSlot=I,this.recaptchaWidgetId=-1,XN({container:I,siteKey:g,onSolved:()=>{this.recaptchaSolved=!0,this.removeRecaptchaSlot(),this.render()},onExpired:()=>{this.recaptchaSolved=!1}}).then(B=>{this.recaptchaWidgetId=B}).catch(B=>{console.warn("[trillet-widget] reCAPTCHA failed to load; starting without it",B),this.recaptchaSolved=!0,this.removeRecaptchaSlot(),this.render()})}removeRecaptchaSlot(){var g;(g=this.recaptchaSlot)==null||g.remove(),this.recaptchaSlot=null,this.recaptchaSolved||(this.recaptchaWidgetId=null)}renderBody(){let g=document.createElement("div");return g.className="tw-body",this.needsRecaptcha()&&(this.state==="idle"||this.state==="ended")?(this.renderRecaptchaGate(g),g):this.isBothPreCall()?(this.renderBothEntry(g),g):(this.resolvedMode()==="text"?this.renderTextBody(g):this.renderVoiceBody(g),g)}buildOrb(){let g=document.createElement("div");g.className="tw-orb-wrap",this.state==="connecting"&&g.classList.add("tw-connecting"),this.state==="live"&&g.classList.add("tw-listening"),this.state==="live"&&this.speaking&&g.classList.add("tw-speaking");let I=document.createElement("div");return I.className="tw-orb",this.config.avatar&&(I.classList.add("tw-orb-avatar"),I.style.backgroundImage=`url("${this.config.avatar}")`),g.appendChild(I),g}renderBothEntry(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-controls tw-controls-stack";let B=document.createElement("button");B.className="tw-primary-btn",B.type="button",B.textContent=this.config.startCallText,B.addEventListener("click",()=>{this.beginCall("voice")}),I.appendChild(B);let Q=document.createElement("button");Q.className="tw-primary-btn tw-secondary",Q.type="button",Q.textContent=this.config.messageText,Q.addEventListener("click",()=>{this.beginCall("text")}),I.appendChild(Q),g.appendChild(I)}renderVoiceBody(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-status",I.textContent=this.statusText(),g.appendChild(I);let B=document.createElement("div");B.className="tw-timer",B.textContent=this.timerText(),g.appendChild(B),g.appendChild(this.renderControls());let Q=this.renderSwitchButton();Q&&g.appendChild(Q)}renderTextBody(g){if(this.state==="idle"||this.state==="ended"){if(this.state==="ended"){let Q=document.createElement("div");Q.className="tw-status",Q.textContent="Chat ended",g.appendChild(Q)}g.appendChild(this.renderControls());return}if(this.state==="connecting"){let Q=document.createElement("div");Q.className="tw-status",Q.textContent="Connecting\u2026",g.appendChild(Q)}g.appendChild(this.renderTranscript()),this.state==="live"&&g.appendChild(this.renderTextInput());let I=document.createElement("button");I.className="tw-primary-btn tw-danger",I.type="button",I.textContent=this.config.endCallText,I.disabled=this.state==="connecting",I.addEventListener("click",()=>this.endConversation()),g.appendChild(I);let B=this.renderSwitchButton();B&&g.appendChild(B)}renderSwitchButton(){if(this.config.mode!=="both"||this.state!=="live")return null;let g=this.resolvedMode()==="voice",I=document.createElement("button");return I.className="tw-switch-btn",I.type="button",I.textContent=g?this.config.switchToTextText:this.config.switchToVoiceText,I.addEventListener("click",()=>{this.switchModality()}),I}renderControls(){let g=document.createElement("div");if(g.className="tw-controls",this.state==="idle"||this.state==="ended"){let B=document.createElement("button");return B.className="tw-primary-btn",B.type="button",B.textContent=this.config.startCallText,B.addEventListener("click",()=>{this.beginCall()}),g.appendChild(B),g}if(this.resolvedMode()==="voice"&&this.state==="live"){let B=document.createElement("button");B.className=`tw-mic-btn${this.micEnabled?"":" tw-muted"}`,B.type="button",B.title=this.micEnabled?"Mute microphone":"Unmute microphone",B.textContent=this.micEnabled?"\u{1F399}":"\u{1F507}",B.addEventListener("click",()=>{var Q;this.micEnabled=!this.micEnabled,(Q=this.agent)==null||Q.toggleMicrophone(this.micEnabled),this.render()}),g.appendChild(B)}let I=document.createElement("button");return I.className="tw-primary-btn tw-danger",I.type="button",I.textContent=this.config.endCallText,I.disabled=this.state==="connecting",I.addEventListener("click",()=>this.endConversation()),g.appendChild(I),g}renderTextInput(){let g=document.createElement("div");g.className="tw-text-input-row";let I=document.createElement("input");I.className="tw-text-input",I.type="text",I.placeholder="Type a message\u2026";let B=()=>{var E;let i=I.value.trim();i&&((E=this.agent)==null||E.sendTextMessage(i),I.value="")};I.addEventListener("keydown",i=>{i.key==="Enter"&&B()});let Q=document.createElement("button");return Q.className="tw-send-btn",Q.type="button",Q.textContent="Send",Q.addEventListener("click",B),g.appendChild(I),g.appendChild(Q),g}renderTranscript(){let g=document.createElement("div");if(g.className="tw-transcript tw-chat",this.transcripts.length===0){let I=document.createElement("div");I.className="tw-chat-empty",I.textContent=this.state==="connecting"?"":"\u2026",g.appendChild(I)}else for(let I of this.transcripts){let B=document.createElement("div");B.className=`tw-msg ${I.role==="user"?"tw-user":"tw-assistant"}`,B.textContent=I.text,g.appendChild(B)}return requestAnimationFrame(()=>{g.scrollTop=g.scrollHeight}),g}};var IE="trillet-widget";function CE(C=IE){typeof customElements!="undefined"&&(customElements.get(C)||customElements.define(C,qI))}CE();var lK={register:CE,TrilletWidgetElement:qI,tag:IE};typeof window!="undefined"&&(window.TrilletWidget=lK);return Cc(pK);})();
|
|
3882
|
+
`;var vN="https://www.google.com/recaptcha/api.js?render=explicit";function dK(){return window.grecaptcha}var hB=null;function JK(){return hB||(hB=new Promise((C,A)=>{if(document.querySelector(`script[src="${vN}"]`)){C();return}let I=document.createElement("script");I.src=vN,I.async=!0,I.defer=!0,I.onload=()=>C(),I.onerror=()=>{hB=null,A(new Error("Failed to load Google reCAPTCHA"))},document.head.appendChild(I)}),hB)}async function LK(){await JK();let C=dK();if(!C)throw new Error("reCAPTCHA loaded but grecaptcha is undefined");return await new Promise(A=>C.ready(A)),C}async function XN(C){return(await LK()).render(C.container,{sitekey:C.siteKey,callback:()=>C.onSolved(),"expired-callback":()=>C.onExpired(),"error-callback":()=>C.onExpired()})}var qI=class extends HTMLElement{constructor(){super();this.agent=null;this.state="idle";this.open=!1;this.micEnabled=!0;this.speaking=!1;this.transcripts=[];this.display=null;this.activeMode=null;this.switching=!1;this.durationTimer=null;this.remainingSeconds=null;this.elapsedSeconds=0;this.recaptchaSolved=!1;this.recaptchaSlot=null;this.recaptchaWidgetId=null;this.root=this.attachShadow({mode:"open"}),this.styleEl=document.createElement("style"),this.styleEl.textContent=VN,this.container=document.createElement("div"),this.container.className="tw-root",this.root.appendChild(this.styleEl),this.root.appendChild(this.container)}static get observedAttributes(){return WN}connectedCallback(){this.config=bD(this),this.syncActiveMode(),this.applyTheme(),this.render()}disconnectedCallback(){this.clearDurationTimer(),this.teardownAgent(),this.removeRecaptchaSlot()}attributeChangedCallback(){this.isConnected&&(this.config=bD(this),this.syncActiveMode(),this.applyTheme(),this.render())}async startConversation(g){if(!(this.state==="connecting"||this.state==="live")){if(this.isInlineVoice()||(this.open=!0),this.needsRecaptcha()){this.render();return}await this.beginCall(g)}}endConversation(){var g;(g=this.agent)==null||g.endCall()}startCall(){return this.startConversation()}endCall(){this.endConversation()}isInlineVoice(){return this.config.mode==="voice"}resolvedMode(){var g;return this.config.mode==="both"?(g=this.activeMode)!=null?g:"voice":this.config.mode}syncActiveMode(){(this.config.mode==="voice"||this.config.mode==="text")&&(this.activeMode=this.config.mode)}teardownAgent(){if(this.agent){try{this.agent.removeAllListeners()}catch(g){}try{this.agent.endCall()}catch(g){}this.agent=null}}async switchModality(){if(this.config.mode!=="both"||this.state!=="live")return;let g=this.resolvedMode()==="voice"?"text":"voice";this.switching=!0,this.clearDurationTimer(),this.speaking=!1,this.teardownAgent(),await this.beginCall(g)}async beginCall(g){g&&(this.activeMode=g);let I=this.resolvedMode();this.switching||(this.display=null),this.setState("connecting");try{let B=new gE({agentId:this.config.agentId,workspaceId:this.config.workspaceId,mode:I,variables:this.config.variables});if(this.agent=B,this.wireAgentEvents(B),this.config.signedUrlEndpoint||this.getRoomDetails){let Q;if(this.getRoomDetails)Q=await this.getRoomDetails();else{let i=await fetch(this.config.signedUrlEndpoint,{method:"GET",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`signed-url-endpoint returned ${i.status}`);Q=await i.json()}await B.joinRoom(Q)}else if(this.roomDetails)await B.joinRoom(this.roomDetails);else if(this.config.workspaceId)await B.startPublicCall();else throw new Error("No credential path configured: set signed-url-endpoint, roomDetails/getRoomDetails, or workspace-id")}catch(B){this.handleError(B)}}wireAgentEvents(g){g.on("connected",I=>{var Q;this.display=(Q=I==null?void 0:I.display)!=null?Q:null;let B=this.switching;this.setState("live"),this.startDurationTimer(!B),this.switching=!1}),g.on("disconnected",()=>{this.endLocalCall()}),g.on("error",I=>{this.handleError(I)}),g.on("assistantStartedSpeaking",()=>{this.speaking=!0,this.render()}),g.on("assistantStoppedSpeaking",()=>{this.speaking=!1,this.render()}),g.on("transcriptUpdate",I=>{this.transcripts=I,this.dispatch("transcript",I),this.render()})}endLocalCall(){this.state==="ended"||this.state==="idle"||(this.clearDurationTimer(),this.speaking=!1,this.dispatch("call-end",this.elapsedSeconds),this.dispatch("conversationEnded",this.elapsedSeconds),this.setState("ended"),this.agent=null,this.config.mode==="both"&&(this.activeMode=null))}handleError(g){let I=g instanceof Error?g:new Error(String(g));this.clearDurationTimer(),this.speaking=!1,this.switching=!1,this.dispatch("error",I),this.agent=null,this.config.mode==="both"&&(this.activeMode=null),this.setState("idle")}startDurationTimer(g=!0){this.clearDurationTimer(),g&&(this.elapsedSeconds=0,this.remainingSeconds=typeof this.config.maxCallDuration=="number"?this.config.maxCallDuration:null),this.durationTimer=setInterval(()=>{var I;if(this.elapsedSeconds+=1,this.remainingSeconds!==null&&(this.remainingSeconds-=1,this.remainingSeconds<=0)){(I=this.agent)==null||I.endCall();return}this.renderStatus()},1e3)}clearDurationTimer(){this.durationTimer&&(clearInterval(this.durationTimer),this.durationTimer=null)}setState(g){let I=this.state;this.state=g,g==="live"&&I!=="live"&&!this.switching&&(this.dispatch("call-start",void 0),this.dispatch("conversationStarted",void 0)),this.render()}dispatch(g,I){this.dispatchEvent(new CustomEvent(g,{detail:I,bubbles:!0,composed:!0}))}applyTheme(){let g=this.config.orbColor1||xN,I=this.config.orbColor2||ZN;this.style.setProperty("--trillet-orb-1",g),this.style.setProperty("--trillet-orb-2",I);let B=this.config.brand.primaryColor||this.config.orbColor1;B?this.style.setProperty("--trillet-accent",B):this.style.removeProperty("--trillet-accent")}formatTime(g){let I=Math.floor(g/60),B=g%60;return`${I}:${B.toString().padStart(2,"0")}`}statusText(){switch(this.state){case"connecting":return"Connecting\u2026";case"live":return this.speaking?this.config.speakingText:this.config.listeningText;case"ended":return"Call ended";default:return this.config.actionText||""}}renderStatus(){let g=this.root.querySelector(".tw-status");g&&(g.textContent=this.statusText());let I=this.root.querySelector(".tw-timer");I&&(I.textContent=this.timerText())}timerText(){return this.state!=="live"?"":this.remainingSeconds!==null?this.formatTime(Math.max(0,this.remainingSeconds)):this.formatTime(this.elapsedSeconds)}render(){if(this.container.replaceChildren(),this.isInlineVoice()){this.container.appendChild(this.renderInlineVoice());return}if(!this.open){this.container.appendChild(this.renderLauncher());return}this.container.appendChild(this.renderPanel())}renderInlineVoice(){let g=document.createElement("div");g.className="tw-inline";let I=this.needsRecaptcha()&&(this.state==="idle"||this.state==="ended");if(I){let o=document.createElement("div");o.className="tw-inline-hint",o.textContent=this.config.recaptchaText,g.appendChild(o);let D=document.createElement("slot");D.setAttribute("name","recaptcha"),D.className="tw-recaptcha-slot",g.appendChild(D),this.ensureRecaptchaSlot()}let B=document.createElement("button");B.className="tw-launcher",B.type="button";let Q=document.createElement("span");Q.className="tw-launcher-orb",this.config.avatar&&(Q.classList.add("tw-launcher-orb-avatar"),Q.style.backgroundImage=`url("${this.config.avatar}")`),B.appendChild(Q);let i=document.createElement("span");i.className="tw-launcher-copy";let E=document.createElement("span");if(E.className="tw-launcher-label",this.state==="connecting")B.classList.add("tw-connecting"),B.disabled=!0,E.textContent="Connecting\u2026",B.setAttribute("aria-label","Connecting");else if(this.state==="live"){B.classList.add("tw-hangup"),B.classList.add(this.speaking?"tw-speaking":"tw-listening"),E.textContent=this.config.endCallText;let o=this.speaking?this.config.speakingText:this.config.listeningText,D=document.createElement("span");D.className="tw-launcher-state",D.textContent=o,i.appendChild(E),i.appendChild(D),B.setAttribute("aria-label",`${this.config.endCallText}. ${o}`),B.addEventListener("click",()=>{this.endConversation()})}else E.textContent=this.config.actionText||this.config.startCallText,B.disabled=I,B.setAttribute("aria-label",E.textContent),B.addEventListener("click",()=>{this.state==="connecting"||this.state==="live"||this.needsRecaptcha()||this.beginCall()});return this.state!=="live"&&i.appendChild(E),B.appendChild(i),g.appendChild(B),g}renderLauncher(){let g=document.createElement("button");g.className="tw-launcher",g.type="button";let I=document.createElement("span");I.className="tw-launcher-orb",this.config.avatar&&(I.classList.add("tw-launcher-orb-avatar"),I.style.backgroundImage=`url("${this.config.avatar}")`),g.appendChild(I);let B=document.createElement("span");return B.textContent=this.config.actionText||this.config.startCallText,g.appendChild(B),g.addEventListener("click",()=>{this.open=!0,this.render()}),g}renderPanel(){let g=document.createElement("div");return g.className=`tw-panel${this.config.variant==="expanded"?" tw-expanded":""}`,g.appendChild(this.renderHeader()),g.appendChild(this.renderBody()),g}headerTitle(){var Q,i,E,o,D,e;let g=(i=this.config.agentName)!=null?i:(Q=this.display)==null?void 0:Q.agent_name,I=(o=this.config.callFlowName)!=null?o:(E=this.display)==null?void 0:E.call_flow_name,B=(e=this.config.workspaceName)!=null?e:(D=this.display)==null?void 0:D.workspace_name;return g||I||B||""}renderHeader(){let g=document.createElement("div");if(g.className="tw-header",this.config.brand.logo||this.config.brand.name){let Q=document.createElement("div");if(Q.className="tw-brand",this.config.brand.logo){let i=document.createElement("img");i.className="tw-brand-logo",i.src=this.config.brand.logo,i.alt=this.config.brand.name||"",Q.appendChild(i)}if(this.config.brand.name){let i=document.createElement("span");i.className="tw-brand-name",i.textContent=this.config.brand.name,i.title=this.config.brand.name,Q.appendChild(i)}g.appendChild(Q)}let I=document.createElement("div");I.className="tw-header-title";let B=this.headerTitle();if(I.textContent=B,B&&(I.title=B),g.appendChild(I),this.config.variant==="compact"){let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title=this.config.expandText,Q.textContent="\u2922",Q.addEventListener("click",()=>{this.config={...this.config,variant:"expanded"},this.render()}),g.appendChild(Q)}else{let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title=this.config.collapseText,Q.textContent="\u2921",Q.addEventListener("click",()=>{this.config={...this.config,variant:"compact"},this.render()}),g.appendChild(Q)}if(this.config.dismissible){let Q=document.createElement("button");Q.className="tw-icon-btn",Q.type="button",Q.title="Close",Q.textContent="\u2715",Q.addEventListener("click",()=>{(this.state==="live"||this.state==="connecting")&&this.endConversation(),this.open=!1,this.render()}),g.appendChild(Q)}return g}isBothPreCall(){return this.config.mode==="both"&&this.activeMode===null&&(this.state==="idle"||this.state==="ended")}needsRecaptcha(){return!!this.config.recaptchaSiteKey&&!this.recaptchaSolved}renderRecaptchaGate(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-status",I.textContent=this.config.recaptchaText,g.appendChild(I);let B=document.createElement("slot");B.setAttribute("name","recaptcha"),B.className="tw-recaptcha-slot",g.appendChild(B);let Q=document.createElement("div");Q.className="tw-controls";let i=document.createElement("button");i.className="tw-primary-btn",i.type="button",i.textContent=this.config.startCallText,i.disabled=!0,Q.appendChild(i),g.appendChild(Q),this.ensureRecaptchaSlot()}ensureRecaptchaSlot(){let g=this.config.recaptchaSiteKey;if(!g||this.recaptchaSolved||this.recaptchaSlot)return;let I=document.createElement("div");I.setAttribute("slot","recaptcha"),I.className="tw-recaptcha-mount",this.appendChild(I),this.recaptchaSlot=I,this.recaptchaWidgetId=-1,XN({container:I,siteKey:g,onSolved:()=>{this.recaptchaSolved=!0,this.removeRecaptchaSlot(),this.render()},onExpired:()=>{this.recaptchaSolved=!1}}).then(B=>{this.recaptchaWidgetId=B}).catch(B=>{console.warn("[trillet-widget] reCAPTCHA failed to load; starting without it",B),this.recaptchaSolved=!0,this.removeRecaptchaSlot(),this.render()})}removeRecaptchaSlot(){var g;(g=this.recaptchaSlot)==null||g.remove(),this.recaptchaSlot=null,this.recaptchaSolved||(this.recaptchaWidgetId=null)}renderBody(){let g=document.createElement("div");return g.className="tw-body",this.needsRecaptcha()&&(this.state==="idle"||this.state==="ended")?(this.renderRecaptchaGate(g),g):this.isBothPreCall()?(this.renderBothEntry(g),g):(this.resolvedMode()==="text"?this.renderTextBody(g):this.renderVoiceBody(g),g)}buildOrb(){let g=document.createElement("div");g.className="tw-orb-wrap",this.state==="connecting"&&g.classList.add("tw-connecting"),this.state==="live"&&g.classList.add("tw-listening"),this.state==="live"&&this.speaking&&g.classList.add("tw-speaking");let I=document.createElement("div");return I.className="tw-orb",this.config.avatar&&(I.classList.add("tw-orb-avatar"),I.style.backgroundImage=`url("${this.config.avatar}")`),g.appendChild(I),g}renderBothEntry(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-controls tw-controls-stack";let B=document.createElement("button");B.className="tw-primary-btn",B.type="button",B.textContent=this.config.startCallText,B.addEventListener("click",()=>{this.beginCall("voice")}),I.appendChild(B);let Q=document.createElement("button");Q.className="tw-primary-btn tw-secondary",Q.type="button",Q.textContent=this.config.messageText,Q.addEventListener("click",()=>{this.beginCall("text")}),I.appendChild(Q),g.appendChild(I)}renderVoiceBody(g){g.appendChild(this.buildOrb());let I=document.createElement("div");I.className="tw-status",I.textContent=this.statusText(),g.appendChild(I);let B=document.createElement("div");B.className="tw-timer",B.textContent=this.timerText(),g.appendChild(B),g.appendChild(this.renderControls());let Q=this.renderSwitchButton();Q&&g.appendChild(Q)}renderTextBody(g){if(this.state==="idle"||this.state==="ended"){if(this.state==="ended"){let Q=document.createElement("div");Q.className="tw-status",Q.textContent="Chat ended",g.appendChild(Q)}g.appendChild(this.renderControls());return}if(this.state==="connecting"){let Q=document.createElement("div");Q.className="tw-status",Q.textContent="Connecting\u2026",g.appendChild(Q)}g.appendChild(this.renderTranscript()),this.state==="live"&&g.appendChild(this.renderTextInput());let I=document.createElement("button");I.className="tw-primary-btn tw-danger",I.type="button",I.textContent=this.config.endCallText,I.disabled=this.state==="connecting",I.addEventListener("click",()=>this.endConversation()),g.appendChild(I);let B=this.renderSwitchButton();B&&g.appendChild(B)}renderSwitchButton(){if(this.config.mode!=="both"||this.state!=="live")return null;let g=this.resolvedMode()==="voice",I=document.createElement("button");return I.className="tw-switch-btn",I.type="button",I.textContent=g?this.config.switchToTextText:this.config.switchToVoiceText,I.addEventListener("click",()=>{this.switchModality()}),I}renderControls(){let g=document.createElement("div");if(g.className="tw-controls",this.state==="idle"||this.state==="ended"){let B=document.createElement("button");return B.className="tw-primary-btn",B.type="button",B.textContent=this.config.startCallText,B.addEventListener("click",()=>{this.beginCall()}),g.appendChild(B),g}if(this.resolvedMode()==="voice"&&this.state==="live"){let B=document.createElement("button");B.className=`tw-mic-btn${this.micEnabled?"":" tw-muted"}`,B.type="button",B.title=this.micEnabled?"Mute microphone":"Unmute microphone",B.textContent=this.micEnabled?"\u{1F399}":"\u{1F507}",B.addEventListener("click",()=>{var Q;this.micEnabled=!this.micEnabled,(Q=this.agent)==null||Q.toggleMicrophone(this.micEnabled),this.render()}),g.appendChild(B)}let I=document.createElement("button");return I.className="tw-primary-btn tw-danger",I.type="button",I.textContent=this.config.endCallText,I.disabled=this.state==="connecting",I.addEventListener("click",()=>this.endConversation()),g.appendChild(I),g}renderTextInput(){let g=document.createElement("div");g.className="tw-text-input-row";let I=document.createElement("input");I.className="tw-text-input",I.type="text",I.placeholder="Type a message\u2026";let B=()=>{var E;let i=I.value.trim();i&&((E=this.agent)==null||E.sendTextMessage(i),I.value="")};I.addEventListener("keydown",i=>{i.key==="Enter"&&B()});let Q=document.createElement("button");return Q.className="tw-send-btn",Q.type="button",Q.textContent="Send",Q.addEventListener("click",B),g.appendChild(I),g.appendChild(Q),g}renderTranscript(){let g=document.createElement("div");if(g.className="tw-transcript tw-chat",this.transcripts.length===0){let I=document.createElement("div");I.className="tw-chat-empty",I.textContent=this.state==="connecting"?"":"\u2026",g.appendChild(I)}else for(let I of this.transcripts){let B=document.createElement("div");B.className=`tw-msg ${I.role==="user"?"tw-user":"tw-assistant"}`,B.textContent=I.text,g.appendChild(B)}return requestAnimationFrame(()=>{g.scrollTop=g.scrollHeight}),g}};var IE="trillet-widget";function CE(C=IE){typeof customElements!="undefined"&&(customElements.get(C)||customElements.define(C,qI))}CE();var lK={register:CE,TrilletWidgetElement:qI,tag:IE};typeof window!="undefined"&&(window.TrilletWidget=lK);return Cc(pK);})();
|
|
3831
3883
|
//# sourceMappingURL=widget.umd.js.map
|