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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79ae05b
 
 
7d94a77
79ae05b
 
 
 
7d94a77
79ae05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d94a77
79ae05b
7d94a77
 
 
 
 
 
 
 
79ae05b
7d94a77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79ae05b
 
 
 
 
 
 
7d94a77
79ae05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d94a77
79ae05b
 
 
 
 
 
 
7d94a77
79ae05b
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
"use client";

import { ChatData, ChatState, Conversation, Message, ResearchOptions, ResearchResults, ResearchTree, StatusUpdate } from "@/lib/types";
import { ReactNode, createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
import { v4 as uuidv4 } from "uuid";
import { disconnectSocket, getSocket, initializeSocket, initializeSse, closeSse } from "@/lib/socket";

// Utility functions for local storage
const saveToStorage = (data: ChatData) => {
  if (typeof window !== "undefined") {
    localStorage.setItem("chatData", JSON.stringify(data));
  }
};

const loadFromStorage = (): ChatData => {
  if (typeof window === "undefined") {
    return { conversations: [], currentConversationId: null };
  }
  const data = localStorage.getItem("chatData");
  if (!data) {
    return { conversations: [], currentConversationId: null };
  }
  try {
    const parsed = JSON.parse(data);
    return {
      conversations: Array.isArray(parsed.conversations)
        ? parsed.conversations.map((conv: Conversation) => ({
            ...conv,
            messages: Array.isArray(conv.messages)
              ? conv.messages.map((msg: Message) => ({
                  ...msg,
                  // Ensure media property is preserved if it exists
                  media: msg.media || undefined,
                }))
              : [],
          }))
        : [],
      currentConversationId: parsed.currentConversationId,
    };
  } catch (e) {
    return { conversations: [], currentConversationId: null };
  }
};

// Define the context type
interface ChatContextType {
  // State
  chatState: ChatState;
  conversations: Conversation[];
  currentConversationId: string | null;
  researchOptions: ResearchOptions;
  userInputRef: React.RefObject<HTMLTextAreaElement>;
  connectionMode: "agent" | "workflow";

  // Actions
  setResearchOptions: (options: ResearchOptions) => void;
  sendMessage: (content: string) => void;
  newConversation: () => void;
  selectConversation: (id: string) => void;
  deleteConversation: (id: string) => void;
  deleteAllConversations: () => void;
  abortResearch: () => void; // New function to abort research
  setConnectionMode: (mode: "agent" | "workflow") => void;
}

// Create the context with a default value
const ChatContext = createContext<ChatContextType | undefined>(undefined);

// Provider component
export const ChatProvider = ({ children }: { children: ReactNode }) => {
  const [chatState, setChatState] = useState<ChatState>({ messages: [], isLoading: false, error: null });
  const [conversations, setConversations] = useState<Conversation[]>([]);
  const [currentConversationId, setCurrentConversationId] = useState<string | null>(null);
  const [connectionMode, setConnectionMode] = useState<"agent" | "workflow">("agent");
  const [researchOptions, setResearchOptions] = useState<ResearchOptions>({
    depth: "basic",
    sources: true,
    citations: false,
    max_depth: 1,
    num_sites_per_query: 3,
    create_report: false,
  });

  const userInputRef = useRef<HTMLTextAreaElement>(null);

  // Focus management
  useEffect(() => {
    const focusInput = () => {
      setTimeout(() => {
        userInputRef.current?.focus();
      }, 100);
    };

    focusInput();
  }, [currentConversationId]);

  // Load initial data
  useEffect(() => {
    const data = loadFromStorage();
    setConversations(data.conversations);
    setCurrentConversationId(data.currentConversationId);

    if (data.currentConversationId) {
      const conversation = data.conversations.find((c) => c.id === data.currentConversationId);
      if (conversation) {
        // Check if any loaded message has isProgress true
        let messages = conversation.messages;
        if (messages.some((msg) => msg.isProgress === true)) {
          // Convert any progress messages to error messages
          messages = messages.map((msg) => (msg.isProgress === true ? { ...msg, content: "Connection error", isProgress: false } : msg));
        }

        setChatState((prev) => ({ ...prev, messages, isLoading: false }));
      }
    }
  }, []);

  // Socket initialization - REMOVED as we are using HTTP/SSE for both modes
  // useEffect(() => {
  //   if (connectionMode === "workflow") {
  //     const socket = initializeSocket();

  //     socket.on("connect", () => {
  //       console.log("Connected to research server");
  //     });

  //     socket.on("disconnect", () => {
  //       console.log("Disconnected from research server");

  //       // When socket disconnects, update any progress messages to show connection error
  //       setChatState((prevState) => {
  //         const updatedMessages = prevState.messages.map((msg) => (msg.isProgress === true ? { ...msg, content: "Connection error", isProgress: false } : msg));

  //         return {
  //           ...prevState,
  //           messages: updatedMessages,
  //           isLoading: false,
  //           error: "Lost connection to research server",
  //         };
  //       });
  //     });

  //     socket.on("status", (data: StatusUpdate) => {
  //       setChatState((prevState) => {
  //         const messages = [...prevState.messages];
  //         const progressText = data.message;
  //         const progress = data.progress;

  //         // Find the last assistant message that is a progress update
  //         const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

  //         if (lastProgressIndex !== -1) {
  //           // Update existing progress message with research_tree data
  //           messages[lastProgressIndex] = {
  //             ...messages[lastProgressIndex],
  //             content: progressText,
  //             progress: progress,
  //             timestamp: new Date(),
  //             research_tree: data.research_tree, // Update the research_tree in real-time
  //           };
  //         } else {
  //           // Add new progress message with research_tree
  //           messages.push({
  //             id: uuidv4(),
  //             content: progressText,
  //             role: "assistant",
  //             timestamp: new Date(),
  //             progress: progress,
  //             isProgress: true,
  //             research_tree: data.research_tree, // Include the research_tree
  //             media: {}, // Initialize empty media object
  //           });
  //         }

  //         return {
  //           ...prevState,
  //           messages,
  //           isLoading: true,
  //         };
  //       });
  //     });

  //     socket.on("research_complete", (results: ResearchResults) => {
  //       setChatState((prevState) => {
  //         const messages = [...prevState.messages];

  //         // Remove the last progress message if it exists
  //         const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

  //         if (lastProgressIndex !== -1) {
  //           messages.splice(lastProgressIndex, 1);
  //         }

  //         const newMessages = [
  //           ...messages,
  //           {
  //             id: uuidv4(),
  //             content: results.content || "Error: No content available",
  //             role: "assistant" as const,
  //             timestamp: new Date(results.timestamp),
  //             media: results.media,
  //             research_tree: results.research_tree,
  //           },
  //         ];

  //         // Save updated messages to localStorage
  //         const updatedState = {
  //           ...prevState,
  //           isLoading: false,
  //           messages: newMessages,
  //         };

  //         // Update localStorage with the new messages
  //         const updatedData: ChatData = {
  //           conversations: conversations.map((conv) => ({
  //             ...conv,
  //             messages: conv.id === currentConversationId ? newMessages : conv.messages || [],
  //             lastUpdated: conv.id === currentConversationId ? new Date().toISOString() : conv.lastUpdated,
  //           })),
  //           currentConversationId,
  //         };

  //         saveToStorage(updatedData);

  //         return updatedState;
  //       });
  //     });

  //     socket.on("research_aborted", () => {
  //       setChatState((prevState) => {
  //         const messages = [...prevState.messages];
  //         const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

  //         if (lastProgressIndex !== -1) {
  //           messages.splice(lastProgressIndex, 1);
  //         }

  //         // Add a message indicating the research was canceled
  //         messages.push({
  //           id: uuidv4(),
  //           content: "Research has been canceled.",
  //           role: "assistant",
  //           timestamp: new Date(),
  //         });

  //         return {
  //           ...prevState,
  //           isLoading: false,
  //           messages,
  //         };
  //       });
  //     });

  //     socket.on("error", (error: { message: string }) => {
  //       setChatState((prevState) => ({
  //         ...prevState,
  //         error: error.message,
  //         isLoading: false,
  //       }));
  //     });

  //     return () => {
  //       disconnectSocket();
  //     };
  //   }
  // }, [connectionMode]);

  // Save data whenever conversations or messages change
  useEffect(() => {
    const data: ChatData = {
      conversations: conversations.map((conv) => ({
        ...conv,
        messages: conv.id === currentConversationId ? chatState.messages : conv.messages || [],
      })),
      currentConversationId,
    };

    saveToStorage(data);
  }, [conversations, currentConversationId, chatState.messages]);

  // Action handlers
  const sendMessage = useCallback(
    async (content: string) => {
      if (!content.trim()) return;

      let conversationId = currentConversationId;
      const newMessage: Message = {
        id: uuidv4(),
        content,
        role: "user",
        timestamp: new Date(),
      };
      const newLoadingMessage: Message = {
        id: uuidv4(),
        content: "Loading...",
        role: "assistant",
        timestamp: new Date(),
        isProgress: true,
      };

      // Create a new conversation if none exists
      if (!conversationId) {
        conversationId = uuidv4();
        setCurrentConversationId(conversationId);
        setConversations((prev) => [
          {
            id: conversationId as string,
            title: content.length > 30 ? `${content.substring(0, 30)}...` : content,
            lastUpdated: new Date().toISOString(),
            messages: [newMessage],
            active: true,
          },
          ...prev.map((c) => ({ ...c, active: false })),
        ]);
      } else {
        // Update the existing conversation
        setConversations((prev) =>
          prev.map((conv) => ({
            ...conv,
            lastUpdated: conv.id === conversationId ? new Date().toISOString() : conv.lastUpdated,
            active: conv.id === conversationId,
            messages: conv.id === conversationId ? [...(conv.messages || []), newMessage] : conv.messages || [],
          }))
        );
      }

      // Add loading message immediately for workflow mode
      const messagesToAdd = connectionMode === "workflow" ? [newMessage, newLoadingMessage] : [newMessage];

      setChatState((prevState) => ({
        ...prevState,
        messages: [...prevState.messages, ...messagesToAdd],
        isLoading: true,
        error: null,
      }));

      try {
        const sseUrl = process.env.NEXT_PUBLIC_LANGGRAPH_BACKEND || "http://127.0.0.1:5000";
        const endpoint = connectionMode === "agent" ? "/chat" : "/start_research";
        const body =
          connectionMode === "agent"
            ? {
                message: content,
                thread_id: currentConversationId,
                create_report: researchOptions.create_report,
              }
            : {
                topic: content,
                max_depth: researchOptions.max_depth,
                num_sites_per_query: researchOptions.num_sites_per_query,
                session_id: currentConversationId,
              };

        const response = await fetch(`${sseUrl}${endpoint}`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(body),
        });

        if (!response.body) {
          return;
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder();

        let done = false;
        while (!done) {
          const { value, done: readerDone } = await reader.read();
          done = readerDone;
          const chunk = decoder.decode(value, { stream: true });
          const lines = chunk.split("\n\n");

          for (const line of lines) {
            if (line.startsWith("data: ")) {
              const jsonStr = line.substring(6);
              if (jsonStr) {
                try {
                  const data = JSON.parse(jsonStr);
                  if (data.event === "progress") {
                    setChatState((prevState) => {
                      const messages = [...prevState.messages];

                      if (connectionMode === "agent") {
                        const eventData = data.data[0]; // Assuming data.data is an array with one element

                        if (eventData.type === "ai_msg" || eventData.type === "ai_msg_report") {
                          messages.push({
                            id: uuidv4(),
                            content: eventData.content,
                            role: "assistant",
                            timestamp: new Date(),
                            tool_calls: eventData.tool_calls, // Store tool calls
                          });
                        } else if (eventData.type === "tool_resp") {
                          // Do not display tool responses directly in chat
                          return prevState;
                        }
                      } else {
                        // Workflow mode progress
                        const eventData = data.data;
                        const progress = eventData.progress;
                        const progressText = eventData.message;

                        // Find the last progress message
                        const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

                        if (lastProgressIndex !== -1) {
                          // Update existing progress message with research_tree data
                          messages[lastProgressIndex] = {
                            ...messages[lastProgressIndex],
                            content: progressText,
                            progress: progress,
                            timestamp: new Date(),
                            research_tree: eventData.research_tree, // Update the research_tree in real-time
                          };
                        } else {
                          // Add new progress message with research_tree
                          messages.push({
                            id: uuidv4(),
                            content: progressText,
                            role: "assistant",
                            timestamp: new Date(),
                            progress: progress,
                            isProgress: true,
                            research_tree: eventData.research_tree, // Include the research_tree
                            media: {}, // Initialize empty media object
                          });
                        }
                      }

                      return {
                        ...prevState,
                        messages,
                        isLoading: true, // Keep loading until stream ends
                      };
                    });
                  } else if (data.event === "result") {
                    setChatState((prevState) => {
                      const messages = [...prevState.messages];

                      // Remove the last progress message if it exists
                      const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

                      if (lastProgressIndex !== -1) {
                        messages.splice(lastProgressIndex, 1);
                      }

                      const resultData = connectionMode === "agent" ? data.data : data.data; // Both seem to be object in result event?
                      // Wait, agent mode result handling was:
                      // content: data.data.content || "Error: No content available",
                      // media: data.data.media,
                      // research_tree: data.data.research_tree,
                      //
                      // Workflow mode result handling was:
                      // content: results.content || "Error: No content available",
                      // media: results.media,
                      // research_tree: results.research_tree,
                      //
                      // They look compatible.

                      const newMessages = [
                        ...messages,
                        {
                          id: uuidv4(),
                          content: resultData.content || "Error: No content available",
                          role: "assistant" as const,
                          timestamp: new Date(resultData.timestamp || new Date()),
                          media: resultData.media,
                          research_tree: resultData.research_tree,
                        },
                      ];

                      // Save updated messages to localStorage
                      const updatedState = {
                        ...prevState,
                        isLoading: false,
                        messages: newMessages,
                      };

                      // Update localStorage with the new messages
                      const updatedData: ChatData = {
                        conversations: conversations.map((conv) => ({
                          ...conv,
                          messages: conv.id === currentConversationId ? newMessages : conv.messages || [],
                          lastUpdated: conv.id === currentConversationId ? new Date().toISOString() : conv.lastUpdated,
                        })),
                        currentConversationId,
                      };

                      saveToStorage(updatedData);

                      return updatedState;
                    });
                  }
                } catch (e) {
                  console.error("Failed to parse SSE data", e);
                }
              }
            }
          }
        }
      } catch (error) {
        setChatState((prevState) => ({
          ...prevState,
          error: "Failed to connect to server",
          isLoading: false,
        }));
      }
    },
    [currentConversationId, conversations, researchOptions, connectionMode]
  );

  const newConversation = useCallback(() => {
    userInputRef.current?.focus();
    setCurrentConversationId(null);
    setChatState(() => ({
      messages: [],
      isLoading: false,
      error: null,
    }));
  }, []);

  const selectConversation = useCallback((id: string) => {
    const data = loadFromStorage();
    const conversation = data.conversations.find((c) => c.id === id);

    setCurrentConversationId(id);
    setChatState((prev) => ({
      ...prev,
      messages: conversation?.messages || [],
      isLoading: false,
      error: null,
    }));
    setConversations((prev) =>
      prev.map((conv) => ({
        ...conv,
        active: conv.id === id,
      }))
    );
  }, []);

  const deleteConversation = useCallback(
    (id: string) => {
      setConversations((prev) => prev.filter((conv) => conv.id !== id));
      if (currentConversationId === id) {
        newConversation();
      }
    },
    [currentConversationId, newConversation]
  );

  const deleteAllConversations = useCallback(() => {
    setConversations([]);
    newConversation();
  }, [newConversation]);

  const abortResearch = useCallback(async () => {
    try {
      const sseUrl = process.env.NEXT_PUBLIC_LANGGRAPH_BACKEND || "http://127.0.0.1:5000";
      await fetch(`${sseUrl}/abort_research`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ session_id: currentConversationId }),
      });

      setChatState((prevState) => {
        const messages = [...prevState.messages];
        const lastProgressIndex = messages.findLastIndex((msg) => msg.role === "assistant" && msg.isProgress === true);

        if (lastProgressIndex !== -1) {
          messages.splice(lastProgressIndex, 1);
        }

        // Add a message indicating the research was canceled
        messages.push({
          id: uuidv4(),
          content: "Research has been canceled.",
          role: "assistant",
          timestamp: new Date(),
        });

        return {
          ...prevState,
          isLoading: false,
          messages,
        };
      });
    } catch (error) {
      console.error("Failed to abort research:", error);
      setChatState((prevState) => ({
        ...prevState,
        error: "Failed to abort research",
      }));
    }
  }, [currentConversationId]);

  // Keyboard shortcuts | Ctrl + I to new chat
  useEffect(() => {
    const handleKeyDown = (event: KeyboardEvent) => {
      if (event.ctrlKey && event.key === "i") {
        event.preventDefault();
        newConversation();
      }
    };

    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
    };
  }, []);

  const value = {
    chatState,
    conversations,
    currentConversationId,
    researchOptions,
    userInputRef,
    connectionMode,
    setResearchOptions,
    sendMessage,
    newConversation,
    selectConversation,
    deleteConversation,
    deleteAllConversations,
    abortResearch,
    setConnectionMode,
  };

  return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
};

// Custom hook for using the chat context
export const useChatContext = () => {
  const context = useContext(ChatContext);
  if (context === undefined) {
    throw new Error("useChatContext must be used within a ChatProvider");
  }
  return context;
};