Browse Source

refactor: commandline

Yeuoly 9 months ago
parent
commit
b195b93ae3
42 changed files with 146 additions and 153 deletions
  1. 4 142
      cmd/commandline/plugin.go
  2. 1 1
      cmd/commandline/init/category.go
  3. 44 0
      cmd/commandline/plugin/checksum.go
  4. 1 1
      cmd/commandline/init/encoder.go
  5. 1 1
      cmd/commandline/init/init.go
  6. 1 1
      cmd/commandline/init/language.go
  7. 33 0
      cmd/commandline/plugin/package.go
  8. 1 1
      cmd/commandline/init/permission.go
  9. 1 1
      cmd/commandline/init/profile.go
  10. 1 1
      cmd/commandline/init/python.go
  11. 1 1
      cmd/commandline/init/python_categories.go
  12. 1 1
      cmd/commandline/init/render_template_test.go
  13. 1 1
      cmd/commandline/init/submenu.go
  14. 1 1
      cmd/commandline/init/template.go
  15. 0 0
      cmd/commandline/plugin/templates/.env.example
  16. 0 0
      cmd/commandline/plugin/templates/README.md
  17. 0 0
      cmd/commandline/plugin/templates/python/GUIDE.md
  18. 0 0
      cmd/commandline/plugin/templates/python/endpoint.py
  19. 0 0
      cmd/commandline/plugin/templates/python/endpoint.yaml
  20. 0 0
      cmd/commandline/plugin/templates/python/endpoint_group.yaml
  21. 0 0
      cmd/commandline/plugin/templates/python/icon.svg
  22. 0 0
      cmd/commandline/plugin/templates/python/llm.py
  23. 0 0
      cmd/commandline/plugin/templates/python/llm.yaml
  24. 0 0
      cmd/commandline/plugin/templates/python/main.py
  25. 0 0
      cmd/commandline/plugin/templates/python/model_provider.py
  26. 0 0
      cmd/commandline/plugin/templates/python/model_provider.yaml
  27. 0 0
      cmd/commandline/plugin/templates/python/moderation.py
  28. 0 0
      cmd/commandline/plugin/templates/python/moderation.yaml
  29. 0 0
      cmd/commandline/plugin/templates/python/requirements.txt
  30. 0 0
      cmd/commandline/plugin/templates/python/rerank.py
  31. 0 0
      cmd/commandline/plugin/templates/python/rerank.yaml
  32. 0 0
      cmd/commandline/plugin/templates/python/speech2text.py
  33. 0 0
      cmd/commandline/plugin/templates/python/speech2text.yaml
  34. 0 0
      cmd/commandline/plugin/templates/python/text-embedding.py
  35. 0 0
      cmd/commandline/plugin/templates/python/text-embedding.yaml
  36. 0 0
      cmd/commandline/plugin/templates/python/tool.py
  37. 0 0
      cmd/commandline/plugin/templates/python/tool.yaml
  38. 0 0
      cmd/commandline/plugin/templates/python/tool_provider.py
  39. 0 0
      cmd/commandline/plugin/templates/python/tool_provider.yaml
  40. 0 0
      cmd/commandline/plugin/templates/python/tts.py
  41. 0 0
      cmd/commandline/plugin/templates/python/tts.yaml
  42. 54 0
      cmd/commandline/plugin/test.go

+ 4 - 142
cmd/commandline/plugin.go

@@ -2,13 +2,9 @@ package main
 
 import (
 	"fmt"
-	"os"
 	"path/filepath"
 
-	initPkg "github.com/langgenius/dify-plugin-daemon/cmd/commandline/init"
-	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
-	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
-	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+	"github.com/langgenius/dify-plugin-daemon/cmd/commandline/plugin"
 	"github.com/spf13/cobra"
 )
 
@@ -18,7 +14,7 @@ var (
 		Short: "Init",
 		Long:  "Init",
 		Run: func(c *cobra.Command, args []string) {
-			initPkg.InitPlugin()
+			plugin.InitPlugin()
 		},
 	}
 
@@ -41,27 +37,7 @@ var (
 				outputPath = filepath.Base(inputPath) + ".difypkg"
 			}
 
-			decoder, err := decoder.NewFSPluginDecoder(inputPath)
-			if err != nil {
-				log.Error("failed to create plugin decoder , plugin path: %s, error: %v", inputPath, err)
-				return
-			}
-
-			packager := packager.NewPackager(decoder)
-			zipFile, err := packager.Pack()
-
-			if err != nil {
-				log.Error("failed to package plugin %v", err)
-				return
-			}
-
-			err = os.WriteFile(outputPath, zipFile, 0644)
-			if err != nil {
-				log.Error("failed to write package file %v", err)
-				return
-			}
-
-			log.Info("plugin packaged successfully, output path: %s", outputPath)
+			plugin.PackagePlugin(inputPath, outputPath)
 		},
 	}
 
