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

120 lines
3.4 KiB
Go

package server
import (
"cmp"
"net/http"
"slices"
ssync "skillshare/internal/sync"
)
type analyzeCharTokensResponse struct {
Chars int `json:"chars"`
EstimatedTokens int `json:"estimated_tokens"`
}
type analyzeSkillResponse struct {
Name string `json:"name"`
DescriptionChars int `json:"description_chars"`
DescriptionTokens int `json:"description_tokens"`
BodyChars int `json:"body_chars"`
BodyTokens int `json:"body_tokens"`
LintIssues []ssync.LintIssue `json:"lint_issues,omitempty"`
Path string `json:"path"`
IsTracked bool `json:"is_tracked"`
Targets []string `json:"targets,omitempty"`
Description string `json:"description,omitempty"`
}
type analyzeTargetResponse struct {
Name string `json:"name"`
SkillCount int `json:"skill_count"`
AlwaysLoaded analyzeCharTokensResponse `json:"always_loaded"`
OnDemandMax analyzeCharTokensResponse `json:"on_demand_max"`
Skills []analyzeSkillResponse `json:"skills"`
}
const analyzeCharsPerToken = 4
func (s *Server) handleAnalyze(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
targets := s.cfg.Targets
defaultMode := s.cfg.Mode
s.mu.RUnlock()
discovered, err := ssync.DiscoverSourceSkillsForAnalyze(source)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
entries := make([]analyzeTargetResponse, 0)
for name, target := range targets {
sc := target.SkillsConfig()
tMode := sc.Mode
if tMode == "" {
tMode = defaultMode
}
var filtered []ssync.DiscoveredSkill
if tMode == "symlink" {
filtered = discovered
} else {
var ferr error
filtered, ferr = ssync.FilterSkills(discovered, sc.Include, sc.Exclude)
if ferr != nil {
continue
}
filtered = ssync.FilterSkillsByTarget(filtered, name)
}
if len(filtered) == 0 {
continue
}
skills := make([]analyzeSkillResponse, 0, len(filtered))
var totalDescChars, totalBodyChars int
for _, sk := range filtered {
totalDescChars += sk.DescChars
totalBodyChars += sk.BodyChars
skills = append(skills, analyzeSkillResponse{
Name: sk.FlatName,
DescriptionChars: sk.DescChars,
DescriptionTokens: sk.DescChars / analyzeCharsPerToken,
BodyChars: sk.BodyChars,
BodyTokens: sk.BodyChars / analyzeCharsPerToken,
LintIssues: sk.LintIssues,
Path: sk.RelPath,
IsTracked: sk.IsInRepo,
Targets: sk.Targets,
Description: sk.Description,
})
}
slices.SortFunc(skills, func(a, b analyzeSkillResponse) int {
return cmp.Compare(b.DescriptionChars, a.DescriptionChars)
})
entries = append(entries, analyzeTargetResponse{
Name: name,
SkillCount: len(skills),
AlwaysLoaded: analyzeCharTokensResponse{
Chars: totalDescChars,
EstimatedTokens: totalDescChars / analyzeCharsPerToken,
},
OnDemandMax: analyzeCharTokensResponse{
Chars: totalBodyChars,
EstimatedTokens: totalBodyChars / analyzeCharsPerToken,
},
Skills: skills,
})
}
slices.SortFunc(entries, func(a, b analyzeTargetResponse) int {
return cmp.Compare(b.AlwaysLoaded.Chars, a.AlwaysLoaded.Chars)
})
writeJSON(w, map[string]any{"targets": entries})
}