@egain/ai-agent-sdk 0.1.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/README.md +295 -0
- package/dist/browser.js +22739 -0
- package/dist/core/AiAgent.d.ts +1126 -0
- package/dist/core/AiAgent.d.ts.map +1 -0
- package/dist/core/AiAgent.js +2037 -0
- package/dist/core/AiAgent.js.map +1 -0
- package/dist/core/api/ApiHelper.d.ts +433 -0
- package/dist/core/api/ApiHelper.d.ts.map +1 -0
- package/dist/core/api/ApiHelper.js +689 -0
- package/dist/core/api/ApiHelper.js.map +1 -0
- package/dist/core/api/CacheAdapter.d.ts +295 -0
- package/dist/core/api/CacheAdapter.d.ts.map +1 -0
- package/dist/core/api/CacheAdapter.js +298 -0
- package/dist/core/api/CacheAdapter.js.map +1 -0
- package/dist/core/auth/AnonymousAuthStrategy.d.ts +87 -0
- package/dist/core/auth/AnonymousAuthStrategy.d.ts.map +1 -0
- package/dist/core/auth/AnonymousAuthStrategy.js +257 -0
- package/dist/core/auth/AnonymousAuthStrategy.js.map +1 -0
- package/dist/core/auth/AuthProvider.d.ts +13 -0
- package/dist/core/auth/AuthProvider.d.ts.map +1 -0
- package/dist/core/auth/AuthProvider.js +2 -0
- package/dist/core/auth/AuthProvider.js.map +1 -0
- package/dist/core/auth/AuthStrategy.d.ts +74 -0
- package/dist/core/auth/AuthStrategy.d.ts.map +1 -0
- package/dist/core/auth/AuthStrategy.js +2 -0
- package/dist/core/auth/AuthStrategy.js.map +1 -0
- package/dist/core/auth/AuthenticationService.d.ts +226 -0
- package/dist/core/auth/AuthenticationService.d.ts.map +1 -0
- package/dist/core/auth/AuthenticationService.js +344 -0
- package/dist/core/auth/AuthenticationService.js.map +1 -0
- package/dist/core/auth/ClientCredentialsAuthStrategy.d.ts +62 -0
- package/dist/core/auth/ClientCredentialsAuthStrategy.d.ts.map +1 -0
- package/dist/core/auth/ClientCredentialsAuthStrategy.js +78 -0
- package/dist/core/auth/ClientCredentialsAuthStrategy.js.map +1 -0
- package/dist/core/auth/PKCEAuthStrategy.d.ts +136 -0
- package/dist/core/auth/PKCEAuthStrategy.d.ts.map +1 -0
- package/dist/core/auth/PKCEAuthStrategy.js +409 -0
- package/dist/core/auth/PKCEAuthStrategy.js.map +1 -0
- package/dist/core/auth/PreAuthStrategy.d.ts +101 -0
- package/dist/core/auth/PreAuthStrategy.d.ts.map +1 -0
- package/dist/core/auth/PreAuthStrategy.js +216 -0
- package/dist/core/auth/PreAuthStrategy.js.map +1 -0
- package/dist/core/auth/msal-browser.js +19683 -0
- package/dist/core/auth/msal-loader.d.ts +14 -0
- package/dist/core/auth/msal-loader.d.ts.map +1 -0
- package/dist/core/auth/msal-loader.js +43 -0
- package/dist/core/auth/msal-loader.js.map +1 -0
- package/dist/core/connection/Connection.d.ts +168 -0
- package/dist/core/connection/Connection.d.ts.map +1 -0
- package/dist/core/connection/Connection.js +290 -0
- package/dist/core/connection/Connection.js.map +1 -0
- package/dist/core/connection/ConnectionState.d.ts +11 -0
- package/dist/core/connection/ConnectionState.d.ts.map +1 -0
- package/dist/core/connection/ConnectionState.js +12 -0
- package/dist/core/connection/ConnectionState.js.map +1 -0
- package/dist/core/connection/Transport.d.ts +98 -0
- package/dist/core/connection/Transport.d.ts.map +1 -0
- package/dist/core/connection/Transport.js +27 -0
- package/dist/core/connection/Transport.js.map +1 -0
- package/dist/core/connection/WebSocketTransport.d.ts +65 -0
- package/dist/core/connection/WebSocketTransport.d.ts.map +1 -0
- package/dist/core/connection/WebSocketTransport.js +177 -0
- package/dist/core/connection/WebSocketTransport.js.map +1 -0
- package/dist/core/errors/SDKError.d.ts +27 -0
- package/dist/core/errors/SDKError.d.ts.map +1 -0
- package/dist/core/errors/SDKError.js +43 -0
- package/dist/core/errors/SDKError.js.map +1 -0
- package/dist/core/events/EventEmitter.d.ts +120 -0
- package/dist/core/events/EventEmitter.d.ts.map +1 -0
- package/dist/core/events/EventEmitter.js +183 -0
- package/dist/core/events/EventEmitter.js.map +1 -0
- package/dist/core/logging/LogLevel.d.ts +33 -0
- package/dist/core/logging/LogLevel.d.ts.map +1 -0
- package/dist/core/logging/LogLevel.js +40 -0
- package/dist/core/logging/LogLevel.js.map +1 -0
- package/dist/core/logging/Logger.d.ts +120 -0
- package/dist/core/logging/Logger.d.ts.map +1 -0
- package/dist/core/logging/Logger.js +204 -0
- package/dist/core/logging/Logger.js.map +1 -0
- package/dist/core/logging/globalLogger.d.ts +8 -0
- package/dist/core/logging/globalLogger.d.ts.map +1 -0
- package/dist/core/logging/globalLogger.js +12 -0
- package/dist/core/logging/globalLogger.js.map +1 -0
- package/dist/core/logging/types.d.ts +45 -0
- package/dist/core/logging/types.d.ts.map +1 -0
- package/dist/core/logging/types.js +2 -0
- package/dist/core/logging/types.js.map +1 -0
- package/dist/core/message/BaseMessageHandler.d.ts +208 -0
- package/dist/core/message/BaseMessageHandler.d.ts.map +1 -0
- package/dist/core/message/BaseMessageHandler.js +155 -0
- package/dist/core/message/BaseMessageHandler.js.map +1 -0
- package/dist/core/message/Message.d.ts +69 -0
- package/dist/core/message/Message.d.ts.map +1 -0
- package/dist/core/message/Message.js +131 -0
- package/dist/core/message/Message.js.map +1 -0
- package/dist/core/message/MessageProcessor.d.ts +51 -0
- package/dist/core/message/MessageProcessor.d.ts.map +1 -0
- package/dist/core/message/MessageProcessor.js +123 -0
- package/dist/core/message/MessageProcessor.js.map +1 -0
- package/dist/core/message/MessageTypes.d.ts +123 -0
- package/dist/core/message/MessageTypes.d.ts.map +1 -0
- package/dist/core/message/MessageTypes.js +106 -0
- package/dist/core/message/MessageTypes.js.map +1 -0
- package/dist/core/message/Transcript.d.ts +373 -0
- package/dist/core/message/Transcript.d.ts.map +1 -0
- package/dist/core/message/Transcript.js +355 -0
- package/dist/core/message/Transcript.js.map +1 -0
- package/dist/core/message/handlers/AgentMessageHandler.d.ts +26 -0
- package/dist/core/message/handlers/AgentMessageHandler.d.ts.map +1 -0
- package/dist/core/message/handlers/AgentMessageHandler.js +130 -0
- package/dist/core/message/handlers/AgentMessageHandler.js.map +1 -0
- package/dist/core/message/handlers/ChatHistoryHandler.d.ts +12 -0
- package/dist/core/message/handlers/ChatHistoryHandler.d.ts.map +1 -0
- package/dist/core/message/handlers/ChatHistoryHandler.js +49 -0
- package/dist/core/message/handlers/ChatHistoryHandler.js.map +1 -0
- package/dist/core/message/handlers/ErrorMessageHandler.d.ts +12 -0
- package/dist/core/message/handlers/ErrorMessageHandler.d.ts.map +1 -0
- package/dist/core/message/handlers/ErrorMessageHandler.js +49 -0
- package/dist/core/message/handlers/ErrorMessageHandler.js.map +1 -0
- package/dist/core/message/handlers/HeartbeatHandler.d.ts +12 -0
- package/dist/core/message/handlers/HeartbeatHandler.d.ts.map +1 -0
- package/dist/core/message/handlers/HeartbeatHandler.js +46 -0
- package/dist/core/message/handlers/HeartbeatHandler.js.map +1 -0
- package/dist/core/message/handlers/TokenRefreshHandler.d.ts +30 -0
- package/dist/core/message/handlers/TokenRefreshHandler.d.ts.map +1 -0
- package/dist/core/message/handlers/TokenRefreshHandler.js +84 -0
- package/dist/core/message/handlers/TokenRefreshHandler.js.map +1 -0
- package/dist/core/message/types.d.ts +107 -0
- package/dist/core/message/types.d.ts.map +1 -0
- package/dist/core/message/types.js +30 -0
- package/dist/core/message/types.js.map +1 -0
- package/dist/core/platform/HookContract.d.ts +112 -0
- package/dist/core/platform/HookContract.d.ts.map +1 -0
- package/dist/core/platform/HookContract.js +13 -0
- package/dist/core/platform/HookContract.js.map +1 -0
- package/dist/core/platform/PlatformComponentService.d.ts +40 -0
- package/dist/core/platform/PlatformComponentService.d.ts.map +1 -0
- package/dist/core/platform/PlatformComponentService.js +12 -0
- package/dist/core/platform/PlatformComponentService.js.map +1 -0
- package/dist/core/platform/PlatformScriptLoader.d.ts +41 -0
- package/dist/core/platform/PlatformScriptLoader.d.ts.map +1 -0
- package/dist/core/platform/PlatformScriptLoader.js +110 -0
- package/dist/core/platform/PlatformScriptLoader.js.map +1 -0
- package/dist/core/polyfills.d.ts +16 -0
- package/dist/core/polyfills.d.ts.map +1 -0
- package/dist/core/polyfills.js +168 -0
- package/dist/core/polyfills.js.map +1 -0
- package/dist/core/portal-initializer/PortalInitializer.d.ts +234 -0
- package/dist/core/portal-initializer/PortalInitializer.d.ts.map +1 -0
- package/dist/core/portal-initializer/PortalInitializer.js +636 -0
- package/dist/core/portal-initializer/PortalInitializer.js.map +1 -0
- package/dist/core/queue/MessageQueue.d.ts +277 -0
- package/dist/core/queue/MessageQueue.d.ts.map +1 -0
- package/dist/core/queue/MessageQueue.js +291 -0
- package/dist/core/queue/MessageQueue.js.map +1 -0
- package/dist/core/types/PortalTypes.d.ts +51 -0
- package/dist/core/types/PortalTypes.d.ts.map +1 -0
- package/dist/core/types/PortalTypes.js +8 -0
- package/dist/core/types/PortalTypes.js.map +1 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/package.json +84 -0
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
import { createCacheAdapter, } from './CacheAdapter.js';
|
|
2
|
+
/**
|
|
3
|
+
* Valid Accept-Language header values for v12 onwards
|
|
4
|
+
*/
|
|
5
|
+
const VALID_ACCEPT_LANGUAGES_HEADER_FOR_V12_ONWARDS = {
|
|
6
|
+
'da-dk': true,
|
|
7
|
+
'de-de': true,
|
|
8
|
+
'en-us': true,
|
|
9
|
+
'es-es': true,
|
|
10
|
+
'fr-ca': true,
|
|
11
|
+
'fr-fr': true,
|
|
12
|
+
'it-it': true,
|
|
13
|
+
'ja-jp': true,
|
|
14
|
+
'ko-kr': true,
|
|
15
|
+
'nl-nl': true,
|
|
16
|
+
'pt-br': true,
|
|
17
|
+
'pt-pt': true,
|
|
18
|
+
'ru-ru': true,
|
|
19
|
+
'sv-se': true,
|
|
20
|
+
'zh-cn': true,
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Language mapping for special cases
|
|
24
|
+
*/
|
|
25
|
+
const LANGUAGE_MAPPING = {
|
|
26
|
+
'da-DA': 'da-dk',
|
|
27
|
+
'ja-JA': 'ja-jp',
|
|
28
|
+
'ko-KO': 'ko-kr',
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Default cache TTL (5 minutes)
|
|
32
|
+
*/
|
|
33
|
+
const DEFAULT_CACHE_TTL = 5 * 60 * 1000;
|
|
34
|
+
/**
|
|
35
|
+
* Default cache key prefix
|
|
36
|
+
*/
|
|
37
|
+
const DEFAULT_CACHE_PREFIX = 'egain_aiagent_cache_';
|
|
38
|
+
/**
|
|
39
|
+
* API Helper class for making eGain AI Agent API calls
|
|
40
|
+
*/
|
|
41
|
+
export class ApiHelper {
|
|
42
|
+
/**
|
|
43
|
+
* Gets or creates the static cache adapter for deployment info
|
|
44
|
+
*/
|
|
45
|
+
static getStaticCacheAdapter() {
|
|
46
|
+
if (!ApiHelper.staticCacheAdapter) {
|
|
47
|
+
ApiHelper.staticCacheAdapter = createCacheAdapter('session');
|
|
48
|
+
}
|
|
49
|
+
return ApiHelper.staticCacheAdapter;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets a custom static cache adapter for deployment info caching.
|
|
53
|
+
* Call this before any AiAgent.initialize() calls to use a custom adapter
|
|
54
|
+
* for caching deployment information.
|
|
55
|
+
* @param adapter - Custom CacheAdapter implementation
|
|
56
|
+
*/
|
|
57
|
+
static setStaticCacheAdapter(adapter) {
|
|
58
|
+
ApiHelper.staticCacheAdapter = adapter;
|
|
59
|
+
}
|
|
60
|
+
constructor(config) {
|
|
61
|
+
this.apiDomain =
|
|
62
|
+
config.apiDomain.indexOf('http') !== 0
|
|
63
|
+
? 'https://' + config.apiDomain
|
|
64
|
+
: config.apiDomain;
|
|
65
|
+
this.language = config.language || 'en-us';
|
|
66
|
+
// Initialize cache settings
|
|
67
|
+
const cacheConfig = config.cache || {};
|
|
68
|
+
this.cacheEnabled = cacheConfig.enabled !== false;
|
|
69
|
+
this.cacheKeyPrefix = cacheConfig.keyPrefix || DEFAULT_CACHE_PREFIX;
|
|
70
|
+
this.cacheTtl = cacheConfig.ttl || DEFAULT_CACHE_TTL;
|
|
71
|
+
// Create cache adapter if caching is enabled
|
|
72
|
+
// Use custom adapter if provided, otherwise fall back to built-in adapters
|
|
73
|
+
if (this.cacheEnabled) {
|
|
74
|
+
this.cacheAdapter = cacheConfig.adapter ?? createCacheAdapter(cacheConfig.storageType || 'session');
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.cacheAdapter = null;
|
|
78
|
+
}
|
|
79
|
+
this.tokenProvider = config.getToken;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Resolves the bearer/access token from explicit options or from {@link ApiHelperConfig.getToken}.
|
|
83
|
+
*/
|
|
84
|
+
async resolveAuthToken(explicit) {
|
|
85
|
+
if (explicit != null && String(explicit).trim() !== '') {
|
|
86
|
+
return String(explicit);
|
|
87
|
+
}
|
|
88
|
+
if (this.tokenProvider) {
|
|
89
|
+
const t = await this.tokenProvider();
|
|
90
|
+
if (t != null && String(t).trim() !== '') {
|
|
91
|
+
return t;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
throw new Error('Authentication token is required: pass authToken in options or configure ApiHelper with getToken()');
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Gets the language for the API call
|
|
98
|
+
* @returns {string} The language
|
|
99
|
+
*/
|
|
100
|
+
getLanguage() {
|
|
101
|
+
if (this.language) {
|
|
102
|
+
// Check for special mappings
|
|
103
|
+
if (LANGUAGE_MAPPING[this.language]) {
|
|
104
|
+
return LANGUAGE_MAPPING[this.language];
|
|
105
|
+
}
|
|
106
|
+
// Convert locale format (e.g., "en-US" -> "en-us")
|
|
107
|
+
const localeArr = this.language.split('-');
|
|
108
|
+
if (localeArr && localeArr.length > 1) {
|
|
109
|
+
return (localeArr[0].toLowerCase() + '-' + localeArr[1].toLowerCase());
|
|
110
|
+
}
|
|
111
|
+
return this.language;
|
|
112
|
+
}
|
|
113
|
+
return 'en-us';
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Gets the Accept-Language header value for v12 onwards
|
|
117
|
+
* @returns {string} The Accept-Language header value
|
|
118
|
+
*/
|
|
119
|
+
getAcceptLanguageHeader() {
|
|
120
|
+
let language = this.language;
|
|
121
|
+
language =
|
|
122
|
+
language && typeof language === 'string'
|
|
123
|
+
? language.toLowerCase()
|
|
124
|
+
: 'en-us';
|
|
125
|
+
language = VALID_ACCEPT_LANGUAGES_HEADER_FOR_V12_ONWARDS[language]
|
|
126
|
+
? language
|
|
127
|
+
: 'en-us';
|
|
128
|
+
return language;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generates a unique cache key for a method and its parameters
|
|
132
|
+
* @param method - The method name
|
|
133
|
+
* @param params - The parameters object
|
|
134
|
+
* @returns The cache key
|
|
135
|
+
*/
|
|
136
|
+
getCacheKey(method, params) {
|
|
137
|
+
const sortedParams = Object.keys(params)
|
|
138
|
+
.sort()
|
|
139
|
+
.map((key) => `${key}=${params[key]}`)
|
|
140
|
+
.join('&');
|
|
141
|
+
return `${this.cacheKeyPrefix}${method}:${sortedParams}`;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets a value from the cache if it exists and hasn't expired
|
|
145
|
+
* @param key - The cache key
|
|
146
|
+
* @returns The cached value or null if not found/expired
|
|
147
|
+
*/
|
|
148
|
+
getFromCache(key) {
|
|
149
|
+
if (!this.cacheEnabled || !this.cacheAdapter) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
const entry = this.cacheAdapter.get(key);
|
|
153
|
+
if (!entry) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
// Check if entry has expired
|
|
157
|
+
const now = Date.now();
|
|
158
|
+
if (now - entry.timestamp > this.cacheTtl) {
|
|
159
|
+
// Entry expired, remove it
|
|
160
|
+
this.cacheAdapter.delete(key);
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
return entry.value;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Sets a value in the cache with the current timestamp
|
|
167
|
+
* @param key - The cache key
|
|
168
|
+
* @param value - The value to cache
|
|
169
|
+
*/
|
|
170
|
+
setInCache(key, value) {
|
|
171
|
+
if (!this.cacheEnabled || !this.cacheAdapter) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.cacheAdapter.set(key, {
|
|
175
|
+
value,
|
|
176
|
+
timestamp: Date.now(),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Gets the AI Agent details
|
|
181
|
+
* @param {GetAiAgentDetailsOptions} options - The options for the API call
|
|
182
|
+
* @returns {Promise<Object>} The AI Agent details
|
|
183
|
+
*/
|
|
184
|
+
async getAiAgentDetails(options) {
|
|
185
|
+
const { agentId, authToken } = options;
|
|
186
|
+
const token = await this.resolveAuthToken(authToken);
|
|
187
|
+
// Check cache first
|
|
188
|
+
const cacheKey = this.getCacheKey('getAiAgentDetails', { agentId });
|
|
189
|
+
const cached = this.getFromCache(cacheKey);
|
|
190
|
+
if (cached) {
|
|
191
|
+
return cached;
|
|
192
|
+
}
|
|
193
|
+
const url = `${this.apiDomain}/core/aiservices/v4/aiagent/details/agent/${agentId}`;
|
|
194
|
+
const response = await fetch(url, {
|
|
195
|
+
method: 'GET',
|
|
196
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
197
|
+
});
|
|
198
|
+
if (!response.ok) {
|
|
199
|
+
throw new Error(`Failed to fetch AI Agent details: ${response.status} ${response.statusText}`);
|
|
200
|
+
}
|
|
201
|
+
const result = await response.json();
|
|
202
|
+
// Cache the result
|
|
203
|
+
this.setInCache(cacheKey, result);
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Gets the AI Agent session
|
|
208
|
+
* @param {GetAiAgentSessionOptions} options - The options for the API call
|
|
209
|
+
* @returns {Promise<string>} The AI Agent session ID
|
|
210
|
+
*/
|
|
211
|
+
async getAiAgentSession(options) {
|
|
212
|
+
const { agentId, authToken } = options;
|
|
213
|
+
const token = await this.resolveAuthToken(authToken);
|
|
214
|
+
const url = `${this.apiDomain}/core/aiservices/v4/aiagent/chat/agent/${agentId}/session`;
|
|
215
|
+
const response = await fetch(url, {
|
|
216
|
+
method: 'GET',
|
|
217
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
218
|
+
});
|
|
219
|
+
if (!response.ok) {
|
|
220
|
+
throw new Error(`Failed to fetch AI Agent session: ${response.status} ${response.statusText}`);
|
|
221
|
+
}
|
|
222
|
+
const sessionResponse = await response.json();
|
|
223
|
+
return sessionResponse?.sessionId;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Gets the portal details
|
|
227
|
+
* @param {GetPortalDetailsOptions} options - The options for the API call
|
|
228
|
+
* @returns {Promise<Object>} The portal details
|
|
229
|
+
*/
|
|
230
|
+
async getPortalDetails(options) {
|
|
231
|
+
const { portalId, authToken, language } = options;
|
|
232
|
+
const token = await this.resolveAuthToken(authToken);
|
|
233
|
+
const lang = language || this.getLanguage();
|
|
234
|
+
// Check cache first
|
|
235
|
+
const cacheKey = this.getCacheKey('getPortalDetails', {
|
|
236
|
+
portalId,
|
|
237
|
+
language: lang,
|
|
238
|
+
});
|
|
239
|
+
const cached = this.getFromCache(cacheKey);
|
|
240
|
+
if (cached) {
|
|
241
|
+
return cached;
|
|
242
|
+
}
|
|
243
|
+
const portalDetailsURL = `${this.apiDomain}/knowledge/portalmgr/v3/internal/portals/${portalId}?$lang=${lang}`;
|
|
244
|
+
const response = await fetch(portalDetailsURL, {
|
|
245
|
+
headers: {
|
|
246
|
+
Authorization: token,
|
|
247
|
+
Accept: 'application/json',
|
|
248
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
if (!response.ok) {
|
|
252
|
+
throw new Error(`Failed to fetch portal details: ${response.status} ${response.statusText}`);
|
|
253
|
+
}
|
|
254
|
+
const portalDetails = await response.json();
|
|
255
|
+
const result = portalDetails.portal?.[0];
|
|
256
|
+
// Cache the result
|
|
257
|
+
this.setInCache(cacheKey, result);
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Gets the connected apps
|
|
262
|
+
* @param {GetConnectedAppsOptions} options - The options for the API call
|
|
263
|
+
* @returns {Promise<Object>} The connected apps
|
|
264
|
+
*/
|
|
265
|
+
async getConnectedApps(options) {
|
|
266
|
+
const { apiDomain, tenantId, agentId } = options;
|
|
267
|
+
const baseUrl = apiDomain || this.apiDomain;
|
|
268
|
+
// Check cache first
|
|
269
|
+
const cacheKey = this.getCacheKey('getConnectedApps', {
|
|
270
|
+
tenantId,
|
|
271
|
+
agentId,
|
|
272
|
+
});
|
|
273
|
+
const cached = this.getFromCache(cacheKey);
|
|
274
|
+
if (cached) {
|
|
275
|
+
return cached;
|
|
276
|
+
}
|
|
277
|
+
const url = `${baseUrl}/ai-agent-cc-connector/v1/tenants/${tenantId}/agents/${agentId}/apps`;
|
|
278
|
+
const response = await fetch(url, { method: 'GET' });
|
|
279
|
+
if (!response.ok) {
|
|
280
|
+
throw new Error(`Failed to fetch connected apps: ${response.status} ${response.statusText}`);
|
|
281
|
+
}
|
|
282
|
+
const result = await response.json();
|
|
283
|
+
// Cache the result
|
|
284
|
+
this.setInCache(cacheKey, result);
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Gets the previous transcript
|
|
289
|
+
* @param {GetPreviousTranscriptOptions} options - The options for the API call
|
|
290
|
+
* @returns {Promise<Array>} The previous transcript messages
|
|
291
|
+
*/
|
|
292
|
+
async getPreviousTranscript(options) {
|
|
293
|
+
const { apiDomain, agentId, sessionId, authToken } = options;
|
|
294
|
+
const token = await this.resolveAuthToken(authToken);
|
|
295
|
+
const baseUrl = apiDomain || this.apiDomain;
|
|
296
|
+
// Dummy deptId, deptId is required in v2 api
|
|
297
|
+
const deptId = '1000';
|
|
298
|
+
const url = `${baseUrl}/core/aiservices/v4/aiagent/details/department/${deptId}/agent/${agentId}/conversations/sessions/${sessionId}`;
|
|
299
|
+
const response = await fetch(url, {
|
|
300
|
+
method: 'GET',
|
|
301
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
302
|
+
});
|
|
303
|
+
if (!response.ok) {
|
|
304
|
+
throw new Error(`Failed to fetch previous transcript: ${response.status} ${response.statusText}`);
|
|
305
|
+
}
|
|
306
|
+
const data = await response.json();
|
|
307
|
+
return data?.messages || [];
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Gets the masking patterns for a department and channel
|
|
311
|
+
* @param {GetMaskingPatternsOptions} options - The options for the API call
|
|
312
|
+
* @returns {Promise<Object>} The masking patterns response
|
|
313
|
+
* @throws {Error} If the API request fails
|
|
314
|
+
*/
|
|
315
|
+
async getMaskingPatterns(options) {
|
|
316
|
+
const { departmentId, channel = 'chat', authToken } = options;
|
|
317
|
+
const token = await this.resolveAuthToken(authToken);
|
|
318
|
+
// Check cache first
|
|
319
|
+
const cacheKey = this.getCacheKey('getMaskingPatterns', {
|
|
320
|
+
departmentId,
|
|
321
|
+
channel,
|
|
322
|
+
});
|
|
323
|
+
const cached = this.getFromCache(cacheKey);
|
|
324
|
+
if (cached) {
|
|
325
|
+
return cached;
|
|
326
|
+
}
|
|
327
|
+
const url = `${this.apiDomain}/core/securitymgr/v4/departments/${departmentId}/maskingpatterns/${channel}`;
|
|
328
|
+
const response = await fetch(url, {
|
|
329
|
+
method: 'GET',
|
|
330
|
+
headers: {
|
|
331
|
+
Accept: 'application/json',
|
|
332
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
333
|
+
Authorization: `Bearer ${token}`,
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
if (!response.ok) {
|
|
337
|
+
throw new Error(`Failed to fetch masking patterns: ${response.status} ${response.statusText}`);
|
|
338
|
+
}
|
|
339
|
+
const result = await response.json();
|
|
340
|
+
// Cache the result
|
|
341
|
+
this.setInCache(cacheKey, result);
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Gets portals for the authenticated user via `GET .../knowledge/portalmgr/v3/myportals` (paginated).
|
|
346
|
+
*
|
|
347
|
+
* @param options - Options for the API call
|
|
348
|
+
* @returns Promise resolving to array of Portal objects
|
|
349
|
+
* @throws Error if the API request fails
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```typescript
|
|
353
|
+
* const portals = await apiHelper.getMyPortals({
|
|
354
|
+
* authToken: token,
|
|
355
|
+
* language: 'en-us',
|
|
356
|
+
* userId: 'user-123'
|
|
357
|
+
* });
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
async getMyPortals(options) {
|
|
361
|
+
const { authToken, language, userId, shortUrlTemplate } = options;
|
|
362
|
+
const token = await this.resolveAuthToken(authToken);
|
|
363
|
+
const lang = language || this.getLanguage();
|
|
364
|
+
const cacheParams = { lang, userId: userId ?? 'default' };
|
|
365
|
+
if (shortUrlTemplate)
|
|
366
|
+
cacheParams.shortUrlTemplate = shortUrlTemplate;
|
|
367
|
+
const cacheKey = this.getCacheKey('getMyPortals', cacheParams);
|
|
368
|
+
const cached = this.getFromCache(cacheKey);
|
|
369
|
+
if (cached) {
|
|
370
|
+
return cached;
|
|
371
|
+
}
|
|
372
|
+
const allPortals = await this.fetchPortalmgrV3PortalPages({
|
|
373
|
+
authToken: token,
|
|
374
|
+
lang,
|
|
375
|
+
shortUrlTemplate,
|
|
376
|
+
pathSegment: 'myportals',
|
|
377
|
+
});
|
|
378
|
+
this.setInCache(cacheKey, allPortals);
|
|
379
|
+
return allPortals;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Paginated GET .../knowledge/portalmgr/v3/{myportals|portals}
|
|
383
|
+
*/
|
|
384
|
+
async fetchPortalmgrV3PortalPages(params) {
|
|
385
|
+
const { authToken, lang, shortUrlTemplate, pathSegment } = params;
|
|
386
|
+
const baseUrl = `${this.apiDomain}/knowledge/portalmgr/v3/${pathSegment}`;
|
|
387
|
+
const buildPageUrl = (pageNum, pagesize) => {
|
|
388
|
+
const pageUrl = new URL(baseUrl);
|
|
389
|
+
pageUrl.searchParams.append('$lang', lang);
|
|
390
|
+
pageUrl.searchParams.append('$pagesize', pagesize);
|
|
391
|
+
pageUrl.searchParams.append('$pagenum', String(pageNum));
|
|
392
|
+
if (shortUrlTemplate?.trim()) {
|
|
393
|
+
pageUrl.searchParams.append('shortUrlTemplate', shortUrlTemplate.trim());
|
|
394
|
+
}
|
|
395
|
+
return pageUrl;
|
|
396
|
+
};
|
|
397
|
+
const firstUrl = buildPageUrl(1, '75');
|
|
398
|
+
const response = await fetch(firstUrl.toString(), {
|
|
399
|
+
method: 'GET',
|
|
400
|
+
headers: {
|
|
401
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
402
|
+
Authorization: `Bearer ${authToken}`,
|
|
403
|
+
Accept: 'application/json',
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
if (!response.ok) {
|
|
407
|
+
throw new Error(`Failed to fetch portals: ${response.status} ${response.statusText}`);
|
|
408
|
+
}
|
|
409
|
+
const data = await response.json();
|
|
410
|
+
let allPortals = data.portal || [];
|
|
411
|
+
if (data.paginationInfo && data.paginationInfo.count > data.paginationInfo.pagesize) {
|
|
412
|
+
const { count, pagesize } = data.paginationInfo;
|
|
413
|
+
const totalPages = Math.ceil(count / pagesize);
|
|
414
|
+
for (let pageNum = 2; pageNum <= totalPages; pageNum++) {
|
|
415
|
+
const pageUrl = buildPageUrl(pageNum, String(pagesize));
|
|
416
|
+
const pageResponse = await fetch(pageUrl.toString(), {
|
|
417
|
+
method: 'GET',
|
|
418
|
+
headers: {
|
|
419
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
420
|
+
Authorization: `Bearer ${authToken}`,
|
|
421
|
+
Accept: 'application/json',
|
|
422
|
+
},
|
|
423
|
+
});
|
|
424
|
+
if (pageResponse.ok) {
|
|
425
|
+
const pageData = await pageResponse.json();
|
|
426
|
+
allPortals = allPortals.concat(pageData.portal || []);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return allPortals;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Gets AI agents by portal and department.
|
|
434
|
+
*
|
|
435
|
+
* @param options - Options for the API call
|
|
436
|
+
* @returns Promise resolving to array of agent list items
|
|
437
|
+
* @throws Error if the API request fails
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* const agents = await apiHelper.getAgentsByPortal({
|
|
442
|
+
* departmentId: department.id,
|
|
443
|
+
* portalId: portal.id,
|
|
444
|
+
* agentType: 'contact-center',
|
|
445
|
+
* authToken: token
|
|
446
|
+
* });
|
|
447
|
+
* ```
|
|
448
|
+
*/
|
|
449
|
+
async getAgentsByPortal(options) {
|
|
450
|
+
const { departmentId, portalId, agentType = 'contact-center', authToken } = options;
|
|
451
|
+
const token = await this.resolveAuthToken(authToken);
|
|
452
|
+
const cacheKey = this.getCacheKey('getAgentsByPortal', {
|
|
453
|
+
departmentId,
|
|
454
|
+
portalId,
|
|
455
|
+
agentType,
|
|
456
|
+
});
|
|
457
|
+
const cached = this.getFromCache(cacheKey);
|
|
458
|
+
if (cached) {
|
|
459
|
+
return cached;
|
|
460
|
+
}
|
|
461
|
+
const url = `${this.apiDomain}/core/aiservices/v4/aiagent/details/department/${departmentId}/portal/${portalId}?agentType=${agentType}`;
|
|
462
|
+
const response = await fetch(url, {
|
|
463
|
+
method: 'GET',
|
|
464
|
+
headers: {
|
|
465
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
466
|
+
Authorization: `Bearer ${token}`,
|
|
467
|
+
Accept: 'application/json',
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
if (!response.ok) {
|
|
471
|
+
throw new Error(`Failed to fetch agents by portal: ${response.status} ${response.statusText}`);
|
|
472
|
+
}
|
|
473
|
+
const result = await response.json();
|
|
474
|
+
const agentList = Array.isArray(result) ? result : [];
|
|
475
|
+
this.setInCache(cacheKey, agentList);
|
|
476
|
+
return agentList;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Gets user profiles for a portal.
|
|
480
|
+
*
|
|
481
|
+
* @param options - Options for the API call
|
|
482
|
+
* @returns Promise resolving to array of UserProfile objects
|
|
483
|
+
* @throws Error if the API request fails
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* const profiles = await apiHelper.getUserProfiles({
|
|
488
|
+
* portalId: portal.id,
|
|
489
|
+
* authToken: token
|
|
490
|
+
* });
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
493
|
+
async getUserProfiles(options) {
|
|
494
|
+
const { portalId, authToken } = options;
|
|
495
|
+
const token = await this.resolveAuthToken(authToken);
|
|
496
|
+
const cacheKey = this.getCacheKey('getUserProfiles', { portalId });
|
|
497
|
+
const cached = this.getFromCache(cacheKey);
|
|
498
|
+
if (cached) {
|
|
499
|
+
return cached;
|
|
500
|
+
}
|
|
501
|
+
const url = `${this.apiDomain}/knowledge/portalmgr/v3/portals/${portalId}/userprofiles`;
|
|
502
|
+
const response = await fetch(url, {
|
|
503
|
+
method: 'GET',
|
|
504
|
+
headers: {
|
|
505
|
+
Accept: 'application/json',
|
|
506
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
507
|
+
Authorization: `Bearer ${token}`,
|
|
508
|
+
},
|
|
509
|
+
});
|
|
510
|
+
if (!response.ok) {
|
|
511
|
+
throw new Error(`Failed to fetch user profiles: ${response.status} ${response.statusText}`);
|
|
512
|
+
}
|
|
513
|
+
if (response.status === 204) {
|
|
514
|
+
this.setInCache(cacheKey, []);
|
|
515
|
+
return [];
|
|
516
|
+
}
|
|
517
|
+
const data = await response.json();
|
|
518
|
+
const profiles = data.profile || [];
|
|
519
|
+
this.setInCache(cacheKey, profiles);
|
|
520
|
+
return profiles;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Selects a user profile for a portal.
|
|
524
|
+
* Persists the selection on the server.
|
|
525
|
+
*
|
|
526
|
+
* @param options - Options for the API call
|
|
527
|
+
* @returns Promise resolving when selection is complete
|
|
528
|
+
* @throws Error if the API request fails
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```typescript
|
|
532
|
+
* await apiHelper.selectUserProfile({
|
|
533
|
+
* portalId: portal.id,
|
|
534
|
+
* profileId: profile.id,
|
|
535
|
+
* authToken: token
|
|
536
|
+
* });
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
async selectUserProfile(options) {
|
|
540
|
+
const { portalId, profileId, authToken } = options;
|
|
541
|
+
const token = await this.resolveAuthToken(authToken);
|
|
542
|
+
const url = `${this.apiDomain}/knowledge/portalmgr/v3/portals/${portalId}/userprofiles/${profileId}/select`;
|
|
543
|
+
const response = await fetch(url, {
|
|
544
|
+
method: 'PUT',
|
|
545
|
+
headers: {
|
|
546
|
+
'Accept-Language': this.getAcceptLanguageHeader(),
|
|
547
|
+
Accept: 'application/json',
|
|
548
|
+
Authorization: `Bearer ${token}`,
|
|
549
|
+
},
|
|
550
|
+
});
|
|
551
|
+
if (!response.ok) {
|
|
552
|
+
throw new Error(`Failed to select user profile: ${response.status} ${response.statusText}`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Clears all cached entries for this ApiHelper instance
|
|
557
|
+
*/
|
|
558
|
+
clearCache() {
|
|
559
|
+
if (this.cacheAdapter) {
|
|
560
|
+
this.cacheAdapter.clear(this.cacheKeyPrefix);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Invalidates cached entries matching a specific pattern or method name
|
|
565
|
+
* @param pattern - Optional pattern to match (e.g., 'getAiAgentDetails', 'getPortalDetails')
|
|
566
|
+
* If not provided, clears all cache entries for this instance
|
|
567
|
+
*/
|
|
568
|
+
invalidateCache(pattern) {
|
|
569
|
+
if (!this.cacheAdapter) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (!pattern) {
|
|
573
|
+
this.clearCache();
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
// Get all keys matching the pattern
|
|
577
|
+
const keys = this.cacheAdapter.keys(this.cacheKeyPrefix);
|
|
578
|
+
for (const key of keys) {
|
|
579
|
+
if (key.includes(pattern)) {
|
|
580
|
+
this.cacheAdapter.delete(key);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Fetches the authenticated user's details (for agent/user auth type).
|
|
586
|
+
* Returns null on failure so that initialization is not blocked.
|
|
587
|
+
*/
|
|
588
|
+
async getUserDetails(options) {
|
|
589
|
+
const { authToken } = options;
|
|
590
|
+
const token = await this.resolveAuthToken(authToken);
|
|
591
|
+
const url = `${this.apiDomain}/knowledge/portalmgr/v3/portals/user`;
|
|
592
|
+
try {
|
|
593
|
+
const response = await fetch(url, {
|
|
594
|
+
method: 'GET',
|
|
595
|
+
credentials: 'include',
|
|
596
|
+
headers: {
|
|
597
|
+
Authorization: `Bearer ${token}`,
|
|
598
|
+
Accept: 'application/json',
|
|
599
|
+
"Accept-Language": this.getAcceptLanguageHeader(),
|
|
600
|
+
},
|
|
601
|
+
});
|
|
602
|
+
if (!response.ok) {
|
|
603
|
+
return null;
|
|
604
|
+
}
|
|
605
|
+
const data = (await response.json())?.user?.[0];
|
|
606
|
+
return data ?? null;
|
|
607
|
+
}
|
|
608
|
+
catch {
|
|
609
|
+
return null;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Fetches the authenticated customer's details (for customer auth type).
|
|
614
|
+
* Returns null on failure so that initialization is not blocked.
|
|
615
|
+
*/
|
|
616
|
+
async getCustomerDetails(options) {
|
|
617
|
+
const { authToken } = options;
|
|
618
|
+
const token = await this.resolveAuthToken(authToken);
|
|
619
|
+
const url = `${this.apiDomain}/core/customermgr/v3/internal/customer?$attribute=all`;
|
|
620
|
+
try {
|
|
621
|
+
const response = await fetch(url, {
|
|
622
|
+
method: 'GET',
|
|
623
|
+
credentials: 'include',
|
|
624
|
+
headers: {
|
|
625
|
+
Authorization: `Bearer ${token}`,
|
|
626
|
+
Accept: 'application/json',
|
|
627
|
+
"Accept-Language": this.getAcceptLanguageHeader(),
|
|
628
|
+
},
|
|
629
|
+
});
|
|
630
|
+
if (!response.ok) {
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
const data = (await response.json())?.customer?.[0];
|
|
634
|
+
return data ?? null;
|
|
635
|
+
}
|
|
636
|
+
catch {
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Gets the deployment information for a given domain
|
|
642
|
+
* @param {string} domain - The domain to get the deployment information for
|
|
643
|
+
* @returns {Promise<Object>} The deployment information
|
|
644
|
+
*/
|
|
645
|
+
static async getDeploymentInfo(domain) {
|
|
646
|
+
const cacheAdapter = ApiHelper.getStaticCacheAdapter();
|
|
647
|
+
const cacheKey = `${ApiHelper.staticCachePrefix}deployment-info-${domain}`;
|
|
648
|
+
// Check session storage cache first
|
|
649
|
+
if (cacheAdapter) {
|
|
650
|
+
const cached = cacheAdapter.get(cacheKey);
|
|
651
|
+
if (cached) {
|
|
652
|
+
const now = Date.now();
|
|
653
|
+
if (now - cached.timestamp <= ApiHelper.staticCacheTtl) {
|
|
654
|
+
return cached.value;
|
|
655
|
+
}
|
|
656
|
+
// Entry expired, remove it
|
|
657
|
+
cacheAdapter.delete(cacheKey);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
const url = `${domain}/system/deploymentInfo`;
|
|
661
|
+
const response = await fetch(url, { method: 'GET' });
|
|
662
|
+
if (!response.ok) {
|
|
663
|
+
throw new Error(`Failed to fetch deployment info: ${response.status} ${response.statusText}`);
|
|
664
|
+
}
|
|
665
|
+
const result = await response.json();
|
|
666
|
+
// Cache the result in session storage
|
|
667
|
+
if (cacheAdapter) {
|
|
668
|
+
cacheAdapter.set(cacheKey, {
|
|
669
|
+
value: result,
|
|
670
|
+
timestamp: Date.now(),
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
return result;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Clears the static deployment info cache
|
|
677
|
+
*/
|
|
678
|
+
static clearDeploymentInfoCache() {
|
|
679
|
+
const cacheAdapter = ApiHelper.getStaticCacheAdapter();
|
|
680
|
+
if (cacheAdapter) {
|
|
681
|
+
cacheAdapter.clear(ApiHelper.staticCachePrefix);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
// Static cache adapter for getDeploymentInfo (uses session storage)
|
|
686
|
+
ApiHelper.staticCacheAdapter = null;
|
|
687
|
+
ApiHelper.staticCacheTtl = DEFAULT_CACHE_TTL;
|
|
688
|
+
ApiHelper.staticCachePrefix = 'egain_aiagent_static_';
|
|
689
|
+
//# sourceMappingURL=ApiHelper.js.map
|