Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions internal/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ type HookExtension struct {
Args []string `json:"args"`
}

// PluginAuthor is optional manifest authorship metadata. All fields are
// best-effort; missing values stay empty.
type PluginAuthor struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
URL string `json:"url,omitempty"`
}

// PluginInterface is optional presentation metadata describing how a plugin
// surfaces in a UI. All fields are optional and default to empty.
type PluginInterface struct {
DisplayName string `json:"displayName,omitempty"`
Category string `json:"category,omitempty"`
BrandColor string `json:"brandColor,omitempty"`
DefaultPrompts []string `json:"defaultPrompts,omitempty"`
}

type LoadedPlugin struct {
SchemaVersion int `json:"schemaVersion"`
ID string `json:"id"`
Expand All @@ -96,6 +113,15 @@ type LoadedPlugin struct {
Prompts []PathExtension `json:"prompts"`
Skills []PathExtension `json:"skills"`
Hooks []HookExtension `json:"hooks"`
// Optional, additive metadata (omitempty so existing plugins are unchanged).
// Author/Interface are pointers: a non-pointer struct is never "empty" to
// encoding/json, so omitempty would still emit `author:{}` / `interface:{}`
// and change the serialized form of plugins that don't set them.
Author *PluginAuthor `json:"author,omitempty"`
License string `json:"license,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Homepage string `json:"homepage,omitempty"`
Interface *PluginInterface `json:"interface,omitempty"`
}

type LoadResult struct {
Expand Down Expand Up @@ -300,6 +326,11 @@ func ParseManifest(raw any, options ParseManifestOptions) (LoadedPlugin, error)
return LoadedPlugin{}, err
}

// NOTE: the tools/prompts/skills/hooks extensions parsed below are validated
// for DISCOVERY only (the `zero plugins` listing + backend snapshots). They
// are not yet registered into the tool registry / hook dispatcher / skills
// loader at runtime — that wiring is tracked as a separate feature, so these
// parsed-but-unconsumed fields are intentional, not dead code.
tools, err := parseTools(obj["tools"], options.AllowManifestToolAutoApproval)
if err != nil {
return LoadedPlugin{}, err
Expand Down Expand Up @@ -332,9 +363,91 @@ func ParseManifest(raw any, options ParseManifestOptions) (LoadedPlugin, error)
Prompts: prompts,
Skills: skills,
Hooks: hooks,
Author: parseAuthor(obj["author"]),
License: optionalMetaString(obj["license"]),
Keywords: coerceMetaStringSlice(obj["keywords"]),
Homepage: optionalMetaString(obj["homepage"]),
Interface: parseInterface(obj["interface"]),
}, nil
}

// parseAuthor reads optional authorship metadata. It is intentionally tolerant:
// an absent, non-object, or partially-typed value yields zero/empty fields so it
// never fails an otherwise-valid manifest.
func parseAuthor(raw any) *PluginAuthor {
obj, ok := raw.(map[string]any)
if !ok {
return nil
}
author := PluginAuthor{
Name: optionalMetaString(obj["name"]),
Email: optionalMetaString(obj["email"]),
URL: optionalMetaString(obj["url"]),
}
if author.Name == "" && author.Email == "" && author.URL == "" {
return nil
}
return &author
}

// parseInterface reads optional presentation metadata, tolerating missing keys.
func parseInterface(raw any) *PluginInterface {
obj, ok := raw.(map[string]any)
if !ok {
return nil
}
iface := PluginInterface{
DisplayName: optionalMetaString(obj["displayName"]),
Category: optionalMetaString(obj["category"]),
BrandColor: optionalMetaString(obj["brandColor"]),
DefaultPrompts: coerceMetaStringSlice(firstNonNil(obj["defaultPrompts"], obj["defaultPrompt"])),
}
if iface.DisplayName == "" && iface.Category == "" && iface.BrandColor == "" && len(iface.DefaultPrompts) == 0 {
return nil
}
return &iface
}

// optionalMetaString returns a trimmed string for additive metadata fields, or
// "" for anything that is not a usable string. It never errors — optional
// metadata must not fail validation of an otherwise-valid manifest.
func optionalMetaString(raw any) string {
if text, ok := raw.(string); ok {
return strings.TrimSpace(text)
}
return ""
}

// coerceMetaStringSlice extracts a []string from a JSON array of strings,
// silently dropping non-string entries and returning nil for non-arrays.
func coerceMetaStringSlice(raw any) []string {
items, ok := raw.([]any)
if !ok {
return nil
}
values := make([]string, 0, len(items))
for _, item := range items {
if text, ok := item.(string); ok {
if trimmed := strings.TrimSpace(text); trimmed != "" {
values = append(values, trimmed)
}
}
}
if len(values) == 0 {
return nil
}
return values
}

func firstNonNil(values ...any) any {
for _, value := range values {
if value != nil {
return value
}
}
return nil
}

func FormatList(plugins []LoadedPlugin, diagnostics []Diagnostic) string {
lines := []string{}
if len(plugins) == 0 {
Expand All @@ -348,6 +461,9 @@ func FormatList(plugins []LoadedPlugin, diagnostics []Diagnostic) string {
state = "disabled"
}
lines = append(lines, fmt.Sprintf(" %s@%s %s [%s] %s - %s", plugin.ID, plugin.Version, plugin.Name, plugin.Source, state, counts))
for _, meta := range formatPluginMetadata(plugin) {
lines = append(lines, " "+meta)
}
}
}

Expand All @@ -364,6 +480,32 @@ func FormatList(plugins []LoadedPlugin, diagnostics []Diagnostic) string {
return strings.Join(lines, "\n")
}

// formatPluginMetadata renders only the optional metadata fields that are
// present, one entry per line, so listings stay clean for plugins that omit them.
func formatPluginMetadata(plugin LoadedPlugin) []string {
meta := []string{}
if author := formatAuthor(plugin.Author); author != "" {
meta = append(meta, "author: "+author)
}
if plugin.License != "" {
meta = append(meta, "license: "+plugin.License)
}
if len(plugin.Keywords) > 0 {
meta = append(meta, "keywords: "+strings.Join(plugin.Keywords, ", "))
}
return meta
}

func formatAuthor(author *PluginAuthor) string {
if author == nil || author.Name == "" {
return ""
}
if author.Email != "" {
return fmt.Sprintf("%s <%s>", author.Name, author.Email)
}
return author.Name
}

func formatDiagnosticLocation(diagnostic Diagnostic) string {
parts := []string{}
if diagnostic.ManifestPath != "" {
Expand Down
105 changes: 105 additions & 0 deletions internal/plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,111 @@ func TestParseManifestNormalizesExtensionsAndPaths(t *testing.T) {
}
}

func TestParseManifestReadsOptionalMetadata(t *testing.T) {
root := filepath.Join(t.TempDir(), "plugins")
pluginDir := filepath.Join(root, "zero-demo")
manifestPath := filepath.Join(pluginDir, "plugin.json")

plugin, err := ParseManifest(map[string]any{
"schemaVersion": float64(1),
"id": "zero.demo",
"name": "Zero Demo",
"version": "0.1.0",
"author": map[string]any{
"name": "OpenAI",
"email": "support@openai.com",
"url": "https://openai.com/",
},
"license": "Proprietary",
"keywords": []any{"automation", "macos"},
"homepage": "https://openai.com/",
"interface": map[string]any{
"displayName": "Computer Use",
"category": "Productivity",
"brandColor": "#0F172A",
"defaultPrompt": []any{"Play a playlist", "Build my project"},
},
}, ParseManifestOptions{
Source: SourceProject,
Root: root,
PluginDir: pluginDir,
ManifestPath: manifestPath,
})
if err != nil {
t.Fatalf("ParseManifest returned error: %v", err)
}
if plugin.Author.Name != "OpenAI" || plugin.Author.Email != "support@openai.com" || plugin.Author.URL != "https://openai.com/" {
t.Fatalf("author = %#v", plugin.Author)
}
if plugin.License != "Proprietary" {
t.Fatalf("license = %q", plugin.License)
}
if len(plugin.Keywords) != 2 || plugin.Keywords[0] != "automation" {
t.Fatalf("keywords = %#v", plugin.Keywords)
}
if plugin.Homepage != "https://openai.com/" {
t.Fatalf("homepage = %q", plugin.Homepage)
}
if plugin.Interface.DisplayName != "Computer Use" || plugin.Interface.Category != "Productivity" || plugin.Interface.BrandColor != "#0F172A" {
t.Fatalf("interface = %#v", plugin.Interface)
}
if len(plugin.Interface.DefaultPrompts) != 2 || plugin.Interface.DefaultPrompts[0] != "Play a playlist" {
t.Fatalf("default prompts = %#v", plugin.Interface.DefaultPrompts)
}
}

func TestParseManifestWithoutOptionalMetadataLeavesZeroValues(t *testing.T) {
root := filepath.Join(t.TempDir(), "plugins")
pluginDir := filepath.Join(root, "zero-bare")
manifestPath := filepath.Join(pluginDir, "plugin.json")

plugin, err := ParseManifest(map[string]any{
"schemaVersion": float64(1),
"id": "zero.bare",
"name": "Bare",
"version": "0.1.0",
}, ParseManifestOptions{
Source: SourceUser,
Root: root,
PluginDir: pluginDir,
ManifestPath: manifestPath,
})
if err != nil {
t.Fatalf("ParseManifest returned error: %v", err)
}
if plugin.Author != nil {
t.Fatalf("author should be nil when absent, got %#v", plugin.Author)
}
if plugin.License != "" || plugin.Homepage != "" {
t.Fatalf("license/homepage should be empty, got %q / %q", plugin.License, plugin.Homepage)
}
if len(plugin.Keywords) != 0 {
t.Fatalf("keywords should be empty, got %#v", plugin.Keywords)
}
if plugin.Interface != nil {
t.Fatalf("interface should be nil when absent, got %#v", plugin.Interface)
}
}

func TestFormatListSurfacesOptionalMetadata(t *testing.T) {
output := FormatList([]LoadedPlugin{{
SchemaVersion: 1,
ID: "zero.demo",
Name: "Zero Demo",
Version: "0.1.0",
Enabled: true,
Source: SourceUser,
Author: &PluginAuthor{Name: "OpenAI"},
License: "MIT",
Keywords: []string{"automation", "macos"},
}}, nil)
for _, want := range []string{"author: OpenAI", "license: MIT", "keywords: automation, macos"} {
if !strings.Contains(output, want) {
t.Fatalf("FormatList output missing %q:\n%s", want, output)
}
}
}

func TestParseManifestClampsAutoApprovalByDefault(t *testing.T) {
root := t.TempDir()
pluginDir := filepath.Join(root, "zero-demo")
Expand Down
Loading
Loading