template.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package model
  2. import (
  3. "embed"
  4. "fmt"
  5. "strings"
  6. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  7. )
  8. //go:embed templates
  9. var templates embed.FS
  10. // provider_templates is a map of provider type to the template name
  11. var provider_templates map[string]string
  12. // model_templates is a map of model type to a map of template name to the template content
  13. var model_templates map[string]map[string]string
  14. func init() {
  15. provider_templates = make(map[string]string)
  16. model_templates = make(map[string]map[string]string)
  17. files, err := templates.ReadDir("templates")
  18. if err != nil {
  19. log.Error("Failed to read templates: %v", err)
  20. return
  21. }
  22. for _, file := range files {
  23. if file.IsDir() {
  24. continue
  25. }
  26. // get the file name
  27. filename := file.Name()
  28. // read the file content
  29. file_content, err := templates.ReadFile("templates/" + filename)
  30. if err != nil {
  31. log.Error("Failed to read template: %v", err)
  32. continue
  33. }
  34. filenames := strings.Split(filename, "_")
  35. // check the first element is a provider
  36. if filenames[0] == "provider" {
  37. if len(filenames) != 2 {
  38. log.Error("Invalid provider template: %s", filename)
  39. continue
  40. }
  41. provider_templates[filenames[1]] = string(file_content)
  42. } else if filenames[0] == "model" {
  43. if len(filenames) != 3 {
  44. log.Error("Invalid model template: %s", filename)
  45. continue
  46. }
  47. if _, ok := model_templates[filenames[1]]; !ok {
  48. model_templates[filenames[1]] = make(map[string]string)
  49. }
  50. model_templates[filenames[1]][filenames[2]] = string(file_content)
  51. }
  52. }
  53. }
  54. func ListTemplates(typ string, model_type string, name string) {
  55. color_reset := "\033[0m"
  56. color_cyan := "\033[36m"
  57. color_yellow := "\033[33m"
  58. color_green := "\033[32m"
  59. if typ == "provider" || typ == "" {
  60. fmt.Printf("%sProvider Templates:%s\n", color_cyan, color_reset)
  61. for template := range provider_templates {
  62. if name == "" || strings.Contains(template, name) {
  63. fmt.Printf(" %s%s%s\n", color_yellow, template, color_reset)
  64. }
  65. }
  66. fmt.Println()
  67. }
  68. if typ == "model" || typ == "" {
  69. fmt.Printf("%sModel Templates:%s\n", color_cyan, color_reset)
  70. if model_type == "" {
  71. for model_type, templates := range model_templates {
  72. fmt.Printf("%s%s:%s\n", color_yellow, model_type, color_reset)
  73. for template := range templates {
  74. if name == "" || strings.Contains(template, name) {
  75. fmt.Printf(" %s%s%s\n", color_green, template, color_reset)
  76. }
  77. }
  78. fmt.Println()
  79. }
  80. } else {
  81. if templates, ok := model_templates[model_type]; ok {
  82. fmt.Printf("%s%s:%s\n", color_yellow, model_type, color_reset)
  83. for template := range templates {
  84. if name == "" || strings.Contains(template, name) {
  85. fmt.Printf(" %s%s%s\n", color_green, template, color_reset)
  86. }
  87. }
  88. fmt.Println()
  89. }
  90. }
  91. }
  92. }
  93. func GetTemplate(typ string, name string) {
  94. }
  95. func CreateFromTemplate(root string, typ string, name string) {
  96. }