narrator.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import annotations
  2. from typing import Any
  3. from .models import ContractNarration
  4. TASK_LABELS = {
  5. "direction": "创作方向",
  6. "pattern-retrieval": "模式检索",
  7. "decode-retrieval": "案例检索",
  8. "external-retrieval": "外部资料检索",
  9. "knowledge-retrieval": "知识检索",
  10. "structure": "整体结构",
  11. "paragraph": "内容段落",
  12. "element-set": "内容元素",
  13. "compare": "候选比较",
  14. "compose": "完整脚本组合",
  15. "candidate-portfolio": "候选方案集",
  16. "root": "最终交付",
  17. }
  18. def task_label(task_kind: str) -> str:
  19. return TASK_LABELS.get(task_kind, task_kind.replace("-", " "))
  20. def narrate_contract(contract: dict[str, Any]) -> ContractNarration:
  21. task_kind = str(contract.get("task_kind") or "task")
  22. label = task_label(task_kind)
  23. objective = _plain(contract.get("objective")) or f"完成{label}"
  24. goals = [str(value) for value in contract.get("goal_ids") or [] if str(value).strip()]
  25. inputs = []
  26. for item in contract.get("input_decision_refs") or []:
  27. if not isinstance(item, dict):
  28. continue
  29. source_kind = task_label(str(item.get("expected_task_kind") or "上游成果"))
  30. inputs.append(f"已接受的{source_kind}")
  31. if contract.get("base_artifact_ref"):
  32. inputs.append("已冻结的基础成果")
  33. if not inputs:
  34. inputs.append("当前已冻结的创作输入")
  35. criteria = [
  36. _plain(item.get("description"))
  37. for item in contract.get("criteria") or []
  38. if isinstance(item, dict) and _plain(item.get("description"))
  39. ]
  40. return ContractNarration(
  41. action=f"创建{label} Task",
  42. reasoning=f"根据任务合同:{objective}",
  43. goals=goals,
  44. inputs=_unique(inputs),
  45. output=label,
  46. criteria=_unique(criteria),
  47. )
  48. def _plain(value: Any) -> str:
  49. return " ".join(str(value or "").split())
  50. def _unique(values: list[str]) -> list[str]:
  51. return list(dict.fromkeys(value for value in values if value))