Files
skillshare/internal/server/handler_instructions_setup.go
T
Willie b8291895e1 feat(ui): share one AGENTS.md across tools from the dashboard
Claude Code now reads AGENTS.md, but every tool still keeps its standing
instructions in its own file (CLAUDE.md, GEMINI.md, AGENTS.md), and Claude
has no user-level AGENTS.md at all. Keeping several tools on the same
instructions meant copying text between those files by hand.

Each target page gets a tab named after the file it reads, with its read
order, an editor, and a convert flow from CLAUDE.md to AGENTS.md. In global
mode the Extras page gets an AGENTS.md tab where several shared files can
coexist and each target picks one or keeps its own. Shared files are
single-file extras: tools that follow @imports get a managed import block
and keep their own lines, others get a link in place of their file.
Projects get small shims instead, since every tool there reads ./AGENTS.md.
Custom targets can declare which file they read.

Restore records the target's state when it is first attached (file,
symlink, or no file) and always returns to it. Edits made after attaching
are kept as drift backups instead of becoming the restore point, so sync,
reapply and mode switches can no longer lose the user's original file.
2026-09-27 03:02:51 +08:00

134 lines
4.4 KiB
Go

package server
import (
"encoding/json"
"net/http"
"path/filepath"
"strings"
"time"
"skillshare/internal/config"
"skillshare/internal/instructions"
)
// errInstructionsInUse is the error code for moving or removing a target's
// instruction file while shared instruction files are attached to it.
const errInstructionsInUse = "instructions_in_use"
// setTargetInstructionsConfig sets (ic != nil) or clears the user-set
// instruction file of a target in the config of the current mode. Callers
// must hold s.mu and save the config afterwards.
func (s *Server) setTargetInstructionsConfig(name string, ic *config.TargetInstructionsConfig) {
tc := s.cfg.Targets[name]
tc.Instructions = ic
s.cfg.Targets[name] = tc
if !s.IsProjectMode() {
return
}
for i := range s.projectCfg.Targets {
if s.projectCfg.Targets[i].Name == name {
s.projectCfg.Targets[i].Instructions = ic
return
}
}
}
// attachedShared returns the shared instruction files attached to the
// target's current instruction file. Callers must hold s.mu.
func (s *Server) attachedShared(name string) []instructions.Assignment {
it, _, ok := s.targetInstructions(name)
if !ok {
return nil
}
return instructions.Assignments(s.extrasConfig(), it.Path, s.instructionsResolver())
}
func writeInstructionsInUse(w http.ResponseWriter, name string) {
writeCodedError(w, http.StatusConflict, errInstructionsInUse,
name+" uses shared instruction files; switch it back to its own file first",
map[string]string{"target": name})
}
// handlePutTargetInstructionsSetup — PUT /api/targets/{name}/instructions/setup
// Tells skillshare which instruction file the target reads.
func (s *Server) handlePutTargetInstructionsSetup(w http.ResponseWriter, r *http.Request) {
start := time.Now()
name := r.PathValue("name")
var body struct {
Path string `json:"path"`
Import bool `json:"import"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, http.StatusBadRequest, "invalid JSON body")
return
}
ic := &config.TargetInstructionsConfig{Path: strings.TrimSpace(body.Path), Import: body.Import}
if err := config.ValidateTargetInstructions(ic, s.IsProjectMode()); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
s.mu.Lock()
defer s.mu.Unlock()
tc, found := s.cfg.Targets[name]
if !found {
writeError(w, http.StatusNotFound, "target not found: "+name)
return
}
if tc.ProjectRoot() != "" {
writeError(w, http.StatusBadRequest, name+" belongs to a project; edit the project instead")
return
}
// Shared files are attached to the current path; moving it would strand them.
old, _, hadFile := s.targetInstructions(name)
shared := s.attachedShared(name)
tc.Instructions = ic
next, _ := config.TargetInstructions(name, tc, s.IsProjectMode())
if hadFile && len(shared) > 0 && filepath.Clean(old.Path) != filepath.Clean(instructions.Resolve(next, s.projectRoot).Path) {
writeInstructionsInUse(w, name)
return
}
s.setTargetInstructionsConfig(name, ic)
args := map[string]any{"target": name, "path": ic.Path, "import": ic.Import, "scope": "ui"}
if err := s.saveAndReloadConfig(); err != nil {
s.writeOpsLog("instructions-setup", "error", start, args, err.Error())
writeError(w, http.StatusInternalServerError, err.Error())
return
}
s.writeOpsLog("instructions-setup", "ok", start, args, "")
writeJSON(w, map[string]any{"success": true})
}
// handleDeleteTargetInstructionsSetup — DELETE /api/targets/{name}/instructions/setup
// Forgets the user-set instruction file; the built-in one, if any, applies again.
func (s *Server) handleDeleteTargetInstructionsSetup(w http.ResponseWriter, r *http.Request) {
start := time.Now()
name := r.PathValue("name")
s.mu.Lock()
defer s.mu.Unlock()
tc, found := s.cfg.Targets[name]
if !found {
writeError(w, http.StatusNotFound, "target not found: "+name)
return
}
if tc.Instructions == nil {
writeJSON(w, map[string]any{"success": true})
return
}
if len(s.attachedShared(name)) > 0 {
writeInstructionsInUse(w, name)
return
}
s.setTargetInstructionsConfig(name, nil)
args := map[string]any{"target": name, "scope": "ui"}
if err := s.saveAndReloadConfig(); err != nil {
s.writeOpsLog("instructions-setup-remove", "error", start, args, err.Error())
writeError(w, http.StatusInternalServerError, err.Error())
return
}
s.writeOpsLog("instructions-setup-remove", "ok", start, args, "")
writeJSON(w, map[string]any{"success": true})
}