R/config.R

Defines functions foundry_get_api_version foundry_get_project_endpoint foundry_set_project_endpoint foundry_get_endpoint foundry_set_endpoint

Documented in foundry_get_api_version foundry_get_endpoint foundry_get_project_endpoint foundry_set_endpoint foundry_set_project_endpoint

#' Set Azure AI Foundry Endpoint
#'
#' Set the base endpoint URL for your Azure AI Foundry resource.
#'
#' @param endpoint Character string containing the endpoint URL.
#'   Example: the endpoint URL from your Foundry resource.
#' @param store Logical. If `TRUE`, stores the endpoint in foundryR's
#'   package-specific user configuration file. Default: `FALSE`.
#'
#' @return Invisibly returns TRUE if endpoint was set successfully.
#' @export
#'
#' @examplesIf requireNamespace("withr", quietly = TRUE)
#' withr::with_envvar(c(AZURE_FOUNDRY_ENDPOINT = NA_character_), {
#'   foundry_set_endpoint("https://example.openai.azure.com")
#'   foundry_get_endpoint()
#' })
#'
#' local({
#'   config_file <- tempfile("foundryR-config-", fileext = ".json")
#'   on.exit(unlink(config_file))
#'   withr::with_options(list(foundryR.config_file = config_file), {
#'     withr::with_envvar(c(AZURE_FOUNDRY_ENDPOINT = NA_character_), {
#'       foundry_set_endpoint("https://example.openai.azure.com", store = TRUE)
#'     })
#'   })
#' })
foundry_set_endpoint <- function(endpoint, store = FALSE) {

  if (missing(endpoint) || is.null(endpoint) || endpoint == "") {
    cli::cli_abort("Endpoint URL is required.")
  }

  # Remove trailing slash if present
  endpoint <- sub("/$", "", endpoint)

  # Set for current session
  Sys.setenv(AZURE_FOUNDRY_ENDPOINT = endpoint)
  cli::cli_alert_success("Endpoint set to {.url {endpoint}}")

  if (store) {
    path <- foundry_store_setting("AZURE_FOUNDRY_ENDPOINT", endpoint)
    cli::cli_alert_success("Endpoint stored in {.file {path}}")
  }

  invisible(TRUE)
}


#' Get Azure AI Foundry Endpoint
#'
#' Retrieve the endpoint URL from the environment or a provided value.
#'
#' @param endpoint Character. Optional endpoint to use instead of environment variable.
#' @param required Logical. If TRUE, throws an error when no endpoint is found.
#'
#' @return The endpoint URL string, or NULL if not found and not required.
#' @export
#'
#' @examples
#' foundry_get_endpoint("https://example.openai.azure.com/")
foundry_get_endpoint <- function(endpoint = NULL, required = FALSE) {
  if (is.null(endpoint)) {
    endpoint <- Sys.getenv("AZURE_FOUNDRY_ENDPOINT")
    if (endpoint == "") {
      endpoint <- foundry_get_stored_setting("AZURE_FOUNDRY_ENDPOINT")
    }
  }

  # Remove trailing slash if present
  if (!is.null(endpoint)) {
    endpoint <- sub("/$", "", endpoint)
  }

  if (required && is.null(endpoint)) {
    cli::cli_abort(c(
      "Azure AI Foundry endpoint is required.",
      "i" = "Set one with {.code foundry_set_endpoint()} or set the {.envvar AZURE_FOUNDRY_ENDPOINT} environment variable."
    ))
  }

  endpoint
}


#' Set Azure AI Foundry project endpoint
#'
#' Set the project endpoint used by project-scoped Foundry APIs such as Azure
#' evaluators and Agent Service operations. Prefer copying the full endpoint
#' from the Foundry portal because Azure's project endpoint shape can vary by
#' service generation.
#'
#' @param endpoint Character string containing the project endpoint URL.
#' @param store Logical. If `TRUE`, stores the endpoint in foundryR's
#'   package-specific user configuration file.
#'
#' @return Invisibly returns `TRUE` if the endpoint was set successfully.
#' @export
#'
#' @examplesIf requireNamespace("withr", quietly = TRUE)
#' withr::with_envvar(c(AZURE_FOUNDRY_PROJECT_ENDPOINT = NA_character_), {
#'   foundry_set_project_endpoint(
#'     "https://example.services.ai.azure.com/api/projects/demo"
#'   )
#'   foundry_get_project_endpoint()
#' })
foundry_set_project_endpoint <- function(endpoint, store = FALSE) {
  if (missing(endpoint) || is.null(endpoint) || endpoint == "") {
    cli::cli_abort("Project endpoint URL is required.")
  }

  endpoint <- sub("/$", "", endpoint)
  Sys.setenv(AZURE_FOUNDRY_PROJECT_ENDPOINT = endpoint)
  cli::cli_alert_success("Project endpoint set to {.url {endpoint}}")

  if (store) {
    path <- foundry_store_setting("AZURE_FOUNDRY_PROJECT_ENDPOINT", endpoint)
    cli::cli_alert_success("Project endpoint stored in {.file {path}}")
  }

  invisible(TRUE)
}


#' Get Azure AI Foundry project endpoint
#'
#' Retrieve the project endpoint URL from the environment or a provided value.
#'
#' @param endpoint Character. Optional endpoint to use instead of
#'   `AZURE_FOUNDRY_PROJECT_ENDPOINT`.
#' @param required Logical. If `TRUE`, throws an error when no endpoint is found.
#'
#' @return The project endpoint URL string, or `NULL`.
#' @export
#'
#' @examples
#' foundry_get_project_endpoint(
#'   "https://example.services.ai.azure.com/api/projects/demo"
#' )
foundry_get_project_endpoint <- function(endpoint = NULL, required = FALSE) {
  if (is.null(endpoint)) {
    endpoint <- Sys.getenv("AZURE_FOUNDRY_PROJECT_ENDPOINT")
    if (endpoint == "") {
      endpoint <- foundry_get_stored_setting("AZURE_FOUNDRY_PROJECT_ENDPOINT")
    }
  }

  if (!is.null(endpoint)) {
    endpoint <- sub("/$", "", endpoint)
  }

  if (required && is.null(endpoint)) {
    cli::cli_abort(c(
      "Azure AI Foundry project endpoint is required.",
      "i" = "Set one with {.code foundry_set_project_endpoint()} or set the {.envvar AZURE_FOUNDRY_PROJECT_ENDPOINT} environment variable."
    ))
  }

  endpoint
}


#' Get API Version
#'
#' Retrieve the API version to use for requests.
#'
#' @param api_version Character. Optional version to use instead of default.
#'
#' @return The API version string.
#' @keywords internal
foundry_get_api_version <- function(api_version = NULL) {
  if (is.null(api_version)) {
    api_version <- Sys.getenv("AZURE_FOUNDRY_API_VERSION", "2024-10-21")
    if (!nzchar(api_version)) api_version <- "2024-10-21"
  }
  api_version
}

Try the foundryR package in your browser

Any scripts or data that you put into this service are public.

foundryR documentation built on Sept. 25, 2026, 1:10 a.m.