init.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package plugin
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. _ "embed"
  8. tea "github.com/charmbracelet/bubbletea"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  13. )
  14. //go:embed templates/python/icon.svg
  15. var icon []byte
  16. func InitPlugin() {
  17. m := initialize()
  18. p := tea.NewProgram(m)
  19. if result, err := p.Run(); err != nil {
  20. fmt.Println("Error running program:", err)
  21. } else {
  22. if m, ok := result.(model); ok {
  23. if m.completed {
  24. m.createPlugin()
  25. }
  26. } else {
  27. log.Error("Error running program:", err)
  28. return
  29. }
  30. }
  31. }
  32. type subMenuKey string
  33. const (
  34. SUB_MENU_KEY_PROFILE subMenuKey = "profile"
  35. SUB_MENU_KEY_LANGUAGE subMenuKey = "language"
  36. SUB_MENU_KEY_CATEGORY subMenuKey = "category"
  37. SUB_MENU_KEY_PERMISSION subMenuKey = "permission"
  38. )
  39. type model struct {
  40. subMenus map[subMenuKey]subMenu
  41. subMenuSeq []subMenuKey
  42. currentSubMenu subMenuKey
  43. completed bool
  44. }
  45. func initialize() model {
  46. m := model{}
  47. m.subMenus = map[subMenuKey]subMenu{
  48. SUB_MENU_KEY_PROFILE: newProfile(),
  49. SUB_MENU_KEY_LANGUAGE: newLanguage(),
  50. SUB_MENU_KEY_CATEGORY: newCategory(),
  51. SUB_MENU_KEY_PERMISSION: newPermission(plugin_entities.PluginPermissionRequirement{}),
  52. }
  53. m.currentSubMenu = SUB_MENU_KEY_PROFILE
  54. m.subMenuSeq = []subMenuKey{
  55. SUB_MENU_KEY_PROFILE,
  56. SUB_MENU_KEY_LANGUAGE,
  57. SUB_MENU_KEY_CATEGORY,
  58. SUB_MENU_KEY_PERMISSION,
  59. }
  60. return m
  61. }
  62. func (m model) Init() tea.Cmd {
  63. return nil
  64. }
  65. func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  66. currentSubMenu, event, cmd := m.subMenus[m.currentSubMenu].Update(msg)
  67. m.subMenus[m.currentSubMenu] = currentSubMenu
  68. switch event {
  69. case SUB_MENU_EVENT_NEXT:
  70. if m.currentSubMenu != m.subMenuSeq[len(m.subMenuSeq)-1] {
  71. // move the current sub menu to the next one
  72. for i, key := range m.subMenuSeq {
  73. if key == m.currentSubMenu {
  74. // check if the next sub menu is permission
  75. if key == SUB_MENU_KEY_CATEGORY {
  76. // get the type of current category
  77. category := m.subMenus[SUB_MENU_KEY_CATEGORY].(category).Category()
  78. if category == "agent-strategy" {
  79. // update the permission to add tool and model invocation
  80. perm := m.subMenus[SUB_MENU_KEY_PERMISSION].(permission)
  81. perm.UpdatePermission(plugin_entities.PluginPermissionRequirement{
  82. Tool: &plugin_entities.PluginPermissionToolRequirement{
  83. Enabled: true,
  84. },
  85. Model: &plugin_entities.PluginPermissionModelRequirement{
  86. Enabled: true,
  87. LLM: true,
  88. },
  89. })
  90. m.subMenus[SUB_MENU_KEY_PERMISSION] = perm
  91. }
  92. }
  93. m.currentSubMenu = m.subMenuSeq[i+1]
  94. break
  95. }
  96. }
  97. } else {
  98. m.completed = true
  99. return m, tea.Quit
  100. }
  101. case SUB_MENU_EVENT_PREV:
  102. if m.currentSubMenu != m.subMenuSeq[0] {
  103. // move the current sub menu to the previous one
  104. for i, key := range m.subMenuSeq {
  105. if key == m.currentSubMenu {
  106. m.currentSubMenu = m.subMenuSeq[i-1]
  107. break
  108. }
  109. }
  110. }
  111. }
  112. return m, cmd
  113. }
  114. func (m model) View() string {
  115. return m.subMenus[m.currentSubMenu].View()
  116. }
  117. func (m model) createPlugin() {
  118. permission := m.subMenus[SUB_MENU_KEY_PERMISSION].(permission).Permission()
  119. manifest := &plugin_entities.PluginDeclaration{
  120. PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{
  121. Version: manifest_entities.Version("0.0.1"),
  122. Type: manifest_entities.PluginType,
  123. Icon: "icon.svg",
  124. Author: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Author(),
  125. Name: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name(),
  126. Description: plugin_entities.NewI18nObject(m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Description()),
  127. CreatedAt: time.Now(),
  128. Resource: plugin_entities.PluginResourceRequirement{
  129. Memory: 1024 * 1024 * 256, // 256MB
  130. Permission: &permission,
  131. },
  132. Label: plugin_entities.NewI18nObject(m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name()),
  133. },
  134. }
  135. categoryString := m.subMenus[SUB_MENU_KEY_CATEGORY].(category).Category()
  136. if categoryString == "tool" {
  137. manifest.Plugins.Tools = []string{fmt.Sprintf("provider/%s.yaml", manifest.Name)}
  138. }
  139. if categoryString == "llm" ||
  140. categoryString == "text-embedding" ||
  141. categoryString == "speech2text" ||
  142. categoryString == "moderation" ||
  143. categoryString == "rerank" ||
  144. categoryString == "tts" {
  145. manifest.Plugins.Models = []string{fmt.Sprintf("provider/%s.yaml", manifest.Name)}
  146. }
  147. if categoryString == "extension" {
  148. manifest.Plugins.Endpoints = []string{fmt.Sprintf("group/%s.yaml", manifest.Name)}
  149. }
  150. if categoryString == "agent-strategy" {
  151. manifest.Plugins.AgentStrategies = []string{fmt.Sprintf("provider/%s.yaml", manifest.Name)}
  152. }
  153. manifest.Meta = plugin_entities.PluginMeta{
  154. Version: "0.0.1",
  155. Arch: []constants.Arch{
  156. constants.AMD64,
  157. constants.ARM64,
  158. },
  159. Runner: plugin_entities.PluginRunner{},
  160. }
  161. switch m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language() {
  162. case constants.Python:
  163. manifest.Meta.Runner.Entrypoint = "main"
  164. manifest.Meta.Runner.Language = constants.Python
  165. manifest.Meta.Runner.Version = "3.12"
  166. default:
  167. log.Error("unsupported language: %s", m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language())
  168. return
  169. }
  170. success := false
  171. clear := func() {
  172. if !success {
  173. os.RemoveAll(manifest.Name)
  174. }
  175. }
  176. defer clear()
  177. manifestFile := marshalYamlBytes(manifest)
  178. // create the plugin directory
  179. cwd, err := os.Getwd()
  180. if err != nil {
  181. log.Error("failed to get current working directory: %s", err)
  182. return
  183. }
  184. pluginDir := filepath.Join(cwd, manifest.Name)
  185. if err := writeFile(filepath.Join(pluginDir, "manifest.yaml"), string(manifestFile)); err != nil {
  186. log.Error("failed to write manifest file: %s", err)
  187. return
  188. }
  189. // create icon.svg
  190. if err := writeFile(filepath.Join(pluginDir, "_assets", "icon.svg"), string(icon)); err != nil {
  191. log.Error("failed to write icon file: %s", err)
  192. return
  193. }
  194. // create README.md
  195. readme, err := renderTemplate(README, manifest, []string{})
  196. if err != nil {
  197. log.Error("failed to render README template: %s", err)
  198. return
  199. }
  200. if err := writeFile(filepath.Join(pluginDir, "README.md"), readme); err != nil {
  201. log.Error("failed to write README file: %s", err)
  202. return
  203. }
  204. // create .env.example
  205. if err := writeFile(filepath.Join(pluginDir, ".env.example"), string(ENV_EXAMPLE)); err != nil {
  206. log.Error("failed to write .env.example file: %s", err)
  207. return
  208. }
  209. err = createPythonEnvironment(
  210. pluginDir,
  211. manifest.Meta.Runner.Entrypoint,
  212. manifest,
  213. m.subMenus[SUB_MENU_KEY_CATEGORY].(category).Category(),
  214. )
  215. if err != nil {
  216. log.Error("failed to create python environment: %s", err)
  217. return
  218. }
  219. success = true
  220. log.Info("plugin %s created successfully, you can refer to `%s/GUIDE.md` for more information about how to develop it", manifest.Name, manifest.Name)
  221. }