Skip to content

Commit ad93e2d

Browse files
authored
Merge pull request #126 from posit-dev/feat/evaluate-tool
feat: A tool to run R code
2 parents e3d5108 + 5b63e7a commit ad93e2d

32 files changed

Lines changed: 2009 additions & 14 deletions

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
^scripts$
1717
^\.claude$
1818
^CRAN-SUBMISSION$
19+
^\.prettierrc$
20+
^node_modules$

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ${{ matrix.config.os }}
1919

2020
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
21-
timeout-minutes: 45
21+
timeout-minutes: 180
2222

2323
strategy:
2424
fail-fast: false

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"organizeImportsSkipDestructiveCodeActions": true,
3+
"singleQuote": false,
4+
"semi": false,
5+
"trailingComma": "all",
6+
"overrides": [
7+
{
8+
"files": "**/*.scss",
9+
"options": {
10+
"printWidth": 150
11+
}
12+
}
13+
]
14+
}

DESCRIPTION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ Suggests:
5353
chromote,
5454
DBI,
5555
duckdb,
56+
evaluate,
57+
fansi,
5658
gert,
5759
gh,
5860
htmltools,
5961
pandoc,
62+
ragg,
6063
shiny,
6164
shinychat (>= 0.2.0),
6265
testthat (>= 3.0.0),
@@ -93,6 +96,7 @@ Collate:
9396
'tool-git.R'
9497
'tool-github.R'
9598
'tool-rstudioapi.R'
99+
'tool-run.R'
96100
'tool-search-packages.R'
97101
'tool-session-package-installed.R'
98102
'tool-sessioninfo.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export(btw_tool_git_log)
4747
export(btw_tool_git_status)
4848
export(btw_tool_github)
4949
export(btw_tool_ide_read_current_editor)
50+
export(btw_tool_run_r)
5051
export(btw_tool_search_package_info)
5152
export(btw_tool_search_packages)
5253
export(btw_tool_session_check_package_installed)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# btw (development version)
22

3+
* New `btw_tool_run_r()` tool allows LLMs to run R code and to see the output, including of plots. Because this tool lets LLMs run R arbitrary R code in the global environment (which can be great but can also have security implications), it is opt-in and disabled by default. See `?btw_tool_run_r` for more details (#126).
4+
35
* `btw_tool_docs_help_page()` now uses markdown headings and sections for argument descriptions, rather than a table. This is considerably more token efficient when the argument descriptions have more than one paragraph and can't be converted into a markdown table (@jeanchristophe13v, #123).
46

57
* btw now removes large inline base64-encoded images, replacing them with a placeholder containing the image's alt text (@jeanchristophe13v, #119).

R/btw_client.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ btw_client <- function(
133133
}
134134

135135
btw_client_config <- function(client = NULL, tools = NULL, config = list()) {
136-
config$options <- flatten_config_options(config$options)
136+
# Options should be flattened and btw-prefixed by `read_btw_file()`.
137137
withr::local_options(config$options)
138138

139139
config$tools <-
@@ -275,7 +275,11 @@ flatten_config_options <- function(opts, prefix = "btw", sep = ".") {
275275
}
276276

277277
for (i in seq_along(x)) {
278-
new_key <- paste(key_prefix, nm[i], sep = sep)
278+
if (nzchar(key_prefix)) {
279+
new_key <- paste(key_prefix, nm[i], sep = sep)
280+
} else {
281+
new_key <- nm[i]
282+
}
279283
recurse(x[[i]], new_key)
280284
}
281285
} else {

R/btw_client_app.R

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ btw_app_from_client <- function(client, messages = list(), ...) {
118118
class = "btn-close",
119119
style = "position: fixed; top: 6px; right: 6px;"
120120
),
121+
class = "bslib-page-dashboard",
121122
btw_title(FALSE),
122123
shinychat::chat_mod_ui(
123124
"chat",
@@ -137,6 +138,13 @@ btw_app_from_client <- function(client, messages = list(), ...) {
137138
.sidebar-collapsed > .main > main .sidebar-title { display: block; }
138139
.bslib-sidebar-layout.sidebar-collapsed>.collapse-toggle { top: 1.8rem; }
139140
.bslib-page-main { gap: 0.5rem; }
141+
aside#tools_sidebar {
142+
box-shadow: 2px 2px 5px rgba(var(--bs-emphasis-color-rgb), 10%);
143+
}
144+
shiny-chat-message .message-icon {
145+
background-color: var(--bs-white);
146+
box-shadow: 2px 2px 5px rgba(var(--bs-emphasis-color-rgb), 10%);
147+
}
140148
"
141149
)),
142150
)
@@ -215,13 +223,16 @@ btw_app_from_client <- function(client, messages = list(), ...) {
215223
if (!length(selected_tools())) {
216224
client$set_tools(list())
217225
} else {
218-
.btw_tools <- keep(btw_tools(), function(tool) {
219-
tool@name %in% selected_tools()
220-
})
221-
.other_tools <- keep(other_tools, function(tool) {
226+
sel_btw_tools <- btw_tools(
227+
intersect(names(.btw_tools), selected_tools())
228+
)
229+
sel_other_tools <- keep(other_tools, function(tool) {
222230
tool@name %in% selected_tools()
223231
})
224-
client$set_tools(c(.btw_tools, other_tools))
232+
sel_tools <- c(sel_btw_tools, sel_other_tools)
233+
# tool_names <- map_chr(tools, S7::prop, "name")
234+
# cli::cli_inform("Setting {.field client} tools to: {.val {tool_names}}")
235+
client$set_tools(sel_tools)
225236
}
226237
})
227238

@@ -269,6 +280,10 @@ btw_app_from_client <- function(client, messages = list(), ...) {
269280
save.interface = old_save
270281
))
271282

283+
if (identical(Sys.getenv("BTW_IN_TESTING"), "true")) {
284+
return(list(ui = ui, server = server))
285+
}
286+
272287
app <- shiny::shinyApp(ui, server, ...)
273288
if (getOption("btw.app.in_addin", FALSE)) {
274289
shiny::runApp(app, launch.browser = function(url) {
@@ -597,10 +612,12 @@ app_tool_group_choice_input <- function(
597612
group,
598613
"docs" = shiny::span(label_icon, "Documentation"),
599614
"env" = shiny::span(label_icon, "Environment"),
615+
"eval" = shiny::span(label_icon, "Code Evaluation"),
600616
"files" = shiny::span(label_icon, "Files"),
601617
"git" = shiny::span(label_icon, "Git"),
602618
"github" = shiny::span(label_icon, "GitHub"),
603619
"ide" = shiny::span(label_icon, "IDE"),
620+
"run" = shiny::span(label_icon, "Run Code"),
604621
"search" = shiny::span(label_icon, "Search"),
605622
"session" = shiny::span(label_icon, "Session Info"),
606623
"web" = shiny::span(label_icon, "Web Tools"),

0 commit comments

Comments
 (0)