Skip to content

Commit 47e2bd1

Browse files
committed
feat(chat): agent cut too long req msgs
1 parent 6d80da1 commit 47e2bd1

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

chat/agent.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,33 +195,43 @@ func togobaev(ev *zero.Event) *goba.Event {
195195
}
196196
}
197197

198-
// countParamsLength 统计 params 中所有叶子节点字符串的长度之和(非递归实现)
199-
func countParamsLength(params map[string]any) int {
200-
total := 0
201-
// 使用栈存储待处理的 map
198+
func truncate(params map[string]any) {
202199
stack := []map[string]any{params}
203-
204200
for len(stack) > 0 {
205201
// 弹出栈顶元素
206202
current := stack[len(stack)-1]
207203
stack = stack[:len(stack)-1]
208204

209205
// 遍历当前 map 的所有值
210-
for _, v := range current {
206+
for k, v := range current {
211207
switch val := v.(type) {
212208
case string:
213-
// 叶子节点,累加长度
214-
total += len(val)
209+
if len(val) > 512 {
210+
current[k] = val[:200] + " ... " + val[len(val)-200:]
211+
}
212+
case int64, int:
215213
case map[string]any:
216214
// 非叶子节点,压入栈中待处理
217215
stack = append(stack, val)
216+
case message.Message:
217+
for _, m := range val {
218+
for k, v := range m.Data {
219+
if len(v) > 512 {
220+
m.Data[k] = v[:200] + " ... " + v[len(v)-200:]
221+
}
222+
}
223+
}
224+
case message.Segment:
225+
for k, v := range val.Data {
226+
if len(v) > 512 {
227+
val.Data[k] = v[:200] + " ... " + v[len(v)-200:]
228+
}
229+
}
218230
default:
219231
logrus.Warnln("[chat] agent unknown params typ:", reflect.TypeOf(v))
220232
}
221233
}
222234
}
223-
224-
return total
225235
}
226236

227237
func logev(ctx *zero.Ctx) {
@@ -234,11 +244,6 @@ func logev(ctx *zero.Ctx) {
234244
if gid == 0 {
235245
gid = -ctx.Event.UserID
236246
}
237-
plen := countParamsLength(req.Params)
238-
if plen > 1024 { // skip too long req&resp
239-
logrus.Infoln("[chat] agent", gid, "skip too long", plen, "bytes requ.")
240-
return
241-
}
242247
if _, ok := ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_triggered__"]; ok {
243248
logrus.Debugln("[chat] agent", gid, "skip agent triggered requ:", &req)
244249
return
@@ -249,6 +254,7 @@ func logev(ctx *zero.Ctx) {
249254
logrus.Debugln("[chat] agent", gid, "skip non-msg other triggered action:", req.Action)
250255
return
251256
}
257+
truncate(req.Params)
252258
ag := AgentOf(ctx.Event.SelfID, "aichat")
253259
logrus.Debugln("[chat] agent others", gid, "add requ:", &req)
254260
ag.AddRequest(gid, &req)

0 commit comments

Comments
 (0)