mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-14 10:44:46 +08:00
feat: implement basic web app
This commit is contained in:
17
frontend/src/core/api/client.ts
Normal file
17
frontend/src/core/api/client.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
"use client";
|
||||
|
||||
import { Client as LangGraphClient } from "@langchain/langgraph-sdk/client";
|
||||
|
||||
let _singleton: LangGraphClient | null = null;
|
||||
export function getLangGraphClient(): LangGraphClient {
|
||||
let url: URL | null = null;
|
||||
if (typeof window === "undefined") {
|
||||
url = new URL("/api/langgraph", "http://localhost:3000");
|
||||
} else {
|
||||
url = new URL("/api/langgraph", window.location.origin);
|
||||
}
|
||||
_singleton ??= new LangGraphClient({
|
||||
apiUrl: "http://localhost:2024",
|
||||
});
|
||||
return _singleton;
|
||||
}
|
||||
45
frontend/src/core/api/hooks.ts
Normal file
45
frontend/src/core/api/hooks.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { ThreadsClient } from "@langchain/langgraph-sdk/client";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import type { MessageThread, MessageThreadState } from "../thread";
|
||||
|
||||
import { getLangGraphClient } from "./client";
|
||||
|
||||
export function useThreads(
|
||||
params: Parameters<ThreadsClient["search"]>[0] = {
|
||||
limit: 50,
|
||||
sortBy: "updated_at",
|
||||
sortOrder: "desc",
|
||||
},
|
||||
) {
|
||||
const langGraphClient = getLangGraphClient();
|
||||
return useQuery<MessageThread[]>({
|
||||
queryKey: ["threads", "search", params],
|
||||
queryFn: async () => {
|
||||
const response =
|
||||
await langGraphClient.threads.search<MessageThreadState>(params);
|
||||
return response as MessageThread[];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteThread() {
|
||||
const queryClient = useQueryClient();
|
||||
const langGraphClient = getLangGraphClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({ threadId }: { threadId: string }) => {
|
||||
await langGraphClient.threads.delete(threadId);
|
||||
},
|
||||
onSuccess(_, { threadId }) {
|
||||
queryClient.setQueriesData(
|
||||
{
|
||||
queryKey: ["threads", "search"],
|
||||
exact: false,
|
||||
},
|
||||
(oldData: Array<MessageThread>) => {
|
||||
return oldData.filter((t) => t.thread_id !== threadId);
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
2
frontend/src/core/api/index.ts
Normal file
2
frontend/src/core/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./client";
|
||||
export * from "./hooks";
|
||||
Reference in New Issue
Block a user