-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-websocket.html
251 lines (219 loc) · 6.72 KB
/
test-websocket.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<head>
<style>
.message {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<form id="form">
<input type="text" id="input" placeholder="Enter text here" />
<button type="submit">Submit</button>
</form>
<div>
<span id="chat-name">New Chat</span>
<div id="chat-log"></div>
</div>
<script type="module">
import { io } from 'https://cdn.skypack.dev/socket.io-client@latest?min'
// Do not place your accessKey in frontend code unless you intend for it to be publicly available.
const temporarySessionToken =
'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiIxRUU4RUIxNTdDQ0M2RkUwMDRDQ0NDNDEwMTg2OEZDOSIsImRlbW9BZ2VuY3lJZCI6MSwiaWF0IjoxNzAxMjk3MTc1LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjQwMDAiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjMwMDAiLCJleHAiOjE3MDEzMjU5NzV9.1i-FitEmnmQyKVEpIVqT0ZzZbBqhUStvSKOtBb5YESQ'
const agencyId = 1
const wsUrl = `ws://localhost:4000/ws?agencyId=1&demoSessionToken=${encodeURIComponent(temporarySessionToken)}`;
const socket = new WebSocket(wsUrl);
/*
Typings for the Socket.IO events:
export type NewChatInput = {
agencyId: number,
userPrompt: string,
filterOnlyManager?: ?boolean
}
export type NewChatOutput = {
agencyId: number,
chatId: string,
managerAgentId: number,
}
export type NewMessageInput = {
agencyId: number,
chatId: string,
userPrompt: string,
}
export type LoadChatInput = {
agencyId: number,
chatId: string,
lastMessageIds?: { [agentId: number]: number },
}
export type LoadChatOutput = {
agencyId: number,
chatId: string,
managerAgentId: number,
}
export type UpdateNameOutput = {
agencyId: number,
chatId: string,
managerAgentId: number,
name: string,
}
export type AppendMessageOutput = {
agencyId: number,
chatId: string,
managerAgentId: number,
message: FlatMessage,
}
export type UpdateMessageOutput = {
agencyId: number,
chatId: string,
managerAgentId: number,
message: FlatMessage,
}
export const MessageRole = {
SYSTEM: 'SYSTEM',
ASSISTANT: 'ASSISTANT',
USER: 'USER',
}
export type MessageRoleType = $Keys<typeof MessageRole>
export type FlatMessage = {|
messageId: number,
agentId: number,
agentConversationId: string,
role: MessageRoleType,
linkedMessageId: ?number,
internalInstruction?: ?boolean,
userInstruction?: ?boolean,
correctionInstruction?: ?boolean,
toApi?: ?boolean,
fromApi?: ?boolean,
completed?: ?boolean,
toAgentId?: ?number,
fromAgentId?: ?number,
text: string,
dateCreated: string,
__typename?: string,
id?: string,
|}
* */
let closed = false
socket.onclose = () => {
console.log('disconnected')
closed = true
}
socket.onmessage = (event) => {
console.log("Message from server ", event.data);
}
// socket.on('connect_error', (error) => {
// console.error('connect_error', error)
// })
//
// socket.on('disconnect', () => {
// console.log('disconnected')
// })
//
// socket.on('connect', () => {
// console.log('connected')
//
// socket.on('error', (error) => {
// console.error('error', error)
// })
//
// socket.on('loadChat', (output /*: LoadChatOutput*/) => {
// console.debug('loadChat', output)
// // When you send loadChat,
// // the server will emit many appendMessage and updateMessage to load the client,
// // then finally emit loadChat to indicate that loading is done.
// })
//
// socket.on('newChat', (output /*: NewChatOutput*/) => {
// console.debug('newChat', output)
// chatId = output.chatId
// })
//
// socket.on('updateName', (output /*: UpdateNameOutput*/) => {
// console.debug('updateName', output)
// document.getElementById('chat-name').innerText = output.name
// })
//
// socket.on('appendMessage', (output /*: AppendMessageOutput*/) => {
// console.debug('appendMessage', output)
// renderNewMessage(output)
// })
//
// socket.on('updateMessage', (output /*: UpdateMessageOutput*/) => {
// // console.debug('updateMessage', output)
// updateRenderedMessage(output)
// })
// })
function sendNewChat(input /*: NewChatInput*/) {
if (!closed) {
console.debug('newChat', input)
socket.send('hi')
} else {
window.alert('Unable to connect to server. Please try again later.')
}
}
function sendNewMessage(input /*: NewMessageInput*/) {
if (!closed) {
socket.emit('newMessage', input)
} else {
window.alert('Unable to connect to server. Please try again later.')
}
}
function sendLoadChat(input /*: LoadChatInput*/) {
if (!closed) {
console.debug('loadChat', input)
socket.emit('loadChat', input)
} else {
window.alert('Unable to connect to server. Please try again later.')
}
}
function renderNewMessage(output /*: AppendMessageOutput*/) {
// When a new message is received from the socket,
// render it to the DOM by appending it to the list of existing messages.
const messageElement = document.createElement('div')
messageElement.id = `message-${output.message.messageId}`
messageElement.className = 'message'
messageElement.innerHTML = `
<div>
<span>${output.message.text}</span>
</div>
`
document.getElementById('chat-log').appendChild(messageElement)
}
function updateRenderedMessage(output /*: UpdateMessageOutput*/) {
// When a message is updated,
// update the DOM by replacing the existing message with the updated message.
const messageElement = document.getElementById(
`message-${output.message.messageId}`,
)
messageElement.innerHTML = `
<div>
<span>${output.message.text}</span>
</div>
`
}
let chatId = null
document
.getElementById('form')
.addEventListener('submit', function (event) {
event.preventDefault()
const userPrompt = document.getElementById('input').value
document.getElementById('input').value = ''
if (!chatId) {
sendNewChat({
agencyId,
userPrompt,
filterOnlyManager: true,
})
} else {
sendNewMessage({
agencyId,
chatId,
userPrompt: userPrompt,
})
}
})
</script>
</body>