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:
parent
a22119f738
commit
42c3297e54
@ -31,7 +31,6 @@ 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,
|
||||||
})
|
})
|
||||||
|
@ -17,8 +17,8 @@ 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
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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())
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user