Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

install_openai_plugin.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python3
  2. import sys
  3. from pathlib import Path
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. import httpx
  6. import time
  7. from common import config_helper
  8. from common import Logger
  9. def install_openai_plugin() -> None:
  10. """Install OpenAI plugin using saved access token."""
  11. log = Logger("InstallPlugin")
  12. log.header("Installing OpenAI Plugin")
  13. # Read token from config
  14. access_token = config_helper.get_token()
  15. if not access_token:
  16. log.error("No access token found in config")
  17. log.info("Please run login_admin.py first to get access token")
  18. return
  19. log.step("Installing OpenAI plugin...")
  20. # API endpoint for plugin installation
  21. base_url = "http://localhost:5001"
  22. install_endpoint = (
  23. f"{base_url}/console/api/workspaces/current/plugin/install/marketplace"
  24. )
  25. # Plugin identifier
  26. plugin_payload = {
  27. "plugin_unique_identifiers": [
  28. "langgenius/openai:0.2.5@373362a028986aae53a7baf73a7f11991ba3c22c69eaf97d6cde048cfd4a9f98"
  29. ]
  30. }
  31. headers = {
  32. "Accept": "*/*",
  33. "Accept-Language": "en-US,en;q=0.9",
  34. "Cache-Control": "no-cache",
  35. "Connection": "keep-alive",
  36. "DNT": "1",
  37. "Origin": "http://localhost:3000",
  38. "Pragma": "no-cache",
  39. "Referer": "http://localhost:3000/",
  40. "Sec-Fetch-Dest": "empty",
  41. "Sec-Fetch-Mode": "cors",
  42. "Sec-Fetch-Site": "same-site",
  43. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
  44. "authorization": f"Bearer {access_token}",
  45. "content-type": "application/json",
  46. "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
  47. "sec-ch-ua-mobile": "?0",
  48. "sec-ch-ua-platform": '"macOS"',
  49. }
  50. cookies = {"locale": "en-US"}
  51. try:
  52. # Make the installation request
  53. with httpx.Client() as client:
  54. response = client.post(
  55. install_endpoint,
  56. json=plugin_payload,
  57. headers=headers,
  58. cookies=cookies,
  59. )
  60. if response.status_code == 200:
  61. response_data = response.json()
  62. task_id = response_data.get("task_id")
  63. if not task_id:
  64. log.error("No task ID received from installation request")
  65. return
  66. log.progress(f"Installation task created: {task_id}")
  67. log.info("Polling for task completion...")
  68. # Poll for task completion
  69. task_endpoint = (
  70. f"{base_url}/console/api/workspaces/current/plugin/tasks/{task_id}"
  71. )
  72. max_attempts = 30 # 30 attempts with 2 second delay = 60 seconds max
  73. attempt = 0
  74. log.spinner_start("Installing plugin")
  75. while attempt < max_attempts:
  76. attempt += 1
  77. time.sleep(2) # Wait 2 seconds between polls
  78. task_response = client.get(
  79. task_endpoint,
  80. headers=headers,
  81. cookies=cookies,
  82. )
  83. if task_response.status_code != 200:
  84. log.spinner_stop(
  85. success=False,
  86. message=f"Failed to get task status: {task_response.status_code}",
  87. )
  88. return
  89. task_data = task_response.json()
  90. task_info = task_data.get("task", {})
  91. status = task_info.get("status")
  92. if status == "success":
  93. log.spinner_stop(success=True, message="Plugin installed!")
  94. log.success("OpenAI plugin installed successfully!")
  95. # Display plugin info
  96. plugins = task_info.get("plugins", [])
  97. if plugins:
  98. plugin_info = plugins[0]
  99. log.key_value("Plugin ID", plugin_info.get("plugin_id"))
  100. log.key_value("Message", plugin_info.get("message"))
  101. break
  102. elif status == "failed":
  103. log.spinner_stop(success=False, message="Installation failed")
  104. log.error("Plugin installation failed")
  105. plugins = task_info.get("plugins", [])
  106. if plugins:
  107. for plugin in plugins:
  108. log.list_item(
  109. f"{plugin.get('plugin_id')}: {plugin.get('message')}"
  110. )
  111. break
  112. # Continue polling if status is "pending" or other
  113. else:
  114. log.spinner_stop(success=False, message="Installation timed out")
  115. log.error("Installation timed out after 60 seconds")
  116. elif response.status_code == 401:
  117. log.error("Installation failed: Unauthorized")
  118. log.info("Token may have expired. Please run login_admin.py again")
  119. elif response.status_code == 409:
  120. log.warning("Plugin may already be installed")
  121. log.debug(f"Response: {response.text}")
  122. else:
  123. log.error(
  124. f"Installation failed with status code: {response.status_code}"
  125. )
  126. log.debug(f"Response: {response.text}")
  127. except httpx.ConnectError:
  128. log.error("Could not connect to Dify API at http://localhost:5001")
  129. log.info("Make sure the API server is running with: ./dev/start-api")
  130. except Exception as e:
  131. log.error(f"An error occurred: {e}")
  132. if __name__ == "__main__":
  133. install_openai_plugin()