codex_runtime_history_seed.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Render Hermes' prior transcript as a one-shot seed for a FRESH codex app-server thread.
  2. A codex thread is the model-side continuity store, so a thread that codex hands back via
  3. ``thread/resume`` already knows the conversation. A thread started from scratch does not: a session
  4. that ran on another provider before ``/model`` switched to openai-codex, a session whose stored thread
  5. codex could not resume, or a thread retired mid-session (prompt composition change, wedged client)
  6. would otherwise start blind (#26035, #74712; direction from #26081 by @LeonSGP43).
  7. The seed rides on ``thread/start.developerInstructions`` after the prompt composition: codex inserts
  8. that as the first developer message of every request in the thread, so the cap below bounds a
  9. per-request cost and keeps the most recent turns (the ones the next answer depends on).
  10. """
  11. from __future__ import annotations
  12. from typing import Any, Dict, List
  13. # Tail cap on the rendered history; ~8k tokens, resent by codex on every request of the thread.
  14. MAX_HISTORY_SEED_CHARS = 32_000
  15. _TOOL_RESULT_PREVIEW_CHARS = 400
  16. _HEADER = ("Prior conversation from this Hermes session (the thread you are continuing was started fresh; "
  17. "treat these turns as already having happened):")
  18. def _text_of(content: Any) -> str:
  19. if isinstance(content, str):
  20. return content
  21. if isinstance(content, list):
  22. parts = [p if isinstance(p, str) else p.get("text", "") for p in content if isinstance(p, (str, dict))]
  23. return "\n".join(p for p in parts if p)
  24. return ""
  25. def _render_row(msg: dict[str, Any]) -> str:
  26. role = msg.get("role")
  27. text = _text_of(msg.get("content")).strip()
  28. if role == "user":
  29. return f"[USER]\n{text}" if text else ""
  30. if role == "assistant":
  31. calls = [c.get("function", {}).get("name") for c in msg.get("tool_calls") or [] if isinstance(c, dict)]
  32. lines = [f"[ASSISTANT]\n{text}"] if text else []
  33. if calls:
  34. lines.append("[ASSISTANT called tools: " + ", ".join(c for c in calls if c) + "]")
  35. return "\n".join(lines)
  36. if role == "tool":
  37. if len(text) > _TOOL_RESULT_PREVIEW_CHARS:
  38. text = text[:_TOOL_RESULT_PREVIEW_CHARS] + " …"
  39. return f"[TOOL RESULT]\n{text}" if text else ""
  40. return "" # system rows are the prompt composition, already sent as developerInstructions
  41. def render_history_seed(messages: list[dict[str, Any]] | None) -> str:
  42. """Prior turns as one text block, newest last; empty when there is nothing before the current
  43. user message. The trailing user row is the turn being submitted and is never included."""
  44. rows = list(messages or [])
  45. if rows and rows[-1].get("role") == "user":
  46. rows = rows[:-1]
  47. rendered = [r for r in (_render_row(m) for m in rows if isinstance(m, dict)) if r]
  48. if not rendered:
  49. return ""
  50. body = "\n\n".join(rendered)
  51. if len(body) > MAX_HISTORY_SEED_CHARS:
  52. body = "[… earlier turns omitted …]\n\n" + body[-MAX_HISTORY_SEED_CHARS:]
  53. return f"{_HEADER}\n\n{body}"