Skip to content

Commit 2741050

Browse files
authored
feat(aichat): 添加重置聊天 (#43)
1 parent 95cf57c commit 2741050

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

chat/init.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package chat 提供聊天记录管理和 AI 模型交互功能
12
package chat
23

34
import (
@@ -11,21 +12,27 @@ import (
1112
zero "github.com/wdvxdr1123/ZeroBot"
1213
)
1314

15+
// SystemPrompt 将 README.md 内容嵌入为默认系统提示词
16+
//
1417
//go:embed README.md
1518
var SystemPrompt string
1619

20+
// lst 全局聊天记录,每群/每用户独立保存最近 8 条
1721
var lst = chat.NewLog(8, "\n\n", "自己随机开启新话题", "【", "】", ">>")
1822

1923
func init() {
24+
// 注册 ZeroBot 消息钩子,记录所有非空文本
2025
zero.OnMessage(func(ctx *zero.Ctx) bool {
2126
txt := ctx.ExtractPlainText()
2227
ctx.State["__zbputil_chat_txt__"] = txt
2328
return txt != ""
2429
}).FirstPriority().SetBlock(false).Handle(func(ctx *zero.Ctx) {
30+
// 计算群组 ID(私聊时使用负的 UserID)
2531
gid := ctx.Event.GroupID
2632
if gid == 0 {
2733
gid = -ctx.Event.UserID
2834
}
35+
// 将用户消息追加到对应群组的聊天记录
2936
lst.Add(
3037
gid, ctx.Event.Sender.Name(),
3138
ctx.State["__zbputil_chat_txt__"].(string),
@@ -34,18 +41,26 @@ func init() {
3441
})
3542
}
3643

44+
// Reply 将 AI 回复追加到指定群组的聊天记录
3745
func Reply(grp int64, txt string) {
3846
lst.Add(grp, "", txt, true, false)
3947
}
4048

49+
// Ask 根据聊天记录构造可执行的 deepinfra 模型请求
4150
func Ask(p model.Protocol, grp int64, sysp string, isusersys bool) deepinfra.Model {
4251
return lst.Modelize(p, grp, sysp, isusersys)
4352
}
4453

54+
// AskCustom 通用日志转换函数,允许自定义每条记录的映射逻辑
4555
func AskCustom[T any](grp int64, f func(int, string) T) []T {
4656
return chat.Modelize(&lst, grp, f)
4757
}
4858

59+
// Sanitize 清洗 AI 返回文本:
60+
// 1. 去掉换行后内容
61+
// 2. 去掉发言前缀(如【name】或[name])
62+
// 3. 去掉重复 10 次以上的子串
63+
// 4. 去除首尾空白
4964
func Sanitize(msg string) string {
5065
msg, _, _ = strings.Cut(msg, "\n")
5166
msg = strings.TrimSpace(msg)
@@ -69,3 +84,8 @@ func Sanitize(msg string) string {
6984
}
7085
return strings.TrimSpace(msg)
7186
}
87+
88+
// Reset 清空全局聊天记录,重新开始
89+
func Reset() {
90+
lst.Reset()
91+
}

driver/funcall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (f *FCClient) handleRequest(req *zero.APIRequest) (r *zero.APIResponse, err
162162
r = &zero.APIResponse{ // 发送api调用响应
163163
Status: s,
164164
Data: data.Get("data"),
165-
Msg: m,
165+
Message: m,
166166
Wording: w,
167167
RetCode: c,
168168
Echo: req.Echo,

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
1313
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
1414
github.com/fumiama/cron v1.3.0
15-
github.com/fumiama/deepinfra v0.0.0-20250330125128-71ec2f7c085e
15+
github.com/fumiama/deepinfra v0.0.0-20250812083039-f1b27f21d8c9
1616
github.com/fumiama/go-base16384 v1.7.0
1717
github.com/gin-gonic/gin v1.8.2
1818
github.com/go-playground/assert/v2 v2.2.0
@@ -24,7 +24,7 @@ require (
2424
github.com/swaggo/gin-swagger v1.5.3
2525
github.com/swaggo/swag v1.8.10
2626
github.com/tidwall/gjson v1.18.0
27-
github.com/wdvxdr1123/ZeroBot v1.8.0
27+
github.com/wdvxdr1123/ZeroBot v1.8.2-0.20250804063440-ccc03e33ac20
2828
)
2929

3030
require (

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 h1:BBade+Jl
3939
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4/go.mod h1:H7chHJglrhPPzetLdzBleF8d22WYOv7UM/lEKYiwlKM=
4040
github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo=
4141
github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY=
42-
github.com/fumiama/deepinfra v0.0.0-20250330125128-71ec2f7c085e h1:L/Z5N6UfpuqNIiPUrjSzmrnWj3mjd3auwl/2ctpGXNY=
43-
github.com/fumiama/deepinfra v0.0.0-20250330125128-71ec2f7c085e/go.mod h1:wW05PQSn8mo1mZIoa6LBUE+3xIBjkoONvnfPTV5ZOhY=
42+
github.com/fumiama/deepinfra v0.0.0-20250812083039-f1b27f21d8c9 h1:X2h8RnCgC04LmwBoizYbFawXh/h6CouXmhYtaVuUn7k=
43+
github.com/fumiama/deepinfra v0.0.0-20250812083039-f1b27f21d8c9/go.mod h1:wW05PQSn8mo1mZIoa6LBUE+3xIBjkoONvnfPTV5ZOhY=
4444
github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA=
4545
github.com/fumiama/go-base16384 v1.7.0/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM=
4646
github.com/fumiama/go-registry v0.2.7 h1:tLEqgEpsiybQMqBv0dLHm5leia/z1DhajMupwnOHeNs=
@@ -180,8 +180,8 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6
180180
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
181181
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
182182
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
183-
github.com/wdvxdr1123/ZeroBot v1.8.0 h1:v7m+0kGtL6XQlUH9O/LzmOntDJs2clzVj93YsAWWMbk=
184-
github.com/wdvxdr1123/ZeroBot v1.8.0/go.mod h1:C86nQ0gIdAri4K2vg8IIQIslt08zzrKMcqYt8zhkx1M=
183+
github.com/wdvxdr1123/ZeroBot v1.8.2-0.20250804063440-ccc03e33ac20 h1:Yzd+cbiJQYtf6cZDP5ZB/LqjNWiV752+5P6Eua+wnic=
184+
github.com/wdvxdr1123/ZeroBot v1.8.2-0.20250804063440-ccc03e33ac20/go.mod h1:C86nQ0gIdAri4K2vg8IIQIslt08zzrKMcqYt8zhkx1M=
185185
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
186186
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
187187
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)