* Add Find Skills and Install Skills * Improvements * fix file name * Update pkg/skills/clawhub_registry.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix * Comments addressed * Resolve comments * fix tests * fixes * Comments resolved * Update pkg/skills/search_cache_repro_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * minor fix * fix test * fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
305 lines
8.5 KiB
Go
305 lines
8.5 KiB
Go
// PicoClaw - Ultra-lightweight personal AI agent
|
|
// License: MIT
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"github.com/sipeed/picoclaw/pkg/skills"
|
|
"github.com/sipeed/picoclaw/pkg/utils"
|
|
)
|
|
|
|
func skillsHelp() {
|
|
fmt.Println("\nSkills commands:")
|
|
fmt.Println(" list List installed skills")
|
|
fmt.Println(" install <repo> Install skill from GitHub")
|
|
fmt.Println(" install-builtin Install all builtin skills to workspace")
|
|
fmt.Println(" list-builtin List available builtin skills")
|
|
fmt.Println(" remove <name> Remove installed skill")
|
|
fmt.Println(" search Search available skills")
|
|
fmt.Println(" show <name> Show skill details")
|
|
fmt.Println()
|
|
fmt.Println("Examples:")
|
|
fmt.Println(" picoclaw skills list")
|
|
fmt.Println(" picoclaw skills install sipeed/picoclaw-skills/weather")
|
|
fmt.Println(" picoclaw skills install-builtin")
|
|
fmt.Println(" picoclaw skills list-builtin")
|
|
fmt.Println(" picoclaw skills remove weather")
|
|
fmt.Println(" picoclaw skills install --registry clawhub github")
|
|
}
|
|
|
|
func skillsListCmd(loader *skills.SkillsLoader) {
|
|
allSkills := loader.ListSkills()
|
|
|
|
if len(allSkills) == 0 {
|
|
fmt.Println("No skills installed.")
|
|
return
|
|
}
|
|
|
|
fmt.Println("\nInstalled Skills:")
|
|
fmt.Println("------------------")
|
|
for _, skill := range allSkills {
|
|
fmt.Printf(" ✓ %s (%s)\n", skill.Name, skill.Source)
|
|
if skill.Description != "" {
|
|
fmt.Printf(" %s\n", skill.Description)
|
|
}
|
|
}
|
|
}
|
|
|
|
func skillsInstallCmd(installer *skills.SkillInstaller, cfg *config.Config) {
|
|
if len(os.Args) < 4 {
|
|
fmt.Println("Usage: picoclaw skills install <github-repo>")
|
|
fmt.Println(" picoclaw skills install --registry <name> <slug>")
|
|
return
|
|
}
|
|
|
|
// Check for --registry flag.
|
|
if os.Args[3] == "--registry" {
|
|
if len(os.Args) < 6 {
|
|
fmt.Println("Usage: picoclaw skills install --registry <name> <slug>")
|
|
fmt.Println("Example: picoclaw skills install --registry clawhub github")
|
|
return
|
|
}
|
|
registryName := os.Args[4]
|
|
slug := os.Args[5]
|
|
skillsInstallFromRegistry(cfg, registryName, slug)
|
|
return
|
|
}
|
|
|
|
// Default: install from GitHub (backward compatible).
|
|
repo := os.Args[3]
|
|
fmt.Printf("Installing skill from %s...\n", repo)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := installer.InstallFromGitHub(ctx, repo); err != nil {
|
|
fmt.Printf("\u2717 Failed to install skill: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("\u2713 Skill '%s' installed successfully!\n", filepath.Base(repo))
|
|
}
|
|
|
|
// skillsInstallFromRegistry installs a skill from a named registry (e.g. clawhub).
|
|
func skillsInstallFromRegistry(cfg *config.Config, registryName, slug string) {
|
|
err := utils.ValidateSkillIdentifier(registryName)
|
|
if err != nil {
|
|
fmt.Printf("\u2717 Invalid registry name: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = utils.ValidateSkillIdentifier(slug)
|
|
if err != nil {
|
|
fmt.Printf("\u2717 Invalid slug: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Installing skill '%s' from %s registry...\n", slug, registryName)
|
|
|
|
registryMgr := skills.NewRegistryManagerFromConfig(skills.RegistryConfig{
|
|
MaxConcurrentSearches: cfg.Tools.Skills.MaxConcurrentSearches,
|
|
ClawHub: skills.ClawHubConfig(cfg.Tools.Skills.Registries.ClawHub),
|
|
})
|
|
|
|
registry := registryMgr.GetRegistry(registryName)
|
|
if registry == nil {
|
|
fmt.Printf("\u2717 Registry '%s' not found or not enabled. Check your config.json.\n", registryName)
|
|
os.Exit(1)
|
|
}
|
|
|
|
workspace := cfg.WorkspacePath()
|
|
targetDir := filepath.Join(workspace, "skills", slug)
|
|
|
|
if _, err := os.Stat(targetDir); err == nil {
|
|
fmt.Printf("\u2717 Skill '%s' already installed at %s\n", slug, targetDir)
|
|
os.Exit(1)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
if err := os.MkdirAll(filepath.Join(workspace, "skills"), 0755); err != nil {
|
|
fmt.Printf("\u2717 Failed to create skills directory: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
result, err := registry.DownloadAndInstall(ctx, slug, "", targetDir)
|
|
if err != nil {
|
|
rmErr := os.RemoveAll(targetDir)
|
|
if rmErr != nil {
|
|
fmt.Printf("\u2717 Failed to remove partial install: %v\n", rmErr)
|
|
}
|
|
fmt.Printf("\u2717 Failed to install skill: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if result.IsMalwareBlocked {
|
|
rmErr := os.RemoveAll(targetDir)
|
|
if rmErr != nil {
|
|
fmt.Printf("\u2717 Failed to remove partial install: %v\n", rmErr)
|
|
}
|
|
fmt.Printf("\u2717 Skill '%s' is flagged as malicious and cannot be installed.\n", slug)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if result.IsSuspicious {
|
|
fmt.Printf("\u26a0\ufe0f Warning: skill '%s' is flagged as suspicious.\n", slug)
|
|
}
|
|
|
|
fmt.Printf("\u2713 Skill '%s' v%s installed successfully!\n", slug, result.Version)
|
|
if result.Summary != "" {
|
|
fmt.Printf(" %s\n", result.Summary)
|
|
}
|
|
}
|
|
|
|
func skillsRemoveCmd(installer *skills.SkillInstaller, skillName string) {
|
|
fmt.Printf("Removing skill '%s'...\n", skillName)
|
|
|
|
if err := installer.Uninstall(skillName); err != nil {
|
|
fmt.Printf("✗ Failed to remove skill: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("✓ Skill '%s' removed successfully!\n", skillName)
|
|
}
|
|
|
|
func skillsInstallBuiltinCmd(workspace string) {
|
|
builtinSkillsDir := "./picoclaw/skills"
|
|
workspaceSkillsDir := filepath.Join(workspace, "skills")
|
|
|
|
fmt.Printf("Copying builtin skills to workspace...\n")
|
|
|
|
skillsToInstall := []string{
|
|
"weather",
|
|
"news",
|
|
"stock",
|
|
"calculator",
|
|
}
|
|
|
|
for _, skillName := range skillsToInstall {
|
|
builtinPath := filepath.Join(builtinSkillsDir, skillName)
|
|
workspacePath := filepath.Join(workspaceSkillsDir, skillName)
|
|
|
|
if _, err := os.Stat(builtinPath); err != nil {
|
|
fmt.Printf("⊘ Builtin skill '%s' not found: %v\n", skillName, err)
|
|
continue
|
|
}
|
|
|
|
if err := os.MkdirAll(workspacePath, 0755); err != nil {
|
|
fmt.Printf("✗ Failed to create directory for %s: %v\n", skillName, err)
|
|
continue
|
|
}
|
|
|
|
if err := copyDirectory(builtinPath, workspacePath); err != nil {
|
|
fmt.Printf("✗ Failed to copy %s: %v\n", skillName, err)
|
|
}
|
|
}
|
|
|
|
fmt.Println("\n✓ All builtin skills installed!")
|
|
fmt.Println("Now you can use them in your workspace.")
|
|
}
|
|
|
|
func skillsListBuiltinCmd() {
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
fmt.Printf("Error loading config: %v\n", err)
|
|
return
|
|
}
|
|
builtinSkillsDir := filepath.Join(filepath.Dir(cfg.WorkspacePath()), "picoclaw", "skills")
|
|
|
|
fmt.Println("\nAvailable Builtin Skills:")
|
|
fmt.Println("-----------------------")
|
|
|
|
entries, err := os.ReadDir(builtinSkillsDir)
|
|
if err != nil {
|
|
fmt.Printf("Error reading builtin skills: %v\n", err)
|
|
return
|
|
}
|
|
|
|
if len(entries) == 0 {
|
|
fmt.Println("No builtin skills available.")
|
|
return
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
skillName := entry.Name()
|
|
skillFile := filepath.Join(builtinSkillsDir, skillName, "SKILL.md")
|
|
|
|
description := "No description"
|
|
if _, err := os.Stat(skillFile); err == nil {
|
|
data, err := os.ReadFile(skillFile)
|
|
if err == nil {
|
|
content := string(data)
|
|
if idx := strings.Index(content, "\n"); idx > 0 {
|
|
firstLine := content[:idx]
|
|
if strings.Contains(firstLine, "description:") {
|
|
descLine := strings.Index(content[idx:], "\n")
|
|
if descLine > 0 {
|
|
description = strings.TrimSpace(content[idx+descLine : idx+descLine])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
status := "✓"
|
|
fmt.Printf(" %s %s\n", status, entry.Name())
|
|
if description != "" {
|
|
fmt.Printf(" %s\n", description)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func skillsSearchCmd(installer *skills.SkillInstaller) {
|
|
fmt.Println("Searching for available skills...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
availableSkills, err := installer.ListAvailableSkills(ctx)
|
|
if err != nil {
|
|
fmt.Printf("✗ Failed to fetch skills list: %v\n", err)
|
|
return
|
|
}
|
|
|
|
if len(availableSkills) == 0 {
|
|
fmt.Println("No skills available.")
|
|
return
|
|
}
|
|
|
|
fmt.Printf("\nAvailable Skills (%d):\n", len(availableSkills))
|
|
fmt.Println("--------------------")
|
|
for _, skill := range availableSkills {
|
|
fmt.Printf(" 📦 %s\n", skill.Name)
|
|
fmt.Printf(" %s\n", skill.Description)
|
|
fmt.Printf(" Repo: %s\n", skill.Repository)
|
|
if skill.Author != "" {
|
|
fmt.Printf(" Author: %s\n", skill.Author)
|
|
}
|
|
if len(skill.Tags) > 0 {
|
|
fmt.Printf(" Tags: %v\n", skill.Tags)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func skillsShowCmd(loader *skills.SkillsLoader, skillName string) {
|
|
content, ok := loader.LoadSkill(skillName)
|
|
if !ok {
|
|
fmt.Printf("✗ Skill '%s' not found\n", skillName)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("\n📦 Skill: %s\n", skillName)
|
|
fmt.Println("----------------------")
|
|
fmt.Println(content)
|
|
}
|