Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Use codex-utils-template for collaboration mode presets (#15995)

jif-oai37b057f0030a
diff --git a/codex-rs/core/src/models_manager/collaboration_mode_presets.rs b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs
index dceab9f3bd..afb002bce8 100644
--- a/codex-rs/core/src/models_manager/collaboration_mode_presets.rs
+++ b/codex-rs/core/src/models_manager/collaboration_mode_presets.rs
@@ -2,13 +2,19 @@ use codex_protocol::config_types::CollaborationModeMask;
use codex_protocol::config_types::ModeKind;
use codex_protocol::config_types::TUI_VISIBLE_COLLABORATION_MODES;
use codex_protocol::openai_models::ReasoningEffort;
+use codex_utils_template::Template;
+use std::sync::LazyLock;
const COLLABORATION_MODE_PLAN: &str = include_str!("../../templates/collaboration_mode/plan.md");
const COLLABORATION_MODE_DEFAULT: &str =
include_str!("../../templates/collaboration_mode/default.md");
-const KNOWN_MODE_NAMES_PLACEHOLDER: &str = "{{KNOWN_MODE_NAMES}}";
-const REQUEST_USER_INPUT_AVAILABILITY_PLACEHOLDER: &str = "{{REQUEST_USER_INPUT_AVAILABILITY}}";
-const ASKING_QUESTIONS_GUIDANCE_PLACEHOLDER: &str = "{{ASKING_QUESTIONS_GUIDANCE}}";
+const KNOWN_MODE_NAMES_TEMPLATE_KEY: &str = "KNOWN_MODE_NAMES";
+const REQUEST_USER_INPUT_AVAILABILITY_TEMPLATE_KEY: &str = "REQUEST_USER_INPUT_AVAILABILITY";
+const ASKING_QUESTIONS_GUIDANCE_TEMPLATE_KEY: &str = "ASKING_QUESTIONS_GUIDANCE";
+static COLLABORATION_MODE_DEFAULT_TEMPLATE: LazyLock<Template> = LazyLock::new(|| {
+ Template::parse(COLLABORATION_MODE_DEFAULT)
+ .unwrap_or_else(|err| panic!("collaboration mode default template must parse: {err}"))
+});
/// Stores feature flags that control collaboration-mode behavior.
///
@@ -21,7 +27,7 @@ pub struct CollaborationModesConfig {
pub default_mode_request_user_input: bool,
}
-pub(crate) fn builtin_collaboration_mode_presets(
+pub fn builtin_collaboration_mode_presets(
collaboration_modes_config: CollaborationModesConfig,
) -> Vec<CollaborationModeMask> {
vec![plan_preset(), default_preset(collaboration_modes_config)]
@@ -56,16 +62,19 @@ fn default_mode_instructions(collaboration_modes_config: CollaborationModesConfi
let asking_questions_guidance = asking_questions_guidance_message(
collaboration_modes_config.default_mode_request_user_input,
);
- COLLABORATION_MODE_DEFAULT
- .replace(KNOWN_MODE_NAMES_PLACEHOLDER, &known_mode_names)
- .replace(
- REQUEST_USER_INPUT_AVAILABILITY_PLACEHOLDER,
- &request_user_input_availability,
- )
- .replace(
- ASKING_QUESTIONS_GUIDANCE_PLACEHOLDER,
- &asking_questions_guidance,
- )
+ COLLABORATION_MODE_DEFAULT_TEMPLATE
+ .render([
+ (KNOWN_MODE_NAMES_TEMPLATE_KEY, known_mode_names.as_str()),
+ (
+ REQUEST_USER_INPUT_AVAILABILITY_TEMPLATE_KEY,
+ request_user_input_availability.as_str(),
+ ),
+ (
+ ASKING_QUESTIONS_GUIDANCE_TEMPLATE_KEY,
+ asking_questions_guidance.as_str(),
+ ),
+ ])
+ .unwrap_or_else(|err| panic!("collaboration mode default template must render: {err}"))
}
fn format_mode_names(modes: &[ModeKind]) -> String {
diff --git a/codex-rs/core/src/models_manager/collaboration_mode_presets_tests.rs b/codex-rs/core/src/models_manager/collaboration_mode_presets_tests.rs
index b0969f6eba..9b43e3bbd7 100644
--- a/codex-rs/core/src/models_manager/collaboration_mode_presets_tests.rs
+++ b/codex-rs/core/src/models_manager/collaboration_mode_presets_tests.rs
@@ -23,9 +23,9 @@ fn default_mode_instructions_replace_mode_names_placeholder() {
.expect("default preset should include instructions")
.expect("default instructions should be set");
- assert!(!default_instructions.contains(KNOWN_MODE_NAMES_PLACEHOLDER));
- assert!(!default_instructions.contains(REQUEST_USER_INPUT_AVAILABILITY_PLACEHOLDER));
- assert!(!default_instructions.contains(ASKING_QUESTIONS_GUIDANCE_PLACEHOLDER));
+ assert!(!default_instructions.contains("{{KNOWN_MODE_NAMES}}"));
+ assert!(!default_instructions.contains("{{REQUEST_USER_INPUT_AVAILABILITY}}"));
+ assert!(!default_instructions.contains("{{ASKING_QUESTIONS_GUIDANCE}}"));
let known_mode_names = format_mode_names(&TUI_VISIBLE_COLLABORATION_MODES);
let expected_snippet = format!("Known mode names are {known_mode_names}.");
diff --git a/codex-rs/tui_app_server/src/model_catalog.rs b/codex-rs/tui_app_server/src/model_catalog.rs
index 569085bcaa..0025293fe0 100644
--- a/codex-rs/tui_app_server/src/model_catalog.rs
+++ b/codex-rs/tui_app_server/src/model_catalog.rs
@@ -1,19 +1,9 @@
use codex_core::models_manager::collaboration_mode_presets::CollaborationModesConfig;
+use codex_core::models_manager::collaboration_mode_presets::builtin_collaboration_mode_presets;
use codex_protocol::config_types::CollaborationModeMask;
-use codex_protocol::config_types::ModeKind;
-use codex_protocol::config_types::TUI_VISIBLE_COLLABORATION_MODES;
use codex_protocol::openai_models::ModelPreset;
-use codex_protocol::openai_models::ReasoningEffort;
use std::convert::Infallible;
-const COLLABORATION_MODE_PLAN: &str =
- include_str!("../../core/templates/collaboration_mode/plan.md");
-const COLLABORATION_MODE_DEFAULT: &str =
- include_str!("../../core/templates/collaboration_mode/default.md");
-const KNOWN_MODE_NAMES_PLACEHOLDER: &str = "{{KNOWN_MODE_NAMES}}";
-const REQUEST_USER_INPUT_AVAILABILITY_PLACEHOLDER: &str = "{{REQUEST_USER_INPUT_AVAILABILITY}}";
-const ASKING_QUESTIONS_GUIDANCE_PLACEHOLDER: &str = "{{ASKING_QUESTIONS_GUIDANCE}}";
-
#[derive(Debug, Clone)]
pub(crate) struct ModelCatalog {
models: Vec<ModelPreset>,
@@ -40,83 +30,21 @@ impl ModelCatalog {
}
}
-fn builtin_collaboration_mode_presets(
- collaboration_modes_config: CollaborationModesConfig,
-) -> Vec<CollaborationModeMask> {
- vec![plan_preset(), default_preset(collaboration_modes_config)]
-}
-
-fn plan_preset() -> CollaborationModeMask {
- CollaborationModeMask {
- name: ModeKind::Plan.display_name().to_string(),
- mode: Some(ModeKind::Plan),
- model: None,
- reasoning_effort: Some(Some(ReasoningEffort::Medium)),
- developer_instructions: Some(Some(COLLABORATION_MODE_PLAN.to_string())),
- }
-}
-
-fn default_preset(collaboration_modes_config: CollaborationModesConfig) -> CollaborationModeMask {
- CollaborationModeMask {
- name: ModeKind::Default.display_name().to_string(),
- mode: Some(ModeKind::Default),
- model: None,
- reasoning_effort: None,
- developer_instructions: Some(Some(default_mode_instructions(collaboration_modes_config))),
- }
-}
-
-fn default_mode_instructions(collaboration_modes_config: CollaborationModesConfig) -> String {
- let known_mode_names = format_mode_names(&TUI_VISIBLE_COLLABORATION_MODES);
- let request_user_input_availability = request_user_input_availability_message(
- ModeKind::Default,
- collaboration_modes_config.default_mode_request_user_input,
- );
- let asking_questions_guidance = asking_questions_guidance_message(
- collaboration_modes_config.default_mode_request_user_input,
- );
- COLLABORATION_MODE_DEFAULT
- .replace(KNOWN_MODE_NAMES_PLACEHOLDER, &known_mode_names)
- .replace(
- REQUEST_USER_INPUT_AVAILABILITY_PLACEHOLDER,
- &request_user_input_availability,
- )
- .replace(
- ASKING_QUESTIONS_GUIDANCE_PLACEHOLDER,
- &asking_questions_guidance,
- )
-}
-
-fn format_mode_names(modes: &[ModeKind]) -> String {
- let mode_names: Vec<&str> = modes.iter().map(|mode| mode.display_name()).collect();
- match mode_names.as_slice() {
- [] => "none".to_string(),
- [mode_name] => (*mode_name).to_string(),
- [first, second] => format!("{first} and {second}"),
- [..] => mode_names.join(", "),
- }
-}
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use pretty_assertions::assert_eq;
-fn request_user_input_availability_message(
- mode: ModeKind,
- default_mode_request_user_input: bool,
-) -> String {
- let mode_name = mode.display_name();
- if mode.allows_request_user_input()
- || (default_mode_request_user_input && mode == ModeKind::Default)
- {
- format!("The `request_user_input` tool is available in {mode_name} mode.")
- } else {
- format!(
- "The `request_user_input` tool is unavailable in {mode_name} mode. If you call it while in {mode_name} mode, it will return an error."
- )
- }
-}
+ #[test]
+ fn list_collaboration_modes_matches_core_presets() {
+ let collaboration_modes_config = CollaborationModesConfig {
+ default_mode_request_user_input: true,
+ };
+ let catalog = ModelCatalog::new(Vec::new(), collaboration_modes_config);
-fn asking_questions_guidance_message(default_mode_request_user_input: bool) -> String {
- if default_mode_request_user_input {
- "In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, prefer using the `request_user_input` tool rather than writing a multiple choice question as a textual assistant message. Never write a multiple choice question as a textual assistant message.".to_string()
- } else {
- "In Default mode, strongly prefer making reasonable assumptions and executing the user's request rather than stopping to ask questions. If you absolutely must ask a question because the answer cannot be discovered from local context and a reasonable assumption would be risky, ask the user directly with a concise plain-text question. Never write a multiple choice question as a textual assistant message.".to_string()
+ assert_eq!(
+ catalog.list_collaboration_modes(),
+ builtin_collaboration_mode_presets(collaboration_modes_config)
+ );
}
}