"""`POST /hooks/t3code`: тред завершился, упал или спрашивает - срочно в мастер.""" from __future__ import annotations from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from beaver_gateway.jobs.scheduler import JobRun QUESTION_HINT = ( "Если ответ следует из задания - ответь сам: t3_answer(thread_id, " "{id вопроса: label}). Не знаешь - спроси Бобра через say своими словами, " "потом t3_answer. Тред стоит, пока не ответят." ) async def t3code_event(run: JobRun) -> None: await run.inject_master(describe(run.payload), urgency="urgent", origin="t3code") def describe(p: dict[str, Any]) -> str: head = ( f"t3code: тред «{p.get('title')}» ({p.get('machine')}, {p.get('project')}, " f"thread_id {p.get('thread_id')})" ) event = p.get("event") if event in ("question", "approval"): lines = [f"{head} - спрашивает."] for q in (p.get("question") or {}).get("questions", []): multi = " (можно несколько)" if q.get("multi_select") else "" lines.append( f"- [{q['id']}] {q.get('header') or ''}: {q['question']}{multi}" ) lines.extend( f" · {o['label']}" + (f" - {o['description']}" if o.get("description") else "") for o in q.get("options", []) ) lines.append(QUESTION_HINT) return "\n".join(lines) if event == "completed": lines = [f"{head} - завершён."] elif event == "interrupted": lines = [f"{head} - прерван."] else: lines = [f"{head} - упал: {p.get('error') or 'без текста ошибки'}."] if p.get("text"): lines.append(f"Ответ треда:\n{p['text']}") if p.get("files"): lines.append("Файлы: " + ", ".join(f["path"] for f in p["files"])) return "\n".join(lines)