mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
## What changed - Mark `clock.sleep` as `DirectModelOnly` so it remains directly callable in code-mode-only sessions without appearing in the nested code-mode tool surface. - Cover both code mode configurations with a tool-plan test that verifies the sleep tool's exposure and visibility. GitOrigin-RevId: 7ef67bea0678f27fdb17d88004368e5f1d475125
157 lines
5.6 KiB
Rust
157 lines
5.6 KiB
Rust
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_extension_items::ExtensionItem;
|
|
use codex_extension_items::sleep::SleepItem;
|
|
use codex_protocol::items::TurnItem;
|
|
use codex_tools::JsonSchema;
|
|
use codex_tools::ResponsesApiNamespace;
|
|
use codex_tools::ResponsesApiNamespaceTool;
|
|
use codex_tools::ResponsesApiTool;
|
|
use codex_tools::ToolExposure;
|
|
use codex_tools::ToolName;
|
|
use codex_tools::ToolSpec;
|
|
use serde::Deserialize;
|
|
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
use std::time::Instant;
|
|
|
|
const NAMESPACE: &str = "clock";
|
|
const TOOL_NAME: &str = "sleep";
|
|
const MAX_SLEEP_DURATION_MS: u64 = 12 * 60 * 60 * 1000;
|
|
|
|
pub struct SleepHandler;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct SleepArgs {
|
|
duration_ms: u64,
|
|
}
|
|
|
|
fn create_sleep_tool() -> ToolSpec {
|
|
let properties = BTreeMap::from([(
|
|
"duration_ms".to_string(),
|
|
JsonSchema::number(Some(format!(
|
|
"How long to sleep in milliseconds. Must be between 1 and {MAX_SLEEP_DURATION_MS}."
|
|
))),
|
|
)]);
|
|
|
|
ToolSpec::Namespace(ResponsesApiNamespace {
|
|
name: NAMESPACE.to_string(),
|
|
description: "Tools for reading and waiting on time.".to_string(),
|
|
tools: vec![ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
|
name: TOOL_NAME.to_string(),
|
|
description: "Pause execution for a specified duration. The sleep ends early when new input arrives for the active turn. Returns the elapsed wall-clock time."
|
|
.to_string(),
|
|
strict: false,
|
|
defer_loading: None,
|
|
parameters: JsonSchema::object(
|
|
properties,
|
|
Some(vec!["duration_ms".to_string()]),
|
|
/*additional_properties*/ Some(false.into()),
|
|
),
|
|
output_schema: None,
|
|
})],
|
|
})
|
|
}
|
|
|
|
impl ToolExecutor<ToolInvocation> for SleepHandler {
|
|
fn tool_name(&self) -> ToolName {
|
|
ToolName::namespaced(NAMESPACE, TOOL_NAME)
|
|
}
|
|
|
|
fn spec(&self) -> ToolSpec {
|
|
create_sleep_tool()
|
|
}
|
|
|
|
fn exposure(&self) -> ToolExposure {
|
|
ToolExposure::DirectModelOnly
|
|
}
|
|
|
|
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
|
|
Box::pin(async move {
|
|
let ToolInvocation {
|
|
session,
|
|
turn,
|
|
call_id,
|
|
payload,
|
|
..
|
|
} = invocation;
|
|
let ToolPayload::Function { arguments } = payload else {
|
|
return Err(FunctionCallError::RespondToModel(format!(
|
|
"{TOOL_NAME} handler received unsupported payload"
|
|
)));
|
|
};
|
|
let args: SleepArgs = parse_arguments(&arguments)?;
|
|
if !(1..=MAX_SLEEP_DURATION_MS).contains(&args.duration_ms) {
|
|
return Err(FunctionCallError::RespondToModel(format!(
|
|
"duration_ms must be between 1 and {MAX_SLEEP_DURATION_MS}"
|
|
)));
|
|
}
|
|
|
|
let started = Instant::now();
|
|
let item = TurnItem::Extension(ExtensionItem::Sleep(SleepItem {
|
|
id: call_id,
|
|
duration_ms: args.duration_ms,
|
|
}));
|
|
session.emit_turn_item_started(turn.as_ref(), &item).await;
|
|
let turn_state = session
|
|
.input_queue
|
|
.turn_state_for_sub_id(&session.active_turn, &turn.sub_id)
|
|
.await;
|
|
let (mut activity_rx, pending_activity) = session
|
|
.input_queue
|
|
.subscribe_activity(turn_state.as_deref())
|
|
.await;
|
|
let sleep_result: Result<bool, FunctionCallError> = if pending_activity.is_some() {
|
|
Ok(true)
|
|
} else {
|
|
let sleep = session
|
|
.services
|
|
.time_provider
|
|
.sleep(session.thread_id, Duration::from_millis(args.duration_ms));
|
|
tokio::pin!(sleep);
|
|
tokio::select! {
|
|
result = &mut sleep => result
|
|
.map(|()| false)
|
|
.map_err(|err| {
|
|
FunctionCallError::Fatal(format!("failed to sleep: {err:#}"))
|
|
}),
|
|
result = activity_rx.changed() => {
|
|
if result.is_ok() {
|
|
Ok(true)
|
|
} else {
|
|
sleep
|
|
.await
|
|
.map(|()| false)
|
|
.map_err(|err| {
|
|
FunctionCallError::Fatal(format!("failed to sleep: {err:#}"))
|
|
})
|
|
}
|
|
}
|
|
}
|
|
};
|
|
session.emit_turn_item_completed(turn.as_ref(), item).await;
|
|
let interrupted = sleep_result?;
|
|
|
|
let message = if interrupted {
|
|
"Sleep interrupted by new input."
|
|
} else {
|
|
"Sleep completed."
|
|
};
|
|
let wall_time_seconds = started.elapsed().as_secs_f64();
|
|
Ok(boxed_tool_output(FunctionToolOutput::from_text(
|
|
format!("Wall time: {wall_time_seconds:.4} seconds\n{message}"),
|
|
/*success*/ Some(true),
|
|
)))
|
|
})
|
|
}
|
|
}
|
|
|
|
impl CoreToolRuntime for SleepHandler {}
|