## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>",
  eval = identical(tolower(Sys.getenv("LLMRAGENT_RUN_VIGNETTES", "false")), "true")
)

## ----setup--------------------------------------------------------------------
# library(LLMRagent)
# cfg <- LLMR::llm_config("groq", "openai/gpt-oss-20b", temperature = 0.3)

## ----linear-------------------------------------------------------------------
# wf <- agent_workflow("draft_then_polish") |>
#   add_node("draft", function(state) {
#     a <- agent("Drafter", cfg)
#     state$draft <- a$reply(paste("Draft two sentences on:", state$topic))
#     state
#   }) |>
#   add_node("polish", function(state) {
#     a <- agent("Editor", cfg)
#     state$final <- a$reply(paste("Tighten this to one sentence:", state$draft))
#     state
#   }) |>
#   add_edge("draft", "polish")
# 
# run <- run_workflow(wf, input = list(topic = "why peer review is slow"))
# run$state$final

## ----loop---------------------------------------------------------------------
# wf2 <- agent_workflow("until_short_enough") |>
#   add_node("write", function(state) {
#     a <- agent("Writer", cfg)
#     state$tries <- (state$tries %||% 0L) + 1L
#     state$text <- a$reply(paste0("Write a one-line summary of: ", state$topic,
#                                  " (attempt ", state$tries, ")"))
#     state
#   }) |>
#   add_node("done", function(state) state) |>
#   add_edge("write", "write", when = function(state) nchar(state$text) > 120 && state$tries < 3) |>
#   add_edge("write", "done", when = function(state) nchar(state$text) <= 120 || state$tries >= 3)
# 
# run2 <- run_workflow(wf2, input = list(topic = "the history of the printing press"),
#                      max_steps = 10)
# run2$state$tries   # how many attempts it took

## ----resume-------------------------------------------------------------------
# dir <- file.path(tempdir(), "long_run")
# run3 <- run_workflow(wf, input = list(topic = "open access publishing"),
#                      checkpoint_dir = dir)
# # had it failed, resume_workflow(run3, wf) would continue from the last good node

## ----replay-------------------------------------------------------------------
# rp <- replay_run(run, wf, verify = "structural")
# rp$steps[, c("node", "replay_match")]

