category.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. ` + BOLD + `- Agent Strategy` + RESET + `: ` + GREEN + `You can implement your own agent strategy like Function Calling, ReAct, ToT, Cot, etc. anyway you want.` + RESET + `
  20. Based on the ability you want to extend, we have divided the Plugin into four types: ` + BOLD + `Tool` + RESET + `, ` + BOLD + `Model` + RESET + `, ` + BOLD + `Extension` + RESET + `, and ` + BOLD + `Agent Strategy` + RESET + `.
  21. ` + BOLD + `- Tool` + RESET + `: ` + YELLOW + `It's a tool provider, but not only limited to tools, you can implement an 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 + `
  22. ` + BOLD + `- Model` + RESET + `: ` + YELLOW + `Just a model provider, extending others is not allowed.` + RESET + `
  23. ` + 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 + `
  24. ` + BOLD + `- Agent Strategy` + RESET + `: ` + YELLOW + `Implement your own logics here, just by focusing on Agent itself` + RESET + `
  25. What's more, we have provided the template for you, you can choose one of them below:
  26. `
  27. type category struct {
  28. cursor int
  29. }
  30. var categories = []string{
  31. "tool",
  32. "agent-strategy",
  33. "llm",
  34. "text-embedding",
  35. "rerank",
  36. "tts",
  37. "speech2text",
  38. "moderation",
  39. "extension",
  40. }
  41. func newCategory() category {
  42. return category{
  43. // default category is tool
  44. cursor: 0,
  45. }
  46. }
  47. func (c category) Category() string {
  48. return categories[c.cursor]
  49. }
  50. func (c category) View() string {
  51. s := "Select the type of plugin you want to create, and press `Enter` to continue\n"
  52. s += PLUGIN_GUIDE
  53. for i, category := range categories {
  54. if i == c.cursor {
  55. s += fmt.Sprintf("\033[32m-> %s\033[0m\n", category)
  56. } else {
  57. s += fmt.Sprintf(" %s\n", category)
  58. }
  59. }
  60. return s
  61. }
  62. func (c category) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  63. switch msg := msg.(type) {
  64. case tea.KeyMsg:
  65. switch msg.String() {
  66. case "ctrl+c", "q":
  67. return c, SUB_MENU_EVENT_NONE, tea.Quit
  68. case "j", "down":
  69. c.cursor++
  70. if c.cursor >= len(categories) {
  71. c.cursor = len(categories) - 1
  72. }
  73. case "k", "up":
  74. c.cursor--
  75. if c.cursor < 0 {
  76. c.cursor = 0
  77. }
  78. case "enter":
  79. return c, SUB_MENU_EVENT_NEXT, nil
  80. }
  81. }
  82. return c, SUB_MENU_EVENT_NONE, nil
  83. }
  84. func (c category) Init() tea.Cmd {
  85. return nil
  86. }