Files
skillshare/internal/server/handler_agentignore.go
T
Willie 36a314442b refactor(server): decode JSON request bodies through one size-limited helper
About 57 handlers decoded request bodies with no size limit, and the few
that had one each picked their own. They now go through decodeJSON with a
1 MiB default; handlers that need more keep their larger limit. An
oversized body gets 413 instead of whatever the handler returned for bad
JSON (MCP and plugin requests used to answer 400). Every other decode
error keeps the handler's existing status and message.
2026-09-30 02:25:42 +08:00

127 lines
3.0 KiB
Go

package server
import (
"errors"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/resource"
"skillshare/internal/skillignore"
"skillshare/internal/utils"
)
type agentignoreStats struct {
PatternCount int `json:"pattern_count"`
IgnoredCount int `json:"ignored_count"`
Patterns []string `json:"patterns"`
IgnoredAgents []string `json:"ignored_agents"`
}
type agentignoreResponse struct {
Exists bool `json:"exists"`
Path string `json:"path"`
Raw string `json:"raw"`
Stats *agentignoreStats `json:"stats,omitempty"`
}
func (s *Server) handleGetAgentignore(w http.ResponseWriter, _ *http.Request) {
s.mu.RLock()
source := s.agentsSource()
s.mu.RUnlock()
if source == "" {
writeJSON(w, agentignoreResponse{Path: "", Raw: ""})
return
}
resolved := utils.ResolveSymlink(source)
ignorePath := filepath.Join(resolved, ".agentignore")
raw, err := os.ReadFile(ignorePath)
resp := agentignoreResponse{
Exists: err == nil,
Path: ignorePath,
Raw: string(raw),
}
// Discover agents to compute ignore stats
matcher := skillignore.ReadAgentIgnoreMatcher(resolved)
if matcher.HasRules() {
patterns := matcher.Patterns()
if patterns == nil {
patterns = []string{}
}
all, _ := resource.AgentKind{}.Discover(source)
var ignored []string
for _, a := range all {
if a.Disabled {
ignored = append(ignored, a.RelPath)
}
}
if ignored == nil {
ignored = []string{}
}
resp.Stats = &agentignoreStats{
PatternCount: len(patterns),
IgnoredCount: len(ignored),
Patterns: patterns,
IgnoredAgents: ignored,
}
}
writeJSON(w, resp)
}
func (s *Server) handlePutAgentignore(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Raw string `json:"raw"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
source := s.agentsSource()
if source == "" {
writeError(w, http.StatusBadRequest, "agents source not configured")
return
}
resolved := utils.ResolveSymlink(source)
ignorePath := filepath.Join(resolved, ".agentignore")
if body.Raw == "" {
if err := os.Remove(ignorePath); err != nil && !errors.Is(err, os.ErrNotExist) {
writeError(w, http.StatusInternalServerError, "failed to delete .agentignore: "+err.Error())
return
}
} else {
// Ensure directory exists
if err := os.MkdirAll(resolved, 0755); err != nil {
writeError(w, http.StatusInternalServerError, "failed to create agents directory: "+err.Error())
return
}
if err := os.WriteFile(ignorePath, []byte(body.Raw), 0644); err != nil {
writeError(w, http.StatusInternalServerError, "failed to write .agentignore: "+err.Error())
return
}
}
s.writeOpsLog("agentignore", "ok", start, map[string]any{
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
}