decoder.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package decoder
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/fs"
  7. "path/filepath"
  8. "strings"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  11. )
  12. // PluginDecoder is an interface for decoding and interacting with plugin files
  13. type PluginDecoder interface {
  14. // Open initializes the decoder and prepares it for use
  15. Open() error
  16. // Walk traverses the plugin files and calls the provided function for each file
  17. // The function is called with the filename and directory of each file
  18. Walk(fn func(filename string, dir string) error) error
  19. // ReadFile reads the entire contents of a file and returns it as a byte slice
  20. ReadFile(filename string) ([]byte, error)
  21. // ReadDir reads the contents of a directory and returns a slice of strings
  22. // The strings are the filenames, it's a full path and directory will not be included
  23. // It executes recursively
  24. // Example:
  25. // - dirname: "config"
  26. // - return: ["config/settings.yaml", "config/default.yaml"]
  27. ReadDir(dirname string) ([]string, error)
  28. // Close releases any resources used by the decoder
  29. Close() error
  30. // Stat returns file info for the specified filename
  31. Stat(filename string) (fs.FileInfo, error)
  32. // FileReader returns an io.ReadCloser for reading the contents of a file
  33. // Remember to close the reader when done using it
  34. FileReader(filename string) (io.ReadCloser, error)
  35. // Signature returns the signature of the plugin, if available
  36. Signature() (string, error)
  37. // CreateTime returns the creation time of the plugin as a Unix timestamp
  38. CreateTime() (int64, error)
  39. // Manifest returns the manifest of the plugin
  40. Manifest() (plugin_entities.PluginDeclaration, error)
  41. // Assets returns a map of assets, the key is the filename, the value is the content
  42. Assets() (map[string][]byte, error)
  43. // UniqueIdentity returns the unique identity of the plugin
  44. UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error)
  45. // Checksum returns the checksum of the plugin
  46. Checksum() (string, error)
  47. }
  48. type PluginDecoderHelper struct {
  49. pluginDeclaration *plugin_entities.PluginDeclaration
  50. checksum string
  51. }
  52. func (p *PluginDecoderHelper) Manifest(decoder PluginDecoder) (plugin_entities.PluginDeclaration, error) {
  53. if p.pluginDeclaration != nil {
  54. return *p.pluginDeclaration, nil
  55. }
  56. // read the manifest file
  57. manifest, err := decoder.ReadFile("manifest.yaml")
  58. if err != nil {
  59. return plugin_entities.PluginDeclaration{}, err
  60. }
  61. dec, err := parser.UnmarshalYamlBytes[plugin_entities.PluginDeclaration](manifest)
  62. if err != nil {
  63. return plugin_entities.PluginDeclaration{}, err
  64. }
  65. // try to load plugins
  66. plugins := dec.Plugins
  67. for _, tool := range plugins.Tools {
  68. // read yaml
  69. plugin_yaml, err := decoder.ReadFile(tool)
  70. if err != nil {
  71. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tool file: %s", tool))
  72. }
  73. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolProviderDeclaration](plugin_yaml)
  74. if err != nil {
  75. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", tool))
  76. }
  77. // read tools
  78. for _, tool_file := range plugin_dec.ToolFiles {
  79. tool_file_content, err := decoder.ReadFile(tool_file)
  80. if err != nil {
  81. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tool file: %s", tool_file))
  82. }
  83. tool_file_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolDeclaration](tool_file_content)
  84. if err != nil {
  85. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tool file: %s", tool_file))
  86. }
  87. plugin_dec.Tools = append(plugin_dec.Tools, tool_file_dec)
  88. }
  89. dec.Tool = &plugin_dec
  90. }
  91. for _, endpoint := range plugins.Endpoints {
  92. // read yaml
  93. plugin_yaml, err := decoder.ReadFile(endpoint)
  94. if err != nil {
  95. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read endpoint file: %s", endpoint))
  96. }
  97. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointProviderDeclaration](plugin_yaml)
  98. if err != nil {
  99. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", endpoint))
  100. }
  101. // read detailed endpoints
  102. endpoints_files := plugin_dec.EndpointFiles
  103. for _, endpoint_file := range endpoints_files {
  104. endpoint_file_content, err := decoder.ReadFile(endpoint_file)
  105. if err != nil {
  106. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read endpoint file: %s", endpoint_file))
  107. }
  108. endpoint_file_dec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointDeclaration](endpoint_file_content)
  109. if err != nil {
  110. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal endpoint file: %s", endpoint_file))
  111. }
  112. plugin_dec.Endpoints = append(plugin_dec.Endpoints, endpoint_file_dec)
  113. }
  114. dec.Endpoint = &plugin_dec
  115. }
  116. for _, model := range plugins.Models {
  117. // read yaml
  118. plugin_yaml, err := decoder.ReadFile(model)
  119. if err != nil {
  120. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read model file: %s", model))
  121. }
  122. plugin_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelProviderDeclaration](plugin_yaml)
  123. if err != nil {
  124. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", model))
  125. }
  126. // read model position file
  127. if plugin_dec.PositionFiles != nil {
  128. plugin_dec.Position = &plugin_entities.ModelPosition{}
  129. llm_file_name, ok := plugin_dec.PositionFiles["llm"]
  130. if ok {
  131. llm_file, err := decoder.ReadFile(llm_file_name)
  132. if err != nil {
  133. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read llm position file: %s", llm_file_name))
  134. }
  135. position, err := parser.UnmarshalYamlBytes[[]string](llm_file)
  136. if err != nil {
  137. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal llm position file: %s", llm_file_name))
  138. }
  139. plugin_dec.Position.LLM = &position
  140. }
  141. text_embedding_file_name, ok := plugin_dec.PositionFiles["text_embedding"]
  142. if ok {
  143. text_embedding_file, err := decoder.ReadFile(text_embedding_file_name)
  144. if err != nil {
  145. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read text embedding position file: %s", text_embedding_file_name))
  146. }
  147. position, err := parser.UnmarshalYamlBytes[[]string](text_embedding_file)
  148. if err != nil {
  149. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal text embedding position file: %s", text_embedding_file_name))
  150. }
  151. plugin_dec.Position.TextEmbedding = &position
  152. }
  153. rerank_file_name, ok := plugin_dec.PositionFiles["rerank"]
  154. if ok {
  155. rerank_file, err := decoder.ReadFile(rerank_file_name)
  156. if err != nil {
  157. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read rerank position file: %s", rerank_file_name))
  158. }
  159. position, err := parser.UnmarshalYamlBytes[[]string](rerank_file)
  160. if err != nil {
  161. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal rerank position file: %s", rerank_file_name))
  162. }
  163. plugin_dec.Position.Rerank = &position
  164. }
  165. tts_file_name, ok := plugin_dec.PositionFiles["tts"]
  166. if ok {
  167. tts_file, err := decoder.ReadFile(tts_file_name)
  168. if err != nil {
  169. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tts position file: %s", tts_file_name))
  170. }
  171. position, err := parser.UnmarshalYamlBytes[[]string](tts_file)
  172. if err != nil {
  173. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tts position file: %s", tts_file_name))
  174. }
  175. plugin_dec.Position.TTS = &position
  176. }
  177. speech2text_file_name, ok := plugin_dec.PositionFiles["speech2text"]
  178. if ok {
  179. speech2text_file, err := decoder.ReadFile(speech2text_file_name)
  180. if err != nil {
  181. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read speech2text position file: %s", speech2text_file_name))
  182. }
  183. position, err := parser.UnmarshalYamlBytes[[]string](speech2text_file)
  184. if err != nil {
  185. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal speech2text position file: %s", speech2text_file_name))
  186. }
  187. plugin_dec.Position.Speech2text = &position
  188. }
  189. moderation_file_name, ok := plugin_dec.PositionFiles["moderation"]
  190. if ok {
  191. moderation_file, err := decoder.ReadFile(moderation_file_name)
  192. if err != nil {
  193. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read moderation position file: %s", moderation_file_name))
  194. }
  195. position, err := parser.UnmarshalYamlBytes[[]string](moderation_file)
  196. if err != nil {
  197. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal moderation position file: %s", moderation_file_name))
  198. }
  199. plugin_dec.Position.Moderation = &position
  200. }
  201. }
  202. // read models
  203. if err := decoder.Walk(func(filename, dir string) error {
  204. model_patterns := plugin_dec.ModelFiles
  205. // using glob to match if dir/filename is in models
  206. model_file_name := filepath.Join(dir, filename)
  207. if strings.HasSuffix(model_file_name, "_position.yaml") {
  208. return nil
  209. }
  210. for _, model_pattern := range model_patterns {
  211. matched, err := filepath.Match(model_pattern, model_file_name)
  212. if err != nil {
  213. return err
  214. }
  215. if matched {
  216. // read model file
  217. model_file, err := decoder.ReadFile(model_file_name)
  218. if err != nil {
  219. return err
  220. }
  221. model_dec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelDeclaration](model_file)
  222. if err != nil {
  223. return err
  224. }
  225. plugin_dec.Models = append(plugin_dec.Models, model_dec)
  226. }
  227. }
  228. return nil
  229. }); err != nil {
  230. return plugin_entities.PluginDeclaration{}, err
  231. }
  232. dec.Model = &plugin_dec
  233. }
  234. dec.FillInDefaultValues()
  235. if err := dec.ManifestValidate(); err != nil {
  236. return plugin_entities.PluginDeclaration{}, err
  237. }
  238. p.pluginDeclaration = &dec
  239. return dec, nil
  240. }
  241. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  242. files, err := decoder.ReadDir("_assets")
  243. if err != nil {
  244. return nil, err
  245. }
  246. assets := make(map[string][]byte)
  247. for _, file := range files {
  248. content, err := decoder.ReadFile(file)
  249. if err != nil {
  250. return nil, err
  251. }
  252. // trim _assets
  253. file, _ = strings.CutPrefix(file, "_assets/")
  254. assets[file] = content
  255. }
  256. return assets, nil
  257. }
  258. func (p *PluginDecoderHelper) Checksum(decoder PluginDecoder) (string, error) {
  259. if p.checksum != "" {
  260. return p.checksum, nil
  261. }
  262. var err error
  263. p.checksum, err = CalculateChecksum(decoder)
  264. if err != nil {
  265. return "", err
  266. }
  267. return p.checksum, nil
  268. }
  269. func (p *PluginDecoderHelper) UniqueIdentity(decoder PluginDecoder) (plugin_entities.PluginUniqueIdentifier, error) {
  270. manifest, err := decoder.Manifest()
  271. if err != nil {
  272. return plugin_entities.PluginUniqueIdentifier(""), err
  273. }
  274. identity := manifest.Identity()
  275. checksum, err := decoder.Checksum()
  276. if err != nil {
  277. return plugin_entities.PluginUniqueIdentifier(""), err
  278. }
  279. return plugin_entities.NewPluginUniqueIdentifier(fmt.Sprintf("%s@%s", identity, checksum))
  280. }