resolveAgentChat finds the target chat from either an explicit ID or auto-detection via the agent's active chats.
( ctx context.Context, db database.Store, agentID uuid.UUID, workspaceOwnerID uuid.UUID, explicitChatID uuid.UUID, )
| 2731 | // resolveAgentChat finds the target chat from either an explicit ID |
| 2732 | // or auto-detection via the agent's active chats. |
| 2733 | func resolveAgentChat( |
| 2734 | ctx context.Context, |
| 2735 | db database.Store, |
| 2736 | agentID uuid.UUID, |
| 2737 | workspaceOwnerID uuid.UUID, |
| 2738 | explicitChatID uuid.UUID, |
| 2739 | ) (database.Chat, error) { |
| 2740 | if explicitChatID == uuid.Nil { |
| 2741 | chats, err := db.GetActiveChatsByAgentID(ctx, agentID) |
| 2742 | if err != nil { |
| 2743 | return database.Chat{}, xerrors.Errorf("list active chats: %w", err) |
| 2744 | } |
| 2745 | ownerChats := make([]database.Chat, 0, len(chats)) |
| 2746 | for _, chat := range chats { |
| 2747 | if chat.OwnerID != workspaceOwnerID { |
| 2748 | continue |
| 2749 | } |
| 2750 | ownerChats = append(ownerChats, chat) |
| 2751 | } |
| 2752 | return resolveDefaultAgentChat(ownerChats) |
| 2753 | } |
| 2754 | |
| 2755 | chat, err := db.GetChatByID(ctx, explicitChatID) |
| 2756 | if err != nil { |
| 2757 | if errors.Is(err, sql.ErrNoRows) { |
| 2758 | return database.Chat{}, errChatNotFound |
| 2759 | } |
| 2760 | return database.Chat{}, xerrors.Errorf("get chat by id: %w", err) |
| 2761 | } |
| 2762 | if !chat.AgentID.Valid || chat.AgentID.UUID != agentID { |
| 2763 | return database.Chat{}, errChatDoesNotBelongToAgent |
| 2764 | } |
| 2765 | if chat.OwnerID != workspaceOwnerID { |
| 2766 | return database.Chat{}, errChatDoesNotBelongToWorkspaceOwner |
| 2767 | } |
| 2768 | if !isActiveAgentChat(chat) { |
| 2769 | return database.Chat{}, errChatNotActive |
| 2770 | } |
| 2771 | return chat, nil |
| 2772 | } |
| 2773 | |
| 2774 | func isActiveAgentChat(chat database.Chat) bool { |
| 2775 | if chat.Archived { |
no test coverage detected