plugin.go 4.5 KB

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