Parte "sicurezza" della fase 3 di aKode (e39aae00), senza le funzioni di imeAI:
- via i provider integrati openai, amazon-bedrock, amazon-bedrock-runtime e gcp
(crate model-provider: moduli amazon_bedrock, gcp, chatgpt_base_url; login Bedrock;
onboarding Bedrock/GCP; richieste app-server di setup cloud);
- nessun URL predefinito verso api.openai.com / chatgpt.com: un provider senza
base_url non viene mai contattato; senza model_provider configurato exec e TUI
mostrano come scriverne uno in config.toml (NO_MODEL_PROVIDER_CONFIGURED_ERROR);
- via la skill openai-docs, la sample skill-installer (scaricava da github.com/openai/skills),
request_plugin_install, AppsInstructions, URL predefiniti di realtime e updater;
- test di sicurezza adattati (provider "mock" al posto di quello integrato), non cancellati.
Restano fuori da questo commit (categoria F/D/E, non portate): chiave per provider nel
portachiavi, modelli da /models, unione dei messaggi di sistema, crate web-tools e
SearXNG, header @@ in apply_patch, modello ausiliario, tool function per apply_patch.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
197 lines
8.2 KiB
Rust
197 lines
8.2 KiB
Rust
use codex_core::test_support::all_model_presets;
|
|
use codex_protocol::config_types::ReasoningSummary;
|
|
use codex_protocol::openai_models::ConfigShellToolType;
|
|
use codex_protocol::openai_models::ModelInfo;
|
|
use codex_protocol::openai_models::ModelMessages;
|
|
use codex_protocol::openai_models::ModelPreset;
|
|
use codex_protocol::openai_models::ModelVisibility;
|
|
use codex_protocol::openai_models::TruncationPolicyConfig;
|
|
use codex_protocol::openai_models::default_input_modalities;
|
|
use std::path::Path;
|
|
|
|
/// Convert a ModelPreset to ModelInfo for cache storage.
|
|
fn preset_to_info(preset: &ModelPreset, priority: i32) -> ModelInfo {
|
|
ModelInfo {
|
|
slug: preset.id.clone(),
|
|
display_name: preset.display_name.clone(),
|
|
description: Some(preset.description.clone()),
|
|
default_reasoning_level: Some(preset.default_reasoning_effort.clone()),
|
|
supported_reasoning_levels: preset.supported_reasoning_efforts.clone(),
|
|
shell_type: ConfigShellToolType::UnifiedExec,
|
|
visibility: if preset.show_in_picker {
|
|
ModelVisibility::List
|
|
} else {
|
|
ModelVisibility::Hide
|
|
},
|
|
supported_in_api: preset.supported_in_api,
|
|
priority,
|
|
additional_speed_tiers: preset.additional_speed_tiers.clone(),
|
|
service_tiers: preset.service_tiers.clone(),
|
|
default_service_tier: preset.default_service_tier.clone(),
|
|
upgrade: preset.upgrade.as_ref().map(Into::into),
|
|
model_messages: Some(ModelMessages {
|
|
apps: None,
|
|
plugins: None,
|
|
content_filter_guidance: None,
|
|
persistent_instructions: None,
|
|
tools: None,
|
|
instructions_template: Some("base instructions".to_string()),
|
|
approvals: None,
|
|
collaboration_modes: None,
|
|
auto_review: None,
|
|
permissions: None,
|
|
multi_agent: None,
|
|
token_budget: None,
|
|
confirmation_policies: None,
|
|
guardian_v2: None,
|
|
}),
|
|
include_skills_usage_instructions: false,
|
|
include_plugin_usage_instructions: false,
|
|
include_apps_usage_instructions: false,
|
|
supports_reasoning_summary_parameter: true,
|
|
default_reasoning_summary: ReasoningSummary::Auto,
|
|
support_verbosity: false,
|
|
default_verbosity: None,
|
|
availability_nux: preset.availability_nux.clone(),
|
|
apply_patch_tool_type: None,
|
|
web_search_tool_type: Default::default(),
|
|
web_search_provider: None,
|
|
truncation_policy: TruncationPolicyConfig::bytes(/*limit*/ 10_000),
|
|
supports_image_detail_original: false,
|
|
context_window: Some(272_000),
|
|
max_context_window: None,
|
|
auto_compact_token_limit: None,
|
|
comp_hash: None,
|
|
effective_context_window_percent: 95,
|
|
experimental_supported_tools: Vec::new(),
|
|
input_modalities: default_input_modalities(),
|
|
supports_search_tool: false,
|
|
supports_experimental_context: false,
|
|
use_responses_lite: false,
|
|
supports_reasoning_effort_updates: false,
|
|
guardian: None,
|
|
node_repl_auto_review_required: false,
|
|
node_repl_disabled: false,
|
|
auto_review_model_override: None,
|
|
model_specialty: None,
|
|
tool_mode: None,
|
|
multi_agent_version: preset.multi_agent_version,
|
|
multi_agent_reasoning_effort: None,
|
|
}
|
|
}
|
|
|
|
/// Write a models_cache.json file to the codex home directory.
|
|
/// This prevents ModelsManager from making network requests to refresh models.
|
|
/// The cache will be treated as fresh (within TTL) and used instead of fetching from the network.
|
|
/// Uses bundled-catalog-derived presets, converted to ModelInfo format.
|
|
pub async fn write_models_cache(codex_home: &Path) -> std::io::Result<()> {
|
|
// Get a stable bundled-catalog-derived preset list and filter for picker-visible entries.
|
|
let presets: Vec<&ModelPreset> = all_model_presets()
|
|
.iter()
|
|
.filter(|preset| preset.show_in_picker)
|
|
.collect();
|
|
// Convert presets to ModelInfo, assigning priorities (lower = earlier in list).
|
|
// Priority is used for sorting, so the first model gets the lowest priority.
|
|
let models: Vec<ModelInfo> = presets
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(idx, preset)| {
|
|
// Lower priority = earlier in list.
|
|
let priority = idx as i32;
|
|
preset_to_info(preset, priority)
|
|
})
|
|
.collect();
|
|
|
|
write_models_cache_with_models(codex_home, models).await
|
|
}
|
|
|
|
/// Write a models_cache.json file with specific models.
|
|
/// Useful when tests need specific models to be available.
|
|
pub async fn write_models_cache_with_models(
|
|
codex_home: &Path,
|
|
models: Vec<ModelInfo>,
|
|
) -> std::io::Result<()> {
|
|
let config = codex_core::config::ConfigBuilder::default()
|
|
.loader_overrides(codex_config::LoaderOverrides::without_managed_config_for_tests())
|
|
.codex_home(codex_home.to_path_buf())
|
|
.build()
|
|
.await?;
|
|
let auth = codex_login::CodexAuth::from_auth_storage(
|
|
codex_home,
|
|
config.cli_auth_credentials_store_mode,
|
|
config.auth_keyring_backend_kind(),
|
|
)
|
|
.await?;
|
|
let cache = codex_model_provider::test_support::models_cache_entry(
|
|
&config.model_provider,
|
|
auth.as_ref(),
|
|
models.clone(),
|
|
);
|
|
let cache_path = codex_home.join("models_cache.json");
|
|
std::fs::write(cache_path, serde_json::to_string_pretty(&cache)?)?;
|
|
// aKode: providers without `model_catalog_url` never read `models_cache.json` (they
|
|
// use an explicit catalog or the provider's `/models` listing). Keep the cached
|
|
// models authoritative, as they were upstream, by serving them as the explicit
|
|
// catalog — replacing the bundled one `MockResponsesConfig` installs by default.
|
|
if config.model_provider.model_catalog_url.is_none() {
|
|
let config_path = codex_home.join("config.toml");
|
|
let config_text = std::fs::read_to_string(&config_path).unwrap_or_default();
|
|
let harness_catalog = codex_home.join(HARNESS_CATALOG_FILE);
|
|
// Upstream merged cached models over the bundled catalog by slug.
|
|
let mut catalog = serde_json::from_str::<codex_protocol::openai_models::ModelsResponse>(
|
|
include_str!("../../../models-manager/models.json"),
|
|
)?
|
|
.models;
|
|
for model in models {
|
|
match catalog
|
|
.iter()
|
|
.position(|existing| existing.slug == model.slug)
|
|
{
|
|
Some(index) => catalog[index] = model,
|
|
None => catalog.push(model),
|
|
}
|
|
}
|
|
if config_text.contains(HARNESS_CATALOG_FILE) && harness_catalog.exists() {
|
|
std::fs::write(
|
|
harness_catalog,
|
|
serde_json::to_string_pretty(&serde_json::json!({ "models": catalog }))?,
|
|
)?;
|
|
} else if !config_text.contains("model_catalog_json") {
|
|
if !config_path.exists() {
|
|
std::fs::write(&config_path, "")?;
|
|
}
|
|
write_model_catalog_json(codex_home, catalog)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Catalog file `MockResponsesConfig::write` installs unless a test configures one.
|
|
pub(crate) const HARNESS_CATALOG_FILE: &str = "test-models.json";
|
|
|
|
/// Write `models` as a `model_catalog_json` catalog and point `config.toml` at it.
|
|
///
|
|
/// API-key sessions without a provider model catalog URL never read `models_cache.json`, so
|
|
/// tests that pair specific model metadata with API-key auth configure the catalog directly.
|
|
pub fn write_model_catalog_json(codex_home: &Path, models: Vec<ModelInfo>) -> std::io::Result<()> {
|
|
let catalog_path = codex_home.join("models.json");
|
|
std::fs::write(
|
|
&catalog_path,
|
|
serde_json::to_string_pretty(&serde_json::json!({ "models": models }))?,
|
|
)?;
|
|
let config_path = codex_home.join("config.toml");
|
|
// Replace the default catalog `MockResponsesConfig::write` may have installed.
|
|
let config = std::fs::read_to_string(&config_path)?
|
|
.lines()
|
|
.filter(|line| !line.trim_start().starts_with("model_catalog_json"))
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
std::fs::write(
|
|
&config_path,
|
|
format!(
|
|
"model_catalog_json = {}\n{config}",
|
|
serde_json::to_string(&catalog_path)?
|
|
),
|
|
)
|
|
}
|