Files
skillshare/internal/plugin/model.go
T
Willie e0c1111b79 feat(plugins): add a plugin without choosing Agents yet
Adding a plugin forced at least one target, so keeping a plugin in
Skillshare for later meant installing it somewhere and then deselecting
every Agent by hand.

A package now records its source, so it stays managed with no Agent
bound. Agents are bound later from the same source, installing it as it
is then. Removing the last Agent keeps such a plugin; removing the
plugin itself drops it. In the terminal picker, confirming with nothing
selected records it, while Esc still cancels.

The plugin list also shows the installed version as a tag, and an
update check shows old -> new for plugins whose source has moved on.
2026-09-21 17:18:03 +08:00

238 lines
8.5 KiB
Go

// Package plugin manages complete plugins through their native host CLIs.
// Plugin components are never published as standalone skills or MCP entries.
package plugin
import (
"context"
"encoding/json"
"errors"
"slices"
)
var Targets = func() []string {
var targets []string
for _, d := range TargetDefinitions() {
targets = append(targets, d.Target)
}
return targets
}()
type Binding struct {
Entry string `json:"entry,omitempty" yaml:"entry,omitempty"`
SourceRef string `json:"sourceRef,omitempty" yaml:"source_ref,omitempty"`
Commit string `json:"commit,omitempty" yaml:"commit,omitempty"`
Components []string `json:"components,omitempty" yaml:"components,omitempty"`
Sync *bool `json:"sync,omitempty" yaml:"sync,omitempty"`
ID string `json:"id" yaml:"id"`
Source string `json:"source,omitempty" yaml:"source,omitempty"`
Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"`
Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Pending string `json:"pending,omitempty" yaml:"pending,omitempty"`
}
// Source, SourceRef, Plugin and Entry keep a package managed while no Agent is bound, so
// Agents can be bound later from the same source.
type Package struct {
Source string `json:"source,omitempty" yaml:"source,omitempty"`
SourceRef string `json:"sourceRef,omitempty" yaml:"source_ref,omitempty"`
Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"`
Entry string `json:"entry,omitempty" yaml:"entry,omitempty"`
Bindings map[string]Binding `json:"bindings" yaml:"bindings"`
}
type Request struct {
Entry string `json:"entry,omitempty"`
SourceRef string `json:"sourceRef,omitempty"`
Action string `json:"action"`
Name string `json:"name,omitempty"`
Source string `json:"source,omitempty"`
Plugin string `json:"plugin,omitempty"`
Targets []string `json:"targets,omitempty"`
From string `json:"from,omitempty"`
}
// ProblemKey names the sentence the dashboard can translate and ProblemArgs fill its
// {placeholders}. Problem stays the English the CLI prints, and is the fallback.
type TargetPackage struct {
Manifest string `json:"manifest"`
// Path is set when this Agent's catalog points at its own folder; empty means Candidate.Path.
Path string `json:"path,omitempty"`
Version string `json:"version,omitempty"`
Entry string `json:"entry,omitempty"`
Components []string `json:"components"`
Problem string `json:"problem,omitempty"`
ProblemKey string `json:"problemKey,omitempty"`
ProblemArgs map[string]string `json:"problemArgs,omitempty"`
}
// block records why this Agent cannot take the package.
func (p *TargetPackage) block(key, message string, args map[string]string) {
p.Problem, p.ProblemKey, p.ProblemArgs = message, key, args
}
type Candidate struct {
TargetInfo map[string]TargetPackage `json:"targetInfo,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Marketplace string `json:"marketplace,omitempty"`
Path string `json:"path"`
Entry string `json:"entry,omitempty"`
Targets []string `json:"targets"`
Components []string `json:"components"`
Problem string `json:"problem,omitempty"`
ProblemKey string `json:"problemKey,omitempty"`
ProblemArgs map[string]string `json:"problemArgs,omitempty"`
// catalogEntry keeps the component fields a Claude catalog entry defines itself, for the
// catalog Skillshare writes at install time (strict: false plugins have nothing else).
catalogEntry map[string]json.RawMessage
}
// pathFor is the folder, relative to the source, that target installs from.
func (c Candidate) pathFor(target string) string {
if p := c.TargetInfo[target].Path; p != "" {
return p
}
return c.Path
}
// collectTargets derives Targets, Components and the OpenCode entry from TargetInfo.
func (c *Candidate) collectTargets() {
c.Targets, c.Components = []string{}, []string{}
for target, info := range c.TargetInfo {
if info.Problem != "" {
continue
}
c.Targets = append(c.Targets, target)
for _, component := range info.Components {
if !slices.Contains(c.Components, component) {
c.Components = append(c.Components, component)
}
}
if target == "opencode" {
c.Entry = info.Entry
}
}
slices.Sort(c.Targets)
slices.Sort(c.Components)
}
// block records why this package cannot be installed from the source.
func (c *Candidate) block(key, message string, args map[string]string) {
c.Problem, c.ProblemKey, c.ProblemArgs = message, key, args
}
type Discovery struct {
Warnings []string `json:"warnings,omitempty"`
TargetDefinitions []TargetDefinition `json:"targetDefinitions"`
SourceRef string `json:"sourceRef,omitempty"`
Commit string `json:"commit,omitempty"`
Source string `json:"source"`
Digest string `json:"digest"`
Candidates []Candidate `json:"candidates"`
}
type Installed struct {
ID string `json:"id,omitempty"`
PluginID string `json:"pluginId,omitempty"`
Name string `json:"name,omitempty"`
Marketplace string `json:"marketplaceName,omitempty"`
Version string `json:"version,omitempty"`
Scope string `json:"scope,omitempty"`
ProjectPath string `json:"projectPath,omitempty"`
EnabledKnown bool `json:"enabledKnown"`
Enabled bool `json:"enabled"`
Installed bool `json:"installed"`
Filtered bool `json:"filtered,omitempty"`
}
// What a caller would do about a host, so the dashboard can group Agents by the
// remedy instead of reading the English message back out of Error.
const (
HostReady = "ready" // usable
HostMissing = "missing" // its CLI is not installed or not on PATH here
HostBlocked = "blocked" // reachable, but the Agent itself has to be dealt with
)
// NoteKey and ErrorKey name the fixed sentences the dashboard can translate. Note and
// Error stay the English the CLI prints, and are the fallback when a locale lacks the key.
// A key is absent whenever the message is assembled at runtime.
type Host struct {
Target string `json:"target"`
Fingerprint string `json:"fingerprint,omitempty"`
Note string `json:"note,omitempty"`
NoteKey string `json:"noteKey,omitempty"`
Version string `json:"version"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
ErrorKey string `json:"errorKey,omitempty"`
Installed []Installed `json:"installed"`
}
// fail records a failure and buckets it by its cause.
func (h *Host) fail(err error) {
h.Error = err.Error()
h.ErrorKey = ""
h.Status = HostBlocked
var keyed agentError
if errors.As(err, &keyed) {
h.ErrorKey = keyed.key
}
if errors.Is(err, ErrCLIMissing) {
h.Status = HostMissing
}
}
// block records a failure the caller resolves in the Agent, not by installing anything.
// key names the sentence for the dashboard; message is the English the CLI prints.
func (h *Host) block(key, message string) {
h.Error = message
h.ErrorKey = key
h.Status = HostBlocked
}
type Inventory struct {
TargetDefinitions []TargetDefinition `json:"targetDefinitions"`
Packages map[string]Package `json:"packages"`
Hosts []Host `json:"hosts"`
}
type Change struct {
Name string `json:"name"`
Target string `json:"target"`
ID string `json:"id"`
Action string `json:"action"`
Message string `json:"message,omitempty"`
Binding Binding `json:"binding"`
Components []string `json:"components,omitempty"`
}
type Plan struct {
Revision string `json:"revision"`
Blocked bool `json:"blocked"`
Changes []Change `json:"changes"`
}
type Outcome struct {
Name string `json:"name"`
Target string `json:"target"`
Status string `json:"status"`
Message string `json:"message,omitempty"`
}
type Result struct {
Results []Outcome `json:"results"`
}
type Runner func(context.Context, string, string, ...string) ([]byte, error)
type Service struct {
ConfigPath string
StateDir string
ProjectRoot string
Run Runner
}
func (b Binding) Selected() bool { return b.Sync == nil || *b.Sync }