fix start eventid

This commit is contained in:
Erik Winter 2023-05-23 15:40:16 +02:00
parent aee2d483b3
commit 5bb6427b1d
4 changed files with 83 additions and 4 deletions

View File

@ -18,7 +18,7 @@ type Conversation struct {
Messages []Message Messages []Message
} }
func NewConversation(question string) *Conversation { func NewConversation(id id.EventID, question string) *Conversation {
return &Conversation{ return &Conversation{
Messages: []Message{ Messages: []Message{
{ {
@ -26,6 +26,7 @@ func NewConversation(question string) *Conversation {
Content: systemPrompt, Content: systemPrompt,
}, },
{ {
EventID: id,
Role: openai.ChatMessageRoleUser, Role: openai.ChatMessageRoleUser,
Content: question, Content: question,
}, },

77
bot/conversation_test.go Normal file
View File

@ -0,0 +1,77 @@
package bot_test
import (
"testing"
"ewintr.nl/matrix-bots/bot"
)
func TestNewConversation(t *testing.T) {
t.Parallel()
conv := bot.NewConversation("test", "question")
if conv == nil {
t.Error("NewConversation returned nil")
}
if len(conv.Messages) != 2 {
t.Error("NewConversation did not create 2 messages")
}
if conv.Messages[1].Content != "question" {
t.Error("NewConversation did not set question")
}
}
func TestConversation_Contains(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
conv *bot.Conversation
exp bool
}{
{
name: "empty",
conv: &bot.Conversation{},
exp: false,
},
{
name: "not contains",
conv: &bot.Conversation{
Messages: []bot.Message{
{
EventID: "other",
Content: "content",
},
},
},
},
{
name: "contains",
conv: &bot.Conversation{
Messages: []bot.Message{
{
EventID: "id",
Content: "content",
},
},
},
exp: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
if tc.conv.Contains("id") != tc.exp {
t.Errorf("expected %v, got %v", tc.exp, tc.conv.Contains("test"))
}
})
}
}
func TestConversation_Add(t *testing.T) {
conv := &bot.Conversation{}
conv.Add(bot.Message{
EventID: "id",
})
if !conv.Contains("id") {
t.Error("Add did not add message")
}
}

View File

@ -116,7 +116,7 @@ func (m *Matrix) InviteHandler() (event.Type, mautrix.EventHandler) {
} }
} }
func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) { func (m *Matrix) ResponseHandler() (event.Type, mautrix.EventHandler) {
return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
content := evt.Content.AsMessage() content := evt.Content.AsMessage()
eventID := evt.ID eventID := evt.ID
@ -137,6 +137,7 @@ func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
parentID = relatesTo.GetReplyTo() parentID = relatesTo.GetReplyTo()
} }
if parentID != "" { if parentID != "" {
m.client.Log.Info().Msg("parent found, looking for conversation")
conv = m.conversations.FindByEventID(parentID) conv = m.conversations.FindByEventID(parentID)
} }
if conv != nil { if conv != nil {
@ -148,7 +149,7 @@ func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
}) })
m.client.Log.Info().Msg("found parent, appending message to conversation") m.client.Log.Info().Msg("found parent, appending message to conversation")
} else { } else {
conv = NewConversation(content.Body) conv = NewConversation(eventID, content.Body)
m.conversations = append(m.conversations, conv) m.conversations = append(m.conversations, conv)
m.client.Log.Info().Msg("no parent found, starting new conversation") m.client.Log.Info().Msg("no parent found, starting new conversation")
} }

View File

@ -29,7 +29,7 @@ func main() {
go matrixClient.Run() go matrixClient.Run()
matrixClient.AddEventHandler(matrixClient.InviteHandler()) matrixClient.AddEventHandler(matrixClient.InviteHandler())
matrixClient.AddEventHandler(matrixClient.RespondHandler()) matrixClient.AddEventHandler(matrixClient.ResponseHandler())
done := make(chan os.Signal) done := make(chan os.Signal)
signal.Notify(done, os.Interrupt) signal.Notify(done, os.Interrupt)