@epam/ai-dial-chat-visualizer-connector 0.40.0-rc.8 → 0.40.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 CHANGED
@@ -83,13 +83,16 @@ Expects following required arguments:
83
83
  ```typescript
84
84
  /**
85
85
  * Params for a ChatVisualizerConnector
86
- * @param dialHost {string} DIAL CHAT host
86
+ * @param dialHost {string | string[]} DIAL CHAT host
87
87
  * @param appName {string} name of the Visualizer same as in config
88
88
  * @param dataCallback {(visualizerData: AttachmentData) => void} callback to get data that will be used in the Visualizer
89
89
  */
90
90
 
91
- //instance example
92
- new ChatVisualizerConnector(dialHost, appName, setData);
91
+ //instance example with single host
92
+ new ChatVisualizerConnector('https://hosted-dial-chat-domain.com', 'CUSTOM_VISUALIZER', setData);
93
+
94
+ //instance example with multiple hosts
95
+ new ChatVisualizerConnector(['https://hosted-dial-chat-domain.com', 'https://backup-dial-chat-domain.com'], 'CUSTOM_VISUALIZER', setData);
93
96
  ```
94
97
 
95
98
  `AttachmentData` - interface for the payload you will get from the _DIAL CHAT_
@@ -118,6 +121,12 @@ export interface CustomVisualizerDataLayout {
118
121
  const dialHost = 'https://hosted-dial-chat-domain.com';
119
122
  ```
120
123
 
124
+ Or (new):
125
+
126
+ ```typescript
127
+ const dialHost = ['https://hosted-dial-chat-domain.com', 'https://backup-dial-chat-domain.com'];
128
+ ```
129
+
121
130
  4. Set `appName` same as `title` in the _DIAL CHAT_ configuration in the `CUSTOM_VISUALIZERS` environmental variable:
122
131
 
123
132
  ```typescript
@@ -176,9 +185,19 @@ data.visualizerData as { dataToRender: string; layout: YourVisualizerLayout };
176
185
  <div>{typedVisualizerData.dataToRender}</div>
177
186
  ```
178
187
 
179
- ### Full React code example to connect your custom visualizer
188
+ ### Sending to a particular host when multiple were passed
180
189
 
181
- `Module.tsx`
190
+ ```typescript
191
+ chatVisualizerConnector.current?.send({
192
+ type: VisualizerConnectorEvents.sendMessage,
193
+ payload: { message: 'hello from visualizer' },
194
+ dialHost: 'https://hosted-dial-chat-domain.com',
195
+ });
196
+ ```
197
+
198
+ If `dialHost` is **not** provided in `send(...)`, the connector will send the message to **all** hosts passed in the constructor.
199
+
200
+ ### Full React code example to connect your custom visualizer
182
201
 
183
202
  ```typescript
184
203
 
@@ -192,8 +211,11 @@ export const Module: FC = () => {
192
211
  null
193
212
  );
194
213
 
195
- //DIAL CHAT host
196
- const dialHost = 'https://hosted-dial-chat-domain.com';
214
+ //DIAL CHAT host (one or multiple)
215
+ const dialHost = [
216
+ 'https://hosted-dial-chat-domain.com',
217
+ 'https://backup-dial-chat-domain.com'
218
+ ];
197
219
 
198
220
  //Visualizer title. Should be same as in the DIAL CHAT configuration in CUSTOM_VISUALIZERS
199
221
  const appName = 'CUSTOM_VISUALIZER';
package/index.esm.js CHANGED
@@ -38,12 +38,18 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
38
38
  class ChatVisualizerConnector {
39
39
  /**
40
40
  * Creates a ChatVisualizerConnector
41
- * @param dialHost {string} DIAL CHAT host
41
+ * @param dialHost {string | string[]} DIAL CHAT host(s)
42
42
  * @param appName {string} name of the Visualizer same as in config
43
43
  * @param dataCallback {(visualizerData: AttachmentData) => void} callback to get data that will be used in the Visualizer
44
44
  */
45
45
  constructor(dialHost, appName, dataCallback) {
46
- this.dialHost = dialHost;
46
+ const hosts = Array.isArray(dialHost) ? dialHost : [dialHost];
47
+ if (!hosts.length) {
48
+ throw new Error('[ChatVisualizerConnector] No dial host(s) provided');
49
+ }
50
+ this.dialHosts = hosts;
51
+ // keep old field for backward compatibility
52
+ this.dialHost = hosts[0];
47
53
  this.appName = appName;
48
54
  this.dataCallback = dataCallback;
49
55
  this.postMessageListener = this.postMessageListener.bind(this);
@@ -53,16 +59,19 @@ class ChatVisualizerConnector {
53
59
  * Sends the post message to the DIAL Chat
54
60
  * @param type Visualizer Event name
55
61
  * @param payload Event payload
56
- * @param dialHost host of the DIAL Chat
62
+ * @param dialHost host of the DIAL Chat (optional) — if provided, message goes ONLY to this host
57
63
  */
58
- send({ type, payload, dialHost = this.dialHost, }) {
64
+ send({ type, payload, dialHost, }) {
59
65
  if (!(window === null || window === void 0 ? void 0 : window.parent)) {
60
66
  throw new Error(`[${this.appName}] There is no parent window to send requests`);
61
67
  }
62
- window.parent.postMessage({
63
- type: `${this.appName}/${type}`,
64
- payload,
65
- }, dialHost);
68
+ const targets = dialHost ? [dialHost] : this.dialHosts;
69
+ for (const target of targets) {
70
+ window.parent.postMessage({
71
+ type: `${this.appName}/${type}`,
72
+ payload,
73
+ }, target);
74
+ }
66
75
  }
67
76
  /**
68
77
  * Sends response via postMessage to the DIAL CHAT to notify it data received
@@ -77,9 +86,10 @@ class ChatVisualizerConnector {
77
86
  }, dialHost);
78
87
  }
79
88
  postMessageListener(event) {
80
- if (event.origin !== this.dialHost)
89
+ // accept messages only from known hosts
90
+ if (!this.dialHosts.includes(event.origin))
81
91
  return;
82
- //check if there is a payload
92
+ // check if there is a payload
83
93
  if (typeof event.data.payload !== 'object' || event.data.payload === null)
84
94
  return;
85
95
  if (event.data.type ===
package/package.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "@epam/ai-dial-chat-visualizer-connector",
3
3
  "description": "Package for custom visualizer to connect with DIAL Chat",
4
4
  "homepage": "https://dialx.ai",
5
- "version": "0.40.0-rc.8",
5
+ "version": "0.40.0",
6
6
  "dependencies": {
7
- "@epam/ai-dial-shared": "0.40.0-rc.8"
7
+ "@epam/ai-dial-shared": "0.40.0"
8
8
  },
9
9
  "type": "module",
10
10
  "bugs": {
@@ -12,20 +12,26 @@ export interface PostMessageRequestParams extends RequestParams {
12
12
  */
13
13
  export declare class ChatVisualizerConnector {
14
14
  protected dialHost: string;
15
+ /**
16
+ * List of allowed/target hosts.
17
+ * All outgoing messages (without explicit dialHost) will be sent to each of them.
18
+ * All incoming messages must originate from one of them.
19
+ */
20
+ protected dialHosts: string[];
15
21
  protected appName: string;
16
22
  protected dataCallback: (visualizerData: AttachmentData) => void;
17
23
  /**
18
24
  * Creates a ChatVisualizerConnector
19
- * @param dialHost {string} DIAL CHAT host
25
+ * @param dialHost {string | string[]} DIAL CHAT host(s)
20
26
  * @param appName {string} name of the Visualizer same as in config
21
27
  * @param dataCallback {(visualizerData: AttachmentData) => void} callback to get data that will be used in the Visualizer
22
28
  */
23
- constructor(dialHost: string, appName: string, dataCallback: (visualizerData: AttachmentData) => void);
29
+ constructor(dialHost: string | string[], appName: string, dataCallback: (visualizerData: AttachmentData) => void);
24
30
  /**
25
31
  * Sends the post message to the DIAL Chat
26
32
  * @param type Visualizer Event name
27
33
  * @param payload Event payload
28
- * @param dialHost host of the DIAL Chat
34
+ * @param dialHost host of the DIAL Chat (optional) — if provided, message goes ONLY to this host
29
35
  */
30
36
  send({ type, payload, dialHost, }: {
31
37
  type: VisualizerConnectorEvents;