mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
90ef94d3b3
In the past, we were treating `input exceeded context window` as a streaming error and retrying on it. Retrying on it has no point because it won't change the behavior. In this PR, we surface the error to the client without retry and also send a token count event to indicate that the context window is full. <img width="650" height="125" alt="image" src="https://github.com/user-attachments/assets/c26b1213-4c27-4bfc-90f4-51a270a3efd5" />
78 lines
2.2 KiB
Rust
78 lines
2.2 KiB
Rust
//! Session-wide mutable state.
|
|
|
|
use codex_protocol::models::ResponseItem;
|
|
|
|
use crate::conversation_history::ConversationHistory;
|
|
use crate::protocol::RateLimitSnapshot;
|
|
use crate::protocol::TokenUsage;
|
|
use crate::protocol::TokenUsageInfo;
|
|
|
|
/// Persistent, session-scoped state previously stored directly on `Session`.
|
|
#[derive(Default)]
|
|
pub(crate) struct SessionState {
|
|
pub(crate) history: ConversationHistory,
|
|
pub(crate) token_info: Option<TokenUsageInfo>,
|
|
pub(crate) latest_rate_limits: Option<RateLimitSnapshot>,
|
|
}
|
|
|
|
impl SessionState {
|
|
/// Create a new session state mirroring previous `State::default()` semantics.
|
|
pub(crate) fn new() -> Self {
|
|
Self {
|
|
history: ConversationHistory::new(),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
// History helpers
|
|
pub(crate) fn record_items<I>(&mut self, items: I)
|
|
where
|
|
I: IntoIterator,
|
|
I::Item: std::ops::Deref<Target = ResponseItem>,
|
|
{
|
|
self.history.record_items(items)
|
|
}
|
|
|
|
pub(crate) fn history_snapshot(&self) -> Vec<ResponseItem> {
|
|
self.history.contents()
|
|
}
|
|
|
|
pub(crate) fn replace_history(&mut self, items: Vec<ResponseItem>) {
|
|
self.history.replace(items);
|
|
}
|
|
|
|
// Token/rate limit helpers
|
|
pub(crate) fn update_token_info_from_usage(
|
|
&mut self,
|
|
usage: &TokenUsage,
|
|
model_context_window: Option<u64>,
|
|
) {
|
|
self.token_info = TokenUsageInfo::new_or_append(
|
|
&self.token_info,
|
|
&Some(usage.clone()),
|
|
model_context_window,
|
|
);
|
|
}
|
|
|
|
pub(crate) fn set_rate_limits(&mut self, snapshot: RateLimitSnapshot) {
|
|
self.latest_rate_limits = Some(snapshot);
|
|
}
|
|
|
|
pub(crate) fn token_info_and_rate_limits(
|
|
&self,
|
|
) -> (Option<TokenUsageInfo>, Option<RateLimitSnapshot>) {
|
|
(self.token_info.clone(), self.latest_rate_limits.clone())
|
|
}
|
|
|
|
pub(crate) fn set_token_usage_full(&mut self, context_window: u64) {
|
|
match &mut self.token_info {
|
|
Some(info) => info.fill_to_context_window(context_window),
|
|
None => {
|
|
self.token_info = Some(TokenUsageInfo::full_context_window(context_window));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pending input/approval moved to TurnState.
|
|
}
|