plugin.go 4.9 KB

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