@nafzo/react-native-sdk 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +651 -2
- package/package.json +25 -4
- package/src/NafzoAssistant.js +46 -0
- package/src/NafzoProvider.js +92 -0
- package/src/components/ChatHeader.js +97 -0
- package/src/components/ChatInput.js +133 -0
- package/src/components/FloatingChat.js +157 -0
- package/src/components/FullscreenChat.js +83 -0
- package/src/components/MessageBubble.js +71 -0
- package/src/components/MessageList.js +119 -0
- package/src/components/TypingIndicator.js +107 -0
- package/src/hooks/useNafzoChat.js +223 -0
- package/src/index.js +23 -0
- package/src/services/api.js +112 -0
- package/src/services/history.js +19 -0
- package/src/services/session.js +95 -0
- package/src/utils/helpers.js +123 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
|
|
2
|
+
import React, { useEffect, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
FlatList,
|
|
6
|
+
View,
|
|
7
|
+
Text,
|
|
8
|
+
StyleSheet,
|
|
9
|
+
} from "react-native";
|
|
10
|
+
|
|
11
|
+
import MessageBubble from "./MessageBubble.js";
|
|
12
|
+
import TypingIndicator from "./TypingIndicator.js";
|
|
13
|
+
|
|
14
|
+
export default function MessageList({
|
|
15
|
+
messages = [],
|
|
16
|
+
sending = false,
|
|
17
|
+
loading = false,
|
|
18
|
+
colors = {},
|
|
19
|
+
emptyMessage = "Start a conversation with us.",
|
|
20
|
+
}) {
|
|
21
|
+
const listRef = useRef(null);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (messages.length === 0 && !sending) return;
|
|
25
|
+
|
|
26
|
+
const timer = setTimeout(() => {
|
|
27
|
+
listRef.current?.scrollToEnd({
|
|
28
|
+
animated: true,
|
|
29
|
+
});
|
|
30
|
+
}, 100);
|
|
31
|
+
|
|
32
|
+
return () => clearTimeout(timer);
|
|
33
|
+
}, [messages.length, sending]);
|
|
34
|
+
|
|
35
|
+
const renderItem = ({ item }) => (
|
|
36
|
+
<MessageBubble
|
|
37
|
+
message={item}
|
|
38
|
+
userBubbleColor={colors.userBubble || "#2563EB"}
|
|
39
|
+
assistantBubbleColor={
|
|
40
|
+
colors.assistantBubble || "#F1F5F9"
|
|
41
|
+
}
|
|
42
|
+
userTextColor={colors.userText || "#FFFFFF"}
|
|
43
|
+
assistantTextColor={
|
|
44
|
+
colors.assistantText || "#1E293B"
|
|
45
|
+
}
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (loading && messages.length === 0) {
|
|
50
|
+
return (
|
|
51
|
+
<View style={styles.center}>
|
|
52
|
+
<Text style={styles.infoText}>
|
|
53
|
+
Loading conversation...
|
|
54
|
+
</Text>
|
|
55
|
+
</View>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<FlatList
|
|
61
|
+
ref={listRef}
|
|
62
|
+
data={messages}
|
|
63
|
+
keyExtractor={(item) => String(item.id)}
|
|
64
|
+
renderItem={renderItem}
|
|
65
|
+
contentContainerStyle={[
|
|
66
|
+
styles.content,
|
|
67
|
+
messages.length === 0 && styles.emptyContent,
|
|
68
|
+
]}
|
|
69
|
+
ListEmptyComponent={
|
|
70
|
+
<Text style={styles.emptyText}>
|
|
71
|
+
{emptyMessage}
|
|
72
|
+
</Text>
|
|
73
|
+
}
|
|
74
|
+
ListFooterComponent={
|
|
75
|
+
sending ? (
|
|
76
|
+
<TypingIndicator
|
|
77
|
+
color={colors.typingColor || "#64748B"}
|
|
78
|
+
backgroundColor={
|
|
79
|
+
colors.assistantBubble || "#F1F5F9"
|
|
80
|
+
}
|
|
81
|
+
/>
|
|
82
|
+
) : null
|
|
83
|
+
}
|
|
84
|
+
keyboardShouldPersistTaps="handled"
|
|
85
|
+
showsVerticalScrollIndicator={false}
|
|
86
|
+
onContentSizeChange={() =>
|
|
87
|
+
listRef.current?.scrollToEnd({
|
|
88
|
+
animated: true,
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const styles = StyleSheet.create({
|
|
96
|
+
content: {
|
|
97
|
+
paddingVertical: 12,
|
|
98
|
+
flexGrow: 1,
|
|
99
|
+
},
|
|
100
|
+
emptyContent: {
|
|
101
|
+
justifyContent: "center",
|
|
102
|
+
alignItems: "center",
|
|
103
|
+
},
|
|
104
|
+
emptyText: {
|
|
105
|
+
color: "#94A3B8",
|
|
106
|
+
fontSize: 14,
|
|
107
|
+
textAlign: "center",
|
|
108
|
+
paddingHorizontal: 25,
|
|
109
|
+
},
|
|
110
|
+
center: {
|
|
111
|
+
flex: 1,
|
|
112
|
+
justifyContent: "center",
|
|
113
|
+
alignItems: "center",
|
|
114
|
+
},
|
|
115
|
+
infoText: {
|
|
116
|
+
color: "#64748B",
|
|
117
|
+
fontSize: 14,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
|
|
2
|
+
import React, { useEffect, useRef } from "react";
|
|
3
|
+
import {
|
|
4
|
+
View,
|
|
5
|
+
Text,
|
|
6
|
+
Animated,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
} from "react-native";
|
|
9
|
+
|
|
10
|
+
export default function TypingIndicator({
|
|
11
|
+
color = "#64748B",
|
|
12
|
+
backgroundColor = "#F1F5F9",
|
|
13
|
+
}) {
|
|
14
|
+
const animations = useRef([
|
|
15
|
+
new Animated.Value(0.3),
|
|
16
|
+
new Animated.Value(0.3),
|
|
17
|
+
new Animated.Value(0.3),
|
|
18
|
+
]).current;
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const loops = animations.map((animation, index) =>
|
|
22
|
+
Animated.loop(
|
|
23
|
+
Animated.sequence([
|
|
24
|
+
Animated.delay(index * 150),
|
|
25
|
+
Animated.timing(animation, {
|
|
26
|
+
toValue: 1,
|
|
27
|
+
duration: 350,
|
|
28
|
+
useNativeDriver: true,
|
|
29
|
+
}),
|
|
30
|
+
Animated.timing(animation, {
|
|
31
|
+
toValue: 0.3,
|
|
32
|
+
duration: 350,
|
|
33
|
+
useNativeDriver: true,
|
|
34
|
+
}),
|
|
35
|
+
])
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
loops.forEach((loop) => loop.start());
|
|
40
|
+
|
|
41
|
+
return () => {
|
|
42
|
+
loops.forEach((loop) => loop.stop());
|
|
43
|
+
};
|
|
44
|
+
}, [animations]);
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<View style={styles.row}>
|
|
48
|
+
<View style={[styles.bubble, { backgroundColor }]}>
|
|
49
|
+
<View style={styles.dots}>
|
|
50
|
+
{animations.map((animation, index) => (
|
|
51
|
+
<Animated.View
|
|
52
|
+
key={index}
|
|
53
|
+
style={[
|
|
54
|
+
styles.dot,
|
|
55
|
+
{
|
|
56
|
+
backgroundColor: color,
|
|
57
|
+
opacity: animation,
|
|
58
|
+
transform: [
|
|
59
|
+
{
|
|
60
|
+
translateY: animation.interpolate({
|
|
61
|
+
inputRange: [0.3, 1],
|
|
62
|
+
outputRange: [0, -4],
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
]}
|
|
68
|
+
/>
|
|
69
|
+
))}
|
|
70
|
+
</View>
|
|
71
|
+
<Text style={[styles.label, { color }]}>
|
|
72
|
+
Typing...
|
|
73
|
+
</Text>
|
|
74
|
+
</View>
|
|
75
|
+
</View>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const styles = StyleSheet.create({
|
|
80
|
+
row: {
|
|
81
|
+
alignItems: "flex-start",
|
|
82
|
+
paddingHorizontal: 12,
|
|
83
|
+
marginVertical: 5,
|
|
84
|
+
},
|
|
85
|
+
bubble: {
|
|
86
|
+
flexDirection: "row",
|
|
87
|
+
alignItems: "center",
|
|
88
|
+
paddingHorizontal: 14,
|
|
89
|
+
paddingVertical: 11,
|
|
90
|
+
borderRadius: 16,
|
|
91
|
+
gap: 5,
|
|
92
|
+
},
|
|
93
|
+
dots: {
|
|
94
|
+
flexDirection: "row",
|
|
95
|
+
alignItems: "center",
|
|
96
|
+
gap: 4,
|
|
97
|
+
},
|
|
98
|
+
dot: {
|
|
99
|
+
width: 6,
|
|
100
|
+
height: 6,
|
|
101
|
+
borderRadius: 3,
|
|
102
|
+
},
|
|
103
|
+
label: {
|
|
104
|
+
fontSize: 12,
|
|
105
|
+
marginLeft: 5,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
} from "react";
|
|
7
|
+
|
|
8
|
+
import { useNafzoContext } from "../NafzoProvider.js";
|
|
9
|
+
import { loadChatHistory } from "../services/history.js";
|
|
10
|
+
import {
|
|
11
|
+
normalizeMessage,
|
|
12
|
+
generateUUID,
|
|
13
|
+
} from "../utils/helpers.js";
|
|
14
|
+
|
|
15
|
+
function extractMessages(history) {
|
|
16
|
+
let messages = [];
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(history)) {
|
|
19
|
+
messages = history;
|
|
20
|
+
} else if (Array.isArray(history?.messages)) {
|
|
21
|
+
messages = history.messages;
|
|
22
|
+
} else if (Array.isArray(history?.data?.messages)) {
|
|
23
|
+
messages = history.data.messages;
|
|
24
|
+
} else if (Array.isArray(history?.history)) {
|
|
25
|
+
messages = history.history;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return messages
|
|
29
|
+
.filter(
|
|
30
|
+
(message) =>
|
|
31
|
+
["user", "assistant"].includes(message?.role) &&
|
|
32
|
+
typeof message?.content === "string"
|
|
33
|
+
)
|
|
34
|
+
.map((message) => ({
|
|
35
|
+
id: message.id || message.message_id || generateUUID(),
|
|
36
|
+
role: message.role,
|
|
37
|
+
content: message.content,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function useNafzoChat() {
|
|
42
|
+
const {
|
|
43
|
+
api,
|
|
44
|
+
session,
|
|
45
|
+
ready,
|
|
46
|
+
error: initializationError,
|
|
47
|
+
} = useNafzoContext();
|
|
48
|
+
|
|
49
|
+
const [messages, setMessages] = useState([]);
|
|
50
|
+
const [loading, setLoading] = useState(false);
|
|
51
|
+
const [sending, setSending] = useState(false);
|
|
52
|
+
const [error, setError] = useState(null);
|
|
53
|
+
|
|
54
|
+
const sendingRef = useRef(false);
|
|
55
|
+
const historyRequestRef = useRef(0);
|
|
56
|
+
|
|
57
|
+
// Safely get the session ID.
|
|
58
|
+
// Prevents errors during provider re-initialization
|
|
59
|
+
// or config changes.
|
|
60
|
+
const visitorSessionId =
|
|
61
|
+
ready && session?.initialized
|
|
62
|
+
? session.getSessionId()
|
|
63
|
+
: null;
|
|
64
|
+
|
|
65
|
+
const sendMessage = useCallback(
|
|
66
|
+
async (value) => {
|
|
67
|
+
const content = normalizeMessage(value);
|
|
68
|
+
|
|
69
|
+
if (!content) {
|
|
70
|
+
throw new Error("Message cannot be empty");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!ready || !visitorSessionId) {
|
|
74
|
+
throw new Error("Nafzo is not initialized yet");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (sendingRef.current) {
|
|
78
|
+
throw new Error("A message is already being sent");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
sendingRef.current = true;
|
|
82
|
+
setSending(true);
|
|
83
|
+
setError(null);
|
|
84
|
+
|
|
85
|
+
const userMessage = {
|
|
86
|
+
id: generateUUID(),
|
|
87
|
+
role: "user",
|
|
88
|
+
content,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
setMessages((previous) => [
|
|
92
|
+
...previous,
|
|
93
|
+
userMessage,
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const result = await api.sendMessage({
|
|
98
|
+
message: content,
|
|
99
|
+
visitorSessionId,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const assistantMessage = {
|
|
103
|
+
id: generateUUID(),
|
|
104
|
+
role: "assistant",
|
|
105
|
+
content: result.reply || "",
|
|
106
|
+
actions: result.actions || [],
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
setMessages((previous) => [
|
|
110
|
+
...previous,
|
|
111
|
+
assistantMessage,
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
return result;
|
|
115
|
+
} catch (err) {
|
|
116
|
+
setMessages((previous) =>
|
|
117
|
+
previous.filter(
|
|
118
|
+
(message) => message.id !== userMessage.id
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
setError(err);
|
|
123
|
+
throw err;
|
|
124
|
+
} finally {
|
|
125
|
+
sendingRef.current = false;
|
|
126
|
+
setSending(false);
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
[api, ready, visitorSessionId]
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
const loadHistory = useCallback(async () => {
|
|
133
|
+
if (!ready || !visitorSessionId) {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const requestId = ++historyRequestRef.current;
|
|
138
|
+
|
|
139
|
+
setLoading(true);
|
|
140
|
+
setError(null);
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const result = await loadChatHistory(
|
|
144
|
+
api,
|
|
145
|
+
visitorSessionId
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const historyMessages = extractMessages(result);
|
|
149
|
+
|
|
150
|
+
// Ignore responses belonging to an older session.
|
|
151
|
+
if (requestId !== historyRequestRef.current) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setMessages(historyMessages);
|
|
156
|
+
|
|
157
|
+
return historyMessages;
|
|
158
|
+
} catch (err) {
|
|
159
|
+
if (requestId === historyRequestRef.current) {
|
|
160
|
+
setError(err);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
throw err;
|
|
164
|
+
} finally {
|
|
165
|
+
if (requestId === historyRequestRef.current) {
|
|
166
|
+
setLoading(false);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}, [api, ready, visitorSessionId]);
|
|
170
|
+
|
|
171
|
+
const resetChat = useCallback(async () => {
|
|
172
|
+
if (!ready || !visitorSessionId) {
|
|
173
|
+
throw new Error("Nafzo is not initialized yet");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (sendingRef.current) {
|
|
177
|
+
throw new Error("Cannot reset while a message is sending");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Cancel any pending history response.
|
|
181
|
+
historyRequestRef.current += 1;
|
|
182
|
+
|
|
183
|
+
const newSessionId = await session.reset();
|
|
184
|
+
|
|
185
|
+
setMessages([]);
|
|
186
|
+
setError(null);
|
|
187
|
+
setLoading(false);
|
|
188
|
+
|
|
189
|
+
return newSessionId;
|
|
190
|
+
}, [session, ready, visitorSessionId]);
|
|
191
|
+
|
|
192
|
+
// Handle initialization errors.
|
|
193
|
+
useEffect(() => {
|
|
194
|
+
if (initializationError) {
|
|
195
|
+
setError(initializationError);
|
|
196
|
+
}
|
|
197
|
+
}, [initializationError]);
|
|
198
|
+
|
|
199
|
+
// Load history only when a valid session is ready.
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (!ready || !visitorSessionId) {
|
|
202
|
+
setMessages([]);
|
|
203
|
+
setLoading(false);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
loadHistory().catch(() => {
|
|
208
|
+
// Error is already stored in hook state.
|
|
209
|
+
});
|
|
210
|
+
}, [ready, visitorSessionId, loadHistory]);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
messages,
|
|
214
|
+
loading,
|
|
215
|
+
sending,
|
|
216
|
+
ready,
|
|
217
|
+
error,
|
|
218
|
+
visitorSessionId,
|
|
219
|
+
sendMessage,
|
|
220
|
+
loadHistory,
|
|
221
|
+
resetChat,
|
|
222
|
+
};
|
|
223
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nafzo React Native SDK
|
|
3
|
+
* Public package entry point.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const SDK_VERSION = "1.0.0";
|
|
7
|
+
|
|
8
|
+
export { NafzoProvider, useNafzoContext } from "./NafzoProvider.js";
|
|
9
|
+
export { NafzoAssistant } from "./NafzoAssistant.js";
|
|
10
|
+
|
|
11
|
+
export { NafzoSession } from "./services/session.js";
|
|
12
|
+
export { NafzoApi } from "./services/api.js";
|
|
13
|
+
export { loadChatHistory } from "./services/history.js";
|
|
14
|
+
export { useNafzoChat } from "./hooks/useNafzoChat.js";
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
generateUUID,
|
|
18
|
+
normalizeBaseUrl,
|
|
19
|
+
validateConfig,
|
|
20
|
+
createNafzoError,
|
|
21
|
+
normalizeMessage,
|
|
22
|
+
getPlatformIdentifier,
|
|
23
|
+
} from "./utils/helpers.js";
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
normalizeBaseUrl,
|
|
4
|
+
createNafzoError,
|
|
5
|
+
generateUUID,
|
|
6
|
+
} from "../utils/helpers.js";
|
|
7
|
+
|
|
8
|
+
export class NafzoApi {
|
|
9
|
+
constructor({
|
|
10
|
+
baseUrl = "https://nafzo.wabothub.com",
|
|
11
|
+
assistantId,
|
|
12
|
+
platform,
|
|
13
|
+
appIdentifier,
|
|
14
|
+
}) {
|
|
15
|
+
this.baseUrl = normalizeBaseUrl(baseUrl);
|
|
16
|
+
this.assistantId = assistantId;
|
|
17
|
+
this.platform = platform;
|
|
18
|
+
this.appIdentifier = appIdentifier;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get headers() {
|
|
22
|
+
return {
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"x-nafzo-platform": this.platform,
|
|
25
|
+
"x-nafzo-app-id": this.appIdentifier,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async request(endpoint, options = {}) {
|
|
30
|
+
let response;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
34
|
+
...options,
|
|
35
|
+
headers: {
|
|
36
|
+
...this.headers,
|
|
37
|
+
...options.headers,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw createNafzoError(
|
|
42
|
+
error.message || "Unable to connect to Nafzo server"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let data;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
data = await response.json();
|
|
50
|
+
} catch {
|
|
51
|
+
data = {};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
throw createNafzoError(
|
|
56
|
+
data.error || "Nafzo API request failed",
|
|
57
|
+
response.status,
|
|
58
|
+
data
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async sendMessage({
|
|
66
|
+
message,
|
|
67
|
+
visitorSessionId,
|
|
68
|
+
clientMessageId = generateUUID(),
|
|
69
|
+
}) {
|
|
70
|
+
if (!message || !message.trim()) {
|
|
71
|
+
throw new Error("Message cannot be empty");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!visitorSessionId) {
|
|
75
|
+
throw new Error("visitorSessionId is required");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return this.request("/public/v1/mobile/chat", {
|
|
79
|
+
method: "POST",
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
assistant_id: this.assistantId,
|
|
82
|
+
visitor_session_id: visitorSessionId,
|
|
83
|
+
client_message_id: clientMessageId,
|
|
84
|
+
messages: [
|
|
85
|
+
{
|
|
86
|
+
role: "user",
|
|
87
|
+
content: message.trim(),
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async getHistory(visitorSessionId) {
|
|
95
|
+
if (!visitorSessionId) {
|
|
96
|
+
throw new Error("visitorSessionId is required");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const query = [
|
|
100
|
+
`assistant_id=${encodeURIComponent(this.assistantId)}`,
|
|
101
|
+
`visitor_session_id=${encodeURIComponent(visitorSessionId)}`,
|
|
102
|
+
].join("&");
|
|
103
|
+
|
|
104
|
+
return this.request(
|
|
105
|
+
`/public/v1/mobile/chat/history?${query}`,
|
|
106
|
+
{
|
|
107
|
+
method: "GET",
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Load conversation history through the Nafzo API.
|
|
4
|
+
*
|
|
5
|
+
* Returns the original backend response so the SDK
|
|
6
|
+
* does not assume a history response format prematurely.
|
|
7
|
+
*/
|
|
8
|
+
export async function loadChatHistory(api, visitorSessionId) {
|
|
9
|
+
if (!api) {
|
|
10
|
+
throw new Error("Nafzo history: API client is required");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!visitorSessionId) {
|
|
14
|
+
throw new Error("Nafzo history: Session ID is required");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return api.getHistory(visitorSessionId);
|
|
18
|
+
}
|
|
19
|
+
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
2
|
+
import { generateUUID } from "../utils/helpers.js";
|
|
3
|
+
|
|
4
|
+
export class NafzoSession {
|
|
5
|
+
constructor({ assistantId, userId = null, storage = AsyncStorage }) {
|
|
6
|
+
if (!assistantId) {
|
|
7
|
+
throw new Error("NafzoSession: assistantId is required");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
this.assistantId = assistantId;
|
|
11
|
+
this.storage = storage;
|
|
12
|
+
this.userId = userId ? String(userId) : "guest";
|
|
13
|
+
this.storageKey = `nafzo_session_${assistantId}_${this.userId}`;
|
|
14
|
+
|
|
15
|
+
this.visitorSessionId = null;
|
|
16
|
+
this.initialized = false;
|
|
17
|
+
this.initializing = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async initialize() {
|
|
21
|
+
if (this.initialized) {
|
|
22
|
+
return this.visitorSessionId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (this.initializing) {
|
|
26
|
+
return this.initializing;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.initializing = this.restoreSession();
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
return await this.initializing;
|
|
33
|
+
} finally {
|
|
34
|
+
this.initializing = null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async restoreSession() {
|
|
39
|
+
let savedSessionId = null;
|
|
40
|
+
|
|
41
|
+
if (this.storage) {
|
|
42
|
+
savedSessionId = await this.storage.getItem(this.storageKey);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.visitorSessionId = savedSessionId || generateUUID();
|
|
46
|
+
|
|
47
|
+
if (!savedSessionId) {
|
|
48
|
+
await this.persist();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.initialized = true;
|
|
52
|
+
|
|
53
|
+
return this.visitorSessionId;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async persist() {
|
|
57
|
+
if (!this.visitorSessionId) {
|
|
58
|
+
throw new Error("NafzoSession: No active session to save");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (this.storage) {
|
|
62
|
+
await this.storage.setItem(
|
|
63
|
+
this.storageKey,
|
|
64
|
+
this.visitorSessionId
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this.visitorSessionId;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getSessionId() {
|
|
72
|
+
if (!this.initialized || !this.visitorSessionId) {
|
|
73
|
+
throw new Error("NafzoSession: Call initialize() first");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return this.visitorSessionId;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async reset() {
|
|
80
|
+
this.visitorSessionId = generateUUID();
|
|
81
|
+
|
|
82
|
+
await this.persist();
|
|
83
|
+
|
|
84
|
+
return this.visitorSessionId;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async clear() {
|
|
88
|
+
this.visitorSessionId = null;
|
|
89
|
+
this.initialized = false;
|
|
90
|
+
|
|
91
|
+
if (this.storage?.removeItem) {
|
|
92
|
+
await this.storage.removeItem(this.storageKey);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|