helper.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package decoder
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  8. "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
  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. pluginYaml, 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. pluginDec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolProviderDeclaration](pluginYaml)
  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 pluginDec.ToolFiles {
  41. toolFileContent, 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. toolFileDec, err := parser.UnmarshalYamlBytes[plugin_entities.ToolDeclaration](toolFileContent)
  46. if err != nil {
  47. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tool file: %s", tool_file))
  48. }
  49. pluginDec.Tools = append(pluginDec.Tools, toolFileDec)
  50. }
  51. dec.Tool = &pluginDec
  52. }
  53. for _, endpoint := range plugins.Endpoints {
  54. // read yaml
  55. pluginYaml, 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. pluginDec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointProviderDeclaration](pluginYaml)
  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. endpointsFiles := pluginDec.EndpointFiles
  65. for _, endpoint_file := range endpointsFiles {
  66. endpointFileContent, 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. endpointFileDec, err := parser.UnmarshalYamlBytes[plugin_entities.EndpointDeclaration](endpointFileContent)
  71. if err != nil {
  72. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal endpoint file: %s", endpoint_file))
  73. }
  74. pluginDec.Endpoints = append(pluginDec.Endpoints, endpointFileDec)
  75. }
  76. dec.Endpoint = &pluginDec
  77. }
  78. for _, model := range plugins.Models {
  79. // read yaml
  80. pluginYaml, 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. pluginDec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelProviderDeclaration](pluginYaml)
  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 pluginDec.PositionFiles != nil {
  90. pluginDec.Position = &plugin_entities.ModelPosition{}
  91. llmFileName, ok := pluginDec.PositionFiles["llm"]
  92. if ok {
  93. llmFile, err := decoder.ReadFile(llmFileName)
  94. if err != nil {
  95. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read llm position file: %s", llmFileName))
  96. }
  97. position, err := parser.UnmarshalYamlBytes[[]string](llmFile)
  98. if err != nil {
  99. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal llm position file: %s", llmFileName))
  100. }
  101. pluginDec.Position.LLM = &position
  102. }
  103. textEmbeddingFileName, ok := pluginDec.PositionFiles["text_embedding"]
  104. if ok {
  105. textEmbeddingFile, err := decoder.ReadFile(textEmbeddingFileName)
  106. if err != nil {
  107. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read text embedding position file: %s", textEmbeddingFileName))
  108. }
  109. position, err := parser.UnmarshalYamlBytes[[]string](textEmbeddingFile)
  110. if err != nil {
  111. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal text embedding position file: %s", textEmbeddingFileName))
  112. }
  113. pluginDec.Position.TextEmbedding = &position
  114. }
  115. rerankFileName, ok := pluginDec.PositionFiles["rerank"]
  116. if ok {
  117. rerankFile, err := decoder.ReadFile(rerankFileName)
  118. if err != nil {
  119. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read rerank position file: %s", rerankFileName))
  120. }
  121. position, err := parser.UnmarshalYamlBytes[[]string](rerankFile)
  122. if err != nil {
  123. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal rerank position file: %s", rerankFileName))
  124. }
  125. pluginDec.Position.Rerank = &position
  126. }
  127. ttsFileName, ok := pluginDec.PositionFiles["tts"]
  128. if ok {
  129. ttsFile, err := decoder.ReadFile(ttsFileName)
  130. if err != nil {
  131. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read tts position file: %s", ttsFileName))
  132. }
  133. position, err := parser.UnmarshalYamlBytes[[]string](ttsFile)
  134. if err != nil {
  135. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal tts position file: %s", ttsFileName))
  136. }
  137. pluginDec.Position.TTS = &position
  138. }
  139. speech2textFileName, ok := pluginDec.PositionFiles["speech2text"]
  140. if ok {
  141. speech2textFile, err := decoder.ReadFile(speech2textFileName)
  142. if err != nil {
  143. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read speech2text position file: %s", speech2textFileName))
  144. }
  145. position, err := parser.UnmarshalYamlBytes[[]string](speech2textFile)
  146. if err != nil {
  147. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal speech2text position file: %s", speech2textFileName))
  148. }
  149. pluginDec.Position.Speech2text = &position
  150. }
  151. moderationFileName, ok := pluginDec.PositionFiles["moderation"]
  152. if ok {
  153. moderationFile, err := decoder.ReadFile(moderationFileName)
  154. if err != nil {
  155. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read moderation position file: %s", moderationFileName))
  156. }
  157. position, err := parser.UnmarshalYamlBytes[[]string](moderationFile)
  158. if err != nil {
  159. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal moderation position file: %s", moderationFileName))
  160. }
  161. pluginDec.Position.Moderation = &position
  162. }
  163. }
  164. // read models
  165. if err := decoder.Walk(func(filename, dir string) error {
  166. modelPatterns := pluginDec.ModelFiles
  167. // using glob to match if dir/filename is in models
  168. modelFileName := filepath.Join(dir, filename)
  169. if strings.HasSuffix(modelFileName, "_position.yaml") {
  170. return nil
  171. }
  172. for _, model_pattern := range modelPatterns {
  173. matched, err := filepath.Match(model_pattern, modelFileName)
  174. if err != nil {
  175. return err
  176. }
  177. if matched {
  178. // read model file
  179. modelFile, err := decoder.ReadFile(modelFileName)
  180. if err != nil {
  181. return err
  182. }
  183. modelDec, err := parser.UnmarshalYamlBytes[plugin_entities.ModelDeclaration](modelFile)
  184. if err != nil {
  185. return err
  186. }
  187. pluginDec.Models = append(pluginDec.Models, modelDec)
  188. }
  189. }
  190. return nil
  191. }); err != nil {
  192. return plugin_entities.PluginDeclaration{}, err
  193. }
  194. dec.Model = &pluginDec
  195. }
  196. for _, agentStrategy := range plugins.AgentStrategies {
  197. // read yaml
  198. pluginYaml, err := decoder.ReadFile(agentStrategy)
  199. if err != nil {
  200. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read agent strategy file: %s", agentStrategy))
  201. }
  202. pluginDec, err := parser.UnmarshalYamlBytes[plugin_entities.AgentStrategyProviderDeclaration](pluginYaml)
  203. if err != nil {
  204. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal plugin file: %s", agentStrategy))
  205. }
  206. for _, strategyFile := range pluginDec.StrategyFiles {
  207. strategyFileContent, err := decoder.ReadFile(strategyFile)
  208. if err != nil {
  209. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to read agent strategy file: %s", strategyFile))
  210. }
  211. strategyDec, err := parser.UnmarshalYamlBytes[plugin_entities.AgentStrategyDeclaration](strategyFileContent)
  212. if err != nil {
  213. return plugin_entities.PluginDeclaration{}, errors.Join(err, fmt.Errorf("failed to unmarshal agent strategy file: %s", strategyFile))
  214. }
  215. pluginDec.Strategies = append(pluginDec.Strategies, strategyDec)
  216. }
  217. dec.AgentStrategy = &pluginDec
  218. }
  219. dec.FillInDefaultValues()
  220. // verify signature
  221. dec.Verified = VerifyPlugin(decoder) == nil
  222. if err := dec.ManifestValidate(); err != nil {
  223. return plugin_entities.PluginDeclaration{}, err
  224. }
  225. p.pluginDeclaration = &dec
  226. return dec, nil
  227. }
  228. func (p *PluginDecoderHelper) Assets(decoder PluginDecoder) (map[string][]byte, error) {
  229. files, err := decoder.ReadDir("_assets")
  230. if err != nil {
  231. return nil, err
  232. }
  233. assets := make(map[string][]byte)
  234. for _, file := range files {
  235. content, err := decoder.ReadFile(file)
  236. if err != nil {
  237. return nil, err
  238. }
  239. // trim _assets
  240. file, _ = strings.CutPrefix(file, "_assets"+string(filepath.Separator))
  241. assets[file] = content
  242. }
  243. return assets, nil
  244. }
  245. func (p *PluginDecoderHelper) Checksum(decoder_instance PluginDecoder) (string, error) {
  246. if p.checksum != "" {
  247. return p.checksum, nil
  248. }
  249. var err error
  250. p.checksum, err = CalculateChecksum(decoder_instance)
  251. if err != nil {
  252. return "", err
  253. }
  254. return p.checksum, nil
  255. }
  256. func (p *PluginDecoderHelper) UniqueIdentity(decoder PluginDecoder) (plugin_entities.PluginUniqueIdentifier, error) {
  257. manifest, err := decoder.Manifest()
  258. if err != nil {
  259. return plugin_entities.PluginUniqueIdentifier(""), err
  260. }
  261. identity := manifest.Identity()
  262. checksum, err := decoder.Checksum()
  263. if err != nil {
  264. return plugin_entities.PluginUniqueIdentifier(""), err
  265. }
  266. return plugin_entities.NewPluginUniqueIdentifier(fmt.Sprintf("%s@%s", identity, checksum))
  267. }
  268. func (p *PluginDecoderHelper) CheckAssetsValid(decoder PluginDecoder) error {
  269. declaration, err := decoder.Manifest()
  270. if err != nil {
  271. return errors.Join(err, fmt.Errorf("failed to get manifest"))
  272. }
  273. assets, err := decoder.Assets()
  274. if err != nil {
  275. return errors.Join(err, fmt.Errorf("failed to get assets"))
  276. }
  277. if declaration.Model != nil {
  278. if declaration.Model.IconSmall != nil {
  279. if declaration.Model.IconSmall.EnUS != "" {
  280. if _, ok := assets[declaration.Model.IconSmall.EnUS]; !ok {
  281. return errors.Join(err, fmt.Errorf("model icon small en_US not found"))
  282. }
  283. }
  284. if declaration.Model.IconSmall.ZhHans != "" {
  285. if _, ok := assets[declaration.Model.IconSmall.ZhHans]; !ok {
  286. return errors.Join(err, fmt.Errorf("model icon small zh_Hans not found"))
  287. }
  288. }
  289. if declaration.Model.IconSmall.JaJp != "" {
  290. if _, ok := assets[declaration.Model.IconSmall.JaJp]; !ok {
  291. return errors.Join(err, fmt.Errorf("model icon small ja_JP not found"))
  292. }
  293. }
  294. if declaration.Model.IconSmall.PtBr != "" {
  295. if _, ok := assets[declaration.Model.IconSmall.PtBr]; !ok {
  296. return errors.Join(err, fmt.Errorf("model icon small pt_BR not found"))
  297. }
  298. }
  299. }
  300. if declaration.Model.IconLarge != nil {
  301. if declaration.Model.IconLarge.EnUS != "" {
  302. if _, ok := assets[declaration.Model.IconLarge.EnUS]; !ok {
  303. return errors.Join(err, fmt.Errorf("model icon large en_US not found"))
  304. }
  305. }
  306. if declaration.Model.IconLarge.ZhHans != "" {
  307. if _, ok := assets[declaration.Model.IconLarge.ZhHans]; !ok {
  308. return errors.Join(err, fmt.Errorf("model icon large zh_Hans not found"))
  309. }
  310. }
  311. if declaration.Model.IconLarge.JaJp != "" {
  312. if _, ok := assets[declaration.Model.IconLarge.JaJp]; !ok {
  313. return errors.Join(err, fmt.Errorf("model icon large ja_JP not found"))
  314. }
  315. }
  316. if declaration.Model.IconLarge.PtBr != "" {
  317. if _, ok := assets[declaration.Model.IconLarge.PtBr]; !ok {
  318. return errors.Join(err, fmt.Errorf("model icon large pt_BR not found"))
  319. }
  320. }
  321. }
  322. }
  323. if declaration.Tool != nil {
  324. if declaration.Tool.Identity.Icon != "" {
  325. if _, ok := assets[declaration.Tool.Identity.Icon]; !ok {
  326. return errors.Join(err, fmt.Errorf("tool icon not found"))
  327. }
  328. }
  329. }
  330. if declaration.Icon != "" {
  331. if _, ok := assets[declaration.Icon]; !ok {
  332. return errors.Join(err, fmt.Errorf("plugin icon not found"))
  333. }
  334. }
  335. return nil
  336. }