99 "reflect"
1010 "strconv"
1111 "strings"
12- "sync"
1312 "time"
1413
1514 "github.com/RomiChan/syncx"
@@ -21,11 +20,17 @@ import (
2120 "gopkg.in/yaml.v3"
2221
2322 "github.com/FloatTech/floatbox/binary"
23+ "github.com/FloatTech/zbputils/control"
2424 "github.com/FloatTech/zbputils/vevent"
2525 zero "github.com/wdvxdr1123/ZeroBot"
2626 "github.com/wdvxdr1123/ZeroBot/message"
2727)
2828
29+ const (
30+ StateKeyAgentHooked = zero .StateKeyPrefixKeep + "_chat_ag_hooked__"
31+ StateKeyAgentTriggered = zero .StateKeyPrefixKeep + "_chat_ag_triggered__"
32+ )
33+
2934// agentchar 将 char.yaml 内容嵌入为默认 agent 性格
3035//
3136//go:embed char.yaml
@@ -255,8 +260,6 @@ func truncatecopy(params map[string]any) map[string]any {
255260 return cpp
256261}
257262
258- var logevmu sync.Mutex
259-
260263func logev (ctx * zero.Ctx ) {
261264 gid := ctx .Event .GroupID
262265 if gid == 0 {
@@ -270,27 +273,23 @@ func logev(ctx *zero.Ctx) {
270273 logrus .Debugln ("[chat] agent" , gid , "add ev:" , binary .BytesToString (data ))
271274 AgentOf (ctx .Event .SelfID , "aichat" ).AddEvent (gid , ev )
272275
273- logevmu .Lock ()
274- if _ , ok := ctx .State [zero .StateKeyPrefixKeep + "_chat_ag_hooked__" ]; ok {
275- logevmu .Unlock ()
276+ mp := ctx .State [control .StateKeySyncxState ].(* syncx.Map [string , any ])
277+
278+ _ , hasvalue := mp .LoadOrStore (StateKeyAgentHooked , struct {}{})
279+ if hasvalue {
276280 return
277281 }
278- ctx .State [zero .StateKeyPrefixKeep + "_chat_ag_hooked__" ] = struct {}{}
279- logevmu .Unlock ()
280282
281283 vevent .HookCtxCaller (ctx , vevent .NewAPICallerReturnHook (
282284 ctx , func (req zero.APIRequest , rsp zero.APIResponse , _ error ) {
283285 gid := ctx .Event .GroupID
284286 if gid == 0 {
285287 gid = - ctx .Event .UserID
286288 }
287- logevmu .Lock ()
288- if _ , ok := ctx .State [zero .StateKeyPrefixKeep + "_chat_ag_triggered__" ]; ok {
289- logevmu .Unlock ()
289+ if _ , ok := mp .Load (StateKeyAgentTriggered ); ok {
290290 logrus .Debugln ("[chat] agent" , gid , "skip agent triggered requ:" , & req )
291291 return
292292 }
293- logevmu .Unlock ()
294293 if req .Action != "send_private_msg" &&
295294 req .Action != "send_group_msg" &&
296295 req .Action != "delete_msg" {
0 commit comments