From 3f026e5d4ec3957cde87ec8b0b4225af1f4fa8f2 Mon Sep 17 00:00:00 2001 From: al2na Date: Thu, 21 Dec 2023 22:56:33 +0100 Subject: [PATCH] generic send prompt returns correct error message,more stability to dependency installation --- R/extractInstallPkg.R | 54 +++++++++++++++++++++++++++++++++------- R/sendPrompt.R | 2 +- man/check_install.Rd | 2 +- man/extractInstallPkg.Rd | 3 ++- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/R/extractInstallPkg.R b/R/extractInstallPkg.R index 1caecf3..77e9056 100644 --- a/R/extractInstallPkg.R +++ b/R/extractInstallPkg.R @@ -8,7 +8,7 @@ #' @importFrom BiocManager install #' @importFrom BiocManager valid #' @importFrom utils install.packages -#' @return No value returned. Called for installation of package. +#' @return TRUE if the package is already installed or can be installed. FALSE otherwise #' @examples #' \donttest{ #' # Check and install "dplyr" package @@ -27,6 +27,7 @@ check_install<-function(package_name){ # ----------------------------------------------------------------------------- if (!require(package_name, character.only = TRUE)) { + message("\ntrying to install: ", package_name,"\n") if (!requireNamespace("BiocManager", quietly = TRUE)) { install.packages("BiocManager") } @@ -40,6 +41,10 @@ check_install<-function(package_name){ ) } } + + # returns true if the package is installed + require(package_name, character.only = TRUE) + } #' extract package names and install them @@ -48,7 +53,8 @@ check_install<-function(package_name){ #' are needed to run the code returned by the agent #' and installs them as needed. #' @param code code block returned by the agent. -#' @return No value returned. Called for installation of package. +#' @return final status of the packages as a logical vector, TRUE if packages is already installed +#' or could be installed. FALSE if the package can't be install. #' @examples #' \donttest{ #' # Check code for packages that need installing @@ -70,13 +76,43 @@ extractInstallPkg<-function(code){ # Split the code into separate lines code_lines <- strsplit(code, "\n")[[1]] + + # Define a regex pattern to match 'library' or 'require' calls. + # It allows for optional spaces (\\s*) between the function name and the opening parenthesis, + # and captures the argument inside the parentheses. + pattern <- "(library|require)\\s*\\(([^)]+)\\)" + + # Apply the regex pattern to each line. + # 'gregexpr' returns a list of match positions for each line. + matches <- regmatches(code_lines, gregexpr(pattern, code_lines)) + + # Process each line's matches. + package_names <- lapply(matches, function(match) { + # For each match, extract the package name. + sapply(match, function(m) { + # Extract the content within the parentheses. + # We use a regex with lookaheads and lookbehinds to capture this content. + pkg_name <- regmatches(m, regexpr("(?<=\\()[^)]+(?=\\))", m, perl = TRUE)) + # trim white space + trimws(pkg_name) + }) + }) + + # remove extra quotation marks and make the vecor + pkg_names <- unique(gsub('\"', '', unlist(package_names) ) ) + + + ## ! old code refactored above # For each line look for library call and install things if not installed - for(a in 1:length(code_lines)){ + #for(a in 1:length(code_lines)){ - if(grepl("(install|require)\\(", code_lines[a])){ - new_code <- gsub("(install|require)\\((.*)\\)", "check_install('\\2')", code_lines[a]) - message("trying to install ",new_code,"\n") - eval(str2expression(new_code)) # Execute install code - } - } + # if(grepl("(library|require)\\(", code_lines[a])){ + # new_code <- gsub("(library|require)\\((.*)\\)", "check_install('\\2')", code_lines[a]) + # message("trying to install ",new_code,"\n") + # eval(str2expression(new_code)) # Execute install code + # } + #} + + # check and if doesn't exist, install packages + sapply(pkg_names,check_install) } diff --git a/R/sendPrompt.R b/R/sendPrompt.R index 3961b15..ef82a41 100644 --- a/R/sendPrompt.R +++ b/R/sendPrompt.R @@ -276,7 +276,7 @@ genericChat<-function(agent, prompt,...){ } if(httr::status_code(response)>200) { - result <- trimws(httr::content(response)$error$message) + result <- trimws(httr::content(response)) } else { result <- trimws(httr::content(response)$choices[[1]]$message$content) } diff --git a/man/check_install.Rd b/man/check_install.Rd index 553cc99..7e5096c 100644 --- a/man/check_install.Rd +++ b/man/check_install.Rd @@ -10,7 +10,7 @@ check_install(package_name) \item{package_name}{A character string specifying the name of the package to be checked and installed.} } \value{ -No value returned. Called for installation of package. +TRUE if the package is already installed or can be installed. FALSE otherwise } \description{ This function checks if an R package is installed, and if not, attempts to install diff --git a/man/extractInstallPkg.Rd b/man/extractInstallPkg.Rd index 20390c0..7e8ad76 100644 --- a/man/extractInstallPkg.Rd +++ b/man/extractInstallPkg.Rd @@ -10,7 +10,8 @@ extractInstallPkg(code) \item{code}{code block returned by the agent.} } \value{ -No value returned. Called for installation of package. +final status of the packages as a logical vector, TRUE if packages is already installed +or could be installed. FALSE if the package can't be install. } \description{ This function extracts all package names that