-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-agent.go
More file actions
376 lines (339 loc) · 12.4 KB
/
Copy pathbasic-agent.go
File metadata and controls
376 lines (339 loc) · 12.4 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type ovonMess struct {
Ovon struct {
Conversation struct {
ID string `json:"id"`
} `json:"conversation"`
Sender struct {
From string `json:"from"`
} `json:"sender"`
ResponseCode int `json:"responseCode"`
Events []Event `json:"events"`
} `json:"ovon"`
}
type Event struct {
EventType string `json:"eventType"`
Parameters struct {
DialogEvent struct {
SpeakerID string `json:"speakerId"`
Span struct {
StartTime string `json:"startTime"`
} `json:"span"`
Features struct {
Text struct {
MimeType string `json:"mimeType"`
Tokens []Token `json:"tokens"`
} `json:"text"`
} `json:"features"`
} `json:"dialogEvent"`
} `json:"parameters"`
}
type Token struct {
Value string `json:"value"`
}
//---------------- end of OVON messages -----------------------
var sendJson string
var recvJson string
func handler(w http.ResponseWriter, r *http.Request) {
mthd := r.Method
fmt.Fprintf(w, "basic agent is alive.\nRequest method is: %s", mthd)
}
func respHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now().Local().String() + "------------------------ respHandler ---------------------------------------")
fmt.Println("--------------------- respHandler Request Headers-----------------------")
for name, value := range r.Header {
fmt.Printf("%v: %v\n", name, value)
}
fmt.Println("------------------------------------------------------------------------")
reqBody, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("reqBody: %s\n", reqBody)
params, _ := url.ParseQuery(string(reqBody))
agentUrl := params.Get("agenturl") //URL.Query().Get("agenturl")
question := params.Get("question") //URL.Query().Get("question")
dispjson := params.Get("dispjson") //URL.Query().Get("dispjson")
agentResp := ""
sendJson = ""
recvJson = ""
fmt.Println("---------------------------------------------")
fmt.Println("agentUrl = " + agentUrl)
fmt.Println("dispjson = " + dispjson)
fmt.Println("---------------------------------------------")
if agentUrl != "" {
agentResp = getAgentResp(agentUrl, question)
}
content := "<head>"
content += "<title>OVON Agent Browser</title>"
content += "<style type=\"text/css\">"
content += " span#art{"
content += " vertical-align:middle;"
content += " line-height: 60px;"
content += " font-size: 30px;"
content += " font-weight: bold;"
content += " font-family: Ariel,sans-serif,monospace;"
content += " color: red;"
content += " padding-left: 30px;"
content += " }"
content += " div#intro {"
content += " width:60%;"
content += " margin-left:20px;"
content += " padding:10px;"
content += " font-family: Ariel,sans-serif,monospace;"
content += " }"
content += " form#reqform {"
content += " width:100%;"
content += " margin-left:30px;"
content += " margin-top:10px;"
content += " margin-bottom:10px;"
content += " }"
content += " div.json {"
content += " width:75%;"
content += " background-color: #faeecd;"
content += " margin-left:20px;"
content += " margin-top:10px;"
content += " margin-bottom:10px;"
content += " padding:10px;"
content += " }"
content += " div#agentresp {"
content += " width:75%;"
content += " margin-left:20px;"
content += " padding:10px;"
content += " font-family: Ariel,sans-serif,monospace;"
content += " border-style:solid;"
content += " border-width:1px;"
content += " }"
content += " div#sendjson {"
content += " }"
content += " div#recvjson {"
content += " }"
content += "</style>"
content += "</head>"
content += "<body>"
content += "<span id='art'>OVON Agent Browser</span>"
content += "<div id = 'intro'>"
content += "Enter the URL of an OVON-message compliant agent, "
content += "then enter the message/question that you would like "
content += "to send to the agent.<br> The response from the agent will be shown "
content += "below.<br> Tick the 'Show JSON' box to display the "
content += "JSON messages sent to, and received from the agent. "
content += "</div>"
content += "<form id=\"reqform\" action=\"/getresponse\" method=\"post\" enctype=\"application/x-www-form-urlencoded\" "
content += " <label for=\"agenturl\">Agent URL:</label><br>"
content += " <input type=\"text\" id=\"agenturl\" name=\"agenturl\" value=\""
content += agentUrl
content += "\" size=\"50\"><br>"
content += " <label for=\"question\">Enter your question:</label><br>"
content += "<textarea id=\"question\" name=\"question\" rows=\"4\" cols=\"80\">"
content += question
content += "</textarea><br><br>"
content += "<input type=\"checkbox\" id=\"dispjson\" name=\"dispjson\" value=\"yes\">"
content += "<label for=\"dispjson\"> Show JSON</label><br><br>"
content += " <input type=\"submit\" value=\"Submit your question to the Agent\">"
content += "</form> "
content += "<div id='agentresp'>"
content += agentResp
content += "</div>"
if strings.Contains(dispjson, "yes") {
content += "<div div id='sendjson' class='json'>"
content += "<u>JSON sent to agent</u><br>"
content += sendJson
content += "</div>"
content += "<div id-'recvjson' class='json'>"
content += "<u>JSON received from agent</u><br>"
content += recvJson
content += "</div>"
}
content += "</body>"
//w.Header().Add("Content-Type", "text/plain")
fmt.Fprint(w, content)
}
func getAgentResp(agenturl string, question string) string {
retval := ""
now := time.Now().Local().String()
var newtoken Token
newtoken.Value = question
var utterevent Event
utterevent.EventType = "utterance"
utterevent.Parameters.DialogEvent.SpeakerID = "textBrowser"
utterevent.Parameters.DialogEvent.Span.StartTime = now
utterevent.Parameters.DialogEvent.Features.Text.MimeType = "text/plain"
utterevent.Parameters.DialogEvent.Features.Text.Tokens = append(utterevent.Parameters.DialogEvent.Features.Text.Tokens, newtoken)
var omess ovonMess
omess.Ovon.Sender.From = "https://www.someserver.com/getresponse"
omess.Ovon.ResponseCode = 200
omess.Ovon.Conversation.ID = "OvonDemo137"
omess.Ovon.Events = append(omess.Ovon.Events, utterevent)
jsonBytes, _ := json.Marshal(omess)
fmt.Println("getAgentResp: " + string(jsonBytes))
sendJson = string(jsonBytes)
url := agenturl
contentType := "application/json"
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", contentType)
//req.Header.Add("Access-Control-Allow-Origin:", "*") //use only in response
resp, err := client.Do(req) //// send the http request
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println("\n\n message back: " + string(body))
test := string(body)
if strings.Contains(test, "Error") {
return test
} else {
recvJson = string(body)
var respMess ovonMess
json.Unmarshal(body, &respMess)
//retval = respMess.Ovon.Events[0].Parameters.DialogEvent.Features.Text.Tokens[0].Value
var eventSlice = respMess.Ovon.Events
numevents := len(eventSlice)
fmt.Printf("getAgentResp: numevents = %d\n", numevents)
for i := 0; i < numevents; i++ {
if eventSlice[i].EventType == "invite" {
//doInviteResponse(w, conversationId)
break
} else if eventSlice[i].EventType == "utterance" {
tokenslice := eventSlice[i].Parameters.DialogEvent.Features.Text.Tokens
numtokens := len(tokenslice)
fmt.Printf("event[%d] has %d tokens \n", i, numtokens)
if numtokens > 0 {
retval = tokenslice[0].Value
}
break
}
}
}
return retval
}
func ovonHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now().Local().String() + "------------------------ ovonHandler ---------------------------------------")
bodyBytes, _ := io.ReadAll(r.Body)
var method = r.Method
var origin = ""
origin = r.Header.Get("Origin")
var reqMethod = ""
reqMethod = r.Header.Get("Access-Control-Request-Method")
fmt.Printf("%v: %v\n", "Method", method)
fmt.Println("--------------------- ovonHandler Request Headers-----------------------")
for name, value := range r.Header {
fmt.Printf("%v: %v\n", name, value)
}
fmt.Println("--------------------------------------------------------- ")
if method == "OPTIONS" && origin != "" && reqMethod != "" { //It's a PREFLIGHT REQUEST
w.WriteHeader(204)
w.Header().Add("Access-Control-Allow-Method", r.Header.Get("Access-Control-Request-Method"))
w.Header().Add("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers"))
fmt.Fprint(w, "preflight") /// only visible if response code is 200
fmt.Printf("%v\n", "Request is a PREFLIGHT")
fmt.Println("--------------------- ovonHandler Response Headers-----------------------")
for name, value := range w.Header() {
fmt.Printf("%v: %v\n", name, value)
}
fmt.Println("-------------------------------------------------------------------------")
} else { // It's a DATA REQUEST
bodyString := string(bodyBytes)
fmt.Printf("\nbodystring: %v\n", bodyString)
var mess string
// Convert response body to Todo struct
var inmess ovonMess
json.Unmarshal(bodyBytes, &inmess)
fmt.Printf("\ninmess\n%+v\n", inmess)
var conversationId = inmess.Ovon.Conversation.ID
var eventSlice = inmess.Ovon.Events
numevents := len(eventSlice)
fmt.Printf("numevents = %d\n", numevents)
for i := 0; i < numevents; i++ {
if eventSlice[i].EventType == "invite" {
doInviteResponse(w, conversationId)
break
} else if eventSlice[i].EventType == "utterance" {
tokenslice := eventSlice[i].Parameters.DialogEvent.Features.Text.Tokens
numtokens := len(tokenslice)
fmt.Printf("event[%d] has %d tokens \n", i, numtokens)
if numtokens > 0 {
mess = tokenslice[0].Value
}
doResponse(w, mess, conversationId)
break
}
}
}
}
func doInviteResponse(w http.ResponseWriter, conversationId string) {
now := time.Now().Local().String()
var intoken Token
intoken.Value = "Ready"
var utterevent Event
utterevent.EventType = "utterance"
utterevent.Parameters.DialogEvent.SpeakerID = "basic-agent"
utterevent.Parameters.DialogEvent.Span.StartTime = now
utterevent.Parameters.DialogEvent.Features.Text.MimeType = "text/plain"
utterevent.Parameters.DialogEvent.Features.Text.Tokens = append(utterevent.Parameters.DialogEvent.Features.Text.Tokens, intoken)
var omess ovonMess
omess.Ovon.Sender.From = "https://www.someserver.com/ovontest"
omess.Ovon.ResponseCode = 200
omess.Ovon.Conversation.ID = conversationId
omess.Ovon.Events = append(omess.Ovon.Events, utterevent)
fmt.Printf("\n%+v\n", omess)
outBytes, _ := json.Marshal(omess)
w.Header().Add("Access-Control-Allow-Origin", "*") /// needed by CORS Request
fmt.Printf("\n%+v\n", string(outBytes))
fmt.Fprint(w, string(outBytes))
}
func doResponse(w http.ResponseWriter, mess string, conversationId string) {
now := time.Now().Local().String()
var intoken Token
intoken.Value = getAgentResponse(mess) ///////// call another agent or API
var utterevent Event
utterevent.EventType = "utterance"
utterevent.Parameters.DialogEvent.SpeakerID = "basic-agent"
utterevent.Parameters.DialogEvent.Span.StartTime = now
utterevent.Parameters.DialogEvent.Features.Text.MimeType = "text/plain"
utterevent.Parameters.DialogEvent.Features.Text.Tokens = append(utterevent.Parameters.DialogEvent.Features.Text.Tokens, intoken)
var omess ovonMess
omess.Ovon.Sender.From = "https://www.someserver.com/ovontest"
omess.Ovon.ResponseCode = 200
omess.Ovon.Conversation.ID = conversationId
omess.Ovon.Events = append(omess.Ovon.Events, utterevent)
fmt.Printf("\n%+v\n", omess)
outBytes, _ := json.Marshal(omess)
w.Header().Add("Access-Control-Allow-Origin", "*") /// needed by CORS Request
fmt.Println("--------------------- ovonHandler Response Headers-----------------------")
for name, value := range w.Header() {
fmt.Printf("%v: %v\n", name, value)
}
fmt.Println("-------------------------------------------------------------------------")
fmt.Printf("\n%+v\n", string(outBytes))
fmt.Fprint(w, string(outBytes))
}
func getAgentResponse(mess string) string {
retval := "You said - " + mess
return retval
}
func main() {
http.HandleFunc("/ovontest", ovonHandler)
http.HandleFunc("/getresponse", respHandler)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8082", nil))
}