You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

import_workflow_app.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 json
  7. from common import config_helper, Logger
  8. def import_workflow_app() -> None:
  9. """Import workflow app from DSL file and save app_id."""
  10. log = Logger("ImportApp")
  11. log.header("Importing Workflow Application")
  12. # Read token from config
  13. access_token = config_helper.get_token()
  14. if not access_token:
  15. log.error("No access token found in config")
  16. log.info("Please run login_admin.py first to get access token")
  17. return
  18. # Read workflow DSL file
  19. dsl_path = Path(__file__).parent / "dsl" / "workflow_llm.yml"
  20. if not dsl_path.exists():
  21. log.error(f"DSL file not found: {dsl_path}")
  22. return
  23. with open(dsl_path, "r") as f:
  24. yaml_content = f.read()
  25. log.step("Importing workflow app from DSL...")
  26. log.key_value("DSL file", dsl_path.name)
  27. # API endpoint for app import
  28. base_url = "http://localhost:5001"
  29. import_endpoint = f"{base_url}/console/api/apps/imports"
  30. # Import payload
  31. import_payload = {"mode": "yaml-content", "yaml_content": yaml_content}
  32. headers = {
  33. "Accept": "*/*",
  34. "Accept-Language": "en-US,en;q=0.9",
  35. "Cache-Control": "no-cache",
  36. "Connection": "keep-alive",
  37. "DNT": "1",
  38. "Origin": "http://localhost:3000",
  39. "Pragma": "no-cache",
  40. "Referer": "http://localhost:3000/",
  41. "Sec-Fetch-Dest": "empty",
  42. "Sec-Fetch-Mode": "cors",
  43. "Sec-Fetch-Site": "same-site",
  44. "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",
  45. "authorization": f"Bearer {access_token}",
  46. "content-type": "application/json",
  47. "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
  48. "sec-ch-ua-mobile": "?0",
  49. "sec-ch-ua-platform": '"macOS"',
  50. }
  51. cookies = {"locale": "en-US"}
  52. try:
  53. # Make the import request
  54. with httpx.Client() as client:
  55. response = client.post(
  56. import_endpoint,
  57. json=import_payload,
  58. headers=headers,
  59. cookies=cookies,
  60. )
  61. if response.status_code == 200:
  62. response_data = response.json()
  63. # Check import status
  64. if response_data.get("status") == "completed":
  65. app_id = response_data.get("app_id")
  66. if app_id:
  67. log.success("Workflow app imported successfully!")
  68. log.key_value("App ID", app_id)
  69. log.key_value("App Mode", response_data.get("app_mode"))
  70. log.key_value(
  71. "DSL Version", response_data.get("imported_dsl_version")
  72. )
  73. # Save app_id to config
  74. app_config = {
  75. "app_id": app_id,
  76. "app_mode": response_data.get("app_mode"),
  77. "app_name": "workflow_llm",
  78. "dsl_version": response_data.get("imported_dsl_version"),
  79. }
  80. if config_helper.write_config("app_config", app_config):
  81. log.info(
  82. f"App config saved to: {config_helper.get_config_path('benchmark_state')}"
  83. )
  84. else:
  85. log.error("Import completed but no app_id received")
  86. log.debug(f"Response: {json.dumps(response_data, indent=2)}")
  87. elif response_data.get("status") == "failed":
  88. log.error("Import failed")
  89. log.error(f"Error: {response_data.get('error')}")
  90. else:
  91. log.warning(f"Import status: {response_data.get('status')}")
  92. log.debug(f"Response: {json.dumps(response_data, indent=2)}")
  93. elif response.status_code == 401:
  94. log.error("Import failed: Unauthorized")
  95. log.info("Token may have expired. Please run login_admin.py again")
  96. else:
  97. log.error(f"Import failed with status code: {response.status_code}")
  98. log.debug(f"Response: {response.text}")
  99. except httpx.ConnectError:
  100. log.error("Could not connect to Dify API at http://localhost:5001")
  101. log.info("Make sure the API server is running with: ./dev/start-api")
  102. except Exception as e:
  103. log.error(f"An error occurred: {e}")
  104. if __name__ == "__main__":
  105. import_workflow_app()