Files
skillshare/internal/server/handler_toggle.go
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

127 lines
3.6 KiB
Go

package server
import (
"fmt"
"net/http"
"path/filepath"
"time"
"skillshare/internal/skillignore"
ssync "skillshare/internal/sync"
)
func (s *Server) handleDisableSkill(w http.ResponseWriter, r *http.Request) {
s.handleToggleSkill(w, r, false)
}
func (s *Server) handleEnableSkill(w http.ResponseWriter, r *http.Request) {
s.handleToggleSkill(w, r, true)
}
func (s *Server) handleToggleSkill(w http.ResponseWriter, r *http.Request, enable bool) {
start := time.Now()
name := r.PathValue("name")
kind := r.URL.Query().Get("kind")
if kind != "" && kind != "agent" && kind != "skill" {
writeError(w, http.StatusBadRequest, "invalid kind: "+kind)
return
}
// Resolve under RLock — discovery is I/O-heavy, don't hold write lock
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
s.mu.RUnlock()
relPath := ""
isDisabled := false
ignorePath := ""
if kind == "agent" {
if agentsSource == "" {
writeError(w, http.StatusNotFound, "agent not found: "+name)
return
}
var err error
relPath, isDisabled, err = s.resolveAgentRelPathWithStatus(agentsSource, name)
if err != nil {
writeError(w, http.StatusNotFound, err.Error())
return
}
ignorePath = filepath.Join(agentsSource, ".agentignore")
} else {
var err error
relPath, isDisabled, err = s.resolveSkillRelPathWithStatus(source, name)
if err != nil {
writeError(w, http.StatusNotFound, err.Error())
return
}
ignorePath = filepath.Join(source, ".skillignore")
}
// Write lock only for the file mutation
s.mu.Lock()
defer s.mu.Unlock()
action := "disable"
disabled := true
if enable {
action = "enable"
disabled = false
if !isDisabled {
writeJSON(w, map[string]any{"success": true, "name": name, "disabled": false, "message": "not disabled"})
return
}
removed, err := skillignore.RemovePattern(ignorePath, relPath)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update .skillignore: "+err.Error())
return
}
if !removed {
writeError(w, http.StatusConflict, "skill is disabled by a glob or directory pattern in .skillignore or .skillignore.local — edit the file manually to remove the matching rule")
return
}
} else {
added, err := skillignore.AddPattern(ignorePath, relPath)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update .skillignore: "+err.Error())
return
}
if !added {
writeJSON(w, map[string]any{"success": true, "name": name, "disabled": true, "message": "already disabled"})
return
}
}
s.writeOpsLog(action, "ok", start, map[string]any{
"name": name,
"kind": kind,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true, "name": name, "disabled": disabled})
}
// resolveSkillRelPath finds a skill's relPath by flatName or baseName.
// Searches all skills including disabled ones. Caller must provide source path.
func (s *Server) resolveSkillRelPath(source, name string) (string, error) {
relPath, _, err := s.resolveSkillRelPathWithStatus(source, name)
return relPath, err
}
// resolveSkillRelPathWithStatus is like resolveSkillRelPath but also returns
// whether the skill is currently disabled (matched by .skillignore).
func (s *Server) resolveSkillRelPathWithStatus(source, name string) (string, bool, error) {
discovered, err := ssync.DiscoverSourceSkillsAll(source)
if err != nil {
return "", false, fmt.Errorf("failed to discover skills: %w", err)
}
for _, d := range discovered {
baseName := filepath.Base(d.SourcePath)
if d.FlatName == name || baseName == name || d.RelPath == name {
return d.RelPath, d.Disabled, nil
}
}
return "", false, fmt.Errorf("skill not found: %s", name)
}