@memori.ai/memori-react 8.39.0 → 8.40.1
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/CHANGELOG.md +20 -0
- package/dist/components/MemoriWidget/MemoriWidget.d.ts +2 -1
- package/dist/components/MemoriWidget/MemoriWidget.js +450 -146
- package/dist/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/dist/helpers/credits.d.ts +3 -2
- package/dist/helpers/credits.js +4 -3
- package/dist/helpers/credits.js.map +1 -1
- package/dist/helpers/nats/getNatsConfig.d.ts +5 -0
- package/dist/helpers/nats/getNatsConfig.js +29 -0
- package/dist/helpers/nats/getNatsConfig.js.map +1 -0
- package/dist/helpers/nats/useNats.d.ts +12 -0
- package/dist/helpers/nats/useNats.js +72 -0
- package/dist/helpers/nats/useNats.js.map +1 -0
- package/dist/helpers/nats/useNatsSession.d.ts +27 -0
- package/dist/helpers/nats/useNatsSession.js +108 -0
- package/dist/helpers/nats/useNatsSession.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/esm/components/MemoriWidget/MemoriWidget.d.ts +2 -1
- package/esm/components/MemoriWidget/MemoriWidget.js +453 -147
- package/esm/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/esm/helpers/credits.d.ts +3 -2
- package/esm/helpers/credits.js +4 -3
- package/esm/helpers/credits.js.map +1 -1
- package/esm/helpers/nats/getNatsConfig.d.ts +5 -0
- package/esm/helpers/nats/getNatsConfig.js +25 -0
- package/esm/helpers/nats/getNatsConfig.js.map +1 -0
- package/esm/helpers/nats/useNats.d.ts +12 -0
- package/esm/helpers/nats/useNats.js +68 -0
- package/esm/helpers/nats/useNats.js.map +1 -0
- package/esm/helpers/nats/useNatsSession.d.ts +27 -0
- package/esm/helpers/nats/useNatsSession.js +103 -0
- package/esm/helpers/nats/useNatsSession.js.map +1 -0
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/package.json +3 -2
- package/src/components/MemoriWidget/MemoriWidget.tsx +543 -149
- package/src/components/layouts/layouts.stories.tsx +29 -35
- package/src/helpers/credits.ts +6 -3
- package/src/helpers/nats/getNatsConfig.ts +69 -0
- package/src/helpers/nats/useNats.ts +122 -0
- package/src/helpers/nats/useNatsSession.ts +210 -0
- package/src/index.stories.tsx +19 -3
- package/src/index.tsx +1 -0
- package/src/version.ts +1 -1
|
@@ -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>
|
|
@@ -45,7 +43,7 @@ DefaultLayout.args = {
|
|
|
45
43
|
uiLang: 'IT',
|
|
46
44
|
spokenLang: 'IT',
|
|
47
45
|
integrationID: '0b1256c1-530c-4e67-aef8-36667c8887bb',
|
|
48
|
-
autoStart:
|
|
46
|
+
autoStart: true,
|
|
49
47
|
sessionID: '' as string | undefined,
|
|
50
48
|
showUpload: true,
|
|
51
49
|
showReasoning: false,
|
|
@@ -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:
|
|
158
|
-
ownerUserName:
|
|
159
|
-
memoriID:
|
|
160
|
-
ownerUserID:
|
|
161
|
-
tenantID:
|
|
162
|
-
engineURL:
|
|
163
|
-
apiURL:
|
|
164
|
-
baseURL:
|
|
165
|
-
layout:
|
|
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:
|
|
168
|
-
spokenLang:
|
|
161
|
+
uiLang: 'IT',
|
|
162
|
+
spokenLang: 'IT',
|
|
169
163
|
showOnlyLastMessages: true,
|
|
170
|
-
integrationID:
|
|
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:
|
|
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:
|
|
232
|
-
ownerUserName:
|
|
233
|
-
memoriID:
|
|
234
|
-
ownerUserID:
|
|
235
|
-
tenantID:
|
|
236
|
-
engineURL:
|
|
237
|
-
apiURL:
|
|
238
|
-
baseURL:
|
|
239
|
-
layout:
|
|
240
|
-
uiLang:
|
|
241
|
-
spokenLang:
|
|
242
|
-
integrationID:
|
|
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
|
};
|
package/src/helpers/credits.ts
CHANGED
|
@@ -10,12 +10,14 @@ export const getCredits = async ({
|
|
|
10
10
|
operation = 'session_creation',
|
|
11
11
|
baseUrl,
|
|
12
12
|
userID,
|
|
13
|
+
userName,
|
|
13
14
|
tenant,
|
|
14
15
|
characters,
|
|
15
16
|
}: {
|
|
16
17
|
operation?: CreditsOperation;
|
|
17
18
|
baseUrl: string;
|
|
18
|
-
userID
|
|
19
|
+
userID?: string | null;
|
|
20
|
+
userName?: string | null;
|
|
19
21
|
tenant: string;
|
|
20
22
|
characters?: number;
|
|
21
23
|
}): Promise<{
|
|
@@ -23,8 +25,8 @@ export const getCredits = async ({
|
|
|
23
25
|
required: number;
|
|
24
26
|
tokens?: number;
|
|
25
27
|
}> => {
|
|
26
|
-
if (!userID) {
|
|
27
|
-
throw new Error('userID must be provided');
|
|
28
|
+
if (!userID && !userName) {
|
|
29
|
+
throw new Error('Either userID or userName must be provided');
|
|
28
30
|
}
|
|
29
31
|
if (operation === 'import_document' && characters == null) {
|
|
30
32
|
throw new Error('characters must be provided for import_document');
|
|
@@ -38,6 +40,7 @@ export const getCredits = async ({
|
|
|
38
40
|
body: JSON.stringify({
|
|
39
41
|
operation,
|
|
40
42
|
userID,
|
|
43
|
+
userName,
|
|
41
44
|
tenant,
|
|
42
45
|
...(operation === 'import_document' ? { characters } : {}),
|
|
43
46
|
}),
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// helpers/nats/useNatsSession.ts - Subscribe to a session's NATS channel.
|
|
2
|
+
//
|
|
3
|
+
// Opens a W3C WebSocket connection (via `wsconnect`), subscribes to the
|
|
4
|
+
// session subject (`sessions.<sessionId>`) and forwards every decoded event to
|
|
5
|
+
// `onMessage`. The cleanup (`nc.close()`) is essential: without it, every
|
|
6
|
+
// `sessionId` change would leave orphan connections / iterators.
|
|
7
|
+
import { useEffect, useRef } from 'react';
|
|
8
|
+
import { wsconnect, NatsConnection } from '@nats-io/nats-core';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `progress` event: job advancement updates.
|
|
12
|
+
*/
|
|
13
|
+
export interface NatsProgressEvent {
|
|
14
|
+
eventType: 'progress';
|
|
15
|
+
jobId?: string;
|
|
16
|
+
currentStep?: number;
|
|
17
|
+
finalStep?: number;
|
|
18
|
+
message?: string;
|
|
19
|
+
startingTime?: string;
|
|
20
|
+
correlationID?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `dialog.text_entered_response` event: same body returned by
|
|
25
|
+
* `POST /memori/v2/EnterTextAsync/{sessionId}` (via NATS).
|
|
26
|
+
*/
|
|
27
|
+
export interface NatsDialogResponseEvent {
|
|
28
|
+
eventType: 'dialog_text_entered_response';
|
|
29
|
+
requestID?: string;
|
|
30
|
+
resultCode?: number;
|
|
31
|
+
resultMessage?: string;
|
|
32
|
+
currentState?: any;
|
|
33
|
+
correlationID?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `error` event.
|
|
38
|
+
*/
|
|
39
|
+
export interface NatsErrorEvent {
|
|
40
|
+
eventType: 'error';
|
|
41
|
+
errorCode?: string | number;
|
|
42
|
+
errorMessage?: string;
|
|
43
|
+
backtrace?: string;
|
|
44
|
+
correlationID?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Discriminated union of all events received on the session subject.
|
|
49
|
+
*/
|
|
50
|
+
export type NatsSessionEvent =
|
|
51
|
+
| NatsProgressEvent
|
|
52
|
+
| NatsDialogResponseEvent
|
|
53
|
+
| NatsErrorEvent;
|
|
54
|
+
|
|
55
|
+
function readString(
|
|
56
|
+
raw: Record<string, unknown>,
|
|
57
|
+
camelKey: string,
|
|
58
|
+
snakeKey: string
|
|
59
|
+
): string | undefined {
|
|
60
|
+
const value = raw[camelKey] ?? raw[snakeKey];
|
|
61
|
+
return typeof value === 'string' ? value : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readNumber(
|
|
65
|
+
raw: Record<string, unknown>,
|
|
66
|
+
camelKey: string,
|
|
67
|
+
snakeKey: string
|
|
68
|
+
): number | undefined {
|
|
69
|
+
const value = raw[camelKey] ?? raw[snakeKey];
|
|
70
|
+
return typeof value === 'number' ? value : undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Normalizes raw NATS payloads (snake_case or camelCase) into typed events.
|
|
75
|
+
*/
|
|
76
|
+
export function normalizeNatsEvent(raw: Record<string, unknown>): NatsSessionEvent {
|
|
77
|
+
const eventType = (readString(raw, 'eventType', 'event_type') ??
|
|
78
|
+
'unknown') as NatsSessionEvent['eventType'];
|
|
79
|
+
|
|
80
|
+
const correlationID =
|
|
81
|
+
readString(raw, 'correlationID', 'correlation_id') ??
|
|
82
|
+
readString(raw, 'correlationId', 'correlation_id');
|
|
83
|
+
|
|
84
|
+
if (eventType === 'progress') {
|
|
85
|
+
return {
|
|
86
|
+
eventType: 'progress',
|
|
87
|
+
jobId: readString(raw, 'jobId', 'job_id'),
|
|
88
|
+
currentStep: readNumber(raw, 'currentStep', 'current_step'),
|
|
89
|
+
finalStep: readNumber(raw, 'finalStep', 'final_step'),
|
|
90
|
+
message: typeof raw.message === 'string' ? raw.message : undefined,
|
|
91
|
+
startingTime: readString(raw, 'startingTime', 'starting_time'),
|
|
92
|
+
correlationID,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (eventType === 'dialog_text_entered_response') {
|
|
97
|
+
return {
|
|
98
|
+
eventType: 'dialog_text_entered_response',
|
|
99
|
+
requestID:
|
|
100
|
+
typeof raw.requestID === 'string' ? raw.requestID : undefined,
|
|
101
|
+
resultCode:
|
|
102
|
+
typeof raw.resultCode === 'number' ? raw.resultCode : undefined,
|
|
103
|
+
resultMessage:
|
|
104
|
+
typeof raw.resultMessage === 'string' ? raw.resultMessage : undefined,
|
|
105
|
+
currentState: raw.currentState,
|
|
106
|
+
correlationID,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (eventType === 'error') {
|
|
111
|
+
return {
|
|
112
|
+
eventType: 'error',
|
|
113
|
+
errorCode: (raw.errorCode ?? raw.error_code) as string | number | undefined,
|
|
114
|
+
errorMessage: readString(raw, 'errorMessage', 'error_message'),
|
|
115
|
+
backtrace: typeof raw.backtrace === 'string' ? raw.backtrace : undefined,
|
|
116
|
+
correlationID,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
eventType,
|
|
122
|
+
correlationID,
|
|
123
|
+
} as NatsSessionEvent;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Open a NATS WebSocket connection, subscribe to the session subject and
|
|
128
|
+
* forward decoded events to `onMessage`.
|
|
129
|
+
*
|
|
130
|
+
* `onMessage` is kept in a ref so callers do not need to memoize it: the
|
|
131
|
+
* connection is only (re)opened when `sessionId`, `natsWsUrl` or `natsToken`
|
|
132
|
+
* change.
|
|
133
|
+
*
|
|
134
|
+
* @param sessionId Current session UUID (subscription is skipped when falsy).
|
|
135
|
+
* @param natsWsUrl WebSocket URL of the NATS server.
|
|
136
|
+
* @param natsToken Bearer token for authentication.
|
|
137
|
+
* @param onMessage Callback invoked for each decoded event.
|
|
138
|
+
*/
|
|
139
|
+
export function useNatsSession(
|
|
140
|
+
sessionId: string | undefined,
|
|
141
|
+
natsWsUrl: string | undefined,
|
|
142
|
+
natsToken: string | undefined,
|
|
143
|
+
onMessage: (event: NatsSessionEvent) => void
|
|
144
|
+
) {
|
|
145
|
+
const connRef = useRef<NatsConnection | null>(null);
|
|
146
|
+
const onMessageRef = useRef(onMessage);
|
|
147
|
+
|
|
148
|
+
// Keep the latest callback without retriggering the connection effect.
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
onMessageRef.current = onMessage;
|
|
151
|
+
}, [onMessage]);
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!sessionId || !natsWsUrl || !natsToken) {
|
|
155
|
+
console.debug(
|
|
156
|
+
'[NATS] subscription skipped (missing sessionId/url/token)',
|
|
157
|
+
{ sessionId, hasUrl: !!natsWsUrl, hasToken: !!natsToken }
|
|
158
|
+
);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
let closed = false;
|
|
162
|
+
const subject = sessionId;
|
|
163
|
+
|
|
164
|
+
(async () => {
|
|
165
|
+
console.info('[NATS] connecting to', natsWsUrl, 'for session', sessionId);
|
|
166
|
+
const nc = await wsconnect({
|
|
167
|
+
servers: [natsWsUrl],
|
|
168
|
+
token: natsToken,
|
|
169
|
+
});
|
|
170
|
+
if (closed) {
|
|
171
|
+
console.debug(
|
|
172
|
+
'[NATS] connection established after cleanup, closing immediately'
|
|
173
|
+
);
|
|
174
|
+
await nc.close();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
connRef.current = nc;
|
|
178
|
+
console.info('[NATS] connected to', nc.getServer());
|
|
179
|
+
|
|
180
|
+
const sub = nc.subscribe(subject);
|
|
181
|
+
console.info('[NATS] subscribed to subject', subject);
|
|
182
|
+
let received = 0;
|
|
183
|
+
for await (const msg of sub) {
|
|
184
|
+
received += 1;
|
|
185
|
+
try {
|
|
186
|
+
const payload = normalizeNatsEvent(
|
|
187
|
+
msg.json<Record<string, unknown>>()
|
|
188
|
+
);
|
|
189
|
+
console.debug(
|
|
190
|
+
`[NATS] message #${received} on ${subject} (eventType=${payload.eventType})`,
|
|
191
|
+
payload
|
|
192
|
+
);
|
|
193
|
+
onMessageRef.current(payload);
|
|
194
|
+
} catch (err) {
|
|
195
|
+
console.error('[NATS] message decode error', err);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
console.info(
|
|
199
|
+
`[NATS] subscription on ${subject} closed (received ${received} messages)`
|
|
200
|
+
);
|
|
201
|
+
})().catch(err => console.error('[NATS] connection error', err));
|
|
202
|
+
|
|
203
|
+
return () => {
|
|
204
|
+
console.info('[NATS] cleanup: closing connection for subject', subject);
|
|
205
|
+
closed = true;
|
|
206
|
+
connRef.current?.close();
|
|
207
|
+
connRef.current = null;
|
|
208
|
+
};
|
|
209
|
+
}, [sessionId, natsWsUrl, natsToken]);
|
|
210
|
+
}
|
package/src/index.stories.tsx
CHANGED
|
@@ -22,7 +22,6 @@ export default meta;
|
|
|
22
22
|
|
|
23
23
|
const Template: Story<Props> = args => <Memori {...args} />;
|
|
24
24
|
|
|
25
|
-
|
|
26
25
|
export const WithInitialContextAndQuestion = Template.bind({});
|
|
27
26
|
WithInitialContextAndQuestion.args = {
|
|
28
27
|
ownerUserName: 'nzambello',
|
|
@@ -150,7 +149,8 @@ const piiDetectionConfig = {
|
|
|
150
149
|
{
|
|
151
150
|
id: 'iban',
|
|
152
151
|
label: { it: 'IBAN', en: 'IBAN' },
|
|
153
|
-
pattern:
|
|
152
|
+
pattern:
|
|
153
|
+
'\\b[A-Z]{2}\\d{2}(?:[ ]?[A-Z0-9]{4}){3,7}(?:[ ]?[A-Z0-9]{1,4})?\\b',
|
|
154
154
|
message: {
|
|
155
155
|
it: 'Il messaggio contiene un codice IBAN.',
|
|
156
156
|
en: 'The message contains an IBAN code.',
|
|
@@ -185,4 +185,20 @@ WithPiiDetection.args = {
|
|
|
185
185
|
lang: 'it',
|
|
186
186
|
}),
|
|
187
187
|
},
|
|
188
|
-
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const WithLocalNats = Template.bind({});
|
|
191
|
+
WithLocalNats.args = {
|
|
192
|
+
memoriName: 'test324',
|
|
193
|
+
ownerUserName: 'andrea.patini',
|
|
194
|
+
memoriID: 'd661a9ca-e907-4396-a986-5095ccd582d6',
|
|
195
|
+
ownerUserID: '69fcc557-9cb6-4e5e-b8ab-140cff975492',
|
|
196
|
+
tenantID: 'localhost:3000',
|
|
197
|
+
engineURL: 'http://localhost:7778/memori/v2',
|
|
198
|
+
apiURL: 'http://localhost:7778/api/v2',
|
|
199
|
+
baseURL: 'http://localhost:3000',
|
|
200
|
+
layout: 'FULLPAGE',
|
|
201
|
+
uiLang: 'IT',
|
|
202
|
+
spokenLang: 'IT',
|
|
203
|
+
integrationID: 'ee1c3d98-7819-4506-ba28-818e79ba86cb',
|
|
204
|
+
};
|
package/src/index.tsx
CHANGED
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// This file is auto-generated. Do not edit manually.
|
|
2
|
-
export const version = '8.
|
|
2
|
+
export const version = '8.40.1';
|