MCPcopy Create free account
hub / github.com/CodeFox-Repo/codefox / useChatStream

Function useChatStream

frontend/src/hooks/useChatStream.ts:23–179  ·  view source on GitHub ↗
({
  chatId,
  input,
  setInput,
  setMessages,
  setThinkingProcess,
  selectedModel,
  setIsTPUpdating,
}: UseChatStreamProps)

Source from the content-addressed store, hash-verified

21export const useChatStream = ({
22 chatId,
23 input,
24 setInput,
25 setMessages,
26 selectedModel,
27}: UseChatStreamProps) => {
28 const [loadingSubmit, setLoadingSubmit] = useState(false);
29 /** What the agent is doing right now, surfaced while the turn streams. */
30 const [activity, setActivity] = useState<{
31 tool?: string;
32 file?: string;
33 } | null>(null);
34 /** What the last turn's page got wrong. Replaced per turn, not appended:
35 * the agent may well have fixed the previous set, and a stale finding is
36 * worse than none. */
37 const [lint, setLint] = useState<LintFinding[]>([]);
38 // A restyle rewrites the page, so the findings on screen are about a page
39 // the user is no longer looking at. Both restyle paths announce; this is the
40 // only place the list lives.
41 useEffect(() => {
42 const clear = () => setLint([]);
43 window.addEventListener(PROJECT_RESTYLED, clear);
44 return () => window.removeEventListener(PROJECT_RESTYLED, clear);
45 }, []);
46 const abortRef = useRef<AbortController | null>(null);
47 /** Attachments for the turn being submitted. A ref because the first turn of
48 * a new chat is dispatched from createChat's onCompleted, outside the
49 * handleSubmit closure. */
50 const pendingImagesRef = useRef<string[] | undefined>(undefined);
51 const [currentChatId, setCurrentChatId] = useState<string>(chatId);
52 const { token } = useAuthContext();
53 const { curProject, refreshProjects, setFilePath, editorRef, turnFinished } =
54 useContext(ProjectContext);
55 const [curProjectPath, setCurProjectPath] = useState('');
56
57 useEffect(() => {
58 if (curProject) {
59 setCurProjectPath(curProject.projectPath);
60 }
61 }, [curProject]);
62
63 useEffect(() => {
64 const updateChatId = () => {
65 setCurrentChatId('');
66 setMessages([]); // Clear messages for new chat
67 };
68
69 // Only add event listener when we want to create a new chat
70 if (!chatId) {
71 window.addEventListener('newchat', updateChatId);
72 }
73
74 // Cleanup
75 return () => {
76 window.removeEventListener('newchat', updateChatId);
77 };
78 }, [chatId, setMessages]);
79
80 // Update currentChatId when chatId prop changes

Callers 1

ChatFunction · 0.90

Calls 2

useAuthContextFunction · 0.90
handleChatResponseFunction · 0.85

Tested by

no test coverage detected