iframe-pubsub 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/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,50 @@ 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
+ CONFIRM_ACTION = "confirmAction",
86
+ SHOW_OPTIONS = "showOptions",
87
+ SEND_CHAT_PROMPT = "sendChatPrompt",
88
+ DEFAULT = "default"
89
+ }
90
+ declare class AIChatClient extends Client {
91
+ /**
92
+ * Create a new client instance.
93
+ *
94
+ * @param pageId The ID of the page or component to register to.
95
+ */
96
+ constructor(pageId: string);
96
97
  private sendAIChatMethod;
97
98
  /**
98
99
  * Set the parent name then the AI Chat can know who should it communicate with.
@@ -154,26 +155,46 @@ declare class Client {
154
155
  * @param prompt The prompt to send.
155
156
  */
156
157
  sendChatPrompt(prompt: string): void;
158
+ }
159
+
160
+ declare class PubSub {
161
+ private static instance;
162
+ private subscribers;
163
+ private mainCallback?;
164
+ private constructor();
165
+ static getInstance(): PubSub;
166
+ onMessage(callback: MessageCallback): void;
157
167
  /**
158
- * Check if a client with the given ID exists in the PubSub system.
168
+ * Registers a callback function to be called when a message is received.
159
169
  *
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.
170
+ * @param pageId The ID of the page or component to register for.
171
+ * @param callback The callback function to be called when a message is received.
164
172
  */
165
- checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
173
+ register(pageId: string, callback: MessageCallback): void;
166
174
  /**
167
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
175
+ * Unregister a page or component from the pubsub.
176
+ * Should unregister the registered page or component if it has been removed.
177
+ *
178
+ * @param pageId The ID of the page or component to unregister from.
179
+ */
180
+ unregister(pageId: string): void;
181
+ sendMessage(message: IMessage): void;
182
+ /**
183
+ * Try to send a message to a client with retry logic.
168
184
  * Will retry up to 3 times with 1 second delay between retries.
169
185
  *
170
- * @param clientId The ID of the client to check.
186
+ * @param message The message to send.
171
187
  * @param retryCount The current retry count.
172
- * @returns A Promise that resolves to true if the client exists, false otherwise.
173
- * @private
174
188
  */
175
- private checkClientExistsWithRetry;
189
+ private trySendMessageWithRetry;
176
190
  private handleMessage;
191
+ /**
192
+ * Check if a client with the given ID exists in the PubSub system.
193
+ *
194
+ * @param clientId The ID of the client to check.
195
+ * @returns True if the client exists, false otherwise.
196
+ */
197
+ isClientExists(clientId: string): boolean;
177
198
  }
178
199
 
179
- export { AIChatNameEnum, Client, type IMessage, type MessageCallback, PubSub };
200
+ 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,50 @@ 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
+ CONFIRM_ACTION = "confirmAction",
86
+ SHOW_OPTIONS = "showOptions",
87
+ SEND_CHAT_PROMPT = "sendChatPrompt",
88
+ DEFAULT = "default"
89
+ }
90
+ declare class AIChatClient extends Client {
91
+ /**
92
+ * Create a new client instance.
93
+ *
94
+ * @param pageId The ID of the page or component to register to.
95
+ */
96
+ constructor(pageId: string);
96
97
  private sendAIChatMethod;
97
98
  /**
98
99
  * Set the parent name then the AI Chat can know who should it communicate with.
@@ -154,26 +155,46 @@ declare class Client {
154
155
  * @param prompt The prompt to send.
155
156
  */
156
157
  sendChatPrompt(prompt: string): void;
158
+ }
159
+
160
+ declare class PubSub {
161
+ private static instance;
162
+ private subscribers;
163
+ private mainCallback?;
164
+ private constructor();
165
+ static getInstance(): PubSub;
166
+ onMessage(callback: MessageCallback): void;
157
167
  /**
158
- * Check if a client with the given ID exists in the PubSub system.
168
+ * Registers a callback function to be called when a message is received.
159
169
  *
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.
170
+ * @param pageId The ID of the page or component to register for.
171
+ * @param callback The callback function to be called when a message is received.
164
172
  */
165
- checkClientExists(clientId: string, maxRetries?: number, retryInterval?: number): Promise<boolean>;
173
+ register(pageId: string, callback: MessageCallback): void;
166
174
  /**
167
- * Check if a client with the given ID exists in the PubSub system with retry mechanism.
175
+ * Unregister a page or component from the pubsub.
176
+ * Should unregister the registered page or component if it has been removed.
177
+ *
178
+ * @param pageId The ID of the page or component to unregister from.
179
+ */
180
+ unregister(pageId: string): void;
181
+ sendMessage(message: IMessage): void;
182
+ /**
183
+ * Try to send a message to a client with retry logic.
168
184
  * Will retry up to 3 times with 1 second delay between retries.
169
185
  *
170
- * @param clientId The ID of the client to check.
186
+ * @param message The message to send.
171
187
  * @param retryCount The current retry count.
172
- * @returns A Promise that resolves to true if the client exists, false otherwise.
173
- * @private
174
188
  */
175
- private checkClientExistsWithRetry;
189
+ private trySendMessageWithRetry;
176
190
  private handleMessage;
191
+ /**
192
+ * Check if a client with the given ID exists in the PubSub system.
193
+ *
194
+ * @param clientId The ID of the client to check.
195
+ * @returns True if the client exists, false otherwise.
196
+ */
197
+ isClientExists(clientId: string): boolean;
177
198
  }
