plugin.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/decoder"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  9. "github.com/spf13/cobra"
  10. )
  11. var (
  12. pluginInitCommand = &cobra.Command{
  13. Use: "init",
  14. Short: "Init",
  15. Long: "Init",
  16. Run: func(c *cobra.Command, args []string) {
  17. init_pkg.InitPlugin()
  18. },
  19. }
  20. pluginPackageCommand = &cobra.Command{
  21. Use: "package plugin_path [-o output_path]",
  22. Short: "Package",
  23. Long: "Package plugins",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. if len(args) < 1 {
  26. fmt.Println("Error: plugin_path is required")
  27. return
  28. }
  29. output_path := "./plugin.difypkg"
  30. if cmd.Flag("output_path") != nil {
  31. output_path = cmd.Flag("output_path").Value.String()
  32. }
  33. decoder, err := decoder.NewFSPluginDecoder(args[0])
  34. if err != nil {
  35. log.Error("failed to create plugin decoder , plugin path: %s, error: %v", args[0], err)
  36. return
  37. }
  38. packager := packager.NewPackager(decoder)
  39. zip_file, err := packager.Pack()
  40. if err != nil {
  41. log.Error("failed to package plugin %v", err)
  42. return
  43. }
  44. err = os.WriteFile(output_path, zip_file, 0644)
  45. if err != nil {
  46. log.Error("failed to write package file %v", err)
  47. return
  48. }
  49. log.Info("plugin packaged successfully, output path: %s", output_path)
  50. },
  51. }
  52. pluginPermissionCommand = &cobra.Command{
  53. Use: "permission",
  54. Short: "Permission",
  55. Long: `Permission, available values:
  56. tools - allow plugin to call tools
  57. models - allow plugin to call models
  58. models.llm - allow plugin to call llm
  59. models.text_embedding - allow plugin to call text_embedding model
  60. models.rerank - allow plugin to call rerank model
  61. models.tts - allow plugin to call tts
  62. models.speech2text - allow plugin to call speech2text
  63. models.moderation - allow plugin to call moderation
  64. apps - allow plugin to call apps
  65. storage - allow plugin to use storage
  66. endpoint - allow plugin to register endpoint`,
  67. }
  68. pluginPermissionAddCommand = &cobra.Command{
  69. Use: "add permission",
  70. Short: "",
  71. Long: "Add permission to plugin, you can find the available permission by running `dify plugin permission`",
  72. }
  73. pluginPermissionDropCommand = &cobra.Command{
  74. Use: "drop permission",
  75. Short: "",
  76. Long: "Drop permission from plugin, you can find the available permission by running `dify plugin permission`",
  77. }
  78. )
  79. func init() {
  80. pluginCommand.AddCommand(pluginInitCommand)
  81. pluginCommand.AddCommand(pluginPackageCommand)
  82. pluginCommand.AddCommand(pluginPermissionCommand)
  83. pluginPermissionCommand.AddCommand(pluginPermissionAddCommand)
  84. pluginPermissionCommand.AddCommand(pluginPermissionDropCommand)
  85. }