fix(matrix): Limit msg history to stop responses to old msgs
Signed-off-by: Richard Palethorpe <io@richiejp.com>
This commit is contained in:
@@ -126,7 +126,8 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
|
|||||||
|
|
||||||
// Skip if message does not mention the bot
|
// Skip if message does not mention the bot
|
||||||
mentioned := false
|
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)
|
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))
|
agentOptions = append(agentOptions, types.WithConversationHistory(currentConv))
|
||||||
|
|
||||||
// Add room to metadata for tracking
|
// Add room to metadata for tracking
|
||||||
metadata := map[string]interface{}{
|
metadata := map[string]any{
|
||||||
"room": evt.RoomID.String(),
|
"room": evt.RoomID.String(),
|
||||||
}
|
}
|
||||||
agentOptions = append(agentOptions, types.WithMetadata(metadata))
|
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) {
|
func (m *Matrix) Start(a *agent.Agent) {
|
||||||
// Create Matrix client
|
|
||||||
client, err := mautrix.NewClient(m.homeserverURL, id.UserID(m.userID), m.accessToken)
|
client, err := mautrix.NewClient(m.homeserverURL, id.UserID(m.userID), m.accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error(fmt.Sprintf("Error creating Matrix client: %v", err))
|
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")
|
xlog.Info("Matrix client created")
|
||||||
m.client = client
|
m.client = client
|
||||||
|
|
||||||
// Set up event handler
|
|
||||||
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
||||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||||
xlog.Info("Received message", evt.Content.AsMessage().Body)
|
xlog.Info("Received message", evt.Content.AsMessage().Body)
|
||||||
@@ -245,7 +244,15 @@ func (m *Matrix) Start(a *agent.Agent) {
|
|||||||
//m.handleRoomMessage(a, evt)
|
//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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -271,29 +278,34 @@ func MatrixConfigMeta() []config.Field {
|
|||||||
{
|
{
|
||||||
Name: "homeserverURL",
|
Name: "homeserverURL",
|
||||||
Label: "Homeserver URL",
|
Label: "Homeserver URL",
|
||||||
|
HelpText: "e.g. http://host.docker.internal:8008",
|
||||||
Type: config.FieldTypeText,
|
Type: config.FieldTypeText,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "userID",
|
Name: "userID",
|
||||||
Label: "User ID",
|
Label: "User ID",
|
||||||
|
HelpText: "e.g. @bot:host",
|
||||||
Type: config.FieldTypeText,
|
Type: config.FieldTypeText,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "accessToken",
|
Name: "accessToken",
|
||||||
Label: "Access Token",
|
Label: "Access Token",
|
||||||
|
HelpText: "Token obtained from _matrix/client/v3/login",
|
||||||
Type: config.FieldTypeText,
|
Type: config.FieldTypeText,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "roomID",
|
Name: "roomID",
|
||||||
Label: "Room ID",
|
Label: "Internal Room ID",
|
||||||
|
HelpText: "The autogenerated unique identifier for a room",
|
||||||
Type: config.FieldTypeText,
|
Type: config.FieldTypeText,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "roomMode",
|
Name: "roomMode",
|
||||||
Label: "Room Mode",
|
Label: "Room Mode",
|
||||||
|
HelpText: "Respond to all messages in the specified room",
|
||||||
Type: config.FieldTypeCheckbox,
|
Type: config.FieldTypeCheckbox,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user