mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Merge 98ea503dbf into sapling-pr-archive-bolinfest
This commit is contained in:
@@ -238,7 +238,8 @@ pub struct ModelInfo {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub context_window: Option<i64>,
|
||||
/// Token threshold for automatic compaction. When omitted, core derives it
|
||||
/// from `context_window` (90%).
|
||||
/// from `context_window` (90%). When provided, core clamps it to 90% of the
|
||||
/// context window when available.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub auto_compact_token_limit: Option<i64>,
|
||||
/// Percentage of the context window considered usable for inputs, after
|
||||
@@ -256,10 +257,16 @@ pub struct ModelInfo {
|
||||
|
||||
impl ModelInfo {
|
||||
pub fn auto_compact_token_limit(&self) -> Option<i64> {
|
||||
self.auto_compact_token_limit.or_else(|| {
|
||||
self.context_window
|
||||
.map(|context_window| (context_window * 9) / 10)
|
||||
})
|
||||
let context_limit = self
|
||||
.context_window
|
||||
.map(|context_window| (context_window * 9) / 10);
|
||||
let config_limit = self.auto_compact_token_limit;
|
||||
if let Some(context_limit) = context_limit {
|
||||
return Some(
|
||||
config_limit.map_or(context_limit, |limit| std::cmp::min(limit, context_limit)),
|
||||
);
|
||||
}
|
||||
config_limit
|
||||
}
|
||||
|
||||
pub fn supports_personality(&self) -> bool {
|
||||
|
||||
@@ -1358,6 +1358,9 @@ impl TokenUsageInfo {
|
||||
if let Some(last) = last {
|
||||
info.append_last_usage(last);
|
||||
}
|
||||
if let Some(model_context_window) = model_context_window {
|
||||
info.model_context_window = Some(model_context_window);
|
||||
}
|
||||
Some(info)
|
||||
}
|
||||
|
||||
@@ -3004,4 +3007,46 @@ mod tests {
|
||||
assert_eq!(value["msg"]["cancelled"][0], "c");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_usage_info_new_or_append_updates_context_window_when_provided() {
|
||||
let initial = Some(TokenUsageInfo {
|
||||
total_token_usage: TokenUsage::default(),
|
||||
last_token_usage: TokenUsage::default(),
|
||||
model_context_window: Some(258_400),
|
||||
});
|
||||
let last = Some(TokenUsage {
|
||||
input_tokens: 10,
|
||||
cached_input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
reasoning_output_tokens: 0,
|
||||
total_tokens: 10,
|
||||
});
|
||||
|
||||
let info = TokenUsageInfo::new_or_append(&initial, &last, Some(128_000))
|
||||
.expect("new_or_append should return info");
|
||||
|
||||
assert_eq!(info.model_context_window, Some(128_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_usage_info_new_or_append_preserves_context_window_when_not_provided() {
|
||||
let initial = Some(TokenUsageInfo {
|
||||
total_token_usage: TokenUsage::default(),
|
||||
last_token_usage: TokenUsage::default(),
|
||||
model_context_window: Some(258_400),
|
||||
});
|
||||
let last = Some(TokenUsage {
|
||||
input_tokens: 10,
|
||||
cached_input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
reasoning_output_tokens: 0,
|
||||
total_tokens: 10,
|
||||
});
|
||||
|
||||
let info = TokenUsageInfo::new_or_append(&initial, &last, None)
|
||||
.expect("new_or_append should return info");
|
||||
|
||||
assert_eq!(info.model_context_window, Some(258_400));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user