-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.js
182 lines (159 loc) · 5.62 KB
/
action.js
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
import { connectRemoteModel, connectLocalModel, displayResult, clearResult } from './model.js'
const category = 'travel'
const content = 'My personal information: As a female college student traveling with a daily budget of $200.'
const flightRequest = `Cathay Pacific flight CX840 departs from Hong Kong at 4:20 p.m. on August 1, 2024,
and is expected to fly for 16 hours. tell me the estimated local time in New York JFK airport when it arrives?`
const hotelRequest = `Based on my personal information and travel destination,
suggest one hotel in the city which is suitable for me.`
const insert = (insertEntry, ctx) => async (e) => {
e.target.disabled = true
ctx.vue.entryId = await insertEntry(category, ctx.vue.content)
e.target.disabled = false
notify(`Insert entry ${ctx.vue.entryId ? 'successfully' : 'failed'}!`)
}
const update = (updateEntry, ctx) => async (e) => {
e.target.disabled = true
const result = await updateEntry(ctx.vue.entryId, ctx.vue.content)
e.target.disabled = false
notify(`Update entry ${result ? 'successfully' : 'failed'}!`)
}
const remove = (removeEntry, ctx) => async (e) => {
if (confirm('Do you want to remove the entry?')) {
e.target.disabled = true
const result = await removeEntry(ctx.vue.entryId)
e.target.disabled = false
notify(`Remove entry ${result ? 'successfully' : 'failed'}!`)
}
}
const query = (queryEntry) => async (e) => {
e.target.disabled = true
const result = await queryEntry(category)
e.target.disabled = false
notify(result)
}
const clear = (clearAll) => async (e) => {
if (confirm('Do you want to clear all entries?')) {
e.target.disabled = true
const result = await clearAll()
e.target.disabled = false
notify(`Clear all ${result ? 'successfully' : 'failed'}!`)
}
}
const flight = ({ insertEntry, updateEntry, removeEntry, queryEntry, clearAll }) => {
return ({ dom = '#app', connectModel, unifiedApi } = {}) => {
let ctx = {}
const app = Vue.createApp({
data() {
ctx.vue = this
return {
content: '',
flightRequest,
remoteModel: null,
entryId: ''
}
},
methods: {
async sendRequest(e) {
const button = e.target
const output = document.getElementById('flightRemoteOutput')
clearResult(output, button, 'Sending request...')
const request = this.flightRequest
const display = displayResult(output, button, (result) => {
const lines = result.split('\n').reverse()
const dest = lines.find((line) => line.includes('2024'))
if (dest) {
this.content = 'My travel destination: ' + dest
for (let btn of document.querySelectorAll('.entry')) btn.disabled = false
} else {
notify('Unable to retrieve valid flight information!\nYou need to resend the request.')
}
})
if (unifiedApi) {
await unifiedApi(request, display)
} else {
try {
const connect = connectModel || connectRemoteModel
this.remoteModel = this.remoteModel || (await connect())
await this.remoteModel.generateResponse(request, display)
} catch (error) {
display(error, console.error)
}
}
},
insert: insert(insertEntry, ctx),
update: update(updateEntry, ctx),
remove: remove(removeEntry, ctx),
query: query(queryEntry),
clear: clear(clearAll)
}
})
app.mount(dom)
}
}
const hotel = ({ insertEntry, updateEntry, removeEntry, queryEntry, clearAll }) => {
return ({ dom = '#app', connectModel, needCategory, unifiedApi } = {}) => {
let ctx = {}
const app = Vue.createApp({
data() {
ctx.vue = this
return {
content,
hotelRequest,
localModel: null,
entryId: ''
}
},
methods: {
async sendRequest(e) {
const button = e.target
const output = document.getElementById('hotelLocalOutput')
clearResult(output, button, 'Sending request...')
const request = needCategory ? (await queryEntry(category)) + '\n' + this.hotelRequest : this.hotelRequest
const display = displayResult(output, button)
if (unifiedApi) {
await unifiedApi(request, display)
} else {
try {
const connect = connectModel || connectLocalModel
this.localModel = this.localModel || (await connect())
if (needCategory) {
await this.localModel.generateResponse(request, display)
} else {
await this.localModel.generateResponse(request, display, category)
}
} catch (error) {
display(error, console.error)
}
}
},
insert: insert(insertEntry, ctx),
update: update(updateEntry, ctx),
remove: remove(removeEntry, ctx),
query: query(queryEntry),
clear: clear(clearAll)
}
})
app.mount(dom)
}
}
const load = async (url) => {
return new Promise((resolve, reject) => {
fetch(url)
.then((response) => response.text())
.then(resolve)
.catch(reject)
})
}
const notify = (message) => {
if (Notification.permission === 'granted') {
return new Notification(message)
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
return new Notification(message)
}
})
}
alert(message)
}
export { load, flight, hotel }