## ----setup, include = FALSE---------------------------------------------------
fixture_dir <- "responses-api"
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)

# 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
)

## ----basic-response-----------------------------------------------------------
library(foundryR)

foundry_response(
  "Answer in one sentence: what is retrieval-augmented generation?"
)

## ----stateful-turns-----------------------------------------------------------
first <- foundry_response(
  "Define catastrophic forgetting in one sentence."
)

second <- foundry_response(
  "Explain it for a college freshman in one sentence.",
  previous_response_id = first$response_id
)

second$output_text

## ----structured-extraction----------------------------------------------------
schema <- list(
  type = "object",
  properties = list(
    sentiment = list(
      type = "string",
      enum = c("positive", "negative", "neutral")
    ),
    entities = list(
      type = "array",
      items = list(type = "string")
    ),
    summary = list(type = "string")
  ),
  required = c("sentiment", "entities", "summary"),
  additionalProperties = FALSE
)

texts <- c(
  "The new data pipeline reduced manual coding time by half.",
  "Participants reported confusion about the consent form."
)

foundry_extract(
  texts,
  schema = schema
)

## ----function-tools, eval = FALSE---------------------------------------------
# get_weather <- function(location) {
#   list(location = location, temperature = "70 F")
# }
# 
# weather_tool <- foundry_tool(
#   get_weather,
#   description = "Get weather for a location",
#   parameters = list(
#     type = "object",
#     properties = list(location = list(type = "string")),
#     required = "location"
#   )
# )
# 
# turns <- foundry_agent(
#   "What is the weather in San Francisco?",
#   tools = list(weather_tool),
#   max_iterations = 4
# )
# 
# turns[, c("iteration", "final", "output_text")]
# turns$tool_results[[1]]

## ----mcp-tool, eval = FALSE---------------------------------------------------
# mcp_tool <- list(
#   type = "mcp",
#   server_label = "my_mcp_server",
#   server_url = Sys.getenv("MY_MCP_SERVER_URL"),
#   require_approval = "never"
# )
# 
# foundry_response(
#   "Use the MCP server if it helps answer the question.",
#   tools = list(mcp_tool)
# )

## ----web-search, eval = FALSE-------------------------------------------------
# answer <- foundry_web_search(
#   "What changed recently in Azure AI Foundry Responses API?",
#   search_context_size = "high"
# )
# 
# answer$output_text
# answer$citations[[1]]
# answer$tool_calls[[1]]

## ----web-search-location, eval = FALSE----------------------------------------
# foundry_web_search(
#   "Find a recent AI research event near me.",
#   country = "US",
#   region = "Washington",
#   city = "Seattle",
#   timezone = "America/Los_Angeles"
# )

## ----reasoning, eval = FALSE--------------------------------------------------
# foundry_response(
#   "Compare the two arguments and identify the weaker premise.",
#   model = "my-reasoning-deployment",
#   reasoning_effort = "medium"
# )

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

