-
Notifications
You must be signed in to change notification settings - Fork 4
/
conversation.go
172 lines (148 loc) · 4.19 KB
/
conversation.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
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
// Copyright (C) 2014 Constantin Schomburg <me@cschomburg.com>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package natural
import (
"encoding/json"
"strings"
"time"
"github.com/sarifsystems/sarif/pkg/natural"
"github.com/sarifsystems/sarif/pkg/schema"
"github.com/sarifsystems/sarif/sarif"
)
type Conversation struct {
service *Service
Device string
LastTime time.Time
LastMessage sarif.Message
LastMessageAction Actionable
LastUserTime time.Time
LastUserText string
LastUserMessage sarif.Message
}
type MsgErrNatural struct {
Original string `json:"original"`
Type string `json:"-"`
Action interface{} `json:"action"`
Result *natural.ParseResult `json:"result"`
}
type Actionable struct {
Action *schema.Action `json:"action"`
Actions []*schema.Action `json:"actions"`
}
func (a Actionable) IsAction() bool {
if a.Action == nil || a.Action.Thing == nil {
return false
}
return strings.HasSuffix(a.Action.SchemaType, "Action")
}
func (pl MsgErrNatural) String() string {
switch pl.Type {
case "sentence":
return "I didn't understand your message. Please give me a rule to learn this sentence."
case "meaning":
return "I didn't understand your message. Please give me an example message or action to learn this sentence."
}
return "I didn't understand your message."
}
func (cv *Conversation) PublishForClient(msg sarif.Message) {
msg.Source = cv.service.DeviceId() + "/" + cv.Device
cv.service.Publish(msg)
}
func (cv *Conversation) SendToClient(msg sarif.Message) {
// Save conversation.
cv.LastTime = time.Now()
cv.LastMessage = msg
// Analyze message for possible user actions
cv.LastMessageAction = Actionable{}
msg.DecodePayload(&cv.LastMessageAction)
msg = cv.service.AnnotateReply(msg)
// Forward response to client.
msg.Id = sarif.GenerateId()
msg.Destination = cv.Device
cv.service.Publish(msg)
}
func (cv *Conversation) HandleClientMessage(msg sarif.Message) {
if msg.Text == ".full" || msg.Text == "/full" {
text, err := json.MarshalIndent(cv.LastMessage, "", " ")
if err != nil {
panic(err)
}
cv.service.Reply(msg, sarif.Message{
Action: "natural/full",
Text: string(text),
})
return
}
// Check if client answers a conversation.
if time.Now().Sub(cv.LastTime) < 5*time.Minute {
if cv.LastMessageAction.IsAction() {
parsed, ok := cv.answer(cv.LastMessageAction.Action, msg.Text)
cv.LastTime = time.Time{}
parsed.Destination = cv.LastMessage.Source
if ok {
cv.PublishForClient(parsed)
}
return
}
}
// Otherwise parse message as normal request.
ctx := &natural.Context{
Text: msg.Text,
Sender: "user",
Recipient: "sarif",
}
res, err := cv.service.Parse(ctx)
if err != nil || len(res.Intents) == 0 {
cv.handleUnknownUserMessage(msg)
return
}
pred := res.Intents[0]
if pred.Type == "exclamatory" {
cv.SendToClient(msg.Reply(sarif.Message{
Action: "natural/phrase",
Text: cv.service.phrases.Answer(msg.Text),
}))
return
}
if pred.Message.Text == "" && pred.Type != "simple" {
pred.Message.Text = msg.Text
}
cv.LastUserTime = time.Now()
cv.LastUserText = msg.Text
cv.LastUserMessage = pred.Message
pred.Message.CorrId = msg.Id
cv.PublishForClient(pred.Message)
}
func (cv *Conversation) answer(a *schema.Action, text string) (sarif.Message, bool) {
reply := sarif.Message{
Action: a.Reply,
Text: text,
}
if text == ".cancel" || text == "cancel" || strings.HasPrefix(text, "cancel ") {
return reply, false
}
t := a.SchemaType
if t == "ConfirmAction" || t == "DeleteAction" || t == "CancelAction" {
ctx := &natural.Context{Text: text, ExpectedReply: "affirmative"}
r, err := cv.service.Parse(ctx)
if err != nil || len(r.Intents) == 0 {
return reply, false
}
if r.Intents[0].Type == "neg" {
reply.Action = a.ReplyNegative
return reply, reply.Action != ""
}
}
if a.Payload != nil {
reply.EncodePayload(a.Payload)
}
return reply, true
}
func (cv *Conversation) handleUnknownUserMessage(msg sarif.Message) {
pl := &MsgErrNatural{
Original: msg.Text,
}
cv.SendToClient(msg.Reply(sarif.CreateMessage("err/natural", pl)))
}