init.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package init
  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/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  12. )
  13. //go:embed templates/python/icon.svg
  14. var icon []byte
  15. func InitPlugin() {
  16. m := initialize()
  17. p := tea.NewProgram(m)
  18. if result, err := p.Run(); err != nil {
  19. fmt.Println("Error running program:", err)
  20. } else {
  21. if m, ok := result.(model); ok {
  22. if m.completed {
  23. m.createPlugin()
  24. }
  25. } else {
  26. log.Error("Error running program:", err)
  27. return
  28. }
  29. }
  30. }
  31. type subMenuKey string
  32. const (
  33. SUB_MENU_KEY_PROFILE subMenuKey = "profile"
  34. SUB_MENU_KEY_LANGUAGE subMenuKey = "language"
  35. SUB_MENU_KEY_CATEGORY subMenuKey = "category"
  36. SUB_MENU_KEY_PERMISSION subMenuKey = "permission"
  37. )
  38. type model struct {
  39. subMenus map[subMenuKey]subMenu
  40. subMenuSeq []subMenuKey
  41. currentSubMenu subMenuKey
  42. completed bool
  43. }
  44. func initialize() model {
  45. m := model{}
  46. m.subMenus = map[subMenuKey]subMenu{
  47. SUB_MENU_KEY_PROFILE: newProfile(),
  48. SUB_MENU_KEY_LANGUAGE: newLanguage(),
  49. SUB_MENU_KEY_CATEGORY: newCategory(),
  50. SUB_MENU_KEY_PERMISSION: newPermission(),
  51. }
  52. m.currentSubMenu = SUB_MENU_KEY_PROFILE
  53. m.subMenuSeq = []subMenuKey{
  54. SUB_MENU_KEY_PROFILE,
  55. SUB_MENU_KEY_LANGUAGE,
  56. SUB_MENU_KEY_CATEGORY,
  57. SUB_MENU_KEY_PERMISSION,
  58. }
  59. return m
  60. }
  61. func (m model) Init() tea.Cmd {
  62. return nil
  63. }
  64. func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  65. currentSubMenu, event, cmd := m.subMenus[m.currentSubMenu].Update(msg)
  66. m.subMenus[m.currentSubMenu] = currentSubMenu
  67. switch event {
  68. case SUB_MENU_EVENT_NEXT:
  69. if m.currentSubMenu != m.subMenuSeq[len(m.subMenuSeq)-1] {
  70. // move the current sub menu to the next one
  71. for i, key := range m.subMenuSeq {
  72. if key == m.currentSubMenu {
  73. m.currentSubMenu = m.subMenuSeq[i+1]
  74. break
  75. }
  76. }
  77. } else {
  78. m.completed = true
  79. return m, tea.Quit
  80. }
  81. case SUB_MENU_EVENT_PREV:
  82. if m.currentSubMenu != m.subMenuSeq[0] {
  83. // move the current sub menu to the previous one
  84. for i, key := range m.subMenuSeq {
  85. if key == m.currentSubMenu {
  86. m.currentSubMenu = m.subMenuSeq[i-1]
  87. break
  88. }
  89. }
  90. }
  91. }
  92. return m, cmd
  93. }
  94. func (m model) View() string {
  95. return m.subMenus[m.currentSubMenu].View()
  96. }
  97. func (m model) createPlugin() {
  98. permission := m.subMenus[SUB_MENU_KEY_PERMISSION].(permission).Permission()
  99. manifest := &plugin_entities.PluginDeclaration{
  100. PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{
  101. Version: "0.0.1",
  102. Type: plugin_entities.PluginType,
  103. Icon: "icon.svg",
  104. Author: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Author(),
  105. Name: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name(),
  106. Description: plugin_entities.I18nObject{
  107. EnUS: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Description(),
  108. },
  109. CreatedAt: time.Now(),
  110. Resource: plugin_entities.PluginResourceRequirement{
  111. Memory: 1024 * 1024 * 256, // 256MB
  112. Permission: &permission,
  113. },
  114. Label: plugin_entities.I18nObject{
  115. EnUS: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name(),
  116. },
  117. },
  118. }
  119. category_string := m.subMenus[SUB_MENU_KEY_CATEGORY].(category).Category()
  120. if category_string == "tool" {
  121. manifest.Plugins.Tools = []string{fmt.Sprintf("provider/%s.yaml", manifest.Name)}
  122. }
  123. manifest.Meta = plugin_entities.PluginMeta{
  124. Version: "0.0.1",
  125. Arch: []constants.Arch{
  126. constants.AMD64,
  127. constants.ARM64,
  128. },
  129. Runner: plugin_entities.PluginRunner{},
  130. }
  131. switch m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language() {
  132. case constants.Python:
  133. manifest.Meta.Runner.Entrypoint = "main"
  134. manifest.Meta.Runner.Language = constants.Python
  135. manifest.Meta.Runner.Version = "3.10"
  136. default:
  137. log.Error("unsupported language: %s", m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language())
  138. return
  139. }
  140. success := false
  141. clear := func() {
  142. if !success {
  143. os.RemoveAll(manifest.Name)
  144. }
  145. }
  146. defer clear()
  147. manifest_file := marshalYamlBytes(manifest)
  148. // create the plugin directory
  149. cwd, err := os.Getwd()
  150. if err != nil {
  151. log.Error("failed to get current working directory: %s", err)
  152. return
  153. }
  154. plugin_dir := filepath.Join(cwd, manifest.Name)
  155. if err := os.MkdirAll(plugin_dir, 0o755); err != nil {
  156. log.Error("failed to create plugin directory: %s", err)
  157. return
  158. }
  159. manifest_file_path := filepath.Join(plugin_dir, "manifest.yaml")
  160. if err := os.WriteFile(manifest_file_path, manifest_file, 0o644); err != nil {
  161. log.Error("failed to write manifest file: %s", err)
  162. return
  163. }
  164. // create _assets directory
  165. assets_dir := filepath.Join(plugin_dir, "_assets")
  166. if err := os.MkdirAll(assets_dir, 0o755); err != nil {
  167. log.Error("failed to create assets directory: %s", err)
  168. return
  169. }
  170. // create icon.svg
  171. icon_file_path := filepath.Join(assets_dir, "icon.svg")
  172. if err := os.WriteFile(icon_file_path, icon, 0o644); err != nil {
  173. log.Error("failed to write icon file: %s", err)
  174. return
  175. }
  176. err = createPythonEnvironment(
  177. plugin_dir,
  178. manifest.Meta.Runner.Entrypoint,
  179. manifest,
  180. m.subMenus[SUB_MENU_KEY_CATEGORY].(category).Category(),
  181. )
  182. if err != nil {
  183. log.Error("failed to create python environment: %s", err)
  184. return
  185. }
  186. success = true
  187. log.Info("plugin %s created successfully", manifest.Name)
  188. }