Skip to content

Commit

Permalink
simplify provider status
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodanswer committed Sep 8, 2024
1 parent 98c6bf0 commit c0aff1e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 67 deletions.
4 changes: 1 addition & 3 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,6 @@ export function ChatPage({
} else if (Object.hasOwn(packet, "error")) {
error = (packet as StreamingError).error;
stackTrace = (packet as StreamingError).stack_trace;
console.log("error", error);
console.log("stackTrace", stackTrace);
} else if (Object.hasOwn(packet, "message_id")) {
finalMessage = packet as BackendMessage;
} else if (Object.hasOwn(packet, "stop_reason")) {
Expand Down Expand Up @@ -1605,7 +1603,7 @@ export function ChatPage({
{!shouldShowWelcomeModal &&
!shouldDisplaySourcesIncompleteModal &&
showApiKeyModal && (
<ApiKeyModal hide={() => setShowApiKeyModal(false)} user={user} />
<ApiKeyModal hide={() => setShowApiKeyModal(false)} />
)}
{shouldShowWelcomeModal
? "should show welcome modal"
Expand Down
5 changes: 0 additions & 5 deletions web/src/app/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from "@/lib/userSS";
import { redirect } from "next/navigation";
import { HealthCheckBanner } from "@/components/health/healthcheck";
import { ApiKeyModal } from "@/components/llm/ApiKeyModal";
import { fetchSS } from "@/lib/utilsSS";
import { CCPairBasicInfo, DocumentSet, Tag, User } from "@/lib/types";
import { cookies } from "next/headers";
Expand Down Expand Up @@ -187,10 +186,6 @@ export default async function Home() {
{shouldShowWelcomeModal && <WelcomeModal user={user} />}
<InstantSSRAutoRefresh />

{!shouldShowWelcomeModal &&
!shouldDisplayNoSourcesModal &&
!shouldDisplaySourcesIncompleteModal && <ApiKeyModal user={user} />}

{shouldDisplayNoSourcesModal && <NoSourcesModal />}

{shouldDisplaySourcesIncompleteModal && (
Expand Down
30 changes: 3 additions & 27 deletions web/src/components/chat_search/NoCredentialText.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import { WellKnownLLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces";
import { useEffect, useState } from "react";
import { checkLlmProvider } from "../initialSetup/welcome/lib";
import { useUser } from "../user/UserProvider";
import { useRouter } from "next/navigation";
import { useProviderStatus } from "./hooks";

export default function NoCredentialText({
showConfigureAPIKey,
}: {
showConfigureAPIKey: () => void;
}) {
const { user } = useUser();
const router = useRouter();
const [validProviderExists, setValidProviderExists] = useState<boolean>(true);
const [providerOptions, setProviderOptions] = useState<
WellKnownLLMProviderDescriptor[]
>([]);
const { shouldShowConfigurationNeeded } = useProviderStatus();

useEffect(() => {
async function fetchProviderInfo() {
const { providers, options, defaultCheckSuccessful } =
await checkLlmProvider(user);
setValidProviderExists(providers.length > 0 && defaultCheckSuccessful);
setProviderOptions(options);
}

fetchProviderInfo();
}, [router]);

// don't show if
// (1) a valid provider has been setup or
// (2) there are no provider options (e.g. user isn't an admin)
// (3) user explicitly hides the modal
if (validProviderExists || !providerOptions.length) {
if (!shouldShowConfigurationNeeded) {
return null;
}

Expand Down
34 changes: 34 additions & 0 deletions web/src/components/chat_search/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { useUser } from "../user/UserProvider";
import { useRouter } from "next/navigation";
import { checkLlmProvider } from "../initialSetup/welcome/lib";
import { WellKnownLLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces";

import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";

interface UseSidebarVisibilityProps {
Expand Down Expand Up @@ -81,3 +86,32 @@ export const useSidebarVisibility = ({

return { showDocSidebar };
};

export function useProviderStatus() {
const { user } = useUser();
const router = useRouter();
const [validProviderExists, setValidProviderExists] = useState<boolean>(true);
const [providerOptions, setProviderOptions] = useState<
WellKnownLLMProviderDescriptor[]
>([]);

useEffect(() => {
async function fetchProviderInfo() {
const { providers, options, defaultCheckSuccessful } =
await checkLlmProvider(user);
setValidProviderExists(providers.length > 0 && defaultCheckSuccessful);
setProviderOptions(options);
}

fetchProviderInfo();
}, [router, user]);

// don't show if
// (1) a valid provider has been setup or
// (2) there are no provider options (e.g. user isn't an admin)
// (3) (handled elsewhere) user explicitly hides the modal
const shouldShowConfigurationNeeded =
!validProviderExists && providerOptions.length > 0;

return { shouldShowConfigurationNeeded, providerOptions };
}
36 changes: 5 additions & 31 deletions web/src/components/llm/ApiKeyModal.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
"use client";

import { useState, useEffect } from "react";
import { ApiKeyForm } from "./ApiKeyForm";
import { Modal } from "../Modal";
import { WellKnownLLMProviderDescriptor } from "@/app/admin/configuration/llm/interfaces";
import { checkLlmProvider } from "../initialSetup/welcome/lib";
import { User } from "@/lib/types";
import { useRouter } from "next/navigation";
import { useProviderStatus } from "../chat_search/hooks";

export const ApiKeyModal = ({
user,
hide,
}: {
user: User | null;
hide: () => void;
}) => {
export const ApiKeyModal = ({ hide }: { hide: () => void }) => {
const router = useRouter();

const [validProviderExists, setValidProviderExists] = useState<boolean>(true);
const [providerOptions, setProviderOptions] = useState<
WellKnownLLMProviderDescriptor[]
>([]);
const { shouldShowConfigurationNeeded, providerOptions } =
useProviderStatus();

useEffect(() => {
async function fetchProviderInfo() {
const { providers, options, defaultCheckSuccessful } =
await checkLlmProvider(user);
setValidProviderExists(providers.length > 0 && defaultCheckSuccessful);
setProviderOptions(options);
}

fetchProviderInfo();
}, []);

// don't show if
// (1) a valid provider has been setup or
// (2) there are no provider options (e.g. user isn't an admin)
// (3) user explicitly hides the modal
if (validProviderExists || !providerOptions.length) {
if (!shouldShowConfigurationNeeded) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/search/SearchSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export const SearchSection = ({
!shouldDisplayNoSources &&
!shouldDisplaySourcesIncomplete &&
showApiKeyModal && (
<ApiKeyModal hide={() => setShowApiKeyModal(false)} user={user} />
<ApiKeyModal hide={() => setShowApiKeyModal(false)} />
)}

{deletingChatSession && (
Expand Down

0 comments on commit c0aff1e

Please sign in to comment.