decoder.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  43. type PluginDecoderHelper struct {
  44. pluginDeclaration *plugin_entities.PluginDeclaration
  45. }
  46. func (p *PluginDecoderHelper) Manifest(decoder PluginDecoder) (plugin_entities.PluginDeclaration, error) {
  47. if p.pluginDeclaration != nil {
  48. return *p.pluginDeclaration, nil
  49. }
  50. // read the manifest file
  51. manifest, err := decoder.ReadFile("manifest.yaml")
  52. if err != nil {
  53. return plugin_entities.PluginDeclaration{}, err
  54. }
  55. dec, err := parser.UnmarshalYamlBytes[plugin_entities.PluginDeclaration](manifest)
  56. if err != nil {
  57. return plugin_entities.PluginDeclaration{}, err
  58. }
  59. // try to load plugins
  60. plugins := dec.Plugins
  61. for _, plugin := range plugins {
  62. // read yaml
  63. plugin_yaml, err := decoder.ReadFile(plugin)
  64. if err != nil {
  65. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read plugin file: %s", plugin))
  66. }
  67. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.GenericProviderDeclaration](plugin_yaml)
  68. if err != nil {
  69. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", plugin))
  70. }
  71. switch plugin_dec.Type {
  72. case plugin_entities.PROVIDER_TYPE_ENDPOINT:
  73. dec.Endpoint, err = parser.MapToStruct[plugin_entities.EndpointProviderDeclaration](plugin_dec.Provider)
  74. if err != nil {
  75. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert endpoint to struct: %s", plugin))
  76. }
  77. case plugin_entities.PROVIDER_TYPE_TOOL:
  78. dec.Tool, err = parser.MapToStruct[plugin_entities.ToolProviderDeclaration](plugin_dec.Provider)
  79. if err != nil {
  80. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert tool to struct: %s", plugin))
  81. }
  82. case plugin_entities.PROVIDER_TYPE_MODEL:
  83. dec.Model, err = parser.MapToStruct[plugin_entities.ModelProviderDeclaration](plugin_dec.Provider)
  84. if err != nil {
  85. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to convert model to struct: %s", plugin))
  86. }
  87. default:
  88. return plugin_entities.PluginDeclaration{}, fmt.Errorf("unknown provider type: %s", plugin_dec.Type)
  89. }
  90. }
  91. if err := dec.ManifestValidate(); err != nil {
  92. return plugin_entities.PluginDeclaration{}, err
  93. }
  94. p.pluginDeclaration = &dec
  95. return dec, nil
  96. }
  97. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  98. files, err := decoder.ReadDir("_assets")
  99. if err != nil {
  100. return nil, err
  101. }
  102. assets := make(map[string][]byte)
  103. for _, file := range files {
  104. content, err := decoder.ReadFile(file)
  105. if err != nil {
  106. return nil, err
  107. }
  108. // trim _assets
  109. file, _ = strings.CutPrefix(file, "_assets/")
  110. assets[file] = content
  111. }
  112. return assets, nil
  113. }