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amlit UI)
|
| 3 |
+
|
| 4 |
+
Modes:
|
| 5 |
+
- CLI (default): python agentic_orchestrator_single.py --requirement "..."
|
| 6 |
+
- Streamlit UI: AGENTIC_UI=streamlit streamlit run agentic_orchestrator_single.py
|
| 7 |
+
|
| 8 |
+
Features:
|
| 9 |
+
- 5 agents (Requirements → Coding → Review → Test → Deploy+Notify)
|
| 10 |
+
- Writes outputs to ./generated and logs to ./events/*.jsonl
|
| 11 |
+
- Optional SMTP email on completion via env vars
|
| 12 |
+
|
| 13 |
+
Env:
|
| 14 |
+
OPENAI_API_KEY (required)
|
| 15 |
+
OPENAI_MODEL (default: gpt-4o-mini)
|
| 16 |
+
SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, NOTIFY_TO, [NOTIFY_FROM] (optional)
|
| 17 |
+
|
| 18 |
+
Author: Agentic-AI
|
| 19 |
+
"""
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
import os, sys, json, time, re, pathlib, smtplib, argparse
|
| 22 |
+
from email.mime.text import MIMEText
|
| 23 |
+
from dataclasses import dataclass
|
| 24 |
+
from typing import Dict, Any
|
| 25 |
+
|
| 26 |
+
# -------- Optional deps (loaded lazily) --------
|
| 27 |
+
try:
|
| 28 |
+
from rich.console import Console
|
| 29 |
+
from rich.table import Table
|
| 30 |
+
except Exception: # pragma: no cover
|
| 31 |
+
Console = None
|
| 32 |
+
Table = None
|
| 33 |
+
|
| 34 |
+
# OpenAI SDK
|
| 35 |
+
try:
|
| 36 |
+
from openai import OpenAI
|
| 37 |
+
except Exception as e: # pragma: no cover
|
| 38 |
+
raise SystemExit("OpenAI SDK not installed. Run: pip install openai")
|
| 39 |
+
|
| 40 |
+
# ========== Config & Paths ==========
|
| 41 |
+
MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
|
| 42 |
+
OUT_DIR = pathlib.Path("generated")
|
| 43 |
+
EVENT_DIR = pathlib.Path("events")
|
| 44 |
+
OUT_DIR.mkdir(exist_ok=True)
|
| 45 |
+
EVENT_DIR.mkdir(exist_ok=True)
|
| 46 |
+
EVENT_LOG = EVENT_DIR / f"run_{int(time.time())}.jsonl"
|
| 47 |
+
|
| 48 |
+
client = OpenAI()
|
| 49 |
+
console = Console() if Console else None
|
| 50 |
+
|
| 51 |
+
# ========== Agent Prompts ==========
|
| 52 |
+
REQUIREMENTS_AGENT = (
|
| 53 |
+
"""
|
| 54 |
+
You are the Requirements & Gap Analysis Agent.
|
| 55 |
+
Task: Given a short requirement about a "login then dashboard" app, produce:
|
| 56 |
+
- A concise MVP spec (fill obvious gaps).
|
| 57 |
+
- Acceptance criteria (bullets).
|
| 58 |
+
- Risks & assumptions.
|
| 59 |
+
- Final line: APPROVED or NEEDS INFO.
|
| 60 |
+
Return strict JSON with keys: spec, acceptance_criteria, risks, assumptions, approval.
|
| 61 |
+
"""
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
CODING_AGENT = (
|
| 65 |
+
"""
|
| 66 |
+
You are the Coding Agent. Generate a minimal Flask web app with:
|
| 67 |
+
- / (GET) redirects to /login if not authenticated
|
| 68 |
+
- /login (GET Form, POST auth). One hardcoded demo user: user="demo@example.com", pwd="demo123".
|
| 69 |
+
- /dashboard (GET) protected route showing 3 KPI cards and a small table of 5 rows (dummy data).
|
| 70 |
+
- Use Flask sessions (secret key). No DB. Inline templates with render_template_string.
|
| 71 |
+
- App in a single file named app.py compatible with `python app.py` to run on port 5000.
|
| 72 |
+
Return ONLY a triple backticked code block containing app.py.
|
| 73 |
+
"""
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
REVIEW_AGENT = (
|
| 77 |
+
"""
|
| 78 |
+
You are the Code Review Agent.
|
| 79 |
+
- Read app.py content.
|
| 80 |
+
- Report under: Security, Correctness, Readability, Performance.
|
| 81 |
+
- Suggest specific fixes (diff-style bullets).
|
| 82 |
+
- End with: APPROVED or CHANGES REQUESTED.
|
| 83 |
+
Output in Markdown.
|
| 84 |
+
"""
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
TEST_AGENT = (
|
| 88 |
+
"""
|
| 89 |
+
You are the Test Agent. Create pytest tests for app.py:
|
| 90 |
+
- test_login_success()
|
| 91 |
+
- test_login_failure()
|
| 92 |
+
- test_dashboard_requires_login()
|
| 93 |
+
- test_dashboard_after_login()
|
| 94 |
+
Use Flask test client by importing the app as: `import app as app_module; app = app_module.app`.
|
| 95 |
+
Return ONLY a triple backticked code block for tests named test_app.py.
|
| 96 |
+
"""
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
DEPLOY_AGENT = (
|
| 100 |
+
"""
|
| 101 |
+
You are the Deploy & Notify Agent.
|
| 102 |
+
- Provide concise steps to run locally with python.
|
| 103 |
+
- If SMTP env variables are present, assume an email will be sent and summarize a subject/body.
|
| 104 |
+
- Else, state that a console log file will be written.
|
| 105 |
+
Return strict JSON with keys: run_steps (list of steps), notification (string summary).
|
| 106 |
+
"""
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# ========== Agent Wrapper ==========
|
| 110 |
+
@dataclass
|
| 111 |
+
class Agent:
|
| 112 |
+
name: str
|
| 113 |
+
system: str
|
| 114 |
+
|
| 115 |
+
def run(self, user_content: str) -> str:
|
| 116 |
+
messages = [
|
| 117 |
+
{"role": "system", "content": self.system},
|
| 118 |
+
{"role": "user", "content": user_content},
|
| 119 |
+
]
|
| 120 |
+
response = client.chat.completions.create(
|
| 121 |
+
model=MODEL,
|
| 122 |
+
messages=messages,
|
| 123 |
+
temperature=0.2,
|
| 124 |
+
)
|
| 125 |
+
return response.choices[0].message.content or ""
|
| 126 |
+
|
| 127 |
+
requirements_agent = Agent("Requirements", REQUIREMENTS_AGENT)
|
| 128 |
+
coding_agent = Agent("Coding", CODING_AGENT)
|
| 129 |
+
review_agent = Agent("Review", REVIEW_AGENT)
|
| 130 |
+
test_agent = Agent("Test", TEST_AGENT)
|
| 131 |
+
deploy_agent = Agent("Deploy", DEPLOY_AGENT)
|
| 132 |
+
|
| 133 |
+
# ========== Helpers ==========
|
| 134 |
+
def log_event(kind: str, payload: Dict[str, Any]):
|
| 135 |
+
rec = {"ts": time.time(), "kind": kind, **payload}
|
| 136 |
+
with open(EVENT_LOG, "a", encoding="utf-8") as f:
|
| 137 |
+
f.write(json.dumps(rec) + "\n")
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def extract_code_block(text: str) -> str:
|
| 141 |
+
m = re.search(r"```[a-zA-Z0-9]*\n(.*?)```", text, re.DOTALL)
|
| 142 |
+
return m.group(1).strip() if m else text
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def write_file(path: pathlib.Path, content: str):
|
| 146 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 147 |
+
path.write_text(content, encoding="utf-8")
|
| 148 |
+
log_event("file_written", {"path": str(path), "bytes": len(content)})
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def send_email_if_configured(subject: str, body: str) -> str:
|
| 152 |
+
host = os.getenv("SMTP_HOST")
|
| 153 |
+
port = int(os.getenv("SMTP_PORT", "0") or 0)
|
| 154 |
+
user = os.getenv("SMTP_USER")
|
| 155 |
+
pwd = os.getenv("SMTP_PASSWORD")
|
| 156 |
+
to_addr = os.getenv("NOTIFY_TO")
|
| 157 |
+
from_addr = os.getenv("NOTIFY_FROM", to_addr or "noreply@example.com")
|
| 158 |
+
|
| 159 |
+
if not (host and port and user and pwd and to_addr):
|
| 160 |
+
# No SMTP, log to file
|
| 161 |
+
note_path = EVENT_DIR / "notification.txt"
|
| 162 |
+
write_file(note_path, f"SUBJECT: {subject}\n\n{body}")
|
| 163 |
+
return f"SMTP not configured. Wrote notification to {note_path}."
|
| 164 |
+
|
| 165 |
+
msg = MIMEText(body, "plain", "utf-8")
|
| 166 |
+
msg["Subject"] = subject
|
| 167 |
+
msg["From"] = from_addr
|
| 168 |
+
msg["To"] = to_addr
|
| 169 |
+
|
| 170 |
+
with smtplib.SMTP(host, port) as s:
|
| 171 |
+
s.starttls()
|
| 172 |
+
s.login(user, pwd)
|
| 173 |
+
s.sendmail(from_addr, [to_addr], msg.as_string())
|
| 174 |
+
return f"Email sent to {to_addr}."
|
| 175 |
+
|
| 176 |
+
# ========== Pipeline ==========
|
| 177 |
+
|
| 178 |
+
def run_pipeline(requirement_text: str) -> Dict[str, Any]:
|
| 179 |
+
log_event("start", {"requirement": requirement_text})
|
| 180 |
+
|
| 181 |
+
# 1) Requirements
|
| 182 |
+
log_event("agent_start", {"agent": "Requirements"})
|
| 183 |
+
req_json_str = requirements_agent.run(requirement_text)
|
| 184 |
+
log_event("agent_output", {"agent": "Requirements", "content": req_json_str})
|
| 185 |
+
try:
|
| 186 |
+
req = json.loads(req_json_str)
|
| 187 |
+
except Exception:
|
| 188 |
+
req = {"spec": req_json_str, "acceptance_criteria": [], "risks": [], "assumptions": [], "approval": "APPROVED"}
|
| 189 |
+
|
| 190 |
+
if str(req.get("approval", "APPROVED")).upper().strip() != "APPROVED":
|
| 191 |
+
log_event("halt", {"reason": "Requirements not approved", "details": req})
|
| 192 |
+
return {"status": "STOPPED", "stage": "Requirements", "details": req, "event_log": str(EVENT_LOG.resolve())}
|
| 193 |
+
|
| 194 |
+
# 2) Coding
|
| 195 |
+
log_event("agent_start", {"agent": "Coding"})
|
| 196 |
+
code_response = coding_agent.run(json.dumps(req, indent=2))
|
| 197 |
+
log_event("agent_output", {"agent": "Coding", "content": code_response[:2000]})
|
| 198 |
+
app_py = extract_code_block(code_response)
|
| 199 |
+
app_path = OUT_DIR / "app.py"
|
| 200 |
+
write_file(app_path, app_py)
|
| 201 |
+
|
| 202 |
+
# 3) Review
|
| 203 |
+
log_event("agent_start", {"agent": "Review"})
|
| 204 |
+
review_md = review_agent.run(app_py)
|
| 205 |
+
log_event("agent_output", {"agent": "Review", "content": review_md})
|
| 206 |
+
|
| 207 |
+
# 4) Tests
|
| 208 |
+
log_event("agent_start", {"agent": "Test"})
|
| 209 |
+
test_response = test_agent.run(app_py)
|
| 210 |
+
log_event("agent_output", {"agent": "Test", "content": test_response[:2000]})
|
| 211 |
+
tests_py = extract_code_block(test_response)
|
| 212 |
+
tests_path = OUT_DIR / "test_app.py"
|
| 213 |
+
write_file(tests_path, tests_py)
|
| 214 |
+
|
| 215 |
+
# 5) Deploy & Notify
|
| 216 |
+
log_event("agent_start", {"agent": "Deploy"})
|
| 217 |
+
deploy_json_str = deploy_agent.run("App at generated/app.py, tests at generated/test_app.py. Provide local run steps and summarize notification.")
|
| 218 |
+
log_event("agent_output", {"agent": "Deploy", "content": deploy_json_str})
|
| 219 |
+
try:
|
| 220 |
+
deploy_info = json.loads(deploy_json_str)
|
| 221 |
+
except Exception:
|
| 222 |
+
deploy_info = {"run_steps": ["pip install -r requirements.txt", "python generated/app.py"], "notification": deploy_json_str}
|
| 223 |
+
|
| 224 |
+
notify_result = send_email_if_configured(
|
| 225 |
+
subject="Agentic POC Deployed",
|
| 226 |
+
body="Deployment complete. Steps:\n" + "\n".join(deploy_info.get("run_steps", []))
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
summary = {
|
| 230 |
+
"status": "DONE",
|
| 231 |
+
"app_path": str(app_path.resolve()),
|
| 232 |
+
"tests_path": str(tests_path.resolve()),
|
| 233 |
+
"review": review_md,
|
| 234 |
+
"deploy": deploy_info,
|
| 235 |
+
"notify": notify_result,
|
| 236 |
+
"event_log": str(EVENT_LOG.resolve()),
|
| 237 |
+
}
|
| 238 |
+
log_event("done", summary)
|
| 239 |
+
return summary
|
| 240 |
+
|
| 241 |
+
# ========== CLI ==========
|
| 242 |
+
|
| 243 |
+
def main_cli(argv=None):
|
| 244 |
+
p = argparse.ArgumentParser(description="Agentic Pipeline (single file)")
|
| 245 |
+
p.add_argument("--requirement", "-r", type=str, default=(
|
| 246 |
+
"User logs in with email/password; after success, show a dashboard with 3 KPI cards (Visitors,"
|
| 247 |
+
" Conversions, Revenue) and a 5-row table. Logout supported."
|
| 248 |
+
))
|
| 249 |
+
args = p.parse_args(argv)
|
| 250 |
+
|
| 251 |
+
if console:
|
| 252 |
+
console.rule("Agentic Pipeline")
|
| 253 |
+
console.print("[bold]Model:[/bold]", MODEL)
|
| 254 |
+
console.print("[bold]Requirement:[/bold]", args.requirement)
|
| 255 |
+
|
| 256 |
+
result = run_pipeline(args.requirement)
|
| 257 |
+
|
| 258 |
+
if Table and console:
|
| 259 |
+
table = Table(title="Pipeline Summary")
|
| 260 |
+
table.add_column("Key")
|
| 261 |
+
table.add_column("Value")
|
| 262 |
+
for k, v in result.items():
|
| 263 |
+
v_str = v if isinstance(v, str) else json.dumps(v)[:800]
|
| 264 |
+
table.add_row(k, v_str)
|
| 265 |
+
console.print(table)
|
| 266 |
+
console.print(f"Event log: {EVENT_LOG}")
|
| 267 |
+
else:
|
| 268 |
+
print(json.dumps(result, indent=2))
|
| 269 |
+
|
| 270 |
+
# ========== Optional Streamlit UI (in the same file) ==========
|
| 271 |
+
# Usage: AGENTIC_UI=streamlit streamlit run agentic_orchestrator_single.py
|
| 272 |
+
if os.getenv("AGENTIC_UI") == "streamlit":
|
| 273 |
+
try:
|
| 274 |
+
import streamlit as st # type: ignore
|
| 275 |
+
st.set_page_config(page_title="Agentic Pipeline (Single File)", layout="wide")
|
| 276 |
+
st.title("🧩 Agentic AI Pipeline — Single File POC")
|
| 277 |
+
with st.sidebar:
|
| 278 |
+
st.header("Run Settings")
|
| 279 |
+
req = st.text_area("Requirement", value=(
|
| 280 |
+
"User logs in with email/password; after success, show a dashboard with 3 KPI cards "
|
| 281 |
+
"(Visitors, Conversions, Revenue) and a 5-row table. Logout supported."
|
| 282 |
+
), height=140)
|
| 283 |
+
if st.button("Run Pipeline", type="primary"):
|
| 284 |
+
with st.spinner("Running agents..."):
|
| 285 |
+
result = run_pipeline(req)
|
| 286 |
+
st.session_state["last_result"] = result
|
| 287 |
+
st.success("Pipeline finished")
|
| 288 |
+
result = st.session_state.get("last_result")
|
| 289 |
+
st.subheader("📜 Event Log (latest run)")
|
| 290 |
+
logs = sorted(pathlib.Path(EVENT_DIR).glob("run_*.jsonl"))
|
| 291 |
+
if logs:
|
| 292 |
+
latest = logs[-1]
|
| 293 |
+
st.caption(f"Latest log: {latest.name}")
|
| 294 |
+
lines = latest.read_text(encoding="utf-8").strip().splitlines()
|
| 295 |
+
for line in lines:
|
| 296 |
+
rec = json.loads(line)
|
| 297 |
+
st.write({k: rec[k] for k in ["ts", "kind"] if k in rec})
|
| 298 |
+
with st.expander("details", expanded=False):
|
| 299 |
+
st.json(rec)
|
| 300 |
+
else:
|
| 301 |
+
st.info("No runs yet.")
|
| 302 |
+
if result:
|
| 303 |
+
st.subheader("✅ Summary")
|
| 304 |
+
st.json(result)
|
| 305 |
+
st.markdown("---")
|
| 306 |
+
st.markdown("**Run locally:**")
|
| 307 |
+
st.code("""
|
| 308 |
+
pip install -r requirements.txt
|
| 309 |
+
python generated/app.py
|
| 310 |
+
# Visit http://localhost:5000
|
| 311 |
+
""")
|
| 312 |
+
except ImportError:
|
| 313 |
+
pass # Streamlit not installed; ignore
|
| 314 |
+
|
| 315 |
+
# ========== Entry ==========
|
| 316 |
+
if __name__ == "__main__" and os.getenv("AGENTIC_UI") != "streamlit":
|
| 317 |
+
main_cli()
|