decoder.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package decoder
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/fs"
  7. "strings"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  10. )
  11. // PluginDecoder is an interface for decoding and interacting with plugin files
  12. type PluginDecoder interface {
  13. // Open initializes the decoder and prepares it for use
  14. Open() error
  15. // Walk traverses the plugin files and calls the provided function for each file
  16. // The function is called with the filename and directory of each file
  17. Walk(fn func(filename string, dir string) error) error
  18. // ReadFile reads the entire contents of a file and returns it as a byte slice
  19. ReadFile(filename string) ([]byte, error)
  20. // ReadDir reads the contents of a directory and returns a slice of strings
  21. // The strings are the filenames, it's a full path and directory will not be included
  22. // It executes recursively
  23. // Example:
  24. // - dirname: "config"
  25. // - return: ["config/settings.yaml", "config/default.yaml"]
  26. ReadDir(dirname string) ([]string, error)
  27. // Close releases any resources used by the decoder
  28. Close() error
  29. // Stat returns file info for the specified filename
  30. Stat(filename string) (fs.FileInfo, error)
  31. // FileReader returns an io.ReadCloser for reading the contents of a file
  32. // Remember to close the reader when done using it
  33. FileReader(filename string) (io.ReadCloser, error)
  34. // Signature returns the signature of the plugin, if available
  35. Signature() (string, error)
  36. // CreateTime returns the creation time of the plugin as a Unix timestamp
  37. CreateTime() (int64, error)
  38. // Manifest returns the manifest of the plugin
  39. Manifest() (plugin_entities.PluginDeclaration, error)
  40. // Assets returns a map of assets, the key is the filename, the value is the content
  41. Assets() (map[string][]byte, error)
  42. // UniqueIdentity returns the unique identity of the plugin
  43. UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error)
  44. // Checksum returns the checksum of the plugin
  45. Checksum() (string, error)
  46. }
  47. type PluginDecoderHelper struct {
  48. pluginDeclaration *plugin_entities.PluginDeclaration
  49. checksum string
  50. }
  51. func (p *PluginDecoderHelper) Manifest(decoder PluginDecoder) (plugin_entities.PluginDeclaration, error) {
  52. if p.pluginDeclaration != nil {
  53. return *p.pluginDeclaration, nil
  54. }
  55. // read the manifest file
  56. manifest, err := decoder.ReadFile("manifest.yaml")
  57. if err != nil {
  58. return plugin_entities.PluginDeclaration{}, err
  59. }
  60. dec, err := parser.UnmarshalYamlBytes[plugin_entities.PluginDeclaration](manifest)
  61. if err != nil {
  62. return plugin_entities.PluginDeclaration{}, err
  63. }
  64. // try to load plugins
  65. plugins := dec.Plugins
  66. for _, plugin := range plugins {
  67. // read yaml
  68. plugin_yaml, err := decoder.ReadFile(plugin)
  69. if err != nil {
  70. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read plugin file: %s", plugin))
  71. }
  72. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.GenericProviderDeclaration](plugin_yaml)
  73. if err != nil {
  74. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", plugin))
  75. }
  76. switch plugin_dec.Type {
  77. case plugin_entities.PROVIDER_TYPE_ENDPOINT:
  78. dec.Endpoint, err = parser.MapToStruct[plugin_entities.EndpointProviderDeclaration](plugin_dec.Provider)
  79. if err != nil {
  80. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert endpoint to struct: %s", plugin))
  81. }
  82. case plugin_entities.PROVIDER_TYPE_TOOL:
  83. dec.Tool, err = parser.MapToStruct[plugin_entities.ToolProviderDeclaration](plugin_dec.Provider)
  84. if err != nil {
  85. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert tool to struct: %s", plugin))
  86. }
  87. case plugin_entities.PROVIDER_TYPE_MODEL:
  88. dec.Model, err = parser.MapToStruct[plugin_entities.ModelProviderDeclaration](plugin_dec.Provider)
  89. if err != nil {
  90. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert model to struct: %s", plugin))
  91. }
  92. default:
  93. return plugin_entities.PluginDeclaration{}, fmt.Errorf("unknown provider type: %s", plugin_dec.Type)
  94. }
  95. }
  96. if err := dec.ManifestValidate(); err != nil {
  97. return plugin_entities.PluginDeclaration{}, err
  98. }
  99. p.pluginDeclaration = &dec
  100. return dec, nil
  101. }
  102. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  103. files, err := decoder.ReadDir("_assets")
  104. if err != nil {
  105. return nil, err
  106. }
  107. assets := make(map[string][]byte)
  108. for _, file := range files {
  109. content, err := decoder.ReadFile(file)
  110. if err != nil {
  111. return nil, err
  112. }
  113. // trim _assets
  114. file, _ = strings.CutPrefix(file, "_assets/")
  115. assets[file] = content
  116. }
  117. return assets, nil
  118. }
  119. func (p *PluginDecoderHelper) Checksum(decoder PluginDecoder) (string, error) {
  120. if p.checksum != "" {
  121. return p.checksum, nil
  122. }
  123. var err error
  124. p.checksum, err = CalculateChecksum(decoder)
  125. if err != nil {
  126. return "", err
  127. }
  128. return p.checksum, nil
  129. }
  130. func (p *PluginDecoderHelper) UniqueIdentity(decoder PluginDecoder) (plugin_entities.PluginUniqueIdentifier, error) {
  131. manifest, err := decoder.Manifest()
  132. if err != nil {
  133. return plugin_entities.PluginUniqueIdentifier(""), err
  134. }
  135. identity := manifest.Identity()
  136. checksum, err := decoder.Checksum()
  137. if err != nil {
  138. return plugin_entities.PluginUniqueIdentifier(""), err
  139. }
  140. return plugin_entities.PluginUniqueIdentifier(fmt.Sprintf("%s-%s", identity, checksum)), nil
  141. }