| 2704 | } |
| 2705 | |
| 2706 | func resolveDefaultAgentChat(chats []database.Chat) (database.Chat, error) { |
| 2707 | switch len(chats) { |
| 2708 | case 0: |
| 2709 | return database.Chat{}, errNoActiveChats |
| 2710 | case 1: |
| 2711 | return chats[0], nil |
| 2712 | } |
| 2713 | |
| 2714 | var rootChat *database.Chat |
| 2715 | for i := range chats { |
| 2716 | chat := &chats[i] |
| 2717 | if chat.ParentChatID.Valid { |
| 2718 | continue |
| 2719 | } |
| 2720 | if rootChat != nil { |
| 2721 | return database.Chat{}, &multipleActiveChatsError{count: len(chats)} |
| 2722 | } |
| 2723 | rootChat = chat |
| 2724 | } |
| 2725 | if rootChat != nil { |
| 2726 | return *rootChat, nil |
| 2727 | } |
| 2728 | return database.Chat{}, &multipleActiveChatsError{count: len(chats)} |
| 2729 | } |
| 2730 | |
| 2731 | // resolveAgentChat finds the target chat from either an explicit ID |
| 2732 | // or auto-detection via the agent's active chats. |