Skip to content

Commit

Permalink
check + document role more thoroughly (closes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 15, 2024
1 parent fa8d029 commit 96cde35
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion R/init-pal.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
...,
.ns = "elmer"
) {
check_string(role, allow_empty = FALSE)
check_role(role)
if (!role %in% list_pals()) {
cli::cli_abort(c(
"No pals with role {.arg {role}} registered.",
Expand Down
7 changes: 2 additions & 5 deletions R/pal-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' The pals created by those functions will be persistent across sessions.
#'
#' @param role A single string giving a descriptor of the pal's functionality.
# TODO: actually do this once elmer implements
#' Cand only contain letters and numbers.
#' @param prompt A single string giving the system prompt. In most cases, this
#' is a rather long string, containing several newlines.
# TODO: only add prefix when not supplied one
Expand All @@ -31,12 +31,9 @@
prompt = NULL,
interface = c("replace", "prefix", "suffix")
) {
# TODO: need to check that there are no spaces (or things that can't be
# included in a variable name)
check_string(role, allow_empty = FALSE)
check_role(role)
check_string(prompt)

# TODO: make this an elmer interpolate or an .md file
.stash_prompt(prompt, role)
parse_interface(interface, role)

Expand Down
2 changes: 1 addition & 1 deletion R/prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
#' @rdname prompt
#' @export
prompt_new <- function(role, interface, contents = NULL) {
check_string(role)
check_role(role)
arg_match0(interface, supported_interfaces)
check_string(contents, allow_null = TRUE)

Expand Down
18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,23 @@ list_pals <- function() {
gsub(".pal_prompt_", "", prompt_names)
}

# ad-hoc check helpers -------
check_role <- function(role, call = caller_env()) {
check_string(role, allow_empty = FALSE, call = call)

if (!is_valid_role(role)) {
cli::cli_abort(
"{.arg role} must be a single string containing only letters and digits.",
call = call
)
}

invisible(role)
}

# miscellaneous ----------------------------------------------------------------
interactive <- NULL

is_valid_role <- function(role) {
grepl("^[a-zA-Z0-9]+$", role)
}
3 changes: 2 additions & 1 deletion man/pal_add_remove.Rd

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

3 changes: 2 additions & 1 deletion man/prompt.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/pal-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
---

Code
.init_pal("beep bop boop")
.init_pal("beepBopBoop")
Condition
Error in `.init_pal()`:
! No pals with role `beep bop boop` registered.
! No pals with role `beepBopBoop` registered.
i See `.pal_add()`.

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@
-- A cli pal using gpt-4o-mini.

# role checks error informatively

Code
check_role("hey there")
Condition
Error:
! `role` must be a single string containing only letters and digits.

---

Code
check_role(identity)
Condition
Error:
! `role` must be a single string, not a function.

2 changes: 1 addition & 1 deletion tests/testthat/test-pal-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ test_that("can use other models", {
test_that("errors informatively with bad role", {
expect_snapshot(.init_pal(), error = TRUE)
expect_snapshot(.init_pal(NULL), error = TRUE)
expect_snapshot(.init_pal("beep bop boop"), error = TRUE)
expect_snapshot(.init_pal("beepBopBoop"), error = TRUE)
})
23 changes: 23 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,26 @@ test_that(".pal_last is up to date with most recent pal", {
.init_pal("cli", "chat_openai", model = "gpt-4o-mini")
expect_snapshot(env_get(pal_env(), ".pal_last"))
})


test_that("role checks error informatively", {
expect_snapshot(error = TRUE, check_role("hey there"))
expect_snapshot(error = TRUE, check_role(identity))
})

test_that("is_valid_role works", {
expect_true(is_valid_role("role"))
expect_true(is_valid_role("newRole"))
expect_true(is_valid_role("role123"))
expect_true(is_valid_role("ROLE"))
expect_true(is_valid_role("r"))
expect_true(is_valid_role("1"))

expect_false(is_valid_role("new role"))
expect_false(is_valid_role("new@role"))
expect_false(is_valid_role("role_"))
expect_false(is_valid_role("role-"))
expect_false(is_valid_role("new-role"))
expect_false(is_valid_role("role\n123"))
expect_false(is_valid_role(" "))
})

0 comments on commit 96cde35

Please sign in to comment.