helper.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package decoder
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  9. )
  10. type PluginDecoderHelper struct {
  11. pluginDeclaration *plugin_entities.PluginDeclaration
  12. checksum string
  13. }
  14. func (p *PluginDecoderHelper) Manifest(decoder PluginDecoder) (plugin_entities.PluginDeclaration, error) {
  15. if p.pluginDeclaration != nil {
  16. return *p.pluginDeclaration, nil
  17. }
  18. // read the manifest file
  19. manifest, err := decoder.ReadFile("manifest.yaml")
  20. if err != nil {
  21. return plugin_entities.PluginDeclaration{}, err
  22. }
  23. dec, err := parser.UnmarshalYamlBytes[plugin_entities.PluginDeclaration](manifest)
  24. if err != nil {
  25. return plugin_entities.PluginDeclaration{}, err
  26. }
  27. // try to load plugins
  28. plugins := dec.Plugins
  29. for _, tool := range plugins.Tools {
  30. // read yaml
  31. plugin_yaml, err := decoder.ReadFile(tool)
  32. if err != nil {
  33. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tool file: %s", tool))
  34. }
  35. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolProviderDeclaration](plugin_yaml)
  36. if err != nil {
  37. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", tool))
  38. }
  39. // read tools
  40. for _, tool_file := range plugin_dec.ToolFiles {
  41. tool_file_content, err := decoder.ReadFile(tool_file)
  42. if err != nil {
  43. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tool file: %s", tool_file))
  44. }
  45. tool_file_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolDeclaration](tool_file_content)
  46. if err != nil {
  47. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tool file: %s", tool_file))
  48. }
  49. plugin_dec.Tools = append(plugin_dec.Tools, tool_file_dec)
  50. }
  51. dec.Tool = &plugin_dec
  52. }
  53. for _, endpoint := range plugins.Endpoints {
  54. // read yaml
  55. plugin_yaml, err := decoder.ReadFile(endpoint)
  56. if err != nil {
  57. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read endpoint file: %s", endpoint))
  58. }
  59. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointProviderDeclaration](plugin_yaml)
  60. if err != nil {
  61. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", endpoint))
  62. }
  63. // read detailed endpoints
  64. endpoints_files := plugin_dec.EndpointFiles
  65. for _, endpoint_file := range endpoints_files {
  66. endpoint_file_content, err := decoder.ReadFile(endpoint_file)
  67. if err != nil {
  68. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read endpoint file: %s", endpoint_file))
  69. }
  70. endpoint_file_dec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointDeclaration](endpoint_file_content)
  71. if err != nil {
  72. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal endpoint file: %s", endpoint_file))
  73. }
  74. plugin_dec.Endpoints = append(plugin_dec.Endpoints, endpoint_file_dec)
  75. }
  76. dec.Endpoint = &plugin_dec
  77. }
  78. for _, model := range plugins.Models {
  79. // read yaml
  80. plugin_yaml, err := decoder.ReadFile(model)
  81. if err != nil {
  82. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read model file: %s", model))
  83. }
  84. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelProviderDeclaration](plugin_yaml)
  85. if err != nil {
  86. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", model))
  87. }
  88. // read model position file
  89. if plugin_dec.PositionFiles != nil {
  90. plugin_dec.Position = &plugin_entities.ModelPosition{}
  91. llm_file_name, ok := plugin_dec.PositionFiles["llm"]
  92. if ok {
  93. llm_file, err := decoder.ReadFile(llm_file_name)
  94. if err != nil {
  95. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read llm position file: %s", llm_file_name))
  96. }
  97. position, err := parser.UnmarshalYamlBytes[[]string](llm_file)
  98. if err != nil {
  99. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal llm position file: %s", llm_file_name))
  100. }
  101. plugin_dec.Position.LLM = &position
  102. }
  103. text_embedding_file_name, ok := plugin_dec.PositionFiles["text_embedding"]
  104. if ok {
  105. text_embedding_file, err := decoder.ReadFile(text_embedding_file_name)
  106. if err != nil {
  107. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read text embedding position file: %s", text_embedding_file_name))
  108. }
  109. position, err := parser.UnmarshalYamlBytes[[]string](text_embedding_file)
  110. if err != nil {
  111. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal text embedding position file: %s", text_embedding_file_name))
  112. }
  113. plugin_dec.Position.TextEmbedding = &position
  114. }
  115. rerank_file_name, ok := plugin_dec.PositionFiles["rerank"]
  116. if ok {
  117. rerank_file, err := decoder.ReadFile(rerank_file_name)
  118. if err != nil {
  119. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read rerank position file: %s", rerank_file_name))
  120. }
  121. position, err := parser.UnmarshalYamlBytes[[]string](rerank_file)
  122. if err != nil {
  123. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal rerank position file: %s", rerank_file_name))
  124. }
  125. plugin_dec.Position.Rerank = &position
  126. }
  127. tts_file_name, ok := plugin_dec.PositionFiles["tts"]
  128. if ok {
  129. tts_file, err := decoder.ReadFile(tts_file_name)
  130. if err != nil {
  131. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tts position file: %s", tts_file_name))
  132. }
  133. position, err := parser.UnmarshalYamlBytes[[]string](tts_file)
  134. if err != nil {
  135. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tts position file: %s", tts_file_name))
  136. }
  137. plugin_dec.Position.TTS = &position
  138. }
  139. speech2text_file_name, ok := plugin_dec.PositionFiles["speech2text"]
  140. if ok {
  141. speech2text_file, err := decoder.ReadFile(speech2text_file_name)
  142. if err != nil {
  143. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read speech2text position file: %s", speech2text_file_name))
  144. }
  145. position, err := parser.UnmarshalYamlBytes[[]string](speech2text_file)
  146. if err != nil {
  147. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal speech2text position file: %s", speech2text_file_name))
  148. }
  149. plugin_dec.Position.Speech2text = &position
  150. }
  151. moderation_file_name, ok := plugin_dec.PositionFiles["moderation"]
  152. if ok {
  153. moderation_file, err := decoder.ReadFile(moderation_file_name)
  154. if err != nil {
  155. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read moderation position file: %s", moderation_file_name))
  156. }
  157. position, err := parser.UnmarshalYamlBytes[[]string](moderation_file)
  158. if err != nil {
  159. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal moderation position file: %s", moderation_file_name))
  160. }
  161. plugin_dec.Position.Moderation = &position
  162. }
  163. }
  164. // read models
  165. if err := decoder.Walk(func(filename, dir string) error {
  166. model_patterns := plugin_dec.ModelFiles
  167. // using glob to match if dir/filename is in models
  168. model_file_name := filepath.Join(dir, filename)
  169. if strings.HasSuffix(model_file_name, "_position.yaml") {
  170. return nil
  171. }
  172. for _, model_pattern := range model_patterns {
  173. matched, err := filepath.Match(model_pattern, model_file_name)
  174. if err != nil {
  175. return err
  176. }
  177. if matched {
  178. // read model file
  179. model_file, err := decoder.ReadFile(model_file_name)
  180. if err != nil {
  181. return err
  182. }
  183. model_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelDeclaration](model_file)
  184. if err != nil {
  185. return err
  186. }
  187. plugin_dec.Models = append(plugin_dec.Models, model_dec)
  188. }
  189. }
  190. return nil
  191. }); err != nil {
  192. return plugin_entities.PluginDeclaration{}, err
  193. }
  194. dec.Model = &plugin_dec
  195. }
  196. dec.FillInDefaultValues()
  197. // verify signature
  198. dec.Verified = VerifyPlugin(decoder) == nil
  199. if err := dec.ManifestValidate(); err != nil {
  200. return plugin_entities.PluginDeclaration{}, err
  201. }
  202. p.pluginDeclaration = &dec
  203. return dec, nil
  204. }
  205. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  206. files, err := decoder.ReadDir("_assets")
  207. if err != nil {
  208. return nil, err
  209. }
  210. assets := make(map[string][]byte)
  211. for _, file := range files {
  212. content, err := decoder.ReadFile(file)
  213. if err != nil {
  214. return nil, err
  215. }
  216. // trim _assets
  217. file, _ = strings.CutPrefix(file, "_assets/")
  218. assets[file] = content
  219. }
  220. return assets, nil
  221. }
  222. func (p *PluginDecoderHelper) Checksum(decoder_instance PluginDecoder) (string, error) {
  223. if p.checksum != "" {
  224. return p.checksum, nil
  225. }
  226. var err error
  227. p.checksum, err = CalculateChecksum(decoder_instance)
  228. if err != nil {
  229. return "", err
  230. }
  231. return p.checksum, nil
  232. }
  233. func (p *PluginDecoderHelper) UniqueIdentity(decoder PluginDecoder) (plugin_entities.PluginUniqueIdentifier, error) {
  234. manifest, err := decoder.Manifest()
  235. if err != nil {
  236. return plugin_entities.PluginUniqueIdentifier(""), err
  237. }
  238. identity := manifest.Identity()
  239. checksum, err := decoder.Checksum()
  240. if err != nil {
  241. return plugin_entities.PluginUniqueIdentifier(""), err
  242. }
  243. return plugin_entities.NewPluginUniqueIdentifier(fmt.Sprintf("%s@%s", identity, checksum))
  244. }