mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
A target declared with `agent` and `config_dir` is another config directory of a built-in Agent, such as a second Claude account. Its skills and agents paths are derived from the Agent's defaults at load time, so the config only records the directory. The same target name works for MCP (written in the Agent's format into the account's own file), for plugins (the Agent CLI runs with CLAUDE_CONFIG_DIR, CODEX_HOME or PI_CODING_AGENT_DIR) and for `mcp import --from`. Removing such a target warns when MCP config still names it instead of failing later at sync. The dashboard gains an "another account" mode in the add-target dialog and lists accounts next to the Agents on the MCP and plugin pages. Refs #289
170 lines
4.8 KiB
Go
170 lines
4.8 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/plugin"
|
|
)
|
|
|
|
func (s *Server) pluginService() *plugin.Service {
|
|
service := &plugin.Service{ConfigPath: s.configPath(), ProjectRoot: s.projectRoot, StateDir: config.StateDir()}
|
|
if !s.IsProjectMode() {
|
|
service.Accounts = pluginAccounts(s.cfg)
|
|
}
|
|
return service
|
|
}
|
|
|
|
// pluginAccounts are the configured targets that are another config directory of an
|
|
// Agent, such as a second Claude account. They are plugin targets of their own.
|
|
func pluginAccounts(cfg *config.Config) map[string]plugin.Account {
|
|
accounts := map[string]plugin.Account{}
|
|
for name, target := range cfg.Targets {
|
|
if target.Agent != "" && target.ConfigDir != "" {
|
|
accounts[name] = plugin.Account{Agent: target.Agent, Dir: target.ConfigDir}
|
|
}
|
|
}
|
|
return accounts
|
|
}
|
|
|
|
func (s *Server) requireLocalPlugin(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if !mcpRequestAllowed(r, s.addr) {
|
|
writeError(w, http.StatusForbidden, "Plugin management is available only from the local dashboard")
|
|
return
|
|
}
|
|
// Native downloads can exceed the dashboard's normal 30-second deadline.
|
|
// Limit this extension to the local plugin endpoints.
|
|
_ = http.NewResponseController(w).SetWriteDeadline(time.Now().Add(5 * time.Minute))
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
type pluginRequest struct {
|
|
Request plugin.Request `json:"request"`
|
|
Revision string `json:"revision,omitempty"`
|
|
}
|
|
|
|
func decodePluginRequest(w http.ResponseWriter, r *http.Request, v any) bool {
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024*1024)
|
|
d := json.NewDecoder(r.Body)
|
|
d.DisallowUnknownFields()
|
|
if d.Decode(v) != nil || d.Decode(new(any)) != io.EOF {
|
|
writeError(w, 400, "invalid plugin request")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) handlePluginList(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
// hosts=false skips the Agent CLIs: the page draws the plugin list from this answer
|
|
// and fills in the Agents when the full one arrives.
|
|
var inventory *plugin.Inventory
|
|
var err error
|
|
if r.URL.Query().Get("hosts") == "false" {
|
|
inventory, err = s.pluginService().Packages()
|
|
} else {
|
|
inventory, err = s.pluginService().Inventory(r.Context())
|
|
}
|
|
if err != nil {
|
|
writeError(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, inventory)
|
|
}
|
|
|
|
func (s *Server) handlePluginDiscover(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Source string `json:"source"`
|
|
SourceRef string `json:"sourceRef,omitempty"`
|
|
Entry string `json:"entry,omitempty"`
|
|
}
|
|
if !decodePluginRequest(w, r, &body) {
|
|
return
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
result, err := s.pluginService().Discover(r.Context(), body.Source, body.SourceRef, body.Entry)
|
|
if err != nil {
|
|
writeError(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, result)
|
|
}
|
|
|
|
func (s *Server) handlePluginPreview(w http.ResponseWriter, r *http.Request) {
|
|
var body pluginRequest
|
|
if !decodePluginRequest(w, r, &body) {
|
|
return
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
p, err := s.pluginService().Preview(r.Context(), body.Request)
|
|
if err != nil {
|
|
writeError(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, p)
|
|
}
|
|
|
|
func (s *Server) handlePluginApply(w http.ResponseWriter, r *http.Request) {
|
|
var body pluginRequest
|
|
if !decodePluginRequest(w, r, &body) {
|
|
return
|
|
}
|
|
if body.Revision == "" {
|
|
writeError(w, 400, "preview before applying plugin changes")
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
start := time.Now()
|
|
result, err := s.pluginService().Apply(r.Context(), body.Request, body.Revision)
|
|
status := "ok"
|
|
message := ""
|
|
if err != nil {
|
|
status = "error"
|
|
message = err.Error()
|
|
}
|
|
s.writeOpsLog("plugin "+body.Request.Action, status, start, map[string]any{"scope": "ui"}, "")
|
|
if reloadErr := s.reloadConfig(); reloadErr != nil && err == nil {
|
|
message = "Plugin operation completed, but configuration reload failed"
|
|
}
|
|
// Preserve all per-target outcomes even when only some targets succeed.
|
|
writeJSON(w, map[string]any{"result": result, "failure": message})
|
|
}
|
|
|
|
// handlePluginFiles lists the reviewed snapshot of a plugin; an imported one has none and lists nothing.
|
|
func (s *Server) handlePluginFiles(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
files, err := s.pluginService().Files(r.PathValue("name"))
|
|
if err != nil {
|
|
writeError(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{"files": files})
|
|
}
|
|
|
|
func (s *Server) handlePluginFile(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
data, err := s.pluginService().ReadFile(r.PathValue("name"), r.PathValue("filepath"))
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
writeError(w, http.StatusNotFound, "file not found: "+r.PathValue("filepath"))
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeError(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{"content": string(data)})
|
|
}
|