iframe-pubsub 1.0.1 → 1.0.3

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/index.d.mts CHANGED
@@ -10,61 +10,18 @@ interface IMessage {
10
10
  to: string;
11
11
  payload: any;
12
12
  }
13
- declare enum AIChatNameEnum {
14
- AI_CHAT_CLIENT_ID = "aichat",
15
- AI_CHAT_INTERNAL_COMM_TYPE = "pubsub",
16
- SET_PARENT_NAME = "setParentName",
17
- SET_USER_ID = "setUserId",
18
- SET_CUSTOM_HEADERS = "setCustomHeaders",
19
- SET_CUSTOM_PAYLOAD = "setCustomPayload",
20
- SET_SUGGESTIONS = "setSuggestions",
21
- SHOW_SUGGESTIONS = "showSuggestions",
22
- SHOW_CHAT_MESSAGE = "showChatMessage",
23
- CONFIRM_ACTION = "confirmAction",
24
- SHOW_OPTIONS = "showOptions",
25
- SEND_CHAT_PROMPT = "sendChatPrompt",
26
- DEFAULT = "default"
13
+ /**
14
+ * Message to register a page or component.
15
+ *
16
+ * @property {string} type - The type of the message.
17
+ * @property {string} pageId - The ID of the page or component.
18
+ */
19
+ interface IRegistrationMessage {
20
+ type: 'REGISTER' | 'UNREGISTER';
21
+ pageId: string;
27
22
  }
28
23
  type MessageCallback = (message: IMessage) => void | Promise<void>;
29
- declare class PubSub {
30
- private static instance;
31
- private subscribers;
32
- private mainCallback?;
33
- private constructor();
34
- static getInstance(): PubSub;
35
- onMessage(callback: MessageCallback): void;
36
- /**
37
- * Registers a callback function to be called when a message is received.
38
- *
39
- * @param pageId The ID of the page or component to register for.
40
- * @param callback The callback function to be called when a message is received.
41
- */
42
- register(pageId: string, callback: MessageCallback): void;
43
- /**
44
- * Unregister a page or component from the pubsub.
45
- * Should unregister the registered page or component if it has been removed.
46
- *
47
- * @param pageId The ID of the page or component to unregister from.
48
- */
49
- unregister(pageId: string): void;
50
- sendMessage(message: IMessage): void;
51
- /**
52
- * Try to send a message to a client with retry logic.
53
- * Will retry up to 3 times with 1 second delay between retries.
54
- *
55
- * @param message The message to send.
56
- * @param retryCount The current retry count.
57
- */
58
- private trySendMessageWithRetry;
59
- private handleMessage;
60
- /**
61
- * Check if a client with the given ID exists in the PubSub system.
62
- *
63
- * @param clientId The ID of the client to check.
64
- * @returns True if the client exists, false otherwise.
65
- */
66
- isClientExists(clientId: string): boolean;
67
- }
24
+
68
25
  declare class Client {
69
26
  private pageId;
70
27
  private callback?;
@@ -93,6 +50,53 @@ declare class Client {
93
50
  * @param payload The payload of the message.
94
51
  */
95
52
  sendMessage(to: string, payload: any): void;
53
+ /**
54
+ * Check if a client with the given ID exists in the PubSub system.
55
+ *
56
+ * @param clientId The ID of the client to check.
57
+ * @param maxRetries Maximum number of retries. Default is 3.
58
+ * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
59
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
60
+ */
61
+ checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
62
+ /**
63
+ * Check if a client with the given ID exists in the PubSub system with retry mechanism.
64
+ * Will retry up to 3 times with 1 second delay between retries.
65
+ *
66
+ * @param clientId The ID of the client to check.
67
+ * @param retryCount The current retry count.
68
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
69
+ * @private
70
+ */
71
+ private checkClientExistsWithRetry;
72
+ private handleMessage;
73
+ }
74
+
75
+ declare enum AIChatNameEnum {
76
+ AI_CHAT_CLIENT_ID = "aichat",
77
+ AI_CHAT_INTERNAL_COMM_TYPE = "pubsub",
78
+ SET_PARENT_NAME = "setParentName",
79
+ SET_USER_ID = "setUserId",
80
+ SET_CUSTOM_HEADERS = "setCustomHeaders",
81
+ SET_CUSTOM_PAYLOAD = "setCustomPayload",
82
+ SET_SUGGESTIONS = "setSuggestions",
83
+ SHOW_SUGGESTIONS = "showSuggestions",
84
+ SHOW_CHAT_MESSAGE = "showChatMessage",
85
+ SHOW_ERROR_MESSAGE = "showErrorMessage",
86
+ SHOW_UNKNOWN_MESSAGE = "showUnknownMessage",
87
+ CONFIRM_ACTION = "confirmAction",
88
+ SHOW_OPTIONS = "showOptions",
89
+ SEND_CHAT_PROMPT = "sendChatPrompt",
90
+ GENERIC_ACTION = "genericAction",
91
+ DEFAULT = "default"
92
+ }
93
+ declare class AIChatClient extends Client {
94
+ /**
95
+ * Create a new client instance.
96
+ *
97
+ * @param pageId The ID of the page or component to register to.
98
+ */
99
+ constructor(pageId: string);
96
100
  private sendAIChatMethod;
97
101
  /**
98
102
  * Set the parent name then the AI Chat can know who should it communicate with.
@@ -155,25 +159,55 @@ declare class Client {
155
159
  */
156
160
  sendChatPrompt(prompt: string): void;
157
161
  /**
158
- * Check if a client with the given ID exists in the PubSub system.
162
+ * Show an error message in the AI Chat component.
159
163
  *
160
- * @param clientId The ID of the client to check.
161
- * @param maxRetries Maximum number of retries. Default is 3.
162
- * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
163
- * @returns A Promise that resolves to true if the client exists, false otherwise.
164
+ * @param message The error message to show.
164
165
  */
165
- checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
166
+ showErrorMessage(message: string): void;
166
167
  /**
167
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
168
+ * Show an unknown error message in the AI Chat component.
169
+ */
170
+ showUnknownMessage(): void;
171
+ }
172
+
173
+ declare class PubSub {
174
+ private static instance;
175
+ private subscribers;
176
+ private mainCallback?;
177
+ private constructor();
178
+ static getInstance(): PubSub;
179
+ onMessage(callback: MessageCallback): void;
180
+ /**
181
+ * Registers a callback function to be called when a message is received.
182
+ *
183
+ * @param pageId The ID of the page or component to register for.
184
+ * @param callback The callback function to be called when a message is received.
185
+ */
186
+ register(pageId: string, callback: MessageCallback): void;
187
+ /**
188
+ * Unregister a page or component from the pubsub.
189
+ * Should unregister the registered page or component if it has been removed.
190
+ *
191
+ * @param pageId The ID of the page or component to unregister from.
192
+ */
193
+ unregister(pageId: string): void;
194
+ sendMessage(message: IMessage): void;
195
+ /**
196
+ * Try to send a message to a client with retry logic.
168
197
  * Will retry up to 3 times with 1 second delay between retries.
169
198
  *
170
- * @param clientId The ID of the client to check.
199
+ * @param message The message to send.
171
200
  * @param retryCount The current retry count.
172
- * @returns A Promise that resolves to true if the client exists, false otherwise.
173
- * @private
174
201
  */
175
- private checkClientExistsWithRetry;
202
+ private trySendMessageWithRetry;
176
203
  private handleMessage;
204
+ /**
205
+ * Check if a client with the given ID exists in the PubSub system.
206
+ *
207
+ * @param clientId The ID of the client to check.
208
+ * @returns True if the client exists, false otherwise.
209
+ */
210
+ isClientExists(clientId: string): boolean;
177
211
  }
178
212
 
179
- export { AIChatNameEnum, Client, type IMessage, type MessageCallback, PubSub };
213
+ export { AIChatClient, AIChatNameEnum, Client, type IMessage, type IRegistrationMessage, type MessageCallback, PubSub };
package/dist/index.d.ts CHANGED
@@ -10,61 +10,18 @@ interface IMessage {
10
10
  to: string;
11
11
  payload: any;
12
12
  }
13
- declare enum AIChatNameEnum {
14
- AI_CHAT_CLIENT_ID = "aichat",
15
- AI_CHAT_INTERNAL_COMM_TYPE = "pubsub",
16
- SET_PARENT_NAME = "setParentName",
17
- SET_USER_ID = "setUserId",
18
- SET_CUSTOM_HEADERS = "setCustomHeaders",
19
- SET_CUSTOM_PAYLOAD = "setCustomPayload",
20
- SET_SUGGESTIONS = "setSuggestions",
21
- SHOW_SUGGESTIONS = "showSuggestions",
22
- SHOW_CHAT_MESSAGE = "showChatMessage",
23
- CONFIRM_ACTION = "confirmAction",
24
- SHOW_OPTIONS = "showOptions",
25
- SEND_CHAT_PROMPT = "sendChatPrompt",
26
- DEFAULT = "default"
13
+ /**
14
+ * Message to register a page or component.
15
+ *
16
+ * @property {string} type - The type of the message.
17
+ * @property {string} pageId - The ID of the page or component.
18
+ */
19
+ interface IRegistrationMessage {
20
+ type: 'REGISTER' | 'UNREGISTER';
21
+ pageId: string;
27
22
  }
28
23
  type MessageCallback = (message: IMessage) => void | Promise<void>;
29
- declare class PubSub {
30
- private static instance;
31
- private subscribers;
32
- private mainCallback?;
33
- private constructor();
34
- static getInstance(): PubSub;
35
- onMessage(callback: MessageCallback): void;
36
- /**
37
- * Registers a callback function to be called when a message is received.
38
- *
39
- * @param pageId The ID of the page or component to register for.
40
- * @param callback The callback function to be called when a message is received.
41
- */
42
- register(pageId: string, callback: MessageCallback): void;
43
- /**
44
- * Unregister a page or component from the pubsub.
45
- * Should unregister the registered page or component if it has been removed.
46
- *
47
- * @param pageId The ID of the page or component to unregister from.
48
- */
49
- unregister(pageId: string): void;
50
- sendMessage(message: IMessage): void;
51
- /**
52
- * Try to send a message to a client with retry logic.
53
- * Will retry up to 3 times with 1 second delay between retries.
54
- *
55
- * @param message The message to send.
56
- * @param retryCount The current retry count.
57
- */
58
- private trySendMessageWithRetry;
59
- private handleMessage;
60
- /**
61
- * Check if a client with the given ID exists in the PubSub system.
62
- *
63
- * @param clientId The ID of the client to check.
64
- * @returns True if the client exists, false otherwise.
65
- */
66
- isClientExists(clientId: string): boolean;
67
- }
24
+
68
25
  declare class Client {
69
26
  private pageId;
70
27
  private callback?;
@@ -93,6 +50,53 @@ declare class Client {
93
50
  * @param payload The payload of the message.
94
51
  */
95
52
  sendMessage(to: string, payload: any): void;
53
+ /**
54
+ * Check if a client with the given ID exists in the PubSub system.
55
+ *
56
+ * @param clientId The ID of the client to check.
57
+ * @param maxRetries Maximum number of retries. Default is 3.
58
+ * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
59
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
60
+ */
61
+ checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
62
+ /**
63
+ * Check if a client with the given ID exists in the PubSub system with retry mechanism.
64
+ * Will retry up to 3 times with 1 second delay between retries.
65
+ *
66
+ * @param clientId The ID of the client to check.
67
+ * @param retryCount The current retry count.
68
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
69
+ * @private
70
+ */
71
+ private checkClientExistsWithRetry;
72
+ private handleMessage;
73
+ }
74
+
75
+ declare enum AIChatNameEnum {
76
+ AI_CHAT_CLIENT_ID = "aichat",
77
+ AI_CHAT_INTERNAL_COMM_TYPE = "pubsub",
78
+ SET_PARENT_NAME = "setParentName",
79
+ SET_USER_ID = "setUserId",
80
+ SET_CUSTOM_HEADERS = "setCustomHeaders",
81
+ SET_CUSTOM_PAYLOAD = "setCustomPayload",
82
+ SET_SUGGESTIONS = "setSuggestions",
83
+ SHOW_SUGGESTIONS = "showSuggestions",
84
+ SHOW_CHAT_MESSAGE = "showChatMessage",
85
+ SHOW_ERROR_MESSAGE = "showErrorMessage",
86
+ SHOW_UNKNOWN_MESSAGE = "showUnknownMessage",
87
+ CONFIRM_ACTION = "confirmAction",
88
+ SHOW_OPTIONS = "showOptions",
89
+ SEND_CHAT_PROMPT = "sendChatPrompt",
90
+ GENERIC_ACTION = "genericAction",
91
+ DEFAULT = "default"
92
+ }
93
+ declare class AIChatClient extends Client {
94
+ /**
95
+ * Create a new client instance.
96
+ *
97
+ * @param pageId The ID of the page or component to register to.
98
+ */
99
+ constructor(pageId: string);
96
100
  private sendAIChatMethod;
97
101
  /**
98
102
  * Set the parent name then the AI Chat can know who should it communicate with.
@@ -155,25 +159,55 @@ declare class Client {
155
159
  */
156
160
  sendChatPrompt(prompt: string): void;
157
161
  /**
158
- * Check if a client with the given ID exists in the PubSub system.
162
+ * Show an error message in the AI Chat component.
159
163
  *
160
- * @param clientId The ID of the client to check.
161
- * @param maxRetries Maximum number of retries. Default is 3.
162
- * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
163
- * @returns A Promise that resolves to true if the client exists, false otherwise.
164
+ * @param message The error message to show.
164
165
  */
165
- checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
166
+ showErrorMessage(message: string): void;
166
167
  /**
167
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
168
+ * Show an unknown error message in the AI Chat component.
169
+ */
170
+ showUnknownMessage(): void;
171
+ }
172
+
173
+ declare class PubSub {
174
+ private static instance;
175
+ private subscribers;
176
+ private mainCallback?;
177
+ private constructor();
178
+ static getInstance(): PubSub;
179
+ onMessage(callback: MessageCallback): void;
180
+ /**
181
+ * Registers a callback function to be called when a message is received.
182
+ *
183
+ * @param pageId The ID of the page or component to register for.
184
+ * @param callback The callback function to be called when a message is received.
185
+ */
186
+ register(pageId: string, callback: MessageCallback): void;
187
+ /**
188
+ * Unregister a page or component from the pubsub.
189
+ * Should unregister the registered page or component if it has been removed.
190
+ *
191
+ * @param pageId The ID of the page or component to unregister from.
192
+ */
193
+ unregister(pageId: string): void;
194
+ sendMessage(message: IMessage): void;
195
+ /**
196
+ * Try to send a message to a client with retry logic.
168
197
  * Will retry up to 3 times with 1 second delay between retries.
169
198
  *
170
- * @param clientId The ID of the client to check.
199
+ * @param message The message to send.
171
200
  * @param retryCount The current retry count.
172
- * @returns A Promise that resolves to true if the client exists, false otherwise.
173
- * @private
174
201
  */
175
- private checkClientExistsWithRetry;
202
+ private trySendMessageWithRetry;
176
203
  private handleMessage;
204
+ /**
205
+ * Check if a client with the given ID exists in the PubSub system.
206
+ *
207
+ * @param clientId The ID of the client to check.
208
+ * @returns True if the client exists, false otherwise.
209
+ */
210
+ isClientExists(clientId: string): boolean;
177
211
  }
178
212
 
179
- export { AIChatNameEnum, Client, type IMessage, type MessageCallback, PubSub };
213
+ export { AIChatClient, AIChatNameEnum, Client, type IMessage, type IRegistrationMessage, type MessageCallback, PubSub };
package/dist/index.js CHANGED
@@ -22,27 +22,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
22
22
  // src/index.ts
23
23
  var index_exports = {};
24
24
  __export(index_exports, {
25
+ AIChatClient: () => AIChatClient,
25
26
  AIChatNameEnum: () => AIChatNameEnum,
26
27
  Client: () => Client,
27
28
  PubSub: () => PubSub
28
29
  });
29
30
  module.exports = __toCommonJS(index_exports);
30
- var AIChatNameEnum = /* @__PURE__ */ ((AIChatNameEnum2) => {
31
- AIChatNameEnum2["AI_CHAT_CLIENT_ID"] = "aichat";
32
- AIChatNameEnum2["AI_CHAT_INTERNAL_COMM_TYPE"] = "pubsub";
33
- AIChatNameEnum2["SET_PARENT_NAME"] = "setParentName";
34
- AIChatNameEnum2["SET_USER_ID"] = "setUserId";
35
- AIChatNameEnum2["SET_CUSTOM_HEADERS"] = "setCustomHeaders";
36
- AIChatNameEnum2["SET_CUSTOM_PAYLOAD"] = "setCustomPayload";
37
- AIChatNameEnum2["SET_SUGGESTIONS"] = "setSuggestions";
38
- AIChatNameEnum2["SHOW_SUGGESTIONS"] = "showSuggestions";
39
- AIChatNameEnum2["SHOW_CHAT_MESSAGE"] = "showChatMessage";
40
- AIChatNameEnum2["CONFIRM_ACTION"] = "confirmAction";
41
- AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
42
- AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
43
- AIChatNameEnum2["DEFAULT"] = "default";
44
- return AIChatNameEnum2;
45
- })(AIChatNameEnum || {});
31
+
32
+ // src/PubSub.ts
46
33
  var _PubSub = class _PubSub {
47
34
  constructor() {
48
35
  __publicField(this, "subscribers");
@@ -160,6 +147,8 @@ var _PubSub = class _PubSub {
160
147
  };
161
148
  __publicField(_PubSub, "instance");
162
149
  var PubSub = _PubSub;
150
+
151
+ // src/Client.ts
163
152
  var Client = class {
164
153
  /**
165
154
  * Create a new client instance.
@@ -223,6 +212,119 @@ var Client = class {
223
212
  this.pubsub.sendMessage(message);
224
213
  }
225
214
  }
215
+ /**
216
+ * Check if a client with the given ID exists in the PubSub system.
217
+ *
218
+ * @param clientId The ID of the client to check.
219
+ * @param maxRetries Maximum number of retries. Default is 3.
220
+ * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
221
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
222
+ */
223
+ checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
224
+ return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
225
+ }
226
+ /**
227
+ * Check if a client with the given ID exists in the PubSub system with retry mechanism.
228
+ * Will retry up to 3 times with 1 second delay between retries.
229
+ *
230
+ * @param clientId The ID of the client to check.
231
+ * @param retryCount The current retry count.
232
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
233
+ * @private
234
+ */
235
+ checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
236
+ return new Promise((resolve) => {
237
+ if (this.isIframe) {
238
+ const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
239
+ const messageHandler = (event) => {
240
+ const data = event.data;
241
+ if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
242
+ window.removeEventListener("message", messageHandler);
243
+ clearTimeout(removeHandlerTimeout);
244
+ if (data.exists) {
245
+ resolve(true);
246
+ } else if (retryCount < maxRetries - 1) {
247
+ setTimeout(() => {
248
+ this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
249
+ }, retryInterval);
250
+ } else {
251
+ resolve(false);
252
+ }
253
+ }
254
+ };
255
+ window.addEventListener("message", messageHandler);
256
+ window.parent.postMessage({
257
+ type: "CLIENT_EXISTS_CHECK",
258
+ clientId,
259
+ requestId,
260
+ from: this.pageId
261
+ }, "*");
262
+ const removeHandlerTimeout = setTimeout(() => {
263
+ window.removeEventListener("message", messageHandler);
264
+ resolve(false);
265
+ }, retryInterval + 1e3);
266
+ } else {
267
+ const exists = this.pubsub.isClientExists(clientId);
268
+ if (exists) {
269
+ resolve(true);
270
+ } else if (retryCount < maxRetries - 1) {
271
+ setTimeout(() => {
272
+ this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
273
+ }, retryInterval);
274
+ } else {
275
+ resolve(false);
276
+ }
277
+ }
278
+ });
279
+ }
280
+ async handleMessage(event) {
281
+ let message;
282
+ if (event.data) {
283
+ const evt = event;
284
+ message = evt.data;
285
+ } else {
286
+ message = event;
287
+ }
288
+ if (!message || !message.from || !message.to || message.to !== this.pageId) return;
289
+ if (this.callback) {
290
+ try {
291
+ await this.callback(message);
292
+ } catch (error) {
293
+ console.error(`Client ${this.pageId} failed to process message:`, error);
294
+ }
295
+ }
296
+ }
297
+ };
298
+
299
+ // src/aichat/AIChatClient.ts
300
+ var AIChatNameEnum = /* @__PURE__ */ ((AIChatNameEnum2) => {
301
+ AIChatNameEnum2["AI_CHAT_CLIENT_ID"] = "aichat";
302
+ AIChatNameEnum2["AI_CHAT_INTERNAL_COMM_TYPE"] = "pubsub";
303
+ AIChatNameEnum2["SET_PARENT_NAME"] = "setParentName";
304
+ AIChatNameEnum2["SET_USER_ID"] = "setUserId";
305
+ AIChatNameEnum2["SET_CUSTOM_HEADERS"] = "setCustomHeaders";
306
+ AIChatNameEnum2["SET_CUSTOM_PAYLOAD"] = "setCustomPayload";
307
+ AIChatNameEnum2["SET_SUGGESTIONS"] = "setSuggestions";
308
+ AIChatNameEnum2["SHOW_SUGGESTIONS"] = "showSuggestions";
309
+ AIChatNameEnum2["SHOW_CHAT_MESSAGE"] = "showChatMessage";
310
+ AIChatNameEnum2["SHOW_ERROR_MESSAGE"] = "showErrorMessage";
311
+ AIChatNameEnum2["SHOW_UNKNOWN_MESSAGE"] = "showUnknownMessage";
312
+ AIChatNameEnum2["CONFIRM_ACTION"] = "confirmAction";
313
+ AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
314
+ AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
315
+ AIChatNameEnum2["GENERIC_ACTION"] = "genericAction";
316
+ AIChatNameEnum2["DEFAULT"] = "default";
317
+ return AIChatNameEnum2;
318
+ })(AIChatNameEnum || {});
319
+ var AIChatClient = class extends Client {
320
+ /**
321
+ * Create a new client instance.
322
+ *
323
+ * @param pageId The ID of the page or component to register to.
324
+ */
325
+ constructor(pageId) {
326
+ super(pageId);
327
+ }
226
328
  sendAIChatMethod(methodName, arg) {
227
329
  this.sendMessage(
228
330
  "aichat" /* AI_CHAT_CLIENT_ID */,
@@ -316,90 +418,23 @@ var Client = class {
316
418
  this.sendAIChatMethod("sendChatPrompt" /* SEND_CHAT_PROMPT */, prompt);
317
419
  }
318
420
  /**
319
- * Check if a client with the given ID exists in the PubSub system.
421
+ * Show an error message in the AI Chat component.
320
422
  *
321
- * @param clientId The ID of the client to check.
322
- * @param maxRetries Maximum number of retries. Default is 3.
323
- * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
324
- * @returns A Promise that resolves to true if the client exists, false otherwise.
423
+ * @param message The error message to show.
325
424
  */
326
- checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
327
- return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
425
+ showErrorMessage(message) {
426
+ this.sendAIChatMethod("showErrorMessage" /* SHOW_ERROR_MESSAGE */, message);
328
427
  }
329
428
  /**
330
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
331
- * Will retry up to 3 times with 1 second delay between retries.
332
- *
333
- * @param clientId The ID of the client to check.
334
- * @param retryCount The current retry count.
335
- * @returns A Promise that resolves to true if the client exists, false otherwise.
336
- * @private
429
+ * Show an unknown error message in the AI Chat component.
337
430
  */
338
- checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
339
- return new Promise((resolve) => {
340
- if (this.isIframe) {
341
- const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
342
- const messageHandler = (event) => {
343
- const data = event.data;
344
- if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
345
- window.removeEventListener("message", messageHandler);
346
- clearTimeout(removeHandlerTimeout);
347
- if (data.exists) {
348
- resolve(true);
349
- } else if (retryCount < maxRetries - 1) {
350
- setTimeout(() => {
351
- this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
352
- }, retryInterval);
353
- } else {
354
- resolve(false);
355
- }
356
- }
357
- };
358
- window.addEventListener("message", messageHandler);
359
- window.parent.postMessage({
360
- type: "CLIENT_EXISTS_CHECK",
361
- clientId,
362
- requestId,
363
- from: this.pageId
364
- }, "*");
365
- const removeHandlerTimeout = setTimeout(() => {
366
- window.removeEventListener("message", messageHandler);
367
- resolve(false);
368
- }, retryInterval + 1e3);
369
- } else {
370
- const exists = this.pubsub.isClientExists(clientId);
371
- if (exists) {
372
- resolve(true);
373
- } else if (retryCount < maxRetries - 1) {
374
- setTimeout(() => {
375
- this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
376
- }, retryInterval);
377
- } else {
378
- resolve(false);
379
- }
380
- }
381
- });
382
- }
383
- async handleMessage(event) {
384
- let message;
385
- if (event.data) {
386
- const evt = event;
387
- message = evt.data;
388
- } else {
389
- message = event;
390
- }
391
- if (!message || !message.from || !message.to || message.to !== this.pageId) return;
392
- if (this.callback) {
393
- try {
394
- await this.callback(message);
395
- } catch (error) {
396
- console.error(`Client ${this.pageId} failed to process message:`, error);
397
- }
398
- }
431
+ showUnknownMessage() {
432
+ this.sendAIChatMethod("showUnknownMessage" /* SHOW_UNKNOWN_MESSAGE */, "unknown_error");
399
433
  }
400
434
  };
401
435
  // Annotate the CommonJS export names for ESM import in node:
402
436
  0 && (module.exports = {
437
+ AIChatClient,
403
438
  AIChatNameEnum,
404
439
  Client,
405
440
  PubSub
package/dist/index.mjs CHANGED
@@ -2,23 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
 
5
- // src/index.ts
6
- var AIChatNameEnum = /* @__PURE__ */ ((AIChatNameEnum2) => {
7
- AIChatNameEnum2["AI_CHAT_CLIENT_ID"] = "aichat";
8
- AIChatNameEnum2["AI_CHAT_INTERNAL_COMM_TYPE"] = "pubsub";
9
- AIChatNameEnum2["SET_PARENT_NAME"] = "setParentName";
10
- AIChatNameEnum2["SET_USER_ID"] = "setUserId";
11
- AIChatNameEnum2["SET_CUSTOM_HEADERS"] = "setCustomHeaders";
12
- AIChatNameEnum2["SET_CUSTOM_PAYLOAD"] = "setCustomPayload";
13
- AIChatNameEnum2["SET_SUGGESTIONS"] = "setSuggestions";
14
- AIChatNameEnum2["SHOW_SUGGESTIONS"] = "showSuggestions";
15
- AIChatNameEnum2["SHOW_CHAT_MESSAGE"] = "showChatMessage";
16
- AIChatNameEnum2["CONFIRM_ACTION"] = "confirmAction";
17
- AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
18
- AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
19
- AIChatNameEnum2["DEFAULT"] = "default";
20
- return AIChatNameEnum2;
21
- })(AIChatNameEnum || {});
5
+ // src/PubSub.ts
22
6
  var _PubSub = class _PubSub {
23
7
  constructor() {
24
8
  __publicField(this, "subscribers");
@@ -136,6 +120,8 @@ var _PubSub = class _PubSub {
136
120
  };
137
121
  __publicField(_PubSub, "instance");
138
122
  var PubSub = _PubSub;
123
+
124
+ // src/Client.ts
139
125
  var Client = class {
140
126
  /**
141
127
  * Create a new client instance.
@@ -199,6 +185,119 @@ var Client = class {
199
185
  this.pubsub.sendMessage(message);
200
186
  }
201
187
  }
188
+ /**
189
+ * Check if a client with the given ID exists in the PubSub system.
190
+ *
191
+ * @param clientId The ID of the client to check.
192
+ * @param maxRetries Maximum number of retries. Default is 3.
193
+ * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
194
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
195
+ */
196
+ checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
197
+ return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
198
+ }
199
+ /**
200
+ * Check if a client with the given ID exists in the PubSub system with retry mechanism.
201
+ * Will retry up to 3 times with 1 second delay between retries.
202
+ *
203
+ * @param clientId The ID of the client to check.
204
+ * @param retryCount The current retry count.
205
+ * @returns A Promise that resolves to true if the client exists, false otherwise.
206
+ * @private
207
+ */
208
+ checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
209
+ return new Promise((resolve) => {
210
+ if (this.isIframe) {
211
+ const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
212
+ const messageHandler = (event) => {
213
+ const data = event.data;
214
+ if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
215
+ window.removeEventListener("message", messageHandler);
216
+ clearTimeout(removeHandlerTimeout);
217
+ if (data.exists) {
218
+ resolve(true);
219
+ } else if (retryCount < maxRetries - 1) {
220
+ setTimeout(() => {
221
+ this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
222
+ }, retryInterval);
223
+ } else {
224
+ resolve(false);
225
+ }
226
+ }
227
+ };
228
+ window.addEventListener("message", messageHandler);
229
+ window.parent.postMessage({
230
+ type: "CLIENT_EXISTS_CHECK",
231
+ clientId,
232
+ requestId,
233
+ from: this.pageId
234
+ }, "*");
235
+ const removeHandlerTimeout = setTimeout(() => {
236
+ window.removeEventListener("message", messageHandler);
237
+ resolve(false);
238
+ }, retryInterval + 1e3);
239
+ } else {
240
+ const exists = this.pubsub.isClientExists(clientId);
241
+ if (exists) {
242
+ resolve(true);
243
+ } else if (retryCount < maxRetries - 1) {
244
+ setTimeout(() => {
245
+ this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
246
+ }, retryInterval);
247
+ } else {
248
+ resolve(false);
249
+ }
250
+ }
251
+ });
252
+ }
253
+ async handleMessage(event) {
254
+ let message;
255
+ if (event.data) {
256
+ const evt = event;
257
+ message = evt.data;
258
+ } else {
259
+ message = event;
260
+ }
261
+ if (!message || !message.from || !message.to || message.to !== this.pageId) return;
262
+ if (this.callback) {
263
+ try {
264
+ await this.callback(message);
265
+ } catch (error) {
266
+ console.error(`Client ${this.pageId} failed to process message:`, error);
267
+ }
268
+ }
269
+ }
270
+ };
271
+
272
+ // src/aichat/AIChatClient.ts
273
+ var AIChatNameEnum = /* @__PURE__ */ ((AIChatNameEnum2) => {
274
+ AIChatNameEnum2["AI_CHAT_CLIENT_ID"] = "aichat";
275
+ AIChatNameEnum2["AI_CHAT_INTERNAL_COMM_TYPE"] = "pubsub";
276
+ AIChatNameEnum2["SET_PARENT_NAME"] = "setParentName";
277
+ AIChatNameEnum2["SET_USER_ID"] = "setUserId";
278
+ AIChatNameEnum2["SET_CUSTOM_HEADERS"] = "setCustomHeaders";
279
+ AIChatNameEnum2["SET_CUSTOM_PAYLOAD"] = "setCustomPayload";
280
+ AIChatNameEnum2["SET_SUGGESTIONS"] = "setSuggestions";
281
+ AIChatNameEnum2["SHOW_SUGGESTIONS"] = "showSuggestions";
282
+ AIChatNameEnum2["SHOW_CHAT_MESSAGE"] = "showChatMessage";
283
+ AIChatNameEnum2["SHOW_ERROR_MESSAGE"] = "showErrorMessage";
284
+ AIChatNameEnum2["SHOW_UNKNOWN_MESSAGE"] = "showUnknownMessage";
285
+ AIChatNameEnum2["CONFIRM_ACTION"] = "confirmAction";
286
+ AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
287
+ AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
288
+ AIChatNameEnum2["GENERIC_ACTION"] = "genericAction";
289
+ AIChatNameEnum2["DEFAULT"] = "default";
290
+ return AIChatNameEnum2;
291
+ })(AIChatNameEnum || {});
292
+ var AIChatClient = class extends Client {
293
+ /**
294
+ * Create a new client instance.
295
+ *
296
+ * @param pageId The ID of the page or component to register to.
297
+ */
298
+ constructor(pageId) {
299
+ super(pageId);
300
+ }
202
301
  sendAIChatMethod(methodName, arg) {
203
302
  this.sendMessage(
204
303
  "aichat" /* AI_CHAT_CLIENT_ID */,
@@ -292,89 +391,22 @@ var Client = class {
292
391
  this.sendAIChatMethod("sendChatPrompt" /* SEND_CHAT_PROMPT */, prompt);
293
392
  }
294
393
  /**
295
- * Check if a client with the given ID exists in the PubSub system.
394
+ * Show an error message in the AI Chat component.
296
395
  *
297
- * @param clientId The ID of the client to check.
298
- * @param maxRetries Maximum number of retries. Default is 3.
299
- * @param retryInterval Interval between retries in milliseconds. Default is 1000ms.
300
- * @returns A Promise that resolves to true if the client exists, false otherwise.
396
+ * @param message The error message to show.
301
397
  */
302
- checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
303
- return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
398
+ showErrorMessage(message) {
399
+ this.sendAIChatMethod("showErrorMessage" /* SHOW_ERROR_MESSAGE */, message);
304
400
  }
305
401
  /**
306
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
307
- * Will retry up to 3 times with 1 second delay between retries.
308
- *
309
- * @param clientId The ID of the client to check.
310
- * @param retryCount The current retry count.
311
- * @returns A Promise that resolves to true if the client exists, false otherwise.
312
- * @private
402
+ * Show an unknown error message in the AI Chat component.
313
403
  */
314
- checkClientExistsWithRetry(clientId, retryCount, maxRetries, retryInterval) {
315
- return new Promise((resolve) => {
316
- if (this.isIframe) {
317
- const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
318
- const messageHandler = (event) => {
319
- const data = event.data;
320
- if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
321
- window.removeEventListener("message", messageHandler);
322
- clearTimeout(removeHandlerTimeout);
323
- if (data.exists) {
324
- resolve(true);
325
- } else if (retryCount < maxRetries - 1) {
326
- setTimeout(() => {
327
- this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists) => resolve(exists));
328
- }, retryInterval);
329
- } else {
330
- resolve(false);
331
- }
332
- }
333
- };
334
- window.addEventListener("message", messageHandler);
335
- window.parent.postMessage({
336
- type: "CLIENT_EXISTS_CHECK",
337
- clientId,
338
- requestId,
339
- from: this.pageId
340
- }, "*");
341
- const removeHandlerTimeout = setTimeout(() => {
342
- window.removeEventListener("message", messageHandler);
343
- resolve(false);
344
- }, retryInterval + 1e3);
345
- } else {
346
- const exists = this.pubsub.isClientExists(clientId);
347
- if (exists) {
348
- resolve(true);
349
- } else if (retryCount < maxRetries - 1) {
350
- setTimeout(() => {
351
- this.checkClientExistsWithRetry(clientId, retryCount + 1, maxRetries, retryInterval).then((exists2) => resolve(exists2));
352
- }, retryInterval);
353
- } else {
354
- resolve(false);
355
- }
356
- }
357
- });
358
- }
359
- async handleMessage(event) {
360
- let message;
361
- if (event.data) {
362
- const evt = event;
363
- message = evt.data;
364
- } else {
365
- message = event;
366
- }
367
- if (!message || !message.from || !message.to || message.to !== this.pageId) return;
368
- if (this.callback) {
369
- try {
370
- await this.callback(message);
371
- } catch (error) {
372
- console.error(`Client ${this.pageId} failed to process message:`, error);
373
- }
374
- }
404
+ showUnknownMessage() {
405
+ this.sendAIChatMethod("showUnknownMessage" /* SHOW_UNKNOWN_MESSAGE */, "unknown_error");
375
406
  }
376
407
  };
377
408
  export {
409
+ AIChatClient,
378
410
  AIChatNameEnum,
379
411
  Client,
380
412
  PubSub
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iframe-pubsub",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",