init.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "time"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. )
  11. type subMenuKey string
  12. const (
  13. SUB_MENU_KEY_PROFILE subMenuKey = "profile"
  14. SUB_MENU_KEY_LANGUAGE subMenuKey = "language"
  15. SUB_MENU_KEY_PERMISSION subMenuKey = "permission"
  16. )
  17. type model struct {
  18. subMenus map[subMenuKey]subMenu
  19. subMenuSeq []subMenuKey
  20. currentSubMenu subMenuKey
  21. }
  22. func initialize() model {
  23. m := model{}
  24. m.subMenus = map[subMenuKey]subMenu{
  25. SUB_MENU_KEY_PROFILE: newProfile(),
  26. SUB_MENU_KEY_LANGUAGE: newLanguage(),
  27. SUB_MENU_KEY_PERMISSION: newPermission(),
  28. }
  29. m.currentSubMenu = SUB_MENU_KEY_PROFILE
  30. m.subMenuSeq = []subMenuKey{
  31. SUB_MENU_KEY_PROFILE,
  32. SUB_MENU_KEY_LANGUAGE,
  33. SUB_MENU_KEY_PERMISSION,
  34. }
  35. return m
  36. }
  37. func (m model) Init() tea.Cmd {
  38. return nil
  39. }
  40. func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  41. currentSubMenu, event, cmd := m.subMenus[m.currentSubMenu].Update(msg)
  42. m.subMenus[m.currentSubMenu] = currentSubMenu
  43. switch event {
  44. case SUB_MENU_EVENT_NEXT:
  45. if m.currentSubMenu != m.subMenuSeq[len(m.subMenuSeq)-1] {
  46. // move the current sub menu to the next one
  47. for i, key := range m.subMenuSeq {
  48. if key == m.currentSubMenu {
  49. m.currentSubMenu = m.subMenuSeq[i+1]
  50. break
  51. }
  52. }
  53. } else {
  54. return m, tea.Quit
  55. }
  56. case SUB_MENU_EVENT_PREV:
  57. if m.currentSubMenu != m.subMenuSeq[0] {
  58. // move the current sub menu to the previous one
  59. for i, key := range m.subMenuSeq {
  60. if key == m.currentSubMenu {
  61. m.currentSubMenu = m.subMenuSeq[i-1]
  62. break
  63. }
  64. }
  65. }
  66. }
  67. return m, cmd
  68. }
  69. func (m model) View() string {
  70. return m.subMenus[m.currentSubMenu].View()
  71. }
  72. func (m model) createPlugin() {
  73. permission := m.subMenus[SUB_MENU_KEY_PERMISSION].(permission).Permission()
  74. manifest := &plugin_entities.PluginDeclaration{
  75. Version: "0.0.1",
  76. Type: plugin_entities.PluginType,
  77. Author: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Author(),
  78. Name: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name(),
  79. CreatedAt: time.Now(),
  80. Resource: plugin_entities.PluginResourceRequirement{
  81. Permission: &permission,
  82. },
  83. Label: plugin_entities.I18nObject{
  84. EnUS: m.subMenus[SUB_MENU_KEY_PROFILE].(profile).Name(),
  85. },
  86. }
  87. manifest.Meta = plugin_entities.PluginMeta{
  88. Version: "0.0.1",
  89. Arch: []constants.Arch{
  90. constants.AMD64,
  91. constants.ARM64,
  92. },
  93. Runner: plugin_entities.PluginRunner{},
  94. }
  95. switch m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language() {
  96. case constants.Python:
  97. manifest.Meta.Runner.Entrypoint = "main"
  98. manifest.Meta.Runner.Language = constants.Python
  99. manifest.Meta.Runner.Version = "3.10"
  100. default:
  101. log.Error("unsupported language: %s", m.subMenus[SUB_MENU_KEY_LANGUAGE].(language).Language())
  102. return
  103. }
  104. success := false
  105. clear := func() {
  106. if !success {
  107. os.RemoveAll(manifest.Name)
  108. }
  109. }
  110. defer clear()
  111. manifest_file := marshalYamlBytes(manifest)
  112. // create the plugin directory
  113. cwd, err := os.Getwd()
  114. if err != nil {
  115. log.Error("failed to get current working directory: %s", err)
  116. return
  117. }
  118. plugin_dir := filepath.Join(cwd, manifest.Name)
  119. if err := os.MkdirAll(plugin_dir, 0o755); err != nil {
  120. log.Error("failed to create plugin directory: %s", err)
  121. return
  122. }
  123. manifest_file_path := filepath.Join(plugin_dir, "manifest.yaml")
  124. if err := os.WriteFile(manifest_file_path, manifest_file, 0o644); err != nil {
  125. log.Error("failed to write manifest file: %s", err)
  126. return
  127. }
  128. err = createPythonEnvironment(plugin_dir, manifest.Meta.Runner.Entrypoint)
  129. if err != nil {
  130. log.Error("failed to create python environment: %s", err)
  131. return
  132. }
  133. success = true
  134. log.Info("plugin %s created successfully", manifest.Name)
  135. }