From ddec10f0e33bfd0f4d9e079921ab3039ab423463 Mon Sep 17 00:00:00 2001 From: Janaka-Steph Date: Tue, 3 Oct 2023 13:52:35 +0200 Subject: [PATCH] Use service url for services --- .../prem-audio/api/generateTranscriptions.ts | 9 ++++---- src/modules/prem-image/api/generateImage.ts | 9 ++++---- .../api/generateImageViaBaseImage.ts | 7 ++++--- .../prem-text-audio/api/generateAudio.ts | 10 ++++----- .../api/generateUpscalerImage.ts | 11 +++++++--- src/shared/helpers/utils.ts | 21 +++++++++++++++++++ src/shared/hooks/usePremAudio.ts | 2 +- src/shared/hooks/usePremChatStream.ts | 19 +++++++++-------- src/shared/hooks/usePremImage.ts | 4 +--- src/shared/hooks/usePremTextAudio.ts | 2 +- src/shared/hooks/usePremUpscaler.ts | 2 +- 11 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/modules/prem-audio/api/generateTranscriptions.ts b/src/modules/prem-audio/api/generateTranscriptions.ts index 58d17f10..c3baf64c 100644 --- a/src/modules/prem-audio/api/generateTranscriptions.ts +++ b/src/modules/prem-audio/api/generateTranscriptions.ts @@ -1,13 +1,14 @@ import axios from "axios"; -import { generateUrl, isProxyEnabled } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl, isProxyEnabled } from "shared/helpers/utils"; import useSettingStore from "../../../shared/store/setting"; +import type { Service } from "../../service/types"; import type { TranscriptionsGeneration } from "../types"; -const generateTranscriptions = async (port: number, data: TranscriptionsGeneration) => { +const generateTranscriptions = async (service: Service, data: TranscriptionsGeneration) => { const backendUrl = generateUrl( - useSettingStore.getState().backendUrl, - port, + getServiceUrl(service.id), + service.runningPort, "v1/audio/transcriptions", ); const formData = new FormData(); diff --git a/src/modules/prem-image/api/generateImage.ts b/src/modules/prem-image/api/generateImage.ts index cc958313..95a788d6 100644 --- a/src/modules/prem-image/api/generateImage.ts +++ b/src/modules/prem-image/api/generateImage.ts @@ -1,13 +1,14 @@ import axios from "axios"; -import { generateUrl, isProxyEnabled } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl, 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 (port: number, data: ImageGeneration) => { +const generateImage = async (service: Service, data: ImageGeneration) => { const backendUrl = generateUrl( - useSettingStore.getState().backendUrl, - port, + getServiceUrl(service.id), + service.runningPort, "v1/images/generations", ); const isIP = useSettingStore.getState().isIP; diff --git a/src/modules/prem-image/api/generateImageViaBaseImage.ts b/src/modules/prem-image/api/generateImageViaBaseImage.ts index d54eacd7..815beaea 100644 --- a/src/modules/prem-image/api/generateImageViaBaseImage.ts +++ b/src/modules/prem-image/api/generateImageViaBaseImage.ts @@ -1,11 +1,12 @@ import axios from "axios"; -import { generateUrl, isProxyEnabled } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl, isProxyEnabled } from "shared/helpers/utils"; import useSettingStore from "../../../shared/store/setting"; +import type { Service } from "../../service/types"; import type { ImageGeneration } from "../types"; -const generateImageViaBaseImage = async (port: number, image: File, data: ImageGeneration) => { - const backendUrl = generateUrl(useSettingStore.getState().backendUrl, port, "v1/images/edits"); +const generateImageViaBaseImage = async (service: Service, image: File, data: ImageGeneration) => { + const backendUrl = generateUrl(getServiceUrl(service.id), service.runningPort, "v1/images/edits"); const formData = new FormData(); formData.append("image", image); diff --git a/src/modules/prem-text-audio/api/generateAudio.ts b/src/modules/prem-text-audio/api/generateAudio.ts index 05a8973d..c3e9a3f8 100644 --- a/src/modules/prem-text-audio/api/generateAudio.ts +++ b/src/modules/prem-text-audio/api/generateAudio.ts @@ -1,13 +1,13 @@ import axios from "axios"; -import { generateUrl } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl } from "shared/helpers/utils"; -import useSettingStore from "../../../shared/store/setting"; +import type { Service } from "../../service/types"; import type { AudioGenerationData } from "../types"; -const generateAudio = async (port: number, data: AudioGenerationData) => { +const generateAudio = async (service: Service, data: AudioGenerationData) => { const backendUrl = generateUrl( - useSettingStore.getState().backendUrl, - port, + getServiceUrl(service.id), + service.runningPort, "v1/audio/generation", ); diff --git a/src/modules/prem-upscaler/api/generateUpscalerImage.ts b/src/modules/prem-upscaler/api/generateUpscalerImage.ts index edf6e847..b9a6cc22 100644 --- a/src/modules/prem-upscaler/api/generateUpscalerImage.ts +++ b/src/modules/prem-upscaler/api/generateUpscalerImage.ts @@ -1,11 +1,16 @@ import axios from "axios"; -import { generateUrl, isProxyEnabled } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl, isProxyEnabled } from "shared/helpers/utils"; import useSettingStore from "../../../shared/store/setting"; +import type { Service } from "../../service/types"; import type { ImageGeneration } from "../types"; -const generateUpscalerImage = async (port: number, data: ImageGeneration) => { - const backendUrl = generateUrl(useSettingStore.getState().backendUrl, port, "v1/images/upscale"); +const generateUpscalerImage = async (service: Service, data: ImageGeneration) => { + const backendUrl = generateUrl( + getServiceUrl(service.id), + service.runningPort, + "v1/images/upscale", + ); const formData = new FormData(); formData.append("image", data.image); formData.append("prompt", data.prompt); diff --git a/src/shared/helpers/utils.ts b/src/shared/helpers/utils.ts index 04961889..7b17a3b3 100644 --- a/src/shared/helpers/utils.ts +++ b/src/shared/helpers/utils.ts @@ -195,6 +195,27 @@ export const generateUrl = (baseUrl: string, port: number, pathname: string) => return url.toString(); }; +export const getServiceUrl = (serviceId: Service["id"]) => { + let serviceUrl = "http://localhost:54321"; + const isBackendSet = () => { + return ( + ((window as any).VITE_BACKEND_URL !== undefined || import.meta.env.VITE_BACKEND_URL) && + ((window as any).VITE_BACKEND_URL !== "" || import.meta.env.VITE_BACKEND_URL !== "") + ); + }; + if (isBackendSet()) { + serviceUrl = (window as any).VITE_BACKEND_URL || import.meta.env.VITE_BACKEND_URL; + } + if (isPackaged()) { + serviceUrl = `${window.location.protocol}//${window.location.host}/`; + } + if (isProxyEnabled() && !isIP(window.location.host)) { + const arr = serviceUrl.split("://"); + serviceUrl = arr[0] + `://${serviceId}.` + arr[1]; + } + return serviceUrl; +}; + export const isDeveloperMode = () => { return (window as any).VITE_DEVELOPER_MODE === "1" || import.meta.env.VITE_DEVELOPER_MODE === "1"; }; diff --git a/src/shared/hooks/usePremAudio.ts b/src/shared/hooks/usePremAudio.ts index 6d310e87..7b8f5330 100644 --- a/src/shared/hooks/usePremAudio.ts +++ b/src/shared/hooks/usePremAudio.ts @@ -29,7 +29,7 @@ const usePremAudio = (serviceId: string, historyId: string | undefined): PremAud const { isLoading, isError, mutate } = useMutation( () => - generateTranscriptions(service?.runningPort ?? 0, { + generateTranscriptions(service!, { file: file!, model, }), diff --git a/src/shared/hooks/usePremChatStream.ts b/src/shared/hooks/usePremChatStream.ts index 8c3dee87..62c5f53e 100644 --- a/src/shared/hooks/usePremChatStream.ts +++ b/src/shared/hooks/usePremChatStream.ts @@ -4,7 +4,7 @@ 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 { generateUrl, isProxyEnabled } from "shared/helpers/utils"; +import { generateUrl, getServiceUrl, isProxyEnabled } from "shared/helpers/utils"; import { v4 as uuid } from "uuid"; import { shallow } from "zustand/shallow"; @@ -59,14 +59,15 @@ const usePremChatStream = (serviceId: string, chatId: string | null): PremChatRe const [backendUrlState, setBackendUrlState] = useState(""); useEffect(() => { - const backendUrl = generateUrl( - useSettingStore.getState().backendUrl, - service?.runningPort ?? 0, - "v1/chat/completions", - ); - setBackendUrlState(backendUrl); - setChatServiceUrl(backendUrl); - + if (service) { + const backendUrl = generateUrl( + getServiceUrl(service.id), + service?.runningPort ?? 0, + "v1/chat/completions", + ); + setBackendUrlState(backendUrl); + setChatServiceUrl(backendUrl); + } if (!promptTemplate) { setPromptTemplate(service?.promptTemplate || ""); } diff --git a/src/shared/hooks/usePremImage.ts b/src/shared/hooks/usePremImage.ts index 84bd0202..e7742c13 100644 --- a/src/shared/hooks/usePremImage.ts +++ b/src/shared/hooks/usePremImage.ts @@ -44,9 +44,7 @@ const usePremImage = (serviceId: string, historyId: string | undefined): PremIma const { isLoading, isError, mutate } = useMutation( () => - file - ? generateImageViaBaseImage(service?.runningPort ?? 0, file, payload) - : generateImage(service?.runningPort ?? 0, payload), + file ? generateImageViaBaseImage(service!, file, payload) : generateImage(service!, payload), { onSuccess: (response) => { setFile(undefined); diff --git a/src/shared/hooks/usePremTextAudio.ts b/src/shared/hooks/usePremTextAudio.ts index dd0e582d..9024c9f0 100644 --- a/src/shared/hooks/usePremTextAudio.ts +++ b/src/shared/hooks/usePremTextAudio.ts @@ -31,7 +31,7 @@ const usePremTextAudio = (serviceId: string, historyId: string | undefined): Pre const { isLoading, isError, mutate } = useMutation( () => - generateAudio(service?.runningPort ?? 0, { + generateAudio(service!, { prompt, }), { diff --git a/src/shared/hooks/usePremUpscaler.ts b/src/shared/hooks/usePremUpscaler.ts index 486d533e..6cafa0e8 100644 --- a/src/shared/hooks/usePremUpscaler.ts +++ b/src/shared/hooks/usePremUpscaler.ts @@ -42,7 +42,7 @@ const usePremUpscaler = (serviceId: string, historyId: string | undefined): Prem const { isLoading, isError, mutate } = useMutation( () => - generateUpscalerImage(service?.runningPort ?? 0, { + generateUpscalerImage(service!, { image: file!, prompt, n,