-
Notifications
You must be signed in to change notification settings - Fork 15.4k
current time reminders impl for system clock (2/n) #28824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
601a2ce
Inject current-time reminders before sampling
rka-oai 28a79c4
Simplify current-time reminders
rka-oai 994d483
Use current-time reminder naming
rka-oai 1dc4eb8
Update current-time reminder references
rka-oai 7b0fa01
Report current-time provider failures
rka-oai 3b1af39
Use external current time source
rka-oai 5d5fff5
Simplify current time provider wiring
rka-oai 46fa2cd
Simplify current time reminder state
rka-oai ba927fa
Unify current time reminder errors
rka-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use chrono::DateTime; | ||
| use chrono::Utc; | ||
|
|
||
| use super::ContextualUserFragment; | ||
|
|
||
| pub(crate) struct CurrentTimeReminder { | ||
| current_time: DateTime<Utc>, | ||
| } | ||
|
|
||
| impl CurrentTimeReminder { | ||
| pub(crate) fn new(current_time: DateTime<Utc>) -> Self { | ||
| Self { current_time } | ||
| } | ||
| } | ||
|
|
||
| impl ContextualUserFragment for CurrentTimeReminder { | ||
| fn role(&self) -> &'static str { | ||
| "developer" | ||
| } | ||
|
|
||
| fn markers(&self) -> (&'static str, &'static str) { | ||
| Self::type_markers() | ||
| } | ||
|
|
||
| fn type_markers() -> (&'static str, &'static str) { | ||
| ("", "") | ||
| } | ||
|
|
||
| fn body(&self) -> String { | ||
| format!( | ||
| "It is {}.", | ||
| self.current_time.format("%Y-%m-%d %H:%M:%S UTC") | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::Result; | ||
| use anyhow::anyhow; | ||
| use chrono::DateTime; | ||
| use chrono::Utc; | ||
| use codex_features::CurrentTimeSource; | ||
| use codex_protocol::ThreadId; | ||
|
|
||
| use crate::config::CurrentTimeReminderConfig; | ||
|
|
||
| pub type TimeFuture<'a> = Pin<Box<dyn Future<Output = Result<DateTime<Utc>>> + Send + 'a>>; | ||
|
|
||
| /// Host integration boundary for obtaining the current time. | ||
| pub trait TimeProvider: Send + Sync { | ||
| fn current_time(&self, thread_id: ThreadId) -> TimeFuture<'_>; | ||
| } | ||
|
|
||
| pub(crate) struct SystemTimeProvider; | ||
|
|
||
| impl TimeProvider for SystemTimeProvider { | ||
| fn current_time(&self, _thread_id: ThreadId) -> TimeFuture<'_> { | ||
| Box::pin(async { Ok(Utc::now()) }) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn resolve_time_provider( | ||
| config: Option<&CurrentTimeReminderConfig>, | ||
| external_provider: Option<Arc<dyn TimeProvider>>, | ||
| ) -> Result<Arc<dyn TimeProvider>> { | ||
| match config.map(|config| config.clock_source).unwrap_or_default() { | ||
| CurrentTimeSource::System => Ok(Arc::new(SystemTimeProvider)), | ||
| CurrentTimeSource::External => external_provider.ok_or_else(|| { | ||
| anyhow!( | ||
| "features.current_time_reminder.clock_source is external, but no external current-time provider is available" | ||
| ) | ||
| }), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| use codex_protocol::error::CodexErr; | ||
| use codex_protocol::error::Result as CodexResult; | ||
|
|
||
| use super::session::Session; | ||
| use super::turn_context::TurnContext; | ||
| use crate::context::ContextualUserFragment; | ||
|
|
||
| #[derive(Default)] | ||
| pub(crate) struct CurrentTimeReminderState { | ||
| model_requests_since_delivery: u64, | ||
| last_window_id: Option<String>, | ||
| } | ||
|
|
||
| impl CurrentTimeReminderState { | ||
| fn take_reminder_due(&mut self, window_id: &str, interval: u64) -> bool { | ||
| self.model_requests_since_delivery = self.model_requests_since_delivery.saturating_add(1); | ||
| let reminder_is_due = self.last_window_id.as_deref() != Some(window_id) | ||
| || self.model_requests_since_delivery >= interval; | ||
|
|
||
| if reminder_is_due { | ||
| self.model_requests_since_delivery = 0; | ||
| self.last_window_id = Some(window_id.to_string()); | ||
| } | ||
|
|
||
| reminder_is_due | ||
| } | ||
| } | ||
|
|
||
| pub(super) async fn maybe_record_current_time_reminder( | ||
| sess: &Session, | ||
| turn_context: &TurnContext, | ||
| window_id: &str, | ||
| ) -> CodexResult<()> { | ||
| let Some(config) = turn_context.config.current_time_reminder else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let reminder_is_due = { | ||
| let mut state = sess.state.lock().await; | ||
| state | ||
| .current_time_reminder | ||
| .take_reminder_due(window_id, config.reminder_interval_model_requests) | ||
| }; | ||
| if !reminder_is_due { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let current_time = sess | ||
| .services | ||
| .time_provider | ||
| .current_time(sess.thread_id) | ||
| .await | ||
| .map_err(|err| CodexErr::Fatal(format!("failed to read current time: {err:#}")))?; | ||
|
|
||
| let response_item = | ||
| ContextualUserFragment::into(crate::context::CurrentTimeReminder::new(current_time)); | ||
| sess.record_conversation_items(turn_context, std::slice::from_ref(&response_item)) | ||
| .await; | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.