codex_event_projector.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """Projects codex app-server ``item/*`` notifications into OpenAI-shaped messages.
  2. userMessage → user; agentMessage → assistant; reasoning → stashed onto the next
  3. assistant entry; commandExecution / fileChange / mcpToolCall / dynamicToolCall /
  4. webSearch → assistant tool_call + tool result; anything else → opaque assistant note.
  5. Each item yields AT MOST one assistant + one tool entry (message-alternation
  6. invariant). ``is_tool_iteration`` ticks once per completed tool-shaped item.
  7. """
  8. from __future__ import annotations
  9. import hashlib
  10. import json
  11. from dataclasses import dataclass, field
  12. from typing import Any, Callable, Optional
  13. def _deterministic_call_id(item_type: str, item_id: str) -> str:
  14. """Stable tool_call id: the codex item id, else a hash so replay keeps prefix caches valid."""
  15. return f"codex_{item_type}_{item_id or hashlib.sha256(f'{item_type}'.encode()).hexdigest()[:16]}"
  16. def _format_tool_args(d: dict) -> str:
  17. """Format a dict as JSON the way Hermes' existing tool_calls path does."""
  18. return json.dumps(d, ensure_ascii=False, sort_keys=True)
  19. def _dict_args(raw: Any) -> dict:
  20. args = raw or {}
  21. return args if isinstance(args, dict) else {"arguments": args}
  22. @dataclass
  23. class ProjectionResult:
  24. """Output of projecting one Codex item; empty ``messages`` = ignored (e.g. a streaming delta)."""
  25. messages: list[dict] = field(default_factory=list)
  26. is_tool_iteration: bool = False
  27. final_text: Optional[str] = None # Set when an agentMessage completes
  28. class CodexEventProjector:
  29. """Stateful projector; stashes codex's separate reasoning items onto the next assistant message."""
  30. def __init__(self) -> None:
  31. self._pending_reasoning: list[str] = []
  32. def project(self, notification: dict) -> ProjectionResult:
  33. """Project one notification; only ``item/completed`` materializes messages (deltas are display-only)."""
  34. if notification.get("method", "") != "item/completed":
  35. return ProjectionResult()
  36. item = (notification.get("params", {}) or {}).get("item") or {}
  37. item_type = item.get("type") or ""
  38. item_id = item.get("id") or ""
  39. if item_type == "agentMessage":
  40. return self._project_agent_message(item)
  41. if item_type == "reasoning":
  42. self._pending_reasoning.extend(item.get("summary") or [])
  43. self._pending_reasoning.extend(item.get("content") or [])
  44. return ProjectionResult()
  45. if item_type == "userMessage":
  46. return self._project_user_message(item)
  47. tool_projection = self._TOOL_PROJECTIONS.get(item_type)
  48. if tool_projection is not None:
  49. return self._project_tool_item(item, item_id, tool_projection)
  50. # Unknown / rare items (plan, hookPrompt, ...): opaque note, no fabricated tool_call structure.
  51. return self._project_opaque(item, item_type)
  52. def _assistant_message(self, content: Optional[str], **extra: Any) -> dict[str, Any]:
  53. msg: dict[str, Any] = {"role": "assistant", "content": content, **extra}
  54. if self._pending_reasoning:
  55. msg["reasoning"] = "\n".join(self._pending_reasoning)
  56. self._pending_reasoning = []
  57. return msg
  58. def _project_agent_message(self, item: dict) -> ProjectionResult:
  59. text = item.get("text") or ""
  60. return ProjectionResult(messages=[self._assistant_message(text)], final_text=text)
  61. @staticmethod
  62. def _project_user_message(item: dict) -> ProjectionResult:
  63. # userMessage content is a list of UserInput variants; flatten text
  64. # fragments and drop non-text parts (Hermes' messages store text only).
  65. text_parts = [
  66. (fragment.get("text") or "") if fragment.get("type") == "text" else str(fragment["text"])
  67. for fragment in item.get("content") or []
  68. if isinstance(fragment, dict) and (fragment.get("type") == "text" or "text" in fragment)
  69. ]
  70. return ProjectionResult(messages=[{"role": "user", "content": "\n".join(text_parts)}])
  71. def _project_tool_item(
  72. self, item: dict, item_id: str, spec: Callable[[dict], tuple[str, str, dict, str]]
  73. ) -> ProjectionResult:
  74. """Emit the (assistant tool_call, tool result) pair; ``spec(item)`` -> ``(id_type, name, args, content)``."""
  75. id_type, name, args, content = spec(item)
  76. call_id = _deterministic_call_id(id_type, item_id)
  77. assistant_msg = self._assistant_message(
  78. None,
  79. tool_calls=[{"id": call_id, "type": "function", "function": {"name": name, "arguments": _format_tool_args(args)}}],
  80. )
  81. tool_msg = {"role": "tool", "tool_call_id": call_id, "content": content}
  82. return ProjectionResult(messages=[assistant_msg, tool_msg], is_tool_iteration=True)
  83. @staticmethod
  84. def _command_spec(item: dict) -> tuple[str, str, dict, str]:
  85. args = {"command": item.get("command") or "", "cwd": item.get("cwd") or ""}
  86. output = item.get("aggregatedOutput") or ""
  87. exit_code = item.get("exitCode")
  88. if exit_code is not None:
  89. # Preserve executor status so successful marker-like output is not an interrupt.
  90. output = json.dumps({"exit_code": exit_code, "output": output}, ensure_ascii=False)
  91. # Unknown exit status keeps the legacy text shape and conservative replay handling.
  92. return "exec", "exec_command", args, output
  93. @staticmethod
  94. def _file_change_spec(item: dict) -> tuple[str, str, dict, str]:
  95. # Per-file change kinds only — full file contents can be huge.
  96. changes_summary = [
  97. {"kind": (change.get("kind") or {}).get("type") or "update", "path": change.get("path") or ""}
  98. for change in item.get("changes") or []
  99. ]
  100. status = item.get("status") or "unknown"
  101. content = f"apply_patch status={status}, {len(changes_summary)} change(s)"
  102. return "apply_patch", "apply_patch", {"changes": changes_summary}, content
  103. @staticmethod
  104. def _mcp_tool_call_spec(item: dict) -> tuple[str, str, dict, str]:
  105. server = item.get("server") or "mcp"
  106. tool = item.get("tool") or "unknown"
  107. result, error = item.get("result"), item.get("error")
  108. if error:
  109. content = f"[error] {json.dumps(error, ensure_ascii=False)[:1000]}"
  110. else:
  111. content = json.dumps(result, ensure_ascii=False)[:4000] if result is not None else ""
  112. # Call id mirrors the native MCP name convention (mcp__server__tool).
  113. return f"mcp__{server}__{tool}", f"mcp.{server}.{tool}", _dict_args(item.get("arguments")), content
  114. @staticmethod
  115. def _dynamic_tool_call_spec(item: dict) -> tuple[str, str, dict, str]:
  116. tool = item.get("tool") or "unknown"
  117. content_items = item.get("contentItems") or []
  118. content = (
  119. json.dumps(content_items, ensure_ascii=False)[:4000] if isinstance(content_items, list) and content_items
  120. else f"success={item.get('success')}"
  121. )
  122. return f"dyn_{tool}", tool, _dict_args(item.get("arguments")), content
  123. @staticmethod
  124. def _web_search_spec(item: dict) -> tuple[str, str, dict, str]:
  125. # Codex ran the search itself; the result names it so a later non-Codex
  126. # turn does not read this as a Hermes web_search call.
  127. result = {"provider": "codex"}
  128. if item.get("status"):
  129. result["status"] = item["status"]
  130. return "web_search", "web_search", {"query": item.get("query") or ""}, json.dumps(result, ensure_ascii=False)
  131. _TOOL_PROJECTIONS: dict[str, Callable[[dict], tuple[str, str, dict, str]]] = {
  132. "commandExecution": _command_spec,
  133. "fileChange": _file_change_spec,
  134. "mcpToolCall": _mcp_tool_call_spec,
  135. "dynamicToolCall": _dynamic_tool_call_spec,
  136. "webSearch": _web_search_spec,
  137. }
  138. @staticmethod
  139. def _project_opaque(item: dict, item_type: str) -> ProjectionResult:
  140. try:
  141. payload = json.dumps(item, ensure_ascii=False)[:1500]
  142. except (TypeError, ValueError):
  143. payload = repr(item)[:1500]
  144. return ProjectionResult(messages=[{"role": "assistant", "content": f"[codex {item_type}] {payload}"}])