## ----setup, include = FALSE---------------------------------------------------
# The Azure-backed example on this page runs against recorded, credential-free
# fixtures (recorded once with data-raw/record-doc-outputs.R and committed under
# vignettes/foundryr-vs-ellmer/). The ellmer -> foundryR schema conversion is a
# pure-local call and always runs when ellmer is installed, so its real output is
# shown everywhere. Nothing here is fabricated.
fixture_dir <- "foundryr-vs-ellmer"
recording <- nzchar(Sys.getenv("FOUNDRY_RECORD_DOCS"))
have_fixtures <- dir.exists(fixture_dir) && length(list.files(fixture_dir)) > 0
run_api <- requireNamespace("httptest2", quietly = TRUE) &&
  (recording || have_fixtures)
have_ellmer <- requireNamespace("ellmer", quietly = TRUE)

# Attach foundryR before start_vignette(): httptest2 only sources the package's
# inst/httptest2/start-vignette.R (which sets replay placeholders) from attached
# packages.
library(foundryR)

if (run_api) {
  httptest2::start_vignette(fixture_dir)
}

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = run_api
)

## ----library, eval = TRUE-----------------------------------------------------
library(foundryR)

## ----ellmer-interop, eval = have_ellmer---------------------------------------
library(ellmer)

# Describe the structure you want with ellmer's type system.
sentiment_spec <- type_object(
  sentiment = type_enum(
    c("positive", "negative", "neutral"),
    description = "Overall sentiment of the response."
  ),
  theme = type_string("A short theme label for the response.")
)

# Convert it into a foundryR schema for strict extraction.
sentiment_schema <- as_foundry_schema(sentiment_spec)
str(sentiment_schema)

## ----extraction-example, eval = run_api && have_ellmer------------------------
foundry_extract(
  c("The lesson was clear.", "I wanted more examples."),
  schema = sentiment_schema
)

## ----cleanup, include = FALSE-------------------------------------------------
if (run_api) {
  httptest2::end_vignette()
}

