plugin.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. init_pkg "github.com/langgenius/dify-plugin-daemon/cmd/commandline/init"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. "github.com/spf13/cobra"
  11. )
  12. var (
  13. pluginInitCommand = &cobra.Command{
  14. Use: "init",
  15. Short: "Init",
  16. Long: "Init",
  17. Run: func(c *cobra.Command, args []string) {
  18. init_pkg.InitPlugin()
  19. },
  20. }
  21. pluginPackageCommand = &cobra.Command{
  22. Use: "package plugin_path [-o output_path]",
  23. Short: "Package",
  24. Long: "Package plugins",
  25. Run: func(cmd *cobra.Command, args []string) {
  26. if len(args) < 1 {
  27. fmt.Println("Error: plugin_path is required")
  28. return
  29. }
  30. input_path := args[0]
  31. // using filename of input_path as output_path if not specified
  32. output_path := ""
  33. if cmd.Flag("output_path") != nil {
  34. output_path = cmd.Flag("output_path").Value.String()
  35. } else {
  36. output_path = filepath.Base(input_path) + ".difypkg"
  37. }
  38. decoder, err := decoder.NewFSPluginDecoder(input_path)
  39. if err != nil {
  40. log.Error("failed to create plugin decoder , plugin path: %s, error: %v", input_path, err)
  41. return
  42. }
  43. packager := packager.NewPackager(decoder)
  44. zip_file, err := packager.Pack()
  45. if err != nil {
  46. log.Error("failed to package plugin %v", err)
  47. return
  48. }
  49. err = os.WriteFile(output_path, zip_file, 0644)
  50. if err != nil {
  51. log.Error("failed to write package file %v", err)
  52. return
  53. }
  54. log.Info("plugin packaged successfully, output path: %s", output_path)
  55. },
  56. }
  57. pluginChecksumCommand = &cobra.Command{
  58. Use: "checksum plugin_path",
  59. Short: "Checksum",
  60. Long: "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
  61. Run: func(cmd *cobra.Command, args []string) {
  62. if len(args) < 1 {
  63. fmt.Println("Error: plugin_path is required")
  64. return
  65. }
  66. plugin_path := args[0]
  67. var plugin_decoder decoder.PluginDecoder
  68. if stat, err := os.Stat(plugin_path); err == nil {
  69. if stat.IsDir() {
  70. plugin_decoder, err = decoder.NewFSPluginDecoder(plugin_path)
  71. if err != nil {
  72. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
  73. return
  74. }
  75. } else {
  76. bytes, err := os.ReadFile(plugin_path)
  77. if err != nil {
  78. log.Error("failed to read plugin file, plugin path: %s, error: %v", plugin_path, err)
  79. return
  80. }
  81. plugin_decoder, err = decoder.NewZipPluginDecoder(bytes)
  82. if err != nil {
  83. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
  84. return
  85. }
  86. }
  87. } else {
  88. log.Error("failed to get plugin file info, plugin path: %s, error: %v", plugin_path, err)
  89. return
  90. }
  91. checksum, err := plugin_decoder.Checksum()
  92. if err != nil {
  93. log.Error("failed to calculate checksum, plugin path: %s, error: %v", plugin_path, err)
  94. return
  95. }
  96. log.Info("plugin checksum: %s", checksum)
  97. },
  98. }
  99. pluginPermissionCommand = &cobra.Command{
  100. Use: "permission",
  101. Short: "Permission",
  102. Long: `Permission, available values:
  103. tools - allow plugin to call tools
  104. models - allow plugin to call models
  105. models.llm - allow plugin to call llm
  106. models.text_embedding - allow plugin to call text_embedding model
  107. models.rerank - allow plugin to call rerank model
  108. models.tts - allow plugin to call tts
  109. models.speech2text - allow plugin to call speech2text
  110. models.moderation - allow plugin to call moderation
  111. apps - allow plugin to call apps
  112. storage - allow plugin to use storage
  113. endpoint - allow plugin to register endpoint`,
  114. }
  115. pluginPermissionAddCommand = &cobra.Command{
  116. Use: "add permission",
  117. Short: "",
  118. Long: "Add permission to plugin, you can find the available permission by running `dify plugin permission`",
  119. }
  120. pluginPermissionDropCommand = &cobra.Command{
  121. Use: "drop permission",
  122. Short: "",
  123. Long: "Drop permission from plugin, you can find the available permission by running `dify plugin permission`",
  124. }
  125. )
  126. func init() {
  127. pluginCommand.AddCommand(pluginInitCommand)
  128. pluginCommand.AddCommand(pluginPackageCommand)
  129. pluginCommand.AddCommand(pluginChecksumCommand)
  130. pluginCommand.AddCommand(pluginPermissionCommand)
  131. pluginPermissionCommand.AddCommand(pluginPermissionAddCommand)
  132. pluginPermissionCommand.AddCommand(pluginPermissionDropCommand)
  133. }