-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
93 lines (79 loc) · 2.27 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { useState, useEffect } from 'react'
import Chat, { Bubble, useMessages, LocaleProvider } from '@chatui/core'
import '@chatui/core/dist/index.css'
import './App.css'
export default function App() {
const { messages, appendMsg, setTyping } = useMessages([])
const [sessionId, setSessionId] = useState()
const fetchSession = async () => {
if (sessionId) return
const response = await fetch('https://engine.memori.ai/memori/v2/session', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
memoriId: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e'
})
})
const { sessionID, currentState, ...resp } = await response.json()
if (sessionID && currentState && resp.resultCode === 0) {
setSessionId(sessionID)
console.log('fetched session', sessionID, currentState)
if (currentState.emission) {
appendMsg({
type: 'text',
content: { text: currentState.emission }
})
}
}
}
useEffect(() => {
if (!sessionId) fetchSession()
}, [])
const handleSend = async (type, val) => {
if (!sessionId) {
console.error('No session ID')
return
}
if (type === 'text' && val.trim()) {
appendMsg({
type: 'text',
content: { text: val },
position: 'right'
})
setTyping(true)
const response = await fetch(`https://engine.memori.ai/memori/v2/TextEnteredEvent/${sessionId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: val.trim()
})
})
const { currentState } = await response.json()
if (currentState.emission)
appendMsg({
type: 'text',
content: { text: currentState.emission }
})
}
}
function renderMessageContent(msg) {
const { content } = msg
return <Bubble content={content.text} />
}
return (
<LocaleProvider locale="en-US">
<Chat
locale="en-US"
navbar={{ title: 'Parla con Nunzio' }}
messages={messages}
renderMessageContent={renderMessageContent}
onSend={handleSend}
placeholder="Chiedimi qualcosa..."
/>
</LocaleProvider>
)
}