Skip to content

Commit

Permalink
use common prefix for pal_env() objects
Browse files Browse the repository at this point in the history
`.last_pal_` ->  `.pal_last_`
`rs_pal_` -> `.pal_rs_`
`system_prompt_` -> `.pal_prompt_`
  • Loading branch information
simonpcouch committed Oct 10, 2024
1 parent cd09117 commit 8dd6010
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions R/gadget.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.pal <- function() {
pal_fn <- .pal_app()
if (is.null(pal_fn) || identical(pal_fn, "rs_pal_")) {
if (is.null(pal_fn) || identical(pal_fn, ".pal_rs__")) {
return(NULL)
}
try_fetch(
Expand Down Expand Up @@ -39,7 +39,7 @@
server = TRUE
)
shiny::observeEvent(input$done, {
shiny::stopApp(returnValue = paste0("rs_pal_", input$pal))
shiny::stopApp(returnValue = paste0(".pal_rs__", input$pal))
})
shiny::onStop(function() {
shiny::stopApp(returnValue = NULL)
Expand Down
8 changes: 4 additions & 4 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ pal_remove <- function(role) {

env_unbind(
pal_env(),
c(paste0("system_prompt_", role), paste0("rs_pal_", role))
c(paste0(".pal_prompt_", role), paste0(".pal_rs__", role))
)

if (paste0(".last_pal_", role) %in% names(pal_env())) {
env_unbind(pal_env(), paste0(".last_pal_", role))
if (paste0(".pal_last_", role) %in% names(pal_env())) {
env_unbind(pal_env(), paste0(".pal_last_", role))
}

invisible()
Expand Down Expand Up @@ -96,5 +96,5 @@ parse_interface <- function(interface, role, call = caller_env()) {
}
)

paste0("rs_pal_", role)
paste0(".pal_rs__", role)
}
2 changes: 1 addition & 1 deletion R/pal-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pal <- R6::R6Class(
args <- modifyList(default_args, args)

args$system_prompt <- get(
paste0("system_prompt_", role),
paste0(".pal_prompt_", role),
envir = search_envs()[["pkg:pal"]]
)

Expand Down
2 changes: 1 addition & 1 deletion R/pal-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## usethis namespace: end
NULL

utils::globalVariables(c(".last_pal", "modifyList"))
utils::globalVariables(c(".pal_last", "modifyList"))

# address "not imported from" R CMD check error
#' @importFrom elmer content_image_file
Expand Down
2 changes: 1 addition & 1 deletion R/pal.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#'
#' @details
#' Upon successfully creating a pal, this function will assign the
#' result to the search path as `.last_pal`.
#' result to the search path as `.pal_last`.
#'
#' If you have an Anthropic API key (or another API key and the `pal_*()`
#' options) set and this package installed, you are ready to using the add-in
Expand Down
14 changes: 7 additions & 7 deletions R/rstudioapi.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# replace selection with refactored code
rs_replace_selection <- function(context, role) {
# check if pal exists
if (exists(paste0(".last_pal_", role))) {
pal <- get(paste0(".last_pal_", role))
if (exists(paste0(".pal_last_", role))) {
pal <- get(paste0(".pal_last_", role))
} else {
tryCatch(
pal <- pal(role),
Expand Down Expand Up @@ -114,8 +114,8 @@ stream_selection <- function(selection, context, pal, n_lines_orig) {
# prefix selection with new code -----------------------------------------------
rs_prefix_selection <- function(context, role) {
# check if pal exists
if (exists(paste0(".last_pal_", role))) {
pal <- get(paste0(".last_pal_", role))
if (exists(paste0(".pal_last_", role))) {
pal <- get(paste0(".pal_last_", role))
} else {
tryCatch(
pal <- pal(role),
Expand Down Expand Up @@ -154,14 +154,14 @@ rs_prefix_selection <- function(context, role) {
}

# pal-specific helpers ---------------------------------------------------------
rs_pal_cli <- function(context = rstudioapi::getActiveDocumentContext()) {
.pal_rs__cli <- function(context = rstudioapi::getActiveDocumentContext()) {
rs_replace_selection(context = context, role = "cli")
}

rs_pal_testthat <- function(context = rstudioapi::getActiveDocumentContext()) {
.pal_rs__testthat <- function(context = rstudioapi::getActiveDocumentContext()) {
rs_replace_selection(context = context, role = "testthat")
}

rs_pal_roxygen <- function(context = rstudioapi::getActiveDocumentContext()) {
.pal_rs__roxygen <- function(context = rstudioapi::getActiveDocumentContext()) {
rs_prefix_selection(context = context, role = "roxygen")
}
12 changes: 6 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# helpers for the pal environment ----------------------------------------------
.stash_last_pal <- function(x) {
pal_env <- pal_env()
pal_env[[paste0(".last_pal_", x$role)]] <- x
pal_env[[".last_pal"]] <- x
pal_env[[paste0(".pal_last_", x$role)]] <- x
pal_env[[".pal_last"]] <- x
invisible(NULL)
}

.stash_binding <- function(role, fn) {
pal_env <- pal_env()
pal_env[[paste0("rs_pal_", role)]] <- fn
pal_env[[paste0(".pal_rs__", role)]] <- fn
invisible(NULL)
}

.stash_prompt <- function(prompt, role) {
pal_env <- pal_env()
pal_env[[paste0("system_prompt_", role)]] <- prompt
pal_env[[paste0(".pal_prompt_", role)]] <- prompt
invisible(NULL)
}

Expand All @@ -31,8 +31,8 @@ pal_env <- function() {
list_pals <- function() {
pal_env <- pal_env()
pal_env_names <- names(pal_env)
prompt_names <- grep("system_prompt_", names(pal_env), value = TRUE)
gsub("system_prompt_", "", prompt_names)
prompt_names <- grep(".pal_prompt_", names(pal_env), value = TRUE)
gsub(".pal_prompt_", "", prompt_names)
}

# ad-hoc check functions -------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion man/pal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# .last_pal is up to date with most recent pal
# .pal_last is up to date with most recent pal

Code
.last_pal
.pal_last
Message
-- A cli pal using claude-3-5-sonnet-20240620.

---

Code
.last_pal_cli
.pal_last_cli
Message
-- A cli pal using claude-3-5-sonnet-20240620.

---

Code
.last_pal
.pal_last
Message
-- A cli pal using gpt-4o-mini.
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ test_that("pal addition and removal works", {
boop_prompt <- "just reply with beep bop boop regardless of input"
pal_add("boopery", boop_prompt)

expect_equal(system_prompt_boopery, boop_prompt)
expect_true(is_function(rs_pal_boopery))
expect_true("system_prompt_boopery" %in% names(pal_env()))
expect_true("rs_pal_boopery" %in% names(pal_env()))
expect_equal(.pal_prompt_boopery, boop_prompt)
expect_true(is_function(.pal_rs__boopery))
expect_true(".pal_prompt_boopery" %in% names(pal_env()))
expect_true(".pal_rs__boopery" %in% names(pal_env()))

pal_boopery <- pal("boopery")
expect_snapshot(pal_boopery)
expect_true(".last_pal_boopery" %in% names(pal_env()))
expect_true(".pal_last_boopery" %in% names(pal_env()))

res <- pal_boopery$chat("hey there")
expect_true(grepl("bop", res))

pal_remove("boopery")

expect_false(".last_pal_boopery" %in% names(pal_env()))
expect_false("system_prompt_boopery" %in% names(pal_env()))
expect_false("rs_pal_boopery" %in% names(pal_env()))
expect_false(".pal_last_boopery" %in% names(pal_env()))
expect_false(".pal_prompt_boopery" %in% names(pal_env()))
expect_false(".pal_rs__boopery" %in% names(pal_env()))
})

test_that("pal addition with bad inputs", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
test_that(".last_pal is up to date with most recent pal", {
test_that(".pal_last is up to date with most recent pal", {
skip_if(identical(Sys.getenv("ANTHROPIC_API_KEY"), ""))
skip_if(identical(Sys.getenv("OPENAI_API_KEY"), ""))
skip_if_not_installed("withr")
withr::local_options(.pal_fn = NULL, .pal_args = NULL)

pal("cli")
expect_snapshot(.last_pal)
expect_snapshot(.last_pal_cli)
expect_snapshot(.pal_last)
expect_snapshot(.pal_last_cli)

pal("cli", "chat_openai", model = "gpt-4o-mini")
expect_snapshot(.last_pal)
expect_snapshot(.pal_last)
})

0 comments on commit 8dd6010

Please sign in to comment.