Prep for custom models: support reading custom providers (#131)
This commit is contained in:
42
src/hooks/useLanguageModelProviders.ts
Normal file
42
src/hooks/useLanguageModelProviders.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
import type { LanguageModelProvider } from "@/ipc/ipc_types";
|
||||
import { useSettings } from "./useSettings";
|
||||
import { cloudProviders } from "@/lib/schemas";
|
||||
|
||||
export function useLanguageModelProviders() {
|
||||
const ipcClient = IpcClient.getInstance();
|
||||
const { settings, envVars } = useSettings();
|
||||
|
||||
const queryResult = useQuery<LanguageModelProvider[], Error>({
|
||||
queryKey: ["languageModelProviders"],
|
||||
queryFn: async () => {
|
||||
return ipcClient.getLanguageModelProviders();
|
||||
},
|
||||
});
|
||||
|
||||
const isProviderSetup = (provider: string) => {
|
||||
const providerSettings = settings?.providerSettings[provider];
|
||||
if (queryResult.isLoading) {
|
||||
return false;
|
||||
}
|
||||
if (providerSettings?.apiKey?.value) {
|
||||
return true;
|
||||
}
|
||||
const providerData = queryResult.data?.find((p) => p.id === provider);
|
||||
if (providerData?.envVarName && envVars[providerData.envVarName]) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const isAnyProviderSetup = () => {
|
||||
return cloudProviders.some((provider) => isProviderSetup(provider));
|
||||
};
|
||||
|
||||
return {
|
||||
...queryResult,
|
||||
isProviderSetup,
|
||||
isAnyProviderSetup,
|
||||
};
|
||||
}
|
||||
@@ -2,15 +2,9 @@ import { useState, useEffect, useCallback } from "react";
|
||||
import { useAtom } from "jotai";
|
||||
import { userSettingsAtom, envVarsAtom } from "@/atoms/appAtoms";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
import { cloudProviders, type UserSettings } from "@/lib/schemas";
|
||||
import { type UserSettings } from "@/lib/schemas";
|
||||
import { usePostHog } from "posthog-js/react";
|
||||
|
||||
const PROVIDER_TO_ENV_VAR: Record<string, string> = {
|
||||
openai: "OPENAI_API_KEY",
|
||||
anthropic: "ANTHROPIC_API_KEY",
|
||||
google: "GEMINI_API_KEY",
|
||||
};
|
||||
|
||||
const TELEMETRY_CONSENT_KEY = "dyadTelemetryConsent";
|
||||
const TELEMETRY_USER_ID_KEY = "dyadTelemetryUserId";
|
||||
|
||||
@@ -81,17 +75,6 @@ export function useSettings() {
|
||||
}
|
||||
};
|
||||
|
||||
const isProviderSetup = (provider: string) => {
|
||||
const providerSettings = settings?.providerSettings[provider];
|
||||
if (providerSettings?.apiKey?.value) {
|
||||
return true;
|
||||
}
|
||||
if (envVars[PROVIDER_TO_ENV_VAR[provider]]) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return {
|
||||
settings,
|
||||
envVars,
|
||||
@@ -99,13 +82,6 @@ export function useSettings() {
|
||||
error,
|
||||
updateSettings,
|
||||
|
||||
isProviderSetup,
|
||||
isAnyProviderSetup: () => {
|
||||
// Technically we should check for ollama and lmstudio being setup, but
|
||||
// practically most users will want to use a cloud provider (at least
|
||||
// some of the time)
|
||||
return cloudProviders.some((provider) => isProviderSetup(provider));
|
||||
},
|
||||
refreshSettings: () => {
|
||||
return loadInitialData();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user