@memori.ai/memori-react 9.0.0-alpha.22 → 9.0.0-alpha.23
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 +435 -0
- package/dist/components/ChatBubble/ChatBubble.js +12 -7
- package/dist/components/ChatBubble/ChatBubble.js.map +1 -1
- package/dist/components/FeedbackButtons/FeedbackButtons.js +2 -2
- package/dist/components/FeedbackButtons/FeedbackButtons.js.map +1 -1
- package/dist/components/MemoriWidget/MemoriWidget.d.ts +1 -0
- package/dist/components/MemoriWidget/MemoriWidget.js +58 -1
- package/dist/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/dist/components/layouts/chat.css +5 -2
- package/dist/helpers/nats/getNatsConfig.js.map +1 -1
- package/dist/helpers/nats/useNats.d.ts +2 -1
- package/dist/helpers/nats/useNats.js +87 -20
- package/dist/helpers/nats/useNats.js.map +1 -1
- package/dist/helpers/nats/useNatsSession.d.ts +6 -1
- package/dist/helpers/nats/useNatsSession.js +48 -7
- package/dist/helpers/nats/useNatsSession.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/esm/components/ChatBubble/ChatBubble.js +12 -7
- package/esm/components/ChatBubble/ChatBubble.js.map +1 -1
- package/esm/components/FeedbackButtons/FeedbackButtons.js +2 -2
- package/esm/components/FeedbackButtons/FeedbackButtons.js.map +1 -1
- package/esm/components/MemoriWidget/MemoriWidget.d.ts +1 -0
- package/esm/components/MemoriWidget/MemoriWidget.js +57 -1
- package/esm/components/MemoriWidget/MemoriWidget.js.map +1 -1
- package/esm/components/layouts/chat.css +5 -2
- package/esm/helpers/nats/getNatsConfig.js.map +1 -1
- package/esm/helpers/nats/useNats.d.ts +2 -1
- package/esm/helpers/nats/useNats.js +87 -20
- package/esm/helpers/nats/useNats.js.map +1 -1
- package/esm/helpers/nats/useNatsSession.d.ts +6 -1
- package/esm/helpers/nats/useNatsSession.js +48 -8
- package/esm/helpers/nats/useNatsSession.js.map +1 -1
- package/esm/styles.css +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/package.json +4 -3
- package/src/components/Chat/__snapshots__/Chat.test.tsx.snap +71 -71
- package/src/components/ChatBubble/ChatBubble.stories.tsx +11 -0
- package/src/components/ChatBubble/ChatBubble.test.tsx +16 -0
- package/src/components/ChatBubble/ChatBubble.tsx +307 -292
- package/src/components/ChatBubble/__snapshots__/ChatBubble.test.tsx.snap +268 -121
- package/src/components/FeedbackButtons/FeedbackButtons.tsx +2 -0
- package/src/components/FeedbackButtons/__snapshots__/FeedbackButtons.test.tsx.snap +1 -1
- package/src/components/KnownFacts/__snapshots__/KnownFacts.test.tsx.snap +6 -2
- package/src/components/LoginDrawer/__snapshots__/LoginDrawer.test.tsx.snap +33 -11
- package/src/components/MemoriWidget/MemoriWidget.tsx +65 -1
- package/src/components/WhyThisAnswer/__snapshots__/WhyThisAnswer.test.tsx.snap +9 -3
- package/src/components/layouts/chat.css +5 -2
- package/src/helpers/nats/getNatsConfig.ts +4 -1
- package/src/helpers/nats/useNats.test.tsx +81 -0
- package/src/helpers/nats/useNats.ts +109 -29
- package/src/helpers/nats/useNatsSession.test.ts +16 -0
- package/src/helpers/nats/useNatsSession.ts +79 -13
- package/src/version.ts +1 -1
|
@@ -94,6 +94,7 @@ const FeedbackButtons = ({
|
|
|
94
94
|
variant="ghost"
|
|
95
95
|
aria-label={feedbackLabel}
|
|
96
96
|
disabled={!!clicked}
|
|
97
|
+
size="sm"
|
|
97
98
|
icon={
|
|
98
99
|
<MessageSquare
|
|
99
100
|
className={
|
|
@@ -161,6 +162,7 @@ const FeedbackButtons = ({
|
|
|
161
162
|
}
|
|
162
163
|
}}
|
|
163
164
|
variant="ghost"
|
|
165
|
+
size="sm"
|
|
164
166
|
shape="circle"
|
|
165
167
|
icon={
|
|
166
168
|
<MessageSquare
|
|
@@ -14,7 +14,7 @@ exports[`renders FeedbackButtons unchanged 1`] = `
|
|
|
14
14
|
>
|
|
15
15
|
<button
|
|
16
16
|
aria-label="feedback"
|
|
17
|
-
class="memori-button memori-button--ghost memori-button--
|
|
17
|
+
class="memori-button memori-button--ghost memori-button--sm memori-button--circle memori-button--icon-only"
|
|
18
18
|
tabindex="0"
|
|
19
19
|
type="button"
|
|
20
20
|
>
|
|
@@ -15,7 +15,9 @@ exports[`renders KnownFacts hidden unchanged 1`] = `
|
|
|
15
15
|
`;
|
|
16
16
|
|
|
17
17
|
exports[`renders KnownFacts visible unchanged 1`] = `
|
|
18
|
-
<div
|
|
18
|
+
<div
|
|
19
|
+
data-base-ui-inert=""
|
|
20
|
+
>
|
|
19
21
|
<div
|
|
20
22
|
aria-atomic="false"
|
|
21
23
|
aria-label="Notifications"
|
|
@@ -29,7 +31,9 @@ exports[`renders KnownFacts visible unchanged 1`] = `
|
|
|
29
31
|
`;
|
|
30
32
|
|
|
31
33
|
exports[`renders KnownFacts with data unchanged 1`] = `
|
|
32
|
-
<div
|
|
34
|
+
<div
|
|
35
|
+
data-base-ui-inert=""
|
|
36
|
+
>
|
|
33
37
|
<div
|
|
34
38
|
aria-atomic="false"
|
|
35
39
|
aria-label="Notifications"
|
|
@@ -15,7 +15,9 @@ exports[`renders LoginDrawer closed unchanged 1`] = `
|
|
|
15
15
|
`;
|
|
16
16
|
|
|
17
17
|
exports[`renders LoginDrawer logged in unchanged 1`] = `
|
|
18
|
-
<div
|
|
18
|
+
<div
|
|
19
|
+
data-base-ui-inert=""
|
|
20
|
+
>
|
|
19
21
|
<div
|
|
20
22
|
aria-atomic="false"
|
|
21
23
|
aria-label="Notifications"
|
|
@@ -29,7 +31,9 @@ exports[`renders LoginDrawer logged in unchanged 1`] = `
|
|
|
29
31
|
`;
|
|
30
32
|
|
|
31
33
|
exports[`renders LoginDrawer logged with missing data unchanged 1`] = `
|
|
32
|
-
<div
|
|
34
|
+
<div
|
|
35
|
+
data-base-ui-inert=""
|
|
36
|
+
>
|
|
33
37
|
<div
|
|
34
38
|
aria-atomic="false"
|
|
35
39
|
aria-label="Notifications"
|
|
@@ -43,7 +47,9 @@ exports[`renders LoginDrawer logged with missing data unchanged 1`] = `
|
|
|
43
47
|
`;
|
|
44
48
|
|
|
45
49
|
exports[`renders LoginDrawer on change password unchanged 1`] = `
|
|
46
|
-
<div
|
|
50
|
+
<div
|
|
51
|
+
data-base-ui-inert=""
|
|
52
|
+
>
|
|
47
53
|
<div
|
|
48
54
|
aria-atomic="false"
|
|
49
55
|
aria-label="Notifications"
|
|
@@ -57,7 +63,9 @@ exports[`renders LoginDrawer on change password unchanged 1`] = `
|
|
|
57
63
|
`;
|
|
58
64
|
|
|
59
65
|
exports[`renders LoginDrawer on signup unchanged 1`] = `
|
|
60
|
-
<div
|
|
66
|
+
<div
|
|
67
|
+
data-base-ui-inert=""
|
|
68
|
+
>
|
|
61
69
|
<div
|
|
62
70
|
aria-atomic="false"
|
|
63
71
|
aria-label="Notifications"
|
|
@@ -71,7 +79,9 @@ exports[`renders LoginDrawer on signup unchanged 1`] = `
|
|
|
71
79
|
`;
|
|
72
80
|
|
|
73
81
|
exports[`renders LoginDrawer on signup waiting for otp unchanged 1`] = `
|
|
74
|
-
<div
|
|
82
|
+
<div
|
|
83
|
+
data-base-ui-inert=""
|
|
84
|
+
>
|
|
75
85
|
<div
|
|
76
86
|
aria-atomic="false"
|
|
77
87
|
aria-label="Notifications"
|
|
@@ -85,7 +95,9 @@ exports[`renders LoginDrawer on signup waiting for otp unchanged 1`] = `
|
|
|
85
95
|
`;
|
|
86
96
|
|
|
87
97
|
exports[`renders LoginDrawer open unchanged 1`] = `
|
|
88
|
-
<div
|
|
98
|
+
<div
|
|
99
|
+
data-base-ui-inert=""
|
|
100
|
+
>
|
|
89
101
|
<div
|
|
90
102
|
aria-atomic="false"
|
|
91
103
|
aria-label="Notifications"
|
|
@@ -99,7 +111,9 @@ exports[`renders LoginDrawer open unchanged 1`] = `
|
|
|
99
111
|
`;
|
|
100
112
|
|
|
101
113
|
exports[`renders LoginDrawer unlogged unchanged 1`] = `
|
|
102
|
-
<div
|
|
114
|
+
<div
|
|
115
|
+
data-base-ui-inert=""
|
|
116
|
+
>
|
|
103
117
|
<div
|
|
104
118
|
aria-atomic="false"
|
|
105
119
|
aria-label="Notifications"
|
|
@@ -113,7 +127,9 @@ exports[`renders LoginDrawer unlogged unchanged 1`] = `
|
|
|
113
127
|
`;
|
|
114
128
|
|
|
115
129
|
exports[`renders LoginDrawer with OTP form and error unchanged 1`] = `
|
|
116
|
-
<div
|
|
130
|
+
<div
|
|
131
|
+
data-base-ui-inert=""
|
|
132
|
+
>
|
|
117
133
|
<div
|
|
118
134
|
aria-atomic="false"
|
|
119
135
|
aria-label="Notifications"
|
|
@@ -127,7 +143,9 @@ exports[`renders LoginDrawer with OTP form and error unchanged 1`] = `
|
|
|
127
143
|
`;
|
|
128
144
|
|
|
129
145
|
exports[`renders LoginDrawer with OTP form and timer unchanged 1`] = `
|
|
130
|
-
<div
|
|
146
|
+
<div
|
|
147
|
+
data-base-ui-inert=""
|
|
148
|
+
>
|
|
131
149
|
<div
|
|
132
150
|
aria-atomic="false"
|
|
133
151
|
aria-label="Notifications"
|
|
@@ -141,7 +159,9 @@ exports[`renders LoginDrawer with OTP form and timer unchanged 1`] = `
|
|
|
141
159
|
`;
|
|
142
160
|
|
|
143
161
|
exports[`renders LoginDrawer with OTP form loading unchanged 1`] = `
|
|
144
|
-
<div
|
|
162
|
+
<div
|
|
163
|
+
data-base-ui-inert=""
|
|
164
|
+
>
|
|
145
165
|
<div
|
|
146
166
|
aria-atomic="false"
|
|
147
167
|
aria-label="Notifications"
|
|
@@ -155,7 +175,9 @@ exports[`renders LoginDrawer with OTP form loading unchanged 1`] = `
|
|
|
155
175
|
`;
|
|
156
176
|
|
|
157
177
|
exports[`renders LoginDrawer with OTP form unchanged 1`] = `
|
|
158
|
-
<div
|
|
178
|
+
<div
|
|
179
|
+
data-base-ui-inert=""
|
|
180
|
+
>
|
|
159
181
|
<div
|
|
160
182
|
aria-atomic="false"
|
|
161
183
|
aria-label="Notifications"
|
|
@@ -176,6 +176,22 @@ function readCorrelationID(response: {
|
|
|
176
176
|
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
/** Stable engine-owned fields used to tell whether a pending turn completed. */
|
|
180
|
+
export function dialogStateFingerprint(state: DialogState | undefined): string {
|
|
181
|
+
if (!state) return '';
|
|
182
|
+
return JSON.stringify({
|
|
183
|
+
state: state.state,
|
|
184
|
+
previousState: state.previousState,
|
|
185
|
+
emission: state.emission,
|
|
186
|
+
emitter: state.emitter,
|
|
187
|
+
lastMatchedMemoryID: state.lastMatchedMemoryID,
|
|
188
|
+
currentDate: state.currentDate,
|
|
189
|
+
currentMemoryID: state.currentMemoryID,
|
|
190
|
+
contextVars: state.contextVars,
|
|
191
|
+
emittedMedia: state.emittedMedia,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
179
195
|
type MemoriTextEnteredEvent = CustomEvent<{
|
|
180
196
|
text: string;
|
|
181
197
|
waitForPrevious?: boolean;
|
|
@@ -789,6 +805,7 @@ const MemoriWidget = ({
|
|
|
789
805
|
|
|
790
806
|
type PendingEnterText = EnterTextRetryParams & {
|
|
791
807
|
msg?: string;
|
|
808
|
+
stateBeforeRequest?: string;
|
|
792
809
|
waitForResponse?: {
|
|
793
810
|
resolve: (event: NatsDialogResponseEvent) => void;
|
|
794
811
|
reject: (error: Error) => void;
|
|
@@ -1179,6 +1196,7 @@ const MemoriWidget = ({
|
|
|
1179
1196
|
if (response.resultCode === 0 && correlationID) {
|
|
1180
1197
|
registerPendingEnterText(correlationID, {
|
|
1181
1198
|
msg,
|
|
1199
|
+
stateBeforeRequest: dialogStateFingerprint(currentDialogState),
|
|
1182
1200
|
text,
|
|
1183
1201
|
media,
|
|
1184
1202
|
translate,
|
|
@@ -2503,6 +2521,7 @@ const MemoriWidget = ({
|
|
|
2503
2521
|
}, timeoutMs);
|
|
2504
2522
|
|
|
2505
2523
|
registerPendingEnterText(correlationID, {
|
|
2524
|
+
stateBeforeRequest: dialogStateFingerprint(currentDialogState),
|
|
2506
2525
|
waitForResponse: {
|
|
2507
2526
|
resolve: event => {
|
|
2508
2527
|
clearTimeout(timeoutId);
|
|
@@ -2516,9 +2535,53 @@ const MemoriWidget = ({
|
|
|
2516
2535
|
},
|
|
2517
2536
|
});
|
|
2518
2537
|
}),
|
|
2519
|
-
[registerPendingEnterText, clearEnterTextPending]
|
|
2538
|
+
[registerPendingEnterText, clearEnterTextPending, currentDialogState]
|
|
2520
2539
|
);
|
|
2521
2540
|
|
|
2541
|
+
const catchUpPendingEnterText = useCallback(async () => {
|
|
2542
|
+
if (!sessionId || pendingEnterTextRef.current.size === 0) return;
|
|
2543
|
+
|
|
2544
|
+
// Async turns are registered in send order. The current session state can
|
|
2545
|
+
// only represent the latest completed turn, so reconcile the latest one.
|
|
2546
|
+
const latest = Array.from(pendingEnterTextRef.current.entries()).pop();
|
|
2547
|
+
if (!latest) return;
|
|
2548
|
+
const [correlationID, pending] = latest;
|
|
2549
|
+
|
|
2550
|
+
try {
|
|
2551
|
+
const { currentState, resultCode, resultMessage, requestID } =
|
|
2552
|
+
await getSession(sessionId);
|
|
2553
|
+
|
|
2554
|
+
if (resultCode !== 0 || !currentState) {
|
|
2555
|
+
deliverEnterTextNatsResponse(correlationID, {
|
|
2556
|
+
eventType: 'dialog_text_entered_response',
|
|
2557
|
+
correlationID,
|
|
2558
|
+
requestID,
|
|
2559
|
+
resultCode,
|
|
2560
|
+
resultMessage,
|
|
2561
|
+
currentState,
|
|
2562
|
+
});
|
|
2563
|
+
return;
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
if (dialogStateFingerprint(currentState) === pending.stateBeforeRequest) {
|
|
2567
|
+
console.info('[NATS] catch-up: pending turn has not completed yet');
|
|
2568
|
+
return;
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
console.info('[NATS] catch-up: applying current engine session state');
|
|
2572
|
+
deliverEnterTextNatsResponse(correlationID, {
|
|
2573
|
+
eventType: 'dialog_text_entered_response',
|
|
2574
|
+
correlationID,
|
|
2575
|
+
requestID,
|
|
2576
|
+
resultCode,
|
|
2577
|
+
resultMessage,
|
|
2578
|
+
currentState,
|
|
2579
|
+
});
|
|
2580
|
+
} catch (error) {
|
|
2581
|
+
logWidgetError('NATS catch-up failed', error);
|
|
2582
|
+
}
|
|
2583
|
+
}, [sessionId, deliverEnterTextNatsResponse]);
|
|
2584
|
+
|
|
2522
2585
|
// NATS subscription: receives progress updates and the async enter-text response.
|
|
2523
2586
|
useNats({
|
|
2524
2587
|
baseUrl,
|
|
@@ -2543,6 +2606,7 @@ const MemoriWidget = ({
|
|
|
2543
2606
|
[deliverEnterTextNatsResponse]
|
|
2544
2607
|
),
|
|
2545
2608
|
onError: deliverEnterTextNatsError,
|
|
2609
|
+
onCatchUp: catchUpPendingEnterText,
|
|
2546
2610
|
});
|
|
2547
2611
|
|
|
2548
2612
|
const focusChatInput = () => {
|
|
@@ -15,7 +15,9 @@ exports[`renders WhyThisAnswer hidden unchanged 1`] = `
|
|
|
15
15
|
`;
|
|
16
16
|
|
|
17
17
|
exports[`renders WhyThisAnswer loading unchanged 1`] = `
|
|
18
|
-
<div
|
|
18
|
+
<div
|
|
19
|
+
data-base-ui-inert=""
|
|
20
|
+
>
|
|
19
21
|
<div
|
|
20
22
|
aria-atomic="false"
|
|
21
23
|
aria-label="Notifications"
|
|
@@ -29,7 +31,9 @@ exports[`renders WhyThisAnswer loading unchanged 1`] = `
|
|
|
29
31
|
`;
|
|
30
32
|
|
|
31
33
|
exports[`renders WhyThisAnswer visible unchanged 1`] = `
|
|
32
|
-
<div
|
|
34
|
+
<div
|
|
35
|
+
data-base-ui-inert=""
|
|
36
|
+
>
|
|
33
37
|
<div
|
|
34
38
|
aria-atomic="false"
|
|
35
39
|
aria-label="Notifications"
|
|
@@ -43,7 +47,9 @@ exports[`renders WhyThisAnswer visible unchanged 1`] = `
|
|
|
43
47
|
`;
|
|
44
48
|
|
|
45
49
|
exports[`renders WhyThisAnswer with data unchanged 1`] = `
|
|
46
|
-
<div
|
|
50
|
+
<div
|
|
51
|
+
data-base-ui-inert=""
|
|
52
|
+
>
|
|
47
53
|
<div
|
|
48
54
|
aria-atomic="false"
|
|
49
55
|
aria-label="Notifications"
|
|
@@ -129,7 +129,10 @@
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
.memori-widget.memori-layout-chat .memori-conversation-column > .memori-chat-inputs-wrapper .memori-chat-inputs,
|
|
132
|
-
.memori-widget.memori-layout-chat
|
|
132
|
+
.memori-widget.memori-layout-chat
|
|
133
|
+
.memori-conversation-column
|
|
134
|
+
> .memori-chat-inputs-wrapper
|
|
135
|
+
.memori-conversation-footer {
|
|
133
136
|
width: 100%;
|
|
134
137
|
max-width: var(--memori-conversation-max-width, 48rem);
|
|
135
138
|
margin-inline: auto;
|
|
@@ -262,7 +265,7 @@
|
|
|
262
265
|
|
|
263
266
|
.memori-widget.memori-layout-chat .memori-chat-layout--body .memori--start-panel__body {
|
|
264
267
|
padding: var(--memori-spacing-lg) var(--memori-spacing-xl);
|
|
265
|
-
padding-top: 0;
|
|
268
|
+
/* padding-top: 0; */
|
|
266
269
|
background: var(--memori-main-background);
|
|
267
270
|
}
|
|
268
271
|
|
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
export interface NatsConfig {
|
|
8
8
|
/** WebSocket URL of the NATS server (e.g. wss://nats.hz.slnode.net:8080). */
|
|
9
9
|
url: string;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Per-session NATS user JWT. Presented with `jwtAuthenticator` (bearer:
|
|
12
|
+
* no nkey seed on the client).
|
|
13
|
+
*/
|
|
11
14
|
token: string;
|
|
12
15
|
/**
|
|
13
16
|
* JetStream stream that stores session events. When set, the client consumes
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
2
|
+
import { getNatsConfig } from './getNatsConfig';
|
|
3
|
+
import { useNats } from './useNats';
|
|
4
|
+
import { NatsSessionLifecycle, useNatsSession } from './useNatsSession';
|
|
5
|
+
|
|
6
|
+
jest.mock('./getNatsConfig', () => ({
|
|
7
|
+
getNatsConfig: jest.fn(),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
jest.mock('./useNatsSession', () => ({
|
|
11
|
+
useNatsSession: jest.fn(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
const mockGetNatsConfig = getNatsConfig as jest.MockedFunction<
|
|
15
|
+
typeof getNatsConfig
|
|
16
|
+
>;
|
|
17
|
+
const mockUseNatsSession = useNatsSession as jest.MockedFunction<
|
|
18
|
+
typeof useNatsSession
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
function latestLifecycle(): NatsSessionLifecycle {
|
|
22
|
+
return mockUseNatsSession.mock.calls.at(-1)?.[3] ?? {};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('useNats credential lifecycle', () => {
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
jest.clearAllMocks();
|
|
28
|
+
mockGetNatsConfig.mockResolvedValue({
|
|
29
|
+
url: 'wss://nats.example.test',
|
|
30
|
+
token: 'fresh-jwt',
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(document, 'visibilityState', {
|
|
33
|
+
configurable: true,
|
|
34
|
+
value: 'visible',
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('refreshes the JWT on resume and catches up after reconnecting', async () => {
|
|
39
|
+
const onCatchUp = jest.fn();
|
|
40
|
+
renderHook(() =>
|
|
41
|
+
useNats({
|
|
42
|
+
baseUrl: 'https://dashboard.example.test',
|
|
43
|
+
sessionId: 'session-a',
|
|
44
|
+
onCatchUp,
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
await waitFor(() => expect(mockGetNatsConfig).toHaveBeenCalledTimes(1));
|
|
49
|
+
|
|
50
|
+
act(() => {
|
|
51
|
+
(document.dispatchEvent as (event: Event) => boolean)(
|
|
52
|
+
new Event('visibilitychange')
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
await waitFor(() => expect(mockGetNatsConfig).toHaveBeenCalledTimes(2));
|
|
56
|
+
|
|
57
|
+
act(() => latestLifecycle().onConnected?.());
|
|
58
|
+
expect(onCatchUp).toHaveBeenCalledTimes(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('refreshes the JWT after an authorization error', async () => {
|
|
62
|
+
const onCatchUp = jest.fn();
|
|
63
|
+
renderHook(() =>
|
|
64
|
+
useNats({
|
|
65
|
+
baseUrl: 'https://dashboard.example.test',
|
|
66
|
+
sessionId: 'session-a',
|
|
67
|
+
onCatchUp,
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
await waitFor(() => expect(mockGetNatsConfig).toHaveBeenCalledTimes(1));
|
|
72
|
+
|
|
73
|
+
act(() => {
|
|
74
|
+
latestLifecycle().onAuthError?.(new Error('Authorization Violation'));
|
|
75
|
+
});
|
|
76
|
+
await waitFor(() => expect(mockGetNatsConfig).toHaveBeenCalledTimes(2));
|
|
77
|
+
|
|
78
|
+
act(() => latestLifecycle().onConnected?.());
|
|
79
|
+
expect(onCatchUp).toHaveBeenCalledTimes(1);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -24,6 +24,11 @@ export interface UseNatsOptions {
|
|
|
24
24
|
onDialogResponse?: (event: NatsDialogResponseEvent) => void;
|
|
25
25
|
/** `error` events (logging / user notification). */
|
|
26
26
|
onError?: (event: NatsErrorEvent) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Reconcile a pending turn from the engine after a resumed or reauthenticated
|
|
29
|
+
* subscription is installed.
|
|
30
|
+
*/
|
|
31
|
+
onCatchUp?: () => void | Promise<void>;
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
/**
|
|
@@ -38,6 +43,7 @@ export function useNats({
|
|
|
38
43
|
onProgress,
|
|
39
44
|
onDialogResponse,
|
|
40
45
|
onError,
|
|
46
|
+
onCatchUp,
|
|
41
47
|
}: UseNatsOptions) {
|
|
42
48
|
const [config, setConfig] = useState<NatsConfig | null>(null);
|
|
43
49
|
const [configError, setConfigError] = useState<Error | null>(null);
|
|
@@ -46,33 +52,42 @@ export function useNats({
|
|
|
46
52
|
const onProgressRef = useRef(onProgress);
|
|
47
53
|
const onDialogResponseRef = useRef(onDialogResponse);
|
|
48
54
|
const onErrorRef = useRef(onError);
|
|
55
|
+
const onCatchUpRef = useRef(onCatchUp);
|
|
56
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
57
|
+
const catchUpAfterConnectRef = useRef(false);
|
|
58
|
+
const refreshInFlightRef = useRef(false);
|
|
59
|
+
const lastAuthRefreshRef = useRef(0);
|
|
49
60
|
useEffect(() => {
|
|
50
61
|
onProgressRef.current = onProgress;
|
|
51
62
|
onDialogResponseRef.current = onDialogResponse;
|
|
52
63
|
onErrorRef.current = onError;
|
|
53
|
-
|
|
64
|
+
onCatchUpRef.current = onCatchUp;
|
|
65
|
+
}, [onProgress, onDialogResponse, onError, onCatchUp]);
|
|
54
66
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
67
|
+
const refreshConfig = useCallback(
|
|
68
|
+
async (reason: 'initial' | 'resume' | 'auth') => {
|
|
69
|
+
if (!sessionId || refreshInFlightRef.current) return;
|
|
70
|
+
if (reason === 'auth' && Date.now() - lastAuthRefreshRef.current < 5000) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
refreshInFlightRef.current = true;
|
|
75
|
+
if (reason === 'auth') lastAuthRefreshRef.current = Date.now();
|
|
76
|
+
abortRef.current?.abort();
|
|
77
|
+
const controller = new AbortController();
|
|
78
|
+
abortRef.current = controller;
|
|
79
|
+
|
|
80
|
+
console.info(
|
|
81
|
+
'[NATS] fetching config from',
|
|
82
|
+
`${baseUrl}/api/nats`,
|
|
83
|
+
'for session',
|
|
84
|
+
sessionId,
|
|
85
|
+
`(${reason})`
|
|
86
|
+
);
|
|
87
|
+
try {
|
|
88
|
+
const cfg = await getNatsConfig(baseUrl, sessionId, controller.signal);
|
|
89
|
+
if (!controller.signal.aborted) {
|
|
90
|
+
catchUpAfterConnectRef.current = reason !== 'initial';
|
|
76
91
|
console.info('[NATS] config received', {
|
|
77
92
|
url: cfg.url,
|
|
78
93
|
jetStream: !!cfg.stream,
|
|
@@ -81,20 +96,65 @@ export function useNats({
|
|
|
81
96
|
setConfig(cfg);
|
|
82
97
|
setConfigError(null);
|
|
83
98
|
}
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
if (!cancelled && err?.name !== 'AbortError') {
|
|
99
|
+
} catch (err: any) {
|
|
100
|
+
if (!controller.signal.aborted && err?.name !== 'AbortError') {
|
|
87
101
|
console.error('[NATS] config error', err);
|
|
88
102
|
setConfig(null);
|
|
89
103
|
setConfigError(err instanceof Error ? err : new Error(String(err)));
|
|
90
104
|
}
|
|
91
|
-
}
|
|
105
|
+
} finally {
|
|
106
|
+
if (abortRef.current === controller) {
|
|
107
|
+
abortRef.current = null;
|
|
108
|
+
refreshInFlightRef.current = false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
[baseUrl, sessionId]
|
|
113
|
+
);
|
|
92
114
|
|
|
115
|
+
// Fetch connection config whenever the active session changes.
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!sessionId) {
|
|
118
|
+
console.debug('[NATS] no sessionId, skipping config fetch');
|
|
119
|
+
setConfig(null);
|
|
120
|
+
setConfigError(null);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
void refreshConfig('initial');
|
|
93
125
|
return () => {
|
|
94
|
-
|
|
95
|
-
|
|
126
|
+
abortRef.current?.abort();
|
|
127
|
+
abortRef.current = null;
|
|
128
|
+
refreshInFlightRef.current = false;
|
|
96
129
|
};
|
|
97
|
-
}, [
|
|
130
|
+
}, [sessionId, refreshConfig]);
|
|
131
|
+
|
|
132
|
+
// A suspended browser may lose core NATS messages. Refresh the short-lived
|
|
133
|
+
// JWT, reconnect, then reconcile the pending turn from the engine.
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
if (!sessionId) return;
|
|
136
|
+
|
|
137
|
+
const requestResume = () => {
|
|
138
|
+
if (document.visibilityState === 'visible') {
|
|
139
|
+
void refreshConfig('resume');
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const onVisibilityChange = () => {
|
|
143
|
+
if (document.visibilityState === 'visible') requestResume();
|
|
144
|
+
};
|
|
145
|
+
const onPageShow = (event: PageTransitionEvent) => {
|
|
146
|
+
if (event.persisted) requestResume();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
150
|
+
window.addEventListener('online', requestResume);
|
|
151
|
+
window.addEventListener('pageshow', onPageShow);
|
|
152
|
+
return () => {
|
|
153
|
+
document.removeEventListener('visibilitychange', onVisibilityChange);
|
|
154
|
+
window.removeEventListener('online', requestResume);
|
|
155
|
+
window.removeEventListener('pageshow', onPageShow);
|
|
156
|
+
};
|
|
157
|
+
}, [sessionId, refreshConfig]);
|
|
98
158
|
|
|
99
159
|
const handleMessage = useCallback((event: NatsSessionEvent) => {
|
|
100
160
|
console.debug('[NATS] dispatching event', { eventType: event.eventType });
|
|
@@ -113,7 +173,27 @@ export function useNats({
|
|
|
113
173
|
}
|
|
114
174
|
}, []);
|
|
115
175
|
|
|
116
|
-
|
|
176
|
+
const handleConnected = useCallback(() => {
|
|
177
|
+
if (!catchUpAfterConnectRef.current) return;
|
|
178
|
+
catchUpAfterConnectRef.current = false;
|
|
179
|
+
void onCatchUpRef.current?.();
|
|
180
|
+
}, []);
|
|
181
|
+
|
|
182
|
+
const handleAuthError = useCallback(
|
|
183
|
+
(error: Error) => {
|
|
184
|
+
console.warn(
|
|
185
|
+
'[NATS] JWT rejected or expired; refreshing credentials',
|
|
186
|
+
error
|
|
187
|
+
);
|
|
188
|
+
void refreshConfig('auth');
|
|
189
|
+
},
|
|
190
|
+
[refreshConfig]
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
useNatsSession(sessionId, config ?? undefined, handleMessage, {
|
|
194
|
+
onConnected: handleConnected,
|
|
195
|
+
onAuthError: handleAuthError,
|
|
196
|
+
});
|
|
117
197
|
|
|
118
198
|
return {
|
|
119
199
|
/** True once connection config has been retrieved. */
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AuthorizationError } from '@nats-io/nats-core';
|
|
2
|
+
import { isNatsAuthError } from './useNatsSession';
|
|
3
|
+
|
|
4
|
+
describe('isNatsAuthError', () => {
|
|
5
|
+
it('recognizes typed NATS authorization failures', () => {
|
|
6
|
+
expect(isNatsAuthError(new AuthorizationError('test'))).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('recognizes the server authorization violation message', () => {
|
|
10
|
+
expect(isNatsAuthError(new Error('Authorization Violation'))).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('does not classify unrelated connection failures as auth failures', () => {
|
|
14
|
+
expect(isNatsAuthError(new Error('connection refused'))).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
});
|