feat(platform): 插件协议系统 - 支持声明式渠道、工作流与参考媒体映射
This commit is contained in:
1 parent
054e3bbbff
commit
2f3a813ccb
58 files changed
+2074
-706
No files matched your search
@@ -3,6 +3,8 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
@@ -40,12 +42,76 @@ func agentProtocolAdapterForContext(ctx context.Context, id string) (protocol.Ag
|
||||
return agentAdapter, ok
|
||||
}
|
||||
|
||||
// ProtocolCatalog is the backend source of truth for the frontend plugin
|
||||
// center's protocol filtering.
|
||||
// includeUnavailable is reserved for administrators so incomplete plugins remain visible
|
||||
// with an explicit reason instead of silently becoming a selectable option.
|
||||
func (s *Service) ProtocolCatalog(scope, capability string, includeUnavailable bool) []protocol.Metadata {
|
||||
return s.protocolRegistry().List(protocol.Surface(strings.TrimSpace(scope)), protocol.Capability(strings.TrimSpace(capability)), includeUnavailable)
|
||||
type PluginProviderCatalogItem struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"version"`
|
||||
Name string `json:"name"`
|
||||
Vendor string `json:"vendor"`
|
||||
Categories []protocol.Capability `json:"categories"`
|
||||
Scopes []protocol.Surface `json:"scopes"`
|
||||
Create string `json:"create,omitempty"`
|
||||
Poll string `json:"poll,omitempty"`
|
||||
ContentType string `json:"contentType,omitempty"`
|
||||
BaseURL string `json:"baseUrl,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
UnavailableReason string `json:"unavailableReason,omitempty"`
|
||||
Workflows []protocol.ManifestWorkflow `json:"workflows,omitempty"`
|
||||
}
|
||||
|
||||
// PluginProviderCatalog projects provider and workflow contributions from the
|
||||
// unified plugin registry for channel and creation settings.
|
||||
func (s *Service) PluginProviderCatalog(scope, capability string, includeUnavailable bool) []PluginProviderCatalogItem {
|
||||
wantScope := protocol.Surface(strings.TrimSpace(scope))
|
||||
wantCapability := protocol.Capability(strings.TrimSpace(capability))
|
||||
items := make([]PluginProviderCatalogItem, 0)
|
||||
for _, plugin := range s.Plugins() {
|
||||
for _, provider := range plugin.Manifest.Contributes.Providers {
|
||||
if !containsPluginSurface(provider.Scopes, wantScope) || (wantCapability != "" && !containsPluginCapability(provider.Capabilities, wantCapability)) {
|
||||
continue
|
||||
}
|
||||
item := PluginProviderCatalogItem{ID: provider.ID, Version: plugin.Manifest.Version, Name: provider.Label, Vendor: plugin.Manifest.Author, Categories: provider.Capabilities, Scopes: provider.Scopes, BaseURL: provider.BaseURL, Enabled: plugin.Status == "enabled", UnavailableReason: plugin.Error, Workflows: workflowsForProvider(plugin.Manifest.Contributes.Workflows, provider.ID)}
|
||||
item.Create = operationSummary(provider.Create)
|
||||
if provider.Poll != nil {
|
||||
item.Poll = operationSummary(*provider.Poll)
|
||||
}
|
||||
item.ContentType = provider.Create.ContentType
|
||||
if includeUnavailable || item.Enabled {
|
||||
items = append(items, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func containsPluginSurface(items []protocol.Surface, want protocol.Surface) bool {
|
||||
for _, item := range items {
|
||||
if item == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func containsPluginCapability(items []protocol.Capability, want protocol.Capability) bool {
|
||||
for _, item := range items {
|
||||
if item == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func workflowsForProvider(items []protocol.ManifestWorkflow, providerID string) []protocol.ManifestWorkflow {
|
||||
result := make([]protocol.ManifestWorkflow, 0)
|
||||
for _, item := range items {
|
||||
if item.ProviderID == providerID {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func operationSummary(operation protocol.ManifestOperation) string {
|
||||
path := strings.ReplaceAll(operation.Path, "{{model}}", "{model}")
|
||||
path = strings.ReplaceAll(path, "{{taskId}}", "{task_id}")
|
||||
return strings.ToUpper(operation.Method) + " " + path
|
||||
}
|
||||
|
||||
func (s *Service) protocolRegistry() *protocol.Registry {
|
||||
@@ -113,11 +179,31 @@ func (s *Service) PluginsForUser(actor *model.User) ([]PluginView, error) {
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
func (s *Service) InstallPlugin(data []byte) (PluginView, error) {
|
||||
func (s *Service) InstallPlugin(data []byte, fileName string) (PluginView, error) {
|
||||
if s.pluginRuntime == nil {
|
||||
return PluginView{}, fmt.Errorf("插件运行时未初始化")
|
||||
}
|
||||
return s.pluginRuntime.install(data)
|
||||
return s.pluginRuntime.install(data, fileName)
|
||||
}
|
||||
|
||||
func (s *Service) PluginPackage(id string) ([]byte, string, error) {
|
||||
if s.pluginRuntime == nil {
|
||||
return nil, "", fmt.Errorf("插件运行时未初始化")
|
||||
}
|
||||
s.pluginRuntime.mu.RLock()
|
||||
record, ok := s.pluginRuntime.plugins[strings.TrimSpace(id)]
|
||||
s.pluginRuntime.mu.RUnlock()
|
||||
if !ok {
|
||||
return nil, "", fmt.Errorf("插件 %q 不存在", id)
|
||||
}
|
||||
if record.PackagePath == "" {
|
||||
return nil, "", fmt.Errorf("插件 %q 没有可下载的包文件", id)
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(s.pluginRuntime.packageDir, filepath.Base(record.PackagePath)))
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("读取插件包失败:%w", err)
|
||||
}
|
||||
return data, record.FileName, nil
|
||||
}
|
||||
|
||||
func (s *Service) SetPluginEnabled(id string, enabled bool) (PluginView, error) {
|
||||
|
||||
Reference in new issue
Block a user