Skip to content

Commit 0fd7ffa

Browse files
aibrahim-oaiHolovkat
authored andcommitted
Moving token_info to ConversationHistory (#5581)
I want to centralize input processing and management to `ConversationHistory`. This would need `ConversationHistory` to have access to `token_info` (i.e. preventing adding a big input to the history). Besides, it makes more sense to have it on `ConversationHistory` than `state`.
1 parent cdb8f17 commit 0fd7ffa

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

codex-rs/core/src/conversation_history.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
use codex_protocol::models::FunctionCallOutputPayload;
22
use codex_protocol::models::ResponseItem;
3+
use codex_protocol::protocol::TokenUsage;
4+
use codex_protocol::protocol::TokenUsageInfo;
35
use tracing::error;
46

57
/// Transcript of conversation history
68
#[derive(Debug, Clone, Default)]
79
pub(crate) struct ConversationHistory {
810
/// The oldest items are at the beginning of the vector.
911
items: Vec<ResponseItem>,
12+
token_info: Option<TokenUsageInfo>,
1013
}
1114

1215
impl ConversationHistory {
1316
pub(crate) fn new() -> Self {
14-
Self { items: Vec::new() }
17+
Self {
18+
items: Vec::new(),
19+
token_info: TokenUsageInfo::new_or_append(&None, &None, None),
20+
}
21+
}
22+
23+
pub(crate) fn token_info(&self) -> Option<TokenUsageInfo> {
24+
self.token_info.clone()
25+
}
26+
27+
pub(crate) fn set_token_usage_full(&mut self, context_window: i64) {
28+
match &mut self.token_info {
29+
Some(info) => info.fill_to_context_window(context_window),
30+
None => {
31+
self.token_info = Some(TokenUsageInfo::full_context_window(context_window));
32+
}
33+
}
1534
}
1635

1736
/// `items` is ordered from oldest to newest.
@@ -301,6 +320,18 @@ impl ConversationHistory {
301320
self.items.remove(pos);
302321
}
303322
}
323+
324+
pub(crate) fn update_token_info(
325+
&mut self,
326+
usage: &TokenUsage,
327+
model_context_window: Option<i64>,
328+
) {
329+
self.token_info = TokenUsageInfo::new_or_append(
330+
&self.token_info,
331+
&Some(usage.clone()),
332+
model_context_window,
333+
);
334+
}
304335
}
305336

306337
#[inline]

codex-rs/core/src/state/session.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::protocol::TokenUsageInfo;
1212
pub(crate) struct SessionState {
1313
pub(crate) session_configuration: SessionConfiguration,
1414
pub(crate) history: ConversationHistory,
15-
pub(crate) token_info: Option<TokenUsageInfo>,
1615
pub(crate) latest_rate_limits: Option<RateLimitSnapshot>,
1716
}
1817

@@ -22,7 +21,6 @@ impl SessionState {
2221
Self {
2322
session_configuration,
2423
history: ConversationHistory::new(),
25-
token_info: None,
2624
latest_rate_limits: None,
2725
}
2826
}
@@ -54,11 +52,11 @@ impl SessionState {
5452
usage: &TokenUsage,
5553
model_context_window: Option<i64>,
5654
) {
57-
self.token_info = TokenUsageInfo::new_or_append(
58-
&self.token_info,
59-
&Some(usage.clone()),
60-
model_context_window,
61-
);
55+
self.history.update_token_info(usage, model_context_window);
56+
}
57+
58+
pub(crate) fn token_info(&self) -> Option<TokenUsageInfo> {
59+
self.history.token_info()
6260
}
6361

6462
pub(crate) fn set_rate_limits(&mut self, snapshot: RateLimitSnapshot) {
@@ -68,17 +66,10 @@ impl SessionState {
6866
pub(crate) fn token_info_and_rate_limits(
6967
&self,
7068
) -> (Option<TokenUsageInfo>, Option<RateLimitSnapshot>) {
71-
(self.token_info.clone(), self.latest_rate_limits.clone())
69+
(self.token_info(), self.latest_rate_limits.clone())
7270
}
7371

7472
pub(crate) fn set_token_usage_full(&mut self, context_window: i64) {
75-
match &mut self.token_info {
76-
Some(info) => info.fill_to_context_window(context_window),
77-
None => {
78-
self.token_info = Some(TokenUsageInfo::full_context_window(context_window));
79-
}
80-
}
73+
self.history.set_token_usage_full(context_window);
8174
}
82-
83-
// Pending input/approval moved to TurnState.
8475
}

0 commit comments

Comments
 (0)