Spaces:
Running
Running
Download src/multi_agent_sdlc/workflow/runner.py from pedrorezende/multi-agent-sdlc: direct link, hf CLI and curl.
- Browser
- Download file 7.56 kB
-
https://huggingface.co/spaces/pedrorezende/multi-agent-sdlc/resolve/main/src/multi_agent_sdlc/workflow/runner.py
- Command line
-
hf download hf://spaces/pedrorezende/multi-agent-sdlc/src/multi_agent_sdlc/workflow/runner.py
-
curl -L -o runner.py https://huggingface.co/spaces/pedrorezende/multi-agent-sdlc/resolve/main/src/multi_agent_sdlc/workflow/runner.py
7.56 kB
| import logging | |
| from typing import Any | |
| from langchain_core.runnables import RunnableConfig | |
| from langgraph.types import Command, Interrupt | |
| from pydantic import BaseModel | |
| from multi_agent_sdlc.presentation.terminal_plan_review import ( | |
| collect_plan_review_decision, | |
| ) | |
| from multi_agent_sdlc.presentation.terminal_verification_block_review import ( | |
| collect_verification_block_review, | |
| ) | |
| from .checkpointing import build_workflow_config, create_checkpointer | |
| from .graph import build_graph | |
| from .models import WorkflowTermination | |
| from .run_repository import ( | |
| WorkflowRun, | |
| WorkflowRunStatus, | |
| create_workflow_run, | |
| fail_workflow_run, | |
| finish_workflow_run, | |
| get_workflow_run, | |
| update_workflow_run_status, | |
| ) | |
| from .state import build_initial_state | |
| logger = logging.getLogger(__name__) | |
| def run_new_workflow( | |
| request: str, | |
| configurable: dict[str, Any], | |
| ) -> None: | |
| workflow_run = create_workflow_run(request) | |
| config = build_workflow_config( | |
| workflow_run.thread_id, | |
| configurable, | |
| ) | |
| initial_state = build_initial_state(request) | |
| with create_checkpointer() as checkpointer: | |
| graph = build_graph(checkpointer) | |
| result = _invoke_graph( | |
| graph, | |
| initial_state, | |
| config, | |
| workflow_run, | |
| ) | |
| _process_workflow_result( | |
| graph, | |
| result, | |
| workflow_run, | |
| config, | |
| ) | |
| def resume_workflow( | |
| thread_id: str, | |
| checkpoint_id: str | None, | |
| configurable: dict[str, Any], | |
| ) -> None: | |
| workflow_run = get_workflow_run(thread_id) | |
| if workflow_run is None: | |
| raise ValueError(f"Workflow run not found: {thread_id}") | |
| checkpoint_config = build_workflow_config( | |
| workflow_run.thread_id, | |
| configurable, | |
| checkpoint_id, | |
| ) | |
| thread_config = build_workflow_config( | |
| workflow_run.thread_id, | |
| configurable, | |
| ) | |
| with create_checkpointer() as checkpointer: | |
| graph = build_graph(checkpointer) | |
| snapshot = graph.get_state(checkpoint_config) | |
| if snapshot.interrupts: | |
| interrupt = _get_first_interrupt( | |
| snapshot.interrupts, | |
| ) | |
| result = _resume_interrupt( | |
| graph, | |
| interrupt, | |
| workflow_run, | |
| checkpoint_config, | |
| ) | |
| elif snapshot.next: | |
| update_workflow_run_status( | |
| workflow_run.thread_id, | |
| WorkflowRunStatus.RUNNING, | |
| ) | |
| result = _invoke_graph( | |
| graph, | |
| None, | |
| checkpoint_config, | |
| workflow_run, | |
| ) | |
| else: | |
| raise ValueError(f"Workflow run {workflow_run.thread_id!r} has no pending work.") | |
| _process_workflow_result( | |
| graph, | |
| result, | |
| workflow_run, | |
| thread_config, | |
| ) | |
| def _process_workflow_result( | |
| graph, | |
| result: dict[str, object], | |
| workflow_run: WorkflowRun, | |
| config: RunnableConfig, | |
| ) -> None: | |
| interrupts = _get_interrupts(result) | |
| while interrupts: | |
| result = _resume_interrupt( | |
| graph, | |
| interrupts[0], | |
| workflow_run, | |
| config, | |
| ) | |
| interrupts = _get_interrupts(result) | |
| snapshot = graph.get_state(config) | |
| if snapshot.next: | |
| raise RuntimeError(f"Workflow run {workflow_run.thread_id} stopped with pending nodes: {snapshot.next!r}.") | |
| termination = snapshot.values.get("workflow_termination") | |
| if not isinstance(termination, WorkflowTermination): | |
| error = RuntimeError("Workflow reached END without structured termination information.") | |
| fail_workflow_run( | |
| workflow_run.thread_id, | |
| reason=str(error), | |
| node="finalize_workflow", | |
| error_type=type(error).__name__, | |
| ) | |
| raise error | |
| finish_workflow_run( | |
| workflow_run.thread_id, | |
| termination, | |
| ) | |
| print("\nWorkflow execution finished.") | |
| print(f"Outcome: {termination.outcome.value}") | |
| print(f"Terminal node: {termination.node}") | |
| print(f"Reason: {termination.reason}") | |
| print(f"Thread ID: {workflow_run.thread_id}") | |
| def _resume_interrupt( | |
| graph, | |
| interrupt: Interrupt, | |
| workflow_run: WorkflowRun, | |
| config: RunnableConfig, | |
| ) -> dict[str, object]: | |
| update_workflow_run_status( | |
| workflow_run.thread_id, | |
| WorkflowRunStatus.INTERRUPTED, | |
| ) | |
| response = _collect_interrupt_response(interrupt.value) | |
| update_workflow_run_status( | |
| workflow_run.thread_id, | |
| WorkflowRunStatus.RUNNING, | |
| ) | |
| return _invoke_graph( | |
| graph, | |
| Command( | |
| resume=response.model_dump(mode="json"), | |
| ), | |
| config, | |
| workflow_run, | |
| ) | |
| def _invoke_graph( | |
| graph, | |
| graph_input: object, | |
| config: RunnableConfig, | |
| workflow_run: WorkflowRun, | |
| ) -> dict[str, object]: | |
| try: | |
| return graph.invoke( | |
| graph_input, | |
| config=config, | |
| ) | |
| except Exception as error: | |
| terminal_node = _get_pending_node(graph, config) | |
| try: | |
| fail_workflow_run( | |
| workflow_run.thread_id, | |
| reason=str(error) or type(error).__name__, | |
| node=terminal_node, | |
| error_type=type(error).__name__, | |
| ) | |
| except Exception: | |
| logger.exception( | |
| "Could not persist failure for workflow run %s.", | |
| workflow_run.thread_id, | |
| ) | |
| raise | |
| def _get_pending_node(graph, config: RunnableConfig) -> str: | |
| try: | |
| snapshot = graph.get_state(config) | |
| except Exception: | |
| logger.exception("Could not inspect graph state after execution failure.") | |
| return "unknown" | |
| return snapshot.next[0] if snapshot.next else "unknown" | |
| def _collect_interrupt_response( | |
| interrupt_value: dict[str, Any], | |
| ) -> BaseModel: | |
| interrupt_type = interrupt_value.get("type") | |
| content = interrupt_value.get("content") | |
| if not isinstance(interrupt_type, str): | |
| raise ValueError("Interrupt type is missing or invalid.") | |
| if not isinstance(content, str): | |
| raise ValueError("Interrupt content is missing or invalid.") | |
| print() | |
| print(content) | |
| match interrupt_type: | |
| case "plan_review": | |
| print("\nGraph paused for plan review.") | |
| return collect_plan_review_decision() | |
| case "verification_block_review": | |
| print("\nGraph paused because verification is blocked.") | |
| return collect_verification_block_review() | |
| case _: | |
| raise ValueError(f"Unsupported interrupt type: {interrupt_type!r}") | |
| def _get_interrupts( | |
| result: dict[str, object], | |
| ) -> list[Interrupt]: | |
| value = result.get("__interrupt__", ()) | |
| if not isinstance(value, (list, tuple)): | |
| raise TypeError(f"Expected __interrupt__ to be a list or tuple, got {type(value).__name__}.") | |
| interrupts: list[Interrupt] = [] | |
| for item in value: | |
| if not isinstance(item, Interrupt): | |
| raise TypeError(f"Expected Interrupt, got {type(item).__name__}.") | |
| interrupts.append(item) | |
| return interrupts | |
| def _get_first_interrupt( | |
| interrupts: tuple[Interrupt, ...], | |
| ) -> Interrupt: | |
| interrupt = interrupts[0] | |
| if not isinstance(interrupt, Interrupt): | |
| raise TypeError(f"Expected Interrupt, got {type(interrupt).__name__}.") | |
| return interrupt | |