Skip to content

Commit 023b3ce

Browse files
committed
run proj from serve.py
1 parent 2b94a9f commit 023b3ce

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

agentstack/cli/run.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
MAIN_MODULE_NAME = "main"
1616

1717

18-
def _format_friendly_error_message(exception: Exception):
18+
def format_friendly_error_message(exception: Exception):
1919
"""
2020
Projects will throw various errors, especially on first runs, so we catch
2121
them here and print a more helpful message.
@@ -93,7 +93,7 @@ def _import_project_module(path: Path):
9393
return project_module
9494

9595

96-
def run_project(command: str = 'run', cli_args: Optional[List[str]] = None, api_inputs: Optional[Dict[str, str]] = None):
96+
def run_project(command: str = 'run', cli_args: Optional[List[str]] = None):
9797
"""Validate that the project is ready to run and then run it."""
9898
verify_agentstack_project()
9999

@@ -114,9 +114,6 @@ def run_project(command: str = 'run', cli_args: Optional[List[str]] = None, api_
114114
log.debug(f"Using CLI input override: {key}={value}")
115115
inputs.add_input_for_run(key, value)
116116

117-
if api_inputs:
118-
inputs.add_input_for_run(**api_inputs)
119-
120117
load_dotenv(Path.home() / '.env') # load the user's .env file
121118
load_dotenv(conf.PATH / '.env', override=True) # load the project's .env file
122119

@@ -128,4 +125,4 @@ def run_project(command: str = 'run', cli_args: Optional[List[str]] = None, api_
128125
except ImportError as e:
129126
raise ValidationError(f"Failed to import AgentStack project at: {conf.PATH.absolute()}\n{e}")
130127
except Exception as e:
131-
raise Exception(_format_friendly_error_message(e))
128+
raise Exception(format_friendly_error_message(e))

agentstack/log.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"""
2525

2626
from typing import IO, Optional, Callable
27-
import os, sys
2827
import io
2928
import logging
3029
from agentstack import conf

agentstack/serve/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RUN #ls -a agentstack
1919
# Debug: Try installing packages one at a time
2020
RUN uv pip install --system psutil
2121
RUN uv pip install --system flask
22+
# Cache-buster: 2
2223
RUN uv pip install --system git+https://github.com/AgentOps-AI/AgentStack.git@deploy-command #install on cloud
2324
RUN #cd agentstack && uv pip install --system .
2425
RUN mkdir -p src

agentstack/serve/serve.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
# app.py
2+
import importlib
3+
import sys
4+
from pathlib import Path
25
from dotenv import load_dotenv
6+
from agentstack import conf, frameworks, inputs
7+
from agentstack.exceptions import ValidationError
8+
from agentstack.utils import verify_agentstack_project
9+
# TODO: move this to not cli, but cant be utils due to circular import
10+
from agentstack.cli.run import format_friendly_error_message
11+
312
load_dotenv(dotenv_path="/app/.env")
413

514
from flask import Flask, request, jsonify
615
import requests
7-
from typing import Dict, Any
16+
from typing import Dict, Any, Optional
817
import os
9-
from agentstack.run import run_project
18+
19+
MAIN_FILENAME: Path = Path("src/main.py")
20+
MAIN_MODULE_NAME = "main"
1021

1122
app = Flask(__name__)
1223

@@ -84,3 +95,49 @@ def process_agent():
8495
print("Learn more about agent requests at https://docs.agentstack.sh/") # TODO: add docs for this
8596

8697
app.run(host='0.0.0.0', port=port)
98+
99+
100+
def run_project(command: str = 'run', api_args: Optional[Dict[str, str]] = None,
101+
api_inputs: Optional[Dict[str, str]] = None):
102+
"""Validate that the project is ready to run and then run it."""
103+
verify_agentstack_project()
104+
105+
if conf.get_framework() not in frameworks.SUPPORTED_FRAMEWORKS:
106+
raise ValidationError(f"Framework {conf.get_framework()} is not supported by agentstack.")
107+
108+
try:
109+
frameworks.validate_project()
110+
except ValidationError as e:
111+
raise e
112+
113+
inputs.add_input_for_run(**api_inputs)
114+
115+
load_dotenv(Path.home() / '.env') # load the user's .env file
116+
load_dotenv(conf.PATH / '.env', override=True) # load the project's .env file
117+
118+
# import src/main.py from the project path and run `command` from the project's main.py
119+
try:
120+
log.notify("Running your agent...")
121+
project_main = _import_project_module(conf.PATH)
122+
getattr(project_main, command)()
123+
except ImportError as e:
124+
raise ValidationError(f"Failed to import AgentStack project at: {conf.PATH.absolute()}\n{e}")
125+
except Exception as e:
126+
raise Exception(format_friendly_error_message(e))
127+
128+
def _import_project_module(path: Path):
129+
"""
130+
Import `main` from the project path.
131+
132+
We do it this way instead of spawning a subprocess so that we can share
133+
state with the user's project.
134+
"""
135+
spec = importlib.util.spec_from_file_location(MAIN_MODULE_NAME, str(path / MAIN_FILENAME))
136+
137+
assert spec is not None # appease type checker
138+
assert spec.loader is not None # appease type checker
139+
140+
project_module = importlib.util.module_from_spec(spec)
141+
sys.path.insert(0, str((path / MAIN_FILENAME).parent))
142+
spec.loader.exec_module(project_module)
143+
return project_module

0 commit comments

Comments
 (0)