Files
skillshare/internal/server/handler_config.go
T
Willie f6e7265414 feat(hooks): read hook code and scripts from files next to the config (#586)
* feat(hooks): read hook code and scripts from files next to the config

Code Agent extensions and command scripts lived as strings in
config.yaml, so a longer Pi extension or shell script was edited inside
a YAML block scalar with no editor support, and nothing type-checked or
linted it. A breaking change in an Agent's extension API went unnoticed
because the generated file is not part of any tsconfig.

A binding now takes `codeFile: path` instead of `code`, and a `files`
value may be `{from: path}` instead of the contents. Paths resolve
against the folder of the config that holds the entry and are read
every time the config is read, so the regular typecheck or shellcheck
can cover the real source. The revision and drift checks use the
resolved contents, so a file edited after a preview fails the apply as
stale.

Saving keeps the references: enable/disable, `hooks edit` and the
dashboard write `codeFile` and `{from}` back instead of inlining the
contents. JSON carries the contents plus `codeFile`/`filesFrom`, and an
entry echoed back with both must match the file, so an edit is refused
rather than silently dropped. Paths must stay inside the config folder
without symlinks, so a cloned project cannot pull in files from
elsewhere. References are taken from the YAML tree before one strict
decode, which keeps unknown-field checks and anchors shared across
bindings working.

Closes #585

* fix(hooks): refuse an emptied file sent with its reference

An empty files value stood for "contents not sent", so a file cleared in
the dashboard's Git YAML editor while its filesFrom reference stayed was
treated as unsent: the file was read again from disk and the save
reported success with the user's edit discarded.

A value read from {from: path} now keeps a dedicated mark until it is
resolved, so an empty value is compared with the file like any other
contents and refused when they differ. Code keeps using empty as unsent,
since a code binding can never be empty.

* fix(hooks): render referenced files as they are on disk

RenderNative rendered the contents the caller sent. The dashboard sends
the entry it loaded, so after a referenced file changed, View
configuration still showed the old contents while describing them as
what sync would write.

Render now drops sent contents for codeFile and {from} references and
reads every reference again, like preview and save. Draft, render and
config decoding are the only paths that take an entry, and all three now
resolve references.

* fix(hooks): accept file references in JSON entries and anchored values

Two inputs that config.yaml accepts failed to load elsewhere.

A `{from}` value carrying its own YAML anchor, aliased from another
binding, lost its anchor because the value node was replaced instead of
rewritten, so the alias no longer resolved. The node is now rewritten in
place with its anchor kept.

An entry JSON file (`hooks add|edit --file x.json`) could not write a
files value as `{"from": path}`, because JSON decoded straight into
string contents. Binding now decodes JSON files values as contents or a
reference, strictly, like YAML, so both entry formats and the Web API
take the same shapes.
2026-10-19 22:48:00 +08:00

314 lines
9.7 KiB
Go

package server
import (
"cmp"
"errors"
"net/http"
"os"
"path/filepath"
"slices"
"time"
"gopkg.in/yaml.v3"
"skillshare/internal/config"
"skillshare/internal/hooks"
"skillshare/internal/plugin"
"skillshare/internal/utils"
)
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
// Best-effort refresh so UI reflects external config edits without restart.
// Keep endpoint usable even when config is temporarily invalid.
s.mu.Lock()
_ = s.reloadConfig()
cfgObj := any(s.cfg)
if s.IsProjectMode() {
cfgObj = s.projectCfg
}
s.mu.Unlock()
raw, err := os.ReadFile(s.configPath())
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to read config: "+err.Error())
return
}
writeJSON(w, map[string]any{
"config": cfgObj,
"raw": string(raw),
})
}
var syncModes = []string{"merge", "copy", "symlink"}
// handlePatchConfig — PATCH /api/config
// Sets single settings the Settings page owns, so the UI never rewrites the whole YAML.
// All fields live in the global config only; project mode has no default mode, log limit or backup limits.
func (s *Server) handlePatchConfig(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var body struct {
Mode *string `json:"mode"`
LogMaxEntries *int `json:"logMaxEntries"`
BackupMaxCount *int `json:"backupMaxCount"`
BackupMaxSizeMB *int64 `json:"backupMaxSizeMB"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
if body.Mode == nil && body.LogMaxEntries == nil && body.BackupMaxCount == nil && body.BackupMaxSizeMB == nil {
writeError(w, http.StatusBadRequest, "mode, logMaxEntries, backupMaxCount or backupMaxSizeMB is required")
return
}
if body.Mode != nil && !slices.Contains(syncModes, *body.Mode) {
writeError(w, http.StatusBadRequest, "invalid mode: "+*body.Mode)
return
}
if body.LogMaxEntries != nil && *body.LogMaxEntries < 0 {
writeError(w, http.StatusBadRequest, "logMaxEntries cannot be negative")
return
}
if body.BackupMaxCount != nil && *body.BackupMaxCount < 0 || body.BackupMaxSizeMB != nil && *body.BackupMaxSizeMB < 0 {
writeError(w, http.StatusBadRequest, "backup limits cannot be negative")
return
}
s.mu.Lock()
defer s.mu.Unlock()
if s.IsProjectMode() {
writeError(w, http.StatusBadRequest, "these settings are global only")
return
}
// /api/config skips the auto-reload middleware, so pick up outside edits before writing.
_ = s.reloadConfig()
args := map[string]any{"scope": "ui"}
if body.Mode != nil {
// Targets that inherit the mode must still be able to honour their naming.
for name, t := range s.cfg.Targets {
sc := t.SkillsConfig()
if err := config.TargetNamingModeError(sc.TargetNaming, cmp.Or(sc.Mode, *body.Mode)); err != nil && sc.IsEnabled() {
writeError(w, http.StatusBadRequest, "target "+name+": "+err.Error())
return
}
}
if err := s.cfg.ProjectNamingError(s.cfg.Projects, *body.Mode); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
s.cfg.Mode = *body.Mode
args["mode"] = *body.Mode
}
if body.LogMaxEntries != nil {
s.cfg.Log.MaxEntries = body.LogMaxEntries
args["log_max_entries"] = *body.LogMaxEntries
}
if body.BackupMaxCount != nil {
s.cfg.Backup.MaxCount = body.BackupMaxCount
args["backup_max_count"] = *body.BackupMaxCount
}
if body.BackupMaxSizeMB != nil {
s.cfg.Backup.MaxSizeMB = body.BackupMaxSizeMB
args["backup_max_size_mb"] = *body.BackupMaxSizeMB
}
if err := s.saveConfig(); err != nil {
writeError(w, http.StatusInternalServerError, "failed to save config: "+err.Error())
return
}
s.writeOpsLog("config-patch", "ok", start, args, "")
writeJSON(w, map[string]any{"mode": s.cfg.Mode, "logMaxEntries": s.cfg.Log.MaxEntries})
}
func (s *Server) handlePutConfig(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
}
// Validate YAML syntax + semantic validation before saving
var warnings []string
if s.IsProjectMode() {
var testCfg config.ProjectConfig
if err := yaml.Unmarshal([]byte(body.Raw), &testCfg); err != nil {
writeError(w, http.StatusBadRequest, "invalid YAML: "+err.Error())
return
}
if err := plugin.Validate([]byte(body.Raw), nil); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
w2, validErr := config.ValidateProjectConfig(&testCfg, s.projectRoot)
if validErr == nil {
validErr = config.ValidateMCP(testCfg.MCP, testCfg.Sources.MCP)
}
if validErr == nil {
validErr = hooks.ValidateSection(&testCfg.Hooks, s.configPath(), true, nil)
}
if validErr != nil {
writeError(w, http.StatusBadRequest, validErr.Error())
return
}
warnings = w2
} else {
var testCfg config.Config
if err := yaml.Unmarshal([]byte(body.Raw), &testCfg); err != nil {
writeError(w, http.StatusBadRequest, "invalid YAML: "+err.Error())
return
}
// An account of an Agent is a target for plugins under the name the config gives it.
if err := plugin.Validate([]byte(body.Raw), testCfg.AgentConfigDirTargetAgents()); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
w2, validErr := config.ValidateConfig(&testCfg)
if validErr == nil {
validErr = config.ValidateMCP(testCfg.MCP, testCfg.Sources.MCP, testCfg.AgentConfigDirTargets()...)
}
if validErr == nil {
validErr = hooks.ValidateSection(&testCfg.Hooks, s.configPath(), false, testCfg.AgentConfigDirTargetAgents())
}
if validErr != nil {
writeError(w, http.StatusBadRequest, validErr.Error())
return
}
warnings = w2
}
// Write a symlinked config's target; a project config must stay inside the project.
path, err := utils.ConfigWritePath(s.configPath(), s.IsProjectMode())
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if err := os.WriteFile(path, []byte(body.Raw), 0644); err != nil {
writeError(w, http.StatusInternalServerError, "failed to write config: "+err.Error())
return
}
if err := s.reloadConfig(); err != nil {
writeError(w, http.StatusInternalServerError, "config saved but failed to reload: "+err.Error())
return
}
s.writeOpsLog("config", "ok", start, map[string]any{
"scope": "ui",
}, "")
writeJSON(w, map[string]any{
"success": true,
"warnings": warnings,
})
}
func (s *Server) handleAvailableTargets(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
projectRoot := s.projectRoot
targets := s.cloneTargets()
s.mu.RUnlock()
isProjectMode := projectRoot != ""
var defaults map[string]config.TargetConfig
if isProjectMode {
defaults = config.ProjectTargets()
} else {
defaults = config.DefaultTargets()
}
type availTarget struct {
Name string `json:"name"`
Path string `json:"path"`
AgentPath string `json:"agentPath,omitempty"`
// ConfigDir is set when a target can be another config directory of this Agent.
ConfigDir string `json:"configDir,omitempty"`
Installed bool `json:"installed"`
Detected bool `json:"detected"`
// ReadsFrom names the configured targets with skills on whose skills
// folder this tool already reads.
ReadsFrom []string `json:"readsFrom,omitempty"`
// InstructionsFile is the file name of the tool's instructions file
// (AGENTS.md, GEMINI.md, ...), empty when it has none.
InstructionsFile string `json:"instructionsFile,omitempty"`
// InstructionsPath is that file's default path, for the add dialog's list of writes.
InstructionsPath string `json:"instructionsPath,omitempty"`
// ReadBy names the other tools, on this machine or configured, whose
// default scan paths include this tool's skills folder.
ReadBy []string `json:"readBy,omitempty"`
}
var agentDefaults map[string]config.TargetConfig
if isProjectMode {
agentDefaults = config.ProjectAgentTargets()
} else {
agentDefaults = config.DefaultAgentTargets()
}
items := make([]availTarget, 0, len(defaults))
for name, tc := range defaults {
_, installed := targets[name]
// Check if the tool's config directory exists: its declared detect dir,
// else the parent of the skills path.
detected := false
if !installed {
probe := config.DetectDir(name)
if probe == "" {
probe = filepath.Dir(tc.Path)
}
if _, err := os.Stat(probe); err == nil {
detected = true
}
}
item := availTarget{
Name: name,
Path: tc.Path,
Installed: installed,
Detected: detected,
ReadsFrom: config.SkillsReadFrom(targets, name, projectRoot),
}
if agentTC, ok := agentDefaults[name]; ok {
item.AgentPath = agentTC.Path
}
if it, ok := config.LookupInstructions(name, isProjectMode); ok {
item.InstructionsFile = filepath.Base(it.Path)
item.InstructionsPath = it.Path
}
if !isProjectMode {
item.ConfigDir = config.AgentConfigDir(name)
}
items = append(items, item)
}
// Who reads each folder, so a shared folder (universal) can show the tools it serves.
present := func(it availTarget) bool { return it.Installed || it.Detected }
for i := range items {
folder := filepath.Clean(config.ExpandPath(items[i].Path))
for _, other := range items {
if other.Name == items[i].Name || !present(other) {
continue
}
for _, p := range config.RuntimeScanPaths(other.Name, isProjectMode) {
if filepath.Clean(config.ExpandPath(p)) == folder {
items[i].ReadBy = append(items[i].ReadBy, other.Name)
break
}
}
}
slices.Sort(items[i].ReadBy)
}
writeJSON(w, map[string]any{"targets": items})
}