-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
203 lines (180 loc) · 5.06 KB
/
app.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
package main
import (
"context"
"fmt"
"fy-novel/internal/functions"
"fy-novel/internal/model"
progressTool "fy-novel/internal/tools/progress"
"os"
"github.com/sirupsen/logrus"
)
// App struct
type App struct {
ctx context.Context
log *logrus.Logger
checkUpdater *functions.CheckUpdater
downloader *functions.Downloader
confHandler *functions.ConfHandler
getHint *functions.GetHint
chatbot *functions.FyChatbot
funnyToy *functions.FunnyToy
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
log := logrus.New()
// Setup logger
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
// Set log level
log.SetLevel(logrus.ErrorLevel)
a.checkUpdater = functions.NewCheckUpdate(log, 5000)
a.downloader = functions.NewDownload(log)
a.confHandler = functions.NewGetConf(log)
a.getHint = functions.NewGetHint(log)
a.chatbot = functions.NewFyChatbot(log)
a.funnyToy = functions.NewFunnyToy(log)
a.log = log
}
func (a *App) SerachNovel(name string) []*model.SearchResult {
res, err := a.downloader.Serach(name)
if err != nil {
a.log.Errorf("app SerachNovel error: %v", err)
return nil
}
return res
}
func (a *App) DownLoadNovel(sr *model.SearchResult) *model.CrawlResult {
res, err := a.downloader.DownLoad(sr)
if err != nil {
a.log.Errorf("app DownLoadNovel error: %v", err)
return nil
}
return res
}
func (a *App) GetUpdateInfo() *model.GetUpdateInfoResult {
return a.checkUpdater.CheckUpdate()
}
func (a *App) GetUsageInfo() *model.GetUsageInfoResult {
return a.getHint.GetUsageInfo()
}
func (a *App) GetConfig() *model.GetConfigResult {
res := &model.GetConfigResult{}
res.Config = a.confHandler.GetConfig()
return res
}
func (a *App) GetDownloadProgress(sr *model.SearchResult) *model.ProgressResult {
res := &model.ProgressResult{}
completed, total, exists := progressTool.GetProgress(sr.Url)
if exists {
res.Exists = exists
res.Completed = int(completed)
res.Total = int(total)
}
return res
}
func (a *App) SetConfig(conf string) string {
if err := a.confHandler.SetConfig(conf); err != nil {
return err.Error()
}
return ""
}
func (a *App) StartChatbot(userInput string) *model.StartChatbotResult {
res := &model.StartChatbotResult{}
resp, err := a.chatbot.StartChatbot(a.ctx, userInput)
if err != nil {
errMsg := fmt.Sprintf("app StartChatbot error: %v", err)
a.log.Error(errMsg)
res.ErrorMsg = errMsg
return res
}
res.Response = resp
return res
}
func (a *App) HasInitOllama() *model.HasInitOllamaResult {
res := &model.HasInitOllamaResult{}
has, isInit, isSetModel, err := a.chatbot.FindOllamaContainer(a.ctx)
if err != nil {
errMsg := fmt.Sprintf("app HasInitOllama error: %v", err)
a.log.Error(errMsg)
res.ErrorMsg = errMsg
return res
}
res.Has = has
res.IsInit = isInit
res.IsSetModel = isSetModel
return res
}
func (a *App) InitOllama() *model.InitOllamaResult {
res := &model.InitOllamaResult{}
a.chatbot.InitOllama(a.ctx)
return res
}
func (a *App) GetInitOllamaProgress() *model.InitOllamaProgressResult {
res := &model.InitOllamaProgressResult{}
completed, total, exists := a.chatbot.GetInitOllamaProgress(a.ctx)
if exists {
res.Exists = exists
res.Completed = int(completed)
res.Total = int(total)
}
return res
}
func (a *App) InitSetOllamaModelTask() *model.InitSetOllamaModelResult {
res := &model.InitSetOllamaModelResult{}
err := a.chatbot.InitSetOllamaModelTask(a.ctx)
if err != nil {
res.ErrorMsg = err.Error()
}
return res
}
func (a *App) SetOllamaModel(modelName string) *model.SetOllamaModelResult {
res := &model.SetOllamaModelResult{}
a.chatbot.SetOllamaModel(a.ctx, modelName)
return res
}
func (a *App) GetCurrentUseModel() *model.GetCurrentUseModelResult {
res := &model.GetCurrentUseModelResult{}
res.Model = a.chatbot.GetCurrentUseModel(a.ctx)
return res
}
func (a *App) GetSelectModelList() *model.GetSelectModelListResult {
res := &model.GetSelectModelListResult{}
res.Models = a.chatbot.GetSelectModelList(a.ctx)
return res
}
func (a *App) GetSetOllamaModelProgress() *model.GetSetOllamaModelProgressResult {
res := &model.GetSetOllamaModelProgressResult{}
completed, total, exists := a.chatbot.GetSetOllamaModelProgress(a.ctx)
if exists {
res.Exists = exists
res.Completed = int(completed)
res.Total = int(total)
}
return res
}
func (a *App) DeepSeekChat(inputText, deepseekApiKey string) *model.StartChatbotResult {
res := &model.StartChatbotResult{}
resp, err := a.chatbot.CreateChatCompletion(a.ctx, inputText, deepseekApiKey)
if err != nil {
errMsg := fmt.Sprintf("app DeepSeekChat error: %v", err)
a.log.Error(errMsg)
res.ErrorMsg = errMsg
return res
}
res.Response = resp
return res
}
func (a *App) GenerateAsciiImage(yukkuriParams model.YukkuriParams) *model.GenerateAsciiImageResult {
res := &model.GenerateAsciiImageResult{}
a.funnyToy.ConvertToAscii(a.ctx, yukkuriParams)
res.Response = ""
return res
}