Files
knox/internal/skillsrepo/write.go
T
david edf6c03a7c chore: checkpoint uncommitted work before session store fix
Pre-existing work-in-progress in the tree: karakeep, snippets,
skills-health and redaction ingesters, the opencode.db session sweep,
gossip and metrics changes. Checkpointed as-is so the adapter fix in the
next commit stays reviewable on its own.
2026-11-01 13:33:36 -08:00

55 lines
1.6 KiB
Go

package skillsrepo
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// RepoDir returns the skills repo root (~/.skills). It is the same location the
// skills MCP owns; knox writes here only when a snippet is explicitly encoded
// as a skill.
func RepoDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".skills")
}
var slugRe = regexp.MustCompile(`[^a-z0-9]+`)
// Slugify turns a title into a filesystem-safe skill directory name.
func Slugify(s string) string {
s = strings.ToLower(strings.TrimSpace(s))
s = slugRe.ReplaceAllString(s, "-")
s = strings.Trim(s, "-")
if s == "" {
return "snippet"
}
if len(s) > 60 {
s = strings.Trim(s[:60], "-")
}
return s
}
// WriteSkill writes content to <repo>/<slug>/SKILL.md. It refuses to clobber an
// existing skill unless overwrite is true, and returns the written path. It does
// not commit: the skills MCP owns versioning, so an externally written skill
// simply shows up as a working-tree change for it (or the user) to commit.
func WriteSkill(name, content string, overwrite bool) (string, error) {
slug := Slugify(name)
path := filepath.Join(RepoDir(), slug, "SKILL.md")
if !overwrite {
if _, err := os.Stat(path); err == nil {
return "", fmt.Errorf("skill %q already exists at %s (pass overwrite to replace)", slug, path)
}
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return "", fmt.Errorf("mkdir skill dir: %w", err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
return "", fmt.Errorf("write skill: %w", err)
}
return path, nil
}