module.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package plugin
  2. import (
  3. "html/template"
  4. "os"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  6. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  7. )
  8. const TOOL_MODULE_TEMPLATE = `
  9. ========== {{.Identity.Name}} ==========
  10. Author: {{.Identity.Author}}
  11. Label: {{.Identity.Label.EnUS}}
  12. Description: {{.Description.Human.EnUS}}
  13. Parameters:
  14. {{- range .Parameters}}
  15. - Name: {{.Name}}
  16. Type: {{.Type}}
  17. Required: {{.Required}}
  18. Description: {{.HumanDescription.EnUS}}
  19. {{- if .Default}}
  20. Default: {{.Default}}
  21. {{- end}}
  22. {{- if .Options}}
  23. Options:
  24. {{- range .Options}}
  25. - Value: {{.Value}}
  26. Label: {{.Label.EnUS}}
  27. {{- end}}
  28. {{- end}}
  29. {{- end}}
  30. `
  31. const MODEL_MODULE_TEMPLATE = `
  32. ========== {{.Model}} ==========
  33. Name: {{.Model}}
  34. Type: {{.ModelType}}
  35. Label: {{.Label.EnUS}}
  36. Parameters:
  37. {{- range .ParameterRules}}
  38. - Name: {{.Name}}
  39. Type: {{.Type}}
  40. Required: {{.Required}}
  41. Description: {{.Help.EnUS}}
  42. {{- if .Default}}
  43. Default: {{.Default}}
  44. {{- end}}
  45. {{- if .Min}}
  46. Min: {{.Min}}
  47. {{- end}}
  48. {{- if .Max}}
  49. Max: {{.Max}}
  50. {{- end}}
  51. {{- if .Options}}
  52. Options: {{range .Options}}{{.}}, {{end}}
  53. {{- end}}
  54. {{- end}}
  55. `
  56. const ENDPOINT_MODULE_TEMPLATE = `
  57. ========== Endpoints ==========
  58. Path: {{.Path}}
  59. Method: {{.Method}}
  60. `
  61. const PLUGIN_MODULE_TEMPLATE = `
  62. ========== Plugin ==========
  63. Name: {{.Name}}
  64. Version: {{.Version}}
  65. Description: {{.Description.EnUS}}
  66. Author: {{.Author}}
  67. Icon: {{.Icon}}
  68. Tags: {{range .Tags}}{{.}}, {{end}}
  69. Category: {{.Category}}
  70. Resource:
  71. Memory: {{.Resource.Memory}} bytes
  72. Permissions:
  73. {{- if .Resource.Permission.Tool}}
  74. Tool: {{.Resource.Permission.Tool.Enabled}}
  75. {{- end}}
  76. {{- if .Resource.Permission.Model}}
  77. Model:
  78. Enabled: {{.Resource.Permission.Model.Enabled}}
  79. LLM: {{.Resource.Permission.Model.LLM}}
  80. TextEmbedding: {{.Resource.Permission.Model.TextEmbedding}}
  81. Rerank: {{.Resource.Permission.Model.Rerank}}
  82. TTS: {{.Resource.Permission.Model.TTS}}
  83. Speech2text: {{.Resource.Permission.Model.Speech2text}}
  84. Moderation: {{.Resource.Permission.Model.Moderation}}
  85. {{- end}}
  86. {{- if .Resource.Permission.Node}}
  87. Node: {{.Resource.Permission.Node.Enabled}}
  88. {{- end}}
  89. {{- if .Resource.Permission.Endpoint}}
  90. Endpoint: {{.Resource.Permission.Endpoint.Enabled}}
  91. {{- end}}
  92. {{- if .Resource.Permission.App}}
  93. App: {{.Resource.Permission.App.Enabled}}
  94. {{- end}}
  95. {{- if .Resource.Permission.Storage}}
  96. Storage:
  97. Enabled: {{.Resource.Permission.Storage.Enabled}}
  98. Size: {{.Resource.Permission.Storage.Size}} bytes
  99. {{- end}}
  100. `
  101. func ModuleList(pluginPath string) {
  102. decoder, err := decoder.NewFSPluginDecoder(pluginPath)
  103. if err != nil {
  104. log.Error("your plugin is not a valid plugin: %s", err)
  105. return
  106. }
  107. manifest, err := decoder.Manifest()
  108. if err != nil {
  109. log.Error("failed to get manifest: %s", err)
  110. return
  111. }
  112. if manifest.Tool != nil {
  113. for _, tool := range manifest.Tool.Tools {
  114. tmpl, err := template.New("tool").Parse(TOOL_MODULE_TEMPLATE)
  115. if err != nil {
  116. log.Error("failed to parse template: %s", err)
  117. return
  118. }
  119. err = tmpl.Execute(os.Stdout, tool)
  120. if err != nil {
  121. log.Error("failed to execute template: %s", err)
  122. return
  123. }
  124. }
  125. }
  126. if manifest.Model != nil {
  127. for _, model := range manifest.Model.Models {
  128. tmpl, err := template.New("model").Parse(MODEL_MODULE_TEMPLATE)
  129. if err != nil {
  130. log.Error("failed to parse template: %s", err)
  131. return
  132. }
  133. err = tmpl.Execute(os.Stdout, model)
  134. if err != nil {
  135. log.Error("failed to execute template: %s", err)
  136. return
  137. }
  138. }
  139. }
  140. if manifest.Endpoint != nil {
  141. for _, endpoint := range manifest.Endpoint.Endpoints {
  142. tmpl, err := template.New("endpoint").Parse(ENDPOINT_MODULE_TEMPLATE)
  143. if err != nil {
  144. log.Error("failed to parse template: %s", err)
  145. return
  146. }
  147. err = tmpl.Execute(os.Stdout, endpoint)
  148. if err != nil {
  149. log.Error("failed to execute template: %s", err)
  150. return
  151. }
  152. }
  153. }
  154. }
  155. func ModuleAppendTools(pluginPath string) {
  156. }
  157. func ModuleAppendEndpoints(pluginPath string) {
  158. }