@corti/embedded-web 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -7
- package/dist/bundle.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/api.d.ts +15 -15
- package/dist/types/api.js.map +1 -1
- package/dist/utils/PostMessageHandler.js +0 -1
- package/dist/utils/PostMessageHandler.js.map +1 -1
- package/dist/web-bundle.js +1 -1
- package/package.json +11 -1
package/README.md
CHANGED
|
@@ -15,12 +15,6 @@ A web component and React component library that provides an embedded interface
|
|
|
15
15
|
npm install @corti/embedded-web
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
The package provides a web component by default. For React usage, also install React as a peer dependency:
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install react @types/react
|
|
22
|
-
```
|
|
23
|
-
|
|
24
18
|
## Usage
|
|
25
19
|
|
|
26
20
|
### Web Component
|
|
@@ -55,6 +49,21 @@ await myComponent.navigate('/interactions/123');
|
|
|
55
49
|
await myComponent.show()
|
|
56
50
|
```
|
|
57
51
|
|
|
52
|
+
#### Generic Event Listener (Web Component)
|
|
53
|
+
|
|
54
|
+
Use `embedded-event` as the canonical event stream for web component integrations.
|
|
55
|
+
|
|
56
|
+
- Event detail shape is `{ name: string; payload: unknown }`.
|
|
57
|
+
- Full event catalog and payload details are documented at:
|
|
58
|
+
- https://docs.corti.ai/assistant/events
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
myComponent.addEventListener('embedded-event', event => {
|
|
62
|
+
const { detail } = event;
|
|
63
|
+
console.log(detail.name, detail.payload);
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
58
67
|
### React Component
|
|
59
68
|
|
|
60
69
|
```tsx
|
|
@@ -143,11 +152,22 @@ component.hide();
|
|
|
143
152
|
|
|
144
153
|
#### auth
|
|
145
154
|
|
|
155
|
+
Authentication is required before using the embedded app. The payload passed to
|
|
156
|
+
`component.auth(...)` should come directly from your identity provider token
|
|
157
|
+
response (for example, Keycloak/OIDC token endpoint response), plus `mode`.
|
|
158
|
+
|
|
159
|
+
- API details and payload shape: https://docs.corti.ai/assistant/api-reference#auth
|
|
160
|
+
- End-to-end authentication guidance: https://docs.corti.ai/assistant/authentication
|
|
161
|
+
|
|
162
|
+
Use user-based authentication (OAuth2/OIDC). Client-credentials-only flows are
|
|
163
|
+
not supported for the embedded app.
|
|
164
|
+
|
|
146
165
|
```javascript
|
|
147
166
|
const authResponse = await component.auth({
|
|
148
|
-
//
|
|
167
|
+
// Use the full token response from your IdP + mode
|
|
149
168
|
access_token: 'YOUR_JWT',
|
|
150
169
|
token_type: 'Bearer',
|
|
170
|
+
// include other token fields from your provider response (expires_in, refresh_token, etc.)
|
|
151
171
|
mode: 'stateful',
|
|
152
172
|
...
|
|
153
173
|
});
|
package/dist/bundle.js
CHANGED
|
@@ -35,7 +35,7 @@ var k=globalThis,B=k.ShadowRoot&&(k.ShadyCSS===void 0||k.ShadyCSS.nativeShadow)&
|
|
|
35
35
|
margin: 0 !important;
|
|
36
36
|
padding: 0 !important;
|
|
37
37
|
}
|
|
38
|
-
`;function J(r){let e;try{e=new URL(r)}catch{throw new Error("Invalid baseURL: not a parseable URL")}if(e.protocol!=="https:")throw new Error("Invalid baseURL: must use https");let t=e.host.toLowerCase();if(!/^assistant\.[a-z0-9-]+\.corti\.app$/i.test(t))throw new Error("Invalid baseURL: host must match assistant.xxx.corti.app");if(e.pathname&&e.pathname!=="/"&&e.pathname!=="")throw new Error("Invalid baseURL: must not include a path");if(e.username||e.password)throw new Error("Invalid baseURL: must not include credentials");return e.origin.replace(/\/+$/,"")}function le(r){return`${r}/embedded`}function He(r,e){if(!r||r.startsWith("about:"))return!1;try{let t=new URL(r,window.location.href),s=new URL(e);return t.origin!==s.origin?!1:t.pathname.replace(/\/+$/,"")==="/embedded"}catch{return!1}}function et(r){try{return JSON.parse(r)}catch{return null}}function K(r){let e=r.match(/\b([1-5]\d{2})\b/);return e?e[1]:void 0}function he(r){return typeof r=="object"&&r!==null&&("expected"in r||"code"in r||"path"in r||"message"in r)}function pe(r){let{path:e,expected:t,message:s,code:o}=r;if(s)return s;let i=e&&e.length>0?e.join("."):"field";return t?`Invalid ${i}: expected ${t}`:o?`Validation error in ${i} (${o})`:`Validation error in ${i}`}function Pe(r){return r.length===0?"Unknown validation error":r.length===1?pe(r[0]):`Multiple validation errors: ${r.map(pe).join("; ")}`}function y(r,e="An error occurred"){if(!r)return{message:e,details:r};if(r instanceof Error){let t=K(r.message);return{message:r.message||e,code:t,details:r}}if(typeof r=="string"){let t=K(r);return{message:r,code:t,details:r}}if(Array.isArray(r)){if(r.every(he)){let t=r;return{message:Pe(t),code:t[0]?.code,details:r}}return{message:`Multiple errors: ${r.length} item(s)`,details:r}}if(typeof r=="object"){let t=r;if(t.message&&typeof t.message=="string"){let s=t.message,{code:o}=t;if(o||(o=K(s)),t.details?.message&&typeof t.details.message=="string"){let i=et(t.details.message);Array.isArray(i)&&i.every(he)&&(s=Pe(i))}return{message:s,code:o,details:t.details}}if(he(t))return{message:pe(t),code:t.code,details:r};if(t.error||t.message||t.detail){let s=t.error||t.message||t.detail,o=typeof s=="string"?s:e,i=t.code||t.status;return!i&&typeof s=="string"&&(i=K(s)),{message:o,code:i,details:r}}}return{message:e,details:r}}var Z=class r{constructor(e,t={}){this.pendingRequests=new Map,this.messageListener=null,this.isReady=!1,this.iframe=e,this.callbacks=t,this.setupMessageListener()}setupMessageListener(){this.messageListener=e=>{if(e.source!==this.iframe.contentWindow)return;let t=this.getTrustedOrigin();if(!t||e.origin!==t)return;let{data:s}=e;if(s?.type==="CORTI_EMBEDDED_EVENT"){this.handleEvent(s);return}s.requestId&&this.pendingRequests.has(s.requestId)&&this.handleResponse(s)},window.addEventListener("message",this.messageListener)}handleEvent(e){let t=e.event,{payload:s}=e;(t==="ready"||t==="loaded"||t==="embedded.ready")&&(this.isReady=!0),this.callbacks.onEvent?.({name:t,payload:s})}handleResponse(e){let t=this.pendingRequests.get(e.requestId);if(t){let{resolve:s,reject:o}=t;if(this.pendingRequests.delete(e.requestId),e.success===!1||e.error){let i={message:e.error||"Request failed",code:e.errorCode,details:e.errorDetails};this.callbacks.onError?.(i),o(i)}else s(e)}}destroy(){this.messageListener&&(window.removeEventListener("message",this.messageListener),this.messageListener=null),this.pendingRequests.clear()}updateCallbacks(e){this.callbacks={...this.callbacks,...e}}get ready(){return this.isReady}async waitForReady(e=3e4){return this.isReady?Promise.resolve():new Promise((t,s)=>{let o=setTimeout(()=>{s(new Error("Timeout waiting for iframe to be ready"))},e),i=n=>{n.source===this.iframe.contentWindow&&n.origin===this.getTrustedOrigin()&&n.data?.type==="CORTI_EMBEDDED_EVENT"&&(n.data.event==="ready"||n.data.event==="loaded"||n.data.event==="embedded.ready")&&(clearTimeout(o),window.removeEventListener("message",i),t())};window.addEventListener("message",i)})}async postMessage(e,t=1e4){if(!this.iframe.contentWindow)throw new Error("Iframe not ready");await this.waitForReady();let{contentWindow:s}=this.iframe,o=r.generateRequestId();return new Promise((i,n)=>{let c=setTimeout(()=>{this.pendingRequests.delete(o),n(new Error("Request timeout"))},t),a={resolve:l=>{clearTimeout(c),i(l)},reject:l=>{clearTimeout(c),n(l)}};this.pendingRequests.set(o,a);let h={...e,requestId:o},d=this.getTrustedOrigin();if(!d){this.pendingRequests.delete(o),n(new Error("Cannot determine trusted origin for postMessage"));return}s.postMessage(h,d)})}async auth(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"auth",payload:e});if(this.isReady=!1,t.payload&&t.success)return t.payload.user;throw new Error(t.error)}async configureSession(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"configureSession",payload:e})}async navigate(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"navigate",payload:e})}async addFacts(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"addFacts",payload:e})}async createInteraction(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"createInteraction",payload:e});if(t.payload&&t.success)return t.payload;throw new Error(t.error)}async startRecording(){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"startRecording",payload:{}})}async stopRecording(){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"stopRecording",payload:{}})}async getStatus(){let e=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"getStatus",payload:{}});if(e.payload&&e.success)return e.payload;throw new Error(e.error)}async configure(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"configure",payload:e});if(t.payload&&t.success)return t.payload;throw new Error(t.error)}async setCredentials(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"setCredentials",payload:e})}async getTemplates(){let e=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"getTemplates"});if(e.payload&&e.success)return e.payload;throw new Error(e.error)}static generateRequestId(){return`req_${Date.now()}_${Math.random().toString(36).substr(2,9)}`}getTrustedOrigin(){try{let e=this.iframe.getAttribute("src")||this.iframe.src;return e?new URL(e,window.location.href).origin:null}catch{return null}}};var Te=function(r,e,t,s){var o=arguments.length,i=o<3?e:s===null?s=Object.getOwnPropertyDescriptor(e,t):s,n;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(r,e,t,s);else for(var c=r.length-1;c>=0;c--)(n=r[c])&&(i=(o<3?n(i):o>3?n(e,t,i):n(e,t))||i);return o>3&&i&&Object.defineProperty(e,t,i),i},E=class extends _{constructor(){super(...arguments),this.visibility="hidden",this.postMessageHandler=null,this.normalizedBaseURL=null}connectedCallback(){if(super.connectedCallback(),!this.baseURL){this.dispatchErrorEvent({message:"baseURL is required"});return}try{this.normalizedBaseURL=J(this.baseURL)}catch(e){throw this.dispatchErrorEvent({message:e.message||"Invalid baseURL"}),e}}disconnectedCallback(){super.disconnectedCallback(),this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null)}async setupPostMessageHandler(){if(this.postMessageHandler)return;let e=this.getIframe();if(e?.contentWindow){let t={onEvent:s=>{this.dispatchEmbeddedEvent(s.name,s.payload)},onError:s=>{this.dispatchErrorEvent(s)}};this.postMessageHandler=new Z(e,t)}else this.dispatchErrorEvent({message:"No iframe or contentWindow available"})}dispatchPublicEvent(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))}dispatchEmbeddedEvent(e,t){this.dispatchPublicEvent(e,t),this.dispatchPublicEvent("embedded-event",{name:e,payload:t})}dispatchErrorEvent(e){this.dispatchPublicEvent("error",e)}isRealIframeLoad(e){let t=e.getAttribute("src")||"";return this.normalizedBaseURL?He(t,this.normalizedBaseURL):!1}async handleIframeLoad(e){let t=e.target;if(t&&this.isRealIframeLoad(t))try{await this.setupPostMessageHandler()}catch(s){this.dispatchErrorEvent({message:`Failed to setup PostMessageHandler on iframe load: ${s}`})}}getIframe(){return this.shadowRoot?.querySelector("iframe")||null}updated(e){if(super.updated(e),e.has("baseURL")){try{this.normalizedBaseURL=J(this.baseURL)}catch(s){this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null);let o=this.getIframe();o&&o.setAttribute("src","about:blank"),this.dispatchErrorEvent({message:s.message||"Invalid baseURL"});return}this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null);let t=this.getIframe();if(t){let s=this.normalizedBaseURL?le(this.normalizedBaseURL):"";t.getAttribute("src")!==s&&(t.setAttribute("src",s),t.setAttribute("allow",`microphone ${s}; camera ${s}; device-capture ${s}; display-capture ${s}`))}}}async auth(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={access_token:e.access_token,token_type:e.token_type,expires_at:e.expires_at,expires_in:e.expires_in,refresh_expires_in:e.refresh_expires_in,refresh_token:e.refresh_token,id_token:e.id_token,"not-before-policy":e["not-before-policy"],session_state:e.session_state,scope:e.scope,profile:e.profile,mode:e.mode};return await this.postMessageHandler.auth(t)}catch(t){let s=y(t,"Authentication failed");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async createInteraction(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t=await this.postMessageHandler.createInteraction(e);return{id:t.id,createdAt:t.createdAt}}catch(t){let s=y(t,"Failed to create interaction");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async configureSession(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={defaultLanguage:e.defaultLanguage,defaultOutputLanguage:e.defaultOutputLanguage,defaultTemplateKey:e.defaultTemplateKey,defaultMode:e.defaultMode};await this.postMessageHandler.configureSession(t)}catch(t){let s=y(t,"Failed to configure session");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async addFacts(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={facts:e};await this.postMessageHandler.addFacts(t)}catch(t){let s=y(t,"Failed to add facts");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async navigate(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={path:e};await this.postMessageHandler.navigate(t)}catch(t){let s=y(t,"Failed to navigate");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async startRecording(){if(!this.postMessageHandler)throw new Error("Component not ready");try{await this.postMessageHandler.startRecording()}catch(e){let t=y(e,"Failed to start recording");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async stopRecording(){if(!this.postMessageHandler)throw new Error("Component not ready");try{await this.postMessageHandler.stopRecording()}catch(e){let t=y(e,"Failed to stop recording");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async getStatus(){if(!this.postMessageHandler)return{auth:{isAuthenticated:!1,user:void 0},currentUrl:"",interaction:null};try{return await this.postMessageHandler.getStatus()}catch(e){let t=y(e,"Failed to get status");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async configure(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{return await this.postMessageHandler.configure(e)}catch(t){let s=y(t,"Failed to configure component");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async setCredentials(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{if(!e.password)throw new Error("Password is required");await this.postMessageHandler.setCredentials(e)}catch(t){let s=y(t,"Failed to set credentials");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}show(){this.visibility="visible"}hide(){this.visibility="hidden"}async getTemplates(){if(!this.postMessageHandler)throw new Error("Component not ready");try{return await this.postMessageHandler.getTemplates()}catch(e){let t=y(e,"Failed to get templates");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}getDebugStatus(){let e=this.getIframe();return{ready:e?.contentDocument?.readyState==="complete",iframeExists:!!e,iframeSrc:e?.src,iframeContentWindow:!!e?.contentWindow,iframeContentDocument:!!e?.contentDocument,iframeReadyState:e?.contentDocument?.readyState,postMessageHandlerExists:!!this.postMessageHandler,postMessageHandlerReady:this.postMessageHandler?.ready||!1,baseURL:this.baseURL}}render(){return this.baseURL?ce`
|
|
38
|
+
`;function J(r){let e;try{e=new URL(r)}catch{throw new Error("Invalid baseURL: not a parseable URL")}if(e.protocol!=="https:")throw new Error("Invalid baseURL: must use https");let t=e.host.toLowerCase();if(!/^assistant\.[a-z0-9-]+\.corti\.app$/i.test(t))throw new Error("Invalid baseURL: host must match assistant.xxx.corti.app");if(e.pathname&&e.pathname!=="/"&&e.pathname!=="")throw new Error("Invalid baseURL: must not include a path");if(e.username||e.password)throw new Error("Invalid baseURL: must not include credentials");return e.origin.replace(/\/+$/,"")}function le(r){return`${r}/embedded`}function He(r,e){if(!r||r.startsWith("about:"))return!1;try{let t=new URL(r,window.location.href),s=new URL(e);return t.origin!==s.origin?!1:t.pathname.replace(/\/+$/,"")==="/embedded"}catch{return!1}}function et(r){try{return JSON.parse(r)}catch{return null}}function K(r){let e=r.match(/\b([1-5]\d{2})\b/);return e?e[1]:void 0}function he(r){return typeof r=="object"&&r!==null&&("expected"in r||"code"in r||"path"in r||"message"in r)}function pe(r){let{path:e,expected:t,message:s,code:o}=r;if(s)return s;let i=e&&e.length>0?e.join("."):"field";return t?`Invalid ${i}: expected ${t}`:o?`Validation error in ${i} (${o})`:`Validation error in ${i}`}function Pe(r){return r.length===0?"Unknown validation error":r.length===1?pe(r[0]):`Multiple validation errors: ${r.map(pe).join("; ")}`}function y(r,e="An error occurred"){if(!r)return{message:e,details:r};if(r instanceof Error){let t=K(r.message);return{message:r.message||e,code:t,details:r}}if(typeof r=="string"){let t=K(r);return{message:r,code:t,details:r}}if(Array.isArray(r)){if(r.every(he)){let t=r;return{message:Pe(t),code:t[0]?.code,details:r}}return{message:`Multiple errors: ${r.length} item(s)`,details:r}}if(typeof r=="object"){let t=r;if(t.message&&typeof t.message=="string"){let s=t.message,{code:o}=t;if(o||(o=K(s)),t.details?.message&&typeof t.details.message=="string"){let i=et(t.details.message);Array.isArray(i)&&i.every(he)&&(s=Pe(i))}return{message:s,code:o,details:t.details}}if(he(t))return{message:pe(t),code:t.code,details:r};if(t.error||t.message||t.detail){let s=t.error||t.message||t.detail,o=typeof s=="string"?s:e,i=t.code||t.status;return!i&&typeof s=="string"&&(i=K(s)),{message:o,code:i,details:r}}}return{message:e,details:r}}var Z=class r{constructor(e,t={}){this.pendingRequests=new Map,this.messageListener=null,this.isReady=!1,this.iframe=e,this.callbacks=t,this.setupMessageListener()}setupMessageListener(){this.messageListener=e=>{if(e.source!==this.iframe.contentWindow)return;let t=this.getTrustedOrigin();if(!t||e.origin!==t)return;let{data:s}=e;if(s?.type==="CORTI_EMBEDDED_EVENT"){this.handleEvent(s);return}s.requestId&&this.pendingRequests.has(s.requestId)&&this.handleResponse(s)},window.addEventListener("message",this.messageListener)}handleEvent(e){let t=e.event,{payload:s}=e;(t==="ready"||t==="loaded"||t==="embedded.ready")&&(this.isReady=!0),this.callbacks.onEvent?.({name:t,payload:s})}handleResponse(e){let t=this.pendingRequests.get(e.requestId);if(t){let{resolve:s,reject:o}=t;if(this.pendingRequests.delete(e.requestId),e.success===!1||e.error){let i={message:e.error||"Request failed",code:e.errorCode,details:e.errorDetails};this.callbacks.onError?.(i),o(i)}else s(e)}}destroy(){this.messageListener&&(window.removeEventListener("message",this.messageListener),this.messageListener=null),this.pendingRequests.clear()}updateCallbacks(e){this.callbacks={...this.callbacks,...e}}get ready(){return this.isReady}async waitForReady(e=3e4){return this.isReady?Promise.resolve():new Promise((t,s)=>{let o=setTimeout(()=>{s(new Error("Timeout waiting for iframe to be ready"))},e),i=n=>{n.source===this.iframe.contentWindow&&n.origin===this.getTrustedOrigin()&&n.data?.type==="CORTI_EMBEDDED_EVENT"&&(n.data.event==="ready"||n.data.event==="loaded"||n.data.event==="embedded.ready")&&(clearTimeout(o),window.removeEventListener("message",i),t())};window.addEventListener("message",i)})}async postMessage(e,t=1e4){if(!this.iframe.contentWindow)throw new Error("Iframe not ready");await this.waitForReady();let{contentWindow:s}=this.iframe,o=r.generateRequestId();return new Promise((i,n)=>{let c=setTimeout(()=>{this.pendingRequests.delete(o),n(new Error("Request timeout"))},t),a={resolve:l=>{clearTimeout(c),i(l)},reject:l=>{clearTimeout(c),n(l)}};this.pendingRequests.set(o,a);let h={...e,requestId:o},d=this.getTrustedOrigin();if(!d){this.pendingRequests.delete(o),n(new Error("Cannot determine trusted origin for postMessage"));return}s.postMessage(h,d)})}async auth(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"auth",payload:e});if(t.payload&&t.success)return t.payload.user;throw new Error(t.error)}async configureSession(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"configureSession",payload:e})}async navigate(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"navigate",payload:e})}async addFacts(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"addFacts",payload:e})}async createInteraction(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"createInteraction",payload:e});if(t.payload&&t.success)return t.payload;throw new Error(t.error)}async startRecording(){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"startRecording",payload:{}})}async stopRecording(){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"stopRecording",payload:{}})}async getStatus(){let e=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"getStatus",payload:{}});if(e.payload&&e.success)return e.payload;throw new Error(e.error)}async configure(e){let t=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"configure",payload:e});if(t.payload&&t.success)return t.payload;throw new Error(t.error)}async setCredentials(e){await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"setCredentials",payload:e})}async getTemplates(){let e=await this.postMessage({type:"CORTI_EMBEDDED",version:"v1",action:"getTemplates"});if(e.payload&&e.success)return e.payload;throw new Error(e.error)}static generateRequestId(){return`req_${Date.now()}_${Math.random().toString(36).substr(2,9)}`}getTrustedOrigin(){try{let e=this.iframe.getAttribute("src")||this.iframe.src;return e?new URL(e,window.location.href).origin:null}catch{return null}}};var Te=function(r,e,t,s){var o=arguments.length,i=o<3?e:s===null?s=Object.getOwnPropertyDescriptor(e,t):s,n;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(r,e,t,s);else for(var c=r.length-1;c>=0;c--)(n=r[c])&&(i=(o<3?n(i):o>3?n(e,t,i):n(e,t))||i);return o>3&&i&&Object.defineProperty(e,t,i),i},E=class extends _{constructor(){super(...arguments),this.visibility="hidden",this.postMessageHandler=null,this.normalizedBaseURL=null}connectedCallback(){if(super.connectedCallback(),!this.baseURL){this.dispatchErrorEvent({message:"baseURL is required"});return}try{this.normalizedBaseURL=J(this.baseURL)}catch(e){throw this.dispatchErrorEvent({message:e.message||"Invalid baseURL"}),e}}disconnectedCallback(){super.disconnectedCallback(),this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null)}async setupPostMessageHandler(){if(this.postMessageHandler)return;let e=this.getIframe();if(e?.contentWindow){let t={onEvent:s=>{this.dispatchEmbeddedEvent(s.name,s.payload)},onError:s=>{this.dispatchErrorEvent(s)}};this.postMessageHandler=new Z(e,t)}else this.dispatchErrorEvent({message:"No iframe or contentWindow available"})}dispatchPublicEvent(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))}dispatchEmbeddedEvent(e,t){this.dispatchPublicEvent(e,t),this.dispatchPublicEvent("embedded-event",{name:e,payload:t})}dispatchErrorEvent(e){this.dispatchPublicEvent("error",e)}isRealIframeLoad(e){let t=e.getAttribute("src")||"";return this.normalizedBaseURL?He(t,this.normalizedBaseURL):!1}async handleIframeLoad(e){let t=e.target;if(t&&this.isRealIframeLoad(t))try{await this.setupPostMessageHandler()}catch(s){this.dispatchErrorEvent({message:`Failed to setup PostMessageHandler on iframe load: ${s}`})}}getIframe(){return this.shadowRoot?.querySelector("iframe")||null}updated(e){if(super.updated(e),e.has("baseURL")){try{this.normalizedBaseURL=J(this.baseURL)}catch(s){this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null);let o=this.getIframe();o&&o.setAttribute("src","about:blank"),this.dispatchErrorEvent({message:s.message||"Invalid baseURL"});return}this.postMessageHandler&&(this.postMessageHandler.destroy(),this.postMessageHandler=null);let t=this.getIframe();if(t){let s=this.normalizedBaseURL?le(this.normalizedBaseURL):"";t.getAttribute("src")!==s&&(t.setAttribute("src",s),t.setAttribute("allow",`microphone ${s}; camera ${s}; device-capture ${s}; display-capture ${s}`))}}}async auth(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={access_token:e.access_token,token_type:e.token_type,expires_at:e.expires_at,expires_in:e.expires_in,refresh_expires_in:e.refresh_expires_in,refresh_token:e.refresh_token,id_token:e.id_token,"not-before-policy":e["not-before-policy"],session_state:e.session_state,scope:e.scope,profile:e.profile,mode:e.mode};return await this.postMessageHandler.auth(t)}catch(t){let s=y(t,"Authentication failed");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async createInteraction(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t=await this.postMessageHandler.createInteraction(e);return{id:t.id,createdAt:t.createdAt}}catch(t){let s=y(t,"Failed to create interaction");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async configureSession(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={defaultLanguage:e.defaultLanguage,defaultOutputLanguage:e.defaultOutputLanguage,defaultTemplateKey:e.defaultTemplateKey,defaultMode:e.defaultMode};await this.postMessageHandler.configureSession(t)}catch(t){let s=y(t,"Failed to configure session");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async addFacts(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={facts:e};await this.postMessageHandler.addFacts(t)}catch(t){let s=y(t,"Failed to add facts");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async navigate(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{let t={path:e};await this.postMessageHandler.navigate(t)}catch(t){let s=y(t,"Failed to navigate");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async startRecording(){if(!this.postMessageHandler)throw new Error("Component not ready");try{await this.postMessageHandler.startRecording()}catch(e){let t=y(e,"Failed to start recording");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async stopRecording(){if(!this.postMessageHandler)throw new Error("Component not ready");try{await this.postMessageHandler.stopRecording()}catch(e){let t=y(e,"Failed to stop recording");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async getStatus(){if(!this.postMessageHandler)return{auth:{isAuthenticated:!1,user:void 0},currentUrl:"",interaction:null};try{return await this.postMessageHandler.getStatus()}catch(e){let t=y(e,"Failed to get status");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}async configure(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{return await this.postMessageHandler.configure(e)}catch(t){let s=y(t,"Failed to configure component");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}async setCredentials(e){if(!this.postMessageHandler)throw new Error("Component not ready");try{if(!e.password)throw new Error("Password is required");await this.postMessageHandler.setCredentials(e)}catch(t){let s=y(t,"Failed to set credentials");throw this.dispatchErrorEvent(s),new Error(JSON.stringify(s))}}show(){this.visibility="visible"}hide(){this.visibility="hidden"}async getTemplates(){if(!this.postMessageHandler)throw new Error("Component not ready");try{return await this.postMessageHandler.getTemplates()}catch(e){let t=y(e,"Failed to get templates");throw this.dispatchErrorEvent(t),new Error(JSON.stringify(t))}}getDebugStatus(){let e=this.getIframe();return{ready:e?.contentDocument?.readyState==="complete",iframeExists:!!e,iframeSrc:e?.src,iframeContentWindow:!!e?.contentWindow,iframeContentDocument:!!e?.contentDocument,iframeReadyState:e?.contentDocument?.readyState,postMessageHandlerExists:!!this.postMessageHandler,postMessageHandlerReady:this.postMessageHandler?.ready||!1,baseURL:this.baseURL}}render(){return this.baseURL?ce`
|
|
39
39
|
<iframe
|
|
40
40
|
src=${le(J(this.baseURL))}
|
|
41
41
|
title="Corti Embedded UI"
|