fix(matrix): Limit msg history to stop responses to old msgs

Signed-off-by: Richard Palethorpe <io@richiejp.com>
This commit is contained in:
Richard Palethorpe
2025-05-13 22:11:10 +01:00
parent 2b4b2c513c
commit 8849a9ba1b

View File

@@ -126,7 +126,8 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
// Skip if message does not mention the bot
mentioned := false
if evt.Content.AsMessage().Mentions != nil {
msg := evt.Content.AsMessage()
if msg.Mentions != nil {
mentioned = slices.Contains(evt.Content.AsMessage().Mentions.UserIDs, m.client.UserID)
}
@@ -159,7 +160,7 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
agentOptions = append(agentOptions, types.WithConversationHistory(currentConv))
// Add room to metadata for tracking
metadata := map[string]interface{}{
metadata := map[string]any{
"room": evt.RoomID.String(),
}
agentOptions = append(agentOptions, types.WithMetadata(metadata))
@@ -214,7 +215,6 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
}
func (m *Matrix) Start(a *agent.Agent) {
// Create Matrix client
client, err := mautrix.NewClient(m.homeserverURL, id.UserID(m.userID), m.accessToken)
if err != nil {
xlog.Error(fmt.Sprintf("Error creating Matrix client: %v", err))
@@ -223,7 +223,6 @@ func (m *Matrix) Start(a *agent.Agent) {
xlog.Info("Matrix client created")
m.client = client
// Set up event handler
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
xlog.Info("Received message", evt.Content.AsMessage().Body)
@@ -245,7 +244,15 @@ func (m *Matrix) Start(a *agent.Agent) {
//m.handleRoomMessage(a, evt)
})
// Start syncing
// This prevents the agent from picking up a backlog of messages and swamping the chat with responses.
syncer.FilterJSON = &mautrix.Filter{
Room: mautrix.RoomFilter{
Timeline: mautrix.FilterPart{
Limit: 1,
},
},
}
go func() {
for {
select {
@@ -271,30 +278,35 @@ func MatrixConfigMeta() []config.Field {
{
Name: "homeserverURL",
Label: "Homeserver URL",
HelpText: "e.g. http://host.docker.internal:8008",
Type: config.FieldTypeText,
Required: true,
},
{
Name: "userID",
Label: "User ID",
HelpText: "e.g. @bot:host",
Type: config.FieldTypeText,
Required: true,
},
{
Name: "accessToken",
Label: "Access Token",
HelpText: "Token obtained from _matrix/client/v3/login",
Type: config.FieldTypeText,
Required: true,
},
{
Name: "roomID",
Label: "Room ID",
Type: config.FieldTypeText,
Name: "roomID",
Label: "Internal Room ID",
HelpText: "The autogenerated unique identifier for a room",
Type: config.FieldTypeText,
},
{
Name: "roomMode",
Label: "Room Mode",
Type: config.FieldTypeCheckbox,
Name: "roomMode",
Label: "Room Mode",
HelpText: "Respond to all messages in the specified room",
Type: config.FieldTypeCheckbox,
},
}
}