From 3610be566a75c08c66f1b7c304a89a2f12c68743 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 13:26:31 +0100 Subject: [PATCH 1/9] WIP - new request --- agenta-web/src/services/api.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index f793af6776..53c80c1a73 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -120,10 +120,11 @@ export async function callVariant( } const appContainerURI = await fetchAppContainerURL(appId, undefined, baseId) + const {projectId} = getCurrentProject() const jwt = await getJWT() return axios - .post(`${appContainerURI}/generate`, requestBody, { + .post(`${appContainerURI}/generate?project_id=${projectId}`, requestBody, { signal, _ignoreError: ignoreAxiosError, headers: { @@ -163,8 +164,10 @@ export const fetchVariantParametersFromOpenAPI = async ( ignoreAxiosError: boolean = false, ) => { const appContainerURI = await fetchAppContainerURL(appId, variantId, baseId) - const url = `${appContainerURI}/openapi.json` + const {projectId} = getCurrentProject() const jwt = await getJWT() + + const url = `${appContainerURI}/openapi.json?project_id=${projectId}` const response = await axios.get(url, { _ignoreError: ignoreAxiosError, headers: { From 7f46ce991b0beff42a315fcb340d26abce4b1ef7 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 13:30:06 +0100 Subject: [PATCH 2/9] WIP - old request --- agenta-web/src/services/api.ts | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 53c80c1a73..92ebcedd38 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -1,6 +1,5 @@ import {getCurrentProject} from "@/contexts/project.context" import axios from "@/lib//helpers/axiosConfig" -import Session from "supertokens-auth-react/recipe/session" import {formatDay} from "@/lib/helpers/dateTimeHelper" import { detectChatVariantFromOpenAISchema, @@ -120,37 +119,17 @@ export async function callVariant( } const appContainerURI = await fetchAppContainerURL(appId, undefined, baseId) - const {projectId} = getCurrentProject() - const jwt = await getJWT() return axios - .post(`${appContainerURI}/generate?project_id=${projectId}`, requestBody, { + .post(`${appContainerURI}/generate`, requestBody, { signal, _ignoreError: ignoreAxiosError, - headers: { - Authorization: jwt && `Bearer ${jwt}`, - }, } as any) .then((res) => { return res.data }) } -/** - * Get the JWT from SuperTokens - */ -const getJWT = async () => { - try { - if (await Session.doesSessionExist()) { - let jwt = await Session.getAccessToken() - - return jwt - } - } catch (error) {} - - return undefined -} - /** * Parses the openapi.json from a variant and returns the parameters as an array of objects. * @param app @@ -164,15 +143,9 @@ export const fetchVariantParametersFromOpenAPI = async ( ignoreAxiosError: boolean = false, ) => { const appContainerURI = await fetchAppContainerURL(appId, variantId, baseId) - const {projectId} = getCurrentProject() - const jwt = await getJWT() - const url = `${appContainerURI}/openapi.json?project_id=${projectId}` - const response = await axios.get(url, { + const response = await axios.get(`${appContainerURI}/openapi.json`, { _ignoreError: ignoreAxiosError, - headers: { - Authorization: jwt && `Bearer ${jwt}`, - }, } as any) const isChatVariant = detectChatVariantFromOpenAISchema(response.data) let APIParams = openAISchemaToParameters(response.data) From 89a3123c3c5a59342d03a0717f2fd72d072234ca Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 13:26:31 +0100 Subject: [PATCH 3/9] trying to add sequence without and with auth --- agenta-web/src/services/api.ts | 78 ++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 92ebcedd38..d2466f21a2 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -1,5 +1,6 @@ import {getCurrentProject} from "@/contexts/project.context" import axios from "@/lib//helpers/axiosConfig" +import Session from "supertokens-auth-react/recipe/session" import {formatDay} from "@/lib/helpers/dateTimeHelper" import { detectChatVariantFromOpenAISchema, @@ -66,6 +67,21 @@ export async function fetchVariants( return [] } +/** + * Get the JWT from SuperTokens + */ +const getJWT = async () => { + try { + if (await Session.doesSessionExist()) { + let jwt = await Session.getAccessToken() + + return jwt + } + } catch (error) {} + + return undefined +} + /** * Calls the variant endpoint with the input parameters and the optional parameters and returns the response. * @param inputParametersDict A dictionary of the input parameters to be passed to the variant endpoint @@ -119,15 +135,37 @@ export async function callVariant( } const appContainerURI = await fetchAppContainerURL(appId, undefined, baseId) + const {projectId} = getCurrentProject() + const jwt = await getJWT() + + const base_url = `${appContainerURI}/generate` + const secure_url = `${base_url}?project_id=${projectId}` + const secure_headers = {Authorization: jwt && `Bearer ${jwt}`} + + let response = await axios.post(base_url, requestBody, { + signal, + _ignoreError: ignoreAxiosError, + } as any) - return axios - .post(`${appContainerURI}/generate`, requestBody, { + if (response.status === 200) { + // + } else if (response.status === 401) { + response = await axios.post(secure_url, requestBody, { signal, _ignoreError: ignoreAxiosError, + headers: secure_headers, } as any) - .then((res) => { - return res.data - }) + + if (response.status === 200) { + // + } else { + throw new Error("Failed to run /generate") + } + } else { + throw new Error("Failed to run /generate") + } + + return response?.data } /** @@ -143,12 +181,36 @@ export const fetchVariantParametersFromOpenAPI = async ( ignoreAxiosError: boolean = false, ) => { const appContainerURI = await fetchAppContainerURL(appId, variantId, baseId) + const {projectId} = getCurrentProject() + const jwt = await getJWT() + + const base_url = `${appContainerURI}/openapi.json` + const secure_url = `${base_url}?project_id=${projectId}` + const secure_headers = {Authorization: jwt && `Bearer ${jwt}`} - const response = await axios.get(`${appContainerURI}/openapi.json`, { + let response = await axios.get(base_url, { _ignoreError: ignoreAxiosError, } as any) - const isChatVariant = detectChatVariantFromOpenAISchema(response.data) - let APIParams = openAISchemaToParameters(response.data) + + if (response.status === 200) { + // + } else if (response.status === 401) { + response = await axios.get(secure_url, { + _ignoreError: ignoreAxiosError, + headers: secure_headers, + } as any) + + if (response.status === 200) { + // + } else { + throw new Error("Failed to fetch openapi.json") + } + } else { + throw new Error("Failed to fetch openapi.json") + } + + const isChatVariant = detectChatVariantFromOpenAISchema(response?.data) + let APIParams = openAISchemaToParameters(response?.data) // we create a new param for DictInput that will contain the name of the inputs APIParams = APIParams.map((param) => { From 2a823e05e72ebdf619955adde4d6bf3d6dfad5e1 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 17:32:17 +0100 Subject: [PATCH 4/9] add logs --- agenta-web/src/services/api.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index d2466f21a2..018c71c304 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -193,19 +193,24 @@ export const fetchVariantParametersFromOpenAPI = async ( } as any) if (response.status === 200) { - // + console.log("unsecure, 200") } else if (response.status === 401) { + console.log("unsecure, 401") + response = await axios.get(secure_url, { _ignoreError: ignoreAxiosError, headers: secure_headers, } as any) if (response.status === 200) { + console.log("secure, 200") // } else { + console.log("secure, all failed") throw new Error("Failed to fetch openapi.json") } } else { + console.log("unsecure, all failed except 401") throw new Error("Failed to fetch openapi.json") } From d645928810b1640c26aeff8fe3f2b51f97768d98 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 17:33:40 +0100 Subject: [PATCH 5/9] no throw --- agenta-web/src/services/api.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 018c71c304..5fc793cf7d 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -159,10 +159,10 @@ export async function callVariant( if (response.status === 200) { // } else { - throw new Error("Failed to run /generate") + // throw new Error("Failed to run /generate") } } else { - throw new Error("Failed to run /generate") + // throw new Error("Failed to run /generate") } return response?.data @@ -207,11 +207,11 @@ export const fetchVariantParametersFromOpenAPI = async ( // } else { console.log("secure, all failed") - throw new Error("Failed to fetch openapi.json") + // throw new Error("Failed to fetch openapi.json") } } else { console.log("unsecure, all failed except 401") - throw new Error("Failed to fetch openapi.json") + // throw new Error("Failed to fetch openapi.json") } const isChatVariant = detectChatVariantFromOpenAISchema(response?.data) From a4f70b7e910ab79864f2cd79d3d7d7fee365f936 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 18:07:16 +0100 Subject: [PATCH 6/9] chaining axios request --- agenta-web/src/services/api.ts | 51 ++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 5fc793cf7d..cab3f9f698 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -188,30 +188,39 @@ export const fetchVariantParametersFromOpenAPI = async ( const secure_url = `${base_url}?project_id=${projectId}` const secure_headers = {Authorization: jwt && `Bearer ${jwt}`} - let response = await axios.get(base_url, { - _ignoreError: ignoreAxiosError, - } as any) - - if (response.status === 200) { - console.log("unsecure, 200") - } else if (response.status === 401) { - console.log("unsecure, 401") - - response = await axios.get(secure_url, { + let response = await axios + .get(base_url, { _ignoreError: ignoreAxiosError, - headers: secure_headers, } as any) + .then((response) => { + console.log("unsecure, 200") - if (response.status === 200) { - console.log("secure, 200") - // - } else { - console.log("secure, all failed") - // throw new Error("Failed to fetch openapi.json") - } - } else { - console.log("unsecure, all failed except 401") - // throw new Error("Failed to fetch openapi.json") + return response + }) + .catch(async (error) => { + console.log(error) + console.log("unsecure, 401") + + let response = await axios + .get(secure_url, { + _ignoreError: ignoreAxiosError, + headers: secure_headers, + } as any) + .then((response) => { + console.log("secure, 200") + + return response + }) + .catch((error) => { + console.log(error) + console.log("secure, 401") + }) + + return response + }) + + if (response === undefined) { + return } const isChatVariant = detectChatVariantFromOpenAISchema(response?.data) From 62e1a4948ba0ee8927a8152f8f27272dd4601a93 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 18:11:35 +0100 Subject: [PATCH 7/9] ... --- agenta-web/src/services/api.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index cab3f9f698..52baeb17b9 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -219,10 +219,6 @@ export const fetchVariantParametersFromOpenAPI = async ( return response }) - if (response === undefined) { - return - } - const isChatVariant = detectChatVariantFromOpenAISchema(response?.data) let APIParams = openAISchemaToParameters(response?.data) From 7e9c94c1bc04316b5c9dad43044e159571ab9f2e Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 18:32:59 +0100 Subject: [PATCH 8/9] update /generate --- agenta-web/src/services/api.ts | 50 ++++++++++++++++------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 52baeb17b9..37d7b8ddfd 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -142,28 +142,32 @@ export async function callVariant( const secure_url = `${base_url}?project_id=${projectId}` const secure_headers = {Authorization: jwt && `Bearer ${jwt}`} - let response = await axios.post(base_url, requestBody, { - signal, - _ignoreError: ignoreAxiosError, - } as any) - - if (response.status === 200) { - // - } else if (response.status === 401) { - response = await axios.post(secure_url, requestBody, { + let response = await axios + .post(base_url, requestBody, { signal, _ignoreError: ignoreAxiosError, - headers: secure_headers, } as any) + .then((response) => { + return response + }) + .catch(async (error) => { + console.log("Secure call to LLM App failed:", error?.status) - if (response.status === 200) { - // - } else { - // throw new Error("Failed to run /generate") - } - } else { - // throw new Error("Failed to run /generate") - } + let response = await axios + .post(secure_url, requestBody, { + signal, + _ignoreError: ignoreAxiosError, + headers: secure_headers, + } as any) + .then((response) => { + return response + }) + .catch((error) => { + console.log("Secure call to LLM App failed:", error?.status) + }) + + return response + }) return response?.data } @@ -193,13 +197,10 @@ export const fetchVariantParametersFromOpenAPI = async ( _ignoreError: ignoreAxiosError, } as any) .then((response) => { - console.log("unsecure, 200") - return response }) .catch(async (error) => { - console.log(error) - console.log("unsecure, 401") + console.log("Secure call to LLM App failed:", error?.status) let response = await axios .get(secure_url, { @@ -207,13 +208,10 @@ export const fetchVariantParametersFromOpenAPI = async ( headers: secure_headers, } as any) .then((response) => { - console.log("secure, 200") - return response }) .catch((error) => { - console.log(error) - console.log("secure, 401") + console.log("Secure call to LLM App failed:", error?.status) }) return response From 4c7d340774c9c70328b694a1fffebfc72e7daec5 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vega Date: Tue, 26 Nov 2024 18:47:16 +0100 Subject: [PATCH 9/9] fix message for unsecure call --- agenta-web/src/services/api.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index 37d7b8ddfd..93bdd70a0d 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -151,7 +151,7 @@ export async function callVariant( return response }) .catch(async (error) => { - console.log("Secure call to LLM App failed:", error?.status) + console.log("Unsecure call to LLM App failed:", error?.status) let response = await axios .post(secure_url, requestBody, { @@ -200,7 +200,7 @@ export const fetchVariantParametersFromOpenAPI = async ( return response }) .catch(async (error) => { - console.log("Secure call to LLM App failed:", error?.status) + console.log("Unsecure call to LLM App failed:", error?.status) let response = await axios .get(secure_url, {