-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHumanAgent.R
114 lines (105 loc) · 2.33 KB
/
HumanAgent.R
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
HumanAgent <- R6Class(
"HumanAgent",
public = list(
id="",
initialize = function(id) {
self$id = id
self
},
chatWithAgent = function(id,msg) {
msg=commandHandler$encodeCommand(list(
from=self$id,
to=id,
action="chat",
msg=msg
))
commandHandler$handleCommand(msg,self)
},
# called by the command handler to ask
# permission if it can execute the command
askPermission = function(msg) {
a <- "z"
response <- list(
choice="c"
)
# any message originating from C0 or h0
# does not require permission,
# we already validated the sender earlier
if(msg$msg$from %in% c("C0","h0")) {
return(response)
}
while(a!="i") {
cat("c-continue, i-interact, d-debug, q-quit: ")
flush.console()
a <- readline()
if(a=="i") {
response$msg <- readline("Respond: ")
response$choice <- "i"
break
} else if(a=="d") {
browser()
} else if(a=="q") {
response$choice <- "q"
break
} else if(a=="c") {
response$choice <- "c"
break
}
}
response
},
# gets user input
getInput = function(msg) {
if(config$alwaysChat) {
return(readline(": "))
}
a <- "z"
while(a!="i") {
a <- readline("i-interact, d-debug, q-quit: ")
if(a=="i") {
return(readline("Respond: "))
} else if(a=="d") {
browser()
} else if(a=="q") {
return(NULL)
}
}
},
# this is called by other agents when they
# want to chat with us, also by default
# any message that doesn't make sense gets
# sent to us
chat = function(msg) {
# get the response from the user
# pass the message incase the user wants
# it for debug info
response <- self$getInput(msg)
# if the user quits it returns a null response
# if we stop chatting with the agent, our
# conversation cannot continue so this is like
# a quit
#if(!is.null(response)) {
# self$chatWithAgent(msg$from,response)
#}
if(is.null(response)) {
return(commandHandler$encodeCommand(list(
from="h0",
action="exit",
comment="user requested exit"
)))
}
commandHandler$execute(list(
from="h0",
to=msg$from,
action="chat",
msg=response
))
#commandHandler$encodeCommand(list(
# from="h0",
# to=msg$from,
# action="chat",
# msg=response
#))
}
)
)