Skip to content

Commit d5227f1

Browse files
committed
optimize(aichat): use config struct
1 parent 62e9fe6 commit d5227f1

File tree

2 files changed

+132
-195
lines changed

2 files changed

+132
-195
lines changed

plugin/aichat/cfg.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package aichat
2+
3+
import (
4+
"strings"
5+
6+
ctrl "github.com/FloatTech/zbpctrl"
7+
"github.com/FloatTech/zbputils/chat"
8+
"github.com/fumiama/deepinfra"
9+
"github.com/fumiama/deepinfra/model"
10+
"github.com/sirupsen/logrus"
11+
zero "github.com/wdvxdr1123/ZeroBot"
12+
"github.com/wdvxdr1123/ZeroBot/message"
13+
)
14+
15+
var cfg = newconfig()
16+
17+
type config struct {
18+
ModelName string
19+
Type int
20+
SystemP string
21+
API string
22+
Key string
23+
Separator string
24+
NoReplyAT bool
25+
NoSystemP bool
26+
}
27+
28+
func newconfig() config {
29+
return config{
30+
ModelName: model.ModelDeepDeek,
31+
SystemP: chat.SystemPrompt,
32+
API: deepinfra.OpenAIDeepInfra,
33+
}
34+
}
35+
36+
func (c *config) isvalid() bool {
37+
return c.ModelName != "" && c.API != "" && c.Key != ""
38+
}
39+
40+
func ensureconfig(ctx *zero.Ctx) bool {
41+
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
42+
if !ok {
43+
return false
44+
}
45+
if !cfg.isvalid() {
46+
err := c.GetExtra(&cfg)
47+
if err != nil {
48+
logrus.Warnln("ERROR: get extra err:", err)
49+
}
50+
if !cfg.isvalid() {
51+
cfg = newconfig()
52+
}
53+
}
54+
return true
55+
}
56+
57+
func newextrasetstr(ptr *string) func(ctx *zero.Ctx) {
58+
return func(ctx *zero.Ctx) {
59+
args := strings.TrimSpace(ctx.State["args"].(string))
60+
if args == "" {
61+
ctx.SendChain(message.Text("ERROR: empty args"))
62+
return
63+
}
64+
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
65+
if !ok {
66+
ctx.SendChain(message.Text("ERROR: no such plugin"))
67+
return
68+
}
69+
*ptr = args
70+
err := c.SetExtra(&cfg)
71+
if err != nil {
72+
ctx.SendChain(message.Text("ERROR: set extra err: ", err))
73+
return
74+
}
75+
ctx.SendChain(message.Text("成功"))
76+
}
77+
}
78+
79+
func newextrasetbool(ptr *bool) func(ctx *zero.Ctx) {
80+
return func(ctx *zero.Ctx) {
81+
args := ctx.State["regex_matched"].([]string)
82+
isno := args[1] == "不"
83+
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
84+
if !ok {
85+
ctx.SendChain(message.Text("ERROR: no such plugin"))
86+
return
87+
}
88+
*ptr = isno
89+
err := c.SetExtra(&cfg)
90+
if err != nil {
91+
ctx.SendChain(message.Text("ERROR: set extra err: ", err))
92+
return
93+
}
94+
ctx.SendChain(message.Text("成功"))
95+
}
96+
}

0 commit comments

Comments
 (0)