@@ -673,6 +673,18 @@ func (m *Chat) RemoveMessage(id string) {
673673 return
674674 }
675675
676+ // Clean up nested tool IDs that point to this item's index before
677+ // removing it, so they don't end up pointing to a wrong item after
678+ // the rebuild.
679+ if item, ok := m.list.ItemAt(idx).(chat.MessageItem); ok {
680+ if container, ok := item.(chat.NestedToolContainer); ok {
681+ for _, nested := range container.NestedTools() {
682+ delete(m.idInxMap, nested.ID())
683+ delete(m.pausedAnimations, nested.ID())
684+ }
685+ }
686+ }
687+
676688 // Remove from list
677689 m.list.RemoveItem(idx)
678690
@@ -683,6 +695,12 @@ func (m *Chat) RemoveMessage(id string) {
683695 for i := idx; i < m.list.Len(); i++ {
684696 if item, ok := m.list.ItemAt(i).(chat.MessageItem); ok {
685697 m.idInxMap[item.ID()] = i
698+ // Also update nested tool IDs for containers.
699+ if container, ok := item.(chat.NestedToolContainer); ok {
700+ for _, nested := range container.NestedTools() {
701+ m.idInxMap[nested.ID()] = i
702+ }
703+ }
686704 }
687705 }
688706