mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
feat: some minor refactor
This commit is contained in:
@@ -1,37 +1,42 @@
|
|||||||
import json
|
|
||||||
import os
|
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from rich import print
|
from rich.console import Console
|
||||||
|
|
||||||
from .utils import fetch_and_json_env_file, get_git_remote_url, get_project_name
|
from .utils import (
|
||||||
|
fetch_and_json_env_file,
|
||||||
|
get_auth_token,
|
||||||
|
get_git_remote_url,
|
||||||
|
get_project_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
console = Console()
|
||||||
def get_auth_token():
|
|
||||||
return os.environ.get(
|
|
||||||
"TOKEN", "958303356b9a21884a83ddb6e774cc06c6f1dd0e04222fbc5a4e8a9ae02c140e"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DeployCommand:
|
class DeployCommand:
|
||||||
def __init__(self):
|
BASE_URL = getenv("BASE_URL", "http://localhost:3000/crewai_plus/api")
|
||||||
# self.base_url = os.environ.get("BASE_URL", "https://www.crewai.com/api")
|
|
||||||
self.base_url = getenv("BASE_URL", "http://localhost:3000/crewai_plus/api")
|
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
self.project_name = get_project_name()
|
self.project_name = get_project_name()
|
||||||
self.remote_repo_url = get_git_remote_url()
|
self.remote_repo_url = get_git_remote_url()
|
||||||
|
|
||||||
def deploy(self):
|
def _make_request(self, method: str, endpoint: str, **kwargs) -> requests.Response:
|
||||||
print("Deploying the crew...")
|
url = f"{self.BASE_URL}/{endpoint}"
|
||||||
response = requests.post(
|
headers = {
|
||||||
f"{self.base_url}/crews/by-name/{self.project_name}/deploy",
|
"Authorization": f"Bearer {get_auth_token()}",
|
||||||
headers={"Authorization": f"Bearer {get_auth_token()}"},
|
"Content-Type": "application/json",
|
||||||
)
|
}
|
||||||
print(response.json())
|
return requests.request(method, url, headers=headers, **kwargs)
|
||||||
|
|
||||||
def create_crew(self):
|
def deploy(self) -> None:
|
||||||
print("Creating deployment...")
|
console.print("Deploying the crew...", style="bold blue")
|
||||||
|
response = self._make_request(
|
||||||
|
"POST", f"crews/by-name/{self.project_name}/deploy"
|
||||||
|
)
|
||||||
|
console.print(response.json())
|
||||||
|
|
||||||
|
def create_crew(self) -> None:
|
||||||
|
console.print("Creating deployment...", style="bold blue")
|
||||||
env_vars = fetch_and_json_env_file()
|
env_vars = fetch_and_json_env_file()
|
||||||
payload = {
|
payload = {
|
||||||
"deploy": {
|
"deploy": {
|
||||||
@@ -40,94 +45,83 @@ class DeployCommand:
|
|||||||
"env": env_vars,
|
"env": env_vars,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response = requests.post(
|
response = self._make_request("POST", "crews", json=payload)
|
||||||
f"{self.base_url}/crews",
|
console.print(response.json())
|
||||||
data=json.dumps(payload),
|
|
||||||
headers={
|
|
||||||
"Authorization": f"Bearer {get_auth_token()}",
|
|
||||||
"Content-type": "application/json",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
print(response.json())
|
|
||||||
|
|
||||||
def list_crews(self):
|
def list_crews(self) -> None:
|
||||||
print("Listing all Crews")
|
console.print("Listing all Crews", style="bold blue")
|
||||||
|
response = self._make_request("GET", "crews")
|
||||||
response = requests.get(
|
|
||||||
f"{self.base_url}/crews",
|
|
||||||
headers={"Authorization": f"Bearer {get_auth_token()}"},
|
|
||||||
)
|
|
||||||
crews_data = response.json()
|
crews_data = response.json()
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print()
|
if crews_data:
|
||||||
if len(crews_data):
|
|
||||||
for crew_data in crews_data:
|
for crew_data in crews_data:
|
||||||
print(
|
console.print(
|
||||||
f"- {crew_data['name']} ({crew_data['uuid']}) [blue]\[{crew_data['status']}]"
|
f"- {crew_data['name']} ({crew_data['uuid']}) [blue]{crew_data['status']}[/blue]"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("You don't have any crews yet. Let's create one!")
|
console.print(
|
||||||
print()
|
"You don't have any crews yet. Let's create one!", style="yellow"
|
||||||
print("\t[green]crewai create --name \[name]")
|
)
|
||||||
|
console.print("\t[green]crewai create --name [name][/green]")
|
||||||
|
|
||||||
def get_crew_status(self):
|
def get_crew_status(self) -> None:
|
||||||
print("Getting deployment status...")
|
console.print("Getting deployment status...", style="bold blue")
|
||||||
response = requests.get(
|
response = self._make_request(
|
||||||
f"{self.base_url}/crews/by-name/{self.project_name}/status",
|
"GET", f"crews/by-name/{self.project_name}/status"
|
||||||
headers={"Authorization": f"Bearer {get_auth_token()}"},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
status_data = response.json()
|
status_data = response.json()
|
||||||
print(status_data)
|
console.print(f"Name:\t {status_data['name']}")
|
||||||
print("Name:\t", status_data["name"])
|
console.print(f"Status:\t {status_data['status']}")
|
||||||
print("Status:\t", status_data["status"])
|
console.print("\nUsage:")
|
||||||
print()
|
console.print(f"\tcrewai inputs --name \"{status_data['name']}\"")
|
||||||
print("usage:")
|
console.print(
|
||||||
print(f"\tcrewai inputs --name \"{status_data['name']}\" ")
|
|
||||||
print(
|
|
||||||
f"\tcrewai kickoff --name \"{status_data['name']}\" --inputs [INPUTS]"
|
f"\tcrewai kickoff --name \"{status_data['name']}\" --inputs [INPUTS]"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(response.json())
|
console.print(response.json(), style="bold red")
|
||||||
|
|
||||||
def get_crew_logs(self, log_type="deployment"):
|
def get_crew_logs(self, log_type: str = "deployment") -> None:
|
||||||
print("Getting deployment logs...")
|
console.print("Getting deployment logs...", style="bold blue")
|
||||||
response = requests.get(
|
response = self._make_request(
|
||||||
f"{self.base_url}/crews/by-name/{self.project_name}/logs/{log_type}",
|
"GET", f"crews/by-name/{self.project_name}/logs/{log_type}"
|
||||||
headers={"Authorization": f"Bearer {get_auth_token()}"},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
log_messages = response.json()
|
log_messages = response.json()
|
||||||
for log_message in log_messages:
|
for log_message in log_messages:
|
||||||
print(
|
console.print(
|
||||||
f"{log_message['timestamp']} - {log_message['level']}: {log_message['message']}"
|
f"{log_message['timestamp']} - {log_message['level']}: {log_message['message']}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(response.text)
|
console.print(response.text, style="bold red")
|
||||||
|
|
||||||
|
def remove_crew(self) -> None:
|
||||||
|
console.print("Removing deployment...", style="bold blue")
|
||||||
|
response = self._make_request("DELETE", f"crews/by-name/{self.project_name}")
|
||||||
|
|
||||||
def remove_crew(self):
|
|
||||||
print("Removing deployment...")
|
|
||||||
response = requests.delete(
|
|
||||||
f"{self.base_url}/crews/by-name/{self.project_name}",
|
|
||||||
headers={"Authorization": f"Bearer {get_auth_token()}"},
|
|
||||||
)
|
|
||||||
if response.status_code == 204:
|
if response.status_code == 204:
|
||||||
print(f"Crew '{self.project_name}' removed successfully.")
|
console.print(
|
||||||
|
f"Crew '{self.project_name}' removed successfully.", style="green"
|
||||||
def signup(self):
|
|
||||||
print("Signing Up")
|
|
||||||
response = requests.get(f"{self.base_url}/signup_link")
|
|
||||||
if response.status_code == 200:
|
|
||||||
token = response.json()["token"]
|
|
||||||
signup_link = response.json()["signup_link"]
|
|
||||||
print("Temporary credentials: ", token)
|
|
||||||
print(
|
|
||||||
"We are trying to open the following signup link below.\n"
|
|
||||||
"If it doesn't open to you, copy-and-paste into our browser to procceed."
|
|
||||||
)
|
)
|
||||||
print()
|
|
||||||
print(signup_link)
|
|
||||||
# webbrowser.open(signup_link, new=2)
|
|
||||||
else:
|
else:
|
||||||
print(response.text)
|
console.print(
|
||||||
|
f"Failed to remove crew '{self.project_name}'", style="bold red"
|
||||||
|
)
|
||||||
|
|
||||||
|
def signup(self) -> None:
|
||||||
|
console.print("Signing Up", style="bold blue")
|
||||||
|
response = self._make_request("GET", "signup_link")
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
console.print(f"Temporary credentials: {data['token']}")
|
||||||
|
console.print(
|
||||||
|
"We are trying to open the following signup link below.\n"
|
||||||
|
"If it doesn't open for you, copy-and-paste into your browser to proceed."
|
||||||
|
)
|
||||||
|
console.print(f"\n{data['signup_link']}", style="underline blue")
|
||||||
|
else:
|
||||||
|
console.print(response.text, style="bold red")
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@@ -73,3 +74,9 @@ def fetch_and_json_env_file(env_file_path: str = ".env") -> dict:
|
|||||||
print(f"Error reading the .env file: {e}")
|
print(f"Error reading the .env file: {e}")
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def get_auth_token():
|
||||||
|
return os.environ.get(
|
||||||
|
"TOKEN", "958303356b9a21884a83ddb6e774cc06c6f1dd0e04222fbc5a4e8a9ae02c140e"
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user