plugin.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. init_pkg "github.com/langgenius/dify-plugin-daemon/cmd/commandline/init"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  10. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/packager"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  12. "github.com/spf13/cobra"
  13. )
  14. var (
  15. pluginInitCommand = &cobra.Command{
  16. Use: "init",
  17. Short: "Init",
  18. Long: "Init",
  19. Run: func(c *cobra.Command, args []string) {
  20. init_pkg.InitPlugin()
  21. },
  22. }
  23. pluginPackageCommand = &cobra.Command{
  24. Use: "package plugin_path [-o output_path]",
  25. Short: "Package",
  26. Long: "Package plugins",
  27. Run: func(cmd *cobra.Command, args []string) {
  28. if len(args) < 1 {
  29. fmt.Println("Error: plugin_path is required")
  30. return
  31. }
  32. input_path := args[0]
  33. // using filename of input_path as output_path if not specified
  34. output_path := ""
  35. if cmd.Flag("output_path") != nil {
  36. output_path = cmd.Flag("output_path").Value.String()
  37. } else {
  38. output_path = filepath.Base(input_path) + ".difypkg"
  39. }
  40. decoder, err := decoder.NewFSPluginDecoder(input_path)
  41. if err != nil {
  42. log.Error("failed to create plugin decoder , plugin path: %s, error: %v", input_path, err)
  43. return
  44. }
  45. packager := packager.NewPackager(decoder)
  46. zip_file, err := packager.Pack()
  47. if err != nil {
  48. log.Error("failed to package plugin %v", err)
  49. return
  50. }
  51. err = os.WriteFile(output_path, zip_file, 0644)
  52. if err != nil {
  53. log.Error("failed to write package file %v", err)
  54. return
  55. }
  56. log.Info("plugin packaged successfully, output path: %s", output_path)
  57. },
  58. }
  59. pluginChecksumCommand = &cobra.Command{
  60. Use: "checksum plugin_path",
  61. Short: "Checksum",
  62. Long: "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
  63. Run: func(cmd *cobra.Command, args []string) {
  64. if len(args) < 1 {
  65. fmt.Println("Error: plugin_path is required")
  66. return
  67. }
  68. plugin_path := args[0]
  69. var plugin_decoder decoder.PluginDecoder
  70. if stat, err := os.Stat(plugin_path); err == nil {
  71. if stat.IsDir() {
  72. plugin_decoder, err = decoder.NewFSPluginDecoder(plugin_path)
  73. if err != nil {
  74. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
  75. return
  76. }
  77. } else {
  78. bytes, err := os.ReadFile(plugin_path)
  79. if err != nil {
  80. log.Error("failed to read plugin file, plugin path: %s, error: %v", plugin_path, err)
  81. return
  82. }
  83. plugin_decoder, err = decoder.NewZipPluginDecoder(bytes)
  84. if err != nil {
  85. log.Error("failed to create plugin decoder, plugin path: %s, error: %v", plugin_path, err)
  86. return
  87. }
  88. }
  89. } else {
  90. log.Error("failed to get plugin file info, plugin path: %s, error: %v", plugin_path, err)
  91. return
  92. }
  93. checksum, err := plugin_decoder.Checksum()
  94. if err != nil {
  95. log.Error("failed to calculate checksum, plugin path: %s, error: %v", plugin_path, err)
  96. return
  97. }
  98. log.Info("plugin checksum: %s", checksum)
  99. },
  100. }
  101. pluginPermissionCommand = &cobra.Command{
  102. Use: "permission",
  103. Short: "Permission",
  104. Long: `Permission, available values:
  105. tools - allow plugin to call tools
  106. models - allow plugin to call models
  107. models.llm - allow plugin to call llm
  108. models.text_embedding - allow plugin to call text_embedding model
  109. models.rerank - allow plugin to call rerank model
  110. models.tts - allow plugin to call tts
  111. models.speech2text - allow plugin to call speech2text
  112. models.moderation - allow plugin to call moderation
  113. apps - allow plugin to call apps
  114. storage - allow plugin to use storage
  115. endpoint - allow plugin to register endpoint`,
  116. }
  117. pluginPermissionAddCommand = &cobra.Command{
  118. Use: "add permission",
  119. Short: "",
  120. Long: "Add permission to plugin, you can find the available permission by running `dify plugin permission`",
  121. }
  122. pluginPermissionDropCommand = &cobra.Command{
  123. Use: "drop permission",
  124. Short: "",
  125. Long: "Drop permission from plugin, you can find the available permission by running `dify plugin permission`",
  126. }
  127. pluginTestCommand = &cobra.Command{
  128. Use: "test package_path invoke_type invoke_action [-i inputs]",
  129. Short: "",
  130. Long: "Test runs the given plugin package locally, and you can specify the inputs using json format, if not specified, will use default inputs\n" +
  131. "type: invoke type, available values: \n" +
  132. "[\n" +
  133. " tool, model, endpoint\n" +
  134. "]\n" +
  135. "action: invoke action, available values: \n" +
  136. "[\n" +
  137. " invoke_tool, validate_tool_credentials, \n" +
  138. " invoke_endpoint\n" +
  139. " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" +
  140. " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" +
  141. " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" +
  142. "]\n",
  143. Run: func(cmd *cobra.Command, args []string) {
  144. if len(args) < 3 {
  145. log.Error("invalid args, please specify package_path, invoke_type, invoke_action")
  146. return
  147. }
  148. // get package path
  149. package_path_str := args[0]
  150. // get invoke type
  151. invoke_type_str := args[1]
  152. // get invoke action
  153. invoke_action_str := args[2]
  154. // get inputs if specified
  155. inputs_str := "{}"
  156. if len(args) > 3 {
  157. inputs_str = args[3]
  158. }
  159. inputs := map[string]any{}
  160. err := json.Unmarshal([]byte(inputs_str), &inputs)
  161. if err != nil {
  162. log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err)
  163. return
  164. }
  165. // read package file
  166. package_file, err := os.ReadFile(package_path_str)
  167. if err != nil {
  168. log.Error("failed to read package file, package path: %s, error: %v", package_path_str, err)
  169. return
  170. }
  171. // create plugin decoder
  172. plugin_decoder, err := decoder.NewZipPluginDecoder(package_file)
  173. if err != nil {
  174. log.Error("failed to create plugin decoder, package path: %s, error: %v", package_path_str, err)
  175. return
  176. }
  177. // get invoke_type and invoke_action
  178. invoke_type := access_types.PluginAccessType(invoke_type_str)
  179. if !invoke_type.IsValid() {
  180. log.Error("invalid invoke type: %s", invoke_type_str)
  181. return
  182. }
  183. invoke_action := access_types.PluginAccessAction(invoke_action_str)
  184. if !invoke_action.IsValid() {
  185. log.Error("invalid invoke action: %s", invoke_action_str)
  186. return
  187. }
  188. fmt.Println(plugin_decoder)
  189. },
  190. }
  191. )
  192. func init() {
  193. pluginCommand.AddCommand(pluginInitCommand)
  194. pluginCommand.AddCommand(pluginPackageCommand)
  195. pluginCommand.AddCommand(pluginChecksumCommand)
  196. pluginCommand.AddCommand(pluginPermissionCommand)
  197. pluginCommand.AddCommand(pluginTestCommand)
  198. pluginPermissionCommand.AddCommand(pluginPermissionAddCommand)
  199. pluginPermissionCommand.AddCommand(pluginPermissionDropCommand)
  200. }