From 34b7711a5c0431d238620c453bcbc8c9b6edef76 Mon Sep 17 00:00:00 2001 From: Janaka-Steph Date: Mon, 9 Oct 2023 22:50:03 +0200 Subject: [PATCH] Use baseUrl, no header (#370) * Use baseUrl, no header * Fix urls * Add log * Logs IP * Fix chat service url * Fix download * Remove logs --- .../prem-audio/api/generateTranscriptions.ts | 9 +------- src/modules/prem-image/api/generateImage.ts | 9 +------- .../api/generateImageViaBaseImage.ts | 9 +------- .../prem-text-audio/api/generateAudio.ts | 9 +------- .../api/generateUpscalerImage.ts | 9 +------- .../components/ServiceGeneralInfo.tsx | 4 ++-- .../service/api/downloadServiceStream.ts | 10 +++++++-- src/modules/service/types.ts | 5 +---- src/shared/api/dnsd.ts | 3 +-- src/shared/api/v1.ts | 11 +++++++--- src/shared/hooks/usePremChatStream.ts | 21 ++++--------------- src/shared/hooks/usePremTextAudio.ts | 2 +- 12 files changed, 30 insertions(+), 71 deletions(-) diff --git a/src/modules/prem-audio/api/generateTranscriptions.ts b/src/modules/prem-audio/api/generateTranscriptions.ts index 17bd7085..5e9df4ad 100644 --- a/src/modules/prem-audio/api/generateTranscriptions.ts +++ b/src/modules/prem-audio/api/generateTranscriptions.ts @@ -1,7 +1,5 @@ import axios from "axios"; -import { isProxyEnabled } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; import type { Service } from "../../service/types"; import type { TranscriptionsGeneration } from "../types"; @@ -9,13 +7,8 @@ const generateTranscriptions = async (service: Service, data: TranscriptionsGene const formData = new FormData(); formData.append("file", data.file); formData.append("model", data.model); - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "multipart/form-data" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/transcriptions`, formData, { + return axios.post(`${service.baseUrl}/v1/audio/transcriptions`, formData, { headers, }); }; diff --git a/src/modules/prem-image/api/generateImage.ts b/src/modules/prem-image/api/generateImage.ts index 0d531ae8..ce5c33f8 100644 --- a/src/modules/prem-image/api/generateImage.ts +++ b/src/modules/prem-image/api/generateImage.ts @@ -1,18 +1,11 @@ import axios from "axios"; -import { isProxyEnabled } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; import type { Service } from "../../service/types"; import type { ImageGeneration } from "../types"; const generateImage = async (service: Service, data: ImageGeneration) => { - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "application/json" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - return axios.post(`${service.invokeMethod.baseUrl}/v1/images/generation`, data, { headers }); + return axios.post(`${service.baseUrl}/v1/images/generation`, data, { headers }); }; export default generateImage; diff --git a/src/modules/prem-image/api/generateImageViaBaseImage.ts b/src/modules/prem-image/api/generateImageViaBaseImage.ts index 05e19f36..fb244110 100644 --- a/src/modules/prem-image/api/generateImageViaBaseImage.ts +++ b/src/modules/prem-image/api/generateImageViaBaseImage.ts @@ -1,7 +1,5 @@ import axios from "axios"; -import { isProxyEnabled } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; import type { Service } from "../../service/types"; import type { ImageGeneration } from "../types"; @@ -15,13 +13,8 @@ const generateImageViaBaseImage = async (service: Service, image: File, data: Im formData.append("negative_prompt", `${data.negative_prompt || ""}`); formData.append("seed", `${data.seed}`); - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "multipart/form-data" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - return axios.post(`${service.invokeMethod.baseUrl}/v1/images/edits`, formData, { headers }); + return axios.post(`${service.baseUrl}/v1/images/edits`, formData, { headers }); }; export default generateImageViaBaseImage; diff --git a/src/modules/prem-text-audio/api/generateAudio.ts b/src/modules/prem-text-audio/api/generateAudio.ts index c6d1457c..bf55a11c 100644 --- a/src/modules/prem-text-audio/api/generateAudio.ts +++ b/src/modules/prem-text-audio/api/generateAudio.ts @@ -1,18 +1,11 @@ import axios from "axios"; -import { isProxyEnabled } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; import type { Service } from "../../service/types"; import type { AudioGenerationData } from "../types"; const generateAudio = async (service: Service, data: AudioGenerationData) => { - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "application/json" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - return axios.post(`${service.invokeMethod.baseUrl}/v1/audio/generation`, data, { headers }); + return axios.post(`${service.baseUrl}/v1/audio/generation`, data, { headers }); }; export default generateAudio; diff --git a/src/modules/prem-upscaler/api/generateUpscalerImage.ts b/src/modules/prem-upscaler/api/generateUpscalerImage.ts index 67470403..0baa8aa9 100644 --- a/src/modules/prem-upscaler/api/generateUpscalerImage.ts +++ b/src/modules/prem-upscaler/api/generateUpscalerImage.ts @@ -1,7 +1,5 @@ import axios from "axios"; -import { isProxyEnabled } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; import type { Service } from "../../service/types"; import type { ImageGeneration } from "../types"; @@ -13,13 +11,8 @@ const generateUpscalerImage = async (service: Service, data: ImageGeneration) => formData.append("response_format", data.response_format); formData.append("guidance_scale", `${data.guidance_scale}`); formData.append("num_inference_steps", `${data.num_inference_steps}`); - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "multipart/form-data" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - return axios.post(`${service.invokeMethod.baseUrl}/v1/images/upscale`, formData, { headers }); + return axios.post(`${service.baseUrl}/v1/images/upscale`, formData, { headers }); }; export default generateUpscalerImage; diff --git a/src/modules/service-detail/components/ServiceGeneralInfo.tsx b/src/modules/service-detail/components/ServiceGeneralInfo.tsx index 0f9040e8..1415c2bc 100644 --- a/src/modules/service-detail/components/ServiceGeneralInfo.tsx +++ b/src/modules/service-detail/components/ServiceGeneralInfo.tsx @@ -39,8 +39,8 @@ const ServiceGeneralInfo = ({ service }: ServiceGeneralInfoProps) => { if (!service.running) { return ""; } - return `${service.invokeMethod.baseUrl}/docs`; - }, [service.invokeMethod, service.running]); + return `${service.baseUrl}/docs`; + }, [service.baseUrl, service.running]); return (
diff --git a/src/modules/service/api/downloadServiceStream.ts b/src/modules/service/api/downloadServiceStream.ts index 898fb18b..b02fbeeb 100644 --- a/src/modules/service/api/downloadServiceStream.ts +++ b/src/modules/service/api/downloadServiceStream.ts @@ -18,9 +18,15 @@ const downloadServiceStream = async ( onMessage: (message: DownloadMessage) => void, onceCompleted: () => void, ): Promise => { - const backendUrl = new URL(useSettingStore.getState().backendUrl); try { - instantiateEventSource(backendUrl.toString(), serviceId); + const isIP = useSettingStore.getState().isIP; + let baseUrl; + if (isIP) { + baseUrl = `${useSettingStore.getState().backendUrl}premd/`; + } else { + baseUrl = `${window.location.protocol}//premd.${window.location.host}/`; + } + instantiateEventSource(baseUrl, serviceId); eventSources[serviceId].onmessage = (event) => { if (!event.data) return; const parsedData = JSON.parse(event.data); diff --git a/src/modules/service/types.ts b/src/modules/service/types.ts index 14897638..c477f3b6 100644 --- a/src/modules/service/types.ts +++ b/src/modules/service/types.ts @@ -47,10 +47,7 @@ export type Service = { beta: boolean; comingSoon: boolean; promptTemplate: string; - invokeMethod: { - header: string; - baseUrl: string; - }; + baseUrl: string; }; export type SearchFilterProps = { diff --git a/src/shared/api/dnsd.ts b/src/shared/api/dnsd.ts index 4103c53e..b0960975 100644 --- a/src/shared/api/dnsd.ts +++ b/src/shared/api/dnsd.ts @@ -9,8 +9,7 @@ const apiDnsd = () => { let baseURL; if (isProxyEnabled()) { if (isIP) { - baseURL = useSettingStore.getState().backendUrl; - Object.assign(headers, { "X-Host-Override": "dnsd" }); + baseURL = `${useSettingStore.getState().backendUrl}dnsd/`; } else { baseURL = `${window.location.protocol}//dnsd.${window.location.host}/`; } diff --git a/src/shared/api/v1.ts b/src/shared/api/v1.ts index 3b14f691..4c3a5eb3 100644 --- a/src/shared/api/v1.ts +++ b/src/shared/api/v1.ts @@ -6,11 +6,16 @@ import useSettingStore from "../store/setting"; const api = () => { const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "application/json" }; - if (isProxyEnabled() && isIP) { - Object.assign(headers, { "X-Host-Override": "premd" }); + let baseURL; + if (isProxyEnabled()) { + if (isIP) { + baseURL = `${useSettingStore.getState().backendUrl}premd/`; + } else { + baseURL = `${window.location.protocol}//premd.${window.location.host}/`; + } } return axios.create({ - baseURL: useSettingStore.getState().backendUrl, + baseURL, headers, }); }; diff --git a/src/shared/hooks/usePremChatStream.ts b/src/shared/hooks/usePremChatStream.ts index 577f50a6..8176755b 100644 --- a/src/shared/hooks/usePremChatStream.ts +++ b/src/shared/hooks/usePremChatStream.ts @@ -4,12 +4,10 @@ import type { PremChatResponse } from "modules/prem-chat/types"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useNavigate } from "react-router-dom"; import { toast } from "react-toastify"; -import { isProxyEnabled } from "shared/helpers/utils"; import { v4 as uuid } from "uuid"; import { shallow } from "zustand/shallow"; import usePremChatStore from "../store/prem-chat"; -import useSettingStore from "../store/setting"; import useService from "./useService"; @@ -56,16 +54,11 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe }), shallow, ); - const [backendUrlState, setBackendUrlState] = useState(""); useEffect(() => { - if (service) { - const backendUrl = `${window.location.protocol}//${service.invokeMethod.baseUrl}`; - setBackendUrlState(backendUrl); - setChatServiceUrl(backendUrl); - } + setChatServiceUrl(`${service?.baseUrl}/v1/chat/completions`); if (!promptTemplate) { - setPromptTemplate(service?.promptTemplate || ""); + setPromptTemplate(service?.promptTemplate ?? ""); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [service]); @@ -93,14 +86,8 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe setLoading(true); abortController.current = new AbortController(); - const isIP = useSettingStore.getState().isIP; const headers = { "Content-Type": "application/json" }; - if (isProxyEnabled() && isIP && service?.invokeMethod.header) { - const [key, value] = service.invokeMethod.header.split(":"); - Object.assign(headers, { [key]: value }); - } - console.log("chatServiceUrl", chatServiceUrl); try { fetchEventSource(chatServiceUrl, { method: "POST", @@ -204,8 +191,8 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe }, [service, setPromptTemplate]); const resetChatServiceUrl = useCallback(() => { - setChatServiceUrl(backendUrlState); - }, [backendUrlState, setChatServiceUrl]); + setChatServiceUrl(service?.baseUrl ?? ""); + }, [service?.baseUrl, setChatServiceUrl]); return { chatMessages, diff --git a/src/shared/hooks/usePremTextAudio.ts b/src/shared/hooks/usePremTextAudio.ts index ac43426f..0ba15586 100644 --- a/src/shared/hooks/usePremTextAudio.ts +++ b/src/shared/hooks/usePremTextAudio.ts @@ -40,7 +40,7 @@ const usePremTextAudio = (serviceId: string, historyId: string | undefined): Pre file, prompt, timestamp: new Date().toISOString(), - fileUrl: `${service!.invokeMethod.baseUrl}/files/${file}`, + fileUrl: `${service!.baseUrl}/files/${file}`, }); navigate(`/prem-text-audio/${serviceId}/${id}`); },