Browse Source

feat: support list models

Yeuoly 8 months ago
parent
commit
2d24fc8835
2 changed files with 228 additions and 1 deletions
  1. 52 1
      cmd/commandline/plugin.go
  2. 176 0
      cmd/commandline/plugin/module.go

+ 52 - 1
cmd/commandline/plugin.go

@@ -45,7 +45,7 @@ var (
 	}
 
 	pluginChecksumCommand = &cobra.Command{
-		Use:   "checksum plugin_path",
+		Use:   "checksum [plugin_path]",
 		Short: "Checksum",
 		Long:  "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
 		Args:  cobra.ExactArgs(1),
@@ -55,6 +55,51 @@ var (
 		},
 	}
 
+	pluginModuleCommand = &cobra.Command{
+		Use:   "module",
+		Short: "Module",
+		Long:  "Module",
+	}
+
+	pluginModuleListCommand = &cobra.Command{
+		Use:   "list [plugin_path]",
+		Short: "List",
+		Long:  "List modules",
+		Args:  cobra.ExactArgs(1),
+		Run: func(cmd *cobra.Command, args []string) {
+			pluginPath := args[0]
+			plugin.ModuleList(pluginPath)
+		},
+	}
+
+	pluginModuleAppendCommand = &cobra.Command{
+		Use:   "append",
+		Short: "Append",
+		Long:  "Append",
+	}
+
+	pluginModuleAppendToolsCommand = &cobra.Command{
+		Use:   "tools [plugin_path]",
+		Short: "Tools",
+		Long:  "Append tools",
+		Args:  cobra.ExactArgs(1),
+		Run: func(cmd *cobra.Command, args []string) {
+			pluginPath := args[0]
+			plugin.ModuleAppendTools(pluginPath)
+		},
+	}
+
+	pluginModuleAppendEndpointsCommand = &cobra.Command{
+		Use:   "endpoints [plugin_path]",
+		Short: "Endpoints",
+		Long:  "Append endpoints",
+		Args:  cobra.ExactArgs(1),
+		Run: func(cmd *cobra.Command, args []string) {
+			pluginPath := args[0]
+			plugin.ModuleAppendEndpoints(pluginPath)
+		},
+	}
+
 	// NOTE: tester is deprecated, maybe, in several months, we will support this again
 	// pluginTestCommand = &cobra.Command{
 	// 	Use:   "test [-i inputs] [-t timeout] package_path invoke_type invoke_action",
