Skip to main content

gproxy_protocol/transform/compact/
response.rs

1use crate::{
2    transform::TransformError,
3    wire::{
4        DeclaredFields,
5        openai::{codex as c, compact::ClientCompactResponseBody},
6    },
7};
8
9/// Construct the exact client response envelope consumed by CompactClient.
10/// The caller replaces history with this full array; no native encrypted block
11/// is invented and retained tail items are preserved in their original order.
12pub fn replacement_history(
13    summary: Option<String>,
14    retained_tail: Vec<c::ClientResponseItem>,
15) -> Result<ClientCompactResponseBody, TransformError> {
16    let mut output = Vec::with_capacity(retained_tail.len() + usize::from(summary.is_some()));
17    if let Some(summary) = summary {
18        if summary.trim().is_empty() {
19            return Err(TransformError::invalid_result(
20                "compact.summary",
21                "empty summary",
22            ));
23        }
24        output.push(c::ClientResponseItem::Message(
25            c::ClientMessage::builder(
26                "assistant".into(),
27                vec![c::ContentItem::OutputText(
28                    c::ContentItemOutputText::builder(summary).build(),
29                )],
30            )
31            .build(),
32        ));
33    }
34    output.extend(retained_tail.into_declared());
35    Ok(ClientCompactResponseBody::builder(output).build())
36}