use serde_json::Value;
use crate::canonical::{ContentKind, Delta, Event};
use crate::protocol::json::{nonempty, text_of, to_json_string};
use crate::protocol::synth::{next_index, open_text, open_thinking};
use crate::protocol::{DecodeState, OpenBlock};
pub(super) fn thinking(msg: &Value, state: &mut DecodeState, out: &mut Vec<Event>) {
let Some(t) = nonempty(&msg["thinking"]) else {
return;
};
let index = open_thinking(state, out);
out.push(Event::ContentDelta {
index,
delta: Delta::ThinkingDelta(t.to_owned()),
});
}
pub(super) fn text(msg: &Value, state: &mut DecodeState, out: &mut Vec<Event>) {
let Some(t) = nonempty(&msg["content"]) else {
return;
};
let index = open_text(state, out);
out.push(Event::ContentDelta {
index,
delta: Delta::TextDelta(t.to_owned()),
});
}
pub(super) fn tool_call(call: &Value, state: &mut DecodeState, out: &mut Vec<Event>) {
let index = next_index(state);
let kind = ContentKind::ToolUse {
id: format!("call_{index}"), name: text_of(&call["function"], "name"),
};
state.open.insert(index, OpenBlock { kind: kind.clone() });
out.push(Event::ContentStart { index, kind });
out.push(Event::ContentDelta {
index,
delta: Delta::JsonDelta(to_json_string(&call["function"]["arguments"])),
});
}