plugin.go 4.4 KB

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