選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

publish_workflow.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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
  8. from common import Logger
  9. def publish_workflow() -> None:
  10. """Publish the imported workflow app."""
  11. log = Logger("PublishWorkflow")
  12. log.header("Publishing Workflow")
  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. return
  18. # Read app_id from config
  19. app_id = config_helper.get_app_id()
  20. if not app_id:
  21. log.error("No app_id found in config")
  22. return
  23. log.step(f"Publishing workflow for app: {app_id}")
  24. # API endpoint for publishing workflow
  25. base_url = "http://localhost:5001"
  26. publish_endpoint = f"{base_url}/console/api/apps/{app_id}/workflows/publish"
  27. # Publish payload
  28. publish_payload = {"marked_name": "", "marked_comment": ""}
  29. headers = {
  30. "Accept": "*/*",
  31. "Accept-Language": "en-US,en;q=0.9",
  32. "Cache-Control": "no-cache",
  33. "Connection": "keep-alive",
  34. "DNT": "1",
  35. "Origin": "http://localhost:3000",
  36. "Pragma": "no-cache",
  37. "Referer": "http://localhost:3000/",
  38. "Sec-Fetch-Dest": "empty",
  39. "Sec-Fetch-Mode": "cors",
  40. "Sec-Fetch-Site": "same-site",
  41. "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",
  42. "authorization": f"Bearer {access_token}",
  43. "content-type": "application/json",
  44. "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
  45. "sec-ch-ua-mobile": "?0",
  46. "sec-ch-ua-platform": '"macOS"',
  47. }
  48. cookies = {"locale": "en-US"}
  49. try:
  50. # Make the publish request
  51. with httpx.Client() as client:
  52. response = client.post(
  53. publish_endpoint,
  54. json=publish_payload,
  55. headers=headers,
  56. cookies=cookies,
  57. )
  58. if response.status_code == 200 or response.status_code == 201:
  59. log.success("Workflow published successfully!")
  60. log.key_value("App ID", app_id)
  61. # Try to parse response if it has JSON content
  62. if response.text:
  63. try:
  64. response_data = response.json()
  65. if response_data:
  66. log.debug(
  67. f"Response: {json.dumps(response_data, indent=2)}"
  68. )
  69. except json.JSONDecodeError:
  70. # Response might be empty or non-JSON
  71. pass
  72. elif response.status_code == 401:
  73. log.error("Workflow publish failed: Unauthorized")
  74. log.info("Token may have expired. Please run login_admin.py again")
  75. elif response.status_code == 404:
  76. log.error("Workflow publish failed: App not found")
  77. log.info("Make sure the app was imported successfully")
  78. else:
  79. log.error(
  80. f"Workflow publish failed with status code: {response.status_code}"
  81. )
  82. log.debug(f"Response: {response.text}")
  83. except httpx.ConnectError:
  84. log.error("Could not connect to Dify API at http://localhost:5001")
  85. log.info("Make sure the API server is running with: ./dev/start-api")
  86. except Exception as e:
  87. log.error(f"An error occurred: {e}")
  88. if __name__ == "__main__":
  89. publish_workflow()