bulky-car-15738
02/16/2026, 9:46 AMtall-airline-16756
02/16/2026, 9:47 AM# Load the saved timeline (mixed messages + tool calls) in chronological order
saved_timeline = [
{"type": "message", "role": "user", "content": "What's the weather?"},
{"type": "tool_call", "tool": "weather_api", "input": {"loc": "NYC"}, "output": {"temp": 58}},
{"type": "message", "role": "assistant", "content": "It's 58°F in NYC."},
# ...
]
restored_ctx = ChatContext()
for entry in saved_timeline:
if entry["type"] == "message":
restored_ctx.add_message(role=entry["role"], content=entry["content"])
elif entry["type"] == "tool_call":
# Represent tool calls in whatever format your agent expects.
# Example: attach as a special message or a tool record
tool_record = {
"tool": entry["tool"],
"input": entry["input"],
"output": entry["output"],
"meta": entry.get("meta")
}
# Option A: Add as a structured system message (so it remains in chat history)
restored_ctx.add_message(
role="system",
content=f"[tool_call] {tool_record}"
)
# Option B: If your agent has a tool history API, add to that instead:
# restored_agent_tool_history.add(tool_record)
3) Start or update the agent with restored context
• Start a fresh agent with chat_ctx=restored_ctx, or call update_agent() and pass the restored context/tool history.
await session.start(
agent=MyAgent(chat_ctx=restored_ctx),
room=ctx.room
)
Design choices and tips
• How you represent tool calls matters:
◦ If your agent inspects only chat messages, add tool calls as structured system messages (clear tags like [tool_call]) so the assistant can read them.
◦ If your agent supports a separate tool history structure, populate that so tool-call metadata stays distinct from regular chat.
• Preserve metadata: timestamps, tool version, and outputs are useful for correct reasoning and for reproducing or debugging previous tool results.
• Ordering: keep the exact chronological order so the agent can interpret which tool outputs correspond to which user prompts.
• Token management: for large histories, consider summarizing older sections (both messages and tool outputs) while retaining critical tool call details.
If you want, tell me:
• The shape of your saved tool records (an example JSON entry), and
• Whether your agent supports a separate tool-history API or only chat messages,
and I’ll produce a tailored code snippet that matches your implementation.