forked from paradigmxyz/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api key inputs for gemini and anthropic
- Loading branch information
Showing
6 changed files
with
76 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import mixpanel from "mixpanel-browser"; | ||
|
||
import { BoxProps } from "@chakra-ui/react"; | ||
import { LabeledPasswordInputWithLink } from "./LabeledInputs"; | ||
import { useLocalStorage } from "../../utils/lstore"; | ||
import { | ||
API_KEY_LOCAL_STORAGE_KEY, | ||
FLUX_ANTHROPIC_API_KEY, | ||
FLUX_GOOGLE_API_KEY, | ||
} from "../../utils/constants"; | ||
import { ApiKeyProvider, isValidAPIKey } from "../../utils/apikey"; | ||
import { MIXPANEL_TOKEN } from "../../main"; | ||
|
||
const url = import.meta.env.VITE_OPENAI_GET_KEY; | ||
|
||
export function APIKeyInput({ | ||
apiKey, | ||
setApiKey, | ||
...others | ||
}: { | ||
apiKey: string | null; | ||
setApiKey: (apiKey: string) => void; | ||
} & BoxProps) { | ||
interface APIKeyInputProps extends BoxProps { | ||
provider: ApiKeyProvider; | ||
label: string; | ||
} | ||
|
||
const STORAGE_KEYS = { | ||
openai: API_KEY_LOCAL_STORAGE_KEY, | ||
anthropic: FLUX_ANTHROPIC_API_KEY, | ||
google: FLUX_GOOGLE_API_KEY, | ||
}; | ||
|
||
export function APIKeyInput({ provider, ...others }: APIKeyInputProps) { | ||
const [apiKey, setApiKey] = useLocalStorage<string>(STORAGE_KEYS[provider]); | ||
|
||
const setValue = (key: string) => { | ||
setApiKey(key); | ||
|
||
if (isValidAPIKey(apiKey, provider)) { | ||
if (MIXPANEL_TOKEN) mixpanel.track("Entered API Key"); // KPI | ||
|
||
// Hacky way to get the prompt box to focus after the | ||
// modal closes. Long term should probably use a ref. | ||
setTimeout(() => window.document.getElementById("promptBox")?.focus(), 50); | ||
} | ||
}; | ||
|
||
return ( | ||
<LabeledPasswordInputWithLink | ||
width="80%" | ||
label="OpenAI API Key" | ||
width="100%" | ||
linkLabel="Get a key" | ||
placeholder="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | ||
link={url} | ||
value={apiKey ?? ""} | ||
setValue={setApiKey} | ||
setValue={setValue} | ||
{...others} | ||
/> | ||
); | ||
} | ||
|
||
export function APIKeyInputs() { | ||
return ( | ||
<> | ||
<APIKeyInput label="OpenAI API Key" provider="openai" /> | ||
<APIKeyInput label="Anthropic API Key" provider="anthropic" /> | ||
<APIKeyInput label="Google API Key" provider="google" /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
export function isValidAPIKey(apiKey: string | null) { | ||
return (apiKey?.length === 51 && apiKey.startsWith("sk-")) || (apiKey?.length === 56 && apiKey.startsWith("sk-proj-")); | ||
export type ApiKeyProvider = "openai" | "anthropic" | "google"; | ||
|
||
export function isValidAPIKey(apiKey: string | null, provider?: ApiKeyProvider) { | ||
if (!apiKey) return false; | ||
if (provider === "anthropic") { | ||
return apiKey.length > 100 && apiKey?.startsWith("sk-ant-"); | ||
} | ||
if (provider === "google") { | ||
return apiKey.length >= 50; | ||
} | ||
return ( | ||
(apiKey.length === 51 && apiKey.startsWith("sk-")) || | ||
(apiKey.length === 56 && apiKey.startsWith("sk-proj-")) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters