generated from wesen/wesen-go-template
/
backend.go
111 lines (98 loc) · 2.22 KB
/
backend.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
package main
import (
"context"
"github.com/charmbracelet/bubbletea"
"github.com/go-go-golems/bobatea/pkg/chat"
conversationui "github.com/go-go-golems/bobatea/pkg/chat/conversation"
"github.com/go-go-golems/bobatea/pkg/conversation"
"github.com/pkg/errors"
"strings"
"time"
)
type FakeBackend struct {
p *tea.Program
cancel context.CancelFunc
isRunning bool
}
var _ chat.Backend = &FakeBackend{}
func NewBackend(p *tea.Program) *FakeBackend {
return &FakeBackend{
p: p,
}
}
func (f *FakeBackend) Start(ctx context.Context, msgs []*conversation.Message) (tea.Cmd, error) {
if f.isRunning {
return nil, errors.New("Step is already running")
}
return func() tea.Msg {
ctx, f.cancel = context.WithCancel(ctx)
if len(msgs) == 0 {
return nil
}
lastMsg := msgs[len(msgs)-1]
words := strings.Fields(lastMsg.Content.String())
reversedWords := reverseWords(words)
msg := strings.Join(reversedWords, " ")
metadata := conversationui.StreamMetadata{
ID: conversation.NewNodeID(),
ParentID: lastMsg.ID,
}
go func() {
tick := time.Tick(200 * time.Millisecond)
idx := 0
defer func() {
f.p.Send(chat.BackendFinishedMsg{})
f.cancel()
f.cancel = nil
f.isRunning = false
}()
for {
select {
case <-ctx.Done():
return
case <-tick:
if idx < len(reversedWords) {
completion := strings.Join(reversedWords[:idx+1], " ")
f.p.Send(
conversationui.StreamCompletionMsg{
StreamMetadata: metadata,
Delta: reversedWords[idx] + " ",
Completion: completion,
},
)
idx++
} else {
f.p.Send(conversationui.StreamDoneMsg{
StreamMetadata: metadata,
Completion: msg,
})
return
}
}
}
}()
return conversationui.StreamStartMsg{
StreamMetadata: metadata,
}
}, nil
}
func (f *FakeBackend) Interrupt() {
if f.cancel != nil {
f.cancel()
}
}
func (f *FakeBackend) Kill() {
if f.cancel != nil {
f.cancel()
}
}
func (f *FakeBackend) IsFinished() bool {
return !f.isRunning
}
func reverseWords(words []string) []string {
reversed := make([]string, len(words))
for i, word := range words {
reversed[len(words)-1-i] = word
}
return reversed
}