Skip to main content

to_chat_message

Function to_chat_message 

Source
pub fn to_chat_message(m: &Message) -> Option<ChatMessage>
Expand description

Converts a projected Message into the request-body chat shape (dialect v2). Plain string content passes through unchanged (byte-identical to dialect v1); structured content is translated to the strict openai wire shape — anything else is serialized compactly, deterministic under the frozen serialization (lexicographic keys).

  • role:"tool" {call_id, output, is_error} → {role:"tool", tool_call_id, content: <output as a JSON string>} (the is_error flag stays in the event stream; the wire carries the output only, the shape verified live against the real DeepSeek API).
  • assistant {"text", "tool_calls": [...]} → {role:"assistant", content: <text or null>, tool_calls: [{id, type:"function", function:{name, arguments: <JSON string>}}]}.
  • assistant {"id", "name", "arguments"} (the per-call tool/call transcript duplicate) → dropped: the enclosing assistant turn’s tool_calls already carries the same call id, and strict endpoints reject the duplicate. Returns None.

Public: kian session replay re-derives the model context from the event stream and must rebuild the exact chat shape the loop sent, so the A2 byte-identity check compares like with like. This conversion is part of the frozen request-serialization contract (constraint 3) — changing it invalidates every replay comparison and every provider prefix cache.