Base: codex-main originale (prima del fork aKode)
This commit is contained in:
53
codex-rs/core-plugins/src/command_migration/plugin.rs
Normal file
53
codex-rs/core-plugins/src/command_migration/plugin.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use super::CommandDescriptionMode;
|
||||
use super::CommandMigrationProfile;
|
||||
use super::CommandSkillSizeLimit;
|
||||
use super::CommandSource;
|
||||
use super::RewriteProfile;
|
||||
use super::import_command_sources;
|
||||
use super::supported_command_sources;
|
||||
use super::unique_command_sources;
|
||||
use crate::manifest::load_plugin_command_paths;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_plugins::migrated_command_skills_root;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
const PLUGIN_COMMANDS_DIR: &str = "commands";
|
||||
const MAX_MIGRATED_COMMAND_SKILL_BYTES: usize = 4_000;
|
||||
|
||||
const PLUGIN_REWRITE_PROFILE: RewriteProfile = RewriteProfile::new("AGENTS.md", &[]);
|
||||
const PLUGIN_MIGRATION_PROFILE: CommandMigrationProfile = CommandMigrationProfile::new(
|
||||
PLUGIN_REWRITE_PROFILE,
|
||||
CommandDescriptionMode::RequireFrontmatter,
|
||||
);
|
||||
|
||||
pub(crate) fn migrate_plugin_commands(plugin_root: &Path) -> io::Result<()> {
|
||||
let absolute_plugin_root = AbsolutePathBuf::from_absolute_path(plugin_root)?;
|
||||
let target_skills = migrated_command_skills_root(&absolute_plugin_root);
|
||||
if target_skills.is_dir() {
|
||||
fs::remove_dir_all(&target_skills)?;
|
||||
} else if target_skills.exists() {
|
||||
fs::remove_file(&target_skills)?;
|
||||
}
|
||||
import_command_sources(
|
||||
plugin_command_sources(plugin_root)?,
|
||||
&target_skills,
|
||||
PLUGIN_MIGRATION_PROFILE,
|
||||
CommandSkillSizeLimit::MaxBytes(MAX_MIGRATED_COMMAND_SKILL_BYTES),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn plugin_command_sources(plugin_root: &Path) -> io::Result<Vec<CommandSource>> {
|
||||
let command_paths = load_plugin_command_paths(plugin_root)?
|
||||
.unwrap_or_else(|| vec![plugin_root.join(PLUGIN_COMMANDS_DIR)]);
|
||||
let mut sources = Vec::new();
|
||||
for command_path in command_paths {
|
||||
sources.extend(supported_command_sources(
|
||||
&command_path,
|
||||
CommandDescriptionMode::RequireFrontmatter,
|
||||
)?);
|
||||
}
|
||||
Ok(unique_command_sources(sources))
|
||||
}
|
||||
95
codex-rs/core-plugins/src/command_migration/render.rs
Normal file
95
codex-rs/core-plugins/src/command_migration/render.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use super::RewriteProfile;
|
||||
|
||||
pub(super) fn rewrite_terms(content: &str, profile: RewriteProfile) -> String {
|
||||
let mut rewritten =
|
||||
replace_case_insensitive_with_boundaries(content, profile.doc_file_name, "AGENTS.md");
|
||||
for from in profile.term_variants {
|
||||
rewritten = replace_case_insensitive_with_boundaries(&rewritten, from, "Codex");
|
||||
}
|
||||
for from in profile.case_sensitive_term_variants {
|
||||
rewritten = replace_with_boundaries(&rewritten, from, "Codex");
|
||||
}
|
||||
rewritten
|
||||
}
|
||||
|
||||
fn replace_with_boundaries(input: &str, needle: &str, replacement: &str) -> String {
|
||||
if needle.is_empty() {
|
||||
return input.to_string();
|
||||
}
|
||||
|
||||
replace_with_boundaries_impl(input, needle, replacement, input)
|
||||
}
|
||||
|
||||
fn replace_case_insensitive_with_boundaries(
|
||||
input: &str,
|
||||
needle: &str,
|
||||
replacement: &str,
|
||||
) -> String {
|
||||
let needle_lower = needle.to_ascii_lowercase();
|
||||
if needle_lower.is_empty() {
|
||||
return input.to_string();
|
||||
}
|
||||
let haystack_lower = input.to_ascii_lowercase();
|
||||
replace_with_boundaries_impl(input, &needle_lower, replacement, &haystack_lower)
|
||||
}
|
||||
|
||||
fn replace_with_boundaries_impl(
|
||||
input: &str,
|
||||
needle: &str,
|
||||
replacement: &str,
|
||||
searchable_input: &str,
|
||||
) -> String {
|
||||
let bytes = input.as_bytes();
|
||||
let mut output = String::with_capacity(input.len());
|
||||
let mut last_emitted = 0usize;
|
||||
let mut search_start = 0usize;
|
||||
|
||||
while let Some(relative_pos) = searchable_input[search_start..].find(needle) {
|
||||
let start = search_start + relative_pos;
|
||||
let end = start + needle.len();
|
||||
let boundary_before = start == 0 || !is_word_byte(bytes[start - 1]);
|
||||
let boundary_after = end == bytes.len() || !is_word_byte(bytes[end]);
|
||||
|
||||
if boundary_before && boundary_after {
|
||||
output.push_str(&input[last_emitted..start]);
|
||||
output.push_str(replacement);
|
||||
last_emitted = end;
|
||||
}
|
||||
search_start = start + 1;
|
||||
}
|
||||
|
||||
if last_emitted == 0 {
|
||||
return input.to_string();
|
||||
}
|
||||
output.push_str(&input[last_emitted..]);
|
||||
output
|
||||
}
|
||||
|
||||
pub(super) fn yaml_string(value: &str) -> String {
|
||||
format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\""))
|
||||
}
|
||||
|
||||
pub(super) fn slugify_name(value: &str) -> String {
|
||||
let mut slug = String::new();
|
||||
let mut last_was_dash = false;
|
||||
for ch in value.chars() {
|
||||
if ch.is_ascii_alphanumeric() {
|
||||
slug.push(ch.to_ascii_lowercase());
|
||||
last_was_dash = false;
|
||||
} else if !last_was_dash {
|
||||
slug.push('-');
|
||||
last_was_dash = true;
|
||||
}
|
||||
}
|
||||
|
||||
let slug = slug.trim_matches('-').to_string();
|
||||
if slug.is_empty() {
|
||||
"migrated".to_string()
|
||||
} else {
|
||||
slug
|
||||
}
|
||||
}
|
||||
|
||||
fn is_word_byte(byte: u8) -> bool {
|
||||
byte.is_ascii_alphanumeric() || byte == b'_'
|
||||
}
|
||||
Reference in New Issue
Block a user