Skip to content

Commit

Permalink
align $chat() input type (closes #67(
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Nov 25, 2024
1 parent 93eb90c commit 2e4863c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 22 deletions.
14 changes: 2 additions & 12 deletions R/pal-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ Pal <- R6::R6Class(

.stash_last_pal(self)
},
chat = function(expr) {
request <- deparse(substitute(expr))
private$.chat(request)
chat = function(...) {
private$Chat$chat(...)
},
stream = function(expr) {
request <- deparse(substitute(expr))
Expand All @@ -31,15 +30,6 @@ Pal <- R6::R6Class(
),
private = list(
Chat = NULL,
.chat = function(request, call = caller_env()) {
if (identical(request, "")) {
cli::cli_abort("Please supply a non-empty chat request.", call = call)
}
structure(
private$Chat$chat(paste0(request, collapse = "\n")),
class = c("pal_response", "character")
)
},
.stream = function(request, call = caller_env()) {
if (identical(request, "")) {
cli::cli_abort("Please supply a non-empty chat request.", call = call)
Expand Down
2 changes: 1 addition & 1 deletion man-roxygen/manual-interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#' Then, to submit a query, run:
#'
#' ```r
#' pal_<%= role %>$chat({expr})
#' pal_<%= role %>$chat({x})
#' ```
#' @md
2 changes: 1 addition & 1 deletion man/pal_cli.Rd

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

2 changes: 1 addition & 1 deletion man/pal_roxygen.Rd

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

2 changes: 1 addition & 1 deletion man/pal_testthat.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-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
Code
cli_pal$chat()
Condition
Error in `cli_pal$chat()`:
! Please supply a non-empty chat request.
Error in `user_turn()`:
! Must supply at least one input.

7 changes: 3 additions & 4 deletions tests/testthat/test-pal-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ test_that("can find the previous pal", {
withr::local_options(.pal_fn = NULL, .pal_args = NULL)

cli_pal <- .init_pal("cli")
expect_no_error(response <- cli_pal$chat(stop("Error message here")))
expect_s3_class(response, "pal_response")
expect_no_error(response <- cli_pal$chat("stop(\"Error message here\")"))
})

test_that("chat errors informatively with no input", {
Expand All @@ -23,12 +22,12 @@ test_that("pal_chat effectively integrates system prompt", {
withr::local_options(.pal_fn = NULL, .pal_args = NULL)

cli_pal <- .init_pal("cli")
response <- cli_pal$chat(stop("Error message here"))
response <- cli_pal$chat("stop(\"Error message here\")")
expect_true(grepl("cli_abort", response))
expect_true(grepl("Error message here", response))

testthat_pal <- .init_pal("testthat")
response <- testthat_pal$chat(expect_error(beep_bop_boop()))
response <- testthat_pal$chat("expect_error(beep_bop_boop())")
expect_true(grepl("expect_snapshot", response))
expect_true(grepl("beep_bop_boop", response))
})

0 comments on commit 2e4863c

Please sign in to comment.