From 631a5e3e31abe48e1d7fbfbd1f74d526ba671d80 Mon Sep 17 00:00:00 2001 From: Andrew Pennington Date: Thu, 15 Aug 2024 23:40:42 +0100 Subject: [PATCH] Add awaits --- ui/app/library/page.tsx | 2 +- ui/components/ChatWindow.tsx | 4 ++-- ui/components/DeleteChat.tsx | 2 +- ui/components/SearchImages.tsx | 2 +- ui/components/SearchVideos.tsx | 2 +- ui/components/SettingsDialog.tsx | 4 ++-- ui/lib/actions.ts | 2 +- ui/lib/serverEnvironment.ts | 9 +++++---- 8 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ui/app/library/page.tsx b/ui/app/library/page.tsx index a4925ab..fc17b19 100644 --- a/ui/app/library/page.tsx +++ b/ui/app/library/page.tsx @@ -22,7 +22,7 @@ const Page = () => { const fetchChats = async () => { setLoading(true); - const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/chats`, { + const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/chats`, { method: 'GET', headers: { 'Content-Type': 'application/json', diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index 0aa7a25..1cd61e1 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -40,7 +40,7 @@ const useSocket = ( ); const providers = await fetch( - `${getServerEnv("BACKEND_API_URL")}/models`, + `${await getServerEnv("BACKEND_API_URL")}/models`, { headers: { 'Content-Type': 'application/json', @@ -221,7 +221,7 @@ const loadMessages = async ( setNotFound: (notFound: boolean) => void, ) => { const res = await fetch( - `${getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, + `${await getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, { method: 'GET', headers: { diff --git a/ui/components/DeleteChat.tsx b/ui/components/DeleteChat.tsx index 3a18758..2ccc809 100644 --- a/ui/components/DeleteChat.tsx +++ b/ui/components/DeleteChat.tsx @@ -21,7 +21,7 @@ const DeleteChat = ({ setLoading(true); try { const res = await fetch( - `${getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, + `${await getServerEnv("BACKEND_API_URL")}/chats/${chatId}`, { method: 'DELETE', headers: { diff --git a/ui/components/SearchImages.tsx b/ui/components/SearchImages.tsx index be9dba7..92eca83 100644 --- a/ui/components/SearchImages.tsx +++ b/ui/components/SearchImages.tsx @@ -35,7 +35,7 @@ const SearchImages = ({ const chatModel = localStorage.getItem('chatModel'); const res = await fetch( - `${getServerEnv("BACKEND_API_URL")}/images`, + `${await getServerEnv("BACKEND_API_URL")}/images`, { method: 'POST', headers: { diff --git a/ui/components/SearchVideos.tsx b/ui/components/SearchVideos.tsx index 097a57b..703fe28 100644 --- a/ui/components/SearchVideos.tsx +++ b/ui/components/SearchVideos.tsx @@ -48,7 +48,7 @@ const Searchvideos = ({ const chatModel = localStorage.getItem('chatModel'); const res = await fetch( - `${getServerEnv("BACKEND_API_URL")}/videos`, + `${await getServerEnv("BACKEND_API_URL")}/videos`, { method: 'POST', headers: { diff --git a/ui/components/SettingsDialog.tsx b/ui/components/SettingsDialog.tsx index 458bb56..a13ea31 100644 --- a/ui/components/SettingsDialog.tsx +++ b/ui/components/SettingsDialog.tsx @@ -89,7 +89,7 @@ const SettingsDialog = ({ if (isOpen) { const fetchConfig = async () => { setIsLoading(true); - const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/config`, { + const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/config`, { headers: { 'Content-Type': 'application/json', }, @@ -149,7 +149,7 @@ const SettingsDialog = ({ setIsUpdating(true); try { - await fetch(`${getServerEnv("BACKEND_API_URL")}/config`, { + await fetch(`${await getServerEnv("BACKEND_API_URL")}/config`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/ui/lib/actions.ts b/ui/lib/actions.ts index afe6b8a..9c03339 100644 --- a/ui/lib/actions.ts +++ b/ui/lib/actions.ts @@ -5,7 +5,7 @@ export const getSuggestions = async (chatHisory: Message[]) => { const chatModel = localStorage.getItem('chatModel'); const chatModelProvider = localStorage.getItem('chatModelProvider'); - const res = await fetch(`${getServerEnv("BACKEND_API_URL")}/suggestions`, { + const res = await fetch(`${await getServerEnv("BACKEND_API_URL")}/suggestions`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/ui/lib/serverEnvironment.ts b/ui/lib/serverEnvironment.ts index 1f2b55e..c83e57d 100644 --- a/ui/lib/serverEnvironment.ts +++ b/ui/lib/serverEnvironment.ts @@ -1,7 +1,8 @@ import { NextResponse } from 'next/server'; +import process from 'process'; // In-memory cache for configuration data -let cachedConfig: { [key: string]: any } | null = null; +let cachedConfig: { [key: string]: string } ; let cacheTimestamp: number | null = null; const CACHE_DURATION_MS = 5 * 60 * 1000; // Cache duration: 5 minutes @@ -22,13 +23,13 @@ async function fetchConfig() { } } -export async function getServerEnv(envVar: string): Promise { +export async function await getServerEnv(envVar: string): Promise { // Check if the cache is still valid if (cachedConfig && cacheTimestamp && Date.now() - cacheTimestamp < CACHE_DURATION_MS) { - return cachedConfig[envVar] || null; + return cachedConfig[envVar] || process.env[envVar]; } // Fetch and cache the config if not in cache or cache is expired await fetchConfig(); - return cachedConfig ? cachedConfig[envVar] || null : null; + return cachedConfig[envVar]; }