Skip to content

Commit 852ee6f

Browse files
author
shaneski
committed
update code
1 parent 8913e0c commit 852ee6f

File tree

10 files changed

+172
-54
lines changed

10 files changed

+172
-54
lines changed

app/assistant/message/route.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
getAssistant,
23
getThread,
34
createThread,
45
addMessage,
@@ -23,10 +24,14 @@ export async function POST(request) {
2324
console.log("start assistant...", threadId, inquiry, (new Date()).toLocaleTimeString())
2425

2526
let thread_id = threadId ? threadId : ''
27+
let assistant_instructions = ''
2628
let messages_items = []
2729

2830
try {
2931

32+
const assistant = await getAssistant()
33+
assistant_instructions = assistant.instructions
34+
3035
if(thread_id) {
3136

3237
const exist_thread = await getThread({ threadId: thread_id })
@@ -53,7 +58,10 @@ export async function POST(request) {
5358

5459
console.log('message', message)
5560

56-
const run = await startRun({ threadId: thread_id })
61+
const run = await startRun({
62+
threadId: thread_id,
63+
instructions: assistant_instructions + `\nToday is ${new Date()}.`
64+
})
5765

5866
console.log('run', run)
5967

app/chat/function/route.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import get_event from '../../../lib/get_event.json'
77
import search_hotel from '../../../lib/search_hotel.json'
88
import get_hotel from '../../../lib/get_hotel.json'
99
import reserve_hotel from '../../../lib/reserve_hotel.json'
10+
import get_reservation from '../../../lib/get_reservation.json'
1011

1112
export async function POST(request) {
1213

@@ -52,6 +53,7 @@ export async function POST(request) {
5253
`- search_hotel, when the user wants to search for hotel based on given location.\n` +
5354
`- get_hotel, when the user wants to know more about a particular hotel.\n` +
5455
`- reserve_hotel, when the user wants to make room reservation for a particular hotel.\n` +
56+
`- get_reservation, when the user wants to get the details of their reservation.\n` +
5557
`When the user is making hotel reservation, be sure to guide the user to fill up all required information.\n` +
5658
`When you fill up some of the required information yourself, be sure to confirm to user before proceeding.\n` +
5759
`Aside from the listed functions above, answer all other inquiries by telling the user that it is out of scope of your ability.\n\n` +
@@ -85,6 +87,7 @@ export async function POST(request) {
8587
{ type: 'function', function: search_hotel },
8688
{ type: 'function', function: get_hotel },
8789
{ type: 'function', function: reserve_hotel },
90+
{ type: 'function', function: get_reservation },
8891
]
8992
})
9093

app/chat/message/route.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import get_event from '../../../lib/get_event.json'
66
import search_hotel from '../../../lib/search_hotel.json'
77
import get_hotel from '../../../lib/get_hotel.json'
88
import reserve_hotel from '../../../lib/reserve_hotel.json'
9+
import get_reservation from '../../../lib/get_reservation.json'
910

1011
export async function POST(request) {
1112

@@ -32,6 +33,7 @@ export async function POST(request) {
3233
`- search_hotel, when the user wants to search for hotel based on given location.\n` +
3334
`- get_hotel, when the user wants to know more about a particular hotel.\n` +
3435
`- reserve_hotel, when the user wants to make room reservation for a particular hotel.\n` +
36+
`- get_reservation, when the user wants to get the details of their reservation.\n` +
3537
`When the user is making hotel reservation, be sure to guide the user to fill up all required information.\n` +
3638
`When you fill up some of the required information yourself, be sure to confirm to user before proceeding.\n` +
3739
`Aside from the listed functions above, answer all other inquiries by telling the user that it is out of scope of your ability.\n\n` +
@@ -61,6 +63,7 @@ export async function POST(request) {
6163
{ type: 'function', function: search_hotel },
6264
{ type: 'function', function: get_hotel },
6365
{ type: 'function', function: reserve_hotel },
66+
{ type: 'function', function: get_reservation },
6467
]
6568
})
6669

lib/get_event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"date": {
1616
"type": "string",
17-
"description": "Date of the event"
17+
"description": "Date of the event in 'YYYY-MM-DD' format"
1818
}
1919
},
2020
"required": [

lib/get_events.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"date": {
1212
"type": "string",
13-
"description": "Date of the event"
13+
"description": "Date of the event in 'YYYY-MM-DD' format"
1414
}
1515
},
1616
"required": [

lib/get_weather.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"date": {
1212
"type": "string",
13-
"description": "Date of forecast"
13+
"description": "Date of forecast in 'YYYY-MM-DD' format"
1414
}
1515
},
1616
"required": [

lib/mockapi.js

Lines changed: 108 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,74 @@
11
import { getUniqueId } from "./utils"
22
import QuickCache from "./quickcache"
33

4+
const FLAG_ALLOW_UNKNOWN_ERROR = false
5+
46
function getWeather({ location, date }) {
57

68
if(!location) return { error: 'Invalid location', message: 'Please specify the location' }
79
if(!date) return { error: 'Invalid date', message: 'Please specify the date' }
810

9-
if(QuickCache.weather.some((qc) => qc.key === [location, date].join('_'))) {
11+
/*if(QuickCache.weather.some((qc) => qc.key === [location, date].join('_'))) {
1012
const stored_data = QuickCache.weather.find((qc) => qc.key === [location, date].join('_'))
1113
if(stored_data) {
1214
return JSON.parse(stored_data.value)
1315
}
16+
}*/
17+
18+
if(QuickCache.exist('weather', location, date)) {
19+
const stored_data = QuickCache.retrieve('weather', location, date)
20+
if(stored_data) return stored_data
1421
}
1522

16-
const temperature = Math.floor(30 * Math.random())
23+
const temperature = Math.floor(25 * Math.random())
1724

1825
const conditions = ['Sunny', 'Cloudy', 'Rainy']
1926
const index = Math.floor(conditions.length * Math.random())
2027

2128
const weather_data = { location, date, temperature, unit: 'celsius', condition: conditions[index] }
2229

23-
QuickCache.weather.push({ key: [location, date].join('_'), value: JSON.stringify(weather_data)})
30+
//QuickCache.weather.push({ key: [location, date].join('_'), value: JSON.stringify(weather_data)})
31+
QuickCache.save('weather', JSON.stringify(weather_data), location, date)
2432

2533
return weather_data
2634
}
2735

2836
function getEvents({ location, date }) {
2937

30-
//let chance = Math.round(15 * Math.random())
38+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
39+
40+
let chance = Math.round(15 * Math.random())
41+
42+
if(chance === 13) {
43+
return { error: 'Unknown error', message: 'Failed to get events in specified location and date', location, date }
44+
}
3145

32-
/*
33-
if(chance === 13) {
34-
return { error: 'Unknown error', message: 'Failed to get events in specified location and date', location, date }
3546
}
36-
*/
3747

3848
if(!location) return { error: 'Invalid location', message: 'Please specify the location' }
3949
if(!date) return { error: 'Invalid date', message: 'Please specify the date' }
4050

51+
/*
4152
if(QuickCache.events.some((qc) => qc.key === [location, date].join('_'))) {
4253
const stored_data = QuickCache.events.find((qc) => qc.key === [location, date].join('_'))
4354
if(stored_data) {
4455
return JSON.parse(stored_data.value)
4556
}
4657
}
58+
*/
59+
60+
if(QuickCache.exist('events', location, date)) {
61+
const stored_data = QuickCache.retrieve('events', location, date)
62+
if(stored_data) return stored_data
63+
}
4764

4865
const events = ['Outdoor Fiesta', 'Sumo Exhibition', 'Art Festival', 'Street Dance Parade', 'Farm Marche', 'Folk Concert', 'Soul Food Festival', 'Earth Day', 'Ramen Festival', 'Jazz Festival']
4966
const index = Math.floor(events.length * Math.random())
5067

5168
const events_data = { location, date, event: events[index] }
5269

53-
QuickCache.events.push({ key: [location, date].join('_'), value: JSON.stringify(events_data)})
70+
//QuickCache.events.push({ key: [location, date].join('_'), value: JSON.stringify(events_data)})
71+
QuickCache.save('events', JSON.stringify(events_data), location, date)
5472

5573
return events_data
5674

@@ -60,21 +78,28 @@ function getEvent({ location, date, event }) {
6078

6179
let chance = Math.round(15 * Math.random())
6280

63-
/*
64-
if(chance === 13) {
65-
return { error: 'Unknown error', message: 'Failed to get event information', event, location, date }
81+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
82+
83+
if(chance === 13) {
84+
return { error: 'Unknown error', message: 'Failed to get event information', event, location, date }
85+
}
86+
6687
}
67-
*/
6888

6989
if(!location) return { error: 'Invalid location', message: 'Please specify the location' }
7090
if(!date) return { error: 'Invalid date', message: 'Please specify the date' }
7191
if(!event) return { error: 'Invalid event', message: 'Please specify the event name' }
7292

73-
if(QuickCache.event.some((qc) => qc.key === [location, date, event].join('_'))) {
93+
/*if(QuickCache.event.some((qc) => qc.key === [location, date, event].join('_'))) {
7494
const stored_data = QuickCache.event.find((qc) => qc.key === [location, date, event].join('_'))
7595
if(stored_data) {
7696
return JSON.parse(stored_data.value)
7797
}
98+
}*/
99+
100+
if(QuickCache.exist('event', location, date, event)) {
101+
const stored_data = QuickCache.retrieve('event', location, date, event)
102+
if(stored_data) return stored_data
78103
}
79104

80105
const times = ['10:00 - 18:00', '11:00 - 15:00', '13:00 - 18:00', '15:00 - 20:00', '18:00 - 22:00']
@@ -98,7 +123,8 @@ function getEvent({ location, date, event }) {
98123

99124
const event_data = { location, date, event, time: stime, place: splace, links, images }
100125

101-
QuickCache.event.push({ key: [location, date, event].join('_'), value: JSON.stringify(event_data)})
126+
//QuickCache.event.push({ key: [location, date, event].join('_'), value: JSON.stringify(event_data)})
127+
QuickCache.save('event', JSON.stringify(event_data), location, date, event)
102128

103129
return event_data
104130

@@ -108,19 +134,26 @@ function searchHotel({ location }) {
108134

109135
let chance = Math.round(15 * Math.random())
110136

111-
/*
112-
if(chance === 13) {
113-
return { error: 'Unknown error', message: 'Failed to search for hotels in specified location', location }
137+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
138+
139+
if(chance === 13) {
140+
return { error: 'Unknown error', message: 'Failed to search for hotels in specified location', location }
141+
}
142+
114143
}
115-
*/
116144

117145
if(!location) return { error: 'Invalid location', message: 'Please specify the location' }
118146

119-
if(QuickCache.hotels.some((qc) => qc.key === location)) {
147+
/*if(QuickCache.hotels.some((qc) => qc.key === location)) {
120148
const stored_data = QuickCache.hotels.find((qc) => qc.key === location)
121149
if(stored_data) {
122150
return JSON.parse(stored_data.value)
123151
}
152+
}*/
153+
154+
if(QuickCache.exist('hotels', location)) {
155+
const stored_data = QuickCache.retrieve('hotels', location)
156+
if(stored_data) return stored_data
124157
}
125158

126159
chance = Math.round(10 * Math.random())
@@ -148,7 +181,8 @@ function searchHotel({ location }) {
148181

149182
const hotels_data = { location, items, message: `Found ${items.length} hotels` }
150183

151-
QuickCache.hotels.push({ key: location, value: JSON.stringify(hotels_data)})
184+
//QuickCache.hotels.push({ key: location, value: JSON.stringify(hotels_data)})
185+
QuickCache.save('hotels', JSON.stringify(hotels_data), location)
152186

153187
return hotels_data
154188

@@ -157,15 +191,22 @@ function searchHotel({ location }) {
157191
function getHotel({ location, hotel }) {
158192
let chance = Math.round(15 * Math.random())
159193

160-
/*
161-
if(chance === 13) {
162-
return { error: 'Unknown error', message: 'Failed to get hotel information', location, hotel }
194+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
195+
196+
if(chance === 13) {
197+
return { error: 'Unknown error', message: 'Failed to get hotel information', location, hotel }
198+
}
199+
163200
}
164-
*/
165201

166202
if(!location) return { error: 'Invalid location', message: 'Please specify the location or branch' }
167203
if(!hotel) return { error: 'Invalid hotel name', message: 'Please specify the name of hotel' }
168204

205+
if(QuickCache.exist('hotel', location, hotel)) {
206+
const stored_data = QuickCache.retrieve('hotel', location, hotel)
207+
if(stored_data) return stored_data
208+
}
209+
169210
chance = Math.round(10 * Math.random())
170211
if(chance === 8) {
171212
return { location, hotel, message: 'Hotel information not found' }
@@ -216,8 +257,11 @@ function getHotel({ location, hotel }) {
216257
chance = Math.floor(10 * Math.random())
217258
const images = [{ alt: hotel, src: chance > 5 ? 'https://i.postimg.cc/jjc1LSrH/d5592424-e3f0-4dfa-afb2-2dcc7308e321.jpg' : 'https://i.postimg.cc/Xv4hjytN/dea57a4a-532b-43d2-85bb-0e0172d8c594.jpg' }]
218259

219-
return { location, hotel, description, price: price.toLocaleString(), amenities, website, images }
260+
const hotel_data = { location, hotel, description, price: price.toLocaleString(), amenities, website, images }
220261

262+
QuickCache.save('hotel', JSON.stringify(hotel_data), location, hotel)
263+
264+
return hotel_data
221265
}
222266

223267
function reserveHotel({
@@ -230,13 +274,15 @@ function reserveHotel({
230274
roomType,
231275
specialRequests
232276
}) {
233-
//let chance = Math.round(15 * Math.random())
277+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
278+
279+
let chance = Math.round(15 * Math.random())
280+
281+
if(chance === 13) {
282+
return { error: 'Unknown error', message: 'Failed to make hotel reservation. Please try again.', hotel, location }
283+
}
234284

235-
/*
236-
if(chance === 13) {
237-
return { error: 'Unknown error', message: 'Failed to make hotel reservation. Please try again.', hotel, location }
238285
}
239-
*/
240286

241287
if(!location) return { error: 'Invalid location', message: 'Please specify the location or branch' }
242288
if(!hotel) return { error: 'Invalid hotel name', message: 'Please specify the name of hotel' }
@@ -250,9 +296,11 @@ function reserveHotel({
250296
return { status: 'No name provided', message: 'Please ask user provide your full name', hotel, location, numberOfGuests, checkInDate, checkOutDate, roomType, specialRequests }
251297
}
252298

253-
return {
299+
const reservationId = getUniqueId()
300+
301+
const reservation_data = {
254302
status: 'Reservation successful',
255-
reservationId: getUniqueId(),
303+
reservationId: reservationId,
256304
message: 'Your reservation has been completed. Please present your reservationId at the front desk.',
257305
hotel,
258306
location,
@@ -264,18 +312,39 @@ function reserveHotel({
264312
specialRequests
265313
}
266314

315+
QuickCache.save('reservation', JSON.stringify(reservation_data), location, hotel, reservationId)
316+
317+
return reservation_data
318+
267319
}
268320

269321
function getReservation({ reservationId, hotel, location }) {
270-
//let chance = Math.round(15 * Math.random())
322+
323+
let chance = Math.round(15 * Math.random())
324+
325+
if(FLAG_ALLOW_UNKNOWN_ERROR) {
326+
327+
if(chance === 13) {
328+
return { status: 'Server is busy', message: 'Failed to get hotel reservation. Please try again later.', reservationId, hotel, location }
329+
}
271330

272-
/*
273-
if(chance === 13) {
274-
return { status: 'Server is busy', message: 'Failed to get hotel reservation. Please try again later.', reservationId, hotel, location }
275331
}
276-
*/
277332

278-
return { status: "Server is busy", message: "Please try again later." }
333+
if(!location) return { error: 'Invalid location', message: 'Please specify the location or branch' }
334+
if(!hotel) return { error: 'Invalid hotel name', message: 'Please specify the name of hotel' }
335+
if(!reservationId) return { error: 'Invalid reservation id', message: 'Please specify the reservation id provided' }
336+
337+
chance = Math.round(10 * Math.random())
338+
if(chance === 8) {
339+
return { status: "Server is busy", message: "Please try again later.", reservationId, hotel, location }
340+
}
341+
342+
if(QuickCache.exist('reservation', location, hotel, reservationId)) {
343+
const stored_data = QuickCache.retrieve('reservation', location, hotel, reservationId)
344+
if(stored_data) return stored_data
345+
}
346+
347+
return { status: "Reservation not found", message: "There is no record found.", reservationId, hotel, location }
279348

280349
}
281350

0 commit comments

Comments
 (0)