Skip to content

Commit

Permalink
prefill prompt with template (closes #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Nov 7, 2024
1 parent c990af6 commit e1e5d36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
43 changes: 20 additions & 23 deletions R/prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ prompt_new <- function(role, interface, contents = NULL) {
# TODO: should this message "Register with `directory_load()`" or
# something as it creates the file?
file.create(path)
if (!is.null(contents)) {
prompt_prefill(path = path, contents = contents)
}
prompt_prefill(path = path, contents = contents, role = role)
if (interactive()) {
file.edit(path)
}
Expand Down Expand Up @@ -145,34 +143,33 @@ prompt_locate <- function(role, call = caller_env()) {
file.path(path, base_names[match])
}

prompt_prefill <- function(path, contents, call = caller_env()) {
if (!is_markdown_file(contents)) {
prompt_prefill <- function(path, contents, role, call = caller_env()) {
if (!is.null(contents) && !is_markdown_file(contents)) {
cli::cli_abort(
"{.arg contents} must be a connection to a markdown file.",
call = call
)
}

if (!is.null(contents)) {
suppressWarnings(
try_fetch(
{
lines <- base::readLines(contents)
writeLines(text = lines, con = path)
},
error = function(cnd) {
cli::cli_abort(
"An error occurred while pre-filling the prompt with {.arg contents}.",
call = call,
parent = cnd
)
}
)
)
if (is.null(contents)) {
contents <- system.file("template.md", package = "pal")
}

# TODO: should there be some pre-filled instructions on how to write
# prompts here in `else`?
suppressWarnings(
try_fetch(
{
lines <- base::readLines(contents)
writeLines(text = lines, con = path)
},
error = function(cnd) {
cli::cli_abort(
"An error occurred while pre-filling the prompt with {.arg contents}.",
call = call,
parent = cnd
)
}
)
)

invisible(path)
}
Expand Down
15 changes: 15 additions & 0 deletions inst/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Template pal

You are a terse assistant designed to help R users <INSERT SOME INSTRUCTIONS HERE>. Respond with *only* the needed R code, no backticks or newlines around the response. Intersperse newlines within function calls as needed, per tidy style.

As example, given:

```{r}
some_code()
```

Return:

```{r}
some_other_code()
```
14 changes: 14 additions & 0 deletions tests/testthat/test-prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ test_that("new prompts can be pre-filled with contents", {
expect_true(grepl("Given some text", lines))
})

test_that("new prompts are pre-filled with a template by default (#57)", {
skip_if_offline()
# contains two prompts, `boop-replace` and `wop-prefix`
withr::local_options(.pal_dir = "test-prompt-dir")
testthat::local_mocked_bindings(interactive = function(...) {FALSE})
withr::defer(prompt_remove("template"))

path <- prompt_new("template", "prefix")

lines <- base::readLines(path)

expect_equal(lines[1], "# Template pal")
})

test_that("new prompts error informatively with bad pre-fill contents", {
skip_if_offline()
# contains two prompts, `boop-replace` and `wop-prefix`
Expand Down

0 comments on commit e1e5d36

Please sign in to comment.