Skip to content

Commit

Permalink
relocate pal_env to package namespace (closes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 11, 2024
1 parent 6a7149d commit f9ce6a1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 21 deletions.
3 changes: 1 addition & 2 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@

supported_interfaces <- c("replace", "prefix", "suffix")

# given an interface and role, attaches a function binding in pal's
# additional search env
# given an interface and role, attaches a function binding in pal's `.pal_env`
parse_interface <- function(interface, role, call = caller_env()) {
if (isTRUE(identical(interface, supported_interfaces))) {
interface <- interface[1]
Expand Down
2 changes: 1 addition & 1 deletion R/pal-addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# call the binding associated with the chosen pal
try_fetch(
do.call(pal_fn, args = list()),
do.call(env_get(pal_env(), pal_fn), args = list()),
error = function(e) {
cli::cli_abort("Unable to locate the requested pal.")
}
Expand Down
2 changes: 1 addition & 1 deletion R/pal-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Pal <- R6::R6Class(

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

Chat <- rlang::eval_bare(rlang::call2(fn, !!!args, .ns = .ns))
Expand Down
10 changes: 3 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
invisible(NULL)
}

.pal_env <- new_environment()

pal_env <- function() {
if (!"pkg:pal" %in% search()) {
do.call(
"attach",
list(new.env(), pos = length(search()), name = "pkg:pal")
)
}
as.environment("pkg:pal")
.pal_env
}

list_pals <- function() {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# .pal_last is up to date with most recent pal

Code
.pal_last
env_get(pal_env(), ".pal_last")
Message
-- A cli pal using claude-3-5-sonnet-20240620.

---

Code
.pal_last_cli
env_get(pal_env(), ".pal_last_cli")
Message
-- A cli pal using claude-3-5-sonnet-20240620.

---

Code
.pal_last
env_get(pal_env(), ".pal_last")
Message
-- A cli pal using gpt-4o-mini.
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test-pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ 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(.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()))
expect_equal(env_get(pal_env(), ".pal_prompt_boopery"), boop_prompt)
expect_true(is_function(env_get(pal_env(), ".pal_rs_boopery")))

pal_boopery <- .pal_init("boopery")
expect_snapshot(pal_boopery)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ test_that(".pal_last is up to date with most recent pal", {
withr::local_options(.pal_fn = NULL, .pal_args = NULL)

.pal_init("cli")
expect_snapshot(.pal_last)
expect_snapshot(.pal_last_cli)
expect_snapshot(env_get(pal_env(), ".pal_last"))
expect_snapshot(env_get(pal_env(), ".pal_last_cli"))

.pal_init("cli", "chat_openai", model = "gpt-4o-mini")
expect_snapshot(.pal_last)
expect_snapshot(env_get(pal_env(), ".pal_last"))
})

0 comments on commit f9ce6a1

Please sign in to comment.