mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-25 23:14:46 +08:00
feat: rename 'model' to 'model_name'
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { Client as LangGraphClient } from "@langchain/langgraph-sdk/client";
|
||||
|
||||
let _singleton: LangGraphClient | null = null;
|
||||
export function getLangGraphClient(): LangGraphClient {
|
||||
export function getAPIClient(): LangGraphClient {
|
||||
let url: URL | null = null;
|
||||
if (typeof window === "undefined") {
|
||||
url = new URL("/api/langgraph", "http://localhost:3000");
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { ThreadsClient } from "@langchain/langgraph-sdk/client";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import type { AgentThread, AgentThreadState } from "../threads";
|
||||
|
||||
import { getLangGraphClient } from "./client";
|
||||
|
||||
export function useThreads(
|
||||
params: Parameters<ThreadsClient["search"]>[0] = {
|
||||
limit: 50,
|
||||
sortBy: "updated_at",
|
||||
sortOrder: "desc",
|
||||
},
|
||||
) {
|
||||
const langGraphClient = getLangGraphClient();
|
||||
return useQuery<AgentThread[]>({
|
||||
queryKey: ["threads", "search", params],
|
||||
queryFn: async () => {
|
||||
const response =
|
||||
await langGraphClient.threads.search<AgentThreadState>(params);
|
||||
return response as AgentThread[];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
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<AgentThread>) => {
|
||||
return oldData.filter((t) => t.thread_id !== threadId);
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
export * from "./client";
|
||||
export * from "./hooks";
|
||||
export * from "./api-client";
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { AgentThreadContext } from "../threads";
|
||||
|
||||
export const DEFAULT_LOCAL_SETTINGS: LocalSettings = {
|
||||
context: {
|
||||
model: "deepseek-v3.2",
|
||||
model_name: "deepseek-v3.2",
|
||||
thinking_enabled: true,
|
||||
},
|
||||
};
|
||||
|
||||
119
frontend/src/core/threads/hooks.ts
Normal file
119
frontend/src/core/threads/hooks.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import type { HumanMessage } from "@langchain/core/messages";
|
||||
import type { ThreadsClient } from "@langchain/langgraph-sdk/client";
|
||||
import { useStream, type UseStream } from "@langchain/langgraph-sdk/react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useCallback } from "react";
|
||||
|
||||
import type { PromptInputMessage } from "@/components/ai-elements/prompt-input";
|
||||
|
||||
import { getAPIClient } from "../api";
|
||||
|
||||
import type {
|
||||
AgentThread,
|
||||
AgentThreadContext,
|
||||
AgentThreadState,
|
||||
} from "./types";
|
||||
|
||||
export function useThreadStream({
|
||||
threadId,
|
||||
isNewThread,
|
||||
}: {
|
||||
isNewThread: boolean;
|
||||
threadId: string | null | undefined;
|
||||
}) {
|
||||
const queryClient = useQueryClient();
|
||||
return useStream<AgentThreadState>({
|
||||
client: getAPIClient(),
|
||||
assistantId: "lead_agent",
|
||||
threadId: isNewThread ? undefined : threadId,
|
||||
reconnectOnMount: true,
|
||||
fetchStateHistory: true,
|
||||
onFinish() {
|
||||
void queryClient.invalidateQueries({ queryKey: ["threads", "search"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSubmitThread({
|
||||
threadId,
|
||||
thread,
|
||||
threadContext,
|
||||
isNewThread,
|
||||
message,
|
||||
}: {
|
||||
isNewThread: boolean;
|
||||
threadId: string;
|
||||
thread: UseStream<AgentThreadState>;
|
||||
threadContext: AgentThreadContext;
|
||||
message: PromptInputMessage;
|
||||
}) {
|
||||
const queryClient = useQueryClient();
|
||||
const text = message.text.trim();
|
||||
const callback = useCallback(async () => {
|
||||
await thread.submit(
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
type: "human",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text,
|
||||
},
|
||||
],
|
||||
},
|
||||
] as HumanMessage[],
|
||||
},
|
||||
{
|
||||
threadId: isNewThread ? threadId : undefined,
|
||||
streamSubgraphs: true,
|
||||
streamResumable: true,
|
||||
context: {
|
||||
...threadContext,
|
||||
thread_id: threadId,
|
||||
},
|
||||
},
|
||||
);
|
||||
void queryClient.invalidateQueries({ queryKey: ["threads", "search"] });
|
||||
}, [queryClient, thread, threadContext, threadId, isNewThread, text]);
|
||||
return callback;
|
||||
}
|
||||
|
||||
export function useThreads(
|
||||
params: Parameters<ThreadsClient["search"]>[0] = {
|
||||
limit: 50,
|
||||
sortBy: "updated_at",
|
||||
sortOrder: "desc",
|
||||
},
|
||||
) {
|
||||
const langGraphClient = getAPIClient();
|
||||
return useQuery<AgentThread[]>({
|
||||
queryKey: ["threads", "search", params],
|
||||
queryFn: async () => {
|
||||
const response =
|
||||
await langGraphClient.threads.search<AgentThreadState>(params);
|
||||
return response as AgentThread[];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteThread() {
|
||||
const queryClient = useQueryClient();
|
||||
const langGraphClient = getAPIClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({ threadId }: { threadId: string }) => {
|
||||
await langGraphClient.threads.delete(threadId);
|
||||
},
|
||||
onSuccess(_, { threadId }) {
|
||||
queryClient.setQueriesData(
|
||||
{
|
||||
queryKey: ["threads", "search"],
|
||||
exact: false,
|
||||
},
|
||||
(oldData: Array<AgentThread>) => {
|
||||
return oldData.filter((t) => t.thread_id !== threadId);
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -10,6 +10,6 @@ export interface AgentThread extends Thread<AgentThreadState> {}
|
||||
|
||||
export interface AgentThreadContext extends Record<string, unknown> {
|
||||
thread_id: string;
|
||||
model: string;
|
||||
model_name: string;
|
||||
thinking_enabled: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user