Files
skillshare/internal/server/handler_omp_extensions.go
Willie 97446744ff feat(omp): add native Oh My Pi support (#423)
* feat(omp): add native Oh My Pi support

Support OMP skills, instructions, MCP, code hooks and guarded extension selection using its verified native contracts rather than Pi aliases.

Add reviewed marketplace plugin installation, cache-preserving scoped removal and fresh-cache reinstall. Align Pi/OMP extension cards and localize inventory guidance while keeping native ownership and read-only constraints.

Include regression coverage, isolated E2E runbooks, documentation and verification policies. Native plugin updates and unverified runtime platforms remain guarded.

Refs #409

* fix(omp): guard runtime ownership and localize inventory guidance

Native install recursively replaces its runtime destination, and a missing cache manifest could leave the real runtime link installed after removal. Bind and recheck an absent reviewed destination, and recover removal names only from verified scope-local links.

Localize fixed backend reasons and warnings across dashboard locales while preserving paths and external diagnostics. Restore the reviewed source in the existing reinstall fixture and record real native verification.

* fix(omp): allow forgetting natively removed plugins

A different plugin can retain node_modules after native uninstall deletes this installation. When its cache, registration and known runtime state are absent, forget only the stale Skillshare binding instead of demanding runtime-name proof for unrelated files.

Extend the existing removal recovery test with a failing-before-fix case that verifies the binding is dropped and unrelated native lock bytes and plugin files are preserved. make check passes.
2026-10-05 20:36:39 +08:00

144 lines
4.2 KiB
Go

package server
import (
"errors"
"net/http"
"time"
"skillshare/internal/config"
"skillshare/internal/plugin"
)
// Callers hold s.mu; project targets always resolve from config metadata.
func (s *Server) ompExtensionsService(w http.ResponseWriter, name string) (*plugin.Service, string, bool) {
tc, found := s.cfg.Targets[name]
service := s.pluginService()
target := name
switch {
case !found:
case tc.ProjectRoot() != "":
if _, tool, _ := config.SplitProjectTarget(name); tool == "omp" {
service.ProjectRoot, service.Accounts, target = tc.ProjectRoot(), nil, "omp"
} else {
found = false
}
case name == "omp":
case !s.IsProjectMode() && tc.Agent == "omp" && tc.ConfigDir != "":
default:
found = false
}
if !found {
writeCodedError(w, http.StatusNotFound, "omp_extensions_not_omp", "not an OMP target: "+name, map[string]string{"target": name})
return nil, "", false
}
return service, target, true
}
func (s *Server) handleOMPExtensions(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()
name := r.PathValue("name")
service, target, ok := s.ompExtensionsService(w, name)
if !ok {
return
}
view, err := service.OMPExtensions(r.Context(), target)
if err != nil {
writeCodedError(w, http.StatusBadRequest, "omp_extensions_invalid", err.Error(), nil)
return
}
view.Target = name
for i := range view.Rows {
if view.Rows[i].HooksTarget != "" {
view.Rows[i].HooksTarget = name
}
}
writeJSON(w, view)
}
type ompExtensionsRequest struct {
Changes []struct {
Key string `json:"key"`
Enabled *bool `json:"enabled"`
} `json:"changes"`
Revision string `json:"revision"`
}
func decodeOMPExtensionsRequest(w http.ResponseWriter, r *http.Request) ([]plugin.OMPExtensionChange, string, bool) {
var body ompExtensionsRequest
if !decodePluginRequest(w, r, &body) {
return nil, "", false
}
if body.Revision == "" {
writeCodedError(w, http.StatusBadRequest, "omp_extensions_preview_first", "refresh the inventory and preview before applying changes", nil)
return nil, "", false
}
changes := make([]plugin.OMPExtensionChange, 0, len(body.Changes))
for _, c := range body.Changes {
if c.Enabled == nil {
writeCodedError(w, http.StatusBadRequest, "omp_extensions_invalid", "each change requires enabled as a boolean", nil)
return nil, "", false
}
changes = append(changes, plugin.OMPExtensionChange{Key: c.Key, Enabled: *c.Enabled})
}
return changes, body.Revision, true
}
func (s *Server) handleOMPExtensionsPreview(w http.ResponseWriter, r *http.Request) {
changes, revision, ok := decodeOMPExtensionsRequest(w, r)
if !ok {
return
}
s.mu.RLock()
defer s.mu.RUnlock()
service, target, ok := s.ompExtensionsService(w, r.PathValue("name"))
if !ok {
return
}
plan, err := service.PreviewOMPExtensions(r.Context(), target, changes, revision)
if err != nil {
writeOMPExtensionsError(w, err)
return
}
writeJSON(w, plan)
}
func (s *Server) handleOMPExtensionsApply(w http.ResponseWriter, r *http.Request) {
changes, revision, ok := decodeOMPExtensionsRequest(w, r)
if !ok {
return
}
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
service, target, ok := s.ompExtensionsService(w, name)
if !ok {
return
}
start := time.Now()
plan, err := service.ApplyOMPExtensions(r.Context(), target, changes, revision)
status, message := "ok", ""
if err != nil {
status, message = "error", err.Error()
}
s.writeOpsLog("omp-extensions", status, start, map[string]any{"scope": "ui", "target": name, "changes": len(changes)}, message)
if err != nil {
writeOMPExtensionsError(w, err)
return
}
writeJSON(w, plan)
}
func writeOMPExtensionsError(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, plugin.ErrOMPExtensionsStale):
writeCodedError(w, http.StatusConflict, "omp_extensions_stale", err.Error(), nil)
case errors.Is(err, plugin.ErrOMPExtensionsBusy):
writeCodedError(w, http.StatusConflict, "omp_extensions_busy", err.Error(), nil)
case errors.Is(err, plugin.ErrOMPExtensionsReadOnly):
writeCodedError(w, http.StatusForbidden, "omp_extensions_read_only", err.Error(), nil)
default:
writeCodedError(w, http.StatusBadRequest, "omp_extensions_invalid", err.Error(), nil)
}
}