mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
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.
247 lines
5.9 KiB
Go
247 lines
5.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/resource"
|
|
ssync "skillshare/internal/sync"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// matchesFolder reports whether relPath belongs to the given folder filter.
|
|
// folder="" → root-level only (dir == ".")
|
|
// folder="*" → all skills
|
|
// otherwise → dir == folder || dir has folder/ prefix
|
|
func matchesFolder(relPath, folder string) bool {
|
|
if folder == "*" {
|
|
return true
|
|
}
|
|
dir := filepath.ToSlash(filepath.Dir(relPath))
|
|
if folder == "" {
|
|
return dir == "."
|
|
}
|
|
return dir == folder || strings.HasPrefix(dir, folder+"/")
|
|
}
|
|
|
|
type batchSetTargetsRequest struct {
|
|
Folder string `json:"folder"`
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
type batchSetTargetsResponse struct {
|
|
Updated int `json:"updated"`
|
|
Skipped int `json:"skipped"`
|
|
Errors []string `json:"errors"`
|
|
}
|
|
|
|
// handleBatchSetTargets handles POST /api/skills/batch/targets.
|
|
// Sets or removes the target for all skills in a folder.
|
|
func (s *Server) handleBatchSetTargets(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
var req batchSetTargetsRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Reject path traversal in folder
|
|
if strings.Contains(req.Folder, "..") {
|
|
writeError(w, http.StatusBadRequest, "invalid folder: path traversal not allowed")
|
|
return
|
|
}
|
|
folder := filepath.ToSlash(filepath.Clean(req.Folder))
|
|
if req.Folder == "" {
|
|
folder = ""
|
|
}
|
|
|
|
// Snapshot config under read lock, then discover without holding the lock.
|
|
s.mu.RLock()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
s.mu.RUnlock()
|
|
|
|
discovered, err := ssync.DiscoverSourceSkillsAll(source)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
|
|
return
|
|
}
|
|
|
|
var updated, skipped int
|
|
var errors []string
|
|
|
|
// Collect skills that need meta hash refresh (outside the lock).
|
|
type updatedSkill struct {
|
|
name string
|
|
path string
|
|
}
|
|
var updatedSkills []updatedSkill
|
|
|
|
// Acquire write lock only for the file-write loop.
|
|
s.mu.Lock()
|
|
for _, d := range discovered {
|
|
relPath := filepath.ToSlash(d.RelPath)
|
|
if !matchesFolder(relPath, folder) {
|
|
continue
|
|
}
|
|
|
|
// Skip disabled skills and tracked-repo members
|
|
if d.Disabled || d.IsInRepo {
|
|
skipped++
|
|
continue
|
|
}
|
|
|
|
skillMDPath := filepath.Join(d.SourcePath, "SKILL.md")
|
|
var values []string
|
|
if req.Target != "" {
|
|
values = []string{req.Target}
|
|
}
|
|
|
|
if err := utils.SetFrontmatterList(skillMDPath, "metadata.targets", values); err != nil {
|
|
errors = append(errors, d.FlatName+": "+err.Error())
|
|
continue
|
|
}
|
|
|
|
updatedSkills = append(updatedSkills, updatedSkill{
|
|
name: filepath.Base(d.SourcePath),
|
|
path: d.SourcePath,
|
|
})
|
|
updated++
|
|
}
|
|
s.mu.Unlock()
|
|
|
|
// Recompute file hashes outside the lock so reads aren't blocked.
|
|
for _, sk := range updatedSkills {
|
|
s.skillsStore.RefreshHashes(sk.name, sk.path)
|
|
}
|
|
if len(updatedSkills) > 0 {
|
|
s.skillsStore.Save(s.cfg.EffectiveSkillsSource()) //nolint:errcheck
|
|
}
|
|
|
|
s.writeOpsLog("batch-set-targets", "ok", start, map[string]any{
|
|
"folder": req.Folder,
|
|
"target": req.Target,
|
|
"updated": updated,
|
|
"skipped": skipped,
|
|
"scope": "ui",
|
|
}, "")
|
|
|
|
if errors == nil {
|
|
errors = []string{}
|
|
}
|
|
writeJSON(w, batchSetTargetsResponse{
|
|
Updated: updated,
|
|
Skipped: skipped,
|
|
Errors: errors,
|
|
})
|
|
}
|
|
|
|
type setSkillTargetsRequest struct {
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
// handleSetSkillTargets handles PATCH /api/skills/{name}/targets.
|
|
// Sets or removes the target for a single skill.
|
|
func (s *Server) handleSetSkillTargets(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
name := r.PathValue("name")
|
|
|
|
var req setSkillTargetsRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
|
|
return
|
|
}
|
|
|
|
s.mu.RLock()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
s.mu.RUnlock()
|
|
|
|
discovered, err := ssync.DiscoverSourceSkillsAll(source)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
|
|
return
|
|
}
|
|
|
|
for _, d := range discovered {
|
|
baseName := filepath.Base(d.SourcePath)
|
|
if d.FlatName != name && baseName != name {
|
|
continue
|
|
}
|
|
|
|
if d.IsInRepo {
|
|
writeError(w, http.StatusBadRequest, "cannot set target on tracked-repo skill; manage targets on the repo instead")
|
|
return
|
|
}
|
|
|
|
skillMDPath := filepath.Join(d.SourcePath, "SKILL.md")
|
|
var values []string
|
|
if req.Target != "" {
|
|
values = []string{req.Target}
|
|
}
|
|
|
|
s.mu.Lock()
|
|
err := utils.SetFrontmatterList(skillMDPath, "metadata.targets", values)
|
|
s.mu.Unlock()
|
|
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to update skill: "+err.Error())
|
|
return
|
|
}
|
|
|
|
s.skillsStore.RefreshHashes(d.RelPath, d.SourcePath)
|
|
s.skillsStore.Save(s.cfg.EffectiveSkillsSource()) //nolint:errcheck
|
|
|
|
s.writeOpsLog("set-skill-targets", "ok", start, map[string]any{
|
|
"name": name,
|
|
"target": req.Target,
|
|
"scope": "ui",
|
|
}, "")
|
|
|
|
writeJSON(w, map[string]any{"success": true})
|
|
return
|
|
}
|
|
|
|
// Try agents if skill not found
|
|
s.mu.RLock()
|
|
agentsSource := s.agentsSource()
|
|
s.mu.RUnlock()
|
|
|
|
if agentsSource != "" {
|
|
agents, _ := resource.AgentKind{}.Discover(agentsSource)
|
|
for _, d := range agents {
|
|
if d.FlatName != name {
|
|
continue
|
|
}
|
|
|
|
var values []string
|
|
if req.Target != "" {
|
|
values = []string{req.Target}
|
|
}
|
|
|
|
s.mu.Lock()
|
|
err := utils.SetFrontmatterList(d.SourcePath, "targets", values)
|
|
s.mu.Unlock()
|
|
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to update agent: "+err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("set-agent-targets", "ok", start, map[string]any{
|
|
"name": name,
|
|
"target": req.Target,
|
|
"scope": "ui",
|
|
}, "")
|
|
|
|
writeJSON(w, map[string]any{"success": true})
|
|
return
|
|
}
|
|
}
|
|
|
|
writeError(w, http.StatusNotFound, "resource not found: "+name)
|
|
}
|