use std::collections::HashSet;
use std::hash::{DefaultHasher, Hash as _, Hasher as _};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogEntry {
Assistant { created: Option<i64>, text: String },
ToolCall {
created: Option<i64>,
id: String,
name: String,
arguments: String,
},
User { text: String },
ToolResult { name: String, text: String },
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionLog {
pub entries: Vec<LogEntry>,
pub truncated: bool,
}
#[derive(Debug, Default, Deserialize)]
struct RawLine {
#[serde(default)]
created: Option<i64>,
#[serde(default)]
choices: Vec<RawChoice>,
#[serde(default)]
role: Option<String>,
#[serde(default)]
content: Option<Value>,
}
#[derive(Debug, Default, Deserialize)]
struct RawChoice {
#[serde(default)]
delta: RawDelta,
}
#[derive(Debug, Default, Deserialize)]
struct RawDelta {
#[serde(default)]
content: Option<Value>,
#[serde(default)]
tool_calls: Vec<RawToolCall>,
}
#[derive(Debug, Deserialize)]
struct RawToolCall {
#[serde(default)]
id: String,
#[serde(default)]
function: Option<RawFunction>,
}
#[derive(Debug, Deserialize)]
struct RawFunction {
#[serde(default)]
name: String,
#[serde(default)]
arguments: String,
}
fn data_lines(text: &str, truncated: bool) -> Vec<&str> {
let mut lines: Vec<&str> = text
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(str::trim)
.filter(|line| !line.is_empty() && *line != "[DONE]")
.collect();
if truncated {
lines.pop();
}
lines
}
fn content_text(value: Option<&Value>) -> String {
match value {
Some(Value::String(text)) => text.clone(),
Some(Value::Array(parts)) => parts
.iter()
.filter_map(|part| part.get("text").and_then(Value::as_str))
.collect::<Vec<&str>>()
.join(""),
_ => String::new(),
}
}
fn digest(text: &str) -> u64 {
let mut hasher = DefaultHasher::new();
text.hash(&mut hasher);
hasher.finish()
}
fn function_of(call: &RawToolCall) -> (&str, &str) {
match call.function.as_ref() {
Some(function) => (function.name.as_str(), function.arguments.as_str()),
None => ("", ""),
}
}
#[derive(Default)]
struct Reading {
entries: Vec<LogEntry>,
calls: Vec<(String, String)>,
answered: HashSet<u64>,
}
impl Reading {
fn name_of(&self, id: &str) -> String {
self.calls
.iter()
.find(|(known, _)| known == id)
.map(|(_, name)| name.clone())
.unwrap_or_default()
}
fn call(&mut self, created: Option<i64>, call: &RawToolCall) {
let (name, arguments) = function_of(call);
if name.is_empty() || self.calls.iter().any(|(known, _)| known == &call.id) {
return;
}
self.calls.push((call.id.clone(), name.to_owned()));
self.entries.push(LogEntry::ToolCall {
created,
id: call.id.clone(),
name: name.to_owned(),
arguments: arguments.to_owned(),
});
}
fn answer(&mut self, name: String, text: String) {
if text.is_empty() || !self.answered.insert(digest(&text)) {
return;
}
self.entries.push(LogEntry::ToolResult { name, text });
}
fn said(&mut self, created: Option<i64>, text: String) {
if !text.is_empty() {
self.entries.push(LogEntry::Assistant { created, text });
}
}
fn chunk(&mut self, line: &RawLine) {
for choice in &line.choices {
let text = content_text(choice.delta.content.as_ref());
let calls = &choice.delta.tool_calls;
match (calls.first(), text.is_empty()) {
(Some(first), false) => self.answer(self.name_of(&first.id), text),
(Some(_), true) => {
for call in calls {
self.call(line.created, call);
}
}
(None, _) => self.said(line.created, text),
}
}
}
fn line(&mut self, payload: &str) {
let Ok(line) = serde_json::from_str::<RawLine>(payload) else {
return;
};
let text = content_text(line.content.as_ref());
match line.role.as_deref() {
Some("user") if !text.is_empty() => self.entries.push(LogEntry::User { text }),
Some("user") => {}
Some("tool") => self.answer(String::new(), text),
_ => self.chunk(&line),
}
}
}
pub fn parse_log(text: &str, truncated: bool) -> SessionLog {
let mut reading = Reading::default();
for payload in data_lines(text, truncated) {
reading.line(payload);
}
SessionLog {
entries: reading.entries,
truncated,
}
}
#[cfg(test)]
mod tests {
use super::{LogEntry, SessionLog, parse_log};
const FIXTURE: &str = include_str!("../../tests/fixtures/session-log.txt");
fn names(log: &SessionLog) -> Vec<&str> {
log.entries
.iter()
.map(|entry| match entry {
LogEntry::Assistant { .. } => "assistant",
LogEntry::ToolCall { .. } => "tool-call",
LogEntry::User { .. } => "user",
LogEntry::ToolResult { .. } => "tool-result",
})
.collect()
}
#[test]
fn the_known_shapes_become_entries_in_order() {
let log = parse_log(FIXTURE, false);
assert!(!log.truncated);
assert_eq!(
names(&log),
["user", "assistant", "tool-call", "tool-result", "assistant"]
);
assert_eq!(
log.entries[0],
LogEntry::User {
text: "fix the login button".into()
}
);
assert_eq!(
log.entries[1],
LogEntry::Assistant {
created: Some(1_790_000_000),
text: "Looking at the login button.".into()
}
);
}
#[test]
fn a_call_the_stream_repeats_with_its_answer_is_one_row() {
let log = parse_log(FIXTURE, false);
assert_eq!(
log.entries[2],
LogEntry::ToolCall {
created: Some(1_790_000_002),
id: "call_1".into(),
name: "bash".into(),
arguments: r#"{"command":"ls"}"#.into()
}
);
assert_eq!(
log.entries[3],
LogEntry::ToolResult {
name: "bash".into(),
text: "README.md\nsrc\n".into()
},
"the answer chunk and the role:tool line are the same answer"
);
}
#[test]
fn text_parts_are_joined_and_a_second_answer_is_still_shown() {
let log = parse_log(FIXTURE, false);
assert_eq!(
log.entries[4],
LogEntry::Assistant {
created: Some(1_790_000_004),
text: "Done: the first part and the second part.".into()
}
);
}
#[test]
fn an_answer_without_its_call_is_shown_without_a_tool_name() {
let text = "data: {\"role\":\"tool\",\"tool_call_id\":\"call_9\",\"content\":\"done\"}\n";
assert_eq!(
parse_log(text, false).entries,
vec![LogEntry::ToolResult {
name: String::new(),
text: "done".into()
}]
);
}
#[test]
fn unknown_broken_and_empty_lines_are_skipped() {
let text = concat!(
"event: ping\n",
"data: not json\n",
"data: {\"object\":\"something.new\",\"what\":1}\n",
"data: [DONE]\n",
"\n",
"data: {\"role\":\"system\",\"content\":\"\"}\n",
);
assert!(parse_log(text, false).entries.is_empty());
}
#[test]
fn a_truncated_read_drops_its_half_line_and_says_so() {
let text = concat!(
"data: {\"role\":\"user\",\"content\":\"hello\"}\n",
"data: {\"role\":\"user\",\"conte",
);
let log = parse_log(text, true);
assert!(log.truncated);
assert_eq!(names(&log), ["user"]);
assert_eq!(parse_log(text, false).entries.len(), 1, "half line skipped");
}
}