Make Conversation a pointer refernece on Message

Instead of a value, which lead some odd handling of conversation
references.

Also fixed some formatting and removed an unnecessary (and probably
broken) setting of ConversationID in a call to
`cmdutil.HandleConversationReply`
This commit is contained in:
Matt Low 2024-06-09 18:45:22 +00:00
parent a22119f738
commit 42c3297e54
6 changed files with 19 additions and 19 deletions

View File

@ -31,9 +31,8 @@ func ReplyCmd(ctx *lmcli.Context) *cobra.Command {
} }
cmdutil.HandleConversationReply(ctx, conversation, true, model.Message{ cmdutil.HandleConversationReply(ctx, conversation, true, model.Message{
ConversationID: conversation.ID, Role: model.MessageRoleUser,
Role: model.MessageRoleUser, Content: reply,
Content: reply,
}) })
return nil return nil
}, },

View File

@ -16,17 +16,17 @@ const (
) )
type Message struct { type Message struct {
ID uint `gorm:"primaryKey"` ID uint `gorm:"primaryKey"`
ConversationID uint `gorm:"index"` ConversationID *uint `gorm:"index"`
Conversation Conversation `gorm:"foreignKey:ConversationID"` Conversation *Conversation `gorm:"foreignKey:ConversationID"`
Content string Content string
Role MessageRole Role MessageRole
CreatedAt time.Time CreatedAt time.Time
ToolCalls ToolCalls // a json array of tool calls (from the model) ToolCalls ToolCalls // a json array of tool calls (from the model)
ToolResults ToolResults // a json array of tool results ToolResults ToolResults // a json array of tool results
ParentID *uint ParentID *uint
Parent *Message `gorm:"foreignKey:ParentID"` Parent *Message `gorm:"foreignKey:ParentID"`
Replies []Message `gorm:"foreignKey:ParentID"` Replies []Message `gorm:"foreignKey:ParentID"`
SelectedReplyID *uint SelectedReplyID *uint
SelectedReply *Message `gorm:"foreignKey:SelectedReplyID"` SelectedReply *Message `gorm:"foreignKey:SelectedReplyID"`
@ -37,7 +37,7 @@ type Conversation struct {
ShortName sql.NullString ShortName sql.NullString
Title string Title string
SelectedRootID *uint SelectedRootID *uint
SelectedRoot *Message `gorm:"foreignKey:SelectedRootID"` SelectedRoot *Message `gorm:"foreignKey:SelectedRootID"`
} }
type RequestParameters struct { type RequestParameters struct {

View File

@ -159,7 +159,7 @@ func (s *SQLStore) StartConversation(messages ...model.Message) (*model.Conversa
} }
// Create first message // Create first message
messages[0].Conversation = *conversation messages[0].Conversation = conversation
err = s.db.Create(&messages[0]).Error err = s.db.Create(&messages[0]).Error
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -201,7 +201,7 @@ func (s *SQLStore) CloneConversation(toClone model.Conversation) (*model.Convers
for _, root := range rootMessages { for _, root := range rootMessages {
messageCnt++ messageCnt++
newRoot := root newRoot := root
newRoot.ConversationID = clone.ID newRoot.ConversationID = &clone.ID
cloned, count, err := s.CloneBranch(newRoot) cloned, count, err := s.CloneBranch(newRoot)
if err != nil { if err != nil {
@ -232,9 +232,10 @@ func (s *SQLStore) Reply(to *model.Message, messages ...model.Message) ([]model.
err := s.db.Transaction(func(tx *gorm.DB) error { err := s.db.Transaction(func(tx *gorm.DB) error {
currentParent := to currentParent := to
for i := range messages { for i := range messages {
parent := currentParent
message := messages[i] message := messages[i]
message.Parent = currentParent message.Parent = parent
message.Conversation = currentParent.Conversation message.Conversation = parent.Conversation
message.ID = 0 message.ID = 0
message.CreatedAt = time.Time{} message.CreatedAt = time.Time{}
@ -243,9 +244,9 @@ func (s *SQLStore) Reply(to *model.Message, messages ...model.Message) ([]model.
} }
// update parent selected reply // update parent selected reply
currentParent.Replies = append(currentParent.Replies, message) parent.Replies = append(parent.Replies, message)
currentParent.SelectedReply = &message parent.SelectedReply = &message
if err := tx.Model(currentParent).Update("selected_reply_id", message.ID).Error; err != nil { if err := tx.Model(parent).Update("selected_reply_id", message.ID).Error; err != nil {
return err return err
} }

View File

@ -110,7 +110,7 @@ func (m *Model) cloneMessage(message models.Message, selected bool) tea.Cmd {
if selected { if selected {
if msg.Parent == nil { if msg.Parent == nil {
msg.Conversation.SelectedRoot = msg msg.Conversation.SelectedRoot = msg
err = m.Shared.Ctx.Store.UpdateConversation(&msg.Conversation) err = m.Shared.Ctx.Store.UpdateConversation(msg.Conversation)
} else { } else {
msg.Parent.SelectedReply = msg msg.Parent.SelectedReply = msg
err = m.Shared.Ctx.Store.UpdateMessage(msg.Parent) err = m.Shared.Ctx.Store.UpdateMessage(msg.Parent)

View File

@ -173,7 +173,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.updateContent() m.updateContent()
case msgMessageCloned: case msgMessageCloned:
if msg.Parent == nil { if msg.Parent == nil {
m.conversation = &msg.Conversation m.conversation = msg.Conversation
m.rootMessages = append(m.rootMessages, *msg) m.rootMessages = append(m.rootMessages, *msg)
} }
cmds = append(cmds, m.loadConversationMessages()) cmds = append(cmds, m.loadConversationMessages())

View File

@ -155,7 +155,7 @@ func (m *Model) loadConversations() tea.Cmd {
loaded := make([]loadedConversation, len(messages)) loaded := make([]loadedConversation, len(messages))
for i, m := range messages { for i, m := range messages {
loaded[i].lastReply = m loaded[i].lastReply = m
loaded[i].conv = m.Conversation loaded[i].conv = *m.Conversation
} }
return msgConversationsLoaded(loaded) return msgConversationsLoaded(loaded)