import { useCallback } from 'react' import { toast } from 'sonner' import { usePlaygroundStore } from '../store' import { ComboboxAgent, type PlaygroundChatMessage } from '@/types/playground' import { getPlaygroundAgentsAPI, getPlaygroundStatusAPI } from '@/api/playground' import { useQueryState } from 'nuqs' const useChatActions = () => { const { chatInputRef } = usePlaygroundStore() const selectedEndpoint = usePlaygroundStore((state) => state.selectedEndpoint) const [, setSessionId] = useQueryState('session') const setMessages = usePlaygroundStore((state) => state.setMessages) const setIsEndpointActive = usePlaygroundStore( (state) => state.setIsEndpointActive ) const setIsEndpointLoading = usePlaygroundStore( (state) => state.setIsEndpointLoading ) const setAgents = usePlaygroundStore((state) => state.setAgents) const setSelectedModel = usePlaygroundStore((state) => state.setSelectedModel) const [agentId, setAgentId] = useQueryState('agent') const getStatus = useCallback(async () => { try { const status = await getPlaygroundStatusAPI(selectedEndpoint) return status } catch { return 503 } }, [selectedEndpoint]) const getAgents = useCallback(async () => { try { const agents = await getPlaygroundAgentsAPI(selectedEndpoint) return agents } catch { toast.error('Error fetching agents') return [] } }, [selectedEndpoint]) const clearChat = useCallback(() => { setMessages([]) setSessionId(null) // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const focusChatInput = useCallback(() => { setTimeout(() => { requestAnimationFrame(() => chatInputRef?.current?.focus()) }, 0) // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const addMessage = useCallback( (message: PlaygroundChatMessage) => { setMessages((prevMessages) => [...prevMessages, message]) }, [setMessages] ) const initializePlayground = useCallback(async () => { setIsEndpointLoading(true) try { const status = await getStatus() let agents: ComboboxAgent[] = [] if (status === 200) { setIsEndpointActive(true) agents = await getAgents() if (agents.length > 0 && !agentId) { const firstAgent = agents[0] setAgentId(firstAgent.value) setSelectedModel(firstAgent.model.provider || '') } } else { setIsEndpointActive(false) } setAgents(agents) return agents } catch { setIsEndpointLoading(false) } finally { setIsEndpointLoading(false) } }, [ getStatus, getAgents, setIsEndpointActive, setIsEndpointLoading, setAgents, setAgentId, setSelectedModel, agentId ]) return { clearChat, addMessage, getAgents, focusChatInput, initializePlayground } } export default useChatActions