mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-30 12:28:10 +00:00
When a user starts a new conversation, the sidebar list did not display
it until the AI finished streaming and generated a title. This made it
impossible to switch back to an in-progress conversation when working
with multiple threads concurrently.
Optimistically insert the new thread into the TanStack Query cache
during the `onCreated` callback so the sidebar renders a placeholder
entry ("New chat") as soon as the backend acknowledges thread creation.
The existing `onUpdateEvent` title handler and `onFinish` query
invalidation then update the entry in-place with the real title.
This commit is contained in:
parent
37451500eb
commit
0287240728
@ -1,7 +1,12 @@
|
||||
import type { AIMessage, Message, Run } from "@langchain/langgraph-sdk";
|
||||
import type { ThreadsClient } from "@langchain/langgraph-sdk/client";
|
||||
import { useStream } from "@langchain/langgraph-sdk/react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
type QueryClient,
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
} from "@tanstack/react-query";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
@ -149,6 +154,48 @@ export function getVisibleOptimisticMessages(
|
||||
return optimisticMessages;
|
||||
}
|
||||
|
||||
export function upsertThreadInSearchCache(
|
||||
queryClient: QueryClient,
|
||||
thread: AgentThread,
|
||||
) {
|
||||
queryClient.setQueriesData(
|
||||
{
|
||||
queryKey: ["threads", "search"],
|
||||
exact: false,
|
||||
},
|
||||
(oldData: Array<AgentThread> | undefined) => {
|
||||
if (!oldData) {
|
||||
return [thread];
|
||||
}
|
||||
|
||||
const existingIndex = oldData.findIndex(
|
||||
(t) => t.thread_id === thread.thread_id,
|
||||
);
|
||||
if (existingIndex === -1) {
|
||||
return [thread, ...oldData];
|
||||
}
|
||||
|
||||
return oldData.map((t, index) => {
|
||||
if (index !== existingIndex) {
|
||||
return t;
|
||||
}
|
||||
return {
|
||||
...thread,
|
||||
...t,
|
||||
metadata: {
|
||||
...(thread.metadata ?? {}),
|
||||
...(t.metadata ?? {}),
|
||||
},
|
||||
values: {
|
||||
...thread.values,
|
||||
...t.values,
|
||||
},
|
||||
};
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function getStreamErrorMessage(error: unknown): string {
|
||||
if (typeof error === "string" && error.trim()) {
|
||||
return error;
|
||||
@ -241,6 +288,20 @@ export function useThreadStream({
|
||||
fetchStateHistory: { limit: 1 },
|
||||
onCreated(meta) {
|
||||
handleStreamStart(meta.thread_id, meta.run_id);
|
||||
const now = new Date().toISOString();
|
||||
upsertThreadInSearchCache(queryClient, {
|
||||
thread_id: meta.thread_id,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
metadata: context.agent_name ? { agent_name: context.agent_name } : {},
|
||||
status: "busy",
|
||||
values: {
|
||||
title: t.pages.newChat,
|
||||
messages: [],
|
||||
artifacts: [],
|
||||
},
|
||||
interrupts: {},
|
||||
});
|
||||
if (context.agent_name && !isMock) {
|
||||
void getAPIClient()
|
||||
.threads.update(meta.thread_id, {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user