-
Notifications
You must be signed in to change notification settings - Fork 19
/
engine.go
130 lines (108 loc) · 3.75 KB
/
engine.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
package engine
import (
"encoding/json"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent"
"github.com/nyaruka/goflow/flows"
)
// an instance of the engine
type engine struct {
evaluator *excellent.Evaluator
services *services
options *flows.EngineOptions
}
// NewSession creates a new session
func (e *engine) NewSession(sa flows.SessionAssets, trigger flows.Trigger) (flows.Session, flows.Sprint, error) {
s := &session{
uuid: flows.SessionUUID(uuids.New()),
env: envs.NewBuilder().Build(),
engine: e,
assets: sa,
trigger: trigger,
status: flows.SessionStatusActive,
batchStart: trigger.Batch(),
runsByUUID: make(map[flows.RunUUID]flows.Run),
}
sprint, err := s.start(trigger)
return s, sprint, err
}
// ReadSession reads an existing session
func (e *engine) ReadSession(sa flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Session, error) {
return readSession(e, sa, data, missing)
}
func (e *engine) Evaluator() *excellent.Evaluator { return e.evaluator }
func (e *engine) Services() flows.Services { return e.services }
func (e *engine) Options() *flows.EngineOptions { return e.options }
var _ flows.Engine = (*engine)(nil)
//------------------------------------------------------------------------------------------
// Builder
//------------------------------------------------------------------------------------------
// Builder is a builder for engine configs
type Builder struct {
eng *engine
}
// NewBuilder creates a new engine builder
func NewBuilder() *Builder {
return &Builder{
eng: &engine{
evaluator: excellent.NewEvaluator(),
services: newEmptyServices(),
options: &flows.EngineOptions{
MaxStepsPerSprint: 100,
MaxResumesPerSession: 500,
MaxTemplateChars: 10000,
MaxFieldChars: 640,
MaxResultChars: 640,
},
},
}
}
// WithEmailServiceFactory sets the email service factory
func (b *Builder) WithEmailServiceFactory(f EmailServiceFactory) *Builder {
b.eng.services.email = f
return b
}
// WithWebhookServiceFactory sets the webhook service factory
func (b *Builder) WithWebhookServiceFactory(f WebhookServiceFactory) *Builder {
b.eng.services.webhook = f
return b
}
// WithClassificationServiceFactory sets the NLU service factory
func (b *Builder) WithClassificationServiceFactory(f ClassificationServiceFactory) *Builder {
b.eng.services.classification = f
return b
}
// WithAirtimeServiceFactory sets the airtime service factory
func (b *Builder) WithAirtimeServiceFactory(f AirtimeServiceFactory) *Builder {
b.eng.services.airtime = f
return b
}
// WithMaxStepsPerSprint sets the maximum number of steps allowed in a single sprint
func (b *Builder) WithMaxStepsPerSprint(max int) *Builder {
b.eng.options.MaxStepsPerSprint = max
return b
}
// WithMaxResumesPerSession sets the maximum number of resumes allowed in a single session
func (b *Builder) WithMaxResumesPerSession(max int) *Builder {
b.eng.options.MaxResumesPerSession = max
return b
}
// WithMaxTemplateChars sets the maximum number of characters allowed from an evaluated template
func (b *Builder) WithMaxTemplateChars(max int) *Builder {
b.eng.options.MaxTemplateChars = max
return b
}
// WithMaxFieldChars sets the maximum number of characters allowed in a contact field value
func (b *Builder) WithMaxFieldChars(max int) *Builder {
b.eng.options.MaxFieldChars = max
return b
}
// WithMaxResultChars sets the maximum number of characters allowed in a result value
func (b *Builder) WithMaxResultChars(max int) *Builder {
b.eng.options.MaxResultChars = max
return b
}
// Build returns the final engine
func (b *Builder) Build() flows.Engine { return b.eng }