Girish Jeswani commited on
Commit
6fd5d0d
·
1 Parent(s): 637ec04

add message actions

Browse files
phd-advisor-frontend/src/pages/ChatPage.js CHANGED
@@ -302,6 +302,117 @@ const ChatPage = ({ onNavigateToHome }) => {
302
  }
303
  };
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  const handleMessageClick = (message) => {
306
  if (message.type === 'advisor') {
307
  const advisor = advisors[message.advisorId];
@@ -443,7 +554,9 @@ const ChatPage = ({ onNavigateToHome }) => {
443
  <MessageBubble
444
  key={message.id || index}
445
  message={message}
446
- onClick={() => handleMessageClick(message)}
 
 
447
  showReplyButton={message.type === 'advisor'}
448
  />
449
  );
 
302
  }
303
  };
304
 
305
+ const handleCopyMessage = (messageId, content) => {
306
+ // Optional: Show a toast notification or add to message history
307
+ console.log(`Copied message ${messageId}: ${content.substring(0, 50)}...`);
308
+
309
+ // could add a temporary notification here if desired
310
+ // const notificationMessage = {
311
+ // id: generateMessageId(),
312
+ // type: 'system',
313
+ // content: '✅ Response copied to clipboard',
314
+ // timestamp: new Date()
315
+ // };
316
+
317
+ // setMessages(prev => [...prev, notificationMessage]);
318
+
319
+ // // Remove the notification after 3 seconds
320
+ // setTimeout(() => {
321
+ // setMessages(prev => prev.filter(msg => msg.id !== notificationMessage.id));
322
+ // }, 3000);
323
+ };
324
+
325
+ const handleExpandMessage = async (messageId, advisorId) => {
326
+ const advisor = advisors[advisorId];
327
+
328
+ try {
329
+ setIsLoading(true);
330
+ setThinkingAdvisors([advisorId]);
331
+
332
+ // Find the original message to expand on
333
+ const originalMessage = messages.find(msg => msg.id === messageId);
334
+
335
+ if (!originalMessage) {
336
+ console.error('Original message not found');
337
+ return;
338
+ }
339
+
340
+ // Create advisor-specific expansion prompts
341
+ const advisorSpecificPrompts = {
342
+ 'methodologist': `Please expand on your previous response with more methodological detail. Include specific research methods, data collection techniques, analytical approaches, and methodological considerations that would be relevant. Provide practical examples and step-by-step guidance where applicable.`,
343
+ 'theorist': `Please elaborate on your previous response by exploring the theoretical frameworks and conceptual foundations in greater depth. Include additional theoretical perspectives, scholarly context, and conceptual analysis that would enrich understanding of the topic.`,
344
+ 'pragmatist': `Please expand on your previous response with more practical, actionable details. Include specific examples, concrete next steps, real-world applications, and practical strategies that can be immediately implemented.`
345
+ };
346
+
347
+ // Use advisor-specific prompt or general expansion prompt
348
+ const expandPrompt = advisorSpecificPrompts[advisorId] ||
349
+ `Please expand and elaborate on your previous response in more detail. Provide additional insights, practical examples, and deeper analysis that would be helpful for understanding and implementing your advice.`;
350
+
351
+ // Use the existing /reply-to-advisor endpoint
352
+ const response = await fetch('http://localhost:8000/reply-to-advisor', {
353
+ method: 'POST',
354
+ headers: {
355
+ 'Content-Type': 'application/json',
356
+ },
357
+ body: JSON.stringify({
358
+ advisor_id: advisorId,
359
+ user_input: expandPrompt,
360
+ original_message_id: messageId
361
+ }),
362
+ });
363
+
364
+ if (!response.ok) {
365
+ throw new Error(`HTTP error! status: ${response.status}`);
366
+ }
367
+
368
+ const data = await response.json();
369
+ setThinkingAdvisors([]);
370
+
371
+ if (data.type === 'advisor_reply') {
372
+ const expandedMessage = {
373
+ id: generateMessageId(),
374
+ type: 'advisor',
375
+ advisorId: advisorId,
376
+ content: data.response,
377
+ timestamp: new Date(),
378
+ isExpansion: true, // Mark this as an expansion
379
+ expandedFrom: messageId // Reference to original message
380
+ };
381
+ setMessages(prev => [...prev, expandedMessage]);
382
+ } else if (data.type === 'error') {
383
+ const errorMessage = {
384
+ id: generateMessageId(),
385
+ type: 'error',
386
+ content: data.response,
387
+ timestamp: new Date()
388
+ };
389
+ setMessages(prev => [...prev, errorMessage]);
390
+ }
391
+
392
+ } catch (error) {
393
+ console.error('Error expanding message:', error);
394
+ const errorMessage = {
395
+ id: generateMessageId(),
396
+ type: 'error',
397
+ content: 'Sorry, I encountered an error while expanding the response. Please try again.',
398
+ timestamp: new Date()
399
+ };
400
+ setMessages(prev => [...prev, errorMessage]);
401
+ } finally {
402
+ setIsLoading(false);
403
+ setThinkingAdvisors([]);
404
+ }
405
+ };
406
+
407
+ const handleReplyToMessage = (message) => {
408
+ const advisor = advisors[message.advisorId];
409
+ setReplyingTo({
410
+ advisorId: message.advisorId,
411
+ messageId: message.id,
412
+ advisorName: advisor.name
413
+ });
414
+ };
415
+
416
  const handleMessageClick = (message) => {
417
  if (message.type === 'advisor') {
418
  const advisor = advisors[message.advisorId];
 
554
  <MessageBubble
555
  key={message.id || index}
556
  message={message}
557
+ onReply={handleReplyToMessage}
558
+ onCopy={handleCopyMessage}
559
+ onExpand={handleExpandMessage}
560
  showReplyButton={message.type === 'advisor'}
561
  />
562
  );