mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
## What changed - Add `codex-agent-extension` with an `AgentRunner` that starts a resolved agent prompt in a thread forked from its parent. - Propagate the invocation's trace context, select the configured execution environments, submit the initial prompt, and return the spawned thread and turn identifiers. - Reject empty agent prompts and report when the owning thread manager is no longer available. ## Testing - Add an integration test that verifies the agent runs in a forked thread, returns the started turn identifier, completes the turn, and sends the resolved prompt to the model. GitOrigin-RevId: ffb805efabadf758969d6611a6867db6fa9f059a
105 lines
3.2 KiB
Rust
105 lines
3.2 KiB
Rust
use codex_core::CodexThread;
|
|
use codex_core::NewThread;
|
|
use codex_core::StartThreadOptions;
|
|
use codex_core::ThreadManager;
|
|
use codex_core::config::Config;
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::error::CodexErr;
|
|
use codex_protocol::error::Result as CodexResult;
|
|
use codex_protocol::protocol::InitialHistory;
|
|
use codex_protocol::protocol::W3cTraceContext;
|
|
use codex_protocol::user_input::UserInput;
|
|
use std::sync::Arc;
|
|
use std::sync::Weak;
|
|
|
|
/// A fully resolved agent invocation.
|
|
///
|
|
/// Agent discovery owns rendering `prompt`, including any selected skill
|
|
/// references. The runtime only starts that prompt in isolated forked context.
|
|
pub struct AgentInvocation {
|
|
pub config: Config,
|
|
pub prompt: String,
|
|
pub parent_trace: Option<W3cTraceContext>,
|
|
}
|
|
|
|
/// A spawned agent whose initial turn has been submitted.
|
|
pub struct AgentRun {
|
|
pub thread_id: ThreadId,
|
|
pub turn_id: String,
|
|
pub thread: Arc<CodexThread>,
|
|
}
|
|
|
|
/// Runs resolved agents in threads forked by the owning [`ThreadManager`].
|
|
#[derive(Clone)]
|
|
pub struct AgentRunner {
|
|
thread_manager: Weak<ThreadManager>,
|
|
}
|
|
|
|
impl AgentRunner {
|
|
pub fn new(thread_manager: Weak<ThreadManager>) -> Self {
|
|
Self { thread_manager }
|
|
}
|
|
|
|
/// Starts a resolved agent in a fork of `parent_thread_id`.
|
|
pub async fn start(
|
|
&self,
|
|
parent_thread_id: ThreadId,
|
|
invocation: AgentInvocation,
|
|
) -> CodexResult<AgentRun> {
|
|
let AgentInvocation {
|
|
config,
|
|
prompt,
|
|
parent_trace,
|
|
} = invocation;
|
|
if prompt.trim().is_empty() {
|
|
return Err(CodexErr::InvalidRequest(
|
|
"agent prompt must not be empty".to_string(),
|
|
));
|
|
}
|
|
|
|
let thread_manager = self
|
|
.thread_manager
|
|
.upgrade()
|
|
.ok_or_else(|| CodexErr::UnsupportedOperation("thread manager dropped".to_string()))?;
|
|
let environments =
|
|
thread_manager.default_environment_selections(&config.cwd, &config.workspace_roots);
|
|
let NewThread {
|
|
thread_id, thread, ..
|
|
} = thread_manager
|
|
.spawn_subagent(
|
|
parent_thread_id,
|
|
StartThreadOptions {
|
|
config,
|
|
allow_provider_model_fallback: false,
|
|
initial_history: InitialHistory::New,
|
|
history_mode: None,
|
|
session_source: None,
|
|
thread_source: None,
|
|
dynamic_tools: Vec::new(),
|
|
metrics_service_name: None,
|
|
parent_trace: parent_trace.clone(),
|
|
environments,
|
|
thread_extension_init: Default::default(),
|
|
supports_openai_form_elicitation: false,
|
|
},
|
|
)
|
|
.await?;
|
|
let turn_id = thread
|
|
.submit_with_trace(
|
|
vec![UserInput::Text {
|
|
text: prompt,
|
|
text_elements: Vec::new(),
|
|
}]
|
|
.into(),
|
|
parent_trace,
|
|
)
|
|
.await?;
|
|
|
|
Ok(AgentRun {
|
|
thread_id,
|
|
turn_id,
|
|
thread,
|
|
})
|
|
}
|
|
}
|