@@ -76,71 +52,10 @@ var (
 			}
 
 			pluginPath := args[0]
-			var pluginDecoder decoder.PluginDecoder
-			if stat, err := os.Stat(pluginPath); err == nil {
-				if stat.IsDir() {
-					pluginDecoder, err = decoder.NewFSPluginDecoder(pluginPath)
-					if err != nil {
-						log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
-						return
-					}
-				} else {
-					bytes, err := os.ReadFile(pluginPath)
-					if err != nil {
-						log.Error("failed to read plugin file, plugin path: %s, error: %v", pluginPath, err)
-						return
-					}
-
-					pluginDecoder, err = decoder.NewZipPluginDecoder(bytes)
-					if err != nil {
-						log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
-						return
-					}
-				}
-			} else {
-				log.Error("failed to get plugin file info, plugin path: %s, error: %v", pluginPath, err)
-				return
-			}
-
-			checksum, err := pluginDecoder.Checksum()
-			if err != nil {
-				log.Error("failed to calculate checksum, plugin path: %s, error: %v", pluginPath, err)
-				return
-			}
-
-			log.Info("plugin checksum: %s", checksum)
+			plugin.CalculateChecksum(pluginPath)
 		},
 	}
 
-	pluginPermissionCommand = &cobra.Command{
-		Use:   "permission",
-		Short: "Permission",
-		Long: `Permission, available values: 
-tools					- allow plugin to call tools
-models					- allow plugin to call models
-models.llm				- allow plugin to call llm
-models.text_embedding			- allow plugin to call text_embedding model
-models.rerank				- allow plugin to call rerank model
-models.tts				- allow plugin to call tts
-models.speech2text			- allow plugin to call speech2text
-models.moderation			- allow plugin to call moderation
-apps					- allow plugin to call apps
-storage					- allow plugin to use storage
-endpoint				- allow plugin to register endpoint`,
-	}
-
-	pluginPermissionAddCommand = &cobra.Command{
-		Use:   "add permission",
-		Short: "",
-		Long:  "Add permission to plugin, you can find the available permission by running `dify plugin permission`",
-	}
-
-	pluginPermissionDropCommand = &cobra.Command{
-		Use:   "drop permission",
-		Short: "",
-		Long:  "Drop permission from plugin, you can find the available permission by running `dify plugin permission`",
-	}
-
 	// 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",
