Files
skillshare/internal/server/handler_instructions_project.go
T
Willie 7baf716c63 feat(instructions): translate dashboard warnings and errors
Mode changes, attach and the other AGENTS.md actions returned English
sentences, so a zh-TW dashboard showed "backed up ... before replacing
it" and "... is a directory" untranslated. Warnings are now
{code, params, message} and every instructions error carries a code,
so the dashboard shows them from i18n and falls back to the English
message for unknown codes. CLI output is unchanged.

Also from the Windows re-test:
- replacing a user's junction says so and where it pointed;
- status, doctor and sync agree on identical unowned copies (kept, not
  linked), and an identical copy no longer suggests --force;
- the targets list shows a Developer Mode tooltip when file links are
  unavailable;
- file names are taken from Windows paths too.

Refs #299
2026-09-28 16:12:59 +08:00

121 lines
4.7 KiB
Go

package server
import (
"encoding/json"
"net/http"
"os"
"path/filepath"
"sort"
"time"
"skillshare/internal/config"
"skillshare/internal/instructions"
)
// In project mode every target reads the one ./AGENTS.md, so there is nothing
// to share or sync; a few targets need a small file (a shim) to reach it.
func (s *Server) requireProjectInstructions(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !s.IsProjectMode() {
writeCodedError(w, http.StatusBadRequest, "instructions_project_required", "the project AGENTS.md is available in project mode", map[string]string{})
return
}
next(w, r)
}
}
// projectReaches reports how each configured target reaches ./AGENTS.md.
// Callers must hold s.mu.
func (s *Server) projectReaches() []instructions.Reach {
names := make([]string, 0, len(s.cfg.Targets))
for name := range s.cfg.Targets {
names = append(names, name)
}
sort.Strings(names)
out := []instructions.Reach{}
for _, name := range names {
if it, ok := config.TargetInstructions(name, s.cfg.Targets[name], true); ok {
out = append(out, instructions.ProjectReach(s.projectRoot, name, it))
}
}
return out
}
// handleGetProjectInstructions — GET /api/instructions/project
func (s *Server) handleGetProjectInstructions(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()
path := filepath.Join(s.projectRoot, instructions.AgentsFile)
resp := map[string]any{"path": path, "exists": false, "content": "", "size": 0, "targets": s.projectReaches()}
if info, err := os.Stat(path); err == nil && !info.IsDir() {
data, err := readLimited(path)
if err != nil {
writeCodedError(w, http.StatusInternalServerError, "instructions_read_failed", err.Error(), map[string]string{"detail": err.Error()})
return
}
resp["exists"], resp["content"], resp["size"] = true, string(data), info.Size()
}
writeJSON(w, resp)
}
// handlePutProjectInstructions — PUT /api/instructions/project
func (s *Server) handlePutProjectInstructions(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var body struct {
Content string `json:"content"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxInstructionsBytes+4096)).Decode(&body); err != nil {
writeCodedError(w, http.StatusBadRequest, "instructions_invalid_json", "invalid JSON body", map[string]string{})
return
}
s.mu.Lock()
defer s.mu.Unlock()
path := filepath.Join(s.projectRoot, instructions.AgentsFile)
if err := writeInstructionsFile(path, body.Content); err != nil {
s.writeOpsLog("instructions-edit", "error", start, map[string]any{"path": path, "scope": "ui"}, err.Error())
writeCodedError(w, http.StatusInternalServerError, "instructions_write_failed", err.Error(), map[string]string{"detail": err.Error()})
return
}
s.writeOpsLog("instructions-edit", "ok", start, map[string]any{"path": path, "scope": "ui"}, "")
writeJSON(w, map[string]any{"success": true, "path": path})
}
// handleProjectInstructionsShim — POST /api/instructions/project/shim
// Adds the shim a target needs to read ./AGENTS.md.
func (s *Server) handleProjectInstructionsShim(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var body struct {
Target string `json:"target"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Target == "" {
writeCodedError(w, http.StatusBadRequest, "instructions_target_required", "target is required", map[string]string{})
return
}
s.mu.Lock()
defer s.mu.Unlock()
tc, found := s.cfg.Targets[body.Target]
if !found {
writeCodedError(w, http.StatusNotFound, "instructions_target_not_found", "target not found: "+body.Target, map[string]string{"target": body.Target})
return
}
it, ok := config.TargetInstructions(body.Target, tc, true)
if !ok {
writeCodedError(w, http.StatusBadRequest, "instructions_no_file", body.Target+" has no instruction file", map[string]string{"target": body.Target})
return
}
reach := instructions.ProjectReach(s.projectRoot, body.Target, it)
if reach.Shim == "" {
writeCodedError(w, http.StatusBadRequest, "instructions_no_change_needed", body.Target+" needs no change to read "+instructions.AgentsFile, map[string]string{"target": body.Target})
return
}
args := map[string]any{"target": body.Target, "shim": reach.Shim, "scope": "ui"}
if err := instructions.ApplyShim(s.projectRoot, it, reach.Shim); err != nil {
s.writeOpsLog("instructions-shim", "error", start, args, err.Error())
writeCodedError(w, http.StatusInternalServerError, "instructions_write_failed", err.Error(), map[string]string{"detail": err.Error()})
return
}
s.writeOpsLog("instructions-shim", "ok", start, args, "")
writeJSON(w, map[string]any{"success": true, "target": instructions.ProjectReach(s.projectRoot, body.Target, it)})
}