From 79a104391b61f7f1f5f05a2a6c5b73fbd1af309b Mon Sep 17 00:00:00 2001 From: Simon Couch Date: Thu, 18 Dec 2025 09:23:58 -0600 Subject: [PATCH 01/28] implement Claude Skills --- DESCRIPTION | 4 +- NAMESPACE | 1 + R/btw_client.R | 4 + R/tool-skills.R | 289 ++++++++++++++ inst/prompts/skills.md | 14 + inst/skills/skill-creator/SKILL.md | 356 ++++++++++++++++++ .../references/output-patterns.md | 82 ++++ .../skill-creator/references/workflows.md | 28 ++ .../skill-creator/scripts/init_skill.py | 303 +++++++++++++++ .../skill-creator/scripts/package_skill.py | 110 ++++++ .../skill-creator/scripts/quick_validate.py | 95 +++++ man/btw_tool_fetch_skill.Rd | 28 ++ man/btw_tools.Rd | 7 + 13 files changed, 1320 insertions(+), 1 deletion(-) create mode 100644 R/tool-skills.R create mode 100644 inst/prompts/skills.md create mode 100644 inst/skills/skill-creator/SKILL.md create mode 100644 inst/skills/skill-creator/references/output-patterns.md create mode 100644 inst/skills/skill-creator/references/workflows.md create mode 100644 inst/skills/skill-creator/scripts/init_skill.py create mode 100644 inst/skills/skill-creator/scripts/package_skill.py create mode 100644 inst/skills/skill-creator/scripts/quick_validate.py create mode 100644 man/btw_tool_fetch_skill.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4c392808..04221264 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -70,7 +70,8 @@ Suggests: shiny, shinychat (>= 0.3.0), testthat (>= 3.0.0), - usethis + usethis, + yaml Config/Needs/website: tidyverse/tidytemplate Config/testthat/edition: 3 Config/testthat/parallel: true @@ -109,6 +110,7 @@ Collate: 'tool-search-packages.R' 'tool-session-info.R' 'tool-session-package-installed.R' + 'tool-skills.R' 'tool-web.R' 'tools.R' 'utils-ellmer.R' diff --git a/NAMESPACE b/NAMESPACE index f22a35ca..fe0a1a0f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -35,6 +35,7 @@ export(btw_tool_docs_package_news) export(btw_tool_docs_vignette) export(btw_tool_env_describe_data_frame) export(btw_tool_env_describe_environment) +export(btw_tool_fetch_skill) export(btw_tool_files_code_search) export(btw_tool_files_list_files) export(btw_tool_files_read_text_file) diff --git a/R/btw_client.R b/R/btw_client.R index 71cf0dea..52a5590c 100644 --- a/R/btw_client.R +++ b/R/btw_client.R @@ -110,12 +110,16 @@ btw_client <- function( llms_txt <- read_llms_txt(path_llms_txt) project_context <- c(llms_txt, config$btw_system_prompt) project_context <- paste(project_context, collapse = "\n\n") + skills_prompt <- btw_skills_system_prompt() sys_prompt <- c( btw_prompt("btw-system_session.md"), if (!skip_tools) { btw_prompt("btw-system_tools.md") }, + if (nzchar(skills_prompt)) { + skills_prompt + }, if (nzchar(project_context)) { btw_prompt("btw-system_project.md") }, diff --git a/R/tool-skills.R b/R/tool-skills.R new file mode 100644 index 00000000..f39458e2 --- /dev/null +++ b/R/tool-skills.R @@ -0,0 +1,289 @@ +#' @include tool-result.R +NULL + +#' Tool: Fetch a skill +#' +#' @description +#' Fetch a skill's instructions and list its bundled resources. +#' +#' Skills are modular capabilities that extend Claude's functionality with +#' specialized knowledge, workflows, and tools. Each skill is a directory +#' containing a `SKILL.md` file with instructions and optional bundled +#' resources (scripts, references, assets). +#' +#' @param skill_name The name of the skill to fetch. +#' @inheritParams btw_tool_docs_package_news +#' +#' @return A `btw_tool_result` containing the skill instructions and a listing +#' of bundled resources with their paths. +#' +#' @family skills +#' @export +btw_tool_fetch_skill <- function(skill_name, `_intent`) {} + +btw_tool_fetch_skill_impl <- function(skill_name) { + + check_string(skill_name) + + skill_info <- find_skill(skill_name) + + if (is.null(skill_info)) { + available <- btw_list_skills() + skill_names <- vapply(available, function(x) x$name, character(1)) + cli::cli_abort( + c( + "Skill {.val {skill_name}} not found.", + "i" = "Available skills: {.val {skill_names}}" + ) + ) + } + + skill_content <- readLines(skill_info$path, warn = FALSE) + + content_start <- 1 + if (length(skill_content) > 0 && skill_content[1] == "---") { + yaml_end <- which(skill_content == "---") + if (length(yaml_end) >= 2) { + content_start <- yaml_end[2] + 1 + } + } + + skill_text <- paste( + skill_content[content_start:length(skill_content)], + collapse = "\n" + ) + + resources <- list_skill_resources(skill_info$base_dir) + resources_listing <- format_resources_listing(resources, skill_info$base_dir) + + full_content <- paste0(skill_text, resources_listing) + + btw_tool_result( + value = full_content, + data = list( + name = skill_name, + path = skill_info$path, + base_dir = skill_info$base_dir, + resources = resources + ), + display = list( + title = sprintf("Skill: %s", skill_name), + markdown = full_content + ) + ) +} + +.btw_add_to_tools( + name = "btw_tool_fetch_skill", + group = "skills", + tool = function() { + ellmer::tool( + btw_tool_fetch_skill_impl, + name = "btw_tool_fetch_skill", + description = paste( + "Fetch a skill's instructions and list its bundled resources.", + "Skills provide specialized guidance for specific tasks.", + "After fetching, use file read tools to access references,", + "or bash/code tools to run scripts." + ), + annotations = ellmer::tool_annotations( + title = "Fetch Skill", + read_only_hint = TRUE, + open_world_hint = FALSE, + btw_can_register = function() length(btw_list_skills()) > 0 + ), + arguments = list( + skill_name = ellmer::type_string( + "The name of the skill to fetch" + ) + ) + ) + } +) + +# Skill Discovery ---------------------------------------------------------- + +btw_skill_directories <- function() { + + dirs <- character() + + + package_skills <- system.file("skills", package = "btw") + if (nzchar(package_skills) && dir.exists(package_skills)) { + dirs <- c(dirs, package_skills) + } + + + user_skills_dir <- file.path( + tools::R_user_dir("btw", "config"), + "skills" + ) + if (dir.exists(user_skills_dir)) { + dirs <- c(dirs, user_skills_dir) + } + + project_skills_dir <- file.path(getwd(), ".btw", "skills") + if (dir.exists(project_skills_dir)) { + dirs <- c(dirs, project_skills_dir) + } + + dirs +} + +btw_list_skills <- function() { + skill_dirs <- btw_skill_directories() + all_skills <- list() + + for (dir in skill_dirs) { + if (!dir.exists(dir)) { + next + } + + subdirs <- list.dirs(dir, full.names = TRUE, recursive = FALSE) + + for (subdir in subdirs) { + skill_md_path <- file.path(subdir, "SKILL.md") + if (file.exists(skill_md_path)) { + metadata <- extract_skill_metadata(skill_md_path) + skill_name <- basename(subdir) + + all_skills[[skill_name]] <- list( + name = skill_name, + description = metadata$description %||% "No description available", + path = skill_md_path + ) + } + } + } + + all_skills +} + +find_skill <- function(skill_name) { + skill_dirs <- btw_skill_directories() + + for (dir in skill_dirs) { + skill_dir <- file.path(dir, skill_name) + skill_md_path <- file.path(skill_dir, "SKILL.md") + if (dir.exists(skill_dir) && file.exists(skill_md_path)) { + return(list( + path = skill_md_path, + base_dir = skill_dir + )) + } + } + + NULL +} + +extract_skill_metadata <- function(skill_path) { + lines <- readLines(skill_path, warn = FALSE) + + if (length(lines) == 0 || lines[1] != "---") { + return(list()) + } + + yaml_end_indices <- which(lines == "---") + if (length(yaml_end_indices) < 2) { + return(list()) + } + + yaml_lines <- lines[2:(yaml_end_indices[2] - 1)] + yaml_text <- paste(yaml_lines, collapse = "\n") + + check_installed("yaml") + tryCatch( + yaml::yaml.load(yaml_text), + error = function(e) list() + ) +} + +# Skill Resources ---------------------------------------------------------- + +list_skill_resources <- function(skill_dir) { + list( + scripts = list_files_in_subdir(skill_dir, "scripts"), + references = list_files_in_subdir(skill_dir, "references"), + assets = list_files_in_subdir(skill_dir, "assets") + ) +} + +list_files_in_subdir <- function(base_dir, subdir) { + full_path <- file.path(base_dir, subdir) + if (!dir.exists(full_path)) { + return(character(0)) + } + list.files(full_path, full.names = FALSE) +} + +has_skill_resources <- function(resources) { + length(resources$scripts) > 0 || + length(resources$references) > 0 || + length(resources$assets) > 0 +} + +format_resources_listing <- function(resources, base_dir) { + if (!has_skill_resources(resources)) { + return("") + } + + parts <- character() + parts <- c(parts, "\n\n---\n\n## Bundled Resources\n") + + if (length(resources$scripts) > 0) { + parts <- c(parts, "\n**Scripts:**\n") + script_paths <- file.path(base_dir, "scripts", resources$scripts) + parts <- c(parts, paste0("- ", script_paths, collapse = "\n")) + } + + if (length(resources$references) > 0) { + parts <- c(parts, "\n\n**References:**\n") + ref_paths <- file.path(base_dir, "references", resources$references) + parts <- c(parts, paste0("- ", ref_paths, collapse = "\n")) + } + + if (length(resources$assets) > 0) { + parts <- c(parts, "\n\n**Assets:**\n") + asset_paths <- file.path(base_dir, "assets", resources$assets) + parts <- c(parts, paste0("- ", asset_paths, collapse = "\n")) + } + + paste(parts, collapse = "") +} + +# System Prompt ------------------------------------------------------------ + +btw_skills_system_prompt <- function() { + skills <- btw_list_skills() + + if (length(skills) == 0) { + return("") + } + + + skills_prompt_path <- system.file("prompts", "skills.md", package = "btw") + explanation <- if (file.exists(skills_prompt_path)) { + paste(readLines(skills_prompt_path, warn = FALSE), collapse = "\n") + } else { + "## Skills\n\nYou have access to specialized skills that provide detailed guidance for specific tasks." + } + + skill_items <- vapply( + skills, + function(skill) { + sprintf( + "\n%s\n%s\n", + skill$name, + skill$description + ) + }, + character(1) + ) + + paste0( + explanation, + "\n\n\n", + paste(skill_items, collapse = "\n"), + "\n" + ) +} diff --git a/inst/prompts/skills.md b/inst/prompts/skills.md new file mode 100644 index 00000000..09b896a0 --- /dev/null +++ b/inst/prompts/skills.md @@ -0,0 +1,14 @@ +## Skills + +You have access to specialized skills that provide detailed guidance for specific tasks. Skills are loaded on-demand to provide domain-specific expertise without consuming context until needed. + +### Using Skills + +1. **Check available skills**: Review the `` listing below +2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description +3. **Access resources**: After fetching, use file read tools to access references or bash/code tools to run bundled scripts + +Skills may include bundled resources: +- **Scripts**: Executable code (R, Python, bash) for automated tasks +- **References**: Additional documentation to consult as needed +- **Assets**: Templates and files for use in outputs diff --git a/inst/skills/skill-creator/SKILL.md b/inst/skills/skill-creator/SKILL.md new file mode 100644 index 00000000..b7f86598 --- /dev/null +++ b/inst/skills/skill-creator/SKILL.md @@ -0,0 +1,356 @@ +--- +name: skill-creator +description: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations. +license: Complete terms in LICENSE.txt +--- + +# Skill Creator + +This skill provides guidance for creating effective skills. + +## About Skills + +Skills are modular, self-contained packages that extend Claude's capabilities by providing +specialized knowledge, workflows, and tools. Think of them as "onboarding guides" for specific +domains or tasks—they transform Claude from a general-purpose agent into a specialized agent +equipped with procedural knowledge that no model can fully possess. + +### What Skills Provide + +1. Specialized workflows - Multi-step procedures for specific domains +2. Tool integrations - Instructions for working with specific file formats or APIs +3. Domain expertise - Company-specific knowledge, schemas, business logic +4. Bundled resources - Scripts, references, and assets for complex and repetitive tasks + +## Core Principles + +### Concise is Key + +The context window is a public good. Skills share the context window with everything else Claude needs: system prompt, conversation history, other Skills' metadata, and the actual user request. + +**Default assumption: Claude is already very smart.** Only add context Claude doesn't already have. Challenge each piece of information: "Does Claude really need this explanation?" and "Does this paragraph justify its token cost?" + +Prefer concise examples over verbose explanations. + +### Set Appropriate Degrees of Freedom + +Match the level of specificity to the task's fragility and variability: + +**High freedom (text-based instructions)**: Use when multiple approaches are valid, decisions depend on context, or heuristics guide the approach. + +**Medium freedom (pseudocode or scripts with parameters)**: Use when a preferred pattern exists, some variation is acceptable, or configuration affects behavior. + +**Low freedom (specific scripts, few parameters)**: Use when operations are fragile and error-prone, consistency is critical, or a specific sequence must be followed. + +Think of Claude as exploring a path: a narrow bridge with cliffs needs specific guardrails (low freedom), while an open field allows many routes (high freedom). + +### Anatomy of a Skill + +Every skill consists of a required SKILL.md file and optional bundled resources: + +``` +skill-name/ +├── SKILL.md (required) +│ ├── YAML frontmatter metadata (required) +│ │ ├── name: (required) +│ │ └── description: (required) +│ └── Markdown instructions (required) +└── Bundled Resources (optional) + ├── scripts/ - Executable code (Python/Bash/etc.) + ├── references/ - Documentation intended to be loaded into context as needed + └── assets/ - Files used in output (templates, icons, fonts, etc.) +``` + +#### SKILL.md (required) + +Every SKILL.md consists of: + +- **Frontmatter** (YAML): Contains `name` and `description` fields. These are the only fields that Claude reads to determine when the skill gets used, thus it is very important to be clear and comprehensive in describing what the skill is, and when it should be used. +- **Body** (Markdown): Instructions and guidance for using the skill. Only loaded AFTER the skill triggers (if at all). + +#### Bundled Resources (optional) + +##### Scripts (`scripts/`) + +Executable code (Python/Bash/etc.) for tasks that require deterministic reliability or are repeatedly rewritten. + +- **When to include**: When the same code is being rewritten repeatedly or deterministic reliability is needed +- **Example**: `scripts/rotate_pdf.py` for PDF rotation tasks +- **Benefits**: Token efficient, deterministic, may be executed without loading into context +- **Note**: Scripts may still need to be read by Claude for patching or environment-specific adjustments + +##### References (`references/`) + +Documentation and reference material intended to be loaded as needed into context to inform Claude's process and thinking. + +- **When to include**: For documentation that Claude should reference while working +- **Examples**: `references/finance.md` for financial schemas, `references/mnda.md` for company NDA template, `references/policies.md` for company policies, `references/api_docs.md` for API specifications +- **Use cases**: Database schemas, API documentation, domain knowledge, company policies, detailed workflow guides +- **Benefits**: Keeps SKILL.md lean, loaded only when Claude determines it's needed +- **Best practice**: If files are large (>10k words), include grep search patterns in SKILL.md +- **Avoid duplication**: Information should live in either SKILL.md or references files, not both. Prefer references files for detailed information unless it's truly core to the skill—this keeps SKILL.md lean while making information discoverable without hogging the context window. Keep only essential procedural instructions and workflow guidance in SKILL.md; move detailed reference material, schemas, and examples to references files. + +##### Assets (`assets/`) + +Files not intended to be loaded into context, but rather used within the output Claude produces. + +- **When to include**: When the skill needs files that will be used in the final output +- **Examples**: `assets/logo.png` for brand assets, `assets/slides.pptx` for PowerPoint templates, `assets/frontend-template/` for HTML/React boilerplate, `assets/font.ttf` for typography +- **Use cases**: Templates, images, icons, boilerplate code, fonts, sample documents that get copied or modified +- **Benefits**: Separates output resources from documentation, enables Claude to use files without loading them into context + +#### What to Not Include in a Skill + +A skill should only contain essential files that directly support its functionality. Do NOT create extraneous documentation or auxiliary files, including: + +- README.md +- INSTALLATION_GUIDE.md +- QUICK_REFERENCE.md +- CHANGELOG.md +- etc. + +The skill should only contain the information needed for an AI agent to do the job at hand. It should not contain auxilary context about the process that went into creating it, setup and testing procedures, user-facing documentation, etc. Creating additional documentation files just adds clutter and confusion. + +### Progressive Disclosure Design Principle + +Skills use a three-level loading system to manage context efficiently: + +1. **Metadata (name + description)** - Always in context (~100 words) +2. **SKILL.md body** - When skill triggers (<5k words) +3. **Bundled resources** - As needed by Claude (Unlimited because scripts can be executed without reading into context window) + +#### Progressive Disclosure Patterns + +Keep SKILL.md body to the essentials and under 500 lines to minimize context bloat. Split content into separate files when approaching this limit. When splitting out content into other files, it is very important to reference them from SKILL.md and describe clearly when to read them, to ensure the reader of the skill knows they exist and when to use them. + +**Key principle:** When a skill supports multiple variations, frameworks, or options, keep only the core workflow and selection guidance in SKILL.md. Move variant-specific details (patterns, examples, configuration) into separate reference files. + +**Pattern 1: High-level guide with references** + +```markdown +# PDF Processing + +## Quick start + +Extract text with pdfplumber: +[code example] + +## Advanced features + +- **Form filling**: See [FORMS.md](FORMS.md) for complete guide +- **API reference**: See [REFERENCE.md](REFERENCE.md) for all methods +- **Examples**: See [EXAMPLES.md](EXAMPLES.md) for common patterns +``` + +Claude loads FORMS.md, REFERENCE.md, or EXAMPLES.md only when needed. + +**Pattern 2: Domain-specific organization** + +For Skills with multiple domains, organize content by domain to avoid loading irrelevant context: + +``` +bigquery-skill/ +├── SKILL.md (overview and navigation) +└── reference/ + ├── finance.md (revenue, billing metrics) + ├── sales.md (opportunities, pipeline) + ├── product.md (API usage, features) + └── marketing.md (campaigns, attribution) +``` + +When a user asks about sales metrics, Claude only reads sales.md. + +Similarly, for skills supporting multiple frameworks or variants, organize by variant: + +``` +cloud-deploy/ +├── SKILL.md (workflow + provider selection) +└── references/ + ├── aws.md (AWS deployment patterns) + ├── gcp.md (GCP deployment patterns) + └── azure.md (Azure deployment patterns) +``` + +When the user chooses AWS, Claude only reads aws.md. + +**Pattern 3: Conditional details** + +Show basic content, link to advanced content: + +```markdown +# DOCX Processing + +## Creating documents + +Use docx-js for new documents. See [DOCX-JS.md](DOCX-JS.md). + +## Editing documents + +For simple edits, modify the XML directly. + +**For tracked changes**: See [REDLINING.md](REDLINING.md) +**For OOXML details**: See [OOXML.md](OOXML.md) +``` + +Claude reads REDLINING.md or OOXML.md only when the user needs those features. + +**Important guidelines:** + +- **Avoid deeply nested references** - Keep references one level deep from SKILL.md. All reference files should link directly from SKILL.md. +- **Structure longer reference files** - For files longer than 100 lines, include a table of contents at the top so Claude can see the full scope when previewing. + +## Skill Creation Process + +Skill creation involves these steps: + +1. Understand the skill with concrete examples +2. Plan reusable skill contents (scripts, references, assets) +3. Initialize the skill (run init_skill.py) +4. Edit the skill (implement resources and write SKILL.md) +5. Package the skill (run package_skill.py) +6. Iterate based on real usage + +Follow these steps in order, skipping only if there is a clear reason why they are not applicable. + +### Step 1: Understanding the Skill with Concrete Examples + +Skip this step only when the skill's usage patterns are already clearly understood. It remains valuable even when working with an existing skill. + +To create an effective skill, clearly understand concrete examples of how the skill will be used. This understanding can come from either direct user examples or generated examples that are validated with user feedback. + +For example, when building an image-editor skill, relevant questions include: + +- "What functionality should the image-editor skill support? Editing, rotating, anything else?" +- "Can you give some examples of how this skill would be used?" +- "I can imagine users asking for things like 'Remove the red-eye from this image' or 'Rotate this image'. Are there other ways you imagine this skill being used?" +- "What would a user say that should trigger this skill?" + +To avoid overwhelming users, avoid asking too many questions in a single message. Start with the most important questions and follow up as needed for better effectiveness. + +Conclude this step when there is a clear sense of the functionality the skill should support. + +### Step 2: Planning the Reusable Skill Contents + +To turn concrete examples into an effective skill, analyze each example by: + +1. Considering how to execute on the example from scratch +2. Identifying what scripts, references, and assets would be helpful when executing these workflows repeatedly + +Example: When building a `pdf-editor` skill to handle queries like "Help me rotate this PDF," the analysis shows: + +1. Rotating a PDF requires re-writing the same code each time +2. A `scripts/rotate_pdf.py` script would be helpful to store in the skill + +Example: When designing a `frontend-webapp-builder` skill for queries like "Build me a todo app" or "Build me a dashboard to track my steps," the analysis shows: + +1. Writing a frontend webapp requires the same boilerplate HTML/React each time +2. An `assets/hello-world/` template containing the boilerplate HTML/React project files would be helpful to store in the skill + +Example: When building a `big-query` skill to handle queries like "How many users have logged in today?" the analysis shows: + +1. Querying BigQuery requires re-discovering the table schemas and relationships each time +2. A `references/schema.md` file documenting the table schemas would be helpful to store in the skill + +To establish the skill's contents, analyze each concrete example to create a list of the reusable resources to include: scripts, references, and assets. + +### Step 3: Initializing the Skill + +At this point, it is time to actually create the skill. + +Skip this step only if the skill being developed already exists, and iteration or packaging is needed. In this case, continue to the next step. + +When creating a new skill from scratch, always run the `init_skill.py` script. The script conveniently generates a new template skill directory that automatically includes everything a skill requires, making the skill creation process much more efficient and reliable. + +Usage: + +```bash +scripts/init_skill.py --path +``` + +The script: + +- Creates the skill directory at the specified path +- Generates a SKILL.md template with proper frontmatter and TODO placeholders +- Creates example resource directories: `scripts/`, `references/`, and `assets/` +- Adds example files in each directory that can be customized or deleted + +After initialization, customize or remove the generated SKILL.md and example files as needed. + +### Step 4: Edit the Skill + +When editing the (newly-generated or existing) skill, remember that the skill is being created for another instance of Claude to use. Include information that would be beneficial and non-obvious to Claude. Consider what procedural knowledge, domain-specific details, or reusable assets would help another Claude instance execute these tasks more effectively. + +#### Learn Proven Design Patterns + +Consult these helpful guides based on your skill's needs: + +- **Multi-step processes**: See references/workflows.md for sequential workflows and conditional logic +- **Specific output formats or quality standards**: See references/output-patterns.md for template and example patterns + +These files contain established best practices for effective skill design. + +#### Start with Reusable Skill Contents + +To begin implementation, start with the reusable resources identified above: `scripts/`, `references/`, and `assets/` files. Note that this step may require user input. For example, when implementing a `brand-guidelines` skill, the user may need to provide brand assets or templates to store in `assets/`, or documentation to store in `references/`. + +Added scripts must be tested by actually running them to ensure there are no bugs and that the output matches what is expected. If there are many similar scripts, only a representative sample needs to be tested to ensure confidence that they all work while balancing time to completion. + +Any example files and directories not needed for the skill should be deleted. The initialization script creates example files in `scripts/`, `references/`, and `assets/` to demonstrate structure, but most skills won't need all of them. + +#### Update SKILL.md + +**Writing Guidelines:** Always use imperative/infinitive form. + +##### Frontmatter + +Write the YAML frontmatter with `name` and `description`: + +- `name`: The skill name +- `description`: This is the primary triggering mechanism for your skill, and helps Claude understand when to use the skill. + - Include both what the Skill does and specific triggers/contexts for when to use it. + - Include all "when to use" information here - Not in the body. The body is only loaded after triggering, so "When to Use This Skill" sections in the body are not helpful to Claude. + - Example description for a `docx` skill: "Comprehensive document creation, editing, and analysis with support for tracked changes, comments, formatting preservation, and text extraction. Use when Claude needs to work with professional documents (.docx files) for: (1) Creating new documents, (2) Modifying or editing content, (3) Working with tracked changes, (4) Adding comments, or any other document tasks" + +Do not include any other fields in YAML frontmatter. + +##### Body + +Write instructions for using the skill and its bundled resources. + +### Step 5: Packaging a Skill + +Once development of the skill is complete, it must be packaged into a distributable .skill file that gets shared with the user. The packaging process automatically validates the skill first to ensure it meets all requirements: + +```bash +scripts/package_skill.py +``` + +Optional output directory specification: + +```bash +scripts/package_skill.py ./dist +``` + +The packaging script will: + +1. **Validate** the skill automatically, checking: + + - YAML frontmatter format and required fields + - Skill naming conventions and directory structure + - Description completeness and quality + - File organization and resource references + +2. **Package** the skill if validation passes, creating a .skill file named after the skill (e.g., `my-skill.skill`) that includes all files and maintains the proper directory structure for distribution. The .skill file is a zip file with a .skill extension. + +If validation fails, the script will report the errors and exit without creating a package. Fix any validation errors and run the packaging command again. + +### Step 6: Iterate + +After testing the skill, users may request improvements. Often this happens right after using the skill, with fresh context of how the skill performed. + +**Iteration workflow:** + +1. Use the skill on real tasks +2. Notice struggles or inefficiencies +3. Identify how SKILL.md or bundled resources should be updated +4. Implement changes and test again diff --git a/inst/skills/skill-creator/references/output-patterns.md b/inst/skills/skill-creator/references/output-patterns.md new file mode 100644 index 00000000..073ddda5 --- /dev/null +++ b/inst/skills/skill-creator/references/output-patterns.md @@ -0,0 +1,82 @@ +# Output Patterns + +Use these patterns when skills need to produce consistent, high-quality output. + +## Template Pattern + +Provide templates for output format. Match the level of strictness to your needs. + +**For strict requirements (like API responses or data formats):** + +```markdown +## Report structure + +ALWAYS use this exact template structure: + +# [Analysis Title] + +## Executive summary +[One-paragraph overview of key findings] + +## Key findings +- Finding 1 with supporting data +- Finding 2 with supporting data +- Finding 3 with supporting data + +## Recommendations +1. Specific actionable recommendation +2. Specific actionable recommendation +``` + +**For flexible guidance (when adaptation is useful):** + +```markdown +## Report structure + +Here is a sensible default format, but use your best judgment: + +# [Analysis Title] + +## Executive summary +[Overview] + +## Key findings +[Adapt sections based on what you discover] + +## Recommendations +[Tailor to the specific context] + +Adjust sections as needed for the specific analysis type. +``` + +## Examples Pattern + +For skills where output quality depends on seeing examples, provide input/output pairs: + +```markdown +## Commit message format + +Generate commit messages following these examples: + +**Example 1:** +Input: Added user authentication with JWT tokens +Output: +``` +feat(auth): implement JWT-based authentication + +Add login endpoint and token validation middleware +``` + +**Example 2:** +Input: Fixed bug where dates displayed incorrectly in reports +Output: +``` +fix(reports): correct date formatting in timezone conversion + +Use UTC timestamps consistently across report generation +``` + +Follow this style: type(scope): brief description, then detailed explanation. +``` + +Examples help Claude understand the desired style and level of detail more clearly than descriptions alone. diff --git a/inst/skills/skill-creator/references/workflows.md b/inst/skills/skill-creator/references/workflows.md new file mode 100644 index 00000000..a350c3cc --- /dev/null +++ b/inst/skills/skill-creator/references/workflows.md @@ -0,0 +1,28 @@ +# Workflow Patterns + +## Sequential Workflows + +For complex tasks, break operations into clear, sequential steps. It is often helpful to give Claude an overview of the process towards the beginning of SKILL.md: + +```markdown +Filling a PDF form involves these steps: + +1. Analyze the form (run analyze_form.py) +2. Create field mapping (edit fields.json) +3. Validate mapping (run validate_fields.py) +4. Fill the form (run fill_form.py) +5. Verify output (run verify_output.py) +``` + +## Conditional Workflows + +For tasks with branching logic, guide Claude through decision points: + +```markdown +1. Determine the modification type: + **Creating new content?** → Follow "Creation workflow" below + **Editing existing content?** → Follow "Editing workflow" below + +2. Creation workflow: [steps] +3. Editing workflow: [steps] +``` \ No newline at end of file diff --git a/inst/skills/skill-creator/scripts/init_skill.py b/inst/skills/skill-creator/scripts/init_skill.py new file mode 100644 index 00000000..329ad4e5 --- /dev/null +++ b/inst/skills/skill-creator/scripts/init_skill.py @@ -0,0 +1,303 @@ +#!/usr/bin/env python3 +""" +Skill Initializer - Creates a new skill from template + +Usage: + init_skill.py --path + +Examples: + init_skill.py my-new-skill --path skills/public + init_skill.py my-api-helper --path skills/private + init_skill.py custom-skill --path /custom/location +""" + +import sys +from pathlib import Path + + +SKILL_TEMPLATE = """--- +name: {skill_name} +description: [TODO: Complete and informative explanation of what the skill does and when to use it. Include WHEN to use this skill - specific scenarios, file types, or tasks that trigger it.] +--- + +# {skill_title} + +## Overview + +[TODO: 1-2 sentences explaining what this skill enables] + +## Structuring This Skill + +[TODO: Choose the structure that best fits this skill's purpose. Common patterns: + +**1. Workflow-Based** (best for sequential processes) +- Works well when there are clear step-by-step procedures +- Example: DOCX skill with "Workflow Decision Tree" → "Reading" → "Creating" → "Editing" +- Structure: ## Overview → ## Workflow Decision Tree → ## Step 1 → ## Step 2... + +**2. Task-Based** (best for tool collections) +- Works well when the skill offers different operations/capabilities +- Example: PDF skill with "Quick Start" → "Merge PDFs" → "Split PDFs" → "Extract Text" +- Structure: ## Overview → ## Quick Start → ## Task Category 1 → ## Task Category 2... + +**3. Reference/Guidelines** (best for standards or specifications) +- Works well for brand guidelines, coding standards, or requirements +- Example: Brand styling with "Brand Guidelines" → "Colors" → "Typography" → "Features" +- Structure: ## Overview → ## Guidelines → ## Specifications → ## Usage... + +**4. Capabilities-Based** (best for integrated systems) +- Works well when the skill provides multiple interrelated features +- Example: Product Management with "Core Capabilities" → numbered capability list +- Structure: ## Overview → ## Core Capabilities → ### 1. Feature → ### 2. Feature... + +Patterns can be mixed and matched as needed. Most skills combine patterns (e.g., start with task-based, add workflow for complex operations). + +Delete this entire "Structuring This Skill" section when done - it's just guidance.] + +## [TODO: Replace with the first main section based on chosen structure] + +[TODO: Add content here. See examples in existing skills: +- Code samples for technical skills +- Decision trees for complex workflows +- Concrete examples with realistic user requests +- References to scripts/templates/references as needed] + +## Resources + +This skill includes example resource directories that demonstrate how to organize different types of bundled resources: + +### scripts/ +Executable code (Python/Bash/etc.) that can be run directly to perform specific operations. + +**Examples from other skills:** +- PDF skill: `fill_fillable_fields.py`, `extract_form_field_info.py` - utilities for PDF manipulation +- DOCX skill: `document.py`, `utilities.py` - Python modules for document processing + +**Appropriate for:** Python scripts, shell scripts, or any executable code that performs automation, data processing, or specific operations. + +**Note:** Scripts may be executed without loading into context, but can still be read by Claude for patching or environment adjustments. + +### references/ +Documentation and reference material intended to be loaded into context to inform Claude's process and thinking. + +**Examples from other skills:** +- Product management: `communication.md`, `context_building.md` - detailed workflow guides +- BigQuery: API reference documentation and query examples +- Finance: Schema documentation, company policies + +**Appropriate for:** In-depth documentation, API references, database schemas, comprehensive guides, or any detailed information that Claude should reference while working. + +### assets/ +Files not intended to be loaded into context, but rather used within the output Claude produces. + +**Examples from other skills:** +- Brand styling: PowerPoint template files (.pptx), logo files +- Frontend builder: HTML/React boilerplate project directories +- Typography: Font files (.ttf, .woff2) + +**Appropriate for:** Templates, boilerplate code, document templates, images, icons, fonts, or any files meant to be copied or used in the final output. + +--- + +**Any unneeded directories can be deleted.** Not every skill requires all three types of resources. +""" + +EXAMPLE_SCRIPT = '''#!/usr/bin/env python3 +""" +Example helper script for {skill_name} + +This is a placeholder script that can be executed directly. +Replace with actual implementation or delete if not needed. + +Example real scripts from other skills: +- pdf/scripts/fill_fillable_fields.py - Fills PDF form fields +- pdf/scripts/convert_pdf_to_images.py - Converts PDF pages to images +""" + +def main(): + print("This is an example script for {skill_name}") + # TODO: Add actual script logic here + # This could be data processing, file conversion, API calls, etc. + +if __name__ == "__main__": + main() +''' + +EXAMPLE_REFERENCE = """# Reference Documentation for {skill_title} + +This is a placeholder for detailed reference documentation. +Replace with actual reference content or delete if not needed. + +Example real reference docs from other skills: +- product-management/references/communication.md - Comprehensive guide for status updates +- product-management/references/context_building.md - Deep-dive on gathering context +- bigquery/references/ - API references and query examples + +## When Reference Docs Are Useful + +Reference docs are ideal for: +- Comprehensive API documentation +- Detailed workflow guides +- Complex multi-step processes +- Information too lengthy for main SKILL.md +- Content that's only needed for specific use cases + +## Structure Suggestions + +### API Reference Example +- Overview +- Authentication +- Endpoints with examples +- Error codes +- Rate limits + +### Workflow Guide Example +- Prerequisites +- Step-by-step instructions +- Common patterns +- Troubleshooting +- Best practices +""" + +EXAMPLE_ASSET = """# Example Asset File + +This placeholder represents where asset files would be stored. +Replace with actual asset files (templates, images, fonts, etc.) or delete if not needed. + +Asset files are NOT intended to be loaded into context, but rather used within +the output Claude produces. + +Example asset files from other skills: +- Brand guidelines: logo.png, slides_template.pptx +- Frontend builder: hello-world/ directory with HTML/React boilerplate +- Typography: custom-font.ttf, font-family.woff2 +- Data: sample_data.csv, test_dataset.json + +## Common Asset Types + +- Templates: .pptx, .docx, boilerplate directories +- Images: .png, .jpg, .svg, .gif +- Fonts: .ttf, .otf, .woff, .woff2 +- Boilerplate code: Project directories, starter files +- Icons: .ico, .svg +- Data files: .csv, .json, .xml, .yaml + +Note: This is a text placeholder. Actual assets can be any file type. +""" + + +def title_case_skill_name(skill_name): + """Convert hyphenated skill name to Title Case for display.""" + return ' '.join(word.capitalize() for word in skill_name.split('-')) + + +def init_skill(skill_name, path): + """ + Initialize a new skill directory with template SKILL.md. + + Args: + skill_name: Name of the skill + path: Path where the skill directory should be created + + Returns: + Path to created skill directory, or None if error + """ + # Determine skill directory path + skill_dir = Path(path).resolve() / skill_name + + # Check if directory already exists + if skill_dir.exists(): + print(f"❌ Error: Skill directory already exists: {skill_dir}") + return None + + # Create skill directory + try: + skill_dir.mkdir(parents=True, exist_ok=False) + print(f"✅ Created skill directory: {skill_dir}") + except Exception as e: + print(f"❌ Error creating directory: {e}") + return None + + # Create SKILL.md from template + skill_title = title_case_skill_name(skill_name) + skill_content = SKILL_TEMPLATE.format( + skill_name=skill_name, + skill_title=skill_title + ) + + skill_md_path = skill_dir / 'SKILL.md' + try: + skill_md_path.write_text(skill_content) + print("✅ Created SKILL.md") + except Exception as e: + print(f"❌ Error creating SKILL.md: {e}") + return None + + # Create resource directories with example files + try: + # Create scripts/ directory with example script + scripts_dir = skill_dir / 'scripts' + scripts_dir.mkdir(exist_ok=True) + example_script = scripts_dir / 'example.py' + example_script.write_text(EXAMPLE_SCRIPT.format(skill_name=skill_name)) + example_script.chmod(0o755) + print("✅ Created scripts/example.py") + + # Create references/ directory with example reference doc + references_dir = skill_dir / 'references' + references_dir.mkdir(exist_ok=True) + example_reference = references_dir / 'api_reference.md' + example_reference.write_text(EXAMPLE_REFERENCE.format(skill_title=skill_title)) + print("✅ Created references/api_reference.md") + + # Create assets/ directory with example asset placeholder + assets_dir = skill_dir / 'assets' + assets_dir.mkdir(exist_ok=True) + example_asset = assets_dir / 'example_asset.txt' + example_asset.write_text(EXAMPLE_ASSET) + print("✅ Created assets/example_asset.txt") + except Exception as e: + print(f"❌ Error creating resource directories: {e}") + return None + + # Print next steps + print(f"\n✅ Skill '{skill_name}' initialized successfully at {skill_dir}") + print("\nNext steps:") + print("1. Edit SKILL.md to complete the TODO items and update the description") + print("2. Customize or delete the example files in scripts/, references/, and assets/") + print("3. Run the validator when ready to check the skill structure") + + return skill_dir + + +def main(): + if len(sys.argv) < 4 or sys.argv[2] != '--path': + print("Usage: init_skill.py --path ") + print("\nSkill name requirements:") + print(" - Hyphen-case identifier (e.g., 'data-analyzer')") + print(" - Lowercase letters, digits, and hyphens only") + print(" - Max 40 characters") + print(" - Must match directory name exactly") + print("\nExamples:") + print(" init_skill.py my-new-skill --path skills/public") + print(" init_skill.py my-api-helper --path skills/private") + print(" init_skill.py custom-skill --path /custom/location") + sys.exit(1) + + skill_name = sys.argv[1] + path = sys.argv[3] + + print(f"🚀 Initializing skill: {skill_name}") + print(f" Location: {path}") + print() + + result = init_skill(skill_name, path) + + if result: + sys.exit(0) + else: + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/inst/skills/skill-creator/scripts/package_skill.py b/inst/skills/skill-creator/scripts/package_skill.py new file mode 100644 index 00000000..5cd36cb1 --- /dev/null +++ b/inst/skills/skill-creator/scripts/package_skill.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +""" +Skill Packager - Creates a distributable .skill file of a skill folder + +Usage: + python utils/package_skill.py [output-directory] + +Example: + python utils/package_skill.py skills/public/my-skill + python utils/package_skill.py skills/public/my-skill ./dist +""" + +import sys +import zipfile +from pathlib import Path +from quick_validate import validate_skill + + +def package_skill(skill_path, output_dir=None): + """ + Package a skill folder into a .skill file. + + Args: + skill_path: Path to the skill folder + output_dir: Optional output directory for the .skill file (defaults to current directory) + + Returns: + Path to the created .skill file, or None if error + """ + skill_path = Path(skill_path).resolve() + + # Validate skill folder exists + if not skill_path.exists(): + print(f"❌ Error: Skill folder not found: {skill_path}") + return None + + if not skill_path.is_dir(): + print(f"❌ Error: Path is not a directory: {skill_path}") + return None + + # Validate SKILL.md exists + skill_md = skill_path / "SKILL.md" + if not skill_md.exists(): + print(f"❌ Error: SKILL.md not found in {skill_path}") + return None + + # Run validation before packaging + print("🔍 Validating skill...") + valid, message = validate_skill(skill_path) + if not valid: + print(f"❌ Validation failed: {message}") + print(" Please fix the validation errors before packaging.") + return None + print(f"✅ {message}\n") + + # Determine output location + skill_name = skill_path.name + if output_dir: + output_path = Path(output_dir).resolve() + output_path.mkdir(parents=True, exist_ok=True) + else: + output_path = Path.cwd() + + skill_filename = output_path / f"{skill_name}.skill" + + # Create the .skill file (zip format) + try: + with zipfile.ZipFile(skill_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: + # Walk through the skill directory + for file_path in skill_path.rglob('*'): + if file_path.is_file(): + # Calculate the relative path within the zip + arcname = file_path.relative_to(skill_path.parent) + zipf.write(file_path, arcname) + print(f" Added: {arcname}") + + print(f"\n✅ Successfully packaged skill to: {skill_filename}") + return skill_filename + + except Exception as e: + print(f"❌ Error creating .skill file: {e}") + return None + + +def main(): + if len(sys.argv) < 2: + print("Usage: python utils/package_skill.py [output-directory]") + print("\nExample:") + print(" python utils/package_skill.py skills/public/my-skill") + print(" python utils/package_skill.py skills/public/my-skill ./dist") + sys.exit(1) + + skill_path = sys.argv[1] + output_dir = sys.argv[2] if len(sys.argv) > 2 else None + + print(f"📦 Packaging skill: {skill_path}") + if output_dir: + print(f" Output directory: {output_dir}") + print() + + result = package_skill(skill_path, output_dir) + + if result: + sys.exit(0) + else: + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/inst/skills/skill-creator/scripts/quick_validate.py b/inst/skills/skill-creator/scripts/quick_validate.py new file mode 100644 index 00000000..d9fbeb75 --- /dev/null +++ b/inst/skills/skill-creator/scripts/quick_validate.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +""" +Quick validation script for skills - minimal version +""" + +import sys +import os +import re +import yaml +from pathlib import Path + +def validate_skill(skill_path): + """Basic validation of a skill""" + skill_path = Path(skill_path) + + # Check SKILL.md exists + skill_md = skill_path / 'SKILL.md' + if not skill_md.exists(): + return False, "SKILL.md not found" + + # Read and validate frontmatter + content = skill_md.read_text() + if not content.startswith('---'): + return False, "No YAML frontmatter found" + + # Extract frontmatter + match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL) + if not match: + return False, "Invalid frontmatter format" + + frontmatter_text = match.group(1) + + # Parse YAML frontmatter + try: + frontmatter = yaml.safe_load(frontmatter_text) + if not isinstance(frontmatter, dict): + return False, "Frontmatter must be a YAML dictionary" + except yaml.YAMLError as e: + return False, f"Invalid YAML in frontmatter: {e}" + + # Define allowed properties + ALLOWED_PROPERTIES = {'name', 'description', 'license', 'allowed-tools', 'metadata'} + + # Check for unexpected properties (excluding nested keys under metadata) + unexpected_keys = set(frontmatter.keys()) - ALLOWED_PROPERTIES + if unexpected_keys: + return False, ( + f"Unexpected key(s) in SKILL.md frontmatter: {', '.join(sorted(unexpected_keys))}. " + f"Allowed properties are: {', '.join(sorted(ALLOWED_PROPERTIES))}" + ) + + # Check required fields + if 'name' not in frontmatter: + return False, "Missing 'name' in frontmatter" + if 'description' not in frontmatter: + return False, "Missing 'description' in frontmatter" + + # Extract name for validation + name = frontmatter.get('name', '') + if not isinstance(name, str): + return False, f"Name must be a string, got {type(name).__name__}" + name = name.strip() + if name: + # Check naming convention (hyphen-case: lowercase with hyphens) + if not re.match(r'^[a-z0-9-]+$', name): + return False, f"Name '{name}' should be hyphen-case (lowercase letters, digits, and hyphens only)" + if name.startswith('-') or name.endswith('-') or '--' in name: + return False, f"Name '{name}' cannot start/end with hyphen or contain consecutive hyphens" + # Check name length (max 64 characters per spec) + if len(name) > 64: + return False, f"Name is too long ({len(name)} characters). Maximum is 64 characters." + + # Extract and validate description + description = frontmatter.get('description', '') + if not isinstance(description, str): + return False, f"Description must be a string, got {type(description).__name__}" + description = description.strip() + if description: + # Check for angle brackets + if '<' in description or '>' in description: + return False, "Description cannot contain angle brackets (< or >)" + # Check description length (max 1024 characters per spec) + if len(description) > 1024: + return False, f"Description is too long ({len(description)} characters). Maximum is 1024 characters." + + return True, "Skill is valid!" + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python quick_validate.py ") + sys.exit(1) + + valid, message = validate_skill(sys.argv[1]) + print(message) + sys.exit(0 if valid else 1) \ No newline at end of file diff --git a/man/btw_tool_fetch_skill.Rd b/man/btw_tool_fetch_skill.Rd new file mode 100644 index 00000000..dcaeaea6 --- /dev/null +++ b/man/btw_tool_fetch_skill.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_tool_fetch_skill} +\alias{btw_tool_fetch_skill} +\title{Tool: Fetch a skill} +\usage{ +btw_tool_fetch_skill(skill_name, `_intent` = "") +} +\arguments{ +\item{skill_name}{The name of the skill to fetch.} + +\item{_intent}{An optional string describing the intent of the tool use. +When the tool is used by an LLM, the model will use this argument to +explain why it called the tool.} +} +\value{ +A \code{btw_tool_result} containing the skill instructions and a listing +of bundled resources with their paths. +} +\description{ +Fetch a skill's instructions and list its bundled resources. + +Skills are modular capabilities that extend Claude's functionality with +specialized knowledge, workflows, and tools. Each skill is a directory +containing a \code{SKILL.md} file with instructions and optional bundled +resources (scripts, references, assets). +} +\concept{skills} diff --git a/man/btw_tools.Rd b/man/btw_tools.Rd index fc251815..6471487d 100644 --- a/man/btw_tools.Rd +++ b/man/btw_tools.Rd @@ -120,6 +120,13 @@ this function have access to the tools: } +\subsection{Group: skills}{\tabular{ll}{ + Name \tab Description \cr + \code{\link[=btw_tool_fetch_skill]{btw_tool_fetch_skill()}} \tab Fetch a skill's instructions and list its bundled resources. \cr +} + +} + \subsection{Group: web}{\tabular{ll}{ Name \tab Description \cr \code{\link[=btw_tool_web_read_url]{btw_tool_web_read_url()}} \tab Read a web page and convert it to Markdown format. \cr From b2ccb6b3f730af2950bf816bd1bf8468ca215a1d Mon Sep 17 00:00:00 2001 From: Simon Couch Date: Thu, 18 Dec 2025 12:47:24 -0600 Subject: [PATCH 02/28] only snapshot skills prompt once --- tests/testthat/_snaps/tool_skills.md | 27 +++++++++++++++++++++++++++ tests/testthat/test-btw_client.R | 1 + tests/testthat/test-tool_skills.R | 17 +++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/testthat/_snaps/tool_skills.md create mode 100644 tests/testthat/test-tool_skills.R diff --git a/tests/testthat/_snaps/tool_skills.md b/tests/testthat/_snaps/tool_skills.md new file mode 100644 index 00000000..025902c4 --- /dev/null +++ b/tests/testthat/_snaps/tool_skills.md @@ -0,0 +1,27 @@ +# btw_skills_system_prompt() works + + Code + cat(btw_skills_system_prompt()) + Output + ## Skills + + You have access to specialized skills that provide detailed guidance for specific tasks. Skills are loaded on-demand to provide domain-specific expertise without consuming context until needed. + + ### Using Skills + + 1. **Check available skills**: Review the `` listing below + 2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description + 3. **Access resources**: After fetching, use file read tools to access references or bash/code tools to run bundled scripts + + Skills may include bundled resources: + - **Scripts**: Executable code (R, Python, bash) for automated tasks + - **References**: Additional documentation to consult as needed + - **Assets**: Templates and files for use in outputs + + + + skill-creator + Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations. + + + diff --git a/tests/testthat/test-btw_client.R b/tests/testthat/test-btw_client.R index 2d3501e2..393877ec 100644 --- a/tests/testthat/test-btw_client.R +++ b/tests/testthat/test-btw_client.R @@ -1,4 +1,5 @@ local_enable_tools() +local_mocked_bindings(btw_skills_system_prompt = function(...) "") withr::local_options(btw.client.quiet = TRUE) describe("btw_client() chat client", { diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R new file mode 100644 index 00000000..5d31ab04 --- /dev/null +++ b/tests/testthat/test-tool_skills.R @@ -0,0 +1,17 @@ +test_that("btw_skills_system_prompt() works", { + skip_if_not_snapshot_env() + expect_snapshot(cat(btw_skills_system_prompt())) +}) + +test_that("skills prompt is included in btw_client() system prompt", { + withr::local_envvar(list(ANTHROPIC_API_KEY = "beep")) + + with_mocked_platform(ide = "rstudio", { + chat <- btw_client(path_btw = FALSE) + }) + + system_prompt <- chat$get_system_prompt() + + expect_match(system_prompt, "## Skills", fixed = TRUE) + expect_match(system_prompt, "skill-creator", fixed = TRUE) +}) From 8d4af4dddc86bc7cce05e130c806306ec026432a Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Sun, 15 Feb 2026 17:33:27 -0500 Subject: [PATCH 03/28] refactor(skills): switch from yaml to frontmatter package for SKILL.md parsing Use `frontmatter::read_front_matter()` (already in Imports) instead of manual YAML extraction with `yaml::yaml.load()`. This aligns with the pattern used in btw_client.R and simplifies both `extract_skill_metadata()` and `btw_tool_fetch_skill_impl()`. Removes `yaml` from Suggests since it's no longer used anywhere. --- DESCRIPTION | 3 +-- R/tool-skills.R | 37 +++++++------------------------------ 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f50fe322..6f3c4b37 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -73,8 +73,7 @@ Suggests: shinychat (>= 0.3.0), tibble, testthat (>= 3.0.0), - usethis, - yaml + usethis Config/Needs/website: tidyverse/tidytemplate Config/testthat/edition: 3 Config/testthat/parallel: true diff --git a/R/tool-skills.R b/R/tool-skills.R index f39458e2..f4dd18bd 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -38,20 +38,8 @@ btw_tool_fetch_skill_impl <- function(skill_name) { ) } - skill_content <- readLines(skill_info$path, warn = FALSE) - - content_start <- 1 - if (length(skill_content) > 0 && skill_content[1] == "---") { - yaml_end <- which(skill_content == "---") - if (length(yaml_end) >= 2) { - content_start <- yaml_end[2] + 1 - } - } - - skill_text <- paste( - skill_content[content_start:length(skill_content)], - collapse = "\n" - ) + fm <- frontmatter::read_front_matter(skill_info$path) + skill_text <- fm$body %||% "" resources <- list_skill_resources(skill_info$base_dir) resources_listing <- format_resources_listing(resources, skill_info$base_dir) @@ -64,6 +52,7 @@ btw_tool_fetch_skill_impl <- function(skill_name) { name = skill_name, path = skill_info$path, base_dir = skill_info$base_dir, + metadata = fm$data, resources = resources ), display = list( @@ -177,23 +166,11 @@ find_skill <- function(skill_name) { } extract_skill_metadata <- function(skill_path) { - lines <- readLines(skill_path, warn = FALSE) - - if (length(lines) == 0 || lines[1] != "---") { - return(list()) - } - - yaml_end_indices <- which(lines == "---") - if (length(yaml_end_indices) < 2) { - return(list()) - } - - yaml_lines <- lines[2:(yaml_end_indices[2] - 1)] - yaml_text <- paste(yaml_lines, collapse = "\n") - - check_installed("yaml") tryCatch( - yaml::yaml.load(yaml_text), + { + fm <- frontmatter::read_front_matter(skill_path) + fm$data %||% list() + }, error = function(e) list() ) } From 8920cf749417604b498afcd65ac52d0f114f3bac Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Sun, 15 Feb 2026 17:34:46 -0500 Subject: [PATCH 04/28] feat(skills): add spec-compliant validation and enhanced metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add `validate_skill()` internal function implementing the Agent Skills spec validation rules: name format (lowercase, hyphens, max 64 chars, must match directory name), description constraints (max 1024 chars), compatibility (max 500 chars), unexpected field detection - Integrate validation into `btw_list_skills()` — invalid skills are skipped with a warning instead of silently loaded - Surface `compatibility` and `allowed-tools` fields in system prompt XML - Fix `list_files_in_subdir()` to use recursive listing for nested resource directories (e.g., assets/hello-world/) - Add skills group icon mapping in `tool_group_icon()` --- R/tool-skills.R | 156 ++++++++++++++++++++++++++++++++++++++++++++---- R/tools.R | 1 + 2 files changed, 145 insertions(+), 12 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index f4dd18bd..7f9de9ba 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -132,16 +132,36 @@ btw_list_skills <- function() { for (subdir in subdirs) { skill_md_path <- file.path(subdir, "SKILL.md") - if (file.exists(skill_md_path)) { - metadata <- extract_skill_metadata(skill_md_path) - skill_name <- basename(subdir) - - all_skills[[skill_name]] <- list( - name = skill_name, - description = metadata$description %||% "No description available", - path = skill_md_path - ) + if (!file.exists(skill_md_path)) { + next + } + + validation <- validate_skill(subdir) + if (!validation$valid) { + cli::cli_warn(c( + "Skipping invalid skill in {.path {subdir}}.", + set_names(validation$issues, rep("!" , length(validation$issues))) + )) + next + } + + metadata <- extract_skill_metadata(skill_md_path) + skill_name <- basename(subdir) + + skill_entry <- list( + name = skill_name, + description = metadata$description %||% "No description available", + path = skill_md_path + ) + + if (!is.null(metadata$compatibility)) { + skill_entry$compatibility <- metadata$compatibility + } + if (!is.null(metadata[["allowed-tools"]])) { + skill_entry$allowed_tools <- metadata[["allowed-tools"]] } + + all_skills[[skill_name]] <- skill_entry } } @@ -175,6 +195,111 @@ extract_skill_metadata <- function(skill_path) { ) } +# Skill Validation --------------------------------------------------------- + +validate_skill <- function(skill_dir) { + skill_dir <- normalizePath(skill_dir, mustWork = FALSE) + issues <- character() + + skill_md_path <- file.path(skill_dir, "SKILL.md") + if (!file.exists(skill_md_path)) { + return(list( + valid = FALSE, + issues = "SKILL.md not found." + )) + } + + metadata <- tryCatch( + { + fm <- frontmatter::read_front_matter(skill_md_path) + fm$data + }, + error = function(e) { + issues <<- c(issues, sprintf("Failed to parse frontmatter: %s", e$message)) + NULL + } + ) + + if (is.null(metadata)) { + issues <- c(issues, "No YAML frontmatter found.") + return(list(valid = FALSE, issues = issues)) + } + + if (!is.list(metadata)) { + return(list( + valid = FALSE, + issues = "Frontmatter must be a YAML mapping." + )) + } + + # Check for unexpected properties + allowed_fields <- c("name", "description", "license", "compatibility", "metadata", "allowed-tools") + unexpected <- setdiff(names(metadata), allowed_fields) + if (length(unexpected) > 0) { + issues <- c(issues, sprintf( + "Unexpected frontmatter field(s): %s. Allowed fields: %s.", + paste(unexpected, collapse = ", "), + paste(allowed_fields, collapse = ", ") + )) + } + + # Validate name + name <- metadata$name + dir_name <- basename(skill_dir) + if (is.null(name) || !is.character(name) || !nzchar(name)) { + issues <- c(issues, "Missing or empty 'name' field in frontmatter.") + } else { + if (nchar(name) > 64) { + issues <- c(issues, sprintf("Name is too long (%d characters, max 64).", nchar(name))) + } + if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { + issues <- c(issues, sprintf( + "Name '%s' must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen.", + name + )) + } + if (grepl("--", name)) { + issues <- c(issues, sprintf("Name '%s' must not contain consecutive hyphens.", name)) + } + if (name != dir_name) { + issues <- c(issues, sprintf( + "Name '%s' in frontmatter does not match directory name '%s'.", + name, dir_name + )) + } + } + + # Validate description + description <- metadata$description + if (is.null(description) || !is.character(description) || !nzchar(description)) { + issues <- c(issues, "Missing or empty 'description' field in frontmatter.") + } else if (nchar(description) > 1024) { + issues <- c(issues, sprintf( + "Description is too long (%d characters, max 1024).", + nchar(description) + )) + } + + # Validate optional fields + if (!is.null(metadata$compatibility) && is.character(metadata$compatibility)) { + if (nchar(metadata$compatibility) > 500) { + issues <- c(issues, sprintf( + "Compatibility field is too long (%d characters, max 500).", + nchar(metadata$compatibility) + )) + } + } + + if (!is.null(metadata$metadata) && !is.list(metadata$metadata)) { + issues <- c(issues, "The 'metadata' field must be a key-value mapping.") + } + + list( + valid = length(issues) == 0, + issues = issues + ) +} + # Skill Resources ---------------------------------------------------------- list_skill_resources <- function(skill_dir) { @@ -190,7 +315,7 @@ list_files_in_subdir <- function(base_dir, subdir) { if (!dir.exists(full_path)) { return(character(0)) } - list.files(full_path, full.names = FALSE) + list.files(full_path, full.names = FALSE, recursive = TRUE) } has_skill_resources <- function(resources) { @@ -248,11 +373,18 @@ btw_skills_system_prompt <- function() { skill_items <- vapply( skills, function(skill) { - sprintf( - "\n%s\n%s\n", + parts <- sprintf( + "\n%s\n%s", skill$name, skill$description ) + if (!is.null(skill$compatibility)) { + parts <- paste0(parts, sprintf("\n%s", skill$compatibility)) + } + if (!is.null(skill$allowed_tools)) { + parts <- paste0(parts, sprintf("\n%s", skill$allowed_tools)) + } + paste0(parts, "\n") }, character(1) ) diff --git a/R/tools.R b/R/tools.R index cdc7bd38..584e8076 100644 --- a/R/tools.R +++ b/R/tools.R @@ -220,6 +220,7 @@ tool_group_icon <- function(group, default = NULL) { "ide" = tool_icon("code-blocks"), "pkg" = tool_icon("package"), "sessioninfo" = tool_icon("screen-search-desktop"), + "skills" = tool_icon("quick-reference"), "web" = tool_icon("globe-book"), if (!is.null(default)) tool_icon(default) ) From aecf0688138f4c476dc26098bb4fa1ad54bd63ff Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Sun, 15 Feb 2026 17:35:55 -0500 Subject: [PATCH 05/28] feat(skills): add btw_skill_create(), btw_skill_validate(), btw_skill_install() User-facing R functions for skill management: - btw_skill_create(): Initialize a new skill directory with SKILL.md template and optional resource directories (scripts/, references/, assets/). Supports project-level and user-level scopes. - btw_skill_validate(): Validate a skill directory against the Agent Skills spec, reporting issues with name format, description, and frontmatter structure. - btw_skill_install(): Install a skill from a .skill file (ZIP archive) or directory into project or user skill locations. Validates before installing. --- NAMESPACE | 3 + R/tool-skills.R | 240 ++++++++++++++++++++++++++++++++++++ man/btw_skill_create.Rd | 43 +++++++ man/btw_skill_install.Rd | 32 +++++ man/btw_skill_validate.Rd | 28 +++++ man/btw_tool_fetch_skill.Rd | 6 + 6 files changed, 352 insertions(+) create mode 100644 man/btw_skill_create.Rd create mode 100644 man/btw_skill_install.Rd create mode 100644 man/btw_skill_validate.Rd diff --git a/NAMESPACE b/NAMESPACE index a4ebf311..026a5460 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,6 +26,9 @@ export(btw_app) export(btw_client) export(btw_mcp_server) export(btw_mcp_session) +export(btw_skill_create) +export(btw_skill_install) +export(btw_skill_validate) export(btw_task_create_btw_md) export(btw_task_create_readme) export(btw_this) diff --git a/R/tool-skills.R b/R/tool-skills.R index 7f9de9ba..02e708fe 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -396,3 +396,243 @@ btw_skills_system_prompt <- function() { "\n" ) } + +# User-Facing Skill Management --------------------------------------------- + +#' Create a new skill +#' +#' @description +#' Initialize a new skill directory following the +#' [Agent Skills specification](https://agentskills.io). Creates a `SKILL.md` +#' file with proper YAML frontmatter and optionally creates resource +#' directories (`scripts/`, `references/`, `assets/`). +#' +#' @param name The skill name. Must be a valid skill name: lowercase letters, +#' numbers, and hyphens only, 1-64 characters, must not start or end with a +#' hyphen, and must not contain consecutive hyphens. +#' @param description A description of what the skill does and when to use it. +#' Maximum 1024 characters. +#' @param scope Where to create the skill. One of: +#' - `"project"` (default): Creates in `.btw/skills/` in the current +#' working directory +#' - `"user"`: Creates in the user-level skills directory +#' - A directory path: Creates the skill directory inside this path +#' @param resources Logical. If `TRUE` (the default), creates empty +#' `scripts/`, `references/`, and `assets/` subdirectories. +#' +#' @return The path to the created skill directory, invisibly. +#' +#' @family skills +#' @export +btw_skill_create <- function( + name, + description = "", + scope = "project", + resources = TRUE +) { + check_name(name) + check_string(description) + check_string(scope) + check_bool(resources) + + # Validate name format + if (nchar(name) > 64) { + cli::cli_abort("Skill name must be at most 64 characters (got {nchar(name)}).") + } + if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { + cli::cli_abort( + "Skill name must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen." + ) + } + if (grepl("--", name)) { + cli::cli_abort("Skill name must not contain consecutive hyphens.") + } + + # Resolve target directory + parent_dir <- switch( + scope, + project = file.path(getwd(), ".btw", "skills"), + user = file.path(tools::R_user_dir("btw", "config"), "skills"), + scope + ) + + skill_dir <- file.path(parent_dir, name) + + if (dir.exists(skill_dir)) { + cli::cli_abort("Skill directory already exists: {.path {skill_dir}}") + } + + dir.create(skill_dir, recursive = TRUE, showWarnings = FALSE) + + # Generate SKILL.md + skill_title <- gsub("-", " ", name) + skill_title <- paste0( + toupper(substring(skill_title, 1, 1)), + substring(skill_title, 2) + ) + + description_line <- if (nzchar(description)) { + description + } else { + "TODO: Describe what this skill does and when to use it." + } + + skill_md_content <- paste0( + "---\n", + "name: ", name, "\n", + "description: ", description_line, "\n", + "---\n", + "\n", + "# ", skill_title, "\n", + "\n", + "TODO: Add skill instructions here.\n" + ) + + write_lines(skill_md_content, file.path(skill_dir, "SKILL.md")) + + if (resources) { + dir.create(file.path(skill_dir, "scripts"), showWarnings = FALSE) + dir.create(file.path(skill_dir, "references"), showWarnings = FALSE) + dir.create(file.path(skill_dir, "assets"), showWarnings = FALSE) + } + + cli::cli_inform(c( + "v" = "Created skill {.val {name}} at {.path {skill_dir}}", + "i" = "Edit {.file {file.path(skill_dir, 'SKILL.md')}} to add instructions." + )) + + invisible(skill_dir) +} + +#' Validate a skill +#' +#' @description +#' Validate a skill directory against the +#' [Agent Skills specification](https://agentskills.io). Checks that the +#' `SKILL.md` file exists, has valid YAML frontmatter, and that required +#' fields follow the specification's naming and format rules. +#' +#' @param path Path to a skill directory (containing a `SKILL.md` file). +#' +#' @return A list with `valid` (logical) and `issues` (character vector of +#' validation messages), invisibly. Issues are also printed to the console. +#' +#' @family skills +#' @export +btw_skill_validate <- function(path = ".") { + check_string(path) + + path <- normalizePath(path, mustWork = FALSE) + if (!dir.exists(path)) { + cli::cli_abort("Directory does not exist: {.path {path}}") + } + + result <- validate_skill(path) + + if (result$valid) { + cli::cli_inform(c("v" = "Skill at {.path {path}} is valid.")) + } else { + cli::cli_inform(c( + "x" = "Skill at {.path {path}} has validation issues:", + set_names(result$issues, rep("!", length(result$issues))) + )) + } + + invisible(result) +} + +#' Install a skill +#' +#' @description +#' Install a skill from a `.skill` file (ZIP archive) or a directory into +#' a skill location where btw can discover it. +#' +#' @param source Path to a `.skill` file or a skill directory. +#' @param scope Where to install the skill. One of: +#' - `"project"` (default): Installs to `.btw/skills/` in the current +#' working directory +#' - `"user"`: Installs to the user-level skills directory +#' +#' @return The path to the installed skill directory, invisibly. +#' +#' @family skills +#' @export +btw_skill_install <- function(source, scope = "project") { + check_string(source) + check_string(scope) + + source <- normalizePath(source, mustWork = FALSE) + if (!file.exists(source) && !dir.exists(source)) { + cli::cli_abort("Source not found: {.path {source}}") + } + + # Determine target directory + target_parent <- switch( + scope, + project = file.path(getwd(), ".btw", "skills"), + user = file.path(tools::R_user_dir("btw", "config"), "skills"), + cli::cli_abort("scope must be {.val project} or {.val user}, not {.val {scope}}.") + ) + + if (file.info(source)$isdir) { + # Directory source: copy it + skill_name <- basename(source) + target_dir <- file.path(target_parent, skill_name) + + if (dir.exists(target_dir)) { + cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + } + + # Validate before installing + validation <- validate_skill(source) + if (!validation$valid) { + cli::cli_abort(c( + "Cannot install invalid skill:", + set_names(validation$issues, rep("!", length(validation$issues))) + )) + } + + dir.create(target_parent, recursive = TRUE, showWarnings = FALSE) + fs::dir_copy(source, target_dir) + } else { + # File source: must be .skill (ZIP) + if (!grepl("\\.skill$", source)) { + cli::cli_abort("File source must be a {.file .skill} file (ZIP archive).") + } + + # Extract to temp, validate, then move to target + tmp_dir <- tempfile("btw_skill_") + on.exit(unlink(tmp_dir, recursive = TRUE), add = TRUE) + utils::unzip(source, exdir = tmp_dir) + + # Find the skill directory inside the extracted archive + extracted_dirs <- list.dirs(tmp_dir, full.names = TRUE, recursive = FALSE) + if (length(extracted_dirs) != 1) { + cli::cli_abort("Expected exactly one directory in the .skill archive, found {length(extracted_dirs)}.") + } + + skill_name <- basename(extracted_dirs[[1]]) + target_dir <- file.path(target_parent, skill_name) + + if (dir.exists(target_dir)) { + cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + } + + validation <- validate_skill(extracted_dirs[[1]]) + if (!validation$valid) { + cli::cli_abort(c( + "Cannot install invalid skill from {.file {source}}:", + set_names(validation$issues, rep("!", length(validation$issues))) + )) + } + + dir.create(target_parent, recursive = TRUE, showWarnings = FALSE) + fs::dir_copy(extracted_dirs[[1]], target_dir) + } + + cli::cli_inform(c( + "v" = "Installed skill {.val {skill_name}} to {.path {target_dir}}" + )) + + invisible(target_dir) +} diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd new file mode 100644 index 00000000..7b6af6af --- /dev/null +++ b/man/btw_skill_create.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_skill_create} +\alias{btw_skill_create} +\title{Create a new skill} +\usage{ +btw_skill_create(name, description = "", scope = "project", resources = TRUE) +} +\arguments{ +\item{name}{The skill name. Must be a valid skill name: lowercase letters, +numbers, and hyphens only, 1-64 characters, must not start or end with a +hyphen, and must not contain consecutive hyphens.} + +\item{description}{A description of what the skill does and when to use it. +Maximum 1024 characters.} + +\item{scope}{Where to create the skill. One of: +\itemize{ +\item \code{"project"} (default): Creates in \verb{.btw/skills/} in the current +working directory +\item \code{"user"}: Creates in the user-level skills directory +\item A directory path: Creates the skill directory inside this path +}} + +\item{resources}{Logical. If \code{TRUE} (the default), creates empty +\verb{scripts/}, \verb{references/}, and \verb{assets/} subdirectories.} +} +\value{ +The path to the created skill directory, invisibly. +} +\description{ +Initialize a new skill directory following the +\href{https://agentskills.io}{Agent Skills specification}. Creates a \code{SKILL.md} +file with proper YAML frontmatter and optionally creates resource +directories (\verb{scripts/}, \verb{references/}, \verb{assets/}). +} +\seealso{ +Other skills: +\code{\link{btw_skill_install}()}, +\code{\link{btw_skill_validate}()}, +\code{\link{btw_tool_fetch_skill}()} +} +\concept{skills} diff --git a/man/btw_skill_install.Rd b/man/btw_skill_install.Rd new file mode 100644 index 00000000..dfc6d980 --- /dev/null +++ b/man/btw_skill_install.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_skill_install} +\alias{btw_skill_install} +\title{Install a skill} +\usage{ +btw_skill_install(source, scope = "project") +} +\arguments{ +\item{source}{Path to a \code{.skill} file or a skill directory.} + +\item{scope}{Where to install the skill. One of: +\itemize{ +\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current +working directory +\item \code{"user"}: Installs to the user-level skills directory +}} +} +\value{ +The path to the installed skill directory, invisibly. +} +\description{ +Install a skill from a \code{.skill} file (ZIP archive) or a directory into +a skill location where btw can discover it. +} +\seealso{ +Other skills: +\code{\link{btw_skill_create}()}, +\code{\link{btw_skill_validate}()}, +\code{\link{btw_tool_fetch_skill}()} +} +\concept{skills} diff --git a/man/btw_skill_validate.Rd b/man/btw_skill_validate.Rd new file mode 100644 index 00000000..0a34781b --- /dev/null +++ b/man/btw_skill_validate.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_skill_validate} +\alias{btw_skill_validate} +\title{Validate a skill} +\usage{ +btw_skill_validate(path = ".") +} +\arguments{ +\item{path}{Path to a skill directory (containing a \code{SKILL.md} file).} +} +\value{ +A list with \code{valid} (logical) and \code{issues} (character vector of +validation messages), invisibly. Issues are also printed to the console. +} +\description{ +Validate a skill directory against the +\href{https://agentskills.io}{Agent Skills specification}. Checks that the +\code{SKILL.md} file exists, has valid YAML frontmatter, and that required +fields follow the specification's naming and format rules. +} +\seealso{ +Other skills: +\code{\link{btw_skill_create}()}, +\code{\link{btw_skill_install}()}, +\code{\link{btw_tool_fetch_skill}()} +} +\concept{skills} diff --git a/man/btw_tool_fetch_skill.Rd b/man/btw_tool_fetch_skill.Rd index dcaeaea6..fdf3a24a 100644 --- a/man/btw_tool_fetch_skill.Rd +++ b/man/btw_tool_fetch_skill.Rd @@ -25,4 +25,10 @@ specialized knowledge, workflows, and tools. Each skill is a directory containing a \code{SKILL.md} file with instructions and optional bundled resources (scripts, references, assets). } +\seealso{ +Other skills: +\code{\link{btw_skill_create}()}, +\code{\link{btw_skill_install}()}, +\code{\link{btw_skill_validate}()} +} \concept{skills} From b5af0b35a3719f8e69e065465b1f44960555bdd4 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Sun, 15 Feb 2026 17:37:42 -0500 Subject: [PATCH 06/28] test(skills): comprehensive tests for skill validation, discovery, and management 77 tests covering: - validate_skill(): name format rules (uppercase, hyphens, length, directory mismatch), description constraints, optional fields, unexpected fields - btw_list_skills(): discovery, invalid skill warnings, metadata extraction - find_skill() and extract_skill_metadata() helpers - list_skill_resources(): recursive file listing, nested directories - btw_tool_fetch_skill_impl(): content fetching, resource listing, errors - btw_skills_system_prompt(): empty case, metadata including compatibility - btw_skill_create(): valid creation, resource directories, name validation - btw_skill_validate(): valid/invalid reporting, error handling - btw_skill_install(): directory and .skill file installation, validation --- tests/testthat/test-tool_skills.R | 447 ++++++++++++++++++++++++++++++ 1 file changed, 447 insertions(+) diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 5d31ab04..55db90d3 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -1,3 +1,450 @@ +# Helper to create a temp skill directory with valid SKILL.md +create_temp_skill <- function( + name = "test-skill", + description = "A test skill for unit testing.", + extra_frontmatter = "", + body = "\n# Test Skill\n\nInstructions here.\n", + dir = NULL +) { + if (is.null(dir)) { + dir <- withr::local_tempdir(.local_envir = parent.frame()) + } + + skill_dir <- file.path(dir, name) + dir.create(skill_dir, recursive = TRUE, showWarnings = FALSE) + + frontmatter <- paste0( + "---\n", + "name: ", name, "\n", + "description: ", description, "\n", + extra_frontmatter, + "---\n" + ) + + writeLines(paste0(frontmatter, body), file.path(skill_dir, "SKILL.md")) + skill_dir +} + +# Helper to mock skill directories for discovery +local_skill_dirs <- function(dirs, .env = parent.frame()) { + local_mocked_bindings( + btw_skill_directories = function() dirs, + .env = .env + ) +} + +# Validation --------------------------------------------------------------- + +test_that("validate_skill() passes for valid skill", { + skill_dir <- create_temp_skill() + result <- validate_skill(skill_dir) + expect_true(result$valid) + expect_length(result$issues, 0) +}) + +test_that("validate_skill() fails when SKILL.md is missing", { + dir <- withr::local_tempdir() + result <- validate_skill(dir) + expect_false(result$valid) + expect_match(result$issues, "SKILL.md not found") +}) + +test_that("validate_skill() fails for missing name field", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "test-skill") + dir.create(skill_dir) + writeLines("---\ndescription: A skill.\n---\nBody.", file.path(skill_dir, "SKILL.md")) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "Missing or empty 'name'", all = FALSE) +}) + +test_that("validate_skill() fails for missing description field", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "test-skill") + dir.create(skill_dir) + writeLines("---\nname: test-skill\n---\nBody.", file.path(skill_dir, "SKILL.md")) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "Missing or empty 'description'", all = FALSE) +}) + +test_that("validate_skill() fails for name with uppercase", { + skill_dir <- create_temp_skill(name = "test-skill") + # Manually override the name in SKILL.md to have uppercase + writeLines( + "---\nname: Test-Skill\ndescription: A test.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "lowercase letters", all = FALSE) +}) + +test_that("validate_skill() fails for name starting with hyphen", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "-bad-name") + dir.create(skill_dir) + writeLines( + "---\nname: -bad-name\ndescription: A test.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "must not start or end with a hyphen", all = FALSE) +}) + +test_that("validate_skill() fails for consecutive hyphens", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "bad--name") + dir.create(skill_dir) + writeLines( + "---\nname: bad--name\ndescription: A test.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "consecutive hyphens", all = FALSE) +}) + +test_that("validate_skill() fails for name exceeding 64 characters", { + long_name <- paste(rep("a", 65), collapse = "") + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, long_name) + dir.create(skill_dir) + writeLines( + paste0("---\nname: ", long_name, "\ndescription: A test.\n---\nBody."), + file.path(skill_dir, "SKILL.md") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "too long", all = FALSE) +}) + +test_that("validate_skill() fails when name doesn't match directory", { + skill_dir <- create_temp_skill(name = "test-skill") + writeLines( + "---\nname: other-name\ndescription: A test.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "does not match directory name", all = FALSE) +}) + +test_that("validate_skill() fails for description exceeding 1024 characters", { + long_desc <- paste(rep("a", 1025), collapse = "") + skill_dir <- create_temp_skill(description = long_desc) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "Description is too long", all = FALSE) +}) + +test_that("validate_skill() flags unexpected frontmatter fields", { + skill_dir <- create_temp_skill(extra_frontmatter = "bogus: true\n") + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "Unexpected frontmatter", all = FALSE) +}) + +test_that("validate_skill() accepts optional fields", { + skill_dir <- create_temp_skill( + extra_frontmatter = "license: MIT\ncompatibility: Requires git\nallowed-tools: Read Bash\nmetadata:\n author: test\n" + ) + result <- validate_skill(skill_dir) + expect_true(result$valid) +}) + +test_that("validate_skill() fails for compatibility exceeding 500 chars", { + long_compat <- paste(rep("a", 501), collapse = "") + skill_dir <- create_temp_skill( + extra_frontmatter = paste0("compatibility: ", long_compat, "\n") + ) + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "Compatibility field is too long", all = FALSE) +}) + +# Discovery ---------------------------------------------------------------- + +test_that("btw_list_skills() finds valid skills", { + dir <- withr::local_tempdir() + create_temp_skill(name = "my-skill", dir = dir) + local_skill_dirs(dir) + + skills <- btw_list_skills() + expect_length(skills, 1) + expect_equal(skills[["my-skill"]]$name, "my-skill") +}) + +test_that("btw_list_skills() skips invalid skills with warning", { + dir <- withr::local_tempdir() + + # Create a valid skill + create_temp_skill(name = "good-skill", dir = dir) + + # Create an invalid skill (missing description) + bad_dir <- file.path(dir, "bad-skill") + dir.create(bad_dir) + writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) + + local_skill_dirs(dir) + + expect_warning( + skills <- btw_list_skills(), + "Skipping invalid skill" + ) + expect_length(skills, 1) + expect_equal(skills[["good-skill"]]$name, "good-skill") +}) + +test_that("btw_list_skills() includes compatibility and allowed-tools", { + dir <- withr::local_tempdir() + create_temp_skill( + name = "fancy-skill", + dir = dir, + extra_frontmatter = "compatibility: Requires Python 3\nallowed-tools: Read Bash\n" + ) + local_skill_dirs(dir) + + skills <- btw_list_skills() + expect_equal(skills[["fancy-skill"]]$compatibility, "Requires Python 3") + expect_equal(skills[["fancy-skill"]]$allowed_tools, "Read Bash") +}) + +test_that("find_skill() returns NULL for nonexistent skill", { + dir <- withr::local_tempdir() + local_skill_dirs(dir) + expect_null(find_skill("nonexistent")) +}) + +test_that("find_skill() finds a valid skill", { + dir <- withr::local_tempdir() + create_temp_skill(name = "found-skill", dir = dir) + local_skill_dirs(dir) + + result <- find_skill("found-skill") + expect_type(result, "list") + expect_true(file.exists(result$path)) +}) + +# extract_skill_metadata --------------------------------------------------- + +test_that("extract_skill_metadata() returns parsed frontmatter", { + skill_dir <- create_temp_skill( + extra_frontmatter = "license: MIT\n" + ) + metadata <- extract_skill_metadata(file.path(skill_dir, "SKILL.md")) + expect_equal(metadata$name, "test-skill") + expect_equal(metadata$license, "MIT") +}) + +test_that("extract_skill_metadata() returns empty list for bad files", { + tmp <- withr::local_tempfile(lines = "No frontmatter here", fileext = ".md") + metadata <- extract_skill_metadata(tmp) + expect_equal(metadata, list()) +}) + +# Resources ---------------------------------------------------------------- + +test_that("list_skill_resources() finds files recursively", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "test-skill") + dir.create(file.path(skill_dir, "scripts"), recursive = TRUE) + dir.create(file.path(skill_dir, "assets", "templates"), recursive = TRUE) + writeLines("print('hi')", file.path(skill_dir, "scripts", "run.py")) + writeLines("template", file.path(skill_dir, "assets", "templates", "base.html")) + + resources <- list_skill_resources(skill_dir) + expect_equal(resources$scripts, "run.py") + expect_true("templates/base.html" %in% resources$assets) +}) + +test_that("format_resources_listing() returns empty string for no resources", { + resources <- list(scripts = character(0), references = character(0), assets = character(0)) + expect_equal(format_resources_listing(resources, "/tmp"), "") +}) + +# Fetch Skill Tool --------------------------------------------------------- + +test_that("btw_tool_fetch_skill_impl() returns content and resources", { + dir <- withr::local_tempdir() + skill_dir <- create_temp_skill(name = "fetch-test", dir = dir) + dir.create(file.path(skill_dir, "references")) + writeLines("Reference doc.", file.path(skill_dir, "references", "guide.md")) + + local_skill_dirs(dir) + + result <- btw_tool_fetch_skill_impl("fetch-test") + expect_s3_class(result, "ellmer::ContentToolResult") + expect_match(result@value, "Test Skill") + expect_match(result@value, "References:") + expect_equal(result@extra$data$name, "fetch-test") + expect_equal(result@extra$data$resources$references, "guide.md") +}) + +test_that("btw_tool_fetch_skill_impl() errors for missing skill", { + dir <- withr::local_tempdir() + local_skill_dirs(dir) + expect_error(btw_tool_fetch_skill_impl("nonexistent"), "not found") +}) + +# System Prompt ------------------------------------------------------------ + +test_that("btw_skills_system_prompt() returns empty for no skills", { + dir <- withr::local_tempdir() + local_skill_dirs(dir) + expect_equal(btw_skills_system_prompt(), "") +}) + +test_that("btw_skills_system_prompt() includes skill metadata", { + dir <- withr::local_tempdir() + create_temp_skill( + name = "prompt-test", + description = "A skill for testing prompts.", + dir = dir, + extra_frontmatter = "compatibility: Needs R 4.2\n" + ) + local_skill_dirs(dir) + + prompt <- btw_skills_system_prompt() + expect_match(prompt, "prompt-test") + expect_match(prompt, "A skill for testing prompts.") + expect_match(prompt, "Needs R 4.2") +}) + +# btw_skill_create --------------------------------------------------------- + +test_that("btw_skill_create() creates valid skill directory", { + dir <- withr::local_tempdir() + path <- btw_skill_create( + name = "my-new-skill", + description = "A new skill.", + scope = dir, + resources = TRUE + ) + + expect_true(dir.exists(path)) + expect_true(file.exists(file.path(path, "SKILL.md"))) + expect_true(dir.exists(file.path(path, "scripts"))) + expect_true(dir.exists(file.path(path, "references"))) + expect_true(dir.exists(file.path(path, "assets"))) + + # Validate the created skill + result <- validate_skill(path) + expect_true(result$valid) +}) + +test_that("btw_skill_create() without resources omits directories", { + dir <- withr::local_tempdir() + path <- btw_skill_create( + name = "minimal-skill", + description = "Minimal.", + scope = dir, + resources = FALSE + ) + + expect_true(file.exists(file.path(path, "SKILL.md"))) + expect_false(dir.exists(file.path(path, "scripts"))) +}) + +test_that("btw_skill_create() errors for invalid name", { + dir <- withr::local_tempdir() + expect_error(btw_skill_create(name = "Bad-Name", scope = dir), "lowercase") + expect_error(btw_skill_create(name = "-bad", scope = dir), "lowercase|hyphen") + expect_error(btw_skill_create(name = "bad--name", scope = dir), "consecutive") +}) + +test_that("btw_skill_create() errors if skill already exists", { + dir <- withr::local_tempdir() + btw_skill_create(name = "existing", description = "First.", scope = dir) + expect_error( + btw_skill_create(name = "existing", description = "Second.", scope = dir), + "already exists" + ) +}) + +# btw_skill_validate ------------------------------------------------------- + +test_that("btw_skill_validate() reports valid skill", { + skill_dir <- create_temp_skill() + expect_message(result <- btw_skill_validate(skill_dir), "valid") + expect_true(result$valid) +}) + +test_that("btw_skill_validate() reports issues", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "bad-skill") + dir.create(skill_dir) + writeLines("---\nname: bad-skill\n---\nBody.", file.path(skill_dir, "SKILL.md")) + expect_message(result <- btw_skill_validate(skill_dir), "validation issues") + expect_false(result$valid) +}) + +test_that("btw_skill_validate() errors for nonexistent directory", { + expect_error(btw_skill_validate("/nonexistent/path"), "does not exist") +}) + +# btw_skill_install -------------------------------------------------------- + +test_that("btw_skill_install() installs from directory", { + # Create source skill + source_dir <- withr::local_tempdir() + create_temp_skill(name = "installable", dir = source_dir) + + # Install to target + target_base <- withr::local_tempdir() + target_dir <- file.path(target_base, ".btw", "skills") + + withr::local_dir(target_base) + path <- btw_skill_install( + file.path(source_dir, "installable"), + scope = "project" + ) + + expect_true(dir.exists(path)) + expect_true(file.exists(file.path(path, "SKILL.md"))) +}) + +test_that("btw_skill_install() installs from .skill file", { + # Create source skill + source_dir <- withr::local_tempdir() + create_temp_skill(name = "zipped", dir = source_dir) + + # Create .skill archive + skill_file <- file.path(withr::local_tempdir(), "zipped.skill") + zip_wd <- source_dir + withr::with_dir(zip_wd, { + utils::zip(skill_file, "zipped", flags = "-r9Xq") + }) + + # Install + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + path <- btw_skill_install(skill_file, scope = "project") + + expect_true(dir.exists(path)) + expect_true(file.exists(file.path(path, "SKILL.md"))) +}) + +test_that("btw_skill_install() refuses invalid skill", { + source_dir <- withr::local_tempdir() + bad_dir <- file.path(source_dir, "bad-skill") + dir.create(bad_dir) + writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_error(btw_skill_install(bad_dir, scope = "project"), "Cannot install invalid") +}) + +test_that("btw_skill_install() errors for nonexistent source", { + expect_error(btw_skill_install("/nonexistent/path"), "not found") +}) + +# Snapshot test for system prompt (existing) -------------------------------- + test_that("btw_skills_system_prompt() works", { skip_if_not_snapshot_env() expect_snapshot(cat(btw_skills_system_prompt())) From ec47042da35439694f569bd91adc546483a725a9 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 16 Feb 2026 12:43:02 -0500 Subject: [PATCH 07/28] feat(skills): discover project skills from .btw/, .agents/, and .claude/ Scan three project-level directories for skills: 1. .btw/skills/ (preferred) 2. .agents/skills/ 3. .claude/skills/ When installing or creating skills with scope="project": - If no project skill dirs exist, defaults to .btw/skills/ - If exactly one exists, uses it - If multiple exist and session is interactive, prompts the user - If multiple exist and non-interactive, uses first by priority order Users can always pass a custom path via the scope parameter. --- R/tool-skills.R | 58 ++++++++++++++++++++++++---- tests/testthat/test-tool_skills.R | 64 +++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 8 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 02e708fe..de484e30 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -93,16 +93,15 @@ btw_tool_fetch_skill_impl <- function(skill_name) { # Skill Discovery ---------------------------------------------------------- btw_skill_directories <- function() { - dirs <- character() - + # Package-bundled skills package_skills <- system.file("skills", package = "btw") if (nzchar(package_skills) && dir.exists(package_skills)) { dirs <- c(dirs, package_skills) } - + # User-level skills (global installation) user_skills_dir <- file.path( tools::R_user_dir("btw", "config"), "skills" @@ -111,14 +110,57 @@ btw_skill_directories <- function() { dirs <- c(dirs, user_skills_dir) } - project_skills_dir <- file.path(getwd(), ".btw", "skills") - if (dir.exists(project_skills_dir)) { - dirs <- c(dirs, project_skills_dir) + # Project-level skills from multiple conventions + for (project_subdir in project_skill_subdirs()) { + project_skills_dir <- file.path(getwd(), project_subdir) + if (dir.exists(project_skills_dir)) { + dirs <- c(dirs, project_skills_dir) + } } dirs } +project_skill_subdirs <- function() { + c( + file.path(".btw", "skills"), + file.path(".agents", "skills"), + file.path(".claude", "skills") + ) +} + +resolve_project_skill_dir <- function() { + candidates <- file.path(getwd(), project_skill_subdirs()) + existing <- candidates[dir.exists(candidates)] + + if (length(existing) == 0) { + # None exist yet, default to .btw/skills + return(candidates[[1]]) + } + + if (length(existing) == 1) { + return(existing[[1]]) + } + + # Multiple exist — if interactive, let the user choose + if (!is_interactive()) { + return(existing[[1]]) + } + + cli::cli_inform("Multiple project skill directories found:") + choice <- utils::menu( + choices = existing, + graphics = FALSE, + title = "Which directory should be used?" + ) + + if (choice == 0) { + cli::cli_abort("Aborted by user.") + } + + existing[[choice]] +} + btw_list_skills <- function() { skill_dirs <- btw_skill_directories() all_skills <- list() @@ -451,7 +493,7 @@ btw_skill_create <- function( # Resolve target directory parent_dir <- switch( scope, - project = file.path(getwd(), ".btw", "skills"), + project = resolve_project_skill_dir(), user = file.path(tools::R_user_dir("btw", "config"), "skills"), scope ) @@ -569,7 +611,7 @@ btw_skill_install <- function(source, scope = "project") { # Determine target directory target_parent <- switch( scope, - project = file.path(getwd(), ".btw", "skills"), + project = resolve_project_skill_dir(), user = file.path(tools::R_user_dir("btw", "config"), "skills"), cli::cli_abort("scope must be {.val project} or {.val user}, not {.val {scope}}.") ) diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 55db90d3..68e6d7a8 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -212,6 +212,70 @@ test_that("btw_list_skills() includes compatibility and allowed-tools", { expect_equal(skills[["fancy-skill"]]$allowed_tools, "Read Bash") }) +test_that("btw_skill_directories() discovers skills from multiple project dirs", { + project <- withr::local_tempdir() + withr::local_dir(project) + project <- getwd() # resolve symlinks (e.g. /private/var on macOS) + + # Create skills in .btw/skills and .claude/skills + btw_dir <- file.path(project, ".btw", "skills") + claude_dir <- file.path(project, ".claude", "skills") + dir.create(btw_dir, recursive = TRUE) + dir.create(claude_dir, recursive = TRUE) + + dirs <- btw_skill_directories() + expect_true(btw_dir %in% dirs) + expect_true(claude_dir %in% dirs) +}) + +test_that("btw_skill_directories() discovers .agents/skills", { + project <- withr::local_tempdir() + withr::local_dir(project) + project <- getwd() + + agents_dir <- file.path(project, ".agents", "skills") + dir.create(agents_dir, recursive = TRUE) + + dirs <- btw_skill_directories() + expect_true(agents_dir %in% dirs) +}) + +test_that("resolve_project_skill_dir() defaults to .btw/skills when none exist", { + project <- withr::local_tempdir() + withr::local_dir(project) + project <- getwd() + + result <- resolve_project_skill_dir() + expect_equal(result, file.path(project, ".btw", "skills")) +}) + +test_that("resolve_project_skill_dir() returns the one that exists", { + project <- withr::local_tempdir() + withr::local_dir(project) + project <- getwd() + + agents_dir <- file.path(project, ".agents", "skills") + dir.create(agents_dir, recursive = TRUE) + + result <- resolve_project_skill_dir() + expect_equal(result, agents_dir) +}) + +test_that("resolve_project_skill_dir() returns first existing when non-interactive", { + project <- withr::local_tempdir() + withr::local_dir(project) + project <- getwd() + + btw_dir <- file.path(project, ".btw", "skills") + agents_dir <- file.path(project, ".agents", "skills") + dir.create(btw_dir, recursive = TRUE) + dir.create(agents_dir, recursive = TRUE) + + local_mocked_bindings(is_interactive = function() FALSE) + result <- resolve_project_skill_dir() + expect_equal(result, btw_dir) +}) + test_that("find_skill() returns NULL for nonexistent skill", { dir <- withr::local_tempdir() local_skill_dirs(dir) From 8aed34b0e14936b485bc2e4f0930ed0bb81d73c9 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 16 Feb 2026 13:08:29 -0500 Subject: [PATCH 08/28] feat(skills): add to system prompt, exclude skills from MCP Add the skill's SKILL.md absolute path as a field in the system prompt XML, following the Agent Skills integration guide recommendation for filesystem-based agents. This lets agents with file access (e.g. Claude Code) read SKILL.md directly without needing the fetch_skill tool. Exclude the skills tool group from btw_mcp_server() by default via a new btw_mcp_tools() helper. The skill system prompt with metadata is injected by btw_client(), not by MCP, so the fetch_skill tool would appear without context for the model to know what skills exist. --- R/mcp.R | 16 +++++++++++++++- R/tool-skills.R | 5 +++-- tests/testthat/_snaps/tool_skills.md | 1 + tests/testthat/test-tool_skills.R | 7 ++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/R/mcp.R b/R/mcp.R index 9405e356..89279b7a 100644 --- a/R/mcp.R +++ b/R/mcp.R @@ -153,7 +153,7 @@ #' #' @name mcp #' @export -btw_mcp_server <- function(tools = btw_tools()) { +btw_mcp_server <- function(tools = btw_mcp_tools()) { # If given a path to an R script, we'll pass it on to mcp_server() is_likely_r_file <- is.character(tools) && @@ -172,3 +172,17 @@ btw_mcp_server <- function(tools = btw_tools()) { btw_mcp_session <- function() { mcptools::mcp_session() } + +btw_mcp_tools <- function() { + # Skills are excluded from MCP by default: the skill system prompt + # (with metadata) is injected by btw_client(), not by + + # MCP. Without that context the model has no way to know which skills are + # available. Filesystem-based agents (e.g. Claude Code) can read SKILL.md + # files directly via their paths in the system prompt. + all_tools <- btw_tools() + Filter( + function(tool) !identical(tool@annotations$btw_group, "skills"), + all_tools + ) +} diff --git a/R/tool-skills.R b/R/tool-skills.R index de484e30..bcbfbaa4 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -416,9 +416,10 @@ btw_skills_system_prompt <- function() { skills, function(skill) { parts <- sprintf( - "\n%s\n%s", + "\n%s\n%s\n%s", skill$name, - skill$description + skill$description, + skill$path ) if (!is.null(skill$compatibility)) { parts <- paste0(parts, sprintf("\n%s", skill$compatibility)) diff --git a/tests/testthat/_snaps/tool_skills.md b/tests/testthat/_snaps/tool_skills.md index 025902c4..3b286e72 100644 --- a/tests/testthat/_snaps/tool_skills.md +++ b/tests/testthat/_snaps/tool_skills.md @@ -22,6 +22,7 @@ skill-creator Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations. + SKILL_PATH diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 68e6d7a8..f081b6ac 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -511,7 +511,12 @@ test_that("btw_skill_install() errors for nonexistent source", { test_that("btw_skills_system_prompt() works", { skip_if_not_snapshot_env() - expect_snapshot(cat(btw_skills_system_prompt())) + expect_snapshot( + cat(btw_skills_system_prompt()), + transform = function(x) { + gsub(".*?", "SKILL_PATH", x) + } + ) }) test_that("skills prompt is included in btw_client() system prompt", { From c362a5aa714904fdfe743c23b60f28e285526ef7 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 16 Feb 2026 13:12:28 -0500 Subject: [PATCH 09/28] docs(skills): clarify script execution limitation in system prompt Update the skills system prompt to accurately describe that btw cannot directly execute skill scripts. Scripts are listed with paths but the agent should read them for reference or adapt their logic into R code for use with the R code execution tool. This is intentional: executing arbitrary bundled scripts requires a tool approval system that btw does not yet have. --- inst/prompts/skills.md | 4 ++-- tests/testthat/_snaps/tool_skills.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/prompts/skills.md b/inst/prompts/skills.md index 09b896a0..190d807a 100644 --- a/inst/prompts/skills.md +++ b/inst/prompts/skills.md @@ -6,9 +6,9 @@ You have access to specialized skills that provide detailed guidance for specifi 1. **Check available skills**: Review the `` listing below 2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description -3. **Access resources**: After fetching, use file read tools to access references or bash/code tools to run bundled scripts +3. **Access resources**: After fetching, use file read tools to access references Skills may include bundled resources: -- **Scripts**: Executable code (R, Python, bash) for automated tasks +- **Scripts**: Code bundled with the skill. Scripts are not directly executable by btw; read them for reference or adapt their logic into R code for use with the R code execution tool. - **References**: Additional documentation to consult as needed - **Assets**: Templates and files for use in outputs diff --git a/tests/testthat/_snaps/tool_skills.md b/tests/testthat/_snaps/tool_skills.md index 3b286e72..acde286c 100644 --- a/tests/testthat/_snaps/tool_skills.md +++ b/tests/testthat/_snaps/tool_skills.md @@ -11,10 +11,10 @@ 1. **Check available skills**: Review the `` listing below 2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description - 3. **Access resources**: After fetching, use file read tools to access references or bash/code tools to run bundled scripts + 3. **Access resources**: After fetching, use file read tools to access references Skills may include bundled resources: - - **Scripts**: Executable code (R, Python, bash) for automated tasks + - **Scripts**: Code bundled with the skill. Scripts are not directly executable by btw; read them for reference or adapt their logic into R code for use with the R code execution tool. - **References**: Additional documentation to consult as needed - **Assets**: Templates and files for use in outputs From 50231d6339f85ec17d0d9ada81876e61e4cd38ef Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Mon, 16 Feb 2026 13:53:33 -0500 Subject: [PATCH 10/28] feat(skills): replace btw_skill_install() with GitHub and package sources Add btw_skill_install_github() and btw_skill_install_package() as the new public API for installing skills. The original btw_skill_install() is refactored into an internal install_skill_from_dir() helper that both new functions delegate to. The .skill ZIP archive code path is removed. A shared select_skill_dir() helper extracts the duplicated skill selection logic (match by name, auto-select single, interactive menu). --- NAMESPACE | 3 +- R/tool-skills.R | 250 +++++++++++++----- _dev/agents/agent-skills/feature.md | 240 ++++++++++++++++++ man/btw_skill_create.Rd | 3 +- man/btw_skill_install.Rd | 32 --- man/btw_skill_install_github.Rd | 40 +++ man/btw_skill_install_package.Rd | 38 +++ man/btw_skill_validate.Rd | 3 +- man/btw_tool_fetch_skill.Rd | 3 +- man/mcp.Rd | 2 +- tests/testthat/test-tool_skills.R | 377 ++++++++++++++++++++++++++-- 11 files changed, 867 insertions(+), 124 deletions(-) create mode 100644 _dev/agents/agent-skills/feature.md delete mode 100644 man/btw_skill_install.Rd create mode 100644 man/btw_skill_install_github.Rd create mode 100644 man/btw_skill_install_package.Rd diff --git a/NAMESPACE b/NAMESPACE index 026a5460..9d930fce 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -27,7 +27,8 @@ export(btw_client) export(btw_mcp_server) export(btw_mcp_session) export(btw_skill_create) -export(btw_skill_install) +export(btw_skill_install_github) +export(btw_skill_install_package) export(btw_skill_validate) export(btw_task_create_btw_md) export(btw_task_create_readme) diff --git a/R/tool-skills.R b/R/tool-skills.R index bcbfbaa4..4557fef4 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -584,13 +584,65 @@ btw_skill_validate <- function(path = ".") { invisible(result) } -#' Install a skill +select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") { + if (length(skill_dirs) == 0) { + cli::cli_abort("No skills found in {source_label}.") + } + + if (!is.null(skill)) { + check_string(skill) + match_idx <- match(skill, basename(skill_dirs)) + if (is.na(match_idx)) { + available <- basename(skill_dirs) + cli::cli_abort(c( + "Skill {.val {skill}} not found in {source_label}.", + "i" = "Available skills: {.val {available}}" + )) + } + return(skill_dirs[[match_idx]]) + } + + if (length(skill_dirs) == 1) { + return(skill_dirs[[1]]) + } + + # Multiple skills, no name specified + + if (!is_interactive()) { + available <- basename(skill_dirs) + cli::cli_abort(c( + "Multiple skills found in {source_label}.", + "i" = "Available skills: {.val {available}}", + "i" = "Use the {.arg skill} argument to select one." + )) + } + + cli::cli_inform("Multiple skills found in {source_label}:") + choice <- utils::menu( + choices = basename(skill_dirs), + graphics = FALSE, + title = "Which skill would you like to install?" + ) + + if (choice == 0) { + cli::cli_abort("Aborted by user.") + } + + skill_dirs[[choice]] +} + +#' Install a skill from GitHub #' #' @description -#' Install a skill from a `.skill` file (ZIP archive) or a directory into -#' a skill location where btw can discover it. +#' Download and install a skill from a GitHub repository. The repository +#' should contain one or more skill directories, each with a `SKILL.md` file. #' -#' @param source Path to a `.skill` file or a skill directory. +#' @param repo GitHub repository in `"owner/repo"` format. +#' @param skill Optional skill name. If `NULL` and the repository contains +#' multiple skills, an interactive picker is shown (or an error in +#' non-interactive sessions). +#' @param ref Git reference (branch, tag, or SHA) to download. Defaults to +#' `"HEAD"`. #' @param scope Where to install the skill. One of: #' - `"project"` (default): Installs to `.btw/skills/` in the current #' working directory @@ -600,79 +652,157 @@ btw_skill_validate <- function(path = ".") { #' #' @family skills #' @export -btw_skill_install <- function(source, scope = "project") { - check_string(source) +btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = "project") { + check_string(repo) + if (!is.null(skill)) check_string(skill) + check_string(ref) check_string(scope) - source <- normalizePath(source, mustWork = FALSE) - if (!file.exists(source) && !dir.exists(source)) { - cli::cli_abort("Source not found: {.path {source}}") + rlang::check_installed("gh", reason = "to install skills from GitHub.") + + # Validate repo format + parts <- strsplit(repo, "/", fixed = TRUE)[[1]] + if (length(parts) != 2 || !nzchar(parts[[1]]) || !nzchar(parts[[2]])) { + cli::cli_abort( + '{.arg repo} must be in {.val owner/repo} format, not {.val {repo}}.' + ) } + owner <- parts[[1]] + repo_name <- parts[[2]] - # Determine target directory - target_parent <- switch( - scope, - project = resolve_project_skill_dir(), - user = file.path(tools::R_user_dir("btw", "config"), "skills"), - cli::cli_abort("scope must be {.val project} or {.val user}, not {.val {scope}}.") + # Download zipball + tmp_zip <- tempfile(fileext = ".zip") + on.exit(unlink(tmp_zip), add = TRUE) + + tryCatch( + gh::gh( + "/repos/{owner}/{repo}/zipball/{ref}", + owner = owner, + repo = repo_name, + ref = ref, + .destfile = tmp_zip + ), + error = function(e) { + cli::cli_abort( + "Failed to download from GitHub repository {.val {repo}}: {e$message}", + parent = e + ) + } ) - if (file.info(source)$isdir) { - # Directory source: copy it - skill_name <- basename(source) - target_dir <- file.path(target_parent, skill_name) + # Extract to temp dir + tmp_dir <- tempfile("btw_gh_skill_") + on.exit(unlink(tmp_dir, recursive = TRUE), add = TRUE) + utils::unzip(tmp_zip, exdir = tmp_dir) + + # Find all SKILL.md files + skill_files <- list.files( + tmp_dir, + pattern = "^SKILL\\.md$", + recursive = TRUE, + full.names = TRUE + ) - if (dir.exists(target_dir)) { - cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") - } + if (length(skill_files) == 0) { + cli::cli_abort("No skills found in GitHub repository {.val {repo}}.") + } - # Validate before installing - validation <- validate_skill(source) - if (!validation$valid) { - cli::cli_abort(c( - "Cannot install invalid skill:", - set_names(validation$issues, rep("!", length(validation$issues))) - )) - } + skill_dirs <- dirname(skill_files) - dir.create(target_parent, recursive = TRUE, showWarnings = FALSE) - fs::dir_copy(source, target_dir) - } else { - # File source: must be .skill (ZIP) - if (!grepl("\\.skill$", source)) { - cli::cli_abort("File source must be a {.file .skill} file (ZIP archive).") - } + selected <- select_skill_dir( + skill_dirs, + skill = skill, + source_label = paste0("GitHub repository ", repo) + ) - # Extract to temp, validate, then move to target - tmp_dir <- tempfile("btw_skill_") - on.exit(unlink(tmp_dir, recursive = TRUE), add = TRUE) - utils::unzip(source, exdir = tmp_dir) + install_skill_from_dir(selected, scope = scope) +} - # Find the skill directory inside the extracted archive - extracted_dirs <- list.dirs(tmp_dir, full.names = TRUE, recursive = FALSE) - if (length(extracted_dirs) != 1) { - cli::cli_abort("Expected exactly one directory in the .skill archive, found {length(extracted_dirs)}.") - } +#' Install a skill from an R package +#' +#' @description +#' Install a skill bundled in an R package. Packages can bundle skills in +#' their `inst/skills/` directory, where each subdirectory containing a +#' `SKILL.md` file is a skill. +#' +#' @param package Name of an installed R package that bundles skills. +#' @param skill Optional skill name. If `NULL` and the package contains +#' multiple skills, an interactive picker is shown (or an error in +#' non-interactive sessions). +#' @param scope Where to install the skill. One of: +#' - `"project"` (default): Installs to `.btw/skills/` in the current +#' working directory +#' - `"user"`: Installs to the user-level skills directory +#' +#' @return The path to the installed skill directory, invisibly. +#' +#' @family skills +#' @export +btw_skill_install_package <- function(package, skill = NULL, scope = "project") { + check_string(package) + if (!is.null(skill)) check_string(skill) + check_string(scope) - skill_name <- basename(extracted_dirs[[1]]) - target_dir <- file.path(target_parent, skill_name) + rlang::check_installed(package, reason = "to install skills from it.") - if (dir.exists(target_dir)) { - cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") - } + skills_dir <- system.file("skills", package = package) + if (!nzchar(skills_dir) || !dir.exists(skills_dir)) { + cli::cli_abort("Package {.pkg {package}} does not bundle any skills.") + } - validation <- validate_skill(extracted_dirs[[1]]) - if (!validation$valid) { - cli::cli_abort(c( - "Cannot install invalid skill from {.file {source}}:", - set_names(validation$issues, rep("!", length(validation$issues))) - )) - } + # Find subdirectories that contain SKILL.md + subdirs <- list.dirs(skills_dir, full.names = TRUE, recursive = FALSE) + skill_dirs <- subdirs[file.exists(file.path(subdirs, "SKILL.md"))] - dir.create(target_parent, recursive = TRUE, showWarnings = FALSE) - fs::dir_copy(extracted_dirs[[1]], target_dir) + if (length(skill_dirs) == 0) { + cli::cli_abort("Package {.pkg {package}} does not bundle any skills.") } + selected <- select_skill_dir( + skill_dirs, + skill = skill, + source_label = paste0("package ", package) + ) + + install_skill_from_dir(selected, scope = scope) +} + +install_skill_from_dir <- function(source_dir, scope = "project") { + check_string(source_dir) + check_string(scope) + + source_dir <- normalizePath(source_dir, mustWork = FALSE) + if (!dir.exists(source_dir)) { + cli::cli_abort("Source directory not found: {.path {source_dir}}") + } + + # Determine target directory + target_parent <- switch( + scope, + project = resolve_project_skill_dir(), + user = file.path(tools::R_user_dir("btw", "config"), "skills"), + cli::cli_abort("scope must be {.val project} or {.val user}, not {.val {scope}}.") + ) + + skill_name <- basename(source_dir) + target_dir <- file.path(target_parent, skill_name) + + if (dir.exists(target_dir)) { + cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + } + + # Validate before installing + validation <- validate_skill(source_dir) + if (!validation$valid) { + cli::cli_abort(c( + "Cannot install invalid skill:", + set_names(validation$issues, rep("!", length(validation$issues))) + )) + } + + dir.create(target_parent, recursive = TRUE, showWarnings = FALSE) + fs::dir_copy(source_dir, target_dir) + cli::cli_inform(c( "v" = "Installed skill {.val {skill_name}} to {.path {target_dir}}" )) diff --git a/_dev/agents/agent-skills/feature.md b/_dev/agents/agent-skills/feature.md new file mode 100644 index 00000000..475ee8cf --- /dev/null +++ b/_dev/agents/agent-skills/feature.md @@ -0,0 +1,240 @@ +# Agent Skills in btw — Feature Documentation + +> Current state as of 2026-02-16, branch `feat/skills`. + +## Overview + +btw implements the [Agent Skills specification](https://agentskills.io) to let +R users extend LLM capabilities with reusable, modular instructions. Skills are +directories containing a `SKILL.md` file (with YAML frontmatter + markdown +instructions) and optional resource subdirectories (`scripts/`, `references/`, +`assets/`). + +The implementation follows a **progressive disclosure** pattern: + +1. **Metadata** (~100 tokens/skill): Loaded at startup into the system prompt +2. **Instructions** (<5k tokens): Loaded when the agent activates a skill +3. **Resources** (unbounded): Loaded on-demand by the agent as needed + +## Architecture + +### Data Flow + +``` +btw_client() startup + │ + ├── btw_skills_system_prompt() + │ │ + │ ├── btw_skill_directories() → list of dirs to scan + │ ├── btw_list_skills() → metadata from all valid skills + │ │ └── validate_skill() → skip invalid, warn + │ │ └── extract_skill_metadata() → frontmatter::read_front_matter() + │ │ + │ └── generates XML for system prompt + │ + └── system prompt includes skill names, descriptions, locations + +Agent activates a skill + │ + ├── btw_tool_fetch_skill(skill_name) + │ ├── find_skill() → locate by name across all dirs + │ ├── frontmatter::read_front_matter() → body content + │ ├── list_skill_resources() → recursive file listing + │ └── returns SKILL.md body + resource paths as btw_tool_result + │ + └── Agent uses file-read tools for references, adapts scripts to R +``` + +### Discovery Locations + +Skills are discovered in this order (later entries override earlier by name): + +| Priority | Location | Purpose | +|----------|----------|---------| +| 1 | `system.file("skills", package = "btw")` | Package-bundled skills | +| 2 | `tools::R_user_dir("btw", "config")/skills` | User-level (global install) | +| 3 | `.btw/skills/` in working directory | Project-level (preferred) | +| 4 | `.agents/skills/` in working directory | Project-level (cross-tool convention) | +| 5 | `.claude/skills/` in working directory | Project-level (Claude Code convention) | + +All existing project-level directories are scanned for discovery. When +**installing or creating** skills with `scope = "project"`, if multiple +project dirs exist the user is prompted interactively; if none exist, defaults +to `.btw/skills/`. + +Controlled by: `btw_skill_directories()`, `project_skill_subdirs()`, +`resolve_project_skill_dir()` — all in `R/tool-skills.R:93-162`. + +### System Prompt Integration + +The skills section is injected into the system prompt by `btw_client()` at +`R/btw_client.R:154`. It sits between the tools prompt and the project context +prompt. The section is only included if skills exist (`nzchar(skills_prompt)`). + +The XML format follows the Agent Skills integration guide: + +```xml + + +skill-creator +Guide for creating effective skills... +/path/to/skills/skill-creator/SKILL.md + + +``` + +Optional fields `` and `` are included when +present in the skill's frontmatter. + +The explanation text before the XML comes from `inst/prompts/skills.md`. + +### MCP Exclusion + +The `btw_tool_fetch_skill` tool is excluded from `btw_mcp_server()` by default +via `btw_mcp_tools()` in `R/mcp.R:176`. Rationale: The `` +system prompt is injected by `btw_client()`, not by MCP. Without that metadata, +the model has no context about what skills exist. Filesystem-based agents (like +Claude Code) can read SKILL.md files directly via their `` paths. + +### Validation + +`validate_skill()` (`R/tool-skills.R:242-343`) implements the full spec: + +- `name`: required, max 64 chars, `^[a-z0-9][a-z0-9-]*[a-z0-9]$`, no `--`, must match directory name +- `description`: required, max 1024 chars +- `compatibility`: optional, max 500 chars +- `metadata`: optional, must be a key-value mapping +- `allowed-tools`: optional (experimental, not enforced) +- No unexpected frontmatter fields allowed + +Invalid skills are **warned about and skipped** during discovery (not errors). + +### Tool Registration + +The fetch skill tool is registered via `.btw_add_to_tools()` at +`R/tool-skills.R:65-91` with: +- Group: `"skills"` +- Icon: `quick-reference` (see `R/tools.R:223`) +- Conditional registration: only if `btw_list_skills()` returns >0 skills + +## Key Files + +| File | What it contains | +|------|-----------------| +| `R/tool-skills.R` | All skills logic: tool, discovery, validation, resources, system prompt, user-facing functions | +| `R/mcp.R:156,176` | MCP server default tools exclude skills; `btw_mcp_tools()` helper | +| `R/btw_client.R:154-163` | System prompt injection point | +| `R/tools.R:223` | Skills group icon mapping | +| `inst/prompts/skills.md` | System prompt explanation text shown to the model | +| `inst/skills/skill-creator/` | Bundled meta-skill for creating new skills | +| `inst/skills/skill-creator/SKILL.md` | Comprehensive guide (~356 lines) | +| `inst/skills/skill-creator/scripts/` | Python helpers: `init_skill.py`, `quick_validate.py`, `package_skill.py` | +| `inst/skills/skill-creator/references/` | `workflows.md`, `output-patterns.md` | +| `tests/testthat/test-tool_skills.R` | 110 tests covering all functionality | +| `tests/testthat/_snaps/tool_skills.md` | Snapshot for system prompt output | +| `man/btw_tool_fetch_skill.Rd` | Tool documentation | +| `man/btw_skill_create.Rd` | `btw_skill_create()` docs | +| `man/btw_skill_validate.Rd` | `btw_skill_validate()` docs | +| `man/btw_skill_install_github.Rd` | `btw_skill_install_github()` docs | +| `man/btw_skill_install_package.Rd` | `btw_skill_install_package()` docs | +| `NAMESPACE` | Exports: `btw_tool_fetch_skill`, `btw_skill_create`, `btw_skill_validate`, `btw_skill_install_github`, `btw_skill_install_package` | + +## Exported Functions + +### For agents (tool) + +- **`btw_tool_fetch_skill(skill_name)`** — Fetches a skill's SKILL.md body and + lists resource paths. Returns a `btw_tool_result`. + +### For users (interactive R) + +- **`btw_skill_create(name, description, scope, resources)`** — Initialize a + new skill directory with SKILL.md template. Validates name format. +- **`btw_skill_validate(path)`** — Validate a skill against the spec. Returns + `list(valid, issues)`. +- **`btw_skill_install_github(repo, skill, ref, scope)`** — Install a skill + from a GitHub repository. Downloads the repo zipball, discovers skills + (directories containing `SKILL.md`), and installs. Requires the `gh` package. +- **`btw_skill_install_package(package, skill, scope)`** — Install a skill + bundled in an R package's `inst/skills/` directory. Requires the target + package to be installed. + +## Internal Functions + +| Function | Purpose | +|----------|---------| +| `btw_skill_directories()` | Returns all directories to scan for skills | +| `project_skill_subdirs()` | Returns the three project-level subdir paths | +| `resolve_project_skill_dir()` | Picks one project dir for install/create (interactive prompt if ambiguous) | +| `install_skill_from_dir(source_dir, scope)` | Validates and copies a skill directory to the target scope | +| `select_skill_dir(skill_dirs, skill, source_label)` | Shared selection logic: match by name, auto-select single, interactive menu | +| `btw_list_skills()` | Scans all dirs, validates, returns metadata list | +| `find_skill(name)` | Finds a specific skill by name across all dirs | +| `extract_skill_metadata(path)` | Parses YAML frontmatter from a SKILL.md file | +| `validate_skill(dir)` | Full spec validation, returns `list(valid, issues)` | +| `list_skill_resources(dir)` | Lists files in scripts/, references/, assets/ | +| `list_files_in_subdir(base, sub)` | Recursive file listing helper | +| `has_skill_resources(resources)` | Checks if any resources exist | +| `format_resources_listing(resources, base)` | Formats resource paths for display | +| `btw_skills_system_prompt()` | Generates full skills section for system prompt | +| `btw_mcp_tools()` | `btw_tools()` minus the skills group | + +## Known Limitations + +### Script Execution + +Skills can bundle scripts in `scripts/`, but btw has no tool for executing +them directly. The system prompt instructs the agent to read scripts for +reference or adapt their logic into R code for `btw_tool_run_r`. This is +intentional — executing arbitrary bundled scripts requires a tool approval +system that btw does not yet have. + +### `allowed-tools` Not Enforced + +The `allowed-tools` frontmatter field is parsed and surfaced in the system +prompt, but btw does not enforce it. The spec itself marks this as experimental. + +### No `btw_skill_list()` Export + +There is no exported user-facing function to list available skills interactively. +The internal `btw_list_skills()` serves this purpose and skills are listed in +the system prompt and in error messages from `btw_tool_fetch_skill()`. + +## Dependencies + +- **`frontmatter`** (Imports): Used for YAML frontmatter parsing via + `frontmatter::read_front_matter()`. This replaced the `yaml` package which + was removed from Suggests. +- **`fs`** (Imports): Used for `fs::dir_copy()` in `install_skill_from_dir()`. +- **`gh`** (suggested at runtime): Used by `btw_skill_install_github()` to + download repository zipballs. Checked via `rlang::check_installed()`. +- **`ellmer`** (Imports): Tool registration via `ellmer::tool()` and + `ellmer::tool_annotations()`. + +## Specification Reference + +- Spec: `_dev/agents/agent-skills/specification.md` (local copy) +- Integration guide: `_dev/agents/agent-skills/spec-integrate-skills.md` (local copy) +- Canonical URL: https://agentskills.io +- Gap analysis: `_dev/agents/agent-skills/gap-analysis.md` +- Implementation plan: `_dev/agents/agent-skills/implementation-plan.md` + +## Test Patterns + +Tests use helpers defined at the top of `test-tool_skills.R`: + +- `create_temp_skill(name, description, extra_frontmatter, body, dir)` — Creates + a temp skill directory with valid SKILL.md. Uses `withr::local_tempdir()` for + automatic cleanup. +- `local_skill_dirs(dirs)` — Mocks `btw_skill_directories()` to return only the + specified directories, isolating tests from real installed skills. +- `create_github_zipball(skills)` — Builds a ZIP mimicking GitHub's zipball + format (`owner-repo-sha/skill-name/SKILL.md`). Used with a mocked `gh::gh()` + to test `btw_skill_install_github()` without network access. + +GitHub install tests mock `gh::gh()` to copy a pre-built zipball to `.destfile`. +Package install tests mock `base::system.file()` to point to a temp directory. +Both mock `rlang::check_installed()` to skip dependency checks in the test env. + +The snapshot test for `btw_skills_system_prompt()` uses a transform to scrub +machine-specific `` paths. diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd index 7b6af6af..36661330 100644 --- a/man/btw_skill_create.Rd +++ b/man/btw_skill_create.Rd @@ -36,7 +36,8 @@ directories (\verb{scripts/}, \verb{references/}, \verb{assets/}). } \seealso{ Other skills: -\code{\link{btw_skill_install}()}, +\code{\link{btw_skill_install_github}()}, +\code{\link{btw_skill_install_package}()}, \code{\link{btw_skill_validate}()}, \code{\link{btw_tool_fetch_skill}()} } diff --git a/man/btw_skill_install.Rd b/man/btw_skill_install.Rd deleted file mode 100644 index dfc6d980..00000000 --- a/man/btw_skill_install.Rd +++ /dev/null @@ -1,32 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tool-skills.R -\name{btw_skill_install} -\alias{btw_skill_install} -\title{Install a skill} -\usage{ -btw_skill_install(source, scope = "project") -} -\arguments{ -\item{source}{Path to a \code{.skill} file or a skill directory.} - -\item{scope}{Where to install the skill. One of: -\itemize{ -\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current -working directory -\item \code{"user"}: Installs to the user-level skills directory -}} -} -\value{ -The path to the installed skill directory, invisibly. -} -\description{ -Install a skill from a \code{.skill} file (ZIP archive) or a directory into -a skill location where btw can discover it. -} -\seealso{ -Other skills: -\code{\link{btw_skill_create}()}, -\code{\link{btw_skill_validate}()}, -\code{\link{btw_tool_fetch_skill}()} -} -\concept{skills} diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd new file mode 100644 index 00000000..b4b1cae3 --- /dev/null +++ b/man/btw_skill_install_github.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_skill_install_github} +\alias{btw_skill_install_github} +\title{Install a skill from GitHub} +\usage{ +btw_skill_install_github(repo, skill = NULL, ref = "HEAD", scope = "project") +} +\arguments{ +\item{repo}{GitHub repository in \code{"owner/repo"} format.} + +\item{skill}{Optional skill name. If \code{NULL} and the repository contains +multiple skills, an interactive picker is shown (or an error in +non-interactive sessions).} + +\item{ref}{Git reference (branch, tag, or SHA) to download. Defaults to +\code{"HEAD"}.} + +\item{scope}{Where to install the skill. One of: +\itemize{ +\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current +working directory +\item \code{"user"}: Installs to the user-level skills directory +}} +} +\value{ +The path to the installed skill directory, invisibly. +} +\description{ +Download and install a skill from a GitHub repository. The repository +should contain one or more skill directories, each with a \code{SKILL.md} file. +} +\seealso{ +Other skills: +\code{\link{btw_skill_create}()}, +\code{\link{btw_skill_install_package}()}, +\code{\link{btw_skill_validate}()}, +\code{\link{btw_tool_fetch_skill}()} +} +\concept{skills} diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd new file mode 100644 index 00000000..b3ec4e5c --- /dev/null +++ b/man/btw_skill_install_package.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tool-skills.R +\name{btw_skill_install_package} +\alias{btw_skill_install_package} +\title{Install a skill from an R package} +\usage{ +btw_skill_install_package(package, skill = NULL, scope = "project") +} +\arguments{ +\item{package}{Name of an installed R package that bundles skills.} + +\item{skill}{Optional skill name. If \code{NULL} and the package contains +multiple skills, an interactive picker is shown (or an error in +non-interactive sessions).} + +\item{scope}{Where to install the skill. One of: +\itemize{ +\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current +working directory +\item \code{"user"}: Installs to the user-level skills directory +}} +} +\value{ +The path to the installed skill directory, invisibly. +} +\description{ +Install a skill bundled in an R package. Packages can bundle skills in +their \verb{inst/skills/} directory, where each subdirectory containing a +\code{SKILL.md} file is a skill. +} +\seealso{ +Other skills: +\code{\link{btw_skill_create}()}, +\code{\link{btw_skill_install_github}()}, +\code{\link{btw_skill_validate}()}, +\code{\link{btw_tool_fetch_skill}()} +} +\concept{skills} diff --git a/man/btw_skill_validate.Rd b/man/btw_skill_validate.Rd index 0a34781b..6d328ca1 100644 --- a/man/btw_skill_validate.Rd +++ b/man/btw_skill_validate.Rd @@ -22,7 +22,8 @@ fields follow the specification's naming and format rules. \seealso{ Other skills: \code{\link{btw_skill_create}()}, -\code{\link{btw_skill_install}()}, +\code{\link{btw_skill_install_github}()}, +\code{\link{btw_skill_install_package}()}, \code{\link{btw_tool_fetch_skill}()} } \concept{skills} diff --git a/man/btw_tool_fetch_skill.Rd b/man/btw_tool_fetch_skill.Rd index fdf3a24a..6fd5a19e 100644 --- a/man/btw_tool_fetch_skill.Rd +++ b/man/btw_tool_fetch_skill.Rd @@ -28,7 +28,8 @@ resources (scripts, references, assets). \seealso{ Other skills: \code{\link{btw_skill_create}()}, -\code{\link{btw_skill_install}()}, +\code{\link{btw_skill_install_github}()}, +\code{\link{btw_skill_install_package}()}, \code{\link{btw_skill_validate}()} } \concept{skills} diff --git a/man/mcp.Rd b/man/mcp.Rd index a37e51f0..fc2eb915 100644 --- a/man/mcp.Rd +++ b/man/mcp.Rd @@ -6,7 +6,7 @@ \alias{btw_mcp_session} \title{Start a Model Context Protocol server with btw tools} \usage{ -btw_mcp_server(tools = btw_tools()) +btw_mcp_server(tools = btw_mcp_tools()) btw_mcp_session() } diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index f081b6ac..a6603f04 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -449,62 +449,385 @@ test_that("btw_skill_validate() errors for nonexistent directory", { expect_error(btw_skill_validate("/nonexistent/path"), "does not exist") }) -# btw_skill_install -------------------------------------------------------- +# select_skill_dir ---------------------------------------------------------- -test_that("btw_skill_install() installs from directory", { - # Create source skill +test_that("select_skill_dir() returns single dir directly", { + dir <- withr::local_tempdir() + skill_dir <- create_temp_skill(name = "only-skill", dir = dir) + result <- select_skill_dir(skill_dir) + expect_equal(result, skill_dir) +}) + +test_that("select_skill_dir() matches named skill", { + dir <- withr::local_tempdir() + skill_a <- create_temp_skill(name = "skill-a", dir = dir) + skill_b <- create_temp_skill(name = "skill-b", dir = dir) + result <- select_skill_dir(c(skill_a, skill_b), skill = "skill-b") + expect_equal(result, skill_b) +}) + +test_that("select_skill_dir() aborts when named skill not found", { + dir <- withr::local_tempdir() + skill_a <- create_temp_skill(name = "skill-a", dir = dir) + expect_error( + select_skill_dir(skill_a, skill = "nonexistent"), + "not found" + ) +}) + +test_that("select_skill_dir() aborts for multiple dirs non-interactively", { + dir <- withr::local_tempdir() + skill_a <- create_temp_skill(name = "skill-a", dir = dir) + skill_b <- create_temp_skill(name = "skill-b", dir = dir) + local_mocked_bindings(is_interactive = function() FALSE) + expect_error( + select_skill_dir(c(skill_a, skill_b)), + "Multiple skills found" + ) +}) + +test_that("select_skill_dir() uses menu for multiple dirs interactively", { + dir <- withr::local_tempdir() + skill_a <- create_temp_skill(name = "skill-a", dir = dir) + skill_b <- create_temp_skill(name = "skill-b", dir = dir) + local_mocked_bindings(is_interactive = function() TRUE) + local_mocked_bindings(menu = function(...) 2L, .package = "utils") + expect_message( + result <- select_skill_dir(c(skill_a, skill_b)), + "Multiple skills found" + ) + expect_equal(result, skill_b) +}) + +test_that("select_skill_dir() aborts when no dirs provided", { + expect_error(select_skill_dir(character()), "No skills found") +}) + +# install_skill_from_dir --------------------------------------------------- + +test_that("install_skill_from_dir() installs from directory", { source_dir <- withr::local_tempdir() create_temp_skill(name = "installable", dir = source_dir) - # Install to target target_base <- withr::local_tempdir() - target_dir <- file.path(target_base, ".btw", "skills") - withr::local_dir(target_base) - path <- btw_skill_install( - file.path(source_dir, "installable"), - scope = "project" + expect_message( + path <- install_skill_from_dir( + file.path(source_dir, "installable"), + scope = "project" + ), + "Installed skill" ) expect_true(dir.exists(path)) expect_true(file.exists(file.path(path, "SKILL.md"))) }) -test_that("btw_skill_install() installs from .skill file", { - # Create source skill +test_that("install_skill_from_dir() refuses invalid skill", { source_dir <- withr::local_tempdir() - create_temp_skill(name = "zipped", dir = source_dir) + bad_dir <- file.path(source_dir, "bad-skill") + dir.create(bad_dir) + writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) - # Create .skill archive - skill_file <- file.path(withr::local_tempdir(), "zipped.skill") - zip_wd <- source_dir - withr::with_dir(zip_wd, { - utils::zip(skill_file, "zipped", flags = "-r9Xq") + expect_error(install_skill_from_dir(bad_dir, scope = "project"), "Cannot install invalid") +}) + +test_that("install_skill_from_dir() errors for nonexistent source", { + expect_error(install_skill_from_dir("/nonexistent/path"), "not found") +}) + +# btw_skill_install_github ------------------------------------------------- + +# Helper: create a zip mimicking GitHub's zipball format +create_github_zipball <- function(skills, zip_path = NULL) { + tmp <- withr::local_tempdir(.local_envir = parent.frame()) + repo_dir <- file.path(tmp, "owner-repo-abc1234") + dir.create(repo_dir) + + for (skill in skills) { + skill_dir <- file.path(repo_dir, skill$name) + dir.create(skill_dir, recursive = TRUE) + writeLines( + paste0( + "---\nname: ", skill$name, + "\ndescription: ", skill$description %||% "A test skill.", + "\n---\n\n# ", skill$name, "\n\nInstructions.\n" + ), + file.path(skill_dir, "SKILL.md") + ) + } + + if (is.null(zip_path)) { + zip_path <- tempfile(fileext = ".zip", tmpdir = tmp) + } + withr::with_dir(tmp, { + utils::zip(zip_path, basename(repo_dir), flags = "-r9Xq") }) + zip_path +} + +test_that("btw_skill_install_github() errors for invalid repo format", { + expect_error(btw_skill_install_github("badformat"), "owner/repo") + expect_error(btw_skill_install_github("a/b/c"), "owner/repo") + expect_error(btw_skill_install_github("/repo"), "owner/repo") + expect_error(btw_skill_install_github("owner/"), "owner/repo") +}) + +test_that("btw_skill_install_github() installs single skill", { + zip_path <- create_github_zipball(list( + list(name = "gh-skill", description = "A GitHub skill.") + )) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(..., .destfile = NULL) { + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") - # Install target_base <- withr::local_tempdir() withr::local_dir(target_base) - path <- btw_skill_install(skill_file, scope = "project") + expect_message( + path <- btw_skill_install_github("owner/repo", scope = "project"), + "Installed skill" + ) expect_true(dir.exists(path)) expect_true(file.exists(file.path(path, "SKILL.md"))) + expect_equal(basename(path), "gh-skill") }) -test_that("btw_skill_install() refuses invalid skill", { - source_dir <- withr::local_tempdir() - bad_dir <- file.path(source_dir, "bad-skill") - dir.create(bad_dir) - writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) +test_that("btw_skill_install_github() selects named skill from multiple", { + zip_path <- create_github_zipball(list( + list(name = "skill-a", description = "Skill A."), + list(name = "skill-b", description = "Skill B.") + )) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(..., .destfile = NULL) { + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_message( + path <- btw_skill_install_github("owner/repo", skill = "skill-b", scope = "project"), + "Installed skill" + ) + expect_equal(basename(path), "skill-b") +}) + +test_that("btw_skill_install_github() errors when named skill not found", { + zip_path <- create_github_zipball(list( + list(name = "skill-a", description = "Skill A.") + )) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(..., .destfile = NULL) { + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_error( + btw_skill_install_github("owner/repo", skill = "nonexistent"), + "not found" + ) +}) + +test_that("btw_skill_install_github() errors when no skills in repo", { + # Create a zip with no SKILL.md + tmp <- withr::local_tempdir() + repo_dir <- file.path(tmp, "owner-repo-abc1234") + dir.create(repo_dir) + writeLines("Just a README.", file.path(repo_dir, "README.md")) + zip_path <- file.path(tmp, "empty.zip") + withr::with_dir(tmp, { + utils::zip(zip_path, basename(repo_dir), flags = "-r9Xq") + }) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(..., .destfile = NULL) { + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") target_base <- withr::local_tempdir() withr::local_dir(target_base) - expect_error(btw_skill_install(bad_dir, scope = "project"), "Cannot install invalid") + expect_error( + btw_skill_install_github("owner/repo"), + "No skills found" + ) }) -test_that("btw_skill_install() errors for nonexistent source", { - expect_error(btw_skill_install("/nonexistent/path"), "not found") +test_that("btw_skill_install_github() aborts non-interactively with multiple skills", { + zip_path <- create_github_zipball(list( + list(name = "skill-a", description = "Skill A."), + list(name = "skill-b", description = "Skill B.") + )) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(..., .destfile = NULL) { + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") + local_mocked_bindings(is_interactive = function() FALSE) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_error( + btw_skill_install_github("owner/repo"), + "Multiple skills found" + ) +}) + +test_that("btw_skill_install_github() wraps download errors", { + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + mock_gh <- function(...) { + stop("GitHub API error: Not Found") + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") + + expect_error( + btw_skill_install_github("owner/nonexistent"), + "Failed to download" + ) +}) + +# btw_skill_install_package ------------------------------------------------ + +test_that("btw_skill_install_package() installs single skill from package", { + # Create a mock package skills directory + pkg_skills <- withr::local_tempdir() + skill_dir <- file.path(pkg_skills, "pkg-skill") + dir.create(skill_dir) + writeLines( + "---\nname: pkg-skill\ndescription: A package skill.\n---\n\n# Pkg Skill\n", + file.path(skill_dir, "SKILL.md") + ) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + local_mocked_bindings( + system.file = function(..., package = NULL) pkg_skills, + .package = "base" + ) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_message( + path <- btw_skill_install_package("mypkg", scope = "project"), + "Installed skill" + ) + expect_true(dir.exists(path)) + expect_equal(basename(path), "pkg-skill") +}) + +test_that("btw_skill_install_package() selects named skill", { + pkg_skills <- withr::local_tempdir() + for (nm in c("alpha", "beta")) { + d <- file.path(pkg_skills, nm) + dir.create(d) + writeLines( + paste0("---\nname: ", nm, "\ndescription: Skill ", nm, ".\n---\n\n# ", nm, "\n"), + file.path(d, "SKILL.md") + ) + } + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + local_mocked_bindings( + system.file = function(..., package = NULL) pkg_skills, + .package = "base" + ) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_message( + path <- btw_skill_install_package("mypkg", skill = "beta", scope = "project"), + "Installed skill" + ) + expect_equal(basename(path), "beta") +}) + +test_that("btw_skill_install_package() errors when no skills dir", { + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + local_mocked_bindings( + system.file = function(..., package = NULL) "", + .package = "base" + ) + + expect_error( + btw_skill_install_package("emptypkg"), + "does not bundle any skills" + ) +}) + +test_that("btw_skill_install_package() errors when no SKILL.md in subdirs", { + pkg_skills <- withr::local_tempdir() + dir.create(file.path(pkg_skills, "not-a-skill")) + writeLines("just a file", file.path(pkg_skills, "not-a-skill", "README.md")) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + local_mocked_bindings( + system.file = function(..., package = NULL) pkg_skills, + .package = "base" + ) + + expect_error( + btw_skill_install_package("mypkg"), + "does not bundle any skills" + ) }) # Snapshot test for system prompt (existing) -------------------------------- From c7cfcdb596ffaa85d0696e7c19684c65e1612bbd Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 08:03:24 -0500 Subject: [PATCH 11/28] fix(skills): harden validation, escaping, and install ergonomics - Add validate_skill() call in find_skill() so invalid skills return NULL - Add xml_escape() helper and apply to system prompt skill metadata - Extract validate_skill_name() shared helper, used by both validate_skill() and btw_skill_create() - Flag non-character compatibility field in validate_skill() - Warn on long description (>1024 chars) in btw_skill_create() - Warn on frontmatter parse failure in extract_skill_metadata() - Add overwrite parameter to install_skill_from_dir(), btw_skill_install_github(), and btw_skill_install_package() --- R/tool-skills.R | 132 +++++++++++++++++++----------- man/btw_skill_install_github.Rd | 11 ++- man/btw_skill_install_package.Rd | 10 ++- tests/testthat/test-tool_skills.R | 106 ++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 49 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 4557fef4..155988a1 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -217,6 +217,10 @@ find_skill <- function(skill_name) { skill_dir <- file.path(dir, skill_name) skill_md_path <- file.path(skill_dir, "SKILL.md") if (dir.exists(skill_dir) && file.exists(skill_md_path)) { + validation <- validate_skill(skill_dir) + if (!validation$valid) { + return(NULL) + } return(list( path = skill_md_path, base_dir = skill_dir @@ -233,12 +237,47 @@ extract_skill_metadata <- function(skill_path) { fm <- frontmatter::read_front_matter(skill_path) fm$data %||% list() }, - error = function(e) list() + error = function(e) { + cli::cli_warn( + "Failed to parse frontmatter in {.path {skill_path}}: {e$message}" + ) + list() + } ) } # Skill Validation --------------------------------------------------------- +validate_skill_name <- function(name, dir_name = NULL) { + issues <- character() + + if (is.null(name) || !is.character(name) || !nzchar(name)) { + issues <- c(issues, "Missing or empty 'name' field in frontmatter.") + return(issues) + } + + if (nchar(name) > 64) { + issues <- c(issues, sprintf("Name is too long (%d characters, max 64).", nchar(name))) + } + if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { + issues <- c(issues, sprintf( + "Name '%s' must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen.", + name + )) + } + if (grepl("--", name)) { + issues <- c(issues, sprintf("Name '%s' must not contain consecutive hyphens.", name)) + } + if (!is.null(dir_name) && name != dir_name) { + issues <- c(issues, sprintf( + "Name '%s' in frontmatter does not match directory name '%s'.", + name, dir_name + )) + } + + issues +} + validate_skill <- function(skill_dir) { skill_dir <- normalizePath(skill_dir, mustWork = FALSE) issues <- character() @@ -286,30 +325,7 @@ validate_skill <- function(skill_dir) { } # Validate name - name <- metadata$name - dir_name <- basename(skill_dir) - if (is.null(name) || !is.character(name) || !nzchar(name)) { - issues <- c(issues, "Missing or empty 'name' field in frontmatter.") - } else { - if (nchar(name) > 64) { - issues <- c(issues, sprintf("Name is too long (%d characters, max 64).", nchar(name))) - } - if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { - issues <- c(issues, sprintf( - "Name '%s' must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen.", - name - )) - } - if (grepl("--", name)) { - issues <- c(issues, sprintf("Name '%s' must not contain consecutive hyphens.", name)) - } - if (name != dir_name) { - issues <- c(issues, sprintf( - "Name '%s' in frontmatter does not match directory name '%s'.", - name, dir_name - )) - } - } + issues <- c(issues, validate_skill_name(metadata$name, basename(skill_dir))) # Validate description description <- metadata$description @@ -323,8 +339,10 @@ validate_skill <- function(skill_dir) { } # Validate optional fields - if (!is.null(metadata$compatibility) && is.character(metadata$compatibility)) { - if (nchar(metadata$compatibility) > 500) { + if (!is.null(metadata$compatibility)) { + if (!is.character(metadata$compatibility)) { + issues <- c(issues, "The 'compatibility' field must be a character string.") + } else if (nchar(metadata$compatibility) > 500) { issues <- c(issues, sprintf( "Compatibility field is too long (%d characters, max 500).", nchar(metadata$compatibility) @@ -395,6 +413,13 @@ format_resources_listing <- function(resources, base_dir) { paste(parts, collapse = "") } +xml_escape <- function(x) { + x <- gsub("&", "&", x, fixed = TRUE) + x <- gsub("<", "<", x, fixed = TRUE) + x <- gsub(">", ">", x, fixed = TRUE) + x +} + # System Prompt ------------------------------------------------------------ btw_skills_system_prompt <- function() { @@ -417,15 +442,15 @@ btw_skills_system_prompt <- function() { function(skill) { parts <- sprintf( "\n%s\n%s\n%s", - skill$name, - skill$description, - skill$path + xml_escape(skill$name), + xml_escape(skill$description), + xml_escape(skill$path) ) if (!is.null(skill$compatibility)) { - parts <- paste0(parts, sprintf("\n%s", skill$compatibility)) + parts <- paste0(parts, sprintf("\n%s", xml_escape(skill$compatibility))) } if (!is.null(skill$allowed_tools)) { - parts <- paste0(parts, sprintf("\n%s", skill$allowed_tools)) + parts <- paste0(parts, sprintf("\n%s", xml_escape(skill$allowed_tools))) } paste0(parts, "\n") }, @@ -478,17 +503,19 @@ btw_skill_create <- function( check_string(scope) check_bool(resources) - # Validate name format - if (nchar(name) > 64) { - cli::cli_abort("Skill name must be at most 64 characters (got {nchar(name)}).") - } - if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { - cli::cli_abort( - "Skill name must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen." + if (nchar(description) > 1024) { + cli::cli_warn( + "Skill description is long ({nchar(description)} characters, recommended max 1024)." ) } - if (grepl("--", name)) { - cli::cli_abort("Skill name must not contain consecutive hyphens.") + + # Validate name format + name_issues <- validate_skill_name(name) + if (length(name_issues) > 0) { + cli::cli_abort(c( + "Invalid skill name {.val {name}}:", + set_names(name_issues, rep("!", length(name_issues))) + )) } # Resolve target directory @@ -647,16 +674,19 @@ select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") #' - `"project"` (default): Installs to `.btw/skills/` in the current #' working directory #' - `"user"`: Installs to the user-level skills directory +#' @param overwrite If `TRUE`, overwrite an existing skill with the same name. +#' Defaults to `FALSE`, which errors if the skill already exists. #' #' @return The path to the installed skill directory, invisibly. #' #' @family skills #' @export -btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = "project") { +btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = "project", overwrite = FALSE) { check_string(repo) if (!is.null(skill)) check_string(skill) check_string(ref) check_string(scope) + check_bool(overwrite) rlang::check_installed("gh", reason = "to install skills from GitHub.") @@ -715,7 +745,7 @@ btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = " source_label = paste0("GitHub repository ", repo) ) - install_skill_from_dir(selected, scope = scope) + install_skill_from_dir(selected, scope = scope, overwrite = overwrite) } #' Install a skill from an R package @@ -733,15 +763,18 @@ btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = " #' - `"project"` (default): Installs to `.btw/skills/` in the current #' working directory #' - `"user"`: Installs to the user-level skills directory +#' @param overwrite If `TRUE`, overwrite an existing skill with the same name. +#' Defaults to `FALSE`, which errors if the skill already exists. #' #' @return The path to the installed skill directory, invisibly. #' #' @family skills #' @export -btw_skill_install_package <- function(package, skill = NULL, scope = "project") { +btw_skill_install_package <- function(package, skill = NULL, scope = "project", overwrite = FALSE) { check_string(package) if (!is.null(skill)) check_string(skill) check_string(scope) + check_bool(overwrite) rlang::check_installed(package, reason = "to install skills from it.") @@ -764,12 +797,13 @@ btw_skill_install_package <- function(package, skill = NULL, scope = "project") source_label = paste0("package ", package) ) - install_skill_from_dir(selected, scope = scope) + install_skill_from_dir(selected, scope = scope, overwrite = overwrite) } -install_skill_from_dir <- function(source_dir, scope = "project") { +install_skill_from_dir <- function(source_dir, scope = "project", overwrite = FALSE) { check_string(source_dir) check_string(scope) + check_bool(overwrite) source_dir <- normalizePath(source_dir, mustWork = FALSE) if (!dir.exists(source_dir)) { @@ -788,7 +822,11 @@ install_skill_from_dir <- function(source_dir, scope = "project") { target_dir <- file.path(target_parent, skill_name) if (dir.exists(target_dir)) { - cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + if (overwrite) { + unlink(target_dir, recursive = TRUE) + } else { + cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + } } # Validate before installing diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index b4b1cae3..5a8d1056 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -4,7 +4,13 @@ \alias{btw_skill_install_github} \title{Install a skill from GitHub} \usage{ -btw_skill_install_github(repo, skill = NULL, ref = "HEAD", scope = "project") +btw_skill_install_github( + repo, + skill = NULL, + ref = "HEAD", + scope = "project", + overwrite = FALSE +) } \arguments{ \item{repo}{GitHub repository in \code{"owner/repo"} format.} @@ -22,6 +28,9 @@ non-interactive sessions).} working directory \item \code{"user"}: Installs to the user-level skills directory }} + +\item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. +Defaults to \code{FALSE}, which errors if the skill already exists.} } \value{ The path to the installed skill directory, invisibly. diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index b3ec4e5c..dbd46ded 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -4,7 +4,12 @@ \alias{btw_skill_install_package} \title{Install a skill from an R package} \usage{ -btw_skill_install_package(package, skill = NULL, scope = "project") +btw_skill_install_package( + package, + skill = NULL, + scope = "project", + overwrite = FALSE +) } \arguments{ \item{package}{Name of an installed R package that bundles skills.} @@ -19,6 +24,9 @@ non-interactive sessions).} working directory \item \code{"user"}: Installs to the user-level skills directory }} + +\item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. +Defaults to \code{FALSE}, which errors if the skill already exists.} } \value{ The path to the installed skill directory, invisibly. diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index a6603f04..5f0ba732 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -842,6 +842,112 @@ test_that("btw_skills_system_prompt() works", { ) }) +# validate_skill_name() ---------------------------------------------------- + +test_that("validate_skill() accepts single-character name", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "a") + dir.create(skill_dir) + writeLines("---\nname: a\ndescription: A minimal skill.\n---\nBody.", file.path(skill_dir, "SKILL.md")) + result <- validate_skill(skill_dir) + expect_true(result$valid) + expect_length(result$issues, 0) +}) + +test_that("validate_skill() flags non-character compatibility", { + dir <- withr::local_tempdir() + skill_dir <- file.path(dir, "test-skill") + dir.create(skill_dir) + writeLines( + "---\nname: test-skill\ndescription: A test.\ncompatibility: true\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) + + result <- validate_skill(skill_dir) + expect_false(result$valid) + expect_match(result$issues, "must be a character string", all = FALSE) +}) + +# find_skill() with invalid skill ------------------------------------------ + +test_that("find_skill() returns NULL for invalid skill on disk", { + dir <- withr::local_tempdir() + # Create a skill that exists on disk but fails validation (missing description) + bad_dir <- file.path(dir, "bad-skill") + dir.create(bad_dir) + writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) + + local_skill_dirs(dir) + expect_null(find_skill("bad-skill")) +}) + +# btw_skill_create() long description warning ------------------------------- + +test_that("btw_skill_create() warns on long description", { + dir <- withr::local_tempdir() + long_desc <- paste(rep("a", 1025), collapse = "") + expect_warning( + btw_skill_create(name = "long-desc", description = long_desc, scope = dir), + "long" + ) +}) + +# install_skill_from_dir() overwrite ---------------------------------------- + +test_that("install_skill_from_dir() overwrites with overwrite = TRUE", { + source_dir <- withr::local_tempdir() + create_temp_skill(name = "overwrite-me", description = "Version 1.", dir = source_dir) + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + # First install + expect_message( + path <- install_skill_from_dir( + file.path(source_dir, "overwrite-me"), + scope = "project" + ), + "Installed skill" + ) + + # Update source + writeLines( + "---\nname: overwrite-me\ndescription: Version 2.\n---\nUpdated.", + file.path(source_dir, "overwrite-me", "SKILL.md") + ) + + # Re-install with overwrite + expect_message( + path2 <- install_skill_from_dir( + file.path(source_dir, "overwrite-me"), + scope = "project", + overwrite = TRUE + ), + "Installed skill" + ) + + expect_equal(path, path2) + content <- readLines(file.path(path2, "SKILL.md")) + expect_true(any(grepl("Version 2", content))) +}) + +# xml_escape() in system prompt --------------------------------------------- + +test_that("btw_skills_system_prompt() escapes XML special characters", { + dir <- withr::local_tempdir() + create_temp_skill( + name = "esc-test", + description = "Uses & ampersands.", + dir = dir + ) + local_skill_dirs(dir) + + prompt <- btw_skills_system_prompt() + expect_match(prompt, "<tags>", fixed = TRUE) + expect_match(prompt, "& ampersands", fixed = TRUE) + expect_no_match(prompt, "", fixed = TRUE) +}) + test_that("skills prompt is included in btw_client() system prompt", { withr::local_envvar(list(ANTHROPIC_API_KEY = "beep")) From 0897edfefaa26bfbe448d2496e23e4208ff1ae91 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 08:49:01 -0500 Subject: [PATCH 12/28] fix(skills): harden validation, escaping, and install ergonomics - Add validate_skill() call in find_skill() to reject invalid skills - Add xml_escape() helper, apply to system prompt interpolation - Extract validate_skill_name() shared by validate_skill() and btw_skill_create() - Flag non-character compatibility field in validate_skill() - Warn on long description in btw_skill_create() - Warn on parse failure in extract_skill_metadata() - Add overwrite parameter to install functions - Add resolve_skill_scope() supporting "project", "user", custom paths, and I() escape hatch for literal directory names - Document scope resolution order and user-level path in roxygen --- R/tool-skills.R | 230 ++++++++++++++++++++-------- _dev/agents/agent-skills/feature.md | 43 ++++-- man/btw_skill_create.Rd | 11 +- man/btw_skill_install_github.Rd | 10 +- man/btw_skill_install_package.Rd | 10 +- tests/testthat/test-tool_skills.R | 46 ++++++ 6 files changed, 263 insertions(+), 87 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 155988a1..bb7f8eb1 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -22,7 +22,6 @@ NULL btw_tool_fetch_skill <- function(skill_name, `_intent`) {} btw_tool_fetch_skill_impl <- function(skill_name) { - check_string(skill_name) skill_info <- find_skill(skill_name) @@ -182,7 +181,7 @@ btw_list_skills <- function() { if (!validation$valid) { cli::cli_warn(c( "Skipping invalid skill in {.path {subdir}}.", - set_names(validation$issues, rep("!" , length(validation$issues))) + set_names(validation$issues, rep("!", length(validation$issues))) )) next } @@ -257,22 +256,35 @@ validate_skill_name <- function(name, dir_name = NULL) { } if (nchar(name) > 64) { - issues <- c(issues, sprintf("Name is too long (%d characters, max 64).", nchar(name))) + issues <- c( + issues, + sprintf("Name is too long (%d characters, max 64).", nchar(name)) + ) } if (!grepl("^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$", name)) { - issues <- c(issues, sprintf( - "Name '%s' must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen.", - name - )) + issues <- c( + issues, + sprintf( + "Name '%s' must contain only lowercase letters, numbers, and hyphens, and must not start or end with a hyphen.", + name + ) + ) } if (grepl("--", name)) { - issues <- c(issues, sprintf("Name '%s' must not contain consecutive hyphens.", name)) + issues <- c( + issues, + sprintf("Name '%s' must not contain consecutive hyphens.", name) + ) } if (!is.null(dir_name) && name != dir_name) { - issues <- c(issues, sprintf( - "Name '%s' in frontmatter does not match directory name '%s'.", - name, dir_name - )) + issues <- c( + issues, + sprintf( + "Name '%s' in frontmatter does not match directory name '%s'.", + name, + dir_name + ) + ) } issues @@ -296,7 +308,10 @@ validate_skill <- function(skill_dir) { fm$data }, error = function(e) { - issues <<- c(issues, sprintf("Failed to parse frontmatter: %s", e$message)) + issues <<- c( + issues, + sprintf("Failed to parse frontmatter: %s", e$message) + ) NULL } ) @@ -314,14 +329,24 @@ validate_skill <- function(skill_dir) { } # Check for unexpected properties - allowed_fields <- c("name", "description", "license", "compatibility", "metadata", "allowed-tools") + allowed_fields <- c( + "name", + "description", + "license", + "compatibility", + "metadata", + "allowed-tools" + ) unexpected <- setdiff(names(metadata), allowed_fields) if (length(unexpected) > 0) { - issues <- c(issues, sprintf( - "Unexpected frontmatter field(s): %s. Allowed fields: %s.", - paste(unexpected, collapse = ", "), - paste(allowed_fields, collapse = ", ") - )) + issues <- c( + issues, + sprintf( + "Unexpected frontmatter field(s): %s. Allowed fields: %s.", + paste(unexpected, collapse = ", "), + paste(allowed_fields, collapse = ", ") + ) + ) } # Validate name @@ -329,24 +354,35 @@ validate_skill <- function(skill_dir) { # Validate description description <- metadata$description - if (is.null(description) || !is.character(description) || !nzchar(description)) { + if ( + is.null(description) || !is.character(description) || !nzchar(description) + ) { issues <- c(issues, "Missing or empty 'description' field in frontmatter.") } else if (nchar(description) > 1024) { - issues <- c(issues, sprintf( - "Description is too long (%d characters, max 1024).", - nchar(description) - )) + issues <- c( + issues, + sprintf( + "Description is too long (%d characters, max 1024).", + nchar(description) + ) + ) } # Validate optional fields if (!is.null(metadata$compatibility)) { if (!is.character(metadata$compatibility)) { - issues <- c(issues, "The 'compatibility' field must be a character string.") + issues <- c( + issues, + "The 'compatibility' field must be a character string." + ) } else if (nchar(metadata$compatibility) > 500) { - issues <- c(issues, sprintf( - "Compatibility field is too long (%d characters, max 500).", - nchar(metadata$compatibility) - )) + issues <- c( + issues, + sprintf( + "Compatibility field is too long (%d characters, max 500).", + nchar(metadata$compatibility) + ) + ) } } @@ -429,7 +465,6 @@ btw_skills_system_prompt <- function() { return("") } - skills_prompt_path <- system.file("prompts", "skills.md", package = "btw") explanation <- if (file.exists(skills_prompt_path)) { paste(readLines(skills_prompt_path, warn = FALSE), collapse = "\n") @@ -447,10 +482,22 @@ btw_skills_system_prompt <- function() { xml_escape(skill$path) ) if (!is.null(skill$compatibility)) { - parts <- paste0(parts, sprintf("\n%s", xml_escape(skill$compatibility))) + parts <- paste0( + parts, + sprintf( + "\n%s", + xml_escape(skill$compatibility) + ) + ) } if (!is.null(skill$allowed_tools)) { - parts <- paste0(parts, sprintf("\n%s", xml_escape(skill$allowed_tools))) + parts <- paste0( + parts, + sprintf( + "\n%s", + xml_escape(skill$allowed_tools) + ) + ) } paste0(parts, "\n") }, @@ -465,6 +512,21 @@ btw_skills_system_prompt <- function() { ) } +# Scope Resolution --------------------------------------------------------- + +resolve_skill_scope <- function(scope, error_call = caller_env()) { + if (inherits(scope, "AsIs")) { + return(as.character(scope)) + } + + switch( + scope, + project = resolve_project_skill_dir(), + user = file.path(tools::R_user_dir("btw", "config"), "skills"), + scope + ) +} + # User-Facing Skill Management --------------------------------------------- #' Create a new skill @@ -481,10 +543,15 @@ btw_skills_system_prompt <- function() { #' @param description A description of what the skill does and when to use it. #' Maximum 1024 characters. #' @param scope Where to create the skill. One of: -#' - `"project"` (default): Creates in `.btw/skills/` in the current -#' working directory +#' - `"project"` (default): Creates in a project-level skills directory, +#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' in that order. If one already exists, it is used; otherwise +#' `.btw/skills/` is created. #' - `"user"`: Creates in the user-level skills directory -#' - A directory path: Creates the skill directory inside this path +#' (`tools::R_user_dir("btw", "config")/skills`). +#' - A directory path: Creates the skill inside this path, e.g. +#' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` +#' if you need a literal directory with those names. #' @param resources Logical. If `TRUE` (the default), creates empty #' `scripts/`, `references/`, and `assets/` subdirectories. #' @@ -519,12 +586,7 @@ btw_skill_create <- function( } # Resolve target directory - parent_dir <- switch( - scope, - project = resolve_project_skill_dir(), - user = file.path(tools::R_user_dir("btw", "config"), "skills"), - scope - ) + parent_dir <- resolve_skill_scope(scope) skill_dir <- file.path(parent_dir, name) @@ -549,11 +611,17 @@ btw_skill_create <- function( skill_md_content <- paste0( "---\n", - "name: ", name, "\n", - "description: ", description_line, "\n", + "name: ", + name, + "\n", + "description: ", + description_line, + "\n", "---\n", "\n", - "# ", skill_title, "\n", + "# ", + skill_title, + "\n", "\n", "TODO: Add skill instructions here.\n" ) @@ -611,7 +679,11 @@ btw_skill_validate <- function(path = ".") { invisible(result) } -select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") { +select_skill_dir <- function( + skill_dirs, + skill = NULL, + source_label = "source" +) { if (length(skill_dirs) == 0) { cli::cli_abort("No skills found in {source_label}.") } @@ -644,11 +716,11 @@ select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") )) } - cli::cli_inform("Multiple skills found in {source_label}:") + cli::cli_alert_info("Multiple skills found in {source_label}:") choice <- utils::menu( choices = basename(skill_dirs), graphics = FALSE, - title = "Which skill would you like to install?" + title = "\u276F Which skill would you like to install?" ) if (choice == 0) { @@ -671,9 +743,15 @@ select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") #' @param ref Git reference (branch, tag, or SHA) to download. Defaults to #' `"HEAD"`. #' @param scope Where to install the skill. One of: -#' - `"project"` (default): Installs to `.btw/skills/` in the current -#' working directory +#' - `"project"` (default): Installs to a project-level skills directory, +#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' in that order. If one already exists, it is used; otherwise +#' `.btw/skills/` is created. #' - `"user"`: Installs to the user-level skills directory +#' (`tools::R_user_dir("btw", "config")/skills`). +#' - A directory path: Installs to a custom directory, e.g. +#' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` +#' if you need a literal directory with those names. #' @param overwrite If `TRUE`, overwrite an existing skill with the same name. #' Defaults to `FALSE`, which errors if the skill already exists. #' @@ -681,9 +759,17 @@ select_skill_dir <- function(skill_dirs, skill = NULL, source_label = "source") #' #' @family skills #' @export -btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = "project", overwrite = FALSE) { +btw_skill_install_github <- function( + repo, + skill = NULL, + ref = "HEAD", + scope = "project", + overwrite = FALSE +) { check_string(repo) - if (!is.null(skill)) check_string(skill) + if (!is.null(skill)) { + check_string(skill) + } check_string(ref) check_string(scope) check_bool(overwrite) @@ -742,7 +828,7 @@ btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = " selected <- select_skill_dir( skill_dirs, skill = skill, - source_label = paste0("GitHub repository ", repo) + source_label = cli::format_inline("GitHub repository {.field {repo}}") ) install_skill_from_dir(selected, scope = scope, overwrite = overwrite) @@ -760,9 +846,15 @@ btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = " #' multiple skills, an interactive picker is shown (or an error in #' non-interactive sessions). #' @param scope Where to install the skill. One of: -#' - `"project"` (default): Installs to `.btw/skills/` in the current -#' working directory +#' - `"project"` (default): Installs to a project-level skills directory, +#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' in that order. If one already exists, it is used; otherwise +#' `.btw/skills/` is created. #' - `"user"`: Installs to the user-level skills directory +#' (`tools::R_user_dir("btw", "config")/skills`). +#' - A directory path: Installs to a custom directory, e.g. +#' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` +#' if you need a literal directory with those names. #' @param overwrite If `TRUE`, overwrite an existing skill with the same name. #' Defaults to `FALSE`, which errors if the skill already exists. #' @@ -770,9 +862,16 @@ btw_skill_install_github <- function(repo, skill = NULL, ref = "HEAD", scope = " #' #' @family skills #' @export -btw_skill_install_package <- function(package, skill = NULL, scope = "project", overwrite = FALSE) { +btw_skill_install_package <- function( + package, + skill = NULL, + scope = "project", + overwrite = FALSE +) { check_string(package) - if (!is.null(skill)) check_string(skill) + if (!is.null(skill)) { + check_string(skill) + } check_string(scope) check_bool(overwrite) @@ -794,13 +893,17 @@ btw_skill_install_package <- function(package, skill = NULL, scope = "project", selected <- select_skill_dir( skill_dirs, skill = skill, - source_label = paste0("package ", package) + source_label = cli::format_inline("package {.pkg {package}}") ) install_skill_from_dir(selected, scope = scope, overwrite = overwrite) } -install_skill_from_dir <- function(source_dir, scope = "project", overwrite = FALSE) { +install_skill_from_dir <- function( + source_dir, + scope = "project", + overwrite = FALSE +) { check_string(source_dir) check_string(scope) check_bool(overwrite) @@ -811,12 +914,7 @@ install_skill_from_dir <- function(source_dir, scope = "project", overwrite = FA } # Determine target directory - target_parent <- switch( - scope, - project = resolve_project_skill_dir(), - user = file.path(tools::R_user_dir("btw", "config"), "skills"), - cli::cli_abort("scope must be {.val project} or {.val user}, not {.val {scope}}.") - ) + target_parent <- resolve_skill_scope(scope) skill_name <- basename(source_dir) target_dir <- file.path(target_parent, skill_name) @@ -825,7 +923,9 @@ install_skill_from_dir <- function(source_dir, scope = "project", overwrite = FA if (overwrite) { unlink(target_dir, recursive = TRUE) } else { - cli::cli_abort("Skill {.val {skill_name}} already exists at {.path {target_dir}}.") + cli::cli_abort( + "Skill {.val {skill_name}} already exists at {.path {target_dir}}." + ) } } diff --git a/_dev/agents/agent-skills/feature.md b/_dev/agents/agent-skills/feature.md index 475ee8cf..4b8ae7d4 100644 --- a/_dev/agents/agent-skills/feature.md +++ b/_dev/agents/agent-skills/feature.md @@ -1,6 +1,6 @@ # Agent Skills in btw — Feature Documentation -> Current state as of 2026-02-16, branch `feat/skills`. +> Current state as of 2026-02-17, branch `feat/skills`. ## Overview @@ -38,6 +38,7 @@ Agent activates a skill │ ├── btw_tool_fetch_skill(skill_name) │ ├── find_skill() → locate by name across all dirs + │ │ └── validate_skill() → return NULL if invalid │ ├── frontmatter::read_front_matter() → body content │ ├── list_skill_resources() → recursive file listing │ └── returns SKILL.md body + resource paths as btw_tool_result @@ -84,7 +85,9 @@ The XML format follows the Agent Skills integration guide: ``` Optional fields `` and `` are included when -present in the skill's frontmatter. +present in the skill's frontmatter. All interpolated values are XML-escaped +via `xml_escape()` to prevent special characters (`&`, `<`, `>`) in skill +metadata from breaking the XML structure. The explanation text before the XML comes from `inst/prompts/skills.md`. @@ -98,16 +101,21 @@ Claude Code) can read SKILL.md files directly via their `` paths. ### Validation -`validate_skill()` (`R/tool-skills.R:242-343`) implements the full spec: +`validate_skill()` implements the full spec. Name validation rules are +extracted into `validate_skill_name()`, shared by both `validate_skill()` and +`btw_skill_create()`: - `name`: required, max 64 chars, `^[a-z0-9][a-z0-9-]*[a-z0-9]$`, no `--`, must match directory name - `description`: required, max 1024 chars -- `compatibility`: optional, max 500 chars +- `compatibility`: optional, must be character, max 500 chars - `metadata`: optional, must be a key-value mapping - `allowed-tools`: optional (experimental, not enforced) - No unexpected frontmatter fields allowed Invalid skills are **warned about and skipped** during discovery (not errors). +`find_skill()` also validates and returns `NULL` for invalid skills. +`extract_skill_metadata()` warns on parse failure (rather than silently +returning an empty list). ### Tool Registration @@ -130,7 +138,7 @@ The fetch skill tool is registered via `.btw_add_to_tools()` at | `inst/skills/skill-creator/SKILL.md` | Comprehensive guide (~356 lines) | | `inst/skills/skill-creator/scripts/` | Python helpers: `init_skill.py`, `quick_validate.py`, `package_skill.py` | | `inst/skills/skill-creator/references/` | `workflows.md`, `output-patterns.md` | -| `tests/testthat/test-tool_skills.R` | 110 tests covering all functionality | +| `tests/testthat/test-tool_skills.R` | 123 tests covering all functionality | | `tests/testthat/_snaps/tool_skills.md` | Snapshot for system prompt output | | `man/btw_tool_fetch_skill.Rd` | Tool documentation | | `man/btw_skill_create.Rd` | `btw_skill_create()` docs | @@ -149,15 +157,18 @@ The fetch skill tool is registered via `.btw_add_to_tools()` at ### For users (interactive R) - **`btw_skill_create(name, description, scope, resources)`** — Initialize a - new skill directory with SKILL.md template. Validates name format. + new skill directory with SKILL.md template. Validates name format via + `validate_skill_name()`. Warns (does not error) if description exceeds 1024 + characters. - **`btw_skill_validate(path)`** — Validate a skill against the spec. Returns `list(valid, issues)`. -- **`btw_skill_install_github(repo, skill, ref, scope)`** — Install a skill - from a GitHub repository. Downloads the repo zipball, discovers skills - (directories containing `SKILL.md`), and installs. Requires the `gh` package. -- **`btw_skill_install_package(package, skill, scope)`** — Install a skill - bundled in an R package's `inst/skills/` directory. Requires the target - package to be installed. +- **`btw_skill_install_github(repo, skill, ref, scope, overwrite)`** — Install + a skill from a GitHub repository. Downloads the repo zipball, discovers + skills (directories containing `SKILL.md`), and installs. Requires the `gh` + package. Set `overwrite = TRUE` to replace an existing skill. +- **`btw_skill_install_package(package, skill, scope, overwrite)`** — Install a + skill bundled in an R package's `inst/skills/` directory. Requires the target + package to be installed. Set `overwrite = TRUE` to replace an existing skill. ## Internal Functions @@ -166,12 +177,14 @@ The fetch skill tool is registered via `.btw_add_to_tools()` at | `btw_skill_directories()` | Returns all directories to scan for skills | | `project_skill_subdirs()` | Returns the three project-level subdir paths | | `resolve_project_skill_dir()` | Picks one project dir for install/create (interactive prompt if ambiguous) | -| `install_skill_from_dir(source_dir, scope)` | Validates and copies a skill directory to the target scope | +| `install_skill_from_dir(source_dir, scope, overwrite)` | Validates and copies a skill directory to the target scope; `overwrite = TRUE` deletes existing first | | `select_skill_dir(skill_dirs, skill, source_label)` | Shared selection logic: match by name, auto-select single, interactive menu | | `btw_list_skills()` | Scans all dirs, validates, returns metadata list | -| `find_skill(name)` | Finds a specific skill by name across all dirs | -| `extract_skill_metadata(path)` | Parses YAML frontmatter from a SKILL.md file | +| `find_skill(name)` | Finds a specific skill by name across all dirs; validates and returns `NULL` if invalid | +| `extract_skill_metadata(path)` | Parses YAML frontmatter from a SKILL.md file; warns on parse failure | | `validate_skill(dir)` | Full spec validation, returns `list(valid, issues)` | +| `validate_skill_name(name, dir_name)` | Name format validation shared by `validate_skill()` and `btw_skill_create()` | +| `xml_escape(x)` | Escapes `&`, `<`, `>` for safe XML interpolation in system prompt | | `list_skill_resources(dir)` | Lists files in scripts/, references/, assets/ | | `list_files_in_subdir(base, sub)` | Recursive file listing helper | | `has_skill_resources(resources)` | Checks if any resources exist | diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd index 36661330..7ad15452 100644 --- a/man/btw_skill_create.Rd +++ b/man/btw_skill_create.Rd @@ -16,10 +16,15 @@ Maximum 1024 characters.} \item{scope}{Where to create the skill. One of: \itemize{ -\item \code{"project"} (default): Creates in \verb{.btw/skills/} in the current -working directory +\item \code{"project"} (default): Creates in a project-level skills directory, +chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +in that order. If one already exists, it is used; otherwise +\verb{.btw/skills/} is created. \item \code{"user"}: Creates in the user-level skills directory -\item A directory path: Creates the skill directory inside this path +(\code{tools::R_user_dir("btw", "config")/skills}). +\item A directory path: Creates the skill inside this path, e.g. +\code{scope = ".openhands/skills"}. Use \code{I("project")} or \code{I("user")} +if you need a literal directory with those names. }} \item{resources}{Logical. If \code{TRUE} (the default), creates empty diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index 5a8d1056..23ece10f 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -24,9 +24,15 @@ non-interactive sessions).} \item{scope}{Where to install the skill. One of: \itemize{ -\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current -working directory +\item \code{"project"} (default): Installs to a project-level skills directory, +chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +in that order. If one already exists, it is used; otherwise +\verb{.btw/skills/} is created. \item \code{"user"}: Installs to the user-level skills directory +(\code{tools::R_user_dir("btw", "config")/skills}). +\item A directory path: Installs to a custom directory, e.g. +\code{scope = ".openhands/skills"}. Use \code{I("project")} or \code{I("user")} +if you need a literal directory with those names. }} \item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index dbd46ded..5585afa7 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -20,9 +20,15 @@ non-interactive sessions).} \item{scope}{Where to install the skill. One of: \itemize{ -\item \code{"project"} (default): Installs to \verb{.btw/skills/} in the current -working directory +\item \code{"project"} (default): Installs to a project-level skills directory, +chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +in that order. If one already exists, it is used; otherwise +\verb{.btw/skills/} is created. \item \code{"user"}: Installs to the user-level skills directory +(\code{tools::R_user_dir("btw", "config")/skills}). +\item A directory path: Installs to a custom directory, e.g. +\code{scope = ".openhands/skills"}. Use \code{I("project")} or \code{I("user")} +if you need a literal directory with those names. }} \item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 5f0ba732..6ebae85e 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -428,6 +428,18 @@ test_that("btw_skill_create() errors if skill already exists", { ) }) +test_that("btw_skill_create() treats I('project') as literal directory name", { + dir <- withr::local_tempdir() + path <- btw_skill_create( + name = "literal-test", + description = "Literal scope test.", + scope = I(file.path(dir, "project")) + ) + + expect_true(dir.exists(path)) + expect_equal(dirname(path), file.path(dir, "project")) +}) + # btw_skill_validate ------------------------------------------------------- test_that("btw_skill_validate() reports valid skill", { @@ -539,6 +551,40 @@ test_that("install_skill_from_dir() errors for nonexistent source", { expect_error(install_skill_from_dir("/nonexistent/path"), "not found") }) +test_that("install_skill_from_dir() accepts custom path as scope", { + source_dir <- withr::local_tempdir() + create_temp_skill(name = "custom-install", dir = source_dir) + + target <- withr::local_tempdir() + custom_dir <- file.path(target, ".openhands", "skills") + + expect_message( + path <- install_skill_from_dir( + file.path(source_dir, "custom-install"), + scope = custom_dir + ), + "Installed skill" + ) + expect_equal(dirname(path), custom_dir) + expect_true(file.exists(file.path(path, "SKILL.md"))) +}) + +test_that("install_skill_from_dir() treats I('project') as literal path", { + source_dir <- withr::local_tempdir() + create_temp_skill(name = "literal-test", dir = source_dir) + + target <- withr::local_tempdir() + + expect_message( + path <- install_skill_from_dir( + file.path(source_dir, "literal-test"), + scope = I(file.path(target, "project")) + ), + "Installed skill" + ) + expect_equal(dirname(path), file.path(target, "project")) +}) + # btw_skill_install_github ------------------------------------------------- # Helper: create a zip mimicking GitHub's zipball format From 39a11a6215b6bb9325de85807ae1aed704697f7f Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 09:37:33 -0500 Subject: [PATCH 13/28] tests: Refactor into helpers, use frontmatter --- R/tool-skills.R | 13 +- tests/testthat/helpers-skills.R | 35 ++++++ tests/testthat/test-tool_skills.R | 201 ++++++++++++++++++------------ 3 files changed, 164 insertions(+), 85 deletions(-) create mode 100644 tests/testthat/helpers-skills.R diff --git a/R/tool-skills.R b/R/tool-skills.R index bb7f8eb1..55a3af02 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -123,8 +123,7 @@ btw_skill_directories <- function() { project_skill_subdirs <- function() { c( file.path(".btw", "skills"), - file.path(".agents", "skills"), - file.path(".claude", "skills") + file.path(".agents", "skills") ) } @@ -146,11 +145,11 @@ resolve_project_skill_dir <- function() { return(existing[[1]]) } - cli::cli_inform("Multiple project skill directories found:") + cli::cli_alert_success("Multiple skill directories found in project:") choice <- utils::menu( choices = existing, graphics = FALSE, - title = "Which directory should be used?" + title = "\u276F Which directory should be used?" ) if (choice == 0) { @@ -544,7 +543,7 @@ resolve_skill_scope <- function(scope, error_call = caller_env()) { #' Maximum 1024 characters. #' @param scope Where to create the skill. One of: #' - `"project"` (default): Creates in a project-level skills directory, -#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' chosen from `.btw/skills/` or `.agents/skills/` #' in that order. If one already exists, it is used; otherwise #' `.btw/skills/` is created. #' - `"user"`: Creates in the user-level skills directory @@ -744,7 +743,7 @@ select_skill_dir <- function( #' `"HEAD"`. #' @param scope Where to install the skill. One of: #' - `"project"` (default): Installs to a project-level skills directory, -#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' chosen from `.btw/skills/` or `.agents/skills/` #' in that order. If one already exists, it is used; otherwise #' `.btw/skills/` is created. #' - `"user"`: Installs to the user-level skills directory @@ -847,7 +846,7 @@ btw_skill_install_github <- function( #' non-interactive sessions). #' @param scope Where to install the skill. One of: #' - `"project"` (default): Installs to a project-level skills directory, -#' chosen from `.btw/skills/`, `.agents/skills/`, or `.claude/skills/` +#' chosen from `.btw/skills/` or `.agents/skills/` #' in that order. If one already exists, it is used; otherwise #' `.btw/skills/` is created. #' - `"user"`: Installs to the user-level skills directory diff --git a/tests/testthat/helpers-skills.R b/tests/testthat/helpers-skills.R new file mode 100644 index 00000000..68d5de34 --- /dev/null +++ b/tests/testthat/helpers-skills.R @@ -0,0 +1,35 @@ +# Helper to create a temp skill directory with valid SKILL.md +create_temp_skill <- function( + name = "test-skill", + description = "A test skill for unit testing.", + extra_frontmatter = list(), + body = "\n# Test Skill\n\nInstructions here.\n", + dir = NULL +) { + if (is.null(dir)) { + dir <- withr::local_tempdir(.local_envir = parent.frame()) + } + + skill_dir <- file.path(dir, name) + dir.create(skill_dir, recursive = TRUE, showWarnings = FALSE) + + skill <- list( + data = list2( + name = name, + description = description, + !!!extra_frontmatter + ), + body = body + ) + + frontmatter::write_front_matter(skill, file.path(skill_dir, "SKILL.md")) + skill_dir +} + +# Helper to mock skill directories for discovery +local_skill_dirs <- function(dirs, .env = parent.frame()) { + local_mocked_bindings( + btw_skill_directories = function() dirs, + .env = .env + ) +} diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 6ebae85e..8a850ee7 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -1,38 +1,3 @@ -# Helper to create a temp skill directory with valid SKILL.md -create_temp_skill <- function( - name = "test-skill", - description = "A test skill for unit testing.", - extra_frontmatter = "", - body = "\n# Test Skill\n\nInstructions here.\n", - dir = NULL -) { - if (is.null(dir)) { - dir <- withr::local_tempdir(.local_envir = parent.frame()) - } - - skill_dir <- file.path(dir, name) - dir.create(skill_dir, recursive = TRUE, showWarnings = FALSE) - - frontmatter <- paste0( - "---\n", - "name: ", name, "\n", - "description: ", description, "\n", - extra_frontmatter, - "---\n" - ) - - writeLines(paste0(frontmatter, body), file.path(skill_dir, "SKILL.md")) - skill_dir -} - -# Helper to mock skill directories for discovery -local_skill_dirs <- function(dirs, .env = parent.frame()) { - local_mocked_bindings( - btw_skill_directories = function() dirs, - .env = .env - ) -} - # Validation --------------------------------------------------------------- test_that("validate_skill() passes for valid skill", { @@ -53,7 +18,10 @@ test_that("validate_skill() fails for missing name field", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "test-skill") dir.create(skill_dir) - writeLines("---\ndescription: A skill.\n---\nBody.", file.path(skill_dir, "SKILL.md")) + writeLines( + "---\ndescription: A skill.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) result <- validate_skill(skill_dir) expect_false(result$valid) expect_match(result$issues, "Missing or empty 'name'", all = FALSE) @@ -63,7 +31,10 @@ test_that("validate_skill() fails for missing description field", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "test-skill") dir.create(skill_dir) - writeLines("---\nname: test-skill\n---\nBody.", file.path(skill_dir, "SKILL.md")) + writeLines( + "---\nname: test-skill\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) result <- validate_skill(skill_dir) expect_false(result$valid) expect_match(result$issues, "Missing or empty 'description'", all = FALSE) @@ -91,7 +62,11 @@ test_that("validate_skill() fails for name starting with hyphen", { ) result <- validate_skill(skill_dir) expect_false(result$valid) - expect_match(result$issues, "must not start or end with a hyphen", all = FALSE) + expect_match( + result$issues, + "must not start or end with a hyphen", + all = FALSE + ) }) test_that("validate_skill() fails for consecutive hyphens", { @@ -141,7 +116,7 @@ test_that("validate_skill() fails for description exceeding 1024 characters", { }) test_that("validate_skill() flags unexpected frontmatter fields", { - skill_dir <- create_temp_skill(extra_frontmatter = "bogus: true\n") + skill_dir <- create_temp_skill(extra_frontmatter = list(bogus = TRUE)) result <- validate_skill(skill_dir) expect_false(result$valid) expect_match(result$issues, "Unexpected frontmatter", all = FALSE) @@ -149,7 +124,12 @@ test_that("validate_skill() flags unexpected frontmatter fields", { test_that("validate_skill() accepts optional fields", { skill_dir <- create_temp_skill( - extra_frontmatter = "license: MIT\ncompatibility: Requires git\nallowed-tools: Read Bash\nmetadata:\n author: test\n" + extra_frontmatter = list( + license = "MIT", + compatibility = "Requires git", + "allowed-tools" = "Read Bash", + metadata = list(author = "test") + ) ) result <- validate_skill(skill_dir) expect_true(result$valid) @@ -158,7 +138,7 @@ test_that("validate_skill() accepts optional fields", { test_that("validate_skill() fails for compatibility exceeding 500 chars", { long_compat <- paste(rep("a", 501), collapse = "") skill_dir <- create_temp_skill( - extra_frontmatter = paste0("compatibility: ", long_compat, "\n") + extra_frontmatter = list(compatibility = long_compat) ) result <- validate_skill(skill_dir) expect_false(result$valid) @@ -186,7 +166,10 @@ test_that("btw_list_skills() skips invalid skills with warning", { # Create an invalid skill (missing description) bad_dir <- file.path(dir, "bad-skill") dir.create(bad_dir) - writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) + frontmatter::write_front_matter( + list(data = list(name = "bad-skill"), body = "Body."), + file.path(bad_dir, "SKILL.md") + ) local_skill_dirs(dir) @@ -203,7 +186,10 @@ test_that("btw_list_skills() includes compatibility and allowed-tools", { create_temp_skill( name = "fancy-skill", dir = dir, - extra_frontmatter = "compatibility: Requires Python 3\nallowed-tools: Read Bash\n" + extra_frontmatter = list( + compatibility = "Requires Python 3", + `allowed-tools` = "Read Bash" + ) ) local_skill_dirs(dir) @@ -215,17 +201,17 @@ test_that("btw_list_skills() includes compatibility and allowed-tools", { test_that("btw_skill_directories() discovers skills from multiple project dirs", { project <- withr::local_tempdir() withr::local_dir(project) - project <- getwd() # resolve symlinks (e.g. /private/var on macOS) + project <- getwd() # resolve symlinks (e.g. /private/var on macOS) - # Create skills in .btw/skills and .claude/skills + # Create skills in .btw/skills and .agents/skills btw_dir <- file.path(project, ".btw", "skills") - claude_dir <- file.path(project, ".claude", "skills") + agents_dir <- file.path(project, ".agents", "skills") dir.create(btw_dir, recursive = TRUE) - dir.create(claude_dir, recursive = TRUE) + dir.create(agents_dir, recursive = TRUE) dirs <- btw_skill_directories() expect_true(btw_dir %in% dirs) - expect_true(claude_dir %in% dirs) + expect_true(agents_dir %in% dirs) }) test_that("btw_skill_directories() discovers .agents/skills", { @@ -296,7 +282,7 @@ test_that("find_skill() finds a valid skill", { test_that("extract_skill_metadata() returns parsed frontmatter", { skill_dir <- create_temp_skill( - extra_frontmatter = "license: MIT\n" + extra_frontmatter = list(license = "MIT") ) metadata <- extract_skill_metadata(file.path(skill_dir, "SKILL.md")) expect_equal(metadata$name, "test-skill") @@ -317,7 +303,10 @@ test_that("list_skill_resources() finds files recursively", { dir.create(file.path(skill_dir, "scripts"), recursive = TRUE) dir.create(file.path(skill_dir, "assets", "templates"), recursive = TRUE) writeLines("print('hi')", file.path(skill_dir, "scripts", "run.py")) - writeLines("template", file.path(skill_dir, "assets", "templates", "base.html")) + writeLines( + "template", + file.path(skill_dir, "assets", "templates", "base.html") + ) resources <- list_skill_resources(skill_dir) expect_equal(resources$scripts, "run.py") @@ -325,7 +314,11 @@ test_that("list_skill_resources() finds files recursively", { }) test_that("format_resources_listing() returns empty string for no resources", { - resources <- list(scripts = character(0), references = character(0), assets = character(0)) + resources <- list( + scripts = character(0), + references = character(0), + assets = character(0) + ) expect_equal(format_resources_listing(resources, "/tmp"), "") }) @@ -367,7 +360,7 @@ test_that("btw_skills_system_prompt() includes skill metadata", { name = "prompt-test", description = "A skill for testing prompts.", dir = dir, - extra_frontmatter = "compatibility: Needs R 4.2\n" + extra_frontmatter = list(compatibility = "Needs R 4.2") ) local_skill_dirs(dir) @@ -381,11 +374,14 @@ test_that("btw_skills_system_prompt() includes skill metadata", { test_that("btw_skill_create() creates valid skill directory", { dir <- withr::local_tempdir() - path <- btw_skill_create( - name = "my-new-skill", - description = "A new skill.", - scope = dir, - resources = TRUE + expect_message( + path <- btw_skill_create( + name = "my-new-skill", + description = "A new skill.", + scope = dir, + resources = TRUE + ), + "Created skill" ) expect_true(dir.exists(path)) @@ -401,11 +397,14 @@ test_that("btw_skill_create() creates valid skill directory", { test_that("btw_skill_create() without resources omits directories", { dir <- withr::local_tempdir() - path <- btw_skill_create( - name = "minimal-skill", - description = "Minimal.", - scope = dir, - resources = FALSE + expect_message( + path <- btw_skill_create( + name = "minimal-skill", + description = "Minimal.", + scope = dir, + resources = FALSE + ), + "Created skill" ) expect_true(file.exists(file.path(path, "SKILL.md"))) @@ -421,7 +420,10 @@ test_that("btw_skill_create() errors for invalid name", { test_that("btw_skill_create() errors if skill already exists", { dir <- withr::local_tempdir() - btw_skill_create(name = "existing", description = "First.", scope = dir) + expect_message( + btw_skill_create(name = "existing", description = "First.", scope = dir), + "Created skill" + ) expect_error( btw_skill_create(name = "existing", description = "Second.", scope = dir), "already exists" @@ -430,10 +432,13 @@ test_that("btw_skill_create() errors if skill already exists", { test_that("btw_skill_create() treats I('project') as literal directory name", { dir <- withr::local_tempdir() - path <- btw_skill_create( - name = "literal-test", - description = "Literal scope test.", - scope = I(file.path(dir, "project")) + expect_message( + path <- btw_skill_create( + name = "literal-test", + description = "Literal scope test.", + scope = I(file.path(dir, "project")) + ), + "Created skill" ) expect_true(dir.exists(path)) @@ -452,7 +457,10 @@ test_that("btw_skill_validate() reports issues", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "bad-skill") dir.create(skill_dir) - writeLines("---\nname: bad-skill\n---\nBody.", file.path(skill_dir, "SKILL.md")) + writeLines( + "---\nname: bad-skill\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) expect_message(result <- btw_skill_validate(skill_dir), "validation issues") expect_false(result$valid) }) @@ -544,7 +552,10 @@ test_that("install_skill_from_dir() refuses invalid skill", { target_base <- withr::local_tempdir() withr::local_dir(target_base) - expect_error(install_skill_from_dir(bad_dir, scope = "project"), "Cannot install invalid") + expect_error( + install_skill_from_dir(bad_dir, scope = "project"), + "Cannot install invalid" + ) }) test_that("install_skill_from_dir() errors for nonexistent source", { @@ -598,9 +609,13 @@ create_github_zipball <- function(skills, zip_path = NULL) { dir.create(skill_dir, recursive = TRUE) writeLines( paste0( - "---\nname: ", skill$name, - "\ndescription: ", skill$description %||% "A test skill.", - "\n---\n\n# ", skill$name, "\n\nInstructions.\n" + "---\nname: ", + skill$name, + "\ndescription: ", + skill$description %||% "A test skill.", + "\n---\n\n# ", + skill$name, + "\n\nInstructions.\n" ), file.path(skill_dir, "SKILL.md") ) @@ -671,7 +686,11 @@ test_that("btw_skill_install_github() selects named skill from multiple", { withr::local_dir(target_base) expect_message( - path <- btw_skill_install_github("owner/repo", skill = "skill-b", scope = "project"), + path <- btw_skill_install_github( + "owner/repo", + skill = "skill-b", + scope = "project" + ), "Installed skill" ) expect_equal(basename(path), "skill-b") @@ -816,7 +835,15 @@ test_that("btw_skill_install_package() selects named skill", { d <- file.path(pkg_skills, nm) dir.create(d) writeLines( - paste0("---\nname: ", nm, "\ndescription: Skill ", nm, ".\n---\n\n# ", nm, "\n"), + paste0( + "---\nname: ", + nm, + "\ndescription: Skill ", + nm, + ".\n---\n\n# ", + nm, + "\n" + ), file.path(d, "SKILL.md") ) } @@ -834,7 +861,11 @@ test_that("btw_skill_install_package() selects named skill", { withr::local_dir(target_base) expect_message( - path <- btw_skill_install_package("mypkg", skill = "beta", scope = "project"), + path <- btw_skill_install_package( + "mypkg", + skill = "beta", + scope = "project" + ), "Installed skill" ) expect_equal(basename(path), "beta") @@ -894,7 +925,10 @@ test_that("validate_skill() accepts single-character name", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "a") dir.create(skill_dir) - writeLines("---\nname: a\ndescription: A minimal skill.\n---\nBody.", file.path(skill_dir, "SKILL.md")) + writeLines( + "---\nname: a\ndescription: A minimal skill.\n---\nBody.", + file.path(skill_dir, "SKILL.md") + ) result <- validate_skill(skill_dir) expect_true(result$valid) expect_length(result$issues, 0) @@ -933,7 +967,14 @@ test_that("btw_skill_create() warns on long description", { dir <- withr::local_tempdir() long_desc <- paste(rep("a", 1025), collapse = "") expect_warning( - btw_skill_create(name = "long-desc", description = long_desc, scope = dir), + expect_message( + btw_skill_create( + name = "long-desc", + description = long_desc, + scope = dir + ), + "Created skill" + ), "long" ) }) @@ -942,7 +983,11 @@ test_that("btw_skill_create() warns on long description", { test_that("install_skill_from_dir() overwrites with overwrite = TRUE", { source_dir <- withr::local_tempdir() - create_temp_skill(name = "overwrite-me", description = "Version 1.", dir = source_dir) + create_temp_skill( + name = "overwrite-me", + description = "Version 1.", + dir = source_dir + ) target_base <- withr::local_tempdir() withr::local_dir(target_base) From 12ce4a54a1a5b2783ed8b8a5010193c61142fc30 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 10:15:59 -0500 Subject: [PATCH 14/28] feat: skill icon --- R/btw_client_app.R | 1 + R/tools.R | 2 +- inst/icons/category.svg | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 inst/icons/category.svg diff --git a/R/btw_client_app.R b/R/btw_client_app.R index 426e9f7a..c093103a 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -737,6 +737,7 @@ app_tool_group_choice_input <- function( "pkg" = shiny::span(label_icon, "Package Tools"), "run" = shiny::span(label_icon, "Run Code"), "sessioninfo" = shiny::span(label_icon, "Session Info"), + "skills" = shiny::span(label_icon, "Skills"), "web" = shiny::span(label_icon, "Web Tools"), "other" = shiny::span(label_icon, "Other Tools"), to_title_case(group) diff --git a/R/tools.R b/R/tools.R index 584e8076..1e4ec4b6 100644 --- a/R/tools.R +++ b/R/tools.R @@ -220,7 +220,7 @@ tool_group_icon <- function(group, default = NULL) { "ide" = tool_icon("code-blocks"), "pkg" = tool_icon("package"), "sessioninfo" = tool_icon("screen-search-desktop"), - "skills" = tool_icon("quick-reference"), + "skills" = tool_icon("category"), "web" = tool_icon("globe-book"), if (!is.null(default)) tool_icon(default) ) diff --git a/inst/icons/category.svg b/inst/icons/category.svg new file mode 100644 index 00000000..44038ac6 --- /dev/null +++ b/inst/icons/category.svg @@ -0,0 +1 @@ + From 0f0605ec5d15566d39502379419bf0428d05f922 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 12:29:57 -0500 Subject: [PATCH 15/28] remove feature tracking doc from repo --- _dev/agents/agent-skills/feature.md | 253 ---------------------------- 1 file changed, 253 deletions(-) delete mode 100644 _dev/agents/agent-skills/feature.md diff --git a/_dev/agents/agent-skills/feature.md b/_dev/agents/agent-skills/feature.md deleted file mode 100644 index 4b8ae7d4..00000000 --- a/_dev/agents/agent-skills/feature.md +++ /dev/null @@ -1,253 +0,0 @@ -# Agent Skills in btw — Feature Documentation - -> Current state as of 2026-02-17, branch `feat/skills`. - -## Overview - -btw implements the [Agent Skills specification](https://agentskills.io) to let -R users extend LLM capabilities with reusable, modular instructions. Skills are -directories containing a `SKILL.md` file (with YAML frontmatter + markdown -instructions) and optional resource subdirectories (`scripts/`, `references/`, -`assets/`). - -The implementation follows a **progressive disclosure** pattern: - -1. **Metadata** (~100 tokens/skill): Loaded at startup into the system prompt -2. **Instructions** (<5k tokens): Loaded when the agent activates a skill -3. **Resources** (unbounded): Loaded on-demand by the agent as needed - -## Architecture - -### Data Flow - -``` -btw_client() startup - │ - ├── btw_skills_system_prompt() - │ │ - │ ├── btw_skill_directories() → list of dirs to scan - │ ├── btw_list_skills() → metadata from all valid skills - │ │ └── validate_skill() → skip invalid, warn - │ │ └── extract_skill_metadata() → frontmatter::read_front_matter() - │ │ - │ └── generates XML for system prompt - │ - └── system prompt includes skill names, descriptions, locations - -Agent activates a skill - │ - ├── btw_tool_fetch_skill(skill_name) - │ ├── find_skill() → locate by name across all dirs - │ │ └── validate_skill() → return NULL if invalid - │ ├── frontmatter::read_front_matter() → body content - │ ├── list_skill_resources() → recursive file listing - │ └── returns SKILL.md body + resource paths as btw_tool_result - │ - └── Agent uses file-read tools for references, adapts scripts to R -``` - -### Discovery Locations - -Skills are discovered in this order (later entries override earlier by name): - -| Priority | Location | Purpose | -|----------|----------|---------| -| 1 | `system.file("skills", package = "btw")` | Package-bundled skills | -| 2 | `tools::R_user_dir("btw", "config")/skills` | User-level (global install) | -| 3 | `.btw/skills/` in working directory | Project-level (preferred) | -| 4 | `.agents/skills/` in working directory | Project-level (cross-tool convention) | -| 5 | `.claude/skills/` in working directory | Project-level (Claude Code convention) | - -All existing project-level directories are scanned for discovery. When -**installing or creating** skills with `scope = "project"`, if multiple -project dirs exist the user is prompted interactively; if none exist, defaults -to `.btw/skills/`. - -Controlled by: `btw_skill_directories()`, `project_skill_subdirs()`, -`resolve_project_skill_dir()` — all in `R/tool-skills.R:93-162`. - -### System Prompt Integration - -The skills section is injected into the system prompt by `btw_client()` at -`R/btw_client.R:154`. It sits between the tools prompt and the project context -prompt. The section is only included if skills exist (`nzchar(skills_prompt)`). - -The XML format follows the Agent Skills integration guide: - -```xml - - -skill-creator -Guide for creating effective skills... -/path/to/skills/skill-creator/SKILL.md - - -``` - -Optional fields `` and `` are included when -present in the skill's frontmatter. All interpolated values are XML-escaped -via `xml_escape()` to prevent special characters (`&`, `<`, `>`) in skill -metadata from breaking the XML structure. - -The explanation text before the XML comes from `inst/prompts/skills.md`. - -### MCP Exclusion - -The `btw_tool_fetch_skill` tool is excluded from `btw_mcp_server()` by default -via `btw_mcp_tools()` in `R/mcp.R:176`. Rationale: The `` -system prompt is injected by `btw_client()`, not by MCP. Without that metadata, -the model has no context about what skills exist. Filesystem-based agents (like -Claude Code) can read SKILL.md files directly via their `` paths. - -### Validation - -`validate_skill()` implements the full spec. Name validation rules are -extracted into `validate_skill_name()`, shared by both `validate_skill()` and -`btw_skill_create()`: - -- `name`: required, max 64 chars, `^[a-z0-9][a-z0-9-]*[a-z0-9]$`, no `--`, must match directory name -- `description`: required, max 1024 chars -- `compatibility`: optional, must be character, max 500 chars -- `metadata`: optional, must be a key-value mapping -- `allowed-tools`: optional (experimental, not enforced) -- No unexpected frontmatter fields allowed - -Invalid skills are **warned about and skipped** during discovery (not errors). -`find_skill()` also validates and returns `NULL` for invalid skills. -`extract_skill_metadata()` warns on parse failure (rather than silently -returning an empty list). - -### Tool Registration - -The fetch skill tool is registered via `.btw_add_to_tools()` at -`R/tool-skills.R:65-91` with: -- Group: `"skills"` -- Icon: `quick-reference` (see `R/tools.R:223`) -- Conditional registration: only if `btw_list_skills()` returns >0 skills - -## Key Files - -| File | What it contains | -|------|-----------------| -| `R/tool-skills.R` | All skills logic: tool, discovery, validation, resources, system prompt, user-facing functions | -| `R/mcp.R:156,176` | MCP server default tools exclude skills; `btw_mcp_tools()` helper | -| `R/btw_client.R:154-163` | System prompt injection point | -| `R/tools.R:223` | Skills group icon mapping | -| `inst/prompts/skills.md` | System prompt explanation text shown to the model | -| `inst/skills/skill-creator/` | Bundled meta-skill for creating new skills | -| `inst/skills/skill-creator/SKILL.md` | Comprehensive guide (~356 lines) | -| `inst/skills/skill-creator/scripts/` | Python helpers: `init_skill.py`, `quick_validate.py`, `package_skill.py` | -| `inst/skills/skill-creator/references/` | `workflows.md`, `output-patterns.md` | -| `tests/testthat/test-tool_skills.R` | 123 tests covering all functionality | -| `tests/testthat/_snaps/tool_skills.md` | Snapshot for system prompt output | -| `man/btw_tool_fetch_skill.Rd` | Tool documentation | -| `man/btw_skill_create.Rd` | `btw_skill_create()` docs | -| `man/btw_skill_validate.Rd` | `btw_skill_validate()` docs | -| `man/btw_skill_install_github.Rd` | `btw_skill_install_github()` docs | -| `man/btw_skill_install_package.Rd` | `btw_skill_install_package()` docs | -| `NAMESPACE` | Exports: `btw_tool_fetch_skill`, `btw_skill_create`, `btw_skill_validate`, `btw_skill_install_github`, `btw_skill_install_package` | - -## Exported Functions - -### For agents (tool) - -- **`btw_tool_fetch_skill(skill_name)`** — Fetches a skill's SKILL.md body and - lists resource paths. Returns a `btw_tool_result`. - -### For users (interactive R) - -- **`btw_skill_create(name, description, scope, resources)`** — Initialize a - new skill directory with SKILL.md template. Validates name format via - `validate_skill_name()`. Warns (does not error) if description exceeds 1024 - characters. -- **`btw_skill_validate(path)`** — Validate a skill against the spec. Returns - `list(valid, issues)`. -- **`btw_skill_install_github(repo, skill, ref, scope, overwrite)`** — Install - a skill from a GitHub repository. Downloads the repo zipball, discovers - skills (directories containing `SKILL.md`), and installs. Requires the `gh` - package. Set `overwrite = TRUE` to replace an existing skill. -- **`btw_skill_install_package(package, skill, scope, overwrite)`** — Install a - skill bundled in an R package's `inst/skills/` directory. Requires the target - package to be installed. Set `overwrite = TRUE` to replace an existing skill. - -## Internal Functions - -| Function | Purpose | -|----------|---------| -| `btw_skill_directories()` | Returns all directories to scan for skills | -| `project_skill_subdirs()` | Returns the three project-level subdir paths | -| `resolve_project_skill_dir()` | Picks one project dir for install/create (interactive prompt if ambiguous) | -| `install_skill_from_dir(source_dir, scope, overwrite)` | Validates and copies a skill directory to the target scope; `overwrite = TRUE` deletes existing first | -| `select_skill_dir(skill_dirs, skill, source_label)` | Shared selection logic: match by name, auto-select single, interactive menu | -| `btw_list_skills()` | Scans all dirs, validates, returns metadata list | -| `find_skill(name)` | Finds a specific skill by name across all dirs; validates and returns `NULL` if invalid | -| `extract_skill_metadata(path)` | Parses YAML frontmatter from a SKILL.md file; warns on parse failure | -| `validate_skill(dir)` | Full spec validation, returns `list(valid, issues)` | -| `validate_skill_name(name, dir_name)` | Name format validation shared by `validate_skill()` and `btw_skill_create()` | -| `xml_escape(x)` | Escapes `&`, `<`, `>` for safe XML interpolation in system prompt | -| `list_skill_resources(dir)` | Lists files in scripts/, references/, assets/ | -| `list_files_in_subdir(base, sub)` | Recursive file listing helper | -| `has_skill_resources(resources)` | Checks if any resources exist | -| `format_resources_listing(resources, base)` | Formats resource paths for display | -| `btw_skills_system_prompt()` | Generates full skills section for system prompt | -| `btw_mcp_tools()` | `btw_tools()` minus the skills group | - -## Known Limitations - -### Script Execution - -Skills can bundle scripts in `scripts/`, but btw has no tool for executing -them directly. The system prompt instructs the agent to read scripts for -reference or adapt their logic into R code for `btw_tool_run_r`. This is -intentional — executing arbitrary bundled scripts requires a tool approval -system that btw does not yet have. - -### `allowed-tools` Not Enforced - -The `allowed-tools` frontmatter field is parsed and surfaced in the system -prompt, but btw does not enforce it. The spec itself marks this as experimental. - -### No `btw_skill_list()` Export - -There is no exported user-facing function to list available skills interactively. -The internal `btw_list_skills()` serves this purpose and skills are listed in -the system prompt and in error messages from `btw_tool_fetch_skill()`. - -## Dependencies - -- **`frontmatter`** (Imports): Used for YAML frontmatter parsing via - `frontmatter::read_front_matter()`. This replaced the `yaml` package which - was removed from Suggests. -- **`fs`** (Imports): Used for `fs::dir_copy()` in `install_skill_from_dir()`. -- **`gh`** (suggested at runtime): Used by `btw_skill_install_github()` to - download repository zipballs. Checked via `rlang::check_installed()`. -- **`ellmer`** (Imports): Tool registration via `ellmer::tool()` and - `ellmer::tool_annotations()`. - -## Specification Reference - -- Spec: `_dev/agents/agent-skills/specification.md` (local copy) -- Integration guide: `_dev/agents/agent-skills/spec-integrate-skills.md` (local copy) -- Canonical URL: https://agentskills.io -- Gap analysis: `_dev/agents/agent-skills/gap-analysis.md` -- Implementation plan: `_dev/agents/agent-skills/implementation-plan.md` - -## Test Patterns - -Tests use helpers defined at the top of `test-tool_skills.R`: - -- `create_temp_skill(name, description, extra_frontmatter, body, dir)` — Creates - a temp skill directory with valid SKILL.md. Uses `withr::local_tempdir()` for - automatic cleanup. -- `local_skill_dirs(dirs)` — Mocks `btw_skill_directories()` to return only the - specified directories, isolating tests from real installed skills. -- `create_github_zipball(skills)` — Builds a ZIP mimicking GitHub's zipball - format (`owner-repo-sha/skill-name/SKILL.md`). Used with a mocked `gh::gh()` - to test `btw_skill_install_github()` without network access. - -GitHub install tests mock `gh::gh()` to copy a pre-built zipball to `.destfile`. -Package install tests mock `base::system.file()` to point to a temp directory. -Both mock `rlang::check_installed()` to skip dependency checks in the test env. - -The snapshot test for `btw_skills_system_prompt()` uses a transform to scrub -machine-specific `` paths. From 5651faa3e02a33aa6bfa40417d9f98a547183c31 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 12:30:17 -0500 Subject: [PATCH 16/28] feat(skills): parse `owner/repo@ref` in btw_skill_install_github() Remove the separate `ref` argument and instead parse the Git reference inline from the `repo` string, following the convention used by pak::pak() and remotes::install_github(). Extract parse_github_repo() into R/utils.R for reuse, with targeted tests in test-utils.R. --- R/tool-skills.R | 32 +++++++------------ R/utils.R | 29 +++++++++++++++++ man/btw_skill_create.Rd | 2 +- man/btw_skill_install_github.Rd | 11 +++---- man/btw_skill_install_package.Rd | 2 +- tests/testthat/test-tool_skills.R | 30 +++++++++++++++++ tests/testthat/test-utils.R | 53 +++++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 28 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 55a3af02..a2cad28b 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -735,12 +735,13 @@ select_skill_dir <- function( #' Download and install a skill from a GitHub repository. The repository #' should contain one or more skill directories, each with a `SKILL.md` file. #' -#' @param repo GitHub repository in `"owner/repo"` format. +#' @param repo GitHub repository in `"owner/repo"` format. Optionally include +#' a Git reference (branch, tag, or SHA) as `"owner/repo@ref"`, following the +#' convention used by [pak::pak()] and [remotes::install_github()]. Defaults to +#' `"HEAD"` when no ref is specified. #' @param skill Optional skill name. If `NULL` and the repository contains #' multiple skills, an interactive picker is shown (or an error in #' non-interactive sessions). -#' @param ref Git reference (branch, tag, or SHA) to download. Defaults to -#' `"HEAD"`. #' @param scope Where to install the skill. One of: #' - `"project"` (default): Installs to a project-level skills directory, #' chosen from `.btw/skills/` or `.agents/skills/` @@ -761,7 +762,6 @@ select_skill_dir <- function( btw_skill_install_github <- function( repo, skill = NULL, - ref = "HEAD", scope = "project", overwrite = FALSE ) { @@ -769,21 +769,13 @@ btw_skill_install_github <- function( if (!is.null(skill)) { check_string(skill) } - check_string(ref) check_string(scope) check_bool(overwrite) rlang::check_installed("gh", reason = "to install skills from GitHub.") - # Validate repo format - parts <- strsplit(repo, "/", fixed = TRUE)[[1]] - if (length(parts) != 2 || !nzchar(parts[[1]]) || !nzchar(parts[[2]])) { - cli::cli_abort( - '{.arg repo} must be in {.val owner/repo} format, not {.val {repo}}.' - ) - } - owner <- parts[[1]] - repo_name <- parts[[2]] + repo_og <- repo + repo <- parse_github_repo(repo) # Download zipball tmp_zip <- tempfile(fileext = ".zip") @@ -792,14 +784,14 @@ btw_skill_install_github <- function( tryCatch( gh::gh( "/repos/{owner}/{repo}/zipball/{ref}", - owner = owner, - repo = repo_name, - ref = ref, + owner = repo$owner, + repo = repo$repo, + ref = repo$ref, .destfile = tmp_zip ), error = function(e) { cli::cli_abort( - "Failed to download from GitHub repository {.val {repo}}: {e$message}", + "Failed to download from GitHub repository {.val {repo_og}}: {e$message}", parent = e ) } @@ -819,7 +811,7 @@ btw_skill_install_github <- function( ) if (length(skill_files) == 0) { - cli::cli_abort("No skills found in GitHub repository {.val {repo}}.") + cli::cli_abort("No skills found in GitHub repository {.val {repo_og}}.") } skill_dirs <- dirname(skill_files) @@ -827,7 +819,7 @@ btw_skill_install_github <- function( selected <- select_skill_dir( skill_dirs, skill = skill, - source_label = cli::format_inline("GitHub repository {.field {repo}}") + source_label = cli::format_inline("GitHub repository {.field {repo_og}}") ) install_skill_from_dir(selected, scope = scope, overwrite = overwrite) diff --git a/R/utils.R b/R/utils.R index ed09afb5..a2299571 100644 --- a/R/utils.R +++ b/R/utils.R @@ -320,3 +320,32 @@ strip_ansi <- function(text) { to_title_case <- function(x) { paste0(toupper(substring(x, 1, 1)), substring(x, 2)) } + +# GitHub repo parsing -------------------------------------------------------- + +parse_github_repo <- function(repo, error_call = caller_env()) { + check_string(repo, call = error_call) + + ref <- "HEAD" + if (grepl("@", repo, fixed = TRUE)) { + at_pos <- regexpr("@", repo, fixed = TRUE) + ref <- substring(repo, at_pos + 1L) + repo <- substring(repo, 1L, at_pos - 1L) + if (!nzchar(ref)) { + cli::cli_abort( + '{.arg repo} has an empty ref after {.val @}. Use {.val owner/repo@ref} format.', + call = error_call + ) + } + } + + parts <- strsplit(repo, "/", fixed = TRUE)[[1]] + if (length(parts) != 2 || !nzchar(parts[[1]]) || !nzchar(parts[[2]])) { + cli::cli_abort( + '{.arg repo} must be in {.val owner/repo} or {.val owner/repo@ref} format, not {.val {repo}}.', + call = error_call + ) + } + + list(owner = parts[[1]], repo = parts[[2]], ref = ref) +} diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd index 7ad15452..3a8c3cb2 100644 --- a/man/btw_skill_create.Rd +++ b/man/btw_skill_create.Rd @@ -17,7 +17,7 @@ Maximum 1024 characters.} \item{scope}{Where to create the skill. One of: \itemize{ \item \code{"project"} (default): Creates in a project-level skills directory, -chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +chosen from \verb{.btw/skills/} or \verb{.agents/skills/} in that order. If one already exists, it is used; otherwise \verb{.btw/skills/} is created. \item \code{"user"}: Creates in the user-level skills directory diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index 23ece10f..b9295345 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -7,25 +7,24 @@ btw_skill_install_github( repo, skill = NULL, - ref = "HEAD", scope = "project", overwrite = FALSE ) } \arguments{ -\item{repo}{GitHub repository in \code{"owner/repo"} format.} +\item{repo}{GitHub repository in \code{"owner/repo"} format. Optionally include +a Git reference (branch, tag, or SHA) as \code{"owner/repo@ref"}, following the +convention used by \code{\link[pak:pak]{pak::pak()}} and \code{\link[remotes:install_github]{remotes::install_github()}}. Defaults to +\code{"HEAD"} when no ref is specified.} \item{skill}{Optional skill name. If \code{NULL} and the repository contains multiple skills, an interactive picker is shown (or an error in non-interactive sessions).} -\item{ref}{Git reference (branch, tag, or SHA) to download. Defaults to -\code{"HEAD"}.} - \item{scope}{Where to install the skill. One of: \itemize{ \item \code{"project"} (default): Installs to a project-level skills directory, -chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +chosen from \verb{.btw/skills/} or \verb{.agents/skills/} in that order. If one already exists, it is used; otherwise \verb{.btw/skills/} is created. \item \code{"user"}: Installs to the user-level skills directory diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index 5585afa7..2903f13d 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -21,7 +21,7 @@ non-interactive sessions).} \item{scope}{Where to install the skill. One of: \itemize{ \item \code{"project"} (default): Installs to a project-level skills directory, -chosen from \verb{.btw/skills/}, \verb{.agents/skills/}, or \verb{.claude/skills/} +chosen from \verb{.btw/skills/} or \verb{.agents/skills/} in that order. If one already exists, it is used; otherwise \verb{.btw/skills/} is created. \item \code{"user"}: Installs to the user-level skills directory diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 8a850ee7..8c109a99 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -635,6 +635,36 @@ test_that("btw_skill_install_github() errors for invalid repo format", { expect_error(btw_skill_install_github("a/b/c"), "owner/repo") expect_error(btw_skill_install_github("/repo"), "owner/repo") expect_error(btw_skill_install_github("owner/"), "owner/repo") + expect_error(btw_skill_install_github("owner/repo@"), "empty ref") +}) + +test_that("btw_skill_install_github() parses @ref from repo string", { + zip_path <- create_github_zipball(list( + list(name = "gh-skill", description = "A GitHub skill.") + )) + + local_mocked_bindings( + check_installed = function(...) invisible(), + .package = "rlang" + ) + + captured_ref <- NULL + mock_gh <- function(..., .destfile = NULL) { + args <- list(...) + captured_ref <<- args$ref + file.copy(zip_path, .destfile) + } + + local_mocked_bindings(gh = mock_gh, .package = "gh") + + target_base <- withr::local_tempdir() + withr::local_dir(target_base) + + expect_message( + btw_skill_install_github("owner/repo@v1.0", scope = "project"), + "Installed skill" + ) + expect_equal(captured_ref, "v1.0") }) test_that("btw_skill_install_github() installs single skill", { diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 06566a91..e6e456d7 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -22,6 +22,59 @@ test_that("check_inherits() works", { expect_error(check_inherits(as_btw_docs_package("btw"), "not_this_class")) }) +# Tests for parse_github_repo() -------------------------------------------- + +describe("parse_github_repo()", { + it("parses owner/repo", { + result <- parse_github_repo("owner/repo") + expect_equal(result$owner, "owner") + expect_equal(result$repo, "repo") + expect_equal(result$ref, "HEAD") + }) + + it("parses owner/repo@ref", { + result <- parse_github_repo("owner/repo@v1.0") + expect_equal(result$owner, "owner") + expect_equal(result$repo, "repo") + expect_equal(result$ref, "v1.0") + }) + + it("parses owner/repo@branch", { + result <- parse_github_repo("owner/repo@main") + expect_equal(result$ref, "main") + }) + + it("parses owner/repo@sha", { + result <- parse_github_repo("owner/repo@abc1234") + expect_equal(result$ref, "abc1234") + }) + + it("errors for empty ref after @", { + expect_error(parse_github_repo("owner/repo@"), "empty ref") + }) + + it("errors for missing slash", { + expect_error(parse_github_repo("badformat"), "owner/repo") + }) + + it("errors for too many slashes", { + expect_error(parse_github_repo("a/b/c"), "owner/repo") + }) + + it("errors for empty owner", { + expect_error(parse_github_repo("/repo"), "owner/repo") + }) + + it("errors for empty repo", { + expect_error(parse_github_repo("owner/"), "owner/repo") + }) + + it("errors for non-string input", { + expect_error(parse_github_repo(123)) + expect_error(parse_github_repo(NULL)) + }) +}) + # Tests for remove_base64_images() ----------------------------------------- describe("remove_base64_images()", { From 5968e60b2cb7ed7615aac628b36a81c392faf551 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 13:45:02 -0500 Subject: [PATCH 17/28] refactor(skills): two-tier validation, improved error messages, and hardening MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Split validate_skill() into hard errors (missing SKILL.md, unparseable frontmatter, missing name/description) and soft warnings (name format, extra fields, length limits). Skills with only warnings are still loaded. - Eliminate <<- in tryCatch and fix double-message on parse failure - find_skill() now returns validation info instead of NULL for invalid skills - btw_tool_fetch_skill_impl() surfaces "exists but has validation errors" instead of generic "not found" when a skill is on disk but invalid - btw_skill_create() uses frontmatter::write_front_matter() instead of paste0, preventing malformed YAML from special characters in descriptions - btw_list_skills() emits informational message when a skill is overridden by a higher-priority directory - Fix cli_alert_success → cli_alert_info in resolve_project_skill_dir() - Add project_dir param to btw_skill_directories() (default getwd()) - Optimize btw_can_register with lightweight any_skills_exist() check - Extract validate_skill_name_format() for reuse - Add skill_validation() constructor for consistent return structure - New/updated tests: override ordering, parse failure warning, find_skill validation info, two-tier assertion updates (145 total) --- R/tool-skills.R | 268 ++++++++++++++++++++---------- tests/testthat/test-tool_skills.R | 97 +++++++---- 2 files changed, 247 insertions(+), 118 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index a2cad28b..30e3d38f 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -37,6 +37,18 @@ btw_tool_fetch_skill_impl <- function(skill_name) { ) } + if (!skill_info$validation$valid) { + cli::cli_abort( + c( + "Skill {.val {skill_name}} exists but has validation errors:", + set_names( + skill_info$validation$errors, + rep("!", length(skill_info$validation$errors)) + ) + ) + ) + } + fm <- frontmatter::read_front_matter(skill_info$path) skill_text <- fm$body %||% "" @@ -78,7 +90,7 @@ btw_tool_fetch_skill_impl <- function(skill_name) { title = "Fetch Skill", read_only_hint = TRUE, open_world_hint = FALSE, - btw_can_register = function() length(btw_list_skills()) > 0 + btw_can_register = function() any_skills_exist() ), arguments = list( skill_name = ellmer::type_string( @@ -91,7 +103,7 @@ btw_tool_fetch_skill_impl <- function(skill_name) { # Skill Discovery ---------------------------------------------------------- -btw_skill_directories <- function() { +btw_skill_directories <- function(project_dir = getwd()) { dirs <- character() # Package-bundled skills @@ -111,7 +123,7 @@ btw_skill_directories <- function() { # Project-level skills from multiple conventions for (project_subdir in project_skill_subdirs()) { - project_skills_dir <- file.path(getwd(), project_subdir) + project_skills_dir <- file.path(project_dir, project_subdir) if (dir.exists(project_skills_dir)) { dirs <- c(dirs, project_skills_dir) } @@ -120,6 +132,19 @@ btw_skill_directories <- function() { dirs } +any_skills_exist <- function() { + for (dir in btw_skill_directories()) { + if (!dir.exists(dir)) next + subdirs <- list.dirs(dir, full.names = TRUE, recursive = FALSE) + for (subdir in subdirs) { + if (file.exists(file.path(subdir, "SKILL.md"))) { + return(TRUE) + } + } + } + FALSE +} + project_skill_subdirs <- function() { c( file.path(".btw", "skills"), @@ -145,7 +170,7 @@ resolve_project_skill_dir <- function() { return(existing[[1]]) } - cli::cli_alert_success("Multiple skill directories found in project:") + cli::cli_alert_info("Multiple skill directories found in project:") choice <- utils::menu( choices = existing, graphics = FALSE, @@ -180,11 +205,21 @@ btw_list_skills <- function() { if (!validation$valid) { cli::cli_warn(c( "Skipping invalid skill in {.path {subdir}}.", - set_names(validation$issues, rep("!", length(validation$issues))) + set_names(validation$errors, rep("!", length(validation$errors))) )) next } + if (length(validation$warnings) > 0) { + cli::cli_warn(c( + "Skill in {.path {subdir}} has validation warnings.", + set_names( + validation$warnings, + rep("!", length(validation$warnings)) + ) + )) + } + metadata <- extract_skill_metadata(skill_md_path) skill_name <- basename(subdir) @@ -201,6 +236,12 @@ btw_list_skills <- function() { skill_entry$allowed_tools <- metadata[["allowed-tools"]] } + if (skill_name %in% names(all_skills)) { + cli::cli_inform( + "Skill {.val {skill_name}} in {.path {subdir}} overrides earlier skill at {.path {all_skills[[skill_name]]$path}}." + ) + } + all_skills[[skill_name]] <- skill_entry } } @@ -217,11 +258,16 @@ find_skill <- function(skill_name) { if (dir.exists(skill_dir) && file.exists(skill_md_path)) { validation <- validate_skill(skill_dir) if (!validation$valid) { - return(NULL) + return(list( + path = skill_md_path, + base_dir = skill_dir, + validation = validation + )) } return(list( path = skill_md_path, - base_dir = skill_dir + base_dir = skill_dir, + validation = validation )) } } @@ -254,6 +300,25 @@ validate_skill_name <- function(name, dir_name = NULL) { return(issues) } + issues <- c(issues, validate_skill_name_format(name)) + + if (!is.null(dir_name) && name != dir_name) { + issues <- c( + issues, + sprintf( + "Name '%s' in frontmatter does not match directory name '%s'.", + name, + dir_name + ) + ) + } + + issues +} + +validate_skill_name_format <- function(name) { + issues <- character() + if (nchar(name) > 64) { issues <- c( issues, @@ -275,58 +340,82 @@ validate_skill_name <- function(name, dir_name = NULL) { sprintf("Name '%s' must not contain consecutive hyphens.", name) ) } - if (!is.null(dir_name) && name != dir_name) { - issues <- c( - issues, - sprintf( - "Name '%s' in frontmatter does not match directory name '%s'.", - name, - dir_name - ) - ) - } issues } validate_skill <- function(skill_dir) { skill_dir <- normalizePath(skill_dir, mustWork = FALSE) - issues <- character() + errors <- character() + warnings <- character() skill_md_path <- file.path(skill_dir, "SKILL.md") if (!file.exists(skill_md_path)) { - return(list( - valid = FALSE, - issues = "SKILL.md not found." - )) + return(skill_validation(errors = "SKILL.md not found.")) } - metadata <- tryCatch( - { - fm <- frontmatter::read_front_matter(skill_md_path) - fm$data - }, - error = function(e) { - issues <<- c( - issues, - sprintf("Failed to parse frontmatter: %s", e$message) - ) - NULL - } + # Parse frontmatter — return the result or the error, never use <<- + parse_result <- tryCatch( + frontmatter::read_front_matter(skill_md_path), + error = function(e) e ) + if (inherits(parse_result, "error")) { + return(skill_validation( + errors = sprintf("Failed to parse frontmatter: %s", parse_result$message) + )) + } + + metadata <- parse_result$data + if (is.null(metadata)) { - issues <- c(issues, "No YAML frontmatter found.") - return(list(valid = FALSE, issues = issues)) + return(skill_validation(errors = "No YAML frontmatter found.")) } if (!is.list(metadata)) { - return(list( - valid = FALSE, - issues = "Frontmatter must be a YAML mapping." - )) + return(skill_validation(errors = "Frontmatter must be a YAML mapping.")) } + # --- Hard errors: things that prevent the skill from working --- + + # Validate name (missing/empty is an error, format issues are warnings) + name <- metadata$name + if (is.null(name) || !is.character(name) || !nzchar(name)) { + errors <- c(errors, "Missing or empty 'name' field in frontmatter.") + } else { + name_format_issues <- validate_skill_name_format(name) + warnings <- c(warnings, name_format_issues) + + if (!is.null(skill_dir) && name != basename(skill_dir)) { + warnings <- c( + warnings, + sprintf( + "Name '%s' in frontmatter does not match directory name '%s'.", + name, + basename(skill_dir) + ) + ) + } + } + + # Validate description (missing/empty is an error, too long is a warning) + description <- metadata$description + if ( + is.null(description) || !is.character(description) || !nzchar(description) + ) { + errors <- c(errors, "Missing or empty 'description' field in frontmatter.") + } else if (nchar(description) > 1024) { + warnings <- c( + warnings, + sprintf( + "Description is too long (%d characters, max 1024).", + nchar(description) + ) + ) + } + + # --- Soft warnings: spec compliance that doesn't break functionality --- + # Check for unexpected properties allowed_fields <- c( "name", @@ -338,8 +427,8 @@ validate_skill <- function(skill_dir) { ) unexpected <- setdiff(names(metadata), allowed_fields) if (length(unexpected) > 0) { - issues <- c( - issues, + warnings <- c( + warnings, sprintf( "Unexpected frontmatter field(s): %s. Allowed fields: %s.", paste(unexpected, collapse = ", "), @@ -348,35 +437,16 @@ validate_skill <- function(skill_dir) { ) } - # Validate name - issues <- c(issues, validate_skill_name(metadata$name, basename(skill_dir))) - - # Validate description - description <- metadata$description - if ( - is.null(description) || !is.character(description) || !nzchar(description) - ) { - issues <- c(issues, "Missing or empty 'description' field in frontmatter.") - } else if (nchar(description) > 1024) { - issues <- c( - issues, - sprintf( - "Description is too long (%d characters, max 1024).", - nchar(description) - ) - ) - } - # Validate optional fields if (!is.null(metadata$compatibility)) { if (!is.character(metadata$compatibility)) { - issues <- c( - issues, + warnings <- c( + warnings, "The 'compatibility' field must be a character string." ) } else if (nchar(metadata$compatibility) > 500) { - issues <- c( - issues, + warnings <- c( + warnings, sprintf( "Compatibility field is too long (%d characters, max 500).", nchar(metadata$compatibility) @@ -386,12 +456,22 @@ validate_skill <- function(skill_dir) { } if (!is.null(metadata$metadata) && !is.list(metadata$metadata)) { - issues <- c(issues, "The 'metadata' field must be a key-value mapping.") + warnings <- c( + warnings, + "The 'metadata' field must be a key-value mapping." + ) } + skill_validation(errors = errors, warnings = warnings) +} + +skill_validation <- function(errors = character(), warnings = character()) { list( - valid = length(issues) == 0, - issues = issues + valid = length(errors) == 0, + errors = errors, + warnings = warnings, + # Combined for backward compatibility with callers using $issues + issues = c(errors, warnings) ) } @@ -608,25 +688,15 @@ btw_skill_create <- function( "TODO: Describe what this skill does and when to use it." } - skill_md_content <- paste0( - "---\n", - "name: ", - name, - "\n", - "description: ", - description_line, - "\n", - "---\n", - "\n", - "# ", - skill_title, - "\n", - "\n", - "TODO: Add skill instructions here.\n" + body <- paste0("\n# ", skill_title, "\n\nTODO: Add skill instructions here.\n") + frontmatter::write_front_matter( + list( + data = list(name = name, description = description_line), + body = body + ), + file.path(skill_dir, "SKILL.md") ) - write_lines(skill_md_content, file.path(skill_dir, "SKILL.md")) - if (resources) { dir.create(file.path(skill_dir, "scripts"), showWarnings = FALSE) dir.create(file.path(skill_dir, "references"), showWarnings = FALSE) @@ -666,12 +736,22 @@ btw_skill_validate <- function(path = ".") { result <- validate_skill(path) - if (result$valid) { + if (result$valid && length(result$warnings) == 0) { cli::cli_inform(c("v" = "Skill at {.path {path}} is valid.")) + } else if (result$valid) { + cli::cli_inform(c( + "v" = "Skill at {.path {path}} is valid with warnings:", + set_names(result$warnings, rep("!", length(result$warnings))) + )) } else { cli::cli_inform(c( - "x" = "Skill at {.path {path}} has validation issues:", - set_names(result$issues, rep("!", length(result$issues))) + "x" = "Skill at {.path {path}} has validation errors:", + set_names(result$errors, rep("!", length(result$errors))), + if (length(result$warnings) > 0) { + c( + set_names(result$warnings, rep("!", length(result$warnings))) + ) + } )) } @@ -925,7 +1005,17 @@ install_skill_from_dir <- function( if (!validation$valid) { cli::cli_abort(c( "Cannot install invalid skill:", - set_names(validation$issues, rep("!", length(validation$issues))) + set_names(validation$errors, rep("!", length(validation$errors))) + )) + } + + if (length(validation$warnings) > 0) { + cli::cli_warn(c( + "Installing skill {.val {skill_name}} with validation warnings:", + set_names( + validation$warnings, + rep("!", length(validation$warnings)) + ) )) } diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 8c109a99..5af8db67 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -40,7 +40,7 @@ test_that("validate_skill() fails for missing description field", { expect_match(result$issues, "Missing or empty 'description'", all = FALSE) }) -test_that("validate_skill() fails for name with uppercase", { +test_that("validate_skill() warns for name with uppercase", { skill_dir <- create_temp_skill(name = "test-skill") # Manually override the name in SKILL.md to have uppercase writeLines( @@ -48,11 +48,11 @@ test_that("validate_skill() fails for name with uppercase", { file.path(skill_dir, "SKILL.md") ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "lowercase letters", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "lowercase letters", all = FALSE) }) -test_that("validate_skill() fails for name starting with hyphen", { +test_that("validate_skill() warns for name starting with hyphen", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "-bad-name") dir.create(skill_dir) @@ -61,15 +61,15 @@ test_that("validate_skill() fails for name starting with hyphen", { file.path(skill_dir, "SKILL.md") ) result <- validate_skill(skill_dir) - expect_false(result$valid) + expect_true(result$valid) expect_match( - result$issues, + result$warnings, "must not start or end with a hyphen", all = FALSE ) }) -test_that("validate_skill() fails for consecutive hyphens", { +test_that("validate_skill() warns for consecutive hyphens", { dir <- withr::local_tempdir() skill_dir <- file.path(dir, "bad--name") dir.create(skill_dir) @@ -78,11 +78,11 @@ test_that("validate_skill() fails for consecutive hyphens", { file.path(skill_dir, "SKILL.md") ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "consecutive hyphens", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "consecutive hyphens", all = FALSE) }) -test_that("validate_skill() fails for name exceeding 64 characters", { +test_that("validate_skill() warns for name exceeding 64 characters", { long_name <- paste(rep("a", 65), collapse = "") dir <- withr::local_tempdir() skill_dir <- file.path(dir, long_name) @@ -92,34 +92,34 @@ test_that("validate_skill() fails for name exceeding 64 characters", { file.path(skill_dir, "SKILL.md") ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "too long", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "too long", all = FALSE) }) -test_that("validate_skill() fails when name doesn't match directory", { +test_that("validate_skill() warns when name doesn't match directory", { skill_dir <- create_temp_skill(name = "test-skill") writeLines( "---\nname: other-name\ndescription: A test.\n---\nBody.", file.path(skill_dir, "SKILL.md") ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "does not match directory name", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "does not match directory name", all = FALSE) }) -test_that("validate_skill() fails for description exceeding 1024 characters", { +test_that("validate_skill() warns for description exceeding 1024 characters", { long_desc <- paste(rep("a", 1025), collapse = "") skill_dir <- create_temp_skill(description = long_desc) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "Description is too long", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "Description is too long", all = FALSE) }) -test_that("validate_skill() flags unexpected frontmatter fields", { +test_that("validate_skill() warns for unexpected frontmatter fields", { skill_dir <- create_temp_skill(extra_frontmatter = list(bogus = TRUE)) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "Unexpected frontmatter", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "Unexpected frontmatter", all = FALSE) }) test_that("validate_skill() accepts optional fields", { @@ -135,14 +135,14 @@ test_that("validate_skill() accepts optional fields", { expect_true(result$valid) }) -test_that("validate_skill() fails for compatibility exceeding 500 chars", { +test_that("validate_skill() warns for compatibility exceeding 500 chars", { long_compat <- paste(rep("a", 501), collapse = "") skill_dir <- create_temp_skill( extra_frontmatter = list(compatibility = long_compat) ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "Compatibility field is too long", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "Compatibility field is too long", all = FALSE) }) # Discovery ---------------------------------------------------------------- @@ -181,6 +181,31 @@ test_that("btw_list_skills() skips invalid skills with warning", { expect_equal(skills[["good-skill"]]$name, "good-skill") }) +test_that("btw_list_skills() later directories override earlier by name", { + dir1 <- withr::local_tempdir() + dir2 <- withr::local_tempdir() + + create_temp_skill( + name = "shared-skill", + description = "From dir1.", + dir = dir1 + ) + create_temp_skill( + name = "shared-skill", + description = "From dir2.", + dir = dir2 + ) + + local_skill_dirs(c(dir1, dir2)) + + expect_message( + skills <- btw_list_skills(), + "overrides earlier skill" + ) + expect_length(skills, 1) + expect_equal(skills[["shared-skill"]]$description, "From dir2.") +}) + test_that("btw_list_skills() includes compatibility and allowed-tools", { dir <- withr::local_tempdir() create_temp_skill( @@ -295,6 +320,17 @@ test_that("extract_skill_metadata() returns empty list for bad files", { expect_equal(metadata, list()) }) +test_that("extract_skill_metadata() warns on parse failure", { + tmp <- withr::local_tempfile(fileext = ".md") + # Write invalid YAML frontmatter + writeLines("---\n: invalid yaml: [\n---\nBody.", tmp) + expect_warning( + metadata <- extract_skill_metadata(tmp), + "Failed to parse frontmatter" + ) + expect_equal(metadata, list()) +}) + # Resources ---------------------------------------------------------------- test_that("list_skill_resources() finds files recursively", { @@ -461,7 +497,7 @@ test_that("btw_skill_validate() reports issues", { "---\nname: bad-skill\n---\nBody.", file.path(skill_dir, "SKILL.md") ) - expect_message(result <- btw_skill_validate(skill_dir), "validation issues") + expect_message(result <- btw_skill_validate(skill_dir), "validation errors") expect_false(result$valid) }) @@ -974,13 +1010,13 @@ test_that("validate_skill() flags non-character compatibility", { ) result <- validate_skill(skill_dir) - expect_false(result$valid) - expect_match(result$issues, "must be a character string", all = FALSE) + expect_true(result$valid) + expect_match(result$warnings, "must be a character string", all = FALSE) }) # find_skill() with invalid skill ------------------------------------------ -test_that("find_skill() returns NULL for invalid skill on disk", { +test_that("find_skill() returns validation errors for invalid skill on disk", { dir <- withr::local_tempdir() # Create a skill that exists on disk but fails validation (missing description) bad_dir <- file.path(dir, "bad-skill") @@ -988,7 +1024,10 @@ test_that("find_skill() returns NULL for invalid skill on disk", { writeLines("---\nname: bad-skill\n---\nBody.", file.path(bad_dir, "SKILL.md")) local_skill_dirs(dir) - expect_null(find_skill("bad-skill")) + result <- find_skill("bad-skill") + expect_type(result, "list") + expect_false(result$validation$valid) + expect_match(result$validation$errors, "description", all = FALSE) }) # btw_skill_create() long description warning ------------------------------- From aff5e749276abc0d1d8e75b7ca964f06675fb8c4 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 14:31:30 -0500 Subject: [PATCH 18/28] refactor(skills): code review fixes - find_skill(): collapse dead duplicate return branches; add slow-path metadata$name scan so fetch works when frontmatter name differs from dir name - skill_validation(): carry parsed metadata through; validate_skill() passes it at final return; btw_list_skills() reads validation$metadata directly, eliminating the double-parse of SKILL.md - btw_list_skills(): use metadata$name as authoritative skill identity instead of basename(subdir); remove unreachable description fallback - resolve_project_skill_dir(): accept error_call; resolve_skill_scope() passes it through so cli_abort() reports the right call site - any_skills_exist(): remove redundant dir.exists() check (already gated by btw_skill_directories()) - validate_skill(): improve name/dir mismatch warning to recommend renaming - format_resources_listing(): normalize Scripts to \n\n spacing like References and Assets --- R/tool-skills.R | 57 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 30e3d38f..364fd8a3 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -134,7 +134,6 @@ btw_skill_directories <- function(project_dir = getwd()) { any_skills_exist <- function() { for (dir in btw_skill_directories()) { - if (!dir.exists(dir)) next subdirs <- list.dirs(dir, full.names = TRUE, recursive = FALSE) for (subdir in subdirs) { if (file.exists(file.path(subdir, "SKILL.md"))) { @@ -152,7 +151,7 @@ project_skill_subdirs <- function() { ) } -resolve_project_skill_dir <- function() { +resolve_project_skill_dir <- function(error_call = caller_env()) { candidates <- file.path(getwd(), project_skill_subdirs()) existing <- candidates[dir.exists(candidates)] @@ -178,7 +177,7 @@ resolve_project_skill_dir <- function() { ) if (choice == 0) { - cli::cli_abort("Aborted by user.") + cli::cli_abort("Aborted by user.", call = error_call) } existing[[choice]] @@ -220,12 +219,12 @@ btw_list_skills <- function() { )) } - metadata <- extract_skill_metadata(skill_md_path) - skill_name <- basename(subdir) + metadata <- validation$metadata + skill_name <- metadata$name skill_entry <- list( name = skill_name, - description = metadata$description %||% "No description available", + description = metadata$description, path = skill_md_path ) @@ -252,18 +251,12 @@ btw_list_skills <- function() { find_skill <- function(skill_name) { skill_dirs <- btw_skill_directories() + # Fast path: directory named skill_name for (dir in skill_dirs) { skill_dir <- file.path(dir, skill_name) skill_md_path <- file.path(skill_dir, "SKILL.md") if (dir.exists(skill_dir) && file.exists(skill_md_path)) { validation <- validate_skill(skill_dir) - if (!validation$valid) { - return(list( - path = skill_md_path, - base_dir = skill_dir, - validation = validation - )) - } return(list( path = skill_md_path, base_dir = skill_dir, @@ -272,6 +265,24 @@ find_skill <- function(skill_name) { } } + # Slow path: scan all skills for a matching metadata$name (handles name/dir mismatch) + for (dir in skill_dirs) { + subdirs <- list.dirs(dir, full.names = TRUE, recursive = FALSE) + for (subdir in subdirs) { + skill_md_path <- file.path(subdir, "SKILL.md") + if (!file.exists(skill_md_path)) next + validation <- validate_skill(subdir) + if (!is.null(validation$metadata$name) && + validation$metadata$name == skill_name) { + return(list( + path = skill_md_path, + base_dir = subdir, + validation = validation + )) + } + } + } + NULL } @@ -390,9 +401,10 @@ validate_skill <- function(skill_dir) { warnings <- c( warnings, sprintf( - "Name '%s' in frontmatter does not match directory name '%s'.", + "Name '%s' in frontmatter does not match directory name '%s'. Consider renaming the directory to '%s'.", name, - basename(skill_dir) + basename(skill_dir), + name ) ) } @@ -462,16 +474,21 @@ validate_skill <- function(skill_dir) { ) } - skill_validation(errors = errors, warnings = warnings) + skill_validation(errors = errors, warnings = warnings, metadata = metadata) } -skill_validation <- function(errors = character(), warnings = character()) { +skill_validation <- function( + errors = character(), + warnings = character(), + metadata = NULL +) { list( valid = length(errors) == 0, errors = errors, warnings = warnings, # Combined for backward compatibility with callers using $issues - issues = c(errors, warnings) + issues = c(errors, warnings), + metadata = metadata ) } @@ -508,7 +525,7 @@ format_resources_listing <- function(resources, base_dir) { parts <- c(parts, "\n\n---\n\n## Bundled Resources\n") if (length(resources$scripts) > 0) { - parts <- c(parts, "\n**Scripts:**\n") + parts <- c(parts, "\n\n**Scripts:**\n") script_paths <- file.path(base_dir, "scripts", resources$scripts) parts <- c(parts, paste0("- ", script_paths, collapse = "\n")) } @@ -600,7 +617,7 @@ resolve_skill_scope <- function(scope, error_call = caller_env()) { switch( scope, - project = resolve_project_skill_dir(), + project = resolve_project_skill_dir(error_call = error_call), user = file.path(tools::R_user_dir("btw", "config"), "skills"), scope ) From 5baec0fcaf5bcf3111344ba0a95b30e47812c472 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 14:34:55 -0500 Subject: [PATCH 19/28] tests(skills): normalize paths in Windows-failing assertions Three tests compared paths using == after file.path(), which produces mixed separators on Windows (withr::local_tempdir() normalizes to backslashes, but file.path() appends with forward slashes). Wrap both sides of the affected expect_equal() calls with normalizePath() so the comparison is separator-agnostic. --- tests/testthat/test-tool_skills.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 5af8db67..29c211ae 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -478,7 +478,7 @@ test_that("btw_skill_create() treats I('project') as literal directory name", { ) expect_true(dir.exists(path)) - expect_equal(dirname(path), file.path(dir, "project")) + expect_equal(normalizePath(dirname(path)), normalizePath(file.path(dir, "project"))) }) # btw_skill_validate ------------------------------------------------------- @@ -612,7 +612,7 @@ test_that("install_skill_from_dir() accepts custom path as scope", { ), "Installed skill" ) - expect_equal(dirname(path), custom_dir) + expect_equal(normalizePath(dirname(path)), normalizePath(custom_dir)) expect_true(file.exists(file.path(path, "SKILL.md"))) }) @@ -629,7 +629,7 @@ test_that("install_skill_from_dir() treats I('project') as literal path", { ), "Installed skill" ) - expect_equal(dirname(path), file.path(target, "project")) + expect_equal(normalizePath(dirname(path)), normalizePath(file.path(target, "project"))) }) # btw_skill_install_github ------------------------------------------------- From 82c1b9d36459f2e93cef0d631470b4e5d47d72fe Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Tue, 17 Feb 2026 18:10:03 -0500 Subject: [PATCH 20/28] feat(skills): prompt interactively when overwrite conflict exists Change `overwrite` default from `FALSE` to `NULL` in `btw_skill_install_github()`, `btw_skill_install_package()`, and `install_skill_from_dir()`. When `NULL` and a skill already exists, shows a `utils::menu()` prompt in interactive sessions; in non-interactive sessions silently defaults to `FALSE` (error on conflict). Explicit `TRUE`/`FALSE` behave as before. --- R/tool-skills.R | 42 +++++++++++++++++++++++--------- man/btw_skill_install_github.Rd | 8 +++--- man/btw_skill_install_package.Rd | 8 +++--- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/R/tool-skills.R b/R/tool-skills.R index 364fd8a3..3864bc3d 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -849,8 +849,10 @@ select_skill_dir <- function( #' - A directory path: Installs to a custom directory, e.g. #' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` #' if you need a literal directory with those names. -#' @param overwrite If `TRUE`, overwrite an existing skill with the same name. -#' Defaults to `FALSE`, which errors if the skill already exists. +#' @param overwrite Whether to overwrite an existing skill with the same name. +#' If `NULL` (default), prompts interactively when a conflict exists; in +#' non-interactive sessions defaults to `FALSE`, which errors. Set to `TRUE` +#' to always overwrite, or `FALSE` to always error on conflict. #' #' @return The path to the installed skill directory, invisibly. #' @@ -860,14 +862,14 @@ btw_skill_install_github <- function( repo, skill = NULL, scope = "project", - overwrite = FALSE + overwrite = NULL ) { check_string(repo) if (!is.null(skill)) { check_string(skill) } check_string(scope) - check_bool(overwrite) + if (!is.null(overwrite)) check_bool(overwrite) rlang::check_installed("gh", reason = "to install skills from GitHub.") @@ -943,8 +945,10 @@ btw_skill_install_github <- function( #' - A directory path: Installs to a custom directory, e.g. #' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` #' if you need a literal directory with those names. -#' @param overwrite If `TRUE`, overwrite an existing skill with the same name. -#' Defaults to `FALSE`, which errors if the skill already exists. +#' @param overwrite Whether to overwrite an existing skill with the same name. +#' If `NULL` (default), prompts interactively when a conflict exists; in +#' non-interactive sessions defaults to `FALSE`, which errors. Set to `TRUE` +#' to always overwrite, or `FALSE` to always error on conflict. #' #' @return The path to the installed skill directory, invisibly. #' @@ -954,14 +958,14 @@ btw_skill_install_package <- function( package, skill = NULL, scope = "project", - overwrite = FALSE + overwrite = NULL ) { check_string(package) if (!is.null(skill)) { check_string(skill) } check_string(scope) - check_bool(overwrite) + if (!is.null(overwrite)) check_bool(overwrite) rlang::check_installed(package, reason = "to install skills from it.") @@ -990,11 +994,11 @@ btw_skill_install_package <- function( install_skill_from_dir <- function( source_dir, scope = "project", - overwrite = FALSE + overwrite = NULL ) { check_string(source_dir) check_string(scope) - check_bool(overwrite) + if (!is.null(overwrite)) check_bool(overwrite) source_dir <- normalizePath(source_dir, mustWork = FALSE) if (!dir.exists(source_dir)) { @@ -1008,7 +1012,23 @@ install_skill_from_dir <- function( target_dir <- file.path(target_parent, skill_name) if (dir.exists(target_dir)) { - if (overwrite) { + do_overwrite <- if (is.null(overwrite)) { + if (rlang::is_interactive()) { + choice <- utils::menu( + c("Yes", "No"), + title = cli::format_inline( + "Skill {.val {skill_name}} already exists at {.path {target_dir}}. Overwrite?" + ) + ) + choice == 1L + } else { + FALSE + } + } else { + overwrite + } + + if (do_overwrite) { unlink(target_dir, recursive = TRUE) } else { cli::cli_abort( diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index b9295345..42e1106c 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -8,7 +8,7 @@ btw_skill_install_github( repo, skill = NULL, scope = "project", - overwrite = FALSE + overwrite = NULL ) } \arguments{ @@ -34,8 +34,10 @@ in that order. If one already exists, it is used; otherwise if you need a literal directory with those names. }} -\item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. -Defaults to \code{FALSE}, which errors if the skill already exists.} +\item{overwrite}{Whether to overwrite an existing skill with the same name. +If \code{NULL} (default), prompts interactively when a conflict exists; in +non-interactive sessions defaults to \code{FALSE}, which errors. Set to \code{TRUE} +to always overwrite, or \code{FALSE} to always error on conflict.} } \value{ The path to the installed skill directory, invisibly. diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index 2903f13d..16c3a7ae 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -8,7 +8,7 @@ btw_skill_install_package( package, skill = NULL, scope = "project", - overwrite = FALSE + overwrite = NULL ) } \arguments{ @@ -31,8 +31,10 @@ in that order. If one already exists, it is used; otherwise if you need a literal directory with those names. }} -\item{overwrite}{If \code{TRUE}, overwrite an existing skill with the same name. -Defaults to \code{FALSE}, which errors if the skill already exists.} +\item{overwrite}{Whether to overwrite an existing skill with the same name. +If \code{NULL} (default), prompts interactively when a conflict exists; in +non-interactive sessions defaults to \code{FALSE}, which errors. Set to \code{TRUE} +to always overwrite, or \code{FALSE} to always error on conflict.} } \value{ The path to the installed skill directory, invisibly. From 94a845d832bc39cc3d2467a7f879a4a4689ccee5 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 18 Feb 2026 08:09:13 -0500 Subject: [PATCH 21/28] feat(skills): warn when skills tool enabled without read file tool In btw_client(), emit a cli::cli_warn() after setting tools if btw_tool_fetch_skill is present but btw_tool_files_read is not. In btw_app(), track the mismatch state with a shiny::reactive() and use shiny::observeEvent() to show a toast notification (via notifier()) only when transitioning into the mismatched state. Extracts notifier() from btw_status_bar_server to module scope so it can be reused for consistent toast styling. --- R/btw_client.R | 15 +++++ R/btw_client_app.R | 94 ++++++++++++++++++-------------- tests/testthat/test-btw_client.R | 33 +++++++++++ tests/testthat/test-mcp.R | 7 +++ 4 files changed, 109 insertions(+), 40 deletions(-) diff --git a/R/btw_client.R b/R/btw_client.R index 79896ee4..b205cddb 100644 --- a/R/btw_client.R +++ b/R/btw_client.R @@ -174,6 +174,8 @@ btw_client <- function( client$set_tools(tools = c(client$get_tools(), config$tools)) } + warn_skills_without_read_file(client$get_tools()) + client } @@ -665,3 +667,16 @@ remove_hidden_content <- function(text) { paste(lines[starts - shift(cumsum(ends)) <= 0], collapse = "\n") } + +warn_skills_without_read_file <- function(tools) { + tool_names <- names(tools) + has_skills <- "btw_tool_fetch_skill" %in% tool_names + has_read_file <- "btw_tool_files_read" %in% tool_names + if (has_skills && !has_read_file) { + cli::cli_warn(c( + "The {.fn btw_tool_fetch_skill} tool is enabled but {.fn btw_tool_files_read} is not.", + "i" = "Skills work best with the read file tool, which lets the model read skill resource files.", + "i" = "Add {.code btw_tools(\"files\")} or enable {.fn btw_tool_files_read} to get full skill support." + )) + } +} diff --git a/R/btw_client_app.R b/R/btw_client_app.R index c093103a..96839152 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -270,6 +270,20 @@ btw_app_from_client <- function( } }) + skills_read_file_mismatch <- shiny::reactive({ + sel <- selected_tools() + "btw_tool_fetch_skill" %in% sel && !"btw_tool_files_read" %in% sel + }) + + shiny::observeEvent(skills_read_file_mismatch(), { + if (isTRUE(skills_read_file_mismatch())) { + notifier( + shiny::icon("triangle-exclamation"), + "Skills tool works best with the Read File tool enabled" + ) + } + }) + output$ui_other_tools <- shiny::renderUI({ if (length(other_tools) == 0) { return(NULL) @@ -392,6 +406,46 @@ btw_app_from_client <- function( # Status Bar ---- +notifier <- function(icon, action, error = NULL) { + error_body <- if (!is.null(error)) { + shiny::p(shiny::HTML(sprintf("%s", error$message))) + } + + bslib_toast <- asNamespace("bslib")[["toast"]] + bslib_show_toast <- asNamespace("bslib")[["show_toast"]] + bslib_toast_header <- asNamespace("bslib")[["toast_header"]] + + if (is.null(bslib_toast) || is.null(bslib_show_toast)) { + if (!is.null(error)) { + body <- shiny::span(icon, action) + } else { + body <- shiny::tagList( + shiny::p( + shiny::icon("warning"), + "Failed to update system prompt", + class = "fw-bold" + ), + error_body + ) + } + shiny::showNotification( + body, + type = if (is.null(error)) "message" else "error" + ) + return() + } + + toast <- bslib_toast( + if (is.null(error)) action else error_body, + header = if (!is.null(error)) { + bslib_toast_header(action, icon = icon) + }, + icon = if (is.null(error)) icon, + position = "top-right" + ) + bslib_show_toast(toast) +} + btw_status_bar_ui <- function(id, provider_model) { ns <- shiny::NS(id) shiny::tagList( @@ -575,46 +629,6 @@ btw_status_bar_server <- function(id, chat) { shiny::showModal(modal) }) - notifier <- function(icon, action, error = NULL) { - error_body <- if (!is.null(error)) { - shiny::p(shiny::HTML(sprintf("%s", error$message))) - } - - bslib_toast <- asNamespace("bslib")[["toast"]] - bslib_show_toast <- asNamespace("bslib")[["show_toast"]] - bslib_toast_header <- asNamespace("bslib")[["toast_header"]] - - if (is.null(bslib_toast) || is.null(bslib_show_toast)) { - if (!is.null(error)) { - body <- shiny::span(icon, action) - } else { - body <- shiny::tagList( - shiny::p( - shiny::icon("warning"), - "Failed to update system prompt", - class = "fw-bold" - ), - error_body - ) - } - shiny::showNotification( - body, - type = if (is.null(error)) "message" else "error" - ) - return() - } - - toast <- bslib_toast( - if (is.null(error)) action else error_body, - header = if (!is.null(error)) { - bslib_toast_header(action, icon = icon) - }, - icon = if (is.null(error)) icon, - position = "top-right" - ) - bslib_show_toast(toast) - } - shiny::observeEvent( input$system_prompt, ignoreInit = TRUE, diff --git a/tests/testthat/test-btw_client.R b/tests/testthat/test-btw_client.R index a2372a5d..6800bd16 100644 --- a/tests/testthat/test-btw_client.R +++ b/tests/testthat/test-btw_client.R @@ -1080,3 +1080,36 @@ describe("btw_client() with multiple clients", { expect_s3_class(chat$get_provider(), "ellmer::ProviderAnthropic") }) }) + +# warn_skills_without_read_file() ----------------------------------------- + +test_that("warn_skills_without_read_file() warns when skills present without read file", { + tools <- list( + btw_tool_fetch_skill = "placeholder", + btw_tool_docs_help_page = "placeholder" + ) + expect_warning( + warn_skills_without_read_file(tools), + "btw_tool_files_read" + ) +}) + +test_that("warn_skills_without_read_file() is silent when both skills and read file present", { + tools <- list( + btw_tool_fetch_skill = "placeholder", + btw_tool_files_read = "placeholder" + ) + expect_no_warning(warn_skills_without_read_file(tools)) +}) + +test_that("warn_skills_without_read_file() is silent when skills not present", { + tools <- list( + btw_tool_docs_help_page = "placeholder", + btw_tool_files_read = "placeholder" + ) + expect_no_warning(warn_skills_without_read_file(tools)) +}) + +test_that("warn_skills_without_read_file() is silent when no tools", { + expect_no_warning(warn_skills_without_read_file(list())) +}) diff --git a/tests/testthat/test-mcp.R b/tests/testthat/test-mcp.R index b26d6d50..cbaa80e4 100644 --- a/tests/testthat/test-mcp.R +++ b/tests/testthat/test-mcp.R @@ -11,3 +11,10 @@ test_that("btw_mcp_server errors informatively with bad `tools`", { btw_mcp_server(tools = "bop") ) }) + +test_that("btw_mcp_tools() excludes skills group by default", { + local_enable_tools() + tools <- btw_mcp_tools() + tool_names <- vapply(tools, function(t) t@name, character(1)) + expect_false("btw_tool_fetch_skill" %in% tool_names) +}) From fc72d103bf43a75f16e8457e095f1ad116f6f7dc Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 18 Feb 2026 08:18:06 -0500 Subject: [PATCH 22/28] feat: make toast a warning --- R/btw_client_app.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/btw_client_app.R b/R/btw_client_app.R index 96839152..8bb0e4bc 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -279,7 +279,10 @@ btw_app_from_client <- function( if (isTRUE(skills_read_file_mismatch())) { notifier( shiny::icon("triangle-exclamation"), - "Skills tool works best with the Read File tool enabled" + shiny::HTML( + "The Fetch Skill tool works best with the Read File tool enabled" + ), + type = "warning" ) } }) @@ -406,7 +409,7 @@ btw_app_from_client <- function( # Status Bar ---- -notifier <- function(icon, action, error = NULL) { +notifier <- function(icon, action, error = NULL, ...) { error_body <- if (!is.null(error)) { shiny::p(shiny::HTML(sprintf("%s", error$message))) } @@ -441,7 +444,8 @@ notifier <- function(icon, action, error = NULL) { bslib_toast_header(action, icon = icon) }, icon = if (is.null(error)) icon, - position = "top-right" + position = "top-right", + ... ) bslib_show_toast(toast) } From 4acbbb73c64c304562aa4d2b522a3062aacc6343 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 18 Feb 2026 08:57:52 -0500 Subject: [PATCH 23/28] feat: adjust toast style --- R/btw_client_app.R | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/R/btw_client_app.R b/R/btw_client_app.R index 8bb0e4bc..86c633da 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -278,15 +278,35 @@ btw_app_from_client <- function( shiny::observeEvent(skills_read_file_mismatch(), { if (isTRUE(skills_read_file_mismatch())) { notifier( - shiny::icon("triangle-exclamation"), - shiny::HTML( - "The Fetch Skill tool works best with the Read File tool enabled" - ), - type = "warning" + id = "skills_read_file_mismatch", + shiny::icon("triangle-exclamation", class = "text-warning"), + shiny::tagList( + shiny::HTML( + "The Fetch Skill tool works best with the Read File tool enabled" + ), + shiny::actionButton( + class = "btn-sm mt-2", + "enable_read_file_tool", + "Enable Read File Tool" + ) + ) ) } }) + shiny::observeEvent(input$enable_read_file_tool, { + file_tools <- c(input$tools_files, "btw_tool_files_read") + shiny::updateCheckboxGroupInput( + session = session, + inputId = "tools_files", + selected = file_tools + ) + bslib_hide_toast <- asNamespace("bslib")[["hide_toast"]] + if (!is.null(bslib_hide_toast)) { + bslib_hide_toast("skills_read_file_mismatch") + } + }) + output$ui_other_tools <- shiny::renderUI({ if (length(other_tools) == 0) { return(NULL) From a39b43d1858f8bf57376f7d13305c3952bf7956f Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 18 Feb 2026 08:59:55 -0500 Subject: [PATCH 24/28] app: list skills first --- R/btw_client_app.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/btw_client_app.R b/R/btw_client_app.R index 86c633da..33f65fef 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -729,7 +729,7 @@ app_tool_group_inputs <- function(tools_df, initial_tool_names = NULL) { # then other, then deprecated (if shown) group_names <- names(tools_df) - priority_groups <- c("agent", "docs", "files", "env") + priority_groups <- c("agent", "skills", "docs", "files", "env") trailing_groups <- c("other", "deprecated") priority_present <- intersect(priority_groups, group_names) middle_groups <- sort(setdiff( From 04232e0a9829ea321e8690299578e4dcf4a9fe79 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 18 Feb 2026 10:50:26 -0500 Subject: [PATCH 25/28] chore: remove unintended new line --- R/mcp.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/mcp.R b/R/mcp.R index 89279b7a..868ac63a 100644 --- a/R/mcp.R +++ b/R/mcp.R @@ -176,7 +176,6 @@ btw_mcp_session <- function() { btw_mcp_tools <- function() { # Skills are excluded from MCP by default: the skill system prompt # (with metadata) is injected by btw_client(), not by - # MCP. Without that context the model has no way to know which skills are # available. Filesystem-based agents (e.g. Claude Code) can read SKILL.md # files directly via their paths in the system prompt. From a4a57f6d3e4a76f9128b8747c5d5570dc9243fe9 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Thu, 19 Feb 2026 14:36:47 -0500 Subject: [PATCH 26/28] feat(skills): rename btw_tool_fetch_skill to btw_tool_skill Align with naming conventions used by other agent frameworks (Claude Code's Skill, OpenCode's skill). Renames parameter skill_name to name, updates tool description to instruct the model to proactively load matching skills and handle /{name} syntax, and adds guidance to reuse previously loaded skills. --- NAMESPACE | 2 +- R/btw_client.R | 4 +- R/btw_client_app.R | 4 +- R/tool-skills.R | 46 ++++++++++--------- inst/prompts/skills.md | 5 +- man/btw_skill_create.Rd | 2 +- man/btw_skill_install_github.Rd | 2 +- man/btw_skill_install_package.Rd | 2 +- man/btw_skill_validate.Rd | 2 +- ..._tool_fetch_skill.Rd => btw_tool_skill.Rd} | 12 ++--- man/btw_tools.Rd | 2 +- tests/testthat/_snaps/tool_skills.md | 5 +- tests/testthat/test-btw_client.R | 4 +- tests/testthat/test-mcp.R | 2 +- tests/testthat/test-tool_skills.R | 10 ++-- 15 files changed, 55 insertions(+), 49 deletions(-) rename man/{btw_tool_fetch_skill.Rd => btw_tool_skill.Rd} (78%) diff --git a/NAMESPACE b/NAMESPACE index ebfc120b..63f60d7f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,7 +43,6 @@ export(btw_tool_docs_package_news) export(btw_tool_docs_vignette) export(btw_tool_env_describe_data_frame) export(btw_tool_env_describe_environment) -export(btw_tool_fetch_skill) export(btw_tool_files_code_search) export(btw_tool_files_edit) export(btw_tool_files_list) @@ -77,6 +76,7 @@ export(btw_tool_session_platform_info) export(btw_tool_sessioninfo_is_package_installed) export(btw_tool_sessioninfo_package) export(btw_tool_sessioninfo_platform) +export(btw_tool_skill) export(btw_tool_web_read_url) export(btw_tools) export(edit_btw_md) diff --git a/R/btw_client.R b/R/btw_client.R index b205cddb..4400cb56 100644 --- a/R/btw_client.R +++ b/R/btw_client.R @@ -670,11 +670,11 @@ remove_hidden_content <- function(text) { warn_skills_without_read_file <- function(tools) { tool_names <- names(tools) - has_skills <- "btw_tool_fetch_skill" %in% tool_names + has_skills <- "btw_tool_skill" %in% tool_names has_read_file <- "btw_tool_files_read" %in% tool_names if (has_skills && !has_read_file) { cli::cli_warn(c( - "The {.fn btw_tool_fetch_skill} tool is enabled but {.fn btw_tool_files_read} is not.", + "The {.fn btw_tool_skill} tool is enabled but {.fn btw_tool_files_read} is not.", "i" = "Skills work best with the read file tool, which lets the model read skill resource files.", "i" = "Add {.code btw_tools(\"files\")} or enable {.fn btw_tool_files_read} to get full skill support." )) diff --git a/R/btw_client_app.R b/R/btw_client_app.R index 33f65fef..4bdda61c 100644 --- a/R/btw_client_app.R +++ b/R/btw_client_app.R @@ -272,7 +272,7 @@ btw_app_from_client <- function( skills_read_file_mismatch <- shiny::reactive({ sel <- selected_tools() - "btw_tool_fetch_skill" %in% sel && !"btw_tool_files_read" %in% sel + "btw_tool_skill" %in% sel && !"btw_tool_files_read" %in% sel }) shiny::observeEvent(skills_read_file_mismatch(), { @@ -282,7 +282,7 @@ btw_app_from_client <- function( shiny::icon("triangle-exclamation", class = "text-warning"), shiny::tagList( shiny::HTML( - "The Fetch Skill tool works best with the Read File tool enabled" + "The Load Skill tool works best with the Read File tool enabled" ), shiny::actionButton( class = "btn-sm mt-2", diff --git a/R/tool-skills.R b/R/tool-skills.R index 3864bc3d..2c7f1ea7 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -1,17 +1,17 @@ #' @include tool-result.R NULL -#' Tool: Fetch a skill +#' Tool: Load a skill #' #' @description -#' Fetch a skill's instructions and list its bundled resources. +#' Load a skill's specialized instructions and list its bundled resources. #' #' Skills are modular capabilities that extend Claude's functionality with #' specialized knowledge, workflows, and tools. Each skill is a directory #' containing a `SKILL.md` file with instructions and optional bundled #' resources (scripts, references, assets). #' -#' @param skill_name The name of the skill to fetch. +#' @param name The name of the skill to load. #' @inheritParams btw_tool_docs_package_news #' #' @return A `btw_tool_result` containing the skill instructions and a listing @@ -19,19 +19,19 @@ NULL #' #' @family skills #' @export -btw_tool_fetch_skill <- function(skill_name, `_intent`) {} +btw_tool_skill <- function(name, `_intent`) {} -btw_tool_fetch_skill_impl <- function(skill_name) { - check_string(skill_name) +btw_tool_skill_impl <- function(name) { + check_string(name) - skill_info <- find_skill(skill_name) + skill_info <- find_skill(name) if (is.null(skill_info)) { available <- btw_list_skills() skill_names <- vapply(available, function(x) x$name, character(1)) cli::cli_abort( c( - "Skill {.val {skill_name}} not found.", + "Skill {.val {name}} not found.", "i" = "Available skills: {.val {skill_names}}" ) ) @@ -40,7 +40,7 @@ btw_tool_fetch_skill_impl <- function(skill_name) { if (!skill_info$validation$valid) { cli::cli_abort( c( - "Skill {.val {skill_name}} exists but has validation errors:", + "Skill {.val {name}} exists but has validation errors:", set_names( skill_info$validation$errors, rep("!", length(skill_info$validation$errors)) @@ -60,41 +60,45 @@ btw_tool_fetch_skill_impl <- function(skill_name) { btw_tool_result( value = full_content, data = list( - name = skill_name, + name = name, path = skill_info$path, base_dir = skill_info$base_dir, metadata = fm$data, resources = resources ), display = list( - title = sprintf("Skill: %s", skill_name), + title = sprintf("Skill: %s", name), markdown = full_content ) ) } .btw_add_to_tools( - name = "btw_tool_fetch_skill", + name = "btw_tool_skill", group = "skills", + tool = function() { ellmer::tool( - btw_tool_fetch_skill_impl, - name = "btw_tool_fetch_skill", + btw_tool_skill_impl, + name = "btw_tool_skill", description = paste( - "Fetch a skill's instructions and list its bundled resources.", - "Skills provide specialized guidance for specific tasks.", - "After fetching, use file read tools to access references,", - "or bash/code tools to run scripts." + "Load a skill's specialized instructions and list its bundled", + "resources. When you recognize that a task matches one of the", + "available skills, use this tool to load the full skill", + "instructions. If the user references a skill with /{name}", + "syntax, use this tool to load that skill. After loading, use", + "file read tools to access bundled references, or adapt bundled", + "scripts into R code." ), annotations = ellmer::tool_annotations( - title = "Fetch Skill", + title = "Load Skill", read_only_hint = TRUE, open_world_hint = FALSE, btw_can_register = function() any_skills_exist() ), arguments = list( - skill_name = ellmer::type_string( - "The name of the skill to fetch" + name = ellmer::type_string( + "The name of the skill to load" ) ) ) diff --git a/inst/prompts/skills.md b/inst/prompts/skills.md index 190d807a..e3ce8ce5 100644 --- a/inst/prompts/skills.md +++ b/inst/prompts/skills.md @@ -5,8 +5,9 @@ You have access to specialized skills that provide detailed guidance for specifi ### Using Skills 1. **Check available skills**: Review the `` listing below -2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description -3. **Access resources**: After fetching, use file read tools to access references +2. **Load when relevant**: When you recognize that a task matches a skill's description, call `btw_tool_skill(name)` to load the full skill instructions +3. **Don't reload**: If a skill has already been loaded in this conversation, follow its instructions directly without loading it again +4. **Access resources**: After loading, use file read tools to access references Skills may include bundled resources: - **Scripts**: Code bundled with the skill. Scripts are not directly executable by btw; read them for reference or adapt their logic into R code for use with the R code execution tool. diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd index 3a8c3cb2..45e5a136 100644 --- a/man/btw_skill_create.Rd +++ b/man/btw_skill_create.Rd @@ -44,6 +44,6 @@ Other skills: \code{\link{btw_skill_install_github}()}, \code{\link{btw_skill_install_package}()}, \code{\link{btw_skill_validate}()}, -\code{\link{btw_tool_fetch_skill}()} +\code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index 42e1106c..038d4732 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -51,6 +51,6 @@ Other skills: \code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_package}()}, \code{\link{btw_skill_validate}()}, -\code{\link{btw_tool_fetch_skill}()} +\code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index 16c3a7ae..b9f0dbf9 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -49,6 +49,6 @@ Other skills: \code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_github}()}, \code{\link{btw_skill_validate}()}, -\code{\link{btw_tool_fetch_skill}()} +\code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_skill_validate.Rd b/man/btw_skill_validate.Rd index 6d328ca1..5404a139 100644 --- a/man/btw_skill_validate.Rd +++ b/man/btw_skill_validate.Rd @@ -24,6 +24,6 @@ Other skills: \code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_github}()}, \code{\link{btw_skill_install_package}()}, -\code{\link{btw_tool_fetch_skill}()} +\code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_tool_fetch_skill.Rd b/man/btw_tool_skill.Rd similarity index 78% rename from man/btw_tool_fetch_skill.Rd rename to man/btw_tool_skill.Rd index 6fd5a19e..9771e313 100644 --- a/man/btw_tool_fetch_skill.Rd +++ b/man/btw_tool_skill.Rd @@ -1,13 +1,13 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/tool-skills.R -\name{btw_tool_fetch_skill} -\alias{btw_tool_fetch_skill} -\title{Tool: Fetch a skill} +\name{btw_tool_skill} +\alias{btw_tool_skill} +\title{Tool: Load a skill} \usage{ -btw_tool_fetch_skill(skill_name, `_intent` = "") +btw_tool_skill(name, `_intent` = "") } \arguments{ -\item{skill_name}{The name of the skill to fetch.} +\item{name}{The name of the skill to load.} \item{_intent}{An optional string describing the intent of the tool use. When the tool is used by an LLM, the model will use this argument to @@ -18,7 +18,7 @@ A \code{btw_tool_result} containing the skill instructions and a listing of bundled resources with their paths. } \description{ -Fetch a skill's instructions and list its bundled resources. +Load a skill's specialized instructions and list its bundled resources. Skills are modular capabilities that extend Claude's functionality with specialized knowledge, workflows, and tools. Each skill is a directory diff --git a/man/btw_tools.Rd b/man/btw_tools.Rd index f3e17972..221b890b 100644 --- a/man/btw_tools.Rd +++ b/man/btw_tools.Rd @@ -133,7 +133,7 @@ this function have access to the tools: \subsection{Group: skills}{\tabular{ll}{ Name \tab Description \cr - \code{\link[=btw_tool_fetch_skill]{btw_tool_fetch_skill()}} \tab Fetch a skill's instructions and list its bundled resources. \cr + \code{\link[=btw_tool_skill]{btw_tool_skill()}} \tab Load a skill's specialized instructions and list its bundled resources. \cr } } diff --git a/tests/testthat/_snaps/tool_skills.md b/tests/testthat/_snaps/tool_skills.md index acde286c..a3c47034 100644 --- a/tests/testthat/_snaps/tool_skills.md +++ b/tests/testthat/_snaps/tool_skills.md @@ -10,8 +10,9 @@ ### Using Skills 1. **Check available skills**: Review the `` listing below - 2. **Fetch when relevant**: Call `btw_tool_fetch_skill(skill_name)` when a task matches a skill's description - 3. **Access resources**: After fetching, use file read tools to access references + 2. **Load when relevant**: When you recognize that a task matches a skill's description, call `btw_tool_skill(name)` to load the full skill instructions + 3. **Don't reload**: If a skill has already been loaded in this conversation, follow its instructions directly without loading it again + 4. **Access resources**: After loading, use file read tools to access references Skills may include bundled resources: - **Scripts**: Code bundled with the skill. Scripts are not directly executable by btw; read them for reference or adapt their logic into R code for use with the R code execution tool. diff --git a/tests/testthat/test-btw_client.R b/tests/testthat/test-btw_client.R index 6800bd16..bfbd62cc 100644 --- a/tests/testthat/test-btw_client.R +++ b/tests/testthat/test-btw_client.R @@ -1085,7 +1085,7 @@ describe("btw_client() with multiple clients", { test_that("warn_skills_without_read_file() warns when skills present without read file", { tools <- list( - btw_tool_fetch_skill = "placeholder", + btw_tool_skill = "placeholder", btw_tool_docs_help_page = "placeholder" ) expect_warning( @@ -1096,7 +1096,7 @@ test_that("warn_skills_without_read_file() warns when skills present without rea test_that("warn_skills_without_read_file() is silent when both skills and read file present", { tools <- list( - btw_tool_fetch_skill = "placeholder", + btw_tool_skill = "placeholder", btw_tool_files_read = "placeholder" ) expect_no_warning(warn_skills_without_read_file(tools)) diff --git a/tests/testthat/test-mcp.R b/tests/testthat/test-mcp.R index cbaa80e4..2b14b185 100644 --- a/tests/testthat/test-mcp.R +++ b/tests/testthat/test-mcp.R @@ -16,5 +16,5 @@ test_that("btw_mcp_tools() excludes skills group by default", { local_enable_tools() tools <- btw_mcp_tools() tool_names <- vapply(tools, function(t) t@name, character(1)) - expect_false("btw_tool_fetch_skill" %in% tool_names) + expect_false("btw_tool_skill" %in% tool_names) }) diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index 29c211ae..aebda906 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -358,9 +358,9 @@ test_that("format_resources_listing() returns empty string for no resources", { expect_equal(format_resources_listing(resources, "/tmp"), "") }) -# Fetch Skill Tool --------------------------------------------------------- +# Skill Tool ---------------------------------------------------------------- -test_that("btw_tool_fetch_skill_impl() returns content and resources", { +test_that("btw_tool_skill_impl() returns content and resources", { dir <- withr::local_tempdir() skill_dir <- create_temp_skill(name = "fetch-test", dir = dir) dir.create(file.path(skill_dir, "references")) @@ -368,7 +368,7 @@ test_that("btw_tool_fetch_skill_impl() returns content and resources", { local_skill_dirs(dir) - result <- btw_tool_fetch_skill_impl("fetch-test") + result <- btw_tool_skill_impl("fetch-test") expect_s3_class(result, "ellmer::ContentToolResult") expect_match(result@value, "Test Skill") expect_match(result@value, "References:") @@ -376,10 +376,10 @@ test_that("btw_tool_fetch_skill_impl() returns content and resources", { expect_equal(result@extra$data$resources$references, "guide.md") }) -test_that("btw_tool_fetch_skill_impl() errors for missing skill", { +test_that("btw_tool_skill_impl() errors for missing skill", { dir <- withr::local_tempdir() local_skill_dirs(dir) - expect_error(btw_tool_fetch_skill_impl("nonexistent"), "not found") + expect_error(btw_tool_skill_impl("nonexistent"), "not found") }) # System Prompt ------------------------------------------------------------ From 55f45a450e21069a772b2c1e3515ea644547c0b4 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Fri, 20 Feb 2026 09:13:46 -0500 Subject: [PATCH 27/28] refactor(skills): remove btw_skill_create() and btw_skill_validate() These exported functions add maintenance surface area without proportional user value. Internal validation via validate_skill() remains intact for skill loading and installation. --- NAMESPACE | 2 - R/tool-skills.R | 176 ------------------------------ man/btw_skill_create.Rd | 49 --------- man/btw_skill_install_github.Rd | 2 - man/btw_skill_install_package.Rd | 2 - man/btw_skill_validate.Rd | 29 ----- man/btw_tool_skill.Rd | 4 +- tests/testthat/test-tool_skills.R | 117 -------------------- 8 files changed, 1 insertion(+), 380 deletions(-) delete mode 100644 man/btw_skill_create.Rd delete mode 100644 man/btw_skill_validate.Rd diff --git a/NAMESPACE b/NAMESPACE index 63f60d7f..2796e166 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,10 +26,8 @@ export(btw_app) export(btw_client) export(btw_mcp_server) export(btw_mcp_session) -export(btw_skill_create) export(btw_skill_install_github) export(btw_skill_install_package) -export(btw_skill_validate) export(btw_task_create_btw_md) export(btw_task_create_readme) export(btw_this) diff --git a/R/tool-skills.R b/R/tool-skills.R index 2c7f1ea7..9ad8b98b 100644 --- a/R/tool-skills.R +++ b/R/tool-skills.R @@ -307,30 +307,6 @@ extract_skill_metadata <- function(skill_path) { # Skill Validation --------------------------------------------------------- -validate_skill_name <- function(name, dir_name = NULL) { - issues <- character() - - if (is.null(name) || !is.character(name) || !nzchar(name)) { - issues <- c(issues, "Missing or empty 'name' field in frontmatter.") - return(issues) - } - - issues <- c(issues, validate_skill_name_format(name)) - - if (!is.null(dir_name) && name != dir_name) { - issues <- c( - issues, - sprintf( - "Name '%s' in frontmatter does not match directory name '%s'.", - name, - dir_name - ) - ) - } - - issues -} - validate_skill_name_format <- function(name) { issues <- character() @@ -627,158 +603,6 @@ resolve_skill_scope <- function(scope, error_call = caller_env()) { ) } -# User-Facing Skill Management --------------------------------------------- - -#' Create a new skill -#' -#' @description -#' Initialize a new skill directory following the -#' [Agent Skills specification](https://agentskills.io). Creates a `SKILL.md` -#' file with proper YAML frontmatter and optionally creates resource -#' directories (`scripts/`, `references/`, `assets/`). -#' -#' @param name The skill name. Must be a valid skill name: lowercase letters, -#' numbers, and hyphens only, 1-64 characters, must not start or end with a -#' hyphen, and must not contain consecutive hyphens. -#' @param description A description of what the skill does and when to use it. -#' Maximum 1024 characters. -#' @param scope Where to create the skill. One of: -#' - `"project"` (default): Creates in a project-level skills directory, -#' chosen from `.btw/skills/` or `.agents/skills/` -#' in that order. If one already exists, it is used; otherwise -#' `.btw/skills/` is created. -#' - `"user"`: Creates in the user-level skills directory -#' (`tools::R_user_dir("btw", "config")/skills`). -#' - A directory path: Creates the skill inside this path, e.g. -#' `scope = ".openhands/skills"`. Use `I("project")` or `I("user")` -#' if you need a literal directory with those names. -#' @param resources Logical. If `TRUE` (the default), creates empty -#' `scripts/`, `references/`, and `assets/` subdirectories. -#' -#' @return The path to the created skill directory, invisibly. -#' -#' @family skills -#' @export -btw_skill_create <- function( - name, - description = "", - scope = "project", - resources = TRUE -) { - check_name(name) - check_string(description) - check_string(scope) - check_bool(resources) - - if (nchar(description) > 1024) { - cli::cli_warn( - "Skill description is long ({nchar(description)} characters, recommended max 1024)." - ) - } - - # Validate name format - name_issues <- validate_skill_name(name) - if (length(name_issues) > 0) { - cli::cli_abort(c( - "Invalid skill name {.val {name}}:", - set_names(name_issues, rep("!", length(name_issues))) - )) - } - - # Resolve target directory - parent_dir <- resolve_skill_scope(scope) - - skill_dir <- file.path(parent_dir, name) - - if (dir.exists(skill_dir)) { - cli::cli_abort("Skill directory already exists: {.path {skill_dir}}") - } - - dir.create(skill_dir, recursive = TRUE, showWarnings = FALSE) - - # Generate SKILL.md - skill_title <- gsub("-", " ", name) - skill_title <- paste0( - toupper(substring(skill_title, 1, 1)), - substring(skill_title, 2) - ) - - description_line <- if (nzchar(description)) { - description - } else { - "TODO: Describe what this skill does and when to use it." - } - - body <- paste0("\n# ", skill_title, "\n\nTODO: Add skill instructions here.\n") - frontmatter::write_front_matter( - list( - data = list(name = name, description = description_line), - body = body - ), - file.path(skill_dir, "SKILL.md") - ) - - if (resources) { - dir.create(file.path(skill_dir, "scripts"), showWarnings = FALSE) - dir.create(file.path(skill_dir, "references"), showWarnings = FALSE) - dir.create(file.path(skill_dir, "assets"), showWarnings = FALSE) - } - - cli::cli_inform(c( - "v" = "Created skill {.val {name}} at {.path {skill_dir}}", - "i" = "Edit {.file {file.path(skill_dir, 'SKILL.md')}} to add instructions." - )) - - invisible(skill_dir) -} - -#' Validate a skill -#' -#' @description -#' Validate a skill directory against the -#' [Agent Skills specification](https://agentskills.io). Checks that the -#' `SKILL.md` file exists, has valid YAML frontmatter, and that required -#' fields follow the specification's naming and format rules. -#' -#' @param path Path to a skill directory (containing a `SKILL.md` file). -#' -#' @return A list with `valid` (logical) and `issues` (character vector of -#' validation messages), invisibly. Issues are also printed to the console. -#' -#' @family skills -#' @export -btw_skill_validate <- function(path = ".") { - check_string(path) - - path <- normalizePath(path, mustWork = FALSE) - if (!dir.exists(path)) { - cli::cli_abort("Directory does not exist: {.path {path}}") - } - - result <- validate_skill(path) - - if (result$valid && length(result$warnings) == 0) { - cli::cli_inform(c("v" = "Skill at {.path {path}} is valid.")) - } else if (result$valid) { - cli::cli_inform(c( - "v" = "Skill at {.path {path}} is valid with warnings:", - set_names(result$warnings, rep("!", length(result$warnings))) - )) - } else { - cli::cli_inform(c( - "x" = "Skill at {.path {path}} has validation errors:", - set_names(result$errors, rep("!", length(result$errors))), - if (length(result$warnings) > 0) { - c( - set_names(result$warnings, rep("!", length(result$warnings))) - ) - } - )) - } - - invisible(result) -} - select_skill_dir <- function( skill_dirs, skill = NULL, diff --git a/man/btw_skill_create.Rd b/man/btw_skill_create.Rd deleted file mode 100644 index 45e5a136..00000000 --- a/man/btw_skill_create.Rd +++ /dev/null @@ -1,49 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tool-skills.R -\name{btw_skill_create} -\alias{btw_skill_create} -\title{Create a new skill} -\usage{ -btw_skill_create(name, description = "", scope = "project", resources = TRUE) -} -\arguments{ -\item{name}{The skill name. Must be a valid skill name: lowercase letters, -numbers, and hyphens only, 1-64 characters, must not start or end with a -hyphen, and must not contain consecutive hyphens.} - -\item{description}{A description of what the skill does and when to use it. -Maximum 1024 characters.} - -\item{scope}{Where to create the skill. One of: -\itemize{ -\item \code{"project"} (default): Creates in a project-level skills directory, -chosen from \verb{.btw/skills/} or \verb{.agents/skills/} -in that order. If one already exists, it is used; otherwise -\verb{.btw/skills/} is created. -\item \code{"user"}: Creates in the user-level skills directory -(\code{tools::R_user_dir("btw", "config")/skills}). -\item A directory path: Creates the skill inside this path, e.g. -\code{scope = ".openhands/skills"}. Use \code{I("project")} or \code{I("user")} -if you need a literal directory with those names. -}} - -\item{resources}{Logical. If \code{TRUE} (the default), creates empty -\verb{scripts/}, \verb{references/}, and \verb{assets/} subdirectories.} -} -\value{ -The path to the created skill directory, invisibly. -} -\description{ -Initialize a new skill directory following the -\href{https://agentskills.io}{Agent Skills specification}. Creates a \code{SKILL.md} -file with proper YAML frontmatter and optionally creates resource -directories (\verb{scripts/}, \verb{references/}, \verb{assets/}). -} -\seealso{ -Other skills: -\code{\link{btw_skill_install_github}()}, -\code{\link{btw_skill_install_package}()}, -\code{\link{btw_skill_validate}()}, -\code{\link{btw_tool_skill}()} -} -\concept{skills} diff --git a/man/btw_skill_install_github.Rd b/man/btw_skill_install_github.Rd index 038d4732..7b246b5f 100644 --- a/man/btw_skill_install_github.Rd +++ b/man/btw_skill_install_github.Rd @@ -48,9 +48,7 @@ should contain one or more skill directories, each with a \code{SKILL.md} file. } \seealso{ Other skills: -\code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_package}()}, -\code{\link{btw_skill_validate}()}, \code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_skill_install_package.Rd b/man/btw_skill_install_package.Rd index b9f0dbf9..f564d4b8 100644 --- a/man/btw_skill_install_package.Rd +++ b/man/btw_skill_install_package.Rd @@ -46,9 +46,7 @@ their \verb{inst/skills/} directory, where each subdirectory containing a } \seealso{ Other skills: -\code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_github}()}, -\code{\link{btw_skill_validate}()}, \code{\link{btw_tool_skill}()} } \concept{skills} diff --git a/man/btw_skill_validate.Rd b/man/btw_skill_validate.Rd deleted file mode 100644 index 5404a139..00000000 --- a/man/btw_skill_validate.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tool-skills.R -\name{btw_skill_validate} -\alias{btw_skill_validate} -\title{Validate a skill} -\usage{ -btw_skill_validate(path = ".") -} -\arguments{ -\item{path}{Path to a skill directory (containing a \code{SKILL.md} file).} -} -\value{ -A list with \code{valid} (logical) and \code{issues} (character vector of -validation messages), invisibly. Issues are also printed to the console. -} -\description{ -Validate a skill directory against the -\href{https://agentskills.io}{Agent Skills specification}. Checks that the -\code{SKILL.md} file exists, has valid YAML frontmatter, and that required -fields follow the specification's naming and format rules. -} -\seealso{ -Other skills: -\code{\link{btw_skill_create}()}, -\code{\link{btw_skill_install_github}()}, -\code{\link{btw_skill_install_package}()}, -\code{\link{btw_tool_skill}()} -} -\concept{skills} diff --git a/man/btw_tool_skill.Rd b/man/btw_tool_skill.Rd index 9771e313..fb4ff96b 100644 --- a/man/btw_tool_skill.Rd +++ b/man/btw_tool_skill.Rd @@ -27,9 +27,7 @@ resources (scripts, references, assets). } \seealso{ Other skills: -\code{\link{btw_skill_create}()}, \code{\link{btw_skill_install_github}()}, -\code{\link{btw_skill_install_package}()}, -\code{\link{btw_skill_validate}()} +\code{\link{btw_skill_install_package}()} } \concept{skills} diff --git a/tests/testthat/test-tool_skills.R b/tests/testthat/test-tool_skills.R index aebda906..b526b93f 100644 --- a/tests/testthat/test-tool_skills.R +++ b/tests/testthat/test-tool_skills.R @@ -406,105 +406,6 @@ test_that("btw_skills_system_prompt() includes skill metadata", { expect_match(prompt, "Needs R 4.2") }) -# btw_skill_create --------------------------------------------------------- - -test_that("btw_skill_create() creates valid skill directory", { - dir <- withr::local_tempdir() - expect_message( - path <- btw_skill_create( - name = "my-new-skill", - description = "A new skill.", - scope = dir, - resources = TRUE - ), - "Created skill" - ) - - expect_true(dir.exists(path)) - expect_true(file.exists(file.path(path, "SKILL.md"))) - expect_true(dir.exists(file.path(path, "scripts"))) - expect_true(dir.exists(file.path(path, "references"))) - expect_true(dir.exists(file.path(path, "assets"))) - - # Validate the created skill - result <- validate_skill(path) - expect_true(result$valid) -}) - -test_that("btw_skill_create() without resources omits directories", { - dir <- withr::local_tempdir() - expect_message( - path <- btw_skill_create( - name = "minimal-skill", - description = "Minimal.", - scope = dir, - resources = FALSE - ), - "Created skill" - ) - - expect_true(file.exists(file.path(path, "SKILL.md"))) - expect_false(dir.exists(file.path(path, "scripts"))) -}) - -test_that("btw_skill_create() errors for invalid name", { - dir <- withr::local_tempdir() - expect_error(btw_skill_create(name = "Bad-Name", scope = dir), "lowercase") - expect_error(btw_skill_create(name = "-bad", scope = dir), "lowercase|hyphen") - expect_error(btw_skill_create(name = "bad--name", scope = dir), "consecutive") -}) - -test_that("btw_skill_create() errors if skill already exists", { - dir <- withr::local_tempdir() - expect_message( - btw_skill_create(name = "existing", description = "First.", scope = dir), - "Created skill" - ) - expect_error( - btw_skill_create(name = "existing", description = "Second.", scope = dir), - "already exists" - ) -}) - -test_that("btw_skill_create() treats I('project') as literal directory name", { - dir <- withr::local_tempdir() - expect_message( - path <- btw_skill_create( - name = "literal-test", - description = "Literal scope test.", - scope = I(file.path(dir, "project")) - ), - "Created skill" - ) - - expect_true(dir.exists(path)) - expect_equal(normalizePath(dirname(path)), normalizePath(file.path(dir, "project"))) -}) - -# btw_skill_validate ------------------------------------------------------- - -test_that("btw_skill_validate() reports valid skill", { - skill_dir <- create_temp_skill() - expect_message(result <- btw_skill_validate(skill_dir), "valid") - expect_true(result$valid) -}) - -test_that("btw_skill_validate() reports issues", { - dir <- withr::local_tempdir() - skill_dir <- file.path(dir, "bad-skill") - dir.create(skill_dir) - writeLines( - "---\nname: bad-skill\n---\nBody.", - file.path(skill_dir, "SKILL.md") - ) - expect_message(result <- btw_skill_validate(skill_dir), "validation errors") - expect_false(result$valid) -}) - -test_that("btw_skill_validate() errors for nonexistent directory", { - expect_error(btw_skill_validate("/nonexistent/path"), "does not exist") -}) - # select_skill_dir ---------------------------------------------------------- test_that("select_skill_dir() returns single dir directly", { @@ -1030,24 +931,6 @@ test_that("find_skill() returns validation errors for invalid skill on disk", { expect_match(result$validation$errors, "description", all = FALSE) }) -# btw_skill_create() long description warning ------------------------------- - -test_that("btw_skill_create() warns on long description", { - dir <- withr::local_tempdir() - long_desc <- paste(rep("a", 1025), collapse = "") - expect_warning( - expect_message( - btw_skill_create( - name = "long-desc", - description = long_desc, - scope = dir - ), - "Created skill" - ), - "long" - ) -}) - # install_skill_from_dir() overwrite ---------------------------------------- test_that("install_skill_from_dir() overwrites with overwrite = TRUE", { From 000c72f990adcd7d49e4eef4ab1c61401f5ae606 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Fri, 20 Feb 2026 09:47:18 -0500 Subject: [PATCH 28/28] feat(skills): add btw_task_create_skill() interactive task Exposes the bundled skill-creator skill as an interactive task function, following the same pattern as btw_task_create_readme() and btw_task_create_btw_md(). Supports app, console, client, and tool modes with optional skill name parameter. --- DESCRIPTION | 1 + NAMESPACE | 1 + R/task_create_skill.R | 188 ++++++++++++++++++++++++++++++++++ man/btw_task_create_btw_md.Rd | 3 +- man/btw_task_create_readme.Rd | 3 +- man/btw_task_create_skill.Rd | 71 +++++++++++++ 6 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 R/task_create_skill.R create mode 100644 man/btw_task_create_skill.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 11655b3f..2febbc9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -98,6 +98,7 @@ Collate: 'mcp.R' 'task_create_btw_md.R' 'task_create_readme.R' + 'task_create_skill.R' 'tool-result.R' 'tool-agent-subagent.R' 'tool-agent-custom.R' diff --git a/NAMESPACE b/NAMESPACE index 2796e166..4624692a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,6 +30,7 @@ export(btw_skill_install_github) export(btw_skill_install_package) export(btw_task_create_btw_md) export(btw_task_create_readme) +export(btw_task_create_skill) export(btw_this) export(btw_tool_agent_subagent) export(btw_tool_cran_package) diff --git a/R/task_create_skill.R b/R/task_create_skill.R new file mode 100644 index 00000000..1dd13ee9 --- /dev/null +++ b/R/task_create_skill.R @@ -0,0 +1,188 @@ +#' Task: Create a Skill +#' +#' @description +#' Create a new skill for your project using interactive guidance. If launched +#' in app or console mode, this task will start an interactive chat session to +#' guide you through the process of creating a skill that extends Claude's +#' capabilities with specialized knowledge, workflows, or tool integrations. +#' +#' @examples +#' withr::with_envvar(list(ANTHROPIC_API_KEY = "example"), { +#' btw_task_create_skill(mode = "tool", client = "anthropic") +#' }) +#' +#' @param name Optional skill name. If provided, the AI will skip the naming +#' step and use this name directly. +#' @param tools Optional list or character vector of tools to allow the task to +#' use when creating the skill. By default documentation tools are included to +#' allow the task to help create package-based skills. You can include +#' additional tools as needed. +#' +#' Because the task requires file tools to create skills with resources, tools +#' for listing, reading and writing files are always included. +#' @inheritParams btw_task_create_readme +#' @inheritParams btw_client +#' +#' @return When `mode` is `"app"` or `"console"`, this function launches an +#' interactive session in the browser or the R console, respectively. The +#' ellmer chat object with the conversation history is returned invisibly +#' when the session ends. +#' +#' When `mode` is `"client"`, this function returns the configured ellmer +#' chat client object. When `mode` is `"tool"`, this function returns an +#' ellmer tool object that can be used in other chat instances. +#' +#' @family task and agent functions +#' @export +btw_task_create_skill <- function( + ..., + name = NULL, + client = NULL, + mode = c("app", "console", "client", "tool"), + tools = "docs" +) { + mode <- arg_match(mode) + + client <- btw_client( + client = client, + tools = list2( + !!!tools, + "btw_tool_files_list", + "btw_tool_files_read", + "btw_tool_files_write", + "btw_tool_files_replace" + ) + ) + + skill_path <- fs::path_package("btw", "skills", "skill-creator") + skill_info <- validate_skill(skill_path) + + fm <- frontmatter::read_front_matter(fs::path(skill_path, "SKILL.md")) + skill_text <- fm$body %||% "" + + resources <- list_skill_resources(skill_path) + resources_listing <- format_resources_listing(resources, skill_path) + + sys_prompt <- paste0(skill_text, resources_listing) + + dots <- dots_list(...) + if (length(dots) > 0) { + user_context <- btw(..., clipboard = FALSE) + if (nzchar(user_context)) { + sys_prompt <- paste0( + sys_prompt, + "\n\n---\n\n", + "# Additional context provided by the user\n\n", + user_context + ) + } + } + + extra_instructions <- r"---( +## Additional Instructions + +If you don't have access to a bash tool, do not attempt to run the bundled Python scripts. Instead, read the script contents and accomplish the same tasks using the file tools available to you. + +Do not attempt to package the skill (step 5). The user will handle distribution. + +You can only create skills within the current project directory (e.g. `.btw/skills/` or `.agents/skills/`). If the user wants a user-level skill, create it in the project first and advise them to move it to their user skills directory (e.g. `~/.config/btw/skills/` or similar). +)---" + + if (!is.null(name)) { + check_string(name) + extra_instructions <- c( + extra_instructions, + "", + sprintf( + "The user has already chosen the skill name: %s. Skip the naming step.", + name + ) + ) + } + + sys_prompt <- paste0( + sys_prompt, + "\n\n---\n\n", + paste(extra_instructions, collapse = "\n") + ) + + client$set_system_prompt(sys_prompt) + + if (mode == "client") { + return(client) + } + + if (mode == "tool") { + btw_task_create_skill_tool <- function(prompt = "", name = NULL) { + this_client <- client$clone() + + tool_prompt <- this_client$get_system_prompt() + + if (!is.null(name)) { + tool_prompt <- paste0( + tool_prompt, + "\n\n", + sprintf( + "The user has already chosen the skill name: %s. Skip the naming step.", + name + ) + ) + } + + tool_prompt <- paste0( + tool_prompt, + "\n\n---\n\n", + "YOU ARE NOW OPERATING IN TOOL MODE. ", + "The user cannot respond directly to you. ", + "Because you cannot talk to the user, you will need to make your own decisions using the information available to you and the best of your abilities. ", + "You may compensate by doing additional file exploration as needed." + ) + + this_client$set_system_prompt(tool_prompt) + this_client$chat(prompt) + } + + tool <- ellmer::tool( + function(prompt, name = NULL) btw_task_create_skill_tool(prompt, name), + name = "btw_task_create_skill", + description = "Create a new skill for your project with interactive guidance.", + arguments = list( + prompt = ellmer::type_string( + "Additional instructions to the AI. Leave empty to proceed automatically.", + required = FALSE + ), + name = ellmer::type_string( + "Optional skill name. If provided, the AI will skip the naming step.", + required = FALSE + ) + ), + annotations = ellmer::tool_annotations( + title = "Create Skill", + icon = tool_icon("post-add") + ) + ) + return(tool) + } + + if (mode == "console") { + cli::cli_text( + "Starting {.strong btw_task_create_skill()} in live console mode." + ) + cli::cli_text( + "{cli::col_yellow(cli::symbol$play)} ", + "Say {.strong {cli::col_magenta(\"Let's get started.\")}} to begin." + ) + ellmer::live_console(client) + } else { + btw_app_from_client( + client = client, + messages = list(list( + role = "assistant", + content = paste( + "\U1F44B Hi! I'm ready to help you create a new skill for your project.", + "Say Let's get started. to begin." + ) + )) + ) + } +} diff --git a/man/btw_task_create_btw_md.Rd b/man/btw_task_create_btw_md.Rd index 3b13bae2..83703673 100644 --- a/man/btw_task_create_btw_md.Rd +++ b/man/btw_task_create_btw_md.Rd @@ -59,6 +59,7 @@ withr::with_envvar(list(ANTHROPIC_API_KEY = "example"), { } \seealso{ Other task and agent functions: -\code{\link{btw_task_create_readme}()} +\code{\link{btw_task_create_readme}()}, +\code{\link{btw_task_create_skill}()} } \concept{task and agent functions} diff --git a/man/btw_task_create_readme.Rd b/man/btw_task_create_readme.Rd index 37d3eada..314e4c64 100644 --- a/man/btw_task_create_readme.Rd +++ b/man/btw_task_create_readme.Rd @@ -58,6 +58,7 @@ withr::with_envvar(list(ANTHROPIC_API_KEY = "example"), { } \seealso{ Other task and agent functions: -\code{\link{btw_task_create_btw_md}()} +\code{\link{btw_task_create_btw_md}()}, +\code{\link{btw_task_create_skill}()} } \concept{task and agent functions} diff --git a/man/btw_task_create_skill.Rd b/man/btw_task_create_skill.Rd new file mode 100644 index 00000000..d0e3b07d --- /dev/null +++ b/man/btw_task_create_skill.Rd @@ -0,0 +1,71 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/task_create_skill.R +\name{btw_task_create_skill} +\alias{btw_task_create_skill} +\title{Task: Create a Skill} +\usage{ +btw_task_create_skill( + ..., + name = NULL, + client = NULL, + mode = c("app", "console", "client", "tool"), + tools = "docs" +) +} +\arguments{ +\item{...}{Additional context to provide to the AI. This can be any text or +R objects that can be converted to text using \code{\link[=btw]{btw()}}.} + +\item{name}{Optional skill name. If provided, the AI will skip the naming +step and use this name directly.} + +\item{client}{An \link[ellmer:Chat]{ellmer::Chat} client, or a \code{provider/model} string to be +passed to \code{\link[ellmer:chat-any]{ellmer::chat()}} to create a chat client, or an alias to a client +setting in your \code{btw.md} file (see "Multiple Providers" section). Defaults +to \code{\link[ellmer:chat_anthropic]{ellmer::chat_anthropic()}}. You can use the \code{btw.client} option to set a +default client for new \code{btw_client()} calls, or use a \code{btw.md} project file +for default chat client settings, like provider and model. We check the +\code{client} argument, then the \code{btw.client} R option, and finally the \code{btw.md} +project file (falling back to user-level \code{btw.md} if needed), using only +the client definition from the first of these that is available.} + +\item{mode}{The mode to run the task in, which affects what is returned from +this function. \code{"app"} and \code{"console"} modes launch interactive sessions, +while \code{"client"} and \code{"tool"} modes return objects for programmatic use.} + +\item{tools}{Optional list or character vector of tools to allow the task to +use when creating the skill. By default documentation tools are included to +allow the task to help create package-based skills. You can include +additional tools as needed. + +Because the task requires file tools to create skills with resources, tools +for listing, reading and writing files are always included.} +} +\value{ +When \code{mode} is \code{"app"} or \code{"console"}, this function launches an +interactive session in the browser or the R console, respectively. The +ellmer chat object with the conversation history is returned invisibly +when the session ends. + +When \code{mode} is \code{"client"}, this function returns the configured ellmer +chat client object. When \code{mode} is \code{"tool"}, this function returns an +ellmer tool object that can be used in other chat instances. +} +\description{ +Create a new skill for your project using interactive guidance. If launched +in app or console mode, this task will start an interactive chat session to +guide you through the process of creating a skill that extends Claude's +capabilities with specialized knowledge, workflows, or tool integrations. +} +\examples{ +withr::with_envvar(list(ANTHROPIC_API_KEY = "example"), { + btw_task_create_skill(mode = "tool", client = "anthropic") +}) + +} +\seealso{ +Other task and agent functions: +\code{\link{btw_task_create_btw_md}()}, +\code{\link{btw_task_create_readme}()} +} +\concept{task and agent functions}