From 348633b5c6fb68931e8737dac9801e4d4a000dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 24 Sep 2025 21:41:53 +0800 Subject: [PATCH] feat(model): add item pre/post modelize hook --- chat/batch.go | 11 +++++++++-- chat/chat.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/chat/batch.go b/chat/batch.go index 0ec9227..79d0c3d 100644 --- a/chat/batch.go +++ b/chat/batch.go @@ -22,9 +22,16 @@ func (l *Log[T]) newbatch(sz int) *batch[T] { func (cl *batch[T]) String() string { sb := strings.Builder{} - for _, item := range cl.items { + for i := range cl.items { + pre, post := cl.lst.preM, cl.lst.postM + if pre != nil { + pre(&cl.items[i]) + } sb.WriteString(cl.lst.itemsep) - sb.WriteString(item.String()) + sb.WriteString(cl.items[i].String()) + if post != nil { + post(&cl.items[i]) + } } return sb.String()[len(cl.lst.itemsep):] } diff --git a/chat/chat.go b/chat/chat.go index 4924247..555f2c5 100644 --- a/chat/chat.go +++ b/chat/chat.go @@ -14,6 +14,7 @@ type Log[T fmt.Stringer] struct { itemsep string defaultprompt string m map[int64][]*batch[T] + preM, postM func(*T) } func NewLog[T fmt.Stringer](batchcap, itemscap int, itemsep, defaultprompt string) Log[T] { @@ -35,6 +36,18 @@ func NewLog[T fmt.Stringer](batchcap, itemscap int, itemsep, defaultprompt strin } } +// SetPreModelize will be called before calling item.String() +func (l *Log[T]) SetPreModelize(fn func(*T)) *Log[T] { + l.preM = fn + return l +} + +// SetPreModelize will be called after calling item.String() +func (l *Log[T]) SetPostModelize(fn func(*T)) *Log[T] { + l.postM = fn + return l +} + func (l *Log[T]) Add(grp int64, item T, isbot bool) { l.mu.Lock() defer l.mu.Unlock()