Skip to content

Commit 2922571

Browse files
authored
feat(chat): agent selective log & no empty str chk (#44)
* feat(chat): agent selective log & no empty str chk * chore: make lint happy
1 parent ff0902a commit 2922571

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

chat/agent.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,30 @@ func logev(ctx *zero.Ctx) {
210210
return
211211
}
212212
vevent.HookCtxCaller(ctx, vevent.NewAPICallerReturnHook(
213-
ctx, func(req zero.APIRequest, rsp zero.APIResponse, err error) {
213+
ctx, func(req zero.APIRequest, rsp zero.APIResponse, _ error) {
214214
gid := ctx.Event.GroupID
215215
if gid == 0 {
216216
gid = -ctx.Event.UserID
217217
}
218218
plen := countParamsLength(req.Params)
219219
if plen > 1024 { // skip too long req&resp
220-
logrus.Warnln("[chat] agent", gid, "skip too long requ:", &req)
220+
logrus.Debugln("[chat] agent", gid, "skip too long requ:", &req)
221+
return
222+
}
223+
if _, ok := ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_triggered__"]; ok {
224+
logrus.Debugln("[chat] agent", gid, "skip agent triggered requ:", &req)
225+
return
226+
}
227+
if req.Action != "send_private_msg" &&
228+
req.Action != "send_group_msg" &&
229+
req.Action != "delete_msg" {
230+
logrus.Debugln("[chat] agent", gid, "skip non-msg other triggered action:", req.Action)
221231
return
222232
}
223233
ag := AgentOf(ctx.Event.SelfID, "aichat")
224-
logrus.Infoln("[chat] agent", gid, "add requ:", &req)
234+
logrus.Debugln("[chat] agent others", gid, "add requ:", &req)
225235
ag.AddRequest(gid, &req)
226-
logrus.Infoln("[chat] agent", gid, "get resp:", &rsp)
236+
logrus.Debugln("[chat] agent others", gid, "add resp:", &rsp)
227237
ag.AddResponse(gid, &goba.APIResponse{
228238
Status: rsp.Status,
229239
Data: json.RawMessage(rsp.Data.Raw),

chat/cfg.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/wdvxdr1123/ZeroBot/message"
1515
)
1616

17+
// AC is the global agent configuration
1718
var AC AgentConfig
1819

1920
var (
@@ -40,6 +41,7 @@ func (mt ModelType) String() string {
4041
return apilist[mt]
4142
}
4243

44+
// Protocol creates a protocol instance based on the model type
4345
func (mt ModelType) Protocol(modn string, temp float32, topp float32, maxn uint) (mod model.Protocol, err error) {
4446
switch AC.Type {
4547
case 0:
@@ -87,6 +89,7 @@ func (mk ModelKey) String() string {
8789
return key[:2] + strings.Repeat("*", len(key)-4) + key[len(key)-2:]
8890
}
8991

92+
// AgentConfig holds the configuration for the chat agent
9093
type AgentConfig struct {
9194
ModelName string
9295
ImageModelName string
@@ -144,7 +147,7 @@ func (c *AgentConfig) isvalid() bool {
144147
return c.Key != ""
145148
}
146149

147-
// 获取全局模型参数:TopP和最大长度
150+
// MParams returns the global model parameters: TopP and MaxN
148151
func (c *AgentConfig) MParams() (topp float32, maxn uint) {
149152
// 处理TopP参数
150153
topp = c.TopP
@@ -161,6 +164,7 @@ func (c *AgentConfig) MParams() (topp float32, maxn uint) {
161164
return topp, maxn
162165
}
163166

167+
// EnsureConfig ensures the configuration is loaded and valid
164168
func EnsureConfig(ctx *zero.Ctx) bool {
165169
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
166170
if !ok {
@@ -179,13 +183,10 @@ func EnsureConfig(ctx *zero.Ctx) bool {
179183
return true
180184
}
181185

186+
// NewExtraSetStr creates a handler to set a string-based extra config value
182187
func NewExtraSetStr[T ~string](ptr *T) func(ctx *zero.Ctx) {
183188
return func(ctx *zero.Ctx) {
184189
args := strings.TrimSpace(ctx.State["args"].(string))
185-
if args == "" {
186-
ctx.SendChain(message.Text("ERROR: empty args"))
187-
return
188-
}
189190
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
190191
if !ok {
191192
ctx.SendChain(message.Text("ERROR: no such plugin"))
@@ -201,6 +202,7 @@ func NewExtraSetStr[T ~string](ptr *T) func(ctx *zero.Ctx) {
201202
}
202203
}
203204

205+
// NewExtraSetBool creates a handler to set a boolean-based extra config value
204206
func NewExtraSetBool[T ~bool](ptr *T) func(ctx *zero.Ctx) {
205207
return func(ctx *zero.Ctx) {
206208
args := ctx.State["regex_matched"].([]string)
@@ -220,6 +222,7 @@ func NewExtraSetBool[T ~bool](ptr *T) func(ctx *zero.Ctx) {
220222
}
221223
}
222224

225+
// NewExtraSetUint creates a handler to set a uint extra config value
223226
func NewExtraSetUint(ptr *uint) func(ctx *zero.Ctx) {
224227
return func(ctx *zero.Ctx) {
225228
args := strings.TrimSpace(ctx.State["args"].(string))
@@ -247,6 +250,7 @@ func NewExtraSetUint(ptr *uint) func(ctx *zero.Ctx) {
247250
}
248251
}
249252

253+
// NewExtraSetFloat32 creates a handler to set a float32 extra config value
250254
func NewExtraSetFloat32(ptr *float32) func(ctx *zero.Ctx) {
251255
return func(ctx *zero.Ctx) {
252256
args := strings.TrimSpace(ctx.State["args"].(string))
@@ -274,6 +278,7 @@ func NewExtraSetFloat32(ptr *float32) func(ctx *zero.Ctx) {
274278
}
275279
}
276280

281+
// NewExtraSetModelType creates a handler to set a ModelType extra config value
277282
func NewExtraSetModelType(ptr *ModelType) func(ctx *zero.Ctx) {
278283
return func(ctx *zero.Ctx) {
279284
args := strings.TrimSpace(ctx.State["args"].(string))

chat/storage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
zero "github.com/wdvxdr1123/ZeroBot"
66
)
77

8+
// Bitmap constants for storage
89
const (
910
BitmapRate = 0x0000ff
1011
BitmapTemp = 0x00ff00
@@ -13,17 +14,21 @@ const (
1314
BitmapNrat = 0x040000
1415
)
1516

17+
// Storage wraps ctxext.Storage for chat-specific storage operations
1618
type Storage ctxext.Storage
1719

20+
// NewStorage creates a new Storage instance
1821
func NewStorage(ctx *zero.Ctx, gid int64) (Storage, error) {
1922
s, err := ctxext.NewStorage(ctx, gid)
2023
return Storage(s), err
2124
}
2225

26+
// Rate returns the rate value from storage
2327
func (s Storage) Rate() uint8 {
2428
return uint8((ctxext.Storage)(s).Get(BitmapRate))
2529
}
2630

31+
// Temp returns the temperature value from storage
2732
func (s Storage) Temp() float32 {
2833
temp := int8((ctxext.Storage)(s).Get(BitmapTemp))
2934
// 处理温度参数
@@ -36,14 +41,17 @@ func (s Storage) Temp() float32 {
3641
return float32(temp) / 100
3742
}
3843

44+
// NoAgent returns whether the agent is disabled
3945
func (s Storage) NoAgent() bool {
4046
return (ctxext.Storage)(s).GetBool(BitmapNagt)
4147
}
4248

49+
// NoRecord returns whether recording is disabled
4350
func (s Storage) NoRecord() bool {
4451
return (ctxext.Storage)(s).GetBool(BitmapNrec)
4552
}
4653

54+
// NoReplyAt returns whether replying with @ is disabled
4755
func (s Storage) NoReplyAt() bool {
4856
return (ctxext.Storage)(s).GetBool(BitmapNrat)
4957
}

0 commit comments

Comments
 (0)