{"version":3,"sources":["../../../src/rerankers/v2/client.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GoogleAuth } from 'google-auth-library';\nimport { extractErrMsg } from '../../common/utils.js';\nimport {\n  RerankRequest,\n  RerankResponse,\n  VertexRerankerClientOptions,\n} from './types.js';\n\nconst DEFAULT_LOCATION = 'us-central1';\n\nexport async function rerankerRank(\n  model: string,\n  request: RerankRequest,\n  clientOptions: VertexRerankerClientOptions\n): Promise<RerankResponse> {\n  const url = getVertexRerankUrl(clientOptions);\n  const token = await getToken(clientOptions.authClient);\n  const headers = {\n    Authorization: `Bearer ${token}`,\n    'x-goog-user-project': clientOptions.projectId,\n    'Content-Type': 'application/json',\n  };\n  const fetchOptions = {\n    method: 'POST',\n    headers: headers,\n    body: JSON.stringify(request),\n  };\n  const response = await makeRequest(url, fetchOptions);\n\n  return response.json() as Promise<RerankResponse>;\n}\n\nexport function getVertexRerankUrl(clientOptions: VertexRerankerClientOptions) {\n  const loc = clientOptions.location || DEFAULT_LOCATION;\n  return `https://discoveryengine.googleapis.com/v1/projects/${clientOptions.projectId}/locations/${loc}/rankingConfigs/default_ranking_config:rank`;\n}\n\nasync function getToken(authClient: GoogleAuth): Promise<string> {\n  const CREDENTIAL_ERROR_MESSAGE =\n    '\\nUnable to authenticate your request\\\n        \\nDepending on your run time environment, you can get authentication by\\\n        \\n- if in local instance or cloud shell: `!gcloud auth login`\\\n        \\n- if in Colab:\\\n        \\n    -`from google.colab import auth`\\\n        \\n    -`auth.authenticate_user()`\\\n        \\n- if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication';\n  const token = await authClient.getAccessToken().catch((e) => {\n    throw new Error(CREDENTIAL_ERROR_MESSAGE, e);\n  });\n  if (!token) {\n    throw new Error(CREDENTIAL_ERROR_MESSAGE);\n  }\n  return token;\n}\n\nasync function makeRequest(\n  url: string,\n  fetchOptions: RequestInit\n): Promise<Response> {\n  try {\n    const response = await fetch(url, fetchOptions);\n    if (!response.ok) {\n      let errorText = await response.text();\n      let errorMessage = errorText;\n      try {\n        const json = JSON.parse(errorText);\n        if (json.error && json.error.message) {\n          errorMessage = json.error.message;\n        }\n      } catch (e) {\n        // Not JSON or expected format, use the raw text\n      }\n      throw new Error(\n        `Error fetching from ${url}: [${response.status} ${response.statusText}] ${errorMessage}`\n      );\n    }\n    return response;\n  } catch (e: unknown) {\n    console.error(e);\n    throw new Error(`Failed to fetch from ${url}: ${extractErrMsg(e)}`);\n  }\n}\n"],"mappings":"AAiBA,SAAS,qBAAqB;AAO9B,MAAM,mBAAmB;AAEzB,eAAsB,aACpB,OACA,SACA,eACyB;AACzB,QAAM,MAAM,mBAAmB,aAAa;AAC5C,QAAM,QAAQ,MAAM,SAAS,cAAc,UAAU;AACrD,QAAM,UAAU;AAAA,IACd,eAAe,UAAU,KAAK;AAAA,IAC9B,uBAAuB,cAAc;AAAA,IACrC,gBAAgB;AAAA,EAClB;AACA,QAAM,eAAe;AAAA,IACnB,QAAQ;AAAA,IACR;AAAA,IACA,MAAM,KAAK,UAAU,OAAO;AAAA,EAC9B;AACA,QAAM,WAAW,MAAM,YAAY,KAAK,YAAY;AAEpD,SAAO,SAAS,KAAK;AACvB;AAEO,SAAS,mBAAmB,eAA4C;AAC7E,QAAM,MAAM,cAAc,YAAY;AACtC,SAAO,sDAAsD,cAAc,SAAS,cAAc,GAAG;AACvG;AAEA,eAAe,SAAS,YAAyC;AAC/D,QAAM,2BACJ;AAOF,QAAM,QAAQ,MAAM,WAAW,eAAe,EAAE,MAAM,CAAC,MAAM;AAC3D,UAAM,IAAI,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