Skip to content

Commit bd170ab

Browse files
committed
feat(chat): agent configurable char at anytime
1 parent 85a8ee4 commit bd170ab

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

chat/agent.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"reflect"
1010
"strconv"
11+
"strings"
1112
"time"
1213

1314
"github.com/RomiChan/syncx"
@@ -24,43 +25,47 @@ import (
2425
"github.com/wdvxdr1123/ZeroBot/message"
2526
)
2627

27-
// AgentChar 将 char.yaml 内容嵌入为默认 agent 性格
28+
// agentChar 将 char.yaml 内容嵌入为默认 agent 性格
2829
//
2930
//go:embed char.yaml
30-
var AgentChar []byte
31+
var agentChar []byte
3132

32-
// IsAgentCharReady then logev works
33-
var IsAgentCharReady = false
33+
var agentcharcfg charcfg
3434

35-
var (
36-
ags = syncx.Map[int64, *goba.Agent]{}
37-
)
35+
// AgentCharConfig 可随时更改的 agent 性格配置
36+
var AgentCharConfig *goba.Config
37+
38+
var ags = syncx.Map[int64, *goba.Agent]{}
3839

3940
type charcfg struct {
4041
Sex string `yaml:"sex"`
4142
Char string `yaml:"char"`
4243
Default string `yaml:"default"`
4344
}
4445

46+
func init() {
47+
err := yaml.NewDecoder(bytes.NewReader(agentChar)).Decode(&agentcharcfg)
48+
if err != nil {
49+
panic(err)
50+
}
51+
AgentCharConfig = &goba.Config{
52+
Nickname: strings.Join(zero.BotConfig.NickName, "/"),
53+
Sex: agentcharcfg.Sex,
54+
Chars: agentcharcfg.Char,
55+
}
56+
}
57+
4558
// AgentOf id is self_id
4659
func AgentOf(id int64, service string) *goba.Agent {
4760
if ag, ok := ags.Load(id); ok {
4861
return ag
4962
}
50-
var cfg charcfg
51-
err := yaml.NewDecoder(bytes.NewReader(AgentChar)).Decode(&cfg)
52-
if err != nil {
53-
panic(err)
54-
}
63+
5564
mem, err := atomicgetmemstorage(service)
5665
if err != nil {
5766
panic(err)
5867
}
59-
ag := goba.NewAgent(
60-
id, 16, 8, time.Hour*24,
61-
zero.BotConfig.NickName[0],
62-
cfg.Sex, cfg.Char, cfg.Default, mem, true, false,
63-
)
68+
ag := goba.NewAgent(AgentCharConfig, 16, 8, time.Hour*24, agentcharcfg.Default, mem, true, false)
6469
ags.Store(id, &ag)
6570
return &ag
6671
}
@@ -245,9 +250,6 @@ func truncatecopy(params map[string]any) map[string]any {
245250
}
246251

247252
func logev(ctx *zero.Ctx) {
248-
if !IsAgentCharReady {
249-
return
250-
}
251253
vevent.HookCtxCaller(ctx, vevent.NewAPICallerReturnHook(
252254
ctx, func(req zero.APIRequest, rsp zero.APIResponse, _ error) {
253255
gid := ctx.Event.GroupID

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/fumiama/cron v1.3.0
1515
github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a
1616
github.com/fumiama/go-base16384 v1.7.1
17-
github.com/fumiama/go-onebot-agent v0.0.0-20260106161505-f7d25272034c
17+
github.com/fumiama/go-onebot-agent v0.0.0-20260117094018-cd93ccfd6e04
1818
github.com/gin-gonic/gin v1.11.0
1919
github.com/go-playground/assert/v2 v2.2.0
2020
github.com/go-playground/validator/v10 v10.29.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a h1:a0+2vaXajfxsN
4646
github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a/go.mod h1:uqsWK/GM9OvKV0pXZOQB63rWugBbiXInY8E1JoRKhkg=
4747
github.com/fumiama/go-base16384 v1.7.1 h1:1P1x6FWRvd7PtbH4idDAGWAjKKcVxggxlROYKRXbw58=
4848
github.com/fumiama/go-base16384 v1.7.1/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM=
49-
github.com/fumiama/go-onebot-agent v0.0.0-20260106161505-f7d25272034c h1:kCAFF41GkJklKvftchjuEpo4Mp/qUSiEGBeChs4QJdk=
50-
github.com/fumiama/go-onebot-agent v0.0.0-20260106161505-f7d25272034c/go.mod h1:rTrS23rvTYuZcSngENJTvcBFTz1nGsImSv+bW7yfhqs=
49+
github.com/fumiama/go-onebot-agent v0.0.0-20260117094018-cd93ccfd6e04 h1:TAvkyECK+lSVh+AnVSPNfCmeCiYiJCd+z9QQwfjHSuw=
50+
github.com/fumiama/go-onebot-agent v0.0.0-20260117094018-cd93ccfd6e04/go.mod h1:rTrS23rvTYuZcSngENJTvcBFTz1nGsImSv+bW7yfhqs=
5151
github.com/fumiama/go-registry v0.2.7 h1:tLEqgEpsiybQMqBv0dLHm5leia/z1DhajMupwnOHeNs=
5252
github.com/fumiama/go-registry v0.2.7/go.mod h1:m+wp5fF8dYgVoFkBPZl+vlK90loymaJE0JCtocVQLEs=
5353
github.com/fumiama/go-simple-protobuf v0.2.0 h1:ACyN1MAlu7pDR3EszWgzUeNP+IRsSHwH6V9JCJA5R5o=

0 commit comments

Comments
 (0)