decoder.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 _, tool := range plugins.Tools {
  67. // read yaml
  68. plugin_yaml, err := decoder.ReadFile(tool)
  69. if err != nil {
  70. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tool file: %s", tool))
  71. }
  72. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolProviderDeclaration](plugin_yaml)
  73. if err != nil {
  74. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", tool))
  75. }
  76. dec.Tool = &plugin_dec
  77. }
  78. for _, endpoint := range plugins.Endpoints {
  79. // read yaml
  80. plugin_yaml, err := decoder.ReadFile(endpoint)
  81. if err != nil {
  82. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read endpoint file: %s", endpoint))
  83. }
  84. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointProviderDeclaration](plugin_yaml)
  85. if err != nil {
  86. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", endpoint))
  87. }
  88. dec.Endpoint = &plugin_dec
  89. }
  90. for _, model := range plugins.Models {
  91. // read yaml
  92. plugin_yaml, err := decoder.ReadFile(model)
  93. if err != nil {
  94. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read model file: %s", model))
  95. }
  96. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelProviderDeclaration](plugin_yaml)
  97. if err != nil {
  98. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", model))
  99. }
  100. dec.Model = &plugin_dec
  101. }
  102. if err := dec.ManifestValidate(); err != nil {
  103. return plugin_entities.PluginDeclaration{}, err
  104. }
  105. p.pluginDeclaration = &dec
  106. return dec, nil
  107. }
  108. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  109. files, err := decoder.ReadDir("_assets")
  110. if err != nil {
  111. return nil, err
  112. }
  113. assets := make(map[string][]byte)
  114. for _, file := range files {
  115. content, err := decoder.ReadFile(file)
  116. if err != nil {
  117. return nil, err
  118. }
  119. // trim _assets
  120. file, _ = strings.CutPrefix(file, "_assets/")
  121. assets[file] = content
  122. }
  123. return assets, nil
  124. }
  125. func (p *PluginDecoderHelper) Checksum(decoder PluginDecoder) (string, error) {
  126. if p.checksum != "" {
  127. return p.checksum, nil
  128. }
  129. var err error
  130. p.checksum, err = CalculateChecksum(decoder)
  131. if err != nil {
  132. return "", err
  133. }
  134. return p.checksum, nil
  135. }
  136. func (p *PluginDecoderHelper) UniqueIdentity(decoder PluginDecoder) (plugin_entities.PluginUniqueIdentifier, error) {
  137. manifest, err := decoder.Manifest()
  138. if err != nil {
  139. return plugin_entities.PluginUniqueIdentifier(""), err
  140. }
  141. identity := manifest.Identity()
  142. checksum, err := decoder.Checksum()
  143. if err != nil {
  144. return plugin_entities.PluginUniqueIdentifier(""), err
  145. }
  146. return plugin_entities.NewPluginUniqueIdentifier(fmt.Sprintf("%s@%s", identity, checksum))
  147. }