|
3 | 3 | import React, { useState, useRef, useEffect } from 'react'; |
4 | 4 | import { Send, Loader2, Save, Cpu, X, AlertTriangle, Clipboard } from 'lucide-react'; |
5 | 5 | import { sendPrompt } from '@utils/sendApi'; |
6 | | -import { CompleteAgent } from '@utils/agent_database'; |
| 6 | +import { CompleteAgent, updateAgentImageMemory } from '@utils/agent_database'; |
7 | 7 | import { extractAgentConfig, parseAgentResponse, extractImageRequest } from '@utils/agentParser'; |
8 | 8 | import MediaUploadMessage from './MediaUploadMessage'; |
9 | 9 | import getConversationalSystemPrompt from '@utils/conversational_system_prompt'; |
@@ -203,6 +203,11 @@ What would you like to create today?` |
203 | 203 | const [isLoading, setIsLoading] = useState(false); |
204 | 204 | const chatEndRef = useRef<HTMLDivElement>(null); |
205 | 205 |
|
| 206 | + // Auto-scroll to bottom when new messages are added |
| 207 | + useEffect(() => { |
| 208 | + chatEndRef.current?.scrollIntoView({ behavior: 'smooth' }); |
| 209 | + }, [messages]); |
| 210 | + |
206 | 211 | // --- STATE FOR MODAL AND LOCAL MODEL SELECTION --- |
207 | 212 | const [isLocalModalOpen, setIsLocalModalOpen] = useState(false); |
208 | 213 | const [selectedLocalModel, setSelectedLocalModel] = useState(''); |
@@ -236,22 +241,54 @@ What would you like to create today?` |
236 | 241 | // Check for agent config first (priority) |
237 | 242 | const agentConfig = extractAgentConfig(responseText); |
238 | 243 | if (agentConfig) { |
239 | | - const responseMessage: Message = { |
240 | | - id: Date.now() + 1, |
| 244 | + // Extract text outside $$$ blocks |
| 245 | + const textOutsideBlocks = responseText.replace(/\$\$\$\s*\n?[\s\S]*?\n?\$\$\$/g, '').trim(); |
| 246 | + |
| 247 | + const newMessages: Message[] = []; |
| 248 | + |
| 249 | + // Add regular AI message if there's text outside the $$$ blocks |
| 250 | + if (textOutsideBlocks) { |
| 251 | + newMessages.push({ |
| 252 | + id: Date.now() + 1, |
| 253 | + text: textOutsideBlocks, |
| 254 | + sender: 'ai', |
| 255 | + }); |
| 256 | + } |
| 257 | + |
| 258 | + // Add system message |
| 259 | + newMessages.push({ |
| 260 | + id: Date.now() + 2, |
241 | 261 | text: agentConfig, |
242 | 262 | sender: 'system', |
243 | | - }; |
244 | | - setMessages(prev => [...prev, responseMessage]); |
| 263 | + }); |
| 264 | + |
| 265 | + setMessages(prev => [...prev, ...newMessages]); |
245 | 266 | } else { |
246 | 267 | // Check for image request |
247 | 268 | const imageRequest = extractImageRequest(responseText); |
248 | 269 | if (imageRequest) { |
249 | | - const responseMessage: Message = { |
250 | | - id: Date.now() + 1, |
| 270 | + // Extract text outside %%% blocks |
| 271 | + const textOutsideBlocks = responseText.replace(/%%%\s*\n?[\s\S]*?\n?%%%/g, '').trim(); |
| 272 | + |
| 273 | + const newMessages: Message[] = []; |
| 274 | + |
| 275 | + // Add regular AI message if there's text outside the %%% blocks |
| 276 | + if (textOutsideBlocks) { |
| 277 | + newMessages.push({ |
| 278 | + id: Date.now() + 1, |
| 279 | + text: textOutsideBlocks, |
| 280 | + sender: 'ai', |
| 281 | + }); |
| 282 | + } |
| 283 | + |
| 284 | + // Add image request message |
| 285 | + newMessages.push({ |
| 286 | + id: Date.now() + 2, |
251 | 287 | text: imageRequest, |
252 | 288 | sender: 'image-request', |
253 | | - }; |
254 | | - setMessages(prev => [...prev, responseMessage]); |
| 289 | + }); |
| 290 | + |
| 291 | + setMessages(prev => [...prev, ...newMessages]); |
255 | 292 | } else { |
256 | 293 | // Regular AI response |
257 | 294 | const responseMessage: Message = { |
@@ -289,9 +326,18 @@ What would you like to create today?` |
289 | 326 | await sendConversation(allMessages); |
290 | 327 | }; |
291 | 328 |
|
292 | | - const handleConfigureAndSave = (configText: string) => { |
| 329 | + const handleConfigureAndSave = async (configText: string) => { |
293 | 330 | const parsed = parseAgentResponse(configText); |
294 | 331 | if (parsed) { |
| 332 | + // Collect all images from the conversation and store them for the agent |
| 333 | + const images = messages |
| 334 | + .filter(msg => msg.imageData) |
| 335 | + .map(msg => msg.imageData!); |
| 336 | + |
| 337 | + if (images.length > 0) { |
| 338 | + await updateAgentImageMemory(parsed.agent.id, images); |
| 339 | + } |
| 340 | + |
295 | 341 | onAgentGenerated(parsed.agent, parsed.code); |
296 | 342 | } else { |
297 | 343 | setMessages(prev => [...prev, { id: Date.now(), sender: 'ai', text: "I'm sorry, there was an error parsing that. Could you try describing your agent again?" }]); |
|
0 commit comments