178
199
 
179
- export { AIChatNameEnum, Client, type IMessage, type MessageCallback, PubSub };
200
+ 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,116 @@ 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["CONFIRM_ACTION"] = "confirmAction";
311
+ AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
312
+ AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
313
+ AIChatNameEnum2["DEFAULT"] = "default";
314
+ return AIChatNameEnum2;
315
+ })(AIChatNameEnum || {});
316
+ var AIChatClient = class extends Client {
317
+ /**
318
+ * Create a new client instance.
319
+ *
320
+ * @param pageId The ID of the page or component to register to.
321
+ */
322
+ constructor(pageId) {
323
+ super(pageId);
324
+ }
226
325
  sendAIChatMethod(methodName, arg) {
227
326
  this.sendMessage(
228
327
  "aichat" /* AI_CHAT_CLIENT_ID */,
@@ -315,91 +414,10 @@ var Client = class {
315
414
  sendChatPrompt(prompt) {
316
415
  this.sendAIChatMethod("sendChatPrompt" /* SEND_CHAT_PROMPT */, prompt);
317
416
  }
318
- /**
319
- * Check if a client with the given ID exists in the PubSub system.
320
- *
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.
325
- */
326
- checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
327
- return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
328
- }
329
- /**
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
337
- */
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
- }
399
- }
400
417
  };
401
418
  // Annotate the CommonJS export names for ESM import in node:
402
419
  0 && (module.exports = {
420
+ AIChatClient,
403
421
  AIChatNameEnum,
404
422
  Client,
405
423
  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,116 @@ 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["CONFIRM_ACTION"] = "confirmAction";
284
+ AIChatNameEnum2["SHOW_OPTIONS"] = "showOptions";
285
+ AIChatNameEnum2["SEND_CHAT_PROMPT"] = "sendChatPrompt";
286
+ AIChatNameEnum2["DEFAULT"] = "default";
287
+ return AIChatNameEnum2;
288
+ })(AIChatNameEnum || {});
289
+ var AIChatClient = class extends Client {
290
+ /**
291
+ * Create a new client instance.
292
+ *
293
+ * @param pageId The ID of the page or component to register to.
294
+ */
295
+ constructor(pageId) {
296
+ super(pageId);
297
+ }
202
298
  sendAIChatMethod(methodName, arg) {
203
299
  this.sendMessage(
204
300
  "aichat" /* AI_CHAT_CLIENT_ID */,
@@ -291,90 +387,9 @@ var Client = class {
291
387
  sendChatPrompt(prompt) {
292
388
  this.sendAIChatMethod("sendChatPrompt" /* SEND_CHAT_PROMPT */, prompt);
293
389
  }
294
- /**
295
- * Check if a client with the given ID exists in the PubSub system.
296
- *
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.
301
- */
302
- checkClientExists(clientId, maxRetries = 3, retryInterval = 1e3) {
303
- return this.checkClientExistsWithRetry(clientId, 0, maxRetries, retryInterval);
304
- }
305
- /**
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
313
- */
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
- }
375
- }
376
390
  };
377
391
  export {
392
+ AIChatClient,
378
393
  AIChatNameEnum,
379
394
  Client,
380
395
  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.2",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",