From 85ad2901ca3eee88ebb540e9195575d8375938a9 Mon Sep 17 00:00:00 2001 From: Arda Erzin Date: Tue, 10 Dec 2024 22:38:00 +0100 Subject: [PATCH 1/4] feat(frontend): shared SWR config for the application --- agenta-web/src/lib/api/SWRConfig.tsx | 19 +++++++++++++++++++ .../{helpers => api/assets}/axiosConfig.ts | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 agenta-web/src/lib/api/SWRConfig.tsx rename agenta-web/src/lib/{helpers => api/assets}/axiosConfig.ts (94%) diff --git a/agenta-web/src/lib/api/SWRConfig.tsx b/agenta-web/src/lib/api/SWRConfig.tsx new file mode 100644 index 0000000000..b393aedbdf --- /dev/null +++ b/agenta-web/src/lib/api/SWRConfig.tsx @@ -0,0 +1,19 @@ +import {SWRConfig, type SWRConfiguration} from "swr" +import axios from "@/lib/api/assets/axiosConfig" + +const config: SWRConfiguration = { + fetcher: (url: string) => axios.get(url).then((res) => res.data), +} + +const AgSWRConfig = ({ + children, + config: passedConfig = {}, +}: { + children: React.ReactNode + config?: Partial +}) => { + const mergedConfig = {...config, ...passedConfig} + return {children} +} + +export default AgSWRConfig diff --git a/agenta-web/src/lib/helpers/axiosConfig.ts b/agenta-web/src/lib/api/assets/axiosConfig.ts similarity index 94% rename from agenta-web/src/lib/helpers/axiosConfig.ts rename to agenta-web/src/lib/api/assets/axiosConfig.ts index 0961ebe87c..3da6bbe2a5 100644 --- a/agenta-web/src/lib/helpers/axiosConfig.ts +++ b/agenta-web/src/lib/api/assets/axiosConfig.ts @@ -1,8 +1,8 @@ import axiosApi from "axios" -import {getErrorMessage, globalErrorHandler} from "./errorHandler" +import {getErrorMessage, globalErrorHandler} from "../../helpers/errorHandler" import {signOut} from "supertokens-auth-react/recipe/session" import router from "next/router" -import {getAgentaApiUrl} from "./utils" +import {getAgentaApiUrl} from "../../helpers/utils" import isObject from "lodash/isObject" import AlertPopup from "@/components/AlertPopup/AlertPopup" From 91fb899918da318d875581f3c626e65d43f420f4 Mon Sep 17 00:00:00 2001 From: Arda Erzin Date: Tue, 10 Dec 2024 22:38:33 +0100 Subject: [PATCH 2/4] chore(frontend): update existing useSWR hooks --- agenta-web/src/contexts/app.context.tsx | 7 ++----- agenta-web/src/services/testsets/api/index.ts | 11 +++++------ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/agenta-web/src/contexts/app.context.tsx b/agenta-web/src/contexts/app.context.tsx index 548aa34f39..e0e6768dc7 100644 --- a/agenta-web/src/contexts/app.context.tsx +++ b/agenta-web/src/contexts/app.context.tsx @@ -45,12 +45,9 @@ const useApps = () => { const isMockProjectId = projectId === DEFAULT_UUID const {selectedOrg, loading} = useOrgData() + const shouldFetch = !!user && (!isDemo() || !!selectedOrg?.id) const {data, error, isLoading, mutate} = useSWR( - !!user - ? `${getAgentaApiUrl()}/api/apps?` + - (!isMockProjectId ? `project_id=${projectId}&` : "") - : null, - !!user ? (isDemo() ? (selectedOrg?.id ? axiosFetcher : () => {}) : axiosFetcher) : null, + shouldFetch ? `/api/apps?` + (!isMockProjectId ? `project_id=${projectId}&` : "") : null, { shouldRetryOnError: false, }, diff --git a/agenta-web/src/services/testsets/api/index.ts b/agenta-web/src/services/testsets/api/index.ts index e6fb42f774..8527bd46a3 100644 --- a/agenta-web/src/services/testsets/api/index.ts +++ b/agenta-web/src/services/testsets/api/index.ts @@ -1,5 +1,5 @@ import useSWR from "swr" -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {getAgentaApiUrl} from "@/lib/helpers/utils" import {axiosFetcher} from "@/services/api" import {getCurrentProject} from "@/contexts/project.context" @@ -14,11 +14,10 @@ import {getCurrentProject} from "@/contexts/project.context" export const useLoadTestsetsList = () => { const {projectId} = getCurrentProject() - const {data, error, mutate, isLoading} = useSWR( - `${getAgentaApiUrl()}/api/testsets?project_id=${projectId}`, - axiosFetcher, - {revalidateOnFocus: false, shouldRetryOnError: false}, - ) + const {data, error, mutate, isLoading} = useSWR(`/api/testsets?project_id=${projectId}`, { + revalidateOnFocus: false, + shouldRetryOnError: false, + }) return { testsets: data || [], From c4836fbc637e3bc6afe72500a0e1b5f5dc3cd4f5 Mon Sep 17 00:00:00 2001 From: Arda Erzin Date: Tue, 10 Dec 2024 22:38:52 +0100 Subject: [PATCH 3/4] chore(frontend): update axios imports --- .../components/HumanEvaluationModal/HumanEvaluationModal.tsx | 2 +- agenta-web/src/lib/hooks/useVariant.ts | 2 +- agenta-web/src/services/api.ts | 2 +- agenta-web/src/services/app-selector/api/index.ts | 2 +- agenta-web/src/services/deployment/api/index.ts | 2 +- agenta-web/src/services/evaluations/api/index.ts | 2 +- agenta-web/src/services/human-evaluations/api/index.ts | 2 +- agenta-web/src/services/observability/core/index.ts | 2 +- agenta-web/src/services/playground/api/index.ts | 2 +- agenta-web/src/services/project/index.ts | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/agenta-web/src/components/HumanEvaluationModal/HumanEvaluationModal.tsx b/agenta-web/src/components/HumanEvaluationModal/HumanEvaluationModal.tsx index 2383cac732..65ad58c868 100644 --- a/agenta-web/src/components/HumanEvaluationModal/HumanEvaluationModal.tsx +++ b/agenta-web/src/components/HumanEvaluationModal/HumanEvaluationModal.tsx @@ -6,7 +6,7 @@ import {isDemo} from "@/lib/helpers/utils" import {Button, Col, Dropdown, MenuProps, Modal, ModalProps, Row, Spin, message} from "antd" import {getErrorMessage} from "@/lib/helpers/errorHandler" import {EvaluationType} from "@/lib/enums" -import {PERMISSION_ERR_MSG} from "@/lib/helpers/axiosConfig" +import {PERMISSION_ERR_MSG} from "@/lib/api/assets/axiosConfig" import {getAllVariantParameters} from "@/lib/helpers/variantHelper" import {useRouter} from "next/router" import {useAppTheme} from "../Layout/ThemeContextProvider" diff --git a/agenta-web/src/lib/hooks/useVariant.ts b/agenta-web/src/lib/hooks/useVariant.ts index 676fa64899..525f54d6bd 100644 --- a/agenta-web/src/lib/hooks/useVariant.ts +++ b/agenta-web/src/lib/hooks/useVariant.ts @@ -1,7 +1,7 @@ import {useState, useEffect, useRef} from "react" import {Variant, Parameter} from "@/lib/Types" import {getAllVariantParameters, updateInputParams} from "@/lib/helpers/variantHelper" -import {PERMISSION_ERR_MSG} from "../helpers/axiosConfig" +import {PERMISSION_ERR_MSG} from "../api/assets/axiosConfig" import {createNewVariant, fetchVariantLogs, updateVariantParams} from "@/services/playground/api" /** diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 4031e76103..acbf35af51 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -1,5 +1,5 @@ import {getCurrentProject} from "@/contexts/project.context" -import axios from "@/lib//helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import Session from "supertokens-auth-react/recipe/session" import {formatDay} from "@/lib/helpers/dateTimeHelper" import { diff --git a/agenta-web/src/services/app-selector/api/index.ts b/agenta-web/src/services/app-selector/api/index.ts index f7aa7b63d2..fe28fd4550 100644 --- a/agenta-web/src/services/app-selector/api/index.ts +++ b/agenta-web/src/services/app-selector/api/index.ts @@ -1,6 +1,6 @@ import {getCurrentProject} from "@/contexts/project.context" import {AppTemplate} from "@/lib/Types" -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {dynamicContext} from "@/lib/helpers/dynamic" import {LlmProvider} from "@/lib/helpers/llmProviders" import {getAgentaApiUrl} from "@/lib/helpers/utils" diff --git a/agenta-web/src/services/deployment/api/index.ts b/agenta-web/src/services/deployment/api/index.ts index 6a7153c883..97ac167543 100644 --- a/agenta-web/src/services/deployment/api/index.ts +++ b/agenta-web/src/services/deployment/api/index.ts @@ -1,6 +1,6 @@ import {getCurrentProject} from "@/contexts/project.context" import {Environment} from "@/lib/Types" -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {getAgentaApiUrl} from "@/lib/helpers/utils" //Prefix convention: diff --git a/agenta-web/src/services/evaluations/api/index.ts b/agenta-web/src/services/evaluations/api/index.ts index e747856832..13981ae6bb 100644 --- a/agenta-web/src/services/evaluations/api/index.ts +++ b/agenta-web/src/services/evaluations/api/index.ts @@ -1,4 +1,4 @@ -import axios from "@/lib//helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import { ComparisonResultRow, Evaluator, diff --git a/agenta-web/src/services/human-evaluations/api/index.ts b/agenta-web/src/services/human-evaluations/api/index.ts index c4a8cb0e03..1c3e647ed8 100644 --- a/agenta-web/src/services/human-evaluations/api/index.ts +++ b/agenta-web/src/services/human-evaluations/api/index.ts @@ -1,4 +1,4 @@ -import axios from "@/lib//helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import { EvaluationResponseType, Evaluation, diff --git a/agenta-web/src/services/observability/core/index.ts b/agenta-web/src/services/observability/core/index.ts index 40bd3f77c5..714fed4c16 100644 --- a/agenta-web/src/services/observability/core/index.ts +++ b/agenta-web/src/services/observability/core/index.ts @@ -1,6 +1,6 @@ import {getAgentaApiUrl} from "@/lib/helpers/utils" import {_AgentaRootsResponse} from "../types" -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {getCurrentProject} from "@/contexts/project.context" //Prefix convention: diff --git a/agenta-web/src/services/playground/api/index.ts b/agenta-web/src/services/playground/api/index.ts index f04fc7aefb..404392eb4b 100644 --- a/agenta-web/src/services/playground/api/index.ts +++ b/agenta-web/src/services/playground/api/index.ts @@ -1,6 +1,6 @@ import {getCurrentProject} from "@/contexts/project.context" import {Parameter} from "@/lib/Types" -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {getAgentaApiUrl} from "@/lib/helpers/utils" //Prefix convention: diff --git a/agenta-web/src/services/project/index.ts b/agenta-web/src/services/project/index.ts index 95d629d118..cad28a54be 100644 --- a/agenta-web/src/services/project/index.ts +++ b/agenta-web/src/services/project/index.ts @@ -1,4 +1,4 @@ -import axios from "@/lib/helpers/axiosConfig" +import axios from "@/lib/api/assets/axiosConfig" import {getAgentaApiUrl} from "@/lib/helpers/utils" import {ProjectsResponse} from "./types" From 03bc2769a8f4e81b125791856f511302513dde29 Mon Sep 17 00:00:00 2001 From: Arda Erzin Date: Tue, 10 Dec 2024 22:39:21 +0100 Subject: [PATCH 4/4] feat(frontend): update _app to include new swr config provider --- agenta-web/src/pages/_app.tsx | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/agenta-web/src/pages/_app.tsx b/agenta-web/src/pages/_app.tsx index c84846caf9..c3dcd378f0 100644 --- a/agenta-web/src/pages/_app.tsx +++ b/agenta-web/src/pages/_app.tsx @@ -17,6 +17,7 @@ import ProjectContextProvider from "@/contexts/project.context" import "ag-grid-community/styles/ag-grid.css" import "ag-grid-community/styles/ag-theme-alpine.css" import {Inter} from "next/font/google" +import AgSWRConfig from "@/lib/api/SWRConfig" const NoMobilePageWrapper = dynamicComponent("NoMobilePageWrapper/NoMobilePageWrapper") @@ -57,20 +58,22 @@ export default function App({Component, pageProps}: AppProps) {
- - - - - - - - - - - - - - + + + + + + + + + + + + + + + +
)