- Modernized authentication with the introduction of TenantProject
- Created a base mail template - Adapt and improve document API to usage of catalogs and processors - Adapt eveai_sync to new authentication mechanism and usage of catalogs and processors
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
class EveAI {
|
||||
constructor(config) {
|
||||
// Required parameters
|
||||
this.tenantId = config.tenantId;
|
||||
this.serverUrl = config.serverUrl;
|
||||
this.specialistId = config.specialistId;
|
||||
this.proxyUrl = config.proxyUrl;
|
||||
|
||||
// Optional parameters with defaults
|
||||
this.language = config.language || 'en';
|
||||
this.languages = config.languages?.split(',') || ['en'];
|
||||
this.domain = config.domain || window.location.origin;
|
||||
|
||||
// Internal state
|
||||
this.sessionToken = null;
|
||||
this.initialized = false;
|
||||
}
|
||||
|
||||
async getSessionToken() {
|
||||
try {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
// Add WordPress-specific headers if they exist
|
||||
if (window.eveaiWP?.nonce) {
|
||||
headers['X-WP-Nonce'] = window.eveaiWP.nonce;
|
||||
}
|
||||
|
||||
const response = await fetch(this.proxyUrl, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify({
|
||||
tenant_id: this.tenantId,
|
||||
domain: this.domain
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to get session token');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.session_token;
|
||||
} catch (error) {
|
||||
console.error('Error getting session token:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async initializeChat(containerId) {
|
||||
try {
|
||||
// Get session token before initializing chat
|
||||
this.sessionToken = await this.getSessionToken();
|
||||
|
||||
const container = document.getElementById(containerId);
|
||||
if (!container) {
|
||||
throw new Error('Container not found');
|
||||
}
|
||||
|
||||
const chatWidget = document.createElement('eveai-chat-widget');
|
||||
Object.entries({
|
||||
'tenant-id': this.tenantId,
|
||||
'session-token': this.sessionToken,
|
||||
'domain': this.domain,
|
||||
'language': this.language,
|
||||
'languages': this.languages.join(','),
|
||||
'server-url': this.serverUrl,
|
||||
'specialist-id': this.specialistId
|
||||
}).forEach(([attr, value]) => {
|
||||
chatWidget.setAttribute(attr, value);
|
||||
});
|
||||
|
||||
container.appendChild(chatWidget);
|
||||
this.initialized = true;
|
||||
|
||||
return chatWidget;
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize chat:', error);
|
||||
// Re-throw to allow custom error handling
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make available globally
|
||||
window.EveAI = EveAI;
|
||||
Reference in New Issue
Block a user