Files
skillshare/internal/server/handler_analyze.go
T
Willie 41dd81b7f2 refactor: migrate all callers to SkillsConfig() / EnsureSkills()
44 files across CLI commands, sync engine, and server handlers now use
SkillsConfig() for reads and EnsureSkills() for writes instead of
accessing flat TargetConfig fields directly.

Key changes:
- sync engine (SyncTargetMerge/Copy/Symlink): reads sc := target.SkillsConfig()
- target commands: filter add/remove and mode set use EnsureSkills()
- target TUI: mode picker, filter ops, detail panel all via SkillsConfig()
- server handlers: backup, restore, targets, sync, diff, collect, analyze
- init/init_project: new target creation uses Skills sub-key
- doctor, status, diff: target display reads from SkillsConfig()
- computeTargetDiff: accepts config.TargetConfig directly (was anon struct)
2026-03-29 04:59:23 +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.Source
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})
}