@@ -105,6 +150,12 @@ func init() {
 	pluginCommand.AddCommand(pluginInitCommand)
 	pluginCommand.AddCommand(pluginPackageCommand)
 	pluginCommand.AddCommand(pluginChecksumCommand)
+	pluginCommand.AddCommand(pluginModuleCommand)
+	pluginModuleCommand.AddCommand(pluginModuleListCommand)
+	pluginModuleCommand.AddCommand(pluginModuleAppendCommand)
+	pluginModuleAppendCommand.AddCommand(pluginModuleAppendToolsCommand)
+	pluginModuleAppendCommand.AddCommand(pluginModuleAppendEndpointsCommand)
+
 	// pluginCommand.AddCommand(pluginTestCommand)
 	// pluginTestCommand.Flags().StringP("inputs", "i", "", "inputs")
 	// pluginTestCommand.Flags().StringP("timeout", "t", "", "timeout")

+ 176 - 0
cmd/commandline/plugin/module.go

@@ -0,0 +1,176 @@
+package plugin
+
+import (
+	"html/template"
+	"os"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+const TOOL_MODULE_TEMPLATE = `
+========== {{.Identity.Name}} ==========
+Author: {{.Identity.Author}}
+Label: {{.Identity.Label.EnUS}}
+Description: {{.Description.Human.EnUS}}
+Parameters:
+{{- range .Parameters}}
+  - Name: {{.Name}}
+    Type: {{.Type}}
+    Required: {{.Required}}
+    Description: {{.HumanDescription.EnUS}}
+    {{- if .Default}}
+    Default: {{.Default}}
+    {{- end}}
+    {{- if .Options}}
+    Options:
+      {{- range .Options}}
+      - Value: {{.Value}}
+        Label: {{.Label.EnUS}}
+      {{- end}}
+    {{- end}}
+{{- end}}
+`
+
+const MODEL_MODULE_TEMPLATE = `
+========== {{.Model}} ==========
+Name: {{.Model}}
+Type: {{.ModelType}}
+Label: {{.Label.EnUS}}
+Parameters:
+{{- range .ParameterRules}}
+  - Name: {{.Name}}
+    Type: {{.Type}}
+    Required: {{.Required}}
+    Description: {{.Help.EnUS}}
+    {{- if .Default}}
+    Default: {{.Default}}
+    {{- end}}
+    {{- if .Min}}
+    Min: {{.Min}}
+    {{- end}}
+    {{- if .Max}}
+    Max: {{.Max}}
+    {{- end}}
+    {{- if .Options}}
+    Options: {{range .Options}}{{.}}, {{end}}
+    {{- end}}
+{{- end}}
+`
+
+const ENDPOINT_MODULE_TEMPLATE = `
+========== Endpoints ==========
+Path: {{.Path}}
+Method: {{.Method}}
+`
+
+const PLUGIN_MODULE_TEMPLATE = `
+========== Plugin ==========
+Name: {{.Name}}
+Version: {{.Version}}
+Description: {{.Description.EnUS}}
+Author: {{.Author}}
+Icon: {{.Icon}}
+Tags: {{range .Tags}}{{.}}, {{end}}
+Category: {{.Category}}
+Resource:
+  Memory: {{.Resource.Memory}} bytes
+Permissions:
+  {{- if .Resource.Permission.Tool}}
+  Tool: {{.Resource.Permission.Tool.Enabled}}
+  {{- end}}
+  {{- if .Resource.Permission.Model}}
+  Model:
+    Enabled: {{.Resource.Permission.Model.Enabled}}
+    LLM: {{.Resource.Permission.Model.LLM}}
+    TextEmbedding: {{.Resource.Permission.Model.TextEmbedding}}
+    Rerank: {{.Resource.Permission.Model.Rerank}}
+    TTS: {{.Resource.Permission.Model.TTS}}
+    Speech2text: {{.Resource.Permission.Model.Speech2text}}
+    Moderation: {{.Resource.Permission.Model.Moderation}}
+  {{- end}}
+  {{- if .Resource.Permission.Node}}
+  Node: {{.Resource.Permission.Node.Enabled}}
+  {{- end}}
+  {{- if .Resource.Permission.Endpoint}}
+  Endpoint: {{.Resource.Permission.Endpoint.Enabled}}
+  {{- end}}
+  {{- if .Resource.Permission.App}}
+  App: {{.Resource.Permission.App.Enabled}}
+  {{- end}}
+  {{- if .Resource.Permission.Storage}}
+  Storage:
+    Enabled: {{.Resource.Permission.Storage.Enabled}}
+    Size: {{.Resource.Permission.Storage.Size}} bytes
+  {{- end}}
+`
+
+func ModuleList(pluginPath string) {
+	decoder, err := decoder.NewFSPluginDecoder(pluginPath)
+	if err != nil {
+		log.Error("your plugin is not a valid plugin: %s", err)
+		return
+	}
+
+	manifest, err := decoder.Manifest()
+	if err != nil {
+		log.Error("failed to get manifest: %s", err)
+		return
+	}
+
+	if manifest.Tool != nil {
+		for _, tool := range manifest.Tool.Tools {
+			tmpl, err := template.New("tool").Parse(TOOL_MODULE_TEMPLATE)
+			if err != nil {
+				log.Error("failed to parse template: %s", err)
+				return
+			}
+
+			err = tmpl.Execute(os.Stdout, tool)
+			if err != nil {
+				log.Error("failed to execute template: %s", err)
+				return
+			}
+		}
+	}
+
+	if manifest.Model != nil {
+		for _, model := range manifest.Model.Models {
+			tmpl, err := template.New("model").Parse(MODEL_MODULE_TEMPLATE)
+			if err != nil {
+				log.Error("failed to parse template: %s", err)
+				return
+			}
+
+			err = tmpl.Execute(os.Stdout, model)
+			if err != nil {
+				log.Error("failed to execute template: %s", err)
+				return
+			}
+		}
+	}
+
+	if manifest.Endpoint != nil {
+		for _, endpoint := range manifest.Endpoint.Endpoints {
+			tmpl, err := template.New("endpoint").Parse(ENDPOINT_MODULE_TEMPLATE)
+			if err != nil {
+				log.Error("failed to parse template: %s", err)
+				return
+			}
+
+			err = tmpl.Execute(os.Stdout, endpoint)
+			if err != nil {
+				log.Error("failed to execute template: %s", err)
+				return
+			}
+		}
+	}
+}
+
+func ModuleAppendTools(pluginPath string) {
+
+}
+
+func ModuleAppendEndpoints(pluginPath string) {
+
+}