| 750 | } |
| 751 | |
| 752 | deleteAndRelinkMessage(aiChatId: string, messageId: string): void { |
| 753 | const db = this.getDb() |
| 754 | const target = this.getMessageRow(messageId) |
| 755 | if (!target || target.aiChatId !== aiChatId) { |
| 756 | throw new Error('Message not found in AI chat') |
| 757 | } |
| 758 | |
| 759 | db.prepare('UPDATE ai_message SET parent_id = ? WHERE parent_id = ? AND ai_chat_id = ?').run( |
| 760 | target.parentId, |
| 761 | messageId, |
| 762 | aiChatId |
| 763 | ) |
| 764 | |
| 765 | const conv = this.getAIChat(aiChatId) |
| 766 | if (conv?.activeMessageId === messageId) { |
| 767 | const now = Math.floor(Date.now() / 1000) |
| 768 | db.prepare('UPDATE ai_chat SET active_message_id = ?, updated_at = ? WHERE id = ?').run( |
| 769 | target.parentId, |
| 770 | now, |
| 771 | aiChatId |
| 772 | ) |
| 773 | } |
| 774 | |
| 775 | db.prepare('DELETE FROM ai_message WHERE id = ?').run(messageId) |
| 776 | } |
| 777 | |
| 778 | insertMessageAfter( |
| 779 | aiChatId: string, |