From 903a5fc1329bd1c72bc45a5c957bca55a2443dbc Mon Sep 17 00:00:00 2001 From: ashrafchowdury Date: Fri, 22 Nov 2024 16:02:48 +0600 Subject: [PATCH 1/6] fix(frontend): prompt fetching code --- .../endpoints/fetch_config/curl.ts | 4 ++-- .../endpoints/fetch_config/python.ts | 17 ++++++++++------ .../endpoints/fetch_config/typescript.ts | 20 +++++++++---------- .../overview/deployments/DeploymentDrawer.tsx | 8 +++++--- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts index 13de78483e..a735d4fedc 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts @@ -1,6 +1,6 @@ -export default function cURLCode(baseId: string, env_name: string): string { +export default function cURLCode(appName: string, env_name: string): string { return ` - curl -X GET "${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/configs?base_id=${baseId}&environment_name=${env_name}" \\ + curl -X GET "${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/variants/configs/fetch?app_name=${appName}&environment_slug=${env_name}" \\ -H "Authorization: Bearer YOUR_API_KEY" \\ -H "Content-Type: application/json" \\ --connect-timeout 60 diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts index 5f56871fa7..2b8bfd4a25 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts @@ -1,12 +1,17 @@ -export default function pythonCode(baseId: string, env_name: string): string { +export default function pythonCode(appName: string, env_name: string): string { return ` # os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud # os.environ["AGENTA_HOST"] = "${process.env.NEXT_PUBLIC_AGENTA_API_URL}" - from agenta import Agenta - ag = Agenta() - ag.get_config(base_id="${baseId}", - environment="${env_name}", - cache_timeout=600) # timeout 300 per default + import agenta as ag + + # ag.init() <- uncomment if you don't already have this + config = ag.ConfigManager.get_from_registry( + app_slug="${appName}", + environment_slug="${env_name}" # choose production, staging, or development + ) + + print("Fetched configuration from staging:") + print(config) ` } diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts index b2f401244e..87b853bab8 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts @@ -1,18 +1,18 @@ import {js as beautify} from "js-beautify" -export default function tsCode(baseId: string, env_name: string): string { +export default function tsCode(appName: string, env_name: string): string { const codeString = ` import axios from 'axios'; - - const getConfig = async (baseId: string, environmentName: string) => { + + const getConfig = async (appName: string, environmentSlug: string) => { try { const baseUrl = '${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api'; const params = { - base_id: baseId, - environment_name: environmentName + app_name: appName, + environment_slug: environmentSlug }; - - const response = await axios.get(baseUrl + "/configs", { + + const response = await axios.get(baseUrl + "/variants/configs/fetch", { params: params, headers: { 'Authorization': "Bearer YOUR_API_KEY", @@ -20,7 +20,7 @@ export default function tsCode(baseId: string, env_name: string): string { }, timeout: 60000 }); - + if (response.status >= 200 && response.status < 300) { return response.data; } else if (response.status === 422) { @@ -37,8 +37,8 @@ export default function tsCode(baseId: string, env_name: string): string { throw error; } }; - - getConfig('${baseId}', '${env_name}').then(console.log).catch(console.error); + + getConfig('${appName}', '${env_name}').then(console.log).catch(console.error); ` const formattedCodeString = beautify(codeString) diff --git a/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx b/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx index 978ec24e91..e80a40e2fc 100644 --- a/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx +++ b/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx @@ -27,6 +27,7 @@ import {useVariant} from "@/lib/hooks/useVariant" import {isDemo} from "@/lib/helpers/utils" import {dynamicComponent} from "@/lib/helpers/dynamic" import VariantPopover from "../variants/VariantPopover" +import {useAppsData} from "@/contexts/app.context" const DeploymentHistoryModal: any = dynamicComponent( "pages/overview/deployments/DeploymentHistoryModal", @@ -124,6 +125,7 @@ const DeploymentDrawer = ({ const classes = useStyles() const router = useRouter() const appId = router.query.app_id as string + const {currentApp} = useAppsData() const [selectedLang, setSelectedLang] = useState("python") const [uri, setURI] = useState(null) const [variant, setVariant] = useState(null) @@ -165,9 +167,9 @@ const DeploymentDrawer = ({ } const fetchConfigCodeSnippet: Record = { - python: fetchConfigpythonCode(variant?.baseId!, selectedEnvironment?.name!), - bash: fetchConfigcURLCode(variant?.baseId!, selectedEnvironment?.name!), - typescript: fetchConfigtsCode(variant?.baseId!, selectedEnvironment?.name!), + python: fetchConfigpythonCode(currentApp?.app_name as string, selectedEnvironment?.name!), + bash: fetchConfigcURLCode(currentApp?.app_name as string, selectedEnvironment?.name!), + typescript: fetchConfigtsCode(currentApp?.app_name as string, selectedEnvironment?.name!), } return ( From 7a6676dab02c403d237482f5914a460210209da6 Mon Sep 17 00:00:00 2001 From: ashrafchowdury Date: Fri, 22 Nov 2024 16:06:23 +0600 Subject: [PATCH 2/6] fix(frontend): improved code structure --- .../pages/overview/deployments/DeploymentDrawer.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx b/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx index e80a40e2fc..bf50521731 100644 --- a/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx +++ b/agenta-web/src/components/pages/overview/deployments/DeploymentDrawer.tsx @@ -167,9 +167,9 @@ const DeploymentDrawer = ({ } const fetchConfigCodeSnippet: Record = { - python: fetchConfigpythonCode(currentApp?.app_name as string, selectedEnvironment?.name!), - bash: fetchConfigcURLCode(currentApp?.app_name as string, selectedEnvironment?.name!), - typescript: fetchConfigtsCode(currentApp?.app_name as string, selectedEnvironment?.name!), + python: fetchConfigpythonCode(currentApp?.app_name!, selectedEnvironment?.name!), + bash: fetchConfigcURLCode(currentApp?.app_name!, selectedEnvironment?.name!), + typescript: fetchConfigtsCode(currentApp?.app_name!, selectedEnvironment?.name!), } return ( From d3f47178d5089637cce6abd0740015129be2adee Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 22 Nov 2024 16:15:51 +0100 Subject: [PATCH 3/6] Update python.ts --- .../endpoints/fetch_config/python.ts | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts index 2b8bfd4a25..6127d947d3 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/python.ts @@ -1,17 +1,16 @@ export default function pythonCode(appName: string, env_name: string): string { return ` - # os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud - # os.environ["AGENTA_HOST"] = "${process.env.NEXT_PUBLIC_AGENTA_API_URL}" +import os +import agenta as ag - import agenta as ag +os.environ["AGENTA_API_KEY"] = "your_api_key" # Only when using cloud +os.environ["AGENTA_HOST"] = "${process.env.NEXT_PUBLIC_AGENTA_API_URL}" - # ag.init() <- uncomment if you don't already have this - config = ag.ConfigManager.get_from_registry( - app_slug="${appName}", - environment_slug="${env_name}" # choose production, staging, or development - ) - - print("Fetched configuration from staging:") - print(config) - ` +ag.init() +config = ag.ConfigManager.get_from_registry( + app_slug="${appName}", + environment_slug="${env_name}" + ) +print(config) +` } From 2e70c4425b7023e602322475314db22ac5e77753 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 22 Nov 2024 16:33:34 +0100 Subject: [PATCH 4/6] Update curl.ts --- .../endpoints/fetch_config/curl.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts index a735d4fedc..0f44c48977 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/curl.ts @@ -1,8 +1,19 @@ export default function cURLCode(appName: string, env_name: string): string { - return ` - curl -X GET "${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/variants/configs/fetch?app_name=${appName}&environment_slug=${env_name}" \\ - -H "Authorization: Bearer YOUR_API_KEY" \\ - -H "Content-Type: application/json" \\ - --connect-timeout 60 - ` + return `curl -L '${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api/variants/configs/fetch' \\ +-H "Authorization: Bearer YOUR_API_KEY" \\ +-H 'Content-Type: application/json' \\ +-H 'Accept: application/json' \\ +-d '{ + "environment_ref": { + "slug": "${env_name}", + "version": null, + "id": null + }, + "application_ref": { + "slug": "${appName}", + "version": null, + "id": null + } +}' +` } From 28aa267023bed68bad331d95ba301a932f414343 Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 22 Nov 2024 16:37:18 +0100 Subject: [PATCH 5/6] Update typescript.ts --- .../endpoints/fetch_config/typescript.ts | 78 +++++++++---------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts index 87b853bab8..b5ebf507be 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts @@ -1,46 +1,40 @@ -import {js as beautify} from "js-beautify" +import { js as beautify } from "js-beautify"; export default function tsCode(appName: string, env_name: string): string { - const codeString = ` - import axios from 'axios'; - - const getConfig = async (appName: string, environmentSlug: string) => { - try { - const baseUrl = '${process.env.NEXT_PUBLIC_AGENTA_API_URL}/api'; - const params = { - app_name: appName, - environment_slug: environmentSlug - }; - - const response = await axios.get(baseUrl + "/variants/configs/fetch", { - params: params, - headers: { - 'Authorization': "Bearer YOUR_API_KEY", - 'Content-Type': 'application/json' - }, - timeout: 60000 - }); - - if (response.status >= 200 && response.status < 300) { - return response.data; - } else if (response.status === 422) { - throw new Error(JSON.stringify(response.data)); - } - } catch (error: any) { - if (error.response) { - console.error("API Error: " + error.response.status, error.response.data); - } else if (error.request) { - console.error('API Error: No response received', error.request); - } else { - console.error('Error', error.message); - } - throw error; - } - }; - - getConfig('${appName}', '${env_name}').then(console.log).catch(console.error); - ` + const codeString = `import axios from 'axios'; - const formattedCodeString = beautify(codeString) - return formattedCodeString +const getConfig = async (appName: string, environmentSlug: string) => { + const baseUrl = 'https://oss.agenta.ai/api/variants/configs/fetch'; + + try { + const response = await axios.post(baseUrl, { + environment_ref: { + slug: environmentSlug, + version: null, + id: null, + }, + application_ref: { + slug: appName, + version: null, + id: null, + }, + }, { + headers: { + 'Authorization': "Bearer YOUR_API_KEY", + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + }); + + return response.data; + } catch { + throw new Error('Failed to fetch configuration.'); + } +}; + +getConfig('demo', 'production').then(console.log).catch(console.error); + `; + + const formattedCodeString = beautify(codeString); + return formattedCodeString; } From d0163a34314ff594cb2c296987ac6db8427c1c2d Mon Sep 17 00:00:00 2001 From: Mahmoud Mabrouk Date: Fri, 22 Nov 2024 16:43:03 +0100 Subject: [PATCH 6/6] format --- .../code_snippets/endpoints/fetch_config/typescript.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts index b5ebf507be..2e46568422 100644 --- a/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts +++ b/agenta-web/src/code_snippets/endpoints/fetch_config/typescript.ts @@ -1,4 +1,4 @@ -import { js as beautify } from "js-beautify"; +import {js as beautify} from "js-beautify" export default function tsCode(appName: string, env_name: string): string { const codeString = `import axios from 'axios'; @@ -33,8 +33,8 @@ const getConfig = async (appName: string, environmentSlug: string) => { }; getConfig('demo', 'production').then(console.log).catch(console.error); - `; + ` - const formattedCodeString = beautify(codeString); - return formattedCodeString; + const formattedCodeString = beautify(codeString) + return formattedCodeString }