@@ -185,66 +100,13 @@ endpoint				- allow plugin to register endpoint`,
 	// 			timeout = cmd.Flag("timeout").Value.String()
 	// 		}
 
-	// 		// get invoke_type and invoke_action
-	// 		invoke_type := access_types.PluginAccessType(invoke_type_str)
-	// 		if !invoke_type.IsValid() {
-	// 			log.Error("invalid invoke type: %s", invoke_type_str)
-	// 			return
-	// 		}
-	// 		invoke_action := access_types.PluginAccessAction(invoke_action_str)
-	// 		if !invoke_action.IsValid() {
-	// 			log.Error("invalid invoke action: %s", invoke_action_str)
-	// 			return
-	// 		}
-
-	// 		// init routine pool
-	// 		routine.InitPool(1024)
-
-	// 		// clean working directory when test finished
-	// 		defer os.RemoveAll("./working")
-
-	// 		// init testing config
-	// 		config := &app.Config{
-	// 			PluginWorkingPath:    "./working/cwd",
-	// 			PluginStoragePath:    "./working/storage",
-	// 			PluginMediaCachePath: "./working/media_cache",
-	// 			ProcessCachingPath:   "./working/subprocesses",
-	// 			Platform:             app.PLATFORM_LOCAL,
-	// 		}
-	// 		config.SetDefault()
-
-	// 		// init oss
-	// 		oss := local.NewLocalStorage("./storage")
-
-	// 		// init plugin manager
-	// 		plugin_manager := plugin_manager.InitGlobalManager(oss, config)
-
-	// 		response, err := plugin_manager.TestPlugin(package_path_str, inputs, invoke_type, invoke_action, timeout)
-	// 		if err != nil {
-	// 			log.Error("failed to test plugin, package_path: %s, error: %v", package_path_str, err)
-	// 			return
-	// 		}
-
-	// 		for response.Next() {
-	// 			item, err := response.Read()
-	// 			if err != nil {
-	// 				log.Error("failed to read response item, error: %v", err)
-	// 				return
-	// 			}
-	// 			log.Info("%v", parser.MarshalJson(item))
-	// 		}
-	// 	},
-	// }
 )
 
 func init() {
 	pluginCommand.AddCommand(pluginInitCommand)
 	pluginCommand.AddCommand(pluginPackageCommand)
 	pluginCommand.AddCommand(pluginChecksumCommand)
-	pluginCommand.AddCommand(pluginPermissionCommand)
 	// pluginCommand.AddCommand(pluginTestCommand)
 	// pluginTestCommand.Flags().StringP("inputs", "i", "", "inputs")
 	// pluginTestCommand.Flags().StringP("timeout", "t", "", "timeout")
-	pluginPermissionCommand.AddCommand(pluginPermissionAddCommand)
-	pluginPermissionCommand.AddCommand(pluginPermissionDropCommand)
 }

+ 1 - 1
cmd/commandline/init/category.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 44 - 0
cmd/commandline/plugin/checksum.go

@@ -0,0 +1,44 @@
+package plugin
+
+import (
+	"os"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+func CalculateChecksum(pluginPath string) {
+	var pluginDecoder decoder.PluginDecoder
+	if stat, err := os.Stat(pluginPath); err == nil {
+		if stat.IsDir() {
+			pluginDecoder, err = decoder.NewFSPluginDecoder(pluginPath)
+			if err != nil {
+				log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
+				return
+			}
+		} else {
+			bytes, err := os.ReadFile(pluginPath)
+			if err != nil {
+				log.Error("failed to read plugin file, plugin path: %s, error: %v", pluginPath, err)
+				return
+			}
+
+			pluginDecoder, err = decoder.NewZipPluginDecoder(bytes)
+			if err != nil {
+				log.Error("failed to create plugin decoder, plugin path: %s, error: %v", pluginPath, err)
+				return
+			}
+		}
+	} else {
+		log.Error("failed to get plugin file info, plugin path: %s, error: %v", pluginPath, err)
+		return
+	}
+
+	checksum, err := pluginDecoder.Checksum()
+	if err != nil {
+		log.Error("failed to calculate checksum, plugin path: %s, error: %v", pluginPath, err)
+		return
+	}
+
+	log.Info("plugin checksum: %s", checksum)
+}

+ 1 - 1
cmd/commandline/init/encoder.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"bytes"

+ 1 - 1
cmd/commandline/init/init.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 1 - 1
cmd/commandline/init/language.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 33 - 0
cmd/commandline/plugin/package.go

@@ -0,0 +1,33 @@
+package plugin
+
+import (
+	"os"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
+	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+func PackagePlugin(inputPath string, outputPath string) {
+	decoder, err := decoder.NewFSPluginDecoder(inputPath)
+	if err != nil {
+		log.Error("failed to create plugin decoder , plugin path: %s, error: %v", inputPath, err)
+		return
+	}
+
+	packager := packager.NewPackager(decoder)
+	zipFile, err := packager.Pack()
+
+	if err != nil {
+		log.Error("failed to package plugin %v", err)
+		return
+	}
+
+	err = os.WriteFile(outputPath, zipFile, 0644)
+	if err != nil {
+		log.Error("failed to write package file %v", err)
+		return
+	}
+
+	log.Info("plugin packaged successfully, output path: %s", outputPath)
+}

+ 1 - 1
cmd/commandline/init/permission.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 1 - 1
cmd/commandline/init/profile.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 1 - 1
cmd/commandline/init/python.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"bytes"

+ 1 - 1
cmd/commandline/init/python_categories.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"fmt"

+ 1 - 1
cmd/commandline/init/render_template_test.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	"strings"

+ 1 - 1
cmd/commandline/init/submenu.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import tea "github.com/charmbracelet/bubbletea"
 

+ 1 - 1
cmd/commandline/init/template.go

@@ -1,4 +1,4 @@
-package init
+package plugin
 
 import (
 	_ "embed"

cmd/commandline/init/templates/.env.example → cmd/commandline/plugin/templates/.env.example


cmd/commandline/init/templates/README.md → cmd/commandline/plugin/templates/README.md


cmd/commandline/init/templates/python/GUIDE.md → cmd/commandline/plugin/templates/python/GUIDE.md


cmd/commandline/init/templates/python/endpoint.py → cmd/commandline/plugin/templates/python/endpoint.py


cmd/commandline/init/templates/python/endpoint.yaml → cmd/commandline/plugin/templates/python/endpoint.yaml


cmd/commandline/init/templates/python/endpoint_group.yaml → cmd/commandline/plugin/templates/python/endpoint_group.yaml


cmd/commandline/init/templates/python/icon.svg → cmd/commandline/plugin/templates/python/icon.svg


cmd/commandline/init/templates/python/llm.py → cmd/commandline/plugin/templates/python/llm.py


cmd/commandline/init/templates/python/llm.yaml → cmd/commandline/plugin/templates/python/llm.yaml


cmd/commandline/init/templates/python/main.py → cmd/commandline/plugin/templates/python/main.py


cmd/commandline/init/templates/python/model_provider.py → cmd/commandline/plugin/templates/python/model_provider.py


cmd/commandline/init/templates/python/model_provider.yaml → cmd/commandline/plugin/templates/python/model_provider.yaml


cmd/commandline/init/templates/python/moderation.py → cmd/commandline/plugin/templates/python/moderation.py


cmd/commandline/init/templates/python/moderation.yaml → cmd/commandline/plugin/templates/python/moderation.yaml


cmd/commandline/init/templates/python/requirements.txt → cmd/commandline/plugin/templates/python/requirements.txt


cmd/commandline/init/templates/python/rerank.py → cmd/commandline/plugin/templates/python/rerank.py


cmd/commandline/init/templates/python/rerank.yaml → cmd/commandline/plugin/templates/python/rerank.yaml


cmd/commandline/init/templates/python/speech2text.py → cmd/commandline/plugin/templates/python/speech2text.py


cmd/commandline/init/templates/python/speech2text.yaml → cmd/commandline/plugin/templates/python/speech2text.yaml


cmd/commandline/init/templates/python/text-embedding.py → cmd/commandline/plugin/templates/python/text-embedding.py


cmd/commandline/init/templates/python/text-embedding.yaml → cmd/commandline/plugin/templates/python/text-embedding.yaml


cmd/commandline/init/templates/python/tool.py → cmd/commandline/plugin/templates/python/tool.py


cmd/commandline/init/templates/python/tool.yaml → cmd/commandline/plugin/templates/python/tool.yaml


cmd/commandline/init/templates/python/tool_provider.py → cmd/commandline/plugin/templates/python/tool_provider.py


cmd/commandline/init/templates/python/tool_provider.yaml → cmd/commandline/plugin/templates/python/tool_provider.yaml


cmd/commandline/init/templates/python/tts.py → cmd/commandline/plugin/templates/python/tts.py


cmd/commandline/init/templates/python/tts.yaml → cmd/commandline/plugin/templates/python/tts.yaml


+ 54 - 0
cmd/commandline/plugin/test.go

@@ -0,0 +1,54 @@
+package plugin
+
+func TestPlugin(pluginPath string) {
+	// 		// get invoke_type and invoke_action
+	// 		invoke_type := access_types.PluginAccessType(invoke_type_str)
+	// 		if !invoke_type.IsValid() {
+	// 			log.Error("invalid invoke type: %s", invoke_type_str)
+	// 			return
+	// 		}
+	// 		invoke_action := access_types.PluginAccessAction(invoke_action_str)
+	// 		if !invoke_action.IsValid() {
+	// 			log.Error("invalid invoke action: %s", invoke_action_str)
+	// 			return
+	// 		}
+
+	// 		// init routine pool
+	// 		routine.InitPool(1024)
+
+	// 		// clean working directory when test finished
+	// 		defer os.RemoveAll("./working")
+
+	// 		// init testing config
+	// 		config := &app.Config{
+	// 			PluginWorkingPath:    "./working/cwd",
+	// 			PluginStoragePath:    "./working/storage",
+	// 			PluginMediaCachePath: "./working/media_cache",
+	// 			ProcessCachingPath:   "./working/subprocesses",
+	// 			Platform:             app.PLATFORM_LOCAL,
+	// 		}
+	// 		config.SetDefault()
+
+	// 		// init oss
+	// 		oss := local.NewLocalStorage("./storage")
+
+	// 		// init plugin manager
+	// 		plugin_manager := plugin_manager.InitGlobalManager(oss, config)
+
+	// 		response, err := plugin_manager.TestPlugin(package_path_str, inputs, invoke_type, invoke_action, timeout)
+	// 		if err != nil {
+	// 			log.Error("failed to test plugin, package_path: %s, error: %v", package_path_str, err)
+	// 			return
+	// 		}
+
+	// 		for response.Next() {
+	// 			item, err := response.Read()
+	// 			if err != nil {
+	// 				log.Error("failed to read response item, error: %v", err)
+	// 				return
+	// 			}
+	// 			log.Info("%v", parser.MarshalJson(item))
+	// 		}
+	// 	},
+	// }
+}