Files
agents_and_robots/pkg/memory/window.go
T

44 lines
1.1 KiB
Go

package memory
import "github.com/enmanuel/agents/pkg/llm"
// Window is an immutable sliding window of conversation messages for a single room.
type Window struct {
messages []llm.Message
maxSize int
}
// NewWindow creates an empty window with the given capacity.
func NewWindow(maxSize int) Window {
return Window{maxSize: maxSize}
}
// Append returns a new Window with the message added, dropping the oldest
// messages if capacity is exceeded.
func (w Window) Append(msg llm.Message) Window {
msgs := make([]llm.Message, len(w.messages), len(w.messages)+1)
copy(msgs, w.messages)
msgs = append(msgs, msg)
if len(msgs) > w.maxSize {
msgs = msgs[len(msgs)-w.maxSize:]
}
return Window{messages: msgs, maxSize: w.maxSize}
}
// ToLLMMessages returns a copy of the window contents as []llm.Message.
func (w Window) ToLLMMessages() []llm.Message {
out := make([]llm.Message, len(w.messages))
copy(out, w.messages)
return out
}
// Len returns the number of messages in the window.
func (w Window) Len() int {
return len(w.messages)
}
// Clear returns an empty window with the same capacity.
func (w Window) Clear() Window {
return NewWindow(w.maxSize)
}