mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## What changed - Show selectable safer follow-ups alongside safety-block explanations, with an option to enable Daybreak when disabled. - Submit the selected rewrite in a verified fork before the blocked turn, preserving the original chat's draft and current model settings. Mark the new turn with `safety_recourse_follow_up`. - Filter suggestions that could invoke commands, skills, apps, or plugins, and suppress recourse for unsupported servers and non-primary threads. ## Testing Extend coverage for fork history, draft preservation, model selection, turn metadata, suggestion filtering, and the Daybreak picker. GitOrigin-RevId: 49052df6301b6bfbba07262d40b007dcf97bb7c9
469 lines
15 KiB
Rust
469 lines
15 KiB
Rust
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
|
|
use super::ChatWidget;
|
|
use crate::app_event::AppEvent;
|
|
use crate::bottom_pane::SelectionItem;
|
|
use crate::bottom_pane::SelectionViewParams;
|
|
use crate::bottom_pane::SkillsToggleItem;
|
|
use crate::bottom_pane::SkillsToggleView;
|
|
use crate::bottom_pane::popup_consts::standard_popup_hint_line;
|
|
use crate::mention_codec::is_common_env_var;
|
|
use crate::mention_codec::is_mention_name_char;
|
|
use crate::mention_codec::parse_linked_tool_mention;
|
|
use crate::skills_helpers::skill_description;
|
|
use crate::skills_helpers::skill_display_name;
|
|
use codex_app_server_protocol::SkillMetadata;
|
|
use codex_app_server_protocol::SkillsListEntry;
|
|
use codex_app_server_protocol::SkillsListResponse;
|
|
use codex_connectors::AppInfo;
|
|
use codex_protocol::parse_command::ParsedCommand;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use codex_utils_path_uri::LegacyAppPathString;
|
|
use codex_utils_path_uri::PathUri;
|
|
use codex_utils_plugins::mention_syntax::PLUGIN_TEXT_MENTION_SIGIL;
|
|
use codex_utils_plugins::mention_syntax::TOOL_MENTION_SIGIL;
|
|
|
|
impl ChatWidget {
|
|
pub(crate) fn open_skills_list(&mut self) {
|
|
self.insert_str("$");
|
|
}
|
|
|
|
pub(crate) fn open_skills_menu(&mut self) {
|
|
let items = vec![
|
|
SelectionItem {
|
|
name: "List skills".to_string(),
|
|
description: Some("Tip: press $ to open this list directly".to_string()),
|
|
actions: vec![Box::new(|tx| {
|
|
tx.send(AppEvent::OpenSkillsList);
|
|
})],
|
|
dismiss_on_select: true,
|
|
..Default::default()
|
|
},
|
|
SelectionItem {
|
|
name: "Enable/Disable Skills".to_string(),
|
|
description: Some("Enable or disable skills".to_string()),
|
|
actions: vec![Box::new(|tx| {
|
|
tx.send(AppEvent::OpenManageSkillsPopup);
|
|
})],
|
|
dismiss_on_select: true,
|
|
..Default::default()
|
|
},
|
|
];
|
|
|
|
self.bottom_pane.show_selection_view(SelectionViewParams {
|
|
title: Some("Skills".to_string()),
|
|
subtitle: Some("Choose an action".to_string()),
|
|
footer_hint: Some(standard_popup_hint_line()),
|
|
items,
|
|
..SelectionViewParams::picker()
|
|
});
|
|
}
|
|
|
|
pub(crate) fn open_manage_skills_popup(&mut self) {
|
|
if self.skills_all.is_empty() {
|
|
self.add_info_message("No skills available.".to_string(), /*hint*/ None);
|
|
return;
|
|
}
|
|
|
|
let mut initial_state = HashMap::new();
|
|
let mut items = Vec::with_capacity(self.skills_all.len());
|
|
for skill in &self.skills_all {
|
|
let Some(path) = skill.path.to_inferred_path_uri() else {
|
|
tracing::warn!(path = %skill.path, "skill has no absolute path for configuration");
|
|
continue;
|
|
};
|
|
initial_state.insert(path.clone(), skill.enabled);
|
|
items.push(SkillsToggleItem {
|
|
name: skill_display_name(skill),
|
|
skill_name: skill.name.clone(),
|
|
description: skill_description(skill).to_string(),
|
|
enabled: skill.enabled,
|
|
path,
|
|
});
|
|
}
|
|
self.skills_initial_state = Some(initial_state);
|
|
|
|
let view = SkillsToggleView::new(
|
|
items,
|
|
self.app_event_tx.clone(),
|
|
self.bottom_pane.list_keymap(),
|
|
);
|
|
self.bottom_pane.show_view(Box::new(view));
|
|
}
|
|
|
|
pub(crate) fn update_skill_enabled(&mut self, path: PathUri, enabled: bool) {
|
|
for skill in &mut self.skills_all {
|
|
if skill.path.to_inferred_path_uri().as_ref() == Some(&path) {
|
|
skill.enabled = enabled;
|
|
}
|
|
}
|
|
self.set_skills(Some(enabled_skills_for_mentions(&self.skills_all)));
|
|
}
|
|
|
|
pub(crate) fn handle_manage_skills_closed(&mut self) {
|
|
self.refresh_skills_for_current_cwd(/*force_reload*/ true);
|
|
let Some(initial_state) = self.skills_initial_state.take() else {
|
|
return;
|
|
};
|
|
let mut current_state = HashMap::new();
|
|
for skill in &self.skills_all {
|
|
if let Some(path) = skill.path.to_inferred_path_uri() {
|
|
current_state.insert(path, skill.enabled);
|
|
}
|
|
}
|
|
|
|
let mut enabled_count = 0;
|
|
let mut disabled_count = 0;
|
|
for (path, was_enabled) in initial_state {
|
|
let Some(is_enabled) = current_state.get(&path) else {
|
|
continue;
|
|
};
|
|
if was_enabled != *is_enabled {
|
|
if *is_enabled {
|
|
enabled_count += 1;
|
|
} else {
|
|
disabled_count += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if enabled_count == 0 && disabled_count == 0 {
|
|
return;
|
|
}
|
|
let enabled_suffix = if enabled_count == 1 { "" } else { "s" };
|
|
let disabled_suffix = if disabled_count == 1 { "" } else { "s" };
|
|
let summary = match (enabled_count, disabled_count) {
|
|
(0, _) => format!("Disabled {disabled_count} skill{disabled_suffix}"),
|
|
(_, 0) => format!("Enabled {enabled_count} skill{enabled_suffix}"),
|
|
_ => format!(
|
|
"Enabled {enabled_count} skill{enabled_suffix}, disabled {disabled_count} skill{disabled_suffix}"
|
|
),
|
|
};
|
|
self.add_info_message(summary, /*hint*/ None);
|
|
}
|
|
|
|
pub(crate) fn set_skills_from_response(&mut self, response: &SkillsListResponse) {
|
|
let skills = skills_for_cwd(&self.config.cwd, &response.data);
|
|
self.skills_all = skills;
|
|
self.set_skills(Some(enabled_skills_for_mentions(&self.skills_all)));
|
|
}
|
|
|
|
pub(crate) fn annotate_skill_reads_in_parsed_cmd(
|
|
&self,
|
|
mut parsed_cmd: Vec<ParsedCommand>,
|
|
) -> Vec<ParsedCommand> {
|
|
if self.skills_all.is_empty() {
|
|
return parsed_cmd;
|
|
}
|
|
|
|
for parsed in &mut parsed_cmd {
|
|
let ParsedCommand::Read { name, path, .. } = parsed else {
|
|
continue;
|
|
};
|
|
if name != "SKILL.md" {
|
|
continue;
|
|
}
|
|
|
|
// Best effort only: annotate exact SKILL.md path matches from the loaded skills list.
|
|
if let Some(path) = LegacyAppPathString::from_path(path).to_inferred_path_uri()
|
|
&& let Some(skill) = self
|
|
.skills_all
|
|
.iter()
|
|
.find(|skill| skill.path.to_inferred_path_uri().as_ref() == Some(&path))
|
|
{
|
|
*name = format!("{name} ({} skill)", skill.name);
|
|
}
|
|
}
|
|
|
|
parsed_cmd
|
|
}
|
|
}
|
|
|
|
fn skills_for_cwd(cwd: &AbsolutePathBuf, skills_entries: &[SkillsListEntry]) -> Vec<SkillMetadata> {
|
|
skills_entries
|
|
.iter()
|
|
.find(|entry| entry.cwd.as_path() == cwd.as_path())
|
|
.map(|entry| entry.skills.clone())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn enabled_skills_for_mentions(skills: &[SkillMetadata]) -> Vec<SkillMetadata> {
|
|
skills
|
|
.iter()
|
|
.filter(|skill| skill.enabled)
|
|
.cloned()
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn collect_tool_mentions(
|
|
text: &str,
|
|
mention_paths: &HashMap<String, String>,
|
|
) -> ToolMentions {
|
|
let mut mentions = extract_tool_mentions_from_text(text);
|
|
for (name, path) in mention_paths {
|
|
if mentions.names.contains(name) {
|
|
mentions.linked_paths.insert(name.clone(), path.clone());
|
|
}
|
|
}
|
|
mentions
|
|
}
|
|
|
|
/// Generated follow-ups must not activate a skill, app, or plugin through their text.
|
|
pub(super) fn contains_tool_mention(text: &str) -> bool {
|
|
let mentions = extract_tool_mentions_from_text(text);
|
|
let plugins = extract_tool_mentions_from_text_with_sigil(text, PLUGIN_TEXT_MENTION_SIGIL);
|
|
!mentions.names.is_empty()
|
|
|| !mentions.linked_paths.is_empty()
|
|
|| !plugins.linked_paths.is_empty()
|
|
}
|
|
|
|
pub(crate) fn find_skill_mentions_with_tool_mentions(
|
|
mentions: &ToolMentions,
|
|
skills: &[SkillMetadata],
|
|
) -> Vec<SkillMetadata> {
|
|
if mentions.names.is_empty() && mentions.linked_paths.is_empty() {
|
|
return Vec::new();
|
|
}
|
|
|
|
let mention_skill_paths: HashSet<PathUri> = mentions
|
|
.linked_paths
|
|
.values()
|
|
.filter(|path| is_skill_path(path))
|
|
.filter_map(|path| {
|
|
LegacyAppPathString::from_string(normalize_skill_path(path)).to_inferred_path_uri()
|
|
})
|
|
.collect();
|
|
|
|
// Share parsed identities between passes while keeping linked paths ahead of plain names.
|
|
let parsed_skills = skills
|
|
.iter()
|
|
.filter_map(|skill| {
|
|
let path = skill.path.to_inferred_path_uri()?;
|
|
Some((skill, path))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut seen_names = HashSet::new();
|
|
let mut seen_paths = HashSet::new();
|
|
let mut matches: Vec<SkillMetadata> = Vec::new();
|
|
|
|
for (skill, path) in &parsed_skills {
|
|
if seen_paths.contains(path) {
|
|
continue;
|
|
}
|
|
if mention_skill_paths.contains(path) {
|
|
seen_paths.insert(path.clone());
|
|
seen_names.insert(skill.name.clone());
|
|
matches.push((*skill).clone());
|
|
}
|
|
}
|
|
|
|
for (skill, path) in parsed_skills {
|
|
if seen_paths.contains(&path) {
|
|
continue;
|
|
}
|
|
if mentions.names.contains(&skill.name) && seen_names.insert(skill.name.clone()) {
|
|
seen_paths.insert(path);
|
|
matches.push(skill.clone());
|
|
}
|
|
}
|
|
|
|
matches
|
|
}
|
|
|
|
pub(crate) fn find_app_mentions(
|
|
mentions: &ToolMentions,
|
|
apps: &[AppInfo],
|
|
skill_names_lower: &HashSet<String>,
|
|
) -> Vec<AppInfo> {
|
|
let mut explicit_names = HashSet::new();
|
|
let mut selected_ids = HashSet::new();
|
|
for (name, path) in &mentions.linked_paths {
|
|
if let Some(connector_id) = app_id_from_path(path) {
|
|
explicit_names.insert(name.clone());
|
|
selected_ids.insert(connector_id.to_string());
|
|
}
|
|
}
|
|
|
|
let mut slug_counts: HashMap<String, usize> = HashMap::new();
|
|
for app in apps.iter().filter(|app| is_app_mentionable(app)) {
|
|
let slug = codex_connectors::metadata::connector_mention_slug(app);
|
|
*slug_counts.entry(slug).or_insert(0) += 1;
|
|
}
|
|
|
|
for app in apps.iter().filter(|app| is_app_mentionable(app)) {
|
|
let slug = codex_connectors::metadata::connector_mention_slug(app);
|
|
let slug_count = slug_counts.get(&slug).copied().unwrap_or(0);
|
|
if mentions.names.contains(&slug)
|
|
&& !explicit_names.contains(&slug)
|
|
&& slug_count == 1
|
|
&& !skill_names_lower.contains(&slug)
|
|
{
|
|
selected_ids.insert(app.id.clone());
|
|
}
|
|
}
|
|
|
|
apps.iter()
|
|
.filter(|app| is_app_mentionable(app) && selected_ids.contains(&app.id))
|
|
.cloned()
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn is_app_mentionable(app: &AppInfo) -> bool {
|
|
app.is_accessible && app.is_enabled
|
|
}
|
|
|
|
pub(crate) struct ToolMentions {
|
|
names: HashSet<String>,
|
|
linked_paths: HashMap<String, String>,
|
|
}
|
|
|
|
fn extract_tool_mentions_from_text(text: &str) -> ToolMentions {
|
|
extract_tool_mentions_from_text_with_sigil(text, TOOL_MENTION_SIGIL)
|
|
}
|
|
|
|
fn extract_tool_mentions_from_text_with_sigil(text: &str, sigil: char) -> ToolMentions {
|
|
let text_bytes = text.as_bytes();
|
|
let mut names: HashSet<String> = HashSet::new();
|
|
let mut linked_paths: HashMap<String, String> = HashMap::new();
|
|
|
|
let mut index = 0;
|
|
while index < text_bytes.len() {
|
|
let byte = text_bytes[index];
|
|
if byte == b'['
|
|
&& let Some((name, path, end_index)) = parse_linked_tool_mention(text, index, sigil)
|
|
{
|
|
if !is_common_env_var(name) {
|
|
if is_skill_path(path) {
|
|
names.insert(name.to_string());
|
|
}
|
|
linked_paths
|
|
.entry(name.to_string())
|
|
.or_insert(path.to_string());
|
|
}
|
|
index = end_index;
|
|
continue;
|
|
}
|
|
|
|
if byte != sigil as u8 {
|
|
index += 1;
|
|
continue;
|
|
}
|
|
|
|
let name_start = index + 1;
|
|
let Some(first_name_byte) = text_bytes.get(name_start) else {
|
|
index += 1;
|
|
continue;
|
|
};
|
|
if !is_mention_name_char(*first_name_byte) {
|
|
index += 1;
|
|
continue;
|
|
}
|
|
|
|
let mut name_end = name_start + 1;
|
|
while let Some(next_byte) = text_bytes.get(name_end)
|
|
&& is_mention_name_char(*next_byte)
|
|
{
|
|
name_end += 1;
|
|
}
|
|
|
|
let name = &text[name_start..name_end];
|
|
if !is_common_env_var(name) {
|
|
names.insert(name.to_string());
|
|
}
|
|
index = name_end;
|
|
}
|
|
|
|
ToolMentions {
|
|
names,
|
|
linked_paths,
|
|
}
|
|
}
|
|
|
|
fn is_skill_path(path: &str) -> bool {
|
|
!path.starts_with("app://") && !path.starts_with("mcp://") && !path.starts_with("plugin://")
|
|
}
|
|
|
|
fn normalize_skill_path(path: &str) -> &str {
|
|
path.strip_prefix("skill://").unwrap_or(path)
|
|
}
|
|
|
|
fn app_id_from_path(path: &str) -> Option<&str> {
|
|
path.strip_prefix("app://")
|
|
.filter(|value| !value.is_empty())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
fn app(id: &str, name: &str) -> AppInfo {
|
|
AppInfo {
|
|
id: id.to_string(),
|
|
name: name.to_string(),
|
|
description: None,
|
|
logo_url: None,
|
|
logo_url_dark: None,
|
|
icon_assets: None,
|
|
icon_dark_assets: None,
|
|
distribution_channel: None,
|
|
branding: None,
|
|
app_metadata: None,
|
|
labels: None,
|
|
install_url: None,
|
|
is_accessible: true,
|
|
is_enabled: true,
|
|
plugin_display_names: Vec::new(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn find_app_mentions_requires_accessible_enabled_apps_for_slugs() {
|
|
let apps = vec![
|
|
app("google_drive", "Google Drive"),
|
|
AppInfo {
|
|
is_accessible: false,
|
|
..app("arabica_uae", "% Arabica UAE")
|
|
},
|
|
AppInfo {
|
|
is_enabled: false,
|
|
..app("linear", "Linear")
|
|
},
|
|
];
|
|
let mentions = collect_tool_mentions("$google-drive $arabica-uae $linear", &HashMap::new());
|
|
|
|
assert_eq!(
|
|
find_app_mentions(&mentions, &apps, &HashSet::new()),
|
|
vec![apps[0].clone()]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn find_app_mentions_requires_accessible_enabled_apps_for_bound_paths() {
|
|
let apps = vec![
|
|
app("google_drive", "Google Drive"),
|
|
AppInfo {
|
|
is_accessible: false,
|
|
..app("arabica_uae", "% Arabica UAE")
|
|
},
|
|
AppInfo {
|
|
is_enabled: false,
|
|
..app("linear", "Linear")
|
|
},
|
|
];
|
|
let mention_paths = HashMap::from([
|
|
("google-drive".to_string(), "app://google_drive".to_string()),
|
|
("arabica-uae".to_string(), "app://arabica_uae".to_string()),
|
|
("linear".to_string(), "app://linear".to_string()),
|
|
]);
|
|
let mentions = collect_tool_mentions("$google-drive $arabica-uae $linear", &mention_paths);
|
|
|
|
assert_eq!(
|
|
find_app_mentions(&mentions, &apps, &HashSet::new()),
|
|
vec![apps[0].clone()]
|
|
);
|
|
}
|
|
}
|