2025-07-29 11:22:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
use std::sync::LazyLock;
|
|
|
|
|
|
|
|
|
|
use crate::codex::Session;
|
2025-09-24 10:27:35 -07:00
|
|
|
use crate::function_tool::FunctionCallError;
|
2025-07-29 11:22:02 -07:00
|
|
|
use crate::openai_tools::JsonSchema;
|
|
|
|
|
use crate::openai_tools::OpenAiTool;
|
|
|
|
|
use crate::openai_tools::ResponsesApiTool;
|
|
|
|
|
use crate::protocol::Event;
|
|
|
|
|
use crate::protocol::EventMsg;
|
|
|
|
|
|
2025-08-15 12:44:40 -07:00
|
|
|
// Use the canonical plan tool types from the protocol crate to ensure
|
|
|
|
|
// type-identity matches events transported via `codex_protocol`.
|
|
|
|
|
pub use codex_protocol::plan_tool::PlanItemArg;
|
|
|
|
|
pub use codex_protocol::plan_tool::StepStatus;
|
|
|
|
|
pub use codex_protocol::plan_tool::UpdatePlanArgs;
|
2025-07-29 11:22:02 -07:00
|
|
|
|
2025-08-15 12:44:40 -07:00
|
|
|
// Types for the TODO tool arguments matching codex-vscode/todo-mcp/src/main.rs
|
2025-07-29 11:22:02 -07:00
|
|
|
|
|
|
|
|
pub(crate) static PLAN_TOOL: LazyLock<OpenAiTool> = LazyLock::new(|| {
|
|
|
|
|
let mut plan_item_props = BTreeMap::new();
|
2025-08-05 20:44:20 -07:00
|
|
|
plan_item_props.insert("step".to_string(), JsonSchema::String { description: None });
|
|
|
|
|
plan_item_props.insert(
|
|
|
|
|
"status".to_string(),
|
2025-08-13 12:05:13 -07:00
|
|
|
JsonSchema::String {
|
|
|
|
|
description: Some("One of: pending, in_progress, completed".to_string()),
|
|
|
|
|
},
|
2025-08-05 20:44:20 -07:00
|
|
|
);
|
2025-07-29 11:22:02 -07:00
|
|
|
|
|
|
|
|
let plan_items_schema = JsonSchema::Array {
|
2025-08-05 20:44:20 -07:00
|
|
|
description: Some("The list of steps".to_string()),
|
2025-07-29 11:22:02 -07:00
|
|
|
items: Box::new(JsonSchema::Object {
|
|
|
|
|
properties: plan_item_props,
|
2025-08-05 19:27:52 -07:00
|
|
|
required: Some(vec!["step".to_string(), "status".to_string()]),
|
2025-10-02 11:05:51 -06:00
|
|
|
additional_properties: Some(false.into()),
|
2025-07-29 11:22:02 -07:00
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut properties = BTreeMap::new();
|
2025-08-05 20:44:20 -07:00
|
|
|
properties.insert(
|
|
|
|
|
"explanation".to_string(),
|
|
|
|
|
JsonSchema::String { description: None },
|
|
|
|
|
);
|
2025-07-29 11:22:02 -07:00
|
|
|
properties.insert("plan".to_string(), plan_items_schema);
|
|
|
|
|
|
|
|
|
|
OpenAiTool::Function(ResponsesApiTool {
|
2025-08-05 19:27:52 -07:00
|
|
|
name: "update_plan".to_string(),
|
2025-08-13 12:05:13 -07:00
|
|
|
description: r#"Updates the task plan.
|
|
|
|
|
Provide an optional explanation and a list of plan items, each with a step and status.
|
|
|
|
|
At most one step can be in_progress at a time.
|
|
|
|
|
"#
|
|
|
|
|
.to_string(),
|
2025-07-29 11:22:02 -07:00
|
|
|
strict: false,
|
|
|
|
|
parameters: JsonSchema::Object {
|
|
|
|
|
properties,
|
2025-08-05 19:27:52 -07:00
|
|
|
required: Some(vec!["plan".to_string()]),
|
2025-10-02 11:05:51 -06:00
|
|
|
additional_properties: Some(false.into()),
|
2025-07-29 11:22:02 -07:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/// This function doesn't do anything useful. However, it gives the model a structured way to record its plan that clients can read and render.
|
|
|
|
|
/// So it's the _inputs_ to this function that are useful to clients, not the outputs and neither are actually useful for the model other
|
|
|
|
|
/// than forcing it to come up and document a plan (TBD how that affects performance).
|
|
|
|
|
pub(crate) async fn handle_update_plan(
|
|
|
|
|
session: &Session,
|
|
|
|
|
arguments: String,
|
|
|
|
|
sub_id: String,
|
2025-09-24 10:27:35 -07:00
|
|
|
_call_id: String,
|
|
|
|
|
) -> Result<String, FunctionCallError> {
|
|
|
|
|
let args = parse_update_plan_arguments(&arguments)?;
|
|
|
|
|
session
|
|
|
|
|
.send_event(Event {
|
|
|
|
|
id: sub_id.to_string(),
|
|
|
|
|
msg: EventMsg::PlanUpdate(args),
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
Ok("Plan updated".to_string())
|
2025-07-29 11:22:02 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 10:27:35 -07:00
|
|
|
fn parse_update_plan_arguments(arguments: &str) -> Result<UpdatePlanArgs, FunctionCallError> {
|
|
|
|
|
serde_json::from_str::<UpdatePlanArgs>(arguments).map_err(|e| {
|
|
|
|
|
FunctionCallError::RespondToModel(format!("failed to parse function arguments: {e}"))
|
|
|
|
|
})
|
2025-07-29 11:22:02 -07:00
|
|
|
}
|