use cpa_common::json as gj;
use crate::antigravity_compaction as capsule;
pub(super) fn summary_payload(source: &[u8]) -> Vec<u8> {
let mut out = capsule::summary_payload(source);
for field in ["tools", "additional_tools"] {
let value = gj::get(source, field);
if value.exists() {
gj::set_raw(&mut out, field, value.raw());
}
}
out
}
pub(super) fn finalize(body: String) -> String {
let mut out = body.into_bytes();
let tools = gj::get(&out, "tools");
if tools.is_array() && !tools.array().is_empty() {
gj::set_raw(&mut out, "tool_choice", br#"{"type":"none"}"#);
} else if let Some(flattened) = flatten_tool_blocks(&out) {
out = flattened;
}
String::from_utf8(out).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
}
fn flatten_tool_blocks(body: &[u8]) -> Option<Vec<u8>> {
let messages = gj::get(body, "messages");
if !messages.is_array() {
return None;
}
let text_block = |text: Vec<u8>| {
let mut block = br#"{"type":"text","text":""}"#.to_vec();
gj::set_str(&mut block, "text", text);
block
};
let mut changed = false;
let rewritten: Vec<Vec<u8>> = messages
.array()
.iter()
.map(|message| {
let content = message.get("content");
if !content.is_array() {
return message.raw().to_vec();
}
let mut flattened = false;
let parts: Vec<Vec<u8>> = content
.array()
.iter()
.map(|part| match &*part.get("type").bytes() {
b"tool_use" => {
flattened = true;
text_block(
[
b"Tool call ".as_slice(),
&part.get("name").bytes(),
b" (",
&part.get("id").bytes(),
b"): ",
part.get("input").raw(),
]
.concat(),
)
}
b"tool_result" => {
flattened = true;
let result = part.get("content");
let result = if result.kind == gj::Kind::String {
result.bytes().into_owned()
} else {
result.raw().to_vec()
};
text_block(
[
b"Tool result ".as_slice(),
&part.get("tool_use_id").bytes(),
b": ",
&result,
]
.concat(),
)
}
_ => part.raw().to_vec(),
})
.collect();
if !flattened {
return message.raw().to_vec();
}
changed = true;
let mut message = message.raw().to_vec();
gj::set_raw(&mut message, "content", gj::join(&parts));
message
})
.collect();
changed.then(|| {
let mut out = body.to_vec();
gj::set_raw(&mut out, "messages", gj::join(&rewritten));
out
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Usage {
input: i64,
output: i64,
total: i64,
cached: i64,
}
impl Usage {
pub(super) fn of(message: &[u8]) -> Self {
let usage = gj::get(message, "usage");
if !usage.exists() {
return Self {
input: 0,
output: 0,
total: 0,
cached: 0,
};
}
let field = |path: &str| usage.get(path).int();
let cached = field("cache_read_input_tokens");
let input = field("input_tokens") + field("cache_creation_input_tokens") + cached;
let output = field("output_tokens");
Self {
input,
output,
total: input + output,
cached,
}
}
fn counts(self) -> (i64, i64, i64) {
(self.input, self.output, self.total)
}
}
pub(super) fn response(model: &str, sealed: &str, usage: Usage) -> Vec<u8> {
let mut out = capsule::response(model, sealed, usage.counts());
gj::set_int(&mut out, "usage.input_tokens_details.cached_tokens", usage.cached);
out
}
pub(super) fn stream_chunks(model: &str, sealed: &str, usage: Usage) -> Vec<Vec<u8>> {
capsule::stream_chunks(model, sealed, usage.counts())
.into_iter()
.map(|chunk| patch_stream_usage(chunk, usage))
.collect()
}
fn patch_stream_usage(chunk: Vec<u8>, usage: Usage) -> Vec<u8> {
const PREFIX: &[u8] = b"data: ";
let Some(at) = chunk.windows(PREFIX.len()).position(|w| w == PREFIX) else {
return chunk;
};
let mut data = chunk[at + PREFIX.len()..].trim_ascii().to_vec();
let path = if gj::get(&data, "response.usage").exists() {
"response.usage"
} else if gj::get(&data, "usage").exists() {
"usage"
} else {
return chunk;
};
gj::set_int(&mut data, &format!("{path}.input_tokens"), usage.input);
gj::set_int(&mut data, &format!("{path}.output_tokens"), usage.output);
gj::set_int(&mut data, &format!("{path}.total_tokens"), usage.total);
gj::set_int(
&mut data,
&format!("{path}.input_tokens_details.cached_tokens"),
usage.cached,
);
[&chunk[..at], PREFIX, &data, b"\n\n"].concat()
}