-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgwrap.go
335 lines (296 loc) · 8.39 KB
/
msgwrap.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
package codegen
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"reflect"
"github.com/streamingfast/substreams-codegen/loop"
pbconvo "github.com/streamingfast/substreams-codegen/pb/sf/codegen/conversation/v1"
"google.golang.org/protobuf/reflect/protoreflect"
)
type MsgStart struct {
pbconvo.UserInput_Start
}
type IncomingMessage struct {
Msg any
}
func (m *IncomingMessage) Humanize(seconds int) string {
if h, ok := m.Msg.(pbconvo.Humanizable); ok {
return h.Humanize(seconds)
}
if m.Msg == nil {
return ("---")
}
return fmt.Sprintf("%d | %T %v", seconds, m.Msg, m.Msg)
}
type MsgWrapFactory struct {
sendFunc SendFunc
inputTypes map[string]reflect.Type
lastType reflect.Type
loop.EventLoop
}
func NewMsgWrapFactory(sendFunc SendFunc) *MsgWrapFactory {
f := &MsgWrapFactory{
sendFunc: sendFunc,
inputTypes: make(map[string]reflect.Type),
}
return f
}
func (f *MsgWrapFactory) SetupLoop(updateFunc func(msg loop.Msg) loop.Cmd) {
f.EventLoop = loop.NewEventLoop(updateFunc)
}
func (f *MsgWrapFactory) NewMsg(state any) *MsgWrap {
w := &MsgWrap{}
w.Msg = &pbconvo.SystemOutput{}
if state != nil {
cnt, err := json.Marshal(state)
if err != nil {
panic(err)
}
w.Msg.State = string(cnt)
}
return w
}
func (f *MsgWrapFactory) NewInput(inputMsg any, state any) *MsgWrap {
msg := f.NewMsg(state)
reflectType := reflect.TypeOf(inputMsg)
f.lastType = reflectType
el := reflect.New(reflectType).Interface()
_, ok := el.(protoreflect.ProtoMessage)
if !ok {
panic("only use NewInput with messages that embed a return value of type pbconvo.UserInput_*")
}
return msg
}
func (f *MsgWrapFactory) LastInput() reflect.Type {
return f.lastType
}
type MsgWrap struct {
Msg *pbconvo.SystemOutput
Err error
}
func (w *MsgWrap) Messagef(markdown string, args ...interface{}) *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Message_{
Message: &pbconvo.SystemOutput_Message{Markdown: fmt.Sprintf(markdown, args...)},
}
return w
}
func (w *MsgWrap) Errorf(msg string, args ...any) *MsgWrap {
w.Err = fmt.Errorf(msg, args...)
return w
}
func (w *MsgWrap) Message(markdown string) *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Message_{
Message: &pbconvo.SystemOutput_Message{Markdown: markdown},
}
return w
}
func (w *MsgWrap) Confirm(prompt string, acceptLabel, declineLabel string) *MsgWrap {
// TODO: to a type assertion on the `lastType`, to make sure it matches what we're asking here..
w.Msg.Entry = &pbconvo.SystemOutput_Confirm_{
Confirm: &pbconvo.SystemOutput_Confirm{
Prompt: prompt,
AcceptButtonLabel: acceptLabel,
DeclineButtonLabel: declineLabel,
},
}
return w
}
func (w *MsgWrap) DefaultAccept() *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_Confirm_:
entry.Confirm.DefaultButton = pbconvo.SystemOutput_Confirm_CONFIRM
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) DefaultDecline() *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_Confirm_:
entry.Confirm.DefaultButton = pbconvo.SystemOutput_Confirm_DECLINE
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) DownloadFiles() *MsgWrap {
// TODO: to a type assertion on the `lastType`, to make sure it matches what we're asking here..
w.Msg.Entry = &pbconvo.SystemOutput_DownloadFiles_{
DownloadFiles: &pbconvo.SystemOutput_DownloadFiles{},
}
return w
}
func (w *MsgWrap) AddFile(filename string, cnt []byte, fileType string, description string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_DownloadFiles_:
input := entry.DownloadFiles
input.Files = append(input.Files, &pbconvo.SystemOutput_DownloadFile{
Filename: filename,
Content: cnt,
Type: fileType,
Description: description,
})
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Loading(loading bool, label string) *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Loading_{
Loading: &pbconvo.SystemOutput_Loading{
Loading: loading,
Label: label,
},
}
return w
}
func (w *MsgWrap) StopLoading() *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Loading_{
Loading: &pbconvo.SystemOutput_Loading{
Loading: false,
},
}
return w
}
func (w *MsgWrap) Loadingf(loading bool, label string, args ...interface{}) *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Loading_{
Loading: &pbconvo.SystemOutput_Loading{
Loading: loading,
Label: fmt.Sprintf(label, args...),
},
}
return w
}
func (w *MsgWrap) TextInput(prompt string, submitButtonLabel string) *MsgWrap {
// TODO: to a type assertion on the `lastType`, to make sure it matches what we're asking here..
w.Msg.Entry = &pbconvo.SystemOutput_TextInput_{
TextInput: &pbconvo.SystemOutput_TextInput{
Prompt: prompt,
SubmitButtonLabel: submitButtonLabel,
},
}
return w
}
func (w *MsgWrap) DefaultValue(value string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_TextInput_:
entry.TextInput.DefaultValue = value
case *pbconvo.SystemOutput_ListSelect_:
entry.ListSelect.DefaultValue = value
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Description(description string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_TextInput_:
entry.TextInput.Description = description
case *pbconvo.SystemOutput_Confirm_:
entry.Confirm.Description = description
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) ListSelect(instructions string) *MsgWrap {
// TODO: to a type assertion on the `lastType`, to make sure it matches what we're asking here..
w.Msg.Entry = &pbconvo.SystemOutput_ListSelect_{
ListSelect: &pbconvo.SystemOutput_ListSelect{
Instructions: instructions,
},
}
return w
}
func (w *MsgWrap) Labels(labels ...string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_ListSelect_:
entry.ListSelect.Labels = labels
entry.ListSelect.Values = labels
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) SelectButton(label string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_ListSelect_:
entry.ListSelect.SelectButtonLabel = label
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Values(values ...string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_ListSelect_:
entry.ListSelect.Values = values
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Placeholder(message string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_TextInput_:
entry.TextInput.Placeholder = message
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Multiline(val int) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_TextInput_:
entry.TextInput.MultiLine = int32(val)
default:
panic("unsupported message type for this method")
}
return w
}
func (w *MsgWrap) Validation(regexp string, errorMessage string) *MsgWrap {
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_TextInput_:
entry.TextInput.ValidationRegexp = regexp
entry.TextInput.ValidationErrorMessage = errorMessage
default:
panic("unsupported message type for this method")
}
return w
}
func tplMe(templateText string, data interface{}) string {
tpl, err := template.New("tpl").Parse(templateText)
if err != nil {
panic(fmt.Errorf("error parsing template: %w", err))
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
panic(fmt.Errorf("error executing template: %w", err))
}
return buf.String()
}
func (w *MsgWrap) MessageTpl(templateText string, data interface{}) *MsgWrap {
w.Msg.Entry = &pbconvo.SystemOutput_Message_{
Message: &pbconvo.SystemOutput_Message{Markdown: tplMe(templateText, data)},
}
return w
}
func (w *MsgWrap) Style(style string) *MsgWrap {
// for example, "error", "warning", etc..
switch entry := w.Msg.Entry.(type) {
case *pbconvo.SystemOutput_Message_:
entry.Message.Style = style
default:
panic("unsupported message type for this method")
}
return w
}
// This will wait for an answer
func (w *MsgWrap) Cmd() loop.Cmd {
// Make sure this is called only on those that EXPECT a return value
return func() loop.Msg {
return w.Msg
}
}