-
Notifications
You must be signed in to change notification settings - Fork 50
/
AppContainer.vue
581 lines (536 loc) · 20.2 KB
/
AppContainer.vue
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<template>
<nav-bar :shopping-cart="shoppingCart" @open-cart="openCart"></nav-bar>
<shopping-cart
v-if="isCartOpen"
ref="shoppingCart"
:shopping-cart="shoppingCart"
@close-cart="closeCart"
@clear-shopping-cart="clearCart"
@remove-from-cart="removeFromCart"
@registerAction="registerAction"
>
</shopping-cart>
<div >
<div class="row justify-content-center">
<div>
<div class="app-container">
<restaurants-container ref="restaurantsContainer"
@add-to-cart="addToCart"
@register-action="registerAction"
>
</restaurants-container>
<chat-container
ref="chatContainer"
:isChatOpen="isChatOpen"
:config="config"
:messages="messages"
:isRecordingFlag="isRecordingFlag"
:recordedVoiceURL="recordedVoiceURL"
:timeRecorded="timeRecorded"
:botTyping="botTyping"
:botTypingMsg="botTypingMsg"
:userTranscribing="userTranscribing"
:handsFreeFlag="handsFreeFlag"
@send-message="sendMessage"
@toggle-recording="toggleRecording"
@cancel-audio="cancelAudio"
@toggle-chatbot="toggleChatBot"
@toggle-handsfree="toggleHandsFree"
>
</chat-container>
</div>
</div>
</div>
</div>
</template>
<script>
import NavBar from "./Navbar/NavBar.vue"
import ShoppingCart from "./ShoppingCart/ShoppingCart.vue"
import ChatContainer from "./Chatbot/ChatContainer.vue";
import RestaurantsContainer from "./Restaurants/RestaurantsContainer.vue"
import './styles/common.css'
import config from "./general_config.json";
import axios from "axios";
export default {
name: "AppContainer",
components: {
NavBar,
ShoppingCart,
ChatContainer,
RestaurantsContainer
},
data() {
return {
// Init
config,
isCartOpen: false,
shoppingCart: [],
baseApiUrl: import.meta.env.VITE_APP_API_URL + "/api/",
baseChatApiUrl: import.meta.env.VITE_APP_API_URL + "/api/chat",
handsFreeFlag: false,
// Chatbot
isChatOpen: false,
messages: [
{
role: "assistant",
content: config.fstMsg,
logs: {
time: null
}
}
],
// User performed actions logger
actions: [],
// Audio
isRecordingFlag: false,
recordedVoiceURL: null,
lastestAudioBlob: null,
timeRecorded: 0,
// Typing
botTyping: false,
botTypingMsg: null,
userTranscribing: false,
}
},
methods: {
// -- Shopping Cart --
openCart() {
let flag_register_new_action = true;
if (this.isCartOpen) {
flag_register_new_action = false;
}
this.isCartOpen = true;
// registering action
if (flag_register_new_action) {
this.registerAction("the shopping cart was opened")
}
},
closeCart() {
let flag_register_new_action = true;
if (!this.isCartOpen) {
flag_register_new_action = false;
}
this.isCartOpen = false;
// registering action
if (flag_register_new_action) {
this.registerAction("the shopping cart was closed")
}
},
clearCart() {
this.shoppingCart.forEach(foodItem => {
foodItem.fadeAway = true;
setTimeout(() => {
this.shoppingCart = this.shoppingCart.filter(item => item !== foodItem);
}, 200); // Remove the item after 0.2 seconds (200 milliseconds)
foodItem.fadeAway = false;
});
this.registerAction("removed all items from the shopping cart, it is now empty")
},
addToCart(foodItem){
// Should check if the foodItem name is already in the cart
// If so, increment the quantity
if (this.shoppingCart.length === 0){
this.shoppingCart.push(foodItem)
// registering action
this.registerAction(foodItem.name + " was added to the shopping cart")
}
else {
const index = this.shoppingCart.findIndex(
(item) => item.name === foodItem.name
);
if (index === -1){
this.shoppingCart.push(foodItem)
// registering action
this.registerAction(foodItem.name + " was added to the shopping cart")
}
else {
this.shoppingCart[index].quantity += 1
// registering action
this.registerAction(this.shoppingCart[index].name + " was added again to the shopping cart, totalizing " + this.shoppingCart[index].quantity + " items")
}
}
// console.log(this.shoppingCart)
},
removeFromCart(foodItem) {
const index = this.shoppingCart.findIndex(
(item) => item.id === foodItem.id
);
this.shoppingCart[index].fadeAway = true;
// registering action
this.registerAction(this.shoppingCart[index].name + " was removed completely from the shopping cart")
setTimeout(() => {
this.shoppingCart[index].fadeAway = false;
this.shoppingCart.splice(index, 1);
}, 200); // Remove the item after the animation duration
},
// -- Messages management --
// Chatbot
toggleChatBot() {
this.isChatOpen = !this.isChatOpen;
},
// Handsfree
toggleHandsFree() {
this.handsFreeFlag = !this.handsFreeFlag;
console.log("Handsfree toggled: " + this.handsFreeFlag)
},
// Messages handling
sendMessage: async function(textMessage) {
/*Sends message to backend*/
// If theres a new audio
if (!(this.recordedVoiceURL == null)) {
try {
textMessage = await this.sendAudio(this.lastestAudioBlob);
console.log(textMessage)
} catch (error) {
console.log(error);
return;
}
}
// Checking for empty string
if (textMessage){
if (
!textMessage.replace(/\s/g, '').length &&
!this.recordedVoiceURL
) {
return
}
}
// Adding message to list
this.messages.push(
{
role: "user",
content: textMessage,
logs: {
time: this.getCurrentTime()
}
}
)
// This creates the chathistory removing the logs from the message
// TODO this could be done in a better way (maybe with a computed property)
let chatHistory = this.getChatHistory()
// Scrolling down
this.scrollDown()
// Parallelizing calls to backend
Promise.all(
this.generateAnswer(chatHistory),
).then(() => {
console.log("Successfully sent message")
}).catch(error => {
console.log(error)
})
},
generateAnswer: async function(chatHistory, function_call = true, ){
// Message backend interaction
this.botTyping = true
axios.post(
this.baseChatApiUrl + "/send_message", {
query: {
history: chatHistory
},
function_call: function_call
}
)
.then(response => {
console.log(response)
let responseObject = response.data
let answer = responseObject.response
let functionCallSignal = responseObject.function_call
// If we don't have to function call it will return the answer
if (functionCallSignal == false || functionCallSignal == null) {
this.botTypingMsg = null
this.messages.push(
{
role: "assistant",
content: answer,
logs: {
time: this.getCurrentTime(),
}
}
)
this.botTyping = false
this.scrollDown()
}
// if there's a function call, we call the function and get the answer, incorporate to the history and call the function again
else {
this.handleFunctionCall(functionCallSignal)
.then((functionCallOutput) => {
// Then we need to call generateAnswer but with function call deactivated and a new parameter:
this.messages.push(
functionCallOutput
)
this.generateAnswer(
chatHistory=this.getChatHistory(),
// function_call=false,
function_call=true,
)
})
.catch((error) => {
console.log("Error in function call:", error);
this.botTyping=false
});
}
}).catch(error => {
console.log(error)
this.messages.push(
{
role: "assistant",
content: "<span style='color:red;'>Error:</span> Please try again later.<br/> Description: " + error,
}
)
}).finally(() => {
})
return
},
// Function calling
handleFunctionCall: function(functionCallSignal) {
return new Promise(async (resolve, reject) => {
try {
let functionCallResponse = await axios.post(
`${this.baseChatApiUrl}/function_call`,
functionCallSignal
);
let functionCallResponseContent;
switch (functionCallSignal.name) {
case "get_restaurant_pages":
functionCallResponseContent = this.handleGetRestaurant(functionCallResponse);
break;
case "open_restaurant_page":
functionCallResponseContent = this.handleOpenRestaurant(functionCallResponse);
break;
case "close_restaurant_page":
functionCallResponseContent = this.handleCloseRestaurant(functionCallResponse);
break;
case "get_user_actions":
functionCallResponseContent = this.handleGetActions(functionCallResponse);
break;
case "get_menu_of_restaurant":
functionCallResponseContent = this.handleGetCurrentRestaurantMenu(functionCallResponse);
break;
case "open_shopping_cart": // to be implemented
functionCallResponseContent = this.handleOpenShoppingCart(functionCallResponse);
break;
case "close_shopping_cart": // to be implemented
functionCallResponseContent = this.handleCloseShoppingCart(functionCallResponse);
break;
case "add_food_to_cart":
functionCallResponseContent = this.handleAddFoodToCart(functionCallResponse);
break;
case "remove_food_from_cart":
functionCallResponseContent = this.handleRemoveFoodFromCart(functionCallResponse);
break;
case "place_order":
functionCallResponseContent = this.handlePlaceOrder(functionCallResponse);
break;
case "activate_handsfree":
functionCallResponseContent = this.handleActivateHandsFree(functionCallResponse);
break;
default:
functionCallResponseContent = functionCallResponse.data.response.response;
console.log("Name of function not found - returning response raw");
}
console.log(functionCallResponseContent);
resolve({
role: "function",
content: functionCallResponseContent,
name: functionCallSignal.name,
});
} catch (error) {
reject(error);
}
});
},
handleGetRestaurant: function(functionCallResponse){
this.botTypingMsg = "Searching for restaurants..."
let msg = "@agent-action: You found the restaurant page(s) for " + JSON.stringify(functionCallResponse.data.response) + "!";
return msg
},
handleOpenRestaurant: function(functionCallResponse){
this.botTypingMsg = "Opening restaurant page..."
// restaurantId in string format to int
let restaurantId = parseInt(functionCallResponse.data.response.response.restaurant_uuid)
this.$refs.restaurantsContainer.selectRestaurant(parseInt(restaurantId))
let msg = "@agent-action: You opened the restaurant page for " + restaurantId + "!";
console.log(msg)
return msg
},
handleCloseRestaurant: function(functionCallResponse){
this.botTypingMsg = "Closing restaurant page..."
this.$refs.restaurantsContainer.deselectRestaurant()
let msg = "@agent-action: You closed the restaurant page!";
console.log(msg)
return msg
},
handleAddFoodToCart: function(functionCallResponse){
this.botTypingMsg = "Adding food to cart..."
// Parsing ids
let restaurantId = parseInt(functionCallResponse.data.response.response.restaurant_uuid)
let foodId = parseInt(functionCallResponse.data.response.response.food_id)
// Making sure the selected restaurant is the one we want
let restaurantContainer = this.$refs.restaurantsContainer
let restaurants = restaurantContainer.restaurants
restaurantContainer.selectRestaurant(restaurantId)
// Finding the right food, editing it
let restaurantPage = restaurantContainer.$refs.restaurantFullView
let foods = restaurantPage.foods
let food = foods.find(food => food.uuid === foodId)
food.quantity = functionCallResponse.data.response.response.quantity
// Adding it to the shopping cart
restaurantPage.addToCart(food)
// Opening the shopping cart
this.openCart()
return "@agent-action: You added "+ food.quantity + " of "+ food.name + " to the shopping cart!"
},
handleRemoveFoodFromCart: function(functionCallResponse){
this.botTypingMsg = "Removing food from cart..."
// Parsing ids
let restaurantId = parseInt(functionCallResponse.data.response.response.restaurant_uuid)
let foodId = parseInt(functionCallResponse.data.response.response.food_id)
// selecting shopping cart
let shoppingCartContainer = this.$refs.shoppingCart
let shoppingCartItems = shoppingCartContainer.shoppingCart
let foodItem = shoppingCartItems.find(item => item.uuid === foodId)
this.openCart()
this.removeFromCart(foodItem)
return "@agent-action: You removed "+ foodItem.quantity + " of "+ foodItem.name + " from the shopping cart!"
// return
},
handlePlaceOrder: function(functionCallResponse){
this.botTypingMsg = "Placing order..."
this.$refs.shoppingCart.submitOrder()
let msg = "@agent-action: You placed the order!"
return msg
},
handleGetActions: function(functionCallResponse){
this.botTypingMsg = "Getting your latest actions..."
let msg = JSON.stringify(this.actions.splice(-10))
console.log("Compiling latest actions")
console.log(msg)
return msg
},
handleGetCurrentRestaurantMenu: function(functionCallResponse){
this.botTypingMsg = "Getting the menu of the restaurant..."
console.log(functionCallResponse)
let menu = functionCallResponse.data.response["formatted"]
console.log(menu)
return menu
},
handleOpenShoppingCart: function(functionCallResponse){
this.botTypingMsg = "Opening the shopping cart..."
this.openCart()
// If the shopping cart is not empty, we show the content
if (this.shoppingCart.length > 0){
let msg = "@agent-action: You opened the shopping cart! Here's the content you founded:" + JSON.stringify(this.shoppingCart)
return msg
} else {
let msg = "@agent-action: You opened the shopping cart! It's empty!"
return msg
}
return msg
},
handleCloseShoppingCart: function(functionCallResponse){
this.botTypingMsg = "Closing the shopping cart..."
this.closeCart()
let msg = "@agent-action: You closed the shopping cart!"
return msg
},
handleActivateHandsFree: function(functionCallResponse){
this.botTypingMsg = "Activating handsfree experience..."
let msg = "@agent-action: You activated the handsfree voice experience"
this.handsFreeFlag = true
return msg
},
// Utils
getChatHistory: function(){
// This removes the logs from the chat history before sending it to the backedn
let chatHistory = []
for (let i = 0; i < this.messages.length; i++) {
let message = this.messages[i]
let messageObject = {
role: message.role,
content: message.content
}
if (message.name) {
messageObject.name = message.name
}
chatHistory.push(messageObject)
}
return chatHistory
},
registerAction: function(msg) {
this.actions.push("@action:" + msg + " at " + this.getCurrentTime())
console.log(this.actions)
},
// Audio
sendAudio: function(blob) {
try {
this.userTranscribing = true
this.scrollDown()
const fileReader = new FileReader();
fileReader.readAsDataURL(blob);
return new Promise((resolve, reject) => {
fileReader.onloadend = async () => {
const base64String = fileReader.result.replace(/^data:(.*;base64,)?/, '');
try {
const response = await axios.post(this.baseChatApiUrl + "/transcribe", { audio: base64String });
this.userTranscribing = false;
this.recordedVoiceURL = null;
resolve(response.data.response);
} catch (error) {
reject(error);
this.userTranscribing = false
}
};
});
} catch (error) {
console.error('Error sending audio:', error);
this.userTranscribing = false
return Promise.reject(error);
}
},
// Audio Manipulation
toggleRecording() {
this.isRecordingFlag = !this.isRecordingFlag;
if (this.isRecordingFlag) {
this.chunks = []
this.recordedVoiceURL = null; // clear previous recording
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
this.recorder = new MediaRecorder(stream);
this.recorder.addEventListener("dataavailable", (event) => {
this.chunks.push(event.data);
});
this.recorder.addEventListener("stop", () => {
this.lastestAudioBlob = new Blob(this.chunks, { type: "audio/mp3; codecs=opus" });
this.recordedVoiceURL = URL.createObjectURL(this.lastestAudioBlob); // set recorded audio URL
});
this.recorder.start();
this.timerInterval = setInterval(() => {
this.timeRecorded += 10;
}, 10);
});
} else {
clearInterval(this.timerInterval);
this.timeRecorded = 0;
this.recorder.stop();
}
},
cancelAudio(){
this.isRecordingFlag = false;
this.recordedVoiceURL = null;
},
// Auxiliary functions
getCurrentTime() {
const today = new Date();
let hourTime = today.getHours()
let minuteTime = today.getMinutes()
let secondTime = today.getSeconds()
let currentTime = hourTime + ':' + minuteTime + ':' + secondTime
return currentTime
},
scrollDown() {
this.$nextTick(() => {
this.$refs.chatContainer.$refs.chatMessages.$refs.pageChat.scrollTop = this.$refs.chatContainer.$refs.chatMessages.$refs.pageChat.scrollHeight;
});
},
},
};
</script>