Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions chat/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,30 @@ func logev(ctx *zero.Ctx) {
return
}
vevent.HookCtxCaller(ctx, vevent.NewAPICallerReturnHook(
ctx, func(req zero.APIRequest, rsp zero.APIResponse, err error) {
ctx, func(req zero.APIRequest, rsp zero.APIResponse, _ error) {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
plen := countParamsLength(req.Params)
if plen > 1024 { // skip too long req&resp
logrus.Warnln("[chat] agent", gid, "skip too long requ:", &req)
logrus.Debugln("[chat] agent", gid, "skip too long requ:", &req)
return
}
if _, ok := ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_triggered__"]; ok {
logrus.Debugln("[chat] agent", gid, "skip agent triggered requ:", &req)
return
}
if req.Action != "send_private_msg" &&
req.Action != "send_group_msg" &&
req.Action != "delete_msg" {
logrus.Debugln("[chat] agent", gid, "skip non-msg other triggered action:", req.Action)
return
}
ag := AgentOf(ctx.Event.SelfID, "aichat")
logrus.Infoln("[chat] agent", gid, "add requ:", &req)
logrus.Debugln("[chat] agent others", gid, "add requ:", &req)
ag.AddRequest(gid, &req)
logrus.Infoln("[chat] agent", gid, "get resp:", &rsp)
logrus.Debugln("[chat] agent others", gid, "add resp:", &rsp)
ag.AddResponse(gid, &goba.APIResponse{
Status: rsp.Status,
Data: json.RawMessage(rsp.Data.Raw),
Expand Down
15 changes: 10 additions & 5 deletions chat/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
)

// AC is the global agent configuration
var AC AgentConfig

var (
Expand All @@ -40,6 +41,7 @@ func (mt ModelType) String() string {
return apilist[mt]
}

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

// AgentConfig holds the configuration for the chat agent
type AgentConfig struct {
ModelName string
ImageModelName string
Expand Down Expand Up @@ -144,7 +147,7 @@ func (c *AgentConfig) isvalid() bool {
return c.Key != ""
}

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

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

// NewExtraSetStr creates a handler to set a string-based extra config value
func NewExtraSetStr[T ~string](ptr *T) func(ctx *zero.Ctx) {
return func(ctx *zero.Ctx) {
args := strings.TrimSpace(ctx.State["args"].(string))
if args == "" {
ctx.SendChain(message.Text("ERROR: empty args"))
return
}
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok {
ctx.SendChain(message.Text("ERROR: no such plugin"))
Expand All @@ -201,6 +202,7 @@ func NewExtraSetStr[T ~string](ptr *T) func(ctx *zero.Ctx) {
}
}

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

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

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

// NewExtraSetModelType creates a handler to set a ModelType extra config value
func NewExtraSetModelType(ptr *ModelType) func(ctx *zero.Ctx) {
return func(ctx *zero.Ctx) {
args := strings.TrimSpace(ctx.State["args"].(string))
Expand Down
8 changes: 8 additions & 0 deletions chat/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
zero "github.com/wdvxdr1123/ZeroBot"
)

// Bitmap constants for storage
const (
BitmapRate = 0x0000ff
BitmapTemp = 0x00ff00
Expand All @@ -13,17 +14,21 @@ const (
BitmapNrat = 0x040000
)

// Storage wraps ctxext.Storage for chat-specific storage operations
type Storage ctxext.Storage

// NewStorage creates a new Storage instance
func NewStorage(ctx *zero.Ctx, gid int64) (Storage, error) {
s, err := ctxext.NewStorage(ctx, gid)
return Storage(s), err
}

// Rate returns the rate value from storage
func (s Storage) Rate() uint8 {
return uint8((ctxext.Storage)(s).Get(BitmapRate))
}

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

// NoAgent returns whether the agent is disabled
func (s Storage) NoAgent() bool {
return (ctxext.Storage)(s).GetBool(BitmapNagt)
}

// NoRecord returns whether recording is disabled
func (s Storage) NoRecord() bool {
return (ctxext.Storage)(s).GetBool(BitmapNrec)
}

// NoReplyAt returns whether replying with @ is disabled
func (s Storage) NoReplyAt() bool {
return (ctxext.Storage)(s).GetBool(BitmapNrat)
}
Loading