-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
234 lines (194 loc) · 6.24 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"muvtuberdriver/audio"
"muvtuberdriver/chatbot"
"muvtuberdriver/config"
"muvtuberdriver/live2d"
"muvtuberdriver/model"
"muvtuberdriver/sayer"
"net/http"
"os"
"reflect"
"golang.org/x/exp/slog"
)
var (
dryRun = flag.Bool("dryrun", false, "prints config and exit.")
genExampleConfig = flag.Bool("gen_example_config", false, "generate example config, print to stdout and exit")
configFile = flag.String("c", "", "read config file (YAML)")
)
// Config is the global config
var Config = config.UseConfig()
func main() {
flag.Parse()
if *genExampleConfig {
c := config.ExampleConfig()
c.Write(os.Stdout)
return
}
if *configFile != "" {
slog.Info("Reading config file.", "configFile", *configFile)
Config.ReadFromYaml(*configFile)
} else {
slog.Error("Config file is required, use -c path/to/config.yaml to specify")
os.Exit(1)
}
slog.Info("Config loaded:")
Config.DesensitizedCopy().Write(log.Writer())
if *dryRun {
os.Exit(0)
}
os.Setenv("COOLDOWN_INTERVAL", fmt.Sprintf("%v", Config.Chatbot.Chatgpt.GetCooldownDuraton()))
slog.Info("set COOLDOWN_INTERVAL from config value.", "COOLDOWN_INTERVAL", os.Getenv("COOLDOWN_INTERVAL"))
textInChan := make(chan *model.TextIn, RecvMsgChanBuf)
textOutChan := make(chan *model.TextOut, RecvMsgChanBuf)
audioController := audio.NewController()
go func() {
log.Fatal(http.ListenAndServe(
Config.Listen.AudioControllerWs,
audioController.WsHandler()))
}()
live2d := live2d.NewDriver(Config.Live2d.Driver, Config.Live2d.Forwarder)
// sayer := sayer.NewAllInOneSayer(Config.Sayer.Server, Config.Sayer.Role, audioController, live2d)
sayer := sayer.NewLipsyncSayer(Config.Sayer.Server,
audioController, live2d,
sayer.WithTtsRole(Config.Sayer.Role),
sayer.WithLipsyncStrategy(Config.Sayer.GetLipsyncStrategy()))
// (dm) & (http) -> in
if Config.Blivedm.Roomid != 0 {
go TextInFromDm(Config.Blivedm.Roomid, textInChan, WithBlivedmServer(Config.Blivedm.Server))
}
if Config.Listen.TextInHttp != "" {
go TextInFromHTTP(Config.Listen.TextInHttp, "/", textInChan)
}
// in -> filter -> in
textInFiltered := textInChan
// textInFiltered = ChineseFilter4TextIn.FilterTextIn(textInFiltered)
textInFiltered = NewPriorityReduceFilter(Config.GetReduceDuration()).FilterTextIn(textInFiltered)
// read dm
textInFiltered = TextFilterFunc(func(text string) bool {
if !Config.ReadDm {
return true
}
live2d.Live2dToMotion("flick_head") // 准备张嘴说话
sayer.Say(text)
return true
}).FilterTextIn(textInFiltered)
// in -> chatbot -> out
pchatbot, err := initPrioritizedChatbot()
if err != nil {
log.Fatal(err)
}
go chatbot.TextOutFromChatbot(pchatbot, textInFiltered, textOutChan)
// out -> filter -> out
textOutFiltered := textOutChan
// textOutFiltered := ChineseFilter4TextOut.FilterTextOut(textOutChan)
// too long: no say
tooLongFilter := NewTooLongFilter(Config.TooLong.MaxWords, Config.TooLong.Quibbles)
textOutFiltered = tooLongFilter.TextFilterFunc(func(text, quibble *string) {
if quibble != nil {
sayer.Say(*quibble)
} else {
sayer.Say("这是禁止事项。")
}
}).FilterTextOut(textOutFiltered)
textOutFiltered = NewPriorityReduceFilter(Config.GetReduceDuration()).FilterTextOut(textOutFiltered)
// out -> (live2d) & (say) & (stdout)
for {
textOut := <-textOutFiltered
if textOut == nil {
continue
}
// fmt.Println(*textOut)
slog.Info("[textOut]",
"author", textOut.Author,
"priority", textOut.Priority,
"content", textOut.Content)
// emotext result breaks the lipsync
// TODO: emotext on muvtuberdriver side, not on live2ddriver side
if Config.Sayer.LipsyncStrategy != "audio_analyze" {
live2d.TextOutToLive2DDriver(textOut)
}
sayer.Say(textOut.Content)
if Config.TextOutHttp.Server != "" {
if rand.Intn(100) >= Config.TextOutHttp.DropRate {
TextOutToHttp(Config.TextOutHttp.Server, textOut)
} else {
slog.Info("[TextOutHttp] random drop textOut.")
}
}
}
}
// initChatbotFunc is a type of function that initializes a chatbot.
//
// A initChatbotFunc should return a chatbot and nil error if it succeeds.
// If it fails, it should return nil and a non-nil error.
//
// A initChatbotFunc reads config from the global Config variable.
type initChatbotFunc func() (chatbot.Chatbot, error)
// initPrioritizedChatbot initializes a prioritized chatbot with all configured chatbots.
//
// It logs the error and continue if a chatbot fails to initialize.
func initPrioritizedChatbot() (chatbot.Chatbot, error) {
var chatbots []chatbot.Chatbot
// 按照优先级 从低到高 依次加入 chatbots
initChatbotFuncs := []initChatbotFunc{
initMusharingChatbot,
initChatgptChatbot,
}
for _, initChatbotFunc := range initChatbotFuncs {
bot, err := initChatbotFunc()
if err != nil {
slog.Error("init chatbot failed",
"initChatbotFunc", reflect.TypeOf(initChatbotFunc).Name(),
"err", err)
continue
}
if bot != nil {
chatbots = append(chatbots, bot)
}
}
// 按照前面 append 的顺序,index -> priority 从低到高
// 组成 prioritizedChatbot。
chatbotMap := map[model.Priority]chatbot.Chatbot{}
for i, bot := range chatbots {
chatbotMap[model.Priority(i)] = bot
}
prioritizedChatbot := chatbot.NewPrioritizedChatbot(chatbotMap)
return prioritizedChatbot, nil
}
// initMusharingChatbot initializes a musharing chatbot if configured.
//
// This function directly reads the global Config.
func initMusharingChatbot() (chatbot.Chatbot, error) {
enabled, err := Config.Chatbot.Musharing.IsEnabledAndValid()
if !enabled {
slog.Info("musharing chatbot is disabled")
return nil, nil
}
if err != nil {
return nil, err
}
musharingChatbot, err := chatbot.NewMusharingChatbot(Config.Chatbot.Musharing.Server)
return musharingChatbot, err
}
// initChatgptChatbot initializes a chatgpt chatbot if configured.
//
// This function directly reads the global Config.
func initChatgptChatbot() (chatbot.Chatbot, error) {
cfg := Config.Chatbot.Chatgpt
enabled, err := cfg.IsEnabledAndValid()
if !enabled {
slog.Info("chatgpt chatbot is disabled")
return nil, nil
}
if err != nil {
return nil, err
}
chatgptChatbot, err := chatbot.NewChatGPTChatbot(
cfg.Server, cfg.GetCooldownDuraton(), cfg.Configs...)
return chatgptChatbot, err
}