Base: codex-main originale (prima del fork aKode)
This commit is contained in:
15
codex-rs/plugin/BUILD.bazel
Normal file
15
codex-rs/plugin/BUILD.bazel
Normal file
@@ -0,0 +1,15 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "plugin",
|
||||
compile_data = glob(
|
||||
include = ["**"],
|
||||
allow_empty = True,
|
||||
exclude = [
|
||||
"**/* *",
|
||||
"BUILD.bazel",
|
||||
"Cargo.toml",
|
||||
],
|
||||
),
|
||||
crate_name = "codex_plugin",
|
||||
)
|
||||
26
codex-rs/plugin/Cargo.toml
Normal file
26
codex-rs/plugin/Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
name = "codex-plugin"
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
name = "codex_plugin"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
codex-config = { workspace = true }
|
||||
codex-core-plugin-common = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
codex-utils-path-uri = { workspace = true }
|
||||
codex-utils-plugins = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
156
codex-rs/plugin/src/bundled_hooks.rs
Normal file
156
codex-rs/plugin/src/bundled_hooks.rs
Normal file
@@ -0,0 +1,156 @@
|
||||
//! Temporary cleanup-hook allowlist shared by local and executor plugin discovery.
|
||||
//! This narrowly authorizes known MCP cleanup calls; it does not verify plugin signatures.
|
||||
|
||||
use codex_config::HookHandlerConfig;
|
||||
use codex_protocol::protocol::HookEventName;
|
||||
|
||||
struct BundledHook {
|
||||
plugin_id: &'static str,
|
||||
events: &'static [HookEventName],
|
||||
target: BundledHookTarget,
|
||||
}
|
||||
|
||||
enum BundledHookTarget {
|
||||
McpServer {
|
||||
server: &'static str,
|
||||
tool: &'static str,
|
||||
},
|
||||
App {
|
||||
server: &'static str,
|
||||
connector_id: &'static str,
|
||||
tool: &'static str,
|
||||
},
|
||||
}
|
||||
|
||||
// Keep unsigned plugin exceptions together so they can be removed as signing lands.
|
||||
const ALLOWLISTED_BUNDLED_HOOKS: &[BundledHook] = &[
|
||||
BundledHook {
|
||||
plugin_id: "browser@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "node_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "chrome@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "node_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "chrome-dev@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "node_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "chrome-internal@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "node_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "computer-use@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "node_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "unified-computer-use@openai-bundled",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::McpServer {
|
||||
server: "cua_repl",
|
||||
tool: "turn_ended",
|
||||
},
|
||||
},
|
||||
BundledHook {
|
||||
plugin_id: "browser@openai-curated-remote",
|
||||
events: &[
|
||||
HookEventName::Stop,
|
||||
HookEventName::Interrupt,
|
||||
HookEventName::SubagentStop,
|
||||
],
|
||||
target: BundledHookTarget::App {
|
||||
server: "codex_apps",
|
||||
connector_id: "connector_openai_browser",
|
||||
tool: "browser.turn_ended",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
/// Matches a temporary unsigned cleanup exception. App targets additionally require the
|
||||
/// connector identity for this handler's server and tool from the caller's enabled tool
|
||||
/// catalog; callers without that catalog must pass `None`.
|
||||
pub fn is_allowlisted_bundled_cleanup_hook(
|
||||
plugin_id: &str,
|
||||
event: HookEventName,
|
||||
matcher: Option<&str>,
|
||||
handler: &HookHandlerConfig,
|
||||
app_connector_id: Option<&str>,
|
||||
) -> bool {
|
||||
let HookHandlerConfig::McpTool {
|
||||
server,
|
||||
tool,
|
||||
input,
|
||||
..
|
||||
} = handler
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
|
||||
matcher.is_none()
|
||||
&& ALLOWLISTED_BUNDLED_HOOKS.iter().any(|hook| {
|
||||
hook.plugin_id == plugin_id
|
||||
&& hook.events.contains(&event)
|
||||
&& match hook.target {
|
||||
BundledHookTarget::McpServer {
|
||||
server: expected_server,
|
||||
tool: expected_tool,
|
||||
} => server == expected_server && tool == expected_tool,
|
||||
BundledHookTarget::App {
|
||||
server: expected_server,
|
||||
connector_id,
|
||||
tool: expected_tool,
|
||||
} => {
|
||||
// Raw Apps tool names can collide; require the registered connector.
|
||||
server == expected_server
|
||||
&& tool == expected_tool
|
||||
&& input.is_empty()
|
||||
&& app_connector_id == Some(connector_id)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
97
codex-rs/plugin/src/lib.rs
Normal file
97
codex-rs/plugin/src/lib.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
//! Shared plugin package models, identifiers, and telemetry summaries.
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub use codex_utils_plugins::mention_syntax;
|
||||
|
||||
mod bundled_hooks;
|
||||
mod load_outcome;
|
||||
pub mod manifest;
|
||||
use codex_core_plugin_common::plugin_id;
|
||||
mod provider;
|
||||
|
||||
pub use bundled_hooks::is_allowlisted_bundled_cleanup_hook;
|
||||
use codex_config::HookEventsToml;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
pub use load_outcome::LoadedPlugin;
|
||||
pub use load_outcome::PluginLoadOutcome;
|
||||
pub use load_outcome::prompt_safe_plugin_description;
|
||||
pub use plugin_id::PluginId;
|
||||
pub use plugin_id::PluginIdError;
|
||||
pub use plugin_id::validate_plugin_segment;
|
||||
pub use provider::PluginResourceLocator;
|
||||
pub use provider::ResolvedPlugin;
|
||||
pub use provider::ResolvedPluginError;
|
||||
pub use provider::ResolvedPluginLocation;
|
||||
use serde_json::Map;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct AppConnectorId(pub String);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AppDeclaration {
|
||||
pub name: String,
|
||||
pub connector_id: AppConnectorId,
|
||||
pub category: Option<String>,
|
||||
}
|
||||
|
||||
pub fn app_connector_ids_from_declarations<'a>(
|
||||
app_declarations: impl IntoIterator<Item = &'a AppDeclaration>,
|
||||
) -> Vec<AppConnectorId> {
|
||||
let mut connector_ids = Vec::new();
|
||||
let mut seen_connector_ids = HashSet::new();
|
||||
for app in app_declarations {
|
||||
if seen_connector_ids.insert(&app.connector_id) {
|
||||
connector_ids.push(app.connector_id.clone());
|
||||
}
|
||||
}
|
||||
connector_ids
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct PluginCapabilitySummary {
|
||||
pub config_name: String,
|
||||
pub display_name: String,
|
||||
pub plugin_namespace: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub has_skills: bool,
|
||||
pub mcp_server_names: Vec<String>,
|
||||
pub app_connector_ids: Vec<AppConnectorId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PluginHookSource {
|
||||
pub plugin_id: PluginId,
|
||||
pub plugin_root: AbsolutePathBuf,
|
||||
pub plugin_data_root: AbsolutePathBuf,
|
||||
pub source_path: AbsolutePathBuf,
|
||||
pub source_relative_path: String,
|
||||
pub hooks: HookEventsToml,
|
||||
}
|
||||
|
||||
/// Inline plugin hooks discovered in an executor environment.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ExecutorPluginHookSource {
|
||||
pub plugin_id: PluginId,
|
||||
pub environment_id: String,
|
||||
/// An admitted MCP target can run outside the plugin's source environment.
|
||||
pub mcp_environment_id: Option<String>,
|
||||
/// Trusted MCP routing metadata for this cleanup target.
|
||||
pub mcp_metadata: Option<Map<String, Value>>,
|
||||
pub plugin_root: PathUri,
|
||||
pub manifest_path: PathUri,
|
||||
pub source_relative_path: String,
|
||||
pub hooks: HookEventsToml,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PluginTelemetryMetadata {
|
||||
/// Local plugin identifier used by Codex configuration and the plugin cache,
|
||||
/// when it has been resolved.
|
||||
pub plugin_id: Option<PluginId>,
|
||||
/// Optional backend identifier for remote plugins.
|
||||
pub remote_plugin_id: Option<String>,
|
||||
pub capability_summary: Option<PluginCapabilitySummary>,
|
||||
}
|
||||
277
codex-rs/plugin/src/load_outcome.rs
Normal file
277
codex-rs/plugin/src/load_outcome.rs
Normal file
@@ -0,0 +1,277 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use codex_utils_plugins::PluginIdentity;
|
||||
use codex_utils_plugins::PluginSkillRoot;
|
||||
use codex_utils_plugins::SkillDiscoveryMode;
|
||||
|
||||
use crate::AppConnectorId;
|
||||
use crate::AppDeclaration;
|
||||
use crate::PluginCapabilitySummary;
|
||||
use crate::PluginHookSource;
|
||||
use crate::app_connector_ids_from_declarations;
|
||||
|
||||
const MAX_CAPABILITY_SUMMARY_DESCRIPTION_LEN: usize = 1024;
|
||||
|
||||
/// A plugin that was loaded from disk, including merged MCP server definitions.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LoadedPlugin<M> {
|
||||
pub config_name: String,
|
||||
pub remote_plugin_id: Option<String>,
|
||||
pub manifest_name: Option<String>,
|
||||
pub plugin_namespace: Option<String>,
|
||||
pub manifest_description: Option<String>,
|
||||
pub root: AbsolutePathBuf,
|
||||
pub enabled: bool,
|
||||
pub skill_roots: Vec<AbsolutePathBuf>,
|
||||
pub skill_discovery_mode: SkillDiscoveryMode,
|
||||
pub disabled_skill_paths: HashSet<PathUri>,
|
||||
pub has_enabled_skills: bool,
|
||||
pub mcp_servers: HashMap<String, M>,
|
||||
pub apps: Vec<AppDeclaration>,
|
||||
pub hook_sources: Vec<PluginHookSource>,
|
||||
pub hook_load_warnings: Vec<String>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
impl<M> LoadedPlugin<M> {
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.enabled && self.error.is_none()
|
||||
}
|
||||
|
||||
pub fn display_name(&self) -> &str {
|
||||
self.manifest_name.as_deref().unwrap_or(&self.config_name)
|
||||
}
|
||||
|
||||
pub fn is_agent_plugin(&self) -> bool {
|
||||
self.skill_discovery_mode == SkillDiscoveryMode::DirectChildren
|
||||
}
|
||||
}
|
||||
|
||||
fn plugin_capability_summary_from_loaded<M>(
|
||||
plugin: &LoadedPlugin<M>,
|
||||
) -> Option<PluginCapabilitySummary> {
|
||||
if !plugin.is_active() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut mcp_server_names: Vec<String> = plugin.mcp_servers.keys().cloned().collect();
|
||||
mcp_server_names.sort_unstable();
|
||||
|
||||
let summary = PluginCapabilitySummary {
|
||||
config_name: plugin.config_name.clone(),
|
||||
display_name: plugin.display_name().to_string(),
|
||||
plugin_namespace: plugin.plugin_namespace.clone(),
|
||||
description: prompt_safe_plugin_description(plugin.manifest_description.as_deref()),
|
||||
has_skills: plugin.has_enabled_skills,
|
||||
mcp_server_names,
|
||||
app_connector_ids: app_connector_ids_from_declarations(&plugin.apps),
|
||||
};
|
||||
|
||||
(summary.has_skills
|
||||
|| !summary.mcp_server_names.is_empty()
|
||||
|| !summary.app_connector_ids.is_empty())
|
||||
.then_some(summary)
|
||||
}
|
||||
|
||||
/// Normalizes plugin descriptions for inclusion in model-facing capability summaries.
|
||||
pub fn prompt_safe_plugin_description(description: Option<&str>) -> Option<String> {
|
||||
let description = description?
|
||||
.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
if description.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(
|
||||
description
|
||||
.chars()
|
||||
.take(MAX_CAPABILITY_SUMMARY_DESCRIPTION_LEN)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Runtime view of loaded plugins and their derived capability summaries.
|
||||
///
|
||||
/// Runtime exclusions retain loaded metadata while removing derived capabilities.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PluginLoadOutcome<M> {
|
||||
plugins: Vec<LoadedPlugin<M>>,
|
||||
capability_summaries: Vec<PluginCapabilitySummary>,
|
||||
}
|
||||
|
||||
impl<M: Clone> Default for PluginLoadOutcome<M> {
|
||||
fn default() -> Self {
|
||||
Self::from_plugins(Vec::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: Clone> PluginLoadOutcome<M> {
|
||||
pub fn from_plugins(plugins: Vec<LoadedPlugin<M>>) -> Self {
|
||||
let capability_summaries = plugins
|
||||
.iter()
|
||||
.filter_map(plugin_capability_summary_from_loaded)
|
||||
.collect::<Vec<_>>();
|
||||
Self {
|
||||
plugins,
|
||||
capability_summaries,
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks matching canonical plugin IDs inactive while retaining their loaded metadata.
|
||||
pub fn without_plugins(mut self, disabled_plugin_ids: &[String]) -> Self {
|
||||
if disabled_plugin_ids.is_empty() {
|
||||
return self;
|
||||
}
|
||||
for plugin in &mut self.plugins {
|
||||
if disabled_plugin_ids.contains(&plugin.config_name) {
|
||||
plugin.enabled = false;
|
||||
}
|
||||
}
|
||||
Self::from_plugins(self.plugins)
|
||||
}
|
||||
|
||||
pub fn effective_plugin_skill_roots(&self) -> Vec<PluginSkillRoot> {
|
||||
let mut skill_roots = Vec::new();
|
||||
let mut seen_paths = HashSet::new();
|
||||
for plugin in self.plugins.iter().filter(|plugin| plugin.is_active()) {
|
||||
let Some(plugin_namespace) = &plugin.plugin_namespace else {
|
||||
continue;
|
||||
};
|
||||
for path in &plugin.skill_roots {
|
||||
if seen_paths.insert(path.clone()) {
|
||||
skill_roots.push(PluginSkillRoot {
|
||||
path: path.clone(),
|
||||
plugin_identity: PluginIdentity {
|
||||
plugin_id: plugin.config_name.clone(),
|
||||
remote_plugin_id: plugin.remote_plugin_id.clone(),
|
||||
},
|
||||
plugin_namespace: plugin_namespace.clone(),
|
||||
plugin_root: plugin.root.clone(),
|
||||
discovery_mode: plugin.skill_discovery_mode,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
skill_roots.sort_unstable_by(|a, b| a.path.cmp(&b.path));
|
||||
skill_roots
|
||||
}
|
||||
|
||||
pub fn effective_mcp_servers(&self) -> HashMap<String, M> {
|
||||
let mut mcp_servers = HashMap::new();
|
||||
for plugin in self.plugins.iter().filter(|plugin| plugin.is_active()) {
|
||||
for (name, config) in &plugin.mcp_servers {
|
||||
mcp_servers
|
||||
.entry(name.clone())
|
||||
.or_insert_with(|| config.clone());
|
||||
}
|
||||
}
|
||||
mcp_servers
|
||||
}
|
||||
|
||||
pub fn effective_apps(&self) -> Vec<AppConnectorId> {
|
||||
app_connector_ids_from_declarations(
|
||||
self.plugins
|
||||
.iter()
|
||||
.filter(|plugin| plugin.is_active())
|
||||
.flat_map(|plugin| plugin.apps.iter()),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn effective_plugin_hook_sources(&self) -> Vec<PluginHookSource> {
|
||||
self.iter_effective_plugin_hook_sources().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn iter_effective_plugin_hook_sources(&self) -> impl Iterator<Item = &PluginHookSource> {
|
||||
self.plugins
|
||||
.iter()
|
||||
.filter(|plugin| plugin.is_active())
|
||||
.flat_map(|plugin| plugin.hook_sources.iter())
|
||||
}
|
||||
|
||||
pub fn effective_plugin_hook_warnings(&self) -> Vec<String> {
|
||||
self.iter_effective_plugin_hook_warnings()
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn iter_effective_plugin_hook_warnings(&self) -> impl Iterator<Item = &String> {
|
||||
self.plugins
|
||||
.iter()
|
||||
.filter(|plugin| plugin.is_active())
|
||||
.flat_map(|plugin| plugin.hook_load_warnings.iter())
|
||||
}
|
||||
|
||||
pub fn capability_summaries(&self) -> &[PluginCapabilitySummary] {
|
||||
&self.capability_summaries
|
||||
}
|
||||
|
||||
pub fn plugins(&self) -> &[LoadedPlugin<M>] {
|
||||
&self.plugins
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn test_path(name: &str) -> AbsolutePathBuf {
|
||||
AbsolutePathBuf::from_absolute_path_checked(std::env::temp_dir().join(name))
|
||||
.expect("absolute temp path")
|
||||
}
|
||||
|
||||
fn loaded_plugin(config_name: &str, skill_roots: Vec<AbsolutePathBuf>) -> LoadedPlugin<()> {
|
||||
LoadedPlugin {
|
||||
config_name: config_name.to_string(),
|
||||
remote_plugin_id: None,
|
||||
manifest_name: None,
|
||||
plugin_namespace: Some(
|
||||
config_name
|
||||
.split_once('@')
|
||||
.map_or(config_name, |(name, _)| name)
|
||||
.to_string(),
|
||||
),
|
||||
manifest_description: None,
|
||||
root: test_path(config_name),
|
||||
enabled: true,
|
||||
skill_roots,
|
||||
skill_discovery_mode: SkillDiscoveryMode::Recursive,
|
||||
disabled_skill_paths: HashSet::new(),
|
||||
has_enabled_skills: true,
|
||||
mcp_servers: HashMap::new(),
|
||||
apps: Vec::new(),
|
||||
hook_sources: Vec::new(),
|
||||
hook_load_warnings: Vec::new(),
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_plugin_skill_roots_preserves_first_plugin_for_shared_root() {
|
||||
let shared_root = test_path("shared-skills");
|
||||
let mut first_plugin = loaded_plugin("zeta@test", vec![shared_root.clone()]);
|
||||
first_plugin.remote_plugin_id = Some("plugins~Plugin_zeta".to_string());
|
||||
let outcome = PluginLoadOutcome::from_plugins(vec![
|
||||
first_plugin,
|
||||
loaded_plugin("alpha@test", vec![shared_root.clone()]),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
outcome.effective_plugin_skill_roots(),
|
||||
vec![PluginSkillRoot {
|
||||
path: shared_root,
|
||||
plugin_identity: PluginIdentity {
|
||||
plugin_id: "zeta@test".to_string(),
|
||||
remote_plugin_id: Some("plugins~Plugin_zeta".to_string()),
|
||||
},
|
||||
plugin_namespace: "zeta".to_string(),
|
||||
plugin_root: test_path("zeta@test"),
|
||||
discovery_mode: SkillDiscoveryMode::Recursive,
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
194
codex-rs/plugin/src/manifest.rs
Normal file
194
codex-rs/plugin/src/manifest.rs
Normal file
@@ -0,0 +1,194 @@
|
||||
use codex_config::HooksFile;
|
||||
|
||||
/// Parsed plugin metadata parameterized by its resource locator representation.
|
||||
///
|
||||
/// Host loading uses absolute paths, while resolved packages replace them with
|
||||
/// authority-bound locators before exposing the manifest to consumers.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PluginManifest<Resource> {
|
||||
pub name: String,
|
||||
pub version: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub keywords: Vec<String>,
|
||||
pub paths: PluginManifestPaths<Resource>,
|
||||
pub interface: Option<PluginManifestInterface<Resource>>,
|
||||
}
|
||||
|
||||
/// Component resources declared by a plugin manifest.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PluginManifestPaths<Resource> {
|
||||
pub skills: Vec<Resource>,
|
||||
pub onboarding_skill: Option<Resource>,
|
||||
pub mcp_servers: Option<PluginManifestMcpServers<Resource>>,
|
||||
pub apps: Option<Resource>,
|
||||
pub hooks: Option<PluginManifestHooks<Resource>>,
|
||||
}
|
||||
|
||||
/// MCP server declarations embedded in or referenced by a plugin manifest.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PluginManifestMcpServers<Resource> {
|
||||
Path(Resource),
|
||||
Object(String),
|
||||
}
|
||||
|
||||
/// Hook declarations embedded in or referenced by a plugin manifest.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PluginManifestHooks<Resource> {
|
||||
Paths(Vec<Resource>),
|
||||
Inline(Vec<HooksFile>),
|
||||
}
|
||||
|
||||
/// Optional model- and UI-facing plugin metadata.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PluginManifestInterface<Resource> {
|
||||
pub display_name: Option<String>,
|
||||
pub short_description: Option<String>,
|
||||
pub long_description: Option<String>,
|
||||
pub developer_name: Option<String>,
|
||||
pub category: Option<String>,
|
||||
pub capabilities: Vec<String>,
|
||||
pub website_url: Option<String>,
|
||||
pub privacy_policy_url: Option<String>,
|
||||
pub terms_of_service_url: Option<String>,
|
||||
pub default_prompt: Option<Vec<String>>,
|
||||
pub brand_color: Option<String>,
|
||||
pub composer_icon: Option<Resource>,
|
||||
pub logo: Option<Resource>,
|
||||
pub logo_dark: Option<Resource>,
|
||||
pub screenshots: Vec<Resource>,
|
||||
}
|
||||
|
||||
impl<Resource> Default for PluginManifestInterface<Resource> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
display_name: None,
|
||||
short_description: None,
|
||||
long_description: None,
|
||||
developer_name: None,
|
||||
category: None,
|
||||
capabilities: Vec::new(),
|
||||
website_url: None,
|
||||
privacy_policy_url: None,
|
||||
terms_of_service_url: None,
|
||||
default_prompt: None,
|
||||
brand_color: None,
|
||||
composer_icon: None,
|
||||
logo: None,
|
||||
logo_dark: None,
|
||||
screenshots: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Resource> PluginManifest<Resource> {
|
||||
/// Returns the model- and UI-facing package name, falling back to the manifest name.
|
||||
pub fn display_name(&self) -> &str {
|
||||
self.interface
|
||||
.as_ref()
|
||||
.and_then(|interface| interface.display_name.as_deref())
|
||||
.map(str::trim)
|
||||
.filter(|display_name| !display_name.is_empty())
|
||||
.unwrap_or(&self.name)
|
||||
}
|
||||
|
||||
/// Maps every path-bearing resource in the manifest.
|
||||
pub fn try_map_resources<Mapped, Error>(
|
||||
self,
|
||||
mut map: impl FnMut(Resource) -> Result<Mapped, Error>,
|
||||
) -> Result<PluginManifest<Mapped>, Error> {
|
||||
let PluginManifest {
|
||||
name,
|
||||
version,
|
||||
description,
|
||||
keywords,
|
||||
paths,
|
||||
interface,
|
||||
} = self;
|
||||
let PluginManifestPaths {
|
||||
skills,
|
||||
onboarding_skill,
|
||||
mcp_servers,
|
||||
apps,
|
||||
hooks,
|
||||
} = paths;
|
||||
let hooks = match hooks {
|
||||
Some(PluginManifestHooks::Paths(paths)) => Some(PluginManifestHooks::Paths(
|
||||
paths
|
||||
.into_iter()
|
||||
.map(&mut map)
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
)),
|
||||
Some(PluginManifestHooks::Inline(hooks)) => Some(PluginManifestHooks::Inline(hooks)),
|
||||
None => None,
|
||||
};
|
||||
let mcp_servers = match mcp_servers {
|
||||
Some(PluginManifestMcpServers::Path(path)) => {
|
||||
Some(PluginManifestMcpServers::Path(map(path)?))
|
||||
}
|
||||
Some(PluginManifestMcpServers::Object(servers)) => {
|
||||
Some(PluginManifestMcpServers::Object(servers))
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let interface = match interface {
|
||||
Some(interface) => {
|
||||
let PluginManifestInterface {
|
||||
display_name,
|
||||
short_description,
|
||||
long_description,
|
||||
developer_name,
|
||||
category,
|
||||
capabilities,
|
||||
website_url,
|
||||
privacy_policy_url,
|
||||
terms_of_service_url,
|
||||
default_prompt,
|
||||
brand_color,
|
||||
composer_icon,
|
||||
logo,
|
||||
logo_dark,
|
||||
screenshots,
|
||||
} = interface;
|
||||
Some(PluginManifestInterface {
|
||||
display_name,
|
||||
short_description,
|
||||
long_description,
|
||||
developer_name,
|
||||
category,
|
||||
capabilities,
|
||||
website_url,
|
||||
privacy_policy_url,
|
||||
terms_of_service_url,
|
||||
default_prompt,
|
||||
brand_color,
|
||||
composer_icon: composer_icon.map(&mut map).transpose()?,
|
||||
logo: logo.map(&mut map).transpose()?,
|
||||
logo_dark: logo_dark.map(&mut map).transpose()?,
|
||||
screenshots: screenshots
|
||||
.into_iter()
|
||||
.map(&mut map)
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(PluginManifest {
|
||||
name,
|
||||
version,
|
||||
description,
|
||||
keywords,
|
||||
paths: PluginManifestPaths {
|
||||
skills: skills
|
||||
.into_iter()
|
||||
.map(&mut map)
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
onboarding_skill: onboarding_skill.map(&mut map).transpose()?,
|
||||
mcp_servers,
|
||||
apps: apps.map(&mut map).transpose()?,
|
||||
hooks,
|
||||
},
|
||||
interface,
|
||||
})
|
||||
}
|
||||
}
|
||||
108
codex-rs/plugin/src/provider.rs
Normal file
108
codex-rs/plugin/src/provider.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
//! Authority-bound plugin descriptors and resource locations.
|
||||
|
||||
use crate::manifest::PluginManifest;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use thiserror::Error;
|
||||
|
||||
/// A plugin resource paired with the environment that owns its filesystem.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum PluginResourceLocator {
|
||||
Environment {
|
||||
/// Environment whose filesystem owns the resource.
|
||||
environment_id: String,
|
||||
/// Resource URI within that filesystem.
|
||||
path: PathUri,
|
||||
},
|
||||
}
|
||||
|
||||
/// Authority-bound location of a resolved plugin package.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ResolvedPluginLocation {
|
||||
Environment {
|
||||
/// Environment whose filesystem owns the package.
|
||||
environment_id: String,
|
||||
/// Package root URI within that filesystem.
|
||||
root: PathUri,
|
||||
},
|
||||
}
|
||||
|
||||
/// An inert plugin descriptor whose resources retain their source authority.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ResolvedPlugin {
|
||||
selected_root_id: String,
|
||||
location: ResolvedPluginLocation,
|
||||
manifest_path: PluginResourceLocator,
|
||||
manifest: PluginManifest<PluginResourceLocator>,
|
||||
}
|
||||
|
||||
/// Failure to construct a resolved plugin with internally consistent resources.
|
||||
#[derive(Clone, Debug, Error, PartialEq, Eq)]
|
||||
pub enum ResolvedPluginError {
|
||||
#[error("plugin resource path `{path}` is outside package root `{root}`")]
|
||||
ResourceOutsideRoot { root: PathUri, path: PathUri },
|
||||
}
|
||||
|
||||
impl ResolvedPlugin {
|
||||
/// Creates an environment-owned descriptor from a validated plugin manifest.
|
||||
pub fn from_environment(
|
||||
selected_root_id: String,
|
||||
environment_id: String,
|
||||
root: PathUri,
|
||||
manifest_path: PathUri,
|
||||
manifest: PluginManifest<PathUri>,
|
||||
) -> Result<Self, ResolvedPluginError> {
|
||||
let manifest_path = environment_resource(&environment_id, &root, manifest_path)?;
|
||||
let manifest = manifest
|
||||
.try_map_resources(|path| environment_resource(&environment_id, &root, path))?;
|
||||
Ok(Self {
|
||||
selected_root_id,
|
||||
location: ResolvedPluginLocation::Environment {
|
||||
environment_id,
|
||||
root,
|
||||
},
|
||||
manifest_path,
|
||||
manifest,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the opaque ID supplied for the selected capability root.
|
||||
pub fn selected_root_id(&self) -> &str {
|
||||
&self.selected_root_id
|
||||
}
|
||||
|
||||
/// Returns the authority-bound package location.
|
||||
pub fn location(&self) -> &ResolvedPluginLocation {
|
||||
&self.location
|
||||
}
|
||||
|
||||
/// Returns the manifest resource used to resolve this package.
|
||||
pub fn manifest_path(&self) -> &PluginResourceLocator {
|
||||
&self.manifest_path
|
||||
}
|
||||
|
||||
/// Returns package metadata whose resource fields retain their source authority.
|
||||
pub fn manifest(&self) -> &PluginManifest<PluginResourceLocator> {
|
||||
&self.manifest
|
||||
}
|
||||
}
|
||||
|
||||
fn environment_resource(
|
||||
environment_id: &str,
|
||||
root: &PathUri,
|
||||
path: PathUri,
|
||||
) -> Result<PluginResourceLocator, ResolvedPluginError> {
|
||||
if !path.starts_with(root) {
|
||||
return Err(ResolvedPluginError::ResourceOutsideRoot {
|
||||
root: root.clone(),
|
||||
path,
|
||||
});
|
||||
}
|
||||
Ok(PluginResourceLocator::Environment {
|
||||
environment_id: environment_id.to_string(),
|
||||
path,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "provider_tests.rs"]
|
||||
mod tests;
|
||||
139
codex-rs/plugin/src/provider_tests.rs
Normal file
139
codex-rs/plugin/src/provider_tests.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
use super::PluginResourceLocator;
|
||||
use super::ResolvedPlugin;
|
||||
use super::ResolvedPluginError;
|
||||
use crate::manifest::PluginManifest;
|
||||
use crate::manifest::PluginManifestHooks;
|
||||
use crate::manifest::PluginManifestInterface;
|
||||
use crate::manifest::PluginManifestMcpServers;
|
||||
use crate::manifest::PluginManifestPaths;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn absolute(path: impl AsRef<std::path::Path>) -> AbsolutePathBuf {
|
||||
AbsolutePathBuf::from_absolute_path_checked(path.as_ref()).expect("absolute test path")
|
||||
}
|
||||
|
||||
fn path_uri(path: &AbsolutePathBuf) -> PathUri {
|
||||
PathUri::from_abs_path(path)
|
||||
}
|
||||
|
||||
fn resource(environment_id: &str, path: &AbsolutePathBuf) -> PluginResourceLocator {
|
||||
PluginResourceLocator::Environment {
|
||||
environment_id: environment_id.to_string(),
|
||||
path: path_uri(path),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_descriptor_binds_every_manifest_resource() {
|
||||
let root = absolute(std::env::current_dir().expect("cwd").join("plugin-root"));
|
||||
let root_uri = path_uri(&root);
|
||||
let manifest_path = root.join(".codex-plugin/plugin.json");
|
||||
let skills = root.join("skills");
|
||||
let mcp_servers = root.join(".mcp.json");
|
||||
let apps = root.join(".app.json");
|
||||
let hooks = root.join("hooks/hooks.json");
|
||||
let composer_icon = root.join("assets/composer.svg");
|
||||
let logo = root.join("assets/logo.svg");
|
||||
let screenshot = root.join("assets/screenshot.png");
|
||||
let manifest = PluginManifest {
|
||||
name: "demo".to_string(),
|
||||
version: None,
|
||||
description: None,
|
||||
keywords: Vec::new(),
|
||||
paths: PluginManifestPaths {
|
||||
onboarding_skill: None,
|
||||
skills: vec![path_uri(&skills)],
|
||||
mcp_servers: Some(PluginManifestMcpServers::Path(path_uri(&mcp_servers))),
|
||||
apps: Some(path_uri(&apps)),
|
||||
hooks: Some(PluginManifestHooks::Paths(vec![path_uri(&hooks)])),
|
||||
},
|
||||
interface: Some(PluginManifestInterface {
|
||||
composer_icon: Some(path_uri(&composer_icon)),
|
||||
logo: Some(path_uri(&logo)),
|
||||
screenshots: vec![path_uri(&screenshot)],
|
||||
..PluginManifestInterface::default()
|
||||
}),
|
||||
};
|
||||
|
||||
let plugin = ResolvedPlugin::from_environment(
|
||||
"selected-demo".to_string(),
|
||||
"executor-1".to_string(),
|
||||
root_uri,
|
||||
path_uri(&manifest_path),
|
||||
manifest,
|
||||
)
|
||||
.expect("valid descriptor");
|
||||
|
||||
assert_eq!(
|
||||
plugin.manifest_path(),
|
||||
&resource("executor-1", &manifest_path)
|
||||
);
|
||||
assert_eq!(
|
||||
plugin.manifest(),
|
||||
&PluginManifest {
|
||||
name: "demo".to_string(),
|
||||
version: None,
|
||||
description: None,
|
||||
keywords: Vec::new(),
|
||||
paths: PluginManifestPaths {
|
||||
onboarding_skill: None,
|
||||
skills: vec![resource("executor-1", &skills)],
|
||||
mcp_servers: Some(PluginManifestMcpServers::Path(resource(
|
||||
"executor-1",
|
||||
&mcp_servers,
|
||||
))),
|
||||
apps: Some(resource("executor-1", &apps)),
|
||||
hooks: Some(PluginManifestHooks::Paths(vec![resource(
|
||||
"executor-1",
|
||||
&hooks
|
||||
)])),
|
||||
},
|
||||
interface: Some(PluginManifestInterface {
|
||||
composer_icon: Some(resource("executor-1", &composer_icon)),
|
||||
logo: Some(resource("executor-1", &logo)),
|
||||
screenshots: vec![resource("executor-1", &screenshot)],
|
||||
..PluginManifestInterface::default()
|
||||
}),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_descriptor_rejects_resources_outside_package_root() {
|
||||
let cwd = std::env::current_dir().expect("cwd");
|
||||
let root = absolute(cwd.join("plugin-root"));
|
||||
let outside = absolute(cwd.join("outside/.mcp.json"));
|
||||
let manifest = PluginManifest {
|
||||
name: "demo".to_string(),
|
||||
version: None,
|
||||
description: None,
|
||||
keywords: Vec::new(),
|
||||
paths: PluginManifestPaths {
|
||||
onboarding_skill: None,
|
||||
skills: Vec::new(),
|
||||
mcp_servers: Some(PluginManifestMcpServers::Path(path_uri(&outside))),
|
||||
apps: None,
|
||||
hooks: None,
|
||||
},
|
||||
interface: None,
|
||||
};
|
||||
|
||||
let err = ResolvedPlugin::from_environment(
|
||||
"selected-demo".to_string(),
|
||||
"executor-1".to_string(),
|
||||
path_uri(&root),
|
||||
path_uri(&root.join(".codex-plugin/plugin.json")),
|
||||
manifest,
|
||||
)
|
||||
.expect_err("outside resource should fail");
|
||||
|
||||
assert_eq!(
|
||||
err,
|
||||
ResolvedPluginError::ResourceOutsideRoot {
|
||||
root: path_uri(&root),
|
||||
path: path_uri(&outside),
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user