forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
350 lines (251 loc) · 7.21 KB
/
interfaces.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
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
package flows
import (
"time"
"github.com/nyaruka/goflow/utils"
)
type UUID string
type NodeUUID UUID
func (u NodeUUID) String() string { return string(u) }
type ExitUUID UUID
func (u ExitUUID) String() string { return string(u) }
type FlowUUID UUID
func (u FlowUUID) String() string { return string(u) }
type ActionUUID UUID
func (u ActionUUID) String() string { return string(u) }
type ContactUUID UUID
func (u ContactUUID) String() string { return string(u) }
type ChannelUUID UUID
func (u ChannelUUID) String() string { return string(u) }
type RunUUID UUID
func (u RunUUID) String() string { return string(u) }
type StepUUID UUID
func (u StepUUID) String() string { return string(u) }
type LabelUUID UUID
func (u LabelUUID) String() string { return string(u) }
type GroupUUID UUID
func (u GroupUUID) String() string { return string(u) }
type InputUUID UUID
func (u InputUUID) String() string { return string(u) }
// RunStatus represents the current status of the engine session
type SessionStatus string
const (
// SessionStatusActive represents a session that is still active
SessionStatusActive SessionStatus = "active"
// SessionStatusCompleted represents a session that has run to completion
SessionStatusCompleted SessionStatus = "completed"
// SessionStatusWaiting represents a session which is waiting for something from the caller
SessionStatusWaiting SessionStatus = "waiting"
// SessionStatusErrored represents a session that encountered an error
SessionStatusErrored SessionStatus = "errored"
)
func (r SessionStatus) String() string { return string(r) }
// RunStatus represents the current status of the flow run
type RunStatus string
const (
// RunStatusActive represents a run that is still active
RunStatusActive RunStatus = "active"
// RunStatusCompleted represents a run that has run to completion
RunStatusCompleted RunStatus = "completed"
// RunStatusWaiting represents a run which is waiting for something from the caller
RunStatusWaiting RunStatus = "waiting"
// RunStatusErrored represents a run that encountered an error
RunStatusErrored RunStatus = "errored"
// RunStatusExpired represents a run that expired due to inactivity
RunStatusExpired RunStatus = "expired"
// RunStatusInterrupted represents a run that was interrupted by another flow
RunStatusInterrupted RunStatus = "interrupted"
)
func (r RunStatus) String() string { return string(r) }
type SessionAssets interface {
GetChannel(ChannelUUID) (Channel, error)
GetField(FieldKey) (*Field, error)
GetFieldSet() (*FieldSet, error)
GetFlow(FlowUUID) (Flow, error)
GetGroup(GroupUUID) (*Group, error)
GetGroupSet() (*GroupSet, error)
GetLabel(LabelUUID) (*Label, error)
GetLabelSet() (*LabelSet, error)
HasLocations() bool
GetLocationHierarchy() (*utils.LocationHierarchy, error)
}
type Flow interface {
UUID() FlowUUID
Name() string
Language() utils.Language
ExpireAfterMinutes() int
Translations() FlowTranslations
Validate(SessionAssets) error
Nodes() []Node
GetNode(uuid NodeUUID) Node
Reference() *FlowReference
}
type Node interface {
UUID() NodeUUID
Router() Router
Actions() []Action
Exits() []Exit
Wait() Wait
}
type EventLog interface {
Add(Event)
Events() []Event
}
type Action interface {
UUID() ActionUUID
Execute(FlowRun, Step, EventLog) error
Validate(SessionAssets) error
utils.Typed
}
type Router interface {
PickRoute(FlowRun, []Exit, Step) (interface{}, Route, error)
Validate([]Exit) error
ResultName() string
utils.Typed
}
type Route struct {
exit ExitUUID
match string
}
func (r Route) Exit() ExitUUID { return r.exit }
func (r Route) Match() string { return r.match }
var NoRoute = Route{}
func NewRoute(exit ExitUUID, match string) Route {
return Route{exit, match}
}
type Exit interface {
UUID() ExitUUID
DestinationNodeUUID() NodeUUID
Name() string
}
type Wait interface {
utils.Typed
Begin(FlowRun, Step)
CanResume(FlowRun, Step) bool
HasTimedOut() bool
Resume(FlowRun)
ResumeByTimeOut(FlowRun)
}
// FlowTranslations provide a way to get the Translations for a flow for a specific language
type FlowTranslations interface {
GetLanguageTranslations(utils.Language) Translations
}
// Translations provide a way to get the translation for a specific language for a uuid/key pair
type Translations interface {
GetTextArray(uuid UUID, key string) []string
}
type Trigger interface {
utils.Typed
TriggeredOn() time.Time
Flow() Flow
}
type Event interface {
CreatedOn() time.Time
SetCreatedOn(time.Time)
FromCaller() bool
SetFromCaller(bool)
Apply(FlowRun) error
utils.Typed
}
type Input interface {
utils.VariableResolver
utils.Typed
UUID() InputUUID
CreatedOn() time.Time
Channel() Channel
}
type Step interface {
utils.VariableResolver
UUID() StepUUID
NodeUUID() NodeUUID
ExitUUID() ExitUUID
ArrivedOn() time.Time
LeftOn() *time.Time
Leave(ExitUUID)
Events() []Event
}
// LogEntry is a container for a new event generated by the engine, i.e. not from the caller
type LogEntry interface {
Step() Step
Action() Action
Event() Event
}
// Session represents the session of a flow run which may contain many runs
type Session interface {
Assets() SessionAssets
Environment() utils.Environment
SetEnvironment(utils.Environment)
Contact() *Contact
SetContact(*Contact)
Status() SessionStatus
Trigger() Trigger
PushFlow(Flow, FlowRun)
Wait() Wait
FlowOnStack(FlowUUID) bool
Start(Trigger, []Event) error
Resume([]Event) error
Runs() []FlowRun
GetRun(RunUUID) (FlowRun, error)
GetCurrentChild(FlowRun) FlowRun
Log() []LogEntry
LogEvent(Step, Action, Event)
ClearLog()
}
// RunSummary represents the minimum information available about all runs (current or related) and is the
// representation of runs made accessible to router tests.
type RunSummary interface {
UUID() RunUUID
Contact() *Contact
Flow() Flow
Status() RunStatus
Results() Results
}
// FlowRun represents a run in the current session
type FlowRun interface {
RunSummary
Environment() utils.Environment
Session() Session
Context() utils.VariableResolver
Input() Input
Extra() utils.JSONFragment
Webhook() *utils.RequestResponse
SetContact(*Contact)
SetInput(Input)
SetStatus(RunStatus)
SetWebhook(*utils.RequestResponse)
SetExtra(utils.JSONFragment)
ApplyEvent(Step, Action, Event) error
AddError(Step, Action, error)
AddFatalError(Step, Action, error)
CreateStep(Node) Step
Path() []Step
PathLocation() (Step, Node, error)
GetText(uuid UUID, key string, native string) string
GetTextArray(uuid UUID, key string, native []string) []string
Snapshot() RunSummary
Parent() RunSummary
SessionParent() FlowRun
Ancestors() []FlowRun
CreatedOn() time.Time
ExpiresOn() *time.Time
ResetExpiration(*time.Time)
ExitedOn() *time.Time
Exit(RunStatus)
}
// ChannelType represents the type of a Channel
type ChannelType string
func (ct ChannelType) String() string { return string(ct) }
// Channel represents a channel for sending and receiving messages
type Channel interface {
UUID() ChannelUUID
Name() string
Address() string
Type() ChannelType
}
// MsgDirection is the direction of a Msg (either in or out)
type MsgDirection string
const (
// MsgOut represents an outgoing message
MsgOut MsgDirection = "O"
// MsgIn represents an incoming message
MsgIn MsgDirection = "I"
)