diff --git a/R/pal-add-remove.R b/R/pal-add-remove.R index 5f5d6e1..f52393d 100644 --- a/R/pal-add-remove.R +++ b/R/pal-add-remove.R @@ -54,6 +54,13 @@ pal_remove <- function(role) { pal_env(), c(paste0("system_prompt_", role), paste0("rs_pal_", role)) ) + + + if (paste0(".last_pal_", role) %in% names(pal_env())) { + env_unbind(pal_env(), paste0(".last_pal_", role)) + } + + invisible() } supported_interfaces <- c("replace", "prefix", "suffix") diff --git a/tests/testthat/_snaps/pal-add-remove.md b/tests/testthat/_snaps/pal-add-remove.md new file mode 100644 index 0000000..e2da737 --- /dev/null +++ b/tests/testthat/_snaps/pal-add-remove.md @@ -0,0 +1,8 @@ +# pal addition and removal works + + Code + pal_boopery + Message + + -- A boopery pal using claude-3-5-sonnet-20240620. + diff --git a/tests/testthat/test-pal-add-remove.R b/tests/testthat/test-pal-add-remove.R new file mode 100644 index 0000000..fa52d79 --- /dev/null +++ b/tests/testthat/test-pal-add-remove.R @@ -0,0 +1,26 @@ +test_that("pal addition and removal works", { + skip_if(identical(Sys.getenv("ANTHROPIC_API_KEY"), "")) + skip_if_not_installed("withr") + withr::local_options(.pal_fn = NULL, .pal_args = NULL) + + 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())) + + pal_boopery <- pal("boopery") + expect_snapshot(pal_boopery) + expect_true(".last_pal_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())) +})