import { FormEventHandler, useCallback, useRef } from 'react' import { useAgent } from '../agent/TldrawAgentAppProvider' import { ChatHistory } from './chat-history/ChatHistory' import { ChatInput } from './ChatInput' import { TodoList } from './TodoList' export function ChatPanel() { const agent = useAgent() const inputRef = useRef(null) const handleSubmit = useCallback>( async (e) => { e.preventDefault() if (!inputRef.current) return const formData = new FormData(e.currentTarget) const value = formData.get('input') as string // If the user's message is empty, just cancel the current request (if there is one) if (value === '') { agent.cancel() return } // Clear the chat input (context is cleared after it's captured in requestAgentActions) inputRef.current.value = '' // Sending a new message to the agent should interrupt the current request agent.interrupt({ input: { agentMessages: [value], bounds: agent.editor.getViewportPageBounds(), source: 'user', contextItems: agent.context.getItems(), }, }) }, [agent] ) const handleNewChat = useCallback(() => { agent.reset() }, [agent]) return (
) }