Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions R/tool-git.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#' @include tool-result.R
NULL

btw_can_register_git_tool <- function() {
if (!rlang::is_installed("gert")) {
return(FALSE)
}

tryCatch(
{
gert::git_info()
TRUE
},
error = function(e) FALSE
)
}

# Git Status ------------------------------------------------------------------

#' Tool: Git Status
Expand Down Expand Up @@ -74,7 +88,7 @@ RETURNS: A list of file paths, their status (new, modified, deleted, etc.), and
read_only_hint = TRUE,
open_world_hint = FALSE,
idempotent_hint = FALSE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
include = ellmer::type_enum(
Expand Down Expand Up @@ -164,7 +178,7 @@ LIMITATION: This tool does not support diffing between two arbitrary commits.
read_only_hint = TRUE,
open_world_hint = FALSE,
idempotent_hint = TRUE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
ref = ellmer::type_string(
Expand Down Expand Up @@ -267,7 +281,7 @@ RETURNS: A list of commits with SHA (short), author, timestamp, number of files,
read_only_hint = TRUE,
open_world_hint = FALSE,
idempotent_hint = FALSE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
ref = ellmer::type_string(
Expand Down Expand Up @@ -365,7 +379,7 @@ RETURNS: The commit SHA and confirmation message.
read_only_hint = FALSE,
open_world_hint = FALSE,
idempotent_hint = FALSE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
message = ellmer::type_string(
Expand Down Expand Up @@ -451,7 +465,7 @@ RETURNS: A table of branch names, upstream tracking, and last update time.
read_only_hint = TRUE,
open_world_hint = FALSE,
idempotent_hint = TRUE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
include = ellmer::type_enum(
Expand Down Expand Up @@ -541,7 +555,7 @@ RETURNS: Confirmation message with branch name and ref.
read_only_hint = FALSE,
open_world_hint = FALSE,
idempotent_hint = FALSE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
branch = ellmer::type_string(
Expand Down Expand Up @@ -627,7 +641,7 @@ RETURNS: Confirmation message with branch name.
read_only_hint = FALSE,
open_world_hint = FALSE,
idempotent_hint = FALSE,
btw_can_register = function() rlang::is_installed("gert")
btw_can_register = btw_can_register_git_tool
),
arguments = list(
branch = ellmer::type_string(
Expand Down
48 changes: 24 additions & 24 deletions R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,32 @@ btw_tools <- function(...) {
check_character(tools, allow_null = TRUE)

if (length(tools) == 0) {
return(as_ellmer_tools(.btw_tools))
tools <- names(.btw_tools)
} else {
tool_names <- map_chr(.btw_tools, function(x) x$name)
tool_groups <- map_chr(.btw_tools, function(x) x$group)

allowed <- c(
tool_groups,
tool_names,
sub("btw_tool_", "", tool_names, fixed = TRUE)
)
allowed <- unique(allowed)

tools <- tryCatch(
arg_match(tools, allowed[!grepl("^btw_", allowed)], multiple = TRUE),
error = function(err_short) {
tryCatch(
arg_match(tools, allowed, multiple = TRUE),
error = function(err_long) {
class(err_short) <- c("btw_unmatched_tool_error", class(err_short))
cnd_signal(err_short)
}
)
}
)
}

tool_names <- map_chr(.btw_tools, function(x) x$name)
tool_groups <- map_chr(.btw_tools, function(x) x$group)

allowed <- c(
tool_groups,
tool_names,
sub("btw_tool_", "", tool_names, fixed = TRUE)
)
allowed <- unique(allowed)

tools <- tryCatch(
arg_match(tools, allowed[!grepl("^btw_", allowed)], multiple = TRUE),
error = function(err_short) {
tryCatch(
arg_match(tools, allowed, multiple = TRUE),
error = function(err_long) {
class(err_short) <- c("btw_unmatched_tool_error", class(err_short))
cnd_signal(err_short)
}
)
}
)

tools_to_keep <- map_lgl(.btw_tools, is_tool_match, tools)
res <- .btw_tools[tools_to_keep]
res <- as_ellmer_tools(res)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-tool-git.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ test_that("btw_tool_git_branch_checkout()", {
expect_equal(gert::git_branch(), current_branch)
})

test_that("git tools are registered only when in a git repo", {
skip_if_not_installed("gert")

# Not in a git repo
withr::with_tempdir({
expect_false(btw_can_register_git_tool())
tool_names <- names(btw_tools())
expect_no_match(tool_names, "btw_tool_git")
})

# In a git repo
local_temp_git_repo()
expect_true(btw_can_register_git_tool())
expect_true(all(grepl("btw_tool_git", names(btw_tools("git")))))
})

test_that("git tools require gert to be installed", {
skip_if_not_installed("gert")

Expand Down
15 changes: 12 additions & 3 deletions tests/testthat/test-tools.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
test_that("btw_tools works", {
local_mocked_bindings(
has_chromote = function() TRUE,
rstudioapi_has_source_editor_context = function() TRUE,
btw_can_register_git_tool = function() TRUE
)

# returns .btw_tools as-is with no `tools` argument
expect_equal(btw_tools(), as_ellmer_tools(.btw_tools))
expect_equal(btw_tools(NULL), as_ellmer_tools(.btw_tools))
Expand Down Expand Up @@ -61,7 +67,8 @@ test_that("btw_tools can be registered with a chat", {
test_that("All btw tools have annotations", {
local_mocked_bindings(
# Force conditional tools to always be included in this test
rstudioapi_has_source_editor_context = function() TRUE
rstudioapi_has_source_editor_context = function() TRUE,
btw_can_register_git_tool = function() TRUE
)

tools <- btw_tools()
Expand All @@ -78,7 +85,8 @@ test_that("All btw tools have annotations", {
test_that("All btw tools have a btw_can_register() annotation function", {
local_mocked_bindings(
# Force conditional tools to always be included in this test
rstudioapi_has_source_editor_context = function() TRUE
rstudioapi_has_source_editor_context = function() TRUE,
btw_can_register_git_tool = function() TRUE
)

tools <- btw_tools()
Expand All @@ -99,7 +107,8 @@ test_that("All btw tools have a btw_can_register() annotation function", {
test_that("$tool() returns a tool definition for all .btw_tools", {
local_mocked_bindings(
has_chromote = function() FALSE,
rstudioapi_has_source_editor_context = function() FALSE
rstudioapi_has_source_editor_context = function() FALSE,
btw_can_register_git_tool = function() FALSE
)

for (name in names(.btw_tools)) {
Expand Down