后台挂起

This commit is contained in:
fengxiangdebaozi committed 2026-06-04 20:00:58 +08:00
1 parent 788118ce3d
commit 19399482e1
21 files changed
+626 -50

No files matched your search

@@ -140,6 +140,7 @@ async def _run_recommend(
deliveries: dict[str, str] = {}
last_leader = ""
message = seed_message
name_by_id = {a.agent_id: a.name for a in agents}
for cycle in range(1, MAX_CYCLES + 1):
await progress.leader_thinking(cycle)
@@ -151,6 +152,7 @@ async def _run_recommend(
)
if turn.final_text:
last_leader = turn.final_text
await progress.add_leader_dialogue(turn.final_text, cycle)
if turn.is_clarification:
return _LoopOutcome("clarification", question=turn.clarification, last_leader_content=last_leader, deliveries=deliveries)
@@ -163,6 +165,7 @@ async def _run_recommend(
await progress.seat_active(agent_id)
seat = await gateway.run_seat(thread_ids=thread_ids, agent_id=agent_id, task=task, model=model)
deliveries[agent_id] = seat.final_text
await progress.add_seat_dialogue(agent_id, name_by_id.get(agent_id, agent_id), seat.final_text, cycle)
await progress.seat_done(agent_id, cycle)
await progress.bump_consensus()
@@ -208,6 +211,7 @@ async def _run_chain(
)
if turn.final_text:
last_leader = turn.final_text
await progress.add_leader_dialogue(turn.final_text, i + 1)
if turn.is_clarification:
return _LoopOutcome("clarification", question=turn.clarification, last_leader_content=last_leader, deliveries=deliveries)
@@ -217,6 +221,7 @@ async def _run_chain(
await progress.seat_active(seat.agent_id)
seat_turn = await gateway.run_seat(thread_ids=thread_ids, agent_id=seat.agent_id, task=task, model=model)
deliveries[seat.agent_id] = seat_turn.final_text
await progress.add_seat_dialogue(seat.agent_id, seat.name, seat_turn.final_text, i + 1)
await progress.seat_done(seat.agent_id, i + 1)
if total > 0:
@@ -233,6 +238,7 @@ async def _run_chain(
)
if closeout.final_text:
last_leader = closeout.final_text
await progress.add_leader_dialogue(closeout.final_text, total + 1)
return _LoopOutcome("consensus", last_leader_content=last_leader, deliveries=deliveries)
@@ -50,6 +50,9 @@ class JobProgressWriter:
self._state: dict[str, str] = {a.agent_id: _PENDING for a in agents}
self._spoke_cycle: dict[str, int] = {}
self._consensus = 0
# 研讨对话记录(leader 说明 + 各席位交付),随每次落库一并写入。
self._dialogues: list[dict[str, Any]] = []
self._dlg_seq = 0
# ── 节点快照 ──────────────────────────────────────────────────────────
@@ -76,6 +79,7 @@ class JobProgressWriter:
"""落库(始终带最新 dispatch_chain + consensus)+ 外发事件,均 best-effort。"""
fields.setdefault("dispatch_chain", self._nodes())
fields.setdefault("consensus_percentage", self._consensus)
fields.setdefault("dialogues", self._dialogues)
if self._store is not None:
try:
await self._store.update_progress(self._job_id, **fields)
@@ -140,3 +144,23 @@ class JobProgressWriter:
async def error(self, message: str) -> None:
await self._persist(status="error", error=message)
# ── 对话记录 ─────────────────────────────────────────────────────────────
def _add(self, role: str, agent_id: str | None, name: str, content: str, cycle: int) -> None:
self._dlg_seq += 1
self._dialogues.append(
{"id": self._dlg_seq, "role": role, "agentId": agent_id, "name": name, "content": content, "cycle": cycle}
)
async def add_leader_dialogue(self, content: str, cycle: int) -> None:
"""总控一轮发言(派活说明 / 共识结论)。空内容跳过。"""
if not (content or "").strip():
return
self._add("leader", None, "总控协调", content, cycle)
await self._persist()
async def add_seat_dialogue(self, agent_id: str, name: str, content: str, cycle: int) -> None:
"""某席位一轮交付。"""
self._add("seat", agent_id, name, content or "", cycle)
await self._persist()
@@ -0,0 +1,46 @@
"""Add dialogues column to roundtable_jobs.
存研讨对话记录(leader 说明 + 各席位交付),使后台作业跑完 / 进行中用户都能看到
真实研讨内容(而非只有流程图进度)。LONGTEXT(对话可超 64 KB),匹配
``PortableLongText`` on ``RoundtableJobRow.dialogues``。
Revision ID: 20260605_01
Revises: 20260604_01
Create Date: 2026-06-05
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
from sqlalchemy.dialects.mysql import LONGTEXT
revision = "20260605_01"
down_revision = "20260604_01"
branch_labels = None
depends_on = None
def _has_table(table_name: str) -> bool:
inspector = inspect(op.get_bind())
return table_name in inspector.get_table_names()
def _has_column(table_name: str, column_name: str) -> bool:
if not _has_table(table_name):
return False
inspector = inspect(op.get_bind())
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
def upgrade() -> None:
if _has_table("roundtable_jobs") and not _has_column("roundtable_jobs", "dialogues"):
col_type = LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
op.add_column("roundtable_jobs", sa.Column("dialogues", col_type, nullable=True))
def downgrade() -> None:
if _has_column("roundtable_jobs", "dialogues"):
op.drop_column("roundtable_jobs", "dialogues")
@@ -55,6 +55,9 @@ class RoundtableJobRow(Base):
orchestration_mode: Mapped[str] = mapped_column(String(16), nullable=False, default="recommend")
# chain 模式来源链条快照:JSON object { id, title }(recommend 模式为 null).
chain: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
# 研讨对话记录:JSON array of { id, role('leader'|'seat'), agentId, name, content, cycle }。
# 让用户跑完 / 进行中都能看到真实研讨内容(leader 说明 + 各席位交付),而非只有流程图。
dialogues: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
# Terminal error detail when status=error.
error: Mapped[str | None] = mapped_column(Text, nullable=True)
# 时间列必须用 BeijingDateTime(项目规范,与 ai_writing_sessions / roundtable_drafts 对齐)。
@@ -55,7 +55,7 @@ class RoundtableJobRepository:
for k in ("created_at", "updated_at"):
if isinstance(d.get(k), datetime):
d[k] = d[k].isoformat()
for k in ("dispatch_chain", "thread_ids", "chain"):
for k in ("dispatch_chain", "thread_ids", "chain", "dialogues"):
d[k] = _loads(d.get(k))
return d
@@ -118,6 +118,7 @@ class RoundtableJobRepository:
pending_clarification=_UNSET,
thread_ids=_UNSET,
coordinator_name=_UNSET,
dialogues=_UNSET,
error=_UNSET,
) -> dict[str, Any] | None:
resolved_user_id = resolve_user_id(user_id, method_name="RoundtableJobRepository.update_progress")
@@ -145,6 +146,8 @@ class RoundtableJobRepository:
row.thread_ids = _dumps(thread_ids)
if coordinator_name is not _UNSET:
row.coordinator_name = coordinator_name
if dialogues is not _UNSET:
row.dialogues = _dumps(dialogues)
if error is not _UNSET:
row.error = error
row.updated_at = datetime.now(UTC)