File size: 2,139 Bytes
3d700dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import { useMemo } from "react";
import { useQuery } from "@tanstack/react-query";
import { isNoBackend } from "#/api/backend-registry/active-store";
import SettingsService from "#/api/settings-service/settings-service.api";
import { useActiveBackend } from "#/contexts/active-backend-context";
import { SettingsSchema } from "#/types/settings";
import { withLlmSubscriptionSchemaFields } from "#/utils/llm-subscription-schema";
import { useIsAuthed } from "./use-is-authed";
const useSettingsSchema = (
type: "agent" | "conversation",
fallbackSchema?: SettingsSchema | null,
) => {
const { data: userIsAuthenticated } = useIsAuthed();
const { backend, orgId } = useActiveBackend();
const hasBackend = !isNoBackend(backend);
const { data, error, isLoading, isFetching } = useQuery({
queryKey: [
"settings-schema",
type,
backend.id,
orgId,
backend.kind,
backend.host,
backend.apiKey,
],
queryFn:
type === "conversation"
? SettingsService.getConversationSettingsSchema
: SettingsService.getSettingsSchema,
retry: false,
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 5,
gcTime: 1000 * 60 * 15,
enabled: !fallbackSchema && !!userIsAuthenticated && hasBackend,
meta: {
disableToast: true,
},
});
const fallbackData = useMemo(
() =>
type === "agent"
? withLlmSubscriptionSchemaFields(fallbackSchema)
: fallbackSchema,
[fallbackSchema, type],
);
const queryData = useMemo(
() => (type === "agent" ? withLlmSubscriptionSchemaFields(data) : data),
[data, type],
);
if (fallbackSchema) {
return {
data: fallbackData,
error: null,
isLoading: false,
isFetching: false,
};
}
return {
data: queryData,
error,
isLoading,
isFetching,
};
};
export const useAgentSettingsSchema = (
fallbackSchema?: SettingsSchema | null,
) => useSettingsSchema("agent", fallbackSchema);
export const useConversationSettingsSchema = (
fallbackSchema?: SettingsSchema | null,
) => useSettingsSchema("conversation", fallbackSchema);
|