mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* docs(proposals): propose agent-specific skill variants Tracked repositories can ship different files for the same skill across agents. Counting and syncing each copy independently sends incompatible instructions to every target unless users maintain routing filters. Propose repository-scoped logical identity and exact variant routing while preserving source files, explicit controls, and existing naming behavior. Leave activation and compatibility decisions for maintainer review. Refs #648 * docs(proposals): clarify variant routing compatibility Explain how recognition affects universal-only targets and how existing per-variant overrides preserve the desired routing. Fix the precedence between variant identity and generic host-style path inference. Include analyze, move, and catalog consumers while preserving physical identifiers and actual context-cost accounting. Refs #648 * docs(proposals): clarify activation assumptions and scope Make acceptance cases conditional on maintainer-approved recognition being enabled for the tracked source. Separate the provisional implementation estimate from this proposal-only PR so its size is clear. Refs #648 * docs(proposals): qualify naming and opt-in assumptions State that the reproduced routing behavior uses default flat target naming. Leave any opt-in surface with the maintainer's activation decision without ruling out future CLI or config fields. Refs #648 * feat: recognize and route tracked agent skill variants Count tracked variants as logical skills in source and repository summaries, while preserving physical paths, sync operations, and context costs. Select matching canonical agent variants, including account targets, through shared discovery and selection with existing explicit routing precedence. Refs #648 * fix: count logical skills in sync inventory summaries Use shared logical and physical inventory counts in global and project sync closing lines while preserving each target's operation totals. Refs #648 * fix: preserve variant routing diagnostics across previews Resolve target selections once so CLI and server previews keep ambiguity reasons and physical skip counts. Explain managed variants removed by automatic agent routing, and report logical inventory in doctor. Keep warning rows read-only, document upgrade recovery in translated filtering references, and remove the optional README-only addition. Refs #648 * fix: align sync matrix previews with shared variant selection Classify the full source selection once per target so matrix previews honor canonical aliases, account identity, exact-agent routing, and ambiguous variant skips. Preserve each rejected physical path's existing diagnostic and draft filter disambiguation; skipped preview rows remain read-only. Refs #648 * test: expect singular skill wording in doctor source checks Keep the undiscovered-link count assertion aligned with logical source inventory formatting for one skill in global and project mode. Refs #648 * fix: retain selection diagnostics in catalog and diff verdicts Report warning-only diff targets as requiring attention while preserving local-only folder semantics. Reuse shared per-path selection diagnostics in target-scoped catalogs without applying target naming to served paths. Compute source counts once in summary callers and require variant counts in the source status formatter. Refs #648 * test: tighten tracked variant preview fixtures Use a real committed tracked repository for status counts, verify sync preview banners retain server warnings, and use emitted ambiguity reason text in target preview fixtures. Refs #648 * fix(sync): keep agent variants in shared target folders Compatible targets sharing a skills folder need a combined selection so Codex and universal do not prune one another in merge or copy mode. Carry every shared reader through selection, pruning and previews, including project targets, account identities and path aliases. Require a matching skill suffix under two distinct agents before inferring variants, keeping ordinary agent-named categories unrestricted. Separate naming warnings from ambiguous selection in diff and explain ambiguous removals. Add global/project regression coverage for repeated sync, target-only sync, status JSON, diff and API consumers. * fix(variants): align consumer counts, hints and diagnostics Use logical source counts in list, tracked install and init summaries while matching audit caches against physical variants. Restore localized dashboard skip hints and explain symlink routing limits in status and doctor, including project and JSON output. Document paired-agent detection and compatible shared-folder selection in the proposal, built-in skill and English plus translated command pages. Add focused regressions for the affected consumers. * fix(sync): cover initial sync and ignored variant diagnostics Init syncs a fresh in-memory config, so resolve shared target identities before its first sync as well as when loading saved configuration. A whole-directory symlink exposes ignored skills too. Diagnose agent variants using physical inventory when active discovery has none, while keeping logical counts and merge/copy selection restricted to enabled skills. Cover global/project JSON status and doctor with regressions. Reuse sourcewalk directory and canonical-path helpers so traversal guards remain enforced and path resolution has a single existing owner. * fix(variants): address conversion, counts and selection diagnostics Ignore runtime agent identities when comparing convertible project targets and count logical skills during init. Keep selection warnings separate from naming warnings and preserve agent-named parent skills as ordinary skills. Cover account sync/prune, project ambiguity and overrides in merge/copy modes, and init/status agreement. Focused verification ran in an isolated Linux Codex container, not the devcontainer, as authorized. * refactor(variants): remove obsolete selection helpers Remove the unused FilterSkillsByTarget wrapper and prune agent parameter, cache canonical target aliases once, and restore catalog skip ordering to avoid an unrelated behavior change. Completes the optional cleanup from PR 649 review. * fix(ui): expose localized variant selection diagnostics Show target warnings with source paths, localize ambiguity across preview and detail consumers, and let skipped preview rows add a physical exclude pattern. Show tracked repository variant counts alongside logical skills. Drop display-only rendered cases while retaining mutation protection and plain TypeScript logic coverage. Inspect target states in Clean/Playful light/dark and Traditional Chinese. * docs(variants): explain diagnostics and add sync runbook Document doctor variant_routing and logical/physical counts, diff selection skips, and the nested parent-skill boundary. Keep all four command translations aligned and add isolated account/project E2E scenarios. Both runbook steps passed in the authorized Linux Codex container with separate HOME/XDG/TMPDIR fixtures. Website typecheck and all five locale builds passed. * test(config): use native paths in project conversion fixture The shared-agent regression fixture appended slash-separated suffixes to Windows temporary paths, so project path matching returned no candidates. Build each path with filepath.Join and preserve all conversion assertions. Focused project configuration tests passed in the authorized isolated Linux Codex container, not the devcontainer. Windows CI verifies the platform-specific fixture correction. * fix(ui): recompute shared skill routing for filter previews Evaluate edited include/exclude settings in the complete target map before classifying the draft, so a formerly shared folder no longer keeps its old union when the filters differ. Preserve the saved target configuration. * fix(mcp): keep parent skills when peer variants route elsewhere Do not treat an otherwise eligible variant for another agent as an unserved nested skill. Continue blocking parent publication for explicitly filtered, disabled, ambiguous, or invalid children so filters still hide their files. * fix(ui): distinguish agent selection from naming skips Expose separate naming and selection skip counts on target summaries. Show each applicable remedy in target details and sync previews so ambiguity keeps its filter/override guidance alongside naming and collision hints. * fix(list): include disabled rows in tracked repository counts Count the complete inventory that list displays, including ignored skills and physical variants, without changing the enabled counts used by status, install, or sync. Clone rows before counting to preserve disabled state. * docs: align variant counts and routing recovery guidance Show logical skill and physical variant counts in list/install documentation and keep the four translations aligned with the filtering examples. Explain that agent-named skill parents keep ordinary nested skills and retain the devcontainer-only runbook environment. Removal warnings now recommend a source-local override, since include filters cannot undo agent routing. * fix(mcp): preserve explicit target restrictions on nested variants Limit the differently routed child exemption to automatic layout inference. Frontmatter targets and dashboard source-local overrides must still prevent a parent from publishing files explicitly restricted to another target. * fix(ui): fill target name in naming warning guidance Pass the current target name when rendering localized warning hints so the same naming guidance shown beside the controls does not expose a placeholder in the warning banner. Verified in the populated target preview. --------- Co-authored-by: Willie <egg358358@gmail.com>
667 lines
22 KiB
Go
667 lines
22 KiB
Go
package server
|
|
|
|
import (
|
|
"cmp"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/hooks"
|
|
"skillshare/internal/mcp"
|
|
ssync "skillshare/internal/sync"
|
|
"skillshare/internal/targetsummary"
|
|
)
|
|
|
|
type targetItem struct {
|
|
Name string `json:"name"`
|
|
Project string `json:"project,omitempty"` // root of the project this target belongs to
|
|
Agent string `json:"agent,omitempty"` // the built-in Agent this is another config directory of
|
|
ConfigDir string `json:"configDir,omitempty"`
|
|
CLI string `json:"cli,omitempty"` // the executable that runs its plugin commands, when not the Agent's own
|
|
Path string `json:"path"`
|
|
Mode string `json:"mode"`
|
|
TargetNaming string `json:"targetNaming"`
|
|
Status string `json:"status"`
|
|
LinkedCount int `json:"linkedCount"`
|
|
LocalCount int `json:"localCount"`
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
ExpectedSkillCount int `json:"expectedSkillCount"`
|
|
SkippedSkillCount int `json:"skippedSkillCount,omitempty"`
|
|
NamingSkippedSkillCount int `json:"namingSkippedSkillCount,omitempty"`
|
|
SelectionSkippedSkillCount int `json:"selectionSkippedSkillCount,omitempty"`
|
|
Warnings []string `json:"warnings,omitempty"`
|
|
CollisionCount int `json:"collisionCount,omitempty"`
|
|
AgentPath string `json:"agentPath,omitempty"`
|
|
AgentMode string `json:"agentMode,omitempty"`
|
|
AgentExtension string `json:"agentExtension,omitempty"`
|
|
AgentInclude []string `json:"agentInclude,omitempty"`
|
|
AgentExclude []string `json:"agentExclude,omitempty"`
|
|
AgentLinkedCount *int `json:"agentLinkedCount,omitempty"`
|
|
AgentLocalCount *int `json:"agentLocalCount,omitempty"`
|
|
AgentExpectedCount *int `json:"agentExpectedCount,omitempty"`
|
|
SkillsEnabled bool `json:"skillsEnabled"`
|
|
// SkillsReadFrom (skills off) names the enabled targets whose skills folder
|
|
// this tool still reads; SkillsAlsoReadBy (skills on) names the targets with
|
|
// skills off whose tool reads this target's folder.
|
|
SkillsReadFrom []string `json:"skillsReadFrom,omitempty"`
|
|
SkillsAlsoReadBy []string `json:"skillsAlsoReadBy,omitempty"`
|
|
// SkillsSharedWith names the other targets with skills on that write to this
|
|
// target's skills folder, so the page can warn when their settings differ.
|
|
SkillsSharedWith []string `json:"skillsSharedWith,omitempty"`
|
|
}
|
|
|
|
var detachSkillsTarget = ssync.DetachSkillsTarget
|
|
|
|
func (s *Server) handleListTargets(w http.ResponseWriter, r *http.Request) {
|
|
// Snapshot config under RLock, then release before I/O.
|
|
s.mu.RLock()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
cfgMode := s.cfg.Mode
|
|
targets := s.cloneTargets()
|
|
isProjectMode := s.IsProjectMode()
|
|
projectRoot := s.projectRoot
|
|
agentsSourcePath := s.agentsSource()
|
|
cfgSnapshot := *s.cfg
|
|
var projectEntries []config.ProjectTargetEntry
|
|
if isProjectMode && s.projectCfg != nil {
|
|
projectEntries = append([]config.ProjectTargetEntry(nil), s.projectCfg.Targets...)
|
|
}
|
|
s.mu.RUnlock()
|
|
|
|
globalMode := cfgMode
|
|
if globalMode == "" {
|
|
globalMode = "merge"
|
|
}
|
|
|
|
projectEntryByName := make(map[string]config.ProjectTargetEntry, len(projectEntries))
|
|
for _, entry := range projectEntries {
|
|
projectEntryByName[entry.Name] = entry
|
|
}
|
|
|
|
var (
|
|
agentBuilder *targetsummary.Builder
|
|
err error
|
|
)
|
|
if isProjectMode {
|
|
agentBuilder, err = targetsummary.NewProjectBuilder(agentsSourcePath, projectRoot)
|
|
} else {
|
|
agentBuilder, err = targetsummary.NewGlobalBuilder(&cfgSnapshot)
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to discover agents: "+err.Error())
|
|
return
|
|
}
|
|
|
|
items := make([]targetItem, 0, len(targets))
|
|
discovered, discoveredErr := ssync.DiscoverSourceSkills(source, s.skillsWalk())
|
|
|
|
// Project targets are edited under projects, so they are listed only on request:
|
|
// scope=projects for them alone, scope=all for what a sync writes.
|
|
scope := r.URL.Query().Get("scope")
|
|
for name, target := range targets {
|
|
if scope != "all" && (target.ProjectRoot() != "") != (scope == "projects") {
|
|
continue
|
|
}
|
|
sc := target.SkillsConfig()
|
|
mode := sc.Mode
|
|
if mode == "" {
|
|
mode = globalMode
|
|
}
|
|
|
|
item := targetItem{
|
|
Name: name,
|
|
Project: target.ProjectRoot(),
|
|
Agent: target.Agent,
|
|
ConfigDir: target.ConfigDir,
|
|
CLI: target.CLI,
|
|
Path: sc.Path,
|
|
Mode: mode,
|
|
TargetNaming: config.EffectiveTargetNaming(sc.TargetNaming),
|
|
Include: func() []string {
|
|
if len(sc.Include) == 0 {
|
|
return []string{}
|
|
}
|
|
return append([]string(nil), sc.Include...)
|
|
}(),
|
|
Exclude: func() []string {
|
|
if len(sc.Exclude) == 0 {
|
|
return []string{}
|
|
}
|
|
return append([]string(nil), sc.Exclude...)
|
|
}(),
|
|
SkillsEnabled: sc.IsEnabled(),
|
|
}
|
|
|
|
switch {
|
|
case !sc.IsEnabled():
|
|
item.Status = "skills off"
|
|
item.SkillsReadFrom = config.SkillsReadFrom(targets, name, projectRoot)
|
|
case mode == "merge" || mode == "copy":
|
|
if discoveredErr == nil {
|
|
resolution, err := ssync.ResolveTargetSkillsForTarget(name, sc, discovered)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid include/exclude for target "+name+": "+err.Error())
|
|
return
|
|
}
|
|
item.ExpectedSkillCount = len(resolution.Skills)
|
|
item.SkippedSkillCount = resolution.SkippedCount
|
|
item.NamingSkippedSkillCount = resolution.NamingSkippedCount
|
|
item.SelectionSkippedSkillCount = len(resolution.SelectionWarnings)
|
|
item.CollisionCount = len(resolution.Collisions)
|
|
item.Warnings = append(resolution.Warnings, resolution.SelectionWarnings...)
|
|
}
|
|
if mode == "merge" {
|
|
status, linked, local := ssync.CheckStatusMerge(sc.Path, source)
|
|
item.Status = status.String()
|
|
item.LinkedCount = linked
|
|
item.LocalCount = local
|
|
} else {
|
|
status, managed, local := ssync.CheckStatusCopy(sc.Path)
|
|
item.Status = status.String()
|
|
item.LinkedCount = managed
|
|
item.LocalCount = local
|
|
}
|
|
default:
|
|
status := ssync.CheckStatus(sc.Path, source)
|
|
item.Status = status.String()
|
|
}
|
|
item.SkillsAlsoReadBy = config.SkillsAlsoReadBy(targets, name, projectRoot)
|
|
item.SkillsSharedWith = config.SkillsSharedWith(targets, name)
|
|
|
|
var agentSummary *targetsummary.AgentSummary
|
|
if isProjectMode {
|
|
if entry, ok := projectEntryByName[name]; ok {
|
|
agentSummary, err = agentBuilder.ProjectTarget(entry)
|
|
}
|
|
} else {
|
|
agentSummary, err = agentBuilder.GlobalTarget(name, target)
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid agent include/exclude for target "+name+": "+err.Error())
|
|
return
|
|
}
|
|
if agentSummary != nil {
|
|
item.AgentPath = agentSummary.Path
|
|
item.AgentMode = agentSummary.Mode
|
|
item.AgentExtension = agentSummary.Extension
|
|
item.AgentInclude = agentSummary.Include
|
|
item.AgentExclude = agentSummary.Exclude
|
|
item.AgentLinkedCount = intPtr(agentSummary.ManagedCount)
|
|
item.AgentLocalCount = intPtr(agentSummary.LocalCount)
|
|
item.AgentExpectedCount = intPtr(agentSummary.ExpectedCount)
|
|
}
|
|
|
|
items = append(items, item)
|
|
}
|
|
|
|
// Count source skills for drift detection
|
|
counts := ssync.SkillCounts{}
|
|
if discoveredErr == nil {
|
|
counts = ssync.CountSkills(discovered)
|
|
}
|
|
|
|
writeJSON(w, map[string]any{"targets": items, "sourceSkillCount": counts.Skills, "sourceVariantCount": counts.Variants})
|
|
}
|
|
|
|
func intPtr(v int) *int {
|
|
return &v
|
|
}
|
|
|
|
func (s *Server) handleAddTarget(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
var body struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
AgentPath string `json:"agentPath"`
|
|
// Agent and ConfigDir add another config directory of a built-in Agent instead.
|
|
Agent string `json:"agent"`
|
|
ConfigDir string `json:"configDir"`
|
|
CLI string `json:"cli"`
|
|
// Instructions optionally names the instruction file of a custom target.
|
|
Instructions *config.TargetInstructionsConfig `json:"instructions"`
|
|
// SkillsEnabled false adds the target with skills off (default true).
|
|
SkillsEnabled *bool `json:"skills_enabled"`
|
|
}
|
|
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
}
|
|
return
|
|
}
|
|
|
|
if body.Name == "" {
|
|
writeError(w, http.StatusBadRequest, "name is required")
|
|
return
|
|
}
|
|
|
|
if body.Agent != "" || body.ConfigDir != "" {
|
|
if s.IsProjectMode() {
|
|
writeError(w, http.StatusBadRequest, "a project has no Agent config directory to add")
|
|
return
|
|
}
|
|
if _, exists := s.cfg.Targets[body.Name]; exists {
|
|
writeError(w, http.StatusConflict, "target already exists: "+body.Name)
|
|
return
|
|
}
|
|
path, err := s.cfg.AddAgentConfigDirTarget(body.Name, body.Agent, body.ConfigDir, body.CLI)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if body.SkillsEnabled != nil && !*body.SkillsEnabled {
|
|
tc := s.cfg.Targets[body.Name]
|
|
tc.EnsureSkills().SetEnabled(false)
|
|
s.cfg.Targets[body.Name] = tc
|
|
}
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
s.writeOpsLog("target", "ok", start, map[string]any{"action": "add", "name": body.Name, "target": path, "scope": "ui"}, "")
|
|
writeJSON(w, map[string]any{"success": true})
|
|
return
|
|
}
|
|
|
|
// In project mode, path can be resolved from known targets
|
|
if body.Path == "" {
|
|
if s.IsProjectMode() {
|
|
if known, ok := config.LookupProjectTarget(body.Name); ok {
|
|
body.Path = known.Path
|
|
} else {
|
|
writeError(w, http.StatusBadRequest, "unknown target, path is required")
|
|
return
|
|
}
|
|
} else {
|
|
writeError(w, http.StatusBadRequest, "name and path are required")
|
|
return
|
|
}
|
|
}
|
|
|
|
if _, exists := s.cfg.Targets[body.Name]; exists {
|
|
writeError(w, http.StatusConflict, "target already exists: "+body.Name)
|
|
return
|
|
}
|
|
if ic := body.Instructions; ic != nil {
|
|
ic.Path = strings.TrimSpace(ic.Path)
|
|
if err := config.ValidateTargetInstructions(ic, s.IsProjectMode()); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
skillsOff := body.SkillsEnabled != nil && !*body.SkillsEnabled
|
|
tc := config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: body.Path}, Instructions: body.Instructions}
|
|
if !s.IsProjectMode() {
|
|
tc.Skills.Mode = config.NewTargetSkillsMode(s.cfg.TargetNaming, s.cfg.Mode)
|
|
}
|
|
tc.Skills.SetEnabled(!skillsOff)
|
|
if body.AgentPath != "" {
|
|
tc.Agents = &config.ResourceTargetConfig{Path: body.AgentPath}
|
|
}
|
|
s.cfg.Targets[body.Name] = tc
|
|
|
|
// In project mode, also update the project config
|
|
if s.IsProjectMode() {
|
|
entry := config.ProjectTargetEntry{Name: body.Name, Instructions: body.Instructions}
|
|
if skillsOff {
|
|
entry.EnsureSkills().SetEnabled(false)
|
|
}
|
|
if mode := config.NewTargetSkillsMode(s.projectCfg.TargetNaming, ""); mode != "" {
|
|
entry.EnsureSkills().Mode = mode // a project target defaults to merge
|
|
}
|
|
s.projectCfg.Targets = append(s.projectCfg.Targets, entry)
|
|
}
|
|
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("target", "ok", start, map[string]any{
|
|
"action": "add",
|
|
"name": body.Name,
|
|
"target": body.Path,
|
|
"scope": "ui",
|
|
}, "")
|
|
|
|
writeJSON(w, map[string]any{"success": true})
|
|
}
|
|
|
|
func (s *Server) handleRemoveTarget(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
name := r.PathValue("name")
|
|
|
|
target, exists := s.cfg.Targets[name]
|
|
if !exists {
|
|
writeError(w, http.StatusNotFound, "target not found: "+name)
|
|
return
|
|
}
|
|
|
|
sc := target.SkillsConfig()
|
|
|
|
if target.ProjectRoot() != "" {
|
|
writeError(w, http.StatusBadRequest, name+" belongs to a project; remove the project instead")
|
|
return
|
|
}
|
|
// Another target writing the same folder (codex and universal) still owns its
|
|
// links; a target with skills off no longer manages its folder.
|
|
if sc.IsEnabled() && config.SkillsPathKeptBy(s.cfg.Targets, name, nil) == "" {
|
|
if status, err := s.detachSkillsTarget(sc.Path); err != nil {
|
|
writeError(w, status, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// Read the MCP config before it is saved without the target: afterwards its own
|
|
// name no longer resolves, so the config no longer loads.
|
|
var warnings []string
|
|
if target.Agent != "" {
|
|
if warning := mcp.ReferenceWarning(s.configPath(), name); warning != "" {
|
|
warnings = append(warnings, warning)
|
|
}
|
|
if warning := hooks.ReferenceWarning(s.configPath(), name); warning != "" {
|
|
warnings = append(warnings, warning)
|
|
}
|
|
}
|
|
|
|
delete(s.cfg.Targets, name)
|
|
|
|
// In project mode, also remove from project config
|
|
if s.IsProjectMode() {
|
|
filtered := make([]config.ProjectTargetEntry, 0, len(s.projectCfg.Targets))
|
|
for _, t := range s.projectCfg.Targets {
|
|
if t.Name != name {
|
|
filtered = append(filtered, t)
|
|
}
|
|
}
|
|
s.projectCfg.Targets = filtered
|
|
}
|
|
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("target", "ok", start, map[string]any{
|
|
"action": "remove",
|
|
"name": name,
|
|
"target": sc.Path,
|
|
"scope": "ui",
|
|
}, "")
|
|
|
|
writeJSON(w, map[string]any{"success": true, "name": name, "warnings": warnings})
|
|
}
|
|
|
|
func (s *Server) handleUpdateTarget(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
name := r.PathValue("name")
|
|
|
|
target, exists := s.cfg.Targets[name]
|
|
if !exists {
|
|
writeError(w, http.StatusNotFound, "target not found: "+name)
|
|
return
|
|
}
|
|
if target.ProjectRoot() != "" {
|
|
writeError(w, http.StatusBadRequest, name+" belongs to a project; edit the project instead")
|
|
return
|
|
}
|
|
// Skills and Agents are pointers shared with s.cfg; edit copies so a
|
|
// request rejected halfway leaves the config untouched.
|
|
if target.Skills != nil {
|
|
sk := *target.Skills
|
|
target.Skills = &sk
|
|
}
|
|
if target.Agents != nil {
|
|
ag := *target.Agents
|
|
target.Agents = &ag
|
|
}
|
|
|
|
var body struct {
|
|
Include *[]string `json:"include"` // null = no change, [] = clear
|
|
Exclude *[]string `json:"exclude"`
|
|
Mode *string `json:"mode"`
|
|
TargetNaming *string `json:"target_naming"`
|
|
AgentMode *string `json:"agent_mode"`
|
|
AgentInclude *[]string `json:"agent_include"`
|
|
AgentExclude *[]string `json:"agent_exclude"`
|
|
AgentExtension *string `json:"agent_extension"` // "" = clear
|
|
SkillsEnabled *bool `json:"skills_enabled"`
|
|
}
|
|
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
}
|
|
return
|
|
}
|
|
|
|
// An extension converts each agent, so it implies copy mode. Reject a
|
|
// conflicting mode sent in this same call, as extras targets do.
|
|
if body.AgentExtension != nil && *body.AgentExtension != "" && body.AgentMode != nil && *body.AgentMode != "copy" {
|
|
writeError(w, http.StatusBadRequest, "agent extension requires copy mode, but agent_mode "+*body.AgentMode+" was set")
|
|
return
|
|
}
|
|
|
|
target.EnsureSkills()
|
|
|
|
// Validate patterns
|
|
if body.Include != nil {
|
|
if _, err := ssync.FilterSkills(nil, *body.Include, nil); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid include pattern: "+err.Error())
|
|
return
|
|
}
|
|
target.Skills.Include = *body.Include
|
|
}
|
|
if body.Exclude != nil {
|
|
if _, err := ssync.FilterSkills(nil, nil, *body.Exclude); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid exclude pattern: "+err.Error())
|
|
return
|
|
}
|
|
target.Skills.Exclude = *body.Exclude
|
|
}
|
|
|
|
if body.Mode != nil {
|
|
switch *body.Mode {
|
|
case "merge", "symlink", "copy":
|
|
target.Skills.Mode = *body.Mode
|
|
default:
|
|
writeError(w, http.StatusBadRequest, "invalid mode: "+*body.Mode+"; must be merge, symlink, or copy")
|
|
return
|
|
}
|
|
}
|
|
|
|
if body.TargetNaming != nil {
|
|
if !config.IsValidTargetNaming(*body.TargetNaming) {
|
|
writeError(w, http.StatusBadRequest, "invalid target_naming: "+*body.TargetNaming+"; must be flat, standard, or prefixed")
|
|
return
|
|
}
|
|
target.Skills.TargetNaming = *body.TargetNaming
|
|
}
|
|
// Check the pair the target ends up with: an off target may keep a pair its
|
|
// mode cannot sync, and turning skills on checks it.
|
|
skillsOn := target.SkillsConfig().IsEnabled()
|
|
if body.SkillsEnabled != nil {
|
|
skillsOn = *body.SkillsEnabled
|
|
}
|
|
if skillsOn && (body.Mode != nil || body.TargetNaming != nil || body.SkillsEnabled != nil) {
|
|
if err := config.TargetNamingModeError(target.SkillsConfig().TargetNaming, cmp.Or(target.Skills.Mode, s.cfg.Mode)); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
if body.AgentMode != nil {
|
|
switch *body.AgentMode {
|
|
case "merge", "symlink", "copy":
|
|
target.EnsureAgents().Mode = *body.AgentMode
|
|
default:
|
|
writeError(w, http.StatusBadRequest, "invalid agent_mode: "+*body.AgentMode+"; must be merge, symlink, or copy")
|
|
return
|
|
}
|
|
}
|
|
|
|
if body.AgentExtension != nil {
|
|
if *body.AgentExtension != "" {
|
|
target.EnsureAgents().Mode = "copy"
|
|
}
|
|
target.EnsureAgents().Extension = *body.AgentExtension
|
|
}
|
|
|
|
if body.AgentInclude != nil {
|
|
if _, err := ssync.FilterSkills(nil, *body.AgentInclude, nil); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid agent include pattern: "+err.Error())
|
|
return
|
|
}
|
|
target.EnsureAgents().Include = *body.AgentInclude
|
|
}
|
|
if body.AgentExclude != nil {
|
|
if _, err := ssync.FilterSkills(nil, nil, *body.AgentExclude); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid agent exclude pattern: "+err.Error())
|
|
return
|
|
}
|
|
target.EnsureAgents().Exclude = *body.AgentExclude
|
|
}
|
|
|
|
if body.SkillsEnabled != nil {
|
|
target.Skills.SetEnabled(*body.SkillsEnabled)
|
|
}
|
|
|
|
s.cfg.Targets[name] = target
|
|
|
|
// In project mode, also update the project config
|
|
if s.IsProjectMode() {
|
|
for i := range s.projectCfg.Targets {
|
|
if s.projectCfg.Targets[i].Name == name {
|
|
sk := s.projectCfg.Targets[i].EnsureSkills()
|
|
if body.SkillsEnabled != nil {
|
|
sk.SetEnabled(*body.SkillsEnabled)
|
|
}
|
|
if body.Include != nil {
|
|
sk.Include = *body.Include
|
|
}
|
|
if body.Exclude != nil {
|
|
sk.Exclude = *body.Exclude
|
|
}
|
|
if body.Mode != nil {
|
|
sk.Mode = *body.Mode
|
|
}
|
|
if body.TargetNaming != nil {
|
|
sk.TargetNaming = *body.TargetNaming
|
|
}
|
|
if body.AgentMode != nil {
|
|
s.projectCfg.Targets[i].EnsureAgents().Mode = *body.AgentMode
|
|
}
|
|
if body.AgentInclude != nil {
|
|
s.projectCfg.Targets[i].EnsureAgents().Include = *body.AgentInclude
|
|
}
|
|
if body.AgentExclude != nil {
|
|
s.projectCfg.Targets[i].EnsureAgents().Exclude = *body.AgentExclude
|
|
}
|
|
if body.AgentExtension != nil {
|
|
ag := s.projectCfg.Targets[i].EnsureAgents()
|
|
if *body.AgentExtension != "" {
|
|
ag.Mode = "copy"
|
|
}
|
|
ag.Extension = *body.AgentExtension
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Skills off applies at once: the folder keeps only what skillshare did not link.
|
|
var detach *ssync.SkillsOffResult
|
|
if body.SkillsEnabled != nil && !*body.SkillsEnabled {
|
|
res, err := ssync.DetachSkills(s.cfg.Targets, name, s.cfg.EffectiveSkillsSource(), false)
|
|
if err != nil {
|
|
s.writeOpsLog("target", "error", start, map[string]any{"action": "skills", "name": name, "enabled": false, "scope": "ui"}, err.Error())
|
|
writeError(w, http.StatusInternalServerError, "skills off saved, but removing links failed: "+err.Error())
|
|
return
|
|
}
|
|
detach = res
|
|
}
|
|
|
|
hasFilter := body.Include != nil || body.Exclude != nil || body.AgentInclude != nil || body.AgentExclude != nil
|
|
hasSetting := body.Mode != nil || body.TargetNaming != nil || body.AgentMode != nil || body.AgentExtension != nil || body.SkillsEnabled != nil
|
|
action := "filter"
|
|
if hasSetting && hasFilter {
|
|
action = "settings+filter"
|
|
} else if hasSetting {
|
|
action = "settings"
|
|
}
|
|
logArgs := map[string]any{
|
|
"action": action,
|
|
"name": name,
|
|
"scope": "ui",
|
|
}
|
|
if body.SkillsEnabled != nil {
|
|
logArgs["skills_enabled"] = *body.SkillsEnabled
|
|
}
|
|
if detach != nil {
|
|
logArgs["removed"] = len(detach.Removed)
|
|
logArgs["kept"] = len(detach.Kept)
|
|
logArgs["copies"] = len(detach.Copies)
|
|
}
|
|
s.writeOpsLog("target", "ok", start, logArgs, "")
|
|
|
|
resp := map[string]any{"success": true}
|
|
if detach != nil {
|
|
resp["detach"] = map[string]any{"removed": detach.Removed, "kept": detach.Kept, "copies": detach.Copies}
|
|
}
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
// handleSkillsOffPreview reports what turning skills off for a target would
|
|
// remove and keep, without writing anything.
|
|
func (s *Server) handleSkillsOffPreview(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.RLock()
|
|
targets := s.cloneTargets()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
s.mu.RUnlock()
|
|
|
|
name := r.PathValue("name")
|
|
target, ok := targets[name]
|
|
if !ok {
|
|
writeError(w, http.StatusNotFound, "target not found: "+name)
|
|
return
|
|
}
|
|
if target.ProjectRoot() != "" {
|
|
writeError(w, http.StatusBadRequest, name+" belongs to a project; edit the project instead")
|
|
return
|
|
}
|
|
res, err := ssync.DetachSkills(targets, name, source, true)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
resp := map[string]any{"remove": res.Removed, "keep": res.Kept, "copies": res.Copies}
|
|
if res.SharedWith != "" {
|
|
resp["sharedWith"] = res.SharedWith
|
|
}
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
// detachSkillsTarget keeps HTTP error translation at the server boundary.
|
|
func (s *Server) detachSkillsTarget(path string) (int, error) {
|
|
if _, err := detachSkillsTarget(path, ssync.DetachOptions{Source: s.cfg.EffectiveSkillsSource()}); err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
return 0, nil
|
|
}
|