plugin.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // NOTE: tester is deprecated, maybe, in several months, we will support this again
  50. // pluginTestCommand = &cobra.Command{
  51. // Use: "test [-i inputs] [-t timeout] package_path invoke_type invoke_action",
  52. // Short: "",
  53. // 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" +
  54. // "type: invoke type, available values: \n" +
  55. // "[\n" +
  56. // " tool, model, endpoint\n" +
  57. // "]\n" +
  58. // "action: invoke action, available values: \n" +
  59. // "[\n" +
  60. // " invoke_tool, validate_tool_credentials, \n" +
  61. // " invoke_endpoint\n" +
  62. // " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" +
  63. // " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" +
  64. // " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" +
  65. // "]\n",
  66. // Run: func(cmd *cobra.Command, args []string) {
  67. // if len(args) < 3 {
  68. // log.Error("invalid args, please specify package_path, invoke_type, invoke_action")
  69. // return
  70. // }
  71. // // get package path
  72. // package_path_str := args[0]
  73. // // get invoke type
  74. // invoke_type_str := args[1]
  75. // // get invoke action
  76. // invoke_action_str := args[2]
  77. // // get inputs if specified
  78. // inputs := map[string]any{}
  79. // if cmd.Flag("inputs") != nil {
  80. // inputs_str := cmd.Flag("inputs").Value.String()
  81. // err := json.Unmarshal([]byte(inputs_str), &inputs)
  82. // if err != nil {
  83. // log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err)
  84. // return
  85. // }
  86. // }
  87. // // parse flag
  88. // timeout := ""
  89. // if cmd.Flag("timeout") != nil {
  90. // timeout = cmd.Flag("timeout").Value.String()
  91. // }
  92. )
  93. func init() {
  94. pluginCommand.AddCommand(pluginInitCommand)
  95. pluginCommand.AddCommand(pluginPackageCommand)
  96. pluginCommand.AddCommand(pluginChecksumCommand)
  97. // pluginCommand.AddCommand(pluginTestCommand)
  98. // pluginTestCommand.Flags().StringP("inputs", "i", "", "inputs")
  99. // pluginTestCommand.Flags().StringP("timeout", "t", "", "timeout")
  100. pluginPackageCommand.Flags().StringP("output_path", "o", "", "output path")
  101. }