Base: codex-main originale (prima del fork aKode)
This commit is contained in:
417
codex-rs/utils/output-truncation/src/lib.rs
Normal file
417
codex-rs/utils/output-truncation/src/lib.rs
Normal file
@@ -0,0 +1,417 @@
|
||||
//! Shared byte/token truncation for tool and exec output.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::models::FunctionCallOutputBody;
|
||||
use codex_protocol::models::FunctionCallOutputContentItem;
|
||||
use codex_protocol::models::FunctionCallOutputPayload;
|
||||
use codex_utils_string::TextTruncation;
|
||||
pub use codex_utils_string::approx_bytes_for_tokens;
|
||||
pub use codex_utils_string::approx_token_count;
|
||||
pub use codex_utils_string::approx_tokens_from_byte_count;
|
||||
use codex_utils_string::plan_token_truncation;
|
||||
use codex_utils_string::truncate_middle_chars;
|
||||
|
||||
pub use codex_protocol::protocol::TruncationPolicy;
|
||||
|
||||
/// Adds the existing 20% allowance for serialization and headers.
|
||||
/// Saved history budgets already include this allowance.
|
||||
pub fn with_serialization_allowance(policy: TruncationPolicy) -> TruncationPolicy {
|
||||
policy * 1.2
|
||||
}
|
||||
|
||||
pub fn formatted_truncate_text(content: &str, policy: TruncationPolicy) -> String {
|
||||
if content.len() <= policy.byte_budget() {
|
||||
return content.to_string();
|
||||
}
|
||||
|
||||
format_truncated_text(content, policy, &mut [])
|
||||
}
|
||||
|
||||
fn format_truncated_text(
|
||||
content: &str,
|
||||
policy: TruncationPolicy,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> String {
|
||||
let original_token_count = approx_token_count(content);
|
||||
let total_lines = content.lines().count();
|
||||
let header = format!(
|
||||
"Warning: truncated output (original token count: {original_token_count})\nTotal output lines: {total_lines}\n\n"
|
||||
);
|
||||
let (snippet, edit) = truncate_text_with_plan(content, policy);
|
||||
if let Some(edit) = edit {
|
||||
for boundary in boundaries {
|
||||
boundary.text_offset = rendered_offset(&edit, boundary.text_offset, header.len());
|
||||
}
|
||||
}
|
||||
header + &snippet
|
||||
}
|
||||
|
||||
pub fn truncate_text(content: &str, policy: TruncationPolicy) -> String {
|
||||
truncate_text_with_plan(content, policy).0
|
||||
}
|
||||
|
||||
// Only token-budget callers supply boundaries that need remapping.
|
||||
fn truncate_text_with_plan(
|
||||
content: &str,
|
||||
policy: TruncationPolicy,
|
||||
) -> (String, Option<TextTruncation>) {
|
||||
match policy {
|
||||
TruncationPolicy::Bytes(bytes) => (truncate_middle_chars(content, bytes), None),
|
||||
TruncationPolicy::Tokens(tokens) => match plan_token_truncation(content, tokens) {
|
||||
Some(edit) => (edit.apply(content), Some(edit)),
|
||||
None => (content.to_owned(), None),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces an oversized serialized MCP result with a bounded text preview while preserving its
|
||||
/// error status and supported bounded metadata.
|
||||
pub fn truncate_mcp_tool_result(
|
||||
result: &CallToolResult,
|
||||
max_bytes: usize,
|
||||
) -> Cow<'_, CallToolResult> {
|
||||
let Ok(serialized) = serde_json::to_string(result) else {
|
||||
return Cow::Borrowed(result);
|
||||
};
|
||||
if serialized.len() <= max_bytes {
|
||||
return Cow::Borrowed(result);
|
||||
}
|
||||
|
||||
let meta = None;
|
||||
|
||||
let mut preview_budget = max_bytes;
|
||||
loop {
|
||||
let preview = if preview_budget == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
truncate_text(&serialized, TruncationPolicy::Bytes(preview_budget))
|
||||
};
|
||||
let truncated = CallToolResult {
|
||||
content: vec![serde_json::json!({
|
||||
"type": "text",
|
||||
"text": preview,
|
||||
})],
|
||||
structured_content: None,
|
||||
is_error: result.is_error,
|
||||
meta: meta.clone(),
|
||||
};
|
||||
let Ok(truncated_serialized) = serde_json::to_string(&truncated) else {
|
||||
return Cow::Borrowed(result);
|
||||
};
|
||||
let truncated_len = truncated_serialized.len();
|
||||
if truncated_len <= max_bytes || preview_budget == 0 {
|
||||
return Cow::Owned(truncated);
|
||||
}
|
||||
|
||||
// The preview is serialized as a JSON string, so escaping can make it exceed its raw byte
|
||||
// budget. Scale the next budget by the observed size of the complete replacement.
|
||||
preview_budget = (preview_budget.saturating_mul(max_bytes) / truncated_len)
|
||||
.min(preview_budget.saturating_sub(1));
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies the existing byte/token budget without changing success metadata or media ordering.
|
||||
pub fn truncate_function_output_payload(
|
||||
output: &mut FunctionCallOutputPayload,
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
) {
|
||||
match &mut output.body {
|
||||
FunctionCallOutputBody::Text(text) => *text = truncate_text(text, policy),
|
||||
FunctionCallOutputBody::ContentItems(items) => {
|
||||
*items = truncate_function_output_items_with_policy(
|
||||
items,
|
||||
policy,
|
||||
estimate_audio_token_count,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An uncharged boundary in the original logical output. Token-boundary
|
||||
/// formatters update the item index and UTF-8 text offset to the rendered
|
||||
/// position, including when content at that position was omitted.
|
||||
///
|
||||
/// At entry, item indices are clamped to the end of the input, text offsets
|
||||
/// round down to an in-bounds UTF-8 boundary, and offsets on non-text items
|
||||
/// are zeroed. Positions preceding an earlier boundary move to that position,
|
||||
/// preserving caller order. On return, boundaries refer to the rendered items;
|
||||
/// an end boundary precedes any generated omission notices.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct OutputBoundary {
|
||||
pub item_index: usize,
|
||||
pub text_offset: usize,
|
||||
}
|
||||
|
||||
/// Format text-only output as a whole observation, including the truncation
|
||||
/// warning. Charge mixed text and audio against the budget in order while
|
||||
/// preserving images.
|
||||
pub fn formatted_truncate_function_output_items_with_policy<'a>(
|
||||
items: impl Into<Cow<'a, [FunctionCallOutputContentItem]>>,
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
formatted_output(items.into(), policy, estimate_audio_token_count, &mut [])
|
||||
}
|
||||
|
||||
/// As above, with zero-width boundaries for callers that have already dropped
|
||||
/// output. Input positions are normalized as documented on [`OutputBoundary`].
|
||||
pub fn formatted_output_at_token_boundaries<'a>(
|
||||
items: impl Into<Cow<'a, [FunctionCallOutputContentItem]>>,
|
||||
max_tokens: usize,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
let items = items.into();
|
||||
normalize_boundaries(&items, boundaries);
|
||||
formatted_output(
|
||||
items,
|
||||
TruncationPolicy::Tokens(max_tokens),
|
||||
estimate_audio_token_count,
|
||||
boundaries,
|
||||
)
|
||||
}
|
||||
|
||||
fn formatted_output(
|
||||
items: Cow<'_, [FunctionCallOutputContentItem]>,
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
let mut combined_len = 0;
|
||||
for item in items.iter() {
|
||||
let FunctionCallOutputContentItem::InputText { text } = item else {
|
||||
return truncate_output_items(items, policy, estimate_audio_token_count, boundaries);
|
||||
};
|
||||
combined_len += usize::from(combined_len != 0) + text.len();
|
||||
}
|
||||
if combined_len <= policy.byte_budget() {
|
||||
return items.into_owned();
|
||||
}
|
||||
|
||||
let mut combined = String::with_capacity(combined_len);
|
||||
let mut next_boundary = 0;
|
||||
for (index, item) in items.iter().enumerate() {
|
||||
if let FunctionCallOutputContentItem::InputText { text } = item {
|
||||
if !combined.is_empty() {
|
||||
combined.push('\n');
|
||||
}
|
||||
while let Some(boundary) = boundaries.get_mut(next_boundary)
|
||||
&& boundary.item_index == index
|
||||
{
|
||||
boundary.item_index = 0;
|
||||
boundary.text_offset += combined.len();
|
||||
next_boundary += 1;
|
||||
}
|
||||
combined.push_str(text);
|
||||
}
|
||||
}
|
||||
// A boundary after the last item belongs at the end of the merged text.
|
||||
for boundary in &mut boundaries[next_boundary..] {
|
||||
boundary.item_index = 0;
|
||||
boundary.text_offset = combined.len();
|
||||
}
|
||||
vec![FunctionCallOutputContentItem::InputText {
|
||||
text: format_truncated_text(&combined, policy, boundaries),
|
||||
}]
|
||||
}
|
||||
|
||||
pub fn truncate_function_output_items_with_policy(
|
||||
items: &[FunctionCallOutputContentItem],
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
truncate_output_items(
|
||||
Cow::Borrowed(items),
|
||||
policy,
|
||||
estimate_audio_token_count,
|
||||
&mut [],
|
||||
)
|
||||
}
|
||||
|
||||
/// Budget in input order even if a caller previously dropped every media item.
|
||||
/// Input positions are normalized as documented on [`OutputBoundary`].
|
||||
pub fn mixed_output_at_token_boundaries<'a>(
|
||||
items: impl Into<Cow<'a, [FunctionCallOutputContentItem]>>,
|
||||
max_tokens: usize,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
let items = items.into();
|
||||
normalize_boundaries(&items, boundaries);
|
||||
truncate_output_items(
|
||||
items,
|
||||
TruncationPolicy::Tokens(max_tokens),
|
||||
estimate_audio_token_count,
|
||||
boundaries,
|
||||
)
|
||||
}
|
||||
|
||||
fn normalize_boundaries(
|
||||
items: &[FunctionCallOutputContentItem],
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) {
|
||||
let mut previous = (0, 0);
|
||||
for boundary in boundaries {
|
||||
let item_index = boundary.item_index.min(items.len());
|
||||
let text_offset = match items.get(item_index) {
|
||||
Some(FunctionCallOutputContentItem::InputText { text }) => {
|
||||
text.floor_char_boundary(boundary.text_offset.min(text.len()))
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
previous = previous.max((item_index, text_offset));
|
||||
(boundary.item_index, boundary.text_offset) = previous;
|
||||
}
|
||||
}
|
||||
|
||||
fn truncate_output_items(
|
||||
items: Cow<'_, [FunctionCallOutputContentItem]>,
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
match items {
|
||||
Cow::Borrowed(items) => truncate_output_iter(
|
||||
items.iter().map(Cow::Borrowed),
|
||||
policy,
|
||||
estimate_audio_token_count,
|
||||
boundaries,
|
||||
),
|
||||
Cow::Owned(items) => truncate_output_iter(
|
||||
items.into_iter().map(Cow::Owned),
|
||||
policy,
|
||||
estimate_audio_token_count,
|
||||
boundaries,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn truncate_output_iter<'a>(
|
||||
items: impl ExactSizeIterator<Item = Cow<'a, FunctionCallOutputContentItem>>,
|
||||
policy: TruncationPolicy,
|
||||
estimate_audio_token_count: impl Fn(&str) -> usize,
|
||||
boundaries: &mut [OutputBoundary],
|
||||
) -> Vec<FunctionCallOutputContentItem> {
|
||||
let mut out: Vec<FunctionCallOutputContentItem> = Vec::with_capacity(items.len());
|
||||
let mut remaining_budget = match policy {
|
||||
TruncationPolicy::Bytes(_) => policy.byte_budget(),
|
||||
TruncationPolicy::Tokens(_) => policy.token_budget(),
|
||||
};
|
||||
let mut omitted_text_items = 0usize;
|
||||
let mut omitted_audio_items = 0usize;
|
||||
let mut next_boundary = 0;
|
||||
|
||||
for (index, item) in items.enumerate() {
|
||||
let item_out = out.len();
|
||||
let item_policy = match policy {
|
||||
TruncationPolicy::Bytes(_) => TruncationPolicy::Bytes(remaining_budget),
|
||||
TruncationPolicy::Tokens(_) => TruncationPolicy::Tokens(remaining_budget),
|
||||
};
|
||||
let mut text_edit = None;
|
||||
let keep = match item.as_ref() {
|
||||
// Empty text contributes no model content but still consumes an API array slot.
|
||||
FunctionCallOutputContentItem::InputText { text } if text.is_empty() => false,
|
||||
FunctionCallOutputContentItem::InputText { .. } if remaining_budget == 0 => {
|
||||
omitted_text_items += 1;
|
||||
false
|
||||
}
|
||||
FunctionCallOutputContentItem::InputText { text } => {
|
||||
let cost = match policy {
|
||||
TruncationPolicy::Bytes(_) => text.len(),
|
||||
TruncationPolicy::Tokens(_) => approx_token_count(text),
|
||||
};
|
||||
if cost <= remaining_budget {
|
||||
remaining_budget = remaining_budget.saturating_sub(cost);
|
||||
true
|
||||
} else {
|
||||
let (snippet, edit) = truncate_text_with_plan(text, item_policy);
|
||||
text_edit = edit;
|
||||
if snippet.is_empty() {
|
||||
omitted_text_items += 1;
|
||||
} else {
|
||||
out.push(FunctionCallOutputContentItem::InputText { text: snippet });
|
||||
}
|
||||
remaining_budget = 0;
|
||||
false
|
||||
}
|
||||
}
|
||||
FunctionCallOutputContentItem::InputImage { .. }
|
||||
| FunctionCallOutputContentItem::EncryptedContent { .. } => true,
|
||||
FunctionCallOutputContentItem::InputAudio { audio_url } => {
|
||||
let token_cost = estimate_audio_token_count(audio_url);
|
||||
let cost = match policy {
|
||||
TruncationPolicy::Bytes(_) => approx_bytes_for_tokens(token_cost),
|
||||
TruncationPolicy::Tokens(_) => token_cost,
|
||||
};
|
||||
if cost <= remaining_budget {
|
||||
remaining_budget = remaining_budget.saturating_sub(cost);
|
||||
true
|
||||
} else {
|
||||
omitted_audio_items += 1;
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
while let Some(boundary) = boundaries.get_mut(next_boundary)
|
||||
&& boundary.item_index == index
|
||||
{
|
||||
boundary.item_index = item_out;
|
||||
if let Some(edit) = &text_edit {
|
||||
boundary.text_offset =
|
||||
rendered_offset(edit, boundary.text_offset, /*header_bytes*/ 0);
|
||||
} else if !keep {
|
||||
boundary.text_offset = 0;
|
||||
}
|
||||
next_boundary += 1;
|
||||
}
|
||||
if keep {
|
||||
out.push(item.into_owned());
|
||||
}
|
||||
}
|
||||
// End boundaries precede generated omission counts: counts are not original
|
||||
// content and must never move a collector notice away from its source.
|
||||
for boundary in &mut boundaries[next_boundary..] {
|
||||
boundary.item_index = out.len();
|
||||
boundary.text_offset = 0;
|
||||
}
|
||||
|
||||
if omitted_text_items > 0 {
|
||||
out.push(FunctionCallOutputContentItem::InputText {
|
||||
text: format!("[omitted {omitted_text_items} text items ...]"),
|
||||
});
|
||||
}
|
||||
if omitted_audio_items > 0 {
|
||||
out.push(FunctionCallOutputContentItem::InputText {
|
||||
text: format!("[omitted {omitted_audio_items} audio items ...]"),
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn rendered_offset(edit: &TextTruncation, boundary: usize, header_bytes: usize) -> usize {
|
||||
if boundary == 0 {
|
||||
0
|
||||
} else if boundary <= edit.removed.start {
|
||||
header_bytes + boundary
|
||||
} else {
|
||||
header_bytes
|
||||
+ edit.removed.start
|
||||
+ edit.marker.len()
|
||||
+ boundary.saturating_sub(edit.removed.end)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn approx_tokens_from_byte_count_i64(bytes: i64) -> i64 {
|
||||
if bytes <= 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let bytes = usize::try_from(bytes).unwrap_or(usize::MAX);
|
||||
i64::try_from(approx_tokens_from_byte_count(bytes)).unwrap_or(i64::MAX)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod truncate_tests;
|
||||
564
codex-rs/utils/output-truncation/src/truncate_tests.rs
Normal file
564
codex-rs/utils/output-truncation/src/truncate_tests.rs
Normal file
@@ -0,0 +1,564 @@
|
||||
use crate::OutputBoundary;
|
||||
use crate::TruncationPolicy;
|
||||
use crate::approx_token_count;
|
||||
use crate::approx_tokens_from_byte_count_i64;
|
||||
use crate::formatted_output_at_token_boundaries;
|
||||
use crate::formatted_truncate_function_output_items_with_policy;
|
||||
use crate::formatted_truncate_text;
|
||||
use crate::mixed_output_at_token_boundaries;
|
||||
use crate::truncate_function_output_items_with_policy;
|
||||
use crate::truncate_mcp_tool_result;
|
||||
use crate::truncate_text;
|
||||
use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
|
||||
use codex_protocol::models::FunctionCallOutputContentItem;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn truncate_mcp_tool_result_borrows_small_result() {
|
||||
let result = CallToolResult {
|
||||
content: vec![serde_json::json!({
|
||||
"type": "text",
|
||||
"text": "hello",
|
||||
})],
|
||||
structured_content: Some(serde_json::json!({"x": 1})),
|
||||
is_error: Some(false),
|
||||
meta: Some(serde_json::json!({"k": "v"})),
|
||||
};
|
||||
|
||||
let truncated = truncate_mcp_tool_result(&result, /*max_bytes*/ 1024);
|
||||
|
||||
assert!(matches!(truncated, std::borrow::Cow::Borrowed(_)));
|
||||
assert_eq!(truncated.as_ref(), &result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_mcp_tool_result_compacts_large_result() {
|
||||
let max_bytes = 1024;
|
||||
let result = CallToolResult {
|
||||
content: vec![serde_json::json!({
|
||||
"type": "text",
|
||||
"text": format!("head\n{}\ntail", "\"\\\n".repeat(5_000)),
|
||||
})],
|
||||
structured_content: Some(serde_json::json!({"x": "y".repeat(5_000)})),
|
||||
is_error: Some(true),
|
||||
meta: Some(serde_json::json!({
|
||||
"private": format!("meta-head{}meta-tail", "z".repeat(5_000)),
|
||||
})),
|
||||
};
|
||||
|
||||
let truncated = truncate_mcp_tool_result(&result, max_bytes).into_owned();
|
||||
|
||||
let preview = truncated.content[0]["text"]
|
||||
.as_str()
|
||||
.expect("truncated result should contain a text preview");
|
||||
assert_eq!(
|
||||
truncated,
|
||||
CallToolResult {
|
||||
content: vec![serde_json::json!({
|
||||
"type": "text",
|
||||
"text": preview,
|
||||
})],
|
||||
structured_content: None,
|
||||
is_error: Some(true),
|
||||
meta: None,
|
||||
}
|
||||
);
|
||||
assert!(serde_json::to_string(&truncated).unwrap().len() <= max_bytes);
|
||||
assert!(preview.contains("head"));
|
||||
assert!(preview.contains("chars truncated"));
|
||||
assert!(preview.contains("meta-tail"));
|
||||
|
||||
let truncated_again = truncate_mcp_tool_result(&truncated, max_bytes);
|
||||
assert!(matches!(truncated_again, std::borrow::Cow::Borrowed(_)));
|
||||
assert_eq!(truncated_again.as_ref(), &truncated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_bytes_less_than_placeholder_returns_placeholder() {
|
||||
let content = "example output";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 4)\nTotal output lines: 1\n\n…13 chars truncated…t",
|
||||
formatted_truncate_text(content, TruncationPolicy::Bytes(1)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_tokens_less_than_placeholder_returns_placeholder() {
|
||||
let content = "example output";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 4)\nTotal output lines: 1\n\nex…3 tokens truncated…ut",
|
||||
formatted_truncate_text(content, TruncationPolicy::Tokens(1)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_tokens_under_limit_returns_original() {
|
||||
let content = "example output";
|
||||
|
||||
assert_eq!(
|
||||
content,
|
||||
formatted_truncate_text(content, TruncationPolicy::Tokens(10)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_bytes_under_limit_returns_original() {
|
||||
let content = "example output";
|
||||
|
||||
assert_eq!(
|
||||
content,
|
||||
formatted_truncate_text(content, TruncationPolicy::Bytes(20)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_tokens_over_limit_returns_truncated() {
|
||||
let content = "this is an example of a long output that should be truncated";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 15)\nTotal output lines: 1\n\nthis is an…10 tokens truncated… truncated",
|
||||
formatted_truncate_text(content, TruncationPolicy::Tokens(5)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_bytes_over_limit_returns_truncated() {
|
||||
let content = "this is an example of a long output that should be truncated";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 15)\nTotal output lines: 1\n\nthis is an exam…30 chars truncated…ld be truncated",
|
||||
formatted_truncate_text(content, TruncationPolicy::Bytes(30)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_bytes_reports_original_line_count_when_truncated() {
|
||||
let content =
|
||||
"this is an example of a long output that should be truncated\nalso some other line";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 21)\nTotal output lines: 2\n\nthis is an exam…51 chars truncated…some other line",
|
||||
formatted_truncate_text(content, TruncationPolicy::Bytes(30)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_tokens_reports_original_line_count_when_truncated() {
|
||||
let content =
|
||||
"this is an example of a long output that should be truncated\nalso some other line";
|
||||
|
||||
assert_eq!(
|
||||
"Warning: truncated output (original token count: 21)\nTotal output lines: 2\n\nthis is an example o…11 tokens truncated…also some other line",
|
||||
formatted_truncate_text(content, TruncationPolicy::Tokens(10)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_middle_bytes_handles_utf8_content() {
|
||||
let s = "😀😀😀😀😀😀😀😀😀😀\nsecond line with text\n";
|
||||
let out = truncate_text(s, TruncationPolicy::Bytes(20));
|
||||
assert_eq!(out, "😀😀…21 chars truncated…with text\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncates_across_multiple_under_limit_texts_and_reports_omitted() {
|
||||
let chunk = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega.\n";
|
||||
let chunk_tokens = approx_token_count(chunk);
|
||||
assert!(chunk_tokens > 0, "chunk must consume tokens");
|
||||
let limit = chunk_tokens * 3;
|
||||
let t1 = chunk.to_string();
|
||||
let t2 = chunk.to_string();
|
||||
let t3 = chunk.repeat(10);
|
||||
let t4 = chunk.to_string();
|
||||
let t5 = chunk.to_string();
|
||||
|
||||
let items = vec![
|
||||
FunctionCallOutputContentItem::InputText { text: t1.clone() },
|
||||
FunctionCallOutputContentItem::InputText { text: t2.clone() },
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: "img:mid".to_string(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputText { text: t3 },
|
||||
FunctionCallOutputContentItem::InputText { text: t4 },
|
||||
FunctionCallOutputContentItem::InputText { text: t5 },
|
||||
];
|
||||
|
||||
let output =
|
||||
truncate_function_output_items_with_policy(&items, TruncationPolicy::Tokens(limit), |_| 0);
|
||||
|
||||
assert_eq!(output.len(), 5);
|
||||
|
||||
let first_text = match &output[0] {
|
||||
FunctionCallOutputContentItem::InputText { text } => text,
|
||||
other => panic!("unexpected first item: {other:?}"),
|
||||
};
|
||||
assert_eq!(first_text, &t1);
|
||||
|
||||
let second_text = match &output[1] {
|
||||
FunctionCallOutputContentItem::InputText { text } => text,
|
||||
other => panic!("unexpected second item: {other:?}"),
|
||||
};
|
||||
assert_eq!(second_text, &t2);
|
||||
|
||||
assert_eq!(
|
||||
output[2],
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: "img:mid".to_string()
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
}
|
||||
);
|
||||
|
||||
let fourth_text = match &output[3] {
|
||||
FunctionCallOutputContentItem::InputText { text } => text,
|
||||
other => panic!("unexpected fourth item: {other:?}"),
|
||||
};
|
||||
assert!(
|
||||
fourth_text.contains("tokens truncated"),
|
||||
"expected marker in truncated snippet: {fourth_text}"
|
||||
);
|
||||
|
||||
let summary_text = match &output[4] {
|
||||
FunctionCallOutputContentItem::InputText { text } => text,
|
||||
other => panic!("unexpected summary item: {other:?}"),
|
||||
};
|
||||
assert!(summary_text.contains("omitted 2 text items"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_function_output_items_with_policy_discards_empty_text() {
|
||||
let mut items = vec![
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: String::new(),
|
||||
};
|
||||
16_384
|
||||
];
|
||||
for policy in [TruncationPolicy::Bytes(0), TruncationPolicy::Tokens(1)] {
|
||||
assert_eq!(
|
||||
truncate_function_output_items_with_policy(&items, policy, |_| 0),
|
||||
Vec::new()
|
||||
);
|
||||
}
|
||||
|
||||
let content = vec![
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: "caption".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: "img:one".to_string(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_123".to_string(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputAudio {
|
||||
audio_url: "audio:one".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::EncryptedContent {
|
||||
encrypted_content: "enc_opaque".to_string(),
|
||||
},
|
||||
];
|
||||
items.extend(content.clone());
|
||||
for policy in [TruncationPolicy::Bytes(16), TruncationPolicy::Tokens(4)] {
|
||||
assert_eq!(
|
||||
truncate_function_output_items_with_policy(&items, policy, |_| 1),
|
||||
content
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_text_output_reuses_owned_items_under_limit() {
|
||||
let items = vec![text("alpha"), text(""), text("beta")];
|
||||
let policy = TruncationPolicy::Bytes(32);
|
||||
|
||||
let borrowed = formatted_truncate_function_output_items_with_policy(&items, policy, |_| 0);
|
||||
assert_eq!(borrowed, items);
|
||||
|
||||
let allocation = items.as_ptr();
|
||||
let owned = formatted_truncate_function_output_items_with_policy(items, policy, |_| 0);
|
||||
assert_eq!(owned, borrowed);
|
||||
assert_eq!(owned.as_ptr(), allocation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_text_output_preserves_empty_leading_text_behavior() {
|
||||
let items = vec![text(""), text("abc")];
|
||||
|
||||
let output = formatted_truncate_function_output_items_with_policy(
|
||||
items,
|
||||
TruncationPolicy::Bytes(0),
|
||||
|_| 0,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![FunctionCallOutputContentItem::InputText {
|
||||
text: "Warning: truncated output (original token count: 1)\nTotal output lines: 1\n\n…3 chars truncated…".to_string(),
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_mixed_output_budgets_text_and_audio_in_order() {
|
||||
let audio = FunctionCallOutputContentItem::InputAudio {
|
||||
audio_url: "audio:one".to_owned(),
|
||||
};
|
||||
let file_image = FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::File {
|
||||
file_id: "file_123".to_owned(),
|
||||
},
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
};
|
||||
let items = vec![
|
||||
text("abcd"),
|
||||
inline_image("img:one".to_owned()),
|
||||
text("efgh"),
|
||||
audio.clone(),
|
||||
file_image.clone(),
|
||||
text("ijkl"),
|
||||
inline_image("img:two".to_owned()),
|
||||
];
|
||||
|
||||
let output = formatted_truncate_function_output_items_with_policy(
|
||||
items,
|
||||
TruncationPolicy::Bytes(12),
|
||||
|_| 1,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![
|
||||
text("abcd"),
|
||||
inline_image("img:one".to_owned()),
|
||||
text("efgh"),
|
||||
audio,
|
||||
file_image,
|
||||
inline_image("img:two".to_owned()),
|
||||
text("[omitted 1 text items ...]"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_mixed_output_preserves_encrypted_content() {
|
||||
let encrypted = FunctionCallOutputContentItem::EncryptedContent {
|
||||
encrypted_content: "enc_opaque".to_owned(),
|
||||
};
|
||||
let items = vec![text("abcdefgh"), encrypted.clone()];
|
||||
|
||||
let output = formatted_truncate_function_output_items_with_policy(
|
||||
items,
|
||||
TruncationPolicy::Bytes(2),
|
||||
|_| 0,
|
||||
);
|
||||
|
||||
assert_eq!(output, vec![text("a…6 chars truncated…h"), encrypted]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_function_output_items_with_policy_omits_audio_over_budget() {
|
||||
let items = vec![
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: "abcdefgh".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::EncryptedContent {
|
||||
encrypted_content: "enc_opaque".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputAudio {
|
||||
audio_url: "audio:one".to_string(),
|
||||
},
|
||||
];
|
||||
|
||||
let output =
|
||||
truncate_function_output_items_with_policy(&items, TruncationPolicy::Bytes(2), |_| 1);
|
||||
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: "a…6 chars truncated…h".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::EncryptedContent {
|
||||
encrypted_content: "enc_opaque".to_string(),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: "[omitted 1 audio items ...]".to_string(),
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_function_output_items_with_policy_charges_audio_against_byte_budget() {
|
||||
let audio = FunctionCallOutputContentItem::InputAudio {
|
||||
audio_url: "audio:one".to_string(),
|
||||
};
|
||||
let items = vec![
|
||||
audio.clone(),
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: "abcdefgh".to_string(),
|
||||
},
|
||||
];
|
||||
|
||||
let output =
|
||||
truncate_function_output_items_with_policy(&items, TruncationPolicy::Bytes(5), |_| 1);
|
||||
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![
|
||||
audio,
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: truncate_text("abcdefgh", TruncationPolicy::Bytes(1)),
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_text_output_merges_all_text_for_token_budget() {
|
||||
let items = vec![text("abcdefgh"), text("ijklmnop")];
|
||||
|
||||
let output = formatted_truncate_function_output_items_with_policy(
|
||||
items,
|
||||
TruncationPolicy::Tokens(2),
|
||||
|_| 0,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![FunctionCallOutputContentItem::InputText {
|
||||
text: "Warning: truncated output (original token count: 5)\nTotal output lines: 2\n\nabcd…3 tokens truncated…mnop".to_string(),
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn byte_count_conversion_clamps_non_positive_values() {
|
||||
assert_eq!(approx_tokens_from_byte_count_i64(/*bytes*/ -1), 0);
|
||||
assert_eq!(approx_tokens_from_byte_count_i64(/*bytes*/ 0), 0);
|
||||
assert_eq!(approx_tokens_from_byte_count_i64(/*bytes*/ 5), 2);
|
||||
}
|
||||
|
||||
fn boundaries<const N: usize>(positions: [(usize, usize); N]) -> [OutputBoundary; N] {
|
||||
positions.map(|(item_index, text_offset)| OutputBoundary {
|
||||
item_index,
|
||||
text_offset,
|
||||
})
|
||||
}
|
||||
|
||||
fn positions<const N: usize>(boundaries: [OutputBoundary; N]) -> [(usize, usize); N] {
|
||||
boundaries.map(|b| (b.item_index, b.text_offset))
|
||||
}
|
||||
|
||||
fn text(s: &str) -> FunctionCallOutputContentItem {
|
||||
FunctionCallOutputContentItem::InputText { text: s.to_owned() }
|
||||
}
|
||||
|
||||
fn inline_image(image_url: String) -> FunctionCallOutputContentItem {
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail: Some(DEFAULT_IMAGE_DETAIL),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boundary_formatters_normalize_input_and_remap_truncated_text() {
|
||||
let audio_cost: fn(&str) -> usize = |_| 1;
|
||||
let items = [text("left-head"), text("right-tail")];
|
||||
let max_tokens = 2;
|
||||
let mut boundary = boundaries([(0, 0), (0, 2), (1, 0), (1, 8), (2, 0)]);
|
||||
let output =
|
||||
formatted_output_at_token_boundaries(&items, max_tokens, audio_cost, &mut boundary);
|
||||
let [FunctionCallOutputContentItem::InputText { text: rendered }] = output.as_slice() else {
|
||||
panic!("expected one merged text item")
|
||||
};
|
||||
let head = rendered.find("left").expect("expected retained prefix");
|
||||
let tail = rendered
|
||||
.strip_suffix("tail")
|
||||
.expect("expected retained suffix")
|
||||
.len();
|
||||
assert_eq!(
|
||||
positions(boundary),
|
||||
[
|
||||
(0, 0),
|
||||
(0, head + 2),
|
||||
(0, tail),
|
||||
(0, tail + 2),
|
||||
(0, rendered.len())
|
||||
]
|
||||
);
|
||||
|
||||
for (middle, middle_end) in [(text("after"), 5), (inline_image("img:one".to_owned()), 0)] {
|
||||
let items = [text("🙂é boundary"), middle, text("last")];
|
||||
for formatter in [
|
||||
formatted_output_at_token_boundaries,
|
||||
mixed_output_at_token_boundaries,
|
||||
] {
|
||||
for tokens in [0, 1, 2, 256] {
|
||||
let mut b = boundaries([(0, 1), (1, usize::MAX), (0, 8), (usize::MAX, usize::MAX)]);
|
||||
let output = formatter(&items, tokens, audio_cost, &mut b);
|
||||
assert_eq!(output, formatter(&items, tokens, audio_cost, &mut []));
|
||||
let result = positions(b);
|
||||
assert!(result.is_sorted());
|
||||
assert!(
|
||||
result
|
||||
.iter()
|
||||
.all(|&(index, offset)| match output.get(index) {
|
||||
Some(FunctionCallOutputContentItem::InputText { text }) =>
|
||||
text.is_char_boundary(offset),
|
||||
_ => index <= output.len() && offset == 0,
|
||||
})
|
||||
);
|
||||
if tokens == 256 {
|
||||
assert_eq!(result, [(0, 0), (1, middle_end), (1, middle_end), (3, 0)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn owned_mixed_output_moves_a_preserved_image_across_text_truncation() {
|
||||
let image_url = "data:image/png;base64,".to_owned() + &"a".repeat(4096);
|
||||
let url_ptr = image_url.as_ptr();
|
||||
let items = vec![
|
||||
text("long prefix"),
|
||||
inline_image(image_url),
|
||||
text("omitted"),
|
||||
];
|
||||
let mut borrowed = boundaries([(0, 5), (2, 3)]);
|
||||
let mut owned = boundaries([(0, 5), (2, 3)]);
|
||||
let max_tokens = 1;
|
||||
let expected = formatted_output_at_token_boundaries(&items, max_tokens, |_| 0, &mut borrowed);
|
||||
let actual = formatted_output_at_token_boundaries(items, max_tokens, |_| 0, &mut owned);
|
||||
assert_eq!(actual, expected);
|
||||
let projected = positions(owned);
|
||||
assert_eq!(projected, positions(borrowed));
|
||||
let [(index, offset), omitted] = projected;
|
||||
let FunctionCallOutputContentItem::InputText { text: snippet } = &actual[index] else {
|
||||
panic!("expected boundary inside truncated text");
|
||||
};
|
||||
assert_eq!((index, &snippet[offset..], omitted), (0, "ix", (2, 0)));
|
||||
let FunctionCallOutputContentItem::InputImage {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} = &actual[1]
|
||||
else {
|
||||
panic!("expected preserved image")
|
||||
};
|
||||
assert_eq!(image_url.as_ptr(), url_ptr);
|
||||
}
|
||||
Reference in New Issue
Block a user