I discovered that I accidentally introduced a change in
https://github.com/openai/codex/pull/829 where we load a fresh `Config`
in the middle of `codex.rs`:
c3e10e180a/codex-rs/core/src/codex.rs (L515-L522)
This is not good because the `Config` could differ from the one that has
the user's overrides specified from the CLI. Also, in unit tests, it
means the `Config` was picking up my personal settings as opposed to
using a vanilla config, which was problematic.
This PR cleans things up by moving the common case where
`Op::ConfigureSession` is derived from `Config` (originally done in
`codex_wrapper.rs`) and making it the standard way to initialize `Codex`
by putting it in `Codex::spawn()`. Note this also eliminates quite a bit
of boilerplate from the tests and relieves the caller of the
responsibility of minting out unique IDs when invoking `submit()`.
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
use std::sync::Arc;
|
|
|
|
use crate::Codex;
|
|
use crate::config::Config;
|
|
use crate::protocol::Event;
|
|
use crate::protocol::EventMsg;
|
|
use crate::util::notify_on_sigint;
|
|
use tokio::sync::Notify;
|
|
|
|
/// Spawn a new [`Codex`] and initialize the session.
|
|
///
|
|
/// Returns the wrapped [`Codex`] **and** the `SessionInitialized` event that
|
|
/// is received as a response to the initial `ConfigureSession` submission so
|
|
/// that callers can surface the information to the UI.
|
|
pub async fn init_codex(config: Config) -> anyhow::Result<(Codex, Event, Arc<Notify>)> {
|
|
let ctrl_c = notify_on_sigint();
|
|
let (codex, init_id) = Codex::spawn(config, ctrl_c.clone()).await?;
|
|
|
|
// The first event must be `SessionInitialized`. Validate and forward it to
|
|
// the caller so that they can display it in the conversation history.
|
|
let event = codex.next_event().await?;
|
|
if event.id != init_id
|
|
|| !matches!(
|
|
&event,
|
|
Event {
|
|
id: _id,
|
|
msg: EventMsg::SessionConfigured { .. },
|
|
}
|
|
)
|
|
{
|
|
return Err(anyhow::anyhow!(
|
|
"expected SessionInitialized but got {event:?}"
|
|
));
|
|
}
|
|
|
|
Ok((codex, event, ctrl_c))
|
|
}
|