Add onboarding option picker tool

This commit is contained in:
rafaelj
2026-05-21 14:44:57 -04:00
parent df3e2c3265
commit 90006cb729
33 changed files with 894 additions and 3 deletions
+13
View File
@@ -26,6 +26,7 @@ use crate::tasks::UserShellCommandTask;
use crate::tasks::execute_user_shell_command;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::option_picker::OptionPickerResponse;
use codex_protocol::protocol::CodexErrorInfo;
use codex_protocol::protocol::ErrorEvent;
use codex_protocol::protocol::Event;
@@ -435,6 +436,14 @@ pub async fn request_user_input_response(
sess.notify_user_input_response(&id, response).await;
}
pub async fn option_picker_response(
sess: &Arc<Session>,
id: String,
response: OptionPickerResponse,
) {
sess.notify_option_picker_response(&id, response).await;
}
pub async fn request_permissions_response(
sess: &Arc<Session>,
id: String,
@@ -792,6 +801,10 @@ pub(super) async fn submission_loop(
request_user_input_response(&sess, id, response).await;
false
}
Op::OptionPickerResponse { id, response } => {
option_picker_response(&sess, id, response).await;
false
}
Op::RequestPermissionsResponse { id, response } => {
request_permissions_response(&sess, id, response).await;
false
+75
View File
@@ -325,6 +325,8 @@ use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig;
use codex_protocol::option_picker::OptionPickerArgs;
use codex_protocol::option_picker::OptionPickerResponse;
use codex_protocol::protocol::ApplyPatchApprovalRequestEvent;
use codex_protocol::protocol::AskForApproval;
use codex_protocol::protocol::CodexErrorInfo;
@@ -343,6 +345,7 @@ use codex_protocol::protocol::ModelVerificationEvent;
use codex_protocol::protocol::NetworkApprovalContext;
use codex_protocol::protocol::NonSteerableTurnKind;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::OptionPickerEvent;
use codex_protocol::protocol::RateLimitSnapshot;
use codex_protocol::protocol::RequestUserInputEvent;
use codex_protocol::protocol::ReviewDecision;
@@ -2319,6 +2322,78 @@ impl Session {
rx_response.await.ok()
}
#[expect(
clippy::await_holding_invalid_type,
reason = "active turn checks and turn state updates must remain atomic"
)]
pub async fn request_option_picker(
&self,
turn_context: &TurnContext,
call_id: String,
args: OptionPickerArgs,
) -> Option<OptionPickerResponse> {
let sub_id = turn_context.sub_id.clone();
let (tx_response, rx_response) = oneshot::channel();
let event_id = sub_id.clone();
let prev_entry = {
let mut active = self.active_turn.lock().await;
match active.as_mut() {
Some(at) => {
let mut ts = at.turn_state.lock().await;
ts.insert_pending_option_picker(sub_id, tx_response)
}
None => None,
}
};
if prev_entry.is_some() {
warn!("Overwriting existing pending option picker for sub_id: {event_id}");
}
let event = EventMsg::OptionPicker(OptionPickerEvent {
call_id,
turn_id: turn_context.sub_id.clone(),
question: args.question,
options: args.options,
allow_multiple: args.allow_multiple,
submit_label: args.submit_label,
skip_label: args.skip_label,
});
turn_context
.turn_metadata_state
.mark_user_input_requested_during_turn();
self.send_event(turn_context, event).await;
rx_response.await.ok()
}
#[expect(
clippy::await_holding_invalid_type,
reason = "active turn checks and turn state updates must remain atomic"
)]
pub async fn notify_option_picker_response(
&self,
sub_id: &str,
response: OptionPickerResponse,
) {
let entry = {
let mut active = self.active_turn.lock().await;
match active.as_mut() {
Some(at) => {
let mut ts = at.turn_state.lock().await;
ts.remove_pending_option_picker(sub_id)
}
None => None,
}
};
match entry {
Some(tx_response) => {
tx_response.send(response).ok();
}
None => {
warn!("No pending option picker found for sub_id: {sub_id}");
}
}
}
#[expect(
clippy::await_holding_invalid_type,
reason = "active turn checks and turn state updates must remain atomic"
+1
View File
@@ -1362,6 +1362,7 @@ pub(super) fn realtime_text_for_event(msg: &EventMsg) -> Option<String> {
| EventMsg::ExecApprovalRequest(_)
| EventMsg::RequestPermissions(_)
| EventMsg::RequestUserInput(_)
| EventMsg::OptionPicker(_)
| EventMsg::DynamicToolCallRequest(_)
| EventMsg::DynamicToolCallResponse(_)
| EventMsg::GuardianAssessment(_)
+18
View File
@@ -23,6 +23,7 @@ use crate::session::TurnInputQueue;
use crate::session::turn_context::TurnContext;
use crate::tasks::AnySessionTask;
use codex_protocol::models::AdditionalPermissionProfile;
use codex_protocol::option_picker::OptionPickerResponse;
use codex_protocol::protocol::ReviewDecision;
use codex_protocol::protocol::TokenUsage;
@@ -113,6 +114,7 @@ pub(crate) struct TurnState {
pending_approvals: HashMap<String, oneshot::Sender<ReviewDecision>>,
pending_request_permissions: HashMap<String, PendingRequestPermissions>,
pending_user_input: HashMap<String, oneshot::Sender<RequestUserInputResponse>>,
pending_option_picker: HashMap<String, oneshot::Sender<OptionPickerResponse>>,
pending_elicitations: HashMap<(String, RequestId), oneshot::Sender<ElicitationResponse>>,
pending_dynamic_tools: HashMap<String, oneshot::Sender<DynamicToolResponse>>,
pub(crate) pending_input: TurnInputQueue,
@@ -150,6 +152,7 @@ impl TurnState {
self.pending_approvals.clear();
self.pending_request_permissions.clear();
self.pending_user_input.clear();
self.pending_option_picker.clear();
self.pending_elicitations.clear();
self.pending_dynamic_tools.clear();
}
@@ -185,6 +188,21 @@ impl TurnState {
self.pending_user_input.remove(key)
}
pub(crate) fn insert_pending_option_picker(
&mut self,
key: String,
tx: oneshot::Sender<OptionPickerResponse>,
) -> Option<oneshot::Sender<OptionPickerResponse>> {
self.pending_option_picker.insert(key, tx)
}
pub(crate) fn remove_pending_option_picker(
&mut self,
key: &str,
) -> Option<oneshot::Sender<OptionPickerResponse>> {
self.pending_option_picker.remove(key)
}
pub(crate) fn insert_pending_elicitation(
&mut self,
server_name: String,
+2
View File
@@ -15,6 +15,7 @@ pub(crate) mod multi_agents;
pub(crate) mod multi_agents_common;
pub(crate) mod multi_agents_spec;
pub(crate) mod multi_agents_v2;
mod option_picker;
mod plan;
pub(crate) mod plan_spec;
mod request_permissions;
@@ -61,6 +62,7 @@ pub use mcp::McpHandler;
pub use mcp_resource::ListMcpResourceTemplatesHandler;
pub use mcp_resource::ListMcpResourcesHandler;
pub use mcp_resource::ReadMcpResourceHandler;
pub use option_picker::OptionPickerHandler;
pub use plan::PlanHandler;
pub use request_permissions::RequestPermissionsHandler;
pub use request_plugin_install::RequestPluginInstallHandler;
@@ -0,0 +1,77 @@
use crate::function_tool::FunctionCallError;
use crate::tools::context::FunctionToolOutput;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
use crate::tools::context::boxed_tool_output;
use crate::tools::handlers::parse_arguments;
use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::ToolExecutor;
use codex_protocol::option_picker::OptionPickerArgs;
use codex_tools::REQUEST_OPTION_PICKER_TOOL_NAME;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
use codex_tools::create_request_option_picker_tool;
pub struct OptionPickerHandler;
#[async_trait::async_trait]
impl ToolExecutor<ToolInvocation> for OptionPickerHandler {
fn tool_name(&self) -> ToolName {
ToolName::plain(REQUEST_OPTION_PICKER_TOOL_NAME)
}
fn spec(&self) -> ToolSpec {
create_request_option_picker_tool()
}
async fn handle(
&self,
invocation: ToolInvocation,
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
let ToolInvocation {
session,
turn,
call_id,
payload,
..
} = invocation;
let arguments = match payload {
ToolPayload::Function { arguments } => arguments,
_ => {
return Err(FunctionCallError::RespondToModel(format!(
"{REQUEST_OPTION_PICKER_TOOL_NAME} handler received unsupported payload"
)));
}
};
if turn.session_source.is_non_root_agent() {
return Err(FunctionCallError::RespondToModel(
"request_option_picker can only be used by the root thread".to_string(),
));
}
let args: OptionPickerArgs = parse_arguments(&arguments)?;
let response = session
.request_option_picker(turn.as_ref(), call_id, args)
.await
.ok_or_else(|| {
FunctionCallError::RespondToModel(format!(
"{REQUEST_OPTION_PICKER_TOOL_NAME} was cancelled before receiving a response"
))
})?;
let content = serde_json::to_string(&response).map_err(|err| {
FunctionCallError::Fatal(format!(
"failed to serialize {REQUEST_OPTION_PICKER_TOOL_NAME} response: {err}"
))
})?;
Ok(boxed_tool_output(FunctionToolOutput::from_text(
content,
Some(true),
)))
}
}
impl CoreToolRuntime for OptionPickerHandler {}
+4
View File
@@ -13,6 +13,7 @@ use crate::tools::handlers::ListAvailablePluginsToInstallHandler;
use crate::tools::handlers::ListMcpResourceTemplatesHandler;
use crate::tools::handlers::ListMcpResourcesHandler;
use crate::tools::handlers::McpHandler;
use crate::tools::handlers::OptionPickerHandler;
use crate::tools::handlers::PlanHandler;
use crate::tools::handlers::ReadMcpResourceHandler;
use crate::tools::handlers::RequestPermissionsHandler;
@@ -571,6 +572,9 @@ fn add_core_utility_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut
planned_tools.add(RequestUserInputHandler {
available_modes: request_user_input_available_modes(features),
});
if features.enabled(Feature::OnboardingInteractiveTools) {
planned_tools.add(OptionPickerHandler);
}
if features.enabled(Feature::RequestPermissionsTool) {
planned_tools.add(RequestPermissionsHandler);