-
Notifications
You must be signed in to change notification settings - Fork 0
/
convos.go
77 lines (65 loc) · 2.13 KB
/
convos.go
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
package convos
import (
"strings"
"time"
)
var ConvoServices []ConvoService
func init() {
ConvoServices = make([]ConvoService, 0)
}
type ConvoService interface {
Login() error //Login to the conversation service
Query(query *ConversationQuery, lastState *ConversationState) (*ConversationResponse, error) //Query the service with a conversation
}
type Conversation struct {
History []*ConversationState //Conversation state history in order
}
//NewConversation returns an empty conversation
func NewConversation() (Conversation) {
return Conversation{
History: make([]*ConversationState, 0),
}
}
//QueryText returns a new conversation state for the given query text and appends it to the convo history
func (convo *Conversation) QueryText(queryText string) (*ConversationState) {
newState := &ConversationState{
Query: &ConversationQuery{
Time: time.Now(),
Text: queryText,
},
Errors: make([]error, 0),
}
test := strings.ToLower(queryText)
/*if strings.Contains(test, "reset") ||
strings.Contains(test, "reload") ||
strings.Contains(test, "restart") ||
strings.Contains(test, "refresh") {*/
if test == "reset" || test == "reload" || test == "restart" || test == "refresh" {
convo.History = make([]*ConversationState, 0)
newState.Response = &ConversationResponse{
TextSimple: "Refreshed the conversation state! I've forgotten everything...",
}
return newState
}
//Query all available convo services in registered order
for i := 0; i < len(ConvoServices); i++ {
resp, err := ConvoServices[i].Query(newState.Query, convo.LastState())
if err != nil {
newState.Errors = append(newState.Errors, err)
} else {
newState.Response = resp
break
}
}
if newState.Response != nil {
convo.History = append(convo.History, newState) //Only add successful responses to the history
}
return newState
}
//LastResponse returns the most recent conversation state
func (convo *Conversation) LastState() (*ConversationState) {
if len(convo.History) == 0 {
return nil
}
return convo.History[len(convo.History)-1]
}