plugin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "github.com/langgenius/dify-plugin-daemon/cmd/commandline/plugin"
  6. "github.com/spf13/cobra"
  7. )
  8. var (
  9. pluginInitCommand = &cobra.Command{
  10. Use: "init",
  11. Short: "Init",
  12. Long: "Init",
  13. Run: func(c *cobra.Command, args []string) {
  14. plugin.InitPlugin()
  15. },
  16. }
  17. pluginEditPermissionCommand = &cobra.Command{
  18. Use: "permission [plugin_path]",
  19. Short: "Edit permission",
  20. Long: "Edit permission",
  21. Args: cobra.ExactArgs(1),
  22. Run: func(cmd *cobra.Command, args []string) {
  23. plugin.EditPermission(args[0])
  24. },
  25. }
  26. pluginPackageCommand = &cobra.Command{
  27. Use: "package [plugin_path]",
  28. Short: "Package",
  29. Long: "Package plugins",
  30. Args: cobra.ExactArgs(1),
  31. Run: func(cmd *cobra.Command, args []string) {
  32. inputPath := filepath.Clean(args[0])
  33. // using filename of input_path as output_path if not specified
  34. outputPath := ""
  35. if cmd.Flag("output_path").Value.String() != "" {
  36. outputPath = cmd.Flag("output_path").Value.String()
  37. } else {
  38. base := filepath.Base(inputPath)
  39. if base == "." || base == "/" {
  40. fmt.Println("Error: invalid input path, you should specify the path outside of plugin directory")
  41. return
  42. }
  43. outputPath = base + ".difypkg"
  44. }
  45. plugin.PackagePlugin(inputPath, outputPath)
  46. },
  47. }
  48. pluginChecksumCommand = &cobra.Command{
  49. Use: "checksum [plugin_path]",
  50. Short: "Checksum",
  51. Long: "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
  52. Args: cobra.ExactArgs(1),
  53. Run: func(cmd *cobra.Command, args []string) {
  54. pluginPath := args[0]
  55. plugin.CalculateChecksum(pluginPath)
  56. },
  57. }
  58. pluginModuleCommand = &cobra.Command{
  59. Use: "module",
  60. Short: "Module",
  61. Long: "Module",
  62. }
  63. pluginModuleListCommand = &cobra.Command{
  64. Use: "list [plugin_path]",
  65. Short: "List",
  66. Long: "List modules",
  67. Args: cobra.ExactArgs(1),
  68. Run: func(cmd *cobra.Command, args []string) {
  69. pluginPath := args[0]
  70. plugin.ModuleList(pluginPath)
  71. },
  72. }
  73. pluginModuleAppendCommand = &cobra.Command{
  74. Use: "append",
  75. Short: "Append",
  76. Long: "Append",
  77. }
  78. pluginModuleAppendToolsCommand = &cobra.Command{
  79. Use: "tools [plugin_path]",
  80. Short: "Tools",
  81. Long: "Append tools",
  82. Args: cobra.ExactArgs(1),
  83. Run: func(cmd *cobra.Command, args []string) {
  84. pluginPath := args[0]
  85. plugin.ModuleAppendTools(pluginPath)
  86. },
  87. }
  88. pluginModuleAppendEndpointsCommand = &cobra.Command{
  89. Use: "endpoints [plugin_path]",
  90. Short: "Endpoints",
  91. Long: "Append endpoints",
  92. Args: cobra.ExactArgs(1),
  93. Run: func(cmd *cobra.Command, args []string) {
  94. pluginPath := args[0]
  95. plugin.ModuleAppendEndpoints(pluginPath)
  96. },
  97. }
  98. // NOTE: tester is deprecated, maybe, in several months, we will support this again
  99. // pluginTestCommand = &cobra.Command{
  100. // Use: "test [-i inputs] [-t timeout] package_path invoke_type invoke_action",
  101. // Short: "",
  102. // 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" +
  103. // "type: invoke type, available values: \n" +
  104. // "[\n" +
  105. // " tool, model, endpoint\n" +
  106. // "]\n" +
  107. // "action: invoke action, available values: \n" +
  108. // "[\n" +
  109. // " invoke_tool, validate_tool_credentials, \n" +
  110. // " invoke_endpoint\n" +
  111. // " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" +
  112. // " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" +
  113. // " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" +
  114. // "]\n",
  115. // Run: func(cmd *cobra.Command, args []string) {
  116. // if len(args) < 3 {
  117. // log.Error("invalid args, please specify package_path, invoke_type, invoke_action")
  118. // return
  119. // }
  120. // // get package path
  121. // package_path_str := args[0]
  122. // // get invoke type
  123. // invoke_type_str := args[1]
  124. // // get invoke action
  125. // invoke_action_str := args[2]
  126. // // get inputs if specified
  127. // inputs := map[string]any{}
  128. // if cmd.Flag("inputs") != nil {
  129. // inputs_str := cmd.Flag("inputs").Value.String()
  130. // err := json.Unmarshal([]byte(inputs_str), &inputs)
  131. // if err != nil {
  132. // log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err)
  133. // return
  134. // }
  135. // }
  136. // // parse flag
  137. // timeout := ""
  138. // if cmd.Flag("timeout") != nil {
  139. // timeout = cmd.Flag("timeout").Value.String()
  140. // }
  141. )
  142. func init() {
  143. pluginCommand.AddCommand(pluginInitCommand)
  144. pluginCommand.AddCommand(pluginPackageCommand)
  145. pluginCommand.AddCommand(pluginChecksumCommand)
  146. pluginCommand.AddCommand(pluginEditPermissionCommand)
  147. pluginCommand.AddCommand(pluginModuleCommand)
  148. pluginModuleCommand.AddCommand(pluginModuleListCommand)
  149. pluginModuleCommand.AddCommand(pluginModuleAppendCommand)
  150. pluginModuleAppendCommand.AddCommand(pluginModuleAppendToolsCommand)
  151. pluginModuleAppendCommand.AddCommand(pluginModuleAppendEndpointsCommand)
  152. // pluginCommand.AddCommand(pluginTestCommand)
  153. // pluginTestCommand.Flags().StringP("inputs", "i", "", "inputs")
  154. // pluginTestCommand.Flags().StringP("timeout", "t", "", "timeout")
  155. pluginPackageCommand.Flags().StringP("output_path", "o", "", "output path")
  156. }