category.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package plugin
  2. import (
  3. "fmt"
  4. tea "github.com/charmbracelet/bubbletea"
  5. )
  6. const (
  7. // Colors
  8. RESET = "\033[0m"
  9. BOLD = "\033[1m"
  10. // Foreground colors
  11. GREEN = "\033[32m"
  12. YELLOW = "\033[33m"
  13. BLUE = "\033[34m"
  14. )
  15. const PLUGIN_GUIDE = `But before starting, you need some basic knowledge about the Plugin types, Plugin supports to extend the following abilities in Dify:
  16. ` + "\n" + BOLD + `- Tool` + RESET + `: ` + GREEN + `Tool Providers like Google Search, Stable Diffusion, etc. it can be used to perform a specific task.` + RESET + `
  17. ` + BOLD + `- Model` + RESET + `: ` + GREEN + `Model Providers like OpenAI, Anthropic, etc. you can use their models to enhance the AI capabilities.` + RESET + `
  18. ` + BOLD + `- Endpoint` + RESET + `: ` + GREEN + `Like Service API in Dify and Ingress in Kubernetes, you can extend a http service as an endpoint and control its logics using your own code.` + RESET + `
  19. Based on the ability you want to extend, we have divided the Plugin into three types: ` + BOLD + `Tool` + RESET + `, ` + BOLD + `Model` + RESET + `, and ` + BOLD + `Extension` + RESET + `.
  20. ` + BOLD + `- Tool` + RESET + `: ` + YELLOW + `It's a tool provider, but not only limited to tools, you can implement a endpoint there, for example, you need both ` + BLUE + `Sending Message` + RESET + YELLOW + ` and ` + BLUE + `Receiving Message` + RESET + YELLOW + ` if you are building a Discord Bot, ` + BOLD + `Tool` + RESET + YELLOW + ` and ` + BOLD + `Endpoint` + RESET + YELLOW + ` are both required.` + RESET + `
  21. ` + BOLD + `- Model` + RESET + `: ` + YELLOW + `Just a model provider, extending others is not allowed.` + RESET + `
  22. ` + BOLD + `- Extension` + RESET + `: ` + YELLOW + `Other times, you may only need a simple http service to extend the functionalities, ` + BOLD + `Extension` + RESET + YELLOW + ` is the right choice for you.` + RESET + `
  23. ` + `
  24. What's more, we have provided the template for you, you can choose one of them below:
  25. `
  26. type category struct {
  27. cursor int
  28. }
  29. var categories = []string{
  30. "tool",
  31. "llm",
  32. "text-embedding",
  33. "rerank",
  34. "tts",
  35. "speech2text",
  36. "moderation",
  37. "extension",
  38. }
  39. func newCategory() category {
  40. return category{
  41. // default category is tool
  42. cursor: 0,
  43. }
  44. }
  45. func (c category) Category() string {
  46. return categories[c.cursor]
  47. }
  48. func (c category) View() string {
  49. s := "Select the type of plugin you want to create, and press `Enter` to continue\n"
  50. s += PLUGIN_GUIDE
  51. for i, category := range categories {
  52. if i == c.cursor {
  53. s += fmt.Sprintf("\033[32m-> %s\033[0m\n", category)
  54. } else {
  55. s += fmt.Sprintf(" %s\n", category)
  56. }
  57. }
  58. return s
  59. }
  60. func (c category) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  61. switch msg := msg.(type) {
  62. case tea.KeyMsg:
  63. switch msg.String() {
  64. case "ctrl+c", "q":
  65. return c, SUB_MENU_EVENT_NONE, tea.Quit
  66. case "j", "down":
  67. c.cursor++
  68. if c.cursor >= len(categories) {
  69. c.cursor = len(categories) - 1
  70. }
  71. case "k", "up":
  72. c.cursor--
  73. if c.cursor < 0 {
  74. c.cursor = 0
  75. }
  76. case "enter":
  77. return c, SUB_MENU_EVENT_NEXT, nil
  78. }
  79. }
  80. return c, SUB_MENU_EVENT_NONE, nil
  81. }
  82. func (c category) Init() tea.Cmd {
  83. return nil
  84. }