Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 8 additions & 107 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use axum::http::HeaderMap;
use clap::{ArgGroup, Args, Parser, Subcommand, ValueEnum};
use nemo_relay::plugin::{PluginError, load_plugin_config_files};
use serde::Deserialize;
use serde_json::Value;

Expand Down Expand Up @@ -744,31 +744,12 @@ fn load_plugin_toml_config_from_paths<I>(paths: I) -> Result<Option<PluginTomlCo
where
I: IntoIterator<Item = PathBuf>,
{
let mut merged = toml::Value::Table(toml::map::Map::new());
let mut sources = Vec::new();
for path in paths {
if path.exists() {
let raw = std::fs::read_to_string(&path)?;
let parsed = raw
.parse::<toml::Table>()
.map(toml::Value::Table)
.map_err(|error| {
CliError::Config(format!(
"invalid plugin TOML in {}: {error}",
path.display()
))
})?;
validate_plugin_toml_component_kinds(&path, &parsed)?;
merge_plugin_toml(&mut merged, parsed);
sources.push(path);
}
}
if sources.is_empty() {
return Ok(None);
}
let value = serde_json::to_value(merged)
.map_err(|error| CliError::Config(format!("invalid plugin TOML shape: {error}")))?;
Ok(Some(PluginTomlConfig { value, sources }))
// Delegate read/parse/merge to the shared core primitive (file precedence unchanged).
let resolved = load_plugin_config_files(paths).map_err(|err| match err {
PluginError::InvalidConfig(message) => CliError::Config(message),
other => CliError::Config(other.to_string()),
})?;
Ok(resolved.map(|(value, sources)| PluginTomlConfig { value, sources }))
}

fn apply_plugin_toml_config(
Expand Down Expand Up @@ -859,86 +840,6 @@ fn merge_toml(left: &mut toml::Value, right: toml::Value) {
}
}

// Plugin TOML uses normal recursive TOML merging except for the top-level components array. Each
// component is keyed by `kind`, so project/user plugins.toml files can add distinct plugin kinds or
// override one plugin kind without restating every other component.
fn merge_plugin_toml(left: &mut toml::Value, right: toml::Value) {
match (left, right) {
(toml::Value::Table(left), toml::Value::Table(right)) => {
for (key, value) in right {
match (key.as_str(), left.get_mut(&key)) {
("components", Some(existing)) => merge_plugin_components(existing, value),
(_, Some(existing)) => merge_toml(existing, value),
_ => {
left.insert(key, value);
}
}
}
}
(left, right) => *left = right,
}
}

fn merge_plugin_components(left: &mut toml::Value, right: toml::Value) {
let toml::Value::Array(left_components) = left else {
*left = right;
return;
};
let toml::Value::Array(right_components) = right else {
*left = right;
return;
};

for component in right_components {
let Some(kind) = component_kind(&component).map(str::to_owned) else {
left_components.push(component);
continue;
};
if let Some(existing) = left_components
.iter_mut()
.find(|candidate| component_kind(candidate) == Some(kind.as_str()))
{
merge_toml(existing, component);
} else {
left_components.push(component);
}
}
}

fn component_kind(component: &toml::Value) -> Option<&str> {
component
.as_table()
.and_then(|table| table.get("kind"))
.and_then(toml::Value::as_str)
}

fn validate_plugin_toml_component_kinds(path: &Path, value: &toml::Value) -> Result<(), CliError> {
let Some(components) = value.get("components").and_then(toml::Value::as_array) else {
return Ok(());
};
let mut seen = HashSet::new();
let mut duplicates = Vec::new();
for component in components {
let Some(kind) = component_kind(component) else {
continue;
};
if !seen.insert(kind.to_string()) {
duplicates.push(kind.to_string());
}
}
duplicates.sort();
duplicates.dedup();
if duplicates.is_empty() {
Ok(())
} else {
Err(CliError::Config(format!(
"duplicate plugin component kind in {}: {}; declare each kind once per plugins.toml",
path.display(),
duplicates.join(", ")
)))
}
}

fn has_config_toml_plugin_config(value: &toml::Value) -> bool {
value
.get("plugins")
Expand Down
5 changes: 3 additions & 2 deletions crates/cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::extract::State;
use axum::http::HeaderMap;
use axum::routing::{get, post};
use axum::{Json, Router};
use nemo_relay::plugin::{PluginConfig, clear_plugin_configuration, initialize_plugins};
use nemo_relay::plugin::{PluginConfig, clear_plugin_configuration, initialize_plugins_exact};
use nemo_relay_adaptive::plugin_component::register_adaptive_component;
use reqwest::Client;
use serde_json::Value;
Expand Down Expand Up @@ -157,9 +157,10 @@ impl PluginActivation {
register_adaptive_component().map_err(|error| {
CliError::Config(format!("adaptive plugin registration failed: {error}"))
})?;
// Gateway already resolved its config; activate exactly (no re-discovery).
let plugin_config: PluginConfig = serde_json::from_value(config)
.map_err(|error| CliError::Config(format!("invalid plugin config: {error}")))?;
initialize_plugins(plugin_config)
initialize_plugins_exact(plugin_config)
.await
.map_err(|error| CliError::Config(format!("plugin activation failed: {error}")))?;
Ok(Self { active: true })
Expand Down
2 changes: 2 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ openinference = [
uuid = { workspace = true, features = ["v7", "serde"] }
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
toml = { version = "0.9" }
schemars = { version = "0.8", optional = true }
chrono = { version = "0.4", features = ["serde"] }
bitflags = { version = "2", features = ["serde"] }
Expand All @@ -85,6 +86,7 @@ futures = "0.3"
opentelemetry_sdk = { version = "0.31", default-features = false, features = ["trace", "testing"] }
serde_json = "1"
object_store = { version = "0.13", default-features = false, features = ["aws"] }
tempfile = "3"

[[test]]
name = "codec_integration"
Expand Down
Loading