@memori.ai/memori-react 8.38.8 → 8.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/components/MemoriWidget/MemoriWidget.js +401 -87
  3. package/dist/components/MemoriWidget/MemoriWidget.js.map +1 -1
  4. package/dist/helpers/credits.d.ts +5 -2
  5. package/dist/helpers/credits.js +5 -1
  6. package/dist/helpers/credits.js.map +1 -1
  7. package/dist/helpers/nats/getNatsConfig.d.ts +5 -0
  8. package/dist/helpers/nats/getNatsConfig.js +29 -0
  9. package/dist/helpers/nats/getNatsConfig.js.map +1 -0
  10. package/dist/helpers/nats/useNats.d.ts +12 -0
  11. package/dist/helpers/nats/useNats.js +72 -0
  12. package/dist/helpers/nats/useNats.js.map +1 -0
  13. package/dist/helpers/nats/useNatsSession.d.ts +27 -0
  14. package/dist/helpers/nats/useNatsSession.js +108 -0
  15. package/dist/helpers/nats/useNatsSession.js.map +1 -0
  16. package/dist/version.d.ts +1 -1
  17. package/dist/version.js +1 -1
  18. package/esm/components/MemoriWidget/MemoriWidget.js +401 -87
  19. package/esm/components/MemoriWidget/MemoriWidget.js.map +1 -1
  20. package/esm/helpers/credits.d.ts +5 -2
  21. package/esm/helpers/credits.js +5 -1
  22. package/esm/helpers/credits.js.map +1 -1
  23. package/esm/helpers/nats/getNatsConfig.d.ts +5 -0
  24. package/esm/helpers/nats/getNatsConfig.js +25 -0
  25. package/esm/helpers/nats/getNatsConfig.js.map +1 -0
  26. package/esm/helpers/nats/useNats.d.ts +12 -0
  27. package/esm/helpers/nats/useNats.js +68 -0
  28. package/esm/helpers/nats/useNats.js.map +1 -0
  29. package/esm/helpers/nats/useNatsSession.d.ts +27 -0
  30. package/esm/helpers/nats/useNatsSession.js +103 -0
  31. package/esm/helpers/nats/useNatsSession.js.map +1 -0
  32. package/esm/version.d.ts +1 -1
  33. package/esm/version.js +1 -1
  34. package/package.json +3 -2
  35. package/src/components/MemoriWidget/MemoriWidget.tsx +546 -140
  36. package/src/components/StartPanel/StartPanel.stories.tsx +21 -0
  37. package/src/components/StartPanel/StartPanel.test.tsx +66 -1
  38. package/src/components/StartPanel/__snapshots__/StartPanel.test.tsx.snap +156 -0
  39. package/src/components/layouts/layouts.stories.tsx +28 -34
  40. package/src/helpers/credits.ts +16 -1
  41. package/src/helpers/nats/getNatsConfig.ts +69 -0
  42. package/src/helpers/nats/useNats.ts +122 -0
  43. package/src/helpers/nats/useNatsSession.ts +210 -0
  44. package/src/index.stories.tsx +19 -3
  45. package/src/version.ts +1 -1
@@ -381,3 +381,24 @@ WithCompletionProviderDown.args = {
381
381
  onClickStart: () => {},
382
382
  _TEST_forceProviderStatus: 'major',
383
383
  };
384
+
385
+ /** Public agent whose owner has run out of credits: the start button is
386
+ * disabled and a "not enough credits" badge is shown instead of starting a
387
+ * session or asking for a password. */
388
+ export const NotEnoughCredits = Template.bind({});
389
+ NotEnoughCredits.args = {
390
+ memori: {
391
+ ...memori,
392
+ privacyType: 'PUBLIC',
393
+ },
394
+ tenant,
395
+ language: 'it',
396
+ userLang: 'en',
397
+ setUserLang: () => {},
398
+ openPositionDrawer: () => {},
399
+ instruct: false,
400
+ sessionId: sessionID,
401
+ clickedStart: false,
402
+ onClickStart: () => {},
403
+ notEnoughCredits: true,
404
+ };
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
- import { render } from '@testing-library/react';
2
+ import { render, fireEvent } from '@testing-library/react';
3
+ import '@testing-library/jest-dom';
3
4
  import { memori, tenant, sessionID, integration, user } from '../../mocks/data';
4
5
  import StartPanel from './StartPanel';
5
6
 
@@ -293,3 +294,67 @@ it('renders StartPanel with completion provider down unchanged', () => {
293
294
  );
294
295
  expect(container).toMatchSnapshot();
295
296
  });
297
+
298
+ it('renders StartPanel with not enough credits unchanged', () => {
299
+ const { container } = render(
300
+ <StartPanel
301
+ memori={memori}
302
+ tenant={tenant}
303
+ language="it"
304
+ userLang="en"
305
+ setUserLang={() => {}}
306
+ openPositionDrawer={() => {}}
307
+ instruct={false}
308
+ clickedStart={false}
309
+ onClickStart={() => {}}
310
+ setShowLoginDrawer={jest.fn()}
311
+ notEnoughCredits
312
+ />
313
+ );
314
+ expect(container).toMatchSnapshot();
315
+ });
316
+
317
+ // When the agent owner has not enough credits, opening the chat for a PUBLIC
318
+ // agent must not start a session nor ask for a password: the start button is
319
+ // disabled and a "not enough credits" badge is shown instead.
320
+ it('blocks start and shows credits badge when owner has not enough credits', () => {
321
+ const onClickStart = jest.fn();
322
+ const { container, getByText, queryByPlaceholderText } = render(
323
+ <StartPanel
324
+ memori={{ ...memori, privacyType: 'PUBLIC' }}
325
+ tenant={tenant}
326
+ language="it"
327
+ userLang="en"
328
+ setUserLang={() => {}}
329
+ openPositionDrawer={() => {}}
330
+ instruct={false}
331
+ clickedStart={false}
332
+ onClickStart={onClickStart}
333
+ setShowLoginDrawer={jest.fn()}
334
+ notEnoughCredits
335
+ />
336
+ );
337
+
338
+ const startButton = container.querySelector(
339
+ '.memori--start-button'
340
+ ) as HTMLButtonElement;
341
+ expect(startButton).toBeInTheDocument();
342
+ expect(startButton).toBeDisabled();
343
+
344
+ // Clicking the disabled button must not attempt to open a session.
345
+ fireEvent.click(startButton);
346
+ expect(onClickStart).not.toHaveBeenCalled();
347
+
348
+ // No password field is ever rendered for a public agent.
349
+ expect(queryByPlaceholderText('Password')).not.toBeInTheDocument();
350
+
351
+ // The credits badge is rendered and surfaces the proper message on hover.
352
+ const badge = container.querySelector('.blocked-memori-badge--wrapper');
353
+ expect(badge).toBeInTheDocument();
354
+
355
+ const tooltipTrigger = container.querySelector(
356
+ '.blocked-memori-badge--tooltip'
357
+ ) as HTMLElement;
358
+ fireEvent.mouseEnter(tooltipTrigger);
359
+ expect(getByText('notEnoughCredits')).toBeInTheDocument();
360
+ });
@@ -1773,6 +1773,162 @@ exports[`renders StartPanel with multilangual unchanged 1`] = `
1773
1773
  </div>
1774
1774
  `;
1775
1775
 
1776
+ exports[`renders StartPanel with not enough credits unchanged 1`] = `
1777
+ <div>
1778
+ <div
1779
+ class="memori--start-panel"
1780
+ >
1781
+ <div
1782
+ class="memori--cover"
1783
+ />
1784
+ <picture
1785
+ class="memori--avatar"
1786
+ >
1787
+ <source
1788
+ src="https://aisuru.com/images/aisuru/square_logo.png"
1789
+ />
1790
+ <img
1791
+ alt="Memori"
1792
+ src="https://aisuru.com/images/aisuru/square_logo.png"
1793
+ />
1794
+ </picture>
1795
+ <h2
1796
+ class="memori--title"
1797
+ >
1798
+ Memori
1799
+ </h2>
1800
+ <div
1801
+ class="memori--description"
1802
+ >
1803
+ <p>
1804
+ <div
1805
+ class="memori-expandable memori--description-text"
1806
+ >
1807
+ <div
1808
+ class="memori-expandable--inner"
1809
+ style="max-height: 9999px;"
1810
+ >
1811
+ Lorem ipsum.
1812
+ </div>
1813
+ </div>
1814
+ </p>
1815
+ <div
1816
+ class="memori--start-privacy-explanation-container"
1817
+ >
1818
+ <p
1819
+ class="memori--start-privacy-explanation"
1820
+ >
1821
+ write_and_speak.pagePrivacyExplanation
1822
+ </p>
1823
+ <div
1824
+ class="memori-tooltip memori-tooltip--align-topLeft"
1825
+ >
1826
+ <div
1827
+ class="memori-tooltip--trigger"
1828
+ >
1829
+ <svg
1830
+ aria-hidden="true"
1831
+ class="memori--start-privacy-explanation-icon"
1832
+ fill="none"
1833
+ focusable="false"
1834
+ role="img"
1835
+ stroke="currentColor"
1836
+ stroke-linecap="round"
1837
+ stroke-linejoin="round"
1838
+ stroke-width="1.5"
1839
+ viewBox="0 0 24 24"
1840
+ xmlns="http://www.w3.org/2000/svg"
1841
+ >
1842
+ <circle
1843
+ cx="12"
1844
+ cy="12"
1845
+ r="10"
1846
+ />
1847
+ <path
1848
+ d="M9.09 9a3 3 0 015.83 1c0 2-3 3-3 3"
1849
+ />
1850
+ <path
1851
+ d="M12 17L12.01 17"
1852
+ />
1853
+ </svg>
1854
+ </div>
1855
+ </div>
1856
+ </div>
1857
+ <button
1858
+ class="memori-button memori-button--primary memori-button--rounded memori-button--padded memori--start-button"
1859
+ disabled=""
1860
+ >
1861
+ write_and_speak.tryMeButton
1862
+ </button>
1863
+ <div
1864
+ class="memori--completion-provider-status--loading"
1865
+ >
1866
+ <div
1867
+ class="memori-spin memori-spin--spinning"
1868
+ >
1869
+ <div
1870
+ class="memori-spin--spinner"
1871
+ >
1872
+ <svg
1873
+ aria-hidden="true"
1874
+ class="memori-loading-icon"
1875
+ focusable="false"
1876
+ role="img"
1877
+ viewBox="0 0 1024 1024"
1878
+ xmlns="http://www.w3.org/2000/svg"
1879
+ >
1880
+ <path
1881
+ d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"
1882
+ />
1883
+ </svg>
1884
+ </div>
1885
+ </div>
1886
+ </div>
1887
+ <p
1888
+ class="memori--start-description"
1889
+ >
1890
+ write_and_speak.pageTryMeExplanation
1891
+ </p>
1892
+ <div
1893
+ class="memori-tooltip memori-tooltip--align-right blocked-memori-badge--tooltip"
1894
+ >
1895
+ <div
1896
+ class="memori-tooltip--trigger"
1897
+ >
1898
+ <div
1899
+ class="blocked-memori-badge--wrapper"
1900
+ >
1901
+ <div
1902
+ class="blocked-memori-badge margin-left"
1903
+ >
1904
+ <svg
1905
+ aria-hidden="true"
1906
+ class="blocked-memori-badge--icon"
1907
+ color="currentColor"
1908
+ focusable="false"
1909
+ role="img"
1910
+ viewBox="0 0 1024 1024"
1911
+ xmlns="http://www.w3.org/2000/svg"
1912
+ >
1913
+ <path
1914
+ d="M464 720a48 48 0 1096 0 48 48 0 10-96 0zm16-304v184c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V416c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8zm475.7 440l-416-720c-6.2-10.7-16.9-16-27.7-16s-21.6 5.3-27.7 16l-416 720C56 877.4 71.4 904 96 904h832c24.6 0 40-26.6 27.7-48zm-783.5-27.9L512 239.9l339.8 588.2H172.2z"
1915
+ fill="currentColor"
1916
+ />
1917
+ </svg>
1918
+ </div>
1919
+ <span
1920
+ class="blocked-memori-badge--title"
1921
+ >
1922
+ memoriBlockedTitle
1923
+ </span>
1924
+ </div>
1925
+ </div>
1926
+ </div>
1927
+ </div>
1928
+ </div>
1929
+ </div>
1930
+ `;
1931
+
1776
1932
  exports[`renders StartPanel with position required unchanged 1`] = `
1777
1933
  <div>
1778
1934
  <div
@@ -8,10 +8,9 @@ import Spin from '../ui/Spin';
8
8
  import { VisemeProvider } from '../../context/visemeContext';
9
9
  import { ArtifactProvider } from '../MemoriArtifactSystem/context/ArtifactContext';
10
10
 
11
-
12
11
  const meta: Meta = {
13
12
  title: 'General/Layouts',
14
- component: (args: Props) => <Memori {...args} />,
13
+ component: (args: Props) => <Memori {...args} />,
15
14
  argTypes: {},
16
15
  parameters: {
17
16
  controls: { expanded: true },
@@ -21,7 +20,6 @@ const meta: Meta = {
21
20
 
22
21
  export default meta;
23
22
 
24
-
25
23
  const Template: Story<Props> = args => (
26
24
  <I18nWrapper>
27
25
  <ArtifactProvider>
@@ -55,15 +53,11 @@ DefaultLayout.args = {
55
53
  showMessageConsumption: true,
56
54
  };
57
55
 
58
-
59
-
60
56
  export const Default = Template.bind({});
61
57
  Default.args = {
62
58
  ...DefaultLayout.args,
63
59
  };
64
60
 
65
-
66
-
67
61
  export const Totem = Template.bind({});
68
62
  Totem.args = {
69
63
  ...DefaultLayout.args,
@@ -154,23 +148,22 @@ WebsiteAssistant2.args = {
154
148
 
155
149
  export const WebsiteAssistant3 = Template.bind({});
156
150
  WebsiteAssistant3.args = {
157
- memoriName: "Layout Storybook",
158
- ownerUserName: "Andrea-Patini",
159
- memoriID: "ae20fc5a-cc15-4db9-b7dd-2cd4a621b85e",
160
- ownerUserID: "91dbc9ba-b684-4fbe-9828-b5980af6cda9",
161
- tenantID: "aisuru-staging.aclambda.online",
162
- engineURL: "https://engine-staging.memori.ai/memori/v2",
163
- apiURL: "https://backend-staging.memori.ai/api/v2",
164
- baseURL: "http://localhost:3000",
165
- layout: "WEBSITE_ASSISTANT",
151
+ memoriName: 'Layout Storybook',
152
+ ownerUserName: 'Andrea-Patini',
153
+ memoriID: 'ae20fc5a-cc15-4db9-b7dd-2cd4a621b85e',
154
+ ownerUserID: '91dbc9ba-b684-4fbe-9828-b5980af6cda9',
155
+ tenantID: 'aisuru-staging.aclambda.online',
156
+ engineURL: 'https://engine-staging.memori.ai/memori/v2',
157
+ apiURL: 'https://backend-staging.memori.ai/api/v2',
158
+ baseURL: 'http://localhost:3000',
159
+ layout: 'WEBSITE_ASSISTANT',
166
160
  avatar3dHidden: true,
167
- uiLang: "IT",
168
- spokenLang: "IT",
161
+ uiLang: 'IT',
162
+ spokenLang: 'IT',
169
163
  showOnlyLastMessages: true,
170
- integrationID: "716f4728-919c-4015-aae1-88998a081c6f",
164
+ integrationID: '716f4728-919c-4015-aae1-88998a081c6f',
171
165
  };
172
166
 
173
-
174
167
  export const WebsiteAssistant = Template.bind({});
175
168
  WebsiteAssistant.args = {
176
169
  uiLang: 'EN',
@@ -203,7 +196,8 @@ WebsiteAssistant.args = {
203
196
  innerBgAlpha: 0.8,
204
197
  multilanguage: true,
205
198
  avatar: 'readyplayerme',
206
- avatarURL: 'https://assets.memori.ai/api/v2/asset/b791f77c-1a94-4272-829e-eca82fcc62b7.glb',
199
+ avatarURL:
200
+ 'https://assets.memori.ai/api/v2/asset/b791f77c-1a94-4272-829e-eca82fcc62b7.glb',
207
201
  }),
208
202
  },
209
203
  };
@@ -228,17 +222,17 @@ HiddenChat.args = {
228
222
  export const ZoomedFullBody = Template.bind({});
229
223
  ZoomedFullBody.args = {
230
224
  ...DefaultLayout.args,
231
- memoriName: "Layout Storybook",
232
- ownerUserName: "Andrea-Patini",
233
- memoriID: "ae20fc5a-cc15-4db9-b7dd-2cd4a621b85e",
234
- ownerUserID: "91dbc9ba-b684-4fbe-9828-b5980af6cda9",
235
- tenantID: "aisuru-staging.aclambda.online",
236
- engineURL: "https://engine-staging.memori.ai/memori/v2",
237
- apiURL: "https://backend-staging.memori.ai/api/v2",
238
- baseURL: "http://localhost:3000",
239
- layout: "FULLPAGE",
240
- uiLang: "IT",
241
- spokenLang: "IT",
242
- integrationID: "32922e14-24d6-4f5f-a06b-d963da14a658",
243
- showSettings: true
225
+ memoriName: 'Layout Storybook',
226
+ ownerUserName: 'Andrea-Patini',
227
+ memoriID: 'ae20fc5a-cc15-4db9-b7dd-2cd4a621b85e',
228
+ ownerUserID: '91dbc9ba-b684-4fbe-9828-b5980af6cda9',
229
+ tenantID: 'aisuru-staging.aclambda.online',
230
+ engineURL: 'https://engine-staging.memori.ai/memori/v2',
231
+ apiURL: 'https://backend-staging.memori.ai/api/v2',
232
+ baseURL: 'http://localhost:3000',
233
+ layout: 'FULLPAGE',
234
+ uiLang: 'IT',
235
+ spokenLang: 'IT',
236
+ integrationID: '32922e14-24d6-4f5f-a06b-d963da14a658',
237
+ showSettings: true,
244
238
  };
@@ -1,23 +1,37 @@
1
1
  // POST http://localhost:3000/api/verify-tokens operation=session_creation userID=585ec0ff-e805-495e-b8fc-5b0b8dd288ff tenant=aisuru-staging-tokenized.aclambda.online
2
+ export type CreditsOperation =
3
+ | 'twin_creation'
4
+ | 'session_creation'
5
+ | 'import_document'
6
+ // accepted by the API and normalized to session_creation
7
+ | 'dt_session_creation';
8
+
2
9
  export const getCredits = async ({
3
10
  operation = 'session_creation',
4
11
  baseUrl,
5
12
  userID,
6
13
  userName,
7
14
  tenant,
15
+ characters,
8
16
  }: {
9
- operation?: string;
17
+ operation?: CreditsOperation;
10
18
  baseUrl: string;
11
19
  userID?: string | null;
12
20
  userName?: string | null;
13
21
  tenant: string;
22
+ characters?: number;
14
23
  }): Promise<{
15
24
  enough: boolean;
16
25
  required: number;
26
+ tokens?: number;
17
27
  }> => {
18
28
  if (!userID && !userName) {
19
29
  throw new Error('Either userID or userName must be provided');
20
30
  }
31
+ if (operation === 'import_document' && characters == null) {
32
+ throw new Error('characters must be provided for import_document');
33
+ }
34
+
21
35
  const resp = await fetch(`${baseUrl}/api/verify-tokens`, {
22
36
  method: 'POST',
23
37
  headers: {
@@ -28,6 +42,7 @@ export const getCredits = async ({
28
42
  userID,
29
43
  userName,
30
44
  tenant,
45
+ ...(operation === 'import_document' ? { characters } : {}),
31
46
  }),
32
47
  });
33
48
 
@@ -0,0 +1,69 @@
1
+ // helpers/nats/getNatsConfig.ts - Fetch NATS connection params (url + token)
2
+ // from the backend, using the same baseUrl already used for /api/tts and /api/stt.
3
+
4
+ /**
5
+ * Connection parameters returned by `GET /api/nats?sessionId=<uuid>`.
6
+ */
7
+ export interface NatsConfig {
8
+ /** WebSocket URL of the NATS server (e.g. wss://nats.hz.slnode.net:8080). */
9
+ url: string;
10
+ /** Bearer token used to authenticate the WebSocket connection. */
11
+ token: string;
12
+ }
13
+
14
+ /**
15
+ * Fetch the NATS connection config for a given session.
16
+ *
17
+ * Mirrors the error-handling style of the tts/stt helpers: the backend may
18
+ * answer with 400 (sessionId missing), 404 (invalid session) or 500 (NATS
19
+ * config missing). Any non-ok response throws with a descriptive message.
20
+ *
21
+ * @param baseUrl Same baseUrl used for `/api/tts` and `/api/stt`.
22
+ * @param sessionId Current session UUID.
23
+ * @param signal Optional AbortSignal to cancel the request.
24
+ */
25
+ export async function getNatsConfig(
26
+ baseUrl: string,
27
+ sessionId: string,
28
+ signal?: AbortSignal
29
+ ): Promise<NatsConfig> {
30
+ if (!sessionId) {
31
+ throw new Error('Missing sessionId for NATS config request');
32
+ }
33
+
34
+ const response = await fetch(
35
+ `${baseUrl}/api/nats?sessionId=${encodeURIComponent(sessionId)}`,
36
+ { signal }
37
+ );
38
+
39
+ if (!response.ok) {
40
+ const errorData = await response.json().catch(() => ({} as any));
41
+
42
+ switch (response.status) {
43
+ case 400:
44
+ throw new Error(
45
+ errorData.error || 'NATS config error: missing sessionId'
46
+ );
47
+ case 404:
48
+ throw new Error(
49
+ errorData.error || 'NATS config error: invalid session'
50
+ );
51
+ case 500:
52
+ throw new Error(
53
+ errorData.error || 'NATS config error: NATS configuration missing'
54
+ );
55
+ default:
56
+ throw new Error(
57
+ errorData.error || `NATS config error: ${response.status}`
58
+ );
59
+ }
60
+ }
61
+
62
+ const data = (await response.json()) as Partial<NatsConfig>;
63
+
64
+ if (!data.url || !data.token) {
65
+ throw new Error('Invalid response from NATS config service');
66
+ }
67
+
68
+ return { url: data.url, token: data.token };
69
+ }
@@ -0,0 +1,122 @@
1
+ // helpers/nats/useNats.ts - Orchestrates NATS config retrieval + subscription.
2
+ //
3
+ // Additive to the existing HTTP flow: text is sent via `postEnterTextAsync`;
4
+ // this hook *receives* asynchronous events on the session channel (progress /
5
+ // dialog.text_entered_response / error).
6
+ import { useEffect, useRef, useState, useCallback } from 'react';
7
+ import { getNatsConfig } from './getNatsConfig';
8
+ import {
9
+ useNatsSession,
10
+ NatsSessionEvent,
11
+ NatsProgressEvent,
12
+ NatsDialogResponseEvent,
13
+ NatsErrorEvent,
14
+ } from './useNatsSession';
15
+
16
+ export interface UseNatsOptions {
17
+ /** Same baseUrl used for /api/tts and /api/stt. */
18
+ baseUrl: string;
19
+ /** Current session UUID. Subscription is skipped while undefined. */
20
+ sessionId?: string;
21
+ /** `progress` events (e.g. to feed the typing indicator). */
22
+ onProgress?: (event: NatsProgressEvent) => void;
23
+ /** `dialog.text_entered_response` events (optional live updates). */
24
+ onDialogResponse?: (event: NatsDialogResponseEvent) => void;
25
+ /** `error` events (logging / user notification). */
26
+ onError?: (event: NatsErrorEvent) => void;
27
+ }
28
+
29
+ /**
30
+ * Subscribe to the NATS session channel and dispatch decoded events to the
31
+ * provided callbacks. Config is fetched from `/api/nats`; the subscription
32
+ * lifecycle (cleanup on unmount, reconnect on sessionId change) is handled by
33
+ * `useNatsSession`.
34
+ */
35
+ export function useNats({
36
+ baseUrl,
37
+ sessionId,
38
+ onProgress,
39
+ onDialogResponse,
40
+ onError,
41
+ }: UseNatsOptions) {
42
+ const [config, setConfig] = useState<{ url: string; token: string } | null>(
43
+ null
44
+ );
45
+ const [configError, setConfigError] = useState<Error | null>(null);
46
+
47
+ // Keep callbacks in refs so the dispatcher identity stays stable.
48
+ const onProgressRef = useRef(onProgress);
49
+ const onDialogResponseRef = useRef(onDialogResponse);
50
+ const onErrorRef = useRef(onError);
51
+ useEffect(() => {
52
+ onProgressRef.current = onProgress;
53
+ onDialogResponseRef.current = onDialogResponse;
54
+ onErrorRef.current = onError;
55
+ }, [onProgress, onDialogResponse, onError]);
56
+
57
+ // Fetch connection config whenever the active session changes.
58
+ useEffect(() => {
59
+ if (!sessionId) {
60
+ console.debug('[NATS] no sessionId, skipping config fetch');
61
+ setConfig(null);
62
+ setConfigError(null);
63
+ return;
64
+ }
65
+
66
+ const controller = new AbortController();
67
+ let cancelled = false;
68
+
69
+ console.info(
70
+ '[NATS] fetching config from',
71
+ `${baseUrl}/api/nats`,
72
+ 'for session',
73
+ sessionId
74
+ );
75
+ getNatsConfig(baseUrl, sessionId, controller.signal)
76
+ .then(cfg => {
77
+ if (!cancelled) {
78
+ console.info('[NATS] config received, server url:', cfg.url);
79
+ setConfig(cfg);
80
+ setConfigError(null);
81
+ }
82
+ })
83
+ .catch(err => {
84
+ if (!cancelled && err?.name !== 'AbortError') {
85
+ console.error('[NATS] config error', err);
86
+ setConfig(null);
87
+ setConfigError(err instanceof Error ? err : new Error(String(err)));
88
+ }
89
+ });
90
+
91
+ return () => {
92
+ cancelled = true;
93
+ controller.abort();
94
+ };
95
+ }, [baseUrl, sessionId]);
96
+
97
+ const handleMessage = useCallback((event: NatsSessionEvent) => {
98
+ console.debug('[NATS] dispatching event', { eventType: event.eventType });
99
+ switch (event.eventType) {
100
+ case 'progress':
101
+ onProgressRef.current?.(event);
102
+ break;
103
+ case 'dialog_text_entered_response':
104
+ onDialogResponseRef.current?.(event);
105
+ break;
106
+ case 'error':
107
+ onErrorRef.current?.(event);
108
+ break;
109
+ default:
110
+ console.warn('Unknown NATS event', event);
111
+ }
112
+ }, []);
113
+
114
+ useNatsSession(sessionId, config?.url, config?.token, handleMessage);
115
+
116
+ return {
117
+ /** True once connection config has been retrieved. */
118
+ connected: !!config,
119
+ /** Last config-retrieval error, if any. */
120
+ configError,
121
+ };
122
+ }