Spaces:
Sleeping
Sleeping
Update orchestrator.py
Browse files- orchestrator.py +419 -54
orchestrator.py
CHANGED
|
@@ -1,70 +1,435 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
log_event("agent_start", {"agent": "Deploy"})
|
| 23 |
-
deploy_json_str = deploy_agent.run("App written to generated/app.py and tests to generated/test_app.py. Provide steps to run locally and send a completion note.")
|
| 24 |
-
log_event("agent_output", {"agent": "Deploy", "content": deploy_json_str})
|
| 25 |
-
try:
|
| 26 |
-
deploy_info = json.loads(deploy_json_str)
|
| 27 |
-
except Exception:
|
| 28 |
-
deploy_info = {"run_steps": ["pip install -r requirements.txt", "python generated/app.py"], "notification": deploy_json_str}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return summary
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Agentic AI Orchestrator (Login→Dashboard POC)
|
| 2 |
+
|
| 3 |
+
Multi‑agent pipeline that: **(1)** reads a requirement & fills gaps, **(2)** generates Python app code, **(3)** reviews it, **(4)** writes unit tests, **(5)** “deploys” to localhost, and **(6)** sends a completion email. Built with the OpenAI API. Includes a Streamlit monitor UI so you can see every agent’s step.
|
| 4 |
+
|
| 5 |
+
> **POC scope**: a tiny Flask app that authenticates a demo user and shows a simple KPI dashboard after login.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## File layout (you’ll create these)
|
| 10 |
|
| 11 |
+
```
|
| 12 |
+
agentic-poc/
|
| 13 |
+
├── orchestrator.py # Agent pipeline + core logic
|
| 14 |
+
├── streamlit_app.py # Monitoring UI (Hugging Face/locally)
|
| 15 |
+
├── agent_prompts.py # Prompts for each agent
|
| 16 |
+
├── generated/ # Where agents drop code/tests
|
| 17 |
+
├── events/ # JSONL event logs for monitoring
|
| 18 |
+
├── requirements.txt # Python deps for both orchestrator & UI
|
| 19 |
+
└── README.md # Quick start
|
| 20 |
+
```
|
| 21 |
|
| 22 |
+
---
|
| 23 |
|
| 24 |
+
## 1) `requirements.txt`
|
| 25 |
|
| 26 |
+
```
|
| 27 |
+
openai>=1.35.0
|
| 28 |
+
streamlit>=1.37.0
|
| 29 |
+
flask>=3.0.0
|
| 30 |
+
pytest>=8.2.0
|
| 31 |
+
rich>=13.7.0
|
| 32 |
+
python-dotenv>=1.0.1
|
| 33 |
+
```
|
| 34 |
|
| 35 |
+
---
|
| 36 |
|
| 37 |
+
## 2) `agent_prompts.py`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
```python
|
| 40 |
+
# agent_prompts.py
|
| 41 |
+
# System prompts for each agent. Keep them short & deterministic.
|
| 42 |
|
| 43 |
+
REQUIREMENTS_AGENT = (
|
| 44 |
+
"""
|
| 45 |
+
You are the Requirements & Gap Analysis Agent.
|
| 46 |
+
Task: Given a short requirement about a "login then dashboard" app, produce:
|
| 47 |
+
- A concise, unambiguous spec (MVP). Fill obvious gaps, but keep it realistic.
|
| 48 |
+
- Acceptance criteria as bullet points.
|
| 49 |
+
- Risks & assumptions.
|
| 50 |
+
- Final line: APPROVED or NEEDS INFO. If something is truly blocking, say NEEDS INFO.
|
| 51 |
+
Output JSON with keys: spec, acceptance_criteria, risks, assumptions, approval.
|
| 52 |
+
"""
|
| 53 |
)
|
| 54 |
|
| 55 |
+
CODING_AGENT = (
|
| 56 |
+
"""
|
| 57 |
+
You are the Coding Agent. Generate a minimal Flask web app with:
|
| 58 |
+
- / (GET) redirects to /login if not authenticated
|
| 59 |
+
- /login (GET Form, POST auth). One hardcoded demo user: user="demo@example.com", pwd="demo123".
|
| 60 |
+
- /dashboard (GET) protected route showing 3 KPI cards and a small table using dummy data.
|
| 61 |
+
- Use Flask sessions (secret key). No DB. Simple CSS.
|
| 62 |
+
- Place everything in a single file: app.py compatible with `python app.py` to run on port 5000.
|
| 63 |
+
- Include clear comments and Jinja templates inline via render_template_string or use minimal template strings.
|
| 64 |
+
- Return ONLY a triple backticked code block for app.py and nothing else.
|
| 65 |
+
"""
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
REVIEW_AGENT = (
|
| 69 |
+
"""
|
| 70 |
+
You are the Code Review Agent.
|
| 71 |
+
- Read app.py content.
|
| 72 |
+
- Report issues under headings: Security, Correctness, Readability, Performance.
|
| 73 |
+
- Suggest concrete fixes (diff-style bullets).
|
| 74 |
+
- End with: APPROVED or CHANGES REQUESTED.
|
| 75 |
+
Output in Markdown.
|
| 76 |
+
"""
|
| 77 |
+
)
|
|
|
|
| 78 |
|
| 79 |
+
TEST_AGENT = (
|
| 80 |
+
"""
|
| 81 |
+
You are the Test Agent. Create pytest tests for app.py:
|
| 82 |
+
- test_login_success()
|
| 83 |
+
- test_login_failure()
|
| 84 |
+
- test_dashboard_requires_login()
|
| 85 |
+
- test_dashboard_after_login()
|
| 86 |
+
Tests should use Flask test client from the app (import app as app_module; app = app_module.app).
|
| 87 |
+
Return ONLY a triple backticked code block for tests named test_app.py.
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
DEPLOY_AGENT = (
|
| 92 |
+
"""
|
| 93 |
+
You are the Deploy & Notify Agent.
|
| 94 |
+
- Write concise steps to run locally with python.
|
| 95 |
+
- If SMTP env variables are present, send a short completion email; otherwise say that a console log was written.
|
| 96 |
+
- Output JSON with keys: run_steps (list of steps), notification (string summary).
|
| 97 |
+
"""
|
| 98 |
+
)
|
| 99 |
+
```
|
| 100 |
|
| 101 |
+
---
|
| 102 |
|
| 103 |
+
## 3) `orchestrator.py`
|
| 104 |
+
|
| 105 |
+
````python
|
| 106 |
+
# orchestrator.py
|
| 107 |
+
import os, json, time, re, pathlib, smtplib
|
| 108 |
+
from email.mime.text import MIMEText
|
| 109 |
+
from dataclasses import dataclass
|
| 110 |
+
from typing import List, Dict, Any
|
| 111 |
+
from openai import OpenAI
|
| 112 |
+
from rich.console import Console
|
| 113 |
+
from rich.table import Table
|
| 114 |
+
from dotenv import load_dotenv
|
| 115 |
+
|
| 116 |
+
from agent_prompts import (
|
| 117 |
+
REQUIREMENTS_AGENT,
|
| 118 |
+
CODING_AGENT,
|
| 119 |
+
REVIEW_AGENT,
|
| 120 |
+
TEST_AGENT,
|
| 121 |
+
DEPLOY_AGENT,
|
| 122 |
)
|
| 123 |
+
|
| 124 |
+
load_dotenv()
|
| 125 |
+
|
| 126 |
+
# ========= Config =========
|
| 127 |
+
MODEL = os.getenv("OPENAI_MODEL", "GPT-5 nano")
|
| 128 |
+
OUT_DIR = pathlib.Path("generated")
|
| 129 |
+
EVENT_DIR = pathlib.Path("events")
|
| 130 |
+
OUT_DIR.mkdir(exist_ok=True)
|
| 131 |
+
EVENT_DIR.mkdir(exist_ok=True)
|
| 132 |
+
EVENT_LOG = EVENT_DIR / f"run_{int(time.time())}.jsonl"
|
| 133 |
+
|
| 134 |
+
client = OpenAI()
|
| 135 |
+
console = Console()
|
| 136 |
+
|
| 137 |
+
@dataclass
|
| 138 |
+
class Agent:
|
| 139 |
+
name: str
|
| 140 |
+
system: str
|
| 141 |
+
|
| 142 |
+
def run(self, user_content: str, *, json_mode: bool = False) -> str:
|
| 143 |
+
messages = [
|
| 144 |
+
{"role": "system", "content": self.system},
|
| 145 |
+
{"role": "user", "content": user_content},
|
| 146 |
+
]
|
| 147 |
+
response = client.chat.completions.create(
|
| 148 |
+
model=MODEL,
|
| 149 |
+
messages=messages,
|
| 150 |
+
temperature=0.2,
|
| 151 |
+
)
|
| 152 |
+
content = response.choices[0].message.content or ""
|
| 153 |
+
return content
|
| 154 |
+
|
| 155 |
+
# Instantiate agents
|
| 156 |
+
requirements_agent = Agent("Requirements", REQUIREMENTS_AGENT)
|
| 157 |
+
coding_agent = Agent("Coding", CODING_AGENT)
|
| 158 |
+
review_agent = Agent("Review", REVIEW_AGENT)
|
| 159 |
+
test_agent = Agent("Test", TEST_AGENT)
|
| 160 |
+
deploy_agent = Agent("Deploy", DEPLOY_AGENT)
|
| 161 |
+
|
| 162 |
+
# ======= Helpers =======
|
| 163 |
+
|
| 164 |
+
def log_event(kind: str, payload: Dict[str, Any]):
|
| 165 |
+
rec = {"ts": time.time(), "kind": kind, **payload}
|
| 166 |
+
with open(EVENT_LOG, "a", encoding="utf-8") as f:
|
| 167 |
+
f.write(json.dumps(rec) + "\n")
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def extract_code_block(text: str, language_hint: str = "") -> str:
|
| 171 |
+
# Extract the first triple-backtick block, optionally with a language hint
|
| 172 |
+
pattern = r"```" + re.escape(language_hint) + r"\n(.*?)```" if language_hint else r"```\s*\n(.*?)```"
|
| 173 |
+
m = re.search(pattern, text, re.DOTALL)
|
| 174 |
+
return m.group(1) if m else text # fallback to raw text
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def write_file(path: pathlib.Path, content: str):
|
| 178 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 179 |
+
path.write_text(content, encoding="utf-8")
|
| 180 |
+
log_event("file_written", {"path": str(path), "bytes": len(content)})
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def send_email_if_configured(subject: str, body: str) -> str:
|
| 184 |
+
host = os.getenv("SMTP_HOST")
|
| 185 |
+
port = int(os.getenv("SMTP_PORT", "0") or 0)
|
| 186 |
+
user = os.getenv("SMTP_USER")
|
| 187 |
+
pwd = os.getenv("SMTP_PASSWORD")
|
| 188 |
+
to_addr = os.getenv("NOTIFY_TO")
|
| 189 |
+
from_addr = os.getenv("NOTIFY_FROM", to_addr or "noreply@example.com")
|
| 190 |
+
|
| 191 |
+
if not (host and port and user and pwd and to_addr):
|
| 192 |
+
# No SMTP, log to console file
|
| 193 |
+
note_path = EVENT_DIR / "notification.txt"
|
| 194 |
+
write_file(note_path, f"SUBJECT: {subject}\n\n{body}")
|
| 195 |
+
return f"SMTP not configured. Wrote notification to {note_path}."
|
| 196 |
+
|
| 197 |
+
msg = MIMEText(body, "plain", "utf-8")
|
| 198 |
+
msg["Subject"] = subject
|
| 199 |
+
msg["From"] = from_addr
|
| 200 |
+
msg["To"] = to_addr
|
| 201 |
+
|
| 202 |
+
with smtplib.SMTP(host, port) as s:
|
| 203 |
+
s.starttls()
|
| 204 |
+
s.login(user, pwd)
|
| 205 |
+
s.sendmail(from_addr, [to_addr], msg.as_string())
|
| 206 |
+
return f"Email sent to {to_addr}."
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
# ======= Pipeline =======
|
| 210 |
+
|
| 211 |
+
def run_pipeline(requirement_text: str) -> Dict[str, Any]:
|
| 212 |
+
console.rule("[bold cyan]Agentic Pipeline Start")
|
| 213 |
+
|
| 214 |
+
# 1) Requirements & Gaps
|
| 215 |
+
log_event("agent_start", {"agent": "Requirements"})
|
| 216 |
+
req_json_str = requirements_agent.run(requirement_text)
|
| 217 |
+
log_event("agent_output", {"agent": "Requirements", "content": req_json_str})
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
req = json.loads(req_json_str)
|
| 221 |
+
except Exception:
|
| 222 |
+
# If the model didn’t return JSON, wrap it
|
| 223 |
+
req = {"spec": req_json_str, "acceptance_criteria": [], "risks": [], "assumptions": [], "approval": "APPROVED"}
|
| 224 |
+
|
| 225 |
+
approved = str(req.get("approval", "APPROVED")).upper().strip()
|
| 226 |
+
if approved != "APPROVED":
|
| 227 |
+
log_event("halt", {"reason": "Requirements not approved", "details": req})
|
| 228 |
+
return {"status": "STOPPED", "stage": "Requirements", "details": req}
|
| 229 |
+
|
| 230 |
+
# 2) Coding
|
| 231 |
+
log_event("agent_start", {"agent": "Coding"})
|
| 232 |
+
code_response = coding_agent.run(json.dumps(req, indent=2))
|
| 233 |
+
log_event("agent_output", {"agent": "Coding", "content": code_response[:2000]})
|
| 234 |
+
app_py = extract_code_block(code_response)
|
| 235 |
+
app_path = OUT_DIR / "app.py"
|
| 236 |
+
write_file(app_path, app_py)
|
| 237 |
+
|
| 238 |
+
# 3) Review
|
| 239 |
+
log_event("agent_start", {"agent": "Review"})
|
| 240 |
+
review_md = review_agent.run(app_py)
|
| 241 |
+
log_event("agent_output", {"agent": "Review", "content": review_md})
|
| 242 |
+
approved = "APPROVED" in review_md.upper()
|
| 243 |
+
|
| 244 |
+
# You could iterate fixes here if CHANGES REQUESTED; for POC, proceed either way
|
| 245 |
+
|
| 246 |
+
# 4) Tests
|
| 247 |
+
log_event("agent_start", {"agent": "Test"})
|
| 248 |
+
test_response = test_agent.run(app_py)
|
| 249 |
+
log_event("agent_output", {"agent": "Test", "content": test_response[:2000]})
|
| 250 |
+
tests_py = extract_code_block(test_response)
|
| 251 |
+
tests_path = OUT_DIR / "test_app.py"
|
| 252 |
+
write_file(tests_path, tests_py)
|
| 253 |
+
|
| 254 |
+
# 5) Deploy & Notify
|
| 255 |
+
log_event("agent_start", {"agent": "Deploy"})
|
| 256 |
+
deploy_json_str = deploy_agent.run("App written to generated/app.py and tests to generated/test_app.py. Provide steps to run locally and send a completion note.")
|
| 257 |
+
log_event("agent_output", {"agent": "Deploy", "content": deploy_json_str})
|
| 258 |
+
try:
|
| 259 |
+
deploy_info = json.loads(deploy_json_str)
|
| 260 |
+
except Exception:
|
| 261 |
+
deploy_info = {"run_steps": ["pip install -r requirements.txt", "python generated/app.py"], "notification": deploy_json_str}
|
| 262 |
+
|
| 263 |
+
# Send email if possible
|
| 264 |
+
notify_result = send_email_if_configured(
|
| 265 |
+
subject="Agentic POC Deployed",
|
| 266 |
+
body="Deployment complete. Steps:\n" + "\n".join(deploy_info.get("run_steps", []))
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
summary = {
|
| 270 |
+
"status": "DONE",
|
| 271 |
+
"app_path": str(app_path.resolve()),
|
| 272 |
+
"tests_path": str(tests_path.resolve()),
|
| 273 |
+
"review": review_md,
|
| 274 |
+
"deploy": deploy_info,
|
| 275 |
+
"notify": notify_result,
|
| 276 |
+
"event_log": str(EVENT_LOG.resolve()),
|
| 277 |
+
}
|
| 278 |
+
log_event("done", summary)
|
| 279 |
+
return summary
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
if __name__ == "__main__":
|
| 283 |
+
# Default requirement if none provided
|
| 284 |
+
requirement_text = (
|
| 285 |
+
os.getenv("REQUIREMENT")
|
| 286 |
+
or "User logs in with email/password; after success, show a dashboard with 3 KPI cards (Visitors, Conversions, Revenue) and a small table of 5 rows. Logout supported."
|
| 287 |
+
)
|
| 288 |
+
console.print("[bold]Running with requirement:[/bold]", requirement_text)
|
| 289 |
+
result = run_pipeline(requirement_text)
|
| 290 |
+
|
| 291 |
+
# Pretty table
|
| 292 |
+
table = Table(title="Pipeline Summary")
|
| 293 |
+
table.add_column("Key")
|
| 294 |
+
table.add_column("Value")
|
| 295 |
+
for k, v in result.items():
|
| 296 |
+
table.add_row(k, (v if isinstance(v, str) else json.dumps(v)[:500]))
|
| 297 |
+
console.print(table)
|
| 298 |
+
console.print(f"Event log: {EVENT_LOG}")
|
| 299 |
+
````
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
## 4) `streamlit_app.py` (monitor & trigger)
|
| 304 |
+
|
| 305 |
+
```python
|
| 306 |
+
# streamlit_app.py
|
| 307 |
+
import json, pathlib, time, os
|
| 308 |
+
import streamlit as st
|
| 309 |
+
from orchestrator import run_pipeline, EVENT_DIR
|
| 310 |
+
|
| 311 |
+
st.set_page_config(page_title="Agentic Pipeline Monitor", layout="wide")
|
| 312 |
+
st.title("🧩 Agentic AI Pipeline — Login→Dashboard POC")
|
| 313 |
+
|
| 314 |
+
with st.sidebar:
|
| 315 |
+
st.header("Run Settings")
|
| 316 |
+
req = st.text_area(
|
| 317 |
+
"Requirement",
|
| 318 |
+
value=(
|
| 319 |
+
"User logs in with email/password; after success, show a dashboard with 3 KPI cards "
|
| 320 |
+
"(Visitors, Conversions, Revenue) and a 5-row table. Logout supported."
|
| 321 |
+
), height=140
|
| 322 |
+
)
|
| 323 |
+
if st.button("Run Pipeline", type="primary"):
|
| 324 |
+
with st.spinner("Running agents..."):
|
| 325 |
+
result = run_pipeline(req)
|
| 326 |
+
st.session_state["last_result"] = result
|
| 327 |
+
st.success("Pipeline finished")
|
| 328 |
+
|
| 329 |
+
result = st.session_state.get("last_result")
|
| 330 |
+
col1, col2 = st.columns([2,1])
|
| 331 |
+
|
| 332 |
+
with col1:
|
| 333 |
+
st.subheader("📜 Event Log")
|
| 334 |
+
# Load most recent .jsonl in events/
|
| 335 |
+
logs = sorted(pathlib.Path(EVENT_DIR).glob("run_*.jsonl"))
|
| 336 |
+
if logs:
|
| 337 |
+
latest = logs[-1]
|
| 338 |
+
st.caption(f"Latest log: {latest.name}")
|
| 339 |
+
lines = latest.read_text(encoding="utf-8").strip().splitlines()
|
| 340 |
+
for line in lines:
|
| 341 |
+
rec = json.loads(line)
|
| 342 |
+
st.write({k: rec[k] for k in ["ts", "kind"] if k in rec})
|
| 343 |
+
with st.expander("details", expanded=False):
|
| 344 |
+
st.json(rec)
|
| 345 |
+
else:
|
| 346 |
+
st.info("No runs yet.")
|
| 347 |
+
|
| 348 |
+
with col2:
|
| 349 |
+
st.subheader("📦 Outputs")
|
| 350 |
+
gen_dir = pathlib.Path("generated")
|
| 351 |
+
if gen_dir.exists():
|
| 352 |
+
for p in sorted(gen_dir.glob("*.py")):
|
| 353 |
+
st.code(p.read_text(encoding="utf-8")[:4000], language="python")
|
| 354 |
+
else:
|
| 355 |
+
st.caption("No generated files yet.")
|
| 356 |
+
|
| 357 |
+
if result:
|
| 358 |
+
st.subheader("✅ Summary")
|
| 359 |
+
st.json(result)
|
| 360 |
+
st.markdown("---")
|
| 361 |
+
st.markdown("**Run locally:**")
|
| 362 |
+
st.code("""
|
| 363 |
+
pip install -r requirements.txt
|
| 364 |
+
python generated/app.py
|
| 365 |
+
# Visit http://localhost:5000
|
| 366 |
+
""")
|
| 367 |
+
```
|
| 368 |
+
|
| 369 |
+
---
|
| 370 |
+
|
| 371 |
+
## 5) `README.md` (quick start)
|
| 372 |
+
|
| 373 |
+
````markdown
|
| 374 |
+
# Agentic AI Orchestrator — Login→Dashboard POC
|
| 375 |
+
|
| 376 |
+
## Prereqs
|
| 377 |
+
- Python 3.10+
|
| 378 |
+
- `OPENAI_API_KEY` set in your environment
|
| 379 |
+
|
| 380 |
+
## Install
|
| 381 |
+
```bash
|
| 382 |
+
pip install -r requirements.txt
|
| 383 |
+
````
|
| 384 |
+
|
| 385 |
+
## Run (CLI)
|
| 386 |
+
|
| 387 |
+
```bash
|
| 388 |
+
python orchestrator.py
|
| 389 |
+
```
|
| 390 |
+
|
| 391 |
+
This generates `generated/app.py` and `generated/test_app.py`, logs events to `events/run_<ts>.jsonl`, and (optionally) sends an email if SMTP envs are set.
|
| 392 |
+
|
| 393 |
+
## Run the monitor UI (Streamlit)
|
| 394 |
+
|
| 395 |
+
```bash
|
| 396 |
+
streamlit run streamlit_app.py
|
| 397 |
+
```
|
| 398 |
+
|
| 399 |
+
* Paste/Edit your requirement in the sidebar.
|
| 400 |
+
* Click **Run Pipeline** to watch each agent’s output.
|
| 401 |
+
|
| 402 |
+
## Execute the app manually
|
| 403 |
+
|
| 404 |
+
```bash
|
| 405 |
+
python generated/app.py
|
| 406 |
+
# Then open http://localhost:5000
|
| 407 |
+
# Login with demo@example.com / demo123
|
| 408 |
+
```
|
| 409 |
+
|
| 410 |
+
## Run tests
|
| 411 |
+
|
| 412 |
+
```bash
|
| 413 |
+
pytest -q
|
| 414 |
+
```
|
| 415 |
+
|
| 416 |
+
## Optional SMTP (email on completion)
|
| 417 |
+
|
| 418 |
+
Set:
|
| 419 |
+
|
| 420 |
+
```
|
| 421 |
+
SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, NOTIFY_TO, [NOTIFY_FROM]
|
| 422 |
+
```
|
| 423 |
+
|
| 424 |
+
If these are missing, the pipeline writes `events/notification.txt` instead.
|
| 425 |
+
|
| 426 |
+
```
|
| 427 |
+
|
| 428 |
+
---
|
| 429 |
+
|
| 430 |
+
## Notes & Extensibility
|
| 431 |
+
- To enforce *agent-to-agent* handoffs with iterations (e.g., auto‑fix review comments), loop the Coding→Review stages until REVIEW returns APPROVED (or max N).
|
| 432 |
+
- Swap `MODEL` via `OPENAI_MODEL` env.
|
| 433 |
+
- Add tracing by pushing `EVENT_LOG` records to a DB (e.g., SQLite) and plotting a Gantt timeline in Streamlit.
|
| 434 |
+
- For real deployments, have the Deploy Agent produce Dockerfile, compose.yaml, and healthchecks; adjust unit tests to include integration tests.
|
| 435 |
+
```
|