Skip to content

Commit

Permalink
Merge pull request #2309 from Agenta-AI/fix/cors-issue-old-sdk
Browse files Browse the repository at this point in the history
 [Fix] CORS issue with old SDK
  • Loading branch information
jp-agenta authored Nov 26, 2024
2 parents 3ab6204 + 4c7d340 commit 960fd78
Showing 1 changed file with 74 additions and 28 deletions.
102 changes: 74 additions & 28 deletions agenta-web/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

/**
Expand All @@ -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) => {
Expand Down

0 comments on commit 960fd78

Please sign in to comment.