Skip to content

Commit

Permalink
Support custom providers (#530)
Browse files Browse the repository at this point in the history
Enable adding custom providers in user settings
  • Loading branch information
kliu57 committed Apr 3, 2024
1 parent ae1e9f1 commit 73da47c
Show file tree
Hide file tree
Showing 11 changed files with 787 additions and 235 deletions.
8 changes: 3 additions & 5 deletions src/components/Message/AppMessage/Instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import {

import MessageBase, { type MessageBaseProps } from "../MessageBase";
import { ChatCraftAppMessage } from "../../../lib/ChatCraftMessage";
import { providerFromUrl, getSupportedProviders } from "../../../lib/providers";
import { nameToUrlMap, providerFromUrl, supportedProviders } from "../../../lib/providers";
import { OpenRouterProvider } from "../../../lib/providers/OpenRouterProvider";
import RevealablePasswordInput from "../../RevealablePasswordInput";
import PasswordInput from "../../PasswordInput";
import { useSettings } from "../../../hooks/use-settings";
import { FreeModelProvider } from "../../../lib/providers/DefaultProvider/FreeModelProvider";
import { nameToUrlMap } from "../../../lib/ChatCraftProvider";

const ApiKeyInstructionsText = `## Getting Started with ChatCraft
Expand Down Expand Up @@ -57,7 +56,6 @@ function Instructions(props: MessageBaseProps) {
const { settings, setSettings } = useSettings();
const [isValidating, setIsValidating] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);
const supportedProviders = getSupportedProviders();

// Override the text of the message
const message = new ChatCraftAppMessage({ ...props.message, text: ApiKeyInstructionsText });
Expand Down Expand Up @@ -156,7 +154,7 @@ function Instructions(props: MessageBaseProps) {
<FormControl isInvalid={isInvalid}>
<FormLabel>{settings.currentProvider.name} API Key </FormLabel>
<Flex gap={4} align="center">
<RevealablePasswordInput
<PasswordInput
flex="1"
type="password"
name="openai-api-key"
Expand Down
37 changes: 37 additions & 0 deletions src/components/PasswordInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useState, type ComponentPropsWithRef } from "react";
import { Input, InputGroup, InputRightElement, Button } from "@chakra-ui/react";

type PasswordInputProps = ComponentPropsWithRef<typeof Input> & {
inputSize?: "sm" | "md";
isInvalid?: boolean;
};

function PasswordInput({ inputSize = "md", isInvalid, ...props }: PasswordInputProps) {
const [show, setShow] = useState(false);

const paddingRight = inputSize === "sm" ? "2.5rem" : "4.5rem";
const paddingLeft = inputSize === "sm" ? "0.4rem" : undefined;
const inputFieldSize = inputSize === "sm" ? "sm" : "md";
const buttonSize = inputSize === "sm" ? "xs" : "sm";

return (
<InputGroup size={inputFieldSize}>
<Input
{...props}
pr={paddingRight}
pl={paddingLeft}
type={show ? "text" : "password"}
focusBorderColor={isInvalid ? "red.500" : "blue.500"}
borderColor={isInvalid ? "red.500" : "gray.200"}
isInvalid={isInvalid}
/>
<InputRightElement width={paddingRight}>
<Button variant="ghost" h="1.75rem" size={buttonSize} onClick={() => setShow(!show)}>
{show ? "Hide" : "Show"}
</Button>
</InputRightElement>
</InputGroup>
);
}

export default PasswordInput;

0 comments on commit 73da47c

Please sign in to comment.