| 64 | |
| 65 | |
| 66 | class WorkflowProgress: |
| 67 | def __init__( |
| 68 | self, |
| 69 | phases_meta: Optional[list[dict]] = None, |
| 70 | *, |
| 71 | on_change: Callable[["WorkflowProgress"], None] | None = None, |
| 72 | ) -> None: |
| 73 | # Seed declared phases (from meta.phases) so the tree shows them before |
| 74 | # any agent runs; phase() started later by title reuses the same record. |
| 75 | self._phases: list[PhaseRecord] = [ |
| 76 | PhaseRecord(title=p.get("title", ""), detail=p.get("detail")) |
| 77 | for p in (phases_meta or []) |
| 78 | if isinstance(p, dict) and p.get("title") |
| 79 | ] |
| 80 | self._logs: list[str] = [] |
| 81 | self._current_phase: Optional[str] = None |
| 82 | self._on_change = on_change |
| 83 | |
| 84 | # ── mutations ────────────────────────────────────────────────────────── |
| 85 | def start_phase(self, title: str) -> None: |
| 86 | self._current_phase = title |
| 87 | if not any(p.title == title for p in self._phases): |
| 88 | self._phases.append(PhaseRecord(title=title)) |
| 89 | self._changed() |
| 90 | |
| 91 | def log(self, message: str) -> None: |
| 92 | self._logs.append(message) |
| 93 | self._changed() |
| 94 | |
| 95 | def agent_started( |
| 96 | self, |
| 97 | index: int, |
| 98 | label: str, |
| 99 | phase: Optional[str], |
| 100 | key: str = "", |
| 101 | agent_type: str = "", |
| 102 | ) -> AgentRecord: |
| 103 | phase = phase or self._current_phase |
| 104 | record = AgentRecord( |
| 105 | index=index, label=label, phase=phase, key=key, |
| 106 | agent_type=agent_type, started_at=time.monotonic(), |
| 107 | ) |
| 108 | self._phase_for(phase).agents.append(record) |
| 109 | self._changed() |
| 110 | return record |
| 111 | |
| 112 | def agent_finished( |
| 113 | self, |
| 114 | record: AgentRecord, |
| 115 | *, |
| 116 | status: AgentStatus, |
| 117 | tokens: int = 0, |
| 118 | error: Optional[str] = None, |
| 119 | tool_count: int = 0, |
| 120 | ) -> None: |
| 121 | record.status = status |
| 122 | record.tokens = tokens |
| 123 | record.error = error |
no outgoing calls