Spaces:
Running
Running
File size: 23,558 Bytes
b85de73 f3c7154 e288267 f3c7154 e288267 f3c7154 e288267 f3c7154 e288267 f3c7154 e288267 f3c7154 e288267 f3c7154 e288267 f3c7154 b85de73 7359e1b f849e0a 7359e1b f849e0a 7359e1b b85de73 d32594b b85de73 d32594b b85de73 e288267 b85de73 f3c7154 b85de73 f3c7154 b85de73 f3c7154 b85de73 f3c7154 b85de73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | from __future__ import annotations
import json
import os
import re
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
import streamlit as st
ROOT = Path(__file__).resolve().parent
SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
from qa_langgraph.export import build_run_export
from qa_langgraph.graph import build_graph
from qa_langgraph.registry import AGENTS, default_agent_configs
from qa_langgraph.run_logger import read_stage_traces_jsonl, write_run_export
from qa_langgraph.sample_scenarios import CUSTOM_SCENARIO, load_sample_scenario, scenario_options
from qa_langgraph.service_client import DEFAULT_SERVICE_URL, normalize_service_url, validate_agent_service
def init_session() -> None:
default_service_url = normalize_service_url(os.getenv("QA_AGENT_SERVICE_URL", DEFAULT_SERVICE_URL))
st.session_state.setdefault("agent_configs", default_agent_configs())
st.session_state.setdefault("run_state", {})
st.session_state.setdefault("run_export", None)
st.session_state.setdefault("run_status", "not run")
st.session_state.setdefault("current_run_id", "")
st.session_state.setdefault("stage_traces_jsonl", "")
st.session_state.setdefault("selected_scenario", CUSTOM_SCENARIO)
st.session_state.setdefault("title", "")
st.session_state.setdefault("requirements_text", "")
st.session_state.setdefault("qa_agent_service_url", default_service_url)
def agent_status_rows(stage_traces: list[dict]) -> list[dict]:
latest: dict[str, dict] = {}
for trace in stage_traces:
latest[trace.get("agent_key", "")] = trace
rows = []
for key, agent in AGENTS.items():
trace = latest.get(key, {})
rows.append(
{
"agent": agent.name,
"status": trace.get("status", "not started"),
"iteration": trace.get("iteration", ""),
"duration_ms": trace.get("duration_ms", ""),
}
)
return rows
def safe_filename_part(value: str, fallback: str = "scenario") -> str:
cleaned = re.sub(r"[^A-Za-z0-9]+", "_", value.strip()).strip("_")
return cleaned or fallback
def download_basename(export: dict) -> str:
title = safe_filename_part(export.get("title", "scenario"))
created_at = export.get("run_metadata", {}).get("created_at", "")
try:
timestamp = datetime.fromisoformat(created_at.replace("Z", "+00:00")).strftime("%Y%m%d-%H%M%S")
except ValueError:
timestamp = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
return f"LangGraph_{title}_{timestamp}"
def _dot_label(label: str) -> str:
return label.replace('"', '\\"')
def runtime_path_edges(stage_traces: list[dict], orchestration_decision: dict) -> set[tuple[str, str]]:
edges: set[tuple[str, str]] = set()
ordered_nodes = [trace.get("agent_key") for trace in stage_traces if trace.get("agent_key")]
if ordered_nodes:
edges.add(("START", ordered_nodes[0]))
for source, target in zip(ordered_nodes, ordered_nodes[1:]):
edges.add((source, target))
decision = orchestration_decision.get("decision")
target_agent = orchestration_decision.get("target_agent")
if ordered_nodes and ordered_nodes[-1] == "orchestrator":
if decision == "finish":
edges.add(("orchestrator", "END"))
elif decision == "fail":
edges.add(("orchestrator", "END_FAIL"))
elif decision == "backtrack" and target_agent in {"requirements_analyst", "test_designer", "reviewer"}:
edges.add(("orchestrator", target_agent))
return edges
def build_workflow_dot(stage_traces: list[dict] | None = None, orchestration_decision: dict | None = None) -> str:
active_edges = runtime_path_edges(stage_traces or [], orchestration_decision or {})
completed_nodes = {trace.get("agent_key") for trace in (stage_traces or []) if trace.get("status") == "completed"}
failed_nodes = {trace.get("agent_key") for trace in (stage_traces or []) if trace.get("status") == "failed"}
nodes = {
"START": {"label": "START", "shape": "circle", "fillcolor": "#1e293b", "fontcolor": "#f8fafc"},
"requirements_analyst": {
"label": "Requirements Analyst\\n(service)",
"shape": "box",
"fillcolor": "#0b5fff" if "requirements_analyst" in completed_nodes else "#f8fafc",
"fontcolor": "#ffffff" if "requirements_analyst" in completed_nodes else "#0f172a",
},
"test_designer": {
"label": "Test Designer\\n(service)",
"shape": "box",
"fillcolor": "#0b5fff" if "test_designer" in completed_nodes else "#f8fafc",
"fontcolor": "#ffffff" if "test_designer" in completed_nodes else "#0f172a",
},
"reviewer": {
"label": "Reviewer\\n(service)",
"shape": "box",
"fillcolor": "#0b5fff" if "reviewer" in completed_nodes else "#f8fafc",
"fontcolor": "#ffffff" if "reviewer" in completed_nodes else "#0f172a",
},
"orchestrator": {
"label": "Orchestrator\\n(local deterministic)",
"shape": "diamond",
"fillcolor": "#f59e0b" if "orchestrator" in completed_nodes else "#fef3c7",
"fontcolor": "#0f172a",
},
"END": {"label": "END\\nfinish", "shape": "doublecircle", "fillcolor": "#16a34a", "fontcolor": "#ffffff"},
"END_FAIL": {"label": "END\\nfail", "shape": "doublecircle", "fillcolor": "#dc2626", "fontcolor": "#ffffff"},
}
for node_key in failed_nodes:
if node_key in nodes:
nodes[node_key]["fillcolor"] = "#dc2626"
nodes[node_key]["fontcolor"] = "#ffffff"
edges = [
("START", "requirements_analyst", ""),
("requirements_analyst", "test_designer", ""),
("test_designer", "reviewer", ""),
("reviewer", "orchestrator", ""),
("orchestrator", "END", "finish"),
("orchestrator", "requirements_analyst", "backtrack"),
("orchestrator", "test_designer", "backtrack"),
("orchestrator", "reviewer", "backtrack"),
("orchestrator", "END_FAIL", "fail"),
]
lines = [
"digraph LangGraphWorkflow {",
' graph [rankdir=LR, bgcolor="#f8fafc", fontname="Helvetica", nodesep="0.55", ranksep="0.75", pad="0.25"];',
' node [style="rounded,filled", color="#0f172a", penwidth=1.5, fontname="Helvetica"];',
' edge [color="#334155", penwidth=1.9, fontname="Helvetica", fontsize=11, fontcolor="#0f172a"];',
]
for node_key, attrs in nodes.items():
lines.append(
f' {node_key} [label="{_dot_label(attrs["label"])}", shape="{attrs["shape"]}", fillcolor="{attrs["fillcolor"]}", fontcolor="{attrs["fontcolor"]}"];'
)
for source, target, label in edges:
is_active = (source, target) in active_edges
color = "#0b5fff" if is_active else "#334155"
penwidth = "3.4" if is_active else "1.9"
fontcolor = "#0b5fff" if is_active else "#0f172a"
label_attr = f', label="{label}", fontcolor="{fontcolor}"' if label else ""
lines.append(f' {source} -> {target} [color="{color}", penwidth={penwidth}{label_attr}];')
lines.append("}")
return "\n".join(lines)
def runtime_path_summary(stage_traces: list[dict], orchestration_decision: dict) -> str:
ordered_names = [AGENTS[trace["agent_key"]].name for trace in stage_traces if trace.get("agent_key") in AGENTS]
if not ordered_names:
return "No run has been executed yet."
summary = " -> ".join(ordered_names)
decision = orchestration_decision.get("decision")
if decision == "finish":
return f"{summary} -> END (finish)"
if decision == "fail":
return f"{summary} -> END (fail)"
if decision == "backtrack" and orchestration_decision.get("target_agent") in AGENTS:
target_name = AGENTS[orchestration_decision["target_agent"]].name
return f"{summary} -> backtrack to {target_name}"
return summary
def render_service_help(service_url: str) -> None:
rest_url = normalize_service_url(service_url)
st.markdown("**Use REST here, not MCP.**")
st.caption("LangGraph calls FastAPI REST endpoints. MCP on port 8008 is for MCP clients and does not expose Swagger/OpenAPI.")
st.table(
[
{
"server": "Hosted REST/FastAPI",
"default": "https://helmfridsson-qa-agent-service.hf.space/api/v1",
"paths": "/health, /docs, /openapi.json, /agents/{agent_key}/invoke",
},
{
"server": "Local REST/FastAPI",
"default": "http://127.0.0.1:8009/api/v1",
"paths": "/health, /docs, /openapi.json, /agents/{agent_key}/invoke",
},
{"server": "Local MCP", "default": "http://127.0.0.1:8008/mcp", "paths": "/mcp only, no Swagger/OpenAPI"},
]
)
st.caption("You can paste the full REST base URL, the service root, `/docs`, `/health`, or the Hugging Face Space page URL. The app normalizes it to `/api/v1`.")
st.markdown(
f"""
REST links for the configured URL:
- Health: [{rest_url}/health]({rest_url}/health)
- Swagger: [{rest_url}/docs]({rest_url}/docs)
- OpenAPI JSON: [{rest_url}/openapi.json]({rest_url}/openapi.json)
"""
)
st.code(
"""cd /Users/tomashelmfridsson/workspace/qa-agent-service
PYTHONPATH=src uvicorn api:app --reload --port 8009""",
language="bash",
)
st.caption("The PYTHONPATH part fixes ModuleNotFoundError when qa-agent-service is not installed in the active Python environment.")
st.caption("If the hosted /api/v1 health check returns Streamlit HTML, the Space is running the monitor UI but not exposing the REST API surface yet.")
def run_workflow(title: str, requirements_text: str, global_config: dict, agent_configs: list[dict], max_rounds: int) -> None:
if not title.strip():
st.error("Scenario title is required.")
return
if not requirements_text.strip():
st.error("Requirement text is required.")
return
if any(config.get("execution_mode") not in {"Deterministic", "Disabled"} for config in agent_configs):
validate_agent_service(global_config)
run_id = f"run_{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S')}"
state = {
"run_metadata": {
"run_id": run_id,
"created_at": datetime.now(timezone.utc).isoformat(),
"started_monotonic_ms": int(time.time() * 1000),
"app_version": "langgraph-qa-compare-0.1.0",
},
"title": title.strip(),
"source_requirements": requirements_text.strip(),
"requirements": [],
"test_designs": [],
"generated_artifacts": [],
"iterations": 1,
"run_controls": {
"max_rounds": max_rounds,
"max_feedback_messages": 0,
"max_feedback_per_agent_pair": 0,
},
"global_llm_config": global_config,
"agent_configs": agent_configs,
"stage_traces": [],
"working_memory": {"shared": {}, "agent_private": {}, "timeline": []},
"errors": [],
}
st.session_state.current_run_id = run_id
st.session_state.run_status = "running"
st.session_state.run_state = state
st.session_state.run_export = None
st.session_state.stage_traces_jsonl = ""
graph = build_graph()
final_state = state
status_slot = st.empty()
traces_slot = st.empty()
memory_slot = st.empty()
for snapshot in graph.stream(state, {"recursion_limit": max(8, max_rounds * 6)}, stream_mode="values"):
final_state = snapshot
st.session_state.run_state = final_state
st.session_state.run_status = "running"
traces = final_state.get("stage_traces", [])
memory = final_state.get("working_memory", {"shared": {}, "agent_private": {}, "timeline": []})
status_slot.info(f"Running... {len(traces)} stage trace(s), iteration {final_state.get('iterations', 1)}")
traces_slot.dataframe(agent_status_rows(traces), use_container_width=True)
memory_slot.json(
{
"shared": memory.get("shared", {}),
"timeline": memory.get("timeline", []),
}
)
export = build_run_export(final_state)
export_dict = export.model_dump(mode="json")
write_run_export(run_id, export_dict)
st.session_state.run_state = final_state
st.session_state.run_export = export_dict
st.session_state.run_status = export_dict.get("run_metadata", {}).get("status", "partial")
st.session_state.stage_traces_jsonl = read_stage_traces_jsonl(run_id)
init_session()
st.set_page_config(page_title="LangGraph QA Compare", layout="wide")
st.markdown(
"""
<style>
.stApp {
background-color: #0b1220;
color: #e5e7eb;
}
.stApp h1,
.stApp h2,
.stApp h3,
.stApp h4,
.stApp h5,
.stApp h6 {
color: #f8fafc !important;
}
.stApp [data-testid="stHeadingWithActionElements"],
.stApp [data-testid="stHeadingWithActionElements"] *,
.stApp [data-testid="stCaptionContainer"],
.stApp [data-testid="stAlertContent"],
.stApp [data-testid="stJson"] {
color: #dbe4f0 !important;
opacity: 1 !important;
}
.stApp .block-container [data-testid="stMarkdownContainer"] p,
.stApp .block-container [data-testid="stMarkdownContainer"] li,
.stApp .block-container [data-testid="stMarkdownContainer"] span,
.stApp .block-container [data-testid="stCaptionContainer"] p {
color: #dbe4f0 !important;
opacity: 1 !important;
}
.stApp [data-testid="stCodeBlock"],
.stApp pre,
.stApp code {
color: #f8fafc !important;
}
.stApp [data-testid="stCodeBlock"] pre,
.stApp pre {
background-color: #111827 !important;
}
.stApp [data-testid="stMetricValue"],
.stApp [data-testid="stMetricLabel"] {
color: #f8fafc !important;
}
.stApp [data-testid="stWidgetLabel"],
.stApp [data-testid="stExpander"] summary p,
.stApp [data-testid="stTextInput"] label,
.stApp [data-testid="stTextArea"] label,
.stApp [data-testid="stSelectbox"] label,
.stApp [data-testid="stNumberInput"] label {
color: #e2e8f0 !important;
opacity: 1 !important;
}
.stApp input,
.stApp textarea,
.stApp [data-baseweb="select"] > div {
background-color: #111827 !important;
border-color: #374151 !important;
color: #f8fafc !important;
}
.stApp [data-baseweb="select"] input,
.stApp input,
.stApp textarea {
color: #f8fafc !important;
}
.stApp input::placeholder,
.stApp textarea::placeholder {
color: #cbd5e1 !important;
opacity: 1 !important;
}
.agent-directives {
white-space: pre-wrap;
color: #f8fafc;
background: #111827;
border: 1px solid #374151;
border-radius: 0.5rem;
padding: 0.75rem;
line-height: 1.45;
}
</style>
""",
unsafe_allow_html=True,
)
st.title("LangGraph QA Compare")
with st.sidebar:
st.header("QA Agent Service")
service_url = st.text_input("REST API URL", value=st.session_state.get("qa_agent_service_url", DEFAULT_SERVICE_URL))
st.session_state.qa_agent_service_url = normalize_service_url(service_url)
service_timeout_seconds = st.number_input("Service timeout seconds", min_value=10, max_value=600, value=150, step=10)
max_rounds = st.number_input("Max rounds", min_value=1, max_value=8, value=2, step=1)
global_config = {
"qa_agent_service_url": st.session_state.qa_agent_service_url,
"service_timeout_seconds": int(service_timeout_seconds),
}
if st.button("Validate QA Agent Service"):
try:
validate_agent_service(global_config)
st.success("QA Agent Service is reachable.")
except Exception as exc:
st.error(str(exc))
with st.expander("REST vs MCP endpoints", expanded=False):
render_service_help(service_url)
if st.button("Clear session"):
st.session_state.clear()
st.rerun()
tab_input, tab_agents, tab_flow, tab_runtime, tab_memory, tab_results, tab_downloads = st.tabs(
["Scenario Input", "Agent Configuration", "Flow Map", "Runtime Activity", "Working Memory", "Results", "Downloads"]
)
with tab_input:
selected_scenario = st.selectbox(
"Predefined scenario",
scenario_options(),
index=scenario_options().index(st.session_state.get("selected_scenario", CUSTOM_SCENARIO)),
)
if st.button("Load selected scenario"):
title_value, requirements_value = load_sample_scenario(selected_scenario)
st.session_state.selected_scenario = selected_scenario
st.session_state.title = title_value
st.session_state.requirements_text = requirements_value
st.rerun()
title = st.text_input("Scenario title", value=st.session_state.get("title", ""))
requirements_text = st.text_area(
"Raw requirements",
value=st.session_state.get("requirements_text", ""),
height=300,
)
if st.button("Run workflow", type="primary"):
st.session_state.title = title
st.session_state.requirements_text = requirements_text
try:
run_workflow(title, requirements_text, global_config, st.session_state.agent_configs, int(max_rounds))
st.success("Workflow finished. Check Runtime Activity and Results.")
except Exception as exc:
st.session_state.run_status = "failed"
st.error(str(exc))
with tab_agents:
updated_configs = []
for config in st.session_state.agent_configs:
key = config["agent_key"]
agent_definition = AGENTS[key]
with st.expander(config["agent_name"], expanded=False):
mode_options = ["Deterministic", "Disabled"] if key == "orchestrator" else ["Service-backed", "Deterministic", "Disabled"]
current_mode = config.get("execution_mode", "Deterministic" if key == "orchestrator" else "Service-backed")
if current_mode == "LLM-backed":
current_mode = "Service-backed"
if current_mode not in mode_options:
current_mode = mode_options[0]
execution_mode = st.selectbox(
"Execution mode",
mode_options,
index=mode_options.index(current_mode),
key=f"{key}_mode",
)
provider_strategy = st.selectbox(
"Caller provider override",
["global", "ollama", "openai"],
index=["global", "ollama", "openai"].index(config.get("provider_strategy", "global")),
key=f"{key}_provider",
)
model_override = st.text_input("Caller model override", value=config.get("model_override", ""), key=f"{key}_model")
agent_timeout = st.number_input(
"Service call timeout seconds",
min_value=10,
max_value=600,
value=int(config.get("timeout_seconds", 150)),
step=10,
key=f"{key}_timeout",
)
st.markdown("Default directives")
st.markdown(
f"<div class='agent-directives'>{agent_definition.default_directives}</div>",
unsafe_allow_html=True,
)
directives = st.text_area("Additional directives", value=config.get("directives", ""), key=f"{key}_directives")
updated = dict(config)
updated.update(
{
"execution_mode": execution_mode,
"provider_strategy": provider_strategy,
"model_override": model_override,
"timeout_seconds": int(agent_timeout),
"directives": directives,
}
)
updated_configs.append(updated)
st.session_state.agent_configs = updated_configs
run_state = st.session_state.get("run_state", {})
stage_traces = run_state.get("stage_traces", [])
orchestration_decision = run_state.get("orchestration_decision", {})
with tab_flow:
col_static, col_runtime = st.columns(2)
with col_static:
st.subheader("Workflow structure")
st.caption("Static LangGraph routing, including the orchestrator's conditional backtracking paths.")
st.graphviz_chart(build_workflow_dot(), use_container_width=True)
with col_runtime:
st.subheader("Latest run path")
st.caption("Highlighted edges show the path taken by the current run state.")
st.graphviz_chart(build_workflow_dot(stage_traces, orchestration_decision), use_container_width=True)
st.code(runtime_path_summary(stage_traces, orchestration_decision), language="text")
if orchestration_decision:
st.json(
{
"decision": orchestration_decision.get("decision"),
"target_agent": orchestration_decision.get("target_agent"),
"reason": orchestration_decision.get("reason"),
"feedback": orchestration_decision.get("feedback"),
}
)
with tab_runtime:
export = st.session_state.get("run_export")
status = export.get("run_metadata", {}).get("status") if export else st.session_state.get("run_status", "not run")
st.metric("Current run status", status)
if st.session_state.get("current_run_id"):
st.caption(f"Run ID: {st.session_state.current_run_id}")
st.metric("Iteration count", run_state.get("iterations", 0))
st.dataframe(agent_status_rows(stage_traces), use_container_width=True)
st.caption(runtime_path_summary(stage_traces, orchestration_decision))
st.json(stage_traces)
with tab_memory:
memory = run_state.get("working_memory", {"shared": {}, "timeline": []})
st.metric("Current run status", st.session_state.get("run_status", "not run"))
if st.session_state.get("current_run_id"):
st.caption(f"Run ID: {st.session_state.current_run_id}")
st.subheader("Shared")
st.json(memory.get("shared", {}))
st.subheader("Timeline")
st.json(memory.get("timeline", []))
with tab_results:
st.subheader("Requirements")
st.dataframe(run_state.get("requirements", []), use_container_width=True)
st.subheader("Test cases")
st.dataframe(run_state.get("test_designs", []), use_container_width=True)
st.subheader("Review")
st.json(run_state.get("review", {}))
st.subheader("Orchestration decision")
st.json(orchestration_decision)
st.subheader("Final JSON preview")
st.json(st.session_state.get("run_export") or {})
with tab_downloads:
export = st.session_state.get("run_export")
if export:
basename = download_basename(export)
st.download_button(
"Download run_export.json",
data=json.dumps(export, indent=2, ensure_ascii=False),
file_name=f"{basename}_run_export.json",
mime="application/json",
)
st.download_button(
"Download stage_traces.jsonl",
data=st.session_state.get("stage_traces_jsonl", ""),
file_name=f"{basename}_stage_traces.jsonl",
mime="application/x-ndjson",
)
else:
st.info("Run the workflow to generate downloads.")
|