Skip to contents

Evaluates file top-level expression by expression in envir, like source(). An expression that names peter() or gptr_coach(), or calls a harness bound in envir, keeps its source references, which locate the call; the others run without them, so the functions and calls they make hold no part of the document, as at the console. A peter() call whose block is fresh replays without calling a model and the block's code then runs as ordinary R. A call whose block is stale (its prompt or the values interpolated into it changed), or every call under replay = "live" (an undone block runs without being rewritten; replay = "record" regenerates it), asks the model again and rewrites its block in place, and the old block is skipped, which base source() cannot do. Only that block is regenerated: a later turn of the same session keeps its block, answered in the old turn's context, so re-ask it with replay = "live" on that call when it depends on the turn that changed. Only blank lines may separate a call from its blocks (see gptr_doc()). A console turn is asked again with what the console sent with it, from its block's #< lines (the @file heads and ! notes; see gptr_doc()): the request is the same even when the file changed or is gone. It is asked with the model and in the mode its statement names, else those its session has from the turns before, else the model and mode settings of the session that re-asks: a statement names them only where they differed from what it gets without them (see gptr_doc()). A named model must be available there; model asks a peter() or harness call with another one. A re-ask does not send the ## Steer: and ## Follow-up: lines of a turn or the text of an approval answered n <text>, and the new block does not keep them. Direct R lines a transcript turned off are comments and do not run. A block is written only with write consent (see gptr_doc()); a call that cannot be recorded still runs, and under replay = "record" a notice says that gptr did not record it.

Usage

gptr_source(
  file,
  replay = getOption("gptr.replay", "auto"),
  envir = parent.frame(),
  echo = FALSE,
  model = NULL
)

Arguments

file

Path of an .R document.

replay

Replay mode for the calls in the file: "auto", "replay", "live" or "record". A call's own replay = argument wins. Left missing, each call resolves its mode as peter() does: the gptr.replay option, then the GPTR_REPLAY environment variable, then the replay setting, then "auto".

envir

Environment in which the expressions are evaluated.

echo

TRUE prints each expression before it is evaluated.

model

NULL, or a model name or reference string ("sonnet", "anthropic/claude-sonnet-5-5", or one of a provider you registered), not a provider spec object: every peter() or harness call the file asks again (each one under replay = "live") uses it instead of the model it names or its session has, so a recorded analysis can be re-asked with your own model. A gptr_coach() call and a team or fan-out call (agents =, parallel =) keep the models they name. A block that replays asks no model.

Value

Invisibly, the gptr_blocks listing of the file after the run with an extra column action: replayed, regenerated, ran, skipped, or NA for blocks no call touched. A stale block whose call ran but could not be rewritten (for example without write consent) is ran and keeps its status.

Details

Replay executes the recorded R code, including its ordinary side effects. It does not freeze package versions, supply external inputs or validate the analysis. This function accepts .R scripts; render .Rmd and .qmd with their report tools, and execute .ipynb through its R kernel. Use gptr_blocks() to inspect any supported format.

Examples

f = tempfile(fileext = ".R")
writeLines(c("x = 1", "y = x + 1"), f)
gptr_source(f, replay = "replay", envir = new.env())