|
| 1 | +import React, { useCallback, useRef, useEffect, useState, useImperativeHandle, forwardRef } from 'react'; |
| 2 | +import { Button } from '@/components/ui/button'; |
| 3 | +import { MoreHorizontal, Send } from 'lucide-react'; |
| 4 | +import { MessageComponent } from '../chat/chat-message'; |
| 5 | +import { useKanbanStore } from '@/stores/kanban'; |
| 6 | +import { useProjectStore } from '@/stores/project'; |
| 7 | +import { Input } from '@/components/ui/input'; |
| 8 | +import DotMatrix from '@/components/ui/animated-dot-matrix'; |
| 9 | +import { useAgentExecution } from './use-agent-execution'; |
| 10 | + |
| 11 | +interface AgentChatProps { |
| 12 | + taskId: string; |
| 13 | + className?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export interface AgentChatRef { |
| 17 | + executeAgent: () => Promise<void>; |
| 18 | + stopAgent: () => void; |
| 19 | +} |
| 20 | + |
| 21 | +export const AgentChat = forwardRef<AgentChatRef, AgentChatProps>(({ taskId, className = '' }, ref) => { |
| 22 | + const { currentProject } = useProjectStore(); |
| 23 | + const { addMessage } = useKanbanStore(); |
| 24 | + const [inputValue, setInputValue] = useState(''); |
| 25 | + |
| 26 | + // Use the new agent execution hook |
| 27 | + const { |
| 28 | + messages, |
| 29 | + executeAgent, |
| 30 | + stopAgent, |
| 31 | + isExecuting, |
| 32 | + error, |
| 33 | + append, |
| 34 | + } = useAgentExecution({ taskId }); |
| 35 | + |
| 36 | + const messagesEndRef = useRef<HTMLDivElement>(null); |
| 37 | + |
| 38 | + // Expose methods to parent via ref |
| 39 | + useImperativeHandle(ref, () => ({ |
| 40 | + executeAgent, |
| 41 | + stopAgent, |
| 42 | + }), [executeAgent, stopAgent]); |
| 43 | + |
| 44 | + const scrollToBottom = useCallback(() => { |
| 45 | + messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); |
| 46 | + }, []); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + scrollToBottom(); |
| 50 | + }, [messages, scrollToBottom]); |
| 51 | + |
| 52 | + // Add message to chat (no auto-execution) |
| 53 | + const handleSend = useCallback(async () => { |
| 54 | + if (!inputValue.trim()) return; |
| 55 | + |
| 56 | + const userMessage = inputValue.trim(); |
| 57 | + setInputValue(''); |
| 58 | + |
| 59 | + // Save user message to kanban store |
| 60 | + if (currentProject) { |
| 61 | + addMessage(currentProject, taskId, { |
| 62 | + role: 'user', |
| 63 | + content: userMessage |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + // Add to chat via useChat |
| 68 | + await append({ |
| 69 | + role: 'user', |
| 70 | + content: userMessage, |
| 71 | + }); |
| 72 | + }, [inputValue, currentProject, taskId, addMessage, append]); |
| 73 | + |
| 74 | + const handleKeyPress = useCallback((e: React.KeyboardEvent) => { |
| 75 | + if (e.key === 'Enter' && !e.shiftKey) { |
| 76 | + e.preventDefault(); |
| 77 | + handleSend(); |
| 78 | + } |
| 79 | + }, [handleSend]); |
| 80 | + |
| 81 | + const handleCopyMessage = useCallback((content: string) => { |
| 82 | + navigator.clipboard.writeText(content); |
| 83 | + }, []); |
| 84 | + |
| 85 | + const handleDeleteMessage = useCallback((id: string) => { |
| 86 | + // Note: Deleting from useChat messages would require custom implementation |
| 87 | + // For now, we'll just copy to clipboard |
| 88 | + console.log('Delete message:', id); |
| 89 | + }, []); |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className={`flex flex-col h-full ${className}`}> |
| 93 | + {/* Header */} |
| 94 | + <div className="border-b p-3 flex-shrink-0"> |
| 95 | + <div className="flex items-center justify-between"> |
| 96 | + <h3 className="text-sm font-medium">Agent Chat</h3> |
| 97 | + <Button variant="ghost" size="sm" className="h-6 w-6 p-0"> |
| 98 | + <MoreHorizontal className="h-3 w-3" /> |
| 99 | + </Button> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + |
| 103 | + {/* Messages */} |
| 104 | + <div className="flex-1 overflow-hidden"> |
| 105 | + <div className="h-full overflow-y-auto"> |
| 106 | + <div className="p-3 space-y-4"> |
| 107 | + {messages.map((message) => ( |
| 108 | + <MessageComponent |
| 109 | + key={message.id} |
| 110 | + message={message} |
| 111 | + onCopy={handleCopyMessage} |
| 112 | + onDelete={handleDeleteMessage} |
| 113 | + // Note: Tool handling for agents would be different from regular chat |
| 114 | + onToolApprove={() => {}} |
| 115 | + onToolCancel={() => {}} |
| 116 | + /> |
| 117 | + ))} |
| 118 | + |
| 119 | + {isExecuting && ( |
| 120 | + <div className="flex justify-start"> |
| 121 | + <DotMatrix |
| 122 | + baseColor='#444' |
| 123 | + fillColor="#4caf50" |
| 124 | + dotSize={3} |
| 125 | + rows={3} |
| 126 | + fillSpeed={3000} |
| 127 | + autoFill={true} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + )} |
| 131 | + |
| 132 | + {error && ( |
| 133 | + <div className="text-red-500 text-sm p-2 bg-red-50 rounded"> |
| 134 | + Error: {error.message} |
| 135 | + </div> |
| 136 | + )} |
| 137 | + |
| 138 | + <div ref={messagesEndRef} /> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + |
| 143 | + {/* Input Area */} |
| 144 | + <div className="border-t p-3 flex-shrink-0"> |
| 145 | + <div className="flex gap-2"> |
| 146 | + <Input |
| 147 | + value={inputValue} |
| 148 | + onChange={(e) => setInputValue(e.target.value)} |
| 149 | + onKeyPress={handleKeyPress} |
| 150 | + placeholder="Add message for the agent..." |
| 151 | + disabled={isExecuting} |
| 152 | + className="flex-1" |
| 153 | + /> |
| 154 | + <Button |
| 155 | + onClick={handleSend} |
| 156 | + disabled={!inputValue.trim() || isExecuting} |
| 157 | + size="sm" |
| 158 | + className="px-3" |
| 159 | + > |
| 160 | + <Send className="h-4 w-4" /> |
| 161 | + </Button> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +}); |
| 167 | + |
| 168 | +AgentChat.displayName = 'AgentChat'; |
0 commit comments