-
Notifications
You must be signed in to change notification settings - Fork 19
/
interfaces.go
375 lines (278 loc) · 8.34 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
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
package flows
import (
"time"
"github.com/nyaruka/goflow/utils"
)
// NodeUUID is a UUID of a flow node
type NodeUUID utils.UUID
func (u NodeUUID) String() string { return string(u) }
// ExitUUID is the UUID of a node exit
type ExitUUID utils.UUID
func (u ExitUUID) String() string { return string(u) }
// FlowUUID is the UUID of a flow
type FlowUUID utils.UUID
func (u FlowUUID) String() string { return string(u) }
// ActionUUID is the UUID of an action
type ActionUUID utils.UUID
func (u ActionUUID) String() string { return string(u) }
// ContactUUID is the UUID of a contact
type ContactUUID utils.UUID
func (u ContactUUID) String() string { return string(u) }
// ChannelUUID is the UUID of a channel
type ChannelUUID utils.UUID
func (u ChannelUUID) String() string { return string(u) }
// RunUUID is the UUID of a flow run
type RunUUID utils.UUID
func (u RunUUID) String() string { return string(u) }
// StepUUID is the UUID of a run step
type StepUUID utils.UUID
func (u StepUUID) String() string { return string(u) }
// LabelUUID is the UUID of a label
type LabelUUID utils.UUID
func (u LabelUUID) String() string { return string(u) }
// GroupUUID is the UUID of a group
type GroupUUID utils.UUID
func (u GroupUUID) String() string { return string(u) }
// InputUUID is the UUID of an input
type InputUUID utils.UUID
func (u InputUUID) String() string { return string(u) }
// MsgUUID is the UUID of a message
type MsgUUID utils.UUID
func (u MsgUUID) String() string { return string(u) }
// SessionStatus 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) }
// SessionAssets is the assets available to a session
type SessionAssets interface {
GetChannel(ChannelUUID) (Channel, error)
GetChannelSet() (*ChannelSet, 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)
}
// Flow is a graph of nodes containing actions and routers
type Flow interface {
UUID() FlowUUID
Name() string
Language() utils.Language
ExpireAfterMinutes() int
Localization() Localization
Validate(SessionAssets) error
Nodes() []Node
GetNode(uuid NodeUUID) Node
Reference() *FlowReference
}
// Node is a single node in a flow
type Node interface {
UUID() NodeUUID
Router() Router
Actions() []Action
Exits() []Exit
Wait() Wait
}
// Action is an action within a flow node
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)
}
// Localization provide a way to get the translations for a specific language
type Localization interface {
AddItemTranslation(utils.Language, utils.UUID, string, []string)
GetTranslations(utils.Language) Translations
Languages() utils.LanguageList
}
// Translations provide a way to get the translation for a specific language for a uuid/key pair
type Translations interface {
GetTextArray(uuid utils.UUID, key string) []string
}
type Trigger interface {
utils.VariableResolver
utils.Typed
Environment() utils.Environment
Flow() Flow
Contact() *Contact
Params() utils.JSONFragment
TriggeredOn() time.Time
}
// EventOrigin is the allowed origin of an event
type EventOrigin int
const (
// EventOriginCaller means an event can originate from the caller
EventOriginCaller EventOrigin = 1
// EventOriginEngine means an event can originate from the engine
EventOriginEngine EventOrigin = 2
)
// Event describes a state change
type Event interface {
CreatedOn() time.Time
SetCreatedOn(time.Time)
FromCaller() bool
SetFromCaller(bool)
AllowedOrigin() EventOrigin
Validate(SessionAssets) error
Apply(FlowRun) error
utils.Typed
}
// EventLog is the log of events the caller must apply after each call
type EventLog interface {
Add(Event)
Events() []Event
}
type Input interface {
utils.VariableResolver
utils.Typed
UUID() InputUUID
CreatedOn() time.Time
Channel() Channel
}
type Step interface {
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
Webhook() *utils.RequestResponse
SetContact(*Contact)
SetInput(Input)
SetStatus(RunStatus)
SetWebhook(*utils.RequestResponse)
ApplyEvent(Step, Action, Event) error
AddError(Step, Action, error)
AddFatalError(Step, Action, error)
CreateStep(Node) Step
Path() []Step
PathLocation() (Step, Node, error)
GetText(utils.UUID, string, string) string
GetTextArray(utils.UUID, string, []string) []string
GetTranslatedTextArray(utils.UUID, string, []string, utils.LanguageList) []string
Snapshot() RunSummary
Parent() RunSummary
SessionParent() FlowRun
Ancestors() []FlowRun
CreatedOn() time.Time
ExpiresOn() *time.Time
ResetExpiration(*time.Time)
ExitedOn() *time.Time
Exit(RunStatus)
}
// Channel represents a channel for sending and receiving messages
type Channel interface {
UUID() ChannelUUID
Name() string
Address() string
Schemes() []string
SupportsScheme(string) bool
Roles() []ChannelRole
HasRole(ChannelRole) bool
Reference() *ChannelReference
}