Files
skillshare/internal/server/handler_create_skill.go
T
Willie 996d00e342 feat(config): migrate consumers to Effective*Source helpers (Phase 3+5)
Make the v0.19.16 sources map actually do something: route all path
reads through Config.EffectiveSkillsSource() / EffectiveAgentsSource() /
EffectiveExtrasSource() so users who set sources.skills / sources.agents /
sources.extras get the configured paths everywhere.

Mechanical changes (~50 files):
- cmd/skillshare/**: replace cfg.Source / cfg.ExtrasSource reads with
  cfg.EffectiveSkillsSource() / cfg.EffectiveExtrasSource()
- internal/server/**: replace s.cfg.Source / s.cfg.ExtrasSource reads
  with the same helpers; server.skillsSource() / gitignoreDir() use the
  global-mode helper
- internal/config/config_validate.go: validate using EffectiveSkillsSource
  while still rejecting configs that set neither field explicitly
- internal/config/reconcile.go, registry.go: read via helpers

Behavioral fixes (paired with the migration):
- extras init / extras source / POST /api/extras no longer silently
  backfill the legacy extras_source field — the runtime fallback inside
  EffectiveExtrasSource() handles it. This stops surprise diffs in users'
  config.yaml after running extras commands.
- extras source command writes back to whichever field the user already
  configured (sources.extras if set, otherwise legacy extras_source) so
  the update is not shadowed.
- source_type in /api/extras and extras list correctly reports "default"
  vs "extras_source" by inspecting the raw (unresolved) configured value;
  sources.extras counts as configured for this purpose.

UI:
- ui/src/lib/fieldDocs.ts: neutralize sources / sources.* descriptions
  so they describe both project and global modes (Phase 5).

Preserved (intentional):
- Direct writes to cfg.Source / cfg.AgentsSource / cfg.ExtrasSource in
  init, migrate.go, cloneForSave, and server.reloadConfig — these are
  raw-field writes, not path reads.
- Updated test expectations for the removed backfills.
2026-05-21 14:49:11 +08:00

131 lines
3.5 KiB
Go

package server
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/skill"
)
func (s *Server) handleGetTemplates(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{
"patterns": skill.Patterns,
"categories": skill.Categories,
})
}
type createSkillRequest struct {
Name string `json:"name"`
Pattern string `json:"pattern"`
Category string `json:"category"`
ScaffoldDirs []string `json:"scaffoldDirs"`
}
func (s *Server) handleCreateSkill(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var req createSkillRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
// Validate name
if !skill.ValidNameRe.MatchString(req.Name) {
writeError(w, http.StatusBadRequest, "invalid skill name: use lowercase letters, numbers, hyphens, underscores; must start with letter or underscore")
return
}
// Validate pattern
pattern := skill.FindPattern(req.Pattern)
if pattern == nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown pattern: %s", req.Pattern))
return
}
// Validate scaffoldDirs against pattern's allowed dirs
if len(req.ScaffoldDirs) > 0 && len(pattern.ScaffoldDirs) > 0 {
allowed := make(map[string]bool, len(pattern.ScaffoldDirs))
for _, d := range pattern.ScaffoldDirs {
allowed[d] = true
}
for _, d := range req.ScaffoldDirs {
if !allowed[d] {
writeError(w, http.StatusBadRequest, fmt.Sprintf("scaffold dir %q not valid for pattern %q", d, req.Pattern))
return
}
}
}
s.mu.Lock()
defer s.mu.Unlock()
source := s.cfg.EffectiveSkillsSource()
skillDir := filepath.Join(source, req.Name)
// Check if skill already exists
if _, err := os.Stat(skillDir); err == nil {
writeError(w, http.StatusConflict, fmt.Sprintf("skill '%s' already exists", req.Name))
return
}
// Generate SKILL.md content
content := skill.GenerateContent(req.Name, req.Pattern, req.Category)
// Create directory
if err := os.MkdirAll(skillDir, 0755); err != nil {
writeError(w, http.StatusInternalServerError, "failed to create directory: "+err.Error())
return
}
createdFiles := []string{"SKILL.md"}
// Write SKILL.md
skillFile := filepath.Join(skillDir, "SKILL.md")
if err := os.WriteFile(skillFile, []byte(content), 0644); err != nil {
os.RemoveAll(skillDir)
writeError(w, http.StatusInternalServerError, "failed to write SKILL.md: "+err.Error())
return
}
// Create scaffold directories
for _, dir := range req.ScaffoldDirs {
dirPath := filepath.Join(skillDir, dir)
if err := os.MkdirAll(dirPath, 0755); err != nil {
os.RemoveAll(skillDir)
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to create %s: %s", dir, err.Error()))
return
}
gitkeep := filepath.Join(dirPath, ".gitkeep")
if err := os.WriteFile(gitkeep, []byte{}, 0644); err != nil {
os.RemoveAll(skillDir)
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to create %s/.gitkeep: %s", dir, err.Error()))
return
}
createdFiles = append(createdFiles, dir+"/.gitkeep")
}
// Ops log
s.writeOpsLog("create-skill", "ok", start, map[string]any{
"name": req.Name,
"pattern": req.Pattern,
"category": req.Category,
"scope": "ui",
}, "")
w.WriteHeader(http.StatusCreated)
writeJSON(w, map[string]any{
"skill": map[string]any{
"name": req.Name,
"flatName": req.Name,
"relPath": req.Name,
"sourcePath": skillDir,
},
"createdFiles": createdFiles,
})
}