From a77ca7f6912a5f30b56b614228c73232351a98c3 Mon Sep 17 00:00:00 2001 From: ahmednahima0-beep Date: Sat, 9 May 2026 04:36:39 +0700 Subject: [PATCH] fix: remove dead ArtistAgent code and GET /api/agents endpoint (#1737) * fix: remove dead ArtistAgent code and GET /api/agents endpoint Co-authored-by: Cursor * updating --------- Co-authored-by: Cursor --- app/api/agents/route.ts | 29 ---------- .../Modals/DeleteConfirmationModal.tsx | 18 +++---- components/Sidebar/Modals/RenameModal.tsx | 3 +- components/Sidebar/RecentChats/ChatItem.tsx | 8 ++- .../Sidebar/RecentChats/RecentChatList.tsx | 10 ++-- .../Sidebar/RecentChats/useRecentChats.ts | 19 +++---- hooks/useArtistAgents.ts | 31 ----------- hooks/useConversations.tsx | 23 +++----- hooks/useCreateChat.tsx | 7 ++- hooks/useRenameModal.ts | 15 ++---- lib/chat/getChatDisplayInfo.ts | 15 ++---- lib/chat/getChatRoomId.ts | 6 +-- lib/supabase/getArtistAgents.ts | 53 ------------------- 13 files changed, 35 insertions(+), 202 deletions(-) delete mode 100644 app/api/agents/route.ts delete mode 100644 hooks/useArtistAgents.ts delete mode 100644 lib/supabase/getArtistAgents.ts diff --git a/app/api/agents/route.ts b/app/api/agents/route.ts deleted file mode 100644 index 23a666ec0..000000000 --- a/app/api/agents/route.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { getArtistAgents } from "@/lib/supabase/getArtistAgents"; - -export async function GET(request: NextRequest) { - const { searchParams } = new URL(request.url); - const socialIds = searchParams.getAll("socialId"); - - if (!socialIds.length) { - return NextResponse.json( - { error: "At least one Social ID is required" }, - { status: 400 }, - ); - } - - try { - const agents = await getArtistAgents(socialIds); - return NextResponse.json(agents); - } catch (error) { - console.error("Error fetching segments:", error); - return NextResponse.json( - { error: "Failed to fetch segments" }, - { status: 500 }, - ); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/components/Sidebar/Modals/DeleteConfirmationModal.tsx b/components/Sidebar/Modals/DeleteConfirmationModal.tsx index f4499fdc7..bed16e594 100644 --- a/components/Sidebar/Modals/DeleteConfirmationModal.tsx +++ b/components/Sidebar/Modals/DeleteConfirmationModal.tsx @@ -1,22 +1,16 @@ import { useState, useEffect } from "react"; import Modal from "@/components/Modal"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { useDeleteChat } from "@/hooks/useDeleteChat"; interface DeleteConfirmationModalProps { isOpen: boolean; onClose: () => void; - chatRoom: Conversation | ArtistAgent | null; - chatRooms?: Array; + chatRoom: Conversation | null; + chatRooms?: Conversation[]; onDelete: () => void; } -// Helper functions for type handling and data extraction -const isChatRoom = (item: Conversation | ArtistAgent): item is Conversation => 'id' in item; -const getChatName = (item: Conversation | ArtistAgent): string => isChatRoom(item) ? item.topic : item.type; -const getChatId = (item: Conversation | ArtistAgent): string => isChatRoom(item) ? item.id : item.agentId; - const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelete }: DeleteConfirmationModalProps) => { const [error, setError] = useState(""); const [deletingProgress, setDeletingProgress] = useState<{ current: number; total: number } | null>(null); @@ -41,7 +35,7 @@ const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelet const chatCount = chatsToDelete.length; const isSingleDelete = chatCount === 1; - const chatName = isSingleDelete ? getChatName(chatsToDelete[0]) : `${chatCount} chats`; + const chatName = isSingleDelete ? (chatsToDelete[0].topic || "Chat") : `${chatCount} chats`; const buttonText = isDeleting ? (deletingProgress ? `Deleting ${deletingProgress.current}/${deletingProgress.total}...` : 'Deleting...') : 'Delete'; @@ -58,10 +52,10 @@ const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelet setDeletingProgress({ current: i + 1, total: chatCount }); try { - await deleteChat(getChatId(chat)); + await deleteChat(chat.id); } catch (chatError) { - console.error(`Error deleting chat ${getChatName(chat)}:`, chatError); - failedChats.push(getChatName(chat)); + console.error(`Error deleting chat ${chat.topic || "Chat"}:`, chatError); + failedChats.push(chat.topic || "Chat"); } } diff --git a/components/Sidebar/Modals/RenameModal.tsx b/components/Sidebar/Modals/RenameModal.tsx index 0e1394b8b..841f871e3 100644 --- a/components/Sidebar/Modals/RenameModal.tsx +++ b/components/Sidebar/Modals/RenameModal.tsx @@ -1,12 +1,11 @@ import Modal from "@/components/Modal"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { useRenameModal } from "@/hooks/useRenameModal"; interface RenameModalProps { isOpen: boolean; onClose: () => void; - chatRoom: Conversation | ArtistAgent | null; + chatRoom: Conversation | null; } const RenameModal = ({ diff --git a/components/Sidebar/RecentChats/ChatItem.tsx b/components/Sidebar/RecentChats/ChatItem.tsx index 3f9c4f0d8..fc592a095 100644 --- a/components/Sidebar/RecentChats/ChatItem.tsx +++ b/components/Sidebar/RecentChats/ChatItem.tsx @@ -1,13 +1,12 @@ import { useState, useEffect, type RefObject, type MouseEvent } from "react"; import { MoreHorizontal, Pencil, Trash2, Check } from "lucide-react"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { cn } from "@/lib/utils"; import useCreateChat from "@/hooks/useCreateChat"; import { getChatDisplayInfo } from "@/lib/chat/getChatDisplayInfo"; type ChatItemProps = { - chatRoom: Conversation | ArtistAgent; + chatRoom: Conversation; isMobile: boolean; isHovered: boolean; isMenuOpen: boolean; @@ -54,9 +53,8 @@ const ChatItem = ({ const showOptions = isMobile || isHovered || isActive; const isOptimisticChatItem = - "id" in chatRoom && - Array.isArray((chatRoom as Conversation).memories) && - (chatRoom as Conversation).memories.some((memory) => { + Array.isArray(chatRoom.memories) && + chatRoom.memories.some((memory) => { const content = memory?.content as unknown; if (!content || typeof content !== "object") { return false; diff --git a/components/Sidebar/RecentChats/RecentChatList.tsx b/components/Sidebar/RecentChats/RecentChatList.tsx index 60ea7df46..c2dd8b833 100644 --- a/components/Sidebar/RecentChats/RecentChatList.tsx +++ b/components/Sidebar/RecentChats/RecentChatList.tsx @@ -1,12 +1,11 @@ import { AnimatePresence, motion } from "framer-motion"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import ChatItem from "./ChatItem"; import type { MutableRefObject } from "react"; import { getChatRoomId } from "@/lib/chat/getChatRoomId"; interface RecentChatListProps { - conversations: Array; + conversations: Conversation[]; isMobile: boolean; hoveredChatId: string | null; openMenuId: string | null; @@ -17,13 +16,10 @@ interface RecentChatListProps { menuRef: MutableRefObject; buttonRefs: MutableRefObject>; setHoveredChatId: (chatId: string | null) => void; - handleChatClick: (chatRoom: Conversation | ArtistAgent) => void; + handleChatClick: (chatRoom: Conversation) => void; handleChatSelection: (chatId: string, isShiftKey: boolean) => void; toggleMenu: (roomId: string) => void; - openModal: ( - type: "rename" | "delete", - chatRoom: Conversation | ArtistAgent - ) => void; + openModal: (type: "rename" | "delete", chatRoom: Conversation) => void; } const RecentChatList = ({ diff --git a/components/Sidebar/RecentChats/useRecentChats.ts b/components/Sidebar/RecentChats/useRecentChats.ts index d27d570c5..81e64eeb3 100644 --- a/components/Sidebar/RecentChats/useRecentChats.ts +++ b/components/Sidebar/RecentChats/useRecentChats.ts @@ -5,7 +5,6 @@ import { useConversationsProvider } from "@/providers/ConversationsProvider"; import { useMobileDetection } from "@/hooks/useMobileDetection"; import { useOutsideClick } from "@/hooks/useOutsideClick"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { getChatRoomId } from "@/lib/chat/getChatRoomId"; interface UseRecentChatsParams { @@ -22,11 +21,7 @@ export const useRecentChats = ({ toggleModal }: UseRecentChatsParams) => { const [hoveredChatId, setHoveredChatId] = useState(null); const [openMenuId, setOpenMenuId] = useState(null); const [activeChatId, setActiveChatId] = useState( - typeof params?.roomId === "string" - ? params.roomId - : typeof params?.agentId === "string" - ? params.agentId - : null + typeof params?.roomId === "string" ? params.roomId : null ); useEffect(() => { @@ -39,9 +34,7 @@ export const useRecentChats = ({ toggleModal }: UseRecentChatsParams) => { } const roomId = typeof params?.roomId === "string" ? params.roomId : null; - const agentId = - typeof params?.agentId === "string" ? params.agentId : null; - setActiveChatId(roomId || agentId || null); + setActiveChatId(roomId || null); }; updateActiveChatId(); @@ -49,8 +42,8 @@ export const useRecentChats = ({ toggleModal }: UseRecentChatsParams) => { const [modalState, setModalState] = useState<{ type: "rename" | "delete" | null; - chatRoom: Conversation | ArtistAgent | null; - chatRooms?: Array; + chatRoom: Conversation | null; + chatRooms?: Conversation[]; }>({ type: null, chatRoom: null }); const [selectedChatIds, setSelectedChatIds] = useState>( @@ -93,7 +86,7 @@ export const useRecentChats = ({ toggleModal }: UseRecentChatsParams) => { const openModal = ( type: "rename" | "delete", - chatRoom: Conversation | ArtistAgent + chatRoom: Conversation ) => { setModalState({ type, chatRoom }); setOpenMenuId(null); @@ -164,7 +157,7 @@ export const useRecentChats = ({ toggleModal }: UseRecentChatsParams) => { const clearSelection = () => setSelectedChatIds(new Set()); - const handleChatClick = (chatRoom: Conversation | ArtistAgent) => { + const handleChatClick = (chatRoom: Conversation) => { handleClick(chatRoom, toggleModal); }; diff --git a/hooks/useArtistAgents.ts b/hooks/useArtistAgents.ts deleted file mode 100644 index f268e653b..000000000 --- a/hooks/useArtistAgents.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ArtistAgent } from "@/lib/supabase/getArtistAgents"; -import { useArtistProvider } from "@/providers/ArtistProvider"; -import { SOCIAL } from "@/types/Agent"; -import { useEffect, useState } from "react"; - -const useArtistAgents = () => { - const [agents, setAgents] = useState([]); - const { selectedArtist } = useArtistProvider(); - - useEffect(() => { - const getAgents = async () => { - if (selectedArtist) { - const socialIds = selectedArtist.account_socials?.map( - (social: SOCIAL) => social.id - ); - const queryString = socialIds?.map((id) => `socialId=${id}`).join("&"); - const response = await fetch(`/api/agents?${queryString}`); - const data = await response.json(); - if (data.error) return; - setAgents(data); - } - }; - getAgents(); - }, [selectedArtist]); - - return { - agents, - }; -}; - -export default useArtistAgents; diff --git a/hooks/useConversations.tsx b/hooks/useConversations.tsx index b71260246..641432338 100644 --- a/hooks/useConversations.tsx +++ b/hooks/useConversations.tsx @@ -4,14 +4,11 @@ import { useUserProvider } from "@/providers/UserProvder"; import { useArtistProvider } from "@/providers/ArtistProvider"; import getConversations from "@/lib/getConversations"; import { Conversation } from "@/types/Chat"; -import useArtistAgents from "./useArtistAgents"; -import { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { usePrivy } from "@privy-io/react-auth"; const useConversations = () => { const { userData } = useUserProvider(); const { selectedArtist, artists } = useArtistProvider(); - const { agents } = useArtistAgents(); const queryClient = useQueryClient(); const { getAccessToken, authenticated } = usePrivy(); @@ -38,32 +35,24 @@ const useConversations = () => { initialData: [], }); - const combinedConversations = useMemo< - Array - >(() => { - return [...fetchedConversations, ...agents]; - }, [fetchedConversations, agents]); - const conversations = useMemo(() => { // If artist selected, filter to only that artist's conversations if (selectedArtist) { - return combinedConversations.filter( - (item: Conversation | ArtistAgent) => - "artist_id" in item && item.artist_id === selectedArtist.account_id, + return fetchedConversations.filter( + (item) => item.artist_id === selectedArtist.account_id, ); } // No artist selected - filter to artists in the current org view if (orgArtistIds.size > 0) { - return combinedConversations.filter( - (item: Conversation | ArtistAgent) => - "artist_id" in item && orgArtistIds.has(item.artist_id), + return fetchedConversations.filter((item) => + orgArtistIds.has(item.artist_id), ); } // Fallback: no artists in org (shouldn't happen normally) - return combinedConversations; - }, [selectedArtist, combinedConversations, orgArtistIds]); + return fetchedConversations; + }, [selectedArtist, fetchedConversations, orgArtistIds]); // Optimistic update helpers for creating a new chat room const addOptimisticConversation = ( diff --git a/hooks/useCreateChat.tsx b/hooks/useCreateChat.tsx index 1fa7f93c4..2301c01f4 100644 --- a/hooks/useCreateChat.tsx +++ b/hooks/useCreateChat.tsx @@ -4,7 +4,6 @@ import { CreateChatRequest, CreateChatResponse, } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { useArtistProvider } from "@/providers/ArtistProvider"; import { useConversationsProvider } from "@/providers/ConversationsProvider"; import { usePrivy } from "@privy-io/react-auth"; @@ -16,7 +15,7 @@ const useCreateChat = ({ setDisplayName, }: { isOptimisticChatItem: boolean; - chatRoom: Conversation | ArtistAgent; + chatRoom: Conversation; setDisplayName: (displayName: string) => void; }) => { const { selectedArtist } = useArtistProvider(); @@ -32,7 +31,7 @@ const useCreateChat = ({ if (!accessToken) return; // Extract first message from optimistic memories - const firstMessage = (chatRoom as Conversation).memories?.find( + const firstMessage = chatRoom.memories?.find( (memory) => { const content = memory?.content as { optimistic?: boolean; @@ -65,7 +64,7 @@ const useCreateChat = ({ const requestBody: CreateChatRequest = { artistId: selectedArtist?.account_id, - chatId: (chatRoom as Conversation).id, + chatId: chatRoom.id, firstMessage: messageText, }; diff --git a/hooks/useRenameModal.ts b/hooks/useRenameModal.ts index 694263691..1c634cb5a 100644 --- a/hooks/useRenameModal.ts +++ b/hooks/useRenameModal.ts @@ -1,18 +1,9 @@ import { useState, useEffect, useCallback } from "react"; import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; import { usePrivy } from "@privy-io/react-auth"; import { updateChat } from "@/lib/chats/updateChat"; import { useConversationsProvider } from "@/providers/ConversationsProvider"; -type ChatItem = Conversation | ArtistAgent; - -const isChatRoom = (item: ChatItem): item is Conversation => "id" in item; -const getChatName = (item: ChatItem): string => - isChatRoom(item) ? item.topic : item.type; -const getChatId = (item: ChatItem): string => - isChatRoom(item) ? item.id : item.agentId; - const validateName = (value: string): string => { const trimmed = value.trim(); @@ -26,7 +17,7 @@ const validateName = (value: string): string => { type UseRenameModalParams = { isOpen: boolean; - chatRoom: ChatItem | null; + chatRoom: Conversation | null; onClose: () => void; }; @@ -46,7 +37,7 @@ export function useRenameModal({ // Reset form when modal opens useEffect(() => { if (isOpen && chatRoom) { - setName(getChatName(chatRoom)); + setName(chatRoom.topic); setError(""); setTouched(false); setIsSubmitting(false); @@ -99,7 +90,7 @@ export function useRenameModal({ setIsSubmitting(true); try { - const chatId = getChatId(chatRoom); + const chatId = chatRoom.id; await updateChat({ accessToken, chatId, diff --git a/lib/chat/getChatDisplayInfo.ts b/lib/chat/getChatDisplayInfo.ts index 509604ba4..63d6b2526 100644 --- a/lib/chat/getChatDisplayInfo.ts +++ b/lib/chat/getChatDisplayInfo.ts @@ -1,14 +1,5 @@ import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; -import capitalize from "@/lib/capitalize"; -export const getChatDisplayInfo = (item: Conversation | ArtistAgent) => { - const isChatRoom = "id" in item; - const displayName = isChatRoom ? item.topic : capitalize(item.type); - - return { - displayName: - displayName || `${capitalize(isChatRoom ? "Chat" : item.type)} Analysis`, - isChatRoom, - }; -}; +export const getChatDisplayInfo = (item: Conversation) => ({ + displayName: item.topic || "Chat Analysis", +}); diff --git a/lib/chat/getChatRoomId.ts b/lib/chat/getChatRoomId.ts index a9caa0301..bba583ba3 100644 --- a/lib/chat/getChatRoomId.ts +++ b/lib/chat/getChatRoomId.ts @@ -1,7 +1,3 @@ import type { Conversation } from "@/types/Chat"; -import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; - -export const getChatRoomId = ( - chatRoom: Conversation | ArtistAgent, -): string => ("id" in chatRoom ? chatRoom.id : chatRoom.agentId); +export const getChatRoomId = (chatRoom: Conversation): string => chatRoom.id; diff --git a/lib/supabase/getArtistAgents.ts b/lib/supabase/getArtistAgents.ts deleted file mode 100644 index 9cba47cfa..000000000 --- a/lib/supabase/getArtistAgents.ts +++ /dev/null @@ -1,53 +0,0 @@ -import getSocialPlatformByLink from "../getSocialPlatformByLink"; -import supabase from "./serverClient"; - -export interface ArtistAgent { - type: string; - agentId: string; - updated_at: string; -} - -export async function getArtistAgents( - artistSocialIds: string[], -): Promise { - const { data, error } = await supabase - .from("agent_status") - .select("*, agent:agents(*)") - .in("social_id", artistSocialIds); - - if (error) { - console.error("Error fetching artist agents:", error); - return []; - } - - if (!data) return []; - - const agentIds = [...new Set(data.map((ele) => ele.agent.id))]; - - const { data: agents } = await supabase - .from("agents") - .select("*, agent_status(*, social:socials(*))") - .in("id", agentIds); - - if (!agents) return []; - - const transformedAgents = agents.map((agent) => ({ - type: new String( - agent.agent_status.length > 1 - ? "wrapped" - : getSocialPlatformByLink(agent.agent_status[0].social.profile_url), - ).toLowerCase(), - agentId: agent.id, - updated_at: agent.updated_at, - })); - - // eslint-disable-next-line - const aggregatedAgents: any = {}; - - transformedAgents.forEach((agent) => { - const type = agent.type.toLowerCase(); - aggregatedAgents[type] = agent; - }); - - return Object.values(aggregatedAgents); -}