mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-09-27 06:56:17 +00:00
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
|
|
import { UnauthorizedError } from "@/core/api/errors";
|
|
|
|
import { loadModels } from "./api";
|
|
|
|
export const MODELS_QUERY_KEY = ["models"] as const;
|
|
|
|
export function useModels({ enabled = true }: { enabled?: boolean } = {}) {
|
|
const { data, isLoading, isFetching, error, refetch } = useQuery({
|
|
queryKey: MODELS_QUERY_KEY,
|
|
queryFn: () => loadModels(),
|
|
enabled,
|
|
// Surface persistent gateway failures promptly while retaining one retry
|
|
// for transient startup or network errors.
|
|
retry: (failureCount, queryError) =>
|
|
!(queryError instanceof UnauthorizedError) && failureCount < 1,
|
|
refetchOnWindowFocus: false,
|
|
// Model config changes rarely and every subtask card mounts its own
|
|
// observer of this query; without a staleTime each newly-mounted card would
|
|
// refetch /api/models on mount (default staleTime: 0). Treat the list as
|
|
// fresh for the session so a long conversation with many cards issues one
|
|
// request, not one per card.
|
|
staleTime: Infinity,
|
|
});
|
|
return {
|
|
models: data?.models ?? [],
|
|
tokenUsageEnabled: data?.token_usage.enabled ?? false,
|
|
isLoading,
|
|
isFetching,
|
|
error,
|
|
refetch,
|
|
};
|
|
}
|