Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy command #173

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions agentstack/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class ConfigFile(BaseModel):

Config Schema
-------------
project_name: str
The name of the project.
framework: str
The framework used in the project. Defaults to 'crewai'.
tools: list[str]
Expand All @@ -65,15 +67,19 @@ class ConfigFile(BaseModel):
The template used to generate the project.
template_version: Optional[str]
The version of the template system used to generate the project.
hosted_project_id: Optional[str]
The ID of the deployed project on https://AgentStack.sh
"""

project_name: str
framework: str = DEFAULT_FRAMEWORK # TODO this should probably default to None
tools: list[str] = []
telemetry_opt_out: Optional[bool] = None
default_model: Optional[str] = None
agentstack_version: Optional[str] = get_version()
template: Optional[str] = None
template_version: Optional[str] = None
hosted_project_id: Optional[int] = None

def __init__(self):
if os.path.exists(PATH / CONFIG_FILENAME):
Expand Down
94 changes: 94 additions & 0 deletions agentstack/deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
import tempfile
import tomllib
import webbrowser
import zipfile
from pathlib import Path

from agentstack.auth import get_stored_token, login
from agentstack.conf import ConfigFile
from agentstack.utils import term_color
import requests


def deploy():
bearer_token = get_stored_token()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a deploy stub to /cli that manages the CLI interaction and logging, then use the deploy function from this file

if not bearer_token:
success = login()
if success:
bearer_token = get_stored_token()
else:
print(term_color("Failed to authenticate with AgentStack.sh", "red"))
return

project_id = get_project_id()
pyproject = load_pyproject()
files = list(Path('.').rglob('*.py'))

with tempfile.NamedTemporaryFile(suffix='.zip') as tmp:
with zipfile.ZipFile(tmp.name, 'w') as zf:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth just building the project into a whl (which is essentially a zip file) since we already have a pyproject.toml file available in user projects (pip build). pip install user-project.whl after uploading it will install dependencies, too.

https://packaging.python.org/en/latest/tutorials/packaging-projects/#generating-distribution-archives

for file in files:
zf.write(file)
if pyproject:
zf.write("pyproject.toml")

response = requests.post(
'http://localhost:3000/deploy/build',
files={'code': ('code.zip', open(tmp.name, 'rb'))},
params={'projectId': project_id},
headers={'Authorization': bearer_token}
)

if response.status_code != 200:
print(term_color("Failed to deploy with AgentStack.sh", "red"))
print(response.text)
return

webbrowser.open(f"http://localhost:5173/project/{project_id}")


def load_pyproject():
if os.path.exists("pyproject.toml"):
with open("pyproject.toml", "rb") as f:
return tomllib.load(f)
return None

def get_project_id():
project_config = ConfigFile()
project_id = project_config.hosted_project_id

if project_id:
return project_id

bearer_token = get_stored_token()

# if not in config, create project and store it
print(term_color("🚧 Creating AgentStack.sh Project", "green"))
headers = {
'Authorization': f'Bearer {bearer_token}',
'Content-Type': 'application/json'
}

payload = {
'name': project_config.project_name
}

try:
response = requests.post(
url="http://localhost:3000/projects",
# url="https://api.agentstack.sh/projects",
headers=headers,
json=payload
)

response.raise_for_status()
res_data = response.json()
project_id = res_data['id']
project_config.hosted_project_id = project_id
project_config.write()
return project_id

except requests.exceptions.RequestException as e:
print(f"Error making request: {e}")
return None

8 changes: 8 additions & 0 deletions agentstack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from agentstack.utils import get_version, term_color
from agentstack import generation
from agentstack.update import check_for_updates
from agentstack.deploy import deploy


def main():
Expand Down Expand Up @@ -136,13 +137,18 @@ def main():
)
tools_remove_parser.add_argument("name", help="Name of the tool to remove")

# 'export'
export_parser = subparsers.add_parser(
'export', aliases=['e'], help='Export your agent as a template', parents=[global_parser]
)
export_parser.add_argument('filename', help='The name of the file to export to')

# 'update'
update = subparsers.add_parser('update', aliases=['u'], help='Check for updates', parents=[global_parser])

# 'deploy'
deploy_ = subparsers.add_parser('deploy', aliases=['d'], help='Deploy your agent to AgentStack.sh', parents=[global_parser])

# Parse known args and store unknown args in extras; some commands use them later on
args, extra_args = parser.parse_known_args()

Expand Down Expand Up @@ -193,6 +199,8 @@ def main():
export_template(args.filename)
elif args.command in ['login']:
auth.login()
elif args.command in ['deploy', 'd']:
deploy()
elif args.command in ['update', 'u']:
pass # Update check already done
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"project_name": "{{ cookiecutter.project_metadata.project_name }}",
"framework": "{{ cookiecutter.framework }}",
"agentstack_version": "{{ cookiecutter.project_metadata.agentstack_version }}",
"template": "{{ cookiecutter.project_metadata.template }}",
Expand Down
Loading