Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] CORS issue with old SDK #2309

Merged
merged 10 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading