Refactor hooks and improve error handling in chat functionality (#962)

* refactor: update useThreadChat and useThreadStream hooks for improved state management

* fix: improve error handling in agent configuration loading and enhance chat page functionality

* fix: enhance error handling in agent configuration loading

* Update frontend/src/app/workspace/agents/[agent_name]/chats/[thread_id]/page.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
JeffJiang
2026-03-04 09:50:45 +08:00
committed by GitHub
parent 7de94394d4
commit 14d1e01149
6 changed files with 46 additions and 37 deletions

View File

@@ -1,8 +1,8 @@
import type { AIMessage } from "@langchain/langgraph-sdk";
import type { ThreadsClient } from "@langchain/langgraph-sdk/client";
import { useStream, type UseStream } from "@langchain/langgraph-sdk/react";
import { useStream } from "@langchain/langgraph-sdk/react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
import type { PromptInputMessage } from "@/components/ai-elements/prompt-input";
@@ -37,6 +37,13 @@ export function useThreadStream({
onToolEnd,
}: ThreadStreamOptions) {
const [_threadId, setThreadId] = useState<string | null>(threadId ?? null);
useEffect(() => {
if (_threadId && _threadId !== threadId) {
setThreadId(threadId ?? null);
}
}, [threadId, _threadId]);
const queryClient = useQueryClient();
const updateSubtask = useUpdateSubtask();
const thread = useStream<AgentThreadState>({
@@ -74,27 +81,7 @@ export function useThreadStream({
},
onFinish(state) {
onFinish?.(state.values);
// void queryClient.invalidateQueries({ queryKey: ["threads", "search"] });
queryClient.setQueriesData(
{
queryKey: ["threads", "search"],
exact: false,
},
(oldData: Array<AgentThread>) => {
return oldData.map((t) => {
if (t.thread_id === threadId) {
return {
...t,
values: {
...t.values,
title: state.values.title,
},
};
}
return t;
});
},
);
void queryClient.invalidateQueries({ queryKey: ["threads", "search"] });
},
});