python.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package init
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. )
  10. //go:embed templates/python/main.py
  11. var PYTHON_ENTRYPOINT_TEMPLATE []byte
  12. //go:embed templates/python/requirements.txt
  13. var PYTHON_REQUIREMENTS_TEMPLATE []byte
  14. //go:embed templates/python/tool_provider.yaml
  15. var PYTHON_TOOL_PROVIDER_TEMPLATE []byte
  16. //go:embed templates/python/tool.yaml
  17. var PYTHON_TOOL_TEMPLATE []byte
  18. //go:embed templates/python/tool.py
  19. var PYTHON_TOOL_PY_TEMPLATE []byte
  20. //go:embed templates/python/tool_provider.py
  21. var PYTHON_TOOL_PROVIDER_PY_TEMPLATE []byte
  22. func createPythonEnvironment(
  23. root string, entrypoint string, manifest *plugin_entities.PluginDeclaration, category string,
  24. ) error {
  25. // TODO: enhance to use template renderer
  26. // create the python environment
  27. entrypoint_file_path := filepath.Join(root, fmt.Sprintf("%s.py", entrypoint))
  28. if err := os.WriteFile(entrypoint_file_path, PYTHON_ENTRYPOINT_TEMPLATE, 0o644); err != nil {
  29. return err
  30. }
  31. requirements_file_path := filepath.Join(root, "requirements.txt")
  32. if err := os.WriteFile(requirements_file_path, PYTHON_REQUIREMENTS_TEMPLATE, 0o644); err != nil {
  33. return err
  34. }
  35. if category == "tool" {
  36. if err := createPythonTool(root, manifest); err != nil {
  37. return err
  38. }
  39. if err := createPythonToolProvider(root, manifest); err != nil {
  40. return err
  41. }
  42. }
  43. return nil
  44. }
  45. func createPythonTool(root string, manifest *plugin_entities.PluginDeclaration) error {
  46. // create the tool
  47. tool_dir := filepath.Join(root, "tools")
  48. if err := os.MkdirAll(tool_dir, 0o755); err != nil {
  49. return err
  50. }
  51. // replace the plugin name/author/description in the template
  52. tool_file_content := strings.ReplaceAll(
  53. string(PYTHON_TOOL_PY_TEMPLATE), "{{plugin_name}}", manifest.Name,
  54. )
  55. tool_file_content = strings.ReplaceAll(
  56. tool_file_content, "{{author}}", manifest.Author,
  57. )
  58. tool_file_content = strings.ReplaceAll(
  59. tool_file_content, "{{plugin_description}}", manifest.Description.EnUS,
  60. )
  61. tool_file_path := filepath.Join(tool_dir, fmt.Sprintf("%s.py", manifest.Name))
  62. if err := os.WriteFile(tool_file_path, []byte(tool_file_content), 0o644); err != nil {
  63. return err
  64. }
  65. // create the tool manifest
  66. tool_manifest_file_path := filepath.Join(tool_dir, fmt.Sprintf("%s.yaml", manifest.Name))
  67. if err := os.WriteFile(tool_manifest_file_path, PYTHON_TOOL_TEMPLATE, 0o644); err != nil {
  68. return err
  69. }
  70. tool_manifest_file_content := strings.ReplaceAll(
  71. string(PYTHON_TOOL_TEMPLATE), "{{plugin_name}}", manifest.Name,
  72. )
  73. tool_manifest_file_content = strings.ReplaceAll(
  74. tool_manifest_file_content, "{{author}}", manifest.Author,
  75. )
  76. tool_manifest_file_content = strings.ReplaceAll(
  77. tool_manifest_file_content, "{{plugin_description}}", manifest.Description.EnUS,
  78. )
  79. if err := os.WriteFile(tool_manifest_file_path, []byte(tool_manifest_file_content), 0o644); err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func createPythonToolProvider(root string, manifest *plugin_entities.PluginDeclaration) error {
  85. // create the tool provider
  86. tool_provider_dir := filepath.Join(root, "provider")
  87. if err := os.MkdirAll(tool_provider_dir, 0o755); err != nil {
  88. return err
  89. }
  90. // replace the plugin name/author/description in the template
  91. tool_provider_file_content := strings.ReplaceAll(
  92. string(PYTHON_TOOL_PROVIDER_PY_TEMPLATE), "{{plugin_name}}", manifest.Name,
  93. )
  94. tool_provider_file_content = strings.ReplaceAll(
  95. tool_provider_file_content, "{{author}}", manifest.Author,
  96. )
  97. tool_provider_file_content = strings.ReplaceAll(
  98. tool_provider_file_content, "{{plugin_description}}", manifest.Description.EnUS,
  99. )
  100. tool_provider_file_path := filepath.Join(tool_provider_dir, fmt.Sprintf("%s.py", manifest.Name))
  101. if err := os.WriteFile(tool_provider_file_path, []byte(tool_provider_file_content), 0o644); err != nil {
  102. return err
  103. }
  104. // create the tool provider manifest
  105. tool_provider_manifest_file_path := filepath.Join(tool_provider_dir, fmt.Sprintf("%s.yaml", manifest.Name))
  106. if err := os.WriteFile(tool_provider_manifest_file_path, PYTHON_TOOL_PROVIDER_TEMPLATE, 0o644); err != nil {
  107. return err
  108. }
  109. tool_provider_manifest_file_content := strings.ReplaceAll(
  110. string(PYTHON_TOOL_PROVIDER_TEMPLATE), "{{plugin_name}}", manifest.Name,
  111. )
  112. tool_provider_manifest_file_content = strings.ReplaceAll(
  113. tool_provider_manifest_file_content, "{{author}}", manifest.Author,
  114. )
  115. tool_provider_manifest_file_content = strings.ReplaceAll(
  116. tool_provider_manifest_file_content, "{{plugin_description}}", manifest.Description.EnUS,
  117. )
  118. if err := os.WriteFile(tool_provider_manifest_file_path, []byte(tool_provider_manifest_file_content), 0o644); err != nil {
  119. return err
  120. }
  121. return nil
  122. }