| 147 | |
| 148 | |
| 149 | def _load_final_payload(output_dir: Path) -> dict[str, Any] | None: |
| 150 | result_json_path = output_dir / "result.json" |
| 151 | if result_json_path.exists(): |
| 152 | raw = result_json_path.read_text() |
| 153 | if raw.strip(): |
| 154 | return json.loads(raw) |
| 155 | |
| 156 | events_path = output_dir / "events.ndjson" |
| 157 | if not events_path.exists(): |
| 158 | return None |
| 159 | |
| 160 | final_done: dict[str, Any] | None = None |
| 161 | for line in events_path.read_text().splitlines(): |
| 162 | line = line.strip() |
| 163 | if not line: |
| 164 | continue |
| 165 | try: |
| 166 | event = json.loads(line) |
| 167 | except json.JSONDecodeError: |
| 168 | continue |
| 169 | if event.get("type") == "done": |
| 170 | final_done = event |
| 171 | |
| 172 | if final_done is None: |
| 173 | return None |
| 174 | |
| 175 | payload = { |
| 176 | "session_id": final_done.get("session_id"), |
| 177 | "provider": final_done.get("provider"), |
| 178 | "model": final_done.get("model"), |
| 179 | "text": final_done.get("text", ""), |
| 180 | "usage": final_done.get("usage") or {}, |
| 181 | } |
| 182 | result_json_path.write_text(json.dumps(payload, indent=2) + "\n") |
| 183 | return payload |
| 184 | |
| 185 | |
| 186 | class JcodeHarborAgent(BaseAgent): |