permission.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package plugin
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. ti "github.com/charmbracelet/bubbles/textinput"
  7. tea "github.com/charmbracelet/bubbletea"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. )
  10. var permissionKeySeq = []string{
  11. "tool.enabled",
  12. "model.enabled",
  13. "model.llm",
  14. "model.text_embedding",
  15. "model.rerank",
  16. "model.tts",
  17. "model.speech2text",
  18. "model.moderation",
  19. "app.enabled",
  20. "storage.enabled",
  21. "storage.size",
  22. "endpoint.enabled",
  23. }
  24. type permission struct {
  25. cursor string
  26. permission plugin_entities.PluginPermissionRequirement
  27. storageSizeEditor ti.Model
  28. }
  29. func newPermission() permission {
  30. return permission{
  31. cursor: permissionKeySeq[0],
  32. storageSizeEditor: ti.New(),
  33. }
  34. }
  35. func (p permission) Permission() plugin_entities.PluginPermissionRequirement {
  36. return p.permission
  37. }
  38. func (p permission) View() string {
  39. cursor := func(key string) string {
  40. if p.cursor == key {
  41. return "→ "
  42. }
  43. return " "
  44. }
  45. checked := func(enabled bool) string {
  46. if enabled {
  47. return fmt.Sprintf("\033[32m%s\033[0m", "[✔]")
  48. }
  49. return fmt.Sprintf("\033[31m%s\033[0m", "[✘]")
  50. }
  51. s := "Configure the permissions of the plugin, use \033[32mup\033[0m and \033[32mdown\033[0m to navigate, \033[32mtab\033[0m to select, after selection, press \033[32menter\033[0m to finish\n"
  52. s += "Backwards Invocation:\n"
  53. s += "Tools:\n"
  54. s += fmt.Sprintf(" %sEnabled: %v %s You can invoke tools inside Dify if it's enabled %s\n", cursor("tool.enabled"), checked(p.permission.AllowInvokeTool()), YELLOW, RESET)
  55. s += "Models:\n"
  56. s += fmt.Sprintf(" %sEnabled: %v %s You can invoke models inside Dify if it's enabled %s\n", cursor("model.enabled"), checked(p.permission.AllowInvokeModel()), YELLOW, RESET)
  57. s += fmt.Sprintf(" %sLLM: %v %s You can invoke LLM models inside Dify if it's enabled %s\n", cursor("model.llm"), checked(p.permission.AllowInvokeLLM()), YELLOW, RESET)
  58. s += fmt.Sprintf(" %sText Embedding: %v %s You can invoke text embedding models inside Dify if it's enabled %s\n", cursor("model.text_embedding"), checked(p.permission.AllowInvokeTextEmbedding()), YELLOW, RESET)
  59. s += fmt.Sprintf(" %sRerank: %v %s You can invoke rerank models inside Dify if it's enabled %s\n", cursor("model.rerank"), checked(p.permission.AllowInvokeRerank()), YELLOW, RESET)
  60. s += fmt.Sprintf(" %sTTS: %v %s You can invoke TTS models inside Dify if it's enabled %s\n", cursor("model.tts"), checked(p.permission.AllowInvokeTTS()), YELLOW, RESET)
  61. s += fmt.Sprintf(" %sSpeech2Text: %v %s You can invoke speech2text models inside Dify if it's enabled %s\n", cursor("model.speech2text"), checked(p.permission.AllowInvokeSpeech2Text()), YELLOW, RESET)
  62. s += fmt.Sprintf(" %sModeration: %v %s You can invoke moderation models inside Dify if it's enabled %s\n", cursor("model.moderation"), checked(p.permission.AllowInvokeModeration()), YELLOW, RESET)
  63. s += "Apps:\n"
  64. s += fmt.Sprintf(" %sEnabled: %v %s Ability to invoke apps like BasicChat/ChatFlow/Agent/Workflow etc. %s\n", cursor("app.enabled"), checked(p.permission.AllowInvokeApp()), YELLOW, RESET)
  65. s += "Resources:\n"
  66. s += "Storage:\n"
  67. s += fmt.Sprintf(" %sEnabled: %v %s Persistence storage for the plugin %s\n", cursor("storage.enabled"), checked(p.permission.AllowInvokeStorage()), YELLOW, RESET)
  68. if p.permission.AllowInvokeStorage() {
  69. s += fmt.Sprintf(" %sSize: %v\n", cursor("storage.size"), p.storageSizeEditor.View())
  70. } else {
  71. s += fmt.Sprintf(" %sSize: %v %s The maximum size of the storage %s\n", cursor("storage.size"), "N/A", YELLOW, RESET)
  72. }
  73. s += "Endpoints:\n"
  74. s += fmt.Sprintf(" %sEnabled: %v %s Ability to register endpoints %s\n", cursor("endpoint.enabled"), checked(p.permission.AllowRegisterEndpoint()), YELLOW, RESET)
  75. return s
  76. }
  77. func (p *permission) edit() {
  78. if p.cursor == "tool.enabled" {
  79. if p.permission.AllowInvokeTool() {
  80. p.permission.Tool = nil
  81. } else {
  82. p.permission.Tool = &plugin_entities.PluginPermissionToolRequirement{
  83. Enabled: true,
  84. }
  85. }
  86. }
  87. if strings.HasPrefix(p.cursor, "model.") {
  88. if p.permission.AllowInvokeModel() {
  89. if p.cursor == "model.enabled" {
  90. p.permission.Model = nil
  91. return
  92. }
  93. } else {
  94. p.permission.Model = &plugin_entities.PluginPermissionModelRequirement{
  95. Enabled: true,
  96. }
  97. }
  98. }
  99. if p.cursor == "model.llm" {
  100. if p.permission.AllowInvokeLLM() {
  101. p.permission.Model.LLM = false
  102. } else {
  103. p.permission.Model.LLM = true
  104. }
  105. }
  106. if p.cursor == "model.text_embedding" {
  107. if p.permission.AllowInvokeTextEmbedding() {
  108. p.permission.Model.TextEmbedding = false
  109. } else {
  110. p.permission.Model.TextEmbedding = true
  111. }
  112. }
  113. if p.cursor == "model.rerank" {
  114. if p.permission.AllowInvokeRerank() {
  115. p.permission.Model.Rerank = false
  116. } else {
  117. p.permission.Model.Rerank = true
  118. }
  119. }
  120. if p.cursor == "model.tts" {
  121. if p.permission.AllowInvokeTTS() {
  122. p.permission.Model.TTS = false
  123. } else {
  124. p.permission.Model.TTS = true
  125. }
  126. }
  127. if p.cursor == "model.speech2text" {
  128. if p.permission.AllowInvokeSpeech2Text() {
  129. p.permission.Model.Speech2text = false
  130. } else {
  131. p.permission.Model.Speech2text = true
  132. }
  133. }
  134. if p.cursor == "model.moderation" {
  135. if p.permission.AllowInvokeModeration() {
  136. p.permission.Model.Moderation = false
  137. } else {
  138. p.permission.Model.Moderation = true
  139. }
  140. }
  141. if p.cursor == "app.enabled" {
  142. if p.permission.AllowInvokeApp() {
  143. p.permission.App = nil
  144. } else {
  145. p.permission.App = &plugin_entities.PluginPermissionAppRequirement{
  146. Enabled: true,
  147. }
  148. }
  149. }
  150. if p.cursor == "storage.enabled" {
  151. if p.permission.AllowInvokeStorage() {
  152. p.permission.Storage = nil
  153. } else {
  154. p.permission.Storage = &plugin_entities.PluginPermissionStorageRequirement{
  155. Enabled: true,
  156. Size: 1048576,
  157. }
  158. p.storageSizeEditor.SetValue(fmt.Sprintf("%d", p.permission.Storage.Size))
  159. }
  160. }
  161. if p.cursor == "endpoint.enabled" {
  162. if p.permission.AllowRegisterEndpoint() {
  163. p.permission.Endpoint = nil
  164. } else {
  165. p.permission.Endpoint = &plugin_entities.PluginPermissionEndpointRequirement{
  166. Enabled: true,
  167. }
  168. }
  169. }
  170. }
  171. func (p *permission) updateStorageSize() {
  172. if p.cursor == "storage.size" {
  173. // set the storage size editor to the current storage size
  174. if p.permission.AllowInvokeStorage() {
  175. p.storageSizeEditor.SetValue(fmt.Sprintf("%d", p.permission.Storage.Size))
  176. p.storageSizeEditor.Focus()
  177. }
  178. } else {
  179. p.storageSizeEditor.Blur()
  180. // get the storage size from the editor
  181. if p.permission.AllowInvokeStorage() {
  182. p.permission.Storage.Size, _ = strconv.ParseUint(p.storageSizeEditor.Value(), 10, 64)
  183. }
  184. }
  185. }
  186. func (p permission) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  187. switch msg := msg.(type) {
  188. case tea.KeyMsg:
  189. switch msg.String() {
  190. case "ctrl+c":
  191. return p, SUB_MENU_EVENT_NONE, tea.Quit
  192. case "down":
  193. // find the next key in the permissionKeySeq
  194. for i, key := range permissionKeySeq {
  195. if key == p.cursor {
  196. if i == len(permissionKeySeq)-1 {
  197. p.cursor = permissionKeySeq[0]
  198. } else {
  199. p.cursor = permissionKeySeq[i+1]
  200. }
  201. p.updateStorageSize()
  202. break
  203. }
  204. }
  205. case "up":
  206. // find the previous key in the permissionKeySeq
  207. for i, key := range permissionKeySeq {
  208. if key == p.cursor {
  209. if i == 0 {
  210. p.cursor = permissionKeySeq[len(permissionKeySeq)-1]
  211. } else {
  212. p.cursor = permissionKeySeq[i-1]
  213. }
  214. p.updateStorageSize()
  215. break
  216. }
  217. }
  218. case "tab":
  219. p.edit()
  220. case "enter":
  221. if p.cursor == "storage.size" {
  222. break
  223. }
  224. if p.cursor == "endpoint.enabled" {
  225. p.cursor = permissionKeySeq[0]
  226. p.updateStorageSize()
  227. return p, SUB_MENU_EVENT_NEXT, nil
  228. } else {
  229. // find the next key in the permissionKeySeq
  230. for i, key := range permissionKeySeq {
  231. if key == p.cursor {
  232. if i == len(permissionKeySeq)-1 {
  233. p.cursor = permissionKeySeq[0]
  234. } else {
  235. p.cursor = permissionKeySeq[i+1]
  236. }
  237. p.updateStorageSize()
  238. break
  239. }
  240. }
  241. }
  242. }
  243. }
  244. // update storage size editor
  245. if p.cursor == "storage.size" {
  246. if p.storageSizeEditor.Focused() {
  247. // check if msg is a number
  248. model, cmd := p.storageSizeEditor.Update(msg)
  249. p.storageSizeEditor = model
  250. return p, SUB_MENU_EVENT_NONE, cmd
  251. }
  252. }
  253. return p, SUB_MENU_EVENT_NONE, nil
  254. }
  255. func (p permission) Init() tea.Cmd {
  256. return nil
  257. }