diff --git a/agenta-web/src/services/api.ts b/agenta-web/src/services/api.ts index f793af6776..93bdd70a0d 100644 --- a/agenta-web/src/services/api.ts +++ b/agenta-web/src/services/api.ts @@ -67,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 @@ -120,34 +135,41 @@ export async function callVariant( } const appContainerURI = await fetchAppContainerURL(appId, undefined, baseId) + const {projectId} = getCurrentProject() const jwt = await getJWT() - return axios - .post(`${appContainerURI}/generate`, requestBody, { + 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, - headers: { - Authorization: jwt && `Bearer ${jwt}`, - }, } as any) - .then((res) => { - return res.data + .then((response) => { + return response }) -} + .catch(async (error) => { + console.log("Unsecure call to LLM App failed:", error?.status) -/** - * Get the JWT from SuperTokens - */ -const getJWT = async () => { - try { - if (await Session.doesSessionExist()) { - let jwt = await Session.getAccessToken() + 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 jwt - } - } catch (error) {} + return response + }) - return undefined + return response?.data } /** @@ -163,16 +185,40 @@ 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 response = await axios.get(url, { - _ignoreError: ignoreAxiosError, - headers: { - Authorization: jwt && `Bearer ${jwt}`, - }, - } as any) - const isChatVariant = detectChatVariantFromOpenAISchema(response.data) - let APIParams = openAISchemaToParameters(response.data) + + const base_url = `${appContainerURI}/openapi.json` + 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) + .then((response) => { + return response + }) + .catch(async (error) => { + console.log("Unsecure call to LLM App failed:", error?.status) + + let response = await axios + .get(secure_url, { + _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 + }) + + 